From 0bb04b2f8ed00a3e577ee28ba083f978cde38381 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Sun, 4 Aug 2024 19:17:56 +1000 Subject: [PATCH] Very Close --- .../section/MDICSectionRenderer.java | 67 ++++++++++++------- .../core/rendering/section/MDICViewport.java | 15 +++++ .../voxy/shaders/lod/gl46/bindings.glsl | 10 +++ .../assets/voxy/shaders/lod/gl46/cmdgen.comp | 11 ++- 4 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 src/main/java/me/cortex/voxy/client/core/rendering/section/MDICViewport.java 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 e7e60499..6336fa40 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 @@ -22,6 +22,7 @@ import org.lwjgl.system.MemoryUtil; import java.util.List; import static org.lwjgl.opengl.ARBIndirectParameters.GL_PARAMETER_BUFFER_ARB; +import static org.lwjgl.opengl.ARBIndirectParameters.glMultiDrawElementsIndirectCountARB; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; import static org.lwjgl.opengl.GL15.glBindBuffer; @@ -32,23 +33,32 @@ import static org.lwjgl.opengl.GL32.glDrawElementsInstancedBaseVertex; import static org.lwjgl.opengl.GL33.glBindSampler; import static org.lwjgl.opengl.GL40C.GL_DRAW_INDIRECT_BUFFER; import static org.lwjgl.opengl.GL42.glDrawElementsInstancedBaseVertexBaseInstance; -import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BUFFER; +import static org.lwjgl.opengl.GL43.*; import static org.lwjgl.opengl.GL45.glBindTextureUnit; //Uses MDIC to render the sections -public class MDICSectionRenderer extends AbstractSectionRenderer { +public class MDICSectionRenderer extends AbstractSectionRenderer { private final Shader terrainShader = Shader.make() .add(ShaderType.VERTEX, "voxy:lod/gl46/quads2.vert") .add(ShaderType.FRAGMENT, "voxy:lod/gl46/quads.frag") .compile(); + + + private final Shader commandGen = Shader.make() + .add(ShaderType.COMPUTE, "voxy:lod/gl46/cmdgen.comp") + .compile(); + private final GlBuffer uniform = new GlBuffer(1024).zero(); + private final GlBuffer drawCountBuffer = new GlBuffer(64).zero(); + private final GlBuffer drawCallBuffer = new GlBuffer(5*4*400000).zero();//400k draw calls + public MDICSectionRenderer(ModelStore modelStore, int maxSectionCount, long geometryCapacity) { super(modelStore, new BasicSectionGeometryManager(maxSectionCount, geometryCapacity)); } - private void uploadUniformBuffer(BasicViewport viewport) { + private void uploadUniformBuffer(MDICViewport viewport) { long ptr = UploadStream.INSTANCE.upload(this.uniform, 0, 1024); int sx = MathHelper.floor(viewport.cameraX)>>5; @@ -77,16 +87,11 @@ public class MDICSectionRenderer extends AbstractSectionRenderer { + public final GlBuffer visibilityBuffer = new GlBuffer(100_000*4); + public final GlBuffer indirectLookupBuffer = new GlBuffer(100_000*4+4); + + @Override + protected void delete0() { + this.visibilityBuffer.free(); + this.indirectLookupBuffer.free(); + } +} diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl b/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl index d3f23a60..6a73788c 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl @@ -57,6 +57,9 @@ layout(binding = DRAW_BUFFER_BINDING, std430) writeonly restrict buffer DrawBuff #ifdef DRAW_COUNT_BUFFER_BINDING layout(binding = DRAW_COUNT_BUFFER_BINDING, std430) restrict buffer DrawCommandCountBuffer { + uint cmdGenDispatchX; + uint cmdGenDispatchY; + uint cmdGenDispatchZ; uint opaqueDrawCount; uint translucentDrawCount; }; @@ -68,6 +71,13 @@ layout(binding = SECTION_METADA_BUFFER_BINDING, std430) readonly restrict buffer }; #endif +#ifdef INDIRECT_SECTION_LOOKUP_BINDING +layout(binding = INDIRECT_SECTION_LOOKUP_BINDING, std430) readonly restrict buffer IndirectSectionLookupBuffer { + uint sectionCount; + uint indirectLookup[]; +}; +#endif + #ifndef VISIBILITY_ACCESS #define VISIBILITY_ACCESS readonly #endif 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 1c58b73d..dcfdef0e 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/cmdgen.comp +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/cmdgen.comp @@ -3,6 +3,12 @@ layout(local_size_x = 128) in; +#define DRAW_BUFFER_BINDING 1 +#define DRAW_COUNT_BUFFER_BINDING 2 +#define SECTION_METADA_BUFFER_BINDING 3 +#define VISIBILITY_BUFFER_BINDING 4 +#define INDIRECT_SECTION_LOOKUP_BINDING 5 + #import #import #import @@ -42,13 +48,16 @@ void main() { if (gl_GlobalInvocationID.x >= sectionCount) { return; } - SectionMeta meta = sectionData[gl_GlobalInvocationID.x]; + uint sectionId = indirectLookup[gl_GlobalInvocationID.x]; + SectionMeta meta = sectionData[sectionId]; uint detail = extractDetail(meta); ivec3 ipos = extractPosition(meta); vec3 cornerPos = vec3(((ipos<