Skip to content

Commit

Permalink
require GL_EXT_frag_depth on ES when writing into depth
Browse files Browse the repository at this point in the history
  • Loading branch information
aras-p committed Feb 24, 2013
1 parent c5537e6 commit 3d02748
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
60 changes: 35 additions & 25 deletions hlslang/GLSLCodeGen/hlslLinker.cpp
Expand Up @@ -85,6 +85,7 @@ static const char* varOutString[EAttrSemCount] = {
"" ""
}; };



// String table that maps attribute semantics to built-in GLSL input varyings // String table that maps attribute semantics to built-in GLSL input varyings
static const char* varInString[EAttrSemCount] = { static const char* varInString[EAttrSemCount] = {
"", "",
Expand Down Expand Up @@ -144,8 +145,12 @@ static const char* resultString[EAttrSemCount] = {
"", "",
"gl_FragDepth", "gl_FragDepth",
"", "",
"",
"",
"",
}; };



static const char* kUserVaryingPrefix = "xlv_"; static const char* kUserVaryingPrefix = "xlv_";


static inline void AddToVaryings (std::stringstream& s, TPrecision prec, const std::string& type, const std::string& name) static inline void AddToVaryings (std::stringstream& s, TPrecision prec, const std::string& type, const std::string& name)
Expand All @@ -154,7 +159,9 @@ static inline void AddToVaryings (std::stringstream& s, TPrecision prec, const s
s << "varying " << getGLSLPrecisiontring(prec) << type << " " << name << ";\n"; s << "varying " << getGLSLPrecisiontring(prec) << type << " " << name << ";\n";
} }


HlslLinker::HlslLinker(TInfoSink& infoSink_) : infoSink(infoSink_) HlslLinker::HlslLinker(TInfoSink& infoSink_)
: infoSink(infoSink_)
, m_Target(ETargetVersionCount)
{ {
for ( int i = 0; i < EAttrSemCount; i++) for ( int i = 0; i < EAttrSemCount; i++)
{ {
Expand Down Expand Up @@ -302,14 +309,19 @@ bool HlslLinker::getArgumentData2( const std::string &name, const std::string &s


case EClassRes: case EClassRes:
outName = resultString[sem]; outName = resultString[sem];
if ( sem != EAttrSemDepth) if (sem == EAttrSemDepth)
{ {
pad = 4 - size; if (m_Target == ETargetGLSL_ES_100)
ctor = "vec4"; {
outName = "gl_FragDepthEXT";
m_Extensions.insert("GL_EXT_frag_depth");
}
ctor = "float";
} }
else else
{ {
ctor = "float"; pad = 4 - size;
ctor = "vec4";
} }
break; break;


Expand Down Expand Up @@ -514,11 +526,11 @@ static const char* GetEntryName (const char* entryFunc)


static const char* kShaderTypeNames[2] = { "Vertex", "Fragment" }; static const char* kShaderTypeNames[2] = { "Vertex", "Fragment" };


void HlslLinker::addRequiredExtensions(EAttribSemantic sem, ExtensionSet &extensions)
static void add_extension_from_semantic(EAttribSemantic sem, ExtensionSet& extensions)
{ {
if (sem == EAttrSemPrimitiveID || sem == EAttrSemVertexID) if (sem == EAttrSemPrimitiveID || sem == EAttrSemVertexID)
extensions.insert("GL_EXT_gpu_shader4"); extensions.insert("GL_EXT_gpu_shader4");

if (sem == EAttrSemInstanceID) if (sem == EAttrSemInstanceID)
extensions.insert("GL_ARB_draw_instanced"); extensions.insert("GL_ARB_draw_instanced");
} }
Expand Down Expand Up @@ -604,15 +616,15 @@ void HlslLinker::buildUniformsAndLibFunctions(const FunctionSet& calledFunctions
} }




void HlslLinker::emitLibraryFunctions(const std::set<TOperator>& libFunctions, ExtensionSet& extensions, EShLanguage lang, bool usePrecision) void HlslLinker::emitLibraryFunctions(const std::set<TOperator>& libFunctions, EShLanguage lang, bool usePrecision)
{ {
// library Functions & required extensions // library Functions & required extensions
std::string shaderLibFunctions; std::string shaderLibFunctions;
if (!libFunctions.empty()) if (!libFunctions.empty())
{ {
for (std::set<TOperator>::const_iterator it = libFunctions.begin(); it != libFunctions.end(); it++) for (std::set<TOperator>::const_iterator it = libFunctions.begin(); it != libFunctions.end(); it++)
{ {
const std::string &func = getHLSLSupportCode(*it, extensions, lang==EShLangVertex, usePrecision); const std::string &func = getHLSLSupportCode(*it, m_Extensions, lang==EShLangVertex, usePrecision);
if (!func.empty()) if (!func.empty())
{ {
shaderLibFunctions += func; shaderLibFunctions += func;
Expand Down Expand Up @@ -643,12 +655,12 @@ void HlslLinker::emitStructs(HlslCrossCompiler* comp)
} }




void HlslLinker::emitGlobals(const GlslFunction* globalFunction, const std::vector<GlslSymbol*>& constants, ExtensionSet& extensions, ETargetVersion version) void HlslLinker::emitGlobals(const GlslFunction* globalFunction, const std::vector<GlslSymbol*>& constants)
{ {
// write global scope declarations (represented as a fake function) // write global scope declarations (represented as a fake function)
assert(globalFunction); assert(globalFunction);
shader << globalFunction->getCode(); shader << globalFunction->getCode();
globalFunction->addNeededExtensions (extensions, version); globalFunction->addNeededExtensions (m_Extensions, m_Target);


// write mutable uniform declarations // write mutable uniform declarations
const unsigned n_constants = constants.size(); const unsigned n_constants = constants.size();
Expand Down Expand Up @@ -764,7 +776,7 @@ void HlslLinker::emitInputNonStructParam(GlslSymbol* sym, EShLanguage lang, bool
} }




void HlslLinker::emitInputStructParam(GlslSymbol* sym, EShLanguage lang, ExtensionSet& extensions, std::stringstream& attrib, std::stringstream& varying, std::stringstream& preamble, std::stringstream& call) void HlslLinker::emitInputStructParam(GlslSymbol* sym, EShLanguage lang, std::stringstream& attrib, std::stringstream& varying, std::stringstream& preamble, std::stringstream& call)
{ {
GlslStruct* str = sym->getStruct(); GlslStruct* str = sym->getStruct();
assert(str); assert(str);
Expand All @@ -782,7 +794,7 @@ void HlslLinker::emitInputStructParam(GlslSymbol* sym, EShLanguage lang, Extensi
const GlslStruct::StructMember &current = str->getMember(jj); const GlslStruct::StructMember &current = str->getMember(jj);
EAttribSemantic memberSem = parseAttributeSemantic (current.semantic); EAttribSemantic memberSem = parseAttributeSemantic (current.semantic);


addRequiredExtensions(memberSem, extensions); add_extension_from_semantic(memberSem, m_Extensions);


// if member of the struct is an array, we have to loop over all array elements // if member of the struct is an array, we have to loop over all array elements
int arraySize = 1; int arraySize = 1;
Expand Down Expand Up @@ -912,14 +924,14 @@ void HlslLinker::emitOutputStructParam(GlslSymbol* sym, EShLanguage lang, bool u
} }




void HlslLinker::emitMainStart(const HlslCrossCompiler* compiler, const EGlslSymbolType retType, GlslFunction* funcMain, ETargetVersion version, unsigned options, bool usePrecision, std::stringstream& preamble) void HlslLinker::emitMainStart(const HlslCrossCompiler* compiler, const EGlslSymbolType retType, GlslFunction* funcMain, unsigned options, bool usePrecision, std::stringstream& preamble)
{ {
preamble << "void main() {\n"; preamble << "void main() {\n";


std::string arrayInit = compiler->m_DeferredArrayInit.str(); std::string arrayInit = compiler->m_DeferredArrayInit.str();
if (!arrayInit.empty()) if (!arrayInit.empty())
{ {
const bool emit_120_arrays = (version >= ETargetGLSL_120); const bool emit_120_arrays = (m_Target >= ETargetGLSL_120);
const bool emit_old_arrays = !emit_120_arrays || (options & ETranslateOpEmitGLSL120ArrayInitWorkaround); const bool emit_old_arrays = !emit_120_arrays || (options & ETranslateOpEmitGLSL120ArrayInitWorkaround);
const bool emit_both = emit_120_arrays && emit_old_arrays; const bool emit_both = emit_120_arrays && emit_old_arrays;


Expand Down Expand Up @@ -1043,6 +1055,8 @@ bool HlslLinker::emitReturnValue(const EGlslSymbolType retType, GlslFunction* fu


bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETargetVersion targetVersion, unsigned options) bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETargetVersion targetVersion, unsigned options)
{ {
m_Target = targetVersion;
m_Extensions.clear();
if (!linkerSanityCheck(compiler, entryFunc)) if (!linkerSanityCheck(compiler, entryFunc))
return false; return false;


Expand All @@ -1062,10 +1076,6 @@ bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETarge
assert(globalFunction); assert(globalFunction);
assert(funcMain); assert(funcMain);



ExtensionSet extensions;


// uniforms and used built-in functions // uniforms and used built-in functions
std::vector<GlslSymbol*> constants; std::vector<GlslSymbol*> constants;
std::set<TOperator> libFunctions; std::set<TOperator> libFunctions;
Expand All @@ -1075,9 +1085,9 @@ bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETarge


// print all the components collected above. // print all the components collected above.


emitLibraryFunctions (libFunctions, extensions, lang, usePrecision); emitLibraryFunctions (libFunctions, lang, usePrecision);
emitStructs(compiler); emitStructs(compiler);
emitGlobals (globalFunction, constants, extensions, targetVersion); emitGlobals (globalFunction, constants);
EmitCalledFunctions (shader, calledFunctions); EmitCalledFunctions (shader, calledFunctions);




Expand All @@ -1094,7 +1104,7 @@ bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETarge


// Declare return value // Declare return value
const EGlslSymbolType retType = funcMain->getReturnType(); const EGlslSymbolType retType = funcMain->getReturnType();
emitMainStart(compiler, retType, funcMain, targetVersion, options, usePrecision, preamble); emitMainStart(compiler, retType, funcMain, options, usePrecision, preamble);




// Call the entry point // Call the entry point
Expand All @@ -1111,7 +1121,7 @@ bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETarge
GlslSymbol *sym = funcMain->getParameter(ii); GlslSymbol *sym = funcMain->getParameter(ii);
EAttribSemantic attrSem = parseAttributeSemantic( sym->getSemantic()); EAttribSemantic attrSem = parseAttributeSemantic( sym->getSemantic());


addRequiredExtensions(attrSem, extensions); add_extension_from_semantic(attrSem, m_Extensions);


switch (sym->getQualifier()) switch (sym->getQualifier())
{ {
Expand All @@ -1126,7 +1136,7 @@ bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETarge
} }
else else
{ {
emitInputStructParam(sym, lang, extensions, attrib, varying, preamble, call); emitInputStructParam(sym, lang, attrib, varying, preamble, call);
} }


// NOTE: for "inout" parameters need to fallthrough to the next case // NOTE: for "inout" parameters need to fallthrough to the next case
Expand Down Expand Up @@ -1179,7 +1189,7 @@ bool HlslLinker::link(HlslCrossCompiler* compiler, const char* entryFunc, ETarge
// Generate final code of the pieces above. // Generate final code of the pieces above.
{ {
shaderPrefix << kTargetVersionStrings[targetVersion]; shaderPrefix << kTargetVersionStrings[targetVersion];
ExtensionSet::const_iterator it = extensions.begin(), end = extensions.end(); ExtensionSet::const_iterator it = m_Extensions.begin(), end = m_Extensions.end();
for (; it != end; ++it) for (; it != end; ++it)
shaderPrefix << "#extension " << *it << " : require" << std::endl; shaderPrefix << "#extension " << *it << " : require" << std::endl;
} }
Expand Down
12 changes: 7 additions & 5 deletions hlslang/GLSLCodeGen/hlslLinker.h
Expand Up @@ -61,22 +61,21 @@ class HlslLinker
EClassifier c, std::string &outName, std::string &ctor, int &pad, int semanticOffset); EClassifier c, std::string &outName, std::string &ctor, int &pad, int semanticOffset);
bool getArgumentData( GlslSymbol* sym, EClassifier c, std::string &outName, bool getArgumentData( GlslSymbol* sym, EClassifier c, std::string &outName,
std::string &ctor, int &pad); std::string &ctor, int &pad);
void addRequiredExtensions(EAttribSemantic sem, ExtensionSet& extensions);


bool linkerSanityCheck(HlslCrossCompiler* compiler, const char* entryFunc); bool linkerSanityCheck(HlslCrossCompiler* compiler, const char* entryFunc);
bool buildFunctionLists(HlslCrossCompiler* comp, EShLanguage lang, const std::string& entryPoint, GlslFunction*& globalFunction, std::vector<GlslFunction*>& functionList, FunctionSet& calledFunctions, GlslFunction*& funcMain); bool buildFunctionLists(HlslCrossCompiler* comp, EShLanguage lang, const std::string& entryPoint, GlslFunction*& globalFunction, std::vector<GlslFunction*>& functionList, FunctionSet& calledFunctions, GlslFunction*& funcMain);
void buildUniformsAndLibFunctions(const FunctionSet& calledFunctions, std::vector<GlslSymbol*>& constants, std::set<TOperator>& libFunctions); void buildUniformsAndLibFunctions(const FunctionSet& calledFunctions, std::vector<GlslSymbol*>& constants, std::set<TOperator>& libFunctions);
void buildUniformReflection(const std::vector<GlslSymbol*>& constants); void buildUniformReflection(const std::vector<GlslSymbol*>& constants);


void emitLibraryFunctions(const std::set<TOperator>& libFunctions, ExtensionSet& extensions, EShLanguage lang, bool usePrecision); void emitLibraryFunctions(const std::set<TOperator>& libFunctions, EShLanguage lang, bool usePrecision);
void emitStructs(HlslCrossCompiler* comp); void emitStructs(HlslCrossCompiler* comp);
void emitGlobals(const GlslFunction* globalFunction, const std::vector<GlslSymbol*>& constants, ExtensionSet& extensions, ETargetVersion version); void emitGlobals(const GlslFunction* globalFunction, const std::vector<GlslSymbol*>& constants);


void emitInputNonStructParam(GlslSymbol* sym, EShLanguage lang, bool usePrecision, EAttribSemantic attrSem, std::stringstream& attrib, std::stringstream& varying, std::stringstream& preamble, std::stringstream& call); void emitInputNonStructParam(GlslSymbol* sym, EShLanguage lang, bool usePrecision, EAttribSemantic attrSem, std::stringstream& attrib, std::stringstream& varying, std::stringstream& preamble, std::stringstream& call);
void emitInputStructParam(GlslSymbol* sym, EShLanguage lang, ExtensionSet& extensions, std::stringstream& attrib, std::stringstream& varying, std::stringstream& preamble, std::stringstream& call); void emitInputStructParam(GlslSymbol* sym, EShLanguage lang, std::stringstream& attrib, std::stringstream& varying, std::stringstream& preamble, std::stringstream& call);
void emitOutputNonStructParam(GlslSymbol* sym, EShLanguage lang, bool usePrecision, EAttribSemantic attrSem, std::stringstream& varying, std::stringstream& preamble, std::stringstream& postamble, std::stringstream& call); void emitOutputNonStructParam(GlslSymbol* sym, EShLanguage lang, bool usePrecision, EAttribSemantic attrSem, std::stringstream& varying, std::stringstream& preamble, std::stringstream& postamble, std::stringstream& call);
void emitOutputStructParam(GlslSymbol* sym, EShLanguage lang, bool usePrecision, EAttribSemantic attrSem, std::stringstream& varying, std::stringstream& preamble, std::stringstream& postamble, std::stringstream& call); void emitOutputStructParam(GlslSymbol* sym, EShLanguage lang, bool usePrecision, EAttribSemantic attrSem, std::stringstream& varying, std::stringstream& preamble, std::stringstream& postamble, std::stringstream& call);
void emitMainStart(const HlslCrossCompiler* compiler, const EGlslSymbolType retType, GlslFunction* funcMain, ETargetVersion version, unsigned options, bool usePrecision, std::stringstream& preamble); void emitMainStart(const HlslCrossCompiler* compiler, const EGlslSymbolType retType, GlslFunction* funcMain, unsigned options, bool usePrecision, std::stringstream& preamble);
bool emitReturnValue(const EGlslSymbolType retType, GlslFunction* funcMain, EShLanguage lang, std::stringstream& varying, std::stringstream& postamble); bool emitReturnValue(const EGlslSymbolType retType, GlslFunction* funcMain, EShLanguage lang, std::stringstream& varying, std::stringstream& postamble);


private: private:
Expand All @@ -100,6 +99,9 @@ class HlslLinker


// For varyings, determines whether the linker attempts to use user or built-in varyings // For varyings, determines whether the linker attempts to use user or built-in varyings
bool bUserVaryings; bool bUserVaryings;

ExtensionSet m_Extensions;
ETargetVersion m_Target;
}; };


#endif //HLSL_LINKER_H #endif //HLSL_LINKER_H
3 changes: 2 additions & 1 deletion tests/fragment/fragdepth-outES.txt
@@ -1,3 +1,4 @@
#extension GL_EXT_frag_depth : require
void xlat_main( out lowp vec4 ocol, out mediump float oz ); void xlat_main( out lowp vec4 ocol, out mediump float oz );
#line 1 #line 1
void xlat_main( out lowp vec4 ocol, out mediump float oz ) { void xlat_main( out lowp vec4 ocol, out mediump float oz ) {
Expand All @@ -10,6 +11,6 @@ void main() {
mediump float xlt_oz; mediump float xlt_oz;
xlat_main( xlt_ocol, xlt_oz); xlat_main( xlt_ocol, xlt_oz);
gl_FragData[0] = vec4(xlt_ocol); gl_FragData[0] = vec4(xlt_ocol);
gl_FragDepth = float(xlt_oz); gl_FragDepthEXT = float(xlt_oz);
; ;
} }
2 changes: 2 additions & 0 deletions tests/hlsl2glsltest/hlsl2glsltest.cpp
Expand Up @@ -247,12 +247,14 @@ static bool CheckGLSL (bool vertex, ETargetVersion version, const std::string& s
newSrc += "#define texture2DLodEXT texture2DLod\n"; newSrc += "#define texture2DLodEXT texture2DLod\n";
newSrc += "#define texture2DProjLodEXT texture2DProjLod\n"; newSrc += "#define texture2DProjLodEXT texture2DProjLod\n";
newSrc += "#define texture2DGradEXT texture2DGradARB\n"; newSrc += "#define texture2DGradEXT texture2DGradARB\n";
newSrc += "#define gl_FragDepthEXT gl_FragDepth\n";
newSrc += "float shadow2DEXT (sampler2DShadow s, vec3 p) { return shadow2D(s,p).r; }\n"; newSrc += "float shadow2DEXT (sampler2DShadow s, vec3 p) { return shadow2D(s,p).r; }\n";
newSrc += "float shadow2DProjEXT (sampler2DShadow s, vec4 p) { return shadow2DProj(s,p).r; }\n"; newSrc += "float shadow2DProjEXT (sampler2DShadow s, vec4 p) { return shadow2DProj(s,p).r; }\n";
newSrc += source; newSrc += source;
replace_string (newSrc, "GL_EXT_shader_texture_lod", "GL_ARB_shader_texture_lod", 0); replace_string (newSrc, "GL_EXT_shader_texture_lod", "GL_ARB_shader_texture_lod", 0);
replace_string (newSrc, "#extension GL_OES_standard_derivatives : require", "", 0); replace_string (newSrc, "#extension GL_OES_standard_derivatives : require", "", 0);
replace_string (newSrc, "#extension GL_EXT_shadow_samplers : require", "", 0); replace_string (newSrc, "#extension GL_EXT_shadow_samplers : require", "", 0);
replace_string (newSrc, "#extension GL_EXT_frag_depth : require", "", 0);


sourcePtr = newSrc.c_str(); sourcePtr = newSrc.c_str();
} }
Expand Down

0 comments on commit 3d02748

Please sign in to comment.