From f0ddf1913341efc12d570eeaad10b97efcdbe783 Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 22 Jan 2026 12:24:54 -0600 Subject: [PATCH] more shading pass progress --- src/pp/pp_vis/pp_vis.lay | 4 +- src/pp/pp_vis/pp_vis_core.c | 62 ++-- src/pp/pp_vis/pp_vis_gpu.g | 533 ++++++++++++++++---------------- src/pp/pp_vis/pp_vis_gpu.gh | 5 +- src/pp/pp_vis/pp_vis_shared.cgh | 25 +- 5 files changed, 320 insertions(+), 309 deletions(-) diff --git a/src/pp/pp_vis/pp_vis.lay b/src/pp/pp_vis/pp_vis.lay index ef8c932b..d07f7c5d 100644 --- a/src/pp/pp_vis/pp_vis.lay +++ b/src/pp/pp_vis/pp_vis.lay @@ -15,9 +15,9 @@ ////////////////////////////// //- Resources -@ComputeShader V_ClearCellsCS +@ComputeShader V_PrepareShadeCS +@ComputeShader V_PrepareCellsCS @ComputeShader V_ClearParticlesCS -@ComputeShader V_BackdropCS @VertexShader V_QuadVS @PixelShader V_QuadPS @ComputeShader V_EmitParticlesCS diff --git a/src/pp/pp_vis/pp_vis_core.c b/src/pp/pp_vis/pp_vis_core.c index 05b385ad..8a5c30e6 100644 --- a/src/pp/pp_vis/pp_vis_core.c +++ b/src/pp/pp_vis/pp_vis_core.c @@ -2019,8 +2019,6 @@ void V_TickForever(WaveLaneCtx *lane) - - if (0) { for (P_Ent *bullet = P_FirstEnt(local_frame); !P_IsEntNil(bullet); bullet = P_NextEnt(bullet)) @@ -3631,6 +3629,10 @@ void V_TickForever(WaveLaneCtx *lane) UI_BuildLabelF("Hovered ent: %F", P_FmtKey(hovered_ent->key)); } UI_BuildSpacer(UI_PIX(padding, 1), Axis_Y); + { + UI_BuildLabelF("Shade dims: %F", FmtFloat2(frame->shade_dims)); + } + UI_BuildSpacer(UI_PIX(padding, 1), Axis_Y); { UI_BuildLabelF("RTT: %Fms", FmtFloat(smoothed_rtt * 1000, .p = 3)); UI_BuildLabelF("Client send: %F MiB", FmtFloat(CeilF64((f64)vis_pipe_stats.total_bytes_sent / 1024) / 1024, .p = 3)); @@ -4099,10 +4101,9 @@ void V_TickForever(WaveLaneCtx *lane) G_Format_R16G16B16A16_Float, frame->screen_dims, G_Layout_DirectQueue_RenderTargetWrite, - .flags = G_ResourceFlag_AllowShaderReadWrite | G_ResourceFlag_AllowRenderTarget + .flags = G_ResourceFlag_AllowRenderTarget ); G_Texture2DRef screen_target_ro = G_PushTexture2DRef(frame->gpu_arena, screen_target); - G_RWTexture2DRef screen_target_rw = G_PushRWTexture2DRef(frame->gpu_arena, screen_target); Rng3 viewport = RNG3(VEC3(0, 0, 0), VEC3(frame->screen_dims.x, frame->screen_dims.y, 1)); Rng2 scissor = RNG2(VEC2(viewport.p0.x, viewport.p0.y), VEC2(viewport.p1.x, viewport.p1.y)); @@ -4117,6 +4118,16 @@ void V_TickForever(WaveLaneCtx *lane) G_Texture2DRef shade_target_ro = G_PushTexture2DRef(frame->gpu_arena, shade_target); G_RWTexture2DRef shade_target_rw = G_PushRWTexture2DRef(frame->gpu_arena, shade_target); + // Albedo texture + G_ResourceHandle albedo_target = G_PushTexture2D( + frame->gpu_arena, frame->cl, + G_Format_R16G16B16A16_Float, + frame->shade_dims, + G_Layout_DirectQueue_RenderTargetWrite, + .flags = G_ResourceFlag_AllowRenderTarget + ); + G_Texture2DRef albedo_target_ro = G_PushTexture2DRef(frame->gpu_arena, albedo_target); + // Debug shape buffers G_ResourceHandle dverts_buff = G_PushBufferFromCpuCopy(frame->gpu_arena, frame->cl, StringFromArena(frame->dverts_arena)); G_ResourceHandle dvert_idxs_buff = G_PushBufferFromCpuCopy(frame->gpu_arena, frame->cl, StringFromArena(frame->dvert_idxs_arena)); @@ -4144,21 +4155,9 @@ void V_TickForever(WaveLaneCtx *lane) V_GpuParams params = Zi; { params.dt = frame->dt; - params.xf = frame->xf; - - params.screen_dims = frame->screen_dims; - params.screen_ro = screen_target_ro; - params.screen_rw = screen_target_rw; - - params.shade_dims = frame->shade_dims; - params.shade_ro = shade_target_ro; - params.shade_rw = shade_target_rw; - params.tick = frame->tick; params.seed = RandU64FromState(&frame->rand); - - params.pt_clamp_sampler = G_BasicPointClampSampler(); - params.pt_wrap_sampler = G_BasicPointWrapSampler(); + params.xf = frame->xf; params.selection_mode = frame->selection_mode; params.equipped_tile = frame->equipped_tile; @@ -4175,6 +4174,16 @@ void V_TickForever(WaveLaneCtx *lane) params.camera_pos = frame->camera_pos; params.camera_zoom = frame->camera_zoom; + params.pt_clamp_sampler = G_BasicPointClampSampler(); + params.pt_wrap_sampler = G_BasicPointWrapSampler(); + + params.shade_dims = frame->shade_dims; + params.shade_ro = shade_target_ro; + params.shade_rw = shade_target_rw; + params.albedo_ro = albedo_target_ro; + + params.screen_dims = frame->screen_dims; + params.tiles = gpu_tiles_ref; params.shape_verts = dverts_ro; @@ -4237,8 +4246,11 @@ void V_TickForever(WaveLaneCtx *lane) ////////////////////////////// //- Setup pass - // Clear cells - G_Compute(frame->cl, V_ClearCellsCS, V_ThreadGroupSizeFromTexSize(cells_dims)); + // Prepare shade + G_Compute(frame->cl, V_PrepareShadeCS, V_ThreadGroupSizeFromTexSize(frame->shade_dims)); + + // Prepare cells + G_Compute(frame->cl, V_PrepareCellsCS, V_ThreadGroupSizeFromTexSize(cells_dims)); // Clear particles if (should_clear_particles) @@ -4246,6 +4258,9 @@ void V_TickForever(WaveLaneCtx *lane) G_Compute(frame->cl, V_ClearParticlesCS, V_ThreadGroupSizeFromBufferSize(V_ParticlesCap)); } + // Clear albedo RT + G_ClearRenderTarget(frame->cl, albedo_target, VEC4(0, 0, 0, 0)); + // Discard screen RT G_DiscardRenderTarget(frame->cl, screen_target); @@ -4269,15 +4284,6 @@ void V_TickForever(WaveLaneCtx *lane) G_DumbGlobalMemorySync(frame->cl); } - ////////////////////////////// - //- Backdrop pass - - G_DumbMemoryLayoutSync(frame->cl, screen_target, G_Layout_DirectQueue_ShaderReadWrite); - - { - G_Compute(frame->cl, V_BackdropCS, V_ThreadGroupSizeFromTexSize(frame->screen_dims)); - } - ////////////////////////////// //- Shading pass diff --git a/src/pp/pp_vis/pp_vis_gpu.g b/src/pp/pp_vis/pp_vis_gpu.g index 463c13d1..8b1e4e2a 100644 --- a/src/pp/pp_vis/pp_vis_gpu.g +++ b/src/pp/pp_vis/pp_vis_gpu.g @@ -20,12 +20,24 @@ Vec4 V_DryColor(Vec4 color, f32 dryness) //////////////////////////////////////////////////////////// //~ Utility shaders -//- Clear cells -ComputeShader2D(V_ClearCellsCS, 8, 8) +//- Prepare shade +ComputeShader2D(V_PrepareShadeCS, 8, 8) +{ + V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; + RWTexture2D shade = G_Dereference(params.shade_rw); + Vec2 shade_idx = SV_DispatchThreadID; + if (all(shade_idx < countof(shade))) + { + // Clear shade + shade[shade_idx] = 0; + } +} + +//- Prepare cells +ComputeShader2D(V_PrepareCellsCS, 8, 8) { V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; RWTexture2D cells = G_Dereference(params.cells); - RWTexture2D stains = G_Dereference(params.stains); RWTexture2D drynesses = G_Dereference(params.drynesses); Vec2 cells_idx = SV_DispatchThreadID; if (all(cells_idx < countof(cells))) @@ -33,13 +45,6 @@ ComputeShader2D(V_ClearCellsCS, 8, 8) // Clear cell cells[cells_idx] = 0; - // Clear stain - if (params.should_clear_stains) - { - stains[cells_idx] = 0; - drynesses[cells_idx] = 0; - } - // Increase dryness f32 dry_rate = params.dt * 0.1; { @@ -47,6 +52,14 @@ ComputeShader2D(V_ClearCellsCS, 8, 8) f32 new_dryness = lerp(old_dryness, 1, dry_rate); drynesses[cells_idx] = new_dryness; } + + // Clear stain + if (params.should_clear_stains) + { + RWTexture2D stains = G_Dereference(params.stains); + stains[cells_idx] = 0; + drynesses[cells_idx] = 0; + } } } @@ -65,212 +78,212 @@ ComputeShader(V_ClearParticlesCS, 64) //////////////////////////////////////////////////////////// //~ Backdrop -ComputeShader2D(V_BackdropCS, 8, 8) -{ - V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; - RWTexture2D screen = G_Dereference(params.screen_rw); - Texture2D tiles = G_Dereference(params.tiles); - SamplerState wrap_sampler = G_Dereference(params.pt_wrap_sampler); +// ComputeShader2D(V_BackdropCS, 8, 8) +// { +// V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; +// RWTexture2D screen = G_Dereference(params.screen_rw); +// Texture2D tiles = G_Dereference(params.tiles); +// SamplerState wrap_sampler = G_Dereference(params.pt_wrap_sampler); - const Vec4 background_color_a = LinearFromSrgb(Vec4(0.30, 0.30, 0.30, 1)); - const Vec4 background_color_b = LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)); +// const Vec4 background_color_a = LinearFromSrgb(Vec4(0.30, 0.30, 0.30, 1)); +// const Vec4 background_color_b = LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)); - Vec2 screen_pos = SV_DispatchThreadID + Vec2(0.5, 0.5); +// Vec2 screen_pos = SV_DispatchThreadID + Vec2(0.5, 0.5); - Vec4 result = Vec4(0.025, 0.025, 0.025, 1); - Vec2 world_pos = mul(params.xf.screen_to_world, Vec3(screen_pos, 1)); - Vec2 cell_pos = floor(mul(params.xf.world_to_cell, Vec3(world_pos, 1))); - Vec2 tile_pos = mul(params.xf.world_to_tile, Vec3(world_pos, 1)); +// Vec4 result = Vec4(0.025, 0.025, 0.025, 1); +// Vec2 world_pos = mul(params.xf.screen_to_world, Vec3(screen_pos, 1)); +// Vec2 cell_pos = floor(mul(params.xf.world_to_cell, Vec3(world_pos, 1))); +// Vec2 tile_pos = mul(params.xf.world_to_tile, Vec3(world_pos, 1)); - P_TileKind tile = tiles.Load(Vec3(tile_pos, 0)); +// P_TileKind tile = tiles.Load(Vec3(tile_pos, 0)); - f32 half_thickness = 1; - f32 half_world_bounds_size = P_WorldPitch * 0.5; - Vec2 world_bounds_screen_p0 = mul(params.xf.world_to_screen, Vec3(-half_world_bounds_size, -half_world_bounds_size, 1)); - Vec2 world_bounds_screen_p1 = mul(params.xf.world_to_screen, Vec3(half_world_bounds_size, half_world_bounds_size, 1)); - b32 is_in_world_bounds = - screen_pos.x > (world_bounds_screen_p0.x - half_thickness) && - screen_pos.y > (world_bounds_screen_p0.y - half_thickness) && - screen_pos.x < (world_bounds_screen_p1.x + half_thickness) && - screen_pos.y < (world_bounds_screen_p1.y + half_thickness); +// f32 half_thickness = 1; +// f32 half_world_bounds_size = P_WorldPitch * 0.5; +// Vec2 world_bounds_screen_p0 = mul(params.xf.world_to_screen, Vec3(-half_world_bounds_size, -half_world_bounds_size, 1)); +// Vec2 world_bounds_screen_p1 = mul(params.xf.world_to_screen, Vec3(half_world_bounds_size, half_world_bounds_size, 1)); +// b32 is_in_world_bounds = +// screen_pos.x > (world_bounds_screen_p0.x - half_thickness) && +// screen_pos.y > (world_bounds_screen_p0.y - half_thickness) && +// screen_pos.x < (world_bounds_screen_p1.x + half_thickness) && +// screen_pos.y < (world_bounds_screen_p1.y + half_thickness); - if (is_in_world_bounds) - { - // // Checkered grid - // { - // i32 color_idx = 0; - // Vec4 colors[2] = { - // background_color_a, - // background_color_b - // }; - // const f32 checker_size = 0.5; - // Vec2 world_pos_modded = fmod(abs(world_pos), Vec2(checker_size * 2, checker_size * 2)); - // if (world_pos_modded.x < checker_size) - // { - // color_idx = !color_idx; - // } - // if (world_pos_modded.y < checker_size) - // { - // color_idx = !color_idx; - // } - // if (world_pos.x < 0) - // { - // color_idx = !color_idx; - // } - // if (world_pos.y < 0) - // { - // color_idx = !color_idx; - // } - // result = colors[color_idx]; - // } +// if (is_in_world_bounds) +// { +// // // Checkered grid +// // { +// // i32 color_idx = 0; +// // Vec4 colors[2] = { +// // background_color_a, +// // background_color_b +// // }; +// // const f32 checker_size = 0.5; +// // Vec2 world_pos_modded = fmod(abs(world_pos), Vec2(checker_size * 2, checker_size * 2)); +// // if (world_pos_modded.x < checker_size) +// // { +// // color_idx = !color_idx; +// // } +// // if (world_pos_modded.y < checker_size) +// // { +// // color_idx = !color_idx; +// // } +// // if (world_pos.x < 0) +// // { +// // color_idx = !color_idx; +// // } +// // if (world_pos.y < 0) +// // { +// // color_idx = !color_idx; +// // } +// // result = colors[color_idx]; +// // } - // Tile test - // TODO: Remove this - { - P_TileKind tile_tl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y - 1, 0)); - P_TileKind tile_tr = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y - 1, 0)); - P_TileKind tile_br = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y + 1, 0)); - P_TileKind tile_bl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y + 1, 0)); - P_TileKind tile_t = tiles.Load(Vec3(tile_pos.x, tile_pos.y - 1, 0)); - P_TileKind tile_r = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y, 0)); - P_TileKind tile_b = tiles.Load(Vec3(tile_pos.x, tile_pos.y + 1, 0)); - P_TileKind tile_l = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y, 0)); +// // Tile test +// // TODO: Remove this +// { +// P_TileKind tile_tl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y - 1, 0)); +// P_TileKind tile_tr = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y - 1, 0)); +// P_TileKind tile_br = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y + 1, 0)); +// P_TileKind tile_bl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y + 1, 0)); +// P_TileKind tile_t = tiles.Load(Vec3(tile_pos.x, tile_pos.y - 1, 0)); +// P_TileKind tile_r = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y, 0)); +// P_TileKind tile_b = tiles.Load(Vec3(tile_pos.x, tile_pos.y + 1, 0)); +// P_TileKind tile_l = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y, 0)); - f32 tile_edge_dist = Inf; - P_TileKind edge_tile = tile; - if (tile_tl != tile) { edge_tile = tile_tl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), floor(tile_pos.y)))); } - if (tile_tr != tile) { edge_tile = tile_tr; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), floor(tile_pos.y)))); } - if (tile_br != tile) { edge_tile = tile_br; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), ceil(tile_pos.y)))); } - if (tile_bl != tile) { edge_tile = tile_bl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), ceil(tile_pos.y)))); } - if (tile_l != tile) { edge_tile = tile_l; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.x)); } - if (tile_r != tile) { edge_tile = tile_r; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.x)); } - if (tile_t != tile) { edge_tile = tile_t; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.y)); } - if (tile_b != tile) { edge_tile = tile_b; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.y)); } +// f32 tile_edge_dist = Inf; +// P_TileKind edge_tile = tile; +// if (tile_tl != tile) { edge_tile = tile_tl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), floor(tile_pos.y)))); } +// if (tile_tr != tile) { edge_tile = tile_tr; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), floor(tile_pos.y)))); } +// if (tile_br != tile) { edge_tile = tile_br; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), ceil(tile_pos.y)))); } +// if (tile_bl != tile) { edge_tile = tile_bl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), ceil(tile_pos.y)))); } +// if (tile_l != tile) { edge_tile = tile_l; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.x)); } +// if (tile_r != tile) { edge_tile = tile_r; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.x)); } +// if (tile_t != tile) { edge_tile = tile_t; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.y)); } +// if (tile_b != tile) { edge_tile = tile_b; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.y)); } - if (tile == P_TileKind_Wall) - { - Vec4 outer = LinearFromSrgb(Vec4(0.05, 0.05, 0.05, 1)); - Vec4 inner = LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)); - result = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.375)); - // result = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.5)); - } - else if (tile != P_TileKind_Empty) - { - SPR_Slice slice = params.tile_slices[tile]; - Texture2D tile_tex = G_Dereference(slice.tex); - Vec4 tile_col = tile_tex.SampleLevel(wrap_sampler, world_pos, 0); +// if (tile == P_TileKind_Wall) +// { +// Vec4 outer = LinearFromSrgb(Vec4(0.05, 0.05, 0.05, 1)); +// Vec4 inner = LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)); +// result = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.375)); +// // result = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.5)); +// } +// else if (tile != P_TileKind_Empty) +// { +// SPR_Slice slice = params.tile_slices[tile]; +// Texture2D tile_tex = G_Dereference(slice.tex); +// Vec4 tile_col = tile_tex.SampleLevel(wrap_sampler, world_pos, 0); - result = tile_col; - } +// result = tile_col; +// } - // switch (tile) - // { - // default: break; +// // switch (tile) +// // { +// // default: break; - // case P_TileKind_Floor: - // { - // result = Color_Blue; - // } break; +// // case P_TileKind_Floor: +// // { +// // result = Color_Blue; +// // } break; - // case P_TileKind_Wall: - // { - // // result = Color_Red; - // result = Color_Black; - // } break; - // } - } +// // case P_TileKind_Wall: +// // { +// // // result = Color_Red; +// // result = Color_Black; +// // } break; +// // } +// } - // TODO: Remove this - // Cells test - { - RWTexture2D stains = G_Dereference(params.stains); - RWTexture2D cells = G_Dereference(params.cells); - RWTexture2D drynesses = G_Dereference(params.drynesses); +// // TODO: Remove this +// // Cells test +// { +// RWTexture2D stains = G_Dereference(params.stains); +// RWTexture2D cells = G_Dereference(params.cells); +// RWTexture2D drynesses = G_Dereference(params.drynesses); - Vec4 stain = stains.Load(cell_pos); - Vec4 cell = cells.Load(cell_pos); - f32 dryness = drynesses[cell_pos]; +// Vec4 stain = stains.Load(cell_pos); +// Vec4 cell = cells.Load(cell_pos); +// f32 dryness = drynesses[cell_pos]; - stain = V_DryColor(stain, dryness); +// stain = V_DryColor(stain, dryness); - // cell.rgb *= cell.a; - // stain.rgb *= stain.a; +// // cell.rgb *= cell.a; +// // stain.rgb *= stain.a; - result.rgb = (stain.rgb * stain.a) + (result.rgb * (1.0 - stain.a)); - result.a = (stain.a * 1) + (result.a * (1.0 - stain.a)); +// result.rgb = (stain.rgb * stain.a) + (result.rgb * (1.0 - stain.a)); +// result.a = (stain.a * 1) + (result.a * (1.0 - stain.a)); - result.rgb = (cell.rgb * cell.a) + (result.rgb * (1.0 - cell.a)); - result.a = (cell.a * 1) + (result.a * (1.0 - cell.a)); +// result.rgb = (cell.rgb * cell.a) + (result.rgb * (1.0 - cell.a)); +// result.a = (cell.a * 1) + (result.a * (1.0 - cell.a)); - // Vec4 cell = cells.Load(cell_pos); - // if (cell.a != 0) - // { - // result = cell; - // } - // else - // { - // Vec4 stain = stains.Load(cell_pos); - // if (stain.a != 0) - // { - // result = stain; - // } - // } - } +// // Vec4 cell = cells.Load(cell_pos); +// // if (cell.a != 0) +// // { +// // result = cell; +// // } +// // else +// // { +// // Vec4 stain = stains.Load(cell_pos); +// // if (stain.a != 0) +// // { +// // result = stain; +// // } +// // } +// } - // // TODO: Remove this - // // Cells test - // { - // RWTexture2D cells = G_Dereference(params.cells); - // Vec2 cell_pos = floor(mul(params.xf.world_to_cell, Vec3(world_pos, 1))); - // Vec4 cell = cells.Load(cell_pos); - // if (cell.a != 0) - // { - // result = cell; - // } - // else - // { - // RWTexture2D stains = G_Dereference(params.stains); - // Vec4 stain = stains.Load(cell_pos); - // if (stain.a != 0) - // { - // result = stain; - // } - // } - // } +// // // TODO: Remove this +// // // Cells test +// // { +// // RWTexture2D cells = G_Dereference(params.cells); +// // Vec2 cell_pos = floor(mul(params.xf.world_to_cell, Vec3(world_pos, 1))); +// // Vec4 cell = cells.Load(cell_pos); +// // if (cell.a != 0) +// // { +// // result = cell; +// // } +// // else +// // { +// // RWTexture2D stains = G_Dereference(params.stains); +// // Vec4 stain = stains.Load(cell_pos); +// // if (stain.a != 0) +// // { +// // result = stain; +// // } +// // } +// // } - // TODO: Remove this - // Stains test - // { - // RWTexture2D stains = G_Dereference(params.stains); - // Vec2 cell_pos = mul(params.xf.world_to_cell, Vec3(world_pos, 1)); - // V_ParticleKind stain = stains.Load(cell_pos); +// // TODO: Remove this +// // Stains test +// // { +// // RWTexture2D stains = G_Dereference(params.stains); +// // Vec2 cell_pos = mul(params.xf.world_to_cell, Vec3(world_pos, 1)); +// // V_ParticleKind stain = stains.Load(cell_pos); - // if (stain == V_ParticleKind_Test) - // { - // // result = Color_Yellow; - // // result = LinearFromSrgb(Vec4(0.5, 0.1, 0.1, 1)); - // // result = LinearFromSrgb(Vec4(0.5, 0.1, 0.1, 1)); - // } - // } +// // if (stain == V_ParticleKind_Test) +// // { +// // // result = Color_Yellow; +// // // result = LinearFromSrgb(Vec4(0.5, 0.1, 0.1, 1)); +// // // result = LinearFromSrgb(Vec4(0.5, 0.1, 0.1, 1)); +// // } +// // } - } +// } - if (all(screen_pos < countof(screen))) - { - screen[screen_pos] = result; - } -} +// if (all(screen_pos < countof(screen))) +// { +// screen[screen_pos] = result; +// } +// } //////////////////////////////////////////////////////////// //~ Quads @@ -282,7 +295,6 @@ VertexShader(V_QuadVS, V_QuadPSInput) { V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; StructuredBuffer quads = G_Dereference(params.quads); - RWTexture2D screen = G_Dereference(params.screen_rw); V_Quad quad = quads[SV_InstanceID]; @@ -292,7 +304,7 @@ VertexShader(V_QuadVS, V_QuadPSInput) Vec2 screen_pos = 0; V_QuadPSInput result; - result.sv_position = Vec4(NdcFromPos(screen_pos, countof(screen)).xy, 0, 1); + result.sv_position = Vec4(NdcFromPos(screen_pos, params.screen_dims).xy, 0, 1); result.quad_idx = SV_InstanceID; return result; } @@ -470,6 +482,7 @@ ComputeShader2D(V_ShadeCS, 8, 8) { V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; RWTexture2D shade_tex = G_Dereference(params.shade_rw); + Texture2D albedo_tex = G_Dereference(params.albedo_ro); Texture2D tiles = G_Dereference(params.tiles); SamplerState wrap_sampler = G_Dereference(params.pt_wrap_sampler); @@ -483,97 +496,94 @@ ComputeShader2D(V_ShadeCS, 8, 8) Vec2 half_world_dims = Vec2(P_WorldPitch, P_WorldPitch) * 0.5; b32 is_in_world_bounds = all(world_pos > -half_world_dims) && all(world_pos < half_world_dims); - //- Tile color - // TODO: Remove this - b32 tile_is_wall = 0; - Vec4 tile_color = 0; - if (is_in_world_bounds) + ////////////////////////////// + //- Compute albedo + + Vec4 albedo = 0; { - P_TileKind tile_tl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y - 1, 0)); - P_TileKind tile_tr = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y - 1, 0)); - P_TileKind tile_br = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y + 1, 0)); - P_TileKind tile_bl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y + 1, 0)); - P_TileKind tile_t = tiles.Load(Vec3(tile_pos.x, tile_pos.y - 1, 0)); - P_TileKind tile_r = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y, 0)); - P_TileKind tile_b = tiles.Load(Vec3(tile_pos.x, tile_pos.y + 1, 0)); - P_TileKind tile_l = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y, 0)); + //- Tile color + // TODO: Remove this + b32 tile_is_wall = 0; + Vec4 tile_color = Vec4(0.025, 0.025, 0.025, 1); + if (is_in_world_bounds) + { + P_TileKind tile_tl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y - 1, 0)); + P_TileKind tile_tr = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y - 1, 0)); + P_TileKind tile_br = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y + 1, 0)); + P_TileKind tile_bl = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y + 1, 0)); + P_TileKind tile_t = tiles.Load(Vec3(tile_pos.x, tile_pos.y - 1, 0)); + P_TileKind tile_r = tiles.Load(Vec3(tile_pos.x + 1, tile_pos.y, 0)); + P_TileKind tile_b = tiles.Load(Vec3(tile_pos.x, tile_pos.y + 1, 0)); + P_TileKind tile_l = tiles.Load(Vec3(tile_pos.x - 1, tile_pos.y, 0)); - f32 tile_edge_dist = Inf; - P_TileKind edge_tile = tile; - if (tile_tl != tile) { edge_tile = tile_tl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), floor(tile_pos.y)))); } - if (tile_tr != tile) { edge_tile = tile_tr; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), floor(tile_pos.y)))); } - if (tile_br != tile) { edge_tile = tile_br; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), ceil(tile_pos.y)))); } - if (tile_bl != tile) { edge_tile = tile_bl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), ceil(tile_pos.y)))); } - if (tile_l != tile) { edge_tile = tile_l; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.x)); } - if (tile_r != tile) { edge_tile = tile_r; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.x)); } - if (tile_t != tile) { edge_tile = tile_t; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.y)); } - if (tile_b != tile) { edge_tile = tile_b; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.y)); } + f32 tile_edge_dist = Inf; + P_TileKind edge_tile = tile; + if (tile_tl != tile) { edge_tile = tile_tl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), floor(tile_pos.y)))); } + if (tile_tr != tile) { edge_tile = tile_tr; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), floor(tile_pos.y)))); } + if (tile_br != tile) { edge_tile = tile_br; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(ceil(tile_pos.x), ceil(tile_pos.y)))); } + if (tile_bl != tile) { edge_tile = tile_bl; tile_edge_dist = min(tile_edge_dist, length(tile_pos - Vec2(floor(tile_pos.x), ceil(tile_pos.y)))); } + if (tile_l != tile) { edge_tile = tile_l; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.x)); } + if (tile_r != tile) { edge_tile = tile_r; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.x)); } + if (tile_t != tile) { edge_tile = tile_t; tile_edge_dist = min(tile_edge_dist, frac(tile_pos.y)); } + if (tile_b != tile) { edge_tile = tile_b; tile_edge_dist = min(tile_edge_dist, 1.0 - frac(tile_pos.y)); } - if (tile == P_TileKind_Wall) - { - Vec4 outer = LinearFromSrgb(Vec4(0.05, 0.05, 0.05, 1)); - Vec4 inner = LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)); - tile_color = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.375)); - // tile_color = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.5)); - tile_is_wall = 1; - } - else if (tile != P_TileKind_Empty) - { - SPR_Slice slice = params.tile_slices[tile]; - Texture2D tile_tex = G_Dereference(slice.tex); - tile_color = tile_tex.SampleLevel(wrap_sampler, world_pos, 0); - } - // Checkered grid - else if (tile == P_TileKind_Empty) - { - i32 color_idx = 0; - Vec4 colors[2] = { - LinearFromSrgb(Vec4(0.30, 0.30, 0.30, 1)), - LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)) - }; - const f32 checker_size = 0.5; - Vec2 world_pos_modded = fmod(abs(world_pos), Vec2(checker_size * 2, checker_size * 2)); - if (world_pos_modded.x < checker_size) + if (tile == P_TileKind_Wall) { - color_idx = !color_idx; + Vec4 outer = LinearFromSrgb(Vec4(0.05, 0.05, 0.05, 1)); + Vec4 inner = LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)); + tile_color = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.375)); + // tile_color = lerp(outer, inner, smoothstep(0, 1, tile_edge_dist / 0.5)); + tile_is_wall = 1; } - if (world_pos_modded.y < checker_size) + else if (tile != P_TileKind_Empty) { - color_idx = !color_idx; + SPR_Slice slice = params.tile_slices[tile]; + Texture2D tile_tex = G_Dereference(slice.tex); + tile_color = tile_tex.SampleLevel(wrap_sampler, world_pos, 0); } - if (world_pos.x < 0) + // Checkered grid + else if (tile == P_TileKind_Empty) { - color_idx = !color_idx; + i32 color_idx = 0; + Vec4 colors[2] = { + LinearFromSrgb(Vec4(0.30, 0.30, 0.30, 1)), + LinearFromSrgb(Vec4(0.15, 0.15, 0.15, 1)) + }; + const f32 checker_size = 0.5; + Vec2 world_pos_modded = fmod(abs(world_pos), Vec2(checker_size * 2, checker_size * 2)); + if (world_pos_modded.x < checker_size) + { + color_idx = !color_idx; + } + if (world_pos_modded.y < checker_size) + { + color_idx = !color_idx; + } + if (world_pos.x < 0) + { + color_idx = !color_idx; + } + if (world_pos.y < 0) + { + color_idx = !color_idx; + } + tile_color = colors[color_idx]; } - if (world_pos.y < 0) - { - color_idx = !color_idx; - } - tile_color = colors[color_idx]; } - // switch (tile) - // { - // default: break; - - // case P_TileKind_Floor: - // { - // tile_color = Color_Blue; - // } break; - - // case P_TileKind_Wall: - // { - // // tile_color = Color_Red; - // tile_color = Color_Black; - // } break; - // } + //- Composite albedo + albedo = BlendPremul(!tile_is_wall * tile_color, albedo); // Blend floor tile + albedo = BlendPremul(albedo_tex.Load(Vec3(shade_pos, 0)), albedo); + albedo = BlendPremul(tile_is_wall * tile_color, albedo); // Blend wall tile } + ////////////////////////////// + //- Compute result - //- Composite - Vec4 result = 0; - result = BlendPremul(!tile_is_wall * tile_color, result); // Blend floor tile - result = BlendPremul(tile_is_wall * tile_color, result); // Blend wall tile + Vec4 result = albedo; + + ////////////////////////////// + //- Write result if (all(shade_pos < countof(shade_tex))) { @@ -736,7 +746,7 @@ PixelShader(V_CompositePS, V_CompositePSOutput, V_CompositePSInput input) } //- Composite - Vec4 result = 0; + Vec4 result = Vec4(0, 0, 0, 1); result = BlendPremul(shade_color, result); result = BlendPremul(selection_color, result); result = BlendPremul(overlay_color, result); @@ -758,14 +768,13 @@ VertexShader(V_DVertVS, V_DVertPSInput) { V_GpuParams params = G_Dereference(V_ShaderConst_Params)[0]; StructuredBuffer verts = G_Dereference(params.shape_verts); - RWTexture2D screen = G_Dereference(params.screen_rw); V_DVert vert = verts[SV_VertexID]; Vec2 screen_pos = vert.pos; V_DVertPSInput result; - result.sv_position = Vec4(NdcFromPos(screen_pos, countof(screen)).xy, 0, 1); + result.sv_position = Vec4(NdcFromPos(screen_pos, params.screen_dims).xy, 0, 1); result.color_lin = vert.color_lin; return result; } diff --git a/src/pp/pp_vis/pp_vis_gpu.gh b/src/pp/pp_vis/pp_vis_gpu.gh index 9efe8552..eae331a4 100644 --- a/src/pp/pp_vis/pp_vis_gpu.gh +++ b/src/pp/pp_vis/pp_vis_gpu.gh @@ -50,12 +50,9 @@ Vec4 V_DryColor(Vec4 color, f32 dryness); //~ Shaders //- Utility shaders -ComputeShader2D(V_ClearCellsCS, 8, 8); +ComputeShader2D(V_PrepareCellsCS, 8, 8); ComputeShader(V_ClearParticlesCS, 64); -//- Backdrop -ComputeShader2D(V_BackdropCS, 8, 8); - //- Quads VertexShader(V_QuadVS, V_QuadPSInput); PixelShader(V_QuadPS, V_QuadPSOutput, V_QuadPSInput input); diff --git a/src/pp/pp_vis/pp_vis_shared.cgh b/src/pp/pp_vis/pp_vis_shared.cgh index d540756d..3c1cc334 100644 --- a/src/pp/pp_vis/pp_vis_shared.cgh +++ b/src/pp/pp_vis/pp_vis_shared.cgh @@ -59,21 +59,9 @@ Struct(V_GpuParams) { // TODO: Use simulation dt f32 dt; - V_Xforms xf; - - Vec2 screen_dims; - G_Texture2DRef screen_ro; - G_RWTexture2DRef screen_rw; - - Vec2 shade_dims; - G_Texture2DRef shade_ro; - G_RWTexture2DRef shade_rw; - u64 tick; u64 seed; - - G_SamplerStateRef pt_clamp_sampler; - G_SamplerStateRef pt_wrap_sampler; + V_Xforms xf; V_SelectionMode selection_mode; P_TileKind equipped_tile; @@ -90,6 +78,17 @@ Struct(V_GpuParams) Vec2 camera_pos; f32 camera_zoom; + G_SamplerStateRef pt_clamp_sampler; + G_SamplerStateRef pt_wrap_sampler; + + Vec2 screen_dims; + + Vec2 shade_dims; + G_Texture2DRef shade_ro; + G_RWTexture2DRef shade_rw; + G_Texture2DRef albedo_ro; + G_RWTexture2DRef albedo_rw; + G_Texture2DRef tiles; G_StructuredBufferRef quads; G_StructuredBufferRef shape_verts;