LiveOutputVariableWriteReductionOpportunities extends ReductionOpportunitiesBase<LiveOutputVariableWriteReductionOpportunity> { static List<LiveOutputVariableWriteReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  LiveOutputVariableWriteReductionOpportunities(
        TranslationUnit tu,
        ReducerContext context); @Override void visitBlockStmt(BlockStmt block);  }
@Test public void testLiveGlFragColorWriteOpportunity() throws Exception { final String backupName = Constants.GLF_OUT_VAR_BACKUP_PREFIX + OpenGlConstants.GL_FRAG_COLOR; final String program = "void main() {" + " {" + " vec4 " + backupName + ";" + " " + backupName + " = " + OpenGlConstants.GL_FRAG_COLOR + ";" + " " + OpenGlConstants.GL_FRAG_COLOR + " = " + backupName + ";" + " }" + "}"; final String reducedProgram = "void main() {" + " {" + " }" + "}"; final TranslationUnit tu = ParseHelper.parse(program); List<LiveOutputVariableWriteReductionOpportunity> ops = LiveOutputVariableWriteReductionOpportunities .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(reducedProgram, tu); }
@Test public void testLiveGlFragColorWriteOpportunityAtRootOfMain() throws Exception { final String backupName = Constants.GLF_OUT_VAR_BACKUP_PREFIX + OpenGlConstants.GL_FRAG_COLOR; final String program = "void main() {" + " vec4 " + backupName + ";" + " " + backupName + " = " + OpenGlConstants.GL_FRAG_COLOR + ";" + " " + OpenGlConstants.GL_FRAG_COLOR + " = " + backupName + ";" + "}"; final String reducedProgram = "void main() {" + "}"; final TranslationUnit tu = ParseHelper.parse(program); List<LiveOutputVariableWriteReductionOpportunity> ops = LiveOutputVariableWriteReductionOpportunities .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(reducedProgram, tu); }
@Test public void testOpportunityIsPresent1() throws Exception { final String backupName = Constants.GLF_OUT_VAR_BACKUP_PREFIX + OpenGlConstants.GL_FRAG_COLOR; final String program = "void main() {\n" + " {\n" + " vec4 " + backupName + ";\n" + " " + backupName + " = " + OpenGlConstants.GL_FRAG_COLOR + ";\n" + " " + OpenGlConstants.GL_FRAG_COLOR + " = atan(vec4(-5.2, -8.2, 62.53, -205.377));\n" + " if(_GLF_WRAPPED_IF_TRUE(true))\n" + " {\n" + " " + OpenGlConstants.GL_FRAG_COLOR + " = " + backupName + ";\n" + " }\n" + " }\n" + "}\n"; final String expected = "void main() { { } }"; final TranslationUnit tu = ParseHelper.parse(program); final List<LiveOutputVariableWriteReductionOpportunity> ops = LiveOutputVariableWriteReductionOpportunities .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 testOpportunityIsPresent2() throws Exception { final String backupName = Constants.GLF_OUT_VAR_BACKUP_PREFIX + OpenGlConstants.GL_FRAG_COLOR; final String program = "void main() {\n" + " {\n" + " vec4 " + backupName + ";\n" + " " + backupName + " = gl_FragColor;\n" + " gl_FragColor = atan(vec4(-5.2, -8.2, 62.53, -205.377));\n" + " gl_FragColor = " + backupName + ";\n" + " }\n" + "}\n"; final String expected = "void main() { { } }"; final TranslationUnit tu = ParseHelper.parse(program); List<LiveOutputVariableWriteReductionOpportunity> ops = LiveOutputVariableWriteReductionOpportunities .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 testOpportunityIsPresent3() throws Exception { final String backupName = Constants.GLF_OUT_VAR_BACKUP_PREFIX + OpenGlConstants.GL_FRAG_COLOR; final String program = "void main() {\n" + " {\n" + " vec4 " + backupName + ";\n" + " " + backupName + " = gl_FragColor;\n" + " gl_FragColor = atan(vec4(-5.2, -8.2, 62.53, -205.377));\n" + " if(_GLF_WRAPPED_IF_TRUE(true))\n" + " gl_FragColor = " + backupName + ";\n" + " }\n" + "}\n"; final String expected = "void main() { { } }"; final TranslationUnit tu = ParseHelper.parse(program); List<LiveOutputVariableWriteReductionOpportunity> ops = LiveOutputVariableWriteReductionOpportunities .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); }