/** * {@inheritDoc} */ @Override public ShaderGenerator getShaderGenerator(EnumSet<Caps> caps) { if (shaderGenerator == null) { if(caps.contains(Caps.GLSL150)){ shaderGenerator = new Glsl150ShaderGenerator(this); }else{ shaderGenerator = new Glsl100ShaderGenerator(this); } } return shaderGenerator; }
/** * {@inheritDoc} * * Shader outputs are declared and initialized inside the main section */ @Override protected void generateStartOfMainSection(StringBuilder source, ShaderGenerationInfo info, ShaderType type) { source.append("\n"); source.append("void main() {\n"); indent(); appendIndent(source); if (type == ShaderType.Vertex) { declareGlobalPosition(info, source); } else if (type == ShaderType.Fragment) { for (ShaderNodeVariable global : info.getFragmentGlobals()) { declareVariable(source, global, "vec4(1.0)"); } } source.append("\n"); }
source.append("\n"); if (type == ShaderType.Vertex) { appendOutput(source, "gl_Position", info.getVertexGlobal()); } else if (type == ShaderType.Fragment) { List<ShaderNodeVariable> globals = info.getFragmentGlobals(); if (globals.size() == 1) { appendOutput(source, "gl_FragColor", globals.get(0)); } else { int i = 0; appendOutput(source, "gl_FragData[" + i + "]", global); i++; unIndent(); appendIndent(source); source.append("}\n");
/** * Ends a conditional block * @param condition the block condition * @param source the StringBuilder to use */ protected void endCondition(String condition, StringBuilder source) { if (condition != null) { unIndent(); appendIndent(source); source.append("#endif\n"); } }
/** * Starts a conditional block * @param condition the block condition * @param source the StringBuilder to use */ protected void startCondition(String condition, StringBuilder source) { if (condition != null) { appendIndent(source); source.append("#if "); source.append(condition); source.append("\n"); indent(); } }
nodeSource = updateDefinesName(nodeSource, shaderNode); source.append("\n"); comment(source, shaderNode, "Begin"); startCondition(shaderNode.getCondition(), source); if (!isVarying(info, variable)) { declareVariable(source, variable, var.getDefaultValue(), true, null); nodeSource = replaceVariableName(nodeSource, variable); declaredInputs.add(fullName); if (rightVariable != null && isWorldOrMaterialParam(rightVariable) && rightVariable.getType().startsWith("sampler")) { nodeSource = replace(nodeSource, leftVariable, rightVariable.getPrefix() + rightVariable.getName()); } else { map(mapping, source, !isDeclared); nodeSource = replace(nodeSource, leftVariable, newName); declaredInputs.add(newName); ShaderNodeVariable v = new ShaderNodeVariable(var.getType(), shaderNode.getName(), var.getName(), var.getMultiplicity()); if (!declaredInputs.contains(shaderNode.getName() + "_" + var.getName())) { if (!isVarying(info, v)) { declareVariable(source, v); nodeSource = replaceVariableName(nodeSource, v); map(mapping, source, true);
/** * {@inheritDoc} * * if the declaration contains no code nothing is done, else it's appended */ @Override protected void generateDeclarativeSection(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) { if (nodeSource.replaceAll("\\n", "").trim().length() > 0) { nodeSource = updateDefinesName(nodeSource, shaderNode); source.append("\n"); unIndent(); startCondition(shaderNode.getCondition(), source); source.append(nodeSource); source.append("\n"); endCondition(shaderNode.getCondition(), source); indent(); } }
final String rightExpression = mapping.getRightExpression(); startCondition(mapping.getCondition(), source); appendIndent(source); if (!leftVariable.isShaderOutput() && declare) { source.append(leftVariable.getType()); source.append(leftVariable.getType()); source.append("(0.0);\n"); appendIndent(source); String namePrefix = getAppendableNameSpace(rightVariable); source.append(namePrefix); source.append(rightVariable.getPrefix()); endCondition(mapping.getCondition(), source);
startCondition(var.getCondition(), source); appendIndent(source); if (modifier != null) { source.append(modifier); endCondition(var.getCondition(), source);
/** * declares a variable, embed in a conditional block if needed * @param source the StringBuilder to use * @param var the variable to declare * @param appendNameSpace true to append the nameSpace + "_" */ protected void declareVariable(StringBuilder source, ShaderNodeVariable var, boolean appendNameSpace) { declareVariable(source, var, appendNameSpace, null); }
/** * {@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); } }
/** * Appends a comment to the generated code * @param source the StringBuilder to use * @param shaderNode the shader node being processed (to append its name) * @param comment the comment to append */ protected void comment(StringBuilder source, ShaderNode shaderNode, String comment) { appendIndent(source); source.append("//"); source.append(shaderNode.getName()); source.append(" : "); source.append(comment); source.append("\n"); }
@Override protected void generateVaryings(StringBuilder source, ShaderGenerationInfo info, ShaderType type) { source.append("\n"); for (ShaderNodeVariable var : info.getVaryings()) { declareVarying(source, var, type != ShaderType.Vertex); } }
nodeSource = updateDefinesName(nodeSource, shaderNode); source.append("\n"); comment(source, shaderNode, "Begin"); startCondition(shaderNode.getCondition(), source); if (isWorldOrMaterialParam(rightVariable) && rightVariable.getType().startsWith("sampler")) { nodeSource = replace(nodeSource, leftVariable, rightVariable.getPrefix() + rightVariable.getName()); } else { map(mapping, source); nodeSource = replace(nodeSource, leftVariable, newName); declaredInputs.add(newName); if (!isVarying(info, variable)) { declareVariable(source, variable, var.getDefaultValue(), true, null); nodeSource = replaceVariableName(nodeSource, variable); declaredInputs.add(fullName); ShaderNodeVariable v = new ShaderNodeVariable(var.getType(), shaderNode.getName(), var.getName(), var.getMultiplicity()); if (!declaredInputs.contains(shaderNode.getName() + "_" + var.getName())) { if (!isVarying(info, v)) { declareVariable(source, v); nodeSource = replaceVariableName(nodeSource, v); map(mapping, source);
/** * {@inheritDoc} * * if the declaration contains no code nothing is done, else it's appended */ @Override protected void generateDeclarativeSection(StringBuilder source, ShaderNode shaderNode, String nodeSource, ShaderGenerationInfo info) { if (nodeSource.replaceAll("\\n", "").trim().length() > 0) { nodeSource = updateDefinesName(nodeSource, shaderNode); source.append("\n"); unIndent(); startCondition(shaderNode.getCondition(), source); source.append(nodeSource); source.append("\n"); endCondition(shaderNode.getCondition(), source); indent(); } }
startCondition(mapping.getCondition(), source); appendIndent(source); if (!mapping.getLeftVariable().isShaderOutput()) { source.append(mapping.getLeftVariable().getType()); source.append(mapping.getLeftVariable().getType()); source.append("(0.0);\n"); appendIndent(source); String namePrefix = getAppendableNameSpace(mapping.getRightVariable()); source.append(namePrefix); source.append(mapping.getRightVariable().getPrefix()); endCondition(mapping.getCondition(), source);
startCondition(var.getCondition(), source); appendIndent(source); if (modifier != null) { source.append(modifier); endCondition(var.getCondition(), source);
/** * Declares an attribute * @param source the StringBuilder to use * @param var the variable to declare as an attribute */ protected void declareAttribute(StringBuilder source, ShaderNodeVariable var) { declareVariable(source, var, false, "attribute"); }
/** * Ends a conditional block * @param condition the block condition * @param source the StringBuilder to use */ protected void endCondition(String condition, StringBuilder source) { if (condition != null) { unIndent(); appendIndent(source); source.append("#endif\n"); } }
/** * Starts a conditional block * @param condition the block condition * @param source the StringBuilder to use */ protected void startCondition(String condition, StringBuilder source) { if (condition != null) { appendIndent(source); source.append("#if "); source.append(condition); source.append("\n"); indent(); } }