From 113408a07ab506182494975cb273c88700947203 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 18 Jan 2024 18:17:09 -0800 Subject: [PATCH] We had lost track of time - Add some uniforms related to game time. - Move frustum planes to the top as its better for alignment. - Make constantAmbientLight a uint. --- .../backend/engine/uniform/FrameUniforms.java | 34 +++++++++++++------ .../backend/engine/uniform/UniformBuffer.java | 19 +++++++++-- .../flywheel/flywheel/internal/common.frag | 2 +- .../flywheel/internal/uniforms/frame.glsl | 10 ++++-- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java index bfab239e3..755883fbf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java @@ -11,16 +11,16 @@ import net.minecraft.core.Vec3i; import net.minecraft.world.phys.Vec3; public class FrameUniforms implements UniformProvider { - public static final int SIZE = 192; + public static final int SIZE = 194; private RenderContext context; + private final Matrix4f viewProjection = new Matrix4f(); + public int byteSize() { return SIZE; } - private final Matrix4f viewProjection = new Matrix4f(); - public void setContext(RenderContext context) { this.context = context; } @@ -39,17 +39,29 @@ public class FrameUniforms implements UniformProvider { viewProjection.set(context.viewProjection()); viewProjection.translate(-camX, -camY, -camZ); - MatrixMath.writeUnsafe(viewProjection, ptr); - MemoryUtil.memPutFloat(ptr + 64, camX); - MemoryUtil.memPutFloat(ptr + 68, camY); - MemoryUtil.memPutFloat(ptr + 72, camZ); - MemoryUtil.memPutFloat(ptr + 76, 0f); // vec4 alignment - MemoryUtil.memPutInt(ptr + 80, getConstantAmbientLightFlag(context)); - if (!Uniforms.frustumPaused || Uniforms.frustumCapture) { - MatrixMath.writePackedFrustumPlanes(ptr + 96, viewProjection); + MatrixMath.writePackedFrustumPlanes(ptr, viewProjection); Uniforms.frustumCapture = false; } + + MatrixMath.writeUnsafe(viewProjection, ptr + 96); + MemoryUtil.memPutFloat(ptr + 160, camX); + MemoryUtil.memPutFloat(ptr + 164, camY); + MemoryUtil.memPutFloat(ptr + 168, camZ); + MemoryUtil.memPutFloat(ptr + 172, 0f); // empty component of vec4 because we don't trust std140 + MemoryUtil.memPutInt(ptr + 176, getConstantAmbientLightFlag(context)); + + int ticks = context.renderer() + .getTicks(); + float partialTick = context.partialTick(); + float renderTicks = ticks + partialTick; + float renderSeconds = renderTicks / 20f; + + MemoryUtil.memPutInt(ptr + 180, ticks); + MemoryUtil.memPutFloat(ptr + 184, partialTick); + MemoryUtil.memPutFloat(ptr + 188, renderTicks); + MemoryUtil.memPutFloat(ptr + 192, renderSeconds); + } private static int getConstantAmbientLightFlag(RenderContext context) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/UniformBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/UniformBuffer.java index c21865799..00fe89f71 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/UniformBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/UniformBuffer.java @@ -6,8 +6,11 @@ import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage; import com.jozufozu.flywheel.lib.memory.MemoryBlock; +import net.minecraft.util.Mth; + public class UniformBuffer { - // private static final int MAX_SIZE = GL32.glGetInteger(GL32.GL_MAX_UNIFORM_BLOCK_SIZE); + private static final int OFFSET_ALIGNMENT = GL32.glGetInteger(GL32.GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT); + private static final boolean PO2_ALIGNMENT = Mth.isPowerOfTwo(OFFSET_ALIGNMENT); private final int index; private final GlBuffer buffer; @@ -19,7 +22,10 @@ public class UniformBuffer { this.buffer = new GlBuffer(GlBufferUsage.DYNAMIC_DRAW); this.provider = provider; - this.data = MemoryBlock.mallocTracked(provider.byteSize()); + // renderdoc complains if the size of the buffer does not have the offset alignment + var actualBytes = align(provider.byteSize()); + this.data = MemoryBlock.mallocTracked(actualBytes); + this.data.clear(); } public void update() { @@ -35,4 +41,13 @@ public class UniformBuffer { data.free(); buffer.delete(); } + + // https://stackoverflow.com/questions/3407012/rounding-up-to-the-nearest-multiple-of-a-number + private static int align(int numToRound) { + if (PO2_ALIGNMENT) { + return (numToRound + OFFSET_ALIGNMENT - 1) & -OFFSET_ALIGNMENT; + } else { + return ((numToRound + OFFSET_ALIGNMENT - 1) / OFFSET_ALIGNMENT) * OFFSET_ALIGNMENT; + } + } } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common.frag b/src/main/resources/assets/flywheel/flywheel/internal/common.frag index 66fb0fc77..533e905f5 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/common.frag +++ b/src/main/resources/assets/flywheel/flywheel/internal/common.frag @@ -31,7 +31,7 @@ void _flw_main() { if (flw_fragDiffuse) { float diffuseFactor; - if (flw_constantAmbientLight == 1) { + if (flw_constantAmbientLight == 1u) { diffuseFactor = diffuseNether(flw_vertexNormal); } else { diffuseFactor = diffuse(flw_vertexNormal); diff --git a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl index 5f975a838..dc4a73532 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl @@ -10,8 +10,14 @@ struct FrustumPlanes { }; layout(std140) uniform _FlwFrameUniforms { + FrustumPlanes flw_frustumPlanes; mat4 flw_viewProjection; vec4 flw_cameraPos; - int flw_constantAmbientLight; - FrustumPlanes flw_frustumPlanes; + uint flw_constantAmbientLight; + + uint flw_ticks; + float flw_partialTick; + + float flw_renderTicks; + float flw_renderSeconds; };