diff --git a/engine/rend/shaders/lit_mesh.frag.hlsl b/engine/rend/shaders/lit_mesh.frag.hlsl index a97b9e1..9c4f539 100644 --- a/engine/rend/shaders/lit_mesh.frag.hlsl +++ b/engine/rend/shaders/lit_mesh.frag.hlsl @@ -81,6 +81,7 @@ float shadowCalculation(float3 fragPos, float4 DirectionalShadowFragPos) float2 uv = shadowProjection.xy; uv.y = -uv.y; float depthSample = DirectionalShadowDepthMap.Sample(DirectionalShadowSampler, uv); + float fragDepth = DirectionalShadowFragPos.z; float bias = 0.005; @@ -89,6 +90,49 @@ float shadowCalculation(float3 fragPos, float4 DirectionalShadowFragPos) return shadow; } +float shadowCalculationPCF(float3 fragPos, float4 DirectionalShadowFragPos) +{ + // size = 2048 x 2028 + float2 texDim = float2(2048, 2048); + + float scale = 0.5; + float dx = scale * 1.0 / float(texDim.x); + float dy = scale * 1.0 / float(texDim.y); + + float combinedDepth = 0.0; + int count = 0; + int range = 4; + + float3 shadowProjection = DirectionalShadowFragPos.xyz / DirectionalShadowFragPos.w; + shadowProjection = shadowProjection * 0.5 + 0.5 ; + shadowProjection.x = shadowProjection.x; + + float2 uv = shadowProjection.xy; + uv.y = -uv.y; + float fragDepth = DirectionalShadowFragPos.z; + float combinedShadow = 0.0; + float bias = 0.001; + + for (int y = -range ; y <= range ; y++) { + for (int x = -range ; x <= range ; x++) { + float2 Offsets = float2(x * dx, y * dy); + //float3 UVC = float3(uv + Offsets, DirectionalShadowFragPos.z); + float depth = DirectionalShadowDepthMap.Sample(DirectionalShadowSampler, uv + Offsets); + combinedDepth += depth; + combinedShadow += fragDepth - bias > depth ? 1.0 : 0.0; + count += 1; + } + } + + combinedDepth = combinedDepth / count; + combinedShadow = combinedShadow / count; + + // float fragDepth = DirectionalShadowFragPos.z; + // float shadow = fragDepth - bias > combinedDepth ? 1.0 : 0.0; + + return combinedShadow; +} + float3 DirectionalLight(float3 normal, float3 fragPos, float4 DirectionalShadowFragPos) { @@ -106,7 +150,7 @@ float3 DirectionalLight(float3 normal, float3 fragPos, float4 DirectionalShadow // shadow mapping - diffuse = diffuse * (1.0 - shadowCalculation(fragPos, DirectionalShadowFragPos)); + diffuse = diffuse * (1.0 - shadowCalculationPCF(fragPos, DirectionalShadowFragPos)); return diffuse; } @@ -152,9 +196,10 @@ float4 main( // float depthSample = DirectionalShadowDepthMap.Sample(DirectionalShadowSampler, uv); // float fragDepth = shadowProjection.z; - // float t = DirectionalShadowFragPos.z; - // t = depthSample; + // float t = shadowCalculationPCF(WorldPos, DirectionalShadowFragPos); + + // float t = depthSample; // rv = float4(t, t, t, 1.0); // rv.x = rv.x + lightPos.x * 0.0001; diff --git a/engine/rend/src/sgpu/renderer.zig b/engine/rend/src/sgpu/renderer.zig index bc93b8f..a2477ae 100644 --- a/engine/rend/src/sgpu/renderer.zig +++ b/engine/rend/src/sgpu/renderer.zig @@ -594,7 +594,7 @@ pub const Renderer = struct { // can cache this const targetInfo: gpu.GPUColorTargetInfo = std.mem.zeroInit(gpu.GPUColorTargetInfo, .{ .texture = self.swapchainTexture.?, - .clear_color = .{ .r = 0.1, .g = 0.1, .b = 0.1, .a = 1.0 }, + .clear_color = .{ .r = 71.0 / 256.0, .g = 200.0 / 256.0, .b = 1.0, .a = 1.0 }, .load_op = .loadopClear, .store_op = .storeopStore, }); diff --git a/projects/content/_shaders/dxil/lit_mesh.frag.dxil b/projects/content/_shaders/dxil/lit_mesh.frag.dxil index d7893de..66c8b39 100644 Binary files a/projects/content/_shaders/dxil/lit_mesh.frag.dxil and b/projects/content/_shaders/dxil/lit_mesh.frag.dxil differ diff --git a/projects/content/_shaders/msl/lit_mesh.frag.msl b/projects/content/_shaders/msl/lit_mesh.frag.msl index 5c4c56f..74ddcd7 100644 --- a/projects/content/_shaders/msl/lit_mesh.frag.msl +++ b/projects/content/_shaders/msl/lit_mesh.frag.msl @@ -13,7 +13,7 @@ struct type_Uniforms float time; }; -constant float3 _56 = {}; +constant float3 _59 = {}; struct main0_out { @@ -31,53 +31,73 @@ struct main0_in fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], texture2d Texture [[texture(0)]], texture2d DirectionalShadowDepthMap [[texture(1)]], sampler Sampler [[sampler(0)]], sampler DirectionalShadowSampler [[sampler(1)]]) { main0_out out = {}; - float4 _65 = Texture.sample(Sampler, in.in_var_TEXCOORD0); - float _66 = _65.w; - if (_66 < 0.00999999977648258209228515625) + float4 _68 = Texture.sample(Sampler, in.in_var_TEXCOORD0); + float _69 = _68.w; + if (_69 < 0.00999999977648258209228515625) { discard_fragment(); } - float3 _119; + float3 _122; do { - float3 _76 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1; - float _77 = length(_76); - float _78 = _77 * _77; - float _84; - if (_77 > 2.0) + float3 _79 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1; + float _80 = length(_79); + float _81 = _80 * _80; + float _87; + if (_80 > 2.0) { - _84 = 0.5 / _78; + _87 = 0.5 / _81; } else { - _84 = 1.0 / _78; + _87 = 1.0 / _81; } - float _89; - if (_77 > 4.0) + float _92; + if (_80 > 4.0) { - _89 = _84 * 0.5; + _92 = _87 * 0.5; } else { - _89 = _84; + _92 = _87; } - float _91 = (_77 > 15.0) ? 0.0 : _89; - float _93 = (_91 > 1.0) ? 1.0 : _91; + float _94 = (_80 > 15.0) ? 0.0 : _92; + float _96 = (_94 > 1.0) ? 1.0 : _94; if (length(in.in_var_TEXCOORD2) < 0.100000001490116119384765625) { - _119 = (in.in_var_TEXCOORD1 * float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5)) * _93; + _122 = (in.in_var_TEXCOORD1 * float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5)) * _96; break; } - float3 _100 = fast::normalize(_76); - _119 = ((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * precise::max(dot(_100, in.in_var_TEXCOORD2), 0.0)) * _93) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_100 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _93); + float3 _103 = fast::normalize(_79); + _122 = ((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * precise::max(dot(_103, in.in_var_TEXCOORD2), 0.0)) * _96) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_103 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _96); break; } while(false); - float3 _136 = ((in.in_var_TEXCOORD3.xyz / float3(in.in_var_TEXCOORD3.w)) * 0.5) + float3(0.5); - float3 _138; - _138.x = _136.x; - float2 _139 = _138.xy; - _139.y = -_136.y; - out.out_var_SV_Target0 = float4(powr(_65.xyz * ((float3(0.100000001490116119384765625) + _119) + ((Uniforms.directionalLightColor.xyz * precise::max(dot(Uniforms.directionalLight.xyz, in.in_var_TEXCOORD2), 0.0)) * (1.0 - float((in.in_var_TEXCOORD3.z - 0.004999999888241291046142578125) > DirectionalShadowDepthMap.sample(DirectionalShadowSampler, _139).x)))), float3(0.4545454680919647216796875)), _66); + float3 _139 = ((in.in_var_TEXCOORD3.xyz / float3(in.in_var_TEXCOORD3.w)) * 0.5) + float3(0.5); + float3 _141; + _141.x = _139.x; + float2 _142 = _141.xy; + _142.y = -_139.y; + float _149; + int _152; + int _154; + _149 = 0.0; + _152 = 0; + _154 = -4; + float _150; + int _153; + for (; _154 <= 4; _149 = _150, _152 = _153, _154++) + { + _153 = _152; + _150 = _149; + for (int _163 = -4; _163 <= 4; ) + { + _153++; + _150 += float((in.in_var_TEXCOORD3.z - 0.001000000047497451305389404296875) > DirectionalShadowDepthMap.sample(DirectionalShadowSampler, (_142 + float2(float(_163) * 0.000244140625, float(_154) * 0.000244140625))).x); + _163++; + continue; + } + } + out.out_var_SV_Target0 = float4(powr(_68.xyz * ((float3(0.100000001490116119384765625) + _122) + ((Uniforms.directionalLightColor.xyz * precise::max(dot(Uniforms.directionalLight.xyz, in.in_var_TEXCOORD2), 0.0)) * (1.0 - (_149 / float(_152))))), float3(0.4545454680919647216796875)), _69); return out; } diff --git a/projects/content/_shaders/spv/lit_mesh.frag.spv b/projects/content/_shaders/spv/lit_mesh.frag.spv index d7c14fa..22a91f3 100644 Binary files a/projects/content/_shaders/spv/lit_mesh.frag.spv and b/projects/content/_shaders/spv/lit_mesh.frag.spv differ