RemoveRedundantUniformMetadataReductionOpportunities { static List<RemoveRedundantUniformMetadataReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { final List<String> canBeRemoved = new ArrayList<>(shaderJob.getPipelineInfo().getUniformNames()); for (TranslationUnit tu : shaderJob.getShaders()) { new StandardVisitor() { @Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { super.visitVariablesDeclaration(variablesDeclaration); if (variablesDeclaration.getBaseType().hasQualifier(TypeQualifier.UNIFORM)) { for (VariableDeclInfo vdi : variablesDeclaration.getDeclInfos()) { canBeRemoved.remove(vdi.getName()); } } } }.visit(tu); } return canBeRemoved .stream() .map(item -> new RemoveRedundantUniformMetadataReductionOpportunity(item, shaderJob.getPipelineInfo(), new VisitationDepth(0)) ).collect(Collectors.toList()); } private  RemoveRedundantUniformMetadataReductionOpportunities();   }
@Test public void testRemoveUnused() throws Exception { final String emptyShader = "void main() { }"; final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("unused", BasicType.FLOAT, Optional.empty(), Collections.singletonList(10.0)); final ShaderJob shaderJob = new GlslShaderJob(Optional.empty(), pipelineInfo, ParseHelper.parse(emptyShader)); assertEquals(1, shaderJob.getPipelineInfo().getNumUniforms()); List<RemoveRedundantUniformMetadataReductionOpportunity> ops = RemoveRedundantUniformMetadataReductionOpportunities .findOpportunities(shaderJob, new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(emptyShader, shaderJob.getFragmentShader().get()); assertEquals(0, shaderJob.getPipelineInfo().getNumUniforms()); }
@Test public void testRemoveUnusedNameShadowing() throws Exception { final String minimalShader = "void main() { int shadow; }"; final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("shadow", BasicType.FLOAT, Optional.empty(), Collections.singletonList(10.0)); final ShaderJob shaderJob = new GlslShaderJob(Optional.empty(), pipelineInfo, ParseHelper.parse(minimalShader)); assertEquals(1, shaderJob.getPipelineInfo().getNumUniforms()); List<RemoveRedundantUniformMetadataReductionOpportunity> ops = RemoveRedundantUniformMetadataReductionOpportunities .findOpportunities(shaderJob, new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(minimalShader, shaderJob.getFragmentShader().get()); assertEquals(0, shaderJob.getPipelineInfo().getNumUniforms()); }
@Test public void testDoNotRemoveUsed() throws Exception { final String emptyShader = "uniform float unused; void main() { }"; final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("unused", BasicType.FLOAT, Optional.empty(), Collections.singletonList(10.0)); final ShaderJob shaderJob = new GlslShaderJob(Optional.empty(), pipelineInfo, ParseHelper.parse(emptyShader)); assertEquals(1, shaderJob.getPipelineInfo().getNumUniforms()); List<RemoveRedundantUniformMetadataReductionOpportunity> ops = RemoveRedundantUniformMetadataReductionOpportunities .findOpportunities(shaderJob, new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void testDoNotRemoveUsedMultipleShaders() throws Exception { final String minimalVertexShader = "uniform float used_in_vertex_only; void main() { " + "used_in_vertex_only; }"; final String minimalFragmentShader = "uniform float used_in_fragment_only; void main() { " + "used_in_fragment_only; }"; final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("used_in_vertex_only", BasicType.FLOAT, Optional.empty(), Collections.singletonList(10.0)); pipelineInfo.addUniform("used_in_fragment_only", BasicType.FLOAT, Optional.empty(), Collections.singletonList(20.0)); final ShaderJob shaderJob = new GlslShaderJob(Optional.empty(), pipelineInfo, ParseHelper.parse(minimalVertexShader, ShaderKind.VERTEX), ParseHelper.parse(minimalFragmentShader, ShaderKind.FRAGMENT)); assertEquals(2, shaderJob.getPipelineInfo().getNumUniforms()); List<RemoveRedundantUniformMetadataReductionOpportunity> ops = RemoveRedundantUniformMetadataReductionOpportunities .findOpportunities(shaderJob, new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }