import voxel_cache; import bvh; import voxel_tracing; import lod_policies; // Simple screen-space AO: reconstruct the world-space surface from the primary // G-buffer and trace a few short, incoherent hemisphere rays per pixel through // the same tracer the primary pass uses. This is the secondary-ray smoke test: // short `t_max`, any-hit, usage feedback only, no detail requests. struct PushConstants { float4x4 view_proj; float3 cam_pos; uint32_t frame_timestamp; uint32_t width; uint32_t height; uint32_t chunk_width; uint32_t chunk_height; uint32_t chunk_alt; // Canonical table slot of the visible block's minimum corner. uint32_t table_base_slot_x; uint32_t table_base_slot_y; uint32_t table_base_slot_z; } [[vk::push_constant]] uniform PushConstants constants; ParameterBlock> cache_parameters; ParameterBlock table_parameters; [[vk::binding(0, 2)]] [[format("rgba32f")]] WTexture2D target_texture; [[vk::binding(1, 2)]] [[format("rgba32f")]] RWTexture2D gbuffer_position; [[vk::binding(2, 2)]] [[format("rgba32f")]] RWTexture2D gbuffer_albedo; [[vk::binding(3, 2)]] [[format("rgba32f")]] RWTexture2D gbuffer_normal; static const uint32_t AO_SAMPLE_COUNT = 4; static const float AO_RADIUS = 0.25; static const float AO_EPSILON = 0.01; float hash12(float2 p) { var p3 = frac(float3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return frac((p3.x + p3.y) * p3.z); } float3 fetch_normal(uint32_t index, uint32_t child_index) { let a = asfloat(cache_parameters.attributes_pool[1][index * 64 * 3 + child_index * 3]); let b = asfloat(cache_parameters.attributes_pool[1][index * 64 * 3 + child_index * 3 + 1]); let c = asfloat(cache_parameters.attributes_pool[1][index * 64 * 3 + child_index * 3 + 2]); return float3(a, b, c); } /// Cosine-weighted hemisphere sample around `n`, rotated per pixel so the /// fixed sample count does not band. float3 hemisphere_sample(float3 n, uint32_t sample_index, float rotation) { let up = select(abs(n.z) < 0.999, float3(1., 0., 0.), float3(0., 0., 1.)); let tangent = normalize(cross(up, n)); let bitangent = cross(n, tangent); let r = sqrt((float(sample_index) + 0.5) / float(AO_SAMPLE_COUNT)); let phi = 6.28318530718 * frac(rotation + float(sample_index) * 0.61803398875); let disk = r * float2(cos(phi), sin(phi)); return normalize(tangent * disk.x + bitangent * disk.y + n * sqrt(max(0., 1. - r * r))); } [shader("compute")] [numthreads(16, 16, 1)] void gi(uint32_t3 location: SV_DispatchThreadID) { if (location.x >= constants.width || location.y >= constants.height) { return; } let albedo = gbuffer_albedo.Load(location.xy); // Shunt albedo target_texture.Store(location.xy, albedo); return; // AO Example if (albedo.w == 0.) { target_texture.Store(location.xy, float4(0.)); return; } let position = gbuffer_position.Load(location.xy).xyz; let normal_raw = gbuffer_normal.Load(location.xy).xyz; let normal_length = length(normal_raw); if (normal_length < 1e-6) { target_texture.Store(location.xy, albedo); return; } let normal = -normal_raw / normal_length; VoxelCacheTracer<2, ColorHit<2>> cache_tracer; cache_tracer.parameters = cache_parameters; cache_tracer.frame_timestamp = constants.frame_timestamp; cache_tracer.voxel_hit = ColorHit<2>(); Bvh bvh; bvh.parameters = table_parameters; bvh.max_t = AO_RADIUS; // Coarse LOD: AO does not need primary-ray detail. // PixelFootprintLod lod = PixelFootprintLod.create((100.f * 3.14f) / 180.f, 1080.f, 2.f, 0.05f); PixelFootprintLod lod = PixelFootprintLod.create((100.f * 3.14f) / 180.f, 1080.f, 2.f); Tracer>, PixelFootprintLod> tracer; tracer.structure = bvh; tracer.cache = cache_tracer; tracer.lod = lod; TraceSettings settings; settings.t_min = AO_EPSILON; settings.t_max = 100.; settings.max_cells = 64; settings.max_steps = 64; settings.any_hit = false; settings.report_usage = true; settings.report_requests = false; LocalTraceStack stack; let rotation = hash12(float2(location.xy)); uint32_t occluded = 0; // [loop] // for (uint32_t sample_index = 0; sample_index < AO_SAMPLE_COUNT; sample_index++) // { Ray r; r.origin = position + normal * (AO_EPSILON + 0.1); r.direction = reflect(normalize(position - constants.cam_pos), normal); // r.direction = hemisphere_sample(normal, sample_index, rotation); Hit h; if (tracer.trace(h, r, settings, stack)) { occluded += 1; let hit_normal = fetch_normal(h.node_index, h.child_index); let shade = max(dot(hit_normal, float3(0., 1., 0.)), 0.1); target_texture.Store(location.xy, float4(shade)); } else { target_texture.Store(location.xy, float4(0.)); } //} // let ao = 1. - float(occluded) / float(AO_SAMPLE_COUNT); // Soften the effect: a fully black crevice reads as a hole in this demo. // let lit = lerp(1., ao, 0.7); // target_texture.Store(location.xy, float4(albedo.rgb * lit, albedo.a)); }