Skip to content

Commit da5e4a1

Browse files
committed
ShaderNodes now move all the declared extensions at the top of the generated shader source
1 parent dd8271e commit da5e4a1

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

jme3-core/src/main/java/com/jme3/shader/ShaderGenerator.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.jme3.material.TechniqueDef;
3939
import com.jme3.shader.Shader.ShaderType;
4040
import java.util.List;
41+
import java.util.regex.*;
4142

4243
/**
4344
* This class is the base for a shader generator using the ShaderNodes system,
@@ -59,7 +60,11 @@ public abstract class ShaderGenerator {
5960
/**
6061
* the technique def to use for the shader generation
6162
*/
62-
protected TechniqueDef techniqueDef = null;
63+
protected TechniqueDef techniqueDef = null;
64+
/**
65+
* Extension pattern
66+
*/
67+
Pattern extensions = Pattern.compile("(#extension.*\\s+)");
6368

6469
/**
6570
* Build a shaderGenerator
@@ -142,7 +147,23 @@ protected String buildShader(List<ShaderNode> shaderNodes, ShaderGenerationInfo
142147

143148
sourceDeclaration.append(source);
144149

145-
return sourceDeclaration.toString();
150+
return moveExtensionsUp(sourceDeclaration);
151+
}
152+
153+
/**
154+
* parses the source and moves all the extensions at the top of the shader source as having extension declarations
155+
* in the middle of a shader is against the specs and not supported by all drivers.
156+
* @param sourceDeclaration
157+
* @return
158+
*/
159+
private String moveExtensionsUp(StringBuilder sourceDeclaration) {
160+
Matcher m = extensions.matcher( sourceDeclaration.toString());
161+
StringBuilder finalSource = new StringBuilder();
162+
while(m.find()){
163+
finalSource.append(m.group());
164+
}
165+
finalSource.append(m.replaceAll(""));
166+
return finalSource.toString();
146167
}
147168

148169
/**

0 commit comments

Comments
 (0)