Search in sources :

Example 16 with Statement

use of com.jme3.util.blockparser.Statement 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 17 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readOutputMapping.

/**
     * reads an output mapping
     *
     * @param statement1 the statement being read
     * @return the mapping
     * @throws IOException
     */
public VariableMapping readOutputMapping(Statement statement1) throws IOException {
    VariableMapping mapping = null;
    try {
        mapping = parseMapping(statement1, new boolean[] { true, false });
    } catch (Exception e) {
        throw new MatParseException("Unexpected mapping format", statement1, e);
    }
    ShaderNodeVariable left = mapping.getLeftVariable();
    ShaderNodeVariable right = mapping.getRightVariable();
    if (left.getType().startsWith("sampler") || right.getType().startsWith("sampler")) {
        throw new MatParseException("Samplers can only be inputs", statement1);
    }
    if (left.getNameSpace().equals("Global")) {
        //Globals are all vec4 for now (maybe forever...)
        left.setType("vec4");
        storeGlobal(left, statement1);
    } else {
        throw new MatParseException("Only Global nameSpace is allowed for outputMapping, got" + left.getNameSpace(), statement1);
    }
    if (!updateVariableFromList(right, shaderNode.getDefinition().getOutputs())) {
        throw new MatParseException(right.getName() + " is not an output variable of " + shaderNode.getDefinition().getName(), statement1);
    }
    checkTypes(mapping, statement1);
    return mapping;
}
Also used : VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable) IOException(java.io.IOException) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Example 18 with Statement

use of com.jme3.util.blockparser.Statement 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 19 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readShaderNode.

/**
     * reads a list of ShaderNode{} blocks
     *
     * @param statements the list of statements to parse
     * @throws IOException
     */
protected void readShaderNode(List<Statement> statements) throws IOException {
    for (Statement statement : statements) {
        String line = statement.getLine();
        String[] split = statement.getLine().split("[ \\{]");
        if (line.startsWith("Definition")) {
            ShaderNodeDefinition def = findDefinition(statement);
            shaderNode.setDefinition(def);
            if (def.isNoOutput()) {
                techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(shaderNode.getName());
            }
        } else if (line.startsWith("Condition")) {
            String condition = line.substring(line.lastIndexOf(":") + 1).trim();
            extractCondition(condition, statement);
            shaderNode.setCondition(conditionParser.getFormattedExpression());
        } else if (line.startsWith("InputMapping")) {
            for (Statement statement1 : statement.getContents()) {
                VariableMapping mapping = readInputMapping(statement1);
                techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(mapping.getRightVariable().getNameSpace());
                shaderNode.getInputMapping().add(mapping);
            }
        } else if (line.startsWith("OutputMapping")) {
            for (Statement statement1 : statement.getContents()) {
                VariableMapping mapping = readOutputMapping(statement1);
                techniqueDef.getShaderGenerationInfo().getUnusedNodes().remove(shaderNode.getName());
                shaderNode.getOutputMapping().add(mapping);
            }
        } else {
            throw new MatParseException("ShaderNodeDefinition", split[0], statement);
        }
    }
}
Also used : Statement(com.jme3.util.blockparser.Statement) VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeDefinition(com.jme3.shader.ShaderNodeDefinition)

Example 20 with Statement

use of com.jme3.util.blockparser.Statement in project jmonkeyengine by jMonkeyEngine.

the class GLRenderer method setupTextureParams.

@SuppressWarnings("fallthrough")
private void setupTextureParams(int unit, Texture tex) {
    Image image = tex.getImage();
    int target = convertTextureType(tex.getType(), image != null ? image.getMultiSamples() : 1, -1);
    boolean haveMips = true;
    if (image != null) {
        haveMips = image.isGeneratedMipmapsRequired() || image.hasMipmaps();
    }
    LastTextureState curState = image.getLastTextureState();
    if (curState.magFilter != tex.getMagFilter()) {
        bindTextureAndUnit(target, image, unit);
        gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, convertMagFilter(tex.getMagFilter()));
        curState.magFilter = tex.getMagFilter();
    }
    if (curState.minFilter != tex.getMinFilter()) {
        bindTextureAndUnit(target, image, unit);
        gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, convertMinFilter(tex.getMinFilter(), haveMips));
        curState.minFilter = tex.getMinFilter();
    }
    int desiredAnisoFilter = tex.getAnisotropicFilter() == 0 ? defaultAnisotropicFilter : tex.getAnisotropicFilter();
    if (caps.contains(Caps.TextureFilterAnisotropic) && curState.anisoFilter != desiredAnisoFilter) {
        bindTextureAndUnit(target, image, unit);
        gl.glTexParameterf(target, GLExt.GL_TEXTURE_MAX_ANISOTROPY_EXT, desiredAnisoFilter);
        curState.anisoFilter = desiredAnisoFilter;
    }
    switch(tex.getType()) {
        case ThreeDimensional:
        case // cubemaps use 3D coords
        CubeMap:
            if (gl2 != null && curState.rWrap != tex.getWrap(WrapAxis.R)) {
                bindTextureAndUnit(target, image, unit);
                gl2.glTexParameteri(target, GL2.GL_TEXTURE_WRAP_R, convertWrapMode(tex.getWrap(WrapAxis.R)));
                curState.rWrap = tex.getWrap(WrapAxis.R);
            }
        //There is no break statement on purpose here
        case TwoDimensional:
        case TwoDimensionalArray:
            if (curState.tWrap != tex.getWrap(WrapAxis.T)) {
                bindTextureAndUnit(target, image, unit);
                gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_T, convertWrapMode(tex.getWrap(WrapAxis.T)));
                image.getLastTextureState().tWrap = tex.getWrap(WrapAxis.T);
            }
            if (curState.sWrap != tex.getWrap(WrapAxis.S)) {
                bindTextureAndUnit(target, image, unit);
                gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_S, convertWrapMode(tex.getWrap(WrapAxis.S)));
                curState.sWrap = tex.getWrap(WrapAxis.S);
            }
            break;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + tex.getType());
    }
    ShadowCompareMode texCompareMode = tex.getShadowCompareMode();
    if (gl2 != null && curState.shadowCompareMode != texCompareMode) {
        bindTextureAndUnit(target, image, unit);
        if (texCompareMode != ShadowCompareMode.Off) {
            gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_REF_TO_TEXTURE);
            if (texCompareMode == ShadowCompareMode.GreaterOrEqual) {
                gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_GEQUAL);
            } else {
                gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_LEQUAL);
            }
        } else {
            gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL.GL_NONE);
        }
        curState.shadowCompareMode = texCompareMode;
    }
    // If at this point we didn't bind the texture, bind it now
    bindTextureOnly(target, image, unit);
}
Also used : ShadowCompareMode(com.jme3.texture.Texture.ShadowCompareMode) LastTextureState(com.jme3.texture.image.LastTextureState) Image(com.jme3.texture.Image)

Aggregations

Statement (com.jme3.util.blockparser.Statement)16 IOException (java.io.IOException)10 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)4 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)4 VariableMapping (com.jme3.shader.VariableMapping)4 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)3 Material (com.jme3.material.Material)3 MaterialList (com.jme3.material.MaterialList)3 ShaderNodeDefinition (com.jme3.shader.ShaderNodeDefinition)3 MatParam (com.jme3.material.MatParam)2 ShaderNode (com.jme3.shader.ShaderNode)2 Image (com.jme3.texture.Image)2 Texture2D (com.jme3.texture.Texture2D)2 InputStream (java.io.InputStream)2 AssetKey (com.jme3.asset.AssetKey)1 AssetLoadException (com.jme3.asset.AssetLoadException)1 TextureKey (com.jme3.asset.TextureKey)1 ShaderGenerationInfo (com.jme3.material.ShaderGenerationInfo)1 LightMode (com.jme3.material.TechniqueDef.LightMode)1 ShadowMode (com.jme3.material.TechniqueDef.ShadowMode)1