Search in sources :

Example 26 with Shader

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

the class ShaderNodeLoaderDelegate method readShaderNodeDefinition.

/**
     * effectively reads the ShaderNodesDefinitions block
     *
     * @param statements the list of statements to parse
     * @param key the ShaderNodeDefinitionKey
     * @throws IOException
     */
protected void readShaderNodeDefinition(List<Statement> statements, ShaderNodeDefinitionKey key) throws IOException {
    boolean isLoadDoc = key instanceof ShaderNodeDefinitionKey && ((ShaderNodeDefinitionKey) key).isLoadDocumentation();
    for (Statement statement : statements) {
        String[] split = statement.getLine().split("[ \\{]");
        String line = statement.getLine();
        if (line.startsWith("Type")) {
            String type = line.substring(line.lastIndexOf(':') + 1).trim();
            shaderNodeDefinition.setType(Shader.ShaderType.valueOf(type));
        } else if (line.startsWith("Shader ")) {
            readShaderStatement(statement);
            shaderNodeDefinition.getShadersLanguage().add(shaderLanguage);
            shaderNodeDefinition.getShadersPath().add(shaderName);
        } else if (line.startsWith("Documentation")) {
            if (isLoadDoc) {
                String doc = "";
                for (Statement statement1 : statement.getContents()) {
                    doc += "\n" + statement1.getLine();
                }
                shaderNodeDefinition.setDocumentation(doc);
            }
        } else if (line.startsWith("Input")) {
            varNames = "";
            for (Statement statement1 : statement.getContents()) {
                shaderNodeDefinition.getInputs().add(readVariable(statement1));
            }
        } else if (line.startsWith("Output")) {
            varNames = "";
            for (Statement statement1 : statement.getContents()) {
                if (statement1.getLine().trim().equals("None")) {
                    shaderNodeDefinition.setNoOutput(true);
                } else {
                    shaderNodeDefinition.getOutputs().add(readVariable(statement1));
                }
            }
        } else {
            throw new MatParseException("one of Type, Shader, Documentation, Input, Output", split[0], statement);
        }
    }
}
Also used : ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) Statement(com.jme3.util.blockparser.Statement)

Example 27 with Shader

use of com.jme3.shader.Shader 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 28 with Shader

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

the class ShaderNodeLoaderDelegate method findDefinition.

/**
     * find the definition from this statement (loads it if necessary)
     *
     * @param statement the statement being read
     * @return the definition
     * @throws IOException
     */
public ShaderNodeDefinition findDefinition(Statement statement) throws IOException {
    String[] defLine = statement.getLine().split(":");
    String defName = defLine[1].trim();
    ShaderNodeDefinition def = getNodeDefinitions().get(defName);
    if (def == null) {
        if (defLine.length == 3) {
            List<ShaderNodeDefinition> defs = null;
            try {
                defs = assetManager.loadAsset(new ShaderNodeDefinitionKey(defLine[2].trim()));
            } catch (AssetNotFoundException e) {
                throw new MatParseException("Couldn't find " + defLine[2].trim(), statement, e);
            }
            for (ShaderNodeDefinition definition : defs) {
                definition.setPath(defLine[2].trim());
                if (defName.equals(definition.getName())) {
                    def = definition;
                }
                if (!(getNodeDefinitions().containsKey(definition.getName()))) {
                    getNodeDefinitions().put(definition.getName(), definition);
                }
            }
        }
        if (def == null) {
            throw new MatParseException(defName + " is not a declared as Shader Node Definition", statement);
        }
    }
    return def;
}
Also used : ShaderNodeDefinitionKey(com.jme3.asset.ShaderNodeDefinitionKey) ShaderNodeDefinition(com.jme3.shader.ShaderNodeDefinition) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Example 29 with Shader

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

the class ShaderNodeLoaderDelegate method storeVaryings.

/**
     * updates a variable condition form a mapping condition
     *
     * @param var the variable
     * @param mapping the mapping
     */
//    public void updateCondition(ShaderNodeVariable var, VariableMapping mapping) {
//
//        String condition = mergeConditions(shaderNode.getCondition(), mapping.getCondition(), "&&");
//
//        if (var.getCondition() == null) {
//            if (!nulledConditions.contains(var.getNameSpace() + "." + var.getName())) {
//                var.setCondition(condition);
//            }
//        } else {
//            var.setCondition(mergeConditions(var.getCondition(), condition, "||"));
//            if (var.getCondition() == null) {
//                nulledConditions.add(var.getNameSpace() + "." + var.getName());
//            }
//        }
//    }
/**
     * 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);
            }
        }
    }
}
Also used : VariableMapping(com.jme3.shader.VariableMapping)

Example 30 with Shader

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

the class ShaderCheck method checkMatDef.

private static void checkMatDef(String matdefName) {
    MaterialDef def = (MaterialDef) assetManager.loadAsset(matdefName);
    EnumSet<Caps> rendererCaps = EnumSet.noneOf(Caps.class);
    rendererCaps.add(Caps.GLSL100);
    for (TechniqueDef techDef : def.getTechniqueDefs(TechniqueDef.DEFAULT_TECHNIQUE_NAME)) {
        DefineList defines = techDef.createDefineList();
        Shader shader = techDef.getShader(assetManager, rendererCaps, defines);
        for (Validator validator : validators) {
            StringBuilder sb = new StringBuilder();
            validator.validate(shader, sb);
            System.out.println("==== Validator: " + validator.getName() + " " + validator.getInstalledVersion() + " ====");
            System.out.println(sb.toString());
        }
    }
    throw new UnsupportedOperationException();
}
Also used : DefineList(com.jme3.shader.DefineList) MaterialDef(com.jme3.material.MaterialDef) Shader(com.jme3.shader.Shader) Caps(com.jme3.renderer.Caps) TechniqueDef(com.jme3.material.TechniqueDef)

Aggregations

Uniform (com.jme3.shader.Uniform)8 Renderer (com.jme3.renderer.Renderer)6 Caps (com.jme3.renderer.Caps)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 SpotLight (com.jme3.light.SpotLight)4 Material (com.jme3.material.Material)4 Vector3f (com.jme3.math.Vector3f)4 Shader (com.jme3.shader.Shader)4 IOException (java.io.IOException)4 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)3 Light (com.jme3.light.Light)3 ColorRGBA (com.jme3.math.ColorRGBA)3 ShaderType (com.jme3.shader.Shader.ShaderType)3 VarType (com.jme3.shader.VarType)3 TempVars (com.jme3.util.TempVars)3 Statement (com.jme3.util.blockparser.Statement)3 AssetLoadException (com.jme3.asset.AssetLoadException)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)2 MatParam (com.jme3.material.MatParam)2