Add scratch buffer for position.

Fixes lods "disappearing" when you zoom in
This commit is contained in:
mcrcortex
2025-01-21 21:50:44 +10:00
parent 44c66d5c26
commit 75ad35e057
4 changed files with 51 additions and 16 deletions

View File

@@ -57,6 +57,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
//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
private final GlBuffer positionScratchBuffer = new GlBuffer(8*400000).zero();//400k positions
public MDICSectionRenderer(ModelStore modelStore, int maxSectionCount, long geometryCapacity) {
super(modelStore, new BasicSectionGeometryManager(maxSectionCount, geometryCapacity));
@@ -89,6 +90,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, this.geometryManager.getGeometryBufferId());
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, this.geometryManager.getMetadataBufferId());
this.modelStore.bind(3, 4, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, this.positionScratchBuffer.id);
LightMapHelper.bind(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedIndexBuffer.INSTANCE.id());
@@ -139,6 +141,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
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);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, this.positionScratchBuffer.id);
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, this.drawCountCallBuffer.id);
glDispatchComputeIndirect(0);
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
@@ -204,7 +207,7 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
public MDICViewport createViewport() {
return new MDICViewport();
}
//
@Override
public void free() {
super.free();
@@ -215,5 +218,6 @@ public class MDICSectionRenderer extends AbstractSectionRenderer<MDICViewport, B
this.prepShader.free();
this.drawCallBuffer.free();
this.drawCountCallBuffer.free();
this.positionScratchBuffer.free();
}
}

View File

@@ -103,6 +103,15 @@ layout(binding = MODEL_COLOUR_BUFFER_BINDING, std430) readonly restrict buffer M
};
#endif
#ifdef POSITION_SCRATCH_BINDING
#ifndef POSITION_SCRATCH_ACCESS
#define POSITION_SCRATCH_ACCESS readonly
#endif
layout(binding = POSITION_SCRATCH_BINDING, std430) POSITION_SCRATCH_ACCESS restrict buffer PositionScratchBuffer {
uvec2 positionBuffer[];
};
#endif
#ifdef LIGHTING_SAMPLER_BINDING
layout(binding = LIGHTING_SAMPLER_BINDING) uniform sampler2D lightSampler;

View File

@@ -8,6 +8,8 @@ layout(local_size_x = 128) in;
#define SECTION_METADATA_BUFFER_BINDING 3
#define VISIBILITY_BUFFER_BINDING 4
#define INDIRECT_SECTION_LOOKUP_BINDING 5
#define POSITION_SCRATCH_BINDING 6
#define POSITION_SCRATCH_ACCESS writeonly
#import <voxy:lod/quad_format.glsl>
#import <voxy:lod/gl46/bindings.glsl>
@@ -34,13 +36,13 @@ uint encodeLocalLodPos(uint detail, ivec3 pos) {
//Note: if i want reverse indexing i need to use the index buffer offset to offset
void writeCmd(uint idx, uint encodedPos, uint offset, uint quadCount) {
void writeCmd(uint idx, uint instance, uint offset, uint quadCount) {
DrawCommand cmd;
cmd.count = quadCount * 6;
cmd.instanceCount = 1;
cmd.firstIndex = 0;
cmd.baseVertex = int(offset)<<2;
cmd.baseInstance = encodedPos;
cmd.baseInstance = instance;
cmdBuffer[idx] = cmd;
}
@@ -69,14 +71,15 @@ void main() {
//This prevents overflow of the relative position encoder
if (shouldRender) {
shouldRender = !any(lessThan(ivec3(254), abs(ipos-(baseSectionPos>>detail))));
}
if (shouldRender) {
uint encodedPos = encodeLocalLodPos(detail, ipos);
uint ptr = extractQuadStart(meta);
ivec3 relative = ipos-(baseSectionPos>>detail);
uint drawId = gl_GlobalInvocationID.x;
positionBuffer[drawId] = uvec2(meta.posA, meta.posB);
uint msk = 0;
@@ -98,56 +101,56 @@ void main() {
count = meta.cntA&0xFFFF;
if (count != 0) {
//uint translucentCommandPtr = atomicAdd(translucentDrawCount, 1) + 400000;//FIXME: dont hardcode this offset
//writeCmd(translucentCommandPtr, encodedPos, ptr, count);
//writeCmd(translucentCommandPtr, drawId, ptr, count);
}
ptr += count;
//Double sided quads
count = (meta.cntA>>16)&0xFFFF;
if (count != 0) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
//Down
count = (meta.cntB)&0xFFFF;
if (((msk&(1u<<0))!=0)) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
//Up
count = (meta.cntB>>16)&0xFFFF;
if ((msk&(1u<<1))!=0) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
//North
count = (meta.cntC)&0xFFFF;
if ((msk&(1u<<2))!=0) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
//South
count = (meta.cntC>>16)&0xFFFF;
if ((msk&(1u<<3))!=0) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
//West
count = (meta.cntD)&0xFFFF;
if ((msk&(1u<<4))!=0) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
//East
count = (meta.cntD>>16)&0xFFFF;
if ((msk&(1u<<5))!=0) {
writeCmd(cmdPtr++, encodedPos, ptr, count);
writeCmd(cmdPtr++, drawId, ptr, count);
}
ptr += count;
}

View File

@@ -5,6 +5,7 @@
#define SECTION_METADATA_BUFFER_BINDING 2
#define MODEL_BUFFER_BINDING 3
#define MODEL_COLOUR_BUFFER_BINDING 4
#define POSITION_SCRATCH_BINDING 5
#define LIGHTING_SAMPLER_BINDING 1
@@ -25,6 +26,7 @@ layout(location = 5) out flat vec4 conditionalTinting;
layout(location = 6) out flat uint quadDebug;
#endif
/*
uint extractLodLevel() {
return uint(gl_BaseInstance)>>27;
}
@@ -33,7 +35,7 @@ uint extractLodLevel() {
//Gives a relative position of +-255 relative to the player center in its respective lod
ivec3 extractRelativeLodPos() {
return (ivec3(gl_BaseInstance)<<ivec3(5,14,23))>>ivec3(23);
}
}*/
vec4 uint2vec4RGBA(uint colour) {
return vec4((uvec4(colour)>>uvec4(24,16,8,0))&uvec4(0xFF))/255.0;
@@ -69,6 +71,21 @@ vec3 swizzelDataAxis(uint axis, vec3 data) {
return data;
}
uint extractDetail(uvec2 encPos) {
return encPos.x>>28;
}
ivec3 extractLoDPosition(uvec2 encPos) {
int y = ((int(encPos.x)<<4)>>24);
int x = (int(encPos.y)<<4)>>8;
int z = int((encPos.x&((1u<<20)-1))<<4);
z |= int(encPos.y>>28);
z <<= 8;
z >>= 8;
return ivec3(x,y,z);
}
//TODO: add a mechanism so that some quads can ignore backface culling
// this would help alot with stuff like crops as they would look kinda weird i think,
// same with flowers etc
@@ -83,7 +100,9 @@ void main() {
bool hasAO = modelHasMipmaps(model);//TODO: replace with per face AO flag
bool isShaded = hasAO;//TODO: make this a per face flag
uint lodLevel = extractLodLevel();
uvec2 encPos = positionBuffer[gl_BaseInstance];
uint lodLevel = extractDetail(encPos);
vec2 modelUV = vec2(modelId&0xFFu, (modelId>>8)&0xFFu)*(1.0/(256.0));
@@ -153,7 +172,7 @@ void main() {
cornerPos += swizzelDataAxis(face>>1, vec3(faceSize.xz, mix(depthOffset, 1-depthOffset, float(face&1u))));
vec3 origin = vec3(((extractRelativeLodPos()<<lodLevel) - (baseSectionPos&(ivec3((1<<lodLevel)-1))))<<5);
vec3 origin = vec3(((extractLoDPosition(encPos)<<lodLevel) - baseSectionPos)<<5);
gl_Position = MVP*vec4((cornerPos+swizzelDataAxis(face>>1,vec3(cQuadSize,0)))*(1<<lodLevel)+origin, 1.0);
#ifdef DEBUG_RENDER