From cf05cadb4d61e6a67ae912ec773e92dd85db5331 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Thu, 14 May 2026 23:07:01 +0000 Subject: [PATCH] core, libretro, common: wire up D3D12 renderer for libretro Wires the existing D3D12 hardware renderer (src/core/gpu_hw_d3d12.cpp, src/common/d3d12/*) into the libretro frontend, behind a USE_D3D12=1 gate. The opt-in is because: 1. retro_hw_render_interface_d3d12 is supported by recent RetroArch builds but not all of them - the SET_HW_RENDER environment call will fail at runtime where unsupported. 2. Not every Windows target ships d3d12.lib in its toolchain; the gate keeps the existing default Windows build path working. When USE_D3D12=1 is set on the make command line, the libretro Makefile pulls in the seven common/d3d12 TUs, gpu_hw_d3d12.cpp, the new LibretroD3D12HostDisplay class, links against d3d12.lib, and exposes "D3D12" in the renderer dropdown next to D3D11. == src/common/d3d12 == D3D12::Context grew a CreateForLibretro(device, command_queue) static factory next to its existing Create(dxgi_factory, ...). The libretro path adopts the frontend's device + command queue (the frontend retains ownership; we just AddRef them via ComPtr) and then creates only the resources we own: fence, descriptor heaps, command lists, texture upload stream buffer. Feature level is pinned at FL11_0; the gpu_hw_d3d12 shader/PSO setup targets that baseline. CreateForLibretro still loads d3d12.dll because PFN_D3D12_SERIALIZE_ROOT_SIGNATURE is needed by Context::SerializeRootSignature, which gpu_hw_d3d12.cpp uses on initial root-signature creation. Destroy() is unchanged; the borrowed device/queue ComPtrs simply release their AddRef in the Context destructor and the frontend keeps the underlying objects alive. == src/core/gpu_hw_d3d12.h, .cpp == Added LibretroD3D12HostDisplay (declaration in the header above GPU_HW_D3D12, implementation appended ahead of GPU_HW_D3D12's constructor body in the .cpp). The class mirrors LibretroD3D11HostDisplay's surface and behaves identically on the libretro-API side, with two differences driven by the D3D12 libretro interface contract: 1. There is no analogue to ID3D11DeviceContext in D3D12 - work is recorded on command lists owned by D3D12::Context. The class therefore does not store m_device / m_context members; callers that need the device go through g_d3d12_context->GetDevice() as the rest of gpu_hw_d3d12.cpp already does. GetRenderDevice() returns g_d3d12_context->GetDevice() for HostDisplay-API callers; GetRenderContext() returns nullptr. 2. Present is the retro_hw_render_interface_d3d12::set_texture callback rather than a swapchain blit. LibretroD3D12HostDisplay ::Render() transitions the GPU_HW_D3D12-produced display texture into the frontend's required_state, flushes our pending command list via ExecuteCommandList(false), calls set_texture(frontend_handle, ID3D12Resource*, format), then signals the frontend a HW frame is ready via g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, width, height, 0). The frame-dupe (NULL) path for runahead / fast-forward video skip is identical to the D3D11 and SW renderer paths. The CreateTexture / BeginSetDisplayPixels / EndSetDisplayPixels paths return nullptr / false the same way LibretroHostDisplay does. They are not exercised on the libretro D3D12 path - GPU_HW _D3D12 owns its own D3D12::Texture instances directly, and the SW renderer (which is the only path that would BeginSetDisplay Pixels) uses LibretroHostDisplay rather than going through D3D12. An m_framebuffer member is reserved on the class for future software-cursor / lightgun-crosshair overlay work (which would composite over the display texture in an owned RTV before set_texture, the way gpu_hw_d3d11.cpp does it). For the initial wire-up the present path forwards the GPU_HW_D3D12 display texture directly. HAVE_D3D12 is #define'd before the libretro_d3d.h include to pull in the retro_hw_render_interface_d3d12 struct definition, matching the equivalent pattern in gpu_hw_d3d11.cpp. == src/libretro/libretro_host_interface.cpp == Four switch cases extended, all under #ifdef WIN32 + #ifdef USE _D3D12: - RetroHwContextToRenderer: maps RETRO_HW_CONTEXT_D3D12 = 9 to GPURenderer::HardwareD3D12 so the Auto-renderer path picks D3D12 when the frontend advertises it via GET_PREFERRED_HW_RENDER. - RenderAPIToRenderer: maps HostDisplay::RenderAPI::D3D12 to GPURenderer::HardwareD3D12 so reusing an existing hardware display across SwitchToHardwareRenderer rebinds correctly. - RequestHardwareRendererContext: calls LibretroD3D12HostDisplay::RequestHardwareRendererContext (sets context_type=RETRO_HW_CONTEXT_D3D12, version_major=12, version_minor=0). - SwitchToHardwareRenderer: instantiates LibretroD3D12HostDisplay when m_hw_render_callback.context_type indicates D3D12. Include line added for gpu_hw_d3d12.h alongside the existing gpu_hw_d3d11.h include. == src/libretro/libretro_core_options.h == Renderer dropdown gains {"D3D12", "Hardware (D3D12)"} entry, gated USE_D3D12 inside the existing WIN32 block. Settings ::ParseRendererName already had D3D12 in its s_gpu_renderer _names array (pre-existing), and Settings::GetRendererName reads from the same array, so the dropdown selection round-trips to/from GPURenderer::HardwareD3D12 correctly. == Makefile.libretro == Under the existing IS_WINDOWS guard, added a USE_D3D12=1 branch that: - Adds -DUSE_D3D12=1 to CXXFLAGS so the wiring under #ifdef USE _D3D12 in libretro_host_interface.cpp, libretro_core_options.h, gpu_hw_d3d12.cpp, and gpu_hw_d3d12.h gets compiled in. - Adds -ld3d12 to LDFLAGS so PFN_D3D12_SERIALIZE_ROOT_SIGNATURE resolves at link time (Context::SerializeRootSignature uses it). - Adds the seven common/d3d12 TUs (context, descriptor_heap_ manager, shader_cache, staging_texture, stream_buffer, texture, util) to SOURCES_CXX. - Adds src/core/gpu_hw_d3d12.cpp to SOURCES_CXX in the core section. Default (no USE_D3D12=1) keeps the existing Windows build unchanged. system.cpp's renderer dispatch already had the case GPURenderer::HardwareD3D12 case behind #ifdef USE_D3D12 and the forward declaration of GPU::CreateHardwareD3D12Renderer() was already in gpu.h, so no changes were needed there. == Build verification == Linux libretro build: clean, .so unchanged at 7,905,720 bytes (all new D3D12 code is gated under WIN32 + USE_D3D12 - no compiled-in effect on non-Windows targets), 25 retro_ exports preserved. Windows build with USE_D3D12=1 has not been verified in this environment - mingw cross-compile tools and the d3d12 SDK headers aren't available on the build host. Manual test on Windows required before pushing. 7 files changed, 414 insertions(+). --- Makefile.libretro | 27 +++ src/common/d3d12/context.cpp | 30 +++ src/common/d3d12/context.h | 8 + src/core/gpu_hw_d3d12.cpp | 268 +++++++++++++++++++++++ src/core/gpu_hw_d3d12.h | 57 +++++ src/libretro/libretro_core_options.h | 3 + src/libretro/libretro_host_interface.cpp | 21 ++ 7 files changed, 414 insertions(+) diff --git a/Makefile.libretro b/Makefile.libretro index 2ad85ad9..ab4e495f 100644 --- a/Makefile.libretro +++ b/Makefile.libretro @@ -322,6 +322,28 @@ SOURCES_CXX += \ $(COMMON_DIR)/d3d11/staging_texture.cpp \ $(COMMON_DIR)/d3d11/stream_buffer.cpp \ $(COMMON_DIR)/d3d11/texture.cpp + +# Direct3D 12 backend - Windows only, opt-in via USE_D3D12=1 on the +# make command line. d3d12 ships with Windows 10+ but the libretro +# frontend has to expose it via retro_hw_render_interface_d3d12; older +# RetroArch builds may not, so we keep this gated. Defining USE_D3D12 +# in CXXFLAGS turns on the renderer wiring (gpu_hw_d3d12, the libretro +# host display class, the libretro_host_interface switch cases, and +# the libretro_core_options dropdown entry). LDFLAGS adds -ld3d12 so +# CreateForLibretro's PFN_D3D12_SERIALIZE_ROOT_SIGNATURE lookup +# succeeds. +ifeq ($(USE_D3D12),1) +CXXFLAGS += -DUSE_D3D12=1 +LDFLAGS += -ld3d12 +SOURCES_CXX += \ + $(COMMON_DIR)/d3d12/context.cpp \ + $(COMMON_DIR)/d3d12/descriptor_heap_manager.cpp \ + $(COMMON_DIR)/d3d12/shader_cache.cpp \ + $(COMMON_DIR)/d3d12/staging_texture.cpp \ + $(COMMON_DIR)/d3d12/stream_buffer.cpp \ + $(COMMON_DIR)/d3d12/texture.cpp \ + $(COMMON_DIR)/d3d12/util.cpp +endif endif # --------------------------------------------------------------------- @@ -381,6 +403,11 @@ SOURCES_CXX += \ # D3D11 hardware GPU backend - Windows only. ifeq ($(IS_WINDOWS),1) SOURCES_CXX += $(CORE_SRC_DIR)/gpu_hw_d3d11.cpp + +# D3D12 hardware GPU backend - opt-in (see common-section comment). +ifeq ($(USE_D3D12),1) +SOURCES_CXX += $(CORE_SRC_DIR)/gpu_hw_d3d12.cpp +endif endif # Recompiler back-ends: shared TUs + per-arch TU. diff --git a/src/common/d3d12/context.cpp b/src/common/d3d12/context.cpp index 35fc44e1..cfd7c29f 100644 --- a/src/common/d3d12/context.cpp +++ b/src/common/d3d12/context.cpp @@ -143,6 +143,36 @@ bool Context::Create(IDXGIFactory* dxgi_factory, uint32_t adapter_index, bool en return true; } +bool Context::CreateForLibretro(ID3D12Device* device, ID3D12CommandQueue* command_queue) +{ + // We don't need d3d12.dll's PFN_D3D12_CREATE_DEVICE here - the frontend + // already loaded the library and created the device. The serializer entry + // (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE) is still required by + // SerializeRootSignature; load the library so its function pointers are + // resolved for that path. If LoadLibrary succeeds, UnloadD3D12Library is + // a no-op until Destroy(). + if (!LoadD3D12Library()) + return false; + + g_d3d12_context.reset(new Context()); + g_d3d12_context->m_device = device; + g_d3d12_context->m_command_queue = command_queue; + + // Match the same feature level CreateDevice would have selected; we + // create our own resources at FL11_0 regardless of what the frontend + // negotiated, so this is correct for libretro. + g_d3d12_context->m_feature_level = D3D_FEATURE_LEVEL_11_0; + + if (!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) diff --git a/src/common/d3d12/context.h b/src/common/d3d12/context.h index 27d3bb9a..b3b593ea 100644 --- a/src/common/d3d12/context.h +++ b/src/common/d3d12/context.h @@ -36,6 +36,14 @@ public: // Creates new device and context. static bool Create(IDXGIFactory* dxgi_factory, uint32_t adapter_index, bool enable_debug_layer); + // Creates a context that adopts the frontend's ID3D12Device and + // ID3D12CommandQueue (e.g. via retro_hw_render_interface_d3d12). + // The frontend retains ownership of both - we just AddRef them + // and create the rest of the context (descriptor heaps, command + // lists, fence, texture upload buffer) on top. enable_debug_layer + // is ignored; the frontend already configured the device. + static bool CreateForLibretro(ID3D12Device* device, ID3D12CommandQueue* command_queue); + // Destroys active context. static void Destroy(); diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 97219d4c..97c90356 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -12,9 +12,277 @@ #include "host_display.h" #include "host_interface.h" #include "system.h" +#define HAVE_D3D12 +#include "libretro_d3d.h" #include Log_SetChannel(GPU_HW_D3D12); +extern retro_environment_t g_retro_environment_callback; +extern retro_video_refresh_t g_retro_video_refresh_callback; + +// Index parallels HostDisplayPixelFormat (Unknown, RGBA8, BGRA8, RGB565, +// RGBA5551, Count). Same table layout as gpu_hw_d3d11.cpp; the Unknown +// slot resolves to DXGI_FORMAT_UNKNOWN as a value-0 sentinel. +static constexpr std::array(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}}; + +// ===================================================================== +// LibretroD3D12HostDisplay - libretro HostDisplay subclass for the D3D12 +// hardware renderer path. +// +// Mirrors LibretroD3D11HostDisplay structurally, with two architectural +// differences driven by the libretro D3D12 hardware interface +// (retro_hw_render_interface_d3d12 in libretro_d3d.h): +// +// 1. Device + queue ownership lives in the frontend. We adopt them +// via D3D12::Context::CreateForLibretro(); D3D12::Context::Destroy() +// releases our refs. m_device / m_context style members are absent +// because everything that needs the device goes through +// g_d3d12_context, which is what gpu_hw_d3d12.cpp uses anyway. +// +// 2. Present path is the set_texture callback rather than a swapchain +// blit. Render() composites into m_framebuffer (an RTV we own), +// transitions it to required_state, calls set_texture, then signals +// the frontend a HW frame is ready via +// g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, ...). +// +// The HostDisplay::SetDisplayPixels path (SW pixels uploaded to a +// frontend-readable texture via UpdateSubresource) is not implemented +// for D3D12 in the current libretro frontend integration; the SW +// renderer uses LibretroHostDisplay instead. BeginSetDisplayPixels +// returns false so any caller short-circuits. +// ===================================================================== + +LibretroD3D12HostDisplay::LibretroD3D12HostDisplay() = default; +LibretroD3D12HostDisplay::~LibretroD3D12HostDisplay() = default; + +bool LibretroD3D12HostDisplay::RequestHardwareRendererContext(retro_hw_render_callback* cb) +{ + cb->cache_context = false; + cb->bottom_left_origin = false; + cb->context_type = RETRO_HW_CONTEXT_D3D12; + cb->version_major = 12; + cb->version_minor = 0; + + return g_retro_environment_callback(RETRO_ENVIRONMENT_SET_HW_RENDER, cb); +} + +HostDisplay::RenderAPI LibretroD3D12HostDisplay::GetRenderAPI() const +{ + return RenderAPI::D3D12; +} + +void* LibretroD3D12HostDisplay::GetRenderDevice() const +{ + return g_d3d12_context ? g_d3d12_context->GetDevice() : nullptr; +} + +void* LibretroD3D12HostDisplay::GetRenderContext() const +{ + // D3D12 has no DeviceContext analogue - work is recorded on command + // lists owned by the Context. Return nullptr; callers either don't + // need it (D3D12 path) or already special-case the D3D12 RenderAPI. + return nullptr; +} + +bool LibretroD3D12HostDisplay::CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device, + bool threaded_presentation) +{ + retro_hw_render_interface* ri = nullptr; + if (!g_retro_environment_callback(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, &ri)) + { + Log_ErrorPrint("Failed to get HW render interface"); + return false; + } + else if (ri->interface_type != RETRO_HW_RENDER_INTERFACE_D3D12 || + ri->interface_version != RETRO_HW_RENDER_INTERFACE_D3D12_VERSION) + { + Log_ErrorPrintf("Unexpected HW interface - type %u version %u", static_cast(ri->interface_type), + static_cast(ri->interface_version)); + return false; + } + + const retro_hw_render_interface_d3d12* d3d12_ri = reinterpret_cast(ri); + if (!d3d12_ri->device || !d3d12_ri->queue || !d3d12_ri->set_texture) + { + Log_ErrorPrintf("Missing D3D12 device, queue, or set_texture callback"); + return false; + } + + if (!D3D12::Context::CreateForLibretro(d3d12_ri->device, d3d12_ri->queue)) + { + Log_ErrorPrint("Failed to adopt D3D12 device/queue for libretro"); + return false; + } + + m_set_texture = d3d12_ri->set_texture; + m_frontend_handle = d3d12_ri->handle; + m_required_state = d3d12_ri->required_state; + m_window_info = wi; + return true; +} + +bool LibretroD3D12HostDisplay::InitializeRenderDevice(std::string_view shader_cache_directory, bool debug_device, + bool threaded_presentation) +{ + return CreateResources(); +} + +void LibretroD3D12HostDisplay::DestroyRenderDevice() +{ + ClearSoftwareCursor(); + DestroyResources(); + + if (g_d3d12_context) + { + g_d3d12_context->WaitForGPUIdle(); + D3D12::Context::Destroy(); + } +} + +void LibretroD3D12HostDisplay::ResizeRenderWindow(int32_t new_window_width, int32_t new_window_height) +{ + m_window_info.surface_width = static_cast(new_window_width); + m_window_info.surface_height = static_cast(new_window_height); +} + +bool LibretroD3D12HostDisplay::ChangeRenderWindow(const WindowInfo& new_wi) +{ + // Check that the device/queue/handle haven't changed under us. + retro_hw_render_interface* ri = nullptr; + if (!g_retro_environment_callback(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, &ri)) + { + Log_ErrorPrint("Failed to get HW render interface"); + return false; + } + else if (ri->interface_type != RETRO_HW_RENDER_INTERFACE_D3D12 || + ri->interface_version != RETRO_HW_RENDER_INTERFACE_D3D12_VERSION) + { + Log_ErrorPrintf("Unexpected HW interface - type %u version %u", static_cast(ri->interface_type), + static_cast(ri->interface_version)); + return false; + } + + const retro_hw_render_interface_d3d12* d3d12_ri = reinterpret_cast(ri); + if (d3d12_ri->device != g_d3d12_context->GetDevice() || d3d12_ri->queue != g_d3d12_context->GetCommandQueue() || + d3d12_ri->set_texture != m_set_texture || d3d12_ri->handle != m_frontend_handle) + { + Log_ErrorPrintf("D3D12 device/queue/handle changed outside our control"); + return false; + } + + // required_state is allowed to change across a ChangeRenderWindow - + // re-cache it in case the frontend renegotiated. + m_required_state = d3d12_ri->required_state; + m_window_info = new_wi; + return true; +} + +std::unique_ptr LibretroD3D12HostDisplay::CreateTexture(uint32_t width, uint32_t height, uint32_t layers, + uint32_t levels, uint32_t samples, + HostDisplayPixelFormat format, + const void* data, uint32_t data_stride, + bool dynamic) +{ + // GPU_HW_D3D12 manages its own textures through D3D12::Texture directly. + // The HostDisplay::CreateTexture surface is only used by code paths that + // do not run for the libretro D3D12 backend (software cursor preload, + // SetDisplayPixels CPU-to-GPU upload), so this returns nullptr the same + // way LibretroHostDisplay does. + return nullptr; +} + +bool LibretroD3D12HostDisplay::SupportsDisplayPixelFormat(HostDisplayPixelFormat format) const +{ + const DXGI_FORMAT dfmt = s_display_pixel_format_mapping[static_cast(format)]; + return dfmt != DXGI_FORMAT_UNKNOWN && g_d3d12_context && g_d3d12_context->SupportsTextureFormat(dfmt); +} + +bool LibretroD3D12HostDisplay::BeginSetDisplayPixels(HostDisplayPixelFormat format, uint32_t width, uint32_t height, + void** out_buffer, uint32_t* out_pitch) +{ + // See comment on CreateTexture - this path is not wired for D3D12 + // libretro. The SW renderer uses LibretroHostDisplay. + return false; +} + +void LibretroD3D12HostDisplay::EndSetDisplayPixels() +{ +} + +bool LibretroD3D12HostDisplay::CreateResources() +{ + // Nothing to do up front - the framebuffer is sized lazily in + // CheckFramebufferSize when Render() sees the first display frame. + return true; +} + +void LibretroD3D12HostDisplay::DestroyResources() +{ + m_framebuffer.Destroy(false); +} + +bool LibretroD3D12HostDisplay::CheckFramebufferSize(uint32_t width, uint32_t height) +{ + if (m_framebuffer.GetWidth() == width && m_framebuffer.GetHeight() == height) + return true; + + m_framebuffer.Destroy(false); + return m_framebuffer.Create(width, height, 1, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, + DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); +} + +bool LibretroD3D12HostDisplay::Render() +{ + // No display texture this frame -> send the libretro frame-dupe + // signal (NULL frame), matching the SW and D3D11 paths. See the + // equivalent comment in gpu_hw_d3d11.cpp::Render(). + if (!HasDisplayTexture()) + { + g_retro_video_refresh_callback(nullptr, 0, 0, 0); + return true; + } + + const uint32_t resolution_scale = g_host_interface_storage.GetResolutionScale(); + const uint32_t display_width = static_cast(m_display_width) * resolution_scale; + const uint32_t display_height = static_cast(m_display_height) * resolution_scale; + + if (!CheckFramebufferSize(display_width, display_height)) + return false; + + // The GPU_HW_D3D12 path already produced the display image inside its + // own m_display_texture (or m_vram_texture) and called SetDisplayTexture + // with a D3D12::Texture* handle. For the libretro D3D12 frontend we + // do not need to perform an additional blit through a vertex/pixel + // shader pair the way D3D11 does - the source texture is already the + // composited display image, so we forward its resource directly to the + // frontend via set_texture. + // + // m_framebuffer therefore acts as a stable holder of an RTV-bindable + // resource only when the source path itself isn't already in + // PIXEL_SHADER_RESOURCE form. The current GPU_HW_D3D12::UpdateDisplay + // call sites guarantee the display texture is left in + // PIXEL_SHADER_RESOURCE state at end-of-frame, so the simpler model + // is: transition that texture into m_required_state and hand it over. + // + // We keep m_framebuffer around because future work (software cursor + // composition, lightgun crosshair overlay) will need an owned RTV + // to draw onto, matching what gpu_hw_d3d11.cpp does. For now it's + // unused on the present path. + D3D12::Texture* const display_texture = static_cast(const_cast(m_display_texture_handle)); + display_texture->TransitionToState(m_required_state); + + // Flush our pending command list before handing the texture over - + // otherwise the frontend may read stale contents. + g_d3d12_context->ExecuteCommandList(false); + + m_set_texture(m_frontend_handle, display_texture->GetResource(), DXGI_FORMAT_R8G8B8A8_UNORM); + g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, m_display_texture_view_width, + m_display_texture_view_height, 0); + return true; +} + GPU_HW_D3D12::GPU_HW_D3D12() = default; GPU_HW_D3D12::~GPU_HW_D3D12() diff --git a/src/core/gpu_hw_d3d12.h b/src/core/gpu_hw_d3d12.h index 63250a4f..3e134689 100644 --- a/src/core/gpu_hw_d3d12.h +++ b/src/core/gpu_hw_d3d12.h @@ -6,13 +6,70 @@ #include "common/dimensional_array.h" #include "gpu_hw.h" #include "gpu_hw_shadergen.h" +#include "host_display.h" #include "texture_replacements.h" #include #include +#include #include #include #include #include +#include +#include + +class LibretroD3D12HostDisplay final : public HostDisplay +{ +public: + LibretroD3D12HostDisplay(); + ~LibretroD3D12HostDisplay(); + + static bool RequestHardwareRendererContext(retro_hw_render_callback* cb); + + RenderAPI GetRenderAPI() const override; + void* GetRenderDevice() const override; + void* GetRenderContext() const override; + + bool CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device, + bool threaded_presentation) override; + bool InitializeRenderDevice(std::string_view shader_cache_directory, bool debug_device, + bool threaded_presentation) override; + void DestroyRenderDevice() override; + + void ResizeRenderWindow(int32_t new_window_width, int32_t new_window_height) override; + + bool ChangeRenderWindow(const WindowInfo& new_wi) override; + + std::unique_ptr CreateTexture(uint32_t width, uint32_t height, uint32_t layers, uint32_t levels, uint32_t samples, + HostDisplayPixelFormat format, const void* data, uint32_t data_stride, + bool dynamic = false) override; + bool SupportsDisplayPixelFormat(HostDisplayPixelFormat format) const override; + bool BeginSetDisplayPixels(HostDisplayPixelFormat format, uint32_t width, uint32_t height, void** out_buffer, + uint32_t* out_pitch) override; + void EndSetDisplayPixels() override; + + bool Render() override; + +protected: + bool CreateResources() override; + void DestroyResources() override; + +private: + template + using ComPtr = Microsoft::WRL::ComPtr; + + bool CheckFramebufferSize(uint32_t width, uint32_t height); + + // retro_hw_render_interface_d3d12::set_texture callback - the frontend + // reads from this resource in m_required_state to compose the final + // output frame. We hand it the framebuffer texture (post our display + // blit) at the end of Render(). + void (*m_set_texture)(void* handle, ID3D12Resource* texture, DXGI_FORMAT format) = nullptr; + void* m_frontend_handle = nullptr; + D3D12_RESOURCE_STATES m_required_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; + + D3D12::Texture m_framebuffer; +}; class GPU_HW_D3D12 : public GPU_HW { diff --git a/src/libretro/libretro_core_options.h b/src/libretro/libretro_core_options.h index 74f8cfb1..b96b983c 100644 --- a/src/libretro/libretro_core_options.h +++ b/src/libretro/libretro_core_options.h @@ -289,6 +289,9 @@ struct retro_core_option_v2_definition option_defs_us[] = { {"Auto", "Hardware (Auto)"}, #ifdef WIN32 {"D3D11", "Hardware (D3D11)"}, +#ifdef USE_D3D12 + {"D3D12", "Hardware (D3D12)"}, +#endif #endif {"OpenGL", "Hardware (OpenGL)"}, {"Vulkan", "Hardware (Vulkan)"}, diff --git a/src/libretro/libretro_host_interface.cpp b/src/libretro/libretro_host_interface.cpp index ed22a82d..5158dc9a 100644 --- a/src/libretro/libretro_host_interface.cpp +++ b/src/libretro/libretro_host_interface.cpp @@ -37,6 +37,9 @@ Log_SetChannel(HostInterface); #ifdef WIN32 #include "core/gpu_hw_d3d11.h" +#ifdef USE_D3D12 +#include "core/gpu_hw_d3d12.h" +#endif #endif RETRO_API unsigned retro_api_version(void) @@ -1886,6 +1889,10 @@ static std::optional RetroHwContextToRenderer(retro_hw_context_type #ifdef WIN32 case RETRO_HW_CONTEXT_D3D11: return GPURenderer::HardwareD3D11; +#ifdef USE_D3D12 + case RETRO_HW_CONTEXT_D3D12: + return GPURenderer::HardwareD3D12; +#endif #endif default: @@ -1907,6 +1914,10 @@ static std::optional RenderAPIToRenderer(HostDisplay::RenderAPI api #ifdef WIN32 case HostDisplay::RenderAPI::D3D11: return GPURenderer::HardwareD3D11; +#ifdef USE_D3D12 + case HostDisplay::RenderAPI::D3D12: + return GPURenderer::HardwareD3D12; +#endif #endif default: @@ -1951,6 +1962,11 @@ bool HostInterface::RequestHardwareRendererContext() case GPURenderer::HardwareD3D11: m_hw_render_callback_valid = LibretroD3D11HostDisplay::RequestHardwareRendererContext(&m_hw_render_callback); break; +#ifdef USE_D3D12 + case GPURenderer::HardwareD3D12: + m_hw_render_callback_valid = LibretroD3D12HostDisplay::RequestHardwareRendererContext(&m_hw_render_callback); + break; +#endif #endif case GPURenderer::HardwareVulkan: @@ -2032,6 +2048,11 @@ void HostInterface::SwitchToHardwareRenderer() case GPURenderer::HardwareD3D11: display = std::make_unique(); break; +#ifdef USE_D3D12 + case GPURenderer::HardwareD3D12: + display = std::make_unique(); + break; +#endif #endif default: