Files
vxls/shaders/voxel.slang
T
2026-10-04 20:15:00 +02:00

204 lines
5.3 KiB
Plaintext

import ray_cast;
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;
}
public struct VertexOutput
{
public float4 position : SV_Position;
[vk::location(0)]
public float3 world_position;
[vk::location(1)]
public nointerpolation uint32_t structure_id;
[vk::location(2)]
public float3 cam_position;
[vk::location(3)]
public float3 chunk_position;
}
[[vk::push_constant]]
uniform PushConstants constants;
struct ByteColor
{
uint32_t byte_color;
property uint32_t byte_r
{
get {
return byte_color & 0xFF;
}
}
property uint32_t byte_g
{
get {
return (byte_color >> 8) & 0xFF;
}
}
property uint32_t byte_b
{
get {
return (byte_color >> 16) & 0xFF;
}
}
property uint32_t byte_a
{
get {
return byte_color >> 24;
}
}
property float4 float_color
{
get {
return float4(
float(byte_r) / 255.,
float(byte_g) / 255.,
float(byte_b) / 255.,
float(byte_a) / 255.);
}
}
}
[[vk::binding(0, 0)]]
RWStructuredBuffer<StructurePoolElement> structure_pool;
[[vk::binding(1, 0)]]
RWStructuredBuffer<uint32_t> attributes_pool[2];
[[vk::binding(2, 0)]]
RWStructuredBuffer<RequestBufferElement> request_buffer;
[[vk::binding(3, 0)]]
RWStructuredBuffer<uint32_t> usage_buffer;
[[vk::binding(4, 0)]]
RWStructuredBuffer<StructurePointer> structure_table_pointer;
[[vk::binding(5, 0)]]
RWStructuredBuffer<Atomic<uint32_t>> structure_table_request_buffer;
ByteColor fetch_color(uint32_t index, uint32_t child_index)
{
return ByteColor(attributes_pool[0][index * 64 + child_index]);
}
float3 fetch_normal(uint32_t index, uint32_t child_index)
{
let a = asfloat(attributes_pool[1][index * 64 * 3 + child_index * 3]);
let b = asfloat(attributes_pool[1][index * 64 * 3 + child_index * 3 + 1]);
let c = asfloat(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, 1)]]
[[format("rgba32f")]]
WTexture2D<float4> gbuffer_position;
[[vk::binding(1, 1)]]
[[format("rgba32f")]]
WTexture2D<float4> gbuffer_albedo;
[[vk::binding(2, 1)]]
[[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;
var voxel_cache : VoxelCache;
voxel_cache.structure_pool = structure_pool;
voxel_cache.attributes_pool = attributes_pool;
voxel_cache.request_buffer = request_buffer;
voxel_cache.usage_buffer = usage_buffer;
voxel_cache.structure_table_pointer = structure_table_pointer;
voxel_cache.structure_table_request_buffer = structure_table_request_buffer;
let ray_direction = get_ray_direction(location.xy);
var ray_cast_context : RayCastContext;
ray_cast_context.ray_direction = ray_direction;
ray_cast_context.ray_origin = constants.cam_pos;
ray_cast_context.bounds = uint32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_width);
ray_cast_context.frame_timestamp = constants.frame_timestamp;
ray_cast_context.cache_report = true;
let hit = ray_cast(voxel_cache, ray_cast_context, stack_index);
if (hit.hit)
{
// Gather
let hit_pos = hit.hit_pos;
let hit_normal = fetch_normal(hit.node_index, hit.child_index);
let hit_albedo = fetch_color(hit.node_index, hit.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);
}