refactor: drive scene view outline from object id
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#include "SceneViewportSelectionOutlinePass.h"
|
||||
|
||||
#include <XCEngine/Rendering/ObjectIdEncoding.h>
|
||||
#include <XCEngine/RHI/RHICommandList.h>
|
||||
#include <XCEngine/RHI/RHIDevice.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace XCEngine {
|
||||
@@ -16,10 +18,11 @@ const char kSelectionOutlineCompositeHlsl[] = R"(
|
||||
cbuffer OutlineConstants : register(b0) {
|
||||
float4 gViewportSizeAndTexelSize;
|
||||
float4 gOutlineColorAndWidth;
|
||||
float4 gSelectedInfo;
|
||||
float4 gSelectedObjectColors[256];
|
||||
};
|
||||
|
||||
Texture2D gSelectionMask : register(t0);
|
||||
SamplerState gMaskSampler : register(s0);
|
||||
Texture2D gObjectIdTexture : register(t0);
|
||||
|
||||
struct VSOutput {
|
||||
float4 position : SV_POSITION;
|
||||
@@ -37,25 +40,56 @@ VSOutput MainVS(uint vertexId : SV_VertexID) {
|
||||
return output;
|
||||
}
|
||||
|
||||
float SampleMask(float2 uv) {
|
||||
return gSelectionMask.SampleLevel(gMaskSampler, uv, 0).r;
|
||||
int2 ClampPixelCoord(int2 pixelCoord) {
|
||||
const int2 maxCoord = int2(
|
||||
max((int)gViewportSizeAndTexelSize.x - 1, 0),
|
||||
max((int)gViewportSizeAndTexelSize.y - 1, 0));
|
||||
return clamp(pixelCoord, int2(0, 0), maxCoord);
|
||||
}
|
||||
|
||||
float4 LoadObjectId(int2 pixelCoord) {
|
||||
return gObjectIdTexture.Load(int3(ClampPixelCoord(pixelCoord), 0));
|
||||
}
|
||||
|
||||
bool IsSelectedObject(float4 objectIdColor) {
|
||||
if (objectIdColor.a <= 0.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int selectedCount = min((int)gSelectedInfo.x, 256);
|
||||
[loop]
|
||||
for (int i = 0; i < selectedCount; ++i) {
|
||||
const float4 selectedColor = gSelectedObjectColors[i];
|
||||
if (all(abs(objectIdColor - selectedColor) <= float4(
|
||||
0.0025,
|
||||
0.0025,
|
||||
0.0025,
|
||||
0.0025))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
float4 MainPS(VSOutput input) : SV_TARGET {
|
||||
const float2 viewportSize = max(gViewportSizeAndTexelSize.xy, float2(1.0, 1.0));
|
||||
const float2 texelSize = max(gViewportSizeAndTexelSize.zw, float2(1e-6, 1e-6));
|
||||
const float outlineWidth = max(gOutlineColorAndWidth.w, 1.0);
|
||||
const int2 pixelCoord = int2(input.position.xy);
|
||||
const bool debugSelectionMask = gSelectedInfo.y > 0.5;
|
||||
const bool centerSelected = IsSelectedObject(LoadObjectId(pixelCoord));
|
||||
|
||||
const float2 uv = input.position.xy / viewportSize;
|
||||
const float centerMask = SampleMask(uv);
|
||||
if (centerMask > 0.001) {
|
||||
if (debugSelectionMask) {
|
||||
return centerSelected ? float4(1.0, 1.0, 1.0, 1.0) : float4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
if (centerSelected) {
|
||||
discard;
|
||||
}
|
||||
|
||||
const int outlineWidth = max((int)gOutlineColorAndWidth.w, 1);
|
||||
float outline = 0.0;
|
||||
[unroll]
|
||||
[loop]
|
||||
for (int y = -2; y <= 2; ++y) {
|
||||
[unroll]
|
||||
[loop]
|
||||
for (int x = -2; x <= 2; ++x) {
|
||||
if (x == 0 && y == 0) {
|
||||
continue;
|
||||
@@ -66,9 +100,12 @@ float4 MainPS(VSOutput input) : SV_TARGET {
|
||||
continue;
|
||||
}
|
||||
|
||||
const float sampleMask = SampleMask(uv + float2((float)x, (float)y) * texelSize);
|
||||
const float weight = saturate(1.0 - ((distancePixels - 1.0) / max(outlineWidth, 1.0)));
|
||||
outline = max(outline, sampleMask * weight);
|
||||
if (!IsSelectedObject(LoadObjectId(pixelCoord + int2(x, y)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const float weight = saturate(1.0 - ((distancePixels - 1.0) / max((float)outlineWidth, 1.0)));
|
||||
outline = max(outline, weight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +113,7 @@ float4 MainPS(VSOutput input) : SV_TARGET {
|
||||
discard;
|
||||
}
|
||||
|
||||
return float4(gOutlineColorAndWidth.rgb, gOutlineColorAndWidth.a * outline);
|
||||
return float4(gOutlineColorAndWidth.rgb, outline);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -89,10 +126,13 @@ void SceneViewportSelectionOutlinePass::Shutdown() {
|
||||
bool SceneViewportSelectionOutlinePass::Render(
|
||||
const Rendering::RenderContext& renderContext,
|
||||
const Rendering::RenderSurface& surface,
|
||||
RHI::RHIResourceView* maskTextureView) {
|
||||
RHI::RHIResourceView* objectIdTextureView,
|
||||
const std::vector<uint64_t>& selectedObjectIds,
|
||||
bool debugSelectionMask) {
|
||||
if (!renderContext.IsValid() ||
|
||||
renderContext.backendType != RHI::RHIType::D3D12 ||
|
||||
maskTextureView == nullptr) {
|
||||
objectIdTextureView == nullptr ||
|
||||
selectedObjectIds.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -112,8 +152,22 @@ bool SceneViewportSelectionOutlinePass::Render(
|
||||
surface.GetWidth() > 0 ? 1.0f / static_cast<float>(surface.GetWidth()) : 0.0f,
|
||||
surface.GetHeight() > 0 ? 1.0f / static_cast<float>(surface.GetHeight()) : 0.0f);
|
||||
constants.outlineColorAndWidth = Math::Vector4(1.0f, 0.4f, 0.0f, kOutlineWidthPixels);
|
||||
|
||||
const uint32_t selectedCount = (std::min)(
|
||||
static_cast<uint32_t>(selectedObjectIds.size()),
|
||||
kMaxSelectedObjectCount);
|
||||
constants.selectedInfo = Math::Vector4(
|
||||
static_cast<float>(selectedCount),
|
||||
debugSelectionMask ? 1.0f : 0.0f,
|
||||
0.0f,
|
||||
0.0f);
|
||||
for (uint32_t index = 0; index < selectedCount; ++index) {
|
||||
constants.selectedObjectColors[index] =
|
||||
Rendering::EncodeObjectIdToColor(selectedObjectIds[index]);
|
||||
}
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
m_textureSet->Update(0, maskTextureView);
|
||||
m_textureSet->Update(0, objectIdTextureView);
|
||||
|
||||
RHI::RHICommandList* commandList = renderContext.commandList;
|
||||
RHI::RHIResourceView* renderTarget = colorAttachments[0];
|
||||
@@ -139,8 +193,8 @@ bool SceneViewportSelectionOutlinePass::Render(
|
||||
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
|
||||
commandList->SetPipelineState(m_pipelineState);
|
||||
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(0, 3, descriptorSets, m_pipelineLayout);
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet };
|
||||
commandList->SetGraphicsDescriptorSets(0, 2, descriptorSets, m_pipelineLayout);
|
||||
commandList->Draw(3, 1, 0, 0);
|
||||
return true;
|
||||
}
|
||||
@@ -152,9 +206,6 @@ bool SceneViewportSelectionOutlinePass::EnsureInitialized(const Rendering::Rende
|
||||
m_constantSet != nullptr &&
|
||||
m_texturePool != nullptr &&
|
||||
m_textureSet != nullptr &&
|
||||
m_samplerPool != nullptr &&
|
||||
m_samplerSet != nullptr &&
|
||||
m_sampler != nullptr &&
|
||||
m_device == renderContext.device &&
|
||||
m_backendType == renderContext.backendType) {
|
||||
return true;
|
||||
@@ -172,16 +223,13 @@ bool SceneViewportSelectionOutlinePass::CreateResources(const Rendering::RenderC
|
||||
m_device = renderContext.device;
|
||||
m_backendType = renderContext.backendType;
|
||||
|
||||
RHI::DescriptorSetLayoutBinding setBindings[3] = {};
|
||||
RHI::DescriptorSetLayoutBinding setBindings[2] = {};
|
||||
setBindings[0].binding = 0;
|
||||
setBindings[0].type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
|
||||
setBindings[0].count = 1;
|
||||
setBindings[1].binding = 0;
|
||||
setBindings[1].type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
setBindings[1].count = 1;
|
||||
setBindings[2].binding = 0;
|
||||
setBindings[2].type = static_cast<uint32_t>(RHI::DescriptorType::Sampler);
|
||||
setBindings[2].count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc constantLayout = {};
|
||||
constantLayout.bindings = &setBindings[0];
|
||||
@@ -191,18 +239,13 @@ bool SceneViewportSelectionOutlinePass::CreateResources(const Rendering::RenderC
|
||||
textureLayout.bindings = &setBindings[1];
|
||||
textureLayout.bindingCount = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc samplerLayout = {};
|
||||
samplerLayout.bindings = &setBindings[2];
|
||||
samplerLayout.bindingCount = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc setLayouts[3] = {};
|
||||
RHI::DescriptorSetLayoutDesc setLayouts[2] = {};
|
||||
setLayouts[0] = constantLayout;
|
||||
setLayouts[1] = textureLayout;
|
||||
setLayouts[2] = samplerLayout;
|
||||
|
||||
RHI::RHIPipelineLayoutDesc pipelineLayoutDesc = {};
|
||||
pipelineLayoutDesc.setLayouts = setLayouts;
|
||||
pipelineLayoutDesc.setLayoutCount = 3;
|
||||
pipelineLayoutDesc.setLayoutCount = 2;
|
||||
m_pipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc);
|
||||
if (m_pipelineLayout == nullptr) {
|
||||
DestroyResources();
|
||||
@@ -241,39 +284,6 @@ bool SceneViewportSelectionOutlinePass::CreateResources(const Rendering::RenderC
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc samplerPoolDesc = {};
|
||||
samplerPoolDesc.type = RHI::DescriptorHeapType::Sampler;
|
||||
samplerPoolDesc.descriptorCount = 1;
|
||||
samplerPoolDesc.shaderVisible = true;
|
||||
m_samplerPool = m_device->CreateDescriptorPool(samplerPoolDesc);
|
||||
if (m_samplerPool == nullptr) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
m_samplerSet = m_samplerPool->AllocateSet(samplerLayout);
|
||||
if (m_samplerSet == nullptr) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::SamplerDesc samplerDesc = {};
|
||||
samplerDesc.filter = static_cast<uint32_t>(RHI::FilterMode::Linear);
|
||||
samplerDesc.addressU = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
|
||||
samplerDesc.addressV = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
|
||||
samplerDesc.addressW = static_cast<uint32_t>(RHI::TextureAddressMode::Clamp);
|
||||
samplerDesc.mipLodBias = 0.0f;
|
||||
samplerDesc.maxAnisotropy = 1;
|
||||
samplerDesc.comparisonFunc = static_cast<uint32_t>(RHI::ComparisonFunc::Always);
|
||||
samplerDesc.minLod = 0.0f;
|
||||
samplerDesc.maxLod = 1.0f;
|
||||
m_sampler = m_device->CreateSampler(samplerDesc);
|
||||
if (m_sampler == nullptr) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
m_samplerSet->UpdateSampler(0, m_sampler);
|
||||
|
||||
RHI::GraphicsPipelineDesc pipelineDesc = {};
|
||||
pipelineDesc.pipelineLayout = m_pipelineLayout;
|
||||
pipelineDesc.topologyType = static_cast<uint32_t>(RHI::PrimitiveTopologyType::Triangle);
|
||||
@@ -330,24 +340,6 @@ void SceneViewportSelectionOutlinePass::DestroyResources() {
|
||||
m_pipelineState = nullptr;
|
||||
}
|
||||
|
||||
if (m_sampler != nullptr) {
|
||||
m_sampler->Shutdown();
|
||||
delete m_sampler;
|
||||
m_sampler = nullptr;
|
||||
}
|
||||
|
||||
if (m_samplerSet != nullptr) {
|
||||
m_samplerSet->Shutdown();
|
||||
delete m_samplerSet;
|
||||
m_samplerSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_samplerPool != nullptr) {
|
||||
m_samplerPool->Shutdown();
|
||||
delete m_samplerPool;
|
||||
m_samplerPool = nullptr;
|
||||
}
|
||||
|
||||
if (m_textureSet != nullptr) {
|
||||
m_textureSet->Shutdown();
|
||||
delete m_textureSet;
|
||||
|
||||
Reference in New Issue
Block a user