/** * replaced a variable name in a source code with the given name * @param nodeSource the source to use * @param var the variable * @param newName the new name of the variable * @return the modified source code */ protected String replace(String nodeSource, ShaderNodeVariable var, String newName) { nodeSource = nodeSource.replaceAll("(?<=\\W)" + var.getName() + "(?=\\W)", newName); return nodeSource; }
/** * Append a variable initialization to the code * * @param source the StringBuilder to use * @param var the variable to initialize * @param initValue the init value to assign to the variable */ protected void initVariable(StringBuilder source, ShaderNodeVariable var, String initValue) { appendIndent(source); source.append(var.getNameSpace()); source.append("_"); source.append(var.getName()); source.append(" = "); source.append(initValue); source.append(";\n"); } }
/** * Appends an output assignment to a shader globalOutputName = * nameSpace_varName; * * @param source the source StringBuilter to append the code. * @param globalOutputName the name of the global output (can be gl_Position * or gl_FragColor etc...). * @param var the variable to assign to the output. */ protected void appendOutput(StringBuilder source, String globalOutputName, ShaderNodeVariable var) { appendIndent(source); source.append(globalOutputName); source.append(" = "); source.append(var.getNameSpace()); source.append("_"); source.append(var.getName()); source.append(";\n"); }
/** * Searches a variable in a list from its name and merges the conditions of the * variables. * * @param variable the variable. * @param varList the variable list. */ public void storeVariable(ShaderNodeVariable variable, List<ShaderNodeVariable> varList) { for (ShaderNodeVariable var : varList) { if (var.getName().equals(variable.getName())) { return; } } varList.add(variable); }
@Override public String toString() { final StringBuilder builder = new StringBuilder(leftVariable.toString()); if (!leftSwizzling.isEmpty()) { builder.append('.').append(leftSwizzling); } builder.append(" = "); if (rightVariable != null) { builder.append(rightVariable.getType()) .append(' ') .append(rightVariable.getNameSpace()) .append('.') .append(rightVariable.getName()); if (!rightSwizzling.isEmpty()) { builder.append('.').append(rightSwizzling); } } else if (rightExpression != null) { builder.append(rightExpression); } if (condition != null && !condition.isEmpty()) { builder.append(" : ").append(condition); } return builder.toString(); }
/** * Finds a variable with the given name from the list of variable. * * @param vars the list of shader node variables. * @param rightVarName the variable name to search for. * @return the found variable or null is not found. */ public ShaderNodeVariable findNodeOutput(List<ShaderNodeVariable> vars, String rightVarName) { ShaderNodeVariable var = null; for (ShaderNodeVariable variable : vars) { if (variable.getName().equals(rightVarName)) { var = variable; } } return var; }
/** * replaces a variable name in a shaderNode source code by prefixing it * with its nameSpace and "_" if needed. * @param nodeSource the source to modify * @param var the variable to replace * @return the modified source */ protected String replaceVariableName(String nodeSource, ShaderNodeVariable var) { String namePrefix = getAppendableNameSpace(var); String newName = namePrefix + var.getName(); nodeSource = replace(nodeSource, var, newName); return nodeSource; }
source.append(var.getName()); if (var.getMultiplicity() != null) { source.append("[");
/** * Updates a variable from the attribute list. * * @param right the variable * @param mapping the mapping */ public void updateVarFromAttributes(ShaderNodeVariable right, VariableMapping mapping) { DeclaredVariable dv = attributes.get(right.getName()); if (dv == null) { dv = new DeclaredVariable(right); attributes.put(right.getName(), dv); updateRightTypeFromLeftType(mapping); } else { mapping.setRightVariable(dv.var); } dv.addNode(shaderNode); }
source.append(leftVariable.getName()); if (leftVariable.getMultiplicity() != null){ source.append("["); source.append(leftVariable.getName()); source.append("."); source.append(mapping.getLeftSwizzling()); source.append(namePrefix); source.append(rightVariable.getPrefix()); source.append(rightVariable.getName());
for (String param : params) { for (VariableMapping map : shaderNode.getInputMapping()) { if ((map.getLeftVariable().getName()).equals(param)) { if (map.getCondition() != null) { l = l.replaceAll(param, map.getCondition());
throw new MatParseException(right.getName() + " is not an output variable of " + definition.getName(), statement);
/** * Searches a 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; }
private void assertEqualsVariables(ShaderNodeVariable v1, ShaderNodeVariable v2) { assertEquals(v1.getName(), v2.getName()); assertEquals(v1.getNameSpace(), v2.getNameSpace()); assertEquals(v1.getMultiplicity(), v2.getMultiplicity()); assertEquals(v1.getType(), v2.getType()); assertEquals(v1.getCondition(), v2.getCondition()); }
/** * Stores a global output. * * @param var the variable to store. * @param varStatement the statement being read. * @throws MatParseException if we have duplicates of a global vertex output variable. */ public void storeGlobal(ShaderNodeVariable var, Statement varStatement) throws MatParseException { var.setShaderOutput(true); final ShaderGenerationInfo generationInfo = techniqueDef.getShaderGenerationInfo(); final ShaderNodeDefinition definition = shaderNode.getDefinition(); if (definition.getType() == ShaderType.Vertex) { ShaderNodeVariable global = generationInfo.getVertexGlobal(); if (global != null) { 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", varStatement); } } else { generationInfo.setVertexGlobal(var); } } else if (definition.getType() == ShaderType.Fragment) { storeVariable(var, generationInfo.getFragmentGlobals()); } }
/** * {@inheritDoc} * * attributes are all declared, inPosition is declared even if it's not in * the list and its condition is nulled. */ @Override protected void generateAttributes(StringBuilder source, ShaderGenerationInfo info) { source.append("\n"); boolean inPosition = false; for (ShaderNodeVariable var : info.getAttributes()) { if (var.getName().equals("inPosition")) { inPosition = true; var.setCondition(null); fixInPositionType(var); //keep track on the InPosition variable to avoid iterating through attributes again inPosTmp = var; } declareAttribute(source, var); } if (!inPosition) { inPosTmp = new ShaderNodeVariable("vec3", "inPosition"); declareAttribute(source, inPosTmp); } }
/** * updates the right variable of the given mapping from a UniformBinding (a * WorldParam) it checks if the uniform hasn't already been loaded, add it * to the maps if not. * * @param param the WorldParam UniformBinding * @param mapping the mapping * @param map the map of uniforms to search into * @return true if the param was added to the map */ protected boolean updateRightFromUniforms(UniformBinding param, VariableMapping mapping, Map<String, DeclaredVariable> map) { ShaderNodeVariable right = mapping.getRightVariable(); String name = param.toString(); DeclaredVariable dv = map.get(name); if (dv == null) { right.setType(param.getGlslType()); right.setName(name); right.setPrefix("g_"); dv = new DeclaredVariable(right); map.put(right.getName(), dv); dv.addNode(shaderNode); mapping.setRightVariable(right); return true; } dv.addNode(shaderNode); mapping.setRightVariable(dv.var); return false; }