Formalize render object id contract
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
#include "Core/Asset/ResourceManager.h"
|
||||
#include "Debug/Logger.h"
|
||||
#include "Rendering/Picking/ObjectIdCodec.h"
|
||||
#include "Rendering/Detail/ShaderVariantUtils.h"
|
||||
#include "Rendering/Internal/RenderSurfacePipelineUtils.h"
|
||||
#include "Rendering/Internal/ShaderVariantUtils.h"
|
||||
#include "Rendering/Materials/RenderMaterialStateUtils.h"
|
||||
#include "Rendering/Picking/ObjectIdCodec.h"
|
||||
#include "RHI/RHICommandList.h"
|
||||
#include "RHI/RHIDevice.h"
|
||||
#include <XCEngine/Resources/BuiltinResources.h>
|
||||
@@ -23,18 +24,18 @@ const Resources::ShaderPass* FindObjectIdOutlineCompatiblePass(
|
||||
Resources::ShaderBackend backend) {
|
||||
const Resources::ShaderPass* outlinePass = shader.FindPass("ObjectIdOutline");
|
||||
if (outlinePass != nullptr &&
|
||||
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, outlinePass->name, backend)) {
|
||||
::XCEngine::Rendering::Internal::ShaderPassHasGraphicsVariants(shader, outlinePass->name, backend)) {
|
||||
return outlinePass;
|
||||
}
|
||||
|
||||
const Resources::ShaderPass* editorOutlinePass = shader.FindPass("EditorObjectIdOutline");
|
||||
if (editorOutlinePass != nullptr &&
|
||||
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, editorOutlinePass->name, backend)) {
|
||||
::XCEngine::Rendering::Internal::ShaderPassHasGraphicsVariants(shader, editorOutlinePass->name, backend)) {
|
||||
return editorOutlinePass;
|
||||
}
|
||||
|
||||
if (shader.GetPassCount() > 0 &&
|
||||
::XCEngine::Rendering::Detail::ShaderPassHasGraphicsVariants(shader, shader.GetPasses()[0].name, backend)) {
|
||||
::XCEngine::Rendering::Internal::ShaderPassHasGraphicsVariants(shader, shader.GetPasses()[0].name, backend)) {
|
||||
return &shader.GetPasses()[0];
|
||||
}
|
||||
|
||||
@@ -45,14 +46,15 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
|
||||
RHI::RHIType backendType,
|
||||
RHI::RHIPipelineLayout* pipelineLayout,
|
||||
const Resources::Shader& shader,
|
||||
const Containers::String& passName) {
|
||||
const Containers::String& passName,
|
||||
const RenderSurface& surface) {
|
||||
RHI::GraphicsPipelineDesc pipelineDesc = {};
|
||||
pipelineDesc.pipelineLayout = pipelineLayout;
|
||||
pipelineDesc.topologyType = static_cast<uint32_t>(RHI::PrimitiveTopologyType::Triangle);
|
||||
pipelineDesc.renderTargetCount = 1;
|
||||
pipelineDesc.renderTargetFormats[0] = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
::XCEngine::Rendering::Internal::ApplySingleColorAttachmentPropertiesToGraphicsPipelineDesc(
|
||||
surface,
|
||||
pipelineDesc);
|
||||
pipelineDesc.depthStencilFormat = static_cast<uint32_t>(RHI::Format::Unknown);
|
||||
pipelineDesc.sampleCount = 1;
|
||||
|
||||
const Resources::ShaderPass* shaderPass = shader.FindPass(passName);
|
||||
if (shaderPass != nullptr && shaderPass->hasFixedFunctionState) {
|
||||
@@ -74,11 +76,11 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
|
||||
::XCEngine::Rendering::ApplyRenderState(fallbackState, pipelineDesc);
|
||||
}
|
||||
|
||||
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Detail::ToShaderBackend(backendType);
|
||||
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Internal::ToShaderBackend(backendType);
|
||||
if (const Resources::ShaderStageVariant* vertexVariant =
|
||||
shader.FindVariant(passName, Resources::ShaderType::Vertex, backend)) {
|
||||
if (shaderPass != nullptr) {
|
||||
::XCEngine::Rendering::Detail::ApplyShaderStageVariant(
|
||||
::XCEngine::Rendering::Internal::ApplyShaderStageVariant(
|
||||
shader.GetPath(),
|
||||
*shaderPass,
|
||||
backend,
|
||||
@@ -89,7 +91,7 @@ RHI::GraphicsPipelineDesc CreatePipelineDesc(
|
||||
if (const Resources::ShaderStageVariant* fragmentVariant =
|
||||
shader.FindVariant(passName, Resources::ShaderType::Fragment, backend)) {
|
||||
if (shaderPass != nullptr) {
|
||||
::XCEngine::Rendering::Detail::ApplyShaderStageVariant(
|
||||
::XCEngine::Rendering::Internal::ApplyShaderStageVariant(
|
||||
shader.GetPath(),
|
||||
*shaderPass,
|
||||
backend,
|
||||
@@ -128,22 +130,28 @@ void BuiltinObjectIdOutlinePass::Shutdown() {
|
||||
bool BuiltinObjectIdOutlinePass::Render(
|
||||
const RenderContext& renderContext,
|
||||
const RenderSurface& surface,
|
||||
RHI::RHIResourceView* objectIdTextureView,
|
||||
const ObjectIdOutlinePassInputs& inputs,
|
||||
const std::vector<uint64_t>& selectedObjectIds,
|
||||
const ObjectIdOutlineStyle& style) {
|
||||
if (!renderContext.IsValid() ||
|
||||
renderContext.backendType != RHI::RHIType::D3D12 ||
|
||||
objectIdTextureView == nullptr ||
|
||||
inputs.objectIdTextureView == nullptr ||
|
||||
selectedObjectIds.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EnsureInitialized(renderContext)) {
|
||||
const std::vector<RHI::RHIResourceView*>& colorAttachments = surface.GetColorAttachments();
|
||||
if (!::XCEngine::Rendering::Internal::HasSingleColorAttachment(surface) ||
|
||||
colorAttachments.empty() ||
|
||||
colorAttachments[0] == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<RHI::RHIResourceView*>& colorAttachments = surface.GetColorAttachments();
|
||||
if (colorAttachments.empty() || colorAttachments[0] == nullptr) {
|
||||
const Math::RectInt renderArea = surface.GetRenderArea();
|
||||
if (renderArea.width <= 0 || renderArea.height <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EnsureInitialized(renderContext, surface)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -159,39 +167,61 @@ bool BuiltinObjectIdOutlinePass::Render(
|
||||
style.outlineColor.b,
|
||||
style.outlineColor.a);
|
||||
|
||||
const uint32_t selectedCount = (std::min)(
|
||||
static_cast<uint32_t>(selectedObjectIds.size()),
|
||||
kMaxSelectedObjectCount);
|
||||
uint32_t selectedCount = 0u;
|
||||
for (uint64_t selectedObjectId : selectedObjectIds) {
|
||||
if (selectedCount >= kMaxSelectedObjectCount) {
|
||||
break;
|
||||
}
|
||||
|
||||
RenderObjectId renderObjectId = kInvalidRenderObjectId;
|
||||
if (!TryConvertRuntimeObjectIdToRenderObjectId(selectedObjectId, renderObjectId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
constants.selectedObjectColors[selectedCount] =
|
||||
EncodeRenderObjectIdToColor(renderObjectId);
|
||||
++selectedCount;
|
||||
}
|
||||
if (selectedCount == 0u) {
|
||||
return false;
|
||||
}
|
||||
|
||||
constants.selectedInfo = Math::Vector4(
|
||||
static_cast<float>(selectedCount),
|
||||
style.debugSelectionMask ? 1.0f : 0.0f,
|
||||
style.outlineWidthPixels,
|
||||
0.0f);
|
||||
for (uint32_t index = 0; index < selectedCount; ++index) {
|
||||
constants.selectedObjectColors[index] =
|
||||
EncodeObjectIdToColor(selectedObjectIds[index]);
|
||||
}
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
m_textureSet->Update(0, objectIdTextureView);
|
||||
m_textureSet->Update(0, inputs.objectIdTextureView);
|
||||
|
||||
RHI::RHICommandList* commandList = renderContext.commandList;
|
||||
RHI::RHIResourceView* renderTarget = colorAttachments[0];
|
||||
if (surface.IsAutoTransitionEnabled()) {
|
||||
commandList->TransitionBarrier(
|
||||
renderTarget,
|
||||
surface.GetColorStateAfter(),
|
||||
RHI::ResourceStates::RenderTarget);
|
||||
commandList->TransitionBarrier(
|
||||
inputs.objectIdTextureView,
|
||||
inputs.objectIdTextureState,
|
||||
RHI::ResourceStates::PixelShaderResource);
|
||||
}
|
||||
commandList->SetRenderTargets(1, &renderTarget, nullptr);
|
||||
|
||||
const RHI::Viewport viewport = {
|
||||
0.0f,
|
||||
0.0f,
|
||||
static_cast<float>(surface.GetWidth()),
|
||||
static_cast<float>(surface.GetHeight()),
|
||||
static_cast<float>(renderArea.x),
|
||||
static_cast<float>(renderArea.y),
|
||||
static_cast<float>(renderArea.width),
|
||||
static_cast<float>(renderArea.height),
|
||||
0.0f,
|
||||
1.0f
|
||||
};
|
||||
const RHI::Rect scissorRect = {
|
||||
0,
|
||||
0,
|
||||
static_cast<int32_t>(surface.GetWidth()),
|
||||
static_cast<int32_t>(surface.GetHeight())
|
||||
renderArea.x,
|
||||
renderArea.y,
|
||||
renderArea.x + renderArea.width,
|
||||
renderArea.y + renderArea.height
|
||||
};
|
||||
|
||||
commandList->SetViewport(viewport);
|
||||
@@ -202,10 +232,27 @@ bool BuiltinObjectIdOutlinePass::Render(
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet };
|
||||
commandList->SetGraphicsDescriptorSets(0, 2, descriptorSets, m_pipelineLayout);
|
||||
commandList->Draw(3, 1, 0, 0);
|
||||
commandList->EndRenderPass();
|
||||
|
||||
if (surface.IsAutoTransitionEnabled()) {
|
||||
commandList->TransitionBarrier(
|
||||
renderTarget,
|
||||
RHI::ResourceStates::RenderTarget,
|
||||
surface.GetColorStateAfter());
|
||||
commandList->TransitionBarrier(
|
||||
inputs.objectIdTextureView,
|
||||
RHI::ResourceStates::PixelShaderResource,
|
||||
inputs.objectIdTextureState);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BuiltinObjectIdOutlinePass::EnsureInitialized(const RenderContext& renderContext) {
|
||||
bool BuiltinObjectIdOutlinePass::EnsureInitialized(const RenderContext& renderContext, const RenderSurface& surface) {
|
||||
const RHI::Format renderTargetFormat =
|
||||
::XCEngine::Rendering::Internal::ResolveSurfaceColorFormat(surface, 0u);
|
||||
const uint32_t renderTargetSampleCount =
|
||||
::XCEngine::Rendering::Internal::ResolveSurfaceSampleCount(surface);
|
||||
if (m_pipelineLayout != nullptr &&
|
||||
m_pipelineState != nullptr &&
|
||||
m_constantPool != nullptr &&
|
||||
@@ -213,18 +260,25 @@ bool BuiltinObjectIdOutlinePass::EnsureInitialized(const RenderContext& renderCo
|
||||
m_texturePool != nullptr &&
|
||||
m_textureSet != nullptr &&
|
||||
m_device == renderContext.device &&
|
||||
m_backendType == renderContext.backendType) {
|
||||
m_backendType == renderContext.backendType &&
|
||||
m_renderTargetFormat == renderTargetFormat &&
|
||||
m_renderTargetSampleCount == renderTargetSampleCount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (HasCreatedResources()) {
|
||||
DestroyResources();
|
||||
}
|
||||
return CreateResources(renderContext);
|
||||
return CreateResources(renderContext, surface);
|
||||
}
|
||||
|
||||
bool BuiltinObjectIdOutlinePass::CreateResources(const RenderContext& renderContext) {
|
||||
if (!renderContext.IsValid() || renderContext.backendType != RHI::RHIType::D3D12) {
|
||||
bool BuiltinObjectIdOutlinePass::CreateResources(const RenderContext& renderContext, const RenderSurface& surface) {
|
||||
if (!renderContext.IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!::XCEngine::Rendering::Internal::HasSingleColorAttachment(surface) ||
|
||||
::XCEngine::Rendering::Internal::ResolveSurfaceColorFormat(surface, 0u) == RHI::Format::Unknown) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -249,7 +303,7 @@ bool BuiltinObjectIdOutlinePass::CreateResources(const RenderContext& renderCont
|
||||
m_backendType = renderContext.backendType;
|
||||
m_builtinObjectIdOutlineShader.emplace(std::move(shader));
|
||||
|
||||
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Detail::ToShaderBackend(m_backendType);
|
||||
const Resources::ShaderBackend backend = ::XCEngine::Rendering::Internal::ToShaderBackend(m_backendType);
|
||||
const Resources::ShaderPass* outlinePass =
|
||||
FindObjectIdOutlineCompatiblePass(*m_builtinObjectIdOutlineShader->Get(), backend);
|
||||
if (outlinePass == nullptr) {
|
||||
@@ -323,15 +377,19 @@ bool BuiltinObjectIdOutlinePass::CreateResources(const RenderContext& renderCont
|
||||
|
||||
m_pipelineState = m_device->CreatePipelineState(
|
||||
CreatePipelineDesc(
|
||||
m_backendType,
|
||||
m_pipelineLayout,
|
||||
*m_builtinObjectIdOutlineShader->Get(),
|
||||
outlinePass->name));
|
||||
m_backendType,
|
||||
m_pipelineLayout,
|
||||
*m_builtinObjectIdOutlineShader->Get(),
|
||||
outlinePass->name,
|
||||
surface));
|
||||
if (m_pipelineState == nullptr || !m_pipelineState->IsValid()) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
m_renderTargetFormat = ::XCEngine::Rendering::Internal::ResolveSurfaceColorFormat(surface, 0u);
|
||||
m_renderTargetSampleCount = ::XCEngine::Rendering::Internal::ResolveSurfaceSampleCount(surface);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -400,6 +458,8 @@ void BuiltinObjectIdOutlinePass::ResetState() {
|
||||
m_texturePool = nullptr;
|
||||
m_textureSet = nullptr;
|
||||
m_builtinObjectIdOutlineShader.reset();
|
||||
m_renderTargetFormat = RHI::Format::Unknown;
|
||||
m_renderTargetSampleCount = 1u;
|
||||
}
|
||||
|
||||
} // namespace Passes
|
||||
|
||||
Reference in New Issue
Block a user