Search in sources :

Example 1 with ShaderNode

use of com.jme3.shader.ShaderNode in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method updateRightFromUniforms.

/**
     * 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 = "g_" + param.toString();
    DeclaredVariable dv = map.get(name);
    if (dv == null) {
        right.setType(param.getGlslType());
        right.setName(name);
        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;
}
Also used : ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Example 2 with ShaderNode

use of com.jme3.shader.ShaderNode in project jmonkeyengine by jMonkeyEngine.

the class Glsl100ShaderGenerator method updateDefinesName.

/**
     * transforms defines name is the shader node code.
     * One can use a #if defined(inputVariableName) in a shaderNode code.
     * This method is responsible for changing the variable name with the 
     * appropriate defined based on the mapping condition of this variable.
     * Complex condition syntax are handled.     
     * 
     * @param nodeSource the sahderNode source code
     * @param shaderNode the ShaderNode being processed
     * @return the modified shaderNode source.
     */
protected String updateDefinesName(String nodeSource, ShaderNode shaderNode) {
    String[] lines = nodeSource.split("\\n");
    ConditionParser parser = new ConditionParser();
    for (String line : lines) {
        if (line.trim().startsWith("#if")) {
            List<String> params = parser.extractDefines(line.trim());
            //parser.getFormattedExpression();
            String l = line.trim().replaceAll("defined", "").replaceAll("#if ", "").replaceAll("#ifdef", "");
            boolean match = false;
            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());
                            match = true;
                        }
                    }
                }
            }
            if (match) {
                nodeSource = nodeSource.replace(line.trim(), "#if " + l);
            }
        }
    }
    return nodeSource;
}
Also used : ConditionParser(com.jme3.material.plugins.ConditionParser)

Example 3 with ShaderNode

use of com.jme3.shader.ShaderNode in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method updateRightFromUniforms.

/**
     * updates the right variable of the given mapping from a MatParam (a
     * WorldParam) it checks if the uniform hasn't already been loaded, add it
     * to the maps if not.
     *
     * @param param the MatParam
     * @param mapping the mapping
     * @param map the map of uniforms to search into
     * @return true if the param was added to the map
     */
public boolean updateRightFromUniforms(MatParam param, VariableMapping mapping, Map<String, DeclaredVariable> map, Statement statement) throws MatParseException {
    ShaderNodeVariable right = mapping.getRightVariable();
    DeclaredVariable dv = map.get(param.getPrefixedName());
    if (dv == null) {
        right.setType(param.getVarType().getGlslType());
        right.setName(param.getPrefixedName());
        if (mapping.getLeftVariable().getMultiplicity() != null) {
            if (!param.getVarType().name().endsWith("Array")) {
                throw new MatParseException(param.getName() + " is not of Array type", statement);
            }
            String multiplicity = mapping.getLeftVariable().getMultiplicity();
            try {
                Integer.parseInt(multiplicity);
            } catch (NumberFormatException nfe) {
                //multiplicity is not an int attempting to find for a material parameter.
                MatParam mp = findMatParam(multiplicity);
                if (mp != null) {
                    addDefine(multiplicity, VarType.Int);
                    multiplicity = multiplicity.toUpperCase();
                } else {
                    throw new MatParseException("Wrong multiplicity for variable" + mapping.getLeftVariable().getName() + ". " + multiplicity + " should be an int or a declared material parameter.", statement);
                }
            }
            right.setMultiplicity(multiplicity);
        }
        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;
}
Also used : MatParam(com.jme3.material.MatParam) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Example 4 with ShaderNode

use of com.jme3.shader.ShaderNode in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readNodes.

/**
     * Reads a list 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);
        }
    }
}
Also used : ShaderGenerationInfo(com.jme3.material.ShaderGenerationInfo) ShaderNode(com.jme3.shader.ShaderNode) Statement(com.jme3.util.blockparser.Statement)

Example 5 with ShaderNode

use of com.jme3.shader.ShaderNode in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readInputMapping.

/**
     * 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")) {
        //Globals are all vec4 for now (maybe forever...)
        right.setType("vec4");
        //        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;
}
Also used : UniformBinding(com.jme3.shader.UniformBinding) MatParam(com.jme3.material.MatParam) ShaderNode(com.jme3.shader.ShaderNode) VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable) IOException(java.io.IOException) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Aggregations

VariableMapping (com.jme3.shader.VariableMapping)4 ShaderNode (com.jme3.shader.ShaderNode)3 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)3 MatParam (com.jme3.material.MatParam)2 Statement (com.jme3.util.blockparser.Statement)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 ShaderGenerationInfo (com.jme3.material.ShaderGenerationInfo)1 ConditionParser (com.jme3.material.plugins.ConditionParser)1 ShaderNodeDefinition (com.jme3.shader.ShaderNodeDefinition)1 UniformBinding (com.jme3.shader.UniformBinding)1 IOException (java.io.IOException)1