222 lines
6.3 KiB
Plaintext
222 lines
6.3 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;
|
|
|
|
public struct ColorHit<let N : uint32_t> : IVoxelHit<N>
|
|
{
|
|
public __init() {};
|
|
|
|
public bool shouldHit(VoxelCacheParameters<N> cache, uint32_t node_index, uint32_t child_index)
|
|
{
|
|
return ByteColor(cache.attributes_pool[0][node_index * 64 + child_index]).byte_a != 0;
|
|
}
|
|
}
|
|
|
|
// [[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, uint32_t3 local_location: SV_GroupThreadID)
|
|
{
|
|
if (location.x >= constants.width || location.y >= constants.height)
|
|
{
|
|
return;
|
|
}
|
|
|
|
let stack_index = local_location.x + local_location.y * 16;
|
|
|
|
VoxelCacheTracer<2, ColorHit<2>> cache_tracer;
|
|
cache_tracer.parameters = cache_parameters;
|
|
cache_tracer.report_usage = true;
|
|
cache_tracer.report_requests = true;
|
|
cache_tracer.frame_timestamp = constants.frame_timestamp;
|
|
cache_tracer.voxel_hit = ColorHit<2>();
|
|
|
|
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.thread_index = local_location.x;
|
|
BvhCursor c = bvh.begin(r);
|
|
Cell cell;
|
|
|
|
// for (uint32_t i = 0; i < 100; i++)
|
|
// {
|
|
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<ToroidalStructureTableTracer, VoxelCacheTracer<2, ColorHit<2>>, PixelFootprintLod> tracer;
|
|
tracer.structure = table_tracer;
|
|
tracer.cache = cache_tracer;
|
|
tracer.lod = lod;
|
|
|
|
Hit h;
|
|
bool hit = tracer.trace(stack_index, h, r);
|
|
|
|
ToroidalStructureTableTracer.Cursor cursor = table_tracer.begin(r);
|
|
Cell cell;
|
|
|
|
if (hit)
|
|
{
|
|
// Gather
|
|
let hit_pos = h.hit_location;
|
|
|
|
let hit_normal = fetch_normal(h.node_index, h.child_index);
|
|
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);
|
|
}
|
|
|