GlobalVariablesDeclarationReductionOpportunities extends ReductionOpportunitiesBase<GlobalVariablesDeclarationReductionOpportunity> { static List<GlobalVariablesDeclarationReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  GlobalVariablesDeclarationReductionOpportunities(TranslationUnit tu,
                                                           ReducerContext context); @Override void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration); @Override void visitFunctionDefinition(FunctionDefinition functionDefinition);  }
@Test public void testDoNotRemoveUsedLiveCodeDecl() throws Exception { final String program = "void main() {\n" + " int GLF_live0c = 3;" + " GLF_live0c++;" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); List<? extends IReductionOpportunity> ops = GlobalVariablesDeclarationReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void testUsedStructIsNotRemoved() throws Exception { final String program = "struct S { float x; };\n" + "void main() {\n" + " gl_FragColor = vec4(S(3.0).x);\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<GlobalVariablesDeclarationReductionOpportunity> ops = GlobalVariablesDeclarationReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void testUnusedStructIsRemoved() throws Exception { final String original = "struct S { int x; }; void main() { }"; final String expected = "void main() { }"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariablesDeclarationReductionOpportunity> ops = GlobalVariablesDeclarationReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testUsedAnonymousStructIsNotRemoved() throws Exception { final String program = "const struct { float x; } a;\n" + "void main() {\n" + " gl_FragColor = vec4(a.x);\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<GlobalVariablesDeclarationReductionOpportunity> ops = GlobalVariablesDeclarationReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void testUnusedAnonymousStructIsRemoved() throws Exception { final String original = "volatile struct { int x; }; void main() { }"; final String expected = "void main() { }"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariablesDeclarationReductionOpportunity> ops = GlobalVariablesDeclarationReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testVarIsRemovedButStructLeftBehind() throws Exception { final String original = "struct S { float x; } myS; void main() { gl_FragColor = vec4(S(3.0)" + ".x); }"; final String expected = "struct S { float x; } ; void main() { gl_FragColor = vec4(S(3.0)" + ".x); }"; final TranslationUnit tu = ParseHelper.parse(original); final List<VariableDeclReductionOpportunity> ops = VariableDeclReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); final List<GlobalVariablesDeclarationReductionOpportunity> moreOps = GlobalVariablesDeclarationReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, moreOps.size()); }