2026-04-15 08:24:06 +08:00
|
|
|
#include "ViewportRenderTargets.h"
|
2026-04-13 19:57:25 +08:00
|
|
|
|
|
|
|
|
namespace XCEngine::UI::Editor::App {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
bool CreateViewportColorResources(
|
|
|
|
|
::XCEngine::RHI::RHIDevice& device,
|
2026-04-15 08:24:06 +08:00
|
|
|
ViewportRenderTargets& targets) {
|
2026-04-13 19:57:25 +08:00
|
|
|
const auto colorDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureDesc(targets.width, targets.height, ::XCEngine::RHI::Format::R8G8B8A8_UNorm);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.colorTexture = device.CreateTexture(colorDesc);
|
|
|
|
|
if (targets.colorTexture == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto colorViewDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureViewDesc(::XCEngine::RHI::Format::R8G8B8A8_UNorm);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.colorView = device.CreateRenderTargetView(targets.colorTexture, colorViewDesc);
|
|
|
|
|
return targets.colorView != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateViewportDepthResources(
|
|
|
|
|
::XCEngine::RHI::RHIDevice& device,
|
2026-04-15 08:24:06 +08:00
|
|
|
ViewportRenderTargets& targets) {
|
2026-04-13 19:57:25 +08:00
|
|
|
const auto depthDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureDesc(targets.width, targets.height, ::XCEngine::RHI::Format::D24_UNorm_S8_UInt);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.depthTexture = device.CreateTexture(depthDesc);
|
|
|
|
|
if (targets.depthTexture == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto depthViewDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureViewDesc(::XCEngine::RHI::Format::D24_UNorm_S8_UInt);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.depthView = device.CreateDepthStencilView(targets.depthTexture, depthViewDesc);
|
|
|
|
|
if (targets.depthView == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::XCEngine::RHI::ResourceViewDesc depthShaderViewDesc = {};
|
|
|
|
|
depthShaderViewDesc.dimension = ::XCEngine::RHI::ResourceViewDimension::Texture2D;
|
|
|
|
|
depthShaderViewDesc.mipLevel = 0;
|
|
|
|
|
targets.depthShaderView = device.CreateShaderResourceView(
|
|
|
|
|
targets.depthTexture,
|
|
|
|
|
depthShaderViewDesc);
|
|
|
|
|
return targets.depthShaderView != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateViewportObjectIdResources(
|
|
|
|
|
::XCEngine::RHI::RHIDevice& device,
|
2026-04-15 08:24:06 +08:00
|
|
|
ViewportRenderTargets& targets) {
|
2026-04-13 19:57:25 +08:00
|
|
|
const auto objectIdDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureDesc(targets.width, targets.height, ::XCEngine::RHI::Format::R8G8B8A8_UNorm);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.objectIdTexture = device.CreateTexture(objectIdDesc);
|
|
|
|
|
if (targets.objectIdTexture == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto objectIdViewDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureViewDesc(::XCEngine::RHI::Format::R8G8B8A8_UNorm);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.objectIdView = device.CreateRenderTargetView(targets.objectIdTexture, objectIdViewDesc);
|
|
|
|
|
if (targets.objectIdView == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targets.objectIdShaderView = device.CreateShaderResourceView(
|
|
|
|
|
targets.objectIdTexture,
|
|
|
|
|
objectIdViewDesc);
|
|
|
|
|
if (targets.objectIdShaderView == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto objectIdDepthDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureDesc(targets.width, targets.height, ::XCEngine::RHI::Format::D24_UNorm_S8_UInt);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.objectIdDepthTexture = device.CreateTexture(objectIdDepthDesc);
|
|
|
|
|
if (targets.objectIdDepthTexture == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto objectIdDepthViewDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureViewDesc(::XCEngine::RHI::Format::D24_UNorm_S8_UInt);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.objectIdDepthView = device.CreateDepthStencilView(
|
|
|
|
|
targets.objectIdDepthTexture,
|
|
|
|
|
objectIdDepthViewDesc);
|
|
|
|
|
return targets.objectIdDepthView != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateViewportSelectionMaskResources(
|
|
|
|
|
::XCEngine::RHI::RHIDevice& device,
|
2026-04-15 08:24:06 +08:00
|
|
|
ViewportRenderTargets& targets) {
|
2026-04-13 19:57:25 +08:00
|
|
|
const auto selectionMaskDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureDesc(targets.width, targets.height, ::XCEngine::RHI::Format::R8G8B8A8_UNorm);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.selectionMaskTexture = device.CreateTexture(selectionMaskDesc);
|
|
|
|
|
if (targets.selectionMaskTexture == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto selectionMaskViewDesc =
|
2026-04-15 08:24:06 +08:00
|
|
|
BuildViewportTextureViewDesc(::XCEngine::RHI::Format::R8G8B8A8_UNorm);
|
2026-04-13 19:57:25 +08:00
|
|
|
targets.selectionMaskView = device.CreateRenderTargetView(
|
|
|
|
|
targets.selectionMaskTexture,
|
|
|
|
|
selectionMaskViewDesc);
|
|
|
|
|
if (targets.selectionMaskView == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targets.selectionMaskShaderView = device.CreateShaderResourceView(
|
|
|
|
|
targets.selectionMaskTexture,
|
|
|
|
|
selectionMaskViewDesc);
|
|
|
|
|
return targets.selectionMaskShaderView != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateViewportTextureDescriptor(
|
|
|
|
|
Host::D3D12ShaderResourceDescriptorAllocator& textureDescriptorAllocator,
|
2026-04-15 08:24:06 +08:00
|
|
|
ViewportRenderTargets& targets) {
|
2026-04-13 19:57:25 +08:00
|
|
|
if (!textureDescriptorAllocator.CreateTextureDescriptor(
|
|
|
|
|
targets.colorTexture,
|
|
|
|
|
&targets.srvCpuHandle,
|
|
|
|
|
&targets.srvGpuHandle)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targets.textureHandle.nativeHandle = static_cast<std::uintptr_t>(targets.srvGpuHandle.ptr);
|
|
|
|
|
targets.textureHandle.width = targets.width;
|
|
|
|
|
targets.textureHandle.height = targets.height;
|
|
|
|
|
targets.textureHandle.kind = ::XCEngine::UI::UITextureHandleKind::ShaderResourceView;
|
|
|
|
|
targets.textureHandle.resourceHandle =
|
|
|
|
|
reinterpret_cast<std::uintptr_t>(targets.colorTexture);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2026-04-15 08:24:06 +08:00
|
|
|
bool ViewportRenderTargetManager::EnsureTargets(
|
|
|
|
|
ViewportKind kind,
|
2026-04-13 19:57:25 +08:00
|
|
|
std::uint32_t width,
|
|
|
|
|
std::uint32_t height,
|
|
|
|
|
::XCEngine::RHI::RHIDevice& device,
|
|
|
|
|
Host::D3D12ShaderResourceDescriptorAllocator& textureDescriptorAllocator,
|
2026-04-15 08:24:06 +08:00
|
|
|
ViewportRenderTargets& targets) const {
|
2026-04-13 19:57:25 +08:00
|
|
|
if (width == 0u || height == 0u) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DestroyTargets(&textureDescriptorAllocator, targets);
|
|
|
|
|
targets.width = width;
|
|
|
|
|
targets.height = height;
|
|
|
|
|
|
|
|
|
|
if (!CreateViewportColorResources(device, targets) ||
|
|
|
|
|
!CreateViewportDepthResources(device, targets) ||
|
2026-04-15 08:24:06 +08:00
|
|
|
(ViewportRequiresObjectIdResources(kind) &&
|
2026-04-13 19:57:25 +08:00
|
|
|
(!CreateViewportObjectIdResources(device, targets) ||
|
|
|
|
|
!CreateViewportSelectionMaskResources(device, targets))) ||
|
|
|
|
|
!CreateViewportTextureDescriptor(textureDescriptorAllocator, targets)) {
|
|
|
|
|
DestroyTargets(&textureDescriptorAllocator, targets);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targets.colorState = ::XCEngine::RHI::ResourceStates::Common;
|
|
|
|
|
targets.objectIdState = ::XCEngine::RHI::ResourceStates::Common;
|
|
|
|
|
targets.selectionMaskState = ::XCEngine::RHI::ResourceStates::Common;
|
|
|
|
|
targets.hasValidObjectIdFrame = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace XCEngine::UI::Editor::App
|