Revamped shader arch
This commit is contained in:
+3
-190
@@ -1,5 +1,3 @@
|
||||
import ray_cast;
|
||||
|
||||
struct PushConstants
|
||||
{
|
||||
float4x4 view_proj;
|
||||
@@ -13,103 +11,23 @@ struct PushConstants
|
||||
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;
|
||||
RWStructuredBuffer<uint32_t> structure_pool;
|
||||
[[vk::binding(1, 0)]]
|
||||
RWStructuredBuffer<uint32_t> attributes_pool[2];
|
||||
[[vk::binding(2, 0)]]
|
||||
RWStructuredBuffer<RequestBufferElement> request_buffer;
|
||||
RWStructuredBuffer<uint32_t> request_buffer;
|
||||
[[vk::binding(3, 0)]]
|
||||
RWStructuredBuffer<uint32_t> usage_buffer;
|
||||
|
||||
[[vk::binding(0, 1)]]
|
||||
RWStructuredBuffer<StructurePointer> structure_table_pointer;
|
||||
RWStructuredBuffer<uint32_t> 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(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, 2)]]
|
||||
[[format("rgba32f")]]
|
||||
WTexture2D<float4> target_texture;
|
||||
@@ -155,110 +73,5 @@ void gi(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_location: SV_Gr
|
||||
let albedo = gbuffer_albedo.Load(location.xy);
|
||||
target_texture.Store(location.xy, albedo);
|
||||
return;
|
||||
|
||||
let illumination = dot(float3(0., 1., 0.), normal.xyz);
|
||||
|
||||
let view_dir = pos.xyz - constants.cam_pos;
|
||||
// let ray_dir = normalize(reflect(view_dir, normal.xyz));
|
||||
let ray_dir = sampleHemisphere(normal.xyz, constants.frame_timestamp, 6767 + (location.x * 67) + (location.y * 91));
|
||||
if (any(isinf(ray_dir)) || any(isnan(ray_dir)))
|
||||
{
|
||||
target_texture.Store(location.xy, albedo);
|
||||
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;
|
||||
|
||||
var ray_cast_context : RayCastContext;
|
||||
ray_cast_context.ray_direction = ray_dir;
|
||||
ray_cast_context.ray_origin = pos.xyz + normal.xyz * 0.1;
|
||||
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 smpl_albedo = fetch_color(hit.node_index, hit.child_index).float_color;
|
||||
target_texture.Store(location.xy, albedo * smpl_albedo);
|
||||
}
|
||||
else
|
||||
{
|
||||
target_texture.Store(location.xy, float4(float3(illumination), 1.));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Returns a cosine-weighted random direction in the hemisphere
|
||||
// oriented around `normal`.
|
||||
//
|
||||
// `frame` should change every frame/sample sequence.
|
||||
// `seed` can be a per-pixel or per-path identifier.
|
||||
float3 sampleHemisphere(float3 normal, uint frame, uint seed)
|
||||
{
|
||||
// Hash to get two uniform random numbers in [0, 1).
|
||||
uint state = seed ^ (frame * 0x9E3779B9u);
|
||||
|
||||
state ^= state >> 16;
|
||||
state *= 0x7FEB352Du;
|
||||
state ^= state >> 15;
|
||||
state *= 0x846CA68Bu;
|
||||
state ^= state >> 16;
|
||||
|
||||
float u1 = float(state) * (1.0 / 4294967296.0);
|
||||
|
||||
state ^= state >> 16;
|
||||
state *= 0x7FEB352Du;
|
||||
state ^= state >> 15;
|
||||
state *= 0x846CA68Bu;
|
||||
state ^= state >> 16;
|
||||
|
||||
float u2 = float(state) * (1.0 / 4294967296.0);
|
||||
|
||||
// Cosine-weighted hemisphere sampling.
|
||||
float r = sqrt(u1);
|
||||
float phi = 2.0 * 3.14159 * u2;
|
||||
|
||||
float x = r * cos(phi);
|
||||
float y = r * sin(phi);
|
||||
float z = sqrt(max(0.0, 1.0 - u1));
|
||||
|
||||
// Build an orthonormal basis around the normal.
|
||||
float3 tangent;
|
||||
|
||||
if (abs(normal.z) < 0.999)
|
||||
tangent = normalize(cross(float3(0.0, 0.0, 1.0), normal));
|
||||
else
|
||||
tangent = normalize(cross(float3(0.0, 1.0, 0.0), normal));
|
||||
|
||||
float3 bitangent = cross(normal, tangent);
|
||||
|
||||
// Transform from local hemisphere space to world space.
|
||||
return normalize(
|
||||
tangent * x +
|
||||
bitangent * y +
|
||||
normal * z);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user