DestructifyReductionOpportunities extends ScopeTrackingVisitor { static List<DestructifyReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(DestructifyReductionOpportunities::findOpportunitiesForShader) .reduce(Arrays.asList(), ListConcat::concatenate); } private  DestructifyReductionOpportunities(TranslationUnit tu); @Override void visitDeclarationStmt(DeclarationStmt declarationStmt);  }
@Test public void checkForVariableInScope() throws Exception { final String program = "" + "struct _GLF_struct_1 {\n" + " float dist;\n" + "};\n" + "void main() {\n" + " int dist;\n" + " _GLF_struct_1 _GLF_struct_replacement_2 = _GLF_struct_1(1.0);\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void checkForVariableInScope2() throws Exception { final String program = "" + "struct _GLF_struct_1 {\n" + " float dist;\n" + "};\n" + "void main() {\n" + " int dist;\n" + " {\n" + " _GLF_struct_1 _GLF_struct_replacement_2 = _GLF_struct_1(1.0);\n" + " }\n" + "}\n"; final String expected = "" + "struct _GLF_struct_1 {\n" + " float dist;\n" + "};\n" + "void main() {\n" + " int dist;\n" + " {\n" + " float dist = 1.0;\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); assertEquals(PrettyPrinterVisitor.prettyPrintAsString(ParseHelper.parse(expected)), PrettyPrinterVisitor.prettyPrintAsString(tu)); }
@Test public void checkForVariableInScope3() throws Exception { final String program = "" + "struct _GLF_struct_1 {\n" + " float dist;\n" + "};\n" + "void main() {\n" + " float dist;\n" + " {\n" + " _GLF_struct_1 _GLF_struct_replacement_2 = _GLF_struct_1(1.0);\n" + " dist = 2.0;\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void misc() throws Exception { final String program = "struct _GLF_struct_65 {\n" + " vec3 GLF_live6p2;\n" + " mat2 _f1;\n" + " bvec3 _f2;\n" + " bvec3 _f3;\n" + "};\n" + "void main() {\n" + " float GLF_live6formuparam = 31.97;\n" + " _GLF_struct_65 _GLF_struct_replacement_66 = _GLF_struct_65(vec3(1.0), mat2(1.0), " + "bvec3(true), bvec3(true));\n" + " _GLF_struct_replacement_66.GLF_live6p2 = abs(_GLF_struct_replacement_66.GLF_live6p2)" + " / dot(_GLF_struct_replacement_66.GLF_live6p2, _GLF_struct_replacement_66" + ".GLF_live6p2) - GLF_live6formuparam;\n" + "}\n"; final String expected = "struct _GLF_struct_65 {\n" + " vec3 GLF_live6p2;\n" + " mat2 _f1;\n" + " bvec3 _f2;\n" + " bvec3 _f3;\n" + "};\n" + "void main() {\n" + " float GLF_live6formuparam = 31.97;\n" + " vec3 GLF_live6p2 = vec3(1.0);\n" + " GLF_live6p2 = abs(GLF_live6p2) / dot(GLF_live6p2, GLF_live6p2) - " + "GLF_live6formuparam;\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); assertEquals(PrettyPrinterVisitor.prettyPrintAsString(ParseHelper.parse(expected)), PrettyPrinterVisitor.prettyPrintAsString(tu)); }