From da5e4a18c78b1df91dd89831a9d3fa76138cb8f3 Mon Sep 17 00:00:00 2001 From: Nehon Date: Sun, 9 Oct 2016 20:12:39 +0200 Subject: [PATCH] ShaderNodes now move all the declared extensions at the top of the generated shader source --- .../java/com/jme3/shader/ShaderGenerator.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/shader/ShaderGenerator.java b/jme3-core/src/main/java/com/jme3/shader/ShaderGenerator.java index d42dccf17..518639e14 100644 --- a/jme3-core/src/main/java/com/jme3/shader/ShaderGenerator.java +++ b/jme3-core/src/main/java/com/jme3/shader/ShaderGenerator.java @@ -38,6 +38,7 @@ import com.jme3.material.Technique; import com.jme3.material.TechniqueDef; import com.jme3.shader.Shader.ShaderType; import java.util.List; +import java.util.regex.*; /** * This class is the base for a shader generator using the ShaderNodes system, @@ -59,7 +60,11 @@ public abstract class ShaderGenerator { /** * the technique def to use for the shader generation */ - protected TechniqueDef techniqueDef = null; + protected TechniqueDef techniqueDef = null; + /** + * Extension pattern + */ + Pattern extensions = Pattern.compile("(#extension.*\\s+)"); /** * Build a shaderGenerator @@ -142,7 +147,23 @@ public abstract class ShaderGenerator { sourceDeclaration.append(source); - return sourceDeclaration.toString(); + return moveExtensionsUp(sourceDeclaration); + } + + /** + * parses the source and moves all the extensions at the top of the shader source as having extension declarations + * in the middle of a shader is against the specs and not supported by all drivers. + * @param sourceDeclaration + * @return + */ + private String moveExtensionsUp(StringBuilder sourceDeclaration) { + Matcher m = extensions.matcher( sourceDeclaration.toString()); + StringBuilder finalSource = new StringBuilder(); + while(m.find()){ + finalSource.append(m.group()); + } + finalSource.append(m.replaceAll("")); + return finalSource.toString(); } /**