mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2024-11-23 06:15:38 +00:00
GPU: Add D3D12 renderer
This commit is contained in:
parent
5da9edceb9
commit
14e7f8fd13
|
@ -121,6 +121,20 @@ target_link_libraries(common PRIVATE glad stb Threads::Threads libchdr glslang v
|
|||
|
||||
if(WIN32)
|
||||
target_sources(common PRIVATE
|
||||
d3d12/context.cpp
|
||||
d3d12/context.h
|
||||
d3d12/descriptor_heap_manager.cpp
|
||||
d3d12/descriptor_heap_manager.h
|
||||
d3d12/shader_cache.cpp
|
||||
d3d12/shader_cache.h
|
||||
d3d12/staging_texture.cpp
|
||||
d3d12/staging_texture.h
|
||||
d3d12/stream_buffer.cpp
|
||||
d3d12/stream_buffer.h
|
||||
d3d12/texture.cpp
|
||||
d3d12/texture.h
|
||||
d3d12/util.cpp
|
||||
d3d12/util.h
|
||||
d3d11/shader_cache.cpp
|
||||
d3d11/shader_cache.h
|
||||
d3d11/shader_compiler.cpp
|
||||
|
|
|
@ -18,6 +18,13 @@
|
|||
<ClInclude Include="d3d11\staging_texture.h" />
|
||||
<ClInclude Include="d3d11\stream_buffer.h" />
|
||||
<ClInclude Include="d3d11\texture.h" />
|
||||
<ClInclude Include="d3d12\context.h" />
|
||||
<ClInclude Include="d3d12\descriptor_heap_manager.h" />
|
||||
<ClInclude Include="d3d12\shader_cache.h" />
|
||||
<ClInclude Include="d3d12\util.h" />
|
||||
<ClInclude Include="d3d12\staging_texture.h" />
|
||||
<ClInclude Include="d3d12\stream_buffer.h" />
|
||||
<ClInclude Include="d3d12\texture.h" />
|
||||
<ClInclude Include="dimensional_array.h" />
|
||||
<ClInclude Include="easing.h" />
|
||||
<ClInclude Include="error.h" />
|
||||
|
@ -98,6 +105,13 @@
|
|||
<ClCompile Include="d3d11\stream_buffer.cpp" />
|
||||
<ClCompile Include="d3d11\texture.cpp" />
|
||||
<ClCompile Include="error.cpp" />
|
||||
<ClCompile Include="d3d12\context.cpp" />
|
||||
<ClCompile Include="d3d12\descriptor_heap_manager.cpp" />
|
||||
<ClCompile Include="d3d12\shader_cache.cpp" />
|
||||
<ClCompile Include="d3d12\staging_texture.cpp" />
|
||||
<ClCompile Include="d3d12\stream_buffer.cpp" />
|
||||
<ClCompile Include="d3d12\texture.cpp" />
|
||||
<ClCompile Include="d3d12\util.cpp" />
|
||||
<ClCompile Include="event.cpp" />
|
||||
<ClCompile Include="file_system.cpp" />
|
||||
<ClCompile Include="gl\context.cpp" />
|
||||
|
|
|
@ -113,6 +113,27 @@
|
|||
<ClInclude Include="error.h" />
|
||||
<ClInclude Include="platform.h" />
|
||||
<ClInclude Include="cue_parser.h" />
|
||||
<ClInclude Include="d3d12\staging_texture.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="d3d12\stream_buffer.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="d3d12\texture.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="d3d12\context.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="d3d12\descriptor_heap_manager.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="d3d12\util.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="d3d12\shader_cache.h">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="jit_code_buffer.cpp" />
|
||||
|
@ -221,6 +242,27 @@
|
|||
<ClCompile Include="window_info.cpp" />
|
||||
<ClCompile Include="cue_parser.cpp" />
|
||||
<ClCompile Include="cd_image_ppf.cpp" />
|
||||
<ClCompile Include="d3d12\staging_texture.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="d3d12\stream_buffer.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="d3d12\texture.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="d3d12\context.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="d3d12\descriptor_heap_manager.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="d3d12\util.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="d3d12\shader_cache.cpp">
|
||||
<Filter>d3d12</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Natvis Include="bitfield.natvis" />
|
||||
|
@ -238,5 +280,8 @@
|
|||
<Filter Include="thirdparty">
|
||||
<UniqueIdentifier>{fd4150b0-6f82-4251-ab23-34c25fbc5b5e}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="d3d12">
|
||||
<UniqueIdentifier>{358e11c4-34af-4169-9a66-ec66342a6a2f}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
453
src/common/d3d12/context.cpp
Normal file
453
src/common/d3d12/context.cpp
Normal file
|
@ -0,0 +1,453 @@
|
|||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "context.h"
|
||||
#include "../assert.h"
|
||||
#include "../log.h"
|
||||
#include "../scope_guard.h"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <dxgi1_2.h>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
Log_SetChannel(D3D12::Context);
|
||||
|
||||
std::unique_ptr<D3D12::Context> g_d3d12_context;
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
|
||||
// Private D3D12 state
|
||||
static HMODULE s_d3d12_library;
|
||||
static PFN_D3D12_CREATE_DEVICE s_d3d12_create_device;
|
||||
static PFN_D3D12_GET_DEBUG_INTERFACE s_d3d12_get_debug_interface;
|
||||
static PFN_D3D12_SERIALIZE_ROOT_SIGNATURE s_d3d12_serialize_root_signature;
|
||||
|
||||
static bool LoadD3D12Library()
|
||||
{
|
||||
if (!(s_d3d12_library = LoadLibrary("d3d12.dll")) ||
|
||||
!(s_d3d12_create_device =
|
||||
reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(GetProcAddress(s_d3d12_library, "D3D12CreateDevice"))) ||
|
||||
!(s_d3d12_get_debug_interface =
|
||||
reinterpret_cast<PFN_D3D12_GET_DEBUG_INTERFACE>(GetProcAddress(s_d3d12_library, "D3D12GetDebugInterface"))) ||
|
||||
!(s_d3d12_serialize_root_signature = reinterpret_cast<PFN_D3D12_SERIALIZE_ROOT_SIGNATURE>(
|
||||
GetProcAddress(s_d3d12_library, "D3D12SerializeRootSignature"))))
|
||||
{
|
||||
Log_ErrorPrintf("d3d12.dll could not be loaded.");
|
||||
s_d3d12_create_device = nullptr;
|
||||
s_d3d12_get_debug_interface = nullptr;
|
||||
s_d3d12_serialize_root_signature = nullptr;
|
||||
if (s_d3d12_library)
|
||||
FreeLibrary(s_d3d12_library);
|
||||
s_d3d12_library = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void UnloadD3D12Library()
|
||||
{
|
||||
s_d3d12_serialize_root_signature = nullptr;
|
||||
s_d3d12_get_debug_interface = nullptr;
|
||||
s_d3d12_create_device = nullptr;
|
||||
if (s_d3d12_library)
|
||||
{
|
||||
FreeLibrary(s_d3d12_library);
|
||||
s_d3d12_library = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static const PFN_D3D12_CREATE_DEVICE s_d3d12_create_device = D3D12CreateDevice;
|
||||
static const PFN_D3D12_GET_DEBUG_INTERFACE s_d3d12_get_debug_interface = D3D12GetDebugInterface;
|
||||
static const PFN_D3D12_SERIALIZE_ROOT_SIGNATURE s_d3d12_serialize_root_signature = D3D12SerializeRootSignature;
|
||||
|
||||
static bool LoadD3D12Library()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static void UnloadD3D12Library() {}
|
||||
|
||||
#endif
|
||||
|
||||
Context::Context() = default;
|
||||
|
||||
Context::~Context()
|
||||
{
|
||||
DestroyResources();
|
||||
}
|
||||
|
||||
Context::ComPtr<ID3DBlob> Context::SerializeRootSignature(const D3D12_ROOT_SIGNATURE_DESC* desc)
|
||||
{
|
||||
ComPtr<ID3DBlob> blob;
|
||||
ComPtr<ID3DBlob> error_blob;
|
||||
const HRESULT hr = s_d3d12_serialize_root_signature(desc, D3D_ROOT_SIGNATURE_VERSION_1, blob.GetAddressOf(),
|
||||
error_blob.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("D3D12SerializeRootSignature() failed: %08X", hr);
|
||||
if (error_blob)
|
||||
Log_ErrorPrintf("%s", error_blob->GetBufferPointer());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
return blob;
|
||||
}
|
||||
|
||||
D3D12::Context::ComPtr<ID3D12RootSignature> Context::CreateRootSignature(const D3D12_ROOT_SIGNATURE_DESC* desc)
|
||||
{
|
||||
ComPtr<ID3DBlob> blob = SerializeRootSignature(desc);
|
||||
if (!blob)
|
||||
return {};
|
||||
|
||||
ComPtr<ID3D12RootSignature> rs;
|
||||
const HRESULT hr =
|
||||
m_device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(rs.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("CreateRootSignature() failed: %08X", hr);
|
||||
return {};
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
bool Context::SupportsTextureFormat(DXGI_FORMAT format)
|
||||
{
|
||||
constexpr u32 required = D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE;
|
||||
|
||||
D3D12_FEATURE_DATA_FORMAT_SUPPORT support = {format};
|
||||
return SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) &&
|
||||
(support.Support1 & required) == required;
|
||||
}
|
||||
|
||||
bool Context::Create(IDXGIFactory* dxgi_factory, u32 adapter_index, bool enable_debug_layer)
|
||||
{
|
||||
Assert(!g_d3d12_context);
|
||||
|
||||
if (!LoadD3D12Library())
|
||||
return false;
|
||||
|
||||
g_d3d12_context.reset(new Context());
|
||||
if (!g_d3d12_context->CreateDevice(dxgi_factory, adapter_index, enable_debug_layer) ||
|
||||
!g_d3d12_context->CreateCommandQueue() || !g_d3d12_context->CreateFence() ||
|
||||
!g_d3d12_context->CreateDescriptorHeaps() || !g_d3d12_context->CreateCommandLists() ||
|
||||
!g_d3d12_context->CreateTextureStreamBuffer())
|
||||
{
|
||||
Destroy();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Context::Destroy()
|
||||
{
|
||||
if (g_d3d12_context)
|
||||
g_d3d12_context.reset();
|
||||
|
||||
UnloadD3D12Library();
|
||||
}
|
||||
|
||||
bool Context::CreateDevice(IDXGIFactory* dxgi_factory, u32 adapter_index, bool enable_debug_layer)
|
||||
{
|
||||
ComPtr<IDXGIAdapter> adapter;
|
||||
HRESULT hr = dxgi_factory->EnumAdapters(adapter_index, &adapter);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Adapter %u not found, using default", adapter_index);
|
||||
adapter = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
DXGI_ADAPTER_DESC adapter_desc;
|
||||
if (SUCCEEDED(adapter->GetDesc(&adapter_desc)))
|
||||
{
|
||||
char adapter_name_buffer[128];
|
||||
const int name_length = WideCharToMultiByte(CP_UTF8, 0, adapter_desc.Description,
|
||||
static_cast<int>(std::wcslen(adapter_desc.Description)),
|
||||
adapter_name_buffer, countof(adapter_name_buffer), 0, nullptr);
|
||||
if (name_length >= 0)
|
||||
{
|
||||
adapter_name_buffer[name_length] = 0;
|
||||
Log_InfoPrintf("D3D Adapter: %s", adapter_name_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enabling the debug layer will fail if the Graphics Tools feature is not installed.
|
||||
if (enable_debug_layer)
|
||||
{
|
||||
hr = s_d3d12_get_debug_interface(IID_PPV_ARGS(&m_debug_interface));
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
m_debug_interface->EnableDebugLayer();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log_ErrorPrintf("Debug layer requested but not available.");
|
||||
enable_debug_layer = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Create the actual device.
|
||||
hr = s_d3d12_create_device(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&m_device));
|
||||
AssertMsg(SUCCEEDED(hr), "Create D3D12 device");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
if (enable_debug_layer)
|
||||
{
|
||||
ComPtr<ID3D12InfoQueue> info_queue;
|
||||
if (SUCCEEDED(m_device.As(&info_queue)))
|
||||
{
|
||||
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
|
||||
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
|
||||
|
||||
D3D12_INFO_QUEUE_FILTER filter = {};
|
||||
std::array<D3D12_MESSAGE_ID, 5> id_list{
|
||||
D3D12_MESSAGE_ID_CLEARRENDERTARGETVIEW_MISMATCHINGCLEARVALUE,
|
||||
D3D12_MESSAGE_ID_CLEARDEPTHSTENCILVIEW_MISMATCHINGCLEARVALUE,
|
||||
D3D12_MESSAGE_ID_CREATEGRAPHICSPIPELINESTATE_RENDERTARGETVIEW_NOT_SET,
|
||||
D3D12_MESSAGE_ID_CREATEINPUTLAYOUT_TYPE_MISMATCH,
|
||||
D3D12_MESSAGE_ID_DRAW_EMPTY_SCISSOR_RECTANGLE,
|
||||
};
|
||||
filter.DenyList.NumIDs = static_cast<UINT>(id_list.size());
|
||||
filter.DenyList.pIDList = id_list.data();
|
||||
info_queue->PushStorageFilter(&filter);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Context::CreateCommandQueue()
|
||||
{
|
||||
const D3D12_COMMAND_QUEUE_DESC queue_desc = {D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
|
||||
D3D12_COMMAND_QUEUE_FLAG_NONE};
|
||||
HRESULT hr = m_device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&m_command_queue));
|
||||
AssertMsg(SUCCEEDED(hr), "Create command queue");
|
||||
return SUCCEEDED(hr);
|
||||
}
|
||||
|
||||
bool Context::CreateFence()
|
||||
{
|
||||
HRESULT hr = m_device->CreateFence(m_completed_fence_value, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence));
|
||||
AssertMsg(SUCCEEDED(hr), "Create fence");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
m_fence_event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
||||
AssertMsg(m_fence_event != NULL, "Create fence event");
|
||||
if (!m_fence_event)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Context::CreateDescriptorHeaps()
|
||||
{
|
||||
static constexpr size_t MAX_SRVS = 16384;
|
||||
static constexpr size_t MAX_RTVS = 8192;
|
||||
static constexpr size_t MAX_DSVS = 128;
|
||||
static constexpr size_t MAX_SAMPLERS = 128;
|
||||
|
||||
if (!m_descriptor_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, MAX_SRVS, true) ||
|
||||
!m_rtv_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, MAX_RTVS, false) ||
|
||||
!m_dsv_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, MAX_DSVS, false) ||
|
||||
!m_sampler_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, MAX_SAMPLERS, true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_gpu_descriptor_heaps[0] = m_descriptor_heap_manager.GetDescriptorHeap();
|
||||
m_gpu_descriptor_heaps[1] = m_sampler_heap_manager.GetDescriptorHeap();
|
||||
|
||||
// Allocate null SRV descriptor for unbound textures.
|
||||
constexpr D3D12_SHADER_RESOURCE_VIEW_DESC null_srv_desc = {DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_SRV_DIMENSION_TEXTURE2D,
|
||||
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
|
||||
|
||||
if (!m_descriptor_heap_manager.Allocate(&m_null_srv_descriptor))
|
||||
{
|
||||
Panic("Failed to allocate null descriptor");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_device->CreateShaderResourceView(nullptr, &null_srv_desc, m_null_srv_descriptor.cpu_handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Context::CreateCommandLists()
|
||||
{
|
||||
for (u32 i = 0; i < NUM_COMMAND_LISTS; i++)
|
||||
{
|
||||
CommandListResources& res = m_command_lists[i];
|
||||
HRESULT hr = m_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
IID_PPV_ARGS(res.command_allocator.GetAddressOf()));
|
||||
AssertMsg(SUCCEEDED(hr), "Create command allocator");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
hr = m_device->CreateCommandList(1, D3D12_COMMAND_LIST_TYPE_DIRECT, res.command_allocator.Get(), nullptr,
|
||||
IID_PPV_ARGS(res.command_list.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to create command list: %08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close the command list, since the first thing we do is reset them.
|
||||
hr = res.command_list->Close();
|
||||
AssertMsg(SUCCEEDED(hr), "Closing new command list failed");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
}
|
||||
|
||||
MoveToNextCommandList();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Context::CreateTextureStreamBuffer()
|
||||
{
|
||||
return m_texture_stream_buffer.Create(TEXTURE_UPLOAD_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void Context::MoveToNextCommandList()
|
||||
{
|
||||
m_current_command_list = (m_current_command_list + 1) % NUM_COMMAND_LISTS;
|
||||
m_current_fence_value++;
|
||||
|
||||
// We may have to wait if this command list hasn't finished on the GPU.
|
||||
CommandListResources& res = m_command_lists[m_current_command_list];
|
||||
WaitForFence(res.ready_fence_value);
|
||||
|
||||
// Begin command list.
|
||||
res.command_allocator->Reset();
|
||||
res.command_list->Reset(res.command_allocator.Get(), nullptr);
|
||||
res.command_list->SetDescriptorHeaps(static_cast<UINT>(m_gpu_descriptor_heaps.size()), m_gpu_descriptor_heaps.data());
|
||||
res.ready_fence_value = m_current_fence_value;
|
||||
}
|
||||
|
||||
void Context::ExecuteCommandList(bool wait_for_completion)
|
||||
{
|
||||
CommandListResources& res = m_command_lists[m_current_command_list];
|
||||
|
||||
// Close and queue command list.
|
||||
HRESULT hr = res.command_list->Close();
|
||||
AssertMsg(SUCCEEDED(hr), "Close command list");
|
||||
const std::array<ID3D12CommandList*, 1> execute_lists{res.command_list.Get()};
|
||||
m_command_queue->ExecuteCommandLists(static_cast<UINT>(execute_lists.size()), execute_lists.data());
|
||||
|
||||
// Update fence when GPU has completed.
|
||||
hr = m_command_queue->Signal(m_fence.Get(), m_current_fence_value);
|
||||
AssertMsg(SUCCEEDED(hr), "Signal fence");
|
||||
|
||||
MoveToNextCommandList();
|
||||
if (wait_for_completion)
|
||||
WaitForFence(res.ready_fence_value);
|
||||
}
|
||||
|
||||
void Context::DeferResourceDestruction(ID3D12Resource* resource)
|
||||
{
|
||||
if (!resource)
|
||||
return;
|
||||
|
||||
resource->AddRef();
|
||||
m_command_lists[m_current_command_list].pending_resources.push_back(resource);
|
||||
}
|
||||
|
||||
void Context::DeferDescriptorDestruction(DescriptorHeapManager& manager, u32 index)
|
||||
{
|
||||
m_command_lists[m_current_command_list].pending_descriptors.emplace_back(manager, index);
|
||||
}
|
||||
|
||||
void Context::DeferDescriptorDestruction(DescriptorHeapManager& manager, DescriptorHandle* handle)
|
||||
{
|
||||
if (handle->index == DescriptorHandle::INVALID_INDEX)
|
||||
return;
|
||||
|
||||
m_command_lists[m_current_command_list].pending_descriptors.emplace_back(manager, handle->index);
|
||||
handle->Clear();
|
||||
}
|
||||
|
||||
void Context::DestroyPendingResources(CommandListResources& cmdlist)
|
||||
{
|
||||
for (const auto& dd : cmdlist.pending_descriptors)
|
||||
dd.first.Free(dd.second);
|
||||
cmdlist.pending_descriptors.clear();
|
||||
|
||||
for (ID3D12Resource* res : cmdlist.pending_resources)
|
||||
res->Release();
|
||||
cmdlist.pending_resources.clear();
|
||||
}
|
||||
|
||||
void Context::DestroyResources()
|
||||
{
|
||||
ExecuteCommandList(true);
|
||||
|
||||
m_texture_stream_buffer.Destroy(false);
|
||||
m_descriptor_heap_manager.Free(&m_null_srv_descriptor);
|
||||
m_sampler_heap_manager.Destroy();
|
||||
m_dsv_heap_manager.Destroy();
|
||||
m_rtv_heap_manager.Destroy();
|
||||
m_descriptor_heap_manager.Destroy();
|
||||
m_command_lists = {};
|
||||
m_current_command_list = 0;
|
||||
m_completed_fence_value = 0;
|
||||
m_current_fence_value = 0;
|
||||
if (m_fence_event)
|
||||
{
|
||||
CloseHandle(m_fence_event);
|
||||
m_fence_event = {};
|
||||
}
|
||||
|
||||
m_command_queue.Reset();
|
||||
m_debug_interface.Reset();
|
||||
m_device.Reset();
|
||||
}
|
||||
|
||||
void Context::WaitForFence(u64 fence)
|
||||
{
|
||||
if (m_completed_fence_value >= fence)
|
||||
return;
|
||||
|
||||
// Try non-blocking check.
|
||||
m_completed_fence_value = m_fence->GetCompletedValue();
|
||||
if (m_completed_fence_value < fence)
|
||||
{
|
||||
// Fall back to event.
|
||||
HRESULT hr = m_fence->SetEventOnCompletion(fence, m_fence_event);
|
||||
AssertMsg(SUCCEEDED(hr), "Set fence event on completion");
|
||||
WaitForSingleObject(m_fence_event, INFINITE);
|
||||
m_completed_fence_value = m_fence->GetCompletedValue();
|
||||
}
|
||||
|
||||
// Release resources for as many command lists which have completed.
|
||||
u32 index = (m_current_command_list + 1) % NUM_COMMAND_LISTS;
|
||||
for (u32 i = 0; i < NUM_COMMAND_LISTS; i++)
|
||||
{
|
||||
CommandListResources& res = m_command_lists[index];
|
||||
if (m_completed_fence_value < res.ready_fence_value)
|
||||
break;
|
||||
|
||||
DestroyPendingResources(res);
|
||||
index = (index + 1) % NUM_COMMAND_LISTS;
|
||||
}
|
||||
}
|
||||
|
||||
void Context::WaitForGPUIdle()
|
||||
{
|
||||
u32 index = (m_current_command_list + 1) % NUM_COMMAND_LISTS;
|
||||
for (u32 i = 0; i < (NUM_COMMAND_LISTS - 1); i++)
|
||||
{
|
||||
WaitForFence(m_command_lists[index].ready_fence_value);
|
||||
index = (index + 1) % NUM_COMMAND_LISTS;
|
||||
}
|
||||
}
|
||||
} // namespace D3D12
|
142
src/common/d3d12/context.h
Normal file
142
src/common/d3d12/context.h
Normal file
|
@ -0,0 +1,142 @@
|
|||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include "descriptor_heap_manager.h"
|
||||
#include "stream_buffer.h"
|
||||
#include <array>
|
||||
#include <d3d12.h>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <wrl/client.h>
|
||||
|
||||
struct IDXGIFactory;
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
class Context
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
enum : u32
|
||||
{
|
||||
// Number of command lists. One is being built while the other(s) are executed.
|
||||
NUM_COMMAND_LISTS = 3,
|
||||
|
||||
// Textures that don't fit into this buffer will be uploaded with a staging buffer.
|
||||
TEXTURE_UPLOAD_BUFFER_SIZE = 16 * 1024 * 1024,
|
||||
};
|
||||
|
||||
~Context();
|
||||
|
||||
// Creates new device and context.
|
||||
static bool Create(IDXGIFactory* dxgi_factory, u32 adapter_index, bool enable_debug_layer);
|
||||
|
||||
// Destroys active context.
|
||||
static void Destroy();
|
||||
|
||||
ID3D12Device* GetDevice() const { return m_device.Get(); }
|
||||
ID3D12CommandQueue* GetCommandQueue() const { return m_command_queue.Get(); }
|
||||
|
||||
// Returns the current command list, commands can be recorded directly.
|
||||
ID3D12GraphicsCommandList* GetCommandList() const
|
||||
{
|
||||
return m_command_lists[m_current_command_list].command_list.Get();
|
||||
}
|
||||
|
||||
// Descriptor manager access.
|
||||
DescriptorHeapManager& GetDescriptorHeapManager() { return m_descriptor_heap_manager; }
|
||||
DescriptorHeapManager& GetRTVHeapManager() { return m_rtv_heap_manager; }
|
||||
DescriptorHeapManager& GetDSVHeapManager() { return m_dsv_heap_manager; }
|
||||
DescriptorHeapManager& GetSamplerHeapManager() { return m_sampler_heap_manager; }
|
||||
ID3D12DescriptorHeap* const* GetGPUDescriptorHeaps() const { return m_gpu_descriptor_heaps.data(); }
|
||||
u32 GetGPUDescriptorHeapCount() const { return static_cast<u32>(m_gpu_descriptor_heaps.size()); }
|
||||
const DescriptorHandle& GetNullSRVDescriptor() const { return m_null_srv_descriptor; }
|
||||
StreamBuffer& GetTextureStreamBuffer() { return m_texture_stream_buffer; }
|
||||
|
||||
// Root signature access.
|
||||
ComPtr<ID3DBlob> SerializeRootSignature(const D3D12_ROOT_SIGNATURE_DESC* desc);
|
||||
ComPtr<ID3D12RootSignature> CreateRootSignature(const D3D12_ROOT_SIGNATURE_DESC* desc);
|
||||
|
||||
// Fence value for current command list.
|
||||
u64 GetCurrentFenceValue() const { return m_current_fence_value; }
|
||||
|
||||
// Last "completed" fence.
|
||||
u64 GetCompletedFenceValue() const { return m_completed_fence_value; }
|
||||
|
||||
// Feature level to use when compiling shaders.
|
||||
D3D_FEATURE_LEVEL GetFeatureLevel() const { return m_feature_level; }
|
||||
|
||||
// Test for support for the specified texture format.
|
||||
bool SupportsTextureFormat(DXGI_FORMAT format);
|
||||
|
||||
// Executes the current command list.
|
||||
void ExecuteCommandList(bool wait_for_completion);
|
||||
|
||||
// Waits for a specific fence.
|
||||
void WaitForFence(u64 fence);
|
||||
|
||||
// Waits for any in-flight command buffers to complete.
|
||||
void WaitForGPUIdle();
|
||||
|
||||
// Defers destruction of a D3D resource (associates it with the current list).
|
||||
void DeferResourceDestruction(ID3D12Resource* resource);
|
||||
|
||||
// Defers destruction of a descriptor handle (associates it with the current list).
|
||||
void DeferDescriptorDestruction(DescriptorHeapManager& manager, u32 index);
|
||||
void DeferDescriptorDestruction(DescriptorHeapManager& manager, DescriptorHandle* handle);
|
||||
|
||||
private:
|
||||
struct CommandListResources
|
||||
{
|
||||
ComPtr<ID3D12CommandAllocator> command_allocator;
|
||||
ComPtr<ID3D12GraphicsCommandList> command_list;
|
||||
std::vector<ID3D12Resource*> pending_resources;
|
||||
std::vector<std::pair<DescriptorHeapManager&, u32>> pending_descriptors;
|
||||
u64 ready_fence_value = 0;
|
||||
};
|
||||
|
||||
Context();
|
||||
|
||||
bool CreateDevice(IDXGIFactory* dxgi_factory, u32 adapter_index, bool enable_debug_layer);
|
||||
bool CreateCommandQueue();
|
||||
bool CreateFence();
|
||||
bool CreateDescriptorHeaps();
|
||||
bool CreateCommandLists();
|
||||
bool CreateTextureStreamBuffer();
|
||||
void MoveToNextCommandList();
|
||||
void DestroyPendingResources(CommandListResources& cmdlist);
|
||||
void DestroyResources();
|
||||
|
||||
ComPtr<ID3D12Debug> m_debug_interface;
|
||||
ComPtr<ID3D12Device> m_device;
|
||||
ComPtr<ID3D12CommandQueue> m_command_queue;
|
||||
|
||||
ComPtr<ID3D12Fence> m_fence = nullptr;
|
||||
HANDLE m_fence_event = {};
|
||||
u32 m_current_fence_value = 0;
|
||||
u64 m_completed_fence_value = 0;
|
||||
|
||||
std::array<CommandListResources, NUM_COMMAND_LISTS> m_command_lists;
|
||||
u32 m_current_command_list = NUM_COMMAND_LISTS - 1;
|
||||
|
||||
DescriptorHeapManager m_descriptor_heap_manager;
|
||||
DescriptorHeapManager m_rtv_heap_manager;
|
||||
DescriptorHeapManager m_dsv_heap_manager;
|
||||
DescriptorHeapManager m_sampler_heap_manager;
|
||||
std::array<ID3D12DescriptorHeap*, 2> m_gpu_descriptor_heaps = {};
|
||||
DescriptorHandle m_null_srv_descriptor;
|
||||
StreamBuffer m_texture_stream_buffer;
|
||||
|
||||
D3D_FEATURE_LEVEL m_feature_level = D3D_FEATURE_LEVEL_11_0;
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
||||
|
||||
extern std::unique_ptr<D3D12::Context> g_d3d12_context;
|
101
src/common/d3d12/descriptor_heap_manager.cpp
Normal file
101
src/common/d3d12/descriptor_heap_manager.cpp
Normal file
|
@ -0,0 +1,101 @@
|
|||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "descriptor_heap_manager.h"
|
||||
#include "../assert.h"
|
||||
#include "../log.h"
|
||||
#include "context.h"
|
||||
Log_SetChannel(DescriptorHeapManager);
|
||||
|
||||
namespace D3D12 {
|
||||
DescriptorHeapManager::DescriptorHeapManager() = default;
|
||||
DescriptorHeapManager::~DescriptorHeapManager() = default;
|
||||
|
||||
bool DescriptorHeapManager::Create(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE type, u32 num_descriptors,
|
||||
bool shader_visible)
|
||||
{
|
||||
D3D12_DESCRIPTOR_HEAP_DESC desc = {type, static_cast<UINT>(num_descriptors),
|
||||
shader_visible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE :
|
||||
D3D12_DESCRIPTOR_HEAP_FLAG_NONE};
|
||||
|
||||
HRESULT hr = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&m_descriptor_heap));
|
||||
AssertMsg(SUCCEEDED(hr), "Create descriptor heap");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
m_heap_base_cpu = m_descriptor_heap->GetCPUDescriptorHandleForHeapStart();
|
||||
m_heap_base_gpu = m_descriptor_heap->GetGPUDescriptorHandleForHeapStart();
|
||||
m_num_descriptors = num_descriptors;
|
||||
m_descriptor_increment_size = device->GetDescriptorHandleIncrementSize(type);
|
||||
|
||||
// Set all slots to unallocated (1)
|
||||
const u32 bitset_count = num_descriptors / BITSET_SIZE + (((num_descriptors % BITSET_SIZE) != 0) ? 1 : 0);
|
||||
m_free_slots.resize(bitset_count);
|
||||
for (BitSetType& bs : m_free_slots)
|
||||
bs.flip();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DescriptorHeapManager::Destroy()
|
||||
{
|
||||
for (BitSetType& bs : m_free_slots)
|
||||
Assert(bs.all());
|
||||
|
||||
m_num_descriptors = 0;
|
||||
m_descriptor_increment_size = 0;
|
||||
m_heap_base_cpu = {};
|
||||
m_heap_base_gpu = {};
|
||||
m_descriptor_heap.Reset();
|
||||
m_free_slots.clear();
|
||||
}
|
||||
|
||||
bool DescriptorHeapManager::Allocate(DescriptorHandle* handle)
|
||||
{
|
||||
// Start past the temporary slots, no point in searching those.
|
||||
for (u32 group = 0; group < m_free_slots.size(); group++)
|
||||
{
|
||||
BitSetType& bs = m_free_slots[group];
|
||||
if (bs.none())
|
||||
continue;
|
||||
|
||||
u32 bit = 0;
|
||||
for (; bit < BITSET_SIZE; bit++)
|
||||
{
|
||||
if (bs[bit])
|
||||
break;
|
||||
}
|
||||
|
||||
u32 index = group * BITSET_SIZE + bit;
|
||||
bs[bit] = false;
|
||||
|
||||
handle->index = index;
|
||||
handle->cpu_handle.ptr = m_heap_base_cpu.ptr + index * m_descriptor_increment_size;
|
||||
handle->gpu_handle.ptr = m_heap_base_gpu.ptr + index * m_descriptor_increment_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
Panic("Out of fixed descriptors");
|
||||
return false;
|
||||
}
|
||||
|
||||
void DescriptorHeapManager::Free(u32 index)
|
||||
{
|
||||
Assert(index < m_num_descriptors);
|
||||
|
||||
u32 group = index / BITSET_SIZE;
|
||||
u32 bit = index % BITSET_SIZE;
|
||||
m_free_slots[group][bit] = true;
|
||||
}
|
||||
|
||||
void DescriptorHeapManager::Free(DescriptorHandle* handle)
|
||||
{
|
||||
if (handle->index == DescriptorHandle::INVALID_INDEX)
|
||||
return;
|
||||
|
||||
Free(handle->index);
|
||||
handle->Clear();
|
||||
}
|
||||
|
||||
} // namespace D3D12
|
70
src/common/d3d12/descriptor_heap_manager.h
Normal file
70
src/common/d3d12/descriptor_heap_manager.h
Normal file
|
@ -0,0 +1,70 @@
|
|||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include <bitset>
|
||||
#include <d3d12.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace D3D12 {
|
||||
// This class provides an abstraction for D3D12 descriptor heaps.
|
||||
struct DescriptorHandle final
|
||||
{
|
||||
enum : u32
|
||||
{
|
||||
INVALID_INDEX = 0xFFFFFFFF
|
||||
};
|
||||
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE cpu_handle{};
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle{};
|
||||
u32 index = INVALID_INDEX;
|
||||
|
||||
ALWAYS_INLINE operator bool() const { return index != INVALID_INDEX; }
|
||||
|
||||
ALWAYS_INLINE operator D3D12_CPU_DESCRIPTOR_HANDLE() const { return cpu_handle; }
|
||||
ALWAYS_INLINE operator D3D12_GPU_DESCRIPTOR_HANDLE() const { return gpu_handle; }
|
||||
|
||||
ALWAYS_INLINE void Clear()
|
||||
{
|
||||
cpu_handle = {};
|
||||
gpu_handle = {};
|
||||
index = INVALID_INDEX;
|
||||
}
|
||||
};
|
||||
|
||||
class DescriptorHeapManager final
|
||||
{
|
||||
public:
|
||||
DescriptorHeapManager();
|
||||
~DescriptorHeapManager();
|
||||
|
||||
ID3D12DescriptorHeap* GetDescriptorHeap() const { return m_descriptor_heap.Get(); }
|
||||
u32 GetDescriptorIncrementSize() const { return m_descriptor_increment_size; }
|
||||
|
||||
bool Create(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE type, u32 num_descriptors, bool shader_visible);
|
||||
void Destroy();
|
||||
|
||||
bool Allocate(DescriptorHandle* handle);
|
||||
void Free(DescriptorHandle* handle);
|
||||
void Free(u32 index);
|
||||
|
||||
private:
|
||||
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_descriptor_heap;
|
||||
u32 m_num_descriptors = 0;
|
||||
u32 m_descriptor_increment_size = 0;
|
||||
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE m_heap_base_cpu = {};
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE m_heap_base_gpu = {};
|
||||
|
||||
static constexpr u32 BITSET_SIZE = 1024;
|
||||
using BitSetType = std::bitset<BITSET_SIZE>;
|
||||
std::vector<BitSetType> m_free_slots = {};
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
481
src/common/d3d12/shader_cache.cpp
Normal file
481
src/common/d3d12/shader_cache.cpp
Normal file
|
@ -0,0 +1,481 @@
|
|||
#include "shader_cache.h"
|
||||
#include "../d3d11/shader_compiler.h"
|
||||
#include "../file_system.h"
|
||||
#include "../log.h"
|
||||
#include "../md5_digest.h"
|
||||
#include <d3dcompiler.h>
|
||||
Log_SetChannel(D3D12::ShaderCache);
|
||||
|
||||
#ifdef _UWP
|
||||
#include <winrt/Windows.System.Profile.h>
|
||||
#endif
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct CacheIndexEntry
|
||||
{
|
||||
u64 source_hash_low;
|
||||
u64 source_hash_high;
|
||||
u32 source_length;
|
||||
u32 shader_type;
|
||||
u32 file_offset;
|
||||
u32 blob_size;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
static bool CanUsePipelineCache()
|
||||
{
|
||||
#ifdef _UWP
|
||||
// GetCachedBlob crashes on XBox UWP for some reason...
|
||||
const auto version_info = winrt::Windows::System::Profile::AnalyticsInfo::VersionInfo();
|
||||
const auto device_family = version_info.DeviceFamily();
|
||||
return (device_family != L"Windows.Xbox");
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
ShaderCache::ShaderCache() : m_use_pipeline_cache(CanUsePipelineCache()) {}
|
||||
|
||||
ShaderCache::~ShaderCache()
|
||||
{
|
||||
if (m_pipeline_index_file)
|
||||
std::fclose(m_pipeline_index_file);
|
||||
if (m_pipeline_blob_file)
|
||||
std::fclose(m_pipeline_blob_file);
|
||||
if (m_shader_index_file)
|
||||
std::fclose(m_shader_index_file);
|
||||
if (m_shader_blob_file)
|
||||
std::fclose(m_shader_blob_file);
|
||||
}
|
||||
|
||||
bool ShaderCache::CacheIndexKey::operator==(const CacheIndexKey& key) const
|
||||
{
|
||||
return (source_hash_low == key.source_hash_low && source_hash_high == key.source_hash_high &&
|
||||
source_length == key.source_length && type == key.type);
|
||||
}
|
||||
|
||||
bool ShaderCache::CacheIndexKey::operator!=(const CacheIndexKey& key) const
|
||||
{
|
||||
return (source_hash_low != key.source_hash_low || source_hash_high != key.source_hash_high ||
|
||||
source_length != key.source_length || type != key.type);
|
||||
}
|
||||
|
||||
void ShaderCache::Open(std::string_view base_path, D3D_FEATURE_LEVEL feature_level, bool debug)
|
||||
{
|
||||
m_base_path = base_path;
|
||||
m_feature_level = feature_level;
|
||||
m_debug = debug;
|
||||
|
||||
if (!base_path.empty())
|
||||
{
|
||||
const std::string base_shader_filename = GetCacheBaseFileName(base_path, "shaders", feature_level, debug);
|
||||
const std::string shader_index_filename = base_shader_filename + ".idx";
|
||||
const std::string shader_blob_filename = base_shader_filename + ".bin";
|
||||
|
||||
if (!ReadExisting(shader_index_filename, shader_blob_filename, m_shader_index_file, m_shader_blob_file,
|
||||
m_shader_index))
|
||||
{
|
||||
CreateNew(shader_index_filename, shader_blob_filename, m_shader_index_file, m_shader_blob_file);
|
||||
}
|
||||
|
||||
if (m_use_pipeline_cache)
|
||||
{
|
||||
const std::string base_pipelines_filename = GetCacheBaseFileName(base_path, "pipelines", feature_level, debug);
|
||||
const std::string pipelines_index_filename = base_pipelines_filename + ".idx";
|
||||
const std::string pipelines_blob_filename = base_pipelines_filename + ".bin";
|
||||
|
||||
if (!ReadExisting(pipelines_index_filename, pipelines_blob_filename, m_pipeline_index_file, m_pipeline_blob_file,
|
||||
m_pipeline_index))
|
||||
{
|
||||
CreateNew(pipelines_index_filename, pipelines_blob_filename, m_pipeline_index_file, m_pipeline_blob_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderCache::InvalidatePipelineCache()
|
||||
{
|
||||
m_pipeline_index.clear();
|
||||
if (m_pipeline_blob_file)
|
||||
{
|
||||
std::fclose(m_pipeline_blob_file);
|
||||
m_pipeline_blob_file = nullptr;
|
||||
}
|
||||
|
||||
if (m_pipeline_index_file)
|
||||
{
|
||||
std::fclose(m_pipeline_index_file);
|
||||
m_pipeline_index_file = nullptr;
|
||||
}
|
||||
|
||||
if (m_use_pipeline_cache)
|
||||
{
|
||||
const std::string base_pipelines_filename =
|
||||
GetCacheBaseFileName(m_base_path, "pipelines", m_feature_level, m_debug);
|
||||
const std::string pipelines_index_filename = base_pipelines_filename + ".idx";
|
||||
const std::string pipelines_blob_filename = base_pipelines_filename + ".bin";
|
||||
CreateNew(pipelines_index_filename, pipelines_blob_filename, m_pipeline_index_file, m_pipeline_blob_file);
|
||||
}
|
||||
}
|
||||
|
||||
bool ShaderCache::CreateNew(const std::string& index_filename, const std::string& blob_filename, std::FILE*& index_file,
|
||||
std::FILE*& blob_file)
|
||||
{
|
||||
if (FileSystem::FileExists(index_filename.c_str()))
|
||||
{
|
||||
Log_WarningPrintf("Removing existing index file '%s'", index_filename.c_str());
|
||||
FileSystem::DeleteFile(index_filename.c_str());
|
||||
}
|
||||
if (FileSystem::FileExists(blob_filename.c_str()))
|
||||
{
|
||||
Log_WarningPrintf("Removing existing blob file '%s'", blob_filename.c_str());
|
||||
FileSystem::DeleteFile(blob_filename.c_str());
|
||||
}
|
||||
|
||||
index_file = FileSystem::OpenCFile(index_filename.c_str(), "wb");
|
||||
if (!index_file)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to open index file '%s' for writing", index_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const u32 index_version = FILE_VERSION;
|
||||
if (std::fwrite(&index_version, sizeof(index_version), 1, index_file) != 1)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to write version to index file '%s'", index_filename.c_str());
|
||||
std::fclose(index_file);
|
||||
index_file = nullptr;
|
||||
FileSystem::DeleteFile(index_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
blob_file = FileSystem::OpenCFile(blob_filename.c_str(), "w+b");
|
||||
if (!blob_file)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to open blob file '%s' for writing", blob_filename.c_str());
|
||||
std::fclose(blob_file);
|
||||
blob_file = nullptr;
|
||||
FileSystem::DeleteFile(index_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderCache::ReadExisting(const std::string& index_filename, const std::string& blob_filename,
|
||||
std::FILE*& index_file, std::FILE*& blob_file, CacheIndex& index)
|
||||
{
|
||||
index_file = FileSystem::OpenCFile(index_filename.c_str(), "r+b");
|
||||
if (!index_file)
|
||||
return false;
|
||||
|
||||
u32 file_version;
|
||||
if (std::fread(&file_version, sizeof(file_version), 1, index_file) != 1 || file_version != FILE_VERSION)
|
||||
{
|
||||
Log_ErrorPrintf("Bad file version in '%s'", index_filename.c_str());
|
||||
std::fclose(index_file);
|
||||
index_file = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
blob_file = FileSystem::OpenCFile(blob_filename.c_str(), "a+b");
|
||||
if (!blob_file)
|
||||
{
|
||||
Log_ErrorPrintf("Blob file '%s' is missing", blob_filename.c_str());
|
||||
std::fclose(index_file);
|
||||
index_file = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::fseek(blob_file, 0, SEEK_END);
|
||||
const u32 blob_file_size = static_cast<u32>(std::ftell(blob_file));
|
||||
|
||||
for (;;)
|
||||
{
|
||||
CacheIndexEntry entry;
|
||||
if (std::fread(&entry, sizeof(entry), 1, index_file) != 1 || (entry.file_offset + entry.blob_size) > blob_file_size)
|
||||
{
|
||||
if (std::feof(index_file))
|
||||
break;
|
||||
|
||||
Log_ErrorPrintf("Failed to read entry from '%s', corrupt file?", index_filename.c_str());
|
||||
index.clear();
|
||||
std::fclose(blob_file);
|
||||
blob_file = nullptr;
|
||||
std::fclose(index_file);
|
||||
index_file = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
const CacheIndexKey key{entry.source_hash_low, entry.source_hash_high, entry.source_length,
|
||||
static_cast<EntryType>(entry.shader_type)};
|
||||
const CacheIndexData data{entry.file_offset, entry.blob_size};
|
||||
index.emplace(key, data);
|
||||
}
|
||||
|
||||
// ensure we don't write before seeking
|
||||
std::fseek(index_file, 0, SEEK_END);
|
||||
|
||||
Log_InfoPrintf("Read %zu entries from '%s'", index.size(), index_filename.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string ShaderCache::GetCacheBaseFileName(const std::string_view& base_path, const std::string_view& type,
|
||||
D3D_FEATURE_LEVEL feature_level, bool debug)
|
||||
{
|
||||
std::string base_filename(base_path);
|
||||
base_filename += "d3d12_";
|
||||
base_filename += type;
|
||||
base_filename += "_";
|
||||
|
||||
switch (feature_level)
|
||||
{
|
||||
case D3D_FEATURE_LEVEL_10_0:
|
||||
base_filename += "sm40";
|
||||
break;
|
||||
case D3D_FEATURE_LEVEL_10_1:
|
||||
base_filename += "sm41";
|
||||
break;
|
||||
case D3D_FEATURE_LEVEL_11_0:
|
||||
base_filename += "sm50";
|
||||
break;
|
||||
default:
|
||||
base_filename += "unk";
|
||||
break;
|
||||
}
|
||||
|
||||
if (debug)
|
||||
base_filename += "_debug";
|
||||
|
||||
return base_filename;
|
||||
}
|
||||
|
||||
union MD5Hash
|
||||
{
|
||||
struct
|
||||
{
|
||||
u64 low;
|
||||
u64 high;
|
||||
};
|
||||
u8 hash[16];
|
||||
};
|
||||
|
||||
ShaderCache::CacheIndexKey ShaderCache::GetShaderCacheKey(EntryType type, const std::string_view& shader_code)
|
||||
{
|
||||
MD5Hash h;
|
||||
MD5Digest digest;
|
||||
digest.Update(shader_code.data(), static_cast<u32>(shader_code.length()));
|
||||
digest.Final(h.hash);
|
||||
|
||||
return CacheIndexKey{h.low, h.high, static_cast<u32>(shader_code.length()), type};
|
||||
}
|
||||
|
||||
ShaderCache::CacheIndexKey ShaderCache::GetPipelineCacheKey(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& gpdesc)
|
||||
{
|
||||
MD5Digest digest;
|
||||
u32 length = sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC);
|
||||
|
||||
if (gpdesc.VS.BytecodeLength > 0)
|
||||
{
|
||||
digest.Update(gpdesc.VS.pShaderBytecode, static_cast<u32>(gpdesc.VS.BytecodeLength));
|
||||
length += static_cast<u32>(gpdesc.VS.BytecodeLength);
|
||||
}
|
||||
if (gpdesc.GS.BytecodeLength > 0)
|
||||
{
|
||||
digest.Update(gpdesc.GS.pShaderBytecode, static_cast<u32>(gpdesc.GS.BytecodeLength));
|
||||
length += static_cast<u32>(gpdesc.GS.BytecodeLength);
|
||||
}
|
||||
if (gpdesc.PS.BytecodeLength > 0)
|
||||
{
|
||||
digest.Update(gpdesc.PS.pShaderBytecode, static_cast<u32>(gpdesc.PS.BytecodeLength));
|
||||
length += static_cast<u32>(gpdesc.PS.BytecodeLength);
|
||||
}
|
||||
|
||||
digest.Update(&gpdesc.BlendState, sizeof(gpdesc.BlendState));
|
||||
digest.Update(&gpdesc.SampleMask, sizeof(gpdesc.SampleMask));
|
||||
digest.Update(&gpdesc.RasterizerState, sizeof(gpdesc.RasterizerState));
|
||||
digest.Update(&gpdesc.DepthStencilState, sizeof(gpdesc.DepthStencilState));
|
||||
|
||||
for (u32 i = 0; i < gpdesc.InputLayout.NumElements; i++)
|
||||
{
|
||||
const D3D12_INPUT_ELEMENT_DESC& ie = gpdesc.InputLayout.pInputElementDescs[i];
|
||||
digest.Update(ie.SemanticName, static_cast<u32>(std::strlen(ie.SemanticName)));
|
||||
digest.Update(&ie.SemanticIndex, sizeof(ie.SemanticIndex));
|
||||
digest.Update(&ie.Format, sizeof(ie.Format));
|
||||
digest.Update(&ie.InputSlot, sizeof(ie.InputSlot));
|
||||
digest.Update(&ie.AlignedByteOffset, sizeof(ie.AlignedByteOffset));
|
||||
digest.Update(&ie.InputSlotClass, sizeof(ie.InputSlotClass));
|
||||
digest.Update(&ie.InstanceDataStepRate, sizeof(ie.InstanceDataStepRate));
|
||||
length += sizeof(D3D12_INPUT_ELEMENT_DESC);
|
||||
}
|
||||
|
||||
digest.Update(&gpdesc.IBStripCutValue, sizeof(gpdesc.IBStripCutValue));
|
||||
digest.Update(&gpdesc.PrimitiveTopologyType, sizeof(gpdesc.PrimitiveTopologyType));
|
||||
digest.Update(&gpdesc.NumRenderTargets, sizeof(gpdesc.NumRenderTargets));
|
||||
digest.Update(gpdesc.RTVFormats, sizeof(gpdesc.RTVFormats));
|
||||
digest.Update(&gpdesc.DSVFormat, sizeof(gpdesc.DSVFormat));
|
||||
digest.Update(&gpdesc.SampleDesc, sizeof(gpdesc.SampleDesc));
|
||||
digest.Update(&gpdesc.Flags, sizeof(gpdesc.Flags));
|
||||
|
||||
MD5Hash h;
|
||||
digest.Final(h.hash);
|
||||
|
||||
return CacheIndexKey{h.low, h.high, length, EntryType::GraphicsPipeline};
|
||||
}
|
||||
|
||||
ShaderCache::ComPtr<ID3DBlob> ShaderCache::GetShaderBlob(EntryType type, std::string_view shader_code)
|
||||
{
|
||||
const auto key = GetShaderCacheKey(type, shader_code);
|
||||
auto iter = m_shader_index.find(key);
|
||||
if (iter == m_shader_index.end())
|
||||
return CompileAndAddShaderBlob(key, shader_code);
|
||||
|
||||
ComPtr<ID3DBlob> blob;
|
||||
HRESULT hr = D3DCreateBlob(iter->second.blob_size, blob.GetAddressOf());
|
||||
if (FAILED(hr) || std::fseek(m_shader_blob_file, iter->second.file_offset, SEEK_SET) != 0 ||
|
||||
std::fread(blob->GetBufferPointer(), 1, iter->second.blob_size, m_shader_blob_file) != iter->second.blob_size)
|
||||
{
|
||||
Log_ErrorPrintf("Read blob from file failed");
|
||||
return {};
|
||||
}
|
||||
|
||||
return blob;
|
||||
}
|
||||
|
||||
ShaderCache::ComPtr<ID3D12PipelineState> ShaderCache::GetPipelineState(ID3D12Device* device,
|
||||
const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc)
|
||||
{
|
||||
const auto key = GetPipelineCacheKey(desc);
|
||||
|
||||
auto iter = m_pipeline_index.find(key);
|
||||
if (iter == m_pipeline_index.end())
|
||||
return CompileAndAddPipeline(device, key, desc);
|
||||
|
||||
ComPtr<ID3DBlob> blob;
|
||||
HRESULT hr = D3DCreateBlob(iter->second.blob_size, blob.GetAddressOf());
|
||||
if (FAILED(hr) || std::fseek(m_pipeline_blob_file, iter->second.file_offset, SEEK_SET) != 0 ||
|
||||
std::fread(blob->GetBufferPointer(), 1, iter->second.blob_size, m_pipeline_blob_file) != iter->second.blob_size)
|
||||
{
|
||||
Log_ErrorPrintf("Read blob from file failed");
|
||||
return {};
|
||||
}
|
||||
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc_with_blob(desc);
|
||||
desc_with_blob.CachedPSO.pCachedBlob = blob->GetBufferPointer();
|
||||
desc_with_blob.CachedPSO.CachedBlobSizeInBytes = blob->GetBufferSize();
|
||||
|
||||
ComPtr<ID3D12PipelineState> pso;
|
||||
hr = device->CreateGraphicsPipelineState(&desc_with_blob, IID_PPV_ARGS(pso.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_WarningPrintf("Creating cached PSO failed: %08X. Invalidating cache.", hr);
|
||||
InvalidatePipelineCache();
|
||||
pso = CompileAndAddPipeline(device, key, desc);
|
||||
}
|
||||
|
||||
return pso;
|
||||
}
|
||||
|
||||
ShaderCache::ComPtr<ID3DBlob> ShaderCache::CompileAndAddShaderBlob(const CacheIndexKey& key,
|
||||
std::string_view shader_code)
|
||||
{
|
||||
ComPtr<ID3DBlob> blob;
|
||||
|
||||
switch (key.type)
|
||||
{
|
||||
case EntryType::VertexShader:
|
||||
blob = D3D11::ShaderCompiler::CompileShader(D3D11::ShaderCompiler::Type::Vertex, m_feature_level, shader_code,
|
||||
m_debug);
|
||||
break;
|
||||
case EntryType::GeometryShader:
|
||||
blob = D3D11::ShaderCompiler::CompileShader(D3D11::ShaderCompiler::Type::Geometry, m_feature_level, shader_code,
|
||||
m_debug);
|
||||
break;
|
||||
case EntryType::PixelShader:
|
||||
blob =
|
||||
D3D11::ShaderCompiler::CompileShader(D3D11::ShaderCompiler::Type::Pixel, m_feature_level, shader_code, m_debug);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!blob)
|
||||
return {};
|
||||
|
||||
if (!m_shader_blob_file || std::fseek(m_shader_blob_file, 0, SEEK_END) != 0)
|
||||
return blob;
|
||||
|
||||
CacheIndexData data;
|
||||
data.file_offset = static_cast<u32>(std::ftell(m_shader_blob_file));
|
||||
data.blob_size = static_cast<u32>(blob->GetBufferSize());
|
||||
|
||||
CacheIndexEntry entry = {};
|
||||
entry.source_hash_low = key.source_hash_low;
|
||||
entry.source_hash_high = key.source_hash_high;
|
||||
entry.source_length = key.source_length;
|
||||
entry.shader_type = static_cast<u32>(key.type);
|
||||
entry.blob_size = data.blob_size;
|
||||
entry.file_offset = data.file_offset;
|
||||
|
||||
if (std::fwrite(blob->GetBufferPointer(), 1, entry.blob_size, m_shader_blob_file) != entry.blob_size ||
|
||||
std::fflush(m_shader_blob_file) != 0 || std::fwrite(&entry, sizeof(entry), 1, m_shader_index_file) != 1 ||
|
||||
std::fflush(m_shader_index_file) != 0)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to write shader blob to file");
|
||||
return blob;
|
||||
}
|
||||
|
||||
m_shader_index.emplace(key, data);
|
||||
return blob;
|
||||
}
|
||||
|
||||
ShaderCache::ComPtr<ID3D12PipelineState>
|
||||
ShaderCache::CompileAndAddPipeline(ID3D12Device* device, const CacheIndexKey& key,
|
||||
const D3D12_GRAPHICS_PIPELINE_STATE_DESC& gpdesc)
|
||||
{
|
||||
ComPtr<ID3D12PipelineState> pso;
|
||||
HRESULT hr = device->CreateGraphicsPipelineState(&gpdesc, IID_PPV_ARGS(pso.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Creating cached PSO failed: %08X", hr);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!m_pipeline_blob_file || std::fseek(m_pipeline_blob_file, 0, SEEK_END) != 0)
|
||||
return pso;
|
||||
|
||||
ComPtr<ID3DBlob> blob;
|
||||
hr = pso->GetCachedBlob(blob.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_WarningPrintf("Failed to get cached PSO data: %08X", hr);
|
||||
return pso;
|
||||
}
|
||||
|
||||
CacheIndexData data;
|
||||
data.file_offset = static_cast<u32>(std::ftell(m_pipeline_blob_file));
|
||||
data.blob_size = static_cast<u32>(blob->GetBufferSize());
|
||||
|
||||
CacheIndexEntry entry = {};
|
||||
entry.source_hash_low = key.source_hash_low;
|
||||
entry.source_hash_high = key.source_hash_high;
|
||||
entry.source_length = key.source_length;
|
||||
entry.shader_type = static_cast<u32>(key.type);
|
||||
entry.blob_size = data.blob_size;
|
||||
entry.file_offset = data.file_offset;
|
||||
|
||||
if (std::fwrite(blob->GetBufferPointer(), 1, entry.blob_size, m_pipeline_blob_file) != entry.blob_size ||
|
||||
std::fflush(m_pipeline_blob_file) != 0 || std::fwrite(&entry, sizeof(entry), 1, m_pipeline_index_file) != 1 ||
|
||||
std::fflush(m_pipeline_index_file) != 0)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to write pipeline blob to file");
|
||||
return pso;
|
||||
}
|
||||
|
||||
m_shader_index.emplace(key, data);
|
||||
return pso;
|
||||
}
|
||||
|
||||
} // namespace D3D12
|
118
src/common/d3d12/shader_cache.h
Normal file
118
src/common/d3d12/shader_cache.h
Normal file
|
@ -0,0 +1,118 @@
|
|||
#pragma once
|
||||
#include "../hash_combine.h"
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include <cstdio>
|
||||
#include <d3d12.h>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
class ShaderCache
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
enum class EntryType
|
||||
{
|
||||
VertexShader,
|
||||
GeometryShader,
|
||||
PixelShader,
|
||||
ComputeShader,
|
||||
GraphicsPipeline,
|
||||
};
|
||||
|
||||
ShaderCache();
|
||||
~ShaderCache();
|
||||
|
||||
void Open(std::string_view base_path, D3D_FEATURE_LEVEL feature_level, bool debug);
|
||||
|
||||
ALWAYS_INLINE ComPtr<ID3DBlob> GetVertexShader(std::string_view shader_code)
|
||||
{
|
||||
return GetShaderBlob(EntryType::VertexShader, shader_code);
|
||||
}
|
||||
ALWAYS_INLINE ComPtr<ID3DBlob> GetGeometryShader(std::string_view shader_code)
|
||||
{
|
||||
return GetShaderBlob(EntryType::GeometryShader, shader_code);
|
||||
}
|
||||
ALWAYS_INLINE ComPtr<ID3DBlob> GetPixelShader(std::string_view shader_code)
|
||||
{
|
||||
return GetShaderBlob(EntryType::PixelShader, shader_code);
|
||||
}
|
||||
ALWAYS_INLINE ComPtr<ID3DBlob> GetComputeShader(std::string_view shader_code)
|
||||
{
|
||||
return GetShaderBlob(EntryType::ComputeShader, shader_code);
|
||||
}
|
||||
|
||||
ComPtr<ID3DBlob> GetShaderBlob(EntryType type, std::string_view shader_code);
|
||||
|
||||
ComPtr<ID3D12PipelineState> GetPipelineState(ID3D12Device* device, const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc);
|
||||
|
||||
private:
|
||||
static constexpr u32 FILE_VERSION = 1;
|
||||
|
||||
struct CacheIndexKey
|
||||
{
|
||||
u64 source_hash_low;
|
||||
u64 source_hash_high;
|
||||
u32 source_length;
|
||||
EntryType type;
|
||||
|
||||
bool operator==(const CacheIndexKey& key) const;
|
||||
bool operator!=(const CacheIndexKey& key) const;
|
||||
};
|
||||
|
||||
struct CacheIndexEntryHasher
|
||||
{
|
||||
std::size_t operator()(const CacheIndexKey& e) const noexcept
|
||||
{
|
||||
std::size_t h = 0;
|
||||
hash_combine(h, e.source_hash_low, e.source_hash_high, e.source_length, e.type);
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
struct CacheIndexData
|
||||
{
|
||||
u32 file_offset;
|
||||
u32 blob_size;
|
||||
};
|
||||
|
||||
using CacheIndex = std::unordered_map<CacheIndexKey, CacheIndexData, CacheIndexEntryHasher>;
|
||||
|
||||
static std::string GetCacheBaseFileName(const std::string_view& base_path, const std::string_view& type,
|
||||
D3D_FEATURE_LEVEL feature_level, bool debug);
|
||||
static CacheIndexKey GetShaderCacheKey(EntryType type, const std::string_view& shader_code);
|
||||
static CacheIndexKey GetPipelineCacheKey(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& gpdesc);
|
||||
|
||||
bool CreateNew(const std::string& index_filename, const std::string& blob_filename, std::FILE*& index_file,
|
||||
std::FILE*& blob_file);
|
||||
bool ReadExisting(const std::string& index_filename, const std::string& blob_filename, std::FILE*& index_file,
|
||||
std::FILE*& blob_file, CacheIndex& index);
|
||||
void InvalidatePipelineCache();
|
||||
void Close();
|
||||
|
||||
ComPtr<ID3DBlob> CompileAndAddShaderBlob(const CacheIndexKey& key, std::string_view shader_code);
|
||||
ComPtr<ID3D12PipelineState> CompileAndAddPipeline(ID3D12Device* device, const CacheIndexKey& key,
|
||||
const D3D12_GRAPHICS_PIPELINE_STATE_DESC& gpdesc);
|
||||
|
||||
std::string m_base_path;
|
||||
|
||||
std::FILE* m_shader_index_file = nullptr;
|
||||
std::FILE* m_shader_blob_file = nullptr;
|
||||
CacheIndex m_shader_index;
|
||||
|
||||
std::FILE* m_pipeline_index_file = nullptr;
|
||||
std::FILE* m_pipeline_blob_file = nullptr;
|
||||
CacheIndex m_pipeline_index;
|
||||
|
||||
D3D_FEATURE_LEVEL m_feature_level = D3D_FEATURE_LEVEL_11_0;
|
||||
bool m_use_pipeline_cache = false;
|
||||
bool m_debug = false;
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
234
src/common/d3d12/staging_texture.cpp
Normal file
234
src/common/d3d12/staging_texture.cpp
Normal file
|
@ -0,0 +1,234 @@
|
|||
#include "staging_texture.h"
|
||||
#include "../align.h"
|
||||
#include "../assert.h"
|
||||
#include "../log.h"
|
||||
#include "context.h"
|
||||
#include "util.h"
|
||||
Log_SetChannel(D3D12);
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
StagingTexture::StagingTexture() : m_width(0), m_height(0) {}
|
||||
|
||||
StagingTexture::~StagingTexture()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool StagingTexture::Create(u32 width, u32 height, DXGI_FORMAT format, bool for_uploading)
|
||||
{
|
||||
const u32 texel_size = GetTexelSize(format);
|
||||
const u32 row_pitch = Common::AlignUpPow2(width * texel_size, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
|
||||
const u32 buffer_size = height * row_pitch;
|
||||
|
||||
const D3D12_HEAP_PROPERTIES heap_properties = {for_uploading ? D3D12_HEAP_TYPE_UPLOAD : D3D12_HEAP_TYPE_READBACK};
|
||||
|
||||
D3D12_RESOURCE_DESC desc = {};
|
||||
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
desc.Width = buffer_size;
|
||||
desc.Height = 1;
|
||||
desc.DepthOrArraySize = 1;
|
||||
desc.MipLevels = 1;
|
||||
desc.Format = DXGI_FORMAT_UNKNOWN;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||
|
||||
D3D12_RESOURCE_STATES state = for_uploading ? D3D12_RESOURCE_STATE_GENERIC_READ : D3D12_RESOURCE_STATE_COPY_DEST;
|
||||
|
||||
ComPtr<ID3D12Resource> resource;
|
||||
HRESULT hr = g_d3d12_context->GetDevice()->CreateCommittedResource(
|
||||
&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr, IID_PPV_ARGS(resource.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Create buffer failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_resource = std::move(resource);
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_format = format;
|
||||
m_buffer_size = buffer_size;
|
||||
m_row_pitch = row_pitch;
|
||||
m_texel_size = texel_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
void StagingTexture::Destroy(bool defer)
|
||||
{
|
||||
if (IsMapped())
|
||||
Unmap();
|
||||
|
||||
if (m_resource && defer)
|
||||
g_d3d12_context->DeferResourceDestruction(m_resource.Get());
|
||||
m_resource.Reset();
|
||||
m_width = 0;
|
||||
m_height = 0;
|
||||
m_format = DXGI_FORMAT_UNKNOWN;
|
||||
m_buffer_size = 0;
|
||||
m_row_pitch = 0;
|
||||
m_texel_size = 0;
|
||||
}
|
||||
|
||||
bool StagingTexture::Map(bool writing)
|
||||
{
|
||||
D3D12_RANGE range{0u, m_buffer_size};
|
||||
|
||||
Assert(!IsMapped());
|
||||
const HRESULT hr = m_resource->Map(0, writing ? nullptr : &range, &m_mapped_pointer);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Map staging buffer failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_mapped_for_write = writing;
|
||||
return true;
|
||||
}
|
||||
|
||||
void StagingTexture::Unmap()
|
||||
{
|
||||
Assert(IsMapped());
|
||||
|
||||
D3D12_RANGE range{0u, m_buffer_size};
|
||||
m_resource->Unmap(0, m_mapped_for_write ? &range : nullptr);
|
||||
m_mapped_pointer = nullptr;
|
||||
m_mapped_for_write = false;
|
||||
}
|
||||
|
||||
void StagingTexture::Flush()
|
||||
{
|
||||
if (!m_needs_flush)
|
||||
return;
|
||||
|
||||
m_needs_flush = false;
|
||||
|
||||
// If the completed fence is the same as the current command buffer fence, we need to execute
|
||||
// the current list and wait for it to complete. This is the slowest path. Otherwise, if the
|
||||
// command list with the copy has been submitted, we only need to wait for the fence.
|
||||
if (m_completed_fence == g_d3d12_context->GetCurrentFenceValue())
|
||||
g_d3d12_context->ExecuteCommandList(true);
|
||||
else
|
||||
g_d3d12_context->WaitForFence(m_completed_fence);
|
||||
}
|
||||
|
||||
void StagingTexture::CopyToTexture(u32 src_x, u32 src_y, ID3D12Resource* dst_texture, u32 dst_subresource, u32 dst_x,
|
||||
u32 dst_y, u32 width, u32 height)
|
||||
{
|
||||
DebugAssert((src_x + width) <= m_width && (src_y + height) <= m_height);
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION dst;
|
||||
dst.pResource = dst_texture;
|
||||
dst.SubresourceIndex = 0;
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION src;
|
||||
src.pResource = m_resource.Get();
|
||||
src.SubresourceIndex = 0;
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
src.PlacedFootprint.Offset = 0;
|
||||
src.PlacedFootprint.Footprint.Width = m_width;
|
||||
src.PlacedFootprint.Footprint.Height = m_height;
|
||||
src.PlacedFootprint.Footprint.Depth = 1;
|
||||
src.PlacedFootprint.Footprint.Format = m_format;
|
||||
src.PlacedFootprint.Footprint.RowPitch = m_row_pitch;
|
||||
|
||||
const D3D12_BOX src_box{src_x, src_y, 0u, src_x + width, src_y + height, 1u};
|
||||
g_d3d12_context->GetCommandList()->CopyTextureRegion(&dst, dst_x, dst_y, 0, &src, &src_box);
|
||||
}
|
||||
|
||||
void StagingTexture::CopyFromTexture(ID3D12Resource* src_texture, u32 src_subresource, u32 src_x, u32 src_y, u32 dst_x,
|
||||
u32 dst_y, u32 width, u32 height)
|
||||
{
|
||||
DebugAssert((dst_x + width) <= m_width && (dst_y + height) <= m_height);
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION src;
|
||||
src.pResource = src_texture;
|
||||
src.SubresourceIndex = 0;
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION dst;
|
||||
dst.pResource = m_resource.Get();
|
||||
dst.SubresourceIndex = 0;
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
dst.PlacedFootprint.Offset = 0;
|
||||
dst.PlacedFootprint.Footprint.Width = m_width;
|
||||
dst.PlacedFootprint.Footprint.Height = m_height;
|
||||
dst.PlacedFootprint.Footprint.Depth = 1;
|
||||
dst.PlacedFootprint.Footprint.Format = m_format;
|
||||
dst.PlacedFootprint.Footprint.RowPitch = m_row_pitch;
|
||||
|
||||
const D3D12_BOX src_box{src_x, src_y, 0u, src_x + width, src_y + height, 1u};
|
||||
g_d3d12_context->GetCommandList()->CopyTextureRegion(&dst, dst_x, dst_y, 0, &src, &src_box);
|
||||
m_completed_fence = g_d3d12_context->GetCurrentFenceValue();
|
||||
m_needs_flush = true;
|
||||
}
|
||||
|
||||
bool StagingTexture::ReadPixels(u32 x, u32 y, u32 width, u32 height, void* data, u32 row_pitch)
|
||||
{
|
||||
if (m_needs_flush)
|
||||
Flush();
|
||||
|
||||
const bool was_mapped = IsMapped();
|
||||
if (!was_mapped && !Map(false))
|
||||
return false;
|
||||
|
||||
const u8* src_ptr = static_cast<u8*>(m_mapped_pointer) + (y * m_row_pitch) + (x * m_texel_size);
|
||||
u8* dst_ptr = reinterpret_cast<u8*>(data);
|
||||
if (m_row_pitch != row_pitch || width != m_width || x != 0)
|
||||
{
|
||||
const u32 copy_size = m_texel_size * width;
|
||||
for (u32 row = 0; row < height; row++)
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, copy_size);
|
||||
src_ptr += m_row_pitch;
|
||||
dst_ptr += row_pitch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, row_pitch * height);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StagingTexture::WritePixels(u32 x, u32 y, u32 width, u32 height, const void* data, u32 row_pitch)
|
||||
{
|
||||
const bool was_mapped = IsMapped();
|
||||
if (!was_mapped && !Map(true))
|
||||
return false;
|
||||
|
||||
const u8* src_ptr = reinterpret_cast<const u8*>(data);
|
||||
u8* dst_ptr = static_cast<u8*>(m_mapped_pointer) + (y * m_row_pitch) + (x * m_texel_size);
|
||||
if (m_row_pitch != row_pitch || width != m_width || x != 0)
|
||||
{
|
||||
const u32 copy_size = m_texel_size * width;
|
||||
for (u32 row = 0; row < height; row++)
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, copy_size);
|
||||
src_ptr += row_pitch;
|
||||
dst_ptr += m_row_pitch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, m_row_pitch * height);
|
||||
}
|
||||
|
||||
if (!was_mapped)
|
||||
Unmap();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StagingTexture::EnsureSize(u32 width, u32 height, DXGI_FORMAT format, bool for_uploading)
|
||||
{
|
||||
if (m_resource && m_width >= width && m_height >= height && m_format == format)
|
||||
return true;
|
||||
|
||||
return Create(width, height, format, for_uploading);
|
||||
}
|
||||
|
||||
} // namespace D3D12
|
62
src/common/d3d12/staging_texture.h
Normal file
62
src/common/d3d12/staging_texture.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
#pragma once
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include <cstring>
|
||||
#include <d3d12.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace D3D12 {
|
||||
class StagingTexture
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
StagingTexture();
|
||||
~StagingTexture();
|
||||
|
||||
ALWAYS_INLINE ID3D12Resource* GetD3DResource() const { return m_resource.Get(); }
|
||||
|
||||
ALWAYS_INLINE u32 GetWidth() const { return m_width; }
|
||||
ALWAYS_INLINE u32 GetHeight() const { return m_height; }
|
||||
ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return m_format; }
|
||||
ALWAYS_INLINE bool IsMapped() const { return m_mapped_pointer != nullptr; }
|
||||
ALWAYS_INLINE const void* GetMapPointer() const { return m_mapped_pointer; }
|
||||
|
||||
ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_resource); }
|
||||
|
||||
bool Create(u32 width, u32 height, DXGI_FORMAT format, bool for_uploading);
|
||||
void Destroy(bool defer = true);
|
||||
|
||||
bool Map(bool writing);
|
||||
void Unmap();
|
||||
void Flush();
|
||||
|
||||
void CopyToTexture(u32 src_x, u32 src_y, ID3D12Resource* dst_texture, u32 dst_subresource, u32 dst_x, u32 dst_y,
|
||||
u32 width, u32 height);
|
||||
void CopyFromTexture(ID3D12Resource* src_texture, u32 src_subresource, u32 src_x, u32 src_y, u32 dst_x, u32 dst_y,
|
||||
u32 width, u32 height);
|
||||
|
||||
|
||||
bool ReadPixels(u32 x, u32 y, u32 width, u32 height, void* data, u32 row_pitch);
|
||||
|
||||
bool WritePixels(u32 x, u32 y, u32 width, u32 height, const void* data, u32 row_pitch);
|
||||
|
||||
bool EnsureSize(u32 width, u32 height, DXGI_FORMAT format, bool for_uploading);
|
||||
|
||||
protected:
|
||||
ComPtr<ID3D12Resource> m_resource;
|
||||
u32 m_width;
|
||||
u32 m_height;
|
||||
DXGI_FORMAT m_format;
|
||||
u32 m_texel_size;
|
||||
u32 m_row_pitch;
|
||||
u32 m_buffer_size;
|
||||
|
||||
void* m_mapped_pointer = nullptr;
|
||||
u64 m_completed_fence = 0;
|
||||
bool m_mapped_for_write = false;
|
||||
bool m_needs_flush = false;
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
263
src/common/d3d12/stream_buffer.cpp
Normal file
263
src/common/d3d12/stream_buffer.cpp
Normal file
|
@ -0,0 +1,263 @@
|
|||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "stream_buffer.h"
|
||||
#include "../align.h"
|
||||
#include "../assert.h"
|
||||
#include "../log.h"
|
||||
#include "context.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
Log_SetChannel(D3D12::StreamBuffer);
|
||||
|
||||
namespace D3D12 {
|
||||
StreamBuffer::StreamBuffer() = default;
|
||||
|
||||
StreamBuffer::~StreamBuffer()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool StreamBuffer::Create(u32 size)
|
||||
{
|
||||
static const D3D12_HEAP_PROPERTIES heap_properties = {D3D12_HEAP_TYPE_UPLOAD};
|
||||
const D3D12_RESOURCE_DESC resource_desc = {
|
||||
D3D12_RESOURCE_DIMENSION_BUFFER, 0, size, 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
|
||||
D3D12_RESOURCE_FLAG_NONE};
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12Resource> buffer;
|
||||
|
||||
HRESULT hr = g_d3d12_context->GetDevice()->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE,
|
||||
&resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ,
|
||||
nullptr, IID_PPV_ARGS(buffer.GetAddressOf()));
|
||||
AssertMsg(SUCCEEDED(hr), "Allocate buffer");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
static const D3D12_RANGE read_range = {};
|
||||
u8* host_pointer;
|
||||
hr = buffer->Map(0, &read_range, reinterpret_cast<void**>(&host_pointer));
|
||||
AssertMsg(SUCCEEDED(hr), "Map buffer");
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
Destroy(true);
|
||||
|
||||
m_buffer = std::move(buffer);
|
||||
m_host_pointer = host_pointer;
|
||||
m_size = size;
|
||||
m_gpu_pointer = m_buffer->GetGPUVirtualAddress();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StreamBuffer::ReserveMemory(u32 num_bytes, u32 alignment)
|
||||
{
|
||||
const u32 required_bytes = num_bytes + alignment;
|
||||
|
||||
// Check for sane allocations
|
||||
if (required_bytes > m_size)
|
||||
{
|
||||
Log_ErrorPrintf("Attempting to allocate %u bytes from a %u byte stream buffer", static_cast<u32>(num_bytes),
|
||||
static_cast<u32>(m_size));
|
||||
Panic("Stream buffer overflow");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is the GPU behind or up to date with our current offset?
|
||||
UpdateCurrentFencePosition();
|
||||
if (m_current_offset >= m_current_gpu_position)
|
||||
{
|
||||
const u32 remaining_bytes = m_size - m_current_offset;
|
||||
if (required_bytes <= remaining_bytes)
|
||||
{
|
||||
// Place at the current position, after the GPU position.
|
||||
m_current_offset = Common::AlignUp(m_current_offset, alignment);
|
||||
m_current_space = m_size - m_current_offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check for space at the start of the buffer
|
||||
// We use < here because we don't want to have the case of m_current_offset ==
|
||||
// m_current_gpu_position. That would mean the code above would assume the
|
||||
// GPU has caught up to us, which it hasn't.
|
||||
if (required_bytes < m_current_gpu_position)
|
||||
{
|
||||
// Reset offset to zero, since we're allocating behind the gpu now
|
||||
m_current_offset = 0;
|
||||
m_current_space = m_current_gpu_position;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Is the GPU ahead of our current offset?
|
||||
if (m_current_offset < m_current_gpu_position)
|
||||
{
|
||||
// We have from m_current_offset..m_current_gpu_position space to use.
|
||||
const u32 remaining_bytes = m_current_gpu_position - m_current_offset;
|
||||
if (required_bytes < remaining_bytes)
|
||||
{
|
||||
// Place at the current position, since this is still behind the GPU.
|
||||
m_current_offset = Common::AlignUp(m_current_offset, alignment);
|
||||
m_current_space = m_current_gpu_position - m_current_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Can we find a fence to wait on that will give us enough memory?
|
||||
if (WaitForClearSpace(required_bytes))
|
||||
{
|
||||
const u32 align_diff = Common::AlignUp(m_current_offset, alignment) - m_current_offset;
|
||||
m_current_offset += align_diff;
|
||||
m_current_space -= align_diff;
|
||||
return true;
|
||||
}
|
||||
|
||||
// We tried everything we could, and still couldn't get anything. This means that too much space
|
||||
// in the buffer is being used by the command buffer currently being recorded. Therefore, the
|
||||
// only option is to execute it, and wait until it's done.
|
||||
return false;
|
||||
}
|
||||
|
||||
void StreamBuffer::CommitMemory(u32 final_num_bytes)
|
||||
{
|
||||
Assert((m_current_offset + final_num_bytes) <= m_size);
|
||||
Assert(final_num_bytes <= m_current_space);
|
||||
m_current_offset += final_num_bytes;
|
||||
m_current_space -= final_num_bytes;
|
||||
}
|
||||
|
||||
void StreamBuffer::Destroy(bool defer)
|
||||
{
|
||||
if (m_host_pointer)
|
||||
{
|
||||
const D3D12_RANGE written_range = {0, m_size};
|
||||
m_buffer->Unmap(0, &written_range);
|
||||
m_host_pointer = nullptr;
|
||||
}
|
||||
|
||||
if (m_buffer && defer)
|
||||
g_d3d12_context->DeferResourceDestruction(m_buffer.Get());
|
||||
m_buffer.Reset();
|
||||
|
||||
m_current_offset = 0;
|
||||
m_current_space = 0;
|
||||
m_current_gpu_position = 0;
|
||||
m_tracked_fences.clear();
|
||||
}
|
||||
|
||||
void StreamBuffer::UpdateCurrentFencePosition()
|
||||
{
|
||||
// Don't create a tracking entry if the GPU is caught up with the buffer.
|
||||
if (m_current_offset == m_current_gpu_position)
|
||||
return;
|
||||
|
||||
// Has the offset changed since the last fence?
|
||||
const u64 fence = g_d3d12_context->GetCurrentFenceValue();
|
||||
if (!m_tracked_fences.empty() && m_tracked_fences.back().first == fence)
|
||||
{
|
||||
// Still haven't executed a command buffer, so just update the offset.
|
||||
m_tracked_fences.back().second = m_current_offset;
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateGPUPosition();
|
||||
m_tracked_fences.emplace_back(fence, m_current_offset);
|
||||
}
|
||||
|
||||
void StreamBuffer::UpdateGPUPosition()
|
||||
{
|
||||
auto start = m_tracked_fences.begin();
|
||||
auto end = start;
|
||||
|
||||
const u64 completed_counter = g_d3d12_context->GetCompletedFenceValue();
|
||||
while (end != m_tracked_fences.end() && completed_counter >= end->first)
|
||||
{
|
||||
m_current_gpu_position = end->second;
|
||||
++end;
|
||||
}
|
||||
|
||||
if (start != end)
|
||||
m_tracked_fences.erase(start, end);
|
||||
}
|
||||
|
||||
bool StreamBuffer::WaitForClearSpace(u32 num_bytes)
|
||||
{
|
||||
u32 new_offset = 0;
|
||||
u32 new_space = 0;
|
||||
u32 new_gpu_position = 0;
|
||||
|
||||
auto iter = m_tracked_fences.begin();
|
||||
for (; iter != m_tracked_fences.end(); ++iter)
|
||||
{
|
||||
// Would this fence bring us in line with the GPU?
|
||||
// This is the "last resort" case, where a command buffer execution has been forced
|
||||
// after no additional data has been written to it, so we can assume that after the
|
||||
// fence has been signaled the entire buffer is now consumed.
|
||||
u32 gpu_position = iter->second;
|
||||
if (m_current_offset == gpu_position)
|
||||
{
|
||||
new_offset = 0;
|
||||
new_space = m_size;
|
||||
new_gpu_position = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Assuming that we wait for this fence, are we allocating in front of the GPU?
|
||||
if (m_current_offset > gpu_position)
|
||||
{
|
||||
// This would suggest the GPU has now followed us and wrapped around, so we have from
|
||||
// m_current_position..m_size free, as well as and 0..gpu_position.
|
||||
const u32 remaining_space_after_offset = m_size - m_current_offset;
|
||||
if (remaining_space_after_offset >= num_bytes)
|
||||
{
|
||||
// Switch to allocating in front of the GPU, using the remainder of the buffer.
|
||||
new_offset = m_current_offset;
|
||||
new_space = m_size - m_current_offset;
|
||||
new_gpu_position = gpu_position;
|
||||
break;
|
||||
}
|
||||
|
||||
// We can wrap around to the start, behind the GPU, if there is enough space.
|
||||
// We use > here because otherwise we'd end up lining up with the GPU, and then the
|
||||
// allocator would assume that the GPU has consumed what we just wrote.
|
||||
if (gpu_position > num_bytes)
|
||||
{
|
||||
new_offset = 0;
|
||||
new_space = gpu_position;
|
||||
new_gpu_position = gpu_position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We're currently allocating behind the GPU. This would give us between the current
|
||||
// offset and the GPU position worth of space to work with. Again, > because we can't
|
||||
// align the GPU position with the buffer offset.
|
||||
u32 available_space_inbetween = gpu_position - m_current_offset;
|
||||
if (available_space_inbetween > num_bytes)
|
||||
{
|
||||
// Leave the offset as-is, but update the GPU position.
|
||||
new_offset = m_current_offset;
|
||||
new_space = gpu_position - m_current_offset;
|
||||
new_gpu_position = gpu_position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Did any fences satisfy this condition?
|
||||
// Has the command buffer been executed yet? If not, the caller should execute it.
|
||||
if (iter == m_tracked_fences.end() || iter->first == g_d3d12_context->GetCurrentFenceValue())
|
||||
return false;
|
||||
|
||||
// Wait until this fence is signaled. This will fire the callback, updating the GPU position.
|
||||
g_d3d12_context->WaitForFence(iter->first);
|
||||
m_tracked_fences.erase(m_tracked_fences.begin(), m_current_offset == iter->second ? m_tracked_fences.end() : ++iter);
|
||||
m_current_offset = new_offset;
|
||||
m_current_space = new_space;
|
||||
m_current_gpu_position = new_gpu_position;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace D3D12
|
58
src/common/d3d12/stream_buffer.h
Normal file
58
src/common/d3d12/stream_buffer.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include <d3d12.h>
|
||||
#include <deque>
|
||||
#include <utility>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace D3D12 {
|
||||
class StreamBuffer
|
||||
{
|
||||
public:
|
||||
StreamBuffer();
|
||||
~StreamBuffer();
|
||||
|
||||
bool Create(u32 size);
|
||||
|
||||
ALWAYS_INLINE bool IsValid() const { return static_cast<bool>(m_buffer); }
|
||||
ALWAYS_INLINE ID3D12Resource* GetBuffer() const { return m_buffer.Get(); }
|
||||
ALWAYS_INLINE D3D12_GPU_VIRTUAL_ADDRESS GetGPUPointer() const { return m_gpu_pointer; }
|
||||
ALWAYS_INLINE void* GetHostPointer() const { return m_host_pointer; }
|
||||
ALWAYS_INLINE void* GetCurrentHostPointer() const { return m_host_pointer + m_current_offset; }
|
||||
ALWAYS_INLINE D3D12_GPU_VIRTUAL_ADDRESS GetCurrentGPUPointer() const { return m_gpu_pointer + m_current_offset; }
|
||||
ALWAYS_INLINE u32 GetSize() const { return m_size; }
|
||||
ALWAYS_INLINE u32 GetCurrentOffset() const { return m_current_offset; }
|
||||
ALWAYS_INLINE u32 GetCurrentSpace() const { return m_current_space; }
|
||||
|
||||
bool ReserveMemory(u32 num_bytes, u32 alignment);
|
||||
void CommitMemory(u32 final_num_bytes);
|
||||
|
||||
void Destroy(bool defer = true);
|
||||
|
||||
private:
|
||||
void UpdateCurrentFencePosition();
|
||||
void UpdateGPUPosition();
|
||||
|
||||
// Waits for as many fences as needed to allocate num_bytes bytes from the buffer.
|
||||
bool WaitForClearSpace(u32 num_bytes);
|
||||
|
||||
u32 m_size = 0;
|
||||
u32 m_current_offset = 0;
|
||||
u32 m_current_space = 0;
|
||||
u32 m_current_gpu_position = 0;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12Resource> m_buffer;
|
||||
D3D12_GPU_VIRTUAL_ADDRESS m_gpu_pointer = {};
|
||||
u8* m_host_pointer = nullptr;
|
||||
|
||||
// List of fences and the corresponding positions in the buffer
|
||||
std::deque<std::pair<u64, u32>> m_tracked_fences;
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
390
src/common/d3d12/texture.cpp
Normal file
390
src/common/d3d12/texture.cpp
Normal file
|
@ -0,0 +1,390 @@
|
|||
#include "texture.h"
|
||||
#include "../align.h"
|
||||
#include "../assert.h"
|
||||
#include "../log.h"
|
||||
#include "context.h"
|
||||
#include "staging_texture.h"
|
||||
#include "stream_buffer.h"
|
||||
#include "util.h"
|
||||
Log_SetChannel(D3D12);
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
Texture::Texture() = default;
|
||||
|
||||
Texture::Texture(ID3D12Resource* resource, D3D12_RESOURCE_STATES state) : m_resource(std::move(resource))
|
||||
{
|
||||
const D3D12_RESOURCE_DESC desc = GetDesc();
|
||||
m_width = static_cast<u32>(desc.Width);
|
||||
m_height = desc.Height;
|
||||
m_samples = desc.SampleDesc.Count;
|
||||
m_format = desc.Format;
|
||||
}
|
||||
|
||||
Texture::Texture(Texture&& texture)
|
||||
: m_resource(std::move(texture.m_resource)), m_srv_descriptor(texture.m_srv_descriptor),
|
||||
m_rtv_or_dsv_descriptor(texture.m_rtv_or_dsv_descriptor), m_width(texture.m_width), m_height(texture.m_height),
|
||||
m_samples(texture.m_samples), m_format(texture.m_format), m_state(texture.m_state),
|
||||
m_is_depth_view(texture.m_is_depth_view)
|
||||
{
|
||||
texture.m_srv_descriptor = {};
|
||||
texture.m_rtv_or_dsv_descriptor = {};
|
||||
texture.m_width = 0;
|
||||
texture.m_height = 0;
|
||||
texture.m_samples = 0;
|
||||
texture.m_format = DXGI_FORMAT_UNKNOWN;
|
||||
texture.m_state = D3D12_RESOURCE_STATE_COMMON;
|
||||
texture.m_is_depth_view = false;
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
Texture& Texture::operator=(Texture&& texture)
|
||||
{
|
||||
Destroy();
|
||||
m_resource = std::move(texture.m_resource);
|
||||
m_srv_descriptor = texture.m_srv_descriptor;
|
||||
m_rtv_or_dsv_descriptor = texture.m_rtv_or_dsv_descriptor;
|
||||
m_width = texture.m_width;
|
||||
m_height = texture.m_height;
|
||||
m_samples = texture.m_samples;
|
||||
m_format = texture.m_format;
|
||||
m_state = texture.m_state;
|
||||
m_is_depth_view = texture.m_is_depth_view;
|
||||
texture.m_srv_descriptor = {};
|
||||
texture.m_rtv_or_dsv_descriptor = {};
|
||||
texture.m_width = 0;
|
||||
texture.m_height = 0;
|
||||
texture.m_samples = 0;
|
||||
texture.m_format = DXGI_FORMAT_UNKNOWN;
|
||||
texture.m_state = D3D12_RESOURCE_STATE_COMMON;
|
||||
texture.m_is_depth_view = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_DESC Texture::GetDesc() const
|
||||
{
|
||||
return m_resource->GetDesc();
|
||||
}
|
||||
|
||||
bool Texture::Create(u32 width, u32 height, u32 samples, DXGI_FORMAT format, DXGI_FORMAT srv_format,
|
||||
DXGI_FORMAT rtv_format, DXGI_FORMAT dsv_format, D3D12_RESOURCE_FLAGS flags)
|
||||
{
|
||||
constexpr D3D12_HEAP_PROPERTIES heap_properties = {D3D12_HEAP_TYPE_DEFAULT};
|
||||
|
||||
D3D12_RESOURCE_DESC desc = {};
|
||||
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
desc.Width = width;
|
||||
desc.Height = height;
|
||||
desc.DepthOrArraySize = 1;
|
||||
desc.MipLevels = 1;
|
||||
desc.Format = format;
|
||||
desc.SampleDesc.Count = samples;
|
||||
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
||||
desc.Flags = flags;
|
||||
|
||||
D3D12_CLEAR_VALUE optimized_clear_value = {};
|
||||
D3D12_RESOURCE_STATES state;
|
||||
if (rtv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
optimized_clear_value.Format = rtv_format;
|
||||
state = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
||||
}
|
||||
else if (dsv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
optimized_clear_value.Format = dsv_format;
|
||||
state = D3D12_RESOURCE_STATE_DEPTH_WRITE;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
|
||||
}
|
||||
|
||||
ComPtr<ID3D12Resource> resource;
|
||||
HRESULT hr = g_d3d12_context->GetDevice()->CreateCommittedResource(
|
||||
&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, state,
|
||||
(rtv_format != DXGI_FORMAT_UNKNOWN || dsv_format != DXGI_FORMAT_UNKNOWN) ? &optimized_clear_value : nullptr,
|
||||
IID_PPV_ARGS(resource.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Create texture failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
DescriptorHandle srv_descriptor, rtv_descriptor;
|
||||
bool is_depth_view = false;
|
||||
if (srv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
if (!CreateSRVDescriptor(resource.Get(), srv_format, samples > 1, &srv_descriptor))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rtv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
Assert(dsv_format == DXGI_FORMAT_UNKNOWN);
|
||||
if (!CreateRTVDescriptor(resource.Get(), rtv_format, samples > 1, &rtv_descriptor))
|
||||
{
|
||||
g_d3d12_context->GetDescriptorHeapManager().Free(&srv_descriptor);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (dsv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
if (!CreateDSVDescriptor(resource.Get(), dsv_format, samples > 1, &rtv_descriptor))
|
||||
{
|
||||
g_d3d12_context->GetDescriptorHeapManager().Free(&srv_descriptor);
|
||||
return false;
|
||||
}
|
||||
|
||||
is_depth_view = true;
|
||||
}
|
||||
|
||||
Destroy(true);
|
||||
|
||||
m_resource = std::move(resource);
|
||||
m_srv_descriptor = std::move(srv_descriptor);
|
||||
m_rtv_or_dsv_descriptor = std::move(rtv_descriptor);
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_samples = samples;
|
||||
m_format = format;
|
||||
m_state = state;
|
||||
m_is_depth_view = is_depth_view;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Texture::Adopt(ComPtr<ID3D12Resource> texture, DXGI_FORMAT srv_format, DXGI_FORMAT rtv_format,
|
||||
DXGI_FORMAT dsv_format, D3D12_RESOURCE_STATES state)
|
||||
{
|
||||
const D3D12_RESOURCE_DESC desc(texture->GetDesc());
|
||||
|
||||
DescriptorHandle srv_descriptor, rtv_descriptor;
|
||||
if (srv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
if (!CreateSRVDescriptor(texture.Get(), srv_format, desc.SampleDesc.Count > 1, &srv_descriptor))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rtv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
Assert(dsv_format == DXGI_FORMAT_UNKNOWN);
|
||||
if (!CreateRTVDescriptor(texture.Get(), rtv_format, desc.SampleDesc.Count > 1, &rtv_descriptor))
|
||||
{
|
||||
g_d3d12_context->GetDescriptorHeapManager().Free(&srv_descriptor);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (dsv_format != DXGI_FORMAT_UNKNOWN)
|
||||
{
|
||||
if (!CreateDSVDescriptor(texture.Get(), dsv_format, desc.SampleDesc.Count > 1, &rtv_descriptor))
|
||||
{
|
||||
g_d3d12_context->GetDescriptorHeapManager().Free(&srv_descriptor);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_resource = std::move(texture);
|
||||
m_srv_descriptor = std::move(srv_descriptor);
|
||||
m_rtv_or_dsv_descriptor = std::move(rtv_descriptor);
|
||||
m_width = static_cast<u32>(desc.Width);
|
||||
m_height = desc.Height;
|
||||
m_samples = desc.SampleDesc.Count;
|
||||
m_format = desc.Format;
|
||||
m_state = state;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::Destroy(bool defer /* = true */)
|
||||
{
|
||||
if (defer)
|
||||
{
|
||||
g_d3d12_context->DeferDescriptorDestruction(g_d3d12_context->GetDescriptorHeapManager(), &m_srv_descriptor);
|
||||
if (m_is_depth_view)
|
||||
g_d3d12_context->DeferDescriptorDestruction(g_d3d12_context->GetDSVHeapManager(), &m_rtv_or_dsv_descriptor);
|
||||
else
|
||||
g_d3d12_context->DeferDescriptorDestruction(g_d3d12_context->GetRTVHeapManager(), &m_rtv_or_dsv_descriptor);
|
||||
g_d3d12_context->DeferResourceDestruction(m_resource.Get());
|
||||
m_resource.Reset();
|
||||
}
|
||||
else
|
||||
{
|
||||
g_d3d12_context->GetDescriptorHeapManager().Free(&m_srv_descriptor);
|
||||
if (m_is_depth_view)
|
||||
g_d3d12_context->GetDSVHeapManager().Free(&m_rtv_or_dsv_descriptor);
|
||||
else
|
||||
g_d3d12_context->GetRTVHeapManager().Free(&m_rtv_or_dsv_descriptor);
|
||||
|
||||
m_resource.Reset();
|
||||
}
|
||||
|
||||
m_width = 0;
|
||||
m_height = 0;
|
||||
m_samples = 0;
|
||||
m_format = DXGI_FORMAT_UNKNOWN;
|
||||
m_is_depth_view = false;
|
||||
}
|
||||
|
||||
void Texture::TransitionToState(D3D12_RESOURCE_STATES state) const
|
||||
{
|
||||
if (m_state == state)
|
||||
return;
|
||||
|
||||
ResourceBarrier(g_d3d12_context->GetCommandList(), m_resource.Get(), m_state, state);
|
||||
m_state = state;
|
||||
}
|
||||
|
||||
bool Texture::BeginStreamUpdate(u32 x, u32 y, u32 width, u32 height, void** out_data, u32* out_data_pitch)
|
||||
{
|
||||
const u32 copy_pitch = Common::AlignUpPow2(width * GetTexelSize(m_format), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
|
||||
const u32 upload_size = copy_pitch * height;
|
||||
|
||||
if (!g_d3d12_context->GetTextureStreamBuffer().ReserveMemory(upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT))
|
||||
{
|
||||
Log_PerfPrintf("Executing command buffer while waiting for %u bytes (%ux%u) in upload buffer", upload_size, width,
|
||||
height);
|
||||
g_d3d12_context->ExecuteCommandList(false);
|
||||
if (!g_d3d12_context->GetTextureStreamBuffer().ReserveMemory(upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to reserve %u bytes for %ux%u upload", upload_size, width, height);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*out_data = g_d3d12_context->GetTextureStreamBuffer().GetCurrentHostPointer();
|
||||
*out_data_pitch = copy_pitch;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::EndStreamUpdate(u32 x, u32 y, u32 width, u32 height)
|
||||
{
|
||||
const u32 copy_pitch = Common::AlignUpPow2(width * GetTexelSize(m_format), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
|
||||
const u32 upload_size = copy_pitch * height;
|
||||
|
||||
StreamBuffer& sb = g_d3d12_context->GetTextureStreamBuffer();
|
||||
const u32 sb_offset = sb.GetCurrentOffset();
|
||||
sb.CommitMemory(upload_size);
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION src;
|
||||
src.pResource = sb.GetBuffer();
|
||||
src.SubresourceIndex = 0;
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
src.PlacedFootprint.Offset = sb_offset;
|
||||
src.PlacedFootprint.Footprint.Width = width;
|
||||
src.PlacedFootprint.Footprint.Height = height;
|
||||
src.PlacedFootprint.Footprint.Depth = 1;
|
||||
src.PlacedFootprint.Footprint.RowPitch = copy_pitch;
|
||||
src.PlacedFootprint.Footprint.Format = m_format;
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION dst;
|
||||
dst.pResource = m_resource.Get();
|
||||
dst.SubresourceIndex = 0;
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
|
||||
const D3D12_BOX src_box{0u, 0u, 0u, width, height, 1u};
|
||||
const D3D12_RESOURCE_STATES old_state = m_state;
|
||||
TransitionToState(D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
g_d3d12_context->GetCommandList()->CopyTextureRegion(&dst, 0, 0, 0, &src, &src_box);
|
||||
TransitionToState(old_state);
|
||||
}
|
||||
|
||||
bool Texture::LoadData(u32 x, u32 y, u32 width, u32 height, const void* data, u32 pitch)
|
||||
{
|
||||
const u32 texel_size = GetTexelSize(m_format);
|
||||
const u32 upload_pitch = Common::AlignUpPow2(width * texel_size, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
|
||||
const u32 upload_size = upload_pitch * height;
|
||||
if (upload_size > g_d3d12_context->GetTextureStreamBuffer().GetSize())
|
||||
{
|
||||
StagingTexture st;
|
||||
if (!st.Create(width, height, m_format, true) || !st.WritePixels(0, 0, width, height, data, pitch))
|
||||
return false;
|
||||
|
||||
D3D12_RESOURCE_STATES old_state = m_state;
|
||||
TransitionToState(D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
st.CopyToTexture(0, 0, m_resource.Get(), 0, x, y, width, height);
|
||||
st.Destroy(true);
|
||||
TransitionToState(old_state);
|
||||
return true;
|
||||
}
|
||||
|
||||
void* write_ptr;
|
||||
u32 write_pitch;
|
||||
if (!BeginStreamUpdate(x, y, width, height, &write_ptr, &write_pitch))
|
||||
return false;
|
||||
|
||||
CopyToUploadBuffer(data, pitch, height, write_ptr, write_pitch);
|
||||
EndStreamUpdate(x, y, width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::CopyToUploadBuffer(const void* src_data, u32 src_pitch, u32 height, void* dst_data, u32 dst_pitch)
|
||||
{
|
||||
const u8* src_ptr = static_cast<const u8*>(src_data);
|
||||
u8* dst_ptr = static_cast<u8*>(dst_data);
|
||||
if (src_pitch == dst_pitch)
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, dst_pitch * height);
|
||||
}
|
||||
else
|
||||
{
|
||||
const u32 copy_size = std::min(src_pitch, dst_pitch);
|
||||
for (u32 row = 0; row < height; row++)
|
||||
{
|
||||
std::memcpy(dst_ptr, src_ptr, copy_size);
|
||||
src_ptr += src_pitch;
|
||||
dst_ptr += dst_pitch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Texture::CreateSRVDescriptor(ID3D12Resource* resource, DXGI_FORMAT format, bool multisampled, DescriptorHandle* dh)
|
||||
{
|
||||
if (!g_d3d12_context->GetDescriptorHeapManager().Allocate(dh))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to allocate SRV descriptor");
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {
|
||||
format, multisampled ? D3D12_SRV_DIMENSION_TEXTURE2DMS : D3D12_SRV_DIMENSION_TEXTURE2D,
|
||||
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
|
||||
if (!multisampled)
|
||||
desc.Texture2D.MipLevels = 1;
|
||||
|
||||
g_d3d12_context->GetDevice()->CreateShaderResourceView(resource, &desc, dh->cpu_handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Texture::CreateRTVDescriptor(ID3D12Resource* resource, DXGI_FORMAT format, bool multisampled, DescriptorHandle* dh)
|
||||
{
|
||||
if (!g_d3d12_context->GetRTVHeapManager().Allocate(dh))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to allocate SRV descriptor");
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12_RENDER_TARGET_VIEW_DESC desc = {format,
|
||||
multisampled ? D3D12_RTV_DIMENSION_TEXTURE2DMS : D3D12_RTV_DIMENSION_TEXTURE2D};
|
||||
|
||||
g_d3d12_context->GetDevice()->CreateRenderTargetView(resource, &desc, dh->cpu_handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Texture::CreateDSVDescriptor(ID3D12Resource* resource, DXGI_FORMAT format, bool multisampled, DescriptorHandle* dh)
|
||||
{
|
||||
if (!g_d3d12_context->GetDSVHeapManager().Allocate(dh))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to allocate SRV descriptor");
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12_DEPTH_STENCIL_VIEW_DESC desc = {
|
||||
format, multisampled ? D3D12_DSV_DIMENSION_TEXTURE2DMS : D3D12_DSV_DIMENSION_TEXTURE2D, D3D12_DSV_FLAG_NONE};
|
||||
|
||||
g_d3d12_context->GetDevice()->CreateDepthStencilView(resource, &desc, dh->cpu_handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace D3D12
|
80
src/common/d3d12/texture.h
Normal file
80
src/common/d3d12/texture.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
#pragma once
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include "descriptor_heap_manager.h"
|
||||
#include <d3d12.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
class StreamBuffer;
|
||||
|
||||
class Texture final
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
Texture();
|
||||
Texture(ID3D12Resource* resource, D3D12_RESOURCE_STATES state);
|
||||
Texture(Texture&& texture);
|
||||
Texture(const Texture&) = delete;
|
||||
~Texture();
|
||||
|
||||
ALWAYS_INLINE ID3D12Resource* GetResource() const { return m_resource.Get(); }
|
||||
ALWAYS_INLINE const DescriptorHandle& GetSRVDescriptor() const { return m_srv_descriptor; }
|
||||
ALWAYS_INLINE const DescriptorHandle& GetRTVOrDSVDescriptor() const { return m_rtv_or_dsv_descriptor; }
|
||||
ALWAYS_INLINE D3D12_RESOURCE_STATES GetState() const { return m_state; }
|
||||
|
||||
ALWAYS_INLINE u32 GetWidth() const { return m_width; }
|
||||
ALWAYS_INLINE u32 GetHeight() const { return m_height; }
|
||||
ALWAYS_INLINE u32 GetSamples() const { return m_samples; }
|
||||
ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return m_format; }
|
||||
ALWAYS_INLINE bool IsMultisampled() const { return m_samples > 1; }
|
||||
|
||||
ALWAYS_INLINE operator ID3D12Resource*() const { return m_resource.Get(); }
|
||||
ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_resource); }
|
||||
|
||||
bool Create(u32 width, u32 height, u32 samples, DXGI_FORMAT format, DXGI_FORMAT srv_format, DXGI_FORMAT rtv_format,
|
||||
DXGI_FORMAT dsv_format, D3D12_RESOURCE_FLAGS flags);
|
||||
bool Adopt(ComPtr<ID3D12Resource> texture, DXGI_FORMAT srv_format, DXGI_FORMAT rtv_format, DXGI_FORMAT dsv_format,
|
||||
D3D12_RESOURCE_STATES state);
|
||||
|
||||
D3D12_RESOURCE_DESC GetDesc() const;
|
||||
|
||||
void Destroy(bool defer = true);
|
||||
|
||||
void TransitionToState(D3D12_RESOURCE_STATES state) const;
|
||||
|
||||
Texture& operator=(const Texture&) = delete;
|
||||
Texture& operator=(Texture&& texture);
|
||||
|
||||
bool BeginStreamUpdate(u32 x, u32 y, u32 width, u32 height, void** out_data, u32* out_data_pitch);
|
||||
void EndStreamUpdate(u32 x, u32 y, u32 width, u32 height);
|
||||
|
||||
bool LoadData(u32 x, u32 y, u32 width, u32 height, const void* data, u32 pitch);
|
||||
|
||||
static void CopyToUploadBuffer(const void* src_data, u32 src_pitch, u32 height, void* dst_data, u32 dst_pitch);
|
||||
|
||||
private:
|
||||
static bool CreateSRVDescriptor(ID3D12Resource* resource, DXGI_FORMAT format, bool multisampled,
|
||||
DescriptorHandle* dh);
|
||||
static bool CreateRTVDescriptor(ID3D12Resource* resource, DXGI_FORMAT format, bool multisampled,
|
||||
DescriptorHandle* dh);
|
||||
static bool CreateDSVDescriptor(ID3D12Resource* resource, DXGI_FORMAT format, bool multisampled,
|
||||
DescriptorHandle* dh);
|
||||
|
||||
ComPtr<ID3D12Resource> m_resource;
|
||||
DescriptorHandle m_srv_descriptor = {};
|
||||
DescriptorHandle m_rtv_or_dsv_descriptor = {};
|
||||
u32 m_width = 0;
|
||||
u32 m_height = 0;
|
||||
u32 m_samples = 0;
|
||||
DXGI_FORMAT m_format = DXGI_FORMAT_UNKNOWN;
|
||||
|
||||
mutable D3D12_RESOURCE_STATES m_state = D3D12_RESOURCE_STATE_COMMON;
|
||||
|
||||
bool m_is_depth_view = false;
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
343
src/common/d3d12/util.cpp
Normal file
343
src/common/d3d12/util.cpp
Normal file
|
@ -0,0 +1,343 @@
|
|||
#include "util.h"
|
||||
#include "../assert.h"
|
||||
#include "../log.h"
|
||||
#include "../string.h"
|
||||
#include "../string_util.h"
|
||||
#include "context.h"
|
||||
#include "shader_cache.h"
|
||||
#include <cstdarg>
|
||||
#include <limits>
|
||||
Log_SetChannel(D3D12);
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
u32 GetTexelSize(DXGI_FORMAT format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case DXGI_FORMAT_R8G8B8A8_UNORM:
|
||||
case DXGI_FORMAT_R8G8B8A8_SNORM:
|
||||
case DXGI_FORMAT_R8G8B8A8_TYPELESS:
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM:
|
||||
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
|
||||
return 4;
|
||||
|
||||
case DXGI_FORMAT_B5G5R5A1_UNORM:
|
||||
case DXGI_FORMAT_B5G6R5_UNORM:
|
||||
return 2;
|
||||
|
||||
default:
|
||||
Panic("Unknown format");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void SetDefaultSampler(D3D12_SAMPLER_DESC* desc)
|
||||
{
|
||||
desc->Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
desc->AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
|
||||
desc->AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
|
||||
desc->AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
|
||||
desc->MipLODBias = 0;
|
||||
desc->MaxAnisotropy = 1;
|
||||
desc->ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
|
||||
desc->BorderColor[0] = 1.0f;
|
||||
desc->BorderColor[1] = 1.0f;
|
||||
desc->BorderColor[2] = 1.0f;
|
||||
desc->BorderColor[3] = 1.0f;
|
||||
desc->MinLOD = -3.402823466e+38F; // -FLT_MAX
|
||||
desc->MaxLOD = 3.402823466e+38F; // FLT_MAX
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
void SetObjectName(ID3D12Object* object, const char* name)
|
||||
{
|
||||
object->SetName(StringUtil::UTF8StringToWideString(name).c_str());
|
||||
}
|
||||
|
||||
void SetObjectNameFormatted(ID3D12Object* object, const char* format, ...)
|
||||
{
|
||||
std::va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
SmallString str;
|
||||
str.FormatVA(format, ap);
|
||||
|
||||
SetObjectName(object, str);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
GraphicsPipelineBuilder::GraphicsPipelineBuilder()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::Clear()
|
||||
{
|
||||
std::memset(&m_desc, 0, sizeof(m_desc));
|
||||
std::memset(m_input_elements.data(), 0, sizeof(D3D12_INPUT_ELEMENT_DESC) * m_input_elements.size());
|
||||
m_desc.NodeMask = 1;
|
||||
m_desc.SampleMask = 0xFFFFFFFF;
|
||||
m_desc.SampleDesc.Count = 1;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12PipelineState> GraphicsPipelineBuilder::Create(ID3D12Device* device, bool clear /*= true*/)
|
||||
{
|
||||
Microsoft::WRL::ComPtr<ID3D12PipelineState> ps;
|
||||
HRESULT hr = device->CreateGraphicsPipelineState(&m_desc, IID_PPV_ARGS(ps.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("CreateGraphicsPipelineState() failed: %08X", hr);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (clear)
|
||||
Clear();
|
||||
|
||||
return ps;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12PipelineState> GraphicsPipelineBuilder::Create(ID3D12Device* device, ShaderCache& cache,
|
||||
bool clear /*= true*/)
|
||||
{
|
||||
Microsoft::WRL::ComPtr<ID3D12PipelineState> pso = cache.GetPipelineState(device, m_desc);
|
||||
if (!pso)
|
||||
return {};
|
||||
|
||||
if (clear)
|
||||
Clear();
|
||||
|
||||
return pso;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetRootSignature(ID3D12RootSignature* rs)
|
||||
{
|
||||
m_desc.pRootSignature = rs;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetVertexShader(ID3DBlob* blob)
|
||||
{
|
||||
SetVertexShader(blob->GetBufferPointer(), static_cast<u32>(blob->GetBufferSize()));
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetVertexShader(const void* data, u32 data_size)
|
||||
{
|
||||
m_desc.VS.pShaderBytecode = data;
|
||||
m_desc.VS.BytecodeLength = data_size;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetGeometryShader(ID3DBlob* blob)
|
||||
{
|
||||
SetGeometryShader(blob->GetBufferPointer(), static_cast<u32>(blob->GetBufferSize()));
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetGeometryShader(const void* data, u32 data_size)
|
||||
{
|
||||
m_desc.GS.pShaderBytecode = data;
|
||||
m_desc.GS.BytecodeLength = data_size;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetPixelShader(ID3DBlob* blob)
|
||||
{
|
||||
SetPixelShader(blob->GetBufferPointer(), static_cast<u32>(blob->GetBufferSize()));
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetPixelShader(const void* data, u32 data_size)
|
||||
{
|
||||
m_desc.PS.pShaderBytecode = data;
|
||||
m_desc.PS.BytecodeLength = data_size;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::AddVertexAttribute(const char* semantic_name, u32 semantic_index, DXGI_FORMAT format,
|
||||
u32 buffer, u32 offset)
|
||||
{
|
||||
const u32 index = m_desc.InputLayout.NumElements;
|
||||
m_input_elements[index].SemanticIndex = semantic_index;
|
||||
m_input_elements[index].SemanticName = semantic_name;
|
||||
m_input_elements[index].Format = format;
|
||||
m_input_elements[index].AlignedByteOffset = offset;
|
||||
m_input_elements[index].InputSlot = buffer;
|
||||
m_input_elements[index].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
|
||||
m_input_elements[index].InstanceDataStepRate = 0;
|
||||
|
||||
m_desc.InputLayout.pInputElementDescs = m_input_elements.data();
|
||||
m_desc.InputLayout.NumElements++;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE type)
|
||||
{
|
||||
m_desc.PrimitiveTopologyType = type;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetRasterizationState(D3D12_FILL_MODE polygon_mode, D3D12_CULL_MODE cull_mode,
|
||||
bool front_face_ccw)
|
||||
{
|
||||
m_desc.RasterizerState.FillMode = polygon_mode;
|
||||
m_desc.RasterizerState.CullMode = cull_mode;
|
||||
m_desc.RasterizerState.FrontCounterClockwise = front_face_ccw;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetMultisamples(u32 multisamples)
|
||||
{
|
||||
m_desc.RasterizerState.MultisampleEnable = multisamples > 1;
|
||||
m_desc.SampleDesc.Count = multisamples;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetNoCullRasterizationState()
|
||||
{
|
||||
SetRasterizationState(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_NONE, false);
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetDepthState(bool depth_test, bool depth_write, D3D12_COMPARISON_FUNC compare_op)
|
||||
{
|
||||
m_desc.DepthStencilState.DepthEnable = depth_test;
|
||||
m_desc.DepthStencilState.DepthWriteMask = depth_test ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO;
|
||||
m_desc.DepthStencilState.DepthFunc = compare_op;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetNoDepthTestState()
|
||||
{
|
||||
SetDepthState(false, false, D3D12_COMPARISON_FUNC_ALWAYS);
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetBlendState(u32 rt, bool blend_enable, D3D12_BLEND src_factor, D3D12_BLEND dst_factor,
|
||||
D3D12_BLEND_OP op, D3D12_BLEND alpha_src_factor,
|
||||
D3D12_BLEND alpha_dst_factor, D3D12_BLEND_OP alpha_op,
|
||||
u8 write_mask /*= 0xFF*/)
|
||||
{
|
||||
m_desc.BlendState.RenderTarget[rt].BlendEnable = blend_enable;
|
||||
m_desc.BlendState.RenderTarget[rt].SrcBlend = src_factor;
|
||||
m_desc.BlendState.RenderTarget[rt].DestBlend = dst_factor;
|
||||
m_desc.BlendState.RenderTarget[rt].BlendOp = op;
|
||||
m_desc.BlendState.RenderTarget[rt].SrcBlendAlpha = alpha_src_factor;
|
||||
m_desc.BlendState.RenderTarget[rt].DestBlendAlpha = alpha_dst_factor;
|
||||
m_desc.BlendState.RenderTarget[rt].BlendOpAlpha = alpha_op;
|
||||
m_desc.BlendState.RenderTarget[rt].RenderTargetWriteMask = write_mask;
|
||||
|
||||
if (rt > 0)
|
||||
m_desc.BlendState.IndependentBlendEnable = TRUE;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetNoBlendingState()
|
||||
{
|
||||
SetBlendState(0, false, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_BLEND_ONE, D3D12_BLEND_ZERO,
|
||||
D3D12_BLEND_OP_ADD, D3D12_COLOR_WRITE_ENABLE_ALL);
|
||||
m_desc.BlendState.IndependentBlendEnable = FALSE;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::ClearRenderTargets()
|
||||
{
|
||||
m_desc.NumRenderTargets = 0;
|
||||
for (u32 i = 0; i < sizeof(m_desc.RTVFormats) / sizeof(m_desc.RTVFormats[0]); i++)
|
||||
m_desc.RTVFormats[i] = DXGI_FORMAT_UNKNOWN;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetRenderTarget(u32 rt, DXGI_FORMAT format)
|
||||
{
|
||||
m_desc.RTVFormats[rt] = format;
|
||||
if (rt >= m_desc.NumRenderTargets)
|
||||
m_desc.NumRenderTargets = rt + 1;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::ClearDepthStencilFormat()
|
||||
{
|
||||
m_desc.DSVFormat = DXGI_FORMAT_UNKNOWN;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetDepthStencilFormat(DXGI_FORMAT format)
|
||||
{
|
||||
m_desc.DSVFormat = format;
|
||||
}
|
||||
|
||||
RootSignatureBuilder::RootSignatureBuilder()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void RootSignatureBuilder::Clear()
|
||||
{
|
||||
m_desc = {};
|
||||
m_desc.pParameters = m_params.data();
|
||||
m_params = {};
|
||||
m_descriptor_ranges = {};
|
||||
m_num_descriptor_ranges = 0;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12RootSignature> RootSignatureBuilder::Create(bool clear /*= true*/)
|
||||
{
|
||||
Microsoft::WRL::ComPtr<ID3D12RootSignature> rs = g_d3d12_context->CreateRootSignature(&m_desc);
|
||||
if (!rs)
|
||||
return {};
|
||||
|
||||
if (clear)
|
||||
Clear();
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
void RootSignatureBuilder::SetInputAssemblerFlag()
|
||||
{
|
||||
m_desc.Flags |= D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
|
||||
}
|
||||
|
||||
u32 RootSignatureBuilder::Add32BitConstants(u32 shader_reg, u32 num_values, D3D12_SHADER_VISIBILITY visibility)
|
||||
{
|
||||
const u32 index = m_desc.NumParameters++;
|
||||
|
||||
m_params[index].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
|
||||
m_params[index].ShaderVisibility = visibility;
|
||||
m_params[index].Constants.ShaderRegister = shader_reg;
|
||||
m_params[index].Constants.RegisterSpace = 0;
|
||||
m_params[index].Constants.Num32BitValues = num_values;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
u32 RootSignatureBuilder::AddCBVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility)
|
||||
{
|
||||
const u32 index = m_desc.NumParameters++;
|
||||
|
||||
m_params[index].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
|
||||
m_params[index].ShaderVisibility = visibility;
|
||||
m_params[index].Descriptor.ShaderRegister = shader_reg;
|
||||
m_params[index].Descriptor.RegisterSpace = 0;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
u32 RootSignatureBuilder::AddSRVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility)
|
||||
{
|
||||
const u32 index = m_desc.NumParameters++;
|
||||
|
||||
m_params[index].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
|
||||
m_params[index].ShaderVisibility = visibility;
|
||||
m_params[index].Descriptor.ShaderRegister = shader_reg;
|
||||
m_params[index].Descriptor.RegisterSpace = 0;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
u32 RootSignatureBuilder::AddDescriptorTable(D3D12_DESCRIPTOR_RANGE_TYPE rt, u32 start_shader_reg, u32 num_shader_regs,
|
||||
D3D12_SHADER_VISIBILITY visibility)
|
||||
{
|
||||
const u32 index = m_desc.NumParameters++;
|
||||
const u32 dr_index = m_num_descriptor_ranges++;
|
||||
|
||||
m_descriptor_ranges[dr_index].RangeType = rt;
|
||||
m_descriptor_ranges[dr_index].NumDescriptors = num_shader_regs;
|
||||
m_descriptor_ranges[dr_index].BaseShaderRegister = start_shader_reg;
|
||||
m_descriptor_ranges[dr_index].RegisterSpace = 0;
|
||||
m_descriptor_ranges[dr_index].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
|
||||
|
||||
m_params[index].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
||||
m_params[index].DescriptorTable.pDescriptorRanges = &m_descriptor_ranges[dr_index];
|
||||
m_params[index].DescriptorTable.NumDescriptorRanges = 1;
|
||||
m_params[index].ShaderVisibility = visibility;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
} // namespace D3D12
|
152
src/common/d3d12/util.h
Normal file
152
src/common/d3d12/util.h
Normal file
|
@ -0,0 +1,152 @@
|
|||
#pragma once
|
||||
#include "../types.h"
|
||||
#include "../windows_headers.h"
|
||||
#include <array>
|
||||
#include <d3d12.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace D3D12 {
|
||||
|
||||
class ShaderCache;
|
||||
|
||||
static inline void ResourceBarrier(ID3D12GraphicsCommandList* cmdlist, ID3D12Resource* resource,
|
||||
D3D12_RESOURCE_STATES from_state, D3D12_RESOURCE_STATES to_state)
|
||||
{
|
||||
const D3D12_RESOURCE_BARRIER barrier = {D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
|
||||
D3D12_RESOURCE_BARRIER_FLAG_NONE,
|
||||
{{resource, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, from_state, to_state}}};
|
||||
cmdlist->ResourceBarrier(1, &barrier);
|
||||
}
|
||||
|
||||
static inline void SetViewport(ID3D12GraphicsCommandList* cmdlist, int x, int y, int width, int height,
|
||||
float min_depth = 0.0f, float max_depth = 1.0f)
|
||||
{
|
||||
const D3D12_VIEWPORT vp{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(width),
|
||||
static_cast<float>(height),
|
||||
min_depth,
|
||||
max_depth};
|
||||
cmdlist->RSSetViewports(1, &vp);
|
||||
}
|
||||
|
||||
static inline void SetScissor(ID3D12GraphicsCommandList* cmdlist, int x, int y, int width, int height)
|
||||
{
|
||||
const D3D12_RECT r{x, y, x + width, y + height};
|
||||
cmdlist->RSSetScissorRects(1, &r);
|
||||
}
|
||||
|
||||
static inline void SetViewportAndScissor(ID3D12GraphicsCommandList* cmdlist, int x, int y, int width, int height,
|
||||
float min_depth = 0.0f, float max_depth = 1.0f)
|
||||
{
|
||||
SetViewport(cmdlist, x, y, width, height, min_depth, max_depth);
|
||||
SetScissor(cmdlist, x, y, width, height);
|
||||
}
|
||||
|
||||
u32 GetTexelSize(DXGI_FORMAT format);
|
||||
|
||||
void SetDefaultSampler(D3D12_SAMPLER_DESC* desc);
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
void SetObjectName(ID3D12Object* object, const char* name);
|
||||
void SetObjectNameFormatted(ID3D12Object* object, const char* format, ...);
|
||||
|
||||
#else
|
||||
|
||||
static inline void SetObjectName(ID3D12Object* object, const char* name) {}
|
||||
static inline void SetObjectNameFormatted(ID3D12Object* object, const char* format, ...) {}
|
||||
|
||||
#endif
|
||||
|
||||
class RootSignatureBuilder
|
||||
{
|
||||
public:
|
||||
enum : u32
|
||||
{
|
||||
MAX_PARAMETERS = 16,
|
||||
MAX_DESCRIPTOR_RANGES = 16
|
||||
};
|
||||
|
||||
RootSignatureBuilder();
|
||||
|
||||
void Clear();
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12RootSignature> Create(bool clear = true);
|
||||
|
||||
void SetInputAssemblerFlag();
|
||||
|
||||
u32 Add32BitConstants(u32 shader_reg, u32 num_values, D3D12_SHADER_VISIBILITY visibility);
|
||||
u32 AddCBVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility);
|
||||
u32 AddSRVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility);
|
||||
u32 AddDescriptorTable(D3D12_DESCRIPTOR_RANGE_TYPE rt, u32 start_shader_reg, u32 num_shader_regs,
|
||||
D3D12_SHADER_VISIBILITY visibility);
|
||||
|
||||
private:
|
||||
D3D12_ROOT_SIGNATURE_DESC m_desc{};
|
||||
std::array<D3D12_ROOT_PARAMETER, MAX_PARAMETERS> m_params{};
|
||||
std::array<D3D12_DESCRIPTOR_RANGE, MAX_DESCRIPTOR_RANGES> m_descriptor_ranges{};
|
||||
u32 m_num_descriptor_ranges = 0;
|
||||
};
|
||||
|
||||
class GraphicsPipelineBuilder
|
||||
{
|
||||
public:
|
||||
enum : u32
|
||||
{
|
||||
MAX_VERTEX_ATTRIBUTES = 16,
|
||||
};
|
||||
|
||||
GraphicsPipelineBuilder();
|
||||
|
||||
~GraphicsPipelineBuilder() = default;
|
||||
|
||||
void Clear();
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12PipelineState> Create(ID3D12Device* device, bool clear = true);
|
||||
Microsoft::WRL::ComPtr<ID3D12PipelineState> Create(ID3D12Device* device, ShaderCache& cache, bool clear = true);
|
||||
|
||||
void SetRootSignature(ID3D12RootSignature* rs);
|
||||
|
||||
void SetVertexShader(const void* data, u32 data_size);
|
||||
void SetGeometryShader(const void* data, u32 data_size);
|
||||
void SetPixelShader(const void* data, u32 data_size);
|
||||
|
||||
void SetVertexShader(ID3DBlob* blob);
|
||||
void SetGeometryShader(ID3DBlob* blob);
|
||||
void SetPixelShader(ID3DBlob* blob);
|
||||
|
||||
void AddVertexAttribute(const char* semantic_name, u32 semantic_index, DXGI_FORMAT format, u32 buffer, u32 offset);
|
||||
|
||||
void SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE type);
|
||||
|
||||
void SetRasterizationState(D3D12_FILL_MODE polygon_mode, D3D12_CULL_MODE cull_mode, bool front_face_ccw);
|
||||
|
||||
void SetMultisamples(u32 multisamples);
|
||||
|
||||
void SetNoCullRasterizationState();
|
||||
|
||||
void SetDepthState(bool depth_test, bool depth_write, D3D12_COMPARISON_FUNC compare_op);
|
||||
|
||||
void SetNoDepthTestState();
|
||||
|
||||
void SetBlendState(u32 rt, bool blend_enable, D3D12_BLEND src_factor, D3D12_BLEND dst_factor, D3D12_BLEND_OP op,
|
||||
D3D12_BLEND alpha_src_factor, D3D12_BLEND alpha_dst_factor, D3D12_BLEND_OP alpha_op,
|
||||
u8 write_mask = D3D12_COLOR_WRITE_ENABLE_ALL);
|
||||
|
||||
void SetNoBlendingState();
|
||||
|
||||
void ClearRenderTargets();
|
||||
|
||||
void SetRenderTarget(u32 rt, DXGI_FORMAT format);
|
||||
|
||||
void ClearDepthStencilFormat();
|
||||
|
||||
void SetDepthStencilFormat(DXGI_FORMAT format);
|
||||
|
||||
private:
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC m_desc{};
|
||||
std::array<D3D12_INPUT_ELEMENT_DESC, MAX_VERTEX_ATTRIBUTES> m_input_elements{};
|
||||
};
|
||||
|
||||
} // namespace D3D12
|
|
@ -122,6 +122,8 @@ target_compile_definitions(core PUBLIC "-DWITH_IMGUI=1")
|
|||
|
||||
if(WIN32)
|
||||
target_sources(core PRIVATE
|
||||
gpu_hw_d3d12.cpp
|
||||
gpu_hw_d3d12.h
|
||||
gpu_hw_d3d11.cpp
|
||||
gpu_hw_d3d11.h
|
||||
)
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
<ClCompile Include="gpu_backend.cpp" />
|
||||
<ClCompile Include="gpu_commands.cpp" />
|
||||
<ClCompile Include="gpu_hw_d3d11.cpp" />
|
||||
<ClCompile Include="gpu_hw_d3d12.cpp" />
|
||||
<ClCompile Include="gpu_hw_shadergen.cpp" />
|
||||
<ClCompile Include="gpu_hw_vulkan.cpp" />
|
||||
<ClCompile Include="gpu_sw.cpp" />
|
||||
|
@ -99,6 +100,7 @@
|
|||
<ClInclude Include="digital_controller.h" />
|
||||
<ClInclude Include="gpu_backend.h" />
|
||||
<ClInclude Include="gpu_hw_d3d11.h" />
|
||||
<ClInclude Include="gpu_hw_d3d12.h" />
|
||||
<ClInclude Include="gpu_hw_shadergen.h" />
|
||||
<ClInclude Include="gpu_hw_vulkan.h" />
|
||||
<ClInclude Include="gpu_sw.h" />
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
<ClCompile Include="texture_replacements.cpp" />
|
||||
<ClCompile Include="gdb_protocol.h" />
|
||||
<ClCompile Include="multitap.cpp" />
|
||||
<ClCompile Include="gpu_hw_d3d12.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="types.h" />
|
||||
|
@ -118,5 +119,6 @@
|
|||
<ClInclude Include="texture_replacements.h" />
|
||||
<ClInclude Include="shader_cache_version.h" />
|
||||
<ClInclude Include="multitap.h" />
|
||||
<ClInclude Include="gpu_hw_d3d12.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
|
|
@ -146,6 +146,9 @@ public:
|
|||
// gpu_hw_d3d11.cpp
|
||||
static std::unique_ptr<GPU> CreateHardwareD3D11Renderer();
|
||||
|
||||
// gpu_hw_d3d12.cpp
|
||||
static std::unique_ptr<GPU> CreateHardwareD3D12Renderer();
|
||||
|
||||
// gpu_hw_opengl.cpp
|
||||
static std::unique_ptr<GPU> CreateHardwareOpenGLRenderer();
|
||||
|
||||
|
|
1097
src/core/gpu_hw_d3d12.cpp
Normal file
1097
src/core/gpu_hw_d3d12.cpp
Normal file
File diff suppressed because it is too large
Load diff
102
src/core/gpu_hw_d3d12.h
Normal file
102
src/core/gpu_hw_d3d12.h
Normal file
|
@ -0,0 +1,102 @@
|
|||
#pragma once
|
||||
#include "common/dimensional_array.h"
|
||||
#include "common/d3d12/staging_texture.h"
|
||||
#include "common/d3d12/stream_buffer.h"
|
||||
#include "common/d3d12/texture.h"
|
||||
#include "gpu_hw.h"
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
||||
class GPU_HW_D3D12 : public GPU_HW
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
GPU_HW_D3D12();
|
||||
~GPU_HW_D3D12() override;
|
||||
|
||||
GPURenderer GetRendererType() const override;
|
||||
|
||||
bool Initialize(HostDisplay* host_display) override;
|
||||
void Reset(bool clear_vram) override;
|
||||
|
||||
void ResetGraphicsAPIState() override;
|
||||
void RestoreGraphicsAPIState() override;
|
||||
void UpdateSettings() override;
|
||||
|
||||
protected:
|
||||
void ClearDisplay() override;
|
||||
void UpdateDisplay() override;
|
||||
void ReadVRAM(u32 x, u32 y, u32 width, u32 height) override;
|
||||
void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) override;
|
||||
void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, bool set_mask, bool check_mask) override;
|
||||
void CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height) override;
|
||||
void UpdateVRAMReadTexture() override;
|
||||
void UpdateDepthBufferFromMaskBit() override;
|
||||
void ClearDepthBuffer() override;
|
||||
void SetScissorFromDrawingArea() override;
|
||||
void MapBatchVertexPointer(u32 required_vertices) override;
|
||||
void UnmapBatchVertexPointer(u32 used_vertices) override;
|
||||
void UploadUniformBuffer(const void* data, u32 data_size) override;
|
||||
void DrawBatchVertices(BatchRenderMode render_mode, u32 base_vertex, u32 num_vertices) override;
|
||||
|
||||
private:
|
||||
enum : u32
|
||||
{
|
||||
MAX_PUSH_CONSTANTS_SIZE = 64,
|
||||
};
|
||||
void SetCapabilities();
|
||||
void DestroyResources();
|
||||
|
||||
bool CreateRootSignatures();
|
||||
bool CreateSamplers();
|
||||
|
||||
bool CreateFramebuffer();
|
||||
void ClearFramebuffer();
|
||||
void DestroyFramebuffer();
|
||||
|
||||
bool CreateVertexBuffer();
|
||||
bool CreateUniformBuffer();
|
||||
bool CreateTextureBuffer();
|
||||
|
||||
bool CompilePipelines();
|
||||
void DestroyPipelines();
|
||||
|
||||
ComPtr<ID3D12RootSignature> m_batch_root_signature;
|
||||
ComPtr<ID3D12RootSignature> m_single_sampler_root_signature;
|
||||
|
||||
D3D12::Texture m_vram_texture;
|
||||
D3D12::Texture m_vram_depth_texture;
|
||||
D3D12::Texture m_vram_read_texture;
|
||||
D3D12::Texture m_vram_readback_texture;
|
||||
D3D12::StagingTexture m_vram_readback_staging_texture;
|
||||
D3D12::Texture m_display_texture;
|
||||
|
||||
D3D12::DescriptorHandle m_point_sampler;
|
||||
D3D12::DescriptorHandle m_linear_sampler;
|
||||
|
||||
D3D12::StreamBuffer m_vertex_stream_buffer;
|
||||
D3D12::StreamBuffer m_uniform_stream_buffer;
|
||||
D3D12::StreamBuffer m_texture_stream_buffer;
|
||||
D3D12::DescriptorHandle m_texture_stream_buffer_srv;
|
||||
|
||||
u32 m_current_uniform_buffer_offset = 0;
|
||||
|
||||
// [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
|
||||
DimensionalArray<ComPtr<ID3D12PipelineState>, 2, 2, 5, 9, 4, 2> m_batch_pipelines;
|
||||
|
||||
// [interlaced]
|
||||
std::array<ComPtr<ID3D12PipelineState>, 2> m_vram_fill_pipelines;
|
||||
|
||||
// [depth_test]
|
||||
std::array<ComPtr<ID3D12PipelineState>, 2> m_vram_write_pipelines;
|
||||
std::array<ComPtr<ID3D12PipelineState>, 2> m_vram_copy_pipelines;
|
||||
|
||||
ComPtr<ID3D12PipelineState> m_vram_readback_pipeline;
|
||||
ComPtr<ID3D12PipelineState> m_vram_update_depth_pipeline;
|
||||
|
||||
// [depth_24][interlace_mode]
|
||||
DimensionalArray<ComPtr<ID3D12PipelineState>, 3, 2> m_display_pipelines;
|
||||
};
|
|
@ -41,6 +41,7 @@ public:
|
|||
{
|
||||
None,
|
||||
D3D11,
|
||||
D3D12,
|
||||
Vulkan,
|
||||
OpenGL,
|
||||
OpenGLES
|
||||
|
|
|
@ -652,11 +652,13 @@ const char* Settings::GetCPUFastmemModeDisplayName(CPUFastmemMode mode)
|
|||
static constexpr auto s_gpu_renderer_names = make_array(
|
||||
#ifdef _WIN32
|
||||
"D3D11",
|
||||
"D3D12",
|
||||
#endif
|
||||
"Vulkan", "OpenGL", "Software");
|
||||
static constexpr auto s_gpu_renderer_display_names = make_array(
|
||||
#ifdef _WIN32
|
||||
TRANSLATABLE("GPURenderer", "Hardware (D3D11)"),
|
||||
TRANSLATABLE("GPURenderer", "Hardware (D3D12)"),
|
||||
#endif
|
||||
TRANSLATABLE("GPURenderer", "Hardware (Vulkan)"), TRANSLATABLE("GPURenderer", "Hardware (OpenGL)"),
|
||||
TRANSLATABLE("GPURenderer", "Software"));
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
Log_SetChannel(ShaderGen);
|
||||
|
||||
ShaderGen::ShaderGen(HostDisplay::RenderAPI render_api, bool supports_dual_source_blend)
|
||||
: m_render_api(render_api), m_glsl(render_api != HostDisplay::RenderAPI::D3D11),
|
||||
: m_render_api(render_api), m_glsl(render_api != HostDisplay::RenderAPI::D3D11 && render_api != HostDisplay::RenderAPI::D3D12),
|
||||
m_supports_dual_source_blend(supports_dual_source_blend), m_use_glsl_interface_blocks(false)
|
||||
{
|
||||
if (m_glsl)
|
||||
|
@ -133,6 +133,7 @@ void ShaderGen::WriteHeader(std::stringstream& ss)
|
|||
DefineMacro(ss, "API_OPENGL", m_render_api == HostDisplay::RenderAPI::OpenGL);
|
||||
DefineMacro(ss, "API_OPENGL_ES", m_render_api == HostDisplay::RenderAPI::OpenGLES);
|
||||
DefineMacro(ss, "API_D3D11", m_render_api == HostDisplay::RenderAPI::D3D11);
|
||||
DefineMacro(ss, "API_D3D12", m_render_api == HostDisplay::RenderAPI::D3D12);
|
||||
DefineMacro(ss, "API_VULKAN", m_render_api == HostDisplay::RenderAPI::Vulkan);
|
||||
|
||||
if (m_render_api == HostDisplay::RenderAPI::OpenGLES)
|
||||
|
|
|
@ -1012,6 +1012,9 @@ bool CreateGPU(GPURenderer renderer)
|
|||
case GPURenderer::HardwareD3D11:
|
||||
g_gpu = GPU::CreateHardwareD3D11Renderer();
|
||||
break;
|
||||
case GPURenderer::HardwareD3D12:
|
||||
g_gpu = GPU::CreateHardwareD3D12Renderer();
|
||||
break;
|
||||
#endif
|
||||
|
||||
case GPURenderer::Software:
|
||||
|
|
|
@ -56,6 +56,7 @@ enum class GPURenderer : u8
|
|||
{
|
||||
#ifdef _WIN32
|
||||
HardwareD3D11,
|
||||
HardwareD3D12,
|
||||
#endif
|
||||
HardwareVulkan,
|
||||
HardwareOpenGL,
|
||||
|
|
|
@ -24,6 +24,7 @@ Log_SetChannel(NoGUIHostInterface);
|
|||
|
||||
#ifdef _WIN32
|
||||
#include "frontend-common/d3d11_host_display.h"
|
||||
#include "frontend-common/d3d12_host_display.h"
|
||||
#endif
|
||||
|
||||
NoGUIHostInterface::NoGUIHostInterface() = default;
|
||||
|
@ -104,6 +105,10 @@ bool NoGUIHostInterface::CreateDisplay(bool fullscreen)
|
|||
break;
|
||||
|
||||
#ifdef _WIN32
|
||||
case GPURenderer::HardwareD3D12:
|
||||
m_display = std::make_unique<FrontendCommon::D3D12HostDisplay>();
|
||||
break;
|
||||
|
||||
case GPURenderer::HardwareD3D11:
|
||||
default:
|
||||
m_display = std::make_unique<FrontendCommon::D3D11HostDisplay>();
|
||||
|
|
|
@ -42,6 +42,7 @@ Log_SetChannel(QtHostInterface);
|
|||
#ifdef _WIN32
|
||||
#include "common/windows_headers.h"
|
||||
#include "frontend-common/d3d11_host_display.h"
|
||||
#include "frontend-common/d3d12_host_display.h"
|
||||
#include <KnownFolders.h>
|
||||
#include <ShlObj.h>
|
||||
#endif
|
||||
|
@ -598,6 +599,10 @@ HostDisplay* QtHostInterface::createHostDisplay()
|
|||
break;
|
||||
|
||||
#ifdef _WIN32
|
||||
case GPURenderer::HardwareD3D12:
|
||||
m_display = std::make_unique<FrontendCommon::D3D12HostDisplay>();
|
||||
break;
|
||||
|
||||
case GPURenderer::HardwareD3D11:
|
||||
default:
|
||||
m_display = std::make_unique<FrontendCommon::D3D11HostDisplay>();
|
||||
|
|
|
@ -51,10 +51,14 @@ if(WIN32)
|
|||
target_sources(frontend-common PRIVATE
|
||||
d3d11_host_display.cpp
|
||||
d3d11_host_display.h
|
||||
d3d12_host_display.cpp
|
||||
d3d12_host_display.h
|
||||
dinput_controller_interface.cpp
|
||||
dinput_controller_interface.h
|
||||
imgui_impl_dx11.cpp
|
||||
imgui_impl_dx11.h
|
||||
imgui_impl_dx12.cpp
|
||||
imgui_impl_dx12.h
|
||||
xaudio2_audio_stream.cpp
|
||||
xaudio2_audio_stream.h
|
||||
xinput_controller_interface.cpp
|
||||
|
|
871
src/frontend-common/d3d12_host_display.cpp
Normal file
871
src/frontend-common/d3d12_host_display.cpp
Normal file
|
@ -0,0 +1,871 @@
|
|||
#include "d3d12_host_display.h"
|
||||
#include "common/assert.h"
|
||||
#include "common/d3d11/shader_compiler.h"
|
||||
#include "common/d3d12/context.h"
|
||||
#include "common/d3d12/util.h"
|
||||
#include "common/log.h"
|
||||
#include "common/string_util.h"
|
||||
#include "core/host_interface.h"
|
||||
#include "core/settings.h"
|
||||
#include "display_ps.hlsl.h"
|
||||
#include "display_vs.hlsl.h"
|
||||
#include "frontend-common/postprocessing_shadergen.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_dx12.h"
|
||||
#include <array>
|
||||
#include <dxgi1_5.h>
|
||||
Log_SetChannel(D3D12HostDisplay);
|
||||
|
||||
namespace FrontendCommon {
|
||||
|
||||
static constexpr std::array<DXGI_FORMAT, static_cast<u32>(HostDisplayPixelFormat::Count)>
|
||||
s_display_pixel_format_mapping = {{DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM,
|
||||
DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G5R5A1_UNORM}};
|
||||
|
||||
class D3D12HostDisplayTexture : public HostDisplayTexture
|
||||
{
|
||||
public:
|
||||
D3D12HostDisplayTexture(D3D12::Texture texture) : m_texture(std::move(texture)) {}
|
||||
~D3D12HostDisplayTexture() override = default;
|
||||
|
||||
void* GetHandle() const override { return const_cast<D3D12::Texture*>(&m_texture); }
|
||||
u32 GetWidth() const override { return m_texture.GetWidth(); }
|
||||
u32 GetHeight() const override { return m_texture.GetHeight(); }
|
||||
u32 GetLayers() const override { return 1; }
|
||||
u32 GetLevels() const override { return 1; }
|
||||
u32 GetSamples() const override { return m_texture.GetSamples(); }
|
||||
|
||||
HostDisplayPixelFormat GetFormat() const override
|
||||
{
|
||||
for (u32 i = 0; i < static_cast<u32>(s_display_pixel_format_mapping.size()); i++)
|
||||
{
|
||||
if (m_texture.GetFormat() == s_display_pixel_format_mapping[i])
|
||||
return static_cast<HostDisplayPixelFormat>(i);
|
||||
}
|
||||
|
||||
return HostDisplayPixelFormat::Count;
|
||||
}
|
||||
|
||||
const D3D12::Texture& GetTexture() const { return m_texture; }
|
||||
D3D12::Texture& GetTexture() { return m_texture; }
|
||||
|
||||
private:
|
||||
D3D12::Texture m_texture;
|
||||
};
|
||||
|
||||
D3D12HostDisplay::D3D12HostDisplay() = default;
|
||||
|
||||
D3D12HostDisplay::~D3D12HostDisplay()
|
||||
{
|
||||
AssertMsg(!g_d3d12_context, "Context should have been destroyed by now");
|
||||
AssertMsg(!m_swap_chain, "Swap chain should have been destroyed by now");
|
||||
}
|
||||
|
||||
HostDisplay::RenderAPI D3D12HostDisplay::GetRenderAPI() const
|
||||
{
|
||||
return HostDisplay::RenderAPI::D3D12;
|
||||
}
|
||||
|
||||
void* D3D12HostDisplay::GetRenderDevice() const
|
||||
{
|
||||
return g_d3d12_context->GetDevice();
|
||||
}
|
||||
|
||||
void* D3D12HostDisplay::GetRenderContext() const
|
||||
{
|
||||
return g_d3d12_context.get();
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::HasRenderDevice() const
|
||||
{
|
||||
return static_cast<bool>(g_d3d12_context);
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::HasRenderSurface() const
|
||||
{
|
||||
return static_cast<bool>(m_swap_chain);
|
||||
}
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> D3D12HostDisplay::CreateTexture(u32 width, u32 height, u32 layers, u32 levels,
|
||||
u32 samples, HostDisplayPixelFormat format,
|
||||
const void* data, u32 data_stride,
|
||||
bool dynamic /* = false */)
|
||||
{
|
||||
if (layers != 1)
|
||||
return {};
|
||||
|
||||
const DXGI_FORMAT dxgi_format = s_display_pixel_format_mapping[static_cast<u32>(format)];
|
||||
D3D12::Texture tex;
|
||||
if (!tex.Create(width, height, samples, dxgi_format, dxgi_format, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN,
|
||||
D3D12_RESOURCE_FLAG_NONE))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
if (data && !tex.LoadData(0, 0, width, height, data, data_stride))
|
||||
return {};
|
||||
|
||||
return std::make_unique<D3D12HostDisplayTexture>(std::move(tex));
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height,
|
||||
const void* texture_data, u32 texture_data_stride)
|
||||
{
|
||||
static_cast<D3D12HostDisplayTexture*>(texture)->GetTexture().LoadData(x, y, width, height, texture_data,
|
||||
texture_data_stride);
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::DownloadTexture(const void* texture_handle, HostDisplayPixelFormat texture_format, u32 x, u32 y,
|
||||
u32 width, u32 height, void* out_data, u32 out_data_stride)
|
||||
{
|
||||
const D3D12::Texture* texture = static_cast<const D3D12::Texture*>(texture_handle);
|
||||
|
||||
if (!m_readback_staging_texture.EnsureSize(width, height, texture->GetFormat(), false))
|
||||
return false;
|
||||
|
||||
m_readback_staging_texture.CopyFromTexture(texture->GetResource(), 0, x, y, 0, 0, width, height);
|
||||
return m_readback_staging_texture.ReadPixels(0, 0, width, height, out_data, out_data_stride);
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::SupportsDisplayPixelFormat(HostDisplayPixelFormat format) const
|
||||
{
|
||||
const DXGI_FORMAT dfmt = s_display_pixel_format_mapping[static_cast<u32>(format)];
|
||||
if (dfmt == DXGI_FORMAT_UNKNOWN)
|
||||
return false;
|
||||
|
||||
return g_d3d12_context->SupportsTextureFormat(dfmt);
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::BeginSetDisplayPixels(HostDisplayPixelFormat format, u32 width, u32 height, void** out_buffer,
|
||||
u32* out_pitch)
|
||||
{
|
||||
ClearDisplayTexture();
|
||||
|
||||
const DXGI_FORMAT dxgi_format = s_display_pixel_format_mapping[static_cast<u32>(format)];
|
||||
if (m_display_pixels_texture.GetWidth() < width || m_display_pixels_texture.GetHeight() < height ||
|
||||
m_display_pixels_texture.GetFormat() != dxgi_format)
|
||||
{
|
||||
if (!m_display_pixels_texture.Create(width, height, 1, dxgi_format, dxgi_format, DXGI_FORMAT_UNKNOWN,
|
||||
DXGI_FORMAT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_display_pixels_texture.BeginStreamUpdate(0, 0, width, height, out_buffer, out_pitch))
|
||||
return false;
|
||||
|
||||
SetDisplayTexture(&m_display_pixels_texture, format, m_display_pixels_texture.GetWidth(),
|
||||
m_display_pixels_texture.GetHeight(), 0, 0, static_cast<u32>(width), static_cast<u32>(height));
|
||||
return true;
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::EndSetDisplayPixels()
|
||||
{
|
||||
m_display_pixels_texture.EndStreamUpdate(0, 0, m_display_pixels_texture.GetWidth(),
|
||||
m_display_pixels_texture.GetHeight());
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::GetHostRefreshRate(float* refresh_rate)
|
||||
{
|
||||
if (m_swap_chain && IsFullscreen())
|
||||
{
|
||||
DXGI_SWAP_CHAIN_DESC desc;
|
||||
if (SUCCEEDED(m_swap_chain->GetDesc(&desc)) && desc.BufferDesc.RefreshRate.Numerator > 0 &&
|
||||
desc.BufferDesc.RefreshRate.Denominator > 0)
|
||||
{
|
||||
Log_InfoPrintf("using fs rr: %u %u", desc.BufferDesc.RefreshRate.Numerator,
|
||||
desc.BufferDesc.RefreshRate.Denominator);
|
||||
*refresh_rate = static_cast<float>(desc.BufferDesc.RefreshRate.Numerator) /
|
||||
static_cast<float>(desc.BufferDesc.RefreshRate.Denominator);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return HostDisplay::GetHostRefreshRate(refresh_rate);
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::SetVSync(bool enabled)
|
||||
{
|
||||
m_vsync = enabled;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device,
|
||||
bool threaded_presentation)
|
||||
{
|
||||
ComPtr<IDXGIFactory> temp_dxgi_factory;
|
||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
HRESULT hr = CreateDXGIFactory(IID_PPV_ARGS(temp_dxgi_factory.GetAddressOf()));
|
||||
#else
|
||||
HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(temp_dxgi_factory.GetAddressOf()));
|
||||
#endif
|
||||
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to create DXGI factory: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
u32 adapter_index;
|
||||
if (!adapter_name.empty())
|
||||
{
|
||||
AdapterAndModeList adapter_info(GetAdapterAndModeList(temp_dxgi_factory.Get()));
|
||||
for (adapter_index = 0; adapter_index < static_cast<u32>(adapter_info.adapter_names.size()); adapter_index++)
|
||||
{
|
||||
if (adapter_name == adapter_info.adapter_names[adapter_index])
|
||||
break;
|
||||
}
|
||||
if (adapter_index == static_cast<u32>(adapter_info.adapter_names.size()))
|
||||
{
|
||||
Log_WarningPrintf("Could not find adapter '%*s', using first (%s)", static_cast<int>(adapter_name.size()),
|
||||
adapter_name.data(), adapter_info.adapter_names[0].c_str());
|
||||
adapter_index = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log_InfoPrintf("No adapter selected, using first.");
|
||||
adapter_index = 0;
|
||||
}
|
||||
|
||||
if (!D3D12::Context::Create(temp_dxgi_factory.Get(), adapter_index, debug_device))
|
||||
return false;
|
||||
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to create D3D device: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if 0
|
||||
// we need the specific factory for the device, otherwise MakeWindowAssociation() is flaky.
|
||||
ComPtr<IDXGIDevice> dxgi_device;
|
||||
if (FAILED(m_device.As(&dxgi_device)) || FAILED(dxgi_device->GetParent(IID_PPV_ARGS(dxgi_adapter.GetAddressOf()))) ||
|
||||
FAILED(dxgi_adapter->GetParent(IID_PPV_ARGS(m_dxgi_factory.GetAddressOf()))))
|
||||
{
|
||||
Log_WarningPrint("Failed to get parent adapter/device/factory");
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
m_dxgi_factory = std::move(temp_dxgi_factory);
|
||||
#endif
|
||||
|
||||
m_allow_tearing_supported = false;
|
||||
ComPtr<IDXGIFactory5> dxgi_factory5;
|
||||
hr = m_dxgi_factory.As(&dxgi_factory5);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
BOOL allow_tearing_supported = false;
|
||||
hr = dxgi_factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allow_tearing_supported,
|
||||
sizeof(allow_tearing_supported));
|
||||
if (SUCCEEDED(hr))
|
||||
m_allow_tearing_supported = (allow_tearing_supported == TRUE);
|
||||
}
|
||||
|
||||
m_window_info = wi;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::InitializeRenderDevice(std::string_view shader_cache_directory, bool debug_device,
|
||||
bool threaded_presentation)
|
||||
{
|
||||
if (m_window_info.type != WindowInfo::Type::Surfaceless && !CreateSwapChain(nullptr))
|
||||
return false;
|
||||
|
||||
if (!CreateResources())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::DestroyRenderDevice()
|
||||
{
|
||||
g_d3d12_context->ExecuteCommandList(true);
|
||||
|
||||
DestroyResources();
|
||||
DestroyRenderSurface();
|
||||
if (g_d3d12_context)
|
||||
g_d3d12_context->Destroy();
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::MakeRenderContextCurrent()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::DoneRenderContextCurrent()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::CreateSwapChain(const DXGI_MODE_DESC* fullscreen_mode)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
if (m_window_info.type != WindowInfo::Type::Win32)
|
||||
return false;
|
||||
|
||||
const HWND window_hwnd = reinterpret_cast<HWND>(m_window_info.window_handle);
|
||||
RECT client_rc{};
|
||||
GetClientRect(window_hwnd, &client_rc);
|
||||
const u32 width = static_cast<u32>(client_rc.right - client_rc.left);
|
||||
const u32 height = static_cast<u32>(client_rc.bottom - client_rc.top);
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
|
||||
swap_chain_desc.BufferDesc.Width = width;
|
||||
swap_chain_desc.BufferDesc.Height = height;
|
||||
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc.Count = 1;
|
||||
swap_chain_desc.BufferCount = 3;
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.OutputWindow = window_hwnd;
|
||||
swap_chain_desc.Windowed = TRUE;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
|
||||
m_using_allow_tearing = (m_allow_tearing_supported && !fullscreen_mode);
|
||||
if (m_using_allow_tearing)
|
||||
swap_chain_desc.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
|
||||
|
||||
if (fullscreen_mode)
|
||||
{
|
||||
swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
|
||||
swap_chain_desc.Windowed = FALSE;
|
||||
swap_chain_desc.BufferDesc = *fullscreen_mode;
|
||||
}
|
||||
|
||||
Log_InfoPrintf("Creating a %dx%d %s swap chain", swap_chain_desc.BufferDesc.Width, swap_chain_desc.BufferDesc.Height,
|
||||
swap_chain_desc.Windowed ? "windowed" : "full-screen");
|
||||
|
||||
hr =
|
||||
m_dxgi_factory->CreateSwapChain(g_d3d12_context->GetCommandQueue(), &swap_chain_desc, m_swap_chain.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("CreateSwapChain failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
hr = m_dxgi_factory->MakeWindowAssociation(swap_chain_desc.OutputWindow, DXGI_MWA_NO_WINDOW_CHANGES);
|
||||
if (FAILED(hr))
|
||||
Log_WarningPrintf("MakeWindowAssociation() to disable ALT+ENTER failed");
|
||||
#else
|
||||
if (m_window_info.type != WindowInfo::Type::WinRT)
|
||||
return false;
|
||||
|
||||
ComPtr<IDXGIFactory2> factory2;
|
||||
hr = m_dxgi_factory.As(&factory2);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to get DXGI factory: %08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {};
|
||||
swap_chain_desc.Width = m_window_info.surface_width;
|
||||
swap_chain_desc.Height = m_window_info.surface_height;
|
||||
swap_chain_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc.Count = 1;
|
||||
swap_chain_desc.BufferCount = 3;
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
|
||||
m_using_allow_tearing = (m_allow_tearing_supported && !fullscreen_mode);
|
||||
if (m_using_allow_tearing)
|
||||
swap_chain_desc.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
|
||||
|
||||
ComPtr<IDXGISwapChain1> swap_chain1;
|
||||
hr = factory2->CreateSwapChainForCoreWindow(g_d3d12_context->GetCommandQueue(),
|
||||
static_cast<IUnknown*>(m_window_info.window_handle), &swap_chain_desc,
|
||||
nullptr, swap_chain1.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("CreateSwapChainForCoreWindow failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_swap_chain = swap_chain1;
|
||||
#endif
|
||||
|
||||
return CreateSwapChainRTV();
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::CreateSwapChainRTV()
|
||||
{
|
||||
DXGI_SWAP_CHAIN_DESC swap_chain_desc;
|
||||
HRESULT hr = m_swap_chain->GetDesc(&swap_chain_desc);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
for (u32 i = 0; i < swap_chain_desc.BufferCount; i++)
|
||||
{
|
||||
ComPtr<ID3D12Resource> backbuffer;
|
||||
hr = m_swap_chain->GetBuffer(i, IID_PPV_ARGS(backbuffer.GetAddressOf()));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Log_ErrorPrintf("GetBuffer for RTV failed: 0x%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12::Texture tex;
|
||||
if (!tex.Adopt(std::move(backbuffer), DXGI_FORMAT_UNKNOWN, swap_chain_desc.BufferDesc.Format, DXGI_FORMAT_UNKNOWN,
|
||||
D3D12_RESOURCE_STATE_PRESENT))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_swap_chain_buffers.push_back(std::move(tex));
|
||||
}
|
||||
|
||||
m_window_info.surface_width = swap_chain_desc.BufferDesc.Width;
|
||||
m_window_info.surface_height = swap_chain_desc.BufferDesc.Height;
|
||||
Log_InfoPrintf("Swap chain buffer size: %ux%u", m_window_info.surface_width, m_window_info.surface_height);
|
||||
|
||||
if (m_window_info.type == WindowInfo::Type::Win32)
|
||||
{
|
||||
BOOL fullscreen = FALSE;
|
||||
DXGI_SWAP_CHAIN_DESC desc;
|
||||
if (SUCCEEDED(m_swap_chain->GetFullscreenState(&fullscreen, nullptr)) && fullscreen &&
|
||||
SUCCEEDED(m_swap_chain->GetDesc(&desc)))
|
||||
{
|
||||
m_window_info.surface_refresh_rate = static_cast<float>(desc.BufferDesc.RefreshRate.Numerator) /
|
||||
static_cast<float>(desc.BufferDesc.RefreshRate.Denominator);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_window_info.surface_refresh_rate = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
m_current_swap_chain_buffer = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::DestroySwapChainRTVs()
|
||||
{
|
||||
for (D3D12::Texture& buffer : m_swap_chain_buffers)
|
||||
buffer.Destroy(false);
|
||||
m_swap_chain_buffers.clear();
|
||||
m_current_swap_chain_buffer = 0;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::ChangeRenderWindow(const WindowInfo& new_wi)
|
||||
{
|
||||
DestroyRenderSurface();
|
||||
|
||||
m_window_info = new_wi;
|
||||
return CreateSwapChain(nullptr);
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::DestroyRenderSurface()
|
||||
{
|
||||
if (IsFullscreen())
|
||||
SetFullscreen(false, 0, 0, 0.0f);
|
||||
|
||||
DestroySwapChainRTVs();
|
||||
m_swap_chain.Reset();
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::ResizeRenderWindow(s32 new_window_width, s32 new_window_height)
|
||||
{
|
||||
if (!m_swap_chain)
|
||||
return;
|
||||
|
||||
// For some reason if we don't execute the command list here, the swap chain is in use.. not sure where.
|
||||
g_d3d12_context->ExecuteCommandList(true);
|
||||
|
||||
DestroySwapChainRTVs();
|
||||
|
||||
HRESULT hr = m_swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN,
|
||||
m_using_allow_tearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0);
|
||||
if (FAILED(hr))
|
||||
Log_ErrorPrintf("ResizeBuffers() failed: 0x%08X", hr);
|
||||
|
||||
if (!CreateSwapChainRTV())
|
||||
Panic("Failed to recreate swap chain RTV after resize");
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::SupportsFullscreen() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::IsFullscreen()
|
||||
{
|
||||
BOOL is_fullscreen = FALSE;
|
||||
return (m_swap_chain && SUCCEEDED(m_swap_chain->GetFullscreenState(&is_fullscreen, nullptr)) && is_fullscreen);
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::SetFullscreen(bool fullscreen, u32 width, u32 height, float refresh_rate)
|
||||
{
|
||||
if (!m_swap_chain)
|
||||
return false;
|
||||
|
||||
BOOL is_fullscreen = FALSE;
|
||||
HRESULT hr = m_swap_chain->GetFullscreenState(&is_fullscreen, nullptr);
|
||||
if (!fullscreen)
|
||||
{
|
||||
// leaving fullscreen
|
||||
if (is_fullscreen)
|
||||
return SUCCEEDED(m_swap_chain->SetFullscreenState(FALSE, nullptr));
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
IDXGIOutput* output;
|
||||
if (FAILED(hr = m_swap_chain->GetContainingOutput(&output)))
|
||||
return false;
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC current_desc;
|
||||
hr = m_swap_chain->GetDesc(¤t_desc);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
DXGI_MODE_DESC new_mode = current_desc.BufferDesc;
|
||||
new_mode.Width = width;
|
||||
new_mode.Height = height;
|
||||
new_mode.RefreshRate.Numerator = static_cast<UINT>(std::floor(refresh_rate * 1000.0f));
|
||||
new_mode.RefreshRate.Denominator = 1000u;
|
||||
|
||||
DXGI_MODE_DESC closest_mode;
|
||||
if (FAILED(hr = output->FindClosestMatchingMode(&new_mode, &closest_mode, nullptr)) ||
|
||||
new_mode.Format != current_desc.BufferDesc.Format)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to find closest matching mode, hr=%08X", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (new_mode.Width == current_desc.BufferDesc.Width && new_mode.Height == current_desc.BufferDesc.Width &&
|
||||
new_mode.RefreshRate.Numerator == current_desc.BufferDesc.RefreshRate.Numerator &&
|
||||
new_mode.RefreshRate.Denominator == current_desc.BufferDesc.RefreshRate.Denominator)
|
||||
{
|
||||
Log_InfoPrintf("Fullscreen mode already set");
|
||||
return true;
|
||||
}
|
||||
|
||||
DestroySwapChainRTVs();
|
||||
m_swap_chain.Reset();
|
||||
|
||||
if (!CreateSwapChain(&closest_mode))
|
||||
{
|
||||
Log_ErrorPrintf("Failed to create a fullscreen swap chain");
|
||||
if (!CreateSwapChain(nullptr))
|
||||
Panic("Failed to recreate windowed swap chain");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
HostDisplay::AdapterAndModeList D3D12HostDisplay::GetAdapterAndModeList()
|
||||
{
|
||||
return GetAdapterAndModeList(m_dxgi_factory.Get());
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::CreateResources()
|
||||
{
|
||||
D3D12::RootSignatureBuilder rsbuilder;
|
||||
rsbuilder.AddCBVParameter(0, D3D12_SHADER_VISIBILITY_ALL);
|
||||
rsbuilder.AddDescriptorTable(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0, 1, D3D12_SHADER_VISIBILITY_ALL);
|
||||
rsbuilder.AddDescriptorTable(D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 0, 1, D3D12_SHADER_VISIBILITY_ALL);
|
||||
m_display_root_signature = rsbuilder.Create();
|
||||
if (!m_display_root_signature)
|
||||
return false;
|
||||
|
||||
D3D12::GraphicsPipelineBuilder gpbuilder;
|
||||
gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
|
||||
gpbuilder.SetRootSignature(m_display_root_signature.Get());
|
||||
gpbuilder.SetVertexShader(s_display_vs_bytecode, sizeof(s_display_vs_bytecode));
|
||||
gpbuilder.SetPixelShader(s_display_ps_bytecode, sizeof(s_display_ps_bytecode));
|
||||
gpbuilder.SetNoCullRasterizationState();
|
||||
gpbuilder.SetNoDepthTestState();
|
||||
gpbuilder.SetNoBlendingState();
|
||||
gpbuilder.SetRenderTarget(0, DXGI_FORMAT_R8G8B8A8_UNORM);
|
||||
m_display_pipeline = gpbuilder.Create(g_d3d12_context->GetDevice(), false);
|
||||
if (!m_display_pipeline)
|
||||
return false;
|
||||
|
||||
gpbuilder.SetBlendState(0, true, D3D12_BLEND_SRC_ALPHA, D3D12_BLEND_INV_SRC_ALPHA, D3D12_BLEND_OP_ADD,
|
||||
D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_COLOR_WRITE_ENABLE_ALL);
|
||||
m_software_cursor_pipeline = gpbuilder.Create(g_d3d12_context->GetDevice(), false);
|
||||
if (!m_software_cursor_pipeline)
|
||||
return false;
|
||||
|
||||
D3D12_SAMPLER_DESC desc = {};
|
||||
D3D12::SetDefaultSampler(&desc);
|
||||
desc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
desc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
|
||||
|
||||
if (!g_d3d12_context->GetSamplerHeapManager().Allocate(&m_point_sampler))
|
||||
return false;
|
||||
|
||||
g_d3d12_context->GetDevice()->CreateSampler(&desc, m_point_sampler.cpu_handle);
|
||||
|
||||
desc.Filter = D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT;
|
||||
|
||||
if (!g_d3d12_context->GetSamplerHeapManager().Allocate(&m_linear_sampler))
|
||||
return false;
|
||||
|
||||
g_d3d12_context->GetDevice()->CreateSampler(&desc, m_linear_sampler.cpu_handle);
|
||||
|
||||
if (!m_display_uniform_buffer.Create(DISPLAY_UNIFORM_BUFFER_SIZE))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::DestroyResources()
|
||||
{
|
||||
// m_post_processing_chain.ClearStages();
|
||||
// m_post_processing_input_texture.Destroy();
|
||||
// m_post_processing_stages.clear();
|
||||
|
||||
m_readback_staging_texture.Destroy(false);
|
||||
m_display_uniform_buffer.Destroy(false);
|
||||
g_d3d12_context->GetSamplerHeapManager().Free(&m_linear_sampler);
|
||||
g_d3d12_context->GetSamplerHeapManager().Free(&m_point_sampler);
|
||||
m_software_cursor_pipeline.Reset();
|
||||
m_display_pipeline.Reset();
|
||||
m_display_root_signature.Reset();
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::CreateImGuiContext()
|
||||
{
|
||||
ImGui::GetIO().DisplaySize.x = static_cast<float>(m_window_info.surface_width);
|
||||
ImGui::GetIO().DisplaySize.y = static_cast<float>(m_window_info.surface_height);
|
||||
|
||||
return ImGui_ImplDX12_Init(g_d3d12_context->GetDevice(), D3D12::Context::NUM_COMMAND_LISTS,
|
||||
DXGI_FORMAT_R8G8B8A8_UNORM);
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::DestroyImGuiContext()
|
||||
{
|
||||
g_d3d12_context->WaitForGPUIdle();
|
||||
|
||||
ImGui_ImplDX12_Shutdown();
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::UpdateImGuiFontTexture()
|
||||
{
|
||||
return ImGui_ImplDX12_CreateFontsTexture();
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::Render()
|
||||
{
|
||||
if (ShouldSkipDisplayingFrame())
|
||||
{
|
||||
if (ImGui::GetCurrentContext())
|
||||
ImGui::Render();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static constexpr std::array<float, 4> clear_color = {};
|
||||
D3D12::Texture& swap_chain_buf = m_swap_chain_buffers[m_current_swap_chain_buffer];
|
||||
m_current_swap_chain_buffer = ((m_current_swap_chain_buffer + 1) % static_cast<u32>(m_swap_chain_buffers.size()));
|
||||
|
||||
ID3D12GraphicsCommandList* cmdlist = g_d3d12_context->GetCommandList();
|
||||
swap_chain_buf.TransitionToState(D3D12_RESOURCE_STATE_RENDER_TARGET);
|
||||
cmdlist->ClearRenderTargetView(swap_chain_buf.GetRTVOrDSVDescriptor(), clear_color.data(), 0, nullptr);
|
||||
cmdlist->OMSetRenderTargets(1, &swap_chain_buf.GetRTVOrDSVDescriptor().cpu_handle, FALSE, nullptr);
|
||||
cmdlist->SetGraphicsRootSignature(m_display_root_signature.Get());
|
||||
|
||||
RenderDisplay(cmdlist);
|
||||
|
||||
if (ImGui::GetCurrentContext())
|
||||
RenderImGui(cmdlist);
|
||||
|
||||
RenderSoftwareCursor(cmdlist);
|
||||
|
||||
swap_chain_buf.TransitionToState(D3D12_RESOURCE_STATE_PRESENT);
|
||||
g_d3d12_context->ExecuteCommandList(false);
|
||||
|
||||
if (!m_vsync && m_using_allow_tearing)
|
||||
m_swap_chain->Present(0, DXGI_PRESENT_ALLOW_TEARING);
|
||||
else
|
||||
m_swap_chain->Present(BoolToUInt32(m_vsync), 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::RenderScreenshot(u32 width, u32 height, std::vector<u32>* out_pixels, u32* out_stride,
|
||||
HostDisplayPixelFormat* out_format)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::RenderImGui(ID3D12GraphicsCommandList* cmdlist)
|
||||
{
|
||||
ImGui::Render();
|
||||
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), cmdlist);
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::RenderDisplay(ID3D12GraphicsCommandList* cmdlist)
|
||||
{
|
||||
if (!HasDisplayTexture())
|
||||
return;
|
||||
|
||||
const auto [left, top, width, height] = CalculateDrawRect(GetWindowWidth(), GetWindowHeight(), m_display_top_margin);
|
||||
|
||||
// if (!m_post_processing_chain.IsEmpty())
|
||||
// {
|
||||
// ApplyPostProcessingChain(m_swap_chain_rtv.Get(), left, top, width, height, m_display_texture_handle,
|
||||
// m_display_texture_width, m_display_texture_height, m_display_texture_view_x,
|
||||
// m_display_texture_view_y, m_display_texture_view_width,
|
||||
// m_display_texture_view_height);
|
||||
// return;
|
||||
// }
|
||||
|
||||
RenderDisplay(cmdlist, left, top, width, height, m_display_texture_handle, m_display_texture_width,
|
||||
m_display_texture_height, m_display_texture_view_x, m_display_texture_view_y,
|
||||
m_display_texture_view_width, m_display_texture_view_height, m_display_linear_filtering);
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::RenderDisplay(ID3D12GraphicsCommandList* cmdlist, s32 left, s32 top, s32 width, s32 height,
|
||||
void* texture_handle, u32 texture_width, s32 texture_height, s32 texture_view_x,
|
||||
s32 texture_view_y, s32 texture_view_width, s32 texture_view_height,
|
||||
bool linear_filter)
|
||||
{
|
||||
const float uniforms[4] = {static_cast<float>(texture_view_x) / static_cast<float>(texture_width),
|
||||
static_cast<float>(texture_view_y) / static_cast<float>(texture_height),
|
||||
(static_cast<float>(texture_view_width) - 0.5f) / static_cast<float>(texture_width),
|
||||
(static_cast<float>(texture_view_height) - 0.5f) / static_cast<float>(texture_height)};
|
||||
if (!m_display_uniform_buffer.ReserveMemory(sizeof(uniforms), D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT))
|
||||
Panic("Failed to reserve UBO space");
|
||||
|
||||
const u32 ubo_offset = m_display_uniform_buffer.GetCurrentOffset();
|
||||
std::memcpy(m_display_uniform_buffer.GetCurrentHostPointer(), uniforms, sizeof(uniforms));
|
||||
m_display_uniform_buffer.CommitMemory(sizeof(uniforms));
|
||||
|
||||
cmdlist->SetPipelineState(m_display_pipeline.Get());
|
||||
cmdlist->SetGraphicsRootConstantBufferView(0, m_display_uniform_buffer.GetGPUPointer() + ubo_offset);
|
||||
cmdlist->SetGraphicsRootDescriptorTable(1, reinterpret_cast<D3D12::Texture*>(texture_handle)->GetSRVDescriptor());
|
||||
cmdlist->SetGraphicsRootDescriptorTable(2, linear_filter ? m_linear_sampler : m_point_sampler);
|
||||
|
||||
D3D12::SetViewportAndScissor(cmdlist, left, top, width, height);
|
||||
|
||||
cmdlist->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
cmdlist->DrawInstanced(3, 1, 0, 0);
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::RenderSoftwareCursor(ID3D12GraphicsCommandList* cmdlist)
|
||||
{
|
||||
if (!HasSoftwareCursor())
|
||||
return;
|
||||
|
||||
const auto [left, top, width, height] = CalculateSoftwareCursorDrawRect();
|
||||
RenderSoftwareCursor(cmdlist, left, top, width, height, m_cursor_texture.get());
|
||||
}
|
||||
|
||||
void D3D12HostDisplay::RenderSoftwareCursor(ID3D12GraphicsCommandList* cmdlist, s32 left, s32 top, s32 width,
|
||||
s32 height, HostDisplayTexture* texture_handle)
|
||||
{
|
||||
const float uniforms[4] = {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
if (!m_display_uniform_buffer.ReserveMemory(sizeof(uniforms), D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT))
|
||||
Panic("Failed to reserve UBO space");
|
||||
|
||||
const u32 ubo_offset = m_display_uniform_buffer.GetCurrentOffset();
|
||||
std::memcpy(m_display_uniform_buffer.GetCurrentHostPointer(), uniforms, sizeof(uniforms));
|
||||
m_display_uniform_buffer.CommitMemory(sizeof(uniforms));
|
||||
|
||||
cmdlist->SetPipelineState(m_display_pipeline.Get());
|
||||
cmdlist->SetGraphicsRootConstantBufferView(0, m_display_uniform_buffer.GetGPUPointer() + ubo_offset);
|
||||
cmdlist->SetGraphicsRootDescriptorTable(
|
||||
1, static_cast<D3D12HostDisplayTexture*>(texture_handle)->GetTexture().GetRTVOrDSVDescriptor());
|
||||
cmdlist->SetGraphicsRootDescriptorTable(2, m_linear_sampler);
|
||||
|
||||
D3D12::SetViewportAndScissor(cmdlist, left, top, width, height);
|
||||
|
||||
cmdlist->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
cmdlist->DrawInstanced(3, 1, 0, 0);
|
||||
}
|
||||
|
||||
HostDisplay::AdapterAndModeList D3D12HostDisplay::StaticGetAdapterAndModeList()
|
||||
{
|
||||
ComPtr<IDXGIFactory> dxgi_factory;
|
||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
HRESULT hr = CreateDXGIFactory(IID_PPV_ARGS(dxgi_factory.GetAddressOf()));
|
||||
#else
|
||||
HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(dxgi_factory.GetAddressOf()));
|
||||
#endif
|
||||
if (FAILED(hr))
|
||||
return {};
|
||||
|
||||
return GetAdapterAndModeList(dxgi_factory.Get());
|
||||
}
|
||||
|
||||
HostDisplay::AdapterAndModeList D3D12HostDisplay::GetAdapterAndModeList(IDXGIFactory* dxgi_factory)
|
||||
{
|
||||
AdapterAndModeList adapter_info;
|
||||
ComPtr<IDXGIAdapter> current_adapter;
|
||||
while (SUCCEEDED(dxgi_factory->EnumAdapters(static_cast<UINT>(adapter_info.adapter_names.size()),
|
||||
current_adapter.ReleaseAndGetAddressOf())))
|
||||
{
|
||||
DXGI_ADAPTER_DESC adapter_desc;
|
||||
std::string adapter_name;
|
||||
if (SUCCEEDED(current_adapter->GetDesc(&adapter_desc)))
|
||||
{
|
||||
char adapter_name_buffer[128];
|
||||
const int name_length = WideCharToMultiByte(CP_UTF8, 0, adapter_desc.Description,
|
||||
static_cast<int>(std::wcslen(adapter_desc.Description)),
|
||||
adapter_name_buffer, countof(adapter_name_buffer), 0, nullptr);
|
||||
if (name_length >= 0)
|
||||
adapter_name.assign(adapter_name_buffer, static_cast<size_t>(name_length));
|
||||
else
|
||||
adapter_name.assign("(Unknown)");
|
||||
}
|
||||
else
|
||||
{
|
||||
adapter_name.assign("(Unknown)");
|
||||
}
|
||||
|
||||
if (adapter_info.fullscreen_modes.empty())
|
||||
{
|
||||
ComPtr<IDXGIOutput> output;
|
||||
if (SUCCEEDED(current_adapter->EnumOutputs(0, &output)))
|
||||
{
|
||||
UINT num_modes = 0;
|
||||
if (SUCCEEDED(output->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, 0, &num_modes, nullptr)))
|
||||
{
|
||||
std::vector<DXGI_MODE_DESC> modes(num_modes);
|
||||
if (SUCCEEDED(output->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, 0, &num_modes, modes.data())))
|
||||
{
|
||||
for (const DXGI_MODE_DESC& mode : modes)
|
||||
{
|
||||
adapter_info.fullscreen_modes.push_back(StringUtil::StdStringFromFormat(
|
||||
"%u x %u @ %f hz", mode.Width, mode.Height,
|
||||
static_cast<float>(mode.RefreshRate.Numerator) / static_cast<float>(mode.RefreshRate.Denominator)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle duplicate adapter names
|
||||
if (std::any_of(adapter_info.adapter_names.begin(), adapter_info.adapter_names.end(),
|
||||
[&adapter_name](const std::string& other) { return (adapter_name == other); }))
|
||||
{
|
||||
std::string original_adapter_name = std::move(adapter_name);
|
||||
|
||||
u32 current_extra = 2;
|
||||
do
|
||||
{
|
||||
adapter_name = StringUtil::StdStringFromFormat("%s (%u)", original_adapter_name.c_str(), current_extra);
|
||||
current_extra++;
|
||||
} while (std::any_of(adapter_info.adapter_names.begin(), adapter_info.adapter_names.end(),
|
||||
[&adapter_name](const std::string& other) { return (adapter_name == other); }));
|
||||
}
|
||||
|
||||
adapter_info.adapter_names.push_back(std::move(adapter_name));
|
||||
}
|
||||
|
||||
return adapter_info;
|
||||
}
|
||||
|
||||
bool D3D12HostDisplay::SetPostProcessingChain(const std::string_view& config)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace FrontendCommon
|
127
src/frontend-common/d3d12_host_display.h
Normal file
127
src/frontend-common/d3d12_host_display.h
Normal file
|
@ -0,0 +1,127 @@
|
|||
#pragma once
|
||||
#pragma once
|
||||
#include "common/d3d12/descriptor_heap_manager.h"
|
||||
#include "common/d3d12/staging_texture.h"
|
||||
#include "common/d3d12/stream_buffer.h"
|
||||
#include "common/d3d12/texture.h"
|
||||
#include "common/window_info.h"
|
||||
#include "common/windows_headers.h"
|
||||
#include "core/host_display.h"
|
||||
#include <d3d12.h>
|
||||
#include <dxgi.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace FrontendCommon {
|
||||
|
||||
class D3D12HostDisplay : public HostDisplay
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
using ComPtr = Microsoft::WRL::ComPtr<T>;
|
||||
|
||||
D3D12HostDisplay();
|
||||
~D3D12HostDisplay();
|
||||
|
||||
virtual RenderAPI GetRenderAPI() const override;
|
||||
virtual void* GetRenderDevice() const override;
|
||||
virtual void* GetRenderContext() const override;
|
||||
|
||||
virtual bool HasRenderDevice() const override;
|
||||
virtual bool HasRenderSurface() const override;
|
||||
|
||||
virtual bool CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device,
|
||||
bool threaded_presentation) override;
|
||||
virtual bool InitializeRenderDevice(std::string_view shader_cache_directory, bool debug_device,
|
||||
bool threaded_presentation) override;
|
||||
virtual void DestroyRenderDevice() override;
|
||||
|
||||
virtual bool MakeRenderContextCurrent() override;
|
||||
virtual bool DoneRenderContextCurrent() override;
|
||||
|
||||
virtual bool ChangeRenderWindow(const WindowInfo& new_wi) override;
|
||||
virtual void ResizeRenderWindow(s32 new_window_width, s32 new_window_height) override;
|
||||
virtual bool SupportsFullscreen() const override;
|
||||
virtual bool IsFullscreen() override;
|
||||
virtual bool SetFullscreen(bool fullscreen, u32 width, u32 height, float refresh_rate) override;
|
||||
virtual AdapterAndModeList GetAdapterAndModeList() override;
|
||||
virtual void DestroyRenderSurface() override;
|
||||
|
||||
virtual bool SetPostProcessingChain(const std::string_view& config) override;
|
||||
|
||||
std::unique_ptr<HostDisplayTexture> CreateTexture(u32 width, u32 height, u32 layers, u32 levels, u32 samples,
|
||||
HostDisplayPixelFormat format, const void* data, u32 data_stride,
|
||||
bool dynamic = false) override;
|
||||
void UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height, const void* texture_data,
|
||||
u32 texture_data_stride) override;
|
||||
bool DownloadTexture(const void* texture_handle, HostDisplayPixelFormat texture_format, u32 x, u32 y, u32 width,
|
||||
u32 height, void* out_data, u32 out_data_stride) override;
|
||||
bool SupportsDisplayPixelFormat(HostDisplayPixelFormat format) const override;
|
||||
bool BeginSetDisplayPixels(HostDisplayPixelFormat format, u32 width, u32 height, void** out_buffer,
|
||||
u32* out_pitch) override;
|
||||
void EndSetDisplayPixels() override;
|
||||
|
||||
bool GetHostRefreshRate(float* refresh_rate) override;
|
||||
|
||||
virtual void SetVSync(bool enabled) override;
|
||||
|
||||
virtual bool Render() override;
|
||||
virtual bool RenderScreenshot(u32 width, u32 height, std::vector<u32>* out_pixels, u32* out_stride,
|
||||
HostDisplayPixelFormat* out_format) override;
|
||||
|
||||
static AdapterAndModeList StaticGetAdapterAndModeList();
|
||||
|
||||
protected:
|
||||
enum : u32
|
||||
{
|
||||
DISPLAY_UNIFORM_BUFFER_SIZE = 65536,
|
||||
TEXTURE_STREAMING_BUFFER_SIZE = 4 * 1024 * 1024
|
||||
};
|
||||
|
||||
static AdapterAndModeList GetAdapterAndModeList(IDXGIFactory* dxgi_factory);
|
||||
|
||||
virtual bool CreateResources() override;
|
||||
virtual void DestroyResources() override;
|
||||
|
||||
virtual bool CreateImGuiContext();
|
||||
virtual void DestroyImGuiContext();
|
||||
virtual bool UpdateImGuiFontTexture() override;
|
||||
|
||||
bool CreateSwapChain(const DXGI_MODE_DESC* fullscreen_mode);
|
||||
bool CreateSwapChainRTV();
|
||||
void DestroySwapChainRTVs();
|
||||
|
||||
void RenderDisplay(ID3D12GraphicsCommandList* cmdlist);
|
||||
void RenderSoftwareCursor(ID3D12GraphicsCommandList* cmdlist);
|
||||
void RenderImGui(ID3D12GraphicsCommandList* cmdlist);
|
||||
|
||||
void RenderDisplay(ID3D12GraphicsCommandList* cmdlist, s32 left, s32 top, s32 width, s32 height, void* texture_handle,
|
||||
u32 texture_width, s32 texture_height, s32 texture_view_x, s32 texture_view_y,
|
||||
s32 texture_view_width, s32 texture_view_height, bool linear_filter);
|
||||
void RenderSoftwareCursor(ID3D12GraphicsCommandList* cmdlist, s32 left, s32 top, s32 width, s32 height,
|
||||
HostDisplayTexture* texture_handle);
|
||||
|
||||
ComPtr<IDXGIFactory> m_dxgi_factory;
|
||||
ComPtr<IDXGISwapChain> m_swap_chain;
|
||||
std::vector<D3D12::Texture> m_swap_chain_buffers;
|
||||
u32 m_current_swap_chain_buffer = 0;
|
||||
|
||||
ComPtr<ID3D12RootSignature> m_display_root_signature;
|
||||
ComPtr<ID3D12PipelineState> m_display_pipeline;
|
||||
ComPtr<ID3D12PipelineState> m_software_cursor_pipeline;
|
||||
D3D12::DescriptorHandle m_point_sampler;
|
||||
D3D12::DescriptorHandle m_linear_sampler;
|
||||
|
||||
D3D12::Texture m_display_pixels_texture;
|
||||
D3D12::StreamBuffer m_display_uniform_buffer;
|
||||
D3D12::StagingTexture m_readback_staging_texture;
|
||||
|
||||
bool m_allow_tearing_supported = false;
|
||||
bool m_using_allow_tearing = false;
|
||||
bool m_vsync = true;
|
||||
};
|
||||
|
||||
} // namespace FrontendCommon
|
|
@ -10,6 +10,7 @@
|
|||
<ClCompile Include="fullscreen_ui.cpp" />
|
||||
<ClCompile Include="fullscreen_ui_progress_callback.cpp" />
|
||||
<ClCompile Include="game_database.cpp" />
|
||||
<ClCompile Include="d3d12_host_display.cpp" />
|
||||
<ClCompile Include="game_list.cpp" />
|
||||
<ClCompile Include="game_settings.cpp" />
|
||||
<ClCompile Include="http_downloader.cpp" />
|
||||
|
@ -17,6 +18,7 @@
|
|||
<ClCompile Include="icon.cpp" />
|
||||
<ClCompile Include="imgui_fullscreen.cpp" />
|
||||
<ClCompile Include="imgui_impl_dx11.cpp" />
|
||||
<ClCompile Include="imgui_impl_dx12.cpp" />
|
||||
<ClCompile Include="imgui_impl_opengl3.cpp" />
|
||||
<ClCompile Include="imgui_impl_vulkan.cpp" />
|
||||
<ClCompile Include="imgui_styles.cpp" />
|
||||
|
@ -45,6 +47,7 @@
|
|||
<ClInclude Include="fullscreen_ui.h" />
|
||||
<ClInclude Include="fullscreen_ui_progress_callback.h" />
|
||||
<ClInclude Include="game_database.h" />
|
||||
<ClInclude Include="d3d12_host_display.h" />
|
||||
<ClInclude Include="game_list.h" />
|
||||
<ClInclude Include="game_settings.h" />
|
||||
<ClInclude Include="http_downloader.h" />
|
||||
|
@ -52,6 +55,7 @@
|
|||
<ClInclude Include="icon.h" />
|
||||
<ClInclude Include="imgui_fullscreen.h" />
|
||||
<ClInclude Include="imgui_impl_dx11.h" />
|
||||
<ClInclude Include="imgui_impl_dx12.h" />
|
||||
<ClInclude Include="imgui_impl_opengl3.h" />
|
||||
<ClInclude Include="imgui_impl_vulkan.h" />
|
||||
<ClInclude Include="imgui_styles.h" />
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
<ClCompile Include="game_database.cpp" />
|
||||
<ClCompile Include="inhibit_screensaver.cpp" />
|
||||
<ClCompile Include="xaudio2_audio_stream.cpp" />
|
||||
<ClCompile Include="d3d12_host_display.cpp" />
|
||||
<ClCompile Include="imgui_impl_dx12.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="icon.h" />
|
||||
|
@ -69,6 +71,8 @@
|
|||
<ClInclude Include="game_database.h" />
|
||||
<ClInclude Include="inhibit_screensaver.h" />
|
||||
<ClInclude Include="xaudio2_audio_stream.h" />
|
||||
<ClInclude Include="d3d12_host_display.h" />
|
||||
<ClInclude Include="imgui_impl_dx12.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="font_roboto_regular.inl" />
|
||||
|
|
533
src/frontend-common/imgui_impl_dx12.cpp
Normal file
533
src/frontend-common/imgui_impl_dx12.cpp
Normal file
|
@ -0,0 +1,533 @@
|
|||
// dear imgui: Renderer Backend for DirectX12
|
||||
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID!
|
||||
// [X] Renderer: Support for large meshes (64k+ vertices) with 16-bit indices.
|
||||
|
||||
// Important: to compile on 32-bit systems, this backend requires code to be compiled with '#define ImTextureID ImU64'.
|
||||
// This is because we need ImTextureID to carry a 64-bit value and by default ImTextureID is defined as void*.
|
||||
// This define is set in the example .vcxproj file and need to be replicated in your app or by adding it to your imconfig.h file.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
||||
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
||||
|
||||
// CHANGELOG
|
||||
// (minor and older changes stripped away, please see git history for details)
|
||||
// 2021-01-11: DirectX12: Improve Windows 7 compatibility (for D3D12On7) by loading d3d12.dll dynamically.
|
||||
// 2020-09-16: DirectX12: Avoid rendering calls with zero-sized scissor rectangle since it generates a validation layer warning.
|
||||
// 2020-09-08: DirectX12: Clarified support for building on 32-bit systems by redefining ImTextureID.
|
||||
// 2019-10-18: DirectX12: *BREAKING CHANGE* Added extra ID3D12DescriptorHeap parameter to ImGui_ImplDX12_Init() function.
|
||||
// 2019-05-29: DirectX12: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||
// 2019-04-30: DirectX12: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||
// 2019-03-29: Misc: Various minor tidying up.
|
||||
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
|
||||
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||
// 2018-06-12: DirectX12: Moved the ID3D12GraphicsCommandList* parameter from NewFrame() to RenderDrawData().
|
||||
// 2018-06-08: Misc: Extracted imgui_impl_dx12.cpp/.h away from the old combined DX12+Win32 example.
|
||||
// 2018-06-08: DirectX12: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle (to ease support for future multi-viewport).
|
||||
// 2018-02-22: Merged into master with all Win32 code synchronized to other examples.
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_dx12.h"
|
||||
|
||||
// DirectX
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_4.h>
|
||||
#include <d3dcompiler.h>
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
|
||||
#endif
|
||||
|
||||
#include "common/d3d12/texture.h"
|
||||
#include "common/d3d12/context.h"
|
||||
|
||||
// DirectX data
|
||||
static ID3D12Device* g_pd3dDevice = NULL;
|
||||
static ID3D12RootSignature* g_pRootSignature = NULL;
|
||||
static ID3D12PipelineState* g_pPipelineState = NULL;
|
||||
static DXGI_FORMAT g_RTVFormat = DXGI_FORMAT_UNKNOWN;
|
||||
static D3D12::Texture g_FontTexture;
|
||||
|
||||
struct FrameResources
|
||||
{
|
||||
ID3D12Resource* IndexBuffer;
|
||||
ID3D12Resource* VertexBuffer;
|
||||
int IndexBufferSize;
|
||||
int VertexBufferSize;
|
||||
};
|
||||
static FrameResources* g_pFrameResources = NULL;
|
||||
static UINT g_numFramesInFlight = 0;
|
||||
static UINT g_frameIndex = UINT_MAX;
|
||||
|
||||
template<typename T>
|
||||
static void SafeRelease(T*& res)
|
||||
{
|
||||
if (res)
|
||||
res->Release();
|
||||
res = NULL;
|
||||
}
|
||||
|
||||
struct VERTEX_CONSTANT_BUFFER
|
||||
{
|
||||
float mvp[4][4];
|
||||
};
|
||||
|
||||
static void ImGui_ImplDX12_SetupRenderState(ImDrawData* draw_data, ID3D12GraphicsCommandList* ctx, FrameResources* fr)
|
||||
{
|
||||
// Setup orthographic projection matrix into our constant buffer
|
||||
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
|
||||
VERTEX_CONSTANT_BUFFER vertex_constant_buffer;
|
||||
{
|
||||
float L = draw_data->DisplayPos.x;
|
||||
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||
float T = draw_data->DisplayPos.y;
|
||||
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||
float mvp[4][4] =
|
||||
{
|
||||
{ 2.0f / (R - L), 0.0f, 0.0f, 0.0f },
|
||||
{ 0.0f, 2.0f / (T - B), 0.0f, 0.0f },
|
||||
{ 0.0f, 0.0f, 0.5f, 0.0f },
|
||||
{ (R + L) / (L - R), (T + B) / (B - T), 0.5f, 1.0f },
|
||||
};
|
||||
memcpy(&vertex_constant_buffer.mvp, mvp, sizeof(mvp));
|
||||
}
|
||||
|
||||
// Setup viewport
|
||||
D3D12_VIEWPORT vp;
|
||||
memset(&vp, 0, sizeof(D3D12_VIEWPORT));
|
||||
vp.Width = draw_data->DisplaySize.x;
|
||||
vp.Height = draw_data->DisplaySize.y;
|
||||
vp.MinDepth = 0.0f;
|
||||
vp.MaxDepth = 1.0f;
|
||||
vp.TopLeftX = vp.TopLeftY = 0.0f;
|
||||
ctx->RSSetViewports(1, &vp);
|
||||
|
||||
// Bind shader and vertex buffers
|
||||
unsigned int stride = sizeof(ImDrawVert);
|
||||
unsigned int offset = 0;
|
||||
D3D12_VERTEX_BUFFER_VIEW vbv;
|
||||
memset(&vbv, 0, sizeof(D3D12_VERTEX_BUFFER_VIEW));
|
||||
vbv.BufferLocation = fr->VertexBuffer->GetGPUVirtualAddress() + offset;
|
||||
vbv.SizeInBytes = fr->VertexBufferSize * stride;
|
||||
vbv.StrideInBytes = stride;
|
||||
ctx->IASetVertexBuffers(0, 1, &vbv);
|
||||
D3D12_INDEX_BUFFER_VIEW ibv;
|
||||
memset(&ibv, 0, sizeof(D3D12_INDEX_BUFFER_VIEW));
|
||||
ibv.BufferLocation = fr->IndexBuffer->GetGPUVirtualAddress();
|
||||
ibv.SizeInBytes = fr->IndexBufferSize * sizeof(ImDrawIdx);
|
||||
ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
|
||||
ctx->IASetIndexBuffer(&ibv);
|
||||
ctx->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
ctx->SetPipelineState(g_pPipelineState);
|
||||
ctx->SetGraphicsRootSignature(g_pRootSignature);
|
||||
ctx->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0);
|
||||
|
||||
// Setup blend factor
|
||||
const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
|
||||
ctx->OMSetBlendFactor(blend_factor);
|
||||
}
|
||||
|
||||
// Render function
|
||||
void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* ctx)
|
||||
{
|
||||
// Avoid rendering when minimized
|
||||
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
|
||||
return;
|
||||
|
||||
// FIXME: I'm assuming that this only gets called once per frame!
|
||||
// If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator.
|
||||
g_frameIndex = g_frameIndex + 1;
|
||||
FrameResources* fr = &g_pFrameResources[g_frameIndex % g_numFramesInFlight];
|
||||
|
||||
// Create and grow vertex/index buffers if needed
|
||||
if (fr->VertexBuffer == NULL || fr->VertexBufferSize < draw_data->TotalVtxCount)
|
||||
{
|
||||
SafeRelease(fr->VertexBuffer);
|
||||
fr->VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||
D3D12_HEAP_PROPERTIES props;
|
||||
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
|
||||
props.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
D3D12_RESOURCE_DESC desc;
|
||||
memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC));
|
||||
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
desc.Width = fr->VertexBufferSize * sizeof(ImDrawVert);
|
||||
desc.Height = 1;
|
||||
desc.DepthOrArraySize = 1;
|
||||
desc.MipLevels = 1;
|
||||
desc.Format = DXGI_FORMAT_UNKNOWN;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||
if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&fr->VertexBuffer)) < 0)
|
||||
return;
|
||||
}
|
||||
if (fr->IndexBuffer == NULL || fr->IndexBufferSize < draw_data->TotalIdxCount)
|
||||
{
|
||||
SafeRelease(fr->IndexBuffer);
|
||||
fr->IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||
D3D12_HEAP_PROPERTIES props;
|
||||
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
|
||||
props.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
D3D12_RESOURCE_DESC desc;
|
||||
memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC));
|
||||
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
desc.Width = fr->IndexBufferSize * sizeof(ImDrawIdx);
|
||||
desc.Height = 1;
|
||||
desc.DepthOrArraySize = 1;
|
||||
desc.MipLevels = 1;
|
||||
desc.Format = DXGI_FORMAT_UNKNOWN;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||
if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&fr->IndexBuffer)) < 0)
|
||||
return;
|
||||
}
|
||||
|
||||
// Upload vertex/index data into a single contiguous GPU buffer
|
||||
void* vtx_resource, * idx_resource;
|
||||
D3D12_RANGE range;
|
||||
memset(&range, 0, sizeof(D3D12_RANGE));
|
||||
if (fr->VertexBuffer->Map(0, &range, &vtx_resource) != S_OK)
|
||||
return;
|
||||
if (fr->IndexBuffer->Map(0, &range, &idx_resource) != S_OK)
|
||||
return;
|
||||
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource;
|
||||
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource;
|
||||
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||
{
|
||||
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||||
memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
|
||||
memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
|
||||
vtx_dst += cmd_list->VtxBuffer.Size;
|
||||
idx_dst += cmd_list->IdxBuffer.Size;
|
||||
}
|
||||
fr->VertexBuffer->Unmap(0, &range);
|
||||
fr->IndexBuffer->Unmap(0, &range);
|
||||
|
||||
// Setup desired DX state
|
||||
ImGui_ImplDX12_SetupRenderState(draw_data, ctx, fr);
|
||||
|
||||
// Render command lists
|
||||
// (Because we merged all buffers into a single one, we maintain our own offset into them)
|
||||
int global_vtx_offset = 0;
|
||||
int global_idx_offset = 0;
|
||||
ImVec2 clip_off = draw_data->DisplayPos;
|
||||
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||
{
|
||||
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||||
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
|
||||
{
|
||||
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
|
||||
if (pcmd->UserCallback != NULL)
|
||||
{
|
||||
// User callback, registered via ImDrawList::AddCallback()
|
||||
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||
ImGui_ImplDX12_SetupRenderState(draw_data, ctx, fr);
|
||||
else
|
||||
pcmd->UserCallback(cmd_list, pcmd);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Apply Scissor, Bind texture, Draw
|
||||
const D3D12_RECT r = { (LONG)(pcmd->ClipRect.x - clip_off.x), (LONG)(pcmd->ClipRect.y - clip_off.y), (LONG)(pcmd->ClipRect.z - clip_off.x), (LONG)(pcmd->ClipRect.w - clip_off.y) };
|
||||
if (r.right > r.left && r.bottom > r.top)
|
||||
{
|
||||
ctx->SetGraphicsRootDescriptorTable(1, reinterpret_cast<D3D12::Texture*>(pcmd->TextureId)->GetSRVDescriptor());
|
||||
ctx->RSSetScissorRects(1, &r);
|
||||
ctx->DrawIndexedInstanced(pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
global_idx_offset += cmd_list->IdxBuffer.Size;
|
||||
global_vtx_offset += cmd_list->VtxBuffer.Size;
|
||||
}
|
||||
}
|
||||
|
||||
bool ImGui_ImplDX12_CreateFontsTexture()
|
||||
{
|
||||
// Build texture atlas
|
||||
ImGuiIO & io = ImGui::GetIO();
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
|
||||
|
||||
// Upload texture to graphics system
|
||||
D3D12::Texture texture;
|
||||
if (!texture.Create(width, height, 1, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE) ||
|
||||
!texture.LoadData(0, 0, width, height, pixels, width * sizeof(u32)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
g_FontTexture = std::move(texture);
|
||||
|
||||
// Store our identifier
|
||||
io.Fonts->TexID = reinterpret_cast<ImTextureID>(&g_FontTexture);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ImGui_ImplDX12_CreateDeviceObjects()
|
||||
{
|
||||
if (!g_pd3dDevice)
|
||||
return false;
|
||||
if (g_pPipelineState)
|
||||
ImGui_ImplDX12_InvalidateDeviceObjects();
|
||||
|
||||
// Create the root signature
|
||||
{
|
||||
D3D12_DESCRIPTOR_RANGE descRange = {};
|
||||
descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
|
||||
descRange.NumDescriptors = 1;
|
||||
descRange.BaseShaderRegister = 0;
|
||||
descRange.RegisterSpace = 0;
|
||||
descRange.OffsetInDescriptorsFromTableStart = 0;
|
||||
|
||||
D3D12_ROOT_PARAMETER param[2] = {};
|
||||
|
||||
param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
|
||||
param[0].Constants.ShaderRegister = 0;
|
||||
param[0].Constants.RegisterSpace = 0;
|
||||
param[0].Constants.Num32BitValues = 16;
|
||||
param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
|
||||
|
||||
param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
||||
param[1].DescriptorTable.NumDescriptorRanges = 1;
|
||||
param[1].DescriptorTable.pDescriptorRanges = &descRange;
|
||||
param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
|
||||
|
||||
D3D12_STATIC_SAMPLER_DESC staticSampler = {};
|
||||
staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
|
||||
staticSampler.MipLODBias = 0.f;
|
||||
staticSampler.MaxAnisotropy = 0;
|
||||
staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
|
||||
staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
|
||||
staticSampler.MinLOD = 0.f;
|
||||
staticSampler.MaxLOD = 0.f;
|
||||
staticSampler.ShaderRegister = 0;
|
||||
staticSampler.RegisterSpace = 0;
|
||||
staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
|
||||
|
||||
D3D12_ROOT_SIGNATURE_DESC desc = {};
|
||||
desc.NumParameters = _countof(param);
|
||||
desc.pParameters = param;
|
||||
desc.NumStaticSamplers = 1;
|
||||
desc.pStaticSamplers = &staticSampler;
|
||||
desc.Flags =
|
||||
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
|
||||
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
|
||||
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
|
||||
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
|
||||
|
||||
auto blob = g_d3d12_context->SerializeRootSignature(&desc);
|
||||
if (!blob)
|
||||
return false;
|
||||
|
||||
g_pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&g_pRootSignature));
|
||||
}
|
||||
|
||||
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
|
||||
// If you would like to use this DX12 sample code but remove this dependency you can:
|
||||
// 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
|
||||
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
|
||||
// See https://github.com/ocornut/imgui/pull/638 for sources and details.
|
||||
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc;
|
||||
memset(&psoDesc, 0, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));
|
||||
psoDesc.NodeMask = 1;
|
||||
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||
psoDesc.pRootSignature = g_pRootSignature;
|
||||
psoDesc.SampleMask = UINT_MAX;
|
||||
psoDesc.NumRenderTargets = 1;
|
||||
psoDesc.RTVFormats[0] = g_RTVFormat;
|
||||
psoDesc.SampleDesc.Count = 1;
|
||||
psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
|
||||
|
||||
ID3DBlob* vertexShaderBlob;
|
||||
ID3DBlob* pixelShaderBlob;
|
||||
|
||||
// Create the vertex shader
|
||||
{
|
||||
static const char* vertexShader =
|
||||
"cbuffer vertexBuffer : register(b0) \
|
||||
{\
|
||||
float4x4 ProjectionMatrix; \
|
||||
};\
|
||||
struct VS_INPUT\
|
||||
{\
|
||||
float2 pos : POSITION;\
|
||||
float4 col : COLOR0;\
|
||||
float2 uv : TEXCOORD0;\
|
||||
};\
|
||||
\
|
||||
struct PS_INPUT\
|
||||
{\
|
||||
float4 pos : SV_POSITION;\
|
||||
float4 col : COLOR0;\
|
||||
float2 uv : TEXCOORD0;\
|
||||
};\
|
||||
\
|
||||
PS_INPUT main(VS_INPUT input)\
|
||||
{\
|
||||
PS_INPUT output;\
|
||||
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
|
||||
output.col = input.col;\
|
||||
output.uv = input.uv;\
|
||||
return output;\
|
||||
}";
|
||||
|
||||
if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &vertexShaderBlob, NULL)))
|
||||
return false; // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||
psoDesc.VS = { vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize() };
|
||||
|
||||
// Create the input layout
|
||||
static D3D12_INPUT_ELEMENT_DESC local_layout[] =
|
||||
{
|
||||
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)IM_OFFSETOF(ImDrawVert, pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
||||
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)IM_OFFSETOF(ImDrawVert, uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
||||
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)IM_OFFSETOF(ImDrawVert, col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
||||
};
|
||||
psoDesc.InputLayout = { local_layout, 3 };
|
||||
}
|
||||
|
||||
// Create the pixel shader
|
||||
{
|
||||
static const char* pixelShader =
|
||||
"struct PS_INPUT\
|
||||
{\
|
||||
float4 pos : SV_POSITION;\
|
||||
float4 col : COLOR0;\
|
||||
float2 uv : TEXCOORD0;\
|
||||
};\
|
||||
SamplerState sampler0 : register(s0);\
|
||||
Texture2D texture0 : register(t0);\
|
||||
\
|
||||
float4 main(PS_INPUT input) : SV_Target\
|
||||
{\
|
||||
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
|
||||
return out_col; \
|
||||
}";
|
||||
|
||||
if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &pixelShaderBlob, NULL)))
|
||||
{
|
||||
vertexShaderBlob->Release();
|
||||
return false; // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||
}
|
||||
psoDesc.PS = { pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize() };
|
||||
}
|
||||
|
||||
// Create the blending setup
|
||||
{
|
||||
D3D12_BLEND_DESC& desc = psoDesc.BlendState;
|
||||
desc.AlphaToCoverageEnable = false;
|
||||
desc.RenderTarget[0].BlendEnable = true;
|
||||
desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
|
||||
desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
|
||||
desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
|
||||
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
|
||||
desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
|
||||
desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
|
||||
desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
|
||||
}
|
||||
|
||||
// Create the rasterizer state
|
||||
{
|
||||
D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState;
|
||||
desc.FillMode = D3D12_FILL_MODE_SOLID;
|
||||
desc.CullMode = D3D12_CULL_MODE_NONE;
|
||||
desc.FrontCounterClockwise = FALSE;
|
||||
desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
|
||||
desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
|
||||
desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
|
||||
desc.DepthClipEnable = true;
|
||||
desc.MultisampleEnable = FALSE;
|
||||
desc.AntialiasedLineEnable = FALSE;
|
||||
desc.ForcedSampleCount = 0;
|
||||
desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
|
||||
}
|
||||
|
||||
// Create depth-stencil State
|
||||
{
|
||||
D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState;
|
||||
desc.DepthEnable = false;
|
||||
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
|
||||
desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
|
||||
desc.StencilEnable = false;
|
||||
desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
|
||||
desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
|
||||
desc.BackFace = desc.FrontFace;
|
||||
}
|
||||
|
||||
HRESULT result_pipeline_state = g_pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&g_pPipelineState));
|
||||
vertexShaderBlob->Release();
|
||||
pixelShaderBlob->Release();
|
||||
if (result_pipeline_state != S_OK)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGui_ImplDX12_InvalidateDeviceObjects()
|
||||
{
|
||||
if (!g_pd3dDevice)
|
||||
return;
|
||||
|
||||
SafeRelease(g_pRootSignature);
|
||||
SafeRelease(g_pPipelineState);
|
||||
g_FontTexture.Destroy(true);
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.Fonts->SetTexID(NULL); // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
|
||||
|
||||
for (UINT i = 0; i < g_numFramesInFlight; i++)
|
||||
{
|
||||
FrameResources* fr = &g_pFrameResources[i];
|
||||
SafeRelease(fr->IndexBuffer);
|
||||
SafeRelease(fr->VertexBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format)
|
||||
{
|
||||
// Setup backend capabilities flags
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.BackendRendererName = "imgui_impl_dx12";
|
||||
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||
|
||||
g_pd3dDevice = device;
|
||||
g_RTVFormat = rtv_format;
|
||||
g_pFrameResources = new FrameResources[num_frames_in_flight];
|
||||
g_numFramesInFlight = num_frames_in_flight;
|
||||
g_frameIndex = UINT_MAX;
|
||||
|
||||
// Create buffers with a default size (they will later be grown as needed)
|
||||
for (int i = 0; i < num_frames_in_flight; i++)
|
||||
{
|
||||
FrameResources* fr = &g_pFrameResources[i];
|
||||
fr->IndexBuffer = NULL;
|
||||
fr->VertexBuffer = NULL;
|
||||
fr->IndexBufferSize = 10000;
|
||||
fr->VertexBufferSize = 5000;
|
||||
}
|
||||
|
||||
return ImGui_ImplDX12_CreateDeviceObjects();
|
||||
}
|
||||
|
||||
void ImGui_ImplDX12_Shutdown()
|
||||
{
|
||||
ImGui_ImplDX12_InvalidateDeviceObjects();
|
||||
delete[] g_pFrameResources;
|
||||
g_pFrameResources = NULL;
|
||||
g_pd3dDevice = NULL;
|
||||
g_numFramesInFlight = 0;
|
||||
g_frameIndex = UINT_MAX;
|
||||
}
|
47
src/frontend-common/imgui_impl_dx12.h
Normal file
47
src/frontend-common/imgui_impl_dx12.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
// dear imgui: Renderer Backend for DirectX12
|
||||
// This needs to be used along with a Platform Backend (e.g. Win32)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID!
|
||||
// [X] Renderer: Support for large meshes (64k+ vertices) with 16-bit indices.
|
||||
|
||||
// Important: to compile on 32-bit systems, this backend requires code to be compiled with '#define ImTextureID ImU64'.
|
||||
// This is because we need ImTextureID to carry a 64-bit value and by default ImTextureID is defined as void*.
|
||||
// This define is set in the example .vcxproj file and need to be replicated in your app or by adding it to your imconfig.h file.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
||||
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
||||
|
||||
#pragma once
|
||||
#include "imgui.h" // IMGUI_IMPL_API
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (push)
|
||||
#pragma warning (disable: 4471) // a forward declaration of an unscoped enumeration must have an underlying type
|
||||
#endif
|
||||
|
||||
enum DXGI_FORMAT;
|
||||
struct ID3D12Device;
|
||||
struct ID3D12DescriptorHeap;
|
||||
struct ID3D12GraphicsCommandList;
|
||||
struct D3D12_CPU_DESCRIPTOR_HANDLE;
|
||||
struct D3D12_GPU_DESCRIPTOR_HANDLE;
|
||||
|
||||
// cmd_list is the command list that the implementation will use to render imgui draw lists.
|
||||
// Before calling the render function, caller must prepare cmd_list by resetting it and setting the appropriate
|
||||
// render target and descriptor heap that contains font_srv_cpu_desc_handle/font_srv_gpu_desc_handle.
|
||||
// font_srv_cpu_desc_handle and font_srv_gpu_desc_handle are handles to a single SRV descriptor to use for the internal font texture.
|
||||
IMGUI_IMPL_API bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format);
|
||||
IMGUI_IMPL_API void ImGui_ImplDX12_Shutdown();
|
||||
IMGUI_IMPL_API void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* graphics_command_list);
|
||||
|
||||
// Use if you want to reset your rendering device without losing Dear ImGui state.
|
||||
IMGUI_IMPL_API void ImGui_ImplDX12_InvalidateDeviceObjects();
|
||||
IMGUI_IMPL_API bool ImGui_ImplDX12_CreateDeviceObjects();
|
||||
IMGUI_IMPL_API bool ImGui_ImplDX12_CreateFontsTexture();
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (pop)
|
||||
#endif
|
||||
|
Loading…
Reference in a new issue