diff --git a/res/sh/blit.hlsl b/res/sh/blit.hlsl index be20b1a2..caf18a58 100644 --- a/res/sh/blit.hlsl +++ b/res/sh/blit.hlsl @@ -81,7 +81,8 @@ SH_ENTRY(ROOTSIG) struct ps_output ps(struct ps_input input) /* Apply tone map */ if (g_constants.flags & SH_BLIT_FLAG_TONE_MAP) { /* TODO: Dynamic exposure based on average scene luminance */ - color.rgb = tone_map(color.rgb) * g_constants.exposure; + color.rgb *= g_constants.exposure; + color.rgb = tone_map(color.rgb); } /* Apply gamma correction */ diff --git a/res/sh/material.hlsl b/res/sh/material.hlsl index 0067b8ec..b73c26d6 100644 --- a/res/sh/material.hlsl +++ b/res/sh/material.hlsl @@ -54,7 +54,6 @@ SH_ENTRY(ROOTSIG) struct vs_output vs(struct vs_input input) struct sh_material_instance instance = g_instances[input.SV_InstanceID]; float2 vert = unit_quad_verts[input.SV_VertexID]; float2 world_pos = mul(instance.xf, float3(vert, 1)).xy; - struct vs_output output; output.SV_Position = mul(g_constants.projection, float4(world_pos, 0, 1)); output.tex_nurid = instance.tex_nurid; diff --git a/res/sh/sh_common.h b/res/sh/sh_common.h index 8ba06f2c..9947e05a 100644 --- a/res/sh/sh_common.h +++ b/res/sh/sh_common.h @@ -102,6 +102,7 @@ SH_STRUCT(sh_material_constants { /* ---------------------------------------------------- */ SH_DECL(float4x4, projection); /* 16 consts */ /* ---------------------------------------------------- */ + }); SH_ASSERT_ROOT_CONST(struct sh_material_constants, 16); /* Expected to match num32BitConstants in shader's root signature */ @@ -159,11 +160,12 @@ SH_STRUCT(sh_shade_constants { /* ---------------------------------------------------- */ SH_DECL(uint, emittance_tex_urid); /* 01 consts */ SH_DECL(uint, emittance_flood_tex_urid); /* 01 consts */ + SH_DECL(uint, read_tex_urid); /* 01 consts */ SH_DECL(uint, target_tex_urid); /* 01 consts */ - SH_DECL(uint, tex_width); /* 01 consts */ /* ---------------------------------------------------- */ + SH_DECL(uint, tex_width); /* 01 consts */ SH_DECL(uint, tex_height); /* 01 consts */ - SH_DECL(uint3, _pad0); /* 03 consts (padding) */ + SH_DECL(uint2, _pad0); /* 02 consts (padding) */ /* ---------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_shade_constants, 16); /* Expected to match num32BitConstants in shader's root signature */ diff --git a/res/sh/shade.hlsl b/res/sh/shade.hlsl index fedebe17..6d5d2b0a 100644 --- a/res/sh/shade.hlsl +++ b/res/sh/shade.hlsl @@ -25,27 +25,33 @@ struct cs_input { * Lighting * ========================== */ -#define SAMPLES 4 +#define SAMPLES 1 #define MARCHES 16 -#define AMBIENT float4(0, 0, 0, 0) +#define AMBIENT float3(0, 0, 0) -float rand_angle(int2 pos, uint ray_index) { +float rand_angle(uint2 pos, uint ray_index) { Texture3D noise_tex = g_noise_textures[SH_BLUE_NOISE_TEX_ID]; - uint3 noise_coord = uint3(abs(pos.xy), ray_index); - noise_coord += g_constants.frame_seed; - // noise_coord.xy += g_constants.frame_seed.xy; + int3 noise_coord = int3(1, 1, 1); + noise_coord += int3(pos.xy, ray_index); + + // noise_coord.xy += g_constants.frame_index; + // noise_coord.xyz += g_constants.frame_seed.xyz; + // noise_coord.xyz += g_constants.frame_seed.xyz; + // noise_coord.z *= g_constants.frame_seed.w; + + // noise_coord.xy uint noise = noise_tex[noise_coord % uint3(SH_BLUE_NOISE_TEX_WIDTH, SH_BLUE_NOISE_TEX_HEIGHT, SH_BLUE_NOISE_TEX_DEPTH)]; return ((float)noise / (float)0xFFFF) * TAU; } -INLINE float4 get_light_in_dir(uint2 ray_start, float2 ray_dir) +INLINE float3 get_light_in_dir(uint2 ray_start, float2 ray_dir) { Texture2D flood_tex = g_textures_uint2[g_constants.emittance_flood_tex_urid]; Texture2D emittance_tex = g_textures_float4[g_constants.emittance_tex_urid]; - float4 result = AMBIENT; + float3 result = AMBIENT; float2 at_float = ray_start; uint2 at_uint = ray_start; for (uint i = 0; i < MARCHES; ++i) { @@ -53,7 +59,7 @@ INLINE float4 get_light_in_dir(uint2 ray_start, float2 ray_dir) float2 dist_vec = at_float - (float2)flood; float dist = length(dist_vec); if (dist < 1) { - result = emittance_tex[flood]; + result = emittance_tex[flood].rgb; break; } else { at_float += ray_dir * dist; @@ -68,17 +74,16 @@ INLINE float4 get_light_in_dir(uint2 ray_start, float2 ray_dir) return result; } -INLINE float4 get_light_at_pos(int2 pos) +INLINE float3 get_light_at_pos(uint2 pos) { - float4 result = 0; + float3 result = 0; for (uint i = 0; i < SAMPLES; ++i) { float angle = rand_angle(pos, i); float2 dir = float2(cos(angle), sin(angle)); - float4 light_in_dir = get_light_in_dir(pos, dir); + float3 light_in_dir = get_light_in_dir(pos, dir); result += light_in_dir; } result /= SAMPLES; - return result; } @@ -92,6 +97,7 @@ SH_ENTRY(ROOTSIG) void cs(struct cs_input input) uint2 id = input.SV_DispatchThreadID.xy; if (id.x < g_constants.tex_width && id.y < g_constants.tex_height) { Texture2D albedo_tex = g_textures_float4[g_constants.albedo_tex_urid]; + Texture2D read_tex = g_textures_float4[g_constants.read_tex_urid]; RWTexture2D target_tex = g_target_textures[g_constants.target_tex_urid]; float4 color = float4(1, 1, 1, 1); @@ -99,7 +105,13 @@ SH_ENTRY(ROOTSIG) void cs(struct cs_input input) color *= albedo_tex[id]; /* Apply lighting */ - color *= get_light_at_pos(id); + color.rgb *= get_light_at_pos(id); + + /* Apply temporal accumulation */ + float hysterisis = 0; + // hysterisis = 0.2; + // hysterisis = 0.9; + color.rgb = lerp(color.rgb, read_tex[id].rgb, hysterisis); target_tex[id] = color; } diff --git a/src/config.h b/src/config.h index bc21094c..6f5642ce 100644 --- a/src/config.h +++ b/src/config.h @@ -84,6 +84,6 @@ /* TODO: Move these to user-configurable settings */ -#define VSYNC 0 +#define VSYNC 1 #define AUDIO_ENABLED 0 #define FPS_LIMIT 300 diff --git a/src/gp_dx12.c b/src/gp_dx12.c index 06f07471..737e6497 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -3273,6 +3273,7 @@ struct gp_resource *gp_run_render(struct gp_render_sig *render_sig, struct gp_re 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(sig->emittance_flood_read->srv_descriptor->index); + constants.read_tex_urid = sh_uint_from_u32(sig->shade_read->uav_descriptor->index); constants.target_tex_urid = sh_uint_from_u32(sig->shade_target->uav_descriptor->index); constants.tex_width = sh_uint_from_u32(final_target_size.x); constants.tex_height = sh_uint_from_u32(final_target_size.y); @@ -3324,13 +3325,8 @@ struct gp_resource *gp_run_render(struct gp_render_sig *render_sig, struct gp_re ID3D12GraphicsCommandList_SetGraphicsRootSignature(cl->cl, blit_pipeline->rootsig); /* Set Rasterizer State */ -#if 1 D3D12_VIEWPORT viewport = viewport_from_rect(params.draw_target_viewport); D3D12_RECT scissor = scissor_from_rect(params.draw_target_viewport); -#else - D3D12_VIEWPORT viewport = viewport_from_rect(RECT_FROM_V2(V2(0, 0), V2(final_target_size.x, final_target_size.y))); - D3D12_RECT scissor = scissor_from_rect(params.draw_target_viewport); -#endif ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &viewport); ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); diff --git a/src/sim_step.c b/src/sim_step.c index c86f6852..17ea5770 100644 --- a/src/sim_step.c +++ b/src/sim_step.c @@ -282,8 +282,8 @@ INTERNAL void test_spawn_entities2(struct sim_ent *parent, struct v2 pos) } sim_ent_enable_prop(e, SEPROP_DYNAMIC); - e->mass_unscaled = 100; - e->inertia_unscaled = 50; + e->mass_unscaled = 50; + e->inertia_unscaled = 2; #if 0 e->linear_ground_friction = 100; e->angular_ground_friction = 50;