Search in sources :

Example 1 with UniformBinding

use of com.jme3.shader.UniformBinding 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 UniformBinding

use of com.jme3.shader.UniformBinding 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)

Example 3 with UniformBinding

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

the class TechniqueDef method loadShader.

private Shader loadShader(AssetManager assetManager, EnumSet<Caps> rendererCaps, DefineList defines) {
    StringBuilder sb = new StringBuilder();
    sb.append(shaderPrologue);
    defines.generateSource(sb, defineNames, defineTypes);
    String definesSourceCode = sb.toString();
    Shader shader;
    if (isUsingShaderNodes()) {
        ShaderGenerator shaderGenerator = assetManager.getShaderGenerator(rendererCaps);
        if (shaderGenerator == null) {
            throw new UnsupportedOperationException("ShaderGenerator was not initialized, " + "make sure assetManager.getGenerator(caps) has been called");
        }
        shaderGenerator.initialize(this);
        shader = shaderGenerator.generateShader(definesSourceCode);
    } else {
        shader = new Shader();
        for (ShaderType type : ShaderType.values()) {
            String language = shaderLanguages.get(type);
            String shaderSourceAssetName = shaderNames.get(type);
            if (language == null || shaderSourceAssetName == null) {
                continue;
            }
            String shaderSourceCode = (String) assetManager.loadAsset(shaderSourceAssetName);
            shader.addSource(type, shaderSourceAssetName, shaderSourceCode, definesSourceCode, language);
        }
    }
    if (getWorldBindings() != null) {
        for (UniformBinding binding : getWorldBindings()) {
            shader.addUniformBinding(binding);
        }
    }
    return shader;
}
Also used : ShaderType(com.jme3.shader.Shader.ShaderType)

Aggregations

ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 MatParam (com.jme3.material.MatParam)1 ShaderType (com.jme3.shader.Shader.ShaderType)1 ShaderNode (com.jme3.shader.ShaderNode)1 UniformBinding (com.jme3.shader.UniformBinding)1 VariableMapping (com.jme3.shader.VariableMapping)1 IOException (java.io.IOException)1