Skip to content
Merged
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
90 changes: 59 additions & 31 deletions engine/assets/shaders/gi_clip_fill.slang
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ RWTexture3D<float4> gOpacity;
[[vk::image_format("r32ui")]]
RWTexture3D<uint> gAmbient[kGiClipFaces];

// Mirrors scene_trace.slang which is not imported since it pulls in lighting
[[vk::binding(10, 1)]]
StructuredBuffer<VoxelInstance> gTraceVolumes;
/// xyz centre w radius
[[vk::binding(11, 1)]]
StructuredBuffer<float4> gTraceBounds;
[[vk::binding(12, 1)]]
StructuredBuffer<uint> gTraceGrid;
// gTraceVolumes, gTraceBounds and gTraceGrid come from voxel_dda; scene_trace would pull in lighting

static const uint kTraceFarCount = 6u;
static const uint kTraceFarOffset = 8u;
Expand Down Expand Up @@ -66,14 +59,12 @@ bool clipSolidBit(ClipCell cell, uint index)
return (((index < 32u ? cell.solid.x : cell.solid.y) >> (index & 31u)) & 1u) != 0u;
}

void clipSample(uint v, float3 world, float gap, uint index, inout ClipCell cell)
void clipSample(VoxelInstance volume, VolumeRecord record, float3 world, float gap, uint index, inout ClipCell cell)
{
if (clipSolidBit(cell, index))
{
return;
}
const VoxelInstance volume = gTraceVolumes[v];
const VolumeRecord record = gVolumeRecords[volume.recordIndex];
// Pyramid block at least as wide as the sample gap in voxels so nothing thinner slips between samples
const float gapVoxels = gap * length(volume.worldToVoxel[0].xyz);
const uint pyramid = uint(clamp(ceil(log2(max(gapVoxels, 1.0)) - 0.01), 0.0, float(kLevelCoarse)));
Expand Down Expand Up @@ -184,6 +175,32 @@ float3 clipSamplePoint(float3 low, float cellSize, uint index)
return low + (float3(index & 3u, (index >> 2) & 3u, index >> 4) + 0.5) * (cellSize / float(kClipSide));
}

bool clipFull(ClipCell cell)
{
return all(cell.solid == uint2(0xFFFFFFFFu, 0xFFFFFFFFu));
}

/// Only the samples inside the bounds of volume v can hit it
void clipVolume(uint v, float4 bounds, float3 low, float cellSize, float gap, inout ClipCell cell)
{
const VoxelInstance volume = gTraceVolumes[v];
const VolumeRecord record = gVolumeRecords[volume.recordIndex];
const float3 rel = (bounds.xyz - low) / gap - 0.5;
const int3 first = clamp(int3(ceil(rel - bounds.w / gap)), int3(0, 0, 0), int3(3, 3, 3));
const int3 last = clamp(int3(floor(rel + bounds.w / gap)), int3(0, 0, 0), int3(3, 3, 3));
for (int z = first.z; z <= last.z; ++z)
{
for (int y = first.y; y <= last.y; ++y)
{
for (int x = first.x; x <= last.x; ++x)
{
const uint s = uint(x + 4 * y + 16 * z);
clipSample(volume, record, clipSamplePoint(low, cellSize, s), gap, s, cell);
}
}
}
}

[shader("compute")]
[numthreads(4, 4, 4)]
void fillMain(uint3 id : SV_DispatchThreadID)
Expand Down Expand Up @@ -216,36 +233,47 @@ void fillMain(uint3 id : SV_DispatchThreadID)
{
continue;
}
for (uint s = 0u; s < kClipSamples; ++s)
{
clipSample(v, clipSamplePoint(low, cellSize, s), gap, s, cell);
}
clipVolume(v, bounds, low, cellSize, gap, cell);
}

const float3 gridOrigin = float3(asfloat(gTraceGrid[0]), asfloat(gTraceGrid[1]), asfloat(gTraceGrid[2]));
const float gridCell = asfloat(gTraceGrid[3]);
const int gridDim = int(gTraceGrid[4]);
const uint entriesBase = gTraceGrid[kTraceEntriesOffset];
for (uint s = 0u; s < kClipSamples; ++s)
const float3 high = low + cellSize;
const int3 gridLo = max(int3(floor((low - gridOrigin) / gridCell)), int3(0, 0, 0));
const int3 gridHi = min(int3(floor((high - gridOrigin) / gridCell)), int3(gridDim - 1, gridDim - 1, gridDim - 1));
// A fully solid cell cannot change any more
bool full = clipFull(cell);
for (int gz = gridLo.z; gz <= gridHi.z && !full; ++gz)
{
const float3 point = clipSamplePoint(low, cellSize, s);
const int3 g = int3(floor((point - gridOrigin) / gridCell));
if (any(g < 0) || any(g >= gridDim))
{
continue;
}
const uint index = uint(g.x + g.y * gridDim + g.z * gridDim * gridDim);
const uint first = gTraceGrid[kTraceHeaderWords + 2u * index];
const uint count = gTraceGrid[kTraceHeaderWords + 2u * index + 1u];
for (uint e = 0u; e < count; ++e)
for (int gy = gridLo.y; gy <= gridHi.y && !full; ++gy)
{
const uint v = gTraceGrid[entriesBase + first + e];
const float4 bounds = gTraceBounds[v];
if (distance(point, bounds.xyz) > bounds.w)
for (int gx = gridLo.x; gx <= gridHi.x && !full; ++gx)
{
continue;
const int3 g = int3(gx, gy, gz);
const uint index = uint(g.x + g.y * gridDim + g.z * gridDim * gridDim);
const uint first = gTraceGrid[kTraceHeaderWords + 2u * index];
const uint count = gTraceGrid[kTraceHeaderWords + 2u * index + 1u];
for (uint e = 0u; e < count && !full; ++e)
{
const uint v = gTraceGrid[entriesBase + first + e];
const float4 bounds = gTraceBounds[v];
const float3 nearest = clamp(bounds.xyz, low, high);
if (dot(nearest - bounds.xyz, nearest - bounds.xyz) > bounds.w * bounds.w)
{
continue;
}
// A volume listed in several of these grid cells runs once, in the first
const int3 volumeLo = int3(floor((bounds.xyz - bounds.w - gridOrigin) / gridCell));
if (any(max(volumeLo, gridLo) != g) || !clipCellReaches(v, low, cellSize))
{
continue;
}
clipVolume(v, bounds, low, cellSize, gap, cell);
full = clipFull(cell);
}
}
clipSample(v, point, gap, s, cell);
}
}

Expand Down
31 changes: 31 additions & 0 deletions engine/assets/shaders/gi_frame.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Clipmap frame data without the textures, so importers do not get gi_sample samplers in their layout
import gi_clip_common;

/// Published by GiClipmapPass after its inject and size.z 0 means no clipmap GI
[[vk::binding(24, 0)]]
StructuredBuffer<GiClipFrame> gGiClipFrame;

/// Cells of level 0 the cones start off the surface so its own cells do not shade it
static const float kGiClipConeLift = 1.5;

float giClipCellAt(GiClipFrame frame, float3 worldPosition)
{
return frame.levels[0].w * exp2(min(giClipLevelSoft(frame, worldPosition), float(frame.size.y - 1u)));
}

float giIntensity()
{
return gGiClipFrame[0].camera.w;
}

bool giClipCovers(float3 worldPosition)
{
const GiClipFrame frame = gGiClipFrame[0];
return frame.size.z != 0u && giClipLevelSoft(frame, worldPosition) < float(kGiClipMaxLevels);
}

/// Meters of contact a short ray adds below the cell the clipmap blurs at the point, valid where giClipCovers
float giContactReach(float3 worldPosition)
{
return kGiClipConeLift * giClipCellAt(gGiClipFrame[0], worldPosition);
}
4 changes: 4 additions & 0 deletions engine/assets/shaders/gi_frame.slang.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
uid = "G1Fram3C000"
type = "shader"
created_at = "2026-11-28T00:00:00.0000000Z"
modified_at = "2026-11-28T00:00:00.0000000Z"
29 changes: 1 addition & 28 deletions engine/assets/shaders/gi_sample.slang
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Set 0 written by SceneDescriptorSets from GiClipmapPass
import gi_common;
import gi_clip_common;

/// Published by GiClipmapPass after its inject and size.z 0 means no clipmap GI
[[vk::binding(24, 0)]]
StructuredBuffer<GiClipFrame> gGiClipFrame;
__exported import gi_frame;

/// Face radiance at level * kGiClipFaces + face
[[vk::binding(25, 0)]]
Expand All @@ -23,8 +20,6 @@ static const float kGiClipConeSpread = 1.1547;
static const uint kGiClipConeSteps = 10u;
/// 0 since finer levels leave blind rings between cones that show as halos
static const float kGiClipConeBias = 0.0;
/// Cells of level 0 the cones start off the surface so its own cells do not shade it
static const float kGiClipConeLift = 1.5;
static const float kGiClipFaceWeight = 1e-4;
/// Highest level the cones read through the B-spline filter
static const uint kGiClipSmoothLevels = 1u;
Expand Down Expand Up @@ -177,11 +172,6 @@ float4 giClipCone<let Smooth : int>(GiClipFrame frame, float3 origin, float3 dir
return sum;
}

float giClipCellAt(GiClipFrame frame, float3 worldPosition)
{
return frame.levels[0].w * exp2(min(giClipLevelSoft(frame, worldPosition), float(frame.size.y - 1u)));
}

/// xyz irradiance over pi and w the share of the hemisphere the cones left open
/// @p jitter in 0 to 1 starts the cones that share of a cell further out so a history averages away where steps land
/// @p Smooth 0 reads trilinear where a history already averages away the creases the B-spline rounds off
Expand Down Expand Up @@ -349,23 +339,6 @@ bool giHitIrradianceNearest(float3 worldPosition, float3 normal, out float3 irra
}

/// Bounce intensity for lit surfaces only since gi_clip_bounce feeds sampleGiClipmap back into itself
float giIntensity()
{
return gGiClipFrame[0].camera.w;
}

bool giClipCovers(float3 worldPosition)
{
const GiClipFrame frame = gGiClipFrame[0];
return frame.size.z != 0u && giClipLevelSoft(frame, worldPosition) < float(kGiClipMaxLevels);
}

/// Meters of contact a short ray adds below the cell the clipmap blurs at the point, valid where giClipCovers
float giContactReach(float3 worldPosition)
{
return kGiClipConeLift * giClipCellAt(gGiClipFrame[0], worldPosition);
}

/// Each shader calls the one it needs so it compiles only that path

float4 giFull(float3 worldPosition, float3 normal)
Expand Down
8 changes: 6 additions & 2 deletions engine/assets/shaders/lighting.slang
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Set 0 belongs to the engine and importing shaders own set 1
import gi_sample;
// gi_frame, not gi_sample, which would add its clipmap samplers to every layout importing this
import gi_frame;

struct DirectionalLightGpu
{
Expand Down Expand Up @@ -731,6 +732,9 @@ AreaEmitter evaluateAreaEmitter(GpuLight light, float3 position, float3 N)
return emitter;
}

/// The equal-area sphere draws a rect's highlight as a round blob, and traced specular already reflects emissive fixtures
static const float kAreaSpecularScale = 0.2;

/// Whole surface diffuse, Karis 2013 representative point specular with disc and rect as the sphere of equal area
float3 shadeAnalyticLight(GpuLight light, float3 position, float3 N, float3 V, float3 L, float3 radiance, float3 albedo, float metallic,
float roughness, float reflectivity, out float3 specularPart)
Expand Down Expand Up @@ -794,7 +798,7 @@ float3 shadeAnalyticLight(GpuLight light, float3 position, float3 N, float3 V, f
const LightTerms diffuse =
shadeLightTerms(N, V, emitter.direction, diffuseRadiance, albedo, metallic, roughness, reflectivity);
const LightTerms specular = shadeLightTerms(N, V, direction, radiance, albedo, metallic, roughness, reflectivity);
specularPart = specular.specular * normalization;
specularPart = specular.specular * normalization * kAreaSpecularScale;
return diffuse.diffuse * albedo + specularPart;
}

Expand Down
16 changes: 1 addition & 15 deletions engine/assets/shaders/scene_trace.slang
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@
import voxel_dda;
import lighting;

/// Mirrors renderer::VoxelTraceScene in the order of the TLAS custom indices
[[vk::binding(10, 1)]]
StructuredBuffer<VoxelInstance> gTraceVolumes;
/// xyz centre w radius
[[vk::binding(11, 1)]]
StructuredBuffer<float4> gTraceBounds;
/// Mirrors renderer::VoxelTraceScene::writeGrid
[[vk::binding(12, 1)]]
StructuredBuffer<uint> gTraceGrid;
/// x opaque y glass first brick of each volume in gTraceBricks z record
[[vk::binding(14, 1)]]
StructuredBuffer<uint4> gTraceBrickBases;
/// Brick of each brick BLAS primitive as x | y << 10 | z << 20
[[vk::binding(15, 1)]]
StructuredBuffer<uint> gTraceBricks;
// gTraceVolumes gTraceBounds gTraceGrid gTraceBrickBases and gTraceBricks live in voxel_dda.slang VoxelBuffers

/// Mirrors VoxelTraceScene::k_mask_volume and k_mask_glass
static const uint kTraceMaskVolume = 2u;
Expand Down
63 changes: 49 additions & 14 deletions engine/assets/shaders/voxel_dda.slang
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,57 @@ struct VoxelInstance
static const uint kInstanceNoMotion = 1u;
static const uint kInstanceMirrored = 2u;

/// Mirrors renderer::VoxelBufferAddresses, reads through these are not bounds checked
struct VoxelBuffers
{
uint* brickMaterials;
uint* brickOccupancy;
uint* volumeGrids;
uint* volumeCoarse;
uint* palettes;
VolumeRecord* volumeRecords;
VoxelInstance* voxelInstances;
/// Mirrors renderer::VoxelTraceScene in the order of the TLAS custom indices
VoxelInstance* traceVolumes;
/// xyz centre w radius
float4* traceBounds;
/// Mirrors renderer::VoxelTraceScene::writeGrid
uint* traceGrid;
/// x opaque y glass first brick of each volume in gTraceBricks z record
uint4* traceBrickBases;
/// Brick of each brick BLAS primitive as x | y << 10 | z << 20
uint* traceBricks;
};

[[vk::binding(0, 1)]]
StructuredBuffer<uint> gBrickMaterials;
[[vk::binding(1, 1)]]
StructuredBuffer<uint> gBrickOccupancy;
[[vk::binding(2, 1)]]
StructuredBuffer<uint> gVolumeGrids;
[[vk::binding(3, 1)]]
StructuredBuffer<uint> gVolumeCoarse;
ConstantBuffer<VoxelBuffers> gVoxelBuffers;

struct BrickMaterialsView { __subscript(uint i) -> uint { get { return gVoxelBuffers.brickMaterials[i]; } } };
struct BrickOccupancyView { __subscript(uint i) -> uint { get { return gVoxelBuffers.brickOccupancy[i]; } } };
struct VolumeGridsView { __subscript(uint i) -> uint { get { return gVoxelBuffers.volumeGrids[i]; } } };
struct VolumeCoarseView { __subscript(uint i) -> uint { get { return gVoxelBuffers.volumeCoarse[i]; } } };
struct PalettesView { __subscript(uint i) -> uint { get { return gVoxelBuffers.palettes[i]; } } };
struct VolumeRecordsView { __subscript(uint i) -> VolumeRecord { get { return gVoxelBuffers.volumeRecords[i]; } } };
struct VoxelInstancesView { __subscript(uint i) -> VoxelInstance { get { return gVoxelBuffers.voxelInstances[i]; } } };
struct TraceVolumesView { __subscript(uint i) -> VoxelInstance { get { return gVoxelBuffers.traceVolumes[i]; } } };
struct TraceBoundsView { __subscript(uint i) -> float4 { get { return gVoxelBuffers.traceBounds[i]; } } };
struct TraceGridView { __subscript(uint i) -> uint { get { return gVoxelBuffers.traceGrid[i]; } } };
struct TraceBrickBasesView { __subscript(uint i) -> uint4 { get { return gVoxelBuffers.traceBrickBases[i]; } } };
struct TraceBricksView { __subscript(uint i) -> uint { get { return gVoxelBuffers.traceBricks[i]; } } };

static const BrickMaterialsView gBrickMaterials = {};
static const BrickOccupancyView gBrickOccupancy = {};
static const VolumeGridsView gVolumeGrids = {};
static const VolumeCoarseView gVolumeCoarse = {};
static const uint kPaletteEntryWords = 8u;

[[vk::binding(4, 1)]]
StructuredBuffer<uint> gPalettes;
[[vk::binding(5, 1)]]
StructuredBuffer<VolumeRecord> gVolumeRecords;
[[vk::binding(6, 1)]]
StructuredBuffer<VoxelInstance> gVoxelInstances;
static const PalettesView gPalettes = {};
static const VolumeRecordsView gVolumeRecords = {};
static const VoxelInstancesView gVoxelInstances = {};
static const TraceVolumesView gTraceVolumes = {};
static const TraceBoundsView gTraceBounds = {};
static const TraceGridView gTraceGrid = {};
static const TraceBrickBasesView gTraceBrickBases = {};
static const TraceBricksView gTraceBricks = {};

static const uint kBrickDim = 8;
static const int kCoarseVoxels = 32; // gpu::k_coarse_bricks * k_brick_dim
Expand Down
2 changes: 1 addition & 1 deletion engine/assets/shaders/voxel_specular.slang
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Sampler2D gFiltered;

/// x asuint(t) y kHitShade where traceMain left a voxel hit for shadeMain
[[vk::binding(16, 2)]]
Sampler2D<uint4> gHit;
Sampler2D<uint2> gHit;

static const uint kHitShade = 1u;
/// Meters either side of the stored hit shadeMain traces again to find its voxel
Expand Down
5 changes: 3 additions & 2 deletions engine/src/toast/assets/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,9 @@ auto AssetManager::tryLoadBytes(std::string_view uri) -> std::optional<std::vect
return std::nullopt;
}

if (mounts.contains(std::string(uri.substr(0, sep)))) {
return readVirtualPath(uri);
// A miss is expected here since Slang probes for X.slang-module before X.slang, so no error log
if (const auto mount_it = mounts.find(std::string(uri.substr(0, sep))); mount_it != mounts.end()) {
return mount_it->second->read(uri.substr(sep + 3));
}

const auto real_path = resolveVirtualPath(uri);
Expand Down
Loading
Loading