forked from Asek3/Oculus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
atlasSize now properly reflects the texture currently bound to the te…
…x/texture/gtexture sampler Fixes IrisShaders#648 Closes IrisShaders#549
- Loading branch information
1 parent
3a4894b
commit fcd6275
Showing
13 changed files
with
232 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/net/coderbot/iris/gl/uniform/Vector2IntegerJomlUniform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package net.coderbot.iris.gl.uniform; | ||
|
||
import net.coderbot.iris.vendored.joml.Vector2i; | ||
import org.lwjgl.opengl.GL20; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class Vector2IntegerJomlUniform extends Uniform { | ||
private Vector2i cachedValue; | ||
private final Supplier<Vector2i> value; | ||
|
||
Vector2IntegerJomlUniform(int location, Supplier<Vector2i> value) { | ||
this(location, value, null); | ||
} | ||
|
||
Vector2IntegerJomlUniform(int location, Supplier<Vector2i> value, ValueUpdateNotifier notifier) { | ||
super(location, notifier); | ||
|
||
this.cachedValue = null; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void update() { | ||
updateValue(); | ||
|
||
if (notifier != null) { | ||
notifier.setListener(this::updateValue); | ||
} | ||
} | ||
|
||
private void updateValue() { | ||
Vector2i newValue = value.get(); | ||
|
||
if (cachedValue == null || !newValue.equals(cachedValue)) { | ||
cachedValue = newValue; | ||
GL20.glUniform2i(this.location, newValue.x, newValue.y); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/net/coderbot/iris/mixin/MixinGlStateManager_AtlasTracking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.coderbot.iris.mixin; | ||
|
||
import com.mojang.blaze3d.platform.GlStateManager; | ||
import net.coderbot.iris.gl.state.StateUpdateNotifiers; | ||
import net.coderbot.iris.samplers.TextureAtlasTracker; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.nio.IntBuffer; | ||
|
||
@Mixin(GlStateManager.class) | ||
public class MixinGlStateManager_AtlasTracking { | ||
private static Runnable atlasTextureListener; | ||
|
||
@Shadow | ||
private static int activeTexture; | ||
|
||
@Inject(method = "_texImage2D(IIIIIIIILjava/nio/IntBuffer;)V", at = @At("HEAD")) | ||
private static void iris$onTexImage2D(int target, int level, int internalformat, int width, int height, int border, | ||
int format, int type, @Nullable IntBuffer pixels, CallbackInfo ci) { | ||
TextureAtlasTracker.INSTANCE.trackTexImage2D(GlStateManager.getActiveTextureName(), level, width, height); | ||
} | ||
|
||
@Inject(method = "_bindTexture(I)V", at = @At("HEAD")) | ||
private static void iris$onBindTexture(int id, CallbackInfo ci) { | ||
if (activeTexture == 0 && atlasTextureListener != null) { | ||
atlasTextureListener.run(); | ||
} | ||
} | ||
|
||
@Inject(method = "_deleteTexture(I)V", at = @At("HEAD")) | ||
private static void iris$onDeleteTexture(int id, CallbackInfo ci) { | ||
TextureAtlasTracker.INSTANCE.trackDeleteTextures(id); | ||
} | ||
|
||
@Inject(method = "_deleteTextures([I)V", at = @At("HEAD")) | ||
private static void iris$onDeleteTextures(int[] ids, CallbackInfo ci) { | ||
for (int id : ids) { | ||
TextureAtlasTracker.INSTANCE.trackDeleteTextures(id); | ||
} | ||
} | ||
|
||
static { | ||
StateUpdateNotifiers.atlasTextureNotifier = listener -> atlasTextureListener = listener; | ||
} | ||
} |
50 changes: 16 additions & 34 deletions
50
src/main/java/net/coderbot/iris/mixin/MixinTextureAtlas.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,39 @@ | ||
package net.coderbot.iris.mixin; | ||
|
||
import com.mojang.blaze3d.systems.RenderSystem; | ||
import net.coderbot.iris.samplers.TextureAtlasTracker; | ||
import net.coderbot.iris.texunits.TextureAtlasInterface; | ||
import net.minecraft.client.renderer.texture.AbstractTexture; | ||
import net.minecraft.client.renderer.texture.Stitcher; | ||
import net.minecraft.client.renderer.texture.TextureAtlas; | ||
import net.minecraft.client.renderer.texture.TextureAtlasSprite; | ||
import net.minecraft.server.packs.resources.ResourceManager; | ||
import net.minecraft.world.phys.Vec2; | ||
import org.lwjgl.opengl.GL20C; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(TextureAtlas.class) | ||
public abstract class MixinTextureAtlas extends AbstractTexture implements TextureAtlasInterface { | ||
@Unique | ||
private Vec2 atlasSize; | ||
|
||
@Inject(method = "getLoadedSprites", at = @At("HEAD")) | ||
private void getAtlasSize(ResourceManager resourceManager, Stitcher stitcher, int i, CallbackInfoReturnable<List<TextureAtlasSprite>> cir) { | ||
this.atlasSize = new Vec2(stitcher.getWidth(), stitcher.getHeight()); | ||
@Override | ||
public int getId() { | ||
int id = super.getId(); | ||
|
||
TextureAtlasTracker.INSTANCE.trackAtlas(id, (TextureAtlas) (Object) this); | ||
|
||
return id; | ||
} | ||
|
||
@Override | ||
public Vec2 getAtlasSize() { | ||
if (this.atlasSize == null) { | ||
iris$setAtlasSizeFromGlState(); | ||
public void setAtlasSize(int sizeX, int sizeY) { | ||
if (sizeX == 0 && sizeY == 0) { | ||
this.atlasSize = Vec2.ZERO; | ||
} else { | ||
this.atlasSize = new Vec2(sizeX, sizeY); | ||
} | ||
|
||
return this.atlasSize; | ||
} | ||
|
||
@Unique | ||
private void iris$setAtlasSizeFromGlState() { | ||
// support for DashLoader (and other mods which might mess with the other code path) | ||
int glId = this.getId(); | ||
|
||
// Keep track of what texture was bound before | ||
int existingGlId = GL20C.glGetInteger(GL20C.GL_TEXTURE_BINDING_2D); | ||
|
||
// Bind this texture and grab the atlas size from it. | ||
RenderSystem.bindTexture(glId); | ||
int width = GL20C.glGetTexLevelParameteri(GL20C.GL_TEXTURE_2D, 0, GL20C.GL_TEXTURE_WIDTH); | ||
int height = GL20C.glGetTexLevelParameteri(GL20C.GL_TEXTURE_2D, 0, GL20C.GL_TEXTURE_HEIGHT); | ||
this.atlasSize = new Vec2(width, height); | ||
|
||
// Make sure to re-bind the previous texture to avoid issues. | ||
RenderSystem.bindTexture(existingGlId); | ||
@Override | ||
public Vec2 getAtlasSize() { | ||
return this.atlasSize; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/net/coderbot/iris/samplers/TextureAtlasTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.coderbot.iris.samplers; | ||
|
||
import com.mojang.blaze3d.systems.RenderSystem; | ||
import net.coderbot.iris.texunits.TextureAtlasInterface; | ||
import net.minecraft.client.renderer.texture.TextureAtlas; | ||
import net.minecraft.world.phys.Vec2; | ||
import org.lwjgl.opengl.GL20C; | ||
|
||
import java.util.WeakHashMap; | ||
|
||
public class TextureAtlasTracker { | ||
public static final TextureAtlasTracker INSTANCE = new TextureAtlasTracker(); | ||
|
||
private final WeakHashMap<Integer, TextureAtlas> atlases; | ||
|
||
private TextureAtlasTracker() { | ||
atlases = new WeakHashMap<>(); | ||
} | ||
|
||
public void trackAtlas(int id, TextureAtlas atlas) { | ||
TextureAtlas previous = atlases.put(id, atlas); | ||
|
||
if (previous != atlas) { | ||
TextureAtlasInterface atlasI = (TextureAtlasInterface) atlas; | ||
atlasI.setAtlasSize(0, 0); | ||
} | ||
} | ||
|
||
public void trackTexImage2D(int id, int level, int sizeX, int sizeY) { | ||
if (level != 0) { | ||
return; | ||
} | ||
|
||
TextureAtlas atlas = atlases.get(id); | ||
|
||
if (atlas != null) { | ||
((TextureAtlasInterface) atlas).setAtlasSize(sizeX, sizeY); | ||
} | ||
} | ||
|
||
public Vec2 getAtlasSize(int id) { | ||
TextureAtlas atlas = atlases.get(id); | ||
Vec2 size = Vec2.ZERO; | ||
|
||
if (atlas != null) { | ||
size = ((TextureAtlasInterface) atlas).getAtlasSize(); | ||
|
||
if (Vec2.ZERO.equals(size)) { | ||
fetchAtlasSizeFromGlState(atlas); | ||
size = ((TextureAtlasInterface) atlas).getAtlasSize(); | ||
} | ||
} | ||
|
||
return size; | ||
} | ||
|
||
public void trackDeleteTextures(int id) { | ||
atlases.remove(id); | ||
} | ||
|
||
/** | ||
* Fallback path to support DashLoader (and other mods which might mess with the other code path) | ||
* | ||
* @author Kroppeb | ||
*/ | ||
private void fetchAtlasSizeFromGlState(TextureAtlas atlas) { | ||
// Keep track of what texture was bound before | ||
int existingGlId = GL20C.glGetInteger(GL20C.GL_TEXTURE_BINDING_2D); | ||
|
||
// Bind this texture and grab the atlas size from it. | ||
RenderSystem.bindTexture(atlas.getId()); | ||
int width = GL20C.glGetTexLevelParameteri(GL20C.GL_TEXTURE_2D, 0, GL20C.GL_TEXTURE_WIDTH); | ||
int height = GL20C.glGetTexLevelParameteri(GL20C.GL_TEXTURE_2D, 0, GL20C.GL_TEXTURE_HEIGHT); | ||
|
||
TextureAtlasInterface atlasI = (TextureAtlasInterface) atlas; | ||
atlasI.setAtlasSize(width, height); | ||
|
||
// Make sure to re-bind the previous texture to avoid issues. | ||
RenderSystem.bindTexture(existingGlId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
accessWidener v1 named | ||
accessible class com/mojang/blaze3d/platform/GlStateManager$FogState | ||
accessWidener v1 named | ||
accessible class com/mojang/blaze3d/platform/GlStateManager$FogState | ||
accessible class com/mojang/blaze3d/platform/GlStateManager$TextureState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters