From 395f4b1dfc3055f04d7b8b223a2cc9c840af0e8a Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 29 Mar 2024 17:40:49 -0700 Subject: [PATCH] Based crumbling - Fix crumbling on instancing engine by passing a baseInstance uniform --- .../engine/instancing/InstancedDraw.java | 8 +------- .../engine/instancing/InstancedDrawManager.java | 6 ++++-- .../flywheel/backend/gl/shader/GlProgram.java | 17 ++++++++++++++++- .../flywheel/internal/instancing/main.vert | 3 ++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDraw.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDraw.java index 955144cb8..16e6e77e1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDraw.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDraw.java @@ -2,7 +2,6 @@ package com.jozufozu.flywheel.backend.engine.instancing; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.backend.engine.GroupKey; -import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl; import com.jozufozu.flywheel.backend.engine.MeshPool; import com.jozufozu.flywheel.backend.gl.TextureBuffer; @@ -47,16 +46,11 @@ public class InstancedDraw { mesh.draw(instancer.instanceCount()); } - public void renderOne(TextureBuffer buffer, InstanceHandleImpl impl) { + public void renderOne(TextureBuffer buffer) { if (mesh.isInvalid()) { return; } - int instanceCount = instancer.instanceCount(); - if (instanceCount <= 0 || impl.index >= instanceCount) { - return; - } - instancer.bind(buffer); mesh.draw(1); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java index 38f651999..017eb3726 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java @@ -172,7 +172,9 @@ public class InstancedDrawManager extends DrawManager> { for (var instanceHandlePair : progressEntry.getValue()) { InstancedInstancer instancer = instanceHandlePair.first(); - var handle = instanceHandlePair.second(); + var index = instanceHandlePair.second().index; + + program.setInt("_flw_baseInstance", index); for (InstancedDraw draw : instancer.draws()) { CommonCrumbling.applyCrumblingProperties(crumblingMaterial, draw.material()); @@ -182,7 +184,7 @@ public class InstancedDrawManager extends DrawManager> { Samplers.INSTANCE_BUFFER.makeActive(); - draw.renderOne(instanceTexture, handle); + draw.renderOne(instanceTexture); } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java index 2599db49a..3642da5e6 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java @@ -10,6 +10,7 @@ import static org.lwjgl.opengl.GL20.glUniform3f; import static org.lwjgl.opengl.GL20.glUniform4f; import static org.lwjgl.opengl.GL20.glUniformMatrix3fv; import static org.lwjgl.opengl.GL20.glUniformMatrix4fv; +import static org.lwjgl.opengl.GL30.glUniform1ui; import static org.lwjgl.opengl.GL31.GL_INVALID_INDEX; import static org.lwjgl.opengl.GL31.glGetUniformBlockIndex; import static org.lwjgl.opengl.GL31.glUniformBlockBinding; @@ -104,13 +105,27 @@ public class GlProgram extends GlObject { } public void setBool(String glslName, boolean bool) { + setInt(glslName, bool ? 1 : 0); + } + + public void setUInt(String glslName, int value) { int uniform = getUniformLocation(glslName); if (uniform < 0) { return; } - glUniform1i(uniform, bool ? 1 : 0); + glUniform1ui(uniform, value); + } + + public void setInt(String glslName, int value) { + int uniform = getUniformLocation(glslName); + + if (uniform < 0) { + return; + } + + glUniform1i(uniform, value); } /** diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert index ab330d1e1..8c4b9d6a3 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert +++ b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert @@ -2,12 +2,13 @@ #include "flywheel:internal/packed_material.glsl" uniform uvec4 _flw_packedMaterial; +uniform int _flw_baseInstance = 0; void main() { _flw_uberMaterialVertexIndex = _flw_packedMaterial.x; _flw_unpackMaterialProperties(_flw_packedMaterial.w, flw_material); - FlwInstance instance = _flw_unpackInstance(gl_InstanceID); + FlwInstance instance = _flw_unpackInstance(_flw_baseInstance + gl_InstanceID); _flw_main(instance, uint(gl_InstanceID)); }