@Test public void testReplaceSimpleInt() throws Exception { final String vertexShader = "void main() { " + "int a = 1;}"; final String vertexShaderReplaced = "uniform int _GLF_uniform_int_values[2];" + "void main()" + "{" + " int a = _GLF_uniform_int_values[0];" + "}"; final String fragmentShader = "void main() { " + "int a = 2;}"; final String fragmentShaderReplaced = "uniform int _GLF_uniform_int_values[2];" + "void main()" + "{" + " int a = _GLF_uniform_int_values[1];" + "}"; final List<TranslationUnit> shaders = new ArrayList<>(); shaders.add(ParseHelper.parse(vertexShader, ShaderKind.VERTEX)); shaders.add(ParseHelper.parse(fragmentShader, ShaderKind.FRAGMENT)); final PipelineInfo pipelineInfo = new PipelineInfo(); final ShaderJob shaderJob = new GlslShaderJob(Optional.empty(), pipelineInfo, shaders); assertEquals(0, pipelineInfo.getNumUniforms()); final List<LiteralToUniformReductionOpportunity> ops = LiteralToUniformReductionOpportunities .findOpportunities(shaderJob, new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals("There should be two opportunities", 2, ops.size()); assertEquals(0, pipelineInfo.getNumUniforms()); ops.forEach(AbstractReductionOpportunity::applyReduction); CompareAsts.assertEqualAsts(fragmentShaderReplaced, shaderJob.getFragmentShader().get()); CompareAsts.assertEqualAsts(vertexShaderReplaced, shaderJob.getVertexShader().get()); assertTrue(shaderJob.getPipelineInfo().hasUniform(Constants.INT_LITERAL_UNIFORM_VALUES)); assertEquals(1, pipelineInfo.getNumUniforms()); final List<LiteralToUniformReductionOpportunity> ops2 = LiteralToUniformReductionOpportunities .findOpportunities(shaderJob, new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals("There should be no opportunities", 0, ops2.size()); }
static List<LiteralToUniformReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { final List<LiteralToUniformReductionOpportunity> opportunities = new ArrayList<>(); for (TranslationUnit tu : shaderJob.getShaders()) { new StandardVisitor() { @Override public void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr) { if (arrayIndexExpr.getArray() instanceof VariableIdentifierExpr) { String name = ((VariableIdentifierExpr) arrayIndexExpr.getArray()).getName(); if (name.equals(Constants.FLOAT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.INT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.UINT_LITERAL_UNIFORM_VALUES)) { return; } } super.visitArrayIndexExpr(arrayIndexExpr); } @Override public void visitArrayInfo(ArrayInfo arrayInfo) { } @Override public void visitIntConstantExpr(IntConstantExpr intConstantExpr) { super.visitIntConstantExpr(intConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(intConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr) { super.visitFloatConstantExpr(floatConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(floatConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitUIntConstantExpr(UIntConstantExpr uintConstantExpr) { super.visitUIntConstantExpr(uintConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(uintConstantExpr, tu, shaderJob, getVistitationDepth())); } }.visit(tu); } return opportunities; }
LiteralToUniformReductionOpportunities { static List<LiteralToUniformReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { final List<LiteralToUniformReductionOpportunity> opportunities = new ArrayList<>(); for (TranslationUnit tu : shaderJob.getShaders()) { new StandardVisitor() { @Override public void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr) { if (arrayIndexExpr.getArray() instanceof VariableIdentifierExpr) { String name = ((VariableIdentifierExpr) arrayIndexExpr.getArray()).getName(); if (name.equals(Constants.FLOAT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.INT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.UINT_LITERAL_UNIFORM_VALUES)) { return; } } super.visitArrayIndexExpr(arrayIndexExpr); } @Override public void visitArrayInfo(ArrayInfo arrayInfo) { } @Override public void visitIntConstantExpr(IntConstantExpr intConstantExpr) { super.visitIntConstantExpr(intConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(intConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr) { super.visitFloatConstantExpr(floatConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(floatConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitUIntConstantExpr(UIntConstantExpr uintConstantExpr) { super.visitUIntConstantExpr(uintConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(uintConstantExpr, tu, shaderJob, getVistitationDepth())); } }.visit(tu); } return opportunities; } }
LiteralToUniformReductionOpportunities { static List<LiteralToUniformReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { final List<LiteralToUniformReductionOpportunity> opportunities = new ArrayList<>(); for (TranslationUnit tu : shaderJob.getShaders()) { new StandardVisitor() { @Override public void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr) { if (arrayIndexExpr.getArray() instanceof VariableIdentifierExpr) { String name = ((VariableIdentifierExpr) arrayIndexExpr.getArray()).getName(); if (name.equals(Constants.FLOAT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.INT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.UINT_LITERAL_UNIFORM_VALUES)) { return; } } super.visitArrayIndexExpr(arrayIndexExpr); } @Override public void visitArrayInfo(ArrayInfo arrayInfo) { } @Override public void visitIntConstantExpr(IntConstantExpr intConstantExpr) { super.visitIntConstantExpr(intConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(intConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr) { super.visitFloatConstantExpr(floatConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(floatConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitUIntConstantExpr(UIntConstantExpr uintConstantExpr) { super.visitUIntConstantExpr(uintConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(uintConstantExpr, tu, shaderJob, getVistitationDepth())); } }.visit(tu); } return opportunities; } private  LiteralToUniformReductionOpportunities(); }
LiteralToUniformReductionOpportunities { static List<LiteralToUniformReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { final List<LiteralToUniformReductionOpportunity> opportunities = new ArrayList<>(); for (TranslationUnit tu : shaderJob.getShaders()) { new StandardVisitor() { @Override public void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr) { if (arrayIndexExpr.getArray() instanceof VariableIdentifierExpr) { String name = ((VariableIdentifierExpr) arrayIndexExpr.getArray()).getName(); if (name.equals(Constants.FLOAT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.INT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.UINT_LITERAL_UNIFORM_VALUES)) { return; } } super.visitArrayIndexExpr(arrayIndexExpr); } @Override public void visitArrayInfo(ArrayInfo arrayInfo) { } @Override public void visitIntConstantExpr(IntConstantExpr intConstantExpr) { super.visitIntConstantExpr(intConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(intConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr) { super.visitFloatConstantExpr(floatConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(floatConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitUIntConstantExpr(UIntConstantExpr uintConstantExpr) { super.visitUIntConstantExpr(uintConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(uintConstantExpr, tu, shaderJob, getVistitationDepth())); } }.visit(tu); } return opportunities; } private  LiteralToUniformReductionOpportunities();  }
LiteralToUniformReductionOpportunities { static List<LiteralToUniformReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { final List<LiteralToUniformReductionOpportunity> opportunities = new ArrayList<>(); for (TranslationUnit tu : shaderJob.getShaders()) { new StandardVisitor() { @Override public void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr) { if (arrayIndexExpr.getArray() instanceof VariableIdentifierExpr) { String name = ((VariableIdentifierExpr) arrayIndexExpr.getArray()).getName(); if (name.equals(Constants.FLOAT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.INT_LITERAL_UNIFORM_VALUES) || name.equals(Constants.UINT_LITERAL_UNIFORM_VALUES)) { return; } } super.visitArrayIndexExpr(arrayIndexExpr); } @Override public void visitArrayInfo(ArrayInfo arrayInfo) { } @Override public void visitIntConstantExpr(IntConstantExpr intConstantExpr) { super.visitIntConstantExpr(intConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(intConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr) { super.visitFloatConstantExpr(floatConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(floatConstantExpr, tu, shaderJob, getVistitationDepth())); } @Override public void visitUIntConstantExpr(UIntConstantExpr uintConstantExpr) { super.visitUIntConstantExpr(uintConstantExpr); opportunities.add(new LiteralToUniformReductionOpportunity(uintConstantExpr, tu, shaderJob, getVistitationDepth())); } }.visit(tu); } return opportunities; } private  LiteralToUniformReductionOpportunities();   }