From cb58f6075e03736f46bdbb30e3e0613dceb02879 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 21 Sep 2024 12:00:09 -0700 Subject: [PATCH] Crumbling grumbling - Fix crumbling on indirect - Directly use the baseInstance as instance index without indirection - #define base instance and draw id variables to simplify usage - Fix null pointer looking up culling group - Add method to map an instancer's local instance index to a global index in the page file --- .../engine/indirect/IndirectBuffers.java | 2 +- .../backend/engine/indirect/IndirectDraw.java | 2 +- .../engine/indirect/IndirectDrawManager.java | 13 +++++++++--- .../engine/indirect/IndirectInstancer.java | 4 ++++ .../engine/indirect/ObjectStorage.java | 4 ++++ .../flywheel/internal/indirect/main.vert | 21 ++++++++++++------- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectBuffers.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectBuffers.java index f8597184b..dc845ea8b 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectBuffers.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectBuffers.java @@ -113,7 +113,7 @@ public class IndirectBuffers { * Bind all buffers except the draw command buffer. */ public void bindForCrumbling() { - multiBind(3, 3); + multiBind(1, 4); } private void multiBind(int base, int count) { diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDraw.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDraw.java index 48517d1a2..d34087f56 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDraw.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDraw.java @@ -89,7 +89,7 @@ public class IndirectDraw { MemoryUtil.memPutInt(ptr + 4, 1); // instanceCount - only drawing one instance MemoryUtil.memPutInt(ptr + 8, mesh.firstIndex()); // firstIndex MemoryUtil.memPutInt(ptr + 12, mesh.baseVertex()); // baseVertex - MemoryUtil.memPutInt(ptr + 16, instancer.baseInstance() + instanceIndex); // baseInstance + MemoryUtil.memPutInt(ptr + 16, instancer.local2GlobalInstanceIndex(instanceIndex)); // baseInstance MemoryUtil.memPutInt(ptr + 20, instancer.modelIndex()); // modelIndex diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDrawManager.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDrawManager.java index e19999b5c..d3b7c140b 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDrawManager.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDrawManager.java @@ -21,6 +21,7 @@ import dev.engine_room.flywheel.backend.compile.ContextShader; import dev.engine_room.flywheel.backend.compile.IndirectPrograms; import dev.engine_room.flywheel.backend.engine.CommonCrumbling; import dev.engine_room.flywheel.backend.engine.DrawManager; +import dev.engine_room.flywheel.backend.engine.GroupKey; import dev.engine_room.flywheel.backend.engine.InstancerKey; import dev.engine_room.flywheel.backend.engine.LightStorage; import dev.engine_room.flywheel.backend.engine.MaterialRenderState; @@ -204,15 +205,21 @@ public class IndirectDrawManager extends DrawManager> { // Scratch memory for writing draw commands. var block = MemoryBlock.malloc(IndirectBuffers.DRAW_COMMAND_STRIDE); + // Set up the crumbling program buffers. Nothing changes here between draws. GlBufferType.DRAW_INDIRECT_BUFFER.bind(crumblingDrawBuffer.handle()); glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BufferBindings.DRAW, crumblingDrawBuffer.handle(), 0, IndirectBuffers.DRAW_COMMAND_STRIDE); for (var groupEntry : byType.entrySet()) { var byProgress = groupEntry.getValue(); - // Set up the crumbling program buffers. Nothing changes here between draws. - cullingGroups.get(groupEntry.getKey()) - .bindWithContextShader(ContextShader.CRUMBLING); + GroupKey groupKey = groupEntry.getKey(); + IndirectCullingGroup cullingGroup = cullingGroups.get(groupKey.instanceType()); + + if (cullingGroup == null) { + continue; + } + + cullingGroup.bindWithContextShader(ContextShader.CRUMBLING); for (var progressEntry : byProgress.int2ObjectEntrySet()) { Samplers.CRUMBLING.makeActive(); diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java index 1f1f7005e..6096b75f9 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectInstancer.java @@ -144,4 +144,8 @@ public class IndirectInstancer extends AbstractInstancer public int baseInstance() { return baseInstance; } + + public int local2GlobalInstanceIndex(int instanceIndex) { + return mapping.objectIndex2GlobalIndex(instanceIndex); + } } diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/ObjectStorage.java b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/ObjectStorage.java index 027af4c6c..56d39613e 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/ObjectStorage.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/ObjectStorage.java @@ -216,5 +216,9 @@ public class ObjectStorage extends AbstractArena { pages = Arrays.copyOf(pages, neededPages); } + + public int objectIndex2GlobalIndex(int objectIndex) { + return (pages[objectIndex2PageIndex(objectIndex)] << LOG_2_PAGE_SIZE) + (objectIndex & PAGE_MASK); + } } } diff --git a/common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/main.vert b/common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/main.vert index d94725fcb..15583adee 100644 --- a/common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/main.vert +++ b/common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/main.vert @@ -23,12 +23,16 @@ uniform uint _flw_baseDraw; flat out uvec3 _flw_packedMaterial; -void main() { #if __VERSION__ < 460 - uint drawIndex = gl_DrawIDARB + _flw_baseDraw; +#define flw_baseInstance gl_BaseInstanceARB +#define flw_drawId gl_DrawIDARB #else - uint drawIndex = gl_DrawID + _flw_baseDraw; +#define flw_baseInstance gl_BaseInstance +#define flw_drawId gl_DrawID #endif + +void main() { + uint drawIndex = flw_drawId + _flw_baseDraw; MeshDrawCommand draw = _flw_drawCommands[drawIndex]; _flw_uberMaterialVertexIndex = draw.materialVertexIndex; @@ -42,11 +46,12 @@ void main() { // _flw_normalMatrix = mat3(1.); #endif - #if __VERSION__ < 460 - uint instanceIndex = _flw_instanceIndices[gl_BaseInstanceARB + gl_InstanceID]; -#else - uint instanceIndex = _flw_instanceIndices[gl_BaseInstance + gl_InstanceID]; -#endif + #ifdef _FLW_CRUMBLING + uint instanceIndex = flw_baseInstance; + #else + uint instanceIndex = _flw_instanceIndices[flw_baseInstance + gl_InstanceID]; + #endif + FlwInstance instance = _flw_unpackInstance(instanceIndex); _flw_main(instance, instanceIndex);