From aa54947ff36ac2591abef4c8e10c09c97cdddf72 Mon Sep 17 00:00:00 2001 From: Kirill Vainer Date: Tue, 22 Sep 2015 22:01:17 -0400 Subject: [PATCH] GLRenderer: cleanup to shadow compare mode Store compare mode in LastTextureState instead of on Texture object --- .../java/com/jme3/renderer/opengl/GL2.java | 2 +- .../com/jme3/renderer/opengl/GLRenderer.java | 52 +++++++++---------- .../main/java/com/jme3/texture/Texture.java | 14 +---- .../jme3/texture/image/LastTextureState.java | 9 +++- 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL2.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL2.java index db09fdd02..abf6a77d7 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL2.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL2.java @@ -44,7 +44,7 @@ public interface GL2 extends GL { public static final int GL_ALPHA_TEST = 0xBC0; public static final int GL_BGR = 0x80E0; public static final int GL_BGRA = 0x80E1; - public static final int GL_COMPARE_R_TO_TEXTURE = 0x884E; + public static final int GL_COMPARE_REF_TO_TEXTURE = 0x884E; public static final int GL_DEPTH_COMPONENT24 = 0x81A6; public static final int GL_DEPTH_COMPONENT32 = 0x81A7; public static final int GL_DEPTH_TEXTURE_MODE = 0x884B; diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index ee7f7a560..7b40994ad 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -51,7 +51,9 @@ import com.jme3.texture.FrameBuffer; import com.jme3.texture.FrameBuffer.RenderBuffer; import com.jme3.texture.Image; import com.jme3.texture.Texture; +import com.jme3.texture.Texture.ShadowCompareMode; import com.jme3.texture.Texture.WrapAxis; +import com.jme3.texture.image.LastTextureState; import com.jme3.util.BufferUtils; import com.jme3.util.ListMap; import com.jme3.util.MipMapGenerator; @@ -1842,77 +1844,71 @@ public class GLRenderer implements Renderer { int target = convertTextureType(tex.getType(), image != null ? image.getMultiSamples() : 1, -1); boolean haveMips = true; - if (image != null) { haveMips = image.isGeneratedMipmapsRequired() || image.hasMipmaps(); } + + LastTextureState curState = image.getLastTextureState(); - // filter things - if (image.getLastTextureState().magFilter != tex.getMagFilter()) { - int magFilter = convertMagFilter(tex.getMagFilter()); + if (curState.magFilter != tex.getMagFilter()) { bindTextureAndUnit(target, image, unit); - gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, magFilter); - image.getLastTextureState().magFilter = tex.getMagFilter(); + gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, convertMagFilter(tex.getMagFilter())); + curState.magFilter = tex.getMagFilter(); } - if (image.getLastTextureState().minFilter != tex.getMinFilter()) { - int minFilter = convertMinFilter(tex.getMinFilter(), haveMips); + if (curState.minFilter != tex.getMinFilter()) { bindTextureAndUnit(target, image, unit); - gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, minFilter); - image.getLastTextureState().minFilter = tex.getMinFilter(); + gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, convertMinFilter(tex.getMinFilter(), haveMips)); + curState.minFilter = tex.getMinFilter(); } if (caps.contains(Caps.TextureFilterAnisotropic) - && image.getLastTextureState().anisoFilter != tex.getAnisotropicFilter()) { + && curState.anisoFilter != tex.getAnisotropicFilter()) { bindTextureAndUnit(target, image, unit); gl.glTexParameterf(target, GLExt.GL_TEXTURE_MAX_ANISOTROPY_EXT, tex.getAnisotropicFilter()); - image.getLastTextureState().anisoFilter = tex.getAnisotropicFilter(); + curState.anisoFilter = tex.getAnisotropicFilter(); } - // repeat modes switch (tex.getType()) { case ThreeDimensional: case CubeMap: // cubemaps use 3D coords - if (gl2 != null && image.getLastTextureState().rWrap != tex.getWrap(WrapAxis.R)) { + if (gl2 != null && curState.rWrap != tex.getWrap(WrapAxis.R)) { bindTextureAndUnit(target, image, unit); gl2.glTexParameteri(target, GL2.GL_TEXTURE_WRAP_R, convertWrapMode(tex.getWrap(WrapAxis.R))); - image.getLastTextureState().rWrap = tex.getWrap(WrapAxis.R); + curState.rWrap = tex.getWrap(WrapAxis.R); } //There is no break statement on purpose here case TwoDimensional: case TwoDimensionalArray: - if (image.getLastTextureState().tWrap != tex.getWrap(WrapAxis.T)) { + if (curState.tWrap != tex.getWrap(WrapAxis.T)) { bindTextureAndUnit(target, image, unit); gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_T, convertWrapMode(tex.getWrap(WrapAxis.T))); image.getLastTextureState().tWrap = tex.getWrap(WrapAxis.T); } - if (image.getLastTextureState().sWrap != tex.getWrap(WrapAxis.S)) { + if (curState.sWrap != tex.getWrap(WrapAxis.S)) { bindTextureAndUnit(target, image, unit); gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_S, convertWrapMode(tex.getWrap(WrapAxis.S))); - image.getLastTextureState().sWrap = tex.getWrap(WrapAxis.S); + curState.sWrap = tex.getWrap(WrapAxis.S); } break; default: throw new UnsupportedOperationException("Unknown texture type: " + tex.getType()); } - if (tex.isNeedCompareModeUpdate() && gl2 != null) { - // R to Texture compare mode - if (tex.getShadowCompareMode() != Texture.ShadowCompareMode.Off) { - bindTextureAndUnit(target, image, unit); - gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_R_TO_TEXTURE); - gl2.glTexParameteri(target, GL2.GL_DEPTH_TEXTURE_MODE, GL2.GL_INTENSITY); - if (tex.getShadowCompareMode() == Texture.ShadowCompareMode.GreaterOrEqual) { + ShadowCompareMode texCompareMode = tex.getShadowCompareMode(); + if (gl2 != null && curState.shadowCompareMode != texCompareMode) { + bindTextureAndUnit(target, image, unit); + if (texCompareMode != ShadowCompareMode.Off) { + gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_REF_TO_TEXTURE); + if (texCompareMode == ShadowCompareMode.GreaterOrEqual) { gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_GEQUAL); } else { gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_LEQUAL); } } else { - bindTextureAndUnit(target, image, unit); - //restoring default value gl2.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL.GL_NONE); } - tex.compareModeUpdated(); + curState.shadowCompareMode = texCompareMode; } // If at this point we didn't bind the texture, bind it now diff --git a/jme3-core/src/main/java/com/jme3/texture/Texture.java b/jme3-core/src/main/java/com/jme3/texture/Texture.java index 582d06565..6be00700b 100644 --- a/jme3-core/src/main/java/com/jme3/texture/Texture.java +++ b/jme3-core/src/main/java/com/jme3/texture/Texture.java @@ -316,7 +316,6 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable private MinFilter minificationFilter = MinFilter.BilinearNoMipMaps; private MagFilter magnificationFilter = MagFilter.Bilinear; private ShadowCompareMode shadowCompareMode = ShadowCompareMode.Off; - private boolean needCompareModeUpdate = false; private int anisotropicFilter; /** @@ -404,7 +403,6 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable "compareMode can not be null."); } this.shadowCompareMode = compareMode; - needCompareModeUpdate = true; } /** @@ -489,7 +487,7 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable /** * @return the anisotropic filtering level for this texture. Default value * is 0 (use value from config), - * 1 means 1x (no anisotrophy), 2 means x2, 4 is x4, etc. + * 1 means 1x (no anisotropy), 2 means x2, 4 is x4, etc. */ public int getAnisotropicFilter() { return anisotropicFilter; @@ -636,14 +634,4 @@ public abstract class Texture implements CloneableSmartAsset, Savable, Cloneable magnificationFilter = capsule.readEnum("magnificationFilter", MagFilter.class, MagFilter.Bilinear); } - - public boolean isNeedCompareModeUpdate() { - return needCompareModeUpdate; - } - - public void compareModeUpdated() { - this.needCompareModeUpdate = false; - } - - } diff --git a/jme3-core/src/main/java/com/jme3/texture/image/LastTextureState.java b/jme3-core/src/main/java/com/jme3/texture/image/LastTextureState.java index 3b85563ef..32c1b718b 100644 --- a/jme3-core/src/main/java/com/jme3/texture/image/LastTextureState.java +++ b/jme3-core/src/main/java/com/jme3/texture/image/LastTextureState.java @@ -45,10 +45,11 @@ public final class LastTextureState { public Texture.WrapMode sWrap, tWrap, rWrap; public Texture.MagFilter magFilter; public Texture.MinFilter minFilter; - public int anisoFilter = 0; + public int anisoFilter; + public Texture.ShadowCompareMode shadowCompareMode; public LastTextureState() { - // All parameters initialized to null (meaning unset). + reset(); } public void reset() { @@ -58,5 +59,9 @@ public final class LastTextureState { magFilter = null; minFilter = null; anisoFilter = 0; + + // The default in OpenGL is OFF, so we avoid setting this per texture + // if its not used. + shadowCompareMode = Texture.ShadowCompareMode.Off; } }