diff --git a/jme3-core/src/plugins/java/com/jme3/shader/plugins/GLSLLoader.java b/jme3-core/src/plugins/java/com/jme3/shader/plugins/GLSLLoader.java index 544c0c048..f1ef39262 100644 --- a/jme3-core/src/plugins/java/com/jme3/shader/plugins/GLSLLoader.java +++ b/jme3-core/src/plugins/java/com/jme3/shader/plugins/GLSLLoader.java @@ -80,6 +80,7 @@ public class GLSLLoader implements AssetLoader { ShaderDependencyNode node = new ShaderDependencyNode(nodeName); StringBuilder sb = new StringBuilder(); + StringBuilder sbExt = new StringBuilder(); BufferedReader bufReader = null; try { bufReader = new BufferedReader(reader); @@ -108,6 +109,8 @@ public class GLSLLoader implements AssetLoader { node.addDependency(sb.length(), dependNode); } + } else if (ln.trim().startsWith("#extension ")) { + sbExt.append(ln).append('\n'); } else { sb.append(ln).append('\n'); } @@ -126,6 +129,7 @@ public class GLSLLoader implements AssetLoader { } node.setSource(sb.toString()); + node.setExtensions(sbExt.toString()); dependCache.put(nodeName, node); return node; } @@ -150,20 +154,24 @@ public class GLSLLoader implements AssetLoader { throw new IOException("Circular dependency."); } - - private String resolveDependencies(ShaderDependencyNode node, Set alreadyInjectedSet) { + + private String resolveDependencies(ShaderDependencyNode node, Set alreadyInjectedSet, StringBuilder extensions) { if (alreadyInjectedSet.contains(node)) { return "// " + node.getName() + " was already injected at the top.\n"; } else { alreadyInjectedSet.add(node); } + if (!node.getExtensions().isEmpty()) { + extensions.append(node.getExtensions()); + } if (node.getDependencies().isEmpty()) { return node.getSource(); } else { StringBuilder sb = new StringBuilder(node.getSource()); List resolvedShaderNodes = new ArrayList(); + for (ShaderDependencyNode dependencyNode : node.getDependencies()) { - resolvedShaderNodes.add( resolveDependencies(dependencyNode, alreadyInjectedSet) ); + resolvedShaderNodes.add(resolveDependencies(dependencyNode, alreadyInjectedSet, extensions)); } List injectIndices = node.getDependencyInjectIndices(); for (int i = resolvedShaderNodes.size() - 1; i >= 0; i--) { @@ -185,9 +193,11 @@ public class GLSLLoader implements AssetLoader { return reader; } else { ShaderDependencyNode rootNode = loadNode(reader, "[main]"); - String code = resolveDependencies(rootNode, new HashSet()); + StringBuilder extensions = new StringBuilder(); + String code = resolveDependencies(rootNode, new HashSet(), extensions); + extensions.append(code); dependCache.clear(); - return code; + return extensions.toString(); } } } diff --git a/jme3-core/src/plugins/java/com/jme3/shader/plugins/ShaderDependencyNode.java b/jme3-core/src/plugins/java/com/jme3/shader/plugins/ShaderDependencyNode.java index 6af93e30a..a4f371a9d 100644 --- a/jme3-core/src/plugins/java/com/jme3/shader/plugins/ShaderDependencyNode.java +++ b/jme3-core/src/plugins/java/com/jme3/shader/plugins/ShaderDependencyNode.java @@ -36,7 +36,8 @@ import java.util.Collections; import java.util.List; class ShaderDependencyNode { - + + private String extensions; private String shaderSource; private String shaderName; @@ -64,6 +65,14 @@ class ShaderDependencyNode { this.shaderName = shaderName; } + public String getExtensions() { + return extensions; + } + + public void setExtensions(String extensions) { + this.extensions = extensions; + } + public void addDependency(int index, ShaderDependencyNode node){ if (this.dependencies.contains(node)) { // already contains dependency ..