Render basics

This commit is contained in:
mcrcortex
2024-08-04 22:08:49 +10:00
parent 0bb04b2f8e
commit e4fe056586
12 changed files with 120 additions and 33 deletions

View File

@@ -113,7 +113,7 @@ public class VoxelCore {
matrices.translate(-cameraX, -cameraY, -cameraZ);
matrices.pop();
var projection = computeProjectionMat();
var projection = computeProjectionMat();//RenderSystem.getProjectionMatrix();
var viewport = this.renderer.getViewport();
viewport
@@ -121,6 +121,7 @@ public class VoxelCore {
.setModelView(matrices.peek().getPositionMatrix())
.setCamera(cameraX, cameraY, cameraZ)
.setScreenSize(MinecraftClient.getInstance().getFramebuffer().textureWidth, MinecraftClient.getInstance().getFramebuffer().textureHeight);
viewport.frameId++;
int boundFB = GL11.glGetInteger(GL_DRAW_FRAMEBUFFER_BINDING);
if (boundFB == 0) {

View File

@@ -55,8 +55,8 @@ public class RenderService<T extends AbstractSectionRenderer<J, ?>, J extends Vi
world.setDirtyCallback(this.nodeManager::sectionUpdate);
for(int x = -10; x<=10;x++) {
for (int z = -10; z <= 10; z++) {
for(int x = -1; x<=1;x++) {
for (int z = -1; z <= 1; z++) {
for (int y = -3; y <= 3; y++) {
this.renderGen.enqueueTask(0, x, y, z);
}
@@ -104,7 +104,7 @@ public class RenderService<T extends AbstractSectionRenderer<J, ?>, J extends Vi
int depthBuffer = glGetFramebufferAttachmentParameteri(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME);
this.traversal.doTraversal(viewport, depthBuffer);
this.sectionRenderer.buildDrawCallsAndRenderTemporal(viewport, null);
this.sectionRenderer.buildDrawCallsAndRenderTemporal(viewport, this.traversal.getRenderListBuffer());
}
public void renderFarAwayTranslucent(J viewport) {

View File

@@ -22,7 +22,8 @@ public class HierarchicalOcclusionTraverser {
private final GlBuffer requestBuffer;
private final GlBuffer nodeBuffer;
private final GlBuffer uniformBuffer = new GlBuffer(1024);
private final GlBuffer uniformBuffer = new GlBuffer(1024).zero();
private final GlBuffer renderList = new GlBuffer(100_000 * 4 + 4).zero();//100k sections max to render
private final HiZBuffer hiZBuffer = new HiZBuffer();
@@ -50,6 +51,15 @@ public class HierarchicalOcclusionTraverser {
// TODO: swap to persistent gpu thread instead
long uploadPtr = UploadStream.INSTANCE.upload(this.renderList, 0, 1024);
MemoryUtil.memPutInt(uploadPtr, 1024/4-1);
for (int i = 1; i < 1024/4; i++) {
MemoryUtil.memPutInt(uploadPtr + 4*i, i-1);
}
UploadStream.INSTANCE.commit();
this.downloadResetRequestQueue();
}
@@ -60,6 +70,10 @@ public class HierarchicalOcclusionTraverser {
nglClearNamedBufferSubData(this.requestBuffer.id, GL_R32UI, 0, 4, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
}
public GlBuffer getRenderListBuffer() {
return this.renderList;
}
private void forwardDownloadResult(long ptr, long size) {
int count = MemoryUtil.memGetInt(ptr);
if (count < 0 || count > 50000) {
@@ -81,5 +95,6 @@ public class HierarchicalOcclusionTraverser {
this.hiZBuffer.free();
this.nodeBuffer.free();
this.uniformBuffer.free();
this.renderList.free();
}
}

View File

@@ -4,7 +4,6 @@ package me.cortex.voxy.client.core.rendering.section;
import me.cortex.voxy.client.core.gl.GlBuffer;
import me.cortex.voxy.client.core.model.ModelStore;
import me.cortex.voxy.client.core.rendering.Viewport;
import me.cortex.voxy.client.core.rendering.geometry.OLD.AbstractGeometryManager;
import java.util.List;

View File

@@ -5,7 +5,6 @@ import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.cortex.voxy.client.core.gl.GlBuffer;
import me.cortex.voxy.client.core.rendering.building.BuiltSection;
import me.cortex.voxy.client.core.rendering.geometry.OLD.DefaultGeometryManager;
import me.cortex.voxy.client.core.rendering.util.BufferArena;
import me.cortex.voxy.client.core.rendering.util.UploadStream;
import me.cortex.voxy.common.util.HierarchicalBitSet;

View File

@@ -8,11 +8,7 @@ import me.cortex.voxy.client.core.model.ModelStore;
import me.cortex.voxy.client.core.rendering.LightMapHelper;
import me.cortex.voxy.client.core.rendering.RenderService;
import me.cortex.voxy.client.core.rendering.SharedIndexBuffer;
import me.cortex.voxy.client.core.rendering.geometry.OLD.AbstractFarWorldRenderer;
import me.cortex.voxy.client.core.rendering.geometry.OLD.AbstractGeometryManager;
import me.cortex.voxy.client.core.rendering.geometry.OLD.Gl46HierarchicalViewport;
import me.cortex.voxy.client.core.rendering.util.UploadStream;
import me.cortex.voxy.client.mixin.joml.AccessFrustumIntersection;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.util.math.MathHelper;
import org.joml.Matrix4f;
@@ -29,12 +25,11 @@ import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL30.glBindBufferBase;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL31.GL_UNIFORM_BUFFER;
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.*;
import static org.lwjgl.opengl.GL45.glBindTextureUnit;
import static org.lwjgl.opengl.GL45.glCopyNamedBufferSubData;
//Uses MDIC to render the sections
public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, BasicSectionGeometryManager> {
@@ -43,14 +38,23 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
.add(ShaderType.FRAGMENT, "voxy:lod/gl46/quads.frag")
.compile();
private final Shader commandGen = Shader.make()
private final Shader commandGenShader = Shader.make()
.add(ShaderType.COMPUTE, "voxy:lod/gl46/cmdgen.comp")
.compile();
private final Shader prepShader = Shader.make()
.add(ShaderType.COMPUTE, "voxy:lod/gl46/prep.comp")
.compile();
private final Shader cullShader = Shader.make()
.add(ShaderType.VERTEX, "voxy:lod/gl46/cull/raster.vert")
.add(ShaderType.FRAGMENT, "voxy:lod/gl46/cull/raster.frag")
.compile();
private final GlBuffer uniform = new GlBuffer(1024).zero();
private final GlBuffer drawCountBuffer = new GlBuffer(64).zero();
//TODO: needs to be in the viewport, since it contains the compute indirect call/values
private final GlBuffer drawCountCallBuffer = new GlBuffer(1024).zero();
private final GlBuffer drawCallBuffer = new GlBuffer(5*4*400000).zero();//400k draw calls
public MDICSectionRenderer(ModelStore modelStore, int maxSectionCount, long geometryCapacity) {
@@ -72,7 +76,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
MemoryUtil.memPutInt(ptr, sx); ptr += 4;
MemoryUtil.memPutInt(ptr, sy); ptr += 4;
MemoryUtil.memPutInt(ptr, sz); ptr += 4;
MemoryUtil.memPutInt(ptr, viewport.frameId++); ptr += 4;
MemoryUtil.memPutInt(ptr, viewport.frameId); ptr += 4;
innerTranslation.getToAddress(ptr); ptr += 4*3;
UploadStream.INSTANCE.commit();
@@ -88,7 +92,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedIndexBuffer.INSTANCE.id());
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, this.drawCallBuffer.id);
glBindBuffer(GL_PARAMETER_BUFFER_ARB, this.drawCountBuffer.id);
glBindBuffer(GL_PARAMETER_BUFFER_ARB, this.drawCountCallBuffer.id);
}
@@ -102,7 +106,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
glBindVertexArray(RenderService.STATIC_VAO);//Needs to be before binding
this.bindRenderingBuffers();
glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, 0, Math.min((int)(this.geometryManager.getSectionCount()*4.4), 400_000), 0);
glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, 4*3, Math.min((int)(this.geometryManager.getSectionCount()*4.4), 400_000), 0);
glEnable(GL_CULL_FACE);
glBindVertexArray(0);
@@ -124,16 +128,16 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
//TODO compute the draw calls
{
this.commandGen.bind();
this.commandGenShader.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, 2, this.drawCountCallBuffer.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);
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, this.drawCountCallBuffer.id);
glDispatchComputeIndirect(0);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
}
this.renderTerrain();
@@ -146,7 +150,39 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
this.renderTerrain();
//TODO: dont do a copy
// make it so that the viewport contains the original indirectLookupBuffer list!!!
// that way dont need to copy the array
glCopyNamedBufferSubData(sectionRenderList.id, viewport.indirectLookupBuffer.id, 0, 0, sectionRenderList.size());
{//Dispatch prep
this.prepShader.bind();
glBindBufferBase(GL_UNIFORM_BUFFER, 0, this.uniform.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, this.drawCountCallBuffer.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, sectionRenderList.id);
glDispatchCompute(1,1,1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
}
{//Test occlusion
this.cullShader.bind();
glBindVertexArray(RenderService.STATIC_VAO);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, this.uniform.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, this.geometryManager.getMetadataBufferId());
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, viewport.visibilityBuffer.id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, viewport.indirectLookupBuffer.id);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, this.drawCountCallBuffer.id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedIndexBuffer.INSTANCE.id());
glEnable(GL_DEPTH_TEST);
glColorMask(false, false, false, false);
glDepthMask(false);
glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_BYTE, 6*4);
glDepthMask(true);
glColorMask(true, true, true, true);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glDisable(GL_DEPTH_TEST);
}
}
@Override
@@ -170,6 +206,10 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
super.free();
this.uniform.free();
this.terrainShader.free();
this.commandGen.free();
this.commandGenShader.free();
this.cullShader.free();
this.prepShader.free();
this.drawCallBuffer.free();
this.drawCountCallBuffer.free();
}
}

View File

@@ -60,13 +60,17 @@ layout(binding = DRAW_COUNT_BUFFER_BINDING, std430) restrict buffer DrawCommandC
uint cmdGenDispatchX;
uint cmdGenDispatchY;
uint cmdGenDispatchZ;
uint opaqueDrawCount;
uint translucentDrawCount;
uint pad_A;
DrawCommand cullDrawIndirectCommand;
};
#endif
#ifdef SECTION_METADA_BUFFER_BINDING
layout(binding = SECTION_METADA_BUFFER_BINDING, std430) readonly restrict buffer SectionBuffer {
#ifdef SECTION_METADATA_BUFFER_BINDING
layout(binding = SECTION_METADATA_BUFFER_BINDING, std430) readonly restrict buffer SectionBuffer {
SectionMeta sectionData[];
};
#endif

View File

@@ -5,7 +5,7 @@ layout(local_size_x = 128) in;
#define DRAW_BUFFER_BINDING 1
#define DRAW_COUNT_BUFFER_BINDING 2
#define SECTION_METADA_BUFFER_BINDING 3
#define SECTION_METADATA_BUFFER_BINDING 3
#define VISIBILITY_BUFFER_BINDING 4
#define INDIRECT_SECTION_LOOKUP_BINDING 5
@@ -97,8 +97,8 @@ void main() {
//Translucency
count = meta.cntA&0xFFFF;
if (count != 0) {
uint translucentCommandPtr = atomicAdd(translucentDrawCount, 1) + 400000;//FIXME: dont hardcode this offset
writeCmd(translucentCommandPtr, encodedPos, ptr, count);
//uint translucentCommandPtr = atomicAdd(translucentDrawCount, 1) + 400000;//FIXME: dont hardcode this offset
//writeCmd(translucentCommandPtr, encodedPos, ptr, count);
}
ptr += count;

View File

@@ -1,6 +1,7 @@
#version 460 core
#extension GL_ARB_gpu_shader_int64 : enable
#define VISIBILITY_ACCESS writeonly
#define VISIBILITY_BUFFER_BINDING 2
#import <voxy:lod/gl46/bindings.glsl>
layout(early_fragment_tests) in;

View File

@@ -1,6 +1,11 @@
#version 460 core
#extension GL_ARB_gpu_shader_int64 : enable
#define VISIBILITY_ACCESS writeonly
#define SECTION_METADATA_BUFFER_BINDING 1
#define VISIBILITY_BUFFER_BINDING 2
#define INDIRECT_SECTION_LOOKUP_BINDING 3
#import <voxy:lod/gl46/bindings.glsl>
#import <voxy:lod/section.glsl>
@@ -8,7 +13,7 @@ flat out uint id;
flat out uint value;
void main() {
uint sid = gl_InstanceID;
uint sid = indirectLookup[gl_InstanceID];
SectionMeta section = sectionData[sid];
@@ -25,6 +30,6 @@ void main() {
gl_Position = MVP * vec4(vec3(pos),1);
//Write to this id
id = sid;
id = gl_InstanceID;//Note!! we write to the instance id _not_ the section id
value = frameId;
}

View File

@@ -0,0 +1,23 @@
#version 450
layout(local_size_x = 1) in;
#define DRAW_COUNT_BUFFER_BINDING 1
#define INDIRECT_SECTION_LOOKUP_BINDING 2
#import <voxy:lod/gl46/bindings.glsl>
void main() {
cmdGenDispatchX = ((sectionCount+127)/128);
cmdGenDispatchY = 1;
cmdGenDispatchZ = 1;
opaqueDrawCount = 0;
translucentDrawCount = 0;
cullDrawIndirectCommand.count = 6*2*3;
cullDrawIndirectCommand.instanceCount = sectionCount;
cullDrawIndirectCommand.firstIndex = (1<<16)*6*2;
cullDrawIndirectCommand.baseVertex = 0;
cullDrawIndirectCommand.baseInstance = 0;
}

View File

@@ -2,7 +2,7 @@
#extension GL_ARB_gpu_shader_int64 : enable
#define QUAD_BUFFER_BINDING 1
#define SECTION_METADA_BUFFER_BINDING 2
#define SECTION_METADATA_BUFFER_BINDING 2
#define MODEL_BUFFER_BINDING 3
#define MODEL_COLOUR_BUFFER_BINDING 4
#define LIGHTING_BUFFER_BINDING 5