AddInitializers { public static void addInitializers(ShaderJob shaderJob) { for (TranslationUnit tu : shaderJob.getShaders()) { new ScopeTrackingVisitor() { @Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { super.visitVariablesDeclaration(variablesDeclaration); if (variablesDeclaration.getBaseType().hasQualifier(TypeQualifier.SHADER_INPUT) || variablesDeclaration.getBaseType().hasQualifier(TypeQualifier.SHADER_OUTPUT) || variablesDeclaration.getBaseType().hasQualifier(TypeQualifier.UNIFORM)) { return; } for (VariableDeclInfo vdi : variablesDeclaration.getDeclInfos()) { if (vdi.hasInitializer()) { continue; } Type variableType = variablesDeclaration.getBaseType().getWithoutQualifiers(); if (vdi.hasArrayInfo()) { variableType = new ArrayType(variableType, vdi.getArrayInfo()); } if (!variableType.hasCanonicalConstant(getCurrentScope())) { return; } vdi.setInitializer(new Initializer( variableType.getCanonicalConstant(getCurrentScope()))); } } }.visit(tu); } }  static void addInitializers(ShaderJob shaderJob);  }
@Test public void basicTest() throws Exception { final String shader = "#version 310 es\n" + "precision highp float;\n" + "uniform float time;\n" + "layout(location = 0) out vec4 _GLF_color;\n" + "struct S {\n" + " int a;\n" + " float b[3];\n" + "} myS;\n" + "S anotherS;\n" + "const int g;\n" + "const int h = 2;\n" + "void main() {" + " vec2 v;\n" + " mat2x3 m;\n" + " bvec4 b;\n" + "}\n"; final ShaderJob shaderJob = new GlslShaderJob(Optional.empty(), new PipelineInfo(), ParseHelper.parse(shader, ShaderKind.FRAGMENT)); AddInitializers.addInitializers(shaderJob); final String expected = "#version 310 es\n" + "precision highp float;\n" + "uniform float time;\n" + "layout(location = 0) out vec4 _GLF_color;\n" + "struct S {\n" + " int a;\n" + " float b[3];\n" + "} myS = S(1, float[3](1.0, 1.0, 1.0));\n" + "S anotherS = S(1, float[3](1.0, 1.0, 1.0));\n" + "const int g = 1;\n" + "const int h = 2;\n" + "void main() {" + " vec2 v = vec2(1.0);\n" + " mat2x3 m = mat2x3(1.0);\n" + " bvec4 b = bvec4(true);\n" + "}\n"; CompareAsts.assertEqualAsts(expected, shaderJob.getFragmentShader().get()); }