Fixed uploading of all shader stages

experimental
michael 10 years ago
parent 46a48f466d
commit 1ad8ff154c
  1. 13
      jme3-core/src/main/java/com/jme3/asset/DesktopAssetManager.java
  2. 22
      jme3-core/src/main/java/com/jme3/asset/cache/WeakRefCloneAssetCache.java
  3. 13
      jme3-core/src/main/java/com/jme3/shader/ShaderKey.java
  4. 262
      jme3-core/src/plugins/java/com/jme3/material/plugins/J3MLoader.java
  5. 3
      jme3-testdata/src/main/resources/Materials/Geom/SimpleGeom.frag
  6. 19
      jme3-testdata/src/main/resources/Materials/Geom/SimpleGeom.geom
  7. 4
      jme3-testdata/src/main/resources/Materials/Geom/SimpleGeom.j3m
  8. 17
      jme3-testdata/src/main/resources/Materials/Geom/SimpleGeom.j3md
  9. 5
      jme3-testdata/src/main/resources/Materials/Geom/SimpleGeom.vert

@ -393,6 +393,7 @@ public class DesktopAssetManager implements AssetManager {
public Shader loadShader(ShaderKey key){ public Shader loadShader(ShaderKey key){
// cache abuse in method // cache abuse in method
// that doesn't use loaders/locators // that doesn't use loaders/locators
System.out.println();
AssetCache cache = handler.getCache(SimpleAssetCache.class); AssetCache cache = handler.getCache(SimpleAssetCache.class);
Shader shader = (Shader) cache.getFromCache(key); Shader shader = (Shader) cache.getFromCache(key);
if (shader == null){ if (shader == null){
@ -402,17 +403,11 @@ public class DesktopAssetManager implements AssetManager {
} }
shader = shaderGenerator.generateShader(); shader = shaderGenerator.generateShader();
} else { } else {
String vertName = key.getVertName();
String fragName = key.getFragName();
String vertSource = (String) loadAsset(new AssetKey(vertName));
String fragSource = (String) loadAsset(new AssetKey(fragName));
shader = new Shader(); shader = new Shader();
shader.initialize(); shader.initialize();
shader.addSource(Shader.ShaderType.Vertex, vertName, vertSource, key.getDefines().getCompiled(), key.getVertexShaderLanguage()); for (Shader.ShaderType shaderType : key.getUsedShaderPrograms()) {
shader.addSource(Shader.ShaderType.Fragment, fragName, fragSource, key.getDefines().getCompiled(), key.getFragmentShaderLanguage()); shader.addSource(shaderType,key.getShaderProgramName(shaderType),(String) loadAsset(new AssetKey(key.getShaderProgramName(shaderType))),key.getDefines().getCompiled(),key.getShaderProgramLanguage(shaderType));
}
} }
cache.addToCache(key, shader); cache.addToCache(key, shader);

@ -33,6 +33,7 @@ package com.jme3.asset.cache;
import com.jme3.asset.AssetKey; import com.jme3.asset.AssetKey;
import com.jme3.asset.CloneableSmartAsset; import com.jme3.asset.CloneableSmartAsset;
import java.lang.ref.PhantomReference; import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue; import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -48,7 +49,7 @@ import java.util.logging.Logger;
* when all clones of the original asset are collected, will cause the * when all clones of the original asset are collected, will cause the
* asset to be automatically removed from the cache. * asset to be automatically removed from the cache.
* *
* @author Kirill Vainer * @author Kirill Vainer
*/ */
public class WeakRefCloneAssetCache implements AssetCache { public class WeakRefCloneAssetCache implements AssetCache {
@ -102,16 +103,16 @@ public class WeakRefCloneAssetCache implements AssetCache {
} }
}; };
private void removeCollectedAssets(){ private void removeCollectedAssets() {
int removedAssets = 0; int removedAssets = 0;
for (KeyRef ref; (ref = (KeyRef)refQueue.poll()) != null;){ for (KeyRef ref; (ref = (KeyRef) refQueue.poll()) != null; ) {
// (Cannot use ref.get() since it was just collected by GC!) // (Cannot use ref.get() since it was just collected by GC!)
AssetKey key = ref.clonedKey; AssetKey key = ref.clonedKey;
// Asset was collected, note that at this point the asset cache // Asset was collected, note that at this point the asset cache
// might not even have this asset anymore, it is OK. // might not even have this asset anymore, it is OK.
if (smartCache.remove(key) != null){ if (smartCache.remove(key) != null) {
removedAssets ++; removedAssets++;
//System.out.println("WeakRefAssetCache: The asset " + ref.assetKey + " was purged from the cache"); //System.out.println("WeakRefAssetCache: The asset " + ref.assetKey + " was purged from the cache");
} }
} }
@ -123,7 +124,6 @@ public class WeakRefCloneAssetCache implements AssetCache {
public <T> void addToCache(AssetKey<T> originalKey, T obj) { public <T> void addToCache(AssetKey<T> originalKey, T obj) {
// Make room for new asset // Make room for new asset
removeCollectedAssets(); removeCollectedAssets();
CloneableSmartAsset asset = (CloneableSmartAsset) obj; CloneableSmartAsset asset = (CloneableSmartAsset) obj;
// No circular references, since the original asset is // No circular references, since the original asset is
@ -146,7 +146,7 @@ public class WeakRefCloneAssetCache implements AssetCache {
public <T> void registerAssetClone(AssetKey<T> key, T clone) { public <T> void registerAssetClone(AssetKey<T> key, T clone) {
ArrayList<AssetKey> loadStack = assetLoadStack.get(); ArrayList<AssetKey> loadStack = assetLoadStack.get();
((CloneableSmartAsset)clone).setKey(loadStack.remove(loadStack.size() - 1)); ((CloneableSmartAsset) clone).setKey(loadStack.remove(loadStack.size() - 1));
} }
public void notifyNoAssetClone() { public void notifyNoAssetClone() {
@ -156,7 +156,7 @@ public class WeakRefCloneAssetCache implements AssetCache {
public <T> T getFromCache(AssetKey<T> key) { public <T> T getFromCache(AssetKey<T> key) {
AssetRef smartInfo; AssetRef smartInfo;
synchronized (smartCache){ synchronized (smartCache) {
smartInfo = smartCache.get(key); smartInfo = smartCache.get(key);
} }
@ -167,7 +167,7 @@ public class WeakRefCloneAssetCache implements AssetCache {
// can check this and determine that the asset clone // can check this and determine that the asset clone
// belongs to the asset retrieved here. // belongs to the asset retrieved here.
AssetKey keyForTheClone = smartInfo.get(); AssetKey keyForTheClone = smartInfo.get();
if (keyForTheClone == null){ if (keyForTheClone == null) {
// The asset was JUST collected by GC // The asset was JUST collected by GC
// (between here and smartCache.get) // (between here and smartCache.get)
return null; return null;
@ -185,7 +185,7 @@ public class WeakRefCloneAssetCache implements AssetCache {
public boolean deleteFromCache(AssetKey key) { public boolean deleteFromCache(AssetKey key) {
ArrayList<AssetKey> loadStack = assetLoadStack.get(); ArrayList<AssetKey> loadStack = assetLoadStack.get();
if (!loadStack.isEmpty()){ if (!loadStack.isEmpty()) {
throw new UnsupportedOperationException("Cache cannot be modified" throw new UnsupportedOperationException("Cache cannot be modified"
+ "while assets are being loaded"); + "while assets are being loaded");
} }
@ -196,7 +196,7 @@ public class WeakRefCloneAssetCache implements AssetCache {
public void clearCache() { public void clearCache() {
ArrayList<AssetKey> loadStack = assetLoadStack.get(); ArrayList<AssetKey> loadStack = assetLoadStack.get();
if (!loadStack.isEmpty()){ if (!loadStack.isEmpty()) {
throw new UnsupportedOperationException("Cache cannot be modified" throw new UnsupportedOperationException("Cache cannot be modified"
+ "while assets are being loaded"); + "while assets are being loaded");
} }

@ -38,6 +38,7 @@ import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule; import com.jme3.export.OutputCapsule;
import java.io.IOException; import java.io.IOException;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Set;
public class ShaderKey extends AssetKey<Shader> { public class ShaderKey extends AssetKey<Shader> {
@ -141,6 +142,18 @@ public class ShaderKey extends AssetKey<Shader> {
this.usesShaderNodes = usesShaderNodes; this.usesShaderNodes = usesShaderNodes;
} }
public Set<Shader.ShaderType> getUsedShaderPrograms(){
return shaderName.keySet();
}
public String getShaderProgramLanguage(Shader.ShaderType shaderType){
return shaderLanguage.get(shaderType);
}
public String getShaderProgramName(Shader.ShaderType shaderType){
return shaderName.get(shaderType);
}
@Override @Override
public void write(JmeExporter ex) throws IOException{ public void write(JmeExporter ex) throws IOException{
super.write(ex); super.write(ex);

@ -49,6 +49,7 @@ import com.jme3.texture.image.ColorSpace;
import com.jme3.util.PlaceholderAssets; import com.jme3.util.PlaceholderAssets;
import com.jme3.util.blockparser.BlockLanguageParser; import com.jme3.util.blockparser.BlockLanguageParser;
import com.jme3.util.blockparser.Statement; import com.jme3.util.blockparser.Statement;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.EnumMap; import java.util.EnumMap;
@ -71,14 +72,14 @@ public class J3MLoader implements AssetLoader {
private TechniqueDef technique; private TechniqueDef technique;
private RenderState renderState; private RenderState renderState;
private EnumMap<Shader.ShaderType,String> shaderLanguage; private EnumMap<Shader.ShaderType, String> shaderLanguage;
private EnumMap<Shader.ShaderType,String> shaderName; private EnumMap<Shader.ShaderType, String> shaderName;
private static final String whitespacePattern = "\\p{javaWhitespace}+"; private static final String whitespacePattern = "\\p{javaWhitespace}+";
public J3MLoader(){ public J3MLoader() {
shaderLanguage=new EnumMap<Shader.ShaderType, String>(Shader.ShaderType.class); shaderLanguage = new EnumMap<Shader.ShaderType, String>(Shader.ShaderType.class);
shaderName=new EnumMap<Shader.ShaderType, String>(Shader.ShaderType.class); shaderName = new EnumMap<Shader.ShaderType, String>(Shader.ShaderType.class);
} }
@ -94,21 +95,21 @@ public class J3MLoader implements AssetLoader {
} }
for (Shader.ShaderType shaderType : Shader.ShaderType.values()) { for (Shader.ShaderType shaderType : Shader.ShaderType.values()) {
if(typeAndLang[0].equals(shaderType.toString()+"Shader")){ if (typeAndLang[0].equals(shaderType.toString() + "Shader")) {
readShaderDefinition(shaderType,split[1].trim(),typeAndLang[1]); readShaderDefinition(shaderType, split[1].trim(), typeAndLang[1]);
} }
} }
} }
private void readShaderDefinition(Shader.ShaderType shaderType,String name,String language){ private void readShaderDefinition(Shader.ShaderType shaderType, String name, String language) {
shaderName.put(shaderType,name); shaderName.put(shaderType, name);
shaderLanguage.put(shaderType,language); shaderLanguage.put(shaderType, language);
} }
// LightMode <MODE> // LightMode <MODE>
private void readLightMode(String statement) throws IOException{ private void readLightMode(String statement) throws IOException {
String[] split = statement.split(whitespacePattern); String[] split = statement.split(whitespacePattern);
if (split.length != 2){ if (split.length != 2) {
throw new IOException("LightMode statement syntax incorrect"); throw new IOException("LightMode statement syntax incorrect");
} }
LightMode lm = LightMode.valueOf(split[1]); LightMode lm = LightMode.valueOf(split[1]);
@ -116,29 +117,29 @@ public class J3MLoader implements AssetLoader {
} }
// ShadowMode <MODE> // ShadowMode <MODE>
private void readShadowMode(String statement) throws IOException{ private void readShadowMode(String statement) throws IOException {
String[] split = statement.split(whitespacePattern); String[] split = statement.split(whitespacePattern);
if (split.length != 2){ if (split.length != 2) {
throw new IOException("ShadowMode statement syntax incorrect"); throw new IOException("ShadowMode statement syntax incorrect");
} }
ShadowMode sm = ShadowMode.valueOf(split[1]); ShadowMode sm = ShadowMode.valueOf(split[1]);
technique.setShadowMode(sm); technique.setShadowMode(sm);
} }
private Object readValue(VarType type, String value) throws IOException{ private Object readValue(VarType type, String value) throws IOException {
if (type.isTextureType()){ if (type.isTextureType()) {
// String texturePath = readString("[\n;(//)(\\})]"); // String texturePath = readString("[\n;(//)(\\})]");
String texturePath = value.trim(); String texturePath = value.trim();
boolean flipY = false; boolean flipY = false;
boolean repeat = false; boolean repeat = false;
if (texturePath.startsWith("Flip Repeat ")){ if (texturePath.startsWith("Flip Repeat ")) {
texturePath = texturePath.substring(12).trim(); texturePath = texturePath.substring(12).trim();
flipY = true; flipY = true;
repeat = true; repeat = true;
}else if (texturePath.startsWith("Flip ")){ } else if (texturePath.startsWith("Flip ")) {
texturePath = texturePath.substring(5).trim(); texturePath = texturePath.substring(5).trim();
flipY = true; flipY = true;
}else if (texturePath.startsWith("Repeat ")){ } else if (texturePath.startsWith("Repeat ")) {
texturePath = texturePath.substring(7).trim(); texturePath = texturePath.substring(7).trim();
repeat = true; repeat = true;
} }
@ -160,45 +161,45 @@ public class J3MLoader implements AssetLoader {
Texture tex; Texture tex;
try { try {
tex = assetManager.loadTexture(texKey); tex = assetManager.loadTexture(texKey);
} catch (AssetNotFoundException ex){ } catch (AssetNotFoundException ex) {
logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[]{texKey, key}); logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[]{texKey, key});
tex = null; tex = null;
} }
if (tex != null){ if (tex != null) {
if (repeat){ if (repeat) {
tex.setWrap(WrapMode.Repeat); tex.setWrap(WrapMode.Repeat);
} }
}else{ } else {
tex = new Texture2D(PlaceholderAssets.getPlaceholderImage(assetManager)); tex = new Texture2D(PlaceholderAssets.getPlaceholderImage(assetManager));
if (repeat){ if (repeat) {
tex.setWrap(WrapMode.Repeat); tex.setWrap(WrapMode.Repeat);
} }
tex.setKey(texKey); tex.setKey(texKey);
} }
return tex; return tex;
}else{ } else {
String[] split = value.trim().split(whitespacePattern); String[] split = value.trim().split(whitespacePattern);
switch (type){ switch (type) {
case Float: case Float:
if (split.length != 1){ if (split.length != 1) {
throw new IOException("Float value parameter must have 1 entry: " + value); throw new IOException("Float value parameter must have 1 entry: " + value);
} }
return Float.parseFloat(split[0]); return Float.parseFloat(split[0]);
case Vector2: case Vector2:
if (split.length != 2){ if (split.length != 2) {
throw new IOException("Vector2 value parameter must have 2 entries: " + value); throw new IOException("Vector2 value parameter must have 2 entries: " + value);
} }
return new Vector2f(Float.parseFloat(split[0]), return new Vector2f(Float.parseFloat(split[0]),
Float.parseFloat(split[1])); Float.parseFloat(split[1]));
case Vector3: case Vector3:
if (split.length != 3){ if (split.length != 3) {
throw new IOException("Vector3 value parameter must have 3 entries: " + value); throw new IOException("Vector3 value parameter must have 3 entries: " + value);
} }
return new Vector3f(Float.parseFloat(split[0]), return new Vector3f(Float.parseFloat(split[0]),
Float.parseFloat(split[1]), Float.parseFloat(split[1]),
Float.parseFloat(split[2])); Float.parseFloat(split[2]));
case Vector4: case Vector4:
if (split.length != 4){ if (split.length != 4) {
throw new IOException("Vector4 value parameter must have 4 entries: " + value); throw new IOException("Vector4 value parameter must have 4 entries: " + value);
} }
return new ColorRGBA(Float.parseFloat(split[0]), return new ColorRGBA(Float.parseFloat(split[0]),
@ -206,30 +207,30 @@ public class J3MLoader implements AssetLoader {
Float.parseFloat(split[2]), Float.parseFloat(split[2]),
Float.parseFloat(split[3])); Float.parseFloat(split[3]));
case Int: case Int:
if (split.length != 1){ if (split.length != 1) {
throw new IOException("Int value parameter must have 1 entry: " + value); throw new IOException("Int value parameter must have 1 entry: " + value);
} }
return Integer.parseInt(split[0]); return Integer.parseInt(split[0]);
case Boolean: case Boolean:
if (split.length != 1){ if (split.length != 1) {
throw new IOException("Boolean value parameter must have 1 entry: " + value); throw new IOException("Boolean value parameter must have 1 entry: " + value);
} }
return Boolean.parseBoolean(split[0]); return Boolean.parseBoolean(split[0]);
default: default:
throw new UnsupportedOperationException("Unknown type: "+type); throw new UnsupportedOperationException("Unknown type: " + type);
} }
} }
} }
// <TYPE> <NAME> [ "(" <FFBINDING> ")" ] [ ":" <DEFAULTVAL> ] [-LINEAR] // <TYPE> <NAME> [ "(" <FFBINDING> ")" ] [ ":" <DEFAULTVAL> ] [-LINEAR]
private void readParam(String statement) throws IOException{ private void readParam(String statement) throws IOException {
String name; String name;
String defaultVal = null; String defaultVal = null;
ColorSpace colorSpace = null; ColorSpace colorSpace = null;
String[] split = statement.split("-"); String[] split = statement.split("-");
if(split.length>1){ if (split.length > 1) {
if(split[1].equalsIgnoreCase("LINEAR")){ if (split[1].equalsIgnoreCase("LINEAR")) {
colorSpace = ColorSpace.Linear; colorSpace = ColorSpace.Linear;
} }
statement = split[0].trim(); statement = split[0].trim();
@ -238,10 +239,10 @@ public class J3MLoader implements AssetLoader {
split = statement.split(":"); split = statement.split(":");
// Parse default val // Parse default val
if (split.length == 1){ if (split.length == 1) {
// Doesn't contain default value // Doesn't contain default value
}else{ } else {
if (split.length != 2){ if (split.length != 2) {
throw new IOException("Parameter statement syntax incorrect"); throw new IOException("Parameter statement syntax incorrect");
} }
statement = split[0].trim(); statement = split[0].trim();
@ -250,137 +251,137 @@ public class J3MLoader implements AssetLoader {
// Parse ffbinding // Parse ffbinding
int startParen = statement.indexOf("("); int startParen = statement.indexOf("(");
if (startParen != -1){ if (startParen != -1) {
// get content inside parentheses // get content inside parentheses
int endParen = statement.indexOf(")", startParen); int endParen = statement.indexOf(")", startParen);
String bindingStr = statement.substring(startParen+1, endParen).trim(); String bindingStr = statement.substring(startParen + 1, endParen).trim();
// don't care about bindingStr // don't care about bindingStr
statement = statement.substring(0, startParen); statement = statement.substring(0, startParen);
} }
// Parse type + name // Parse type + name
split = statement.split(whitespacePattern); split = statement.split(whitespacePattern);
if (split.length != 2){ if (split.length != 2) {
throw new IOException("Parameter statement syntax incorrect"); throw new IOException("Parameter statement syntax incorrect");
} }
VarType type; VarType type;
if (split[0].equals("Color")){ if (split[0].equals("Color")) {
type = VarType.Vector4; type = VarType.Vector4;
}else{ } else {
type = VarType.valueOf(split[0]); type = VarType.valueOf(split[0]);
} }
name = split[1]; name = split[1];
Object defaultValObj = null; Object defaultValObj = null;
if (defaultVal != null){ if (defaultVal != null) {
defaultValObj = readValue(type, defaultVal); defaultValObj = readValue(type, defaultVal);
} }
if(type.isTextureType()){ if (type.isTextureType()) {
materialDef.addMaterialParamTexture(type, name, colorSpace); materialDef.addMaterialParamTexture(type, name, colorSpace);
}else{ } else {
materialDef.addMaterialParam(type, name, defaultValObj); materialDef.addMaterialParam(type, name, defaultValObj);
} }
} }
private void readValueParam(String statement) throws IOException{ private void readValueParam(String statement) throws IOException {
// Use limit=1 incase filename contains colons // Use limit=1 incase filename contains colons
String[] split = statement.split(":", 2); String[] split = statement.split(":", 2);
if (split.length != 2){ if (split.length != 2) {
throw new IOException("Value parameter statement syntax incorrect"); throw new IOException("Value parameter statement syntax incorrect");
} }
String name = split[0].trim(); String name = split[0].trim();
// parse value // parse value
MatParam p = material.getMaterialDef().getMaterialParam(name); MatParam p = material.getMaterialDef().getMaterialParam(name);
if (p == null){ if (p == null) {
throw new IOException("The material parameter: "+name+" is undefined."); throw new IOException("The material parameter: " + name + " is undefined.");
} }
Object valueObj = readValue(p.getVarType(), split[1]); Object valueObj = readValue(p.getVarType(), split[1]);
if (p.getVarType().isTextureType()){ if (p.getVarType().isTextureType()) {
material.setTextureParam(name, p.getVarType(), (Texture) valueObj); material.setTextureParam(name, p.getVarType(), (Texture) valueObj);
}else{ } else {
material.setParam(name, p.getVarType(), valueObj); material.setParam(name, p.getVarType(), valueObj);
} }
} }
private void readMaterialParams(List<Statement> paramsList) throws IOException{ private void readMaterialParams(List<Statement> paramsList) throws IOException {
for (Statement statement : paramsList){ for (Statement statement : paramsList) {
readParam(statement.getLine()); readParam(statement.getLine());
} }
} }
private void readExtendingMaterialParams(List<Statement> paramsList) throws IOException{ private void readExtendingMaterialParams(List<Statement> paramsList) throws IOException {
for (Statement statement : paramsList){ for (Statement statement : paramsList) {
readValueParam(statement.getLine()); readValueParam(statement.getLine());
} }
} }
private void readWorldParams(List<Statement> worldParams) throws IOException{ private void readWorldParams(List<Statement> worldParams) throws IOException {
for (Statement statement : worldParams){ for (Statement statement : worldParams) {
technique.addWorldParam(statement.getLine()); technique.addWorldParam(statement.getLine());
} }
} }
private boolean parseBoolean(String word){ private boolean parseBoolean(String word) {
return word != null && word.equals("On"); return word != null && word.equals("On");
} }
private void readRenderStateStatement(Statement statement) throws IOException{ private void readRenderStateStatement(Statement statement) throws IOException {
String[] split = statement.getLine().split(whitespacePattern); String[] split = statement.getLine().split(whitespacePattern);
if (split[0].equals("Wireframe")){ if (split[0].equals("Wireframe")) {
renderState.setWireframe(parseBoolean(split[1])); renderState.setWireframe(parseBoolean(split[1]));
}else if (split[0].equals("FaceCull")){ } else if (split[0].equals("FaceCull")) {
renderState.setFaceCullMode(FaceCullMode.valueOf(split[1])); renderState.setFaceCullMode(FaceCullMode.valueOf(split[1]));
}else if (split[0].equals("DepthWrite")){ } else if (split[0].equals("DepthWrite")) {
renderState.setDepthWrite(parseBoolean(split[1])); renderState.setDepthWrite(parseBoolean(split[1]));
}else if (split[0].equals("DepthTest")){ } else if (split[0].equals("DepthTest")) {
renderState.setDepthTest(parseBoolean(split[1])); renderState.setDepthTest(parseBoolean(split[1]));
}else if (split[0].equals("Blend")){ } else if (split[0].equals("Blend")) {
renderState.setBlendMode(BlendMode.valueOf(split[1])); renderState.setBlendMode(BlendMode.valueOf(split[1]));
}else if (split[0].equals("AlphaTestFalloff")){ } else if (split[0].equals("AlphaTestFalloff")) {
renderState.setAlphaTest(true); renderState.setAlphaTest(true);
renderState.setAlphaFallOff(Float.parseFloat(split[1])); renderState.setAlphaFallOff(Float.parseFloat(split[1]));
}else if (split[0].equals("PolyOffset")){ } else if (split[0].equals("PolyOffset")) {
float factor = Float.parseFloat(split[1]); float factor = Float.parseFloat(split[1]);
float units = Float.parseFloat(split[2]); float units = Float.parseFloat(split[2]);
renderState.setPolyOffset(factor, units); renderState.setPolyOffset(factor, units);
}else if (split[0].equals("ColorWrite")){ } else if (split[0].equals("ColorWrite")) {
renderState.setColorWrite(parseBoolean(split[1])); renderState.setColorWrite(parseBoolean(split[1]));
}else if (split[0].equals("PointSprite")){ } else if (split[0].equals("PointSprite")) {
renderState.setPointSprite(parseBoolean(split[1])); renderState.setPointSprite(parseBoolean(split[1]));
}else if (split[0].equals("DepthFunc")){ } else if (split[0].equals("DepthFunc")) {
renderState.setDepthFunc(RenderState.TestFunction.valueOf(split[1])); renderState.setDepthFunc(RenderState.TestFunction.valueOf(split[1]));
}else if (split[0].equals("AlphaFunc")){ } else if (split[0].equals("AlphaFunc")) {
renderState.setAlphaFunc(RenderState.TestFunction.valueOf(split[1])); renderState.setAlphaFunc(RenderState.TestFunction.valueOf(split[1]));
} else { } else {
throw new MatParseException(null, split[0], statement); throw new MatParseException(null, split[0], statement);
} }
} }
private void readAdditionalRenderState(List<Statement> renderStates) throws IOException{ private void readAdditionalRenderState(List<Statement> renderStates) throws IOException {
renderState = material.getAdditionalRenderState(); renderState = material.getAdditionalRenderState();
for (Statement statement : renderStates){ for (Statement statement : renderStates) {
readRenderStateStatement(statement); readRenderStateStatement(statement);
} }
renderState = null; renderState = null;
} }
private void readRenderState(List<Statement> renderStates) throws IOException{ private void readRenderState(List<Statement> renderStates) throws IOException {
renderState = new RenderState(); renderState = new RenderState();
for (Statement statement : renderStates){ for (Statement statement : renderStates) {
readRenderStateStatement(statement); readRenderStateStatement(statement);
} }
technique.setRenderState(renderState); technique.setRenderState(renderState);
renderState = null; renderState = null;
} }
private void readForcedRenderState(List<Statement> renderStates) throws IOException{ private void readForcedRenderState(List<Statement> renderStates) throws IOException {
renderState = new RenderState(); renderState = new RenderState();
for (Statement statement : renderStates){ for (Statement statement : renderStates) {
readRenderStateStatement(statement); readRenderStateStatement(statement);
} }
technique.setForcedRenderState(renderState); technique.setForcedRenderState(renderState);
@ -388,41 +389,44 @@ public class J3MLoader implements AssetLoader {
} }
// <DEFINENAME> [ ":" <PARAMNAME> ] // <DEFINENAME> [ ":" <PARAMNAME> ]
private void readDefine(String statement) throws IOException{ private void readDefine(String statement) throws IOException {
String[] split = statement.split(":"); String[] split = statement.split(":");
if (split.length == 1){ if (split.length == 1) {
// add preset define // add preset define
technique.addShaderPresetDefine(split[0].trim(), VarType.Boolean, true); technique.addShaderPresetDefine(split[0].trim(), VarType.Boolean, true);
}else if (split.length == 2){ } else if (split.length == 2) {
technique.addShaderParamDefine(split[1].trim(), split[0].trim()); technique.addShaderParamDefine(split[1].trim(), split[0].trim());
}else{ } else {
throw new IOException("Define syntax incorrect"); throw new IOException("Define syntax incorrect");
} }
} }
private void readDefines(List<Statement> defineList) throws IOException{ private void readDefines(List<Statement> defineList) throws IOException {
for (Statement statement : defineList){ for (Statement statement : defineList) {
readDefine(statement.getLine()); readDefine(statement.getLine());
} }
} }
private void readTechniqueStatement(Statement statement) throws IOException{ private void readTechniqueStatement(Statement statement) throws IOException {
String[] split = statement.getLine().split("[ \\{]"); String[] split = statement.getLine().split("[ \\{]");
if (split[0].equals("VertexShader") || if (split[0].equals("VertexShader") ||
split[0].equals("FragmentShader")){ split[0].equals("FragmentShader") ||
split[0].equals("GeometryShader") ||
split[0].equals("TesselationControlShader") ||
split[0].equals("TesselationEvaluationShader")) {
readShaderStatement(statement.getLine()); readShaderStatement(statement.getLine());
}else if (split[0].equals("LightMode")){ } else if (split[0].equals("LightMode")) {
readLightMode(statement.getLine()); readLightMode(statement.getLine());
}else if (split[0].equals("ShadowMode")){ } else if (split[0].equals("ShadowMode")) {
readShadowMode(statement.getLine()); readShadowMode(statement.getLine());
}else if (split[0].equals("WorldParameters")){ } else if (split[0].equals("WorldParameters")) {
readWorldParams(statement.getContents()); readWorldParams(statement.getContents());
}else if (split[0].equals("RenderState")){ } else if (split[0].equals("RenderState")) {
readRenderState(statement.getContents()); readRenderState(statement.getContents());
}else if (split[0].equals("ForcedRenderState")){ } else if (split[0].equals("ForcedRenderState")) {
readForcedRenderState(statement.getContents()); readForcedRenderState(statement.getContents());
}else if (split[0].equals("Defines")){ } else if (split[0].equals("Defines")) {
readDefines(statement.getContents()); readDefines(statement.getContents());
} else if (split[0].equals("ShaderNodesDefinitions")) { } else if (split[0].equals("ShaderNodesDefinitions")) {
initNodesLoader(); initNodesLoader();
@ -444,15 +448,15 @@ public class J3MLoader implements AssetLoader {
} }
} }
private void readTransparentStatement(String statement) throws IOException{ private void readTransparentStatement(String statement) throws IOException {
String[] split = statement.split(whitespacePattern); String[] split = statement.split(whitespacePattern);
if (split.length != 2){ if (split.length != 2) {
throw new IOException("Transparent statement syntax incorrect"); throw new IOException("Transparent statement syntax incorrect");
} }
material.setTransparent(parseBoolean(split[1])); material.setTransparent(parseBoolean(split[1]));
} }
private void readTechnique(Statement techStat) throws IOException{ private void readTechnique(Statement techStat) throws IOException {
isUseNodes = false; isUseNodes = false;
String[] split = techStat.getLine().split(whitespacePattern); String[] split = techStat.getLine().split(whitespacePattern);
if (split.length == 1) { if (split.length == 1) {
@ -464,18 +468,18 @@ public class J3MLoader implements AssetLoader {
throw new IOException("Technique statement syntax incorrect"); throw new IOException("Technique statement syntax incorrect");
} }
for (Statement statement : techStat.getContents()){ for (Statement statement : techStat.getContents()) {
readTechniqueStatement(statement); readTechniqueStatement(statement);
} }
if(isUseNodes){ if (isUseNodes) {
nodesLoaderDelegate.computeConditions(); nodesLoaderDelegate.computeConditions();
//used for caching later, the shader here is not a file. //used for caching later, the shader here is not a file.
technique.setShaderFile(technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100"); technique.setShaderFile(technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100");
} }
if(shaderName.containsKey(Shader.ShaderType.Vertex) && shaderName.containsKey(Shader.ShaderType.Fragment)){ if (shaderName.containsKey(Shader.ShaderType.Vertex) && shaderName.containsKey(Shader.ShaderType.Fragment)) {
technique.setShaderFile(shaderName,shaderLanguage); technique.setShaderFile(shaderName, shaderLanguage);
} }
materialDef.addTechniqueDef(technique); materialDef.addTechniqueDef(technique);
@ -484,40 +488,40 @@ public class J3MLoader implements AssetLoader {
shaderName.clear(); shaderName.clear();
} }
private void loadFromRoot(List<Statement> roots) throws IOException{ private void loadFromRoot(List<Statement> roots) throws IOException {
if (roots.size() == 2){ if (roots.size() == 2) {
Statement exception = roots.get(0); Statement exception = roots.get(0);
String line = exception.getLine(); String line = exception.getLine();
if (line.startsWith("Exception")){ if (line.startsWith("Exception")) {
throw new AssetLoadException(line.substring("Exception ".length())); throw new AssetLoadException(line.substring("Exception ".length()));
}else{ } else {
throw new IOException("In multiroot material, expected first statement to be 'Exception'"); throw new IOException("In multiroot material, expected first statement to be 'Exception'");
} }
}else if (roots.size() != 1){ } else if (roots.size() != 1) {
throw new IOException("Too many roots in J3M/J3MD file"); throw new IOException("Too many roots in J3M/J3MD file");
} }
boolean extending = false; boolean extending = false;
Statement materialStat = roots.get(0); Statement materialStat = roots.get(0);
String materialName = materialStat.getLine(); String materialName = materialStat.getLine();
if (materialName.startsWith("MaterialDef")){ if (materialName.startsWith("MaterialDef")) {
materialName = materialName.substring("MaterialDef ".length()).trim(); materialName = materialName.substring("MaterialDef ".length()).trim();
extending = false; extending = false;
}else if (materialName.startsWith("Material")){ } else if (materialName.startsWith("Material")) {
materialName = materialName.substring("Material ".length()).trim(); materialName = materialName.substring("Material ".length()).trim();
extending = true; extending = true;
}else{ } else {
throw new IOException("Specified file is not a Material file"); throw new IOException("Specified file is not a Material file");
} }
String[] split = materialName.split(":", 2); String[] split = materialName.split(":", 2);
if (materialName.equals("")){ if (materialName.equals("")) {
throw new MatParseException("Material name cannot be empty", materialStat); throw new MatParseException("Material name cannot be empty", materialStat);
} }
if (split.length == 2){ if (split.length == 2) {
if (!extending){ if (!extending) {
throw new MatParseException("Must use 'Material' when extending.", materialStat); throw new MatParseException("Must use 'Material' when extending.", materialStat);
} }
@ -531,35 +535,35 @@ public class J3MLoader implements AssetLoader {
material = new Material(def); material = new Material(def);
material.setKey(key); material.setKey(key);
// material.setAssetName(fileName); // material.setAssetName(fileName);
}else if (split.length == 1){ } else if (split.length == 1) {
if (extending){ if (extending) {
throw new MatParseException("Expected ':', got '{'", materialStat); throw new MatParseException("Expected ':', got '{'", materialStat);
} }
materialDef = new MaterialDef(assetManager, materialName); materialDef = new MaterialDef(assetManager, materialName);
// NOTE: pass file name for defs so they can be loaded later // NOTE: pass file name for defs so they can be loaded later
materialDef.setAssetName(key.getName()); materialDef.setAssetName(key.getName());
}else{ } else {
throw new MatParseException("Cannot use colon in material name/path", materialStat); throw new MatParseException("Cannot use colon in material name/path", materialStat);
} }
for (Statement statement : materialStat.getContents()){ for (Statement statement : materialStat.getContents()) {
split = statement.getLine().split("[ \\{]"); split = statement.getLine().split("[ \\{]");
String statType = split[0]; String statType = split[0];
if (extending){ if (extending) {
if (statType.equals("MaterialParameters")){ if (statType.equals("MaterialParameters")) {
readExtendingMaterialParams(statement.getContents()); readExtendingMaterialParams(statement.getContents());
}else if (statType.equals("AdditionalRenderState")){ } else if (statType.equals("AdditionalRenderState")) {
readAdditionalRenderState(statement.getContents()); readAdditionalRenderState(statement.getContents());
}else if (statType.equals("Transparent")){ } else if (statType.equals("Transparent")) {
readTransparentStatement(statement.getLine()); readTransparentStatement(statement.getLine());
} }
}else{ } else {
if (statType.equals("Technique")){ if (statType.equals("Technique")) {
readTechnique(statement); readTechnique(statement);
}else if (statType.equals("MaterialParameters")){ } else if (statType.equals("MaterialParameters")) {
readMaterialParams(statement.getContents()); readMaterialParams(statement.getContents());
}else{ } else {
throw new MatParseException("Expected material statement, got '"+statType+"'", statement); throw new MatParseException("Expected material statement, got '" + statType + "'", statement);
} }
} }
} }
@ -573,18 +577,18 @@ public class J3MLoader implements AssetLoader {
key = info.getKey(); key = info.getKey();
loadFromRoot(BlockLanguageParser.parse(in)); loadFromRoot(BlockLanguageParser.parse(in));
} finally { } finally {
if (in != null){ if (in != null) {
in.close(); in.close();
} }
} }
if (material != null){ if (material != null) {
if (!(info.getKey() instanceof MaterialKey)){ if (!(info.getKey() instanceof MaterialKey)) {
throw new IOException("Material instances must be loaded via MaterialKey"); throw new IOException("Material instances must be loaded via MaterialKey");
} }
// material implementation // material implementation
return material; return material;
}else{ } else {
// material definition // material definition
return materialDef; return materialDef;
} }
@ -601,9 +605,9 @@ public class J3MLoader implements AssetLoader {
if (!isUseNodes) { if (!isUseNodes) {
isUseNodes = shaderName.get(Shader.ShaderType.Vertex) == null && shaderName.get(Shader.ShaderType.Fragment) == null; isUseNodes = shaderName.get(Shader.ShaderType.Vertex) == null && shaderName.get(Shader.ShaderType.Fragment) == null;
if (isUseNodes) { if (isUseNodes) {
if(nodesLoaderDelegate == null){ if (nodesLoaderDelegate == null) {
nodesLoaderDelegate = new ShaderNodeLoaderDelegate(); nodesLoaderDelegate = new ShaderNodeLoaderDelegate();
}else{ } else {
nodesLoaderDelegate.clear(); nodesLoaderDelegate.clear();
} }
nodesLoaderDelegate.setTechniqueDef(technique); nodesLoaderDelegate.setTechniqueDef(technique);

@ -0,0 +1,3 @@
void main(){
gl_FragColor=vec4(1.0,0.0,1.0,0.5);
}

@ -0,0 +1,19 @@
layout (points) in;
layout (line_strip) out;
layout (max_vertices = 11) out;
uniform mat4 g_WorldViewProjectionMatrix;
const float PI = 3.1415926;
void main(){
for (int i = 0; i <= 10; i++) {
float ang = PI * 2.0 / 10.0 * i;
vec4 offset = vec4(cos(ang) * 5, -sin(ang) * 5, 0.0, 0.0);
gl_Position = g_WorldViewProjectionMatrix*vec4(gl_in[0].gl_Position.xyz + offset.xyz,1.0);
EmitVertex();
}
EndPrimitive();
}

@ -0,0 +1,4 @@
Material Pong Rock : Materials/Geom/SimpleGeom.j3md {
MaterialParameters {
}
}

@ -0,0 +1,17 @@
MaterialDef SimpleGeom {
MaterialParameters {
}
Technique {
VertexShader GLSL330: Materials/Geom/SimpleGeom.vert
GeometryShader GLSL330: Materials/Geom/SimpleGeom.geom
FragmentShader GLSL330: Materials/Geom/SimpleGeom.frag
WorldParameters {
WorldViewProjectionMatrix
}
}
}

@ -0,0 +1,5 @@
attribute vec3 inPosition;
void main(){
gl_Position=vec4(inPosition,1);
}
Loading…
Cancel
Save