Skip to content

Commit

Permalink
The origin of all your problems
Browse files Browse the repository at this point in the history
- Expose an ivec3 flw_renderOrigin in the shader api
- Internally add flw_renderOrigin in flw_light(*)
- flw_lightFetch expects an actual world position still
  • Loading branch information
Jozufozu committed Jul 28, 2024
1 parent fe89e00 commit 8dce80b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import net.minecraft.world.phys.Vec3;

public final class FrameUniforms extends UniformWriter {
private static final int SIZE = 96 + 64 * 9 + 16 * 4 + 8 * 2 + 8 + 4 * 10;
private static final int SIZE = 96 + 64 * 9 + 16 * 5 + 8 * 2 + 8 + 4 * 10;
static final UniformBuffer BUFFER = new UniformBuffer(Uniforms.FRAME_INDEX, SIZE);

private static final Matrix4f VIEW = new Matrix4f();
Expand Down Expand Up @@ -94,6 +94,8 @@ public static void update(RenderContext context) {

ptr = writeMatrices(ptr);

ptr = writeRenderOrigin(ptr, renderOrigin);

ptr = writeCamera(ptr);

var window = Minecraft.getInstance()
Expand All @@ -114,6 +116,11 @@ public static void update(RenderContext context) {
BUFFER.markDirty();
}

private static long writeRenderOrigin(long ptr, Vec3i renderOrigin) {
ptr = writeIVec3(ptr, renderOrigin.getX(), renderOrigin.getY(), renderOrigin.getZ());
return ptr;
}

private static void setPrev() {
VIEW_PREV.set(VIEW);
PROJECTION_PREV.set(PROJECTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ static long writeVec4(long ptr, float x, float y, float z, float w) {
return ptr + 16;
}

static long writeIVec2(long ptr, int x, int y) {
MemoryUtil.memPutInt(ptr, x);
MemoryUtil.memPutInt(ptr + 4, y);
return ptr + 8;
}

static long writeIVec3(long ptr, int x, int y, int z) {
MemoryUtil.memPutInt(ptr, x);
MemoryUtil.memPutInt(ptr + 4, y);
MemoryUtil.memPutInt(ptr + 8, z);
MemoryUtil.memPutInt(ptr + 12, 0); // empty component of vec4 because we don't trust std140
return ptr + 16;
}

static long writeIVec4(long ptr, int x, int y, int z, int w) {
MemoryUtil.memPutInt(ptr, x);
MemoryUtil.memPutInt(ptr + 4, y);
MemoryUtil.memPutInt(ptr + 8, z);
MemoryUtil.memPutInt(ptr + 12, w);
return ptr + 16;
}

static long writeMat4(long ptr, Matrix4f mat) {
ExtraMemoryOps.putMatrix4f(ptr, mat);
return ptr + 64;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// TODO: Add config for light smoothness. Should work at a compile flag level

/// Get the light at the given world position from the given normal.
/// Get the light at the given world position relative to flw_renderOrigin from the given normal.
/// This may be interpolated for smooth lighting.
bool flw_light(vec3 worldPos, vec3 normal, out vec2 light);

/// Get the light at the given world position.
/// Get the light at the given world position relative to flw_renderOrigin.
/// This may be interpolated for smooth lighting.
bool flw_light(vec3 worldPos, out vec2 light);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool flw_light(vec3 worldPos, out vec2 lightCoord) {
// Always use the section of the block we are contained in to ensure accuracy.
// We don't want to interpolate between sections, but also we might not be able
// to rely on the existence neighboring sections, so don't do any extra rounding here.
ivec3 blockPos = ivec3(floor(worldPos));
ivec3 blockPos = ivec3(floor(worldPos)) + flw_renderOrigin;

uint lightSectionIndex;
if (_flw_chunkCoordToSectionIndex(blockPos >> 4, lightSectionIndex)) {
Expand Down Expand Up @@ -182,7 +182,7 @@ bool flw_light(vec3 worldPos, vec3 normal, out vec2 lightCoord) {
// Always use the section of the block we are contained in to ensure accuracy.
// We don't want to interpolate between sections, but also we might not be able
// to rely on the existence neighboring sections, so don't do any extra rounding here.
ivec3 blockPos = ivec3(floor(worldPos));
ivec3 blockPos = ivec3(floor(worldPos)) + flw_renderOrigin;

uint lightSectionIndex;
if (_flw_chunkCoordToSectionIndex(blockPos >> 4, lightSectionIndex)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ layout(std140) uniform _FlwFrameUniforms {
mat4 flw_viewProjectionInverse;
mat4 flw_viewProjectionPrev;

ivec4 _flw_renderOrigin;

vec4 _flw_cameraPos;
vec4 _flw_cameraPosPrev;
vec4 _flw_cameraLook;
Expand All @@ -47,10 +49,11 @@ layout(std140) uniform _FlwFrameUniforms {
uint _flw_debugMode;
};

#define flw_cameraPos _flw_cameraPos.xyz
#define flw_cameraLook _flw_cameraLook.xyz
#define flw_cameraPosPrev _flw_cameraPosPrev.xyz
#define flw_cameraLookPrev _flw_cameraLookPrev.xyz
#define flw_renderOrigin (_flw_renderOrigin.xyz)
#define flw_cameraPos (_flw_cameraPos.xyz)
#define flw_cameraLook (_flw_cameraLook.xyz)
#define flw_cameraPosPrev (_flw_cameraPosPrev.xyz)
#define flw_cameraLookPrev (_flw_cameraLookPrev.xyz)

#define FLW_CAMERA_IN_FLUID_WATER 1
#define FLW_CAMERA_IN_FLUID_LAVA 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
void flw_shaderLight() {
vec2 embeddedLight;
if (flw_lightFetch(ivec3(floor(flw_vertexPos.xyz)), embeddedLight)) {
if (flw_lightFetch(ivec3(floor(flw_vertexPos.xyz)) + flw_renderOrigin, embeddedLight)) {
flw_fragLight = max(flw_fragLight, embeddedLight);
}
}

0 comments on commit 8dce80b

Please sign in to comment.