From a403c07e8ae848a3052b4013b2254fed7e88af9f Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 18 Jul 2025 00:54:13 -0500 Subject: [PATCH] lighting testing --- res/sh/common.hlsl | 1 + res/sh/sh_common.h | 3 ++- res/sh/shade.hlsl | 54 ++++++++++++++++++++++++++++++++++++++++------ src/gp_dx12.c | 2 ++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/res/sh/common.hlsl b/res/sh/common.hlsl index 992be30f..fa5b075c 100644 --- a/res/sh/common.hlsl +++ b/res/sh/common.hlsl @@ -1,5 +1,6 @@ #include "sh/sh_common.h" +#define TAU 6.28318530718 #define PI 3.14159265359 #define GOLDEN 1.61803398875 diff --git a/res/sh/sh_common.h b/res/sh/sh_common.h index 879e9f90..5c002a8a 100644 --- a/res/sh/sh_common.h +++ b/res/sh/sh_common.h @@ -118,6 +118,7 @@ SH_ASSERT_32BIT(struct sh_flood_constants, 6); /* Expected 32bit root constant * ========================== */ SH_STRUCT(sh_shade_constants { + SH_DECL(float, time); SH_DECL(uint, albedo_tex_urid); SH_DECL(uint, emittance_tex_urid); SH_DECL(uint, emittance_flood_tex_urid); @@ -125,7 +126,7 @@ SH_STRUCT(sh_shade_constants { SH_DECL(uint, tex_width); SH_DECL(uint, tex_height); }); -SH_ASSERT_32BIT(struct sh_shade_constants, 6); /* Expected 32bit root constant size in shader */ +SH_ASSERT_32BIT(struct sh_shade_constants, 7); /* Expected 32bit root constant size in shader */ /* ========================== * * Shape shader structures diff --git a/res/sh/shade.hlsl b/res/sh/shade.hlsl index 6ea5c963..29b72092 100644 --- a/res/sh/shade.hlsl +++ b/res/sh/shade.hlsl @@ -5,7 +5,7 @@ * ========================== */ #define ROOTSIG \ - "RootConstants(num32BitConstants = 6, b0), " \ + "RootConstants(num32BitConstants = 7, b0), " \ "DescriptorTable(SRV(t0, space = 0, numDescriptors = unbounded, flags = DESCRIPTORS_VOLATILE)), " \ "DescriptorTable(SRV(t0, space = 1, numDescriptors = unbounded, flags = DESCRIPTORS_VOLATILE)), " \ "DescriptorTable(UAV(u0, space = 2, numDescriptors = unbounded, flags = DESCRIPTORS_VOLATILE)), " \ @@ -33,6 +33,51 @@ struct cs_input { DECLS(uint3, SV_DispatchThreadID); }; +#define SAMPLES 4 +#define MARCHES 16 + +INLINE float4 get_light_in_dir(uint2 pos, float2 dir) +{ + float4 result = 0; + uint2 at = pos; + for (uint i = 0; i < MARCHES; ++i) { + uint2 flood = g_emittance_flood_textures[g_constants.emittance_flood_tex_urid][at]; + int2 dist_vec = (int2)at - (int2)flood; + float dist = length(dist_vec); + if (dist < 2) { + result = g_gbuff_textures[g_constants.emittance_tex_urid][flood]; + break; + } else { + at += dir * dist; + } + } + return result; +} + +float rand_float_from_float2(float2 pos) { + return frac(sin(dot(pos.xy, float2(12.9898,78.233))) * 43758.5453123); +} + +INLINE float4 get_light_at_pos(uint2 pos) +{ + float4 result = 0; + for (int i = 0; i < SAMPLES; ++i) { + float angle = TAU * (((float)i + rand_float_from_float2(pos + (float)i + sin(g_constants.time))) / ((float)SAMPLES - 1)); + float2 dir = float2(sin(angle), cos(angle)); + float4 light_in_dir = get_light_in_dir(pos, dir); + result += light_in_dir; + } + result /= SAMPLES; + + return result; + + + // float4 emittance = g_gbuff_textures[g_constants.emittance_tex_urid][pos]; + // uint2 emittance_flood = g_emittance_flood_textures[g_constants.emittance_flood_tex_urid][pos]; + // emittance_flood *= (emittance_flood.x < 0xFFFF && emittance_flood.y < 0xFFFF); + // return float4(float(emittance_flood.x) / float(g_constants.tex_width), float(emittance_flood.y) / float(g_constants.tex_height), 0, 1); +} + [numthreads(8, 8, 1)] SH_ENTRY(ROOTSIG) void cs(struct cs_input input) { @@ -42,12 +87,9 @@ SH_ENTRY(ROOTSIG) void cs(struct cs_input input) } float4 old_color = g_gbuff_textures[g_constants.write_tex_urid][id]; float4 albedo = g_gbuff_textures[g_constants.albedo_tex_urid][id]; - float4 emittance = g_gbuff_textures[g_constants.emittance_tex_urid][id]; + float4 lighting = get_light_at_pos(id); - uint2 emittance_flood = g_emittance_flood_textures[g_constants.emittance_flood_tex_urid][id]; - emittance_flood *= (emittance_flood.x < 0xFFFF && emittance_flood.y < 0xFFFF); - - float4 final_color = old_color + albedo + float4(float(emittance_flood.x) / float(g_constants.tex_width), float(emittance_flood.y) / float(g_constants.tex_height), 0, 1); + float4 final_color = old_color + albedo + lighting; g_write_textures[g_constants.write_tex_urid][id] = final_color; } diff --git a/src/gp_dx12.c b/src/gp_dx12.c index 9c50946f..b311d249 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -2931,6 +2931,8 @@ void gp_run(struct gp_run_params params) /* Set constants */ struct sh_shade_constants constants = ZI; + /* TODO: Remove this */ + constants.time = sh_float_from_f32((f32)SECONDS_FROM_NS(sys_time_ns())); constants.albedo_tex_urid = sh_uint_from_u32(sig->albedo->srv_descriptor->index); constants.emittance_tex_urid = sh_uint_from_u32(sig->emittance->srv_descriptor->index); constants.emittance_flood_tex_urid = sh_uint_from_u32(emittance_flood_read->srv_descriptor->index);