From 3dc4cf0841977c2723946fe08a519e47fea82c54 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 1 Aug 2024 12:14:14 -0700 Subject: [PATCH] All okay - Use struct to separate light and ao fields - Move light config TODO to impl - Fix formatting in InstancerProvider docs --- .../flywheel/api/instance/InstancerProvider.java | 8 ++++---- .../flywheel/backend/compile/FlwPrograms.java | 1 + .../flywheel/flywheel/internal/api_impl.glsl | 7 +++++-- .../flywheel/flywheel/internal/light_lut.glsl | 15 ++++++++++----- .../assets/flywheel/flywheel/light/smooth.glsl | 6 +++--- .../flywheel/light/smooth_when_embedded.glsl | 6 +++--- docs/shader-api/common.glsl | 7 ++++++- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/common/src/api/java/dev/engine_room/flywheel/api/instance/InstancerProvider.java b/common/src/api/java/dev/engine_room/flywheel/api/instance/InstancerProvider.java index a921ca61c..d228eea3c 100644 --- a/common/src/api/java/dev/engine_room/flywheel/api/instance/InstancerProvider.java +++ b/common/src/api/java/dev/engine_room/flywheel/api/instance/InstancerProvider.java @@ -17,13 +17,13 @@ public interface InstancerProvider { *

Render Order

*

In general, you can assume all instances in the same instancer will be rendered in a single draw call. * Backends are free to optimize the ordering of draw calls to a certain extent, but utilities are provided to let - * you control the order of draw calls + * you exert control over the ordering.

*

Mesh Order

- *
For one, Meshes within a Model are guaranteed to render in the order they appear in their containing list. + *

For one, Meshes within a Model are guaranteed to render in the order they appear in their containing list. * This lets you e.g. preserve (or break!) vanilla's chunk RenderType order guarantees or control which Meshes of - * your Model render over others. + * your Model render over others.

*

Bias Order

- *
The other method is via the {@code bias} parameter to this method. An instancer with a lower bias will have + *

The other method is via the {@code bias} parameter to this method. An instancer with a lower bias will have * its instances draw BEFORE an instancer with a higher bias. This allows you to control the render order between * your instances to e.g. create an "overlay" instance to selectively color or apply decals to another instance.

* diff --git a/common/src/backend/java/dev/engine_room/flywheel/backend/compile/FlwPrograms.java b/common/src/backend/java/dev/engine_room/flywheel/backend/compile/FlwPrograms.java index 9eb89b41c..6681d4676 100644 --- a/common/src/backend/java/dev/engine_room/flywheel/backend/compile/FlwPrograms.java +++ b/common/src/backend/java/dev/engine_room/flywheel/backend/compile/FlwPrograms.java @@ -120,6 +120,7 @@ public final class FlwPrograms { .build(loader); } + // TODO: Do not uber this component. Shader compile times are very high now @Nullable private static UberShaderComponent createLightComponent(SourceLoader loader) { return UberShaderComponent.builder(Flywheel.rl("light")) diff --git a/common/src/backend/resources/assets/flywheel/flywheel/internal/api_impl.glsl b/common/src/backend/resources/assets/flywheel/flywheel/internal/api_impl.glsl index 483155c42..1738c38e6 100644 --- a/common/src/backend/resources/assets/flywheel/flywheel/internal/api_impl.glsl +++ b/common/src/backend/resources/assets/flywheel/flywheel/internal/api_impl.glsl @@ -1,8 +1,11 @@ -// TODO: Add config for light smoothness. Should work at a compile flag level +struct FlwLightAo { + vec2 light; + float ao; +}; /// 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 vec3 light); +bool flw_light(vec3 worldPos, vec3 normal, out FlwLightAo light); /// Fetches the light value at the given block position. /// Returns false if the light for the given block is not available. diff --git a/common/src/backend/resources/assets/flywheel/flywheel/internal/light_lut.glsl b/common/src/backend/resources/assets/flywheel/flywheel/internal/light_lut.glsl index 4ff1f60db..63d555708 100644 --- a/common/src/backend/resources/assets/flywheel/flywheel/internal/light_lut.glsl +++ b/common/src/backend/resources/assets/flywheel/flywheel/internal/light_lut.glsl @@ -154,7 +154,7 @@ uint _flw_fetchSolid3x3x3(uint sectionOffset, ivec3 blockInSectionPos) { #define _flw_index3x3x3(x, y, z) ((x) + (z) * 3u + (y) * 9u) #define _flw_index3x3x3v(p) _flw_index3x3x3((p.x), (p.y), (p.z)) -#define _flw_validCountToAO(validCount) (1. - (4. - (validCount)) * 0.2) +#define _flw_validCountToAo(validCount) (1. - (4. - (validCount)) * 0.2) /// Calculate the light for a direction by averaging the light at the corners of the block. /// @@ -229,12 +229,13 @@ vec3 _flw_lightForDirection(uint[27] lights, vec3 interpolant, uvec3 c00, uvec3 // Normalize the light coords light.xy *= 1. / 15.; // Calculate the AO multiplier from the number of valid blocks - light.z = _flw_validCountToAO(light.z); + light.z = _flw_validCountToAo(light.z); return light; } -bool flw_light(vec3 worldPos, vec3 normal, out vec3 light) { +// TODO: Add config for light smoothness. Should work at a compile flag level +bool flw_light(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. @@ -255,7 +256,8 @@ bool flw_light(vec3 worldPos, vec3 normal, out vec3 light) { if (solid == _FLW_COMPLETELY_SOLID) { // No point in doing any work if the entire 3x3x3 volume around us is filled. // Kinda rare but this may happen if our fragment is in the middle of a lot of tinted glass - light = vec3(0., 0., _flw_validCountToAO(0.)); + light.light = vec2(0.); + light.ao = _flw_validCountToAo(0.); return true; } @@ -294,7 +296,10 @@ bool flw_light(vec3 worldPos, vec3 normal, out vec3 light) { } vec3 n2 = normal * normal; - light = lightX * n2.x + lightY * n2.y + lightZ * n2.z; + vec3 lightAo = lightX * n2.x + lightY * n2.y + lightZ * n2.z; + + light.light = lightAo.xy; + light.ao = lightAo.z; return true; } diff --git a/common/src/lib/resources/assets/flywheel/flywheel/light/smooth.glsl b/common/src/lib/resources/assets/flywheel/flywheel/light/smooth.glsl index a8fae63a1..4844308d9 100644 --- a/common/src/lib/resources/assets/flywheel/flywheel/light/smooth.glsl +++ b/common/src/lib/resources/assets/flywheel/flywheel/light/smooth.glsl @@ -1,8 +1,8 @@ void flw_shaderLight() { - vec3 light; + FlwLightAo light; if (flw_light(flw_vertexPos.xyz, flw_vertexNormal, light)) { - flw_fragLight = max(flw_fragLight, light.xy); + flw_fragLight = max(flw_fragLight, light.light); - flw_fragColor.rgb *= light.z; + flw_fragColor.rgb *= light.ao; } } diff --git a/common/src/lib/resources/assets/flywheel/flywheel/light/smooth_when_embedded.glsl b/common/src/lib/resources/assets/flywheel/flywheel/light/smooth_when_embedded.glsl index 609368930..035c6e9ad 100644 --- a/common/src/lib/resources/assets/flywheel/flywheel/light/smooth_when_embedded.glsl +++ b/common/src/lib/resources/assets/flywheel/flywheel/light/smooth_when_embedded.glsl @@ -1,10 +1,10 @@ void flw_shaderLight() { #ifdef FLW_EMBEDDED - vec3 light; + FlwLightAo light; if (flw_light(flw_vertexPos.xyz, flw_vertexNormal, light)) { - flw_fragLight = max(flw_fragLight, light.xy); + flw_fragLight = max(flw_fragLight, light.light); - flw_fragColor *= light.z; + flw_fragColor.rgb *= light.ao; } #endif } diff --git a/docs/shader-api/common.glsl b/docs/shader-api/common.glsl index bbd75d071..4b073f1db 100644 --- a/docs/shader-api/common.glsl +++ b/docs/shader-api/common.glsl @@ -1,6 +1,11 @@ +struct FlwLightAo { + vec2 light; + float ao; +}; + /// Get the light at the given world position. /// This may be interpolated for smooth lighting. -bool flw_light(vec3 worldPos, vec3 normal, out vec3 light); +bool flw_light(vec3 worldPos, vec3 normal, out FlwLightAo light); /// Fetches the light value at the given block position. /// Returns false if the light for the given block is not available.