Search in sources :

Example 1 with TechniqueDef

use of com.jme3.material.TechniqueDef in project jmonkeyengine by jMonkeyEngine.

the class Material method preload.

/**
     * Preloads this material for the given render manager.
     * <p>
     * Preloading the material can ensure that when the material is first
     * used for rendering, there won't be any delay since the material has
     * been already been setup for rendering.
     *
     * @param renderManager The render manager to preload for
     */
public void preload(RenderManager renderManager) {
    if (technique == null) {
        selectTechnique(TechniqueDef.DEFAULT_TECHNIQUE_NAME, renderManager);
    }
    TechniqueDef techniqueDef = technique.getDef();
    Renderer renderer = renderManager.getRenderer();
    EnumSet<Caps> rendererCaps = renderer.getCaps();
    if (techniqueDef.isNoRender()) {
        return;
    }
    Shader shader = technique.makeCurrent(renderManager, null, null, null, rendererCaps);
    updateShaderMaterialParameters(renderer, shader, null, null);
    renderManager.getRenderer().setShader(shader);
}
Also used : Renderer(com.jme3.renderer.Renderer) Shader(com.jme3.shader.Shader) Caps(com.jme3.renderer.Caps)

Example 2 with TechniqueDef

use of com.jme3.material.TechniqueDef in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readTechnique.

private void readTechnique(Statement techStat) throws IOException {
    isUseNodes = false;
    String[] split = techStat.getLine().split(whitespacePattern);
    Cloner cloner = new Cloner();
    String name;
    if (split.length == 1) {
        name = TechniqueDef.DEFAULT_TECHNIQUE_NAME;
    } else if (split.length == 2) {
        name = split[1];
    } else {
        throw new IOException("Technique statement syntax incorrect");
    }
    String techniqueUniqueName = materialDef.getAssetName() + "@" + name;
    technique = new TechniqueDef(name, techniqueUniqueName.hashCode());
    for (Statement statement : techStat.getContents()) {
        readTechniqueStatement(statement);
    }
    technique.setShaderPrologue(createShaderPrologue(presetDefines));
    switch(technique.getLightMode()) {
        case Disable:
            technique.setLogic(new DefaultTechniqueDefLogic(technique));
            break;
        case MultiPass:
            technique.setLogic(new MultiPassLightingLogic(technique));
            break;
        case SinglePass:
            technique.setLogic(new SinglePassLightingLogic(technique));
            break;
        case StaticPass:
            technique.setLogic(new StaticPassLightingLogic(technique));
            break;
        case SinglePassAndImageBased:
            technique.setLogic(new SinglePassAndImageBasedLightingLogic(technique));
            break;
        default:
            throw new UnsupportedOperationException();
    }
    List<TechniqueDef> techniqueDefs = new ArrayList<>();
    if (isUseNodes) {
        nodesLoaderDelegate.computeConditions();
        //used for caching later, the shader here is not a file.
        // KIRILL 9/19/2015
        // Not sure if this is needed anymore, since shader caching
        // is now done by TechniqueDef.
        technique.setShaderFile(technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100");
        techniqueDefs.add(technique);
    } else if (shaderNames.containsKey(Shader.ShaderType.Vertex) && shaderNames.containsKey(Shader.ShaderType.Fragment)) {
        if (shaderLanguages.size() > 1) {
            for (int i = 1; i < shaderLanguages.size(); i++) {
                cloner.clearIndex();
                TechniqueDef td = cloner.clone(technique);
                td.setShaderFile(shaderNames, shaderLanguages.get(i));
                techniqueDefs.add(td);
            }
        }
        technique.setShaderFile(shaderNames, shaderLanguages.get(0));
        techniqueDefs.add(technique);
    } else {
        technique = null;
        shaderLanguages.clear();
        shaderNames.clear();
        presetDefines.clear();
        langSize = 0;
        logger.log(Level.WARNING, "Fixed function technique was ignored");
        logger.log(Level.WARNING, "Fixed function technique ''{0}'' was ignored for material {1}", new Object[] { name, key });
        return;
    }
    for (TechniqueDef techniqueDef : techniqueDefs) {
        materialDef.addTechniqueDef(techniqueDef);
    }
    technique = null;
    langSize = 0;
    shaderLanguages.clear();
    shaderNames.clear();
    presetDefines.clear();
}
Also used : Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException) StaticPassLightingLogic(com.jme3.material.logic.StaticPassLightingLogic) Cloner(com.jme3.util.clone.Cloner)

Example 3 with TechniqueDef

use of com.jme3.material.TechniqueDef in project jmonkeyengine by jMonkeyEngine.

the class ShaderGenerator method generateShader.

/**
     * Generate vertex and fragment shaders for the given technique
     *
     * @return a Shader program
     */
public Shader generateShader(String definesSourceCode) {
    if (techniqueDef == null) {
        throw new UnsupportedOperationException("The shaderGenerator was not " + "properly initialized, call " + "initialize(TechniqueDef) before any generation");
    }
    String techniqueName = techniqueDef.getName();
    ShaderGenerationInfo info = techniqueDef.getShaderGenerationInfo();
    Shader shader = new Shader();
    for (ShaderType type : ShaderType.values()) {
        String extension = type.getExtension();
        String language = getLanguageAndVersion(type);
        String shaderSourceCode = buildShader(techniqueDef.getShaderNodes(), info, type);
        if (shaderSourceCode != null) {
            String shaderSourceAssetName = techniqueName + "." + extension;
            shader.addSource(type, shaderSourceAssetName, shaderSourceCode, definesSourceCode, language);
        }
    }
    techniqueDef = null;
    return shader;
}
Also used : ShaderGenerationInfo(com.jme3.material.ShaderGenerationInfo) ShaderType(com.jme3.shader.Shader.ShaderType)

Example 4 with TechniqueDef

use of com.jme3.material.TechniqueDef 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)

Example 5 with TechniqueDef

use of com.jme3.material.TechniqueDef in project jmonkeyengine by jMonkeyEngine.

the class MaterialMatParamTest method outDefines.

private void outDefines(Define... expectedDefinesArray) {
    StringBuilder expectedDefineSource = new StringBuilder();
    for (Define define : expectedDefinesArray) {
        expectedDefineSource.append(define.toString());
    }
    if (!evaluated) {
        evaluateTechniqueDef();
    }
    Material mat = geometry.getMaterial();
    Technique tech = mat.getActiveTechnique();
    TechniqueDef def = tech.getDef();
    DefineList actualDefines = tech.getDynamicDefines();
    String[] defineNames = def.getDefineNames();
    VarType[] defineTypes = def.getDefineTypes();
    String actualDefineSource = actualDefines.generateSource(Arrays.asList(defineNames), Arrays.asList(defineTypes));
    assertEquals(expectedDefineSource.toString(), actualDefineSource);
}
Also used : DefineList(com.jme3.shader.DefineList) VarType(com.jme3.shader.VarType)

Aggregations

Caps (com.jme3.renderer.Caps)3 Shader (com.jme3.shader.Shader)3 Renderer (com.jme3.renderer.Renderer)2 DefineList (com.jme3.shader.DefineList)2 MaterialDef (com.jme3.material.MaterialDef)1 ShaderGenerationInfo (com.jme3.material.ShaderGenerationInfo)1 TechniqueDef (com.jme3.material.TechniqueDef)1 StaticPassLightingLogic (com.jme3.material.logic.StaticPassLightingLogic)1 J3mdExporter (com.jme3.material.plugin.export.materialdef.J3mdExporter)1 J3MLoader (com.jme3.material.plugins.J3MLoader)1 ShaderType (com.jme3.shader.Shader.ShaderType)1 VarType (com.jme3.shader.VarType)1 Statement (com.jme3.util.blockparser.Statement)1 Cloner (com.jme3.util.clone.Cloner)1 IOException (java.io.IOException)1