mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-24 06:04:27 +00:00
Texture cache fixes (#1198)
This commit is contained in:
1 parent
89132f3ef9
commit
92b6dcc010
8 files changed
+615
-68
No files matched your search
@@ -143,6 +143,52 @@ public class GLStateManager {
|
||||
return Thread.currentThread() == drawableGLHolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if splash screen is complete.
|
||||
* After splash, there's only one GL context and no locking is needed.
|
||||
*/
|
||||
public static boolean isSplashComplete() {
|
||||
return splashComplete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the active texture unit for server-side state operations.
|
||||
* If caching is enabled, returns cached value.
|
||||
* If caching is disabled (SharedDrawable), queries actual GL state.
|
||||
*/
|
||||
public static int getActiveTextureUnitForServerState() {
|
||||
if (isCachingEnabled()) {
|
||||
return getActiveTextureUnit();
|
||||
}
|
||||
// Query actual GL state for SharedDrawable context
|
||||
return GL11.glGetInteger(GL13.GL_ACTIVE_TEXTURE) - GL13.GL_TEXTURE0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the texture bound to the current texture unit for server-side state operations.
|
||||
* If caching is enabled, returns cached value.
|
||||
* If caching is disabled (SharedDrawable), queries actual GL state.
|
||||
*/
|
||||
public static int getBoundTextureForServerState() {
|
||||
if (isCachingEnabled()) {
|
||||
return getBoundTexture();
|
||||
}
|
||||
// Query actual GL state for SharedDrawable context
|
||||
return GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the texture bound to a specific texture unit for server-side state operations.
|
||||
* If caching is enabled, returns cached value.
|
||||
* If caching is disabled (SharedDrawable), returns -1 to force operations to proceed
|
||||
*/
|
||||
public static int getBoundTextureForServerState(int unit) {
|
||||
if (isCachingEnabled()) {
|
||||
return getBoundTexture(unit);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// GLStateManager State Trackers
|
||||
private static final IntStack attribs = new IntArrayList(MAX_ATTRIB_STACK_DEPTH);
|
||||
|
||||
@@ -770,7 +816,7 @@ public class GLStateManager {
|
||||
case GL11.GL_LIST_MODE -> DisplayListManager.getListMode();
|
||||
case GL11.GL_MATRIX_MODE -> matrixMode.getMode();
|
||||
case GL11.GL_SHADE_MODEL -> shadeModelState.getValue();
|
||||
case GL11.GL_TEXTURE_BINDING_2D -> getBoundTexture();
|
||||
case GL11.GL_TEXTURE_BINDING_2D -> getBoundTextureForServerState();
|
||||
case GL14.GL_BLEND_DST_ALPHA -> blendState.getDstAlpha();
|
||||
case GL14.GL_BLEND_DST_RGB -> blendState.getDstRgb();
|
||||
case GL14.GL_BLEND_SRC_ALPHA -> blendState.getSrcAlpha();
|
||||
@@ -1320,11 +1366,11 @@ public class GLStateManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static int getBoundTexture() {
|
||||
private static int getBoundTexture() {
|
||||
return getBoundTexture(activeTextureUnit.getValue());
|
||||
}
|
||||
|
||||
public static int getBoundTexture(int unit) {
|
||||
private static int getBoundTexture(int unit) {
|
||||
return textures.getTextureUnitBindings(unit).getBinding();
|
||||
}
|
||||
|
||||
@@ -1341,21 +1387,21 @@ public class GLStateManager {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isCachingEnabled()) {
|
||||
GL11.glBindTexture(target, texture);
|
||||
return;
|
||||
}
|
||||
|
||||
final int activeUnit = GLStateManager.activeTextureUnit.getValue();
|
||||
final TextureBinding textureUnit = textures.getTextureUnitBindings(activeUnit);
|
||||
final int cachedBinding = textureUnit.getBinding();
|
||||
final boolean bypassCache = shouldBypassCache();
|
||||
final boolean cacheMatches = cachedBinding == texture;
|
||||
final boolean willBind = bypassCache || !cacheMatches;
|
||||
|
||||
if (willBind) {
|
||||
if (cachedBinding != texture) {
|
||||
if (!isRecordingDisplayList()) {
|
||||
GL11.glBindTexture(target, texture);
|
||||
}
|
||||
if (isCachingEnabled()) {
|
||||
textureUnit.setBinding(texture);
|
||||
TextureTracker.INSTANCE.onBindTexture(texture);
|
||||
}
|
||||
textureUnit.setBinding(texture);
|
||||
TextureTracker.INSTANCE.onBindTexture(texture);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1367,7 +1413,7 @@ public class GLStateManager {
|
||||
TextureInfoCache.INSTANCE.onTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
|
||||
if (shouldUseDSA(target)) {
|
||||
// Use DSA to upload directly to the texture
|
||||
RenderSystem.textureImage2D(getBoundTexture(), target, level, internalformat, width, height, border, format, type, pixels);
|
||||
RenderSystem.textureImage2D(getBoundTextureForServerState(), target, level, internalformat, width, height, border, format, type, pixels);
|
||||
} else {
|
||||
// Non-main thread or proxy texture - use direct GL call
|
||||
GL11.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
|
||||
@@ -1402,7 +1448,7 @@ public class GLStateManager {
|
||||
TextureInfoCache.INSTANCE.onTexImage2D(target, level, internalformat, width, height, border, format, type, pixels != null ? pixels.asIntBuffer() : null);
|
||||
if (shouldUseDSA(target)) {
|
||||
// Use DSA to upload directly to the texture - keeps GL binding state unchanged
|
||||
RenderSystem.textureImage2D(getBoundTexture(), target, level, internalformat, width, height, border, format, type, pixels);
|
||||
RenderSystem.textureImage2D(getBoundTextureForServerState(), target, level, internalformat, width, height, border, format, type, pixels);
|
||||
} else {
|
||||
// Non-main thread or proxy texture - use direct GL call
|
||||
GL11.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
|
||||
@@ -2538,7 +2584,7 @@ public class GLStateManager {
|
||||
GL11.glTexParameter(target, pname, params);
|
||||
return;
|
||||
}
|
||||
if(!updateTexParameteriCache(target, getBoundTexture(), pname, params.get(0))) return;
|
||||
if(!updateTexParameteriCache(target, getBoundTextureForServerState(), pname, params.get(0))) return;
|
||||
|
||||
GL11.glTexParameter(target, pname, params);
|
||||
}
|
||||
@@ -2548,7 +2594,7 @@ public class GLStateManager {
|
||||
GL11.glTexParameter(target, pname, params);
|
||||
return;
|
||||
}
|
||||
if(!updateTexParameterfCache(target, getBoundTexture(), pname, params.get(0))) return;
|
||||
if(!updateTexParameterfCache(target, getBoundTextureForServerState(), pname, params.get(0))) return;
|
||||
|
||||
GL11.glTexParameter(target, pname, params);
|
||||
}
|
||||
@@ -2566,7 +2612,7 @@ public class GLStateManager {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!updateTexParameteriCache(target, getBoundTexture(), pname, param)) return;
|
||||
if (!updateTexParameteriCache(target, getBoundTextureForServerState(), pname, param)) return;
|
||||
|
||||
// Skip actual GL call during display list recording (state tracking only)
|
||||
if (!isRecordingDisplayList()) {
|
||||
@@ -2609,7 +2655,7 @@ public class GLStateManager {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!updateTexParameterfCache(getActiveTextureUnit(), target, pname, param)) return;
|
||||
if (!updateTexParameterfCache(target, getBoundTextureForServerState(), pname, param)) return;
|
||||
|
||||
// Skip actual GL call during display list recording (state tracking only)
|
||||
if (!isRecordingDisplayList()) {
|
||||
@@ -2649,19 +2695,20 @@ public class GLStateManager {
|
||||
if (target != GL11.GL_TEXTURE_2D || shouldBypassCache()) {
|
||||
return GL11.glGetTexParameteri(target, pname);
|
||||
}
|
||||
return getTexParameterOrDefault(getBoundTexture(), pname, () -> GL11.glGetTexParameteri(target, pname));
|
||||
return getTexParameterOrDefault(getBoundTextureForServerState(), pname, () -> GL11.glGetTexParameteri(target, pname));
|
||||
}
|
||||
|
||||
public static float glGetTexParameterf(int target, int pname) {
|
||||
if (target != GL11.GL_TEXTURE_2D || shouldBypassCache()) {
|
||||
return GL11.glGetTexParameterf(target, pname);
|
||||
}
|
||||
final TextureInfo info = TextureInfoCache.INSTANCE.getInfo(getBoundTexture());
|
||||
final int boundTexture = getBoundTextureForServerState();
|
||||
final TextureInfo info = TextureInfoCache.INSTANCE.getInfo(boundTexture);
|
||||
if(info == null) {
|
||||
if (isRecordingDisplayList()) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"glGetTexParameterf called during display list recording with no cached TextureInfo for texture %d. " +
|
||||
"Cannot query OpenGL state during compilation!", getBoundTexture()));
|
||||
"Cannot query OpenGL state during compilation!", boundTexture));
|
||||
}
|
||||
return GL11.glGetTexParameterf(target, pname);
|
||||
}
|
||||
@@ -2673,7 +2720,7 @@ public class GLStateManager {
|
||||
if (isRecordingDisplayList()) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"glGetTexParameterf called during display list recording with uncached pname 0x%s for texture %d. " +
|
||||
"Cannot query OpenGL state during compilation!", Integer.toHexString(pname), getBoundTexture()));
|
||||
"Cannot query OpenGL state during compilation!", Integer.toHexString(pname), boundTexture));
|
||||
}
|
||||
yield GL11.glGetTexParameterf(target, pname);
|
||||
}
|
||||
@@ -2684,12 +2731,12 @@ public class GLStateManager {
|
||||
if (target != GL11.GL_TEXTURE_2D || shouldBypassCache()) {
|
||||
return GL11.glGetTexLevelParameteri(target, level, pname);
|
||||
}
|
||||
final TextureInfo info = TextureInfoCache.INSTANCE.getInfo(getBoundTexture());
|
||||
final TextureInfo info = TextureInfoCache.INSTANCE.getInfo(getBoundTextureForServerState());
|
||||
if (info == null) {
|
||||
if (isRecordingDisplayList()) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"glGetTexLevelParameteri called during display list recording with no cached TextureInfo for texture %d. " +
|
||||
"Cannot query OpenGL state during compilation!", getBoundTexture()));
|
||||
"Cannot query OpenGL state during compilation!", getBoundTextureForServerState()));
|
||||
}
|
||||
return GL11.glGetTexLevelParameteri(target, level, pname);
|
||||
}
|
||||
@@ -2701,7 +2748,7 @@ public class GLStateManager {
|
||||
if (isRecordingDisplayList()) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"glGetTexLevelParameteri called during display list recording with uncached pname 0x%s for texture %d. " +
|
||||
"Cannot query OpenGL state during compilation!", Integer.toHexString(pname), getBoundTexture()));
|
||||
"Cannot query OpenGL state during compilation!", Integer.toHexString(pname), getBoundTextureForServerState()));
|
||||
}
|
||||
yield GL11.glGetTexLevelParameteri(target, level, pname);
|
||||
}
|
||||
@@ -3101,7 +3148,7 @@ public class GLStateManager {
|
||||
}
|
||||
if (shouldUseDSA(target)) {
|
||||
// Use DSA to upload directly to the texture - keeps GL binding state unchanged
|
||||
RenderSystem.textureSubImage2D(getBoundTexture(), target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
RenderSystem.textureSubImage2D(getBoundTextureForServerState(), target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
} else {
|
||||
// Non-main thread or proxy texture - use direct GL call
|
||||
GL11.glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
@@ -3114,7 +3161,7 @@ public class GLStateManager {
|
||||
}
|
||||
if (shouldUseDSA(target)) {
|
||||
// Use DSA to upload directly to the texture
|
||||
RenderSystem.textureSubImage2D(getBoundTexture(), target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
RenderSystem.textureSubImage2D(getBoundTextureForServerState(), target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
} else {
|
||||
// Non-main thread or proxy texture - use direct GL call
|
||||
GL11.glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
|
||||
@@ -84,7 +84,7 @@ public class DSAARB extends DSAUnsupported {
|
||||
|
||||
@Override
|
||||
public void bindTextureToUnit(int unit, int texture) {
|
||||
if(GLStateManager.getBoundTexture(unit) == texture) return;
|
||||
if(GLStateManager.getBoundTextureForServerState(unit) == texture) return;
|
||||
|
||||
if (texture == 0) {
|
||||
super.bindTextureToUnit(unit, texture);
|
||||
|
||||
@@ -24,7 +24,7 @@ public class DSAUnsupported implements DSAAccess {
|
||||
@Override
|
||||
public void textureImage2D(int texture, int target, int level, int internalformat, int width, int height, int border, int format, int type, ByteBuffer pixels) {
|
||||
// Get what cache thinks is bound - we'll restore to this to keep cache/GL in sync
|
||||
final int cachedBinding = GLStateManager.getBoundTexture();
|
||||
final int cachedBinding = GLStateManager.getBoundTextureForServerState();
|
||||
GL11.glBindTexture(target, texture);
|
||||
GL11.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
|
||||
GL11.glBindTexture(target, cachedBinding); // Restore to cache value
|
||||
@@ -32,7 +32,7 @@ public class DSAUnsupported implements DSAAccess {
|
||||
|
||||
@Override
|
||||
public void textureImage2D(int texture, int target, int level, int internalformat, int width, int height, int border, int format, int type, IntBuffer pixels) {
|
||||
final int cachedBinding = GLStateManager.getBoundTexture();
|
||||
final int cachedBinding = GLStateManager.getBoundTextureForServerState();
|
||||
GL11.glBindTexture(target, texture);
|
||||
GL11.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
|
||||
GL11.glBindTexture(target, cachedBinding);
|
||||
@@ -40,7 +40,7 @@ public class DSAUnsupported implements DSAAccess {
|
||||
|
||||
@Override
|
||||
public void textureSubImage2D(int texture, int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, ByteBuffer pixels) {
|
||||
final int cachedBinding = GLStateManager.getBoundTexture();
|
||||
final int cachedBinding = GLStateManager.getBoundTextureForServerState();
|
||||
GL11.glBindTexture(target, texture);
|
||||
GL11.glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
GL11.glBindTexture(target, cachedBinding);
|
||||
@@ -48,7 +48,7 @@ public class DSAUnsupported implements DSAAccess {
|
||||
|
||||
@Override
|
||||
public void textureSubImage2D(int texture, int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, IntBuffer pixels) {
|
||||
final int cachedBinding = GLStateManager.getBoundTexture();
|
||||
final int cachedBinding = GLStateManager.getBoundTextureForServerState();
|
||||
GL11.glBindTexture(target, texture);
|
||||
GL11.glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
|
||||
GL11.glBindTexture(target, cachedBinding);
|
||||
@@ -92,7 +92,7 @@ public class DSAUnsupported implements DSAAccess {
|
||||
|
||||
@Override
|
||||
public int getTexLevelParameteri(int texture, int level, int pname) {
|
||||
final int previous = GLStateManager.getBoundTexture();
|
||||
final int previous = GLStateManager.getBoundTextureForServerState();
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
||||
final int result = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, level, pname);
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, previous);
|
||||
@@ -101,7 +101,7 @@ public class DSAUnsupported implements DSAAccess {
|
||||
|
||||
@Override
|
||||
public void copyTexSubImage2D(int destTexture, int target, int i, int i1, int i2, int i3, int i4, int width, int height) {
|
||||
final int previous = GLStateManager.getBoundTexture();
|
||||
final int previous = GLStateManager.getBoundTextureForServerState();
|
||||
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, destTexture);
|
||||
GL11.glCopyTexSubImage2D(target, i, i1, i2, i3, i4, width, height);
|
||||
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, previous);
|
||||
|
||||
@@ -12,7 +12,7 @@ public class TextureInfo {
|
||||
protected int width = -1;
|
||||
protected int height = -1;
|
||||
|
||||
@Getter @Setter protected int minFilter = GL11.GL_LINEAR_MIPMAP_LINEAR;
|
||||
@Getter @Setter protected int minFilter = GL11.GL_NEAREST_MIPMAP_LINEAR;
|
||||
@Getter @Setter protected int magFilter = GL11.GL_LINEAR;
|
||||
@Getter @Setter protected int wrapS = GL11.GL_REPEAT;
|
||||
@Getter @Setter protected int wrapT = GL11.GL_REPEAT;
|
||||
|
||||
@@ -7,48 +7,73 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.nio.Buffer;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class TextureInfoCache {
|
||||
/**
|
||||
* Adapted from Iris for use in GLSM
|
||||
* Adapted from Iris for use in GLSM.
|
||||
*
|
||||
* This cache stores server-side texture state (parameters, dimensions) that is shared across GL contexts.
|
||||
* Locking is only used during splash when multiple contexts exist.
|
||||
*/
|
||||
|
||||
public static final TextureInfoCache INSTANCE = new TextureInfoCache();
|
||||
public static final TextureInfoCache INSTANCE = new TextureInfoCache();
|
||||
|
||||
private final Int2ObjectMap<TextureInfo> cache = new Int2ObjectOpenHashMap<>();
|
||||
private final Int2ObjectMap<TextureInfo> cache = new Int2ObjectOpenHashMap<>();
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
private TextureInfoCache() {
|
||||
}
|
||||
private TextureInfoCache() {
|
||||
}
|
||||
|
||||
public TextureInfo getInfo(int id) {
|
||||
if(id < 0) return null;
|
||||
if(!GLStateManager.isCachingEnabled()) {
|
||||
return new TextureInfo(id);
|
||||
public TextureInfo getInfo(int id) {
|
||||
if (id < 0) return null;
|
||||
|
||||
if (GLStateManager.isSplashComplete()) {
|
||||
return cache.computeIfAbsent(id, TextureInfo::new);
|
||||
}
|
||||
return cache.computeIfAbsent(id, TextureInfo::new);
|
||||
}
|
||||
|
||||
public void onTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, @Nullable Buffer pixels) {
|
||||
if (target == GL11.GL_TEXTURE_2D && level == 0) {
|
||||
final TextureInfo info = getInfo(GLStateManager.getBoundTexture());
|
||||
if(info == null) return;
|
||||
info.internalFormat = internalformat;
|
||||
info.width = width;
|
||||
info.height = height;
|
||||
}
|
||||
}
|
||||
lock.lock();
|
||||
try {
|
||||
return cache.computeIfAbsent(id, TextureInfo::new);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void onTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, @Nullable Buffer pixels) {
|
||||
if (target == GL11.GL_TEXTURE_2D && level == 0) {
|
||||
final TextureInfo info = getInfo(GLStateManager.getBoundTextureForServerState());
|
||||
if (info == null) return;
|
||||
info.internalFormat = internalformat;
|
||||
info.width = width;
|
||||
info.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
public void onTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, long pixels_buffer_offset) {
|
||||
if (target == GL11.GL_TEXTURE_2D && level == 0) {
|
||||
final TextureInfo info = getInfo(GLStateManager.getBoundTexture());
|
||||
if(info == null) return;
|
||||
info.internalFormat = internalformat;
|
||||
info.width = width;
|
||||
info.height = height;
|
||||
}
|
||||
}
|
||||
if (target == GL11.GL_TEXTURE_2D && level == 0) {
|
||||
final TextureInfo info = getInfo(GLStateManager.getBoundTextureForServerState());
|
||||
if (info == null) return;
|
||||
info.internalFormat = internalformat;
|
||||
info.width = width;
|
||||
info.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
public void onDeleteTexture(int id) {
|
||||
if(id >= 0) cache.remove(id);
|
||||
}
|
||||
public void onDeleteTexture(int id) {
|
||||
if (id < 0) return;
|
||||
|
||||
if (GLStateManager.isSplashComplete()) {
|
||||
cache.remove(id);
|
||||
return;
|
||||
}
|
||||
|
||||
// During splash: lock for thread safety
|
||||
lock.lock();
|
||||
try {
|
||||
cache.remove(id);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user