diff --git a/src/main/java/me/cortex/voxy/client/core/gl/shader/AutoBindingShader.java b/src/main/java/me/cortex/voxy/client/core/gl/shader/AutoBindingShader.java index 753d0294..228fb48b 100644 --- a/src/main/java/me/cortex/voxy/client/core/gl/shader/AutoBindingShader.java +++ b/src/main/java/me/cortex/voxy/client/core/gl/shader/AutoBindingShader.java @@ -55,7 +55,7 @@ public class AutoBindingShader extends Shader { } public AutoBindingShader ssbo(int index, GlBuffer buffer, long offset) { - this.bindings.add(new BufferBinding(GL_SHADER_STORAGE_BUFFER, index, buffer, offset, -1)); + this.insertOrReplaceBinding(new BufferBinding(GL_SHADER_STORAGE_BUFFER, index, buffer, offset, -1)); return this; } @@ -69,10 +69,23 @@ public class AutoBindingShader extends Shader { } public AutoBindingShader ubo(int index, GlBuffer buffer, long offset) { - this.bindings.add(new BufferBinding(GL_UNIFORM_BUFFER, index, buffer, offset, -1)); + this.insertOrReplaceBinding(new BufferBinding(GL_UNIFORM_BUFFER, index, buffer, offset, -1)); return this; } + private void insertOrReplaceBinding(BufferBinding binding) { + //Check if there is already a binding at the index with the target, if so, replace it + for (int i = 0; i < this.bindings.size(); i++) { + var entry = this.bindings.get(i); + if (entry.target == binding.target && entry.index == binding.index) { + this.bindings.set(i, binding); + return; + } + } + + //Else add the new binding + this.bindings.add(binding); + } public AutoBindingShader texture(String define, GlTexture texture) { return this.texture(define, -1, texture); @@ -92,6 +105,7 @@ public class AutoBindingShader extends Shader { super.bind(); if (!this.bindings.isEmpty()) { for (var binding : this.bindings) { + binding.buffer.assertNotFreed(); if (binding.offset == 0 && binding.size == -1) { glBindBufferBase(binding.target, binding.index, binding.buffer.id); } else { @@ -102,6 +116,7 @@ public class AutoBindingShader extends Shader { if (!this.textureBindings.isEmpty()) { for (var binding : this.textureBindings) { if (binding.texture != null) { + binding.texture.assertNotFreed(); GlStateManager._activeTexture(GlConst.GL_TEXTURE0+binding.unit); GlStateManager._bindTexture(0); glBindTextureUnit(binding.unit, binding.texture.id); diff --git a/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java b/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java index ce5aba69..755f9ea8 100644 --- a/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java +++ b/src/main/java/me/cortex/voxy/client/mixin/sodium/MixinRenderSectionManager.java @@ -1,9 +1,6 @@ package me.cortex.voxy.client.mixin.sodium; import com.llamalad7.mixinextras.injector.ModifyReturnValue; -import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import me.cortex.voxy.client.VoxyClientInstance; import me.cortex.voxy.client.config.VoxyConfig; import me.cortex.voxy.client.core.IGetVoxyRenderSystem; @@ -11,10 +8,12 @@ import me.cortex.voxy.client.core.VoxyRenderSystem; import me.cortex.voxy.commonImpl.VoxyCommon; import net.caffeinemc.mods.sodium.client.gl.device.CommandList; import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection; +import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionFlags; import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionManager; import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionInfo; import net.minecraft.client.world.ClientWorld; import net.minecraft.util.math.ChunkPos; +import net.minecraft.util.math.ChunkSectionPos; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -48,15 +47,16 @@ public class MixinRenderSectionManager { } }*/ + /* @Inject(method = "onChunkRemoved", at = @At("HEAD")) private void voxy$trackChunkRemove(int x, int z, CallbackInfo ci) { if (this.level.worldRenderer != null) { var system = ((IGetVoxyRenderSystem)(this.level.worldRenderer)).getVoxyRenderSystem(); if (system != null) { - system.chunkBoundRenderer.removeChunk(ChunkPos.toLong(x, z)); + system.chunkBoundRenderer.removeSection(ChunkPos.toLong(x, z)); } } - } + }*/ @Inject(method = "onChunkRemoved", at = @At("HEAD")) private void injectIngest(int x, int z, CallbackInfo ci) { @@ -76,16 +76,27 @@ public class MixinRenderSectionManager { @Redirect(method = "updateSectionInfo", at = @At(value = "INVOKE", target = "Lnet/caffeinemc/mods/sodium/client/render/chunk/RenderSection;setInfo(Lnet/caffeinemc/mods/sodium/client/render/chunk/data/BuiltSectionInfo;)Z")) private boolean voxy$updateOnUpload(RenderSection instance, BuiltSectionInfo info) { - boolean retVal = instance.setInfo(info); - if (!retVal) { + boolean wasBuilt = instance.isBuilt(); + int flags = instance.getFlags(); + if (!instance.setInfo(info)) { return false; } - if (info == null || info == BuiltSectionInfo.EMPTY) { + if (wasBuilt == instance.isBuilt()) {//Only want to do stuff on change return true; } + flags |= instance.getFlags(); + if (flags == 0)//Only process things with stuff + return true; + VoxyRenderSystem system = ((IGetVoxyRenderSystem)(this.level.worldRenderer)).getVoxyRenderSystem(); - if (system != null) { - system.chunkBoundRenderer.addChunk(ChunkPos.toLong(instance.getChunkX(), instance.getChunkZ())); + if (system == null) { + return true; + } + long pos = ChunkSectionPos.asLong(instance.getChunkX(), instance.getChunkY(), instance.getChunkZ()); + if (wasBuilt) {//Remove + system.chunkBoundRenderer.removeSection(pos); + } else {//Add + system.chunkBoundRenderer.addSection(pos); } return true; } @@ -95,6 +106,6 @@ public class MixinRenderSectionManager { if (((IGetVoxyRenderSystem)(this.level.worldRenderer)).getVoxyRenderSystem() == null) { return searchDistance; } - return searchDistance + 20; + return searchDistance + 32; } } diff --git a/src/main/resources/assets/voxy/shaders/chunkoutline/outline.vsh b/src/main/resources/assets/voxy/shaders/chunkoutline/outline.vsh index 1f00834b..4a85dcef 100644 --- a/src/main/resources/assets/voxy/shaders/chunkoutline/outline.vsh +++ b/src/main/resources/assets/voxy/shaders/chunkoutline/outline.vsh @@ -10,16 +10,20 @@ layout(binding = 1, std430) restrict readonly buffer ChunkPosBuffer { ivec2[] chunkPos; }; +ivec3 unpackPos(ivec2 pos) { + return ivec3(pos.y>>10, (pos.x<<12)>>12, ((pos.y<<22)|int(uint(pos.x)>>10))>>10); +} + void main() { uint id = (gl_InstanceID<<5)+gl_BaseInstance+(gl_VertexID>>3); - ivec3 origin = ivec3(chunkPos[id], 0).xzy; + ivec3 origin = unpackPos(chunkPos[id]); origin -= section.xyz; ivec3 cubeCornerI = ivec3(gl_VertexID&1, (gl_VertexID>>2)&1, (gl_VertexID>>1)&1); //Expand the y height to be big (will be +- 8192) //TODO: make it W.R.T world height and offsets - cubeCornerI.y = cubeCornerI.y*1024-512; + //cubeCornerI.y = cubeCornerI.y*1024-512; gl_Position = MVP * vec4(vec3(cubeCornerI+origin)*16, 1); - gl_Position.z -= 0.0001f; + gl_Position.z -= 0.0005f; } \ No newline at end of file