diff --git a/src/gpu/gpu_common.c b/src/gpu/gpu_common.c index 6ad27090..fb74e570 100644 --- a/src/gpu/gpu_common.c +++ b/src/gpu/gpu_common.c @@ -21,7 +21,7 @@ void GPU_StartupCommon(void) { u16 quad_data[6] = { 0, 1, 2, 0, 2, 3 }; GPU_ResourceHandle quad_indices = GPU_PushBuffer(gpu_perm, u16, countof(quad_data)); - GPU_CopyCpuBytes(cl, quad_indices, 0, quad_data, RNGU64(0, sizeof(quad_data))); + GPU_CopyCpuToBuffer(cl, quad_indices, 0, quad_data, RNGU64(0, sizeof(quad_data))); g->quad_indices.resource = quad_indices; g->quad_indices.index_size = sizeof(quad_data[0]); g->quad_indices.index_count = countof(quad_data); @@ -29,6 +29,21 @@ void GPU_StartupCommon(void) /* TODO: Init noise texture */ { + String noise_data = DataFromResource(ResourceKeyFromStore(&GPU_Resources, Lit("noise_128x128x64_16.dat"))); + Vec3I32 noise_dims = VEC3I32(128, 128, 64); + if (noise_data.len != noise_dims.x * noise_dims.y * noise_dims.z * 2) + { + Panic(Lit("Unexpected noise texture size")); + } + GPU_ResourceHandle noise_tex = GPU_PushTexture3D(gpu_perm, + GPU_Format_R16_Uint, + noise_dims, + GPU_Layout_AnyQueue_ShaderRead_CopyRead_CopyWrite_Present); + GPU_CopyCpuToTexture(cl, + noise_tex, VEC3I32(0, 0, 0), + noise_data.text, noise_dims, + RNG3I32(VEC3I32(0, 0, 0), noise_dims)); + g->noise_tex = GPU_PushTexture3DHandle(gpu_perm, noise_tex); } } GPU_CommitCommandList(cl); diff --git a/src/gpu/gpu_core.h b/src/gpu/gpu_core.h index 0b777f2a..68726951 100644 --- a/src/gpu/gpu_core.h +++ b/src/gpu/gpu_core.h @@ -221,16 +221,16 @@ Enum(GPU_Layout) { GPU_Layout_NoChange, - GPU_Layout_Undefined, /* D3D12_BARRIER_LAYOUT_UNDEFINED */ - - /* Allows a resource to be used on any queue with any access type, so long + /* Allows a resource to be used on any queue with any access type, as long * as there is only one writer at a time, and the writer is not writing to * any texels currently being read. * * Resources cannot transition to/from this layout. They must be created * with it and are locked to it. */ - GPU_Layout_Simultaneous, + GPU_Layout_Simultaneous, /* D3D12_BARRIER_LAYOUT_COMMON + D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS */ + + GPU_Layout_Undefined, /* D3D12_BARRIER_LAYOUT_UNDEFINED */ ////////////////////////////// //- Queue-agnostic @@ -238,7 +238,7 @@ Enum(GPU_Layout) GPU_Layout_AnyQueue_ShaderRead_CopyRead_CopyWrite_Present, /* D3D12_BARRIER_LAYOUT_COMMON */ ////////////////////////////// - //- Direct & Compute queue specific + //- Direct & Compute queue GPU_Layout_DirectComputeQueue_ShaderRead_CopyRead, /* D3D12_BARRIER_LAYOUT_GENERIC_READ */ @@ -247,7 +247,7 @@ Enum(GPU_Layout) GPU_Layout_DirectComputeQueue_CopyRead, /* D3D12_BARRIER_LAYOUT_COPY_SOURCE */ ////////////////////////////// - //- Direct queue specific + //- Direct queue GPU_Layout_DirectQueue_ShaderRead_ShaderReadWrite_CopyRead_CopyWrite, /* D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COMMON */ GPU_Layout_DirectQueue_ShaderRead_CopyRead_DepthStencilRead, /* D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ */ @@ -261,7 +261,7 @@ Enum(GPU_Layout) GPU_Layout_DirectQueue_RenderTargetWrite, /* D3D12_BARRIER_LAYOUT_RENDER_TARGET */ ////////////////////////////// - //- Compute queue specific + //- Compute queue GPU_Layout_ComputeQueue_ShaderRead_ShaderReadWrite_CopyRead_CopyWrite, /* D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COMMON */ GPU_Layout_ComputeQueue_ShaderRead_CopyRead, /* D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_GENERIC_READ */ @@ -586,13 +586,15 @@ void GPU_ResetArena(GPU_CommandListHandle cl, GPU_ArenaHandle arena); //- Cpu -> Gpu copy -void GPU_CopyCpuBytes(GPU_CommandListHandle cl, GPU_ResourceHandle dst, u64 dst_offset, void *cpu_src, RngU64 cpu_src_copy_range); -void GPU_CopyCpuTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst, Vec3I32 dst_offset, void *cpu_src, Vec3I32 cpu_src_dims, Rng3I32 cpu_src_copy_range); +void GPU_CopyCpuToBuffer(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, u64 dst_offset, void *src, RngU64 src_copy_range); +void GPU_CopyCpuToTexture(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, void *src, Vec3I32 src_dims, Rng3I32 src_copy_range); //- Gpu <-> Gpu copy -void GPU_CopyBytes(GPU_CommandListHandle cl, GPU_ResourceHandle dst, u64 dst_offset, GPU_ResourceHandle src, RngU64 src_copy_range); -void GPU_CopyTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst, Vec3I32 dst_offset, GPU_ResourceHandle src, Rng3I32 src_copy_range); +void GPU_CopyBufferToBuffer(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, u64 dst_offset, GPU_ResourceHandle src_handle, RngU64 src_copy_range); +void GPU_CopyBufferToTexture(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Vec3I32 src_dims, Rng3I32 src_copy_range); +void GPU_CopyTextureToTexture(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Rng3I32 src_copy_range); +void GPU_CopyTextureToBuffer(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Rng3I32 src_copy_range); //- Constant diff --git a/src/gpu/gpu_dx12/gpu_dx12.c b/src/gpu/gpu_dx12/gpu_dx12.c index 4e228643..3918a91f 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.c +++ b/src/gpu/gpu_dx12/gpu_dx12.c @@ -382,6 +382,7 @@ D3D12_BARRIER_LAYOUT GPU_D12_BarrierLayoutFromLayout(GPU_Layout layout) { PERSIST Readonly D3D12_BARRIER_LAYOUT translate[] = { [GPU_Layout_Undefined] = D3D12_BARRIER_LAYOUT_UNDEFINED, + [GPU_Layout_Simultaneous] = D3D12_BARRIER_LAYOUT_COMMON, [GPU_Layout_AnyQueue_ShaderRead_CopyRead_CopyWrite_Present] = D3D12_BARRIER_LAYOUT_COMMON, [GPU_Layout_DirectComputeQueue_ShaderReadWrite] = D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS, [GPU_Layout_DirectComputeQueue_ShaderRead_CopyRead] = D3D12_BARRIER_LAYOUT_GENERIC_READ, @@ -899,8 +900,9 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) d3d_desc.MipLevels = (desc.flags & GPU_ResourceFlag_MaxMipLevels) ? 0 : MaxI32(desc.texture.mip_levels, 1); d3d_desc.SampleDesc.Count = 1; d3d_desc.SampleDesc.Quality = 0; - d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * AnyBit(desc.flags, GPU_ResourceFlag_Writable); - d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * AnyBit(desc.flags, GPU_ResourceFlag_Rasterizable); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * AnyBit(desc.flags, GPU_ResourceFlag_Writable); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * AnyBit(desc.flags, GPU_ResourceFlag_Rasterizable); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS * (desc.initial_layout == GPU_Layout_Simultaneous); r->state = D3D12_RESOURCE_STATE_COMMON; D3D12_CLEAR_VALUE clear_value = { .Format = d3d_desc.Format, .Color = { 0 } }; clear_value.Color[0] = desc.clear_color.x; @@ -1250,7 +1252,8 @@ GPU_ResourceHandle GPU_PushBufferEx(GPU_ArenaHandle arena_handle, GPU_BufferDesc /* FIXME: Dynamic size */ D3D12_HEAP_DESC d3d_desc = ZI; d3d_desc.SizeInBytes = Mebi(64); - d3d_desc.Flags = D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; /* TODO: Remove this and support tier 1 resource heaps */ + d3d_desc.Flags |= D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; + d3d_desc.Flags |= D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; /* TODO: Remove this and support tier 1 resource heaps */ d3d_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT; ID3D12Heap *heap = 0; @@ -1333,7 +1336,8 @@ GPU_ResourceHandle GPU_PushTextureEx(GPU_ArenaHandle arena_handle, GPU_TextureDe /* FIXME: Dynamic size */ D3D12_HEAP_DESC d3d_desc = ZI; d3d_desc.SizeInBytes = Mebi(64); - d3d_desc.Flags = D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; /* TODO: Remove this and support tier 1 resource heaps */ + d3d_desc.Flags |= D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; + d3d_desc.Flags |= D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; /* TODO: Remove this and support tier 1 resource heaps */ d3d_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT; ID3D12Heap *heap = 0; @@ -1353,7 +1357,7 @@ GPU_ResourceHandle GPU_PushTextureEx(GPU_ArenaHandle arena_handle, GPU_TextureDe { D3D12_RESOURCE_DESC1 d3d_desc = ZI; d3d_desc.Dimension = desc.kind == GPU_TextureKind_1D ? D3D12_RESOURCE_DIMENSION_TEXTURE1D : - GPU_TextureKind_2D ? D3D12_RESOURCE_DIMENSION_TEXTURE2D : + desc.kind == GPU_TextureKind_2D ? D3D12_RESOURCE_DIMENSION_TEXTURE2D : D3D12_RESOURCE_DIMENSION_TEXTURE3D; d3d_desc.Format = GPU_D12_DxgiFormatFromGpuFormat(desc.format); d3d_desc.Width = MaxI32(desc.dims.x, 1); @@ -2148,76 +2152,55 @@ void GPU_CommitCommandListEx(GPU_CommandListHandle cl_handle, u64 fence_ops_coun } break; //- Copy bytes + case GPU_D12_CmdKind_CopyBytes: { + u64 src_offset = cmd->copy_bytes.src_copy_range.min; u64 copy_size = cmd->copy_bytes.src_copy_range.max - cmd->copy_bytes.src_copy_range.min; ID3D12GraphicsCommandList_CopyBufferRegion(d3d_cl, cmd->copy_bytes.dst->d3d_resource, cmd->copy_bytes.dst_offset, cmd->copy_bytes.src->d3d_resource, - cmd->copy_bytes.src_copy_range.min, + src_offset, copy_size); cmd_idx += 1; } break; - //- Copy resource + //- Copy texels - // case GPU_D12_CmdKind_Copy: - // { - // GPU_D12_Resource *dst = cmd->copy.dst; - // GPU_D12_Resource *src = cmd->copy.src; + case GPU_D12_CmdKind_CopyTexels: + { + GPU_D12_Resource *dst = cmd->copy_texels.dst; + GPU_D12_Resource *src = cmd->copy_texels.src; + D3D12_TEXTURE_COPY_LOCATION dst_loc = cmd->copy_texels.dst_loc; + D3D12_TEXTURE_COPY_LOCATION src_loc = cmd->copy_texels.src_loc; + Vec3I32 dst_offset = cmd->copy_texels.dst_offset; + Rng3I32 src_copy_range = cmd->copy_texels.src_copy_range; - // D3D12_RESOURCE_DESC dst_desc = ZI; - // D3D12_RESOURCE_DESC src_desc = ZI; - // ID3D12Resource_GetDesc(dst->d3d_resource, &dst_desc); - // ID3D12Resource_GetDesc(src->d3d_resource, &src_desc); + D3D12_BOX src_box = ZI; + { + src_box.left = src_copy_range.p0.x; + src_box.top = src_copy_range.p0.y; + src_box.front = src_copy_range.p0.z; + src_box.right = src_copy_range.p1.x; + src_box.bottom = src_copy_range.p1.y; + src_box.back = src_copy_range.p1.z; + } - // if (dst_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && src_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) - // { /* Copy buffer -> buffer */ - // u64 dst_len = dst->desc.buffer.count * dst->desc.buffer.stride; - // u64 src_len = src->desc.buffer.count * src->desc.buffer.stride; - // u64 cpy_len = MinU64(dst_len, src_len); - // if (cpy_len > 0) - // { - // ID3D12GraphicsCommandList_CopyBufferRegion(d3d_cl, dst->d3d_resource, 0, src->d3d_resource, 0, cpy_len); - // /* Implicit promotion */ - // if (dst->state == D3D12_RESOURCE_STATE_COMMON) dst->state = D3D12_RESOURCE_STATE_COPY_DEST; - // if (src->state == D3D12_RESOURCE_STATE_COMMON) src->state = D3D12_RESOURCE_STATE_COPY_SOURCE; - // } - // } - // else if (src_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) - // { /* Copy buffer -> texture */ - // D3D12_PLACED_SUBRESOURCE_FOOTPRINT dst_placed_footprint = ZI; - // ID3D12Device_GetCopyableFootprints(g->device, &dst_desc, 0, 1, 0, &dst_placed_footprint, 0, 0, 0); + if (dst->flags & GPU_ResourceFlag_AllowDepthStencil) + { + /* Depth-stencil textures must have src box & dst offset set to 0 + * https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist-copytextureregion + */ + ID3D12GraphicsCommandList_CopyTextureRegion(d3d_cl, &dst_loc, 0, 0, 0, &src_loc, 0); + } + else + { + ID3D12GraphicsCommandList_CopyTextureRegion(d3d_cl, &dst_loc, dst_offset.x, dst_offset.y, dst_offset.z, &src_loc, &src_box); + } - // D3D12_TEXTURE_COPY_LOCATION dst_loc = ZI; - // dst_loc.pResource = dst->d3d_resource; - // dst_loc.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; - // dst_loc.SubresourceIndex = 0; - - // D3D12_TEXTURE_COPY_LOCATION src_loc = ZI; - // src_loc.pResource = src->d3d_resource; - // src_loc.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; - // src_loc.PlacedFootprint = dst_placed_footprint; - - // ID3D12GraphicsCommandList_CopyTextureRegion(d3d_cl, &dst_loc, 0, 0, 0, &src_loc, 0); - // /* Implicit promotion */ - // if (dst->state == D3D12_RESOURCE_STATE_COMMON) dst->state = D3D12_RESOURCE_STATE_COPY_DEST; - // if (src->state == D3D12_RESOURCE_STATE_COMMON) src->state = D3D12_RESOURCE_STATE_COPY_SOURCE; - // } - // else if (dst_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) - // { /* Copy texture -> buffer */ - // /* TODO */ - // Assert(0); - // } - // else if (dst_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER && src_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER) - // { /* Copy texture -> texture */ - // /* TODO */ - // Assert(0); - // } - - // cmd_idx += 1; - // } break; + cmd_idx += 1; + } break; //- Compute @@ -2542,83 +2525,77 @@ void GPU_ResetArena(GPU_CommandListHandle cl_handle, GPU_ArenaHandle arena_handl //- Cpu -> Gpu copy -void GPU_CopyCpuBytes(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, u64 dst_offset, void *src, RngU64 src_copy_range) +void GPU_CopyCpuToBuffer(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, u64 dst_offset, void *src, RngU64 src_copy_range) { GPU_D12_CmdList *cl = GPU_D12_CmdListFromHandle(cl_handle); - u64 size = src_copy_range.max - src_copy_range.min; - GPU_D12_StagingRegionNode *region = GPU_D12_PushStagingRegion(cl, size); - CopyBytes((u8 *)region->heap->mapped + region->pos, (u8 *)src + src_copy_range.min, size); - GPU_CopyBytes(cl_handle, dst_handle, dst_offset, GPU_D12_MakeHandle(GPU_ResourceHandle, ®ion->heap->resource), RNGU64(region->pos, region->pos + size)); + u64 copy_size = src_copy_range.max - src_copy_range.min; + GPU_D12_StagingRegionNode *region = GPU_D12_PushStagingRegion(cl, copy_size); + CopyBytes((u8 *)region->heap->mapped + region->pos, (u8 *)src + src_copy_range.min, copy_size); + GPU_CopyBufferToBuffer(cl_handle, + dst_handle, + dst_offset, + GPU_D12_MakeHandle(GPU_ResourceHandle, ®ion->heap->resource), + RNGU64(region->pos, copy_size)); } -void GPU_CopyCpuTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, void *cpu_src, Vec3I32 cpu_src_dims, Rng3I32 cpu_src_copy_range) +void GPU_CopyCpuToTexture(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, void *src, Vec3I32 src_dims, Rng3I32 src_copy_range) { - /* TODO */ + GPU_D12_SharedState *g = &GPU_D12_shared_state; + GPU_D12_CmdList *cl = GPU_D12_CmdListFromHandle(cl_handle); + GPU_D12_Resource *dst = GPU_D12_ResourceFromHandle(dst_handle); + Assert(dst->is_texture); + Vec3I32 staged_dims = ZI; + { + staged_dims.x = src_copy_range.p1.x - src_copy_range.p0.x; + staged_dims.y = src_copy_range.p1.y - src_copy_range.p0.y; + staged_dims.z = src_copy_range.p1.z - src_copy_range.p0.z; + } + /* Grab footprint info */ + u64 footprint_rows_count = 0; + u64 footprint_row_size = 0; + u64 footprint_size = 0; + D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint = ZI; + { + D3D12_RESOURCE_DESC src_desc = ZI; + { + ID3D12Resource_GetDesc(dst->d3d_resource, &src_desc); + src_desc.Width = staged_dims.x; + 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); + } + /* Fill staging buffer */ + GPU_D12_StagingRegionNode *region = GPU_D12_PushStagingRegion(cl, footprint_size); + { + D3D12_RANGE read_range = ZI; + u8 *dst_base = (u8 *)region->heap->mapped + region->pos + footprint.Offset; + u8 *src_base = src; + u32 z_size = footprint_row_size * 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) + { + u8 *dst_row = dst_base + y * footprint.Footprint.RowPitch + z_offset; + u8 *src_row = src_base + y * footprint_row_size + z_offset; + CopyBytes(dst_row, src_row, footprint_row_size); + } + } + } -// GPU_D12_SharedState *g = &GPU_D12_shared_state; - -// D3D12_RESOURCE_DESC desc = ZI; -// ID3D12Resource_GetDesc(((GPU_D12_Resource *)footprint_reference)->d3d_resource, &desc); - -// u64 upload_size = 0; -// u64 upload_row_size = 0; -// u32 upload_num_rows = 0; -// D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed_footprint = ZI; -// ID3D12Device_GetCopyableFootprints(g->device, &desc, 0, 1, 0, &placed_footprint, &upload_num_rows, &upload_row_size, &upload_size); -// D3D12_SUBRESOURCE_FOOTPRINT footprint = placed_footprint.Footprint; - -// { -// D3D12_RANGE read_range = ZI; -// u8 *dst_base = (u8 *)dst + placed_footprint.Offset; -// u8 *src_base = src; - -// u32 z_size = upload_row_size * upload_num_rows; - -// b32 src_overflow = 0; -// for (u32 z = 0; !src_overflow && z < desc.DepthOrArraySize; ++z) -// { -// u32 z_offset = z * z_size; -// for (u32 y = 0; !src_overflow && y < upload_num_rows; ++y) -// { -// u8 *dst_row = dst_base + y * footprint.RowPitch + z_offset; -// u8 *src_row = src_base + y * upload_row_size + z_offset; -// CopyBytes(dst_row, src_row, upload_row_size); -// } -// } -// } - - - - - - // GPU_D12_Resource *dst = GPU_D12_ResourceFromHandle(dst_handle); - - // D3D12_RESOURCE_DESC desc = ZI; - // ID3D12Resource_GetDesc(dst->d3d_resource, &desc); - - // u64 upload_size = 0; - // u64 upload_row_size = 0; - // u32 upload_num_rows = 0; - // D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed_footprint = ZI; - // ID3D12Device_GetCopyableFootprints(g->device, &desc, 0, dst->texture_mip_levels, 0, &placed_footprint, &upload_num_rows, &upload_row_size, &upload_size); - // D3D12_SUBRESOURCE_FOOTPRINT footprint = placed_footprint.Footprint; - - - - - - // void *copy_start = ((u8 *)src) + src_copy_range.min; - // u64 copy_len = src_copy_range.max - src_copy_range.min; - - + GPU_CopyBufferToTexture(cl_handle, + dst_handle, dst_offset, + GPU_D12_MakeHandle(GPU_ResourceHandle, ®ion->heap->resource), staged_dims, + RNG3I32(VEC3I32(0, 0, 0), staged_dims)); } //- Gpu <-> Gpu copy -void GPU_CopyBytes(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, u64 dst_offset, GPU_ResourceHandle src_handle, RngU64 src_copy_range) +void GPU_CopyBufferToBuffer(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, u64 dst_offset, GPU_ResourceHandle src_handle, RngU64 src_copy_range) { GPU_D12_CmdList *cl = GPU_D12_CmdListFromHandle(cl_handle); GPU_D12_Cmd *cmd = GPU_D12_PushCmd(cl); @@ -2629,9 +2606,87 @@ void GPU_CopyBytes(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handl cmd->copy_bytes.src_copy_range = src_copy_range; } -void GPU_CopyTexels(GPU_CommandListHandle cl, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Rng3I32 src_copy_range) +void GPU_CopyBufferToTexture(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Vec3I32 src_dims, Rng3I32 src_copy_range) +{ + GPU_D12_SharedState *g = &GPU_D12_shared_state; + GPU_D12_CmdList *cl = GPU_D12_CmdListFromHandle(cl_handle); + GPU_D12_Resource *dst = GPU_D12_ResourceFromHandle(dst_handle); + GPU_D12_Resource *src = GPU_D12_ResourceFromHandle(src_handle); + Assert(dst->is_texture); + Assert(!src->is_texture); + + /* Grab footprint info */ + D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint = ZI; + { + D3D12_RESOURCE_DESC src_desc = ZI; + { + ID3D12Resource_GetDesc(dst->d3d_resource, &src_desc); + src_desc.Width = src_dims.x; + src_desc.Height = src_dims.y; + src_desc.DepthOrArraySize = src_dims.z; + } + ID3D12Device_GetCopyableFootprints(g->device, &src_desc, 0, 1, 0, &footprint, 0, 0, 0); + } + + D3D12_TEXTURE_COPY_LOCATION dst_loc = ZI; + D3D12_TEXTURE_COPY_LOCATION src_loc = ZI; + { + dst_loc.pResource = dst->d3d_resource; + dst_loc.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + dst_loc.SubresourceIndex = 0; + } + { + src_loc.pResource = src->d3d_resource; + src_loc.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + src_loc.PlacedFootprint = footprint; + } + + GPU_D12_Cmd *cmd = GPU_D12_PushCmd(cl); + cmd->kind = GPU_D12_CmdKind_CopyTexels; + cmd->copy_texels.dst = dst; + cmd->copy_texels.src = src; + cmd->copy_texels.dst_loc = dst_loc; + cmd->copy_texels.src_loc = src_loc; + cmd->copy_texels.dst_offset = dst_offset; + cmd->copy_texels.src_copy_range = src_copy_range; +} + +void GPU_CopyTextureToTexture(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Rng3I32 src_copy_range) +{ + GPU_D12_SharedState *g = &GPU_D12_shared_state; + GPU_D12_CmdList *cl = GPU_D12_CmdListFromHandle(cl_handle); + GPU_D12_Resource *dst = GPU_D12_ResourceFromHandle(dst_handle); + GPU_D12_Resource *src = GPU_D12_ResourceFromHandle(src_handle); + Assert(dst->is_texture); + Assert(src->is_texture); + + D3D12_TEXTURE_COPY_LOCATION dst_loc = ZI; + D3D12_TEXTURE_COPY_LOCATION src_loc = ZI; + { + dst_loc.pResource = dst->d3d_resource; + dst_loc.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + dst_loc.SubresourceIndex = 0; + } + { + src_loc.pResource = dst->d3d_resource; + src_loc.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + src_loc.SubresourceIndex = 0; + } + + GPU_D12_Cmd *cmd = GPU_D12_PushCmd(cl); + cmd->kind = GPU_D12_CmdKind_CopyTexels; + cmd->copy_texels.dst = dst; + cmd->copy_texels.src = src; + cmd->copy_texels.dst_loc = dst_loc; + cmd->copy_texels.src_loc = src_loc; + cmd->copy_texels.dst_offset = dst_offset; + cmd->copy_texels.src_copy_range = src_copy_range; +} + +void GPU_CopyTextureToBuffer(GPU_CommandListHandle cl_handle, GPU_ResourceHandle dst_handle, Vec3I32 dst_offset, GPU_ResourceHandle src_handle, Rng3I32 src_copy_range) { /* TODO */ + Assert(0); } //- Constant diff --git a/src/gpu/gpu_dx12/gpu_dx12.h b/src/gpu/gpu_dx12/gpu_dx12.h index d91c5da4..134e34ed 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.h +++ b/src/gpu/gpu_dx12/gpu_dx12.h @@ -273,6 +273,8 @@ Struct(GPU_D12_Cmd) { GPU_D12_Resource *dst; GPU_D12_Resource *src; + D3D12_TEXTURE_COPY_LOCATION dst_loc; + D3D12_TEXTURE_COPY_LOCATION src_loc; Vec3I32 dst_offset; Rng3I32 src_copy_range; } copy_texels; diff --git a/src/proto/proto.c b/src/proto/proto.c index 83fbf722..e12f6f20 100644 --- a/src/proto/proto.c +++ b/src/proto/proto.c @@ -48,6 +48,7 @@ JobImpl(PT_RunForever, _sig, _id) GPU_SetConstant(cl, PT_ShaderConst_TestConst, 3.123); GPU_SetConstant(cl, PT_ShaderConst_BlitSampler, GPU_GetCommonPointSampler()); GPU_SetConstant(cl, PT_ShaderConst_BlitSrc, final_target_rhandle); + GPU_SetConstant(cl, PT_ShaderConst_NoiseTex, GPU_GetCommonNoise()); } /* Test pass */ diff --git a/src/proto/proto_shaders.gpu b/src/proto/proto_shaders.gpu index b10811ed..bd4bc4e0 100644 --- a/src/proto/proto_shaders.gpu +++ b/src/proto/proto_shaders.gpu @@ -53,9 +53,19 @@ PixelShader(PT_BlitPS, PT_BlitPSOutput, PT_BlitPSInput input) { SamplerState sampler = SamplerStateFromHandle(PT_ShaderConst_BlitSampler); Texture2D tex = Texture2DFromHandle(PT_ShaderConst_BlitSrc); + Texture3D noise = Texture3DFromHandle(PT_ShaderConst_NoiseTex); Vec2 uv = input.src_uv; - Vec4 result = tex.Sample(sampler, uv); + Vec4 tex_col = tex.Sample(sampler, uv); + + Vec3U32 noise_coord = 0; + noise_coord.x = uv.x * 128.0; + noise_coord.y = uv.y * 128.0; + noise_coord.z = 0; + u32 noise_val = noise[noise_coord]; + + Vec4 result = tex_col; + result.r = (f32)noise_val / (f32)U16Max; PT_BlitPSOutput output; output.SV_Target0 = result; diff --git a/src/proto/proto_shaders.h b/src/proto/proto_shaders.h index 0e7e5abd..1d5c0fca 100644 --- a/src/proto/proto_shaders.h +++ b/src/proto/proto_shaders.h @@ -1,11 +1,13 @@ //////////////////////////////////////////////////////////// //~ Constants +ShaderConstant(Texture3DHandle, PT_ShaderConst_NoiseTex, 0); + /* Test shader */ -ShaderConstant(RWTexture2DHandle, PT_ShaderConst_TestTarget, 0); -ShaderConstant(StructuredBufferHandle, PT_ShaderConst_TestBuff, 1); -ShaderConstant(f32, PT_ShaderConst_TestConst, 2); +ShaderConstant(RWTexture2DHandle, PT_ShaderConst_TestTarget, 1); +ShaderConstant(StructuredBufferHandle, PT_ShaderConst_TestBuff, 2); +ShaderConstant(f32, PT_ShaderConst_TestConst, 3); /* Blit shader */ -ShaderConstant(SamplerStateHandle, PT_ShaderConst_BlitSampler, 3); -ShaderConstant(Texture2DHandle, PT_ShaderConst_BlitSrc, 4); +ShaderConstant(SamplerStateHandle, PT_ShaderConst_BlitSampler, 4); +ShaderConstant(Texture2DHandle, PT_ShaderConst_BlitSrc, 5);