Files
vxls/shaders/voxel.slang
T

224 lines
6.7 KiB
Plaintext

// import ray_cast;
import voxel_cache;
// import fixed_structure_table;
import toroidal_structure_table;
import bvh;
import voxel_tracing;
import lod_policies;
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. Computed on
// the CPU from the toroidal table's current offset.
uint32_t table_base_slot_x;
uint32_t table_base_slot_y;
uint32_t table_base_slot_z;
}
public struct VertexOutput
{
public float4 position : SV_Position;
[vk::location(0)]
public float3 world_position;
[vk::location(1)]
public float3 cam_position;
[vk::location(2)]
public float3 chunk_position;
}
[[vk::push_constant]]
uniform PushConstants constants;
ParameterBlock<VoxelCacheParameters<2>> cache_parameters;
// ParameterBlock<ToroidalStructureTable> table_parameters;
ParameterBlock<BvhParameters> table_parameters;
// ColorHit<2> (the demo hit predicate) lives in voxel_cache.slang so the
// primary and secondary front-ends share one definition.
// [[vk::binding(0, 0)]]
// RWStructuredBuffer<StructurePoolElement> structure_pool;
// [[vk::binding(1, 0)]]
// RWStructuredBuffer<RequestBufferElement> request_buffer;
// [[vk::binding(2, 0)]]
// RWStructuredBuffer<uint32_t> usage_buffer;
// [[vk::binding(3, 0)]]
// RWStructuredBuffer<uint32_t> attributes_pool[2];
//
// [[vk::binding(0, 1)]]
// RWStructuredBuffer<StructurePointer> structure_table_pointer;
// [[vk::binding(1, 1)]]
// RWStructuredBuffer<Atomic<uint32_t>> structure_table_request_buffer;
ByteColor fetch_color(uint32_t index, uint32_t child_index)
{
return ByteColor(cache_parameters.attributes_pool[0][index * 64 + child_index]);
}
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);
}
bool3 min_mask(float3 val)
{
let min_val = min(val.x, min(val.y, val.z));
return val == min_val;
}
[[vk::binding(0, 2)]]
[[format("rgba32f")]]
WTexture2D<float4> gbuffer_position;
[[vk::binding(1, 2)]]
[[format("rgba32f")]]
WTexture2D<float4> gbuffer_albedo;
[[vk::binding(2, 2)]]
[[format("rgba32f")]]
WTexture2D<float4> gbuffer_normal;
float3 get_ray_direction(uint32_t2 pixel_loc)
{
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);
}
[shader("compute")]
[numthreads(16, 16, 1)]
void gi_march(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_location: SV_GroupThreadID)
{
if (location.x >= constants.width || location.y >= constants.height)
{
return;
}
}
[shader("compute")]
[numthreads(16, 16, 1)]
void ray_march_compute(uint32_t3 location: SV_DispatchThreadID)
{
if (location.x >= constants.width || location.y >= constants.height)
{
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>();
// Per-ray trace state. Local storage keeps the tracer independent of the
// workgroup shape; swap this for a groupshared-backed ITraceStack if the
// primary pass needs groupshared memory back.
LocalTraceStack stack;
TraceSettings settings;
settings.t_min = 0.;
settings.t_max = asfloat(0x7f7fffff); // FLT_MAX
settings.max_cells = 0xFFFFFFFF;
settings.max_steps = 500;
settings.any_hit = false;
settings.report_usage = true;
settings.report_requests = true;
let ray_direction = get_ray_direction(location.xy);
Ray r;
r.origin = constants.cam_pos;
r.direction = ray_direction;
Bvh bvh;
bvh.parameters = table_parameters;
bvh.max_t = asfloat(0x7f7fffff); // FLT_MAX: unbounded primary rays
// BvhCursor c = bvh.begin(r);
Cell cell;
// Setup for simple bvh traversal check
// if (bvh.next(c, cell))
// {
// gbuffer_albedo.Store(location.xy, float4(0., 1., 0., 1.));
// return;
// }
// gbuffer_albedo.Store(location.xy, float4(0.));
// ToroidalStructureTableTracer table_tracer;
// table_tracer.structure_table = table_parameters;
// table_tracer.extent =
// uint32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_height);
// table_tracer.base_slot = uint32_t3(
// constants.table_base_slot_x,
// constants.table_base_slot_y,
// constants.table_base_slot_z);
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;
Hit h;
// Closest hit at or past tMin. Re-trace past a hit with:
// settings.t_min = h.t + 1e-4;
// hit = tracer.trace(h, r, settings, stack);
bool hit = tracer.trace(h, r, settings, stack);
// ToroidalStructureTableTracer.Cursor cursor = table_tracer.begin(r);
// Cell cell;
if (hit)
{
// `Hit` is already in world space; only the local attribute normal
// still needs rotating into world space.
let hit_pos = h.hit_location;
let local_normal = mul(h.normal_to_world, fetch_normal(h.node_index, h.child_index));
let hit_normal = local_normal / max(length(local_normal), 1e-6);
var hit_albedo = float4(0.);
hit_albedo = fetch_color(h.node_index, h.child_index).float_color;
// let hit_albedo = hit.color;
gbuffer_position.Store(location.xy, float4(hit_pos, 1.));
gbuffer_normal.Store(location.xy, float4(hit_normal, 1.));
gbuffer_albedo.Store(location.xy, hit_albedo);
}
else
{
gbuffer_albedo.Store(location.xy, float4(0.));
}
}
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
{
let min_ts = (box_min - origin) / ray_direction;
let max_ts = (box_max - origin) / ray_direction;
let far_ts = max(min_ts, max_ts);
let near_ts = min(min_ts, max_ts);
let far_t = min(far_ts.x, min(far_ts.y, far_ts.z));
let near_t = max(near_ts.x, max(near_ts.y, near_ts.z));
return float2(near_t, far_t);
}