diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h index f1b914db..a7379fa5 100644 --- a/src/gpu/gpu.h +++ b/src/gpu/gpu.h @@ -164,6 +164,7 @@ Enum(GPU_Format) Enum(GPU_ResourceKind) { + GPU_ResourceKind_Unknown, GPU_ResourceKind_Buffer, GPU_ResourceKind_Texture1D, GPU_ResourceKind_Texture2D, @@ -173,9 +174,9 @@ Enum(GPU_ResourceKind) Enum(GPU_ResourceFlag) { - GPU_ResourceFlag_None = 0, - GPU_ResourceFlag_AllowUav = (1 << 0), - GPU_ResourceFlag_AllowRtv = (1 << 1), + GPU_ResourceFlag_None = 0, + GPU_ResourceFlag_Writable = (1 << 0), + GPU_ResourceFlag_Renderable = (1 << 1), }; Enum(GPU_HeapKind) @@ -283,6 +284,7 @@ void GPU_ReleaseResource(GPU_Resource *resource, GPU_ReleaseFlag flags); u32 GPU_GetReadableId(GPU_Resource *resource); u32 GPU_GetWritableId(GPU_Resource *resource); +u32 GPU_GetSamplerId(GPU_Resource *resource); Vec2I32 GPU_GetTextureSize2D(GPU_Resource *resource); Vec3I32 GPU_GetTextureSize3D(GPU_Resource *resource); u64 GPU_GetFootprintSize(GPU_Resource *resource); diff --git a/src/gpu/gpu_dx12/gpu_dx12.c b/src/gpu/gpu_dx12/gpu_dx12.c index 14ff0239..740d631a 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.c +++ b/src/gpu/gpu_dx12/gpu_dx12.c @@ -436,7 +436,7 @@ JobDef(GPU_D12_LoadPipeline, sig, _) } else if (ok) { - String cs = DataFromResource(desc.vs.resource); + String cs = DataFromResource(desc.cs.resource); D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc = ZI; pso_desc.pRootSignature = g->bindless_rootsig; @@ -893,6 +893,11 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) GPU_D12_SharedState *g = &GPU_D12_shared_state; GPU_D12_Resource *r = 0; + if (desc.kind == GPU_ResourceKind_Unknown) + { + Panic(Lit("Unknown gpu resource type")); + } + if (desc.kind == GPU_ResourceKind_Buffer) { desc.buffer.size = MaxU64(AlignU64Pow2(desc.buffer.size), Kibi(64)); @@ -972,8 +977,8 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) d3d_desc.MipLevels = 1; d3d_desc.SampleDesc.Count = 1; d3d_desc.SampleDesc.Quality = 0; - d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_AllowUav); - d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * !!(desc.flags & GPU_ResourceFlag_AllowRtv); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_Writable); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * !!(desc.flags & GPU_ResourceFlag_Renderable); r->state = desc.buffer.heap_kind == GPU_HeapKind_Upload ? D3D12_RESOURCE_STATE_GENERIC_READ : D3D12_RESOURCE_STATE_COPY_DEST; HRESULT hr = ID3D12Device_CreateCommittedResource(g->device, &heap_props, heap_flags, &d3d_desc, r->state, 0, &IID_ID3D12Resource, (void **)&r->d3d_resource); if (FAILED(hr)) @@ -1004,8 +1009,8 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) d3d_desc.MipLevels = 1; d3d_desc.SampleDesc.Count = 1; d3d_desc.SampleDesc.Quality = 0; - d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_AllowUav); - d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * !!(desc.flags & GPU_ResourceFlag_AllowRtv); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_Writable); + d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * !!(desc.flags & GPU_ResourceFlag_Renderable); r->state = D3D12_RESOURCE_STATE_COPY_DEST; D3D12_CLEAR_VALUE clear_value = { .Format = d3d_desc.Format, .Color = { 0 } }; D3D12_CLEAR_VALUE *clear_value_ptr = d3d_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET ? &clear_value : 0; @@ -1019,6 +1024,52 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) /* TODO: Sampler */ } + + /* Create texture srv descriptor */ + if (desc.kind == GPU_ResourceKind_Texture1D + || desc.kind == GPU_ResourceKind_Texture2D + || desc.kind == GPU_ResourceKind_Texture3D) + { + r->srv_descriptor = GPU_D12_AcquireCpuDescriptor(g->cbv_srv_uav_heap); + ID3D12Device_CreateShaderResourceView(g->device, r->d3d_resource, 0, r->srv_descriptor->handle); + } + + /* Create buffer srv descriptor */ + if (desc.kind == GPU_ResourceKind_Buffer + && desc.buffer.heap_kind != GPU_HeapKind_Download + && desc.buffer.element_size > 0) + { + D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = ZI; + srv_desc.Format = DXGI_FORMAT_UNKNOWN; + srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; + srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + srv_desc.Buffer.FirstElement = 0; + srv_desc.Buffer.NumElements = MaxU32(desc.buffer.element_count, 1); + srv_desc.Buffer.StructureByteStride = desc.buffer.element_size; + srv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE; + r->srv_descriptor = GPU_D12_AcquireCpuDescriptor(g->cbv_srv_uav_heap); + ID3D12Device_CreateShaderResourceView(g->device, r->d3d_resource, &srv_desc, r->srv_descriptor->handle); + } + + /* Create uav descriptor */ + if (desc.flags & GPU_ResourceFlag_Writable) + { + r->uav_descriptor = GPU_D12_AcquireCpuDescriptor(g->cbv_srv_uav_heap); + ID3D12Device_CreateUnorderedAccessView(g->device, r->d3d_resource, 0, 0, r->uav_descriptor->handle); + } + + /* Create rtv descriptor */ + if (desc.flags & GPU_ResourceFlag_Renderable) + { + r->rtv_descriptor = GPU_D12_AcquireCpuDescriptor(g->rtv_heap); + ID3D12Device_CreateRenderTargetView(g->device, r->d3d_resource, 0, r->rtv_descriptor->handle); + } + + /* Create sampler descriptor */ + if (desc.kind == GPU_ResourceKind_Sampler) + { + /* TODO */ + } } r->desc = desc; @@ -1045,6 +1096,23 @@ void GPU_ReleaseResource(GPU_Resource *gpu_resource, GPU_ReleaseFlag flags) /* TODO: Sampler */ } + if (r->srv_descriptor) + { + GPU_D12_ReleaseCpuDescriptor(r->srv_descriptor); + } + if (r->uav_descriptor) + { + GPU_D12_ReleaseCpuDescriptor(r->uav_descriptor); + } + if (r->rtv_descriptor) + { + GPU_D12_ReleaseCpuDescriptor(r->uav_descriptor); + } + if (r->sampler_descriptor) + { + GPU_D12_ReleaseCpuDescriptor(r->sampler_descriptor); + } + Lock lock = LockE(&g->free_resources_mutex); r->next_free = g->first_free_resource; g->first_free_resource = r; @@ -1053,14 +1121,17 @@ void GPU_ReleaseResource(GPU_Resource *gpu_resource, GPU_ReleaseFlag flags) u32 GPU_GetReadableId(GPU_Resource *resource) { - /* TODO */ - return 0; + return ((GPU_D12_Resource *)resource)->srv_descriptor->index; } u32 GPU_GetWritableId(GPU_Resource *resource) { - /* TODO */ - return 0; + return ((GPU_D12_Resource *)resource)->uav_descriptor->index; +} + +u32 GPU_GetSamplerId(GPU_Resource *resource) +{ + return ((GPU_D12_Resource *)resource)->sampler_descriptor->index; } Vec2I32 GPU_GetTextureSize2D(GPU_Resource *gpu_resource) @@ -1125,16 +1196,10 @@ i64 GPU_EndCommandList(GPU_CommandList *gpu_cl) GPU_D12_RawCommandList *dx12_cl = GPU_D12_BeginRawCommandList(queue_kind); ID3D12GraphicsCommandList *rcl = dx12_cl->cl; - /* Set rootsigs */ - if (cl->has_rasterize_cmd) - { - ID3D12GraphicsCommandList_SetGraphicsRootSignature(rcl, g->bindless_rootsig); - } - if (cl->has_compute_cmd) - { - ID3D12GraphicsCommandList_SetComputeRootSignature(rcl, g->bindless_rootsig); - } - + b32 graphics_rootsig_set = 0; + b32 compute_rootsig_set = 0; + b32 descriptor_heaps_set = 0; + GPU_D12_Pipeline *bound_pipeline = 0; /* Process gpu commands into dx12 commands */ { @@ -1259,6 +1324,7 @@ i64 GPU_EndCommandList(GPU_CommandList *gpu_cl) case GPU_D12_CommandKind_Clear: { /* TODO */ + /* TODO: Set descriptor heaps if UAV copy */ cmd = cmd->next; } break; @@ -1325,11 +1391,38 @@ i64 GPU_EndCommandList(GPU_CommandList *gpu_cl) pipeline = GPU_D12_PipelineFromDesc(pipeline_desc); } - if (pipeline) + if (pipeline + && cmd->rasterize.index_buffer->desc.buffer.element_count > 0) { + /* Set descriptor heaps */ +#if 0 + if (!descriptor_heaps_set) + { + { + ID3D12DescriptorHeap *heap = g->cbv_srv_uav_heap; + Lock lock = LockS(&heap->mutex); + ID3D12Device_CopyDescriptorsSimple(g->device, heap->allocated_count, cdh->start_cpu_handle, dh_cpu->handle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + Unlock(&lock); + } + ID3D12DescriptorHeap *heaps[] = { cl->gpu_cbv_srv_uav_heap, cl->gpu_sampler_heap }; + ID3D12GraphicsCommandList_SetDescriptorHeaps(cl->cl, countof(heaps), heaps); + descriptor_heaps_set = 1; + } +#endif + + /* Bind rootsig */ + if (!graphics_rootsig_set) + { + ID3D12GraphicsCommandList_SetGraphicsRootSignature(rcl, g->bindless_rootsig); + graphics_rootsig_set = 1; + } + /* Bind pipeline */ - /* TODO: Only set dirty */ - ID3D12GraphicsCommandList_SetPipelineState(rcl, pipeline->pso); + if (pipeline != bound_pipeline) + { + ID3D12GraphicsCommandList_SetPipelineState(rcl, pipeline->pso); + bound_pipeline = pipeline; + } /* Fill signature */ /* TODO: Only upload dirty */ @@ -1376,25 +1469,24 @@ i64 GPU_EndCommandList(GPU_CommandList *gpu_cl) } /* Set index buffer */ + /*TODO: Only set dirty */ u32 indices_count = 0; { GPU_D12_Resource *indices = cmd->rasterize.index_buffer; - if (indices) + D3D12_INDEX_BUFFER_VIEW ibv = ZI; + ibv.BufferLocation = indices->buffer_gpu_address; + if (indices->desc.buffer.element_size == 2) { - D3D12_INDEX_BUFFER_VIEW ibv = ZI; - ibv.BufferLocation = indices->buffer_gpu_address; - if (indices->desc.buffer.element_size == 2) - { - ibv.Format = GPU_D12_DxgiFormatFromGpuFormat(DXGI_FORMAT_R16_UINT); - } - else - { - Assert(indices->desc.buffer.element_size == 4); - ibv.Format = GPU_D12_DxgiFormatFromGpuFormat(DXGI_FORMAT_R32_UINT); - } - ibv.SizeInBytes = indices->desc.buffer.element_size * indices->desc.buffer.element_count; - indices_count = indices->desc.buffer.element_count; + ibv.Format = GPU_D12_DxgiFormatFromGpuFormat(DXGI_FORMAT_R16_UINT); } + else + { + Assert(indices->desc.buffer.element_size == 4); + ibv.Format = GPU_D12_DxgiFormatFromGpuFormat(DXGI_FORMAT_R32_UINT); + } + ibv.SizeInBytes = indices->desc.buffer.element_size * indices->desc.buffer.element_count; + indices_count = indices->desc.buffer.element_count; + ID3D12GraphicsCommandList_IASetIndexBuffer(rcl, &ibv); } /* Bind render targets */ @@ -1436,9 +1528,19 @@ i64 GPU_EndCommandList(GPU_CommandList *gpu_cl) if (pipeline) { + /* Bind rootsig */ + if (!compute_rootsig_set) + { + ID3D12GraphicsCommandList_SetComputeRootSignature(rcl, g->bindless_rootsig); + compute_rootsig_set = 1; + } + /* Bind pipeline */ - /* TODO: Only set dirty */ - ID3D12GraphicsCommandList_SetPipelineState(rcl, pipeline->pso); + if (pipeline != bound_pipeline) + { + ID3D12GraphicsCommandList_SetPipelineState(rcl, pipeline->pso); + bound_pipeline = pipeline; + } /* Fill signature */ /* TODO: Only upload dirty */ @@ -1569,7 +1671,6 @@ void GPU_Rasterize_(GPU_CommandList *gpu_cl, cmd->rasterize.instances_count = instances_count; cmd->rasterize.index_buffer = (GPU_D12_Resource *)index_buffer; cmd->rasterize.mode = mode; - cl->has_rasterize_cmd = 1; } void GPU_Compute_(GPU_CommandList *gpu_cl, @@ -1590,7 +1691,6 @@ void GPU_Compute_(GPU_CommandList *gpu_cl, cmd->compute.num_threads_x = num_threads_x; cmd->compute.num_threads_y = num_threads_y; cmd->compute.num_threads_z = num_threads_z; - cl->has_compute_cmd = 1; } //////////////////////////////// diff --git a/src/gpu/gpu_dx12/gpu_dx12.h b/src/gpu/gpu_dx12/gpu_dx12.h index 952e9165..69d38fbc 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.h +++ b/src/gpu/gpu_dx12/gpu_dx12.h @@ -138,6 +138,8 @@ Struct(GPU_D12_RawCommandList) ID3D12CommandAllocator *ca; ID3D12GraphicsCommandList *cl; + ID3D12DescriptorHeap *gpu_cbv_srv_uav_heap; + ID3D12DescriptorHeap *gpu_sampler_heap; }; //////////////////////////////// @@ -223,9 +225,6 @@ Struct(GPU_D12_CommandList) u64 count; GPU_QueueKind queue_kind; - - b32 has_rasterize_cmd; - b32 has_compute_cmd; }; //////////////////////////////// diff --git a/src/pp/pp.c b/src/pp/pp.c index ce1341a6..a80feb24 100644 --- a/src/pp/pp.c +++ b/src/pp/pp.c @@ -394,7 +394,7 @@ GPU_Resource *AcquireGbuffer(GPU_Format format, Vec2I32 size) __prof; GPU_ResourceDesc desc = ZI; desc.kind = GPU_ResourceKind_Texture2D; - desc.flags = GPU_ResourceFlag_AllowUav | GPU_ResourceFlag_AllowRtv; + desc.flags = GPU_ResourceFlag_Writable | GPU_ResourceFlag_Renderable; desc.texture.format = format; desc.texture.size = VEC3I32(size.x, size.y, 1); desc.texture.mip_levels = 1; diff --git a/src/pp/pp_draw.gpu b/src/pp/pp_draw.gpu index a0e2084c..425ef89b 100644 --- a/src/pp/pp_draw.gpu +++ b/src/pp/pp_draw.gpu @@ -458,6 +458,11 @@ UiShapePS_Input VSDef(UiShapeVS, Semantic(u32, SV_VertexID)) UiShapePS_Output PSDef(UiShapePS, UiShapePS_Input input) { UiShapePS_Output output; + /* FIXME: Enable this */ +#if 0 output.SV_Target = input.color_srgb; +#else + output.SV_Target = Vec4(1, 0, 0, 1); +#endif return output; }