From 183ee52863a08de736530cbd1760fab2e94e8af3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 10 May 2026 08:21:54 +0100 Subject: [PATCH] Drive lighting from emissive fields --- src/Aquarium.Engine/Render/D3D12Renderer.cs | 11 +- .../Render/Shaders/D3D12Medium.hlsl | 57 ++++--- .../Render/Shaders/D3D12Post.hlsl | 2 +- .../Render/Shaders/D3D12Scene.hlsl | 150 +++++++++++++----- state/evidence.jsonl | 1 + state/memory.json | 2 +- state/scratch.md | 11 ++ 7 files changed, 168 insertions(+), 66 deletions(-) diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 27a85db..2a3aee2 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -33,7 +33,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private const Format GridHeightFormat = Format.R16_Float; private const int PlanetCount = 5; private const int GridHeightBrushCount = PlanetCount + 1; - private const int FieldInstanceCount = PlanetCount + 5; + private const int FieldInstanceCount = PlanetCount + 6; private const float GridTransparentMinZ = -1.85f; private const float GridTransparentMaxZ = 0.45f; private const int BloomLevelCount = 3; @@ -1328,6 +1328,15 @@ public sealed class D3D12Renderer : IAquariumRenderer mediumId: 1.0f, color: new Vector3(0.30f, 0.50f, 0.68f), medium: new Vector4(0.190f, 0.130f, 0.0f, 1.20f)); + fieldInstances[10] = FieldInstanceGpu.Sphere( + fieldId: 5.0f, + flags: FieldFlags.Solid | FieldFlags.Emitter | FieldFlags.Receiver, + center: new Vector3(frame.CursorWorld.X, frame.CursorWorld.Y, CursorBodyRadius), + radius: CursorBodyBoundRadius, + materialId: 5.0f, + mediumId: 0.0f, + color: Vector3.Zero, + medium: Vector4.Zero); } private bool TryProjectSphereToViewFroxelBounds( diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl index 8114c1a..70cb6ca 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl @@ -36,15 +36,13 @@ StructuredBuffer fieldInstances : register(t12); Texture2D gridHeightTexture : register(t22); SamplerState sourceSampler : register(s0); -static const int FIELD_INSTANCE_COUNT = 10; +static const int FIELD_INSTANCE_COUNT = 11; static const int FIELD_FLAG_CLOUD = 2; +static const int FIELD_FLAG_EMITTER = 8; static const int MEDIUM_FROXEL_ATLAS_COLUMNS = 8; static const int MEDIUM_FROXEL_ATLAS_ROWS = 4; static const int MEDIUM_FROXEL_SLICE_COUNT = MEDIUM_FROXEL_ATLAS_COLUMNS * MEDIUM_FROXEL_ATLAS_ROWS; static const float PI = 3.14159265359; -static const float SELF_RADIUS = 1.12; -static const float3 SELF_CENTER = float3(0.0, 0.0, 2.2); -static const float3 SELF_EMISSIVE_RADIANCE = float3(10.0, 8.7, 4.2); struct VertexOut { @@ -278,11 +276,12 @@ float registeredMediumDensityOnly(float3 p) return saturate(density); } -float emissiveSphereSolidAngle(float3 p) +float emissiveFieldSolidAngle(float3 p, FieldInstance emitter) { - float3 toEmitter = SELF_CENTER - p; - float distanceToEmitter = max(length(toEmitter), SELF_RADIUS + 0.001); - float sinTheta = saturate(SELF_RADIUS / distanceToEmitter); + float emitterRadius = max(emitter.centerRadius.w, 0.001); + float3 toEmitter = emitter.centerRadius.xyz - p; + float distanceToEmitter = max(length(toEmitter), emitterRadius + 0.001); + float sinTheta = saturate(emitterRadius / distanceToEmitter); float cosTheta = sqrt(saturate(1.0 - sinTheta * sinTheta)); return 2.0 * PI * (1.0 - cosTheta); } @@ -293,11 +292,12 @@ float henyeyGreenstein(float cosTheta, float g) return (1.0 - g2) / max(4.0 * PI * pow(1.0 + g2 - 2.0 * g * cosTheta, 1.5), 0.0001); } -float emissiveSurfaceTransmittance(float3 p) +float emissiveSurfaceTransmittance(float3 p, FieldInstance emitter) { - float3 toEmitter = SELF_CENTER - p; + float emitterRadius = max(emitter.centerRadius.w, 0.001); + float3 toEmitter = emitter.centerRadius.xyz - p; float distanceToEmitter = length(toEmitter); - float pathLength = max(distanceToEmitter - SELF_RADIUS, 0.0); + float pathLength = max(distanceToEmitter - emitterRadius, 0.0); if (pathLength <= 0.001) { return 1.0; @@ -317,28 +317,39 @@ float emissiveSurfaceTransmittance(float3 p) return exp(-opticalDepth); } -float3 emissiveSurfaceScattering(float3 p, float3 rayDirection, float density) +float3 injectedEmitterRadiance(float3 p, float3 rayDirection) { - if (density <= 0.0) + float3 radiance = 0.0; + + [loop] + for (int i = 0; i < FIELD_INSTANCE_COUNT; i++) { - return 0.0; + FieldInstance emitter = fieldInstances[i]; + if (((int)(emitter.fieldFlags.y + 0.5) & FIELD_FLAG_EMITTER) == 0) + { + continue; + } + + float emitterRadius = max(emitter.centerRadius.w, 0.001); + float3 toEmitter = emitter.centerRadius.xyz - p; + float emitterDistance = length(toEmitter); + float3 lightDirection = toEmitter / max(emitterDistance, 0.0001); + float solidAngle = emissiveFieldSolidAngle(p, emitter); + float cosTheta = dot(-rayDirection, lightDirection); + float phase = henyeyGreenstein(cosTheta, 0.32); + float visibility = smoothstep(emitterRadius * 0.98, emitterRadius * 1.08, emitterDistance); + float transmittance = emissiveSurfaceTransmittance(p, emitter); + radiance += emitter.colorIntensity.rgb * solidAngle * phase * visibility * transmittance; } - float3 toEmitter = SELF_CENTER - p; - float3 lightDirection = normalize(toEmitter); - float solidAngle = emissiveSphereSolidAngle(p); - float cosTheta = dot(-rayDirection, lightDirection); - float phase = henyeyGreenstein(cosTheta, 0.32); - float visibility = smoothstep(SELF_RADIUS * 0.98, SELF_RADIUS * 1.08, length(toEmitter)); - float transmittance = emissiveSurfaceTransmittance(p); - return SELF_EMISSIVE_RADIANCE * solidAngle * phase * density * visibility * transmittance; + return radiance; } float registeredMediumDensity(float3 p, float3 rayDirection, out float3 scattering) { scattering = 0.0; float density = registeredMediumDensityOnly(p); - scattering = emissiveSurfaceScattering(p, rayDirection, density); + scattering = injectedEmitterRadiance(p, rayDirection) * density; return density; } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl index 0ac4c9d..d8045d9 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl @@ -77,7 +77,7 @@ static const float FIELD_ID_GRID = 4.0; static const float FIELD_ID_CURSOR = 5.0; static const float FIELD_ID_PLANET_BASE = 10.0; static const float MAX_HISTORY_AGE = 32.0; -static const int FIELD_INSTANCE_COUNT = 10; +static const int FIELD_INSTANCE_COUNT = 11; static const int FIELD_FLAG_CLOUD = 2; static const int MEDIUM_RAY_PREVIEW_STEPS = 48; VertexOut FullscreenTriangleVS(uint vertexId : SV_VertexID) diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl index de0691f..f19da9b 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl @@ -28,6 +28,18 @@ Texture2D mediumVolumeTexture : register(t13); Texture2D mediumTransportTexture : register(t14); SamplerState gridSampler : register(s0); +struct FieldInstance +{ + float4 centerRadius; + float4 radiusAngle; + float4 fieldFlags; + float4 materialMedium; + float4 colorIntensity; + float4 mediumTerms; +}; + +StructuredBuffer fieldInstances : register(t12); + static const int PLANET_COUNT = 5; static const float SUN_RADIUS = 1.12; static const float FIELD_ID_SELF = 2.0; @@ -55,8 +67,8 @@ static const float TERRAIN_ISOLINE_PIXEL_WIDTH = 0.54; static const float TERRAIN_FIELD_LINE_PIXEL_WIDTH = 0.38; static const float3 GRID_COLOR = float3(0.30, 0.90, 0.82); static const float GRID_ALPHA_SCALE = 0.56; -static const float3 SELF_CENTER = float3(0.0, 0.0, 2.2); -static const float3 SELF_EMISSIVE_RADIANCE = float3(10.0, 8.7, 4.2); +static const int FIELD_INSTANCE_COUNT = 11; +static const int FIELD_FLAG_EMITTER = 8; struct VertexOut { @@ -713,66 +725,124 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir return result; } -float emissiveSphereSolidAngle(float3 p, float radius) +float emitterSolidAngle(float3 p, FieldInstance emitter) { - float distanceToEmitter = max(length(SELF_CENTER - p), radius + 0.001); - float sinTheta = saturate(radius / distanceToEmitter); + float emitterRadius = max(emitter.centerRadius.w, 0.001); + float distanceToEmitter = max(length(emitter.centerRadius.xyz - p), emitterRadius + 0.001); + float sinTheta = saturate(emitterRadius / distanceToEmitter); float cosTheta = sqrt(saturate(1.0 - sinTheta * sinTheta)); return 2.0 * PI * (1.0 - cosTheta); } -float3 emissiveSphereIrradiance(float3 p, float3 normal, float radius, out float3 lightDirection, out float solidAngle) +float3 primitiveEmissionRadiance(float fieldId) { - float3 toEmitter = SELF_CENTER - p; - lightDirection = normalize(toEmitter); - solidAngle = emissiveSphereSolidAngle(p, radius); - float surfaceFacing = saturate(dot(normal, lightDirection)); - float emitterFacing = smoothstep(radius * 0.98, radius * 1.08, length(toEmitter)); - return SELF_EMISSIVE_RADIANCE * solidAngle * surfaceFacing * emitterFacing; + [loop] + for (int i = 0; i < FIELD_INSTANCE_COUNT; i++) + { + FieldInstance emitter = fieldInstances[i]; + if (abs(emitter.fieldFlags.x - fieldId) < 0.25 && + (((int)(emitter.fieldFlags.y + 0.5) & FIELD_FLAG_EMITTER) != 0)) + { + return emitter.colorIntensity.rgb; + } + } + + return 0.0; +} + +float3 diffuseEmitterIrradiance(float3 p, float3 normal, float receiverFieldId) +{ + float3 irradiance = 0.0; + + [loop] + for (int i = 0; i < FIELD_INSTANCE_COUNT; i++) + { + FieldInstance emitter = fieldInstances[i]; + if ((((int)(emitter.fieldFlags.y + 0.5) & FIELD_FLAG_EMITTER) == 0) || + abs(emitter.fieldFlags.x - receiverFieldId) < 0.25) + { + continue; + } + + float emitterRadius = max(emitter.centerRadius.w, 0.001); + float3 toEmitter = emitter.centerRadius.xyz - p; + float emitterDistance = length(toEmitter); + float3 lightDirection = toEmitter / max(emitterDistance, 0.0001); + float facing = saturate(dot(normal, lightDirection)); + float visibility = smoothstep(emitterRadius * 0.98, emitterRadius * 1.08, emitterDistance); + irradiance += emitter.colorIntensity.rgb * emitterSolidAngle(p, emitter) * facing * visibility; + } + + return irradiance; +} + +float3 cursorSpecularEmitterRadiance(float3 p, float3 normal, float receiverFieldId) +{ + static const float MinimumRoughness = 0.045; + static const float CursorRoughness = 0.22; + + float3 viewDirection = normalize(cameraPosition - p); + float ndv = saturate(dot(normal, viewDirection)); + float roughness = max(CursorRoughness, MinimumRoughness); + float alpha = roughness * roughness; + float alpha2 = alpha * alpha; + float k = (roughness + 1.0); + k = (k * k) * 0.125; + float geometryV = ndv / max(ndv * (1.0 - k) + k, 0.00001); + float3 f0 = float3(0.95, 0.62, 0.26); + float3 reflected = 0.0; + + [loop] + for (int i = 0; i < FIELD_INSTANCE_COUNT; i++) + { + FieldInstance emitter = fieldInstances[i]; + if ((((int)(emitter.fieldFlags.y + 0.5) & FIELD_FLAG_EMITTER) == 0) || + abs(emitter.fieldFlags.x - receiverFieldId) < 0.25) + { + continue; + } + + float emitterRadius = max(emitter.centerRadius.w, 0.001); + float3 toEmitter = emitter.centerRadius.xyz - p; + float emitterDistance = length(toEmitter); + float3 lightDirection = toEmitter / max(emitterDistance, 0.0001); + float3 halfVector = normalize(lightDirection + viewDirection); + float ndl = saturate(dot(normal, lightDirection)); + float ndh = saturate(dot(normal, halfVector)); + float vdh = saturate(dot(viewDirection, halfVector)); + float denominator = ndh * ndh * (alpha2 - 1.0) + 1.0; + float distribution = alpha2 / max(PI * denominator * denominator, 0.00001); + float geometryL = ndl / max(ndl * (1.0 - k) + k, 0.00001); + float geometry = geometryL * geometryV; + float3 fresnel = f0 + (1.0 - f0) * pow(1.0 - vdh, 5.0); + float3 specular = (distribution * geometry) * fresnel / max(4.0 * ndl * ndv, 0.00001); + float visibility = smoothstep(emitterRadius * 0.98, emitterRadius * 1.08, emitterDistance); + reflected += specular * emitter.colorIntensity.rgb * emitterSolidAngle(p, emitter) * ndl * visibility; + } + + return reflected; } float3 shadeBody(float3 p, float3 normal, int primitiveId) { + float fieldId = primitiveFieldId(primitiveId); + float3 emission = primitiveEmissionRadiance(fieldId); if (primitiveId == 0) { - return SELF_EMISSIVE_RADIANCE; + return emission; } - float3 lightDirection; - float solidAngle; - float3 irradiance = emissiveSphereIrradiance(p, normal, SUN_RADIUS, lightDirection, solidAngle); + float3 irradiance = diffuseEmitterIrradiance(p, normal, fieldId); float irradianceLuma = dot(irradiance, float3(0.2126, 0.7152, 0.0722)); if (primitiveId == CURSOR_PRIMITIVE_ID) { - static const float MinimumRoughness = 0.045; - static const float CursorRoughness = 0.22; - - float3 viewDirection = normalize(cameraPosition - p); - float3 halfVector = normalize(lightDirection + viewDirection); - float ndv = saturate(dot(normal, viewDirection)); - float ndh = saturate(dot(normal, halfVector)); - float vdh = saturate(dot(viewDirection, halfVector)); - float ndl = saturate(dot(normal, lightDirection)); - float roughness = max(CursorRoughness, MinimumRoughness); - float alpha = roughness * roughness; - float alpha2 = alpha * alpha; - float denominator = ndh * ndh * (alpha2 - 1.0) + 1.0; - float distribution = alpha2 / max(PI * denominator * denominator, 0.00001); - float k = (roughness + 1.0); - k = (k * k) * 0.125; - float geometryL = ndl / max(ndl * (1.0 - k) + k, 0.00001); - float geometryV = ndv / max(ndv * (1.0 - k) + k, 0.00001); - float geometry = geometryL * geometryV; - float3 f0 = float3(0.95, 0.62, 0.26); - float3 fresnel = f0 + (1.0 - f0) * pow(1.0 - vdh, 5.0); - float3 specular = (distribution * geometry) * fresnel / max(4.0 * ndl * ndv, 0.00001); - return specular * irradiance; + return emission + cursorSpecularEmitterRadiance(p, normal, fieldId); } float hue = hash21(float2(primitiveId, 6.3)); float3 albedo = lerp(float3(0.34, 0.42, 0.18), float3(0.70, 0.76, 0.42), hue); - return albedo * irradianceLuma / PI; + return emission + albedo * irradianceLuma / PI; } SceneOut D3D12ScenePS(VertexOut input) diff --git a/state/evidence.jsonl b/state/evidence.jsonl index cb75350..70dc138 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -128,3 +128,4 @@ {"timestamp":"2026-05-10T07:56:15.1955843+01:00","topic":"d3d12-cursor-ggx-metal","lesson":"Cursor brass now uses a compact GGX microfacet metal model instead of Blinn-style tinted specular. It has brass F0, Schlick Fresnel, GGX distribution, Smith-Schlick visibility, no diffuse metal term, no glow, and no ambient floor. Until a reflection source exists, Self is the only direct radiance term.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]} {"timestamp":"2026-05-10T08:06:35+01:00","topic":"grid-fog-volume","lesson":"Below-Grid pea-soup fog now enters the D3D12 medium path as real density/scattering. The medium pass and post ray-density debug bind the Grid height target at t22, use the actual height surface as the fog ceiling, and apply cheap Aetheria-style triangular-noise domain warping so final/debug views share the same procedural medium term.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]} {"timestamp":"2026-05-10T08:15:20+01:00","topic":"emissive-only-fog-lighting","lesson":"D3D12 fog density no longer contains baked scattering color, and solid shading no longer uses a point-light radiance constant. Self is treated as a finite emissive sphere: medium in-scattering uses solid angle, Henyey-Greenstein phase, and cheap light-path transmittance; surfaces use emissive-sphere irradiance. Full noisy density inside the light-path propagation loop stalled shader pipeline compilation, so propagation uses a cheap extinction approximation while visible samples keep textured density.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]} +{"timestamp":"2026-05-10T08:22:10+01:00","topic":"field-emitter-light-injection","lesson":"The direct medium/surface lighting path now uses field-emitter data instead of hardcoded Self helpers. `FieldFlags.Emitter` plus `colorIntensity.rgb` inject radiance into D3D12 froxel medium samples and drive surface diffuse/specular lighting from the same table. Cursor is present as field 5 with zero emission until enabled; nebula/cloud fields can become emissive through data instead of bespoke shader branches.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]} diff --git a/state/memory.json b/state/memory.json index 82613d1..0e2c406 100644 --- a/state/memory.json +++ b/state/memory.json @@ -52,7 +52,7 @@ "HDR/PBR presentation should follow research/rendering/hdr-pbr-presentation.md: scene-linear lighting, explicit exposure, pre-tonemap bloom/veil, then a display transform. Do not reach for 2000s threshold bloom when the frame needs dynamic range.", "Aquarium's current HDR presentation pass uses fixed manual exposure plus a three-level half/quarter/eighth pre-tonemap bloom pyramid with firefly-safe downsampling and separable blur. Next HDR work is persisted exposure/display controls, not threshold glow.", "Future volumetric lighting should be a field sampled consistently by terrain, planets, fog, particles, and diegetic UI. Do not fake only the planet term if the scene is supposed to bounce light.", - "Volumetric density must not self-illuminate through baked color. D3D12 direct fog lighting currently treats Self as a finite emissive sphere: solid angle plus phase for in-scattering, with cheap depth/radial light-path attenuation so shader compilation stays sane. Do not reintroduce point-light constants or ambient fog glow.", + "Volumetric density must not self-illuminate through baked color. D3D12 direct fog lighting injects radiance from `FieldFlags.Emitter` instances into froxel samples, using emitter `colorIntensity.rgb`, finite solid angle, phase, and cheap depth/radial light-path attenuation so shader compilation stays sane. Self, cursor, nebulae, and future emissive SDFs must all use this field-emitter path; do not reintroduce point-light constants or ambient fog glow.", "Volumetric design must start from research/volumetrics/synthesis.md. The first pass should follow Wronski/Hillaire production shape: explicit low-resolution frustum volume, injected density/extinction/scattering, Self as first emitter, front-to-back transmittance/in-scattering integration, then consistent composition.", "For GPU SDF or newer volumetric storage, consult research/volumetrics/recent-volumetrics-and-gpu-sdf.md. The durable pattern is cascaded sparse bricks, compressed distance/occupancy, separate static/dynamic updates, toroidal partial updates, and learned fields only when runtime evaluation stays cheap and inspectable.", "For research decks with recordings, pull subtitles/transcripts before distilling. The Dreams transcript changed the lesson: preserve the evaluator/field that serves the image, not the elegant SDF/brick/froxel experiment that drifted away from the art direction.", diff --git a/state/scratch.md b/state/scratch.md index 1a9bc6f..cb4b546 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -85,6 +85,17 @@ reusing field identity/travel/control. emitter-to-sample march made shader pipeline compilation stall, so light-path attenuation deliberately uses a cheap extinction approximation while visible camera samples keep the textured density. +- Emissive field injection correction: the D3D12 lighting path no longer + hardcodes Self as the only direct emitter. `FieldInstance` data now carries + the emitter contract for medium injection and surface lighting: + `FieldFlags.Emitter` plus `colorIntensity.rgb` define emissive radiance. + The medium pass injects radiance from all emitter fields into the froxel + sample before scattering, with cheap path transmittance. The scene pass uses + the same emitter table for planet diffuse and cursor GGX specular. Self is + just field `2`; cursor is now present in the field table as field `5` with + zero emission until a control turns it on; nebula/cloud fields can become + emissive by setting the emitter flag and radiance instead of adding bespoke + shader branches. - Cursor locator SDF: `AquariumFrame` carries the mouse cursor projected onto the Grid XY plane. D3D12 bins a cursor primitive into the existing view-froxel solid table and traces it as an asymmetric teardrop lobe: local `z = -1`