Skip to content

Commit

Permalink
Crumbling grumbling
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Jozufozu committed Sep 21, 2024
1 parent b7b7cca commit cb58f60
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void bindForDraw() {
* Bind all buffers except the draw command buffer.
*/
public void bindForCrumbling() {
multiBind(3, 3);
multiBind(1, 4);
}

private void multiBind(int base, int count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void writeWithOverrides(long ptr, int instanceIndex, Material materialOve
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -204,15 +205,21 @@ public void renderCrumbling(List<Engine.CrumblingBlock> crumblingBlocks) {
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,8 @@ public int modelIndex() {
public int baseInstance() {
return baseInstance;
}

public int local2GlobalInstanceIndex(int instanceIndex) {
return mapping.objectIndex2GlobalIndex(instanceIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,9 @@ private void shrink(int oldLength, int neededPages) {

pages = Arrays.copyOf(pages, neededPages);
}

public int objectIndex2GlobalIndex(int objectIndex) {
return (pages[objectIndex2PageIndex(objectIndex)] << LOG_2_PAGE_SIZE) + (objectIndex & PAGE_MASK);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit cb58f60

Please sign in to comment.