From bbe7cd2099fff9771150215794fbbe93df85b2cc Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Fri, 25 Apr 2025 18:30:07 +1000 Subject: [PATCH] Temporal coherance --- .../section/MDICSectionRenderer.java | 25 ++++++++--- .../core/rendering/section/MDICViewport.java | 6 ++- .../assets/voxy/shaders/lod/gl46/cmdgen.comp | 44 ++++++++++++++----- .../voxy/shaders/lod/gl46/cull/raster.frag | 4 +- .../voxy/shaders/lod/gl46/cull/raster.vert | 11 +++-- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/section/MDICSectionRenderer.java b/src/main/java/me/cortex/voxy/client/core/rendering/section/MDICSectionRenderer.java index c4874b07..d906c9ba 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/section/MDICSectionRenderer.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/section/MDICSectionRenderer.java @@ -11,6 +11,7 @@ import me.cortex.voxy.client.core.rendering.RenderService; import me.cortex.voxy.client.core.rendering.SharedIndexBuffer; import me.cortex.voxy.client.core.rendering.util.DownloadStream; import me.cortex.voxy.client.core.rendering.util.UploadStream; +import me.cortex.voxy.common.Logger; import net.minecraft.client.render.RenderLayer; import net.minecraft.util.math.MathHelper; import org.joml.Matrix4f; @@ -36,6 +37,7 @@ import static org.lwjgl.opengl.GL45.glCopyNamedBufferSubData; //Uses MDIC to render the sections public class MDICSectionRenderer extends AbstractSectionRenderer { private static final int TRANSLUCENT_OFFSET = 400_000;//in draw calls + private static final int TEMPORAL_OFFSET = 500_000;//in draw calls private static final int STATISTICS_BUFFER_BINDING = 7; private final Shader terrainShader = Shader.make() .defineIf("DEBUG_RENDER", false) @@ -45,6 +47,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer { - public final GlBuffer visibilityBuffer = new GlBuffer(100_000*4); public final GlBuffer indirectLookupBuffer = new GlBuffer(100_000*4+4); + public final GlBuffer visibilityBuffer; + + public MDICViewport(int maxSectionCount) { + this.visibilityBuffer = new GlBuffer(maxSectionCount*4L); + } @Override protected void delete0() { diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/cmdgen.comp b/src/main/resources/assets/voxy/shaders/lod/gl46/cmdgen.comp index ddaa225f..770ee733 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/cmdgen.comp +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/cmdgen.comp @@ -59,20 +59,16 @@ void main() { vec3 cornerPos = vec3(((ipos<>16)&0xFFFF)!=0)<<6; - uint cmdPtr = atomicAdd(opaqueDrawCount, bitCount(msk)); + uint cmdCnt = bitCount(msk); + uint cmdPtr = atomicAdd(opaqueDrawCount, cmdCnt); + uint tCmdPtr = 0; + if (renderTemporally) { + tCmdPtr = atomicAdd(temporalOpaqueDrawCount, cmdCnt) + TEMPORAL_OFFSET; + } + + //TODO: make totalQuads also have temporal amount #ifdef HAS_STATISTICS uint totalQuads = 0; #endif @@ -117,6 +120,9 @@ void main() { count = (meta.cntA>>16)&0xFFFF; if (count != 0) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif @@ -127,6 +133,9 @@ void main() { count = (meta.cntB)&0xFFFF; if (((msk&(1u<<0))!=0)) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif @@ -137,6 +146,9 @@ void main() { count = (meta.cntB>>16)&0xFFFF; if ((msk&(1u<<1))!=0) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif @@ -147,6 +159,9 @@ void main() { count = (meta.cntC)&0xFFFF; if ((msk&(1u<<2))!=0) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif @@ -157,6 +172,9 @@ void main() { count = (meta.cntC>>16)&0xFFFF; if ((msk&(1u<<3))!=0) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif @@ -167,6 +185,9 @@ void main() { count = (meta.cntD)&0xFFFF; if ((msk&(1u<<4))!=0) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif @@ -177,6 +198,9 @@ void main() { count = (meta.cntD>>16)&0xFFFF; if ((msk&(1u<<5))!=0) { writeCmd(cmdPtr++, drawId, ptr, count); + if (renderTemporally) { + writeCmd(tCmdPtr++, drawId, ptr, count); + } #ifdef HAS_STATISTICS totalQuads += count; #endif diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.frag b/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.frag index 1350fe9c..0323b7dd 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.frag +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.frag @@ -7,9 +7,9 @@ layout(early_fragment_tests) in; flat in uint id; flat in uint value; -out vec4 colour; +//out vec4 colour; void main() { visibilityData[id] = value; - colour = vec4(float(id&7u)/7, float((id>>3)&7u)/7, float((id>>6)&7u)/7, 1); + //colour = vec4(float(id&7u)/7, float((id>>3)&7u)/7, float((id>>6)&7u)/7, 1); } \ No newline at end of file diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.vert b/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.vert index 532bd16d..d2ff8924 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.vert +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/cull/raster.vert @@ -1,6 +1,6 @@ #version 460 core #extension GL_ARB_gpu_shader_int64 : enable -#define VISIBILITY_ACCESS writeonly +#define VISIBILITY_ACCESS readonly #define SECTION_METADATA_BUFFER_BINDING 1 #define VISIBILITY_BUFFER_BINDING 2 @@ -29,7 +29,10 @@ void main() { gl_Position = MVP * vec4(vec3(pos),1); - //Write to this id - id = gl_InstanceID;//Note!! we write to the instance id _not_ the section id - value = frameId; + //Write to the section id, to track temporal over time (litterally just need a single bit, 1 fking bit, but no) + id = sid; + + uint previous = visibilityData[sid]&0x7fffffffu; + bool wasVisibleLastFrame = previous==(frameId-1); + value = (frameId&0x7fffffffu)|(uint(wasVisibleLastFrame)<<31);//Encode if it was visible last frame } \ No newline at end of file