Begin on player uniforms

This commit is contained in:
Kneelawk 2024-03-03 20:06:28 -08:00
parent 56d03601cd
commit 0ea8a31103
No known key found for this signature in database
GPG Key ID: DB4E28870D6D260A
5 changed files with 121 additions and 18 deletions

View File

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

View File

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

View File

@ -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<FrameUniforms> frame;
private static @Nullable UniformBuffer<FogUniforms> fog;
private static @Nullable UniformBuffer<OptionsUniforms> options;
private static @Nullable UniformBuffer<PlayerUniforms> player;
public static UniformBuffer<FrameUniforms> frame() {
if (frame == null) {
@ -35,10 +37,18 @@ public class Uniforms {
return options;
}
public static UniformBuffer<PlayerUniforms> 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;
}
}

View File

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

View File

@ -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"