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
Binary file added editor/Resources/splash_images/Toasty1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added editor/Resources/splash_images/Toasty2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added editor/Resources/splash_images/Toasty3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions engine/assets/shaders/bloom.slang
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ struct BloomParams

float strength;

float2 _pad0;
/// Scale on the light behind glass so emissives the glass dims still bloom
float glassScale;

float _pad0;
};

[[vk::push_constant]]
Expand All @@ -30,10 +33,12 @@ Sampler2D gGlassBackdrop;
[[vk::binding(3,0)]]
Sampler2D gGlassMask;

/// Where glass covers a pixel the brighter of the glass composite and the scaled light behind it
float3 bloomSource(float2 uv)
{
float m = gGlassMask.Sample(uv).r;
return lerp(gSource.Sample(uv).rgb, gGlassBackdrop.Sample(uv).rgb, m);
const float3 source = gSource.Sample(uv).rgb;
const float m = gGlassMask.Sample(uv).r;
return lerp(source, max(source, gGlassBackdrop.Sample(uv).rgb * gParams.glassScale), m);
}

typedef FullscreenVSOutput VSOutput;
Expand Down
59 changes: 51 additions & 8 deletions engine/assets/shaders/debris.slang
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
struct Params {
float3 gravity;
float dt;
float fadeIn;
float restitution;
float friction;
float voxelSize;
Expand All @@ -19,6 +18,7 @@ struct Params {
uint pad0;
uint pad1;
uint pad2;
uint pad3;
};

[[vk::push_constant]]
Expand All @@ -34,7 +34,8 @@ struct DebrisParticle {
uint albedo;
float scale;
uint glass;
uint pad0;
/// Palette emissive intensity
float emissive;
uint pad1;
};

Expand All @@ -58,7 +59,7 @@ struct DebrisSpawn {
float lifetime;
float scale;
uint glass;
uint pad0;
float emissive;
};

struct DebrisVolume {
Expand Down Expand Up @@ -90,6 +91,9 @@ StructuredBuffer<DebrisVolume> gVolumes;
StructuredBuffer<uint> gRetires;
[[vk::binding(8, 0)]]
RWStructuredBuffer<uint> gGrid;
/// Opaque caster box min xyz and max xyz as ordered uints then changing pieces then the fastest speed bits
[[vk::binding(9, 0)]]
RWStructuredBuffer<uint> gShadowBounds;

struct VolumeRecord {
uint brickDimsX;
Expand Down Expand Up @@ -125,7 +129,6 @@ static const uint kTagUniform = 1;
static const uint kMaskBits = 0xFFu;
static const uint kFlagAsleep = 1u << 24;
static const uint kFlagGlass = 1u << 25;
static const float kSpawnScale = 0.4; // grows from this share of its size

static const float kSleepSpeed = 0.2; // below this speed a piece may sleep
static const uint kSleepSteps = 15;
Expand Down Expand Up @@ -239,11 +242,10 @@ float pieceRadius(uint mask, float scale) {
return gParams.voxelSize * scale * lerp(0.6, 1.0, float(countbits(mask & kMaskBits)) / 8.0);
}

// Grows in after spawning and shrinks away before dying
// Shrinks away before dying
float displayScale(DebrisMotion motion) {
float grow = lerp(kSpawnScale, 1.0, saturate(motion.age / max(gParams.fadeIn, 1e-4)));
float shrink = smoothstep(0.0, max(gParams.fadeOut, 1e-4), motion.lifetime - motion.age);
return motion.baseScale * grow * shrink;
return motion.baseScale * shrink;
}

struct Contact {
Expand Down Expand Up @@ -361,9 +363,46 @@ void appendVoxels(uint index, uint mask, bool glass) {
}
}

/// Sorts like the float it came from
uint orderedFloat(float value) {
uint bits = asuint(value);
return (bits & 0x80000000u) != 0u ? ~bits : bits | 0x80000000u;
}

/// Feeds the shadow pass cull and cache, changing means its shadow moves this step
void addShadowCaster(DebrisParticle particle, DebrisMotion motion, bool changing) {
if ((particle.packed & kFlagGlass) != 0u) {
return;
}
// Two voxel wide box around the center at any rotation
const float radius = 1.74 * gParams.voxelSize * motion.baseScale;
const float3 low = particle.position - radius;
const float3 high = particle.position + radius;
InterlockedMin(gShadowBounds[0], orderedFloat(low.x));
InterlockedMin(gShadowBounds[1], orderedFloat(low.y));
InterlockedMin(gShadowBounds[2], orderedFloat(low.z));
InterlockedMax(gShadowBounds[3], orderedFloat(high.x));
InterlockedMax(gShadowBounds[4], orderedFloat(high.y));
InterlockedMax(gShadowBounds[5], orderedFloat(high.z));
const bool fading = motion.age > motion.lifetime - gParams.fadeOut;
if (changing || fading) {
InterlockedAdd(gShadowBounds[6], 1u);
}
// Positive floats sort as uints
InterlockedMax(gShadowBounds[7], asuint(length(particle.velocity)));
}

[shader("compute")]
[numthreads(1, 1, 1)]
void resetMain(uint3 id : SV_DispatchThreadID) {
gShadowBounds[0] = 0xFFFFFFFFu;
gShadowBounds[1] = 0xFFFFFFFFu;
gShadowBounds[2] = 0xFFFFFFFFu;
gShadowBounds[3] = 0u;
gShadowBounds[4] = 0u;
gShadowBounds[5] = 0u;
gShadowBounds[6] = 0u;
gShadowBounds[7] = 0u;
gDrawArgs[0] = 36u;
gDrawArgs[1] = 0u;
gDrawArgs[2] = 0u;
Expand Down Expand Up @@ -544,6 +583,7 @@ void integrateMain(uint3 id : SV_DispatchThreadID, uint3 groupThread : SV_GroupT
gCurrent[i] = particle;
gMotion[i] = motion;
appendVoxels(i, mask, glass);
addShadowCaster(particle, motion, false);
return;
}
particle.packed &= ~kFlagAsleep;
Expand Down Expand Up @@ -695,6 +735,8 @@ void integrateMain(uint3 id : SV_DispatchThreadID, uint3 groupThread : SV_GroupT
gCurrent[i] = particle;
gMotion[i] = motion;
appendVoxels(i, mask, glass);
// Falling asleep this step still moved it
addShadowCaster(particle, motion, true);
}

[shader("compute")]
Expand All @@ -717,7 +759,7 @@ void spawnMain(uint3 id : SV_DispatchThreadID) {
particle.velocity = spawn.velocity;
particle.albedo = spawn.albedo;
particle.glass = spawn.glass;
particle.pad0 = 0u;
particle.emissive = spawn.emissive;
particle.pad1 = 0u;

DebrisMotion motion;
Expand All @@ -733,4 +775,5 @@ void spawnMain(uint3 id : SV_DispatchThreadID) {
gCurrent[spawn.slot] = particle;

appendVoxels(spawn.slot, spawn.packed & kMaskBits, (spawn.packed & kFlagGlass) != 0u);
addShadowCaster(particle, motion, true);
}
36 changes: 32 additions & 4 deletions engine/assets/shaders/debris_draw.slang
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import voxel_gbuffer;
import lighting;
import gi_sample;
import glass_common;
import glass_reflection;

struct PushConstants {
float extrapolation;
float frameDelta;
float voxelSize;
uint listCapacity;
/// Screen march reach in meters and 0 leaves glass with the environment
float reflectionReach;
uint reflectionSteps;
float reflectionThickness;
float reflectionRoughness;
};

[[vk::push_constant]]
Expand All @@ -23,7 +29,8 @@ struct DebrisParticle {
uint albedo;
float scale;
uint glass;
uint pad0;
/// Palette emissive intensity
float emissive;
uint pad1;
};

Expand All @@ -32,6 +39,12 @@ StructuredBuffer<DebrisParticle> gParticles;
[[vk::binding(1, 1)]]
StructuredBuffer<uint> gDrawList;

/// Opaque scene colour and depth before anything blended, only read while reflectionReach is above 0
[[vk::binding(0, 2)]]
Sampler2D gBackdrop;
[[vk::binding(1, 2)]]
Sampler2D gBackdropDepth;

static const float3 kDebrisBoxCorners[8] = {
float3(0.0, 0.0, 0.0), float3(1.0, 0.0, 0.0), float3(1.0, 1.0, 0.0), float3(0.0, 1.0, 0.0),
float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0), float3(1.0, 1.0, 1.0), float3(0.0, 1.0, 1.0),
Expand Down Expand Up @@ -84,6 +97,9 @@ struct VSOutput {

[[vk::location(9)]]
nointerpolation float scale : TEXCOORD9;

[[vk::location(10)]]
nointerpolation float emissive : TEXCOORD10;
};

float3 debrisRotate(float4 q, float3 v) {
Expand All @@ -108,6 +124,7 @@ VSOutput emptyVertex() {
output.cameraLocal = float3(0.0, 0.0, 0.0);
output.cellMin = float3(0.0, 0.0, 0.0);
output.scale = 0.0;
output.emissive = 0.0;
return output;
}

Expand Down Expand Up @@ -140,6 +157,7 @@ VSOutput debrisVertex(uint entry, uint vertexId) {
output.cameraLocal = debrisRotate(inverse, gCamera.position - center) / max(worldScale, 1e-6);
output.cellMin = cell - 1.0;
output.scale = particle.scale;
output.emissive = particle.emissive;
return output;
}

Expand Down Expand Up @@ -188,7 +206,7 @@ FSOutput fragmentMain(VSOutput input) {
output.albedo = float4(debrisAlbedo(input.albedo), kGbufferLit);
output.faceNormal = encodeFaceNormal(normalize(input.normal));
output.detailNormal = output.faceNormal;
output.material = float4(metallic, reflectivity, debrisRoughness(input.albedo), encodeEmissive(0.0));
output.material = float4(metallic, reflectivity, debrisRoughness(input.albedo), encodeEmissive(input.emissive));
output.motion = float4((clip.xy / clip.w - previousClip.xy / previousClip.w) * 0.5, 0.0, 1.0);
return output;
}
Expand Down Expand Up @@ -237,9 +255,19 @@ GlassShade shadeDebrisGlass(VSOutput input) {
const float scatter = glassScatter(alpha, roughness);

const float3 diffuse = max(lit.color - lit.specular, float3(0.0, 0.0, 0.0));
float3 litSpecular = lit.specular;
float3 reflection;
// shortcut: mirror ray only so rough pieces below the cutoff reflect too sharp, upgrade to a GGX lobe sample with TAA
// Pieces past the reach are a few pixels and would only march to miss
if (gPush.reflectionReach > 0.0 && roughness <= gPush.reflectionRoughness &&
distance(gCamera.position, input.worldPosition) <= gPush.reflectionReach &&
glassScreenReflection(gBackdrop, gBackdropDepth, input.worldPosition, reflect(-view, normal), gPush.reflectionReach,
gPush.reflectionSteps, gPush.reflectionThickness, reflection)) {
litSpecular = glassReflectionSpecular(lit.specular, lit.ambientSpecular, reflection, normal, view, roughness);
}
// Lighting assumes F0 0.04 so rescale by the Fresnel ratio
const float3 specular = lit.specular * fresnel / (0.04 + 0.96 * facing);
shade.color = diffuse * scatter + specular;
const float3 specular = litSpecular * fresnel / (0.04 + 0.96 * facing);
shade.color = diffuse * scatter + albedo * input.emissive * alpha + specular;

// Front faces only so the exit face is the far slab of this voxel
const float3 direction = normalize(input.boxLocal - input.cameraLocal);
Expand Down
66 changes: 66 additions & 0 deletions engine/assets/shaders/debris_shadow.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Opaque debris voxels into the shadow maps as moving casters
import shadow_views;

/// Mirrors renderer::ShadowPass::DebrisShadowPush
struct PushConstants {
float extrapolation;
float voxelSize;
uint listCapacity;
uint viewIndex;
};

[[vk::push_constant]]
PushConstants gPush;

// Mirrors physics::DebrisSimulation::GpuParticle and the set 1 of debris_draw.slang so DebrisPass sets bind here
struct DebrisParticle {
float3 position;
uint packed;
float4 rotation;
float3 velocity;
uint albedo;
float scale;
uint glass;
float emissive;
uint pad1;
};

[[vk::binding(0, 1)]]
StructuredBuffer<DebrisParticle> gParticles;
[[vk::binding(1, 1)]]
StructuredBuffer<uint> gDrawList;

static const float3 kDebrisBoxCorners[8] = {
float3(0.0, 0.0, 0.0), float3(1.0, 0.0, 0.0), float3(1.0, 1.0, 0.0), float3(0.0, 1.0, 0.0),
float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0), float3(1.0, 1.0, 1.0), float3(0.0, 1.0, 1.0),
};

static const uint kDebrisBoxIndices[36] = {
0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 1, 5, 0, 5, 4,
3, 7, 6, 3, 6, 2, 0, 4, 7, 0, 7, 3, 1, 2, 6, 1, 6, 5,
};

float3 debrisRotate(float4 q, float3 v) {
const float3 t = 2.0 * cross(q.xyz, v);
return v + q.w * t + cross(q.xyz, t);
}

[shader("vertex")]
float4 vertexMain(uint vertexId : SV_VertexID, uint instanceId : SV_InstanceID, uint viewId : SV_ViewID) : SV_Position {
if (instanceId >= gPush.listCapacity) {
return float4(0.0, 0.0, 0.0, 1.0);
}
const uint entry = gDrawList[instanceId];
const DebrisParticle particle = gParticles[entry >> 3];
const uint bit = entry & 7u;
if (((particle.packed >> bit) & 1u) == 0u || particle.scale <= 0.0) {
return float4(0.0, 0.0, 0.0, 1.0);
}

// Same box as debrisVertex in debris_draw.slang
const float3 cell = float3(float(bit & 1u), float((bit >> 1) & 1u), float(bit >> 2));
const float3 boxLocal = cell - 1.0 + kDebrisBoxCorners[kDebrisBoxIndices[vertexId]];
const float3 center = particle.position + particle.velocity * gPush.extrapolation;
const float3 world = center + debrisRotate(particle.rotation, boxLocal * (gPush.voxelSize * particle.scale));
return mul(gShadow.viewProjection[gPush.viewIndex + viewId], float4(world, 1.0));
}
45 changes: 44 additions & 1 deletion engine/assets/shaders/gi_clip_fill.slang
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,49 @@ void clipSample(uint v, float3 world, float gap, uint index, inout ClipCell cell
cell.emission += palette.albedo * palette.emissive;
}

/// False when no solid coarse block of the volume touches the cell so all its samples would miss
bool clipCellReaches(uint v, float3 low, float cellSize)
{
const VoxelInstance volume = gTraceVolumes[v];
const VolumeRecord record = gVolumeRecords[volume.recordIndex];
float3 lo = float3(kFloatMax, kFloatMax, kFloatMax);
float3 hi = -lo;
for (uint corner = 0u; corner < 8u; ++corner)
{
const float3 world = low + float3(corner & 1u, (corner >> 1) & 1u, corner >> 2) * cellSize;
const float3 voxel = mul(volume.worldToVoxel, float4(world, 1.0)).xyz;
lo = min(lo, voxel);
hi = max(hi, voxel);
}
const int3 dims = int3(record.brickDimsX, record.brickDimsY, record.brickDimsZ) * int(kBrickDim);
const int3 first = max(int3(floor(lo)), int3(0, 0, 0)) >> 5;
const int3 last = min(int3(floor(hi)), dims - 1) >> 5;
if (any(first > last))
{
return false;
}
// A cell spanning many blocks is cheaper to just sample
const int3 count = last - first + 1;
if (count.x * count.y * count.z > 8)
{
return true;
}
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)
{
if (coarseSolid(record, int3(x, y, z)))
{
return true;
}
}
}
}
return false;
}

/// 16 bits of one z layer at x + 4 y
uint clipLayer(ClipCell cell, uint z)
{
Expand Down Expand Up @@ -169,7 +212,7 @@ void fillMain(uint3 id : SV_DispatchThreadID)
{
const uint v = gTraceGrid[farBase + f];
const float4 bounds = gTraceBounds[v];
if (distance(centre, bounds.xyz) > bounds.w + cellSize * 0.8661)
if (distance(centre, bounds.xyz) > bounds.w + cellSize * 0.8661 || !clipCellReaches(v, low, cellSize))
{
continue;
}
Expand Down
Loading
Loading