From 0ea8a31103520a1f04269e777c1505ec7696fc65 Mon Sep 17 00:00:00 2001 From: Kneelawk Date: Sun, 3 Mar 2024 20:06:28 -0800 Subject: [PATCH] Begin on player uniforms --- .../backend/engine/uniform/FrameUniforms.java | 22 ++---- .../engine/uniform/PlayerUniforms.java | 67 +++++++++++++++++++ .../backend/engine/uniform/Uniforms.java | 39 +++++++++++ .../flywheel/internal/uniforms/player.glsl | 10 +++ .../flywheel/internal/uniforms/uniforms.glsl | 1 + 5 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/engine/uniform/PlayerUniforms.java create mode 100644 src/main/resources/assets/flywheel/flywheel/internal/uniforms/player.glsl 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 cc23e8233..63f6d244a 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 @@ -123,7 +123,7 @@ public class FrameUniforms implements UniformProvider { var window = Minecraft.getInstance() .getWindow(); - ptr = writeVec2(ptr, window.getWidth(), window.getHeight()); + ptr = Uniforms.writeVec2(ptr, window.getWidth(), window.getHeight()); // default line width: net.minecraft.client.renderer.RenderStateShard.LineStateShard MemoryUtil.memPutFloat(ptr, Math.max(2.5F, (float) window.getWidth() / 1920.0F * 2.5F)); @@ -187,11 +187,11 @@ public class FrameUniforms implements UniformProvider { private static long writeCamera(long ptr, float camX, float camY, float camZ, Vector3f lookVector, float xRot, float yRot) { - ptr = writeVec3(ptr, camX, camY, camZ); + ptr = Uniforms.writeVec3(ptr, camX, camY, camZ); - ptr = writeVec3(ptr, lookVector.x, lookVector.y, lookVector.z); + ptr = Uniforms.writeVec3(ptr, lookVector.x, lookVector.y, lookVector.z); - ptr = writeVec2(ptr, xRot, yRot); + ptr = Uniforms.writeVec2(ptr, xRot, yRot); return ptr; } @@ -248,20 +248,6 @@ public class FrameUniforms implements UniformProvider { return ptr + 8; } - private static long writeVec3(long ptr, float camX, float camY, float camZ) { - MemoryUtil.memPutFloat(ptr, camX); - MemoryUtil.memPutFloat(ptr + 4, camY); - MemoryUtil.memPutFloat(ptr + 8, camZ); - MemoryUtil.memPutFloat(ptr + 12, 0f); // empty component of vec4 because we don't trust std140 - return ptr + 16; - } - - private static long writeVec2(long ptr, float camX, float camY) { - MemoryUtil.memPutFloat(ptr, camX); - MemoryUtil.memPutFloat(ptr + 4, camY); - return ptr + 8; - } - private static int getConstantAmbientLightFlag(RenderContext context) { var constantAmbientLight = context.level() .effects() diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/PlayerUniforms.java b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/PlayerUniforms.java new file mode 100644 index 000000000..fbd28e629 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/PlayerUniforms.java @@ -0,0 +1,67 @@ +package com.jozufozu.flywheel.backend.engine.uniform; + +import org.jetbrains.annotations.Nullable; +import org.lwjgl.system.MemoryUtil; + +import com.jozufozu.flywheel.api.event.RenderContext; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.BlockPos; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.LightLayer; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.phys.Vec3; + +public class PlayerUniforms implements UniformProvider { + public static final int SIZE = 4 + 8 + 16; + + @Nullable + private RenderContext context; + + @Override + public int byteSize() { + return SIZE; + } + + public void setContext(RenderContext context) { + this.context = context; + } + + @Override + public void write(long ptr) { + Minecraft mc = Minecraft.getInstance(); + LocalPlayer player = mc.player; + if (context == null || player == null) { + MemoryUtil.memSet(ptr, 0, SIZE); + return; + } + + int luminance = 0; + for (InteractionHand hand : InteractionHand.values()) { + Item handItem = player.getItemInHand(hand).getItem(); + if (handItem instanceof BlockItem bitem) { + Block block = bitem.getBlock(); + int blockLight = block.defaultBlockState().getLightEmission(player.clientLevel, BlockPos.ZERO); + if (luminance < blockLight) { + luminance = blockLight; + } + } + } + + MemoryUtil.memPutFloat(ptr, (float) luminance / 15); + ptr += 4; + + Vec3 eyePos = player.getEyePosition(context.partialTick()); + ptr = Uniforms.writeVec3(ptr, (float) eyePos.x, (float) eyePos.y, (float) eyePos.z); + + int blockBrightness = player.clientLevel.getBrightness(LightLayer.BLOCK, player.blockPosition()); + int skyBrightness = player.clientLevel.getBrightness(LightLayer.SKY, player.blockPosition()); + int maxBrightness = player.clientLevel.getMaxLightLevel(); + + ptr = Uniforms.writeVec2(ptr, (float) blockBrightness / (float) maxBrightness, + (float) skyBrightness / (float) maxBrightness); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java index 163fb7ba7..9206d53e0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java @@ -6,6 +6,7 @@ import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.config.DebugMode; import org.jetbrains.annotations.Nullable; +import org.lwjgl.system.MemoryUtil; public class Uniforms { public static boolean frustumPaused = false; @@ -13,6 +14,7 @@ public class Uniforms { private static @Nullable UniformBuffer frame; private static @Nullable UniformBuffer fog; private static @Nullable UniformBuffer options; + private static @Nullable UniformBuffer player; public static UniformBuffer frame() { if (frame == null) { @@ -35,10 +37,18 @@ public class Uniforms { return options; } + public static UniformBuffer player() { + if (player == null) { + player = new UniformBuffer<>(2, new PlayerUniforms()); + } + return player; + } + public static void bindForDraw() { bindFrame(); bindFog(); bindOptions(); + bindPlayer(); } public static void bindFrame() { @@ -59,6 +69,12 @@ public class Uniforms { } } + public static void bindPlayer() { + if (player != null) { + player.bind(); + } + } + public static void onFogUpdate() { try (var restoreState = GlStateTracker.getRestoreState()) { fog().update(); @@ -71,6 +87,10 @@ public class Uniforms { ubo.update(); options(); + + var player = player(); + player.provider.setContext(ctx); + player.update(); } public static void setDebugMode(DebugMode mode) { @@ -92,5 +112,24 @@ public class Uniforms { options.delete(); options = null; } + + if (player != null) { + player.delete(); + player = null; + } + } + + static long writeVec3(long ptr, float camX, float camY, float camZ) { + MemoryUtil.memPutFloat(ptr, camX); + MemoryUtil.memPutFloat(ptr + 4, camY); + MemoryUtil.memPutFloat(ptr + 8, camZ); + MemoryUtil.memPutFloat(ptr + 12, 0f); // empty component of vec4 because we don't trust std140 + return ptr + 16; + } + + static long writeVec2(long ptr, float camX, float camY) { + MemoryUtil.memPutFloat(ptr, camX); + MemoryUtil.memPutFloat(ptr + 4, camY); + return ptr + 8; } } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/player.glsl b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/player.glsl new file mode 100644 index 000000000..13bbbfd53 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/player.glsl @@ -0,0 +1,10 @@ +// player.glsl - Holds uniforms for player state. + +layout (std140) uniform _FlwPlayerUniforms { + float flw_heldLight; + vec4 _flw_eyePos; + /** The brightness at the player's eye position. */ + vec2 flw_eyeBrightness; +}; + +#define flw_eyePos _flw_eyePos.xyz diff --git a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/uniforms.glsl b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/uniforms.glsl index 0cd465a5b..f2f9324b1 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/uniforms.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/uniforms.glsl @@ -3,3 +3,4 @@ #include "flywheel:internal/uniforms/frame.glsl" #include "flywheel:internal/uniforms/fog.glsl" #include "flywheel:internal/uniforms/options.glsl" +#include "flywheel:internal/uniforms/player.glsl"