From 17f6147e133d33aab9f48acc5640081af4c922ed Mon Sep 17 00:00:00 2001 From: TomTheFurry Date: Sat, 9 Apr 2022 15:21:59 +0800 Subject: [PATCH] Fix lightmap, again. --- .../minecraft/MinecraftClientWrapper.java | 34 ----- .../minecraft/MinecraftRenderWrapper.java | 125 +++--------------- .../common/wrappers/misc/LightMapWrapper.java | 37 +++--- core | 2 +- .../fabric/mixins/events/MixinLightmap.java | 28 ++++ .../src/main/resources/fabric.lod.mixins.json | 3 +- .../lod/forge/mixins/MixinFogRenderer.java | 4 +- .../lod/forge/mixins/MixinLightmap.java | 28 ++++ forge/src/main/resources/lod.mixins.json | 3 +- 9 files changed, 100 insertions(+), 164 deletions(-) create mode 100644 fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinLightmap.java create mode 100644 forge/src/main/java/com/seibel/lod/forge/mixins/MixinLightmap.java diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftClientWrapper.java b/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftClientWrapper.java index e3615f0b5..cbf5b311d 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftClientWrapper.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftClientWrapper.java @@ -163,40 +163,6 @@ public class MinecraftClientWrapper implements IMinecraftClientWrapper return LodUtil.getDimensionIDFromWorld(WorldWrapper.getWorldWrapper(mc.level)); } - /** This texture changes every frame */ - @Override - public ILightMapWrapper getCurrentLightMap() - { - // get the current lightMap if the cache is empty - if (lightMap == null) - { - LightTexture tex = mc.gameRenderer.lightTexture(); - lightMap = tex.lightPixels; - } - return new LightMapWrapper(lightMap); - } - - /** - * Returns the color int at the given pixel coordinates - * from the current lightmap. - * @param blockLight x location in texture space - * @param skyLight z location in texture space - */ - @Override - public int getColorIntFromLightMap(int blockLight, int skyLight) - { - if (lightMap == null) - { - //sendChatMessage("new"); - // make sure the lightMap is up-to-date - getCurrentLightMap(); - } - - return lightMap.getPixelRGBA(blockLight, skyLight); - } - - - //=============// // Simple gets // //=============// diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftRenderWrapper.java b/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftRenderWrapper.java index 40a5669a9..d5b5daba1 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftRenderWrapper.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/minecraft/MinecraftRenderWrapper.java @@ -86,7 +86,9 @@ public class MinecraftRenderWrapper implements IMinecraftRenderWrapper private static final Minecraft MC = Minecraft.getInstance(); private static final GameRenderer GAME_RENDERER = MC.gameRenderer; private static final IWrapperFactory FACTORY = WrapperFactory.INSTANCE; - + + public LightMapWrapper lightmap = null; + @Override public Vec3f getLookAtVector() { @@ -216,16 +218,13 @@ public class MinecraftRenderWrapper implements IMinecraftRenderWrapper public boolean usingBackupGetVanillaRenderedChunks = false; @Override - public HashSet getVanillaRenderedChunks() - { + public HashSet getVanillaRenderedChunks() { ISodiumAccessor sodium = ModAccessorHandler.get(ISodiumAccessor.class); - if (sodium != null) - { + if (sodium != null) { return sodium.getNormalRenderedChunks(); } IOptifineAccessor optifine = ModAccessorHandler.get(IOptifineAccessor.class); - if (optifine != null) - { + if (optifine != null) { HashSet pos = optifine.getNormalRenderedChunks(); if (pos == null) pos = getMaximumRenderedChunks(); @@ -260,117 +259,18 @@ public class MinecraftRenderWrapper implements IMinecraftRenderWrapper + " Using Backup Method."); MinecraftClientWrapper.INSTANCE.sendChatMessage( "\u00A7eOverdraw prevention will be worse than normal."); - } catch (Exception e2) {} + } catch (Exception e2) { + } ApiShared.LOGGER.error("getVanillaRenderedChunks Error: ", e); usingBackupGetVanillaRenderedChunks = true; } } return getMaximumRenderedChunks(); } - - @Override - public int[] getLightmapPixels() - { - LightTexture tex = GAME_RENDERER.lightTexture(); - //tex.tick(); // This call makes no sense, but it fixes pause menu flicker bug - NativeImage lightMapPixels = tex.lightTexture.getPixels(); - LightMapWrapper lightMap = new LightMapWrapper(lightMapPixels); - - - int lightMapHeight = getLightmapTextureHeight(); - int lightMapWidth = getLightmapTextureWidth(); - - int[] pixels = new int[lightMapWidth * lightMapHeight]; - for (int u = 0; u < lightMapWidth; u++) - { - for (int v = 0; v < lightMapWidth; v++) - { - // this could probably be kept as a int, but - // it is easier to test and see the colors when debugging this way. - // When creating a new release this should be changed to the int version. - int col = lightMap.getLightValue(u, v); - - // these should both create a totally white image -// int col = -// Integer.MAX_VALUE; -// int col = -// 0b11111111 + // red -// (0b11111111 << 8) + // green -// (0b11111111 << 16) + // blue -// (0b11111111 << 24); // blue - -// int col = -// ((c.getRed() & 0xFF) << 16) | // blue -// ((c.getGreen() & 0xFF) << 8) | // green -// ((c.getBlue() & 0xFF)) | // red -// ((c.getAlpha() & 0xFF) << 24); // alpha - - // 2D array stored in a 1D array. - // Thank you Tim from College ;) - pixels[u * lightMapWidth + v] = col; - } - } - - return pixels; - } @Override public ILightMapWrapper getLightmapWrapper() { - return new LightMapWrapper(GAME_RENDERER.lightTexture()); - } - - - - @Override - public int getLightmapTextureHeight() - { - int height = -1; - - LightTexture lightTexture = GAME_RENDERER.lightTexture(); - if (lightTexture != null) - { - NativeImage tex = lightTexture.lightPixels; - if (tex != null) - { - height = tex.getHeight(); - } - } - - return height; - } - - @Override - public int getLightmapTextureWidth() - { - int width = -1; - - LightTexture lightTexture = GAME_RENDERER.lightTexture(); - if (lightTexture != null) - { - NativeImage tex = lightTexture.lightPixels; - if (tex != null) - { - width = tex.getWidth(); - } - } - - return width; - } - - - @Override - public int getLightmapGLFormat() { - int glFormat = -1; - - LightTexture lightTexture = GAME_RENDERER.lightTexture(); - if (lightTexture != null) { - NativeImage tex = lightTexture.lightPixels; - if (tex != null) { - glFormat = tex.format().glFormat(); - } - } - - return glFormat; + return lightmap; } @Override @@ -394,4 +294,11 @@ public class MinecraftRenderWrapper implements IMinecraftRenderWrapper public boolean tryDisableVanillaFog() { return true; // Handled via MixinFogRenderer in both forge and fabric } + + public void updateLightmap(NativeImage lightPixels) { + if (lightmap== null) { + lightmap = new LightMapWrapper(); + } + lightmap.uploadLightmap(lightPixels); + } } diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/misc/LightMapWrapper.java b/common/src/main/java/com/seibel/lod/common/wrappers/misc/LightMapWrapper.java index 360959ec9..b28922301 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/misc/LightMapWrapper.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/misc/LightMapWrapper.java @@ -22,41 +22,46 @@ package com.seibel.lod.common.wrappers.misc; import com.mojang.blaze3d.platform.NativeImage; import com.seibel.lod.core.wrapperInterfaces.misc.ILightMapWrapper; import net.minecraft.client.renderer.LightTexture; +import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL32; +import java.nio.ByteBuffer; + /** * @author James Seibel * @version 11-21-2021 */ public class LightMapWrapper implements ILightMapWrapper { - static NativeImage lightMap = null; + private int textureId = 0; - private LightTexture tex; - - public LightMapWrapper(NativeImage newLightMap) + public LightMapWrapper() { - lightMap = newLightMap; } - public LightMapWrapper(LightTexture lightTexture) { - tex = lightTexture; + private void createLightmap(NativeImage image) + { + textureId = GL32.glGenTextures(); + GL32.glBindTexture(GL32.GL_TEXTURE_2D, textureId); + GL32.glTexImage2D(GL32.GL_TEXTURE_2D, 0, image.format().glFormat(), image.getWidth(), image.getHeight(), + 0, image.format().glFormat(), GL32.GL_UNSIGNED_BYTE, (ByteBuffer) null); } - public static void setLightMap(NativeImage newLightMap) + public void uploadLightmap(NativeImage image) { - lightMap = newLightMap; - } - - @Override - public int getLightValue(int skyLight, int blockLight) - { - return lightMap.getPixelRGBA(skyLight, blockLight); + int currentBind = GL32.glGetInteger(GL32.GL_TEXTURE_BINDING_2D); + GL32.glBindTexture(GL32.GL_TEXTURE_2D, textureId); + if (textureId == 0) { + createLightmap(image); + } + // NativeImage::upload(int levelOfDetail, int xOffset, int yOffset, bool shouldCleanup?) + image.upload(0, 0, 0, false); + GL32.glBindTexture(GL32.GL_TEXTURE_2D, currentBind); } @Override public void bind() { - GL32.glBindTexture(GL32.GL_TEXTURE_2D, tex.lightTexture.getId()); + GL32.glBindTexture(GL32.GL_TEXTURE_2D, textureId); } @Override diff --git a/core b/core index 7eb0d62f7..25d2b9df7 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 7eb0d62f758e7ea4ed4e94e4bf34e3a288e7f2b1 +Subproject commit 25d2b9df7b72928be86e068dae1c736ed76ddca4 diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinLightmap.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinLightmap.java new file mode 100644 index 000000000..69f3f788a --- /dev/null +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinLightmap.java @@ -0,0 +1,28 @@ +package com.seibel.lod.fabric.mixins.events; + + +import com.mojang.blaze3d.platform.NativeImage; +import com.seibel.lod.common.wrappers.minecraft.MinecraftRenderWrapper; +import com.seibel.lod.core.api.ApiShared; +import net.minecraft.client.renderer.LightTexture; +import org.spongepowered.asm.mixin.Final; +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; + +@Mixin(LightTexture.class) +public class MixinLightmap { + @Shadow + @Final + public NativeImage lightPixels; + + @Inject(method="updateLightTexture", at=@At( + value="INVOKE", + target="Lnet/minecraft/client/renderer/texture/DynamicTexture;upload()V")) + public void updateLightTexture(float f, CallbackInfo ci) { + ApiShared.LOGGER.info("Lightmap update"); + MinecraftRenderWrapper.INSTANCE.updateLightmap(lightPixels); + } +} diff --git a/fabric/src/main/resources/fabric.lod.mixins.json b/fabric/src/main/resources/fabric.lod.mixins.json index a0978b22a..6cfe56a60 100644 --- a/fabric/src/main/resources/fabric.lod.mixins.json +++ b/fabric/src/main/resources/fabric.lod.mixins.json @@ -15,7 +15,8 @@ "MixinChunkGenerator", "events.MixinClientLevel", "events.MixinMinecraft", - "events.MixinBlockUpdate" + "events.MixinBlockUpdate", + "events.MixinLightmap" ], "server": [ "MixinDedicatedServer" diff --git a/forge/src/main/java/com/seibel/lod/forge/mixins/MixinFogRenderer.java b/forge/src/main/java/com/seibel/lod/forge/mixins/MixinFogRenderer.java index 6d9c64b1e..81c1c5fe3 100644 --- a/forge/src/main/java/com/seibel/lod/forge/mixins/MixinFogRenderer.java +++ b/forge/src/main/java/com/seibel/lod/forge/mixins/MixinFogRenderer.java @@ -47,8 +47,8 @@ public class MixinFogRenderer { // Using this instead of Float.MAX_VALUE because Sodium don't like it. (and copy paste in case someone in forge don't like it) - private static final float A_REALLY_REALLY_BIG_VALUE = 4206942069.F; - private static final float A_EVEN_LARGER_VALUE = 420694206942069.F; + private static final float A_REALLY_REALLY_BIG_VALUE = 420694206942069.F; + private static final float A_EVEN_LARGER_VALUE = 42069420694206942069.F; @Inject(at = @At("RETURN"), method = "setupFog(Lnet/minecraft/client/Camera;Lnet/minecraft/client/renderer/FogRenderer$FogMode;FZF)V", diff --git a/forge/src/main/java/com/seibel/lod/forge/mixins/MixinLightmap.java b/forge/src/main/java/com/seibel/lod/forge/mixins/MixinLightmap.java new file mode 100644 index 000000000..7ea3fbb06 --- /dev/null +++ b/forge/src/main/java/com/seibel/lod/forge/mixins/MixinLightmap.java @@ -0,0 +1,28 @@ +package com.seibel.lod.forge.mixins; + + +import com.mojang.blaze3d.platform.NativeImage; +import com.seibel.lod.common.wrappers.minecraft.MinecraftRenderWrapper; +import com.seibel.lod.core.api.ApiShared; +import net.minecraft.client.renderer.LightTexture; +import org.spongepowered.asm.mixin.Final; +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; + +@Mixin(LightTexture.class) +public class MixinLightmap { + @Shadow + @Final + public NativeImage lightPixels; + + @Inject(method="updateLightTexture", at=@At( + value="INVOKE", + target="Lnet/minecraft/client/renderer/texture/DynamicTexture;upload()V")) + public void updateLightTexture(float f, CallbackInfo ci) { + ApiShared.LOGGER.info("Lightmap update"); + MinecraftRenderWrapper.INSTANCE.updateLightmap(lightPixels); + } +} diff --git a/forge/src/main/resources/lod.mixins.json b/forge/src/main/resources/lod.mixins.json index 52fc11c53..3263b184c 100644 --- a/forge/src/main/resources/lod.mixins.json +++ b/forge/src/main/resources/lod.mixins.json @@ -9,7 +9,8 @@ "MixinOptionsScreen", "MixinWorldRenderer", "MixinFogRenderer", - "MixinChunkGenerator" + "MixinChunkGenerator", + "MixinLightmap" ], "server": [] }