diff --git a/gfx/common/d3d12_common.c b/gfx/common/d3d12_common.c index 65899722d8..4fd20d3e49 100644 --- a/gfx/common/d3d12_common.c +++ b/gfx/common/d3d12_common.c @@ -15,6 +15,8 @@ #define CINTERFACE +#include + #include "d3d12_common.h" #include "dxgi_common.h" #include "d3dcompiler_common.h" @@ -254,40 +256,91 @@ static void d3d12_init_descriptor_heap(D3D12Device device, d3d12_descriptor_heap out->cpu = D3D12GetCPUDescriptorHandleForHeapStart(out->handle); out->gpu = D3D12GetGPUDescriptorHandleForHeapStart(out->handle); out->stride = D3D12GetDescriptorHandleIncrementSize(device, out->desc.Type); + out->map = (bool*)calloc(out->desc.NumDescriptors, sizeof(bool)); +} + +static inline void d3d12_release_descriptor_heap(d3d12_descriptor_heap_t* heap) +{ + free(heap->map); + Release(heap->handle); +} + +static D3D12_CPU_DESCRIPTOR_HANDLE d3d12_descriptor_heap_slot_alloc(d3d12_descriptor_heap_t* heap) +{ + int i; + D3D12_CPU_DESCRIPTOR_HANDLE handle = { 0 }; + + for (i = heap->start; i < heap->desc.NumDescriptors; i++) + { + if (!heap->map[i]) + { + heap->map[i] = true; + handle.ptr = heap->cpu.ptr + i * heap->stride; + heap->start = i + 1; + return handle; + } + } + /* if you get here try increasing NumDescriptors for this heap */ + assert(0); + return handle; +} + +static void +d3d12_descriptor_heap_slot_free(d3d12_descriptor_heap_t* heap, D3D12_CPU_DESCRIPTOR_HANDLE handle) +{ + int i; + + if (!handle.ptr) + return; + + assert(((handle.ptr - heap->cpu.ptr) % heap->stride) == 0); + + i = (handle.ptr - heap->cpu.ptr) / heap->stride; + assert(i >= 0 && i < heap->desc.NumDescriptors); + assert(heap->map[i]); + + heap->map[i] = false; + if (heap->start > i) + heap->start = i; +} + +bool d3d12_create_root_signature( + D3D12Device device, D3D12_ROOT_SIGNATURE_DESC* desc, D3D12RootSignature* out) +{ + D3DBlob signature; + D3DBlob error; + D3D12SerializeRootSignature(desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error); + + if (error) + { + RARCH_ERR( + "[D3D12]: CreateRootSignature failed : %s", (const char*)D3DGetBufferPointer(error)); + Release(error); + return false; + } + + D3D12CreateRootSignature( + device, 0, D3DGetBufferPointer(signature), D3DGetBufferSize(signature), out); + Release(signature); + + return true; } bool d3d12_init_descriptors(d3d12_video_t* d3d12) { - D3D12_ROOT_SIGNATURE_DESC desc; - static const D3D12_DESCRIPTOR_RANGE srv_table[] = { - { - D3D12_DESCRIPTOR_RANGE_TYPE_SRV, - 1, - 0, - 0, - D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, - }, - }; - static const D3D12_DESCRIPTOR_RANGE sampler_table[] = { - { - D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, - 1, - 0, - 0, - D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, - }, - }; - - D3D12_ROOT_PARAMETER rootParameters[ROOT_ID_MAX]; + D3D12_ROOT_SIGNATURE_DESC desc; + D3D12_DESCRIPTOR_RANGE srv_tbl[] = { { D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 } }; + D3D12_DESCRIPTOR_RANGE sampler_tbl[] = { { D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1 } }; + D3D12_ROOT_PARAMETER rootParameters[ROOT_ID_MAX]; rootParameters[ROOT_ID_TEXTURE_T].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; - rootParameters[ROOT_ID_TEXTURE_T].DescriptorTable.NumDescriptorRanges = countof(srv_table); - rootParameters[ROOT_ID_TEXTURE_T].DescriptorTable.pDescriptorRanges = srv_table; + rootParameters[ROOT_ID_TEXTURE_T].DescriptorTable.NumDescriptorRanges = countof(srv_tbl); + rootParameters[ROOT_ID_TEXTURE_T].DescriptorTable.pDescriptorRanges = srv_tbl; rootParameters[ROOT_ID_TEXTURE_T].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; rootParameters[ROOT_ID_SAMPLER_T].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; - rootParameters[ROOT_ID_SAMPLER_T].DescriptorTable.NumDescriptorRanges = countof(sampler_table); - rootParameters[ROOT_ID_SAMPLER_T].DescriptorTable.pDescriptorRanges = sampler_table; + rootParameters[ROOT_ID_SAMPLER_T].DescriptorTable.NumDescriptorRanges = countof(sampler_tbl); + rootParameters[ROOT_ID_SAMPLER_T].DescriptorTable.pDescriptorRanges = sampler_tbl; rootParameters[ROOT_ID_SAMPLER_T].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; rootParameters[ROOT_ID_UBO].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; @@ -301,33 +354,19 @@ bool d3d12_init_descriptors(d3d12_video_t* d3d12) desc.pStaticSamplers = NULL; desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; - { - D3DBlob signature; - D3DBlob error; - D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error); + d3d12_create_root_signature(d3d12->device, &desc, &d3d12->desc.rootSignature); - if (error) - { - RARCH_ERR( - "[D3D12]: CreateRootSignature failed :\n%s\n", - (const char*)D3DGetBufferPointer(error)); - Release(error); - return false; - } - - D3D12CreateRootSignature( - d3d12->device, 0, D3DGetBufferPointer(signature), D3DGetBufferSize(signature), - &d3d12->desc.rootSignature); - Release(signature); - } + srv_tbl[0].NumDescriptors = SLANG_NUM_BINDINGS; + sampler_tbl[0].NumDescriptors = SLANG_NUM_BINDINGS; + d3d12_create_root_signature(d3d12->device, &desc, &d3d12->desc.sl_rootSignature); d3d12->desc.rtv_heap.desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; - d3d12->desc.rtv_heap.desc.NumDescriptors = countof(d3d12->chain.renderTargets); + d3d12->desc.rtv_heap.desc.NumDescriptors = countof(d3d12->chain.renderTargets) + GFX_MAX_SHADERS; d3d12->desc.rtv_heap.desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; d3d12_init_descriptor_heap(d3d12->device, &d3d12->desc.rtv_heap); d3d12->desc.srv_heap.desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - d3d12->desc.srv_heap.desc.NumDescriptors = SRV_HEAP_SLOT_MAX; + d3d12->desc.srv_heap.desc.NumDescriptors = 256; d3d12->desc.srv_heap.desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; d3d12_init_descriptor_heap(d3d12->device, &d3d12->desc.srv_heap); @@ -339,6 +378,16 @@ bool d3d12_init_descriptors(d3d12_video_t* d3d12) return true; } +static INLINE D3D12_GPU_DESCRIPTOR_HANDLE + d3d12_create_sampler(D3D12Device device, D3D12_SAMPLER_DESC* desc, d3d12_descriptor_heap_t* heap) +{ + D3D12_CPU_DESCRIPTOR_HANDLE cpu_handle = d3d12_descriptor_heap_slot_alloc(heap); + D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle = { cpu_handle.ptr - heap->cpu.ptr + heap->gpu.ptr }; + + D3D12CreateSampler(device, desc, cpu_handle); + return gpu_handle; +} + void d3d12_init_samplers(d3d12_video_t* d3d12) { int i; @@ -371,15 +420,13 @@ void d3d12_init_samplers(d3d12_video_t* d3d12) desc.AddressV = desc.AddressU; desc.AddressW = desc.AddressU; - desc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; - d3d12->samplers[RARCH_FILTER_LINEAR][i] = d3d12_create_sampler( - d3d12->device, &desc, &d3d12->desc.sampler_heap, - 0 * RARCH_WRAP_MAX + i); + desc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; + d3d12->samplers[RARCH_FILTER_LINEAR][i] = + d3d12_create_sampler(d3d12->device, &desc, &d3d12->desc.sampler_heap); - desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; - d3d12->samplers[RARCH_FILTER_NEAREST][i] = d3d12_create_sampler( - d3d12->device, &desc, &d3d12->desc.sampler_heap, - 1 * RARCH_WRAP_MAX + i); + desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; + d3d12->samplers[RARCH_FILTER_NEAREST][i] = + d3d12_create_sampler(d3d12->device, &desc, &d3d12->desc.sampler_heap); } } @@ -469,14 +516,21 @@ d3d12_create_buffer(D3D12Device device, UINT size_in_bytes, D3D12Resource* buffe return D3D12GetGPUVirtualAddress(*buffer); } -void d3d12_init_texture( - D3D12Device device, - d3d12_descriptor_heap_t* heap, - descriptor_heap_slot_t heap_index, - d3d12_texture_t* texture) +void d3d12_release_texture(d3d12_texture_t* texture) { + if (texture->srv_heap) + d3d12_descriptor_heap_slot_free(texture->srv_heap, texture->cpu_descriptor); + Release(texture->handle); Release(texture->upload_buffer); +} +void d3d12_init_texture(D3D12Device device, d3d12_texture_t* texture) +{ + bool is_render_target = texture->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + D3D12_FORMAT_SUPPORT1 format_support = + D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE; + + d3d12_release_texture(texture); { D3D12_HEAP_PROPERTIES heap_props = { D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, @@ -486,21 +540,40 @@ void d3d12_init_texture( texture->desc.DepthOrArraySize = 1; texture->desc.MipLevels = 1; texture->desc.SampleDesc.Count = 1; + texture->desc.Format = d3d12_get_closest_match(device, texture->desc.Format, format_support); D3D12CreateCommittedResource( device, &heap_props, D3D12_HEAP_FLAG_NONE, &texture->desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, NULL, &texture->handle); } - D3D12GetCopyableFootprints( - device, &texture->desc, 0, 1, 0, &texture->layout, &texture->num_rows, - &texture->row_size_in_bytes, &texture->total_bytes); + if (texture->srv_heap) + { + D3D12_SHADER_RESOURCE_VIEW_DESC view_desc = { texture->desc.Format }; + view_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + view_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + view_desc.Texture2D.MipLevels = texture->desc.MipLevels; + + texture->cpu_descriptor = d3d12_descriptor_heap_slot_alloc(texture->srv_heap); + D3D12CreateShaderResourceView(device, texture->handle, &view_desc, texture->cpu_descriptor); + texture->gpu_descriptor.ptr = + texture->cpu_descriptor.ptr - texture->srv_heap->cpu.ptr + texture->srv_heap->gpu.ptr; + } + + if (is_render_target) + { + } + else { D3D12_HEAP_PROPERTIES heap_props = { D3D12_HEAP_TYPE_UPLOAD, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_UNKNOWN, 1, 1 }; D3D12_RESOURCE_DESC buffer_desc = { D3D12_RESOURCE_DIMENSION_BUFFER }; + D3D12GetCopyableFootprints( + device, &texture->desc, 0, 1, 0, &texture->layout, &texture->num_rows, + &texture->row_size_in_bytes, &texture->total_bytes); + buffer_desc.Width = texture->total_bytes; buffer_desc.Height = 1; buffer_desc.DepthOrArraySize = 1; @@ -512,23 +585,29 @@ void d3d12_init_texture( device, &heap_props, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, &texture->upload_buffer); } - - { - D3D12_CPU_DESCRIPTOR_HANDLE handle; - D3D12_SHADER_RESOURCE_VIEW_DESC view_desc = { DXGI_FORMAT_UNKNOWN }; - - view_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; - view_desc.Format = texture->desc.Format; - view_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; - view_desc.Texture2D.MipLevels = texture->desc.MipLevels; - - handle.ptr = heap->cpu.ptr + heap_index * heap->stride; - - D3D12CreateShaderResourceView(device, texture->handle, &view_desc, handle); - texture->gpu_descriptor.ptr = heap->gpu.ptr + heap_index * heap->stride; - } } +void d3d12_update_texture( + int width, + int height, + int pitch, + DXGI_FORMAT format, + const void* data, + d3d12_texture_t* texture) +{ + uint8_t* dst; + D3D12_RANGE read_range = { 0, 0 }; + + D3D12Map(texture->upload_buffer, 0, &read_range, (void**)&dst); + + dxgi_copy( + width, height, format, pitch, data, texture->desc.Format, + texture->layout.Footprint.RowPitch, dst + texture->layout.Offset); + + D3D12Unmap(texture->upload_buffer, 0, NULL); + + texture->dirty = true; +} void d3d12_upload_texture(D3D12GraphicsCommandList cmd, d3d12_texture_t* texture) { D3D12_TEXTURE_COPY_LOCATION src = { 0 }; diff --git a/gfx/common/d3d12_common.h b/gfx/common/d3d12_common.h index e372306a06..77a6a159d1 100644 --- a/gfx/common/d3d12_common.h +++ b/gfx/common/d3d12_common.h @@ -101,11 +101,13 @@ static INLINE ULONG D3D12ReleaseResource(void* resource) static INLINE HRESULT D3D12Map(void* resource, UINT subresource, D3D12_RANGE* read_range, void** data) { - return ((ID3D12Resource*)resource)->lpVtbl->Map((ID3D12Resource*)resource, subresource, read_range, data); + return ((ID3D12Resource*)resource) + ->lpVtbl->Map((ID3D12Resource*)resource, subresource, read_range, data); } static INLINE void D3D12Unmap(void* resource, UINT subresource, D3D12_RANGE* written_range) { - ((ID3D12Resource*)resource)->lpVtbl->Unmap((ID3D12Resource*)resource, subresource, written_range); + ((ID3D12Resource*)resource) + ->lpVtbl->Unmap((ID3D12Resource*)resource, subresource, written_range); } static INLINE D3D12_GPU_VIRTUAL_ADDRESS D3D12GetGPUVirtualAddress(void* resource) { @@ -121,7 +123,8 @@ static INLINE HRESULT D3D12WriteToSubresource( { return ((ID3D12Resource*)resource) ->lpVtbl->WriteToSubresource( - (ID3D12Resource*)resource, dst_subresource, dst_box, src_data, src_row_pitch, src_depth_pitch); + (ID3D12Resource*)resource, dst_subresource, dst_box, src_data, src_row_pitch, + src_depth_pitch); } static INLINE HRESULT D3D12ReadFromSubresource( void* resource, @@ -133,7 +136,8 @@ static INLINE HRESULT D3D12ReadFromSubresource( { return ((ID3D12Resource*)resource) ->lpVtbl->ReadFromSubresource( - (ID3D12Resource*)resource, dst_data, dst_row_pitch, dst_depth_pitch, src_subresource, src_box); + (ID3D12Resource*)resource, dst_data, dst_row_pitch, dst_depth_pitch, src_subresource, + src_box); } static INLINE HRESULT D3D12GetHeapProperties( void* resource, D3D12_HEAP_PROPERTIES* heap_properties, D3D12_HEAP_FLAGS* heap_flags) @@ -639,8 +643,7 @@ static INLINE UINT D3D12GetNodeCount(D3D12Device device) static INLINE HRESULT D3D12CreateCommandQueue( D3D12Device device, D3D12_COMMAND_QUEUE_DESC* desc, ID3D12CommandQueue** out) { - return device->lpVtbl->CreateCommandQueue( - device, desc, uuidof(ID3D12CommandQueue), (void**)out); + return device->lpVtbl->CreateCommandQueue(device, desc, uuidof(ID3D12CommandQueue), (void**)out); } static INLINE HRESULT D3D12CreateCommandAllocator( D3D12Device device, D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator** out) @@ -1207,8 +1210,7 @@ static INLINE void D3D12ExecuteGraphicsCommandLists( static INLINE HRESULT DXGIGetSwapChainBuffer(DXGISwapChain swapchain, UINT buffer, D3D12Resource* surface) { - return swapchain->lpVtbl->GetBuffer( - swapchain, buffer, uuidof(ID3D12Resource), (void**)surface); + return swapchain->lpVtbl->GetBuffer(swapchain, buffer, uuidof(ID3D12Resource), (void**)surface); } static INLINE void D3D12SetDescriptorHeaps( D3D12GraphicsCommandList command_list, @@ -1289,7 +1291,8 @@ typedef struct D3D12_CPU_DESCRIPTOR_HANDLE cpu; /* descriptor */ D3D12_GPU_DESCRIPTOR_HANDLE gpu; /* descriptor */ UINT stride; - UINT count; + bool* map; + int start; } d3d12_descriptor_heap_t; typedef struct @@ -1297,15 +1300,19 @@ typedef struct D3D12Resource handle; D3D12Resource upload_buffer; D3D12_RESOURCE_DESC desc; + D3D12_CPU_DESCRIPTOR_HANDLE cpu_descriptor; D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor; D3D12_GPU_DESCRIPTOR_HANDLE sampler; D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout; UINT num_rows; UINT64 row_size_in_bytes; UINT64 total_bytes; + d3d12_descriptor_heap_t* srv_heap; bool dirty; } d3d12_texture_t; +#define TEXTURE_DESC_SLOTS_COUNT 128 + typedef struct { unsigned cur_mon_id; @@ -1325,9 +1332,10 @@ typedef struct struct { - D3D12RootSignature rootSignature; /* descriptor layout */ - d3d12_descriptor_heap_t srv_heap; /* ShaderResouceView descritor heap */ - d3d12_descriptor_heap_t rtv_heap; /* RenderTargetView descritor heap */ + D3D12RootSignature sl_rootSignature; /* descriptor layout */ + D3D12RootSignature rootSignature; /* descriptor layout */ + d3d12_descriptor_heap_t srv_heap; /* ShaderResouceView descritor heap */ + d3d12_descriptor_heap_t rtv_heap; /* RenderTargetView descritor heap */ d3d12_descriptor_heap_t sampler_heap; } desc; @@ -1357,9 +1365,9 @@ typedef struct struct { - D3D12Resource vbo; - D3D12_VERTEX_BUFFER_VIEW vbo_view; - d3d12_texture_t texture; + D3D12Resource vbo; + D3D12_VERTEX_BUFFER_VIEW vbo_view; + d3d12_texture_t texture; float alpha; bool enabled; @@ -1370,7 +1378,7 @@ typedef struct { D3D12PipelineState pipe; D3D12PipelineState pipe_font; - D3D12Resource vbo; + D3D12Resource vbo; int offset; int capacity; bool enabled; @@ -1392,21 +1400,13 @@ typedef struct #endif } d3d12_video_t; -typedef enum -{ +typedef enum { ROOT_ID_TEXTURE_T = 0, ROOT_ID_SAMPLER_T, ROOT_ID_UBO, ROOT_ID_MAX, } root_signature_parameter_index_t; -typedef enum { - SRV_HEAP_SLOT_FRAME_TEXTURE = 0, - SRV_HEAP_SLOT_MENU_TEXTURE, - SRV_HEAP_SLOT_CUSTOM, - SRV_HEAP_SLOT_MAX = 16 -} descriptor_heap_slot_t; - RETRO_BEGIN_DECLS extern D3D12_RENDER_TARGET_BLEND_DESC d3d12_blend_enable_desc; @@ -1431,11 +1431,15 @@ bool d3d12_init_queue(d3d12_video_t* d3d12); D3D12_GPU_VIRTUAL_ADDRESS d3d12_create_buffer(D3D12Device device, UINT size_in_bytes, D3D12Resource* buffer); -void d3d12_init_texture( - D3D12Device device, - d3d12_descriptor_heap_t* heap, - descriptor_heap_slot_t heap_index, - d3d12_texture_t* tex); +void d3d12_init_texture(D3D12Device device, d3d12_texture_t* tex); + +void d3d12_update_texture( + int width, + int height, + int pitch, + DXGI_FORMAT format, + const void* data, + d3d12_texture_t* texture); void d3d12_upload_texture(D3D12GraphicsCommandList cmd, d3d12_texture_t* texture); @@ -1445,7 +1449,7 @@ void d3d12_create_fullscreen_quad_vbo( DXGI_FORMAT d3d12_get_closest_match( D3D12Device device, DXGI_FORMAT desired_format, D3D12_FORMAT_SUPPORT1 desired_format_support); -#if !defined(__cplusplus ) || defined(CINTERFACE) +#if !defined(__cplusplus) || defined(CINTERFACE) static INLINE void d3d12_resource_transition( D3D12GraphicsCommandList cmd, D3D12Resource resource, @@ -1473,52 +1477,13 @@ d3d12_set_sampler(D3D12GraphicsCommandList cmd, D3D12_GPU_DESCRIPTOR_HANDLE samp D3D12SetGraphicsRootDescriptorTable(cmd, ROOT_ID_SAMPLER_T, sampler); } -static INLINE void d3d12_set_texture_and_sampler(D3D12GraphicsCommandList cmd, const d3d12_texture_t* texture) +static INLINE void +d3d12_set_texture_and_sampler(D3D12GraphicsCommandList cmd, const d3d12_texture_t* texture) { D3D12SetGraphicsRootDescriptorTable(cmd, ROOT_ID_TEXTURE_T, texture->gpu_descriptor); D3D12SetGraphicsRootDescriptorTable(cmd, ROOT_ID_SAMPLER_T, texture->sampler); } -static INLINE void d3d12_update_texture( - int width, - int height, - int pitch, - DXGI_FORMAT format, - const void* data, - d3d12_texture_t* texture) -{ - uint8_t* dst; - D3D12_RANGE read_range = { 0, 0 }; - - D3D12Map(texture->upload_buffer, 0, &read_range, (void**)&dst); - - dxgi_copy( - width, height, format, pitch, data, texture->desc.Format, - texture->layout.Footprint.RowPitch, dst + texture->layout.Offset); - - D3D12Unmap(texture->upload_buffer, 0, NULL); - - texture->dirty = true; -} - -static INLINE DXGI_FORMAT -d3d12_get_closest_match_texture2D(D3D12Device device, DXGI_FORMAT desired_format) -{ - return d3d12_get_closest_match( - device, desired_format, - D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE); -} - -static INLINE D3D12_GPU_DESCRIPTOR_HANDLE d3d12_create_sampler( - D3D12Device device, D3D12_SAMPLER_DESC* desc, d3d12_descriptor_heap_t* heap, int slot) -{ - D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle = {heap->gpu.ptr + slot * heap->stride}; - D3D12_CPU_DESCRIPTOR_HANDLE cpu_handle = {heap->cpu.ptr + slot * heap->stride}; - - D3D12CreateSampler(device, desc, cpu_handle); - return gpu_handle; -} - #endif RETRO_END_DECLS diff --git a/gfx/drivers/d3d12.c b/gfx/drivers/d3d12.c index 0a2515c72f..b21cfa61ef 100644 --- a/gfx/drivers/d3d12.c +++ b/gfx/drivers/d3d12.c @@ -114,9 +114,9 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) if (!d3d_compile(shader, sizeof(shader), NULL, "PSMain", "ps_5_0", &ps_code)) goto error; - desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; - desc.InputLayout.pInputElementDescs = inputElementDesc; - desc.InputLayout.NumElements = countof(inputElementDesc); + desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + desc.InputLayout.pInputElementDescs = inputElementDesc; + desc.InputLayout.NumElements = countof(inputElementDesc); if (!d3d12_init_pipeline( d3d12->device, vs_code, ps_code, NULL, &desc, @@ -157,9 +157,9 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) if (!d3d_compile(shader, sizeof(shader), NULL, "GSMain", "gs_5_0", &gs_code)) goto error; - desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT; - desc.InputLayout.pInputElementDescs = inputElementDesc; - desc.InputLayout.NumElements = countof(inputElementDesc); + desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT; + desc.InputLayout.pInputElementDescs = inputElementDesc; + desc.InputLayout.NumElements = countof(inputElementDesc); if (!d3d12_init_pipeline( d3d12->device, vs_code, ps_code, gs_code, &desc, &d3d12->sprites.pipe)) @@ -186,27 +186,27 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) { static const char simple_snow[] = #include "d3d_shaders/simple_snow_sm4.hlsl.h" - ; + ; static const char snow[] = #include "d3d_shaders/snow_sm4.hlsl.h" - ; + ; static const char bokeh[] = #include "d3d_shaders/bokeh_sm4.hlsl.h" - ; + ; static const char snowflake[] = #include "d3d_shaders/snowflake_sm4.hlsl.h" - ; + ; D3D12_INPUT_ELEMENT_DESC inputElementDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(d3d12_vertex_t, position), - D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, + D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(d3d12_vertex_t, texcoord), - D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, + D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, }; - desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; - desc.InputLayout.pInputElementDescs = inputElementDesc; - desc.InputLayout.NumElements = countof(inputElementDesc); + desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + desc.InputLayout.pInputElementDescs = inputElementDesc; + desc.InputLayout.NumElements = countof(inputElementDesc); if (!d3d_compile(simple_snow, sizeof(simple_snow), NULL, "VSMain", "vs_5_0", &vs_code)) goto error; @@ -214,8 +214,7 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) goto error; if (!d3d12_init_pipeline( - d3d12->device, vs_code, ps_code, NULL, &desc, - &d3d12->pipes[VIDEO_SHADER_MENU_3])) + d3d12->device, vs_code, ps_code, NULL, &desc, &d3d12->pipes[VIDEO_SHADER_MENU_3])) goto error; Release(vs_code); @@ -229,8 +228,7 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) goto error; if (!d3d12_init_pipeline( - d3d12->device, vs_code, ps_code, NULL, &desc, - &d3d12->pipes[VIDEO_SHADER_MENU_4])) + d3d12->device, vs_code, ps_code, NULL, &desc, &d3d12->pipes[VIDEO_SHADER_MENU_4])) goto error; Release(vs_code); @@ -244,8 +242,7 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) goto error; if (!d3d12_init_pipeline( - d3d12->device, vs_code, ps_code, NULL, &desc, - &d3d12->pipes[VIDEO_SHADER_MENU_5])) + d3d12->device, vs_code, ps_code, NULL, &desc, &d3d12->pipes[VIDEO_SHADER_MENU_5])) goto error; Release(vs_code); @@ -259,8 +256,7 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) goto error; if (!d3d12_init_pipeline( - d3d12->device, vs_code, ps_code, NULL, &desc, - &d3d12->pipes[VIDEO_SHADER_MENU_6])) + d3d12->device, vs_code, ps_code, NULL, &desc, &d3d12->pipes[VIDEO_SHADER_MENU_6])) goto error; Release(vs_code); @@ -272,13 +268,14 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) { static const char ribbon[] = #include "d3d_shaders/ribbon_sm4.hlsl.h" - ; + ; static const char ribbon_simple[] = #include "d3d_shaders/ribbon_simple_sm4.hlsl.h" - ; + ; D3D12_INPUT_ELEMENT_DESC inputElementDesc[] = { - { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, + D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, }; desc.BlendState.RenderTarget[0].SrcBlend = D3D12_BLEND_ONE; @@ -293,8 +290,7 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) goto error; if (!d3d12_init_pipeline( - d3d12->device, vs_code, ps_code, NULL, &desc, - &d3d12->pipes[VIDEO_SHADER_MENU])) + d3d12->device, vs_code, ps_code, NULL, &desc, &d3d12->pipes[VIDEO_SHADER_MENU])) goto error; Release(vs_code); @@ -308,8 +304,7 @@ static bool d3d12_gfx_init_pipelines(d3d12_video_t* d3d12) goto error; if (!d3d12_init_pipeline( - d3d12->device, vs_code, ps_code, NULL, &desc, - &d3d12->pipes[VIDEO_SHADER_MENU_2])) + d3d12->device, vs_code, ps_code, NULL, &desc, &d3d12->pipes[VIDEO_SHADER_MENU_2])) goto error; Release(vs_code); @@ -340,12 +335,18 @@ static void d3d12_gfx_free(void* data) Release(d3d12->menu.texture.handle); Release(d3d12->menu.texture.upload_buffer); - Release(d3d12->ubo); + free(d3d12->desc.sampler_heap.map); + free(d3d12->desc.srv_heap.map); + free(d3d12->desc.rtv_heap.map); Release(d3d12->desc.sampler_heap.handle); Release(d3d12->desc.srv_heap.handle); Release(d3d12->desc.rtv_heap.handle); + + Release(d3d12->desc.sl_rootSignature); Release(d3d12->desc.rootSignature); + Release(d3d12->ubo); + for (i = 0; i < GFX_MAX_SHADERS; i++) Release(d3d12->pipes[i]); @@ -418,8 +419,7 @@ d3d12_gfx_init(const video_info_t* video, const input_driver_t** input, void** i d3d12->keep_aspect = video->force_aspect; d3d12->chain.vsync = video->vsync; d3d12->format = video->rgb32 ? DXGI_FORMAT_B8G8R8X8_UNORM : DXGI_FORMAT_B5G6R5_UNORM; - d3d12->frame.texture.desc.Format = - d3d12_get_closest_match_texture2D(d3d12->device, d3d12->format); + d3d12->frame.texture.desc.Format = d3d12->format; d3d12->ubo_view.SizeInBytes = sizeof(math_matrix_4x4); d3d12->ubo_view.BufferLocation = @@ -514,9 +514,8 @@ static bool d3d12_gfx_frame( { d3d12->frame.texture.desc.Width = width; d3d12->frame.texture.desc.Height = height; - d3d12_init_texture( - d3d12->device, &d3d12->desc.srv_heap, SRV_HEAP_SLOT_FRAME_TEXTURE, - &d3d12->frame.texture); + d3d12->frame.texture.srv_heap = &d3d12->desc.srv_heap; + d3d12_init_texture(d3d12->device, &d3d12->frame.texture); } d3d12_update_texture(width, height, pitch, d3d12->format, frame, &d3d12->frame.texture); @@ -660,9 +659,9 @@ static void d3d12_set_menu_texture_frame( { d3d12->menu.texture.desc.Width = width; d3d12->menu.texture.desc.Height = height; - d3d12->menu.texture.desc.Format = d3d12_get_closest_match_texture2D(d3d12->device, format); - d3d12_init_texture( - d3d12->device, &d3d12->desc.srv_heap, SRV_HEAP_SLOT_MENU_TEXTURE, &d3d12->menu.texture); + d3d12->menu.texture.desc.Format = format; + d3d12->menu.texture.srv_heap = &d3d12->desc.srv_heap; + d3d12_init_texture(d3d12->device, &d3d12->menu.texture); } d3d12_update_texture(width, height, pitch, format, frame, &d3d12->menu.texture);