Very Close

This commit is contained in:
mcrcortex
2024-08-04 19:17:56 +10:00
parent 264e26cb90
commit 0bb04b2f8e
4 changed files with 79 additions and 24 deletions

View File

@@ -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<BasicViewport, BasicSectionGeometryManager> {
public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, BasicSectionGeometryManager> {
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<BasicViewport,
LightMapHelper.bind(5);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedIndexBuffer.INSTANCE.id());
//glBindBuffer(GL_DRAW_INDIRECT_BUFFER, this.glCommandBuffer.id);
//glBindBuffer(GL_PARAMETER_BUFFER_ARB, this.glCommandCountBuffer.id);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, this.drawCallBuffer.id);
glBindBuffer(GL_PARAMETER_BUFFER_ARB, this.drawCountBuffer.id);
}
//Prep the terrain draw calls for this frame, also sets up the
// remaining render pipeline for this frame
private void prepTerrainCallsAndPrep() {
}
private void renderTerrain() {
RenderLayer.getCutoutMipped().startDrawing();
@@ -97,12 +102,9 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<BasicViewport,
glBindVertexArray(RenderService.STATIC_VAO);//Needs to be before binding
this.bindRenderingBuffers();
glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, 1000*6, GL_UNSIGNED_SHORT, 0,1,0,0);
glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, 0, Math.min((int)(this.geometryManager.getSectionCount()*4.4), 400_000), 0);
glEnable(GL_CULL_FACE);
glBindVertexArray(0);
glBindSampler(0, 0);
glBindTextureUnit(0, 0);
@@ -110,27 +112,45 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<BasicViewport,
}
@Override
public void renderOpaque(BasicViewport viewport) {
public void renderOpaque(MDICViewport viewport) {
if (this.geometryManager.getSectionCount() == 0) return;
//Tick the geometry manager to upload all invalidated metadata changes to the gpu before invoking the command gen shader
this.geometryManager.tick();
this.uploadUniformBuffer(viewport);
//TODO compute the draw calls
{
this.commandGen.bind();
glBindBufferBase(GL_UNIFORM_BUFFER, 0, this.uniform.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, this.drawCallBuffer.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, this.drawCountBuffer.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, this.geometryManager.getMetadataBufferId());
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, viewport.visibilityBuffer.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, viewport.indirectLookupBuffer.id);
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, this.drawCountBuffer.id);
glDispatchComputeIndirect(0);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
}
this.renderTerrain();
}
@Override
public void buildDrawCallsAndRenderTemporal(BasicViewport viewport, GlBuffer sectionRenderList) {
public void buildDrawCallsAndRenderTemporal(MDICViewport viewport, GlBuffer sectionRenderList) {
//Can do a sneeky trick, since the sectionRenderList is a list to things to render, it invokes the culler
// which only marks visible sections
//Tick the geometry manager to upload all invalidated metadata changes to the gpu
this.geometryManager.tick();
this.renderTerrain();
}
@Override
public void renderTranslucent(BasicViewport viewport) {
public void renderTranslucent(MDICViewport viewport) {
}
@@ -141,8 +161,8 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<BasicViewport,
}
@Override
public BasicViewport createViewport() {
return new BasicViewport();
public MDICViewport createViewport() {
return new MDICViewport();
}
@Override
@@ -150,5 +170,6 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<BasicViewport,
super.free();
this.uniform.free();
this.terrainShader.free();
this.commandGen.free();
}
}

View File

@@ -0,0 +1,15 @@
package me.cortex.voxy.client.core.rendering.section;
import me.cortex.voxy.client.core.gl.GlBuffer;
import me.cortex.voxy.client.core.rendering.Viewport;
public class MDICViewport extends Viewport<MDICViewport> {
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();
}
}

View File

@@ -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

View File

@@ -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 <voxy:lod/quad_format.glsl>
#import <voxy:lod/gl46/bindings.glsl>
#import <voxy:lod/section.glsl>
@@ -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<<detail)-baseSectionPos)<<5)-cameraSubPos;
//Note! its not with respect to the sectionId
//
//Check the occlusion data from last frame
bool shouldRender = visibilityData[gl_GlobalInvocationID.x] == frameId - 1;