summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-stdlib.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-08-07 14:54:55 -0700
committerTim Foley <tfoley@nvidia.com>2017-08-07 15:16:54 -0700
commit7b54f43fb1b123f451460edb0add218a0428fe95 (patch)
treee492a82b13334955c1c56f6e3f9d25e8165de82c /source/slang/slang-stdlib.cpp
parentca8eea98c89c632dd7b5a6a8b84d379d1e9e59cf (diff)
Remove uses of global variables
There were two main places where global variables were used in the Slang implementation: 1. The "standard library" code was generated as a string at run-time, and stored in a global variable so that it could be amortized across compiles. 2. The representation of types uses some globals (well, class `static` members) to store common types (e.g., `void`) and to deal with memory lifetime for things like canonicalized types. In each case the "simple" fix is to move the relevant state into the `Session` type that controlled their lifetime already (the `Session` destructor was already cleaning up these globals to avoid leaks). For the standard library stuff this really was easy, but for the types it required threading through the `Session` a bit carefully. One more case that I found: there was a function-`static` variable used to generate a unique ID for files output when dumping of intermediates is enabled (this is almost strictly a debugging option). Rather than make this counter per-session (which would lead to different sessions on different threads clobbering the same few files), I went ahead and used an atomic in this case. Note that the remaining case I had been worried about was any function-`static` counter that might be used in generating unique names. It turns out that right now the parser doesn't use such a counter (even in cases where it probably should), and the lowering pass already uses a counter local to the pass (again, whether or not this is a good idea). This change should be a major step toward allowing an application to use Slang in multiple threads, so long as each thread uses a distinct `SlangSession`. The case of using a single session across multiple threads is harder to support, and will require more careful implementation work.
Diffstat (limited to 'source/slang/slang-stdlib.cpp')
-rw-r--r--source/slang/slang-stdlib.cpp32
1 files changed, 5 insertions, 27 deletions
diff --git a/source/slang/slang-stdlib.cpp b/source/slang/slang-stdlib.cpp
index 2a0d2abb2..71e8a6621 100644
--- a/source/slang/slang-stdlib.cpp
+++ b/source/slang/slang-stdlib.cpp
@@ -1,6 +1,6 @@
// slang-stdlib.cpp
-#include "slang-stdlib.h"
+#include "compiler.h"
#include "syntax.h"
#define STRINGIZE(x) STRINGIZE2(x)
@@ -1042,9 +1042,7 @@ typedef Texture2D texture2D;
namespace Slang
{
- static String stdlibPath;
-
- String getStdlibPath()
+ String Session::getStdlibPath()
{
if(stdlibPath.Length() != 0)
return stdlibPath;
@@ -1068,12 +1066,6 @@ namespace Slang
return stdlibPath;
}
- // Cached code for the various libraries
- String coreLibraryCode;
- String slangLibraryCode;
- String hlslLibraryCode;
- String glslLibraryCode;
-
enum
{
SINT_MASK = 1 << 0,
@@ -1156,7 +1148,7 @@ namespace Slang
};
- String getCoreLibraryCode()
+ String Session::getCoreLibraryCode()
{
if (coreLibraryCode.Length() > 0)
return coreLibraryCode;
@@ -1974,7 +1966,7 @@ namespace Slang
return coreLibraryCode;
}
- String getHLSLLibraryCode()
+ String Session::getHLSLLibraryCode()
{
if (hlslLibraryCode.Length() > 0)
return hlslLibraryCode;
@@ -2073,7 +2065,7 @@ namespace Slang
// GLSL-specific library code
- String getGLSLLibraryCode()
+ String Session::getGLSLLibraryCode()
{
if(glslLibraryCode.Length() != 0)
return glslLibraryCode;
@@ -2283,18 +2275,4 @@ namespace Slang
glslLibraryCode = sb.ProduceString();
return glslLibraryCode;
}
-
-
-
- //
-
- void finalizeShaderLibrary()
- {
- stdlibPath = String();
-
- coreLibraryCode = String();
- hlslLibraryCode = String();
- glslLibraryCode = String();
- }
-
}