Commit 69f4b517 by John Kessenich

Add link validation infrastructure for multiple compilation units per stage. …

Add link validation infrastructure for multiple compilation units per stage. Includes a new, straightforward, C++ interface to the front end. git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@22927 e7fa87d3-cd2b-0410-9028-fcbf551c1848
parent 2f1eb37d
...@@ -161,18 +161,16 @@ bool ProcessArguments(int argc, char* argv[]) ...@@ -161,18 +161,16 @@ bool ProcessArguments(int argc, char* argv[])
return true; return true;
} }
// Thread entry point // Thread entry point, for non-linking asynchronous mode.
unsigned int unsigned int
#ifdef _WIN32 #ifdef _WIN32
__stdcall __stdcall
#endif #endif
CompileShaders(void*) CompileShaders(void*)
{ {
ShHandle compiler;
std::string shaderName; std::string shaderName;
while (Worklist.remove(shaderName)) { while (Worklist.remove(shaderName)) {
compiler = ShConstructCompiler(FindLanguage(shaderName), Options); ShHandle compiler = ShConstructCompiler(FindLanguage(shaderName), Options);
if (compiler == 0) if (compiler == 0)
return false; return false;
...@@ -189,6 +187,77 @@ CompileShaders(void*) ...@@ -189,6 +187,77 @@ CompileShaders(void*)
return 0; return 0;
} }
//
// For linking mode: Will independently parse each item in the worklist, but then put them
// in the same program and link them together.
//
// Uses the new C++ interface instead of the old handle-based interface.
//
void CompileAndLinkShaders()
{
// keep track of what to free
std::list<glslang::TShader*> shaders;
EShMessages messages = EShMsgDefault;
if (Options & EOptionRelaxedErrors)
messages = (EShMessages)(messages | EShMsgRelaxedErrors);
if (Options & EOptionIntermediate)
messages = (EShMessages)(messages | EShMsgAST);
TBuiltInResource resources;
GenerateResources(resources);
//
// Per-shader processing...
//
glslang::TProgram program;
std::string shaderName;
while (Worklist.remove(shaderName)) {
EShLanguage stage = FindLanguage(shaderName);
glslang::TShader* shader = new glslang::TShader(stage);
shaders.push_back(shader);
char** shaderStrings = ReadFileData(shaderName.c_str());
if (! shaderStrings) {
usage();
return;
}
shader->setStrings(shaderStrings, 1);
shader->parse(&resources, 100, false, messages);
program.addShader(shader);
if (! (Options & EOptionSuppressInfolog)) {
puts(shaderName.c_str());
puts(shader->getInfoLog());
puts(shader->getInfoDebugLog());
}
FreeFileData(shaderStrings);
}
//
// Program-level processing...
//
program.link(messages);
if (! (Options & EOptionSuppressInfolog)) {
puts(program.getInfoLog());
puts(program.getInfoDebugLog());
}
// free everything up
while (shaders.size() > 0) {
delete shaders.back();
shaders.pop_back();
}
// TODO: memory: for each compile, need a GetThreadPoolAllocator().pop();
}
int C_DECL main(int argc, char* argv[]) int C_DECL main(int argc, char* argv[])
{ {
bool compileFailed = false; bool compileFailed = false;
...@@ -205,6 +274,12 @@ int C_DECL main(int argc, char* argv[]) ...@@ -205,6 +274,12 @@ int C_DECL main(int argc, char* argv[])
return EFailUsage; return EFailUsage;
} }
//
// Two modes:
// 1) linking all arguments together, single-threaded
// 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety
//
// TODO: finish threading, allow external control over number of threads // TODO: finish threading, allow external control over number of threads
const int NumThreads = 1; const int NumThreads = 1;
if (NumThreads > 1) { if (NumThreads > 1) {
...@@ -218,9 +293,13 @@ int C_DECL main(int argc, char* argv[]) ...@@ -218,9 +293,13 @@ int C_DECL main(int argc, char* argv[])
} }
glslang::OS_WaitForAllThreads(threads, NumThreads); glslang::OS_WaitForAllThreads(threads, NumThreads);
} else { } else {
if (Options & EOptionsLinkProgram) {
CompileAndLinkShaders();
} else {
if (! CompileShaders(0)) if (! CompileShaders(0))
compileFailed = true; compileFailed = true;
} }
}
if (Delay) if (Delay)
glslang::OS_Sleep(1000000); glslang::OS_Sleep(1000000);
...@@ -271,9 +350,10 @@ EShLanguage FindLanguage(const std::string& name) ...@@ -271,9 +350,10 @@ EShLanguage FindLanguage(const std::string& name)
} }
// //
// Read a file's data into a string, and compile it using ShCompile // Read a file's data into a string, and compile it using the old interface ShCompile,
// for non-linkable results.
// //
bool CompileFile(const char *fileName, ShHandle compiler, int Options, const TBuiltInResource *resources) bool CompileFile(const char *fileName, ShHandle compiler, int Options, const TBuiltInResource* resources)
{ {
int ret; int ret;
char** shaderStrings = ReadFileData(fileName); char** shaderStrings = ReadFileData(fileName);
...@@ -296,6 +376,7 @@ bool CompileFile(const char *fileName, ShHandle compiler, int Options, const TBu ...@@ -296,6 +376,7 @@ bool CompileFile(const char *fileName, ShHandle compiler, int Options, const TBu
messages = (EShMessages)(messages | EShMsgRelaxedErrors); messages = (EShMessages)(messages | EShMsgRelaxedErrors);
if (Options & EOptionIntermediate) if (Options & EOptionIntermediate)
messages = (EShMessages)(messages | EShMsgAST); messages = (EShMessages)(messages | EShMsgAST);
for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) { for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
//ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, resources, Options, 100, false, messages); //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, resources, Options, 100, false, messages);
...@@ -315,7 +396,6 @@ bool CompileFile(const char *fileName, ShHandle compiler, int Options, const TBu ...@@ -315,7 +396,6 @@ bool CompileFile(const char *fileName, ShHandle compiler, int Options, const TBu
return ret ? true : false; return ret ? true : false;
} }
// //
// print usage to stdout // print usage to stdout
// //
...@@ -429,16 +509,12 @@ char** ReadFileData(const char *fileName) ...@@ -429,16 +509,12 @@ char** ReadFileData(const char *fileName)
return return_data; return return_data;
} }
void FreeFileData(char **data) void FreeFileData(char **data)
{ {
for(int i=0;i<NumShaderStrings;i++) for(int i=0;i<NumShaderStrings;i++)
free(data[i]); free(data[i]);
} }
void InfoLogMsg(const char* msg, const char* name, const int num) void InfoLogMsg(const char* msg, const char* name, const int num)
{ {
printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" : printf(num >= 0 ? "#### %s %s %d INFO LOG ####\n" :
......
WARNING: 0:1: '#version' : statement missing: use #version on first line of shader WARNING: #version: statement missing; use #version on first line of shader
0:? Sequence 0:? Sequence
0:4 Sequence 0:4 Sequence
0:4 move second child to first child (highp float) 0:4 move second child to first child (highp float)
......
WARNING: 0:1: '#version' : statement missing: use #version on first line of shader WARNING: #version: statement missing; use #version on first line of shader
ERROR: 0:1: 'main' : function cannot take any parameter(s) ERROR: 0:1: 'main' : function cannot take any parameter(s)
ERROR: 0:1: 'int' : main function cannot return a value ERROR: 0:1: 'int' : main function cannot return a value
ERROR: 2 compilation errors. No code generated. ERROR: 2 compilation errors. No code generated.
......
mains1.frag
0:3Function Definition: main( (void)
0:3 Function Parameters:
mains2.frag
0:3Function Definition: main( (void)
0:3 Function Parameters:
noMain1.geom
ERROR: #version: geometry shaders require non-es profile and version 150 or above
ERROR: 1 compilation errors. No code generated.
0:3Function Definition: foo( (void)
0:3 Function Parameters:
noMain2.geom
0:3Function Definition: bar( (void)
0:3 Function Parameters:
Linked geometry stage:
ERROR: Missing entry point: Each stage requires one "void main()" entry point
Linked fragment stage:
ERROR: Too many entry points: Each stage can have at most one "void main()" entry point.
noMain.vert
0:3Function Definition: foo( (void)
0:3 Function Parameters:
mains.frag
ERROR: 0:7: 'main' : function already has a body
ERROR: 1 compilation errors. No code generated.
ERROR: node is still EOpNull!
0:3 Function Definition: main( (void)
0:3 Function Parameters:
0:7 Function Definition: main( (void)
0:7 Function Parameters:
Linked vertex stage:
ERROR: Missing entry point: Each stage requires one "void main()" entry point
Linked fragment stage:
ERROR: Too many entry points: Each stage can have at most one "void main()" entry point.
WARNING: 0:1: '#version' : statement missing: use #version on first line of shader WARNING: #version: statement missing; use #version on first line of shader
0:? Sequence 0:? Sequence
0:5 Function Definition: main( (void) 0:5 Function Definition: main( (void)
0:5 Function Parameters: 0:5 Function Parameters:
......
ERROR: 0:1: '#version' : statement must appear first in ESSL shader; before comments or newlines ERROR: #version: statement must appear first in es-profile shader; before comments or newlines
ERROR: 1 compilation errors. No code generated. ERROR: 1 compilation errors. No code generated.
ERROR: node is still EOpNull! ERROR: node is still EOpNull!
......
ERROR: #version: versions before 150 do not allow a profile token ERROR: #version: versions before 150 do not allow a profile token
ERROR: 0:1: '#version' : incorrect
ERROR: 0:38: 'attribute' : not supported in this stage: fragment ERROR: 0:38: 'attribute' : not supported in this stage: fragment
ERROR: 0:40: 'sampler2DRect' : Reserved word. ERROR: 0:40: 'sampler2DRect' : Reserved word.
ERROR: 0:40: 'rectangle texture' : not supported for this version or the enabled extensions ERROR: 0:40: 'rectangle texture' : not supported for this version or the enabled extensions
......
#version 300 es
void main()
{
}
void main()
{
}
#version 110
void main()
{
}
#version 110
void main()
{
}
#version 300 es
void foo()
{
}
#version 110
void foo()
{
}
#version 150
void bar()
{
}
...@@ -2,10 +2,28 @@ ...@@ -2,10 +2,28 @@
TARGETDIR=localResults TARGETDIR=localResults
BASEDIR=baseResults BASEDIR=baseResults
EXE=./glslangValidator.exe
#
# isolated compilation tests
#
while read t; do while read t; do
echo Running $t... echo Running $t...
b=`basename $t` b=`basename $t`
./glslangValidator.exe -i $t > $TARGETDIR/$b.out $EXE -i $t > $TARGETDIR/$b.out
diff -b $BASEDIR/$b.out $TARGETDIR/$b.out diff -b $BASEDIR/$b.out $TARGETDIR/$b.out
done < testlist done < testlist
#
# grouped shaders for link tests
#
function runLinkTest {
echo Running $*...
$EXE -i -l $* > $TARGETDIR/$1.out
diff -b $BASEDIR/$1.out $TARGETDIR/$1.out
}
runLinkTest mains1.frag mains2.frag noMain1.geom noMain2.geom
runLinkTest noMain.vert mains.frag
...@@ -103,7 +103,7 @@ protected: ...@@ -103,7 +103,7 @@ protected:
}; };
// //
// Link operations are base on a list of compile results... // Link operations are based on a list of compile results...
// //
typedef glslang::TVector<TCompiler*> TCompilerList; typedef glslang::TVector<TCompiler*> TCompilerList;
typedef glslang::TVector<TShHandleBase*> THandleList; typedef glslang::TVector<TShHandleBase*> THandleList;
......
...@@ -1429,7 +1429,7 @@ void TBuiltIns::initialize(const TBuiltInResource &resources, int version, EProf ...@@ -1429,7 +1429,7 @@ void TBuiltIns::initialize(const TBuiltInResource &resources, int version, EProf
s.append("uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];"); s.append("uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];");
// //
// Textureg Environment and Generation, p. 152, p. 40-42. // Texture Environment and Generation, p. 152, p. 40-42.
// //
s.append("uniform vec4 gl_TextureEnvColor[gl_MaxTextureImageUnits];"); s.append("uniform vec4 gl_TextureEnvColor[gl_MaxTextureImageUnits];");
s.append("uniform vec4 gl_EyePlaneS[gl_MaxTextureCoords];"); s.append("uniform vec4 gl_EyePlaneS[gl_MaxTextureCoords];");
......
...@@ -910,6 +910,30 @@ void TIntermediate::addSymbolLinkageNode(TIntermAggregate*& linkage, const TVari ...@@ -910,6 +910,30 @@ void TIntermediate::addSymbolLinkageNode(TIntermAggregate*& linkage, const TVari
} }
// //
// Merge the information in 'unit' into 'this'
//
void TIntermediate::merge(TIntermediate& unit)
{
numMains += unit.numMains;
}
void TIntermediate::errorCheck(TInfoSink& infoSink)
{
if (numMains < 1)
error(infoSink, "Missing entry point: Each stage requires one \"void main()\" entry point");
if (numMains > 1)
error(infoSink, "Too many entry points: Each stage can have at most one \"void main()\" entry point.");
}
void TIntermediate::error(TInfoSink& infoSink, const char* message)
{
infoSink.info.prefix(EPrefixError);
infoSink.info << message << "\n";
++numErrors;
}
//
// This deletes the tree. // This deletes the tree.
// //
void TIntermediate::removeTree() void TIntermediate::removeTree()
......
...@@ -692,6 +692,7 @@ TIntermAggregate* TParseContext::handleFunctionPrototype(TSourceLoc loc, TFuncti ...@@ -692,6 +692,7 @@ TIntermAggregate* TParseContext::handleFunctionPrototype(TSourceLoc loc, TFuncti
error(loc, "function cannot take any parameter(s)", function.getName().c_str(), ""); error(loc, "function cannot take any parameter(s)", function.getName().c_str(), "");
if (function.getReturnType().getBasicType() != EbtVoid) if (function.getReturnType().getBasicType() != EbtVoid)
error(loc, "", function.getReturnType().getCompleteTypeString().c_str(), "main function cannot return a value"); error(loc, "", function.getReturnType().getCompleteTypeString().c_str(), "main function cannot return a value");
intermediate.addMainCount();
} }
// //
......
...@@ -164,6 +164,7 @@ public: ...@@ -164,6 +164,7 @@ public:
TScanContext* getScanContext() const { return scanContext; } TScanContext* getScanContext() const { return scanContext; }
void setPpContext(TPpContext* c) { ppContext = c; } void setPpContext(TPpContext* c) { ppContext = c; }
TPpContext* getPpContext() const { return ppContext; } TPpContext* getPpContext() const { return ppContext; }
void addError() { ++numErrors; }
int getNumErrors() const { return numErrors; } int getNumErrors() const { return numErrors; }
protected: protected:
......
...@@ -51,7 +51,8 @@ const char* StageName[EShLangCount] = { ...@@ -51,7 +51,8 @@ const char* StageName[EShLangCount] = {
"tessellation control", "tessellation control",
"tessellation evaluation", "tessellation evaluation",
"geometry", "geometry",
"fragment" "fragment",
"compute"
}; };
const char* ProfileName[EProfileCount] = { const char* ProfileName[EProfileCount] = {
......
...@@ -56,10 +56,16 @@ class TVariable; ...@@ -56,10 +56,16 @@ class TVariable;
// //
class TIntermediate { class TIntermediate {
public: public:
TIntermediate(int v, EProfile p) : treeRoot(0), profile(p), version(v) { } explicit TIntermediate(int v = 0, EProfile p = ENoProfile) : treeRoot(0), profile(p), version(v), numMains(0), numErrors(0) { }
void setVersion(int v) { version = v; }
int getVersion() const { return version; }
void setProfile(EProfile p) { profile = p; }
EProfile getProfile() const { return profile; }
void setTreeRoot(TIntermNode* r) { treeRoot = r; } void setTreeRoot(TIntermNode* r) { treeRoot = r; }
TIntermNode* getTreeRoot() const { return treeRoot; } TIntermNode* getTreeRoot() const { return treeRoot; }
void addMainCount() { ++numMains; }
int getNumErrors() const { return numErrors; }
TIntermSymbol* addSymbol(int Id, const TString&, const TType&, TSourceLoc); TIntermSymbol* addSymbol(int Id, const TString&, const TType&, TSourceLoc);
TIntermTyped* addConversion(TOperator, const TType&, TIntermTyped*); TIntermTyped* addConversion(TOperator, const TType&, TIntermTyped*);
...@@ -93,13 +99,21 @@ public: ...@@ -93,13 +99,21 @@ public:
void addSymbolLinkageNode(TIntermAggregate*& linkage, TSymbolTable&, const TString&); void addSymbolLinkageNode(TIntermAggregate*& linkage, TSymbolTable&, const TString&);
void addSymbolLinkageNode(TIntermAggregate*& linkage, const TVariable&); void addSymbolLinkageNode(TIntermAggregate*& linkage, const TVariable&);
void merge(TIntermediate&);
void errorCheck(TInfoSink& infoSink);
void outputTree(TInfoSink& infoSink); void outputTree(TInfoSink& infoSink);
void removeTree(); void removeTree();
protected: protected:
void error(TInfoSink& infoSink, const char*);
protected:
TIntermNode* treeRoot; TIntermNode* treeRoot;
EProfile profile; EProfile profile;
int version; int version;
int numMains;
int numErrors;
private: private:
void operator=(TIntermediate&); // prevent assignments void operator=(TIntermediate&); // prevent assignments
......
...@@ -94,8 +94,12 @@ typedef enum { ...@@ -94,8 +94,12 @@ typedef enum {
EShLangComputeMask = (1 << EShLangCompute), EShLangComputeMask = (1 << EShLangCompute),
} EShLanguageMask; } EShLanguageMask;
namespace glslang {
extern const char* StageName[EShLangCount]; extern const char* StageName[EShLangCount];
} // end namespace glslang
// //
// Types of output the linker will create. // Types of output the linker will create.
// //
...@@ -236,4 +240,76 @@ enum TDebugOptions { ...@@ -236,4 +240,76 @@ enum TDebugOptions {
} // end extern "C" } // end extern "C"
#endif #endif
////////////////////////////////////////////////////////////////////////////////////////////
//
// Deferred-Lowering C++ Interface
// -----------------------------------
//
// Below is a new alternate C++ interface that might potentially replace the above
// opaque handle-based interface.
//
// The below is further designed to handle multiple compilation units per stage, where
// the intermediate results, including the parse tree, are preserved until link time,
// rather than the above interface which is designed to have each compilation unit
// lowered at compile time. In above model, linking occurs on the lowered results,
// whereas in this model intra-stage linking can occur at the parse tree
// (treeRoot in TIntermediate) level, and then a full stage can be lowered.
//
#include <list>
class TCompiler;
class TInfoSink;
namespace glslang {
class TIntermediate;
class TProgram;
class TShader {
public:
explicit TShader(EShLanguage);
virtual ~TShader();
void setStrings(char** s, int n) { strings = s; numStrings = n; }
bool parse(const TBuiltInResource*, int defaultVersion, bool forwardCompatible, EShMessages);
const char* getInfoLog();
const char* getInfoDebugLog();
protected:
EShLanguage stage;
TCompiler* compiler;
TIntermediate* intermediate;
TInfoSink* infoSink;
char** strings;
int numStrings;
friend class TProgram;
private:
void operator=(TShader&);
};
class TProgram {
public:
TProgram();
virtual ~TProgram();
void addShader(TShader* shader) { stages[shader->stage].push_back(shader); }
bool link(EShMessages);
const char* getInfoLog();
const char* getInfoDebugLog();
protected:
bool linkStage(EShLanguage, EShMessages);
protected:
std::list<TShader*> stages[EShLangCount];
TIntermediate* intermediate[EShLangCount];
TInfoSink* infoSink;
private:
void operator=(TProgram&);
};
} // end namespace glslang
#endif // _COMPILER_INTERFACE_INCLUDED_ #endif // _COMPILER_INTERFACE_INCLUDED_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment