More Caching (#242)

* More Caching
* Pull TextureTracker & TextureInfoCache out of Iris and into GLSM
* Cache more TextureInfo state
* GLSM: glTexParameter, glGetTexParameter(i|f), glGetTexLevelParameteri
* Move IrisRenderSystem -> GLSM.RenderSystem
* Ensure DSA works properly with GLSM
 * Further updates to DSA for texParameter/getTexParameter
* A few more cached fields for TextureInfo
* Properly handle deleteTextures
* Ensure anyone crazy enough to use an Int/FloatBuffer for single value params get redirected
* Add optional runtime disabling of GLSM
* Switch checkGLError from an inject to a redirect, hopefully fewer allocations
* Fewer allocations for clearBufferState
* Microoptimization for (setup|clear)BufferState
This commit is contained in:
Jason Mitchell authored and GitHub committed 2024-01-27 15:52:57 -08:00
1 parent b956a44857
commit 6691511e17
68 files changed
+935 -890

No files matched your search

@@ -1,100 +0,0 @@
package net.coderbot.iris.texture;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL11;
import java.nio.IntBuffer;
public class TextureInfoCache {
public static final TextureInfoCache INSTANCE = new TextureInfoCache();
private final Int2ObjectMap<TextureInfo> cache = new Int2ObjectOpenHashMap<>();
private TextureInfoCache() {
}
public TextureInfo getInfo(int id) {
TextureInfo info = cache.get(id);
if (info == null) {
info = new TextureInfo(id);
cache.put(id, info);
}
return info;
}
public void onTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, @Nullable IntBuffer pixels) {
if (level == 0) {
int id = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
TextureInfo info = getInfo(id);
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 (level == 0) {
int id = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
TextureInfo info = getInfo(id);
info.internalFormat = internalformat;
info.width = width;
info.height = height;
}
}
public void onDeleteTexture(int id) {
cache.remove(id);
}
public static class TextureInfo {
private final int id;
private int internalFormat = -1;
private int width = -1;
private int height = -1;
private TextureInfo(int id) {
this.id = id;
}
public int getId() {
return id;
}
public int getInternalFormat() {
if (internalFormat == -1) {
internalFormat = fetchLevelParameter(GL11.GL_TEXTURE_INTERNAL_FORMAT);
}
return internalFormat;
}
public int getWidth() {
if (width == -1) {
width = fetchLevelParameter(GL11.GL_TEXTURE_WIDTH);
}
return width;
}
public int getHeight() {
if (height == -1) {
height = fetchLevelParameter(GL11.GL_TEXTURE_HEIGHT);
}
return height;
}
private int fetchLevelParameter(int pname) {
// Keep track of what texture was bound before
int previousTextureBinding = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
// Bind this texture and grab the parameter from it.
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id);
int parameter = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, pname);
// Make sure to re-bind the previous texture to avoid issues.
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, previousTextureBinding);
return parameter;
}
}
}
@@ -1,60 +0,0 @@
package net.coderbot.iris.texture;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.state.StateUpdateNotifiers;
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.minecraft.client.renderer.texture.AbstractTexture;
import org.jetbrains.annotations.Nullable;
public class TextureTracker {
public static final TextureTracker INSTANCE = new TextureTracker();
private static Runnable bindTextureListener;
static {
StateUpdateNotifiers.bindTextureNotifier = listener -> bindTextureListener = listener;
}
private final Int2ObjectMap<AbstractTexture> textures = new Int2ObjectOpenHashMap<>();
private boolean lockBindCallback;
private TextureTracker() {
}
public void trackTexture(int id, AbstractTexture texture) {
textures.put(id, texture);
}
@Nullable
public AbstractTexture getTexture(int id) {
return textures.get(id);
}
public void onBindTexture(int id) {
if (lockBindCallback) {
return;
}
if (GLStateManager.getActiveTextureUnit() == 0) {
lockBindCallback = true;
if (bindTextureListener != null) {
bindTextureListener.run();
}
WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable();
if (pipeline != null) {
pipeline.onBindTexture(id);
}
// Reset texture state
IrisRenderSystem.bindTextureToUnit(0, id);
lockBindCallback = false;
}
}
public void onDeleteTexture(int id) {
textures.remove(id);
}
}
@@ -1,6 +1,6 @@
package net.coderbot.iris.texture.format;
import net.coderbot.iris.gl.IrisRenderSystem;
import com.gtnewhorizons.angelica.glsm.RenderSystem;
import net.coderbot.iris.texture.mipmap.CustomMipmapGenerator;
import net.coderbot.iris.texture.pbr.PBRType;
import net.minecraft.client.renderer.texture.AbstractTexture;
@@ -46,12 +46,12 @@ public interface TextureFormat {
default void setupTextureParameters(PBRType pbrType, AbstractTexture texture) {
if (!canInterpolateValues(pbrType)) {
int minFilter = IrisRenderSystem.getTexParameteri(texture.getGlTextureId(), GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER);
int minFilter = RenderSystem.getTexParameteri(texture.getGlTextureId(), GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER);
// Non-mipped filters begin at 0x2600 whereas mipped filters begin at 0x2700,
// so this bit mask can be used to check if the filter is mipped or not
boolean mipmap = (minFilter & 1 << 8) == 1;
IrisRenderSystem.texParameteri(texture.getGlTextureId(), GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, mipmap ? GL11.GL_NEAREST_MIPMAP_NEAREST : GL11.GL_NEAREST);
IrisRenderSystem.texParameteri(texture.getGlTextureId(), GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
RenderSystem.texParameteri(texture.getGlTextureId(), GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, mipmap ? GL11.GL_NEAREST_MIPMAP_NEAREST : GL11.GL_NEAREST);
RenderSystem.texParameteri(texture.getGlTextureId(), GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
}
}
@@ -1,12 +1,12 @@
package net.coderbot.iris.texture.pbr;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import com.gtnewhorizons.angelica.glsm.texture.TextureTracker;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.state.StateUpdateNotifiers;
import net.coderbot.iris.rendertarget.NativeImageBackedSingleColorTexture;
import net.coderbot.iris.texture.TextureTracker;
import net.coderbot.iris.texture.pbr.loader.PBRTextureLoader;
import net.coderbot.iris.texture.pbr.loader.PBRTextureLoader.PBRTextureConsumer;
import net.coderbot.iris.texture.pbr.loader.PBRTextureLoaderRegistry;
@@ -1,13 +1,14 @@
package net.coderbot.iris.texture.pbr.loader;
import com.google.common.collect.Lists;
import com.gtnewhorizons.angelica.mixins.interfaces.ISpriteExt;
import com.gtnewhorizons.angelica.compat.mojang.NativeImage;
import com.gtnewhorizons.angelica.glsm.texture.TextureInfo;
import com.gtnewhorizons.angelica.glsm.texture.TextureInfoCache;
import com.gtnewhorizons.angelica.mixins.early.shaders.accessors.AnimationMetadataSectionAccessor;
import com.gtnewhorizons.angelica.mixins.early.shaders.accessors.TextureAtlasSpriteAccessor;
import com.gtnewhorizons.angelica.mixins.early.shaders.accessors.TextureMapAccessor;
import com.gtnewhorizons.angelica.mixins.interfaces.ISpriteExt;
import net.coderbot.iris.Iris;
import net.coderbot.iris.texture.TextureInfoCache;
import net.coderbot.iris.texture.format.TextureFormat;
import net.coderbot.iris.texture.format.TextureFormatLoader;
import net.coderbot.iris.texture.mipmap.ChannelMipmapGenerator;
@@ -42,7 +43,7 @@ public class AtlasPBRLoader implements PBRTextureLoader<TextureMap> {
@Override
public void load(TextureMap texMap, IResourceManager resourceManager, PBRTextureConsumer pbrTextureConsumer) {
final TextureInfoCache.TextureInfo textureInfo = TextureInfoCache.INSTANCE.getInfo(texMap.getGlTextureId());
final TextureInfo textureInfo = TextureInfoCache.INSTANCE.getInfo(texMap.getGlTextureId());
final int atlasWidth = textureInfo.getWidth();
final int atlasHeight = textureInfo.getHeight();
final int mipLevel = fetchAtlasMipLevel(texMap);