/** * reads an output mapping * * @param statement1 the staement being read * @return the mapping * @throws IOException */ public VariableMapping readOutputMapping(Statement statement1) throws IOException { VariableMapping mapping = null; try { mapping = parseMapping(statement1, new boolean[] {true, false}); } catch (Exception e) { throw new MatParseException("Unexpected mapping format", statement1, e); } ShaderNodeVariable left = mapping.getLeftVariable(); ShaderNodeVariable right = mapping.getRightVariable(); if (left.getType().startsWith("sampler") || right.getType().startsWith("sampler")) { throw new MatParseException("Samplers can only be inputs", statement1); } if (left.getNameSpace().equals("Global")) { left.setType("vec4"); // Globals are all vec4 for now (maybe forever...) storeGlobal(left, statement1); } else { throw new MatParseException( "Only Global nameSpace is allowed for outputMapping, got" + left.getNameSpace(), statement1); } if (!updateVariableFromList(right, shaderNode.getDefinition().getOutputs())) { throw new MatParseException( right.getName() + " is not an output variable of " + shaderNode.getDefinition().getName(), statement1); } checkTypes(mapping, statement1); return mapping; }
protected void computeConditions() { updateConditions(vertexDeclaredUniforms); updateConditions(fragmentDeclaredUniforms); updateConditions(varyings); for (DeclaredVariable v : varyings.values()) { for (ShaderNode sn : techniqueDef.getShaderNodes()) { if (sn.getDefinition().getType() == Shader.ShaderType.Vertex) { for (VariableMapping mapping : sn.getInputMapping()) { if (mapping.getLeftVariable().equals(v.var)) { if (mapping.getCondition() == null || v.var.getCondition() == null) { mapping.setCondition(v.var.getCondition()); } else { mapping.setCondition( "(" + mapping.getCondition() + ") || (" + v.var.getCondition() + ")"); } } } } } } updateConditions(attributes); // updateConditions(fragmentGlobals); // vertexGlobal.makeCondition(); }
/** * Reads alist of ShaderNodes * * @param statements the list of statements to read * @throws IOException */ public void readNodes(List<Statement> statements) throws IOException { if (techniqueDef.getShaderNodes() == null) { techniqueDef.setShaderNodes(new ArrayList<ShaderNode>()); techniqueDef.setShaderGenerationInfo(new ShaderGenerationInfo()); } for (Statement statement : statements) { String[] split = statement.getLine().split("[ \\{]"); if (statement.getLine().startsWith("ShaderNode ")) { String name = statement.getLine().substring("ShaderNode".length()).trim(); if (nodes == null) { nodes = new HashMap<String, ShaderNode>(); } if (!nodes.containsKey(name)) { shaderNode = new ShaderNode(); shaderNode.setName(name); techniqueDef.getShaderGenerationInfo().getUnusedNodes().add(name); readShaderNode(statement.getContents()); nodes.put(name, shaderNode); techniqueDef.getShaderNodes().add(shaderNode); } else { throw new MatParseException("ShaderNode " + name + " is already defined", statement); } } else { throw new MatParseException("ShaderNode", split[0], statement); } } }
/** * searcha variable in the given list and updates its type and namespace * * @param var the variable to update * @param list the variables list * @return true if the variable has been found and updated */ protected boolean updateVariableFromList(ShaderNodeVariable var, List<ShaderNodeVariable> list) { for (ShaderNodeVariable shaderNodeVariable : list) { if (shaderNodeVariable.getName().equals(var.getName())) { var.setType(shaderNodeVariable.getType()); var.setMultiplicity(shaderNodeVariable.getMultiplicity()); var.setNameSpace(shaderNode.getName()); return true; } } return false; }
/** * store a varying * * @param node the shaderNode * @param variable the variable to store */ public void storeVaryings(ShaderNode node, ShaderNodeVariable variable) { variable.setShaderOutput(true); if (node.getDefinition().getType() == Shader.ShaderType.Vertex && shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) { DeclaredVariable dv = varyings.get(variable.getName()); if (dv == null) { techniqueDef.getShaderGenerationInfo().getVaryings().add(variable); dv = new DeclaredVariable(variable); varyings.put(variable.getName(), dv); } dv.addNode(shaderNode); // if a variable is declared with the same name as an input and an output and is a varying, // set it as a shader output so it's declared as a varying only once. for (VariableMapping variableMapping : node.getInputMapping()) { if (variableMapping.getLeftVariable().getName().equals(variable.getName())) { variableMapping.getLeftVariable().setShaderOutput(true); } } } }
/** * reads a list of ShaderNode{} blocks * * @param statements the list of statements to parse * @throws IOException */ protected void readShaderNode(List<Statement> statements) throws IOException { for (Statement statement : statements) { String line = statement.getLine(); String[] split = statement.getLine().split("[ \\{]"); if (line.startsWith("Definition")) { ShaderNodeDefinition def = findDefinition(statement); shaderNode.setDefinition(def); if (def.isNoOutput()) { techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(shaderNode.getName()); } } else if (line.startsWith("Condition")) { String condition = line.substring(line.lastIndexOf(":") + 1).trim(); extractCondition(condition, statement); shaderNode.setCondition(conditionParser.getFormattedExpression()); } else if (line.startsWith("InputMapping")) { for (Statement statement1 : statement.getContents()) { VariableMapping mapping = readInputMapping(statement1); techniqueDef .getShaderGenerationInfo() .getUnusedNodes() .remove(mapping.getRightVariable().getNameSpace()); shaderNode.getInputMapping().add(mapping); } } else if (line.startsWith("OutputMapping")) { for (Statement statement1 : statement.getContents()) { VariableMapping mapping = readOutputMapping(statement1); techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(shaderNode.getName()); shaderNode.getOutputMapping().add(mapping); } } else { throw new MatParseException("ShaderNodeDefinition", split[0], statement); } } }
/** * stores a global output * * @param var the variable to store * @param statement1 the statement being read * @throws IOException */ public void storeGlobal(ShaderNodeVariable var, Statement statement1) throws IOException { var.setShaderOutput(true); if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) { ShaderNodeVariable global = techniqueDef.getShaderGenerationInfo().getVertexGlobal(); if (global != null) { // global.setCondition(mergeConditions(global.getCondition(), // var.getCondition(), "||")); // var.setCondition(global.getCondition()); if (!global.getName().equals(var.getName())) { throw new MatParseException( "A global output is already defined for the vertex shader: " + global.getName() + ". vertex shader can only have one global output", statement1); } } else { techniqueDef.getShaderGenerationInfo().setVertexGlobal(var); } } else if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) { storeVariable(var, techniqueDef.getShaderGenerationInfo().getFragmentGlobals()); } }
public void makeCondition() { var.setCondition(null); for (ShaderNode node : nodes) { String condition = null; for (VariableMapping mapping : node.getInputMapping()) { if (mapping.getRightVariable().equals(var)) { if (mapping.getCondition() == null) { condition = null; break; } if (condition == null) { condition = "(" + mapping.getCondition() + ")"; } else { if (!condition.contains(mapping.getCondition())) { condition = condition + " || (" + mapping.getCondition() + ")"; } } } } if (node.getCondition() == null && condition == null) { var.setCondition(null); return; } if (node.getCondition() != null) { if (condition == null) { condition = node.getCondition(); } else { if (!condition.contains(node.getCondition())) { condition = "(" + node.getCondition() + ") && (" + condition + ")"; } } } if (var.getCondition() == null) { var.setCondition(condition); } else { if (!var.getCondition().contains(condition)) { var.setCondition("(" + var.getCondition() + ") || (" + condition + ")"); } } } }
/** * reads an input mapping * * @param statement1 the statement being read * @return the mapping * @throws IOException */ public VariableMapping readInputMapping(Statement statement1) throws IOException { VariableMapping mapping = null; try { mapping = parseMapping(statement1, new boolean[] {false, true}); } catch (Exception e) { throw new MatParseException("Unexpected mapping format", statement1, e); } ShaderNodeVariable left = mapping.getLeftVariable(); ShaderNodeVariable right = mapping.getRightVariable(); if (!updateVariableFromList(left, shaderNode.getDefinition().getInputs())) { throw new MatParseException( left.getName() + " is not an input variable of " + shaderNode.getDefinition().getName(), statement1); } if (left.getType().startsWith("sampler") && !right.getNameSpace().equals("MatParam")) { throw new MatParseException("Samplers can only be assigned to MatParams", statement1); } if (right.getNameSpace().equals("Global")) { right.setType("vec4"); // Globals are all vec4 for now (maybe forever...) // updateCondition(right, mapping); storeGlobal(right, statement1); } else if (right.getNameSpace().equals("Attr")) { if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) { throw new MatParseException( "Cannot have an attribute as input in a fragment shader" + right.getName(), statement1); } updateVarFromAttributes(mapping.getRightVariable(), mapping); // updateCondition(mapping.getRightVariable(), mapping); storeAttribute(mapping.getRightVariable()); } else if (right.getNameSpace().equals("MatParam")) { MatParam param = findMatParam(right.getName()); if (param == null) { throw new MatParseException( "Could not find a Material Parameter named " + right.getName(), statement1); } if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) { if (updateRightFromUniforms(param, mapping, vertexDeclaredUniforms, statement1)) { storeVertexUniform(mapping.getRightVariable()); } } else { if (updateRightFromUniforms(param, mapping, fragmentDeclaredUniforms, statement1)) { if (mapping.getRightVariable().getType().contains("|")) { String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType()); if (type != null) { mapping.getRightVariable().setType(type); } else { throw new MatParseException( param.getVarType().toString() + " can only be matched to one of " + param.getVarType().getGlslType().replaceAll("\\|", ",") + " found " + left.getType(), statement1); } } storeFragmentUniform(mapping.getRightVariable()); } } } else if (right.getNameSpace().equals("WorldParam")) { UniformBinding worldParam = findWorldParam(right.getName()); if (worldParam == null) { throw new MatParseException( "Could not find a World Parameter named " + right.getName(), statement1); } if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) { if (updateRightFromUniforms(worldParam, mapping, vertexDeclaredUniforms)) { storeVertexUniform(mapping.getRightVariable()); } } else { if (updateRightFromUniforms(worldParam, mapping, fragmentDeclaredUniforms)) { storeFragmentUniform(mapping.getRightVariable()); } } } else { ShaderNode node = nodes.get(right.getNameSpace()); if (node == null) { throw new MatParseException( "Undeclared node" + right.getNameSpace() + ". Make sure this node is declared before the current node", statement1); } ShaderNodeVariable var = findNodeOutput(node.getDefinition().getOutputs(), right.getName()); if (var == null) { throw new MatParseException( "Cannot find output variable" + right.getName() + " form ShaderNode " + node.getName(), statement1); } right.setNameSpace(node.getName()); right.setType(var.getType()); right.setMultiplicity(var.getMultiplicity()); mapping.setRightVariable(right); storeVaryings(node, mapping.getRightVariable()); } checkTypes(mapping, statement1); return mapping; }