Skip to content

Commit 12a2f0f

Browse files
committed
One can now define several versions for the shader in a Technique in a J3md file, ie:
VertexShader GLSL150 GLSL110 : "path/to/shader/file.vert" FragmentShader GLSL150 GLSL110 : "path/to/shader/file.frag" Versions must be separated with spaces. They will be matched together when creating the technique so they have to follow the same order for different shaders.
1 parent 4777c59 commit 12a2f0f

File tree

6 files changed

+187
-51
lines changed

6 files changed

+187
-51
lines changed

jme3-core/src/main/java/com/jme3/material/ShaderGenerationInfo.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*
4949
* @author Nehon
5050
*/
51-
public class ShaderGenerationInfo implements Savable {
51+
public class ShaderGenerationInfo implements Savable, Cloneable {
5252

5353
/**
5454
* the list of attributes of the vertex shader
@@ -187,4 +187,36 @@ public void read(JmeImporter im) throws IOException {
187187
vertexGlobal = (ShaderNodeVariable) ic.readSavable("vertexGlobal", null);
188188

189189
}
190+
191+
@Override
192+
protected Object clone() throws CloneNotSupportedException {
193+
ShaderGenerationInfo clone = (ShaderGenerationInfo) super.clone();
194+
195+
for (ShaderNodeVariable attribute : attributes) {
196+
clone.attributes.add((ShaderNodeVariable) attribute.clone());
197+
}
198+
199+
for (ShaderNodeVariable uniform : vertexUniforms) {
200+
clone.vertexUniforms.add((ShaderNodeVariable) uniform.clone());
201+
}
202+
203+
clone.vertexGlobal = (ShaderNodeVariable) vertexGlobal.clone();
204+
205+
206+
for (ShaderNodeVariable varying : varyings) {
207+
clone.varyings.add((ShaderNodeVariable) varying.clone());
208+
}
209+
210+
for (ShaderNodeVariable uniform : fragmentUniforms) {
211+
clone.fragmentUniforms.add((ShaderNodeVariable) uniform.clone());
212+
}
213+
214+
for (ShaderNodeVariable globals : fragmentGlobals) {
215+
clone.fragmentGlobals.add((ShaderNodeVariable) globals.clone());
216+
}
217+
218+
clone.unusedNodes.addAll(unusedNodes);
219+
220+
return clone;
221+
}
190222
}

jme3-core/src/main/java/com/jme3/material/TechniqueDef.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939
import com.jme3.shader.Shader.ShaderType;
4040

4141
import java.io.IOException;
42+
import java.lang.reflect.InvocationTargetException;
4243
import java.util.*;
4344

4445
/**
4546
* Describes a technique definition.
4647
*
4748
* @author Kirill Vainer
4849
*/
49-
public class TechniqueDef implements Savable {
50+
public class TechniqueDef implements Savable, Cloneable {
5051

5152
/**
5253
* Version #1: Separate shader language for each shader source.
@@ -539,7 +540,7 @@ public Shader getShader(AssetManager assetManager, EnumSet<Caps> rendererCaps, D
539540
public void setShaderFile(EnumMap<Shader.ShaderType, String> shaderNames, EnumMap<Shader.ShaderType, String> shaderLanguages) {
540541
requiredCaps.clear();
541542

542-
int maxCap = 0;
543+
weight = 0;
543544
for (Shader.ShaderType shaderType : shaderNames.keySet()) {
544545
String language = shaderLanguages.get(shaderType);
545546
String shaderFile = shaderNames.get(shaderType);
@@ -549,15 +550,14 @@ public void setShaderFile(EnumMap<Shader.ShaderType, String> shaderNames, EnumMa
549550

550551
Caps cap = Caps.valueOf(language);
551552
requiredCaps.add(cap);
552-
maxCap = Math.max(maxCap, cap.ordinal());
553+
weight = Math.max(weight, cap.ordinal());
553554

554555
if (shaderType.equals(Shader.ShaderType.Geometry)) {
555556
requiredCaps.add(Caps.GeometryShader);
556557
} else if (shaderType.equals(Shader.ShaderType.TessellationControl)) {
557558
requiredCaps.add(Caps.TesselationShader);
558559
}
559560
}
560-
weight = maxCap;
561561
}
562562

563563
/**
@@ -776,4 +776,55 @@ public LightSpace getLightSpace() {
776776
public void setLightSpace(LightSpace lightSpace) {
777777
this.lightSpace = lightSpace;
778778
}
779+
780+
@Override
781+
public Object clone() throws CloneNotSupportedException {
782+
//cannot use super.clone because of the final fields instance that would be shared by the clones.
783+
TechniqueDef clone = new TechniqueDef(name, sortId);
784+
785+
clone.noRender = noRender;
786+
clone.lightMode = lightMode;
787+
clone.shadowMode = shadowMode;
788+
clone.lightSpace = lightSpace;
789+
clone.usesNodes = usesNodes;
790+
clone.shaderPrologue = shaderPrologue;
791+
792+
clone.setShaderFile(shaderNames, shaderLanguages);
793+
794+
clone.defineNames = new ArrayList<>(defineNames.size());
795+
clone.defineNames.addAll(defineNames);
796+
797+
clone.defineTypes = new ArrayList<>(defineTypes.size());
798+
clone.defineTypes.addAll(defineTypes);
799+
800+
clone.paramToDefineId = new HashMap<>(paramToDefineId.size());
801+
clone.paramToDefineId.putAll(paramToDefineId);
802+
803+
if (shaderNodes != null) {
804+
for (ShaderNode shaderNode : shaderNodes) {
805+
clone.shaderNodes.add((ShaderNode) shaderNode.clone());
806+
}
807+
clone.shaderGenerationInfo = (ShaderGenerationInfo) shaderGenerationInfo.clone();
808+
}
809+
810+
if (renderState != null) {
811+
clone.setRenderState(renderState.clone());
812+
}
813+
if (forcedRenderState != null) {
814+
clone.setForcedRenderState(forcedRenderState.clone());
815+
}
816+
817+
try {
818+
clone.logic = logic.getClass().getConstructor(TechniqueDef.class).newInstance(clone);
819+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
820+
e.printStackTrace();
821+
}
822+
823+
if (worldBinds != null) {
824+
clone.worldBinds = new ArrayList<>(worldBinds.size());
825+
clone.worldBinds.addAll(worldBinds);
826+
}
827+
828+
return clone;
829+
}
779830
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
*
5555
* @author Nehon
5656
*/
57-
public class ShaderNode implements Savable {
57+
public class ShaderNode implements Savable, Cloneable {
5858

5959
private String name;
6060
private ShaderNodeDefinition definition;
@@ -212,4 +212,24 @@ public void read(JmeImporter im) throws IOException {
212212
public String toString() {
213213
return "\nShaderNode{" + "\nname=" + name + ", \ndefinition=" + definition.getName() + ", \ncondition=" + condition + ", \ninputMapping=" + inputMapping + ", \noutputMapping=" + outputMapping + '}';
214214
}
215+
216+
@Override
217+
public Object clone() throws CloneNotSupportedException {
218+
ShaderNode clone = (ShaderNode) super.clone();
219+
220+
//No need to clone the definition.
221+
clone.definition = definition;
222+
223+
clone.inputMapping = new ArrayList<>();
224+
for (VariableMapping variableMapping : inputMapping) {
225+
clone.inputMapping.add((VariableMapping) variableMapping.clone());
226+
}
227+
228+
clone.outputMapping = new ArrayList<>();
229+
for (VariableMapping variableMapping : outputMapping) {
230+
clone.outputMapping.add((VariableMapping) variableMapping.clone());
231+
}
232+
233+
return clone;
234+
}
215235
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,6 @@ public String toString() {
243243
return "\n" + type + ' ' + (nameSpace != null ? (nameSpace + '.') : "") + name;
244244
}
245245

246-
@Override
247-
public ShaderNodeVariable clone() {
248-
return new ShaderNodeVariable(type, nameSpace, name);
249-
}
250-
251246
/**
252247
*
253248
* @return true if this variable is a shader output
@@ -281,6 +276,9 @@ public String getMultiplicity() {
281276
public void setMultiplicity(String multiplicity) {
282277
this.multiplicity = multiplicity;
283278
}
284-
285-
279+
280+
@Override
281+
public Object clone() throws CloneNotSupportedException {
282+
return super.clone();
283+
}
286284
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*
4444
* @author Nehon
4545
*/
46-
public class VariableMapping implements Savable {
46+
public class VariableMapping implements Savable, Cloneable {
4747

4848
private ShaderNodeVariable leftVariable;
4949
private ShaderNodeVariable rightVariable;
@@ -195,4 +195,14 @@ public void read(JmeImporter im) throws IOException {
195195
public String toString() {
196196
return "\n{" + leftVariable.toString() + (leftSwizzling.length() > 0 ? ("." + leftSwizzling) : "") + " = " + rightVariable.getType() + " " + rightVariable.getNameSpace() + "." + rightVariable.getName() + (rightSwizzling.length() > 0 ? ("." + rightSwizzling) : "") + " : " + condition + "}";
197197
}
198+
199+
@Override
200+
protected Object clone() throws CloneNotSupportedException {
201+
VariableMapping clone = (VariableMapping) super.clone();
202+
203+
clone.leftVariable = (ShaderNodeVariable) leftVariable.clone();
204+
clone.rightVariable = (ShaderNodeVariable) rightVariable.clone();
205+
206+
return clone;
207+
}
198208
}

0 commit comments

Comments
 (0)