diff --git a/Common/src/main/java/com/lowdragmc/shimmer/client/light/LightManager.java b/Common/src/main/java/com/lowdragmc/shimmer/client/light/LightManager.java index 8a41bc6..f3c1eea 100644 --- a/Common/src/main/java/com/lowdragmc/shimmer/client/light/LightManager.java +++ b/Common/src/main/java/com/lowdragmc/shimmer/client/light/LightManager.java @@ -206,6 +206,9 @@ public void renderLevelPre(int blockLightSize, float camX, float camY, float cam env.bufferSubData(0, new int[]{UV_LIGHT.size() + blockLightSize}); env.bufferSubData(4,new int[]{NO_UV_LIGHT_COUNT}); env.bufferSubData(16, new float[]{camX, camY, camZ}); + + light.bindIndex(irisHandle.getLightsIndex()); + env.bindIndex(irisHandle.getEnvIndex()); } }else { envUBO.bufferSubData(0,new int[4]); @@ -217,8 +220,10 @@ public void renderLevelPre(int blockLightSize, float camX, float camY, float cam public void updateNoUVLight(){ LocalPlayer localPlayer = Minecraft.getInstance().player; + if (localPlayer == null) return; NO_UV_LIGHT_COUNT = 0; Vec3 localPlayerPosition = localPlayer.position(); + if (Minecraft.getInstance().level == null) return; List players = Minecraft.getInstance().level.players(); float partialTicks = Minecraft.getInstance().getFrameTime(); for (AbstractClientPlayer player : players) { @@ -328,6 +333,7 @@ public ColorPointLight addLight(Vector3f pos, int color, float radius, boolean u } @Nullable + @SuppressWarnings("unused") public ColorPointLight addLight(Vector3f pos, int color, float radius) { return addLight(pos, color, radius, false); } @@ -424,7 +430,7 @@ public void registerFluidLight(Fluid fluid, int color, float radius) { public void loadConfig() { ITEM_MAP.clear(); TAG_MAP.clear(); - BLOCK_MAP.clear();; + BLOCK_MAP.clear(); FLUID_MAP.clear(); for (var config : Configuration.configs){ @@ -437,7 +443,8 @@ public void loadConfig() { if (blockLight.hasState()){ - if (Utils.checkBlockProperties(config.configSource, blockLight.state, blockPair.first())) continue; + assert blockLight.state != null; + if (Utils.checkBlockProperties(config.configSource, blockLight.state, blockPair.first())) continue; List validStates = Utils.getAvailableStates(blockLight.state, block); var light = new ColorPointLight.Template(blockLight.radius,blockLight.color()); @@ -550,6 +557,7 @@ public void registerTagLight(ResourceLocation tag,Function set = NO_UV_LIGHT_PLAYER.keySet(); for (UUID uuid : set) { diff --git a/Common/src/main/java/com/lowdragmc/shimmer/comp/iris/IrisHandle.java b/Common/src/main/java/com/lowdragmc/shimmer/comp/iris/IrisHandle.java index 45c0264..e1fd0e2 100644 --- a/Common/src/main/java/com/lowdragmc/shimmer/comp/iris/IrisHandle.java +++ b/Common/src/main/java/com/lowdragmc/shimmer/comp/iris/IrisHandle.java @@ -17,12 +17,19 @@ public interface IrisHandle { */ void updateInfo(Object buffers); + /** + * don't destroy ssbo buffer, it will be destroyed by iris since we delegate to it + */ void onSSBODestroyed(); void setLightsIndex(int index); + int getLightsIndex(); + void setEnvIndex(int index); + int getEnvIndex(); + static T make(Supplier supplier) { return supplier.get(); } @@ -58,10 +65,14 @@ static void analyzeShaderProperties(String shaderProperties) { try { final int index = Integer.parseInt(analyze[1]); switch (analyze[0]) { - case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_LIGHTS_IDENTIFIER -> - INSTANCE.setLightsIndex(index); - case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_ENV_IDENTIFIER -> - INSTANCE.setEnvIndex(index); + case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_LIGHTS_IDENTIFIER -> { + ShimmerConstants.LOGGER.info("detect LIGHT BUFFER ssbo index:" + index); + INSTANCE.setLightsIndex(index); + } + case ShimmerConstants.SHIMMER_SHADERS_PROPERTIES_ENV_IDENTIFIER -> { + ShimmerConstants.LOGGER.info("detect ENV BUFFER ssbo index:" + index); + INSTANCE.setEnvIndex(index); + } } } catch (NumberFormatException ignored) { diff --git a/Fabric/src/main/java/com/lowdragmc/shimmer/fabric/compact/iris/FabricIrisHandle.java b/Fabric/src/main/java/com/lowdragmc/shimmer/fabric/compact/iris/FabricIrisHandle.java index 50bd9ca..26dcdef 100644 --- a/Fabric/src/main/java/com/lowdragmc/shimmer/fabric/compact/iris/FabricIrisHandle.java +++ b/Fabric/src/main/java/com/lowdragmc/shimmer/fabric/compact/iris/FabricIrisHandle.java @@ -11,24 +11,35 @@ import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL46; -import org.lwjgl.system.MemoryStack; -import org.lwjgl.system.MemoryUtil; +@SuppressWarnings("unused") public class FabricIrisHandle implements IrisHandle { + /** + * must have this, this is called by reflect + */ public FabricIrisHandle() { } private boolean available = MixinPluginShared.IS_IRIS_LOAD; @Nullable private Pair ssbos; + /** + * global ssbo index + */ private int lightIndex = -1; private int envIndex = -1; @Override public void updateInfo(Object buffers) { if (!available || !ShimmerConstants.IRIS_COMPACT_ENABLE) return; + if (lightIndex == -1 || envIndex == -1) { + ShimmerConstants.LOGGER.info("env buffer not set fully, light:{}, env:{}", lightIndex, envIndex); + ShimmerConstants.LOGGER.info("shimmer shader support for colored light with ssbo is now offline"); + return; + } if (buffers instanceof ShaderStorageBuffer[] suffers) { + //index in the ShaderStorageBuffer[] int lightBufferIndex = -1; int envBufferIndex = -1; for (int i = 0; i < suffers.length; i++) { @@ -47,30 +58,18 @@ public void updateInfo(Object buffers) { } } } - if (lightBufferIndex == -1 || envBufferIndex == -1) return; + if (lightBufferIndex == -1 || envBufferIndex == -1) { + ShimmerConstants.LOGGER.error("failed to detect ssbo created by iris"); + return; + } else { + ShimmerConstants.LOGGER.info("detect ssbo created by iris success"); + } int finalLightBufferIndex = lightBufferIndex; int finalEnvBufferIndex = envBufferIndex; + if (checkIsRelative(suffers[lightBufferIndex], "light") || checkIsRelative(suffers[envBufferIndex],"env")) return; RenderUtils.warpGLDebugLabel("initSSBO", () -> { - var lightBuffer = new ShaderSSBO(); - //no need to call glShaderStorageBlockBinding here, as we set layout in shader explicitly - { - var oldBuffer = suffers[finalLightBufferIndex]; - lightBuffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY); - lightBuffer.bindIndex(oldBuffer.getIndex()); - ((ShaderStorageBufferAccessor)oldBuffer).callDestroy(); - suffers[finalLightBufferIndex] = new ShaderStorageBuffer(lightBuffer.id, ((ShaderStorageBufferAccessor)oldBuffer).getInfo()); - } - var envBuffer = new ShaderSSBO(); - { - var oldBuffer = suffers[finalEnvBufferIndex]; - envBuffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY); - envBuffer.bufferSubData(0,new int[8]); - envBuffer.bindIndex(oldBuffer.getIndex()); - ((ShaderStorageBufferAccessor)oldBuffer).callDestroy(); - suffers[finalEnvBufferIndex] = new ShaderStorageBuffer(envBuffer.id, ((ShaderStorageBufferAccessor)oldBuffer).getInfo()); - } - suffers[finalLightBufferIndex].bind(); - suffers[finalEnvBufferIndex].bind(); + var lightBuffer = replaceSSBO(suffers, finalLightBufferIndex); + var envBuffer = replaceSSBO(suffers, finalEnvBufferIndex); ssbos = Pair.of(lightBuffer, envBuffer); }); } else { @@ -78,15 +77,34 @@ public void updateInfo(Object buffers) { } } + private static boolean checkIsRelative(ShaderStorageBuffer buffer, String name) { + if (((ShaderStorageBufferAccessor)buffer).getInfo().relative()) { + ShimmerConstants.LOGGER.error("expect buffer:{} not relative", name); + return true; + } { + return false; + } + } + + private static ShaderSSBO replaceSSBO(ShaderStorageBuffer[] suffers, int replaceIndex) { + var oldBuffer = suffers[replaceIndex]; + //destroy origin + ((ShaderStorageBufferAccessor) oldBuffer).callDestroy(); + //create ours + var buffer = new ShaderSSBO(); + buffer.createBufferData(oldBuffer.getSize(), GL46.GL_DYNAMIC_COPY); + buffer.bindIndex(oldBuffer.getIndex()); + //warp and bind + var irisSSBO = new ShaderStorageBuffer(buffer.id, ((ShaderStorageBufferAccessor) oldBuffer).getInfo()); + suffers[replaceIndex] = irisSSBO; + irisSSBO.bind(); + + return buffer; + } + @Override public void onSSBODestroyed() { - if (ssbos != null) { - ssbos.getLeft().close(); - ssbos.getRight().close(); - ssbos = null; - } - lightIndex = -1; - envIndex = -1; + ssbos = null; } @Override @@ -94,11 +112,21 @@ public void setLightsIndex(int index) { this.lightIndex = index; } + @Override + public int getLightsIndex() { + return lightIndex; + } + @Override public void setEnvIndex(int index) { this.envIndex = index; } + @Override + public int getEnvIndex() { + return envIndex; + } + @Override @Nullable public Pair getBuffer() { diff --git a/Forge/src/main/java/com/lowdragmc/shimmer/forge/compat/oculus/ForgeOculusHandle.java b/Forge/src/main/java/com/lowdragmc/shimmer/forge/compat/oculus/ForgeOculusHandle.java index 9925171..b49f7bc 100644 --- a/Forge/src/main/java/com/lowdragmc/shimmer/forge/compat/oculus/ForgeOculusHandle.java +++ b/Forge/src/main/java/com/lowdragmc/shimmer/forge/compat/oculus/ForgeOculusHandle.java @@ -11,20 +11,32 @@ import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL46; +@SuppressWarnings("unused") public class ForgeOculusHandle implements IrisHandle { + /** + * must have this, this is called by reflect + */ public ForgeOculusHandle() { } private boolean available = MixinPluginShared.IS_OCULUS_LOAD; @Nullable private Pair ssbos; + /** + * global ssbo index + */ private int lightIndex = -1; private int envIndex = -1; @Override public void updateInfo(Object buffers) { if (!available || !ShimmerConstants.IRIS_COMPACT_ENABLE) return; + if (lightIndex == -1 || envIndex == -1) { + ShimmerConstants.LOGGER.info("env buffer not set fully, light:{}, env:{}", lightIndex, envIndex); + ShimmerConstants.LOGGER.info("shimmer shader support for colored light with ssbo is now offline"); + return; + } if (buffers instanceof ShaderStorageBuffer[] suffers) { int lightBufferIndex = -1; int envBufferIndex = -1; @@ -44,11 +56,16 @@ public void updateInfo(Object buffers) { } } } - if (lightBufferIndex == -1 || envBufferIndex == -1) return; + if (lightBufferIndex == -1 || envBufferIndex == -1) { + ShimmerConstants.LOGGER.error("failed to detect ssbo created by iris"); + return; + } else { + ShimmerConstants.LOGGER.info("detect ssbo created by iris success"); + } int finalLightBufferIndex = lightBufferIndex; int finalEnvBufferIndex = envBufferIndex; RenderUtils.warpGLDebugLabel("initSSBO", () -> { - var lightBuffer = new ShaderSSBO(); + var lightBuffer = new ShaderSSBO();//TODO add check after oculus follows iris //no need to call glShaderStorageBlockBinding here, as we set layout in shader explicitly { var oldBuffer = suffers[finalLightBufferIndex]; @@ -75,15 +92,11 @@ public void updateInfo(Object buffers) { } } + + @Override public void onSSBODestroyed() { - if (ssbos != null) { - ssbos.getLeft().close(); - ssbos.getRight().close(); - ssbos = null; - } - lightIndex = -1; - envIndex = -1; + ssbos = null; } @Override @@ -91,11 +104,21 @@ public void setLightsIndex(int index) { this.lightIndex = index; } + @Override + public int getLightsIndex() { + return lightIndex; + } + @Override public void setEnvIndex(int index) { this.envIndex = index; } + @Override + public int getEnvIndex() { + return envIndex; + } + @Override @Nullable public Pair getBuffer() {