Key node 1: move main-window UI presentation onto the D3D12 render loop with native UI renderer, text system, and texture host. Key node 2: wire frame timing/FPS display, window runtime, swapchain presentation, and native screenshot capture around the new path. Key node 3: carry editor shell/workspace/viewport/panel interaction updates needed by the new renderer and detached window flow. Key node 4: pump async resource loads and scene bridge follow-up needed for scene content visibility in new_editor.
311 lines
10 KiB
C++
311 lines
10 KiB
C++
#include "D3D12WindowInteropHelpers.h"
|
|
|
|
#include <XCEngine/RHI/D3D12/D3D12Texture.h>
|
|
#include <XCEngine/RHI/RHITexture.h>
|
|
|
|
#include <array>
|
|
#include <unordered_set>
|
|
|
|
namespace XCEngine::UI::Editor::Host::D3D12WindowInteropHelpers {
|
|
|
|
std::string HrToInteropString(const char* operation, HRESULT hr) {
|
|
char buffer[128] = {};
|
|
sprintf_s(buffer, "%s failed with hr=0x%08X.", operation, static_cast<unsigned int>(hr));
|
|
return buffer;
|
|
}
|
|
|
|
D2D1_BITMAP_PROPERTIES1 BuildD2DBitmapProperties(
|
|
DXGI_FORMAT format,
|
|
D2D1_BITMAP_OPTIONS options) {
|
|
return D2D1::BitmapProperties1(
|
|
options,
|
|
D2D1::PixelFormat(format, D2D1_ALPHA_MODE_IGNORE),
|
|
96.0f,
|
|
96.0f);
|
|
}
|
|
|
|
bool IsInteropTextureHandle(const ::XCEngine::UI::UITextureHandle& texture) {
|
|
return texture.kind == ::XCEngine::UI::UITextureHandleKind::ShaderResourceView &&
|
|
texture.resourceHandle != 0u;
|
|
}
|
|
|
|
void CollectInteropTextureHandles(
|
|
const ::XCEngine::UI::UIDrawData& drawData,
|
|
std::vector<::XCEngine::UI::UITextureHandle>& outTextures) {
|
|
outTextures.clear();
|
|
std::unordered_set<std::uintptr_t> seenKeys = {};
|
|
for (const ::XCEngine::UI::UIDrawList& drawList : drawData.GetDrawLists()) {
|
|
for (const ::XCEngine::UI::UIDrawCommand& command : drawList.GetCommands()) {
|
|
if (!IsInteropTextureHandle(command.texture) ||
|
|
!seenKeys.insert(command.texture.resourceHandle).second) {
|
|
continue;
|
|
}
|
|
|
|
outTextures.push_back(command.texture);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace XCEngine::UI::Editor::Host::D3D12WindowInteropHelpers
|
|
|
|
namespace XCEngine::UI::Editor::Host {
|
|
|
|
using namespace D3D12WindowInteropHelpers;
|
|
|
|
bool D3D12WindowInteropContext::Attach(
|
|
D3D12WindowRenderer& windowRenderer,
|
|
ID2D1Factory1& d2dFactory) {
|
|
if (m_windowRenderer != &windowRenderer) {
|
|
Detach();
|
|
m_windowRenderer = &windowRenderer;
|
|
}
|
|
|
|
m_d2dFactory = &d2dFactory;
|
|
return EnsureInterop();
|
|
}
|
|
|
|
void D3D12WindowInteropContext::Detach() {
|
|
ReleaseInteropState();
|
|
m_windowRenderer = nullptr;
|
|
m_d2dFactory = nullptr;
|
|
m_lastError.clear();
|
|
}
|
|
|
|
D3D12WindowRenderer* D3D12WindowInteropContext::GetWindowRenderer() const {
|
|
return m_windowRenderer;
|
|
}
|
|
|
|
ID3D11On12Device* D3D12WindowInteropContext::GetD3D11On12Device() const {
|
|
return m_d3d11On12Device.Get();
|
|
}
|
|
|
|
ID3D11DeviceContext* D3D12WindowInteropContext::GetD3D11DeviceContext() const {
|
|
return m_d3d11DeviceContext.Get();
|
|
}
|
|
|
|
ID2D1DeviceContext* D3D12WindowInteropContext::GetD2DDeviceContext() const {
|
|
return m_d2dDeviceContext.Get();
|
|
}
|
|
|
|
const std::string& D3D12WindowInteropContext::GetLastError() const {
|
|
return m_lastError;
|
|
}
|
|
|
|
bool D3D12WindowInteropContext::EnsureInterop() {
|
|
if (m_windowRenderer == nullptr) {
|
|
m_lastError = "EnsureInterop requires an attached D3D12 window renderer.";
|
|
return false;
|
|
}
|
|
if (m_d2dFactory == nullptr) {
|
|
m_lastError = "EnsureInterop requires an initialized D2D factory.";
|
|
return false;
|
|
}
|
|
if (m_d3d11On12Device != nullptr &&
|
|
m_d2dDeviceContext != nullptr) {
|
|
return true;
|
|
}
|
|
|
|
ReleaseInteropState();
|
|
|
|
ID3D12Device* d3d12Device = m_windowRenderer->GetDevice();
|
|
ID3D12CommandQueue* d3d12CommandQueue = m_windowRenderer->GetCommandQueue();
|
|
if (d3d12Device == nullptr || d3d12CommandQueue == nullptr) {
|
|
m_lastError = "The attached D3D12 window renderer does not expose a native device/queue.";
|
|
return false;
|
|
}
|
|
|
|
const std::array<D3D_FEATURE_LEVEL, 4> featureLevels = {
|
|
D3D_FEATURE_LEVEL_12_1,
|
|
D3D_FEATURE_LEVEL_12_0,
|
|
D3D_FEATURE_LEVEL_11_1,
|
|
D3D_FEATURE_LEVEL_11_0
|
|
};
|
|
const std::array<IUnknown*, 1> commandQueues = {
|
|
reinterpret_cast<IUnknown*>(d3d12CommandQueue)
|
|
};
|
|
|
|
UINT createFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
|
|
#ifdef _DEBUG
|
|
createFlags |= D3D11_CREATE_DEVICE_DEBUG;
|
|
#endif
|
|
|
|
D3D_FEATURE_LEVEL actualFeatureLevel = D3D_FEATURE_LEVEL_11_0;
|
|
HRESULT hr = D3D11On12CreateDevice(
|
|
d3d12Device,
|
|
createFlags,
|
|
featureLevels.data(),
|
|
static_cast<UINT>(featureLevels.size()),
|
|
commandQueues.data(),
|
|
static_cast<UINT>(commandQueues.size()),
|
|
0u,
|
|
m_d3d11Device.ReleaseAndGetAddressOf(),
|
|
m_d3d11DeviceContext.ReleaseAndGetAddressOf(),
|
|
&actualFeatureLevel);
|
|
#ifdef _DEBUG
|
|
if (FAILED(hr)) {
|
|
createFlags &= ~D3D11_CREATE_DEVICE_DEBUG;
|
|
hr = D3D11On12CreateDevice(
|
|
d3d12Device,
|
|
createFlags,
|
|
featureLevels.data(),
|
|
static_cast<UINT>(featureLevels.size()),
|
|
commandQueues.data(),
|
|
static_cast<UINT>(commandQueues.size()),
|
|
0u,
|
|
m_d3d11Device.ReleaseAndGetAddressOf(),
|
|
m_d3d11DeviceContext.ReleaseAndGetAddressOf(),
|
|
&actualFeatureLevel);
|
|
}
|
|
#endif
|
|
if (FAILED(hr) || m_d3d11Device == nullptr || m_d3d11DeviceContext == nullptr) {
|
|
m_lastError = HrToInteropString("D3D11On12CreateDevice", hr);
|
|
ReleaseInteropState();
|
|
return false;
|
|
}
|
|
|
|
hr = m_d3d11Device.As(&m_d3d11On12Device);
|
|
if (FAILED(hr) || m_d3d11On12Device == nullptr) {
|
|
m_lastError = HrToInteropString("ID3D11Device::QueryInterface(ID3D11On12Device)", hr);
|
|
ReleaseInteropState();
|
|
return false;
|
|
}
|
|
|
|
Microsoft::WRL::ComPtr<IDXGIDevice> dxgiDevice = {};
|
|
hr = m_d3d11Device.As(&dxgiDevice);
|
|
if (FAILED(hr) || dxgiDevice == nullptr) {
|
|
m_lastError = HrToInteropString("ID3D11Device::QueryInterface(IDXGIDevice)", hr);
|
|
ReleaseInteropState();
|
|
return false;
|
|
}
|
|
|
|
hr = m_d2dFactory->CreateDevice(dxgiDevice.Get(), m_d2dDevice.ReleaseAndGetAddressOf());
|
|
if (FAILED(hr) || m_d2dDevice == nullptr) {
|
|
m_lastError = HrToInteropString("ID2D1Factory1::CreateDevice", hr);
|
|
ReleaseInteropState();
|
|
return false;
|
|
}
|
|
|
|
hr = m_d2dDevice->CreateDeviceContext(
|
|
D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
|
|
m_d2dDeviceContext.ReleaseAndGetAddressOf());
|
|
if (FAILED(hr) || m_d2dDeviceContext == nullptr) {
|
|
m_lastError = HrToInteropString("ID2D1Device::CreateDeviceContext", hr);
|
|
ReleaseInteropState();
|
|
return false;
|
|
}
|
|
|
|
m_d2dDeviceContext->SetDpi(96.0f, 96.0f);
|
|
m_d2dDeviceContext->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
void D3D12WindowInteropContext::ReleaseInteropState() {
|
|
ClearSourceTextures();
|
|
if (m_d2dDeviceContext != nullptr) {
|
|
m_d2dDeviceContext->SetTarget(nullptr);
|
|
}
|
|
if (m_d3d11DeviceContext != nullptr) {
|
|
m_d3d11DeviceContext->ClearState();
|
|
m_d3d11DeviceContext->Flush();
|
|
}
|
|
m_d2dDeviceContext.Reset();
|
|
m_d2dDevice.Reset();
|
|
m_d3d11On12Device.Reset();
|
|
m_d3d11DeviceContext.Reset();
|
|
m_d3d11Device.Reset();
|
|
}
|
|
|
|
bool D3D12WindowInteropContext::PrepareSourceTextures(
|
|
const ::XCEngine::UI::UIDrawData& drawData) {
|
|
ClearSourceTextures();
|
|
if (m_windowRenderer == nullptr || m_d3d11On12Device == nullptr || m_d2dDeviceContext == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
std::vector<::XCEngine::UI::UITextureHandle> textureHandles = {};
|
|
CollectInteropTextureHandles(drawData, textureHandles);
|
|
m_activeSourceTextures.reserve(textureHandles.size());
|
|
|
|
for (const ::XCEngine::UI::UITextureHandle& textureHandle : textureHandles) {
|
|
auto* texture =
|
|
reinterpret_cast<::XCEngine::RHI::RHITexture*>(textureHandle.resourceHandle);
|
|
auto* nativeTexture = dynamic_cast<::XCEngine::RHI::D3D12Texture*>(texture);
|
|
if (nativeTexture == nullptr || nativeTexture->GetResource() == nullptr) {
|
|
m_lastError = "Failed to resolve a D3D12 source texture for UI composition.";
|
|
ClearSourceTextures();
|
|
return false;
|
|
}
|
|
|
|
D3D11_RESOURCE_FLAGS resourceFlags = {};
|
|
resourceFlags.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
SourceTextureResource resource = {};
|
|
resource.key = textureHandle.resourceHandle;
|
|
HRESULT hr = m_d3d11On12Device->CreateWrappedResource(
|
|
nativeTexture->GetResource(),
|
|
&resourceFlags,
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
|
IID_PPV_ARGS(resource.wrappedResource.ReleaseAndGetAddressOf()));
|
|
if (FAILED(hr) || resource.wrappedResource == nullptr) {
|
|
m_lastError = HrToInteropString("ID3D11On12Device::CreateWrappedResource(source)", hr);
|
|
ClearSourceTextures();
|
|
return false;
|
|
}
|
|
|
|
Microsoft::WRL::ComPtr<IDXGISurface> dxgiSurface = {};
|
|
hr = resource.wrappedResource.As(&dxgiSurface);
|
|
if (FAILED(hr) || dxgiSurface == nullptr) {
|
|
m_lastError = HrToInteropString("ID3D11Resource::QueryInterface(IDXGISurface)", hr);
|
|
ClearSourceTextures();
|
|
return false;
|
|
}
|
|
|
|
const D2D1_BITMAP_PROPERTIES1 bitmapProperties =
|
|
BuildD2DBitmapProperties(
|
|
nativeTexture->GetDesc().Format,
|
|
D2D1_BITMAP_OPTIONS_NONE);
|
|
hr = m_d2dDeviceContext->CreateBitmapFromDxgiSurface(
|
|
dxgiSurface.Get(),
|
|
&bitmapProperties,
|
|
resource.bitmap.ReleaseAndGetAddressOf());
|
|
if (FAILED(hr) || resource.bitmap == nullptr) {
|
|
m_lastError =
|
|
HrToInteropString("ID2D1DeviceContext::CreateBitmapFromDxgiSurface(source)", hr);
|
|
ClearSourceTextures();
|
|
return false;
|
|
}
|
|
|
|
m_activeBitmaps.emplace(resource.key, resource.bitmap);
|
|
m_activeSourceTextures.push_back(std::move(resource));
|
|
}
|
|
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
void D3D12WindowInteropContext::ClearSourceTextures() {
|
|
m_activeBitmaps.clear();
|
|
m_activeSourceTextures.clear();
|
|
}
|
|
|
|
bool D3D12WindowInteropContext::ResolveInteropBitmap(
|
|
const ::XCEngine::UI::UITextureHandle& texture,
|
|
Microsoft::WRL::ComPtr<ID2D1Bitmap>& outBitmap) const {
|
|
outBitmap.Reset();
|
|
if (!IsInteropTextureHandle(texture)) {
|
|
return false;
|
|
}
|
|
|
|
const auto found = m_activeBitmaps.find(texture.resourceHandle);
|
|
if (found == m_activeBitmaps.end() || found->second == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
outBitmap = found->second;
|
|
return true;
|
|
}
|
|
|
|
} // namespace XCEngine::UI::Editor::Host
|