refactor: move scene view infinite grid pass into renderer
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
#include "SceneViewportGrid.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr float kCameraHeightScaleFactor = 0.50f;
|
||||
constexpr float kTransitionStart = 0.65f;
|
||||
constexpr float kTransitionEnd = 0.95f;
|
||||
constexpr float kMinimumVerticalViewComponent = 0.15f;
|
||||
|
||||
float SnapGridSpacing(float targetSpacing) {
|
||||
const float clampedTarget = (std::max)(targetSpacing, 0.02f);
|
||||
const float exponent = std::floor(std::log10(clampedTarget));
|
||||
return std::pow(10.0f, exponent);
|
||||
}
|
||||
|
||||
float ComputeTransitionBlend(float targetSpacing, float baseScale) {
|
||||
const float normalizedSpacing = (std::max)(targetSpacing / (std::max)(baseScale, 1e-4f), 1.0f);
|
||||
const float transitionPosition = std::log10(normalizedSpacing);
|
||||
const float t =
|
||||
(transitionPosition - kTransitionStart) /
|
||||
(kTransitionEnd - kTransitionStart);
|
||||
const float saturated = (std::clamp)(t, 0.0f, 1.0f);
|
||||
return saturated * saturated * (3.0f - 2.0f * saturated);
|
||||
}
|
||||
|
||||
float ComputeViewDistanceToGridPlane(const SceneViewportOverlayData& overlay) {
|
||||
const float cameraHeight = std::abs(overlay.cameraPosition.y);
|
||||
const Math::Vector3 forward = overlay.cameraForward.Normalized();
|
||||
|
||||
const bool lookingTowardGrid =
|
||||
(overlay.cameraPosition.y >= 0.0f && forward.y < 0.0f) ||
|
||||
(overlay.cameraPosition.y < 0.0f && forward.y > 0.0f);
|
||||
if (!lookingTowardGrid) {
|
||||
return cameraHeight;
|
||||
}
|
||||
|
||||
const float verticalViewComponent = (std::max)(std::abs(forward.y), kMinimumVerticalViewComponent);
|
||||
return cameraHeight / verticalViewComponent;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SceneGridParameters BuildSceneGridParameters(const SceneViewportOverlayData& overlay) {
|
||||
SceneGridParameters parameters = {};
|
||||
if (!overlay.valid) {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
const float cameraHeight = std::abs(overlay.cameraPosition.y);
|
||||
const float viewDistance = ComputeViewDistanceToGridPlane(overlay);
|
||||
// Keep grid density stable while orbiting/looking around. Rotation should
|
||||
// only affect how much of the infinite grid is visible, not its base LOD.
|
||||
const float targetSpacing = (std::max)(cameraHeight * kCameraHeightScaleFactor, 0.1f);
|
||||
|
||||
parameters.valid = true;
|
||||
parameters.baseScale = SnapGridSpacing(targetSpacing);
|
||||
parameters.transitionBlend = ComputeTransitionBlend(targetSpacing, parameters.baseScale);
|
||||
parameters.fadeDistance = (std::max)(
|
||||
parameters.baseScale * 320.0f,
|
||||
viewDistance * 80.0f);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
} // namespace Editor
|
||||
} // namespace XCEngine
|
||||
@@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IViewportHostService.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
struct SceneGridParameters {
|
||||
bool valid = false;
|
||||
float baseScale = 1.0f;
|
||||
float transitionBlend = 0.0f;
|
||||
float fadeDistance = 500.0f;
|
||||
};
|
||||
|
||||
SceneGridParameters BuildSceneGridParameters(const SceneViewportOverlayData& overlay);
|
||||
|
||||
} // namespace Editor
|
||||
} // namespace XCEngine
|
||||
@@ -1,394 +0,0 @@
|
||||
#include "SceneViewportInfiniteGridPass.h"
|
||||
|
||||
#include "SceneViewportGrid.h"
|
||||
#include "SceneViewportMath.h"
|
||||
|
||||
#include <XCEngine/Core/Math/Vector4.h>
|
||||
#include <XCEngine/RHI/RHICommandList.h>
|
||||
#include <XCEngine/RHI/RHIDevice.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
namespace {
|
||||
|
||||
const char kInfiniteGridHlsl[] = R"(
|
||||
cbuffer GridConstants : register(b0) {
|
||||
float4x4 gViewProjectionMatrix;
|
||||
float4 gCameraPositionAndScale;
|
||||
float4 gCameraRightAndFade;
|
||||
float4 gCameraUpAndTanHalfFov;
|
||||
float4 gCameraForwardAndAspect;
|
||||
float4 gViewportNearFar;
|
||||
float4 gGridTransition;
|
||||
};
|
||||
|
||||
struct VSOutput {
|
||||
float4 position : SV_POSITION;
|
||||
};
|
||||
|
||||
VSOutput MainVS(uint vertexId : SV_VertexID) {
|
||||
static const float2 positions[3] = {
|
||||
float2(-1.0, -1.0),
|
||||
float2(-1.0, 3.0),
|
||||
float2( 3.0, -1.0)
|
||||
};
|
||||
|
||||
VSOutput output;
|
||||
output.position = float4(positions[vertexId], 0.0, 1.0);
|
||||
return output;
|
||||
}
|
||||
|
||||
float PristineGridLine(float2 uv) {
|
||||
float2 deriv = max(fwidth(uv), float2(1e-6, 1e-6));
|
||||
float2 uvMod = frac(uv);
|
||||
float2 uvDist = min(uvMod, 1.0 - uvMod);
|
||||
float2 distInPixels = uvDist / deriv;
|
||||
float2 lineAlpha = 1.0 - smoothstep(0.0, 1.0, distInPixels);
|
||||
float density = max(deriv.x, deriv.y);
|
||||
float densityFade = 1.0 - smoothstep(0.5, 1.0, density);
|
||||
return max(lineAlpha.x, lineAlpha.y) * densityFade;
|
||||
}
|
||||
|
||||
float AxisLineAA(float coord, float deriv) {
|
||||
float distInPixels = abs(coord) / max(deriv, 1e-6);
|
||||
return 1.0 - smoothstep(0.0, 1.5, distInPixels);
|
||||
}
|
||||
|
||||
struct GridLayer {
|
||||
float minor;
|
||||
float major;
|
||||
};
|
||||
|
||||
GridLayer SampleGridLayer(float2 worldPos2D, float baseScale) {
|
||||
GridLayer layer;
|
||||
const float2 gridCoord1 = worldPos2D / baseScale;
|
||||
const float2 gridCoord10 = worldPos2D / (baseScale * 10.0);
|
||||
const float grid1 = PristineGridLine(gridCoord1);
|
||||
const float grid10 = PristineGridLine(gridCoord10);
|
||||
const float2 deriv1 = fwidth(gridCoord1);
|
||||
const float lodFactor = smoothstep(0.3, 0.6, max(deriv1.x, deriv1.y));
|
||||
|
||||
layer.major = max(grid10, grid1 * 0.35);
|
||||
layer.minor = grid1 * (1.0 - lodFactor);
|
||||
return layer;
|
||||
}
|
||||
|
||||
struct PSOutput {
|
||||
float4 color : SV_TARGET0;
|
||||
float depth : SV_Depth;
|
||||
};
|
||||
|
||||
PSOutput MainPS(VSOutput input) {
|
||||
const float2 viewportSize = max(gViewportNearFar.xy, float2(1.0, 1.0));
|
||||
const float scale = max(gCameraPositionAndScale.w, 1e-4);
|
||||
const float fadeDistance = max(gCameraRightAndFade.w, scale * 10.0);
|
||||
const float tanHalfFov = max(gCameraUpAndTanHalfFov.w, 1e-4);
|
||||
const float aspect = max(gCameraForwardAndAspect.w, 1e-4);
|
||||
const float transitionBlend = saturate(gGridTransition.x);
|
||||
const float nearClip = gViewportNearFar.z;
|
||||
const float sceneFarClip = gViewportNearFar.w;
|
||||
|
||||
const float2 ndc = float2(
|
||||
(input.position.x / viewportSize.x) * 2.0 - 1.0,
|
||||
1.0 - (input.position.y / viewportSize.y) * 2.0);
|
||||
|
||||
const float3 cameraPosition = gCameraPositionAndScale.xyz;
|
||||
const float3 rayDirection = normalize(
|
||||
gCameraForwardAndAspect.xyz +
|
||||
ndc.x * aspect * tanHalfFov * gCameraRightAndFade.xyz +
|
||||
ndc.y * tanHalfFov * gCameraUpAndTanHalfFov.xyz);
|
||||
|
||||
if (abs(rayDirection.y) < 1e-5) {
|
||||
discard;
|
||||
}
|
||||
|
||||
const float t = -cameraPosition.y / rayDirection.y;
|
||||
if (t <= nearClip) {
|
||||
discard;
|
||||
}
|
||||
|
||||
const float3 worldPosition = cameraPosition + rayDirection * t;
|
||||
float depth = 0.999999;
|
||||
if (t < sceneFarClip) {
|
||||
const float4 clipPosition = mul(gViewProjectionMatrix, float4(worldPosition, 1.0));
|
||||
if (clipPosition.w <= 1e-6) {
|
||||
discard;
|
||||
}
|
||||
|
||||
depth = clipPosition.z / clipPosition.w;
|
||||
if (depth <= 0.0 || depth >= 1.0) {
|
||||
discard;
|
||||
}
|
||||
}
|
||||
|
||||
const float radialFade =
|
||||
1.0 - smoothstep(fadeDistance * 0.3, fadeDistance, length(worldPosition - cameraPosition));
|
||||
const float normalFade = smoothstep(0.0, 0.15, abs(rayDirection.y));
|
||||
const float fadeFactor = radialFade * normalFade;
|
||||
if (fadeFactor < 1e-3) {
|
||||
discard;
|
||||
}
|
||||
|
||||
const float2 worldPos2D = worldPosition.xz;
|
||||
const GridLayer baseLayer = SampleGridLayer(worldPos2D, scale);
|
||||
const GridLayer nextLayer = SampleGridLayer(worldPos2D, scale * 10.0);
|
||||
const float minorGridIntensity = lerp(baseLayer.minor, nextLayer.minor, transitionBlend);
|
||||
const float majorGridIntensity = lerp(baseLayer.major, nextLayer.major, transitionBlend);
|
||||
float3 finalColor = float3(0.56, 0.56, 0.56);
|
||||
float finalAlpha = max(
|
||||
0.13 * minorGridIntensity * fadeFactor,
|
||||
0.28 * majorGridIntensity * fadeFactor);
|
||||
|
||||
const float2 worldDeriv = max(fwidth(worldPos2D), float2(1e-6, 1e-6));
|
||||
const float xAxisAlpha = AxisLineAA(worldPos2D.y, worldDeriv.y) * fadeFactor;
|
||||
const float zAxisAlpha = AxisLineAA(worldPos2D.x, worldDeriv.x) * fadeFactor;
|
||||
|
||||
const float axisAlpha = max(xAxisAlpha, zAxisAlpha);
|
||||
finalAlpha = max(finalAlpha, 0.34 * saturate(axisAlpha));
|
||||
|
||||
if (finalAlpha < 1e-3) {
|
||||
discard;
|
||||
}
|
||||
|
||||
PSOutput output;
|
||||
output.color = float4(finalColor, finalAlpha);
|
||||
output.depth = depth;
|
||||
return output;
|
||||
}
|
||||
)";
|
||||
|
||||
struct GridConstants {
|
||||
Math::Matrix4x4 viewProjection = Math::Matrix4x4::Identity();
|
||||
Math::Vector4 cameraPositionAndScale = Math::Vector4::Zero();
|
||||
Math::Vector4 cameraRightAndFade = Math::Vector4::Zero();
|
||||
Math::Vector4 cameraUpAndTanHalfFov = Math::Vector4::Zero();
|
||||
Math::Vector4 cameraForwardAndAspect = Math::Vector4::Zero();
|
||||
Math::Vector4 viewportNearFar = Math::Vector4::Zero();
|
||||
Math::Vector4 gridTransition = Math::Vector4::Zero();
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void SceneViewportInfiniteGridPass::Shutdown() {
|
||||
DestroyResources();
|
||||
}
|
||||
|
||||
bool SceneViewportInfiniteGridPass::Render(
|
||||
const Rendering::RenderContext& renderContext,
|
||||
const Rendering::RenderSurface& surface,
|
||||
const SceneViewportOverlayData& overlay) {
|
||||
if (!overlay.valid || !renderContext.IsValid() || renderContext.backendType != RHI::RHIType::D3D12) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EnsureInitialized(renderContext)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<RHI::RHIResourceView*>& colorAttachments = surface.GetColorAttachments();
|
||||
if (colorAttachments.empty() || colorAttachments[0] == nullptr || surface.GetDepthAttachment() == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const SceneGridParameters parameters = BuildSceneGridParameters(overlay);
|
||||
if (!parameters.valid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const Math::Matrix4x4 viewProjection =
|
||||
BuildSceneViewportProjectionMatrix(
|
||||
overlay,
|
||||
static_cast<float>(surface.GetWidth()),
|
||||
static_cast<float>(surface.GetHeight())) *
|
||||
BuildSceneViewportViewMatrix(overlay);
|
||||
|
||||
const float aspect = surface.GetHeight() > 0
|
||||
? static_cast<float>(surface.GetWidth()) / static_cast<float>(surface.GetHeight())
|
||||
: 1.0f;
|
||||
|
||||
GridConstants constants = {};
|
||||
constants.viewProjection = viewProjection.Transpose();
|
||||
constants.cameraPositionAndScale = Math::Vector4(overlay.cameraPosition, parameters.baseScale);
|
||||
constants.cameraRightAndFade = Math::Vector4(overlay.cameraRight, parameters.fadeDistance);
|
||||
constants.cameraUpAndTanHalfFov = Math::Vector4(
|
||||
overlay.cameraUp,
|
||||
std::tan(overlay.verticalFovDegrees * Math::DEG_TO_RAD * 0.5f));
|
||||
constants.cameraForwardAndAspect = Math::Vector4(overlay.cameraForward, aspect);
|
||||
constants.viewportNearFar = Math::Vector4(
|
||||
static_cast<float>(surface.GetWidth()),
|
||||
static_cast<float>(surface.GetHeight()),
|
||||
overlay.nearClipPlane,
|
||||
overlay.farClipPlane);
|
||||
constants.gridTransition = Math::Vector4(parameters.transitionBlend, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
|
||||
RHI::RHICommandList* commandList = renderContext.commandList;
|
||||
RHI::RHIResourceView* renderTarget = colorAttachments[0];
|
||||
commandList->SetRenderTargets(1, &renderTarget, surface.GetDepthAttachment());
|
||||
|
||||
const RHI::Viewport viewport = {
|
||||
0.0f,
|
||||
0.0f,
|
||||
static_cast<float>(surface.GetWidth()),
|
||||
static_cast<float>(surface.GetHeight()),
|
||||
0.0f,
|
||||
1.0f
|
||||
};
|
||||
const RHI::Rect scissorRect = {
|
||||
0,
|
||||
0,
|
||||
static_cast<int32_t>(surface.GetWidth()),
|
||||
static_cast<int32_t>(surface.GetHeight())
|
||||
};
|
||||
|
||||
commandList->SetViewport(viewport);
|
||||
commandList->SetScissorRect(scissorRect);
|
||||
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
|
||||
commandList->SetPipelineState(m_pipelineState);
|
||||
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet };
|
||||
commandList->SetGraphicsDescriptorSets(0, 1, descriptorSets, m_pipelineLayout);
|
||||
commandList->Draw(3, 1, 0, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SceneViewportInfiniteGridPass::EnsureInitialized(const Rendering::RenderContext& renderContext) {
|
||||
if (m_pipelineState != nullptr &&
|
||||
m_pipelineLayout != nullptr &&
|
||||
m_constantPool != nullptr &&
|
||||
m_constantSet != nullptr &&
|
||||
m_device == renderContext.device &&
|
||||
m_backendType == renderContext.backendType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DestroyResources();
|
||||
return CreateResources(renderContext);
|
||||
}
|
||||
|
||||
bool SceneViewportInfiniteGridPass::CreateResources(const Rendering::RenderContext& renderContext) {
|
||||
if (!renderContext.IsValid() || renderContext.backendType != RHI::RHIType::D3D12) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_device = renderContext.device;
|
||||
m_backendType = renderContext.backendType;
|
||||
|
||||
RHI::DescriptorSetLayoutBinding constantBinding = {};
|
||||
constantBinding.binding = 0;
|
||||
constantBinding.type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
|
||||
constantBinding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc constantLayout = {};
|
||||
constantLayout.bindings = &constantBinding;
|
||||
constantLayout.bindingCount = 1;
|
||||
|
||||
RHI::RHIPipelineLayoutDesc pipelineLayoutDesc = {};
|
||||
pipelineLayoutDesc.setLayouts = &constantLayout;
|
||||
pipelineLayoutDesc.setLayoutCount = 1;
|
||||
m_pipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc);
|
||||
if (m_pipelineLayout == nullptr) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc constantPoolDesc = {};
|
||||
constantPoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
constantPoolDesc.descriptorCount = 1;
|
||||
constantPoolDesc.shaderVisible = false;
|
||||
m_constantPool = m_device->CreateDescriptorPool(constantPoolDesc);
|
||||
if (m_constantPool == nullptr) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
m_constantSet = m_constantPool->AllocateSet(constantLayout);
|
||||
if (m_constantSet == nullptr) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::GraphicsPipelineDesc pipelineDesc = {};
|
||||
pipelineDesc.pipelineLayout = m_pipelineLayout;
|
||||
pipelineDesc.topologyType = static_cast<uint32_t>(RHI::PrimitiveTopologyType::Triangle);
|
||||
pipelineDesc.renderTargetCount = 1;
|
||||
pipelineDesc.renderTargetFormats[0] = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
pipelineDesc.depthStencilFormat = static_cast<uint32_t>(RHI::Format::D24_UNorm_S8_UInt);
|
||||
pipelineDesc.sampleCount = 1;
|
||||
|
||||
pipelineDesc.rasterizerState.fillMode = static_cast<uint32_t>(RHI::FillMode::Solid);
|
||||
pipelineDesc.rasterizerState.cullMode = static_cast<uint32_t>(RHI::CullMode::None);
|
||||
pipelineDesc.rasterizerState.frontFace = static_cast<uint32_t>(RHI::FrontFace::CounterClockwise);
|
||||
pipelineDesc.rasterizerState.depthClipEnable = true;
|
||||
|
||||
pipelineDesc.blendState.blendEnable = true;
|
||||
pipelineDesc.blendState.srcBlend = static_cast<uint32_t>(RHI::BlendFactor::SrcAlpha);
|
||||
pipelineDesc.blendState.dstBlend = static_cast<uint32_t>(RHI::BlendFactor::InvSrcAlpha);
|
||||
pipelineDesc.blendState.srcBlendAlpha = static_cast<uint32_t>(RHI::BlendFactor::One);
|
||||
pipelineDesc.blendState.dstBlendAlpha = static_cast<uint32_t>(RHI::BlendFactor::InvSrcAlpha);
|
||||
pipelineDesc.blendState.blendOp = static_cast<uint32_t>(RHI::BlendOp::Add);
|
||||
pipelineDesc.blendState.blendOpAlpha = static_cast<uint32_t>(RHI::BlendOp::Add);
|
||||
pipelineDesc.blendState.colorWriteMask = 0xF;
|
||||
|
||||
pipelineDesc.depthStencilState.depthTestEnable = true;
|
||||
pipelineDesc.depthStencilState.depthWriteEnable = false;
|
||||
pipelineDesc.depthStencilState.depthFunc = static_cast<uint32_t>(RHI::ComparisonFunc::LessEqual);
|
||||
|
||||
pipelineDesc.vertexShader.source.assign(
|
||||
kInfiniteGridHlsl,
|
||||
kInfiniteGridHlsl + std::strlen(kInfiniteGridHlsl));
|
||||
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
|
||||
pipelineDesc.vertexShader.entryPoint = L"MainVS";
|
||||
pipelineDesc.vertexShader.profile = L"vs_5_0";
|
||||
|
||||
pipelineDesc.fragmentShader.source.assign(
|
||||
kInfiniteGridHlsl,
|
||||
kInfiniteGridHlsl + std::strlen(kInfiniteGridHlsl));
|
||||
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
|
||||
pipelineDesc.fragmentShader.entryPoint = L"MainPS";
|
||||
pipelineDesc.fragmentShader.profile = L"ps_5_0";
|
||||
|
||||
m_pipelineState = m_device->CreatePipelineState(pipelineDesc);
|
||||
if (m_pipelineState == nullptr || !m_pipelineState->IsValid()) {
|
||||
DestroyResources();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SceneViewportInfiniteGridPass::DestroyResources() {
|
||||
if (m_pipelineState != nullptr) {
|
||||
m_pipelineState->Shutdown();
|
||||
delete m_pipelineState;
|
||||
m_pipelineState = nullptr;
|
||||
}
|
||||
|
||||
if (m_constantSet != nullptr) {
|
||||
m_constantSet->Shutdown();
|
||||
delete m_constantSet;
|
||||
m_constantSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_constantPool != nullptr) {
|
||||
m_constantPool->Shutdown();
|
||||
delete m_constantPool;
|
||||
m_constantPool = nullptr;
|
||||
}
|
||||
|
||||
if (m_pipelineLayout != nullptr) {
|
||||
m_pipelineLayout->Shutdown();
|
||||
delete m_pipelineLayout;
|
||||
m_pipelineLayout = nullptr;
|
||||
}
|
||||
|
||||
m_device = nullptr;
|
||||
m_backendType = RHI::RHIType::D3D12;
|
||||
}
|
||||
|
||||
} // namespace Editor
|
||||
} // namespace XCEngine
|
||||
@@ -1,42 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IViewportHostService.h"
|
||||
|
||||
#include <XCEngine/RHI/RHIDescriptorPool.h>
|
||||
#include <XCEngine/RHI/RHIDescriptorSet.h>
|
||||
#include <XCEngine/RHI/RHIEnums.h>
|
||||
#include <XCEngine/RHI/RHIPipelineState.h>
|
||||
#include <XCEngine/RHI/RHIPipelineLayout.h>
|
||||
#include <XCEngine/Rendering/RenderContext.h>
|
||||
#include <XCEngine/Rendering/RenderSurface.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
class SceneViewportInfiniteGridPass {
|
||||
public:
|
||||
~SceneViewportInfiniteGridPass() = default;
|
||||
|
||||
void Shutdown();
|
||||
|
||||
bool Render(
|
||||
const Rendering::RenderContext& renderContext,
|
||||
const Rendering::RenderSurface& surface,
|
||||
const SceneViewportOverlayData& overlay);
|
||||
|
||||
private:
|
||||
bool EnsureInitialized(const Rendering::RenderContext& renderContext);
|
||||
bool CreateResources(const Rendering::RenderContext& renderContext);
|
||||
void DestroyResources();
|
||||
|
||||
private:
|
||||
RHI::RHIDevice* m_device = nullptr;
|
||||
RHI::RHIType m_backendType = RHI::RHIType::D3D12;
|
||||
RHI::RHIPipelineLayout* m_pipelineLayout = nullptr;
|
||||
RHI::RHIPipelineState* m_pipelineState = nullptr;
|
||||
RHI::RHIDescriptorPool* m_constantPool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_constantSet = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Editor
|
||||
} // namespace XCEngine
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "IViewportHostService.h"
|
||||
#include "SceneViewportPicker.h"
|
||||
#include "SceneViewportCameraController.h"
|
||||
#include "SceneViewportInfiniteGridPass.h"
|
||||
#include "SceneViewportPostPassPlan.h"
|
||||
#include "UI/ImGuiBackendBridge.h"
|
||||
|
||||
@@ -18,6 +17,7 @@
|
||||
#include <XCEngine/RHI/RHIResourceView.h>
|
||||
#include <XCEngine/RHI/RHITexture.h>
|
||||
#include <XCEngine/Rendering/ObjectIdEncoding.h>
|
||||
#include <XCEngine/Rendering/Passes/BuiltinInfiniteGridPass.h>
|
||||
#include <XCEngine/Rendering/Passes/BuiltinObjectIdOutlinePass.h>
|
||||
#include <XCEngine/Rendering/RenderContext.h>
|
||||
#include <XCEngine/Rendering/RenderSurface.h>
|
||||
@@ -86,6 +86,21 @@ inline uint32_t ClampViewportPixelCoordinate(float value, uint32_t extent) {
|
||||
return static_cast<uint32_t>(std::floor(clamped));
|
||||
}
|
||||
|
||||
Rendering::Passes::InfiniteGridPassData BuildInfiniteGridPassData(
|
||||
const SceneViewportOverlayData& overlay) {
|
||||
Rendering::Passes::InfiniteGridPassData data = {};
|
||||
data.valid = overlay.valid;
|
||||
data.cameraPosition = overlay.cameraPosition;
|
||||
data.cameraForward = overlay.cameraForward;
|
||||
data.cameraRight = overlay.cameraRight;
|
||||
data.cameraUp = overlay.cameraUp;
|
||||
data.verticalFovDegrees = overlay.verticalFovDegrees;
|
||||
data.nearClipPlane = overlay.nearClipPlane;
|
||||
data.farClipPlane = overlay.farClipPlane;
|
||||
data.orbitDistance = overlay.orbitDistance;
|
||||
return data;
|
||||
}
|
||||
|
||||
Math::Vector3 GetSceneViewportOrientationAxisVector(SceneViewportOrientationAxis axis) {
|
||||
switch (axis) {
|
||||
case SceneViewportOrientationAxis::PositiveX:
|
||||
@@ -595,7 +610,7 @@ private:
|
||||
const bool rendered = m_sceneGridPass.Render(
|
||||
context.renderContext,
|
||||
context.surface,
|
||||
overlay);
|
||||
BuildInfiniteGridPassData(overlay));
|
||||
if (!rendered) {
|
||||
SetViewportStatusIfEmpty(entry.statusText, "Scene grid pass failed");
|
||||
}
|
||||
@@ -1005,7 +1020,7 @@ private:
|
||||
Rendering::RenderContext m_sceneViewLastRenderContext = {};
|
||||
std::array<ViewportEntry, 2> m_entries = {};
|
||||
SceneViewCameraState m_sceneViewCamera;
|
||||
SceneViewportInfiniteGridPass m_sceneGridPass;
|
||||
Rendering::Passes::BuiltinInfiniteGridPass m_sceneGridPass;
|
||||
Rendering::Passes::BuiltinObjectIdOutlinePass m_sceneSelectionOutlinePass;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user