RemoveUnusedParameterReductionOpportunities extends ReductionOpportunitiesBase<RemoveUnusedParameterReductionOpportunity> { static List<RemoveUnusedParameterReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  RemoveUnusedParameterReductionOpportunities(TranslationUnit tu,
                                                      ReducerContext context); @Override void visitTranslationUnit(TranslationUnit translationUnit); @Override void visitFunctionDefinition(FunctionDefinition functionDefinition); @Override void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr); @Override void visitFunctionPrototype(FunctionPrototype functionPrototype);  }
@Test public void testRemoveParam() throws Exception { final String shader = "float foo(int a, float b, float c) {" + " return b;" + "}" + "" + "void main() {" + " foo(5, 4.0, 2.0);" + "}"; final String expectedResult = "float foo(float b) {" + " return b;" + "}" + "" + "void main() {" + " foo(4.0);" + "}"; final TranslationUnit tu = ParseHelper.parse(shader); List<RemoveUnusedParameterReductionOpportunity> opportunities = findOpportunities(tu, true); assertEquals(2, opportunities.size()); opportunities.get(0).applyReduction(); opportunities = findOpportunities(tu, true); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); CompareAsts.assertEqualAsts(expectedResult, tu); }
@Test public void testRemoveParam2() throws Exception { final String shader = "float foo(int a);" + "float foo(int a) {" + " return 0.0;" + "}" + "void main() {" + " foo(2.0);" + "}"; final String expectedResult = "float foo();" + "float foo() {" + " return 0.0;" + "}" + "void main() {" + " foo();" + "}"; final TranslationUnit tu = ParseHelper.parse(shader); List<RemoveUnusedParameterReductionOpportunity> opportunities = findOpportunities(tu, true); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); CompareAsts.assertEqualAsts(expectedResult, tu); }
@Test public void testDoNotRemoveParamIfOverloaded() throws Exception { final String shader = "float foo(int a, float b) {" + " return float(a);" + "}" + "void foo(int a) {" + "}" + "void main() {" + " foo(2, 3.0);" + " foo(2);" + "}"; final TranslationUnit tu = ParseHelper.parse(shader); List<RemoveUnusedParameterReductionOpportunity> opportunities = findOpportunities(tu, true); assertEquals(0, opportunities.size()); }
@Test public void testRemoveParamOnlyIfReduceEverywhere() throws Exception { final String shader = "" + "float x = 0.0;" + "void bar(float a) { }" + "float foo() {" + " x = 1.0;" + " return 3.0;" + "}" + "void main() {" + " bar(foo());" + " gl_FragColor = vec4(x);" + "}"; final String shaderIfReduced = "" + "float x = 0.0;" + "void bar() { }" + "float foo() {" + " x = 1.0;" + " return 3.0;" + "}" + "void main() {" + " bar();" + " gl_FragColor = vec4(x);" + "}"; TranslationUnit tu = ParseHelper.parse(shader); List<RemoveUnusedParameterReductionOpportunity> opportunities = findOpportunities(tu, true); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); CompareAsts.assertEqualAsts(shaderIfReduced, tu); tu = ParseHelper.parse(shader); opportunities = findOpportunities(tu, false); assertEquals(0, opportunities.size()); }