2026-03-28 17:04:14 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "Core/IEditorContext.h"
|
|
|
|
|
#include "Core/ISceneManager.h"
|
2026-03-28 17:40:14 +08:00
|
|
|
#include "Core/ISelectionManager.h"
|
2026-03-28 17:04:14 +08:00
|
|
|
#include "IViewportHostService.h"
|
2026-03-29 15:12:38 +08:00
|
|
|
#include "SceneViewportPicker.h"
|
2026-03-28 17:40:14 +08:00
|
|
|
#include "SceneViewportCameraController.h"
|
2026-04-01 21:37:15 +08:00
|
|
|
#include "ViewportHostRenderFlowUtils.h"
|
2026-04-01 21:01:23 +08:00
|
|
|
#include "ViewportHostRenderTargets.h"
|
2026-04-01 20:24:17 +08:00
|
|
|
#include "ViewportObjectIdPicker.h"
|
2026-03-28 17:04:14 +08:00
|
|
|
#include "UI/ImGuiBackendBridge.h"
|
|
|
|
|
|
|
|
|
|
#include <XCEngine/Components/CameraComponent.h>
|
2026-03-28 17:40:14 +08:00
|
|
|
#include <XCEngine/Components/GameObject.h>
|
2026-04-02 04:42:35 +08:00
|
|
|
#include <XCEngine/Core/Asset/ResourceManager.h>
|
2026-04-01 19:17:44 +08:00
|
|
|
#include <XCEngine/RHI/RHIDevice.h>
|
2026-03-28 17:04:14 +08:00
|
|
|
#include <XCEngine/RHI/RHIEnums.h>
|
|
|
|
|
#include <XCEngine/RHI/RHIResourceView.h>
|
|
|
|
|
#include <XCEngine/RHI/RHITexture.h>
|
2026-04-01 18:31:30 +08:00
|
|
|
#include <XCEngine/Rendering/Passes/BuiltinInfiniteGridPass.h>
|
2026-03-28 17:04:14 +08:00
|
|
|
#include <XCEngine/Rendering/RenderContext.h>
|
|
|
|
|
#include <XCEngine/Rendering/RenderSurface.h>
|
|
|
|
|
#include <XCEngine/Rendering/SceneRenderer.h>
|
|
|
|
|
#include <XCEngine/Scene/Scene.h>
|
|
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2026-04-01 21:50:58 +08:00
|
|
|
#include <vector>
|
2026-03-28 17:04:14 +08:00
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Editor {
|
|
|
|
|
|
2026-03-29 15:12:38 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
constexpr bool kDebugSceneSelectionMask = false;
|
|
|
|
|
|
2026-03-30 03:52:59 +08:00
|
|
|
Math::Vector3 GetSceneViewportOrientationAxisVector(SceneViewportOrientationAxis axis) {
|
|
|
|
|
switch (axis) {
|
|
|
|
|
case SceneViewportOrientationAxis::PositiveX:
|
|
|
|
|
return Math::Vector3::Right();
|
|
|
|
|
case SceneViewportOrientationAxis::NegativeX:
|
|
|
|
|
return Math::Vector3::Left();
|
|
|
|
|
case SceneViewportOrientationAxis::PositiveY:
|
|
|
|
|
return Math::Vector3::Up();
|
|
|
|
|
case SceneViewportOrientationAxis::NegativeY:
|
|
|
|
|
return Math::Vector3::Down();
|
|
|
|
|
case SceneViewportOrientationAxis::PositiveZ:
|
|
|
|
|
return Math::Vector3::Forward();
|
|
|
|
|
case SceneViewportOrientationAxis::NegativeZ:
|
|
|
|
|
return Math::Vector3::Back();
|
|
|
|
|
default:
|
|
|
|
|
return Math::Vector3::Zero();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 15:12:38 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
2026-03-28 17:04:14 +08:00
|
|
|
class ViewportHostService : public IViewportHostService {
|
|
|
|
|
public:
|
|
|
|
|
void Initialize(UI::ImGuiBackendBridge& backend, RHI::RHIDevice* device) {
|
|
|
|
|
Shutdown();
|
|
|
|
|
m_backend = &backend;
|
2026-04-01 19:17:44 +08:00
|
|
|
m_device = device;
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shutdown() {
|
|
|
|
|
for (ViewportEntry& entry : m_entries) {
|
2026-04-01 21:01:23 +08:00
|
|
|
DestroyViewportRenderTargets(m_backend, entry.renderTargets);
|
2026-03-28 17:04:14 +08:00
|
|
|
entry = {};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:40:14 +08:00
|
|
|
m_sceneViewCamera = {};
|
2026-04-01 16:44:11 +08:00
|
|
|
m_sceneViewLastRenderContext = {};
|
2026-03-28 17:04:14 +08:00
|
|
|
m_device = nullptr;
|
|
|
|
|
m_backend = nullptr;
|
|
|
|
|
m_sceneRenderer.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BeginFrame() override {
|
|
|
|
|
for (ViewportEntry& entry : m_entries) {
|
|
|
|
|
entry.requestedThisFrame = false;
|
|
|
|
|
entry.requestedWidth = 0;
|
|
|
|
|
entry.requestedHeight = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EditorViewportFrame RequestViewport(EditorViewportKind kind, const ImVec2& requestedSize) override {
|
|
|
|
|
ViewportEntry& entry = GetEntry(kind);
|
|
|
|
|
entry.requestedThisFrame = requestedSize.x > 1.0f && requestedSize.y > 1.0f;
|
|
|
|
|
entry.requestedWidth = entry.requestedThisFrame
|
|
|
|
|
? static_cast<uint32_t>(requestedSize.x)
|
|
|
|
|
: 0u;
|
|
|
|
|
entry.requestedHeight = entry.requestedThisFrame
|
|
|
|
|
? static_cast<uint32_t>(requestedSize.y)
|
|
|
|
|
: 0u;
|
|
|
|
|
|
|
|
|
|
if (entry.requestedThisFrame && m_backend != nullptr && m_device != nullptr) {
|
|
|
|
|
EnsureViewportResources(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EditorViewportFrame frame = {};
|
2026-04-01 21:01:23 +08:00
|
|
|
frame.textureId = entry.renderTargets.textureId;
|
2026-03-28 17:04:14 +08:00
|
|
|
frame.requestedSize = requestedSize;
|
2026-04-01 21:01:23 +08:00
|
|
|
frame.renderSize = ImVec2(
|
|
|
|
|
static_cast<float>(entry.renderTargets.width),
|
|
|
|
|
static_cast<float>(entry.renderTargets.height));
|
|
|
|
|
frame.hasTexture = entry.renderTargets.textureId != ImTextureID{};
|
2026-03-28 17:04:14 +08:00
|
|
|
frame.wasRequested = entry.requestedThisFrame;
|
|
|
|
|
frame.statusText = entry.statusText;
|
|
|
|
|
return frame;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:40:14 +08:00
|
|
|
void UpdateSceneViewInput(IEditorContext& context, const SceneViewportInput& input) override {
|
|
|
|
|
if (!EnsureSceneViewCamera()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.focusSelectionRequested) {
|
|
|
|
|
FocusSceneView(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SceneViewportCameraInputState controllerInput = {};
|
|
|
|
|
controllerInput.viewportHeight = input.viewportSize.y;
|
|
|
|
|
controllerInput.zoomDelta = input.hovered ? input.mouseWheel : 0.0f;
|
2026-03-29 15:12:38 +08:00
|
|
|
controllerInput.flySpeedDelta = input.hovered ? input.flySpeedDelta : 0.0f;
|
2026-03-28 18:28:11 +08:00
|
|
|
controllerInput.deltaTime = input.deltaTime;
|
|
|
|
|
controllerInput.moveForward = input.moveForward;
|
|
|
|
|
controllerInput.moveRight = input.moveRight;
|
|
|
|
|
controllerInput.moveUp = input.moveUp;
|
|
|
|
|
controllerInput.fastMove = input.fastMove;
|
2026-03-28 17:40:14 +08:00
|
|
|
|
2026-03-28 18:21:18 +08:00
|
|
|
if (input.looking) {
|
|
|
|
|
controllerInput.lookDeltaX = input.mouseDelta.x;
|
|
|
|
|
controllerInput.lookDeltaY = input.mouseDelta.y;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:40:14 +08:00
|
|
|
if (input.orbiting) {
|
|
|
|
|
controllerInput.orbitDeltaX = input.mouseDelta.x;
|
|
|
|
|
controllerInput.orbitDeltaY = input.mouseDelta.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.panning) {
|
|
|
|
|
controllerInput.panDeltaX = input.mouseDelta.x;
|
|
|
|
|
controllerInput.panDeltaY = input.mouseDelta.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sceneViewCamera.controller.ApplyInput(controllerInput);
|
|
|
|
|
ApplySceneViewCameraController();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 15:12:38 +08:00
|
|
|
uint64_t PickSceneViewEntity(
|
|
|
|
|
IEditorContext& context,
|
|
|
|
|
const ImVec2& viewportSize,
|
|
|
|
|
const ImVec2& viewportMousePosition) override {
|
|
|
|
|
if (!EnsureSceneViewCamera()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Components::Scene* scene = context.GetSceneManager().GetScene();
|
|
|
|
|
if (scene == nullptr) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 16:44:11 +08:00
|
|
|
ViewportEntry& entry = GetEntry(EditorViewportKind::Scene);
|
|
|
|
|
uint64_t objectIdEntity = 0;
|
|
|
|
|
if (TryPickSceneViewEntityWithObjectId(
|
|
|
|
|
entry,
|
|
|
|
|
viewportSize,
|
|
|
|
|
viewportMousePosition,
|
|
|
|
|
objectIdEntity)) {
|
|
|
|
|
return objectIdEntity;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 15:12:38 +08:00
|
|
|
SceneViewportPickRequest request = {};
|
|
|
|
|
request.scene = scene;
|
|
|
|
|
request.overlay = GetSceneViewOverlayData();
|
|
|
|
|
request.viewportSize = Math::Vector2(viewportSize.x, viewportSize.y);
|
|
|
|
|
request.viewportPosition = Math::Vector2(viewportMousePosition.x, viewportMousePosition.y);
|
|
|
|
|
return PickSceneViewportEntity(request).entityId;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 03:52:59 +08:00
|
|
|
void AlignSceneViewToOrientationAxis(SceneViewportOrientationAxis axis) override {
|
|
|
|
|
if (!EnsureSceneViewCamera()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Math::Vector3 axisDirection = GetSceneViewportOrientationAxisVector(axis);
|
|
|
|
|
if (axisDirection.SqrMagnitude() <= Math::EPSILON) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To make the clicked cone face the screen, the camera must look back along that axis.
|
|
|
|
|
m_sceneViewCamera.controller.AnimateToForward(axisDirection * -1.0f);
|
|
|
|
|
ApplySceneViewCameraController();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:50:54 +08:00
|
|
|
SceneViewportOverlayData GetSceneViewOverlayData() const override {
|
|
|
|
|
SceneViewportOverlayData data = {};
|
|
|
|
|
if (m_sceneViewCamera.gameObject == nullptr || m_sceneViewCamera.camera == nullptr) {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Components::TransformComponent* transform = m_sceneViewCamera.gameObject->GetTransform();
|
|
|
|
|
if (transform == nullptr) {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.valid = true;
|
|
|
|
|
data.cameraPosition = transform->GetPosition();
|
|
|
|
|
data.cameraForward = transform->GetForward();
|
|
|
|
|
data.cameraRight = transform->GetRight();
|
|
|
|
|
data.cameraUp = transform->GetUp();
|
|
|
|
|
data.verticalFovDegrees = m_sceneViewCamera.camera->GetFieldOfView();
|
|
|
|
|
data.nearClipPlane = m_sceneViewCamera.camera->GetNearClipPlane();
|
|
|
|
|
data.farClipPlane = m_sceneViewCamera.camera->GetFarClipPlane();
|
|
|
|
|
data.orbitDistance = m_sceneViewCamera.controller.GetDistance();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:04:14 +08:00
|
|
|
void RenderRequestedViewports(
|
|
|
|
|
IEditorContext& context,
|
|
|
|
|
const Rendering::RenderContext& renderContext) override {
|
|
|
|
|
if (m_backend == nullptr || m_device == nullptr || !renderContext.IsValid()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EnsureSceneRenderer();
|
2026-04-01 16:44:11 +08:00
|
|
|
m_sceneViewLastRenderContext = renderContext;
|
2026-03-28 17:04:14 +08:00
|
|
|
const auto* scene = context.GetSceneManager().GetScene();
|
|
|
|
|
|
|
|
|
|
for (ViewportEntry& entry : m_entries) {
|
|
|
|
|
if (!entry.requestedThisFrame) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!EnsureViewportResources(entry)) {
|
|
|
|
|
entry.statusText = "Failed to create viewport render targets";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 15:12:38 +08:00
|
|
|
RenderViewportEntry(entry, context, scene, renderContext);
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct ViewportEntry {
|
|
|
|
|
EditorViewportKind kind = EditorViewportKind::Scene;
|
|
|
|
|
uint32_t requestedWidth = 0;
|
|
|
|
|
uint32_t requestedHeight = 0;
|
|
|
|
|
bool requestedThisFrame = false;
|
2026-04-01 21:01:23 +08:00
|
|
|
ViewportRenderTargets renderTargets = {};
|
2026-03-28 17:04:14 +08:00
|
|
|
std::string statusText;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-28 17:40:14 +08:00
|
|
|
struct SceneViewCameraState {
|
|
|
|
|
std::unique_ptr<Components::GameObject> gameObject;
|
|
|
|
|
Components::CameraComponent* camera = nullptr;
|
|
|
|
|
SceneViewportCameraController controller;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
struct SceneViewportRenderState {
|
|
|
|
|
SceneViewportOverlayData overlay = {};
|
2026-04-02 14:49:00 +08:00
|
|
|
Rendering::BuiltinPostProcessRequest builtinPostProcess = {};
|
2026-04-01 17:33:07 +08:00
|
|
|
std::vector<uint64_t> selectedObjectIds;
|
2026-03-31 22:10:27 +08:00
|
|
|
};
|
|
|
|
|
|
2026-03-28 17:04:14 +08:00
|
|
|
ViewportEntry& GetEntry(EditorViewportKind kind) {
|
|
|
|
|
const size_t index = kind == EditorViewportKind::Scene ? 0u : 1u;
|
|
|
|
|
m_entries[index].kind = kind;
|
|
|
|
|
return m_entries[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EnsureSceneRenderer() {
|
|
|
|
|
if (!m_sceneRenderer) {
|
|
|
|
|
m_sceneRenderer = std::make_unique<Rendering::SceneRenderer>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:40:14 +08:00
|
|
|
bool EnsureSceneViewCamera() {
|
|
|
|
|
if (m_sceneViewCamera.gameObject != nullptr && m_sceneViewCamera.camera != nullptr) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sceneViewCamera.gameObject = std::make_unique<Components::GameObject>("EditorSceneCamera");
|
|
|
|
|
m_sceneViewCamera.camera = m_sceneViewCamera.gameObject->AddComponent<Components::CameraComponent>();
|
|
|
|
|
if (m_sceneViewCamera.camera == nullptr) {
|
|
|
|
|
m_sceneViewCamera.gameObject.reset();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sceneViewCamera.camera->SetPrimary(false);
|
|
|
|
|
m_sceneViewCamera.camera->SetProjectionType(Components::CameraProjectionType::Perspective);
|
|
|
|
|
m_sceneViewCamera.camera->SetFieldOfView(60.0f);
|
|
|
|
|
m_sceneViewCamera.camera->SetNearClipPlane(0.03f);
|
|
|
|
|
m_sceneViewCamera.camera->SetFarClipPlane(2000.0f);
|
|
|
|
|
m_sceneViewCamera.controller.Reset();
|
|
|
|
|
ApplySceneViewCameraController();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ApplySceneViewCameraController() {
|
|
|
|
|
if (m_sceneViewCamera.gameObject == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sceneViewCamera.controller.ApplyTo(*m_sceneViewCamera.gameObject->GetTransform());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FocusSceneView(IEditorContext& context) {
|
|
|
|
|
Components::GameObject* target = nullptr;
|
|
|
|
|
const uint64_t selectedEntity = context.GetSelectionManager().GetSelectedEntity();
|
|
|
|
|
if (selectedEntity != 0) {
|
|
|
|
|
target = context.GetSceneManager().GetEntity(selectedEntity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (target != nullptr) {
|
|
|
|
|
m_sceneViewCamera.controller.Focus(target->GetTransform()->GetPosition());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto& roots = context.GetSceneManager().GetRootEntities();
|
|
|
|
|
if (roots.empty()) {
|
|
|
|
|
m_sceneViewCamera.controller.Focus(Math::Vector3::Zero());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Math::Vector3 center = Math::Vector3::Zero();
|
|
|
|
|
uint32_t count = 0;
|
|
|
|
|
for (const Components::GameObject* root : roots) {
|
|
|
|
|
if (root == nullptr) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
center += root->GetTransform()->GetPosition();
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
center /= static_cast<float>(count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sceneViewCamera.controller.Focus(center);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:04:14 +08:00
|
|
|
bool EnsureViewportResources(ViewportEntry& entry) {
|
2026-04-01 21:01:23 +08:00
|
|
|
const ViewportHostResourceReuseQuery reuseQuery =
|
|
|
|
|
BuildViewportRenderTargetsReuseQuery(
|
|
|
|
|
entry.kind,
|
|
|
|
|
entry.renderTargets,
|
|
|
|
|
entry.requestedWidth,
|
|
|
|
|
entry.requestedHeight);
|
2026-04-01 19:47:15 +08:00
|
|
|
if (CanReuseViewportResources(reuseQuery)) {
|
|
|
|
|
return true;
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-01 19:47:15 +08:00
|
|
|
if (entry.requestedWidth == 0 || entry.requestedHeight == 0) {
|
|
|
|
|
return false;
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-01 21:01:23 +08:00
|
|
|
return CreateViewportRenderTargets(
|
|
|
|
|
entry.kind,
|
|
|
|
|
entry.requestedWidth,
|
|
|
|
|
entry.requestedHeight,
|
|
|
|
|
m_device,
|
|
|
|
|
m_backend,
|
|
|
|
|
entry.renderTargets);
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:40:14 +08:00
|
|
|
Rendering::RenderSurface BuildSurface(const ViewportEntry& entry) const {
|
2026-04-01 21:01:23 +08:00
|
|
|
return BuildViewportColorSurface(entry.renderTargets);
|
2026-03-28 17:40:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-01 21:37:15 +08:00
|
|
|
void ApplyViewportRenderFailure(
|
|
|
|
|
ViewportEntry& entry,
|
|
|
|
|
const Rendering::RenderContext& renderContext,
|
|
|
|
|
const ViewportRenderFallbackPolicy& policy) {
|
|
|
|
|
ApplyViewportFailureStatus(entry.statusText, policy);
|
|
|
|
|
if (policy.invalidateObjectIdFrame) {
|
|
|
|
|
InvalidateViewportObjectIdFrame(entry.renderTargets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!policy.shouldClear) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClearViewport(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
policy.clearColor.r,
|
|
|
|
|
policy.clearColor.g,
|
|
|
|
|
policy.clearColor.b,
|
|
|
|
|
policy.clearColor.a);
|
2026-04-01 16:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
void BuildSceneViewportRenderState(
|
2026-03-28 17:04:14 +08:00
|
|
|
ViewportEntry& entry,
|
2026-03-29 15:12:38 +08:00
|
|
|
IEditorContext& context,
|
2026-03-31 22:10:27 +08:00
|
|
|
const Components::Scene& scene,
|
|
|
|
|
SceneViewportRenderState& outState) {
|
2026-04-01 17:33:07 +08:00
|
|
|
(void)scene;
|
2026-03-31 22:10:27 +08:00
|
|
|
outState.overlay = GetSceneViewOverlayData();
|
|
|
|
|
if (!outState.overlay.valid) {
|
2026-03-28 17:04:14 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 17:33:07 +08:00
|
|
|
outState.selectedObjectIds = context.GetSelectionManager().GetSelectedEntities();
|
2026-04-02 15:03:31 +08:00
|
|
|
const SceneViewportBuiltinPostProcessBuildResult builtinPostProcess =
|
|
|
|
|
BuildSceneViewportBuiltinPostProcess(
|
2026-03-31 22:10:27 +08:00
|
|
|
outState.overlay,
|
2026-04-01 17:33:07 +08:00
|
|
|
outState.selectedObjectIds,
|
2026-04-02 15:03:31 +08:00
|
|
|
entry.renderTargets.objectIdShaderView != nullptr,
|
|
|
|
|
kDebugSceneSelectionMask);
|
|
|
|
|
outState.builtinPostProcess = builtinPostProcess.request;
|
|
|
|
|
if (builtinPostProcess.warningStatusText != nullptr) {
|
|
|
|
|
SetViewportStatusIfEmpty(entry.statusText, builtinPostProcess.warningStatusText);
|
|
|
|
|
}
|
2026-03-31 22:10:27 +08:00
|
|
|
}
|
2026-03-28 17:40:14 +08:00
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
bool RenderSceneViewportEntry(
|
|
|
|
|
ViewportEntry& entry,
|
|
|
|
|
IEditorContext& context,
|
|
|
|
|
const Components::Scene* scene,
|
|
|
|
|
const Rendering::RenderContext& renderContext,
|
|
|
|
|
Rendering::RenderSurface& surface) {
|
|
|
|
|
if (!EnsureSceneViewCamera()) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildSceneViewportRenderFailurePolicy(
|
|
|
|
|
SceneViewportRenderFailure::MissingSceneViewCamera));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-28 17:40:14 +08:00
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
ApplySceneViewCameraController();
|
|
|
|
|
entry.statusText.clear();
|
|
|
|
|
if (scene == nullptr) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildSceneViewportRenderFailurePolicy(
|
|
|
|
|
SceneViewportRenderFailure::NoActiveScene));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-29 15:12:38 +08:00
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
SceneViewportRenderState sceneState = {};
|
|
|
|
|
BuildSceneViewportRenderState(entry, context, *scene, sceneState);
|
2026-03-29 15:12:38 +08:00
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
std::vector<Rendering::CameraRenderRequest> requests =
|
|
|
|
|
m_sceneRenderer->BuildRenderRequests(*scene, m_sceneViewCamera.camera, renderContext, surface);
|
|
|
|
|
if (requests.empty()) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildSceneViewportRenderFailurePolicy(
|
|
|
|
|
SceneViewportRenderFailure::SceneRendererFailed));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-29 15:12:38 +08:00
|
|
|
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplySceneViewportRenderRequestSetup(
|
|
|
|
|
entry.renderTargets,
|
2026-04-02 14:49:00 +08:00
|
|
|
&sceneState.builtinPostProcess,
|
2026-04-02 12:47:06 +08:00
|
|
|
nullptr,
|
2026-04-01 21:37:15 +08:00
|
|
|
requests[0]);
|
2026-04-01 22:49:26 +08:00
|
|
|
requests[0].hasClearColorOverride = true;
|
|
|
|
|
requests[0].clearColorOverride = Math::Color(0.27f, 0.27f, 0.27f, 1.0f);
|
2026-04-01 16:44:11 +08:00
|
|
|
|
2026-03-31 22:10:27 +08:00
|
|
|
if (!m_sceneRenderer->Render(requests)) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildSceneViewportRenderFailurePolicy(
|
|
|
|
|
SceneViewportRenderFailure::SceneRendererFailed));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
2026-03-29 15:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-01 21:37:15 +08:00
|
|
|
MarkSceneViewportRenderSuccess(entry.renderTargets, requests[0]);
|
2026-04-02 04:42:35 +08:00
|
|
|
const Core::uint32 pendingAsyncLoads = Resources::ResourceManager::Get().GetAsyncPendingCount();
|
|
|
|
|
if (pendingAsyncLoads > 0) {
|
|
|
|
|
entry.statusText = "Loading scene assets... (" + std::to_string(pendingAsyncLoads) + ")";
|
|
|
|
|
}
|
2026-03-31 22:10:27 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RenderGameViewportEntry(
|
|
|
|
|
ViewportEntry& entry,
|
|
|
|
|
const Components::Scene* scene,
|
|
|
|
|
const Rendering::RenderContext& renderContext,
|
|
|
|
|
const Rendering::RenderSurface& surface) {
|
2026-03-29 15:12:38 +08:00
|
|
|
if (scene == nullptr) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildGameViewportRenderFailurePolicy(
|
|
|
|
|
GameViewportRenderFailure::NoActiveScene));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
2026-03-28 17:40:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 17:04:14 +08:00
|
|
|
const auto cameras = scene->FindObjectsOfType<Components::CameraComponent>();
|
|
|
|
|
if (cameras.empty()) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildGameViewportRenderFailurePolicy(
|
|
|
|
|
GameViewportRenderFailure::NoCameraInScene));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_sceneRenderer->Render(*scene, nullptr, renderContext, surface)) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportRenderFailure(
|
|
|
|
|
entry,
|
|
|
|
|
renderContext,
|
|
|
|
|
BuildGameViewportRenderFailurePolicy(
|
|
|
|
|
GameViewportRenderFailure::SceneRendererFailed));
|
2026-03-31 22:10:27 +08:00
|
|
|
return false;
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-01 21:37:15 +08:00
|
|
|
MarkGameViewportRenderSuccess(entry.renderTargets);
|
2026-03-28 17:04:14 +08:00
|
|
|
entry.statusText.clear();
|
2026-03-31 22:10:27 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderViewportEntry(
|
|
|
|
|
ViewportEntry& entry,
|
|
|
|
|
IEditorContext& context,
|
|
|
|
|
const Components::Scene* scene,
|
|
|
|
|
const Rendering::RenderContext& renderContext) {
|
2026-04-01 21:01:23 +08:00
|
|
|
if (entry.renderTargets.colorView == nullptr || entry.renderTargets.depthView == nullptr) {
|
2026-04-01 21:37:15 +08:00
|
|
|
ApplyViewportFailureStatus(
|
|
|
|
|
entry.statusText,
|
|
|
|
|
BuildViewportRenderTargetUnavailablePolicy());
|
|
|
|
|
InvalidateViewportObjectIdFrame(entry.renderTargets);
|
2026-03-31 22:10:27 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rendering::RenderSurface surface = BuildSurface(entry);
|
|
|
|
|
|
|
|
|
|
if (entry.kind == EditorViewportKind::Scene) {
|
|
|
|
|
RenderSceneViewportEntry(entry, context, scene, renderContext, surface);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderGameViewportEntry(entry, scene, renderContext, surface);
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClearViewport(
|
|
|
|
|
ViewportEntry& entry,
|
|
|
|
|
const Rendering::RenderContext& renderContext,
|
|
|
|
|
float r,
|
|
|
|
|
float g,
|
|
|
|
|
float b,
|
|
|
|
|
float a) {
|
|
|
|
|
RHI::RHICommandList* commandList = renderContext.commandList;
|
|
|
|
|
if (commandList == nullptr) {
|
|
|
|
|
entry.statusText = "Viewport command list is unavailable";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 21:01:23 +08:00
|
|
|
ViewportRenderTargets& targets = entry.renderTargets;
|
2026-03-28 17:04:14 +08:00
|
|
|
const float clearColor[4] = { r, g, b, a };
|
2026-04-01 21:01:23 +08:00
|
|
|
RHI::RHIResourceView* colorView = targets.colorView;
|
2026-03-28 17:04:14 +08:00
|
|
|
commandList->TransitionBarrier(
|
|
|
|
|
colorView,
|
2026-04-01 21:01:23 +08:00
|
|
|
targets.colorState,
|
2026-03-28 17:04:14 +08:00
|
|
|
RHI::ResourceStates::RenderTarget);
|
2026-04-01 21:01:23 +08:00
|
|
|
commandList->SetRenderTargets(1, &colorView, targets.depthView);
|
2026-03-28 17:04:14 +08:00
|
|
|
commandList->ClearRenderTarget(colorView, clearColor);
|
2026-04-01 21:01:23 +08:00
|
|
|
commandList->ClearDepthStencil(targets.depthView, 1.0f, 0);
|
2026-03-28 17:04:14 +08:00
|
|
|
commandList->TransitionBarrier(
|
|
|
|
|
colorView,
|
|
|
|
|
RHI::ResourceStates::RenderTarget,
|
|
|
|
|
RHI::ResourceStates::PixelShaderResource);
|
2026-04-01 21:01:23 +08:00
|
|
|
targets.colorState = RHI::ResourceStates::PixelShaderResource;
|
|
|
|
|
targets.hasValidObjectIdFrame = false;
|
2026-04-01 16:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TryPickSceneViewEntityWithObjectId(
|
|
|
|
|
ViewportEntry& entry,
|
|
|
|
|
const ImVec2& viewportSize,
|
|
|
|
|
const ImVec2& viewportMousePosition,
|
|
|
|
|
uint64_t& outEntityId) {
|
2026-04-01 20:24:17 +08:00
|
|
|
if (m_device == nullptr) {
|
|
|
|
|
outEntityId = 0;
|
2026-04-01 16:44:11 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 20:24:17 +08:00
|
|
|
ViewportObjectIdPickContext pickContext = {};
|
|
|
|
|
pickContext.commandQueue = m_sceneViewLastRenderContext.commandQueue;
|
2026-04-01 21:01:23 +08:00
|
|
|
pickContext.texture = entry.renderTargets.objectIdTexture;
|
|
|
|
|
pickContext.textureState = entry.renderTargets.objectIdState;
|
|
|
|
|
pickContext.textureWidth = entry.renderTargets.width;
|
|
|
|
|
pickContext.textureHeight = entry.renderTargets.height;
|
|
|
|
|
pickContext.hasValidFrame = entry.renderTargets.hasValidObjectIdFrame;
|
2026-04-01 20:24:17 +08:00
|
|
|
pickContext.viewportSize = viewportSize;
|
|
|
|
|
pickContext.viewportMousePosition = viewportMousePosition;
|
|
|
|
|
|
|
|
|
|
return TryPickViewportObjectIdEntity(
|
|
|
|
|
pickContext,
|
|
|
|
|
[this](const ViewportObjectIdReadbackRequest& request, std::array<uint8_t, 4>& outRgba) {
|
|
|
|
|
return m_device != nullptr &&
|
|
|
|
|
m_device->ReadTexturePixelRGBA8(
|
|
|
|
|
request.commandQueue,
|
|
|
|
|
request.texture,
|
|
|
|
|
request.textureState,
|
|
|
|
|
request.pixelX,
|
|
|
|
|
request.pixelY,
|
|
|
|
|
outRgba);
|
|
|
|
|
},
|
|
|
|
|
outEntityId);
|
2026-03-28 17:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UI::ImGuiBackendBridge* m_backend = nullptr;
|
2026-04-01 19:17:44 +08:00
|
|
|
RHI::RHIDevice* m_device = nullptr;
|
2026-03-28 17:04:14 +08:00
|
|
|
std::unique_ptr<Rendering::SceneRenderer> m_sceneRenderer;
|
2026-04-01 16:44:11 +08:00
|
|
|
Rendering::RenderContext m_sceneViewLastRenderContext = {};
|
2026-03-28 17:04:14 +08:00
|
|
|
std::array<ViewportEntry, 2> m_entries = {};
|
2026-03-28 17:40:14 +08:00
|
|
|
SceneViewCameraState m_sceneViewCamera;
|
2026-03-28 17:04:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Editor
|
|
|
|
|
} // namespace XCEngine
|