GLRenderer: cleanup to shadow compare mode

Store compare mode in LastTextureState instead of on Texture object
experimental
Kirill Vainer 9 years ago
parent efe600c38d
commit aa54947ff3
  1. 2
      jme3-core/src/main/java/com/jme3/renderer/opengl/GL2.java
  2. 52
      jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
  3. 14
      jme3-core/src/main/java/com/jme3/texture/Texture.java
  4. 9
      jme3-core/src/main/java/com/jme3/texture/image/LastTextureState.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;

@ -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

@ -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;
}
}

@ -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;
}
}

Loading…
Cancel
Save