feat: add gpu object id scene picking

This commit is contained in:
2026-04-01 16:44:11 +08:00
parent 6e2711f9e8
commit 6927b4b380
10 changed files with 1030 additions and 3 deletions

View File

@@ -15,18 +15,23 @@
#include <XCEngine/Components/CameraComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/RHI/D3D12/D3D12CommandQueue.h>
#include <XCEngine/RHI/D3D12/D3D12Device.h>
#include <XCEngine/RHI/D3D12/D3D12Texture.h>
#include <XCEngine/RHI/RHIEnums.h>
#include <XCEngine/RHI/RHIResourceView.h>
#include <XCEngine/RHI/RHITexture.h>
#include <XCEngine/Rendering/ObjectIdEncoding.h>
#include <XCEngine/Rendering/RenderContext.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <XCEngine/Rendering/SceneRenderer.h>
#include <XCEngine/Scene/Scene.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <functional>
#include <memory>
#include <string>
@@ -74,6 +79,191 @@ inline void SetViewportStatusIfEmpty(std::string& statusText, const char* messag
}
}
inline uint32_t ClampViewportPixelCoordinate(float value, uint32_t extent) {
if (extent == 0) {
return 0;
}
const float maxCoordinate = static_cast<float>(extent - 1u);
const float clamped = (std::max)(0.0f, (std::min)(value, maxCoordinate));
return static_cast<uint32_t>(std::floor(clamped));
}
inline bool WaitForD3D12QueueIdle(
ID3D12Device* device,
ID3D12CommandQueue* commandQueue) {
if (device == nullptr || commandQueue == nullptr) {
return false;
}
ID3D12Fence* fence = nullptr;
if (FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)))) {
return false;
}
HANDLE fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (fenceEvent == nullptr) {
fence->Release();
return false;
}
constexpr UINT64 kFenceValue = 1;
const HRESULT signalResult = commandQueue->Signal(fence, kFenceValue);
if (FAILED(signalResult)) {
CloseHandle(fenceEvent);
fence->Release();
return false;
}
if (fence->GetCompletedValue() < kFenceValue) {
if (FAILED(fence->SetEventOnCompletion(kFenceValue, fenceEvent))) {
CloseHandle(fenceEvent);
fence->Release();
return false;
}
WaitForSingleObject(fenceEvent, INFINITE);
}
CloseHandle(fenceEvent);
fence->Release();
return true;
}
inline bool ReadBackD3D12TexturePixel(
RHI::D3D12Device& device,
RHI::D3D12CommandQueue& commandQueue,
RHI::D3D12Texture& texture,
RHI::ResourceStates sourceState,
uint32_t pixelX,
uint32_t pixelY,
std::array<uint8_t, 4>& outRgba) {
ID3D12Device* nativeDevice = device.GetDevice();
ID3D12CommandQueue* nativeCommandQueue = commandQueue.GetCommandQueue();
ID3D12Resource* sourceResource = texture.GetResource();
if (nativeDevice == nullptr ||
nativeCommandQueue == nullptr ||
sourceResource == nullptr ||
pixelX >= texture.GetWidth() ||
pixelY >= texture.GetHeight()) {
return false;
}
ID3D12CommandAllocator* commandAllocator = nullptr;
if (FAILED(nativeDevice->CreateCommandAllocator(
D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&commandAllocator)))) {
return false;
}
ID3D12GraphicsCommandList* commandList = nullptr;
if (FAILED(nativeDevice->CreateCommandList(
0,
D3D12_COMMAND_LIST_TYPE_DIRECT,
commandAllocator,
nullptr,
IID_PPV_ARGS(&commandList)))) {
commandAllocator->Release();
return false;
}
D3D12_HEAP_PROPERTIES heapProperties = {};
heapProperties.Type = D3D12_HEAP_TYPE_READBACK;
constexpr UINT kRowPitch = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT;
D3D12_RESOURCE_DESC readbackDesc = {};
readbackDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
readbackDesc.Width = kRowPitch;
readbackDesc.Height = 1;
readbackDesc.DepthOrArraySize = 1;
readbackDesc.MipLevels = 1;
readbackDesc.SampleDesc.Count = 1;
readbackDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ID3D12Resource* readbackBuffer = nullptr;
if (FAILED(nativeDevice->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&readbackDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(&readbackBuffer)))) {
commandList->Release();
commandAllocator->Release();
return false;
}
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = sourceResource;
barrier.Transition.StateBefore = RHI::ToD3D12(sourceState);
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
commandList->ResourceBarrier(1, &barrier);
D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
srcLocation.pResource = sourceResource;
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
srcLocation.SubresourceIndex = 0;
D3D12_RESOURCE_DESC sourceDesc = sourceResource->GetDesc();
D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
dstLocation.pResource = readbackBuffer;
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
dstLocation.PlacedFootprint.Offset = 0;
dstLocation.PlacedFootprint.Footprint.Format = sourceDesc.Format;
dstLocation.PlacedFootprint.Footprint.Width = 1;
dstLocation.PlacedFootprint.Footprint.Height = 1;
dstLocation.PlacedFootprint.Footprint.Depth = 1;
dstLocation.PlacedFootprint.Footprint.RowPitch = kRowPitch;
D3D12_BOX sourceBox = {};
sourceBox.left = pixelX;
sourceBox.top = pixelY;
sourceBox.front = 0;
sourceBox.right = pixelX + 1u;
sourceBox.bottom = pixelY + 1u;
sourceBox.back = 1;
commandList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, &sourceBox);
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
barrier.Transition.StateAfter = RHI::ToD3D12(sourceState);
commandList->ResourceBarrier(1, &barrier);
if (FAILED(commandList->Close())) {
readbackBuffer->Release();
commandList->Release();
commandAllocator->Release();
return false;
}
ID3D12CommandList* commandLists[] = { commandList };
nativeCommandQueue->ExecuteCommandLists(1, commandLists);
if (!WaitForD3D12QueueIdle(nativeDevice, nativeCommandQueue)) {
readbackBuffer->Release();
commandList->Release();
commandAllocator->Release();
return false;
}
void* mappedData = nullptr;
D3D12_RANGE readRange = { 0, 4 };
if (FAILED(readbackBuffer->Map(0, &readRange, &mappedData))) {
readbackBuffer->Release();
commandList->Release();
commandAllocator->Release();
return false;
}
std::memcpy(outRgba.data(), mappedData, outRgba.size());
D3D12_RANGE writeRange = { 0, 0 };
readbackBuffer->Unmap(0, &writeRange);
readbackBuffer->Release();
commandList->Release();
commandAllocator->Release();
return true;
}
Math::Vector3 GetSceneViewportOrientationAxisVector(SceneViewportOrientationAxis axis) {
switch (axis) {
case SceneViewportOrientationAxis::PositiveX:
@@ -110,6 +300,7 @@ public:
}
m_sceneViewCamera = {};
m_sceneViewLastRenderContext = {};
m_device = nullptr;
m_backend = nullptr;
m_sceneGridPass.Shutdown();
@@ -201,6 +392,16 @@ public:
return 0;
}
ViewportEntry& entry = GetEntry(EditorViewportKind::Scene);
uint64_t objectIdEntity = 0;
if (TryPickSceneViewEntityWithObjectId(
entry,
viewportSize,
viewportMousePosition,
objectIdEntity)) {
return objectIdEntity;
}
SceneViewportPickRequest request = {};
request.scene = scene;
request.overlay = GetSceneViewOverlayData();
@@ -255,6 +456,7 @@ public:
}
EnsureSceneRenderer();
m_sceneViewLastRenderContext = renderContext;
const auto* scene = context.GetSceneManager().GetScene();
for (ViewportEntry& entry : m_entries) {
@@ -286,11 +488,15 @@ private:
RHI::RHITexture* selectionMaskTexture = nullptr;
RHI::RHIResourceView* selectionMaskView = nullptr;
RHI::RHIResourceView* selectionMaskShaderView = nullptr;
RHI::RHITexture* objectIdTexture = nullptr;
RHI::RHIResourceView* objectIdView = nullptr;
D3D12_CPU_DESCRIPTOR_HANDLE imguiCpuHandle = {};
D3D12_GPU_DESCRIPTOR_HANDLE imguiGpuHandle = {};
ImTextureID textureId = {};
RHI::ResourceStates colorState = RHI::ResourceStates::Common;
RHI::ResourceStates selectionMaskState = RHI::ResourceStates::Common;
RHI::ResourceStates objectIdState = RHI::ResourceStates::Common;
bool hasValidObjectIdFrame = false;
std::string statusText;
};
@@ -458,6 +664,22 @@ private:
return entry.selectionMaskShaderView != nullptr;
}
bool CreateSceneViewportObjectIdResources(ViewportEntry& entry) {
const RHI::TextureDesc objectIdDesc =
BuildViewportTextureDesc(entry.width, entry.height, RHI::Format::R8G8B8A8_UNorm);
entry.objectIdTexture = m_device->CreateTexture(objectIdDesc);
if (entry.objectIdTexture == nullptr) {
return false;
}
const RHI::ResourceViewDesc objectIdViewDesc =
BuildViewportTextureViewDesc(RHI::Format::R8G8B8A8_UNorm);
entry.objectIdView = m_device->CreateRenderTargetView(
entry.objectIdTexture,
objectIdViewDesc);
return entry.objectIdView != nullptr;
}
bool CreateViewportTextureDescriptor(ViewportEntry& entry) {
m_backend->AllocateTextureDescriptor(&entry.imguiCpuHandle, &entry.imguiGpuHandle);
if (entry.imguiCpuHandle.ptr == 0 || entry.imguiGpuHandle.ptr == 0) {
@@ -493,7 +715,9 @@ private:
(entry.kind != EditorViewportKind::Scene ||
(entry.selectionMaskTexture != nullptr &&
entry.selectionMaskView != nullptr &&
entry.selectionMaskShaderView != nullptr)) &&
entry.selectionMaskShaderView != nullptr &&
entry.objectIdTexture != nullptr &&
entry.objectIdView != nullptr)) &&
entry.textureId != ImTextureID{}) {
return true;
}
@@ -519,12 +743,20 @@ private:
return false;
}
if (entry.kind == EditorViewportKind::Scene &&
!CreateSceneViewportObjectIdResources(entry)) {
DestroyViewportResources(entry);
return false;
}
if (!CreateViewportTextureDescriptor(entry)) {
DestroyViewportResources(entry);
return false;
}
entry.colorState = RHI::ResourceStates::Common;
entry.selectionMaskState = RHI::ResourceStates::Common;
entry.objectIdState = RHI::ResourceStates::Common;
entry.hasValidObjectIdFrame = false;
return true;
}
@@ -546,6 +778,15 @@ private:
return surface;
}
Rendering::RenderSurface BuildObjectIdSurface(const ViewportEntry& entry) const {
Rendering::RenderSurface surface(entry.width, entry.height);
surface.SetColorAttachment(entry.objectIdView);
surface.SetDepthAttachment(entry.depthView);
surface.SetColorStateBefore(entry.objectIdState);
surface.SetColorStateAfter(RHI::ResourceStates::PixelShaderResource);
return surface;
}
void AddSceneSelectionMaskPass(
ViewportEntry& entry,
const Rendering::RenderSurface& selectionMaskSurface,
@@ -802,6 +1043,7 @@ private:
if (!EnsureSceneViewCamera()) {
entry.statusText = "Scene view camera is unavailable";
ClearViewport(entry, renderContext, 0.18f, 0.07f, 0.07f, 1.0f);
entry.hasValidObjectIdFrame = false;
return false;
}
@@ -810,6 +1052,7 @@ private:
if (scene == nullptr) {
entry.statusText = "No active scene";
ClearViewport(entry, renderContext, 0.07f, 0.08f, 0.10f, 1.0f);
entry.hasValidObjectIdFrame = false;
return false;
}
@@ -821,6 +1064,7 @@ private:
if (requests.empty()) {
SetViewportStatusIfEmpty(entry.statusText, "Scene renderer failed");
ClearViewport(entry, renderContext, 0.18f, 0.07f, 0.07f, 1.0f);
entry.hasValidObjectIdFrame = false;
return false;
}
@@ -828,13 +1072,21 @@ private:
requests[0].postScenePasses = &sceneState.postPasses;
}
if (entry.objectIdView != nullptr) {
requests[0].objectId.surface = BuildObjectIdSurface(entry);
requests[0].objectId.surface.SetRenderArea(requests[0].surface.GetRenderArea());
}
if (!m_sceneRenderer->Render(requests)) {
SetViewportStatusIfEmpty(entry.statusText, "Scene renderer failed");
ClearViewport(entry, renderContext, 0.18f, 0.07f, 0.07f, 1.0f);
entry.hasValidObjectIdFrame = false;
return false;
}
entry.colorState = RHI::ResourceStates::PixelShaderResource;
entry.objectIdState = RHI::ResourceStates::PixelShaderResource;
entry.hasValidObjectIdFrame = requests[0].objectId.IsRequested();
return true;
}
@@ -859,6 +1111,7 @@ private:
if (!m_sceneRenderer->Render(*scene, nullptr, renderContext, surface)) {
entry.statusText = "Scene renderer failed";
ClearViewport(entry, renderContext, 0.18f, 0.07f, 0.07f, 1.0f);
entry.hasValidObjectIdFrame = false;
return false;
}
@@ -914,6 +1167,56 @@ private:
RHI::ResourceStates::RenderTarget,
RHI::ResourceStates::PixelShaderResource);
entry.colorState = RHI::ResourceStates::PixelShaderResource;
entry.hasValidObjectIdFrame = false;
}
bool TryPickSceneViewEntityWithObjectId(
ViewportEntry& entry,
const ImVec2& viewportSize,
const ImVec2& viewportMousePosition,
uint64_t& outEntityId) {
outEntityId = 0;
if (m_device == nullptr ||
m_sceneViewLastRenderContext.commandQueue == nullptr ||
entry.objectIdTexture == nullptr ||
!entry.hasValidObjectIdFrame ||
viewportSize.x <= 1.0f ||
viewportSize.y <= 1.0f ||
viewportMousePosition.x < 0.0f ||
viewportMousePosition.y < 0.0f ||
viewportMousePosition.x > viewportSize.x ||
viewportMousePosition.y > viewportSize.y) {
return false;
}
auto* commandQueue =
static_cast<RHI::D3D12CommandQueue*>(m_sceneViewLastRenderContext.commandQueue);
auto* objectIdTexture = static_cast<RHI::D3D12Texture*>(entry.objectIdTexture);
if (commandQueue == nullptr || objectIdTexture == nullptr) {
return false;
}
const uint32_t pixelX = ClampViewportPixelCoordinate(viewportMousePosition.x, entry.width);
const uint32_t pixelY = ClampViewportPixelCoordinate(viewportMousePosition.y, entry.height);
std::array<uint8_t, 4> rgba = {};
if (!ReadBackD3D12TexturePixel(
*m_device,
*commandQueue,
*objectIdTexture,
entry.objectIdState,
pixelX,
pixelY,
rgba)) {
return false;
}
outEntityId = static_cast<uint64_t>(Rendering::DecodeObjectIdFromColor(
rgba[0],
rgba[1],
rgba[2],
rgba[3]));
return true;
}
void DestroyViewportResources(ViewportEntry& entry) {
@@ -939,6 +1242,18 @@ private:
entry.selectionMaskTexture = nullptr;
}
if (entry.objectIdView != nullptr) {
entry.objectIdView->Shutdown();
delete entry.objectIdView;
entry.objectIdView = nullptr;
}
if (entry.objectIdTexture != nullptr) {
entry.objectIdTexture->Shutdown();
delete entry.objectIdTexture;
entry.objectIdTexture = nullptr;
}
if (entry.depthView != nullptr) {
entry.depthView->Shutdown();
delete entry.depthView;
@@ -970,11 +1285,14 @@ private:
entry.textureId = {};
entry.colorState = RHI::ResourceStates::Common;
entry.selectionMaskState = RHI::ResourceStates::Common;
entry.objectIdState = RHI::ResourceStates::Common;
entry.hasValidObjectIdFrame = false;
}
UI::ImGuiBackendBridge* m_backend = nullptr;
RHI::D3D12Device* m_device = nullptr;
std::unique_ptr<Rendering::SceneRenderer> m_sceneRenderer;
Rendering::RenderContext m_sceneViewLastRenderContext = {};
std::array<ViewportEntry, 2> m_entries = {};
SceneViewCameraState m_sceneViewCamera;
SceneViewportInfiniteGridPass m_sceneGridPass;