Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,55 @@
// Massive kudos to RogueLogix for figuring out this LUT scheme.
// First layer is Y, then X, then Z.
public final class LightLut {
public final Layer<Layer<IntLayer>> indices = new Layer<>();
public final Layer<Layer<Layer<IntLayer>>> indices = new Layer<>();

public void add(long position, int index) {
public void add(int scene, long position, int index) {
final var x = SectionPos.x(position);
final var y = SectionPos.y(position);
final var z = SectionPos.z(position);

indices.computeIfAbsent(y, Layer::new)
indices.computeIfAbsent(scene, Layer::new)
.computeIfAbsent(y, Layer::new)
.computeIfAbsent(x, IntLayer::new)
.set(z, index + 1);
}

public void prune() {
// Maybe this could be done better incrementally?
indices.prune((middle) -> middle.prune(IntLayer::prune));
indices.prune((scene) -> scene.prune((middle) -> middle.prune(IntLayer::prune)));
}

public void remove(long section) {
public void remove(int scene, long section) {
final var x = SectionPos.x(section);
final var y = SectionPos.y(section);
final var z = SectionPos.z(section);

var first = indices.get(y);
var first = indices.get(scene);

if (first == null) {
return;
}

var second = first.get(x);
var second = first.get(y);

if (second == null) {
return;
}

second.clear(z);
var third = second.get(x);

if (third == null) {
return;
}

third.clear(z);
}

public IntArrayList flatten() {
final var out = new IntArrayList();
indices.fillLut(out, (yIndices, lut) -> yIndices.fillLut(lut, IntLayer::fillLut));
this.indices.fillLut(out, (sceneIndices, lut1) -> sceneIndices.fillLut(lut1,
(yIndices, lut2) -> yIndices.fillLut(lut2, IntLayer::fillLut)
));
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ private void removeUnusedSections() {
}

private void beginTrackingSection(long section, int index) {
lut.add(section, index);
lut.add(0, section, index);
needsLutRebuild = true;
}

private void endTrackingSection(long section) {
lut.remove(section);
lut.remove(0, section);
needsLutRebuild = true;
}

Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to expose the scene ID/embedding ID here

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ struct FlwLightAo {
float ao;
};

/// Get the light at the given world position relative to flw_renderOrigin from the given normal.
/// Get the light at the given scene and 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 FlwLightAo light);
bool flw_light(uint scene, vec3 worldPos, vec3 normal, out FlwLightAo light);

/// Fetches the light value at the given block position.
/// Fetches the light value at the given scene and block position.
/// Returns false if the light for the given block is not available.
bool flw_lightFetch(ivec3 blockPos, out vec2 light);
bool flw_lightFetch(uint scene, ivec3 blockPos, out vec2 light);
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ bool _flw_nextLut(uint base, int coord, out uint next) {
return false;
}

bool _flw_chunkCoordToSectionIndex(ivec3 sectionPos, out uint index) {
bool _flw_chunkCoordToSectionIndex(uint sceneId, ivec3 sectionPos, out uint index) {
uint scene;
if (_flw_nextLut(0u, int(sceneId), scene) || scene == 0u) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast to int here is a little spooky since the data really is a uint, but we'll realistically run out of memory before this causes issues.

Still, it could be good to have a copy of _flw_nextLut that takes a uint index

return true;
}

uint first;
if (_flw_nextLut(0u, sectionPos.y, first) || first == 0u) {
if (_flw_nextLut(scene, sectionPos.y, first) || first == 0u) {
return true;
}

Expand Down Expand Up @@ -90,9 +95,9 @@ bool _flw_isSolid(uint sectionOffset, uvec3 blockInSectionPos) {
return (word & (1u << bitInWordOffset)) != 0u;
}

bool flw_lightFetch(ivec3 blockPos, out vec2 lightCoord) {
bool flw_lightFetch(uint scene, ivec3 blockPos, out vec2 lightCoord) {
uint lightSectionIndex;
if (_flw_chunkCoordToSectionIndex(blockPos >> 4, lightSectionIndex)) {
if (_flw_chunkCoordToSectionIndex(scene, blockPos >> 4, lightSectionIndex)) {
return false;
}
// The offset of the section in the light buffer.
Expand Down Expand Up @@ -307,14 +312,14 @@ vec3 _flw_lightForDirection(uint[27] lights, vec3 interpolant, uint c00, uint c0
return light;
}

bool flw_light(vec3 worldPos, vec3 normal, out FlwLightAo light) {
bool flw_light(uint scene, vec3 worldPos, vec3 normal, out FlwLightAo light) {
// 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)) + flw_renderOrigin;

uint lightSectionIndex;
if (_flw_chunkCoordToSectionIndex(blockPos >> 4, lightSectionIndex)) {
if (_flw_chunkCoordToSectionIndex(scene, blockPos >> 4, lightSectionIndex)) {
return false;
}
// The offset of the section in the light buffer.
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)) + flw_renderOrigin, embeddedLight)) {
if (flw_lightFetch(0, ivec3(floor(flw_vertexPos.xyz)) + flw_renderOrigin, embeddedLight)) {
flw_fragLight = max(flw_fragLight, embeddedLight);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
void flw_shaderLight() {
FlwLightAo light;
if (flw_light(flw_vertexPos.xyz, flw_vertexNormal, light)) {
if (flw_light(0, flw_vertexPos.xyz, flw_vertexNormal, light)) {
flw_fragLight = max(flw_fragLight, light.light);

flw_fragColor.rgb *= light.ao;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
void flw_shaderLight() {
#ifdef FLW_EMBEDDED
FlwLightAo light;
if (flw_light(flw_vertexPos.xyz, flw_vertexNormal, light)) {
if (flw_light(0, flw_vertexPos.xyz, flw_vertexNormal, light)) {
flw_fragLight = max(flw_fragLight, light.light);

flw_fragColor.rgb *= light.ao;
Expand Down
8 changes: 4 additions & 4 deletions docs/shader-api/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ struct FlwLightAo {
float ao;
};

/// Get the light at the given world position.
/// Get the light at the given scene and world position.
/// This may be interpolated for smooth lighting.
bool flw_light(vec3 worldPos, vec3 normal, out FlwLightAo light);
bool flw_light(uint scene, vec3 worldPos, vec3 normal, out FlwLightAo light);

/// Fetches the light value at the given block position.
/// Fetches the light value at the given scene and block position.
/// Returns false if the light for the given block is not available.
bool flw_lightFetch(ivec3 blockPos, out vec2 light);
bool flw_lightFetch(uint scene, ivec3 blockPos, out vec2 light);