From 7ef842b61ed54d5f1df49739017fdbc1d4ddb449 Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Thu, 27 Sep 2012 17:35:56 +0300 Subject: [PATCH] postmerge: change how array initializers are emitted, plus tests for the behavior flip flag --- hlslang/GLSLCodeGen/glslOutput.cpp | 13 +++++------ hlslang/GLSLCodeGen/glslOutput.h | 6 ++--- include/hlsl2glsl.h | 24 +++++++++++--------- tests/fragment/array-unsized-in.txt | 11 +++++++++ tests/fragment/array-unsized-out.txt | 25 +++++++++++++++++++++ tests/fragment/arrayglsl120-unsized-in.txt | 15 +++++++++++++ tests/fragment/arrayglsl120-unsized-out.txt | 18 +++++++++++++++ tests/hlsl2glsltest/hlsl2glsltest.cpp | 23 ++++++++++++------- 8 files changed, 107 insertions(+), 28 deletions(-) create mode 100644 tests/fragment/array-unsized-in.txt create mode 100644 tests/fragment/array-unsized-out.txt create mode 100644 tests/fragment/arrayglsl120-unsized-in.txt create mode 100644 tests/fragment/arrayglsl120-unsized-out.txt diff --git a/hlslang/GLSLCodeGen/glslOutput.cpp b/hlslang/GLSLCodeGen/glslOutput.cpp index 6bb674b..1c6d78b 100644 --- a/hlslang/GLSLCodeGen/glslOutput.cpp +++ b/hlslang/GLSLCodeGen/glslOutput.cpp @@ -298,7 +298,7 @@ TGlslOutputTraverser::TGlslOutputTraverser(TInfoSink& i, std::vectorgetTypePointer()); TType& type = *decl->getTypePointer(); - - bool emit_osx10_6_arrays = goit->m_EmitSnowLeopardCompatibleArrayInitializers + const bool emit_pre_glsl_120_arrays = !goit->emitGLSL120ArrayInitializers && decl->containsArrayInitialization(); - if (emit_osx10_6_arrays) { - assert(decl->isSingleInitialization() && "Emission of multiple in-line array declarations isn't supported when running in OS X 10.6 compatible mode."); + if (emit_pre_glsl_120_arrays) { + assert(decl->isSingleInitialization() && "Emission of multiple in-line array declarations isn't supported when running in pre-GLSL1.20 compatible mode."); current->indent(out); - out << "#if defined(OSX_SNOW_LEOPARD)" << std::endl; + out << "#if !defined(HLSL2GLSL_ENABLE_ARRAY_INIT)" << std::endl; current->increaseDepth(); TQualifier q = type.getQualifier(); @@ -392,7 +391,7 @@ bool TGlslOutputTraverser::traverseDeclaration(bool preVisit, TIntermDeclaration current->endStatement(); - if (emit_osx10_6_arrays) { + if (emit_pre_glsl_120_arrays) { current->decreaseDepth(); current->indent(out); out << "#endif" << std::endl; diff --git a/hlslang/GLSLCodeGen/glslOutput.h b/hlslang/GLSLCodeGen/glslOutput.h index 96a6509..634bfaf 100644 --- a/hlslang/GLSLCodeGen/glslOutput.h +++ b/hlslang/GLSLCodeGen/glslOutput.h @@ -61,10 +61,10 @@ public: // Persistent data for collecting indices std::vector indexList; - unsigned swizzleAssignTempCounter; - bool m_UsePrecision; - bool m_EmitSnowLeopardCompatibleArrayInitializers; TSourceLoc m_LastLineOutput; + unsigned swizzleAssignTempCounter; + bool m_UsePrecision; + bool emitGLSL120ArrayInitializers; }; #endif //GLSL_OUTPUT_H diff --git a/include/hlsl2glsl.h b/include/hlsl2glsl.h index 1c5dd1d..63a668c 100644 --- a/include/hlsl2glsl.h +++ b/include/hlsl2glsl.h @@ -129,9 +129,10 @@ enum TTranslateOptions ETranslateOpIntermediate = (1<<0), ETranslateOpUsePrecision = (1<<1), - /// Array initializers on OS X 10.6.X GLSL 1.20 are broken. Enabling this flag will tell the compiler - /// to emit two versions of all array initializations, one that is compatible with snow leopard and one that - /// is standard GLSL 1.20. + /// Array initializers do not exist on GLSL ES 1.0, and are broken on + /// OS X 10.6.x with GLSL 1.20 as well. By default we'll emit code + /// that can handle both cases, with "real" initialization path + /// kicking in only when you've defined HLSL2GLSL_ENABLE_ARRAY_INIT. /// /// Example of emitted code for a simple array declaration: /// (HLSL Source) @@ -141,15 +142,18 @@ enum TTranslateOptions /// float2(1, 0.1) /// }; /// (GLSL Emitted result) - /// #if defined(OSX_SNOW_LEOPARD) - /// vec2[] samples; - /// samples[0] = vec2(-1.0, 0.1); - /// samples[0] = vec2(0.0, 0.5); - /// samples[0] = vec2(1.0, 0.1); - /// #else + /// #if defined(HLSL2GLSL_ENABLE_ARRAY_INIT) /// const vec2 samples[] = vec2[](vec2(-1.0, 0.1), vec2(0.0, 0.5), vec2(1.0, 0.1)); + /// #else + /// vec2 samples[]; + /// samples[0] = vec2(-1.0, 0.1); + /// samples[1] = vec2(0.0, 0.5); + /// samples[2] = vec2(1.0, 0.1); /// #endif - ETranslateOpEmitSnowLeopardCompatibleArrayInitializers = (1<<2) + /// + /// If you don't need GLSL ES 1.0 support, or OS X 10.6.x support, + /// then pass this flag to always use "real" array initializers. + ETranslateOpEmitGLSL120ArrayInitializers = (1<<2) }; diff --git a/tests/fragment/array-unsized-in.txt b/tests/fragment/array-unsized-in.txt new file mode 100644 index 0000000..df9b0a1 --- /dev/null +++ b/tests/fragment/array-unsized-in.txt @@ -0,0 +1,11 @@ +half4 main () : COLOR0 { + float2 lut[] = { + float2(-1, 0.1), + float2(0, 0.5), + float2(1, 0.1) + }; + half4 c = 0; + for (int i = 0; i < 3; ++i) + c.xy += lut[i]; + return c; +} diff --git a/tests/fragment/array-unsized-out.txt b/tests/fragment/array-unsized-out.txt new file mode 100644 index 0000000..7038612 --- /dev/null +++ b/tests/fragment/array-unsized-out.txt @@ -0,0 +1,25 @@ +vec4 xlat_main( ); +#line 1 +vec4 xlat_main( ) { + #line 2 + #if !defined(HLSL2GLSL_ENABLE_ARRAY_INIT) + vec2[3] lut; + lut = vec2( float((-1)), 0.1); + lut = vec2( 0.0, 0.5); + lut = vec2( 1.0, 0.1); + #else + vec2 lut = vec2[3]( vec2( float((-1)), 0.1), vec2( 0.0, 0.5), vec2( 1.0, 0.1))[3]; + #endif + #line 7 + vec4 c = vec4( 0.0); + int i = 0; + for ( ; (i < 3); (++i)) { + c.xy += lut[i]; + } + return c; +} +void main() { + vec4 xl_retval; + xl_retval = xlat_main( ); + gl_FragData[0] = vec4( xl_retval); +} diff --git a/tests/fragment/arrayglsl120-unsized-in.txt b/tests/fragment/arrayglsl120-unsized-in.txt new file mode 100644 index 0000000..9262e06 --- /dev/null +++ b/tests/fragment/arrayglsl120-unsized-in.txt @@ -0,0 +1,15 @@ +// "arrayglsl120" in the name will make hlsl2glsl test driver +// enable "full GLSL 1.20" array printing mode. +// This is a test to ensure it keeps on working. + +half4 main () : COLOR0 { + float2 lut[] = { + float2(-1, 0.1), + float2(0, 0.5), + float2(1, 0.1) + }; + half4 c = 0; + for (int i = 0; i < 3; ++i) + c.xy += lut[i]; + return c; +} diff --git a/tests/fragment/arrayglsl120-unsized-out.txt b/tests/fragment/arrayglsl120-unsized-out.txt new file mode 100644 index 0000000..6961a47 --- /dev/null +++ b/tests/fragment/arrayglsl120-unsized-out.txt @@ -0,0 +1,18 @@ +vec4 xlat_main( ); +#line 5 +vec4 xlat_main( ) { + #line 6 + vec2 lut = vec2[3]( vec2( float((-1)), 0.1), vec2( 0.0, 0.5), vec2( 1.0, 0.1))[3]; + #line 11 + vec4 c = vec4( 0.0); + int i = 0; + for ( ; (i < 3); (++i)) { + c.xy += lut[i]; + } + return c; +} +void main() { + vec4 xl_retval; + xl_retval = xlat_main( ); + gl_FragData[0] = vec4( xl_retval); +} diff --git a/tests/hlsl2glsltest/hlsl2glsltest.cpp b/tests/hlsl2glsltest/hlsl2glsltest.cpp index a71e502..549150d 100644 --- a/tests/hlsl2glsltest/hlsl2glsltest.cpp +++ b/tests/hlsl2glsltest/hlsl2glsltest.cpp @@ -242,6 +242,7 @@ static bool TestFile (TestRun type, const std::string& outputPath, const char* entryPoint, bool usePrecision, + bool useGLSL120Arrays, bool doCheckGLSL) { std::string input; @@ -264,7 +265,11 @@ static bool TestFile (TestRun type, if (usePrecision) options |= ETranslateOpUsePrecision; - options |= ETranslateOpEmitSnowLeopardCompatibleArrayInitializers; + if (useGLSL120Arrays) + { + assert (!usePrecision); // GLES 1.0 can't do that + options |= ETranslateOpEmitGLSL120ArrayInitializers; + } int parseOk = Hlsl2Glsl_Parse (parser, sourceStr, options); const char* infoLog = Hlsl2Glsl_GetInfoLog( parser ); @@ -410,14 +415,15 @@ static bool TestCombinedFile(const std::string& inputPath, frag_out = outname + "-fragment-out.txt"; } - bool res = TestFile(VERTEX, inputPath, vert_out, "vs_main", usePrecision, !usePrecision && checkGL); - return res & TestFile(FRAGMENT, inputPath, frag_out, "ps_main", usePrecision, !usePrecision && checkGL); + bool res = TestFile(VERTEX, inputPath, vert_out, "vs_main", usePrecision, false, !usePrecision && checkGL); + return res & TestFile(FRAGMENT, inputPath, frag_out, "ps_main", usePrecision, false, !usePrecision && checkGL); } static bool TestFile (TestRun type, const std::string& inputPath, bool usePrecision, + bool useGLSL120Arrays, bool checkGL) { std::string outname = inputPath.substr (0,inputPath.size()-7); @@ -426,9 +432,9 @@ static bool TestFile (TestRun type, return TestFileFailure(type==VERTEX_FAILURES, inputPath, outname + "-out.txt"); } else { if (usePrecision) { - return TestFile(type, inputPath, outname + "-outES.txt", "main", true, false); + return TestFile(type, inputPath, outname + "-outES.txt", "main", true, useGLSL120Arrays, false); } else { - return TestFile(type, inputPath, outname + "-out.txt", "main", false, checkGL); + return TestFile(type, inputPath, outname + "-out.txt", "main", false, useGLSL120Arrays, checkGL); } } return false; @@ -487,9 +493,10 @@ int main (int argc, const char** argv) if (ok) ok = TestCombinedFile(testFolder + "/" + inname, true, false); } else { - ok = TestFile(TestRun(type), testFolder + "/" + inname, false, hasOpenGL); - if (ok) - ok = TestFile(TestRun(type), testFolder + "/" + inname, true, false); + const bool useGLSL120Arrays = (inname.find("arrayglsl120") != std::string::npos); + ok = TestFile(TestRun(type), testFolder + "/" + inname, false, useGLSL120Arrays, hasOpenGL); + if (ok && !useGLSL120Arrays) + ok = TestFile(TestRun(type), testFolder + "/" + inname, true, false, false); } if (!ok)