cpu -> texture upload fixes

This commit is contained in:
jacob 2025-12-17 17:38:18 -06:00
parent d9898eeab8
commit 60608fc3cd
4 changed files with 39 additions and 34 deletions

View File

@ -2434,10 +2434,11 @@ void G_CopyCpuToTexture(G_CommandListHandle cl_handle, G_ResourceHandle dst_hand
Assert(dst->is_texture); Assert(dst->is_texture);
/* Grab footprint info */ /* Grab footprint info */
u64 footprint_rows_count = 0; u64 staging_footprint_rows_count = 0;
u64 footprint_row_size = 0; u64 staging_footprint_row_size = 0;
u64 footprint_size = 0; u64 staging_footprint_row_pitch = 0;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint = Zi; u64 staging_footprint_size = 0;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT staging_footprint = Zi;
{ {
D3D12_RESOURCE_DESC src_desc = 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.Height = staged_dims.y;
src_desc.DepthOrArraySize = staged_dims.z; 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_D12_Resource *staging_resource = staging_region->ring->resource;
G_ResourceHandle staging_resource_handle = G_D12_MakeHandle(G_ResourceHandle, staging_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 */ /* Fill staging buffer */
{ {
D3D12_RANGE read_range = Zi; u8 *src_base = (u8 *)src + (src_copy_range.p0.y * src_row_pitch) + (src_copy_range.p0.x * bytes_per_texel);
u8 *src_base = src; u8 *staged_base = (u8 *)staging_region->ring->base + staging_footprint.Offset;
u8 *dst_base = (u8 *)staging_region->ring->base + footprint.Offset; u64 src_z_pitch = src_row_pitch * src_dims.y;
u32 z_size = footprint_row_size * footprint_rows_count; u64 staged_z_pitch = staging_footprint_row_size * staging_footprint_rows_count;
for (i32 z = 0; z < src_dims.z; ++z) for (i32 z = 0; z < src_dims.z; ++z)
{ {
u32 z_offset = z * z_size; u64 src_z_offset = z * src_z_pitch;
for (i32 y = 0; y < footprint_rows_count; ++y) 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 *src_row = src_base + y * src_row_pitch + src_z_offset;
u8 *dst_row = dst_base + y * footprint.Footprint.RowPitch + z_offset; u8 *staged_row = staged_base + y * staging_footprint_row_pitch + staged_z_offset;
CopyBytes(dst_row, src_row, footprint_row_size); 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( G_CopyBufferToTexture(
cl_handle, cl_handle,
dst_handle, dst_copy_range, dst_handle, dst_copy_range,
staging_resource_handle, footprint.Offset staging_resource_handle, staging_footprint.Offset
); );
} }
} }

View File

@ -65,8 +65,9 @@ S_Key S_RandKey(void)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Tile helpers //~ 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) switch (placement.placement_kind)
{ {
case S_TilePlacementKind_Range: case S_TilePlacementKind_Range:
@ -75,15 +76,17 @@ void S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement)
Rng2I32 range = placement.range; Rng2I32 range = placement.range;
for (i32 tile_y = range.p0.y; tile_y < range.p1.y; ++tile_y) 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); Vec2I32 tile_pos = VEC2I32(tile_x, tile_y);
i32 tile_idx = S_TileIdxFromTilePos(tile_pos); i32 tile_idx = S_TileIdxFromTilePos(tile_pos);
tiles[tile_idx] = (u8)tile; tiles[tile_idx] = (u8)tile;
} }
} }
dirty_rect = range;
} break; } break;
} }
return dirty_rect;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -238,7 +238,7 @@ S_Key S_RandKey(void);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Tile helpers //~ Tile helpers
void S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement); Rng2I32 S_UpdateTilesInPlaceFromPlacement(u8 *tiles, S_TilePlacement placement);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Shape helpers //~ Shape helpers

View File

@ -264,8 +264,6 @@ void V_TickForever(WaveLaneCtx *lane)
//- Update tiles from sim //- 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) for (S_SnapshotNode *n = sim_output->first_snapshot_node; n; n = n->next)
{ {
S_Snapshot *snapshot = &n->snapshot; 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) 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_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.placement_kind = S_TilePlacementKind_Range;
cmd->tile_placement.tile_kind = S_TileKind_Floor; cmd->tile_placement.tile_kind = S_TileKind_Floor;
cmd->tile_placement.range.p0 = VEC2I32(100, 100); 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; } break;
} }
} }