Ghost collision improvements (#61)

- flag to disable hull-mesh speculative collision
- faster atomics #55 
- fix triangle vs hull logic
- fix #58 and #59
- improved shadows in samples app, a bit more expensive
This commit is contained in:
Erin Catto 2026-07-08 23:53:06 -07:00 • committed by GitHub
commit ef8ef0187a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 16639 additions and 12086 deletions

View file

@ -0,0 +1,93 @@
// SPDX-FileCopyrightText: 2026 Erin Catto
// SPDX-License-Identifier: MIT
// Cascaded shadow map PCF, shared by the lit shape shaders via
// @include. The texture and comparison sampler are passed as parameters
// so this file has no binding declarations of its own.
//
// 7x7 uniform kernel evaluated with 16 bilinear comparison taps. Tap
// positions and weights steer the hardware 2x2 compare so the fetches
// reproduce the full 49-texel kernel with triangle-filtered sub-texel
// placement, a moving shadow edge slides smoothly instead of stepping
// texel by texel. Follows the optimized PCF from The Witness (Ignacio
// Castano), via MJP's shadows sample.
//
// Manually unrolled: the HLSL compiler emits warning X3570 ("gradient
// instruction used in a loop with varying iteration") on looped texture
// calls. Unrolling keeps cross-compilation clean on the D3D11 backend.
float sampleShadowTap( texture2DArray tex, samplerShadow smp, vec2 uv, float layer, float ref_z )
{
return texture( sampler2DArrayShadow( tex, smp ), vec4( uv, layer, ref_z ) );
}
// scale multiplies the tap offsets. Cascades cover different world sizes
// per texel, the caller passes GetCascadePcfScale's texel-size ratio so
// the penumbra keeps a constant world width instead of jumping from
// crisp to blurry at every cascade boundary. 1.0 = the full 7x7 kernel.
float sampleShadowPCF( texture2DArray tex, samplerShadow smp, int cascade, vec3 light_uv, float bias, float scale )
{
// Must match SHADOW_RESOLUTION in shadow.h
const float size = 4096.0;
const float inv_size = 1.0 / size;
float ly = float( cascade );
float rz = light_uv.z - bias;
// Texel-space position split into the nearest texel corner and the
// sub-texel fraction that drives the tap weights.
vec2 tuv = light_uv.xy * size;
vec2 base = floor( tuv + 0.5 );
float s = tuv.x + 0.5 - base.x;
float t = tuv.y + 0.5 - base.y;
base = ( base - 0.5 ) * inv_size;
// Shrinking the offsets bunches the 16 bilinear taps inside a smaller
// footprint. The box property degrades gracefully, at the 0.25 floor
// the taps still span ~2 texels and overlap through the hardware 2x2
// compare, so the response stays smooth.
float step = inv_size * scale;
float uw0 = 5.0 * s - 6.0;
float uw1 = 11.0 * s - 28.0;
float uw2 = -( 11.0 * s + 17.0 );
float uw3 = -( 5.0 * s + 1.0 );
float u0 = ( 4.0 * s - 5.0 ) / uw0 - 3.0;
float u1 = ( 4.0 * s - 16.0 ) / uw1 - 1.0;
float u2 = -( 7.0 * s + 5.0 ) / uw2 + 1.0;
float u3 = -s / uw3 + 3.0;
float vw0 = 5.0 * t - 6.0;
float vw1 = 11.0 * t - 28.0;
float vw2 = -( 11.0 * t + 17.0 );
float vw3 = -( 5.0 * t + 1.0 );
float v0 = ( 4.0 * t - 5.0 ) / vw0 - 3.0;
float v1 = ( 4.0 * t - 16.0 ) / vw1 - 1.0;
float v2 = -( 7.0 * t + 5.0 ) / vw2 + 1.0;
float v3 = -t / vw3 + 3.0;
float sum = 0.0;
sum += uw0 * vw0 * sampleShadowTap( tex, smp, base + vec2( u0, v0 ) * step, ly, rz );
sum += uw1 * vw0 * sampleShadowTap( tex, smp, base + vec2( u1, v0 ) * step, ly, rz );
sum += uw2 * vw0 * sampleShadowTap( tex, smp, base + vec2( u2, v0 ) * step, ly, rz );
sum += uw3 * vw0 * sampleShadowTap( tex, smp, base + vec2( u3, v0 ) * step, ly, rz );
sum += uw0 * vw1 * sampleShadowTap( tex, smp, base + vec2( u0, v1 ) * step, ly, rz );
sum += uw1 * vw1 * sampleShadowTap( tex, smp, base + vec2( u1, v1 ) * step, ly, rz );
sum += uw2 * vw1 * sampleShadowTap( tex, smp, base + vec2( u2, v1 ) * step, ly, rz );
sum += uw3 * vw1 * sampleShadowTap( tex, smp, base + vec2( u3, v1 ) * step, ly, rz );
sum += uw0 * vw2 * sampleShadowTap( tex, smp, base + vec2( u0, v2 ) * step, ly, rz );
sum += uw1 * vw2 * sampleShadowTap( tex, smp, base + vec2( u1, v2 ) * step, ly, rz );
sum += uw2 * vw2 * sampleShadowTap( tex, smp, base + vec2( u2, v2 ) * step, ly, rz );
sum += uw3 * vw2 * sampleShadowTap( tex, smp, base + vec2( u3, v2 ) * step, ly, rz );
sum += uw0 * vw3 * sampleShadowTap( tex, smp, base + vec2( u0, v3 ) * step, ly, rz );
sum += uw1 * vw3 * sampleShadowTap( tex, smp, base + vec2( u1, v3 ) * step, ly, rz );
sum += uw2 * vw3 * sampleShadowTap( tex, smp, base + vec2( u2, v3 ) * step, ly, rz );
sum += uw3 * vw3 * sampleShadowTap( tex, smp, base + vec2( u3, v3 ) * step, ly, rz );
return sum / 2704.0;
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,7 @@
Generated by sokol-shdc (https://github.com/floooh/sokol-tools)
Cmdline:
sokol-shdc --input ibl/sky.glsl --output generated/sky.glsl.h --slang hlsl5:metal_macos:glsl430
sokol-shdc --input E:/box/box3d/samples/shaders/ibl/sky.glsl --output E:/box/box3d/samples/shaders/generated/sky.glsl.h --slang hlsl5:metal_macos:glsl430
Overview:
=========

View file

@ -5,7 +5,7 @@
Generated by sokol-shdc (https://github.com/floooh/sokol-tools)
Cmdline:
sokol-shdc --input ibl/sky_to_cube.glsl --output generated/sky_to_cube.glsl.h --slang hlsl5:metal_macos:glsl430
sokol-shdc --input E:/box/box3d/samples/shaders/ibl/sky_to_cube.glsl --output E:/box/box3d/samples/shaders/generated/sky_to_cube.glsl.h --slang hlsl5:metal_macos:glsl430
Overview:
=========

File diff suppressed because it is too large Load diff

View file

@ -142,6 +142,7 @@ layout( binding = 1 ) uniform ub_pass
mat4 view; // for view-space depth debug mode + cascade selection
vec4 cascade_far_view_z; // .xyz = far view-space Z per cascade
mat4 cascade_matrices[3]; // light-space view-proj per cascade
vec4 cascade_pcf_scale; // .xyz = PCF tap-offset scale per cascade (constant world penumbra)
vec4 sh[9]; // IBL diffuse SH coefficients (band 0..2, RGB in .xyz)
vec4 ibl_params; // .x = prefilter cube max_lod, .y = IBL enable (1=IBL, 0=flat ambient), .zw reserved
};
@ -168,26 +169,7 @@ layout( binding = 4 ) uniform texture2D tex_ao;
#pragma sokol @sampler_type smp_ao filtering
layout( binding = 3 ) uniform sampler smp_ao;
// 3x3 PCF over a 1-texel radius. Manually unrolled, see cube.glsl for
// the X3570 rationale.
float sampleShadowPCF( int cascade, vec3 light_uv, float bias )
{
float t = 1.0 / 2048.0;
float ly = float( cascade );
float rz = light_uv.z - bias;
vec2 c = light_uv.xy;
float s = 0.0;
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y + t, ly, rz ) );
return s / 9.0;
}
#pragma sokol @include ../common/shadow_pcf.glsl
float sampleCascade( int cascade, vec3 world_pos, vec3 world_normal )
{
@ -205,7 +187,7 @@ float sampleCascade( int cascade, vec3 world_pos, vec3 world_normal )
float n_dot_l = max( dot( world_normal, normalize( sun_dir_world.xyz ) ), 0.0 );
float bias = mix( 0.0040, 0.0008, n_dot_l );
float pcf = sampleShadowPCF( cascade, light_uv, bias );
float pcf = sampleShadowPCF( tex_shadow, smp_shadow, cascade, light_uv, bias, cascade_pcf_scale[cascade] );
// See cube.glsl for the longer note. Cascade 2 has no fallback at its
// UV boundary, so fade PCF toward 1.0 over the outer 10% to hide the

View file

@ -109,6 +109,7 @@ layout( binding = 1 ) uniform ub_pass
ivec4 flags; // .x = debug_view_mode
vec4 cascade_far_view_z; // .xyz = far view-space Z per cascade
mat4 cascade_matrices[3]; // light-space view-proj per cascade
vec4 cascade_pcf_scale; // .xyz = PCF tap-offset scale per cascade (constant world penumbra)
mat4 view; // IBL: world->view rotation for view->world n_view transform
vec4 camera_pos_world; // IBL: world camera pos for V_world derivation
vec4 sh[9]; // IBL diffuse SH coefficients (band 0..2, RGB in .xyz)
@ -150,31 +151,9 @@ flat in vec4 v_base_color;
flat in vec4 v_material;
out vec4 out_color;
// 3x3 PCF over a 1-texel radius. The comparison sampler returns 0..1
// per tap (0 = occluded, 1 = lit). Uniform 1/9 weighting.
//
// Manually unrolled: the HLSL compiler emits warning X3570 ("gradient
// instruction used in a loop with varying iteration") on the looped
// version because texture uses implicit derivatives. Unrolling here
// keeps cross-compilation clean on the D3D11 backend.
float sampleShadowPCF( int cascade, vec3 light_uv, float bias )
{
float t = 1.0 / 2048.0;
float ly = float( cascade );
float rz = light_uv.z - bias;
vec2 c = light_uv.xy;
float s = 0.0;
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y + t, ly, rz ) );
return s / 9.0;
}
// The comparison sampler returns 0..1 per tap (0 = occluded, 1 = lit).
// Kernel details and the X3570 unrolling note live in the include.
#pragma sokol @include ../common/shadow_pcf.glsl
float sampleCascade( int cascade, vec3 world_pos, float n_dot_l )
{
@ -205,12 +184,12 @@ float sampleCascade( int cascade, vec3 world_pos, float n_dot_l )
float texelDepthStep = 0.001;
float bias = clamp( texelDepthStep * tanAngle, 0.0005, 0.05 );
float pcf = sampleShadowPCF( cascade, light_uv, bias );
float pcf = sampleShadowPCF( tex_shadow, smp_shadow, cascade, light_uv, bias, cascade_pcf_scale[cascade] );
// Cascade 2 is the last cascade, sampleShadowCascaded's view-z blend
// has nothing to blend into beyond it. On flat ground at glancing sun
// angles PCF inside the cascade reads slightly below 1.0 (bias slop
// across a 3x3 kernel that covers ~5 cm of world per texel here), while
// across a kernel that covers ~5 cm of world per texel here), while
// fragments outside the cascade's spatial sphere fall through to the
// OOB return of 1.0. Without smoothing, cascade 2's UV boundary reads
// as a hexagonal polygon on the ground. Fade PCF toward 1.0 over the

View file

@ -112,6 +112,7 @@ layout( binding = 1 ) uniform ub_pass
mat4 view; // for view-space depth debug mode + cascade selection
vec4 cascade_far_view_z; // .xyz = far view-space Z per cascade
mat4 cascade_matrices[3]; // light-space view-proj per cascade
vec4 cascade_pcf_scale; // .xyz = PCF tap-offset scale per cascade (constant world penumbra)
vec4 sh[9]; // IBL diffuse SH coefficients (band 0..2, RGB in .xyz)
vec4 ibl_params; // .x = prefilter cube max_lod, .y = IBL enable (1=IBL, 0=flat ambient), .zw reserved
};
@ -138,26 +139,7 @@ layout( binding = 4 ) uniform texture2D tex_ao;
#pragma sokol @sampler_type smp_ao filtering
layout( binding = 3 ) uniform sampler smp_ao;
// 3x3 PCF over a 1-texel radius. Manually unrolled, see cube.glsl for
// the X3570 rationale.
float sampleShadowPCF( int cascade, vec3 light_uv, float bias )
{
float t = 1.0 / 2048.0;
float ly = float( cascade );
float rz = light_uv.z - bias;
vec2 c = light_uv.xy;
float s = 0.0;
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y + t, ly, rz ) );
return s / 9.0;
}
#pragma sokol @include ../common/shadow_pcf.glsl
float sampleCascade( int cascade, vec3 world_pos, vec3 world_normal )
{
@ -175,7 +157,7 @@ float sampleCascade( int cascade, vec3 world_pos, vec3 world_normal )
float n_dot_l = max( dot( world_normal, normalize( sun_dir_world.xyz ) ), 0.0 );
float bias = mix( 0.0040, 0.0008, n_dot_l );
float pcf = sampleShadowPCF( cascade, light_uv, bias );
float pcf = sampleShadowPCF( tex_shadow, smp_shadow, cascade, light_uv, bias, cascade_pcf_scale[cascade] );
// See cube.glsl for the longer note. Cascade 2 has no fallback at its
// UV boundary, so fade PCF toward 1.0 over the outer 10% to hide the

View file

@ -2,8 +2,9 @@
// SPDX-License-Identifier: MIT
// Shadow caster for capsules. As with the sphere caster, we substitute
// a low-poly proxy mesh for the analytic impostor. The proxy is a
// 12-slice cylinder with two hemispherical caps. Vertices are authored in
// a proxy mesh for the analytic impostor. The proxy is a lat/long cylinder
// with two hemispherical caps (see BuildCapsuleProxy in renderer.c for
// counts and the silhouette-straddling scale). Vertices are authored in
// a "unit-capsule" coordinate frame where each component carries enough
// information to scale independently by halfLength and radius.
//

View file

@ -139,6 +139,7 @@ layout( binding = 1 ) uniform ub_pass
mat4 view; // for view-space depth debug mode + cascade selection
vec4 cascade_far_view_z; // .xyz = far view-space Z per cascade
mat4 cascade_matrices[3]; // light-space view-proj per cascade
vec4 cascade_pcf_scale; // .xyz = PCF tap-offset scale per cascade (constant world penumbra)
vec4 sh[9]; // IBL diffuse SH coefficients (band 0..2, RGB in .xyz)
vec4 ibl_params; // .x = prefilter cube max_lod, .y = IBL enable (1=IBL, 0=flat ambient), .zw reserved
};
@ -165,26 +166,7 @@ layout( binding = 4 ) uniform texture2D tex_ao;
#pragma sokol @sampler_type smp_ao filtering
layout( binding = 3 ) uniform sampler smp_ao;
// 3x3 PCF over a 1-texel radius. Manually unrolled, see cube.glsl for
// the X3570 rationale.
float sampleShadowPCF( int cascade, vec3 light_uv, float bias )
{
float t = 1.0 / 2048.0;
float ly = float( cascade );
float rz = light_uv.z - bias;
vec2 c = light_uv.xy;
float s = 0.0;
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y - t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x - t, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x, c.y + t, ly, rz ) );
s += texture( sampler2DArrayShadow( tex_shadow, smp_shadow ), vec4( c.x + t, c.y + t, ly, rz ) );
return s / 9.0;
}
#pragma sokol @include ../common/shadow_pcf.glsl
float sampleCascade( int cascade, vec3 world_pos, vec3 world_normal )
{
@ -202,7 +184,7 @@ float sampleCascade( int cascade, vec3 world_pos, vec3 world_normal )
float n_dot_l = max( dot( world_normal, normalize( sun_dir_world.xyz ) ), 0.0 );
float bias = mix( 0.0040, 0.0008, n_dot_l );
float pcf = sampleShadowPCF( cascade, light_uv, bias );
float pcf = sampleShadowPCF( tex_shadow, smp_shadow, cascade, light_uv, bias, cascade_pcf_scale[cascade] );
// See cube.glsl for the longer note. Cascade 2 has no fallback at its
// UV boundary, so fade PCF toward 1.0 over the outer 10% to hide the