refactor: cleaner request path + raytracing code
This commit is contained in:
+120
-29
@@ -1,3 +1,13 @@
|
||||
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;
|
||||
@@ -10,6 +20,7 @@ struct PushConstants
|
||||
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;
|
||||
@@ -18,19 +29,8 @@ struct PushConstants
|
||||
[[vk::push_constant]]
|
||||
uniform PushConstants constants;
|
||||
|
||||
[[vk::binding(0, 0)]]
|
||||
RWStructuredBuffer<uint32_t> structure_pool;
|
||||
[[vk::binding(1, 0)]]
|
||||
RWStructuredBuffer<uint32_t> attributes_pool[2];
|
||||
[[vk::binding(2, 0)]]
|
||||
RWStructuredBuffer<uint32_t> request_buffer;
|
||||
[[vk::binding(3, 0)]]
|
||||
RWStructuredBuffer<uint32_t> usage_buffer;
|
||||
|
||||
[[vk::binding(0, 1)]]
|
||||
RWStructuredBuffer<uint32_t> structure_table_pointer;
|
||||
[[vk::binding(1, 1)]]
|
||||
RWStructuredBuffer<Atomic<uint32_t>> structure_table_request_buffer;
|
||||
ParameterBlock<VoxelCacheParameters<2>> cache_parameters;
|
||||
ParameterBlock<BvhParameters> table_parameters;
|
||||
|
||||
[[vk::binding(0, 2)]]
|
||||
[[format("rgba32f")]]
|
||||
@@ -48,38 +48,129 @@ RWTexture2D<float4> gbuffer_albedo;
|
||||
[[format("rgba32f")]]
|
||||
RWTexture2D<float4> gbuffer_normal;
|
||||
|
||||
float3 get_ray_direction(uint32_t2 pixel_loc)
|
||||
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)
|
||||
{
|
||||
let ndc_loc_x = (float)pixel_loc.x / (float)constants.width * 2. - 1.;
|
||||
let ndc_loc_y = 1. - (float)pixel_loc.y / (float)constants.height * 2.;
|
||||
var world_loc = mul(constants.view_proj, float4(ndc_loc_x, ndc_loc_y, 1., 1.));
|
||||
world_loc /= world_loc.w;
|
||||
return normalize(world_loc.xyz - constants.cam_pos);
|
||||
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, uint32_t3 local_location: SV_GroupThreadID)
|
||||
void gi(uint32_t3 location: SV_DispatchThreadID)
|
||||
{
|
||||
if (location.x >= constants.width || location.y >= constants.height)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (gbuffer_albedo.Load(location.xy).w == 0.)
|
||||
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 pos = gbuffer_position.Load(location.xy);
|
||||
let normal = gbuffer_normal.Load(location.xy);
|
||||
let albedo = gbuffer_albedo.Load(location.xy);
|
||||
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;
|
||||
|
||||
// let floored_pos = floor(pos);
|
||||
// let c = (floored_pos.x + floored_pos.y + floored_pos.z) % 2;
|
||||
// target_texture.Store(location.xy, albedo * select(c == 0, 0.5, 1.));
|
||||
target_texture.Store(location.xy, albedo);
|
||||
return;
|
||||
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<Bvh, VoxelCacheTracer<2, ColorHit<2>>, 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));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user