From 60608fc3cd8aad99193dde9afafc11121154e030 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 17 Dec 2025 17:38:18 -0600 Subject: [PATCH] cpu -> texture upload fixes --- src/gpu/gpu_dx12/gpu_dx12_core.c | 40 ++++++++++++++++++-------------- src/pp/pp_sim/pp_sim_core.c | 7 ++++-- src/pp/pp_sim/pp_sim_core.h | 2 +- src/pp/pp_vis/pp_vis_core.c | 24 ++++++++----------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/gpu/gpu_dx12/gpu_dx12_core.c b/src/gpu/gpu_dx12/gpu_dx12_core.c index 95ad92a2..45230e3a 100644 --- a/src/gpu/gpu_dx12/gpu_dx12_core.c +++ b/src/gpu/gpu_dx12/gpu_dx12_core.c @@ -2434,10 +2434,11 @@ void G_CopyCpuToTexture(G_CommandListHandle cl_handle, G_ResourceHandle dst_hand Assert(dst->is_texture); /* Grab footprint info */ - u64 footprint_rows_count = 0; - u64 footprint_row_size = 0; - u64 footprint_size = 0; - D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint = Zi; + u64 staging_footprint_rows_count = 0; + u64 staging_footprint_row_size = 0; + u64 staging_footprint_row_pitch = 0; + u64 staging_footprint_size = 0; + D3D12_PLACED_SUBRESOURCE_FOOTPRINT staging_footprint = Zi; { D3D12_RESOURCE_DESC src_desc = Zi; { @@ -2446,28 +2447,33 @@ void G_CopyCpuToTexture(G_CommandListHandle cl_handle, G_ResourceHandle dst_hand src_desc.Height = staged_dims.y; src_desc.DepthOrArraySize = staged_dims.z; } - ID3D12Device_GetCopyableFootprints(g->device, &src_desc, 0, 1, 0, &footprint, (u32 *)&footprint_rows_count, &footprint_row_size, &footprint_size); + ID3D12Device_GetCopyableFootprints(g->device, &src_desc, 0, 1, 0, &staging_footprint, (u32 *)&staging_footprint_rows_count, &staging_footprint_row_size, &staging_footprint_size); + staging_footprint_row_pitch = staging_footprint.Footprint.RowPitch; } - G_D12_StagingRegionNode *staging_region = G_D12_PushStagingRegion(cl, footprint_size); + i32 bytes_per_texel = staging_footprint_row_size / staged_dims.x; + u64 src_row_pitch = src_dims.x * bytes_per_texel; + + G_D12_StagingRegionNode *staging_region = G_D12_PushStagingRegion(cl, staging_footprint_size); G_D12_Resource *staging_resource = staging_region->ring->resource; G_ResourceHandle staging_resource_handle = G_D12_MakeHandle(G_ResourceHandle, staging_resource); - footprint.Offset = staging_region->pos; + staging_footprint.Offset = staging_region->pos; /* Fill staging buffer */ { - D3D12_RANGE read_range = Zi; - u8 *src_base = src; - u8 *dst_base = (u8 *)staging_region->ring->base + footprint.Offset; - u32 z_size = footprint_row_size * footprint_rows_count; + u8 *src_base = (u8 *)src + (src_copy_range.p0.y * src_row_pitch) + (src_copy_range.p0.x * bytes_per_texel); + u8 *staged_base = (u8 *)staging_region->ring->base + staging_footprint.Offset; + u64 src_z_pitch = src_row_pitch * src_dims.y; + u64 staged_z_pitch = staging_footprint_row_size * staging_footprint_rows_count; for (i32 z = 0; z < src_dims.z; ++z) { - u32 z_offset = z * z_size; - for (i32 y = 0; y < footprint_rows_count; ++y) + u64 src_z_offset = z * src_z_pitch; + u64 staged_z_offset = z * staged_z_pitch; + for (i32 y = 0; y < staging_footprint_rows_count; ++y) { - u8 *src_row = src_base + y * footprint_row_size + z_offset; - u8 *dst_row = dst_base + y * footprint.Footprint.RowPitch + z_offset; - CopyBytes(dst_row, src_row, footprint_row_size); + u8 *src_row = src_base + y * src_row_pitch + src_z_offset; + u8 *staged_row = staged_base + y * staging_footprint_row_pitch + staged_z_offset; + CopyBytes(staged_row, src_row, staging_footprint_row_size); } } } @@ -2480,7 +2486,7 @@ void G_CopyCpuToTexture(G_CommandListHandle cl_handle, G_ResourceHandle dst_hand G_CopyBufferToTexture( cl_handle, dst_handle, dst_copy_range, - staging_resource_handle, footprint.Offset + staging_resource_handle, staging_footprint.Offset ); } } diff --git a/src/pp/pp_sim/pp_sim_core.c b/src/pp/pp_sim/pp_sim_core.c index ca8cf219..1ac75db1 100644 --- a/src/pp/pp_sim/pp_sim_core.c +++ b/src/pp/pp_sim/pp_sim_core.c @@ -65,8 +65,9 @@ S_Key S_RandKey(void) //////////////////////////////////////////////////////////// //~ Tile helpers -void S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement) +Rng2I32 S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement) { + Rng2I32 dirty_rect = Zi; switch (placement.placement_kind) { case S_TilePlacementKind_Range: @@ -75,15 +76,17 @@ void S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement) Rng2I32 range = placement.range; for (i32 tile_y = range.p0.y; tile_y < range.p1.y; ++tile_y) { - for (i32 tile_x = range.p0.y; tile_x < range.p1.y; ++tile_x) + for (i32 tile_x = range.p0.x; tile_x < range.p1.x; ++tile_x) { Vec2I32 tile_pos = VEC2I32(tile_x, tile_y); i32 tile_idx = S_TileIdxFromTilePos(tile_pos); tiles[tile_idx] = (u8)tile; } } + dirty_rect = range; } break; } + return dirty_rect; } //////////////////////////////////////////////////////////// diff --git a/src/pp/pp_sim/pp_sim_core.h b/src/pp/pp_sim/pp_sim_core.h index 7d5569d2..c3a6ea55 100644 --- a/src/pp/pp_sim/pp_sim_core.h +++ b/src/pp/pp_sim/pp_sim_core.h @@ -238,7 +238,7 @@ S_Key S_RandKey(void); //////////////////////////////////////////////////////////// //~ Tile helpers -void S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement); +Rng2I32 S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement); //////////////////////////////////////////////////////////// //~ Shape helpers diff --git a/src/pp/pp_vis/pp_vis_core.c b/src/pp/pp_vis/pp_vis_core.c index d3665587..8d980494 100644 --- a/src/pp/pp_vis/pp_vis_core.c +++ b/src/pp/pp_vis/pp_vis_core.c @@ -264,8 +264,6 @@ void V_TickForever(WaveLaneCtx *lane) //- Update tiles from sim { - /* TODO: Only upload dirty rects to gpu tile map */ - b32 is_dirty = 0; for (S_SnapshotNode *n = sim_output->first_snapshot_node; n; n = n->next) { S_Snapshot *snapshot = &n->snapshot; @@ -273,21 +271,17 @@ void V_TickForever(WaveLaneCtx *lane) { for (u64 placement_idx = 0; placement_idx < snapshot->tile_placements_count; ++placement_idx) { - is_dirty = 1; S_TilePlacement placement = snapshot->tile_placements[placement_idx]; - S_UpdateTilesInPlaceFromPlacement(tiles, placement); + Rng2I32 dirty_rect = S_UpdateTilesInPlaceFromPlacement(tiles, placement); + G_CopyCpuToTexture( + frame->cl, + gpu_tiles, VEC3I32(dirty_rect.p0.x, dirty_rect.p0.y, 0), + tiles, VEC3I32(tiles_dims.x, tiles_dims.y, 1), + RNG3I32(VEC3I32(dirty_rect.p0.x, dirty_rect.p0.y, 0), VEC3I32(dirty_rect.p1.x, dirty_rect.p1.y, 1)) + ); } } } - if (is_dirty) - { - G_CopyCpuToTexture( - frame->cl, - gpu_tiles, VEC3I32(0, 0, 0), - tiles, VEC3I32(tiles_dims.x, tiles_dims.y, 1), - RNG3I32(VEC3I32(0, 0, 0), VEC3I32(tiles_dims.x, tiles_dims.y, 1)) - ); - } } ////////////////////////////// @@ -655,7 +649,9 @@ void V_TickForever(WaveLaneCtx *lane) cmd->tile_placement.placement_kind = S_TilePlacementKind_Range; cmd->tile_placement.tile_kind = S_TileKind_Floor; cmd->tile_placement.range.p0 = VEC2I32(100, 100); - cmd->tile_placement.range.p1 = VEC2I32(200, 200); + cmd->tile_placement.range.p1 = VEC2I32(110, 110); + // cmd->tile_placement.range.p0 = VEC2I32(2, 2); + // cmd->tile_placement.range.p1 = VEC2I32(5, 5); } break; } }