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.
This commit is contained in:
Jozufozu 2024-01-18 18:17:09 -08:00
parent 5f1d1eccb0
commit 233cf4e7a8
4 changed files with 49 additions and 16 deletions

View File

@ -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) {

View File

@ -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<T extends UniformProvider> {
// 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<T extends UniformProvider> {
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<T extends UniformProvider> {
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;
}
}
}

View File

@ -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);

View File

@ -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;
};