Add renderer phase A textured scene path
This commit is contained in:
33
engine/include/XCEngine/Components/MeshFilterComponent.h
Normal file
33
engine/include/XCEngine/Components/MeshFilterComponent.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Components/Component.h>
|
||||
#include <XCEngine/Core/Asset/ResourceHandle.h>
|
||||
#include <XCEngine/Resources/Mesh/Mesh.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
|
||||
class MeshFilterComponent : public Component {
|
||||
public:
|
||||
std::string GetName() const override { return "MeshFilter"; }
|
||||
|
||||
Resources::Mesh* GetMesh() const { return m_mesh.Get(); }
|
||||
const Resources::ResourceHandle<Resources::Mesh>& GetMeshHandle() const { return m_mesh; }
|
||||
const std::string& GetMeshPath() const { return m_meshPath; }
|
||||
|
||||
void SetMesh(const Resources::ResourceHandle<Resources::Mesh>& mesh);
|
||||
void SetMesh(Resources::Mesh* mesh);
|
||||
void ClearMesh();
|
||||
|
||||
void Serialize(std::ostream& os) const override;
|
||||
void Deserialize(std::istream& is) override;
|
||||
|
||||
private:
|
||||
Resources::ResourceHandle<Resources::Mesh> m_mesh;
|
||||
std::string m_meshPath;
|
||||
};
|
||||
|
||||
} // namespace Components
|
||||
} // namespace XCEngine
|
||||
52
engine/include/XCEngine/Components/MeshRendererComponent.h
Normal file
52
engine/include/XCEngine/Components/MeshRendererComponent.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Components/Component.h>
|
||||
#include <XCEngine/Core/Asset/ResourceHandle.h>
|
||||
#include <XCEngine/Resources/Material/Material.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
|
||||
class MeshRendererComponent : public Component {
|
||||
public:
|
||||
std::string GetName() const override { return "MeshRenderer"; }
|
||||
|
||||
size_t GetMaterialCount() const { return m_materials.size(); }
|
||||
Resources::Material* GetMaterial(size_t index) const;
|
||||
const Resources::ResourceHandle<Resources::Material>& GetMaterialHandle(size_t index) const;
|
||||
const std::vector<std::string>& GetMaterialPaths() const { return m_materialPaths; }
|
||||
|
||||
void SetMaterial(size_t index, const Resources::ResourceHandle<Resources::Material>& material);
|
||||
void SetMaterial(size_t index, Resources::Material* material);
|
||||
void SetMaterials(const std::vector<Resources::ResourceHandle<Resources::Material>>& materials);
|
||||
void ClearMaterials();
|
||||
|
||||
bool GetCastShadows() const { return m_castShadows; }
|
||||
void SetCastShadows(bool value) { m_castShadows = value; }
|
||||
|
||||
bool GetReceiveShadows() const { return m_receiveShadows; }
|
||||
void SetReceiveShadows(bool value) { m_receiveShadows = value; }
|
||||
|
||||
uint32_t GetRenderLayer() const { return m_renderLayer; }
|
||||
void SetRenderLayer(uint32_t value) { m_renderLayer = value; }
|
||||
|
||||
void Serialize(std::ostream& os) const override;
|
||||
void Deserialize(std::istream& is) override;
|
||||
|
||||
private:
|
||||
void EnsureMaterialSlot(size_t index);
|
||||
static std::string MaterialPathFromHandle(const Resources::ResourceHandle<Resources::Material>& material);
|
||||
|
||||
std::vector<Resources::ResourceHandle<Resources::Material>> m_materials;
|
||||
std::vector<std::string> m_materialPaths;
|
||||
bool m_castShadows = true;
|
||||
bool m_receiveShadows = true;
|
||||
uint32_t m_renderLayer = 0;
|
||||
};
|
||||
|
||||
} // namespace Components
|
||||
} // namespace XCEngine
|
||||
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Rendering/RenderPipeline.h>
|
||||
#include <XCEngine/Rendering/RenderResourceCache.h>
|
||||
|
||||
#include <XCEngine/RHI/RHIDescriptorPool.h>
|
||||
#include <XCEngine/RHI/RHIDescriptorSet.h>
|
||||
#include <XCEngine/RHI/RHIPipelineLayout.h>
|
||||
#include <XCEngine/RHI/RHIPipelineState.h>
|
||||
#include <XCEngine/RHI/RHIResourceView.h>
|
||||
#include <XCEngine/RHI/RHISampler.h>
|
||||
#include <XCEngine/RHI/RHITexture.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
class Material;
|
||||
class Texture;
|
||||
} // namespace Resources
|
||||
|
||||
namespace Rendering {
|
||||
class RenderSurface;
|
||||
|
||||
namespace Pipelines {
|
||||
|
||||
class BuiltinForwardPipeline : public RenderPipeline {
|
||||
public:
|
||||
BuiltinForwardPipeline() = default;
|
||||
~BuiltinForwardPipeline() override;
|
||||
|
||||
bool Initialize(const RenderContext& context) override;
|
||||
void Shutdown() override;
|
||||
bool Render(
|
||||
const RenderContext& context,
|
||||
const RenderSurface& surface,
|
||||
const RenderSceneData& sceneData) override;
|
||||
|
||||
private:
|
||||
struct PerObjectConstants {
|
||||
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
|
||||
Math::Matrix4x4 view = Math::Matrix4x4::Identity();
|
||||
Math::Matrix4x4 model = Math::Matrix4x4::Identity();
|
||||
};
|
||||
|
||||
bool EnsureInitialized(const RenderContext& context);
|
||||
bool CreatePipelineResources(const RenderContext& context);
|
||||
void DestroyPipelineResources();
|
||||
|
||||
const Resources::Material* ResolveMaterial(const VisibleRenderObject& visibleObject, uint32_t materialIndex) const;
|
||||
const Resources::Texture* ResolveTexture(const Resources::Material* material) const;
|
||||
RHI::RHIResourceView* ResolveTextureView(const VisibleRenderObject& visibleObject, uint32_t materialIndex);
|
||||
bool DrawVisibleObject(
|
||||
const RenderContext& context,
|
||||
const RenderSceneData& sceneData,
|
||||
const VisibleRenderObject& visibleObject);
|
||||
|
||||
RHI::RHIDevice* m_device = nullptr;
|
||||
RHI::RHIType m_backendType = RHI::RHIType::D3D12;
|
||||
bool m_initialized = false;
|
||||
|
||||
RenderResourceCache m_resourceCache;
|
||||
|
||||
RHI::RHIDescriptorPool* m_constantPool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_constantSet = nullptr;
|
||||
RHI::RHIDescriptorPool* m_texturePool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_textureSet = nullptr;
|
||||
RHI::RHIDescriptorPool* m_samplerPool = nullptr;
|
||||
RHI::RHIDescriptorSet* m_samplerSet = nullptr;
|
||||
RHI::RHIPipelineLayout* m_pipelineLayout = nullptr;
|
||||
RHI::RHIPipelineState* m_pipelineState = nullptr;
|
||||
RHI::RHISampler* m_sampler = nullptr;
|
||||
RHI::RHITexture* m_fallbackTexture = nullptr;
|
||||
RHI::RHIResourceView* m_fallbackTextureView = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Pipelines
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
23
engine/include/XCEngine/Rendering/RenderCameraData.h
Normal file
23
engine/include/XCEngine/Rendering/RenderCameraData.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Core/Math/Color.h>
|
||||
#include <XCEngine/Core/Math/Matrix4.h>
|
||||
#include <XCEngine/Core/Math/Vector3.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
struct RenderCameraData {
|
||||
Math::Matrix4x4 view = Math::Matrix4x4::Identity();
|
||||
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
|
||||
Math::Matrix4x4 viewProjection = Math::Matrix4x4::Identity();
|
||||
Math::Vector3 worldPosition = Math::Vector3::Zero();
|
||||
Math::Color clearColor = Math::Color::Black();
|
||||
uint32_t viewportWidth = 0;
|
||||
uint32_t viewportHeight = 0;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
26
engine/include/XCEngine/Rendering/RenderContext.h
Normal file
26
engine/include/XCEngine/Rendering/RenderContext.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/RHI/RHIEnums.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
class RHICommandList;
|
||||
class RHICommandQueue;
|
||||
class RHIDevice;
|
||||
} // namespace RHI
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
struct RenderContext {
|
||||
RHI::RHIDevice* device = nullptr;
|
||||
RHI::RHICommandList* commandList = nullptr;
|
||||
RHI::RHICommandQueue* commandQueue = nullptr;
|
||||
RHI::RHIType backendType = RHI::RHIType::D3D12;
|
||||
|
||||
bool IsValid() const {
|
||||
return device != nullptr && commandList != nullptr && commandQueue != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
24
engine/include/XCEngine/Rendering/RenderPipeline.h
Normal file
24
engine/include/XCEngine/Rendering/RenderPipeline.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Rendering/RenderContext.h>
|
||||
#include <XCEngine/Rendering/RenderSceneExtractor.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
class RenderSurface;
|
||||
|
||||
class RenderPipeline {
|
||||
public:
|
||||
virtual ~RenderPipeline() = default;
|
||||
|
||||
virtual bool Initialize(const RenderContext& context) = 0;
|
||||
virtual void Shutdown() = 0;
|
||||
virtual bool Render(
|
||||
const RenderContext& context,
|
||||
const RenderSurface& surface,
|
||||
const RenderSceneData& sceneData) = 0;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
18
engine/include/XCEngine/Rendering/RenderPipelineAsset.h
Normal file
18
engine/include/XCEngine/Rendering/RenderPipelineAsset.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
class RenderPipeline;
|
||||
|
||||
class RenderPipelineAsset {
|
||||
public:
|
||||
virtual ~RenderPipelineAsset() = default;
|
||||
|
||||
virtual std::unique_ptr<RenderPipeline> CreatePipeline() const = 0;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
51
engine/include/XCEngine/Rendering/RenderResourceCache.h
Normal file
51
engine/include/XCEngine/Rendering/RenderResourceCache.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/RHI/RHIBuffer.h>
|
||||
#include <XCEngine/RHI/RHIDevice.h>
|
||||
#include <XCEngine/RHI/RHIResourceView.h>
|
||||
#include <XCEngine/RHI/RHITexture.h>
|
||||
#include <XCEngine/Resources/Mesh/Mesh.h>
|
||||
#include <XCEngine/Resources/Texture/Texture.h>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
class RenderResourceCache {
|
||||
public:
|
||||
struct CachedMesh {
|
||||
RHI::RHIBuffer* vertexBuffer = nullptr;
|
||||
RHI::RHIResourceView* vertexBufferView = nullptr;
|
||||
RHI::RHIBuffer* indexBuffer = nullptr;
|
||||
RHI::RHIResourceView* indexBufferView = nullptr;
|
||||
uint32_t vertexCount = 0;
|
||||
uint32_t indexCount = 0;
|
||||
uint32_t vertexStride = 0;
|
||||
bool uses32BitIndices = false;
|
||||
};
|
||||
|
||||
struct CachedTexture {
|
||||
RHI::RHITexture* texture = nullptr;
|
||||
RHI::RHIResourceView* shaderResourceView = nullptr;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
};
|
||||
|
||||
~RenderResourceCache();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
const CachedMesh* GetOrCreateMesh(RHI::RHIDevice* device, const Resources::Mesh* mesh);
|
||||
const CachedTexture* GetOrCreateTexture(RHI::RHIDevice* device, const Resources::Texture* texture);
|
||||
|
||||
private:
|
||||
bool UploadMesh(RHI::RHIDevice* device, const Resources::Mesh* mesh, CachedMesh& cachedMesh);
|
||||
bool UploadTexture(RHI::RHIDevice* device, const Resources::Texture* texture, CachedTexture& cachedTexture);
|
||||
|
||||
std::unordered_map<const Resources::Mesh*, CachedMesh> m_meshCache;
|
||||
std::unordered_map<const Resources::Texture*, CachedTexture> m_textureCache;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
48
engine/include/XCEngine/Rendering/RenderSceneExtractor.h
Normal file
48
engine/include/XCEngine/Rendering/RenderSceneExtractor.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Rendering/RenderCameraData.h>
|
||||
#include <XCEngine/Rendering/VisibleRenderObject.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
class CameraComponent;
|
||||
class GameObject;
|
||||
class Scene;
|
||||
} // namespace Components
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
struct RenderSceneData {
|
||||
Components::CameraComponent* camera = nullptr;
|
||||
RenderCameraData cameraData;
|
||||
std::vector<VisibleRenderObject> visibleObjects;
|
||||
|
||||
bool HasCamera() const { return camera != nullptr; }
|
||||
};
|
||||
|
||||
class RenderSceneExtractor {
|
||||
public:
|
||||
RenderSceneData Extract(
|
||||
const Components::Scene& scene,
|
||||
Components::CameraComponent* overrideCamera,
|
||||
uint32_t viewportWidth,
|
||||
uint32_t viewportHeight) const;
|
||||
|
||||
private:
|
||||
Components::CameraComponent* SelectCamera(
|
||||
const Components::Scene& scene,
|
||||
Components::CameraComponent* overrideCamera) const;
|
||||
RenderCameraData BuildCameraData(
|
||||
const Components::CameraComponent& camera,
|
||||
uint32_t viewportWidth,
|
||||
uint32_t viewportHeight) const;
|
||||
void ExtractVisibleObjects(
|
||||
Components::GameObject* gameObject,
|
||||
std::vector<VisibleRenderObject>& visibleObjects) const;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
59
engine/include/XCEngine/Rendering/RenderSurface.h
Normal file
59
engine/include/XCEngine/Rendering/RenderSurface.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Core/Math/Color.h>
|
||||
#include <XCEngine/RHI/RHIEnums.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
class RHIResourceView;
|
||||
} // namespace RHI
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
class RenderSurface {
|
||||
public:
|
||||
RenderSurface() = default;
|
||||
RenderSurface(uint32_t width, uint32_t height);
|
||||
|
||||
uint32_t GetWidth() const { return m_width; }
|
||||
uint32_t GetHeight() const { return m_height; }
|
||||
void SetSize(uint32_t width, uint32_t height);
|
||||
|
||||
void SetColorAttachment(RHI::RHIResourceView* colorAttachment);
|
||||
void SetColorAttachments(const std::vector<RHI::RHIResourceView*>& colorAttachments);
|
||||
const std::vector<RHI::RHIResourceView*>& GetColorAttachments() const { return m_colorAttachments; }
|
||||
|
||||
void SetDepthAttachment(RHI::RHIResourceView* depthAttachment) { m_depthAttachment = depthAttachment; }
|
||||
RHI::RHIResourceView* GetDepthAttachment() const { return m_depthAttachment; }
|
||||
|
||||
void SetClearColorOverride(const Math::Color& clearColor);
|
||||
void ClearClearColorOverride();
|
||||
bool HasClearColorOverride() const { return m_hasClearColorOverride; }
|
||||
const Math::Color& GetClearColorOverride() const { return m_clearColorOverride; }
|
||||
|
||||
void SetAutoTransitionEnabled(bool enabled) { m_autoTransition = enabled; }
|
||||
bool IsAutoTransitionEnabled() const { return m_autoTransition; }
|
||||
|
||||
void SetColorStateBefore(RHI::ResourceStates state) { m_colorStateBefore = state; }
|
||||
RHI::ResourceStates GetColorStateBefore() const { return m_colorStateBefore; }
|
||||
|
||||
void SetColorStateAfter(RHI::ResourceStates state) { m_colorStateAfter = state; }
|
||||
RHI::ResourceStates GetColorStateAfter() const { return m_colorStateAfter; }
|
||||
|
||||
private:
|
||||
uint32_t m_width = 0;
|
||||
uint32_t m_height = 0;
|
||||
std::vector<RHI::RHIResourceView*> m_colorAttachments;
|
||||
RHI::RHIResourceView* m_depthAttachment = nullptr;
|
||||
bool m_hasClearColorOverride = false;
|
||||
Math::Color m_clearColorOverride = Math::Color::Black();
|
||||
bool m_autoTransition = true;
|
||||
RHI::ResourceStates m_colorStateBefore = RHI::ResourceStates::Present;
|
||||
RHI::ResourceStates m_colorStateAfter = RHI::ResourceStates::Present;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
36
engine/include/XCEngine/Rendering/SceneRenderer.h
Normal file
36
engine/include/XCEngine/Rendering/SceneRenderer.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Rendering/RenderPipeline.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
class CameraComponent;
|
||||
class Scene;
|
||||
} // namespace Components
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
class SceneRenderer {
|
||||
public:
|
||||
SceneRenderer();
|
||||
explicit SceneRenderer(std::unique_ptr<RenderPipeline> pipeline);
|
||||
~SceneRenderer();
|
||||
|
||||
void SetPipeline(std::unique_ptr<RenderPipeline> pipeline);
|
||||
RenderPipeline* GetPipeline() const { return m_pipeline.get(); }
|
||||
|
||||
bool Render(
|
||||
const Components::Scene& scene,
|
||||
Components::CameraComponent* overrideCamera,
|
||||
const RenderContext& context,
|
||||
const RenderSurface& surface);
|
||||
|
||||
private:
|
||||
RenderSceneExtractor m_sceneExtractor;
|
||||
std::unique_ptr<RenderPipeline> m_pipeline;
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
24
engine/include/XCEngine/Rendering/VisibleRenderObject.h
Normal file
24
engine/include/XCEngine/Rendering/VisibleRenderObject.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Core/Math/Matrix4.h>
|
||||
#include <XCEngine/Resources/Mesh/Mesh.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
class GameObject;
|
||||
class MeshFilterComponent;
|
||||
class MeshRendererComponent;
|
||||
} // namespace Components
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
struct VisibleRenderObject {
|
||||
Components::GameObject* gameObject = nullptr;
|
||||
Components::MeshFilterComponent* meshFilter = nullptr;
|
||||
Components::MeshRendererComponent* meshRenderer = nullptr;
|
||||
Resources::Mesh* mesh = nullptr;
|
||||
Math::Matrix4x4 localToWorld = Math::Matrix4x4::Identity();
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
64
engine/src/Components/MeshFilterComponent.cpp
Normal file
64
engine/src/Components/MeshFilterComponent.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
|
||||
#include "Core/Asset/ResourceManager.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string ToStdString(const Containers::String& value) {
|
||||
return std::string(value.CStr());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void MeshFilterComponent::SetMesh(const Resources::ResourceHandle<Resources::Mesh>& mesh) {
|
||||
m_mesh = mesh;
|
||||
m_meshPath = mesh.Get() != nullptr ? ToStdString(mesh->GetPath()) : std::string();
|
||||
}
|
||||
|
||||
void MeshFilterComponent::SetMesh(Resources::Mesh* mesh) {
|
||||
SetMesh(Resources::ResourceHandle<Resources::Mesh>(mesh));
|
||||
}
|
||||
|
||||
void MeshFilterComponent::ClearMesh() {
|
||||
m_mesh.Reset();
|
||||
m_meshPath.clear();
|
||||
}
|
||||
|
||||
void MeshFilterComponent::Serialize(std::ostream& os) const {
|
||||
os << "mesh=" << m_meshPath << ";";
|
||||
}
|
||||
|
||||
void MeshFilterComponent::Deserialize(std::istream& is) {
|
||||
m_mesh.Reset();
|
||||
m_meshPath.clear();
|
||||
|
||||
std::string token;
|
||||
while (std::getline(is, token, ';')) {
|
||||
if (token.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const size_t eqPos = token.find('=');
|
||||
if (eqPos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::string key = token.substr(0, eqPos);
|
||||
const std::string value = token.substr(eqPos + 1);
|
||||
|
||||
if (key == "mesh") {
|
||||
m_meshPath = value;
|
||||
if (!m_meshPath.empty()) {
|
||||
m_mesh = Resources::ResourceManager::Get().Load<Resources::Mesh>(m_meshPath.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Components
|
||||
} // namespace XCEngine
|
||||
129
engine/src/Components/MeshRendererComponent.cpp
Normal file
129
engine/src/Components/MeshRendererComponent.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
|
||||
#include "Core/Asset/ResourceManager.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string ToStdString(const Containers::String& value) {
|
||||
return std::string(value.CStr());
|
||||
}
|
||||
|
||||
std::vector<std::string> SplitMaterialPaths(const std::string& value) {
|
||||
std::vector<std::string> paths;
|
||||
std::stringstream stream(value);
|
||||
std::string item;
|
||||
while (std::getline(stream, item, '|')) {
|
||||
paths.push_back(item);
|
||||
}
|
||||
|
||||
if (value.empty()) {
|
||||
paths.clear();
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Resources::Material* MeshRendererComponent::GetMaterial(size_t index) const {
|
||||
return index < m_materials.size() ? m_materials[index].Get() : nullptr;
|
||||
}
|
||||
|
||||
const Resources::ResourceHandle<Resources::Material>& MeshRendererComponent::GetMaterialHandle(size_t index) const {
|
||||
static const Resources::ResourceHandle<Resources::Material> kNullHandle;
|
||||
return index < m_materials.size() ? m_materials[index] : kNullHandle;
|
||||
}
|
||||
|
||||
void MeshRendererComponent::SetMaterial(size_t index, const Resources::ResourceHandle<Resources::Material>& material) {
|
||||
EnsureMaterialSlot(index);
|
||||
m_materials[index] = material;
|
||||
m_materialPaths[index] = MaterialPathFromHandle(material);
|
||||
}
|
||||
|
||||
void MeshRendererComponent::SetMaterial(size_t index, Resources::Material* material) {
|
||||
SetMaterial(index, Resources::ResourceHandle<Resources::Material>(material));
|
||||
}
|
||||
|
||||
void MeshRendererComponent::SetMaterials(const std::vector<Resources::ResourceHandle<Resources::Material>>& materials) {
|
||||
m_materials = materials;
|
||||
m_materialPaths.resize(materials.size());
|
||||
for (size_t i = 0; i < materials.size(); ++i) {
|
||||
m_materialPaths[i] = MaterialPathFromHandle(materials[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void MeshRendererComponent::ClearMaterials() {
|
||||
m_materials.clear();
|
||||
m_materialPaths.clear();
|
||||
}
|
||||
|
||||
void MeshRendererComponent::Serialize(std::ostream& os) const {
|
||||
os << "materials=";
|
||||
for (size_t i = 0; i < m_materialPaths.size(); ++i) {
|
||||
if (i > 0) {
|
||||
os << "|";
|
||||
}
|
||||
os << m_materialPaths[i];
|
||||
}
|
||||
os << ";";
|
||||
os << "castShadows=" << (m_castShadows ? 1 : 0) << ";";
|
||||
os << "receiveShadows=" << (m_receiveShadows ? 1 : 0) << ";";
|
||||
os << "renderLayer=" << m_renderLayer << ";";
|
||||
}
|
||||
|
||||
void MeshRendererComponent::Deserialize(std::istream& is) {
|
||||
ClearMaterials();
|
||||
m_castShadows = true;
|
||||
m_receiveShadows = true;
|
||||
m_renderLayer = 0;
|
||||
|
||||
std::string token;
|
||||
while (std::getline(is, token, ';')) {
|
||||
if (token.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const size_t eqPos = token.find('=');
|
||||
if (eqPos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::string key = token.substr(0, eqPos);
|
||||
const std::string value = token.substr(eqPos + 1);
|
||||
|
||||
if (key == "materials") {
|
||||
m_materialPaths = SplitMaterialPaths(value);
|
||||
m_materials.resize(m_materialPaths.size());
|
||||
for (size_t i = 0; i < m_materialPaths.size(); ++i) {
|
||||
if (!m_materialPaths[i].empty()) {
|
||||
m_materials[i] = Resources::ResourceManager::Get().Load<Resources::Material>(m_materialPaths[i].c_str());
|
||||
}
|
||||
}
|
||||
} else if (key == "castShadows") {
|
||||
m_castShadows = (std::stoi(value) != 0);
|
||||
} else if (key == "receiveShadows") {
|
||||
m_receiveShadows = (std::stoi(value) != 0);
|
||||
} else if (key == "renderLayer") {
|
||||
m_renderLayer = static_cast<uint32_t>(std::stoul(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MeshRendererComponent::EnsureMaterialSlot(size_t index) {
|
||||
if (index >= m_materials.size()) {
|
||||
m_materials.resize(index + 1);
|
||||
m_materialPaths.resize(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
std::string MeshRendererComponent::MaterialPathFromHandle(const Resources::ResourceHandle<Resources::Material>& material) {
|
||||
return material.Get() != nullptr ? ToStdString(material->GetPath()) : std::string();
|
||||
}
|
||||
|
||||
} // namespace Components
|
||||
} // namespace XCEngine
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLPipelineState.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLPipelineLayout.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLShader.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLTexture.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLFramebuffer.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLRenderPass.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLDescriptorSet.h"
|
||||
@@ -567,9 +568,14 @@ void OpenGLCommandList::SetUniformMat4(const char* name, const float* value) {
|
||||
}
|
||||
|
||||
void OpenGLCommandList::TransitionBarrier(RHIResourceView* resource, ResourceStates stateBefore, ResourceStates stateAfter) {
|
||||
(void)resource;
|
||||
(void)stateBefore;
|
||||
(void)stateAfter;
|
||||
if (resource != nullptr && resource->IsValid()) {
|
||||
OpenGLResourceView* view = static_cast<OpenGLResourceView*>(resource);
|
||||
OpenGLTexture* texture = const_cast<OpenGLTexture*>(view->GetTextureResource());
|
||||
if (texture != nullptr) {
|
||||
texture->SetState(stateAfter);
|
||||
}
|
||||
}
|
||||
glMemoryBarrier(GL_ALL_BARRIER_BITS);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,7 @@ bool OpenGLScreenshot::Capture(OpenGLDevice& device, OpenGLSwapChain& swapChain,
|
||||
}
|
||||
|
||||
bool OpenGLScreenshot::Capture(OpenGLDevice& device, OpenGLSwapChain& swapChain, const char* filename, int width, int height) {
|
||||
(void)device;
|
||||
(void)swapChain;
|
||||
device.MakeContextCurrent();
|
||||
|
||||
unsigned char* pixels = (unsigned char*)malloc(width * height * 3);
|
||||
if (!pixels) {
|
||||
@@ -31,12 +30,32 @@ bool OpenGLScreenshot::Capture(OpenGLDevice& device, OpenGLSwapChain& swapChain,
|
||||
}
|
||||
|
||||
glFinish();
|
||||
glReadBuffer(GL_BACK);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
|
||||
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
|
||||
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
|
||||
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
OpenGLTexture* backBufferTexture = static_cast<OpenGLTexture*>(swapChain.GetCurrentBackBuffer());
|
||||
if (backBufferTexture != nullptr &&
|
||||
backBufferTexture->GetID() != 0 &&
|
||||
backBufferTexture->GetState() != ResourceStates::Common) {
|
||||
GLuint readFramebuffer = 0;
|
||||
glGenFramebuffers(1, &readFramebuffer);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, readFramebuffer);
|
||||
glFramebufferTexture2D(
|
||||
GL_READ_FRAMEBUFFER,
|
||||
GL_COLOR_ATTACHMENT0,
|
||||
GL_TEXTURE_2D,
|
||||
backBufferTexture->GetID(),
|
||||
0);
|
||||
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
||||
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
||||
glDeleteFramebuffers(1, &readFramebuffer);
|
||||
} else {
|
||||
glReadBuffer(GL_BACK);
|
||||
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
|
||||
FILE* f = fopen(filename, "wb");
|
||||
if (!f) {
|
||||
@@ -68,4 +87,4 @@ bool OpenGLScreenshot::Capture(RHIDevice* device, RHISwapChain* swapChain, const
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
|
||||
@@ -51,7 +51,38 @@ void OpenGLSwapChain::SwapBuffers() {
|
||||
}
|
||||
|
||||
void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) {
|
||||
(void)syncInterval;
|
||||
(void)flags;
|
||||
if (m_device) {
|
||||
m_device->MakeContextCurrent();
|
||||
|
||||
if (m_backBufferTexture != nullptr &&
|
||||
m_backBufferTexture->GetID() != 0 &&
|
||||
m_backBufferTexture->GetState() != ResourceStates::Common) {
|
||||
GLuint readFramebuffer = 0;
|
||||
glGenFramebuffers(1, &readFramebuffer);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, readFramebuffer);
|
||||
glFramebufferTexture2D(
|
||||
GL_READ_FRAMEBUFFER,
|
||||
GL_COLOR_ATTACHMENT0,
|
||||
GL_TEXTURE_2D,
|
||||
m_backBufferTexture->GetID(),
|
||||
0);
|
||||
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
||||
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glDrawBuffer(GL_BACK);
|
||||
glBlitFramebuffer(
|
||||
0, 0, m_width, m_height,
|
||||
0, 0, m_width, m_height,
|
||||
GL_COLOR_BUFFER_BIT,
|
||||
GL_NEAREST);
|
||||
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glDeleteFramebuffers(1, &readFramebuffer);
|
||||
}
|
||||
|
||||
::SwapBuffers(m_device->GetPresentationDC());
|
||||
}
|
||||
}
|
||||
@@ -70,8 +101,8 @@ RHITexture* OpenGLSwapChain::GetCurrentBackBuffer() {
|
||||
}
|
||||
|
||||
void* OpenGLSwapChain::GetNativeHandle() {
|
||||
return nullptr;
|
||||
return m_hwnd;
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
|
||||
625
engine/src/Rendering/Pipelines/BuiltinForwardPipeline.cpp
Normal file
625
engine/src/Rendering/Pipelines/BuiltinForwardPipeline.cpp
Normal file
@@ -0,0 +1,625 @@
|
||||
#include "Rendering/Pipelines/BuiltinForwardPipeline.h"
|
||||
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
#include "RHI/RHICommandList.h"
|
||||
#include "Rendering/RenderSurface.h"
|
||||
#include "Resources/Material/Material.h"
|
||||
#include "Resources/Texture/Texture.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
namespace Pipelines {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t kDescriptorFirstSet = 1;
|
||||
constexpr uint32_t kDescriptorSetCount = 4;
|
||||
|
||||
const char kBuiltinForwardHlsl[] = R"(
|
||||
Texture2D gBaseColorTexture : register(t1);
|
||||
SamplerState gLinearSampler : register(s1);
|
||||
|
||||
cbuffer PerObjectConstants : register(b1) {
|
||||
float4x4 gProjectionMatrix;
|
||||
float4x4 gViewMatrix;
|
||||
float4x4 gModelMatrix;
|
||||
};
|
||||
|
||||
struct VSInput {
|
||||
float3 position : POSITION;
|
||||
float2 texcoord : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PSInput {
|
||||
float4 position : SV_POSITION;
|
||||
float2 texcoord : TEXCOORD0;
|
||||
};
|
||||
|
||||
PSInput MainVS(VSInput input) {
|
||||
PSInput output;
|
||||
float4 positionWS = mul(gModelMatrix, float4(input.position, 1.0f));
|
||||
float4 positionVS = mul(gViewMatrix, positionWS);
|
||||
output.position = mul(gProjectionMatrix, positionVS);
|
||||
output.texcoord = input.texcoord;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 MainPS(PSInput input) : SV_TARGET {
|
||||
return gBaseColorTexture.Sample(gLinearSampler, input.texcoord);
|
||||
}
|
||||
)";
|
||||
|
||||
const char kBuiltinForwardVertexShader[] = R"(#version 430
|
||||
layout(location = 0) in vec3 aPosition;
|
||||
layout(location = 1) in vec2 aTexCoord;
|
||||
|
||||
layout(std140, binding = 1) uniform PerObjectConstants {
|
||||
mat4 gProjectionMatrix;
|
||||
mat4 gViewMatrix;
|
||||
mat4 gModelMatrix;
|
||||
};
|
||||
|
||||
out vec2 vTexCoord;
|
||||
|
||||
void main() {
|
||||
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
|
||||
vec4 positionVS = gViewMatrix * positionWS;
|
||||
gl_Position = gProjectionMatrix * positionVS;
|
||||
vTexCoord = aTexCoord;
|
||||
}
|
||||
)";
|
||||
|
||||
const char kBuiltinForwardFragmentShader[] = R"(#version 430
|
||||
layout(binding = 1) uniform sampler2D uBaseColorTexture;
|
||||
|
||||
in vec2 vTexCoord;
|
||||
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
|
||||
void main() {
|
||||
fragColor = texture(uBaseColorTexture, vTexCoord);
|
||||
}
|
||||
)";
|
||||
|
||||
RHI::GraphicsPipelineDesc CreatePipelineDesc(RHI::RHIType backendType, RHI::RHIPipelineLayout* pipelineLayout) {
|
||||
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);
|
||||
pipelineDesc.depthStencilFormat = static_cast<uint32_t>(RHI::Format::Unknown);
|
||||
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.depthStencilState.depthTestEnable = false;
|
||||
pipelineDesc.depthStencilState.depthWriteEnable = false;
|
||||
pipelineDesc.depthStencilState.stencilEnable = false;
|
||||
|
||||
RHI::InputElementDesc position = {};
|
||||
position.semanticName = "POSITION";
|
||||
position.semanticIndex = 0;
|
||||
position.format = static_cast<uint32_t>(RHI::Format::R32G32B32A32_Float);
|
||||
position.inputSlot = 0;
|
||||
position.alignedByteOffset = 0;
|
||||
pipelineDesc.inputLayout.elements.push_back(position);
|
||||
|
||||
RHI::InputElementDesc texcoord = {};
|
||||
texcoord.semanticName = "TEXCOORD";
|
||||
texcoord.semanticIndex = 0;
|
||||
texcoord.format = static_cast<uint32_t>(RHI::Format::R32G32_Float);
|
||||
texcoord.inputSlot = 0;
|
||||
texcoord.alignedByteOffset = static_cast<uint32_t>(offsetof(Resources::StaticMeshVertex, uv0));
|
||||
pipelineDesc.inputLayout.elements.push_back(texcoord);
|
||||
|
||||
if (backendType == RHI::RHIType::D3D12) {
|
||||
pipelineDesc.vertexShader.source.assign(
|
||||
kBuiltinForwardHlsl,
|
||||
kBuiltinForwardHlsl + std::strlen(kBuiltinForwardHlsl));
|
||||
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
|
||||
pipelineDesc.vertexShader.entryPoint = L"MainVS";
|
||||
pipelineDesc.vertexShader.profile = L"vs_5_0";
|
||||
|
||||
pipelineDesc.fragmentShader.source.assign(
|
||||
kBuiltinForwardHlsl,
|
||||
kBuiltinForwardHlsl + std::strlen(kBuiltinForwardHlsl));
|
||||
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::HLSL;
|
||||
pipelineDesc.fragmentShader.entryPoint = L"MainPS";
|
||||
pipelineDesc.fragmentShader.profile = L"ps_5_0";
|
||||
} else {
|
||||
pipelineDesc.vertexShader.source.assign(
|
||||
kBuiltinForwardVertexShader,
|
||||
kBuiltinForwardVertexShader + std::strlen(kBuiltinForwardVertexShader));
|
||||
pipelineDesc.vertexShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
|
||||
pipelineDesc.vertexShader.profile = L"vs_4_30";
|
||||
|
||||
pipelineDesc.fragmentShader.source.assign(
|
||||
kBuiltinForwardFragmentShader,
|
||||
kBuiltinForwardFragmentShader + std::strlen(kBuiltinForwardFragmentShader));
|
||||
pipelineDesc.fragmentShader.sourceLanguage = RHI::ShaderLanguage::GLSL;
|
||||
pipelineDesc.fragmentShader.profile = L"fs_4_30";
|
||||
}
|
||||
|
||||
return pipelineDesc;
|
||||
}
|
||||
|
||||
const Resources::Texture* FindMaterialTexture(const Resources::Material& material) {
|
||||
static const char* kTextureNames[] = {
|
||||
"_BaseColorTexture",
|
||||
"_MainTex",
|
||||
"mainTexture",
|
||||
"texture"
|
||||
};
|
||||
|
||||
for (const char* textureName : kTextureNames) {
|
||||
const Resources::ResourceHandle<Resources::Texture> textureHandle = material.GetTexture(textureName);
|
||||
if (textureHandle.Get() != nullptr && textureHandle->IsValid()) {
|
||||
return textureHandle.Get();
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BuiltinForwardPipeline::~BuiltinForwardPipeline() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::Initialize(const RenderContext& context) {
|
||||
return EnsureInitialized(context);
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::Shutdown() {
|
||||
DestroyPipelineResources();
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::Render(
|
||||
const RenderContext& context,
|
||||
const RenderSurface& surface,
|
||||
const RenderSceneData& sceneData) {
|
||||
if (!EnsureInitialized(context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<RHI::RHIResourceView*>& colorAttachments = surface.GetColorAttachments();
|
||||
if (colorAttachments.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<RHI::RHIResourceView*> renderTargets = colorAttachments;
|
||||
RHI::RHICommandList* commandList = context.commandList;
|
||||
|
||||
if (surface.IsAutoTransitionEnabled()) {
|
||||
for (RHI::RHIResourceView* renderTarget : renderTargets) {
|
||||
if (renderTarget != nullptr) {
|
||||
commandList->TransitionBarrier(
|
||||
renderTarget,
|
||||
surface.GetColorStateBefore(),
|
||||
RHI::ResourceStates::RenderTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
commandList->SetRenderTargets(
|
||||
static_cast<uint32_t>(renderTargets.size()),
|
||||
renderTargets.data(),
|
||||
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);
|
||||
|
||||
const Math::Color clearColor = surface.HasClearColorOverride()
|
||||
? surface.GetClearColorOverride()
|
||||
: sceneData.cameraData.clearColor;
|
||||
const float clearValues[4] = { clearColor.r, clearColor.g, clearColor.b, clearColor.a };
|
||||
for (RHI::RHIResourceView* renderTarget : renderTargets) {
|
||||
if (renderTarget != nullptr) {
|
||||
commandList->ClearRenderTarget(renderTarget, clearValues);
|
||||
}
|
||||
}
|
||||
if (surface.GetDepthAttachment() != nullptr) {
|
||||
commandList->ClearDepthStencil(surface.GetDepthAttachment(), 1.0f, 0);
|
||||
}
|
||||
|
||||
commandList->SetPipelineState(m_pipelineState);
|
||||
commandList->SetPrimitiveTopology(RHI::PrimitiveTopology::TriangleList);
|
||||
|
||||
for (const VisibleRenderObject& visibleObject : sceneData.visibleObjects) {
|
||||
DrawVisibleObject(context, sceneData, visibleObject);
|
||||
}
|
||||
|
||||
if (surface.IsAutoTransitionEnabled()) {
|
||||
for (RHI::RHIResourceView* renderTarget : renderTargets) {
|
||||
if (renderTarget != nullptr) {
|
||||
commandList->TransitionBarrier(
|
||||
renderTarget,
|
||||
RHI::ResourceStates::RenderTarget,
|
||||
surface.GetColorStateAfter());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::EnsureInitialized(const RenderContext& context) {
|
||||
if (!context.IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_initialized &&
|
||||
m_device == context.device &&
|
||||
m_backendType == context.backendType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DestroyPipelineResources();
|
||||
m_device = context.device;
|
||||
m_backendType = context.backendType;
|
||||
m_initialized = CreatePipelineResources(context);
|
||||
return m_initialized;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::CreatePipelineResources(const RenderContext& context) {
|
||||
RHI::DescriptorPoolDesc constantPoolDesc = {};
|
||||
constantPoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
constantPoolDesc.descriptorCount = 1;
|
||||
constantPoolDesc.shaderVisible = false;
|
||||
m_constantPool = context.device->CreateDescriptorPool(constantPoolDesc);
|
||||
if (m_constantPool == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
m_constantSet = m_constantPool->AllocateSet(constantLayout);
|
||||
if (m_constantSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc texturePoolDesc = {};
|
||||
texturePoolDesc.type = RHI::DescriptorHeapType::CBV_SRV_UAV;
|
||||
texturePoolDesc.descriptorCount = 1;
|
||||
texturePoolDesc.shaderVisible = true;
|
||||
m_texturePool = context.device->CreateDescriptorPool(texturePoolDesc);
|
||||
if (m_texturePool == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding textureBinding = {};
|
||||
textureBinding.binding = 0;
|
||||
textureBinding.type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
textureBinding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc textureLayout = {};
|
||||
textureLayout.bindings = &textureBinding;
|
||||
textureLayout.bindingCount = 1;
|
||||
m_textureSet = m_texturePool->AllocateSet(textureLayout);
|
||||
if (m_textureSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorPoolDesc samplerPoolDesc = {};
|
||||
samplerPoolDesc.type = RHI::DescriptorHeapType::Sampler;
|
||||
samplerPoolDesc.descriptorCount = 1;
|
||||
samplerPoolDesc.shaderVisible = true;
|
||||
m_samplerPool = context.device->CreateDescriptorPool(samplerPoolDesc);
|
||||
if (m_samplerPool == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding samplerBinding = {};
|
||||
samplerBinding.binding = 0;
|
||||
samplerBinding.type = static_cast<uint32_t>(RHI::DescriptorType::Sampler);
|
||||
samplerBinding.count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc samplerLayout = {};
|
||||
samplerLayout.bindings = &samplerBinding;
|
||||
samplerLayout.bindingCount = 1;
|
||||
m_samplerSet = m_samplerPool->AllocateSet(samplerLayout);
|
||||
if (m_samplerSet == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::DescriptorSetLayoutBinding reservedBindings[3] = {};
|
||||
reservedBindings[0].binding = 0;
|
||||
reservedBindings[0].type = static_cast<uint32_t>(RHI::DescriptorType::CBV);
|
||||
reservedBindings[0].count = 1;
|
||||
reservedBindings[1].binding = 0;
|
||||
reservedBindings[1].type = static_cast<uint32_t>(RHI::DescriptorType::SRV);
|
||||
reservedBindings[1].count = 1;
|
||||
reservedBindings[2].binding = 0;
|
||||
reservedBindings[2].type = static_cast<uint32_t>(RHI::DescriptorType::Sampler);
|
||||
reservedBindings[2].count = 1;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc reservedLayout = {};
|
||||
reservedLayout.bindings = reservedBindings;
|
||||
reservedLayout.bindingCount = 3;
|
||||
|
||||
RHI::DescriptorSetLayoutDesc setLayouts[kDescriptorSetCount] = {};
|
||||
setLayouts[0] = reservedLayout;
|
||||
setLayouts[1] = constantLayout;
|
||||
setLayouts[2] = textureLayout;
|
||||
setLayouts[3] = samplerLayout;
|
||||
|
||||
RHI::RHIPipelineLayoutDesc pipelineLayoutDesc = {};
|
||||
pipelineLayoutDesc.setLayouts = setLayouts;
|
||||
pipelineLayoutDesc.setLayoutCount = kDescriptorSetCount;
|
||||
m_pipelineLayout = context.device->CreatePipelineLayout(pipelineLayoutDesc);
|
||||
if (m_pipelineLayout == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const RHI::GraphicsPipelineDesc pipelineDesc = CreatePipelineDesc(context.backendType, m_pipelineLayout);
|
||||
m_pipelineState = context.device->CreatePipelineState(pipelineDesc);
|
||||
if (m_pipelineState == nullptr || !m_pipelineState->IsValid()) {
|
||||
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 = 1000.0f;
|
||||
m_sampler = context.device->CreateSampler(samplerDesc);
|
||||
if (m_sampler == nullptr) {
|
||||
return false;
|
||||
}
|
||||
m_samplerSet->UpdateSampler(0, m_sampler);
|
||||
|
||||
const unsigned char whitePixel[4] = { 255, 255, 255, 255 };
|
||||
RHI::TextureDesc textureDesc = {};
|
||||
textureDesc.width = 1;
|
||||
textureDesc.height = 1;
|
||||
textureDesc.depth = 1;
|
||||
textureDesc.mipLevels = 1;
|
||||
textureDesc.arraySize = 1;
|
||||
textureDesc.format = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
textureDesc.textureType = static_cast<uint32_t>(RHI::TextureType::Texture2D);
|
||||
textureDesc.sampleCount = 1;
|
||||
textureDesc.sampleQuality = 0;
|
||||
textureDesc.flags = 0;
|
||||
m_fallbackTexture = context.device->CreateTexture(textureDesc, whitePixel, sizeof(whitePixel), 4);
|
||||
if (m_fallbackTexture == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::ResourceViewDesc textureViewDesc = {};
|
||||
textureViewDesc.format = static_cast<uint32_t>(RHI::Format::R8G8B8A8_UNorm);
|
||||
textureViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
|
||||
textureViewDesc.mipLevel = 0;
|
||||
m_fallbackTextureView = context.device->CreateShaderResourceView(m_fallbackTexture, textureViewDesc);
|
||||
if (m_fallbackTextureView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BuiltinForwardPipeline::DestroyPipelineResources() {
|
||||
m_resourceCache.Shutdown();
|
||||
|
||||
if (m_fallbackTextureView != nullptr) {
|
||||
m_fallbackTextureView->Shutdown();
|
||||
delete m_fallbackTextureView;
|
||||
m_fallbackTextureView = nullptr;
|
||||
}
|
||||
|
||||
if (m_fallbackTexture != nullptr) {
|
||||
m_fallbackTexture->Shutdown();
|
||||
delete m_fallbackTexture;
|
||||
m_fallbackTexture = nullptr;
|
||||
}
|
||||
|
||||
if (m_sampler != nullptr) {
|
||||
m_sampler->Shutdown();
|
||||
delete m_sampler;
|
||||
m_sampler = nullptr;
|
||||
}
|
||||
|
||||
if (m_pipelineState != nullptr) {
|
||||
m_pipelineState->Shutdown();
|
||||
delete m_pipelineState;
|
||||
m_pipelineState = nullptr;
|
||||
}
|
||||
|
||||
if (m_pipelineLayout != nullptr) {
|
||||
m_pipelineLayout->Shutdown();
|
||||
delete m_pipelineLayout;
|
||||
m_pipelineLayout = nullptr;
|
||||
}
|
||||
|
||||
if (m_constantSet != nullptr) {
|
||||
m_constantSet->Shutdown();
|
||||
delete m_constantSet;
|
||||
m_constantSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_textureSet != nullptr) {
|
||||
m_textureSet->Shutdown();
|
||||
delete m_textureSet;
|
||||
m_textureSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_samplerSet != nullptr) {
|
||||
m_samplerSet->Shutdown();
|
||||
delete m_samplerSet;
|
||||
m_samplerSet = nullptr;
|
||||
}
|
||||
|
||||
if (m_constantPool != nullptr) {
|
||||
m_constantPool->Shutdown();
|
||||
delete m_constantPool;
|
||||
m_constantPool = nullptr;
|
||||
}
|
||||
|
||||
if (m_texturePool != nullptr) {
|
||||
m_texturePool->Shutdown();
|
||||
delete m_texturePool;
|
||||
m_texturePool = nullptr;
|
||||
}
|
||||
|
||||
if (m_samplerPool != nullptr) {
|
||||
m_samplerPool->Shutdown();
|
||||
delete m_samplerPool;
|
||||
m_samplerPool = nullptr;
|
||||
}
|
||||
|
||||
m_device = nullptr;
|
||||
m_initialized = false;
|
||||
}
|
||||
|
||||
const Resources::Material* BuiltinForwardPipeline::ResolveMaterial(
|
||||
const VisibleRenderObject& visibleObject,
|
||||
uint32_t materialIndex) const {
|
||||
if (visibleObject.meshRenderer != nullptr && materialIndex < visibleObject.meshRenderer->GetMaterialCount()) {
|
||||
if (const Resources::Material* material = visibleObject.meshRenderer->GetMaterial(materialIndex)) {
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleObject.mesh != nullptr && materialIndex < visibleObject.mesh->GetMaterials().Size()) {
|
||||
if (const Resources::Material* material = visibleObject.mesh->GetMaterials()[materialIndex]) {
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleObject.meshRenderer != nullptr && visibleObject.meshRenderer->GetMaterialCount() > 0) {
|
||||
if (const Resources::Material* material = visibleObject.meshRenderer->GetMaterial(0)) {
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleObject.mesh != nullptr && visibleObject.mesh->GetMaterials().Size() > 0) {
|
||||
return visibleObject.mesh->GetMaterials()[0];
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Resources::Texture* BuiltinForwardPipeline::ResolveTexture(const Resources::Material* material) const {
|
||||
return material != nullptr ? FindMaterialTexture(*material) : nullptr;
|
||||
}
|
||||
|
||||
RHI::RHIResourceView* BuiltinForwardPipeline::ResolveTextureView(
|
||||
const VisibleRenderObject& visibleObject,
|
||||
uint32_t materialIndex) {
|
||||
const Resources::Material* material = ResolveMaterial(visibleObject, materialIndex);
|
||||
const Resources::Texture* texture = ResolveTexture(material);
|
||||
if (texture != nullptr) {
|
||||
const RenderResourceCache::CachedTexture* cachedTexture = m_resourceCache.GetOrCreateTexture(m_device, texture);
|
||||
if (cachedTexture != nullptr && cachedTexture->shaderResourceView != nullptr) {
|
||||
return cachedTexture->shaderResourceView;
|
||||
}
|
||||
}
|
||||
|
||||
return m_fallbackTextureView;
|
||||
}
|
||||
|
||||
bool BuiltinForwardPipeline::DrawVisibleObject(
|
||||
const RenderContext& context,
|
||||
const RenderSceneData& sceneData,
|
||||
const VisibleRenderObject& visibleObject) {
|
||||
const RenderResourceCache::CachedMesh* cachedMesh = m_resourceCache.GetOrCreateMesh(m_device, visibleObject.mesh);
|
||||
if (cachedMesh == nullptr || cachedMesh->vertexBufferView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::RHICommandList* commandList = context.commandList;
|
||||
|
||||
RHI::RHIResourceView* vertexBuffers[] = { cachedMesh->vertexBufferView };
|
||||
const uint64_t offsets[] = { 0 };
|
||||
const uint32_t strides[] = { cachedMesh->vertexStride };
|
||||
commandList->SetVertexBuffers(0, 1, vertexBuffers, offsets, strides);
|
||||
if (cachedMesh->indexBufferView != nullptr) {
|
||||
commandList->SetIndexBuffer(cachedMesh->indexBufferView, 0);
|
||||
}
|
||||
|
||||
const PerObjectConstants constants = {
|
||||
sceneData.cameraData.projection,
|
||||
sceneData.cameraData.view,
|
||||
visibleObject.localToWorld.Transpose()
|
||||
};
|
||||
|
||||
const Containers::Array<Resources::MeshSection>& sections = visibleObject.mesh->GetSections();
|
||||
const bool hasSections = !sections.Empty();
|
||||
|
||||
if (hasSections) {
|
||||
for (size_t sectionIndex = 0; sectionIndex < sections.Size(); ++sectionIndex) {
|
||||
const Resources::MeshSection& section = sections[sectionIndex];
|
||||
RHI::RHIResourceView* textureView = ResolveTextureView(visibleObject, section.materialID);
|
||||
if (textureView == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
m_textureSet->Update(0, textureView);
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
|
||||
|
||||
if (cachedMesh->indexBufferView != nullptr && section.indexCount > 0) {
|
||||
commandList->DrawIndexed(section.indexCount, 1, section.startIndex, static_cast<int32_t>(section.baseVertex), 0);
|
||||
} else if (section.vertexCount > 0) {
|
||||
commandList->Draw(section.vertexCount, 1, section.baseVertex, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RHI::RHIResourceView* textureView = ResolveTextureView(visibleObject, 0);
|
||||
if (textureView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_constantSet->WriteConstant(0, &constants, sizeof(constants));
|
||||
m_textureSet->Update(0, textureView);
|
||||
RHI::RHIDescriptorSet* descriptorSets[] = { m_constantSet, m_textureSet, m_samplerSet };
|
||||
commandList->SetGraphicsDescriptorSets(kDescriptorFirstSet, 3, descriptorSets, m_pipelineLayout);
|
||||
|
||||
if (cachedMesh->indexBufferView != nullptr && cachedMesh->indexCount > 0) {
|
||||
commandList->DrawIndexed(cachedMesh->indexCount, 1, 0, 0, 0);
|
||||
} else if (cachedMesh->vertexCount > 0) {
|
||||
commandList->Draw(cachedMesh->vertexCount, 1, 0, 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Pipelines
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
250
engine/src/Rendering/RenderResourceCache.cpp
Normal file
250
engine/src/Rendering/RenderResourceCache.cpp
Normal file
@@ -0,0 +1,250 @@
|
||||
#include "Rendering/RenderResourceCache.h"
|
||||
|
||||
#include <XCEngine/RHI/RHIEnums.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
namespace {
|
||||
|
||||
RHI::Format ToRHITextureFormat(Resources::TextureFormat format) {
|
||||
switch (format) {
|
||||
case Resources::TextureFormat::RGBA8_UNORM:
|
||||
case Resources::TextureFormat::RGBA8_SRGB:
|
||||
return RHI::Format::R8G8B8A8_UNorm;
|
||||
default:
|
||||
return RHI::Format::Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
RHI::TextureType ToRHITextureType(Resources::TextureType textureType) {
|
||||
switch (textureType) {
|
||||
case Resources::TextureType::Texture2DArray:
|
||||
return RHI::TextureType::Texture2DArray;
|
||||
case Resources::TextureType::Texture3D:
|
||||
return RHI::TextureType::Texture3D;
|
||||
case Resources::TextureType::TextureCube:
|
||||
return RHI::TextureType::TextureCube;
|
||||
case Resources::TextureType::TextureCubeArray:
|
||||
return RHI::TextureType::TextureCubeArray;
|
||||
case Resources::TextureType::Texture2D:
|
||||
default:
|
||||
return RHI::TextureType::Texture2D;
|
||||
}
|
||||
}
|
||||
|
||||
void ShutdownMesh(RenderResourceCache::CachedMesh& cachedMesh) {
|
||||
if (cachedMesh.vertexBufferView != nullptr) {
|
||||
cachedMesh.vertexBufferView->Shutdown();
|
||||
delete cachedMesh.vertexBufferView;
|
||||
cachedMesh.vertexBufferView = nullptr;
|
||||
}
|
||||
|
||||
if (cachedMesh.indexBufferView != nullptr) {
|
||||
cachedMesh.indexBufferView->Shutdown();
|
||||
delete cachedMesh.indexBufferView;
|
||||
cachedMesh.indexBufferView = nullptr;
|
||||
}
|
||||
|
||||
if (cachedMesh.vertexBuffer != nullptr) {
|
||||
cachedMesh.vertexBuffer->Shutdown();
|
||||
delete cachedMesh.vertexBuffer;
|
||||
cachedMesh.vertexBuffer = nullptr;
|
||||
}
|
||||
|
||||
if (cachedMesh.indexBuffer != nullptr) {
|
||||
cachedMesh.indexBuffer->Shutdown();
|
||||
delete cachedMesh.indexBuffer;
|
||||
cachedMesh.indexBuffer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void ShutdownTexture(RenderResourceCache::CachedTexture& cachedTexture) {
|
||||
if (cachedTexture.shaderResourceView != nullptr) {
|
||||
cachedTexture.shaderResourceView->Shutdown();
|
||||
delete cachedTexture.shaderResourceView;
|
||||
cachedTexture.shaderResourceView = nullptr;
|
||||
}
|
||||
|
||||
if (cachedTexture.texture != nullptr) {
|
||||
cachedTexture.texture->Shutdown();
|
||||
delete cachedTexture.texture;
|
||||
cachedTexture.texture = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
RenderResourceCache::~RenderResourceCache() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
void RenderResourceCache::Shutdown() {
|
||||
for (auto& entry : m_meshCache) {
|
||||
ShutdownMesh(entry.second);
|
||||
}
|
||||
m_meshCache.clear();
|
||||
|
||||
for (auto& entry : m_textureCache) {
|
||||
ShutdownTexture(entry.second);
|
||||
}
|
||||
m_textureCache.clear();
|
||||
}
|
||||
|
||||
const RenderResourceCache::CachedMesh* RenderResourceCache::GetOrCreateMesh(
|
||||
RHI::RHIDevice* device,
|
||||
const Resources::Mesh* mesh) {
|
||||
if (device == nullptr || mesh == nullptr || !mesh->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = m_meshCache.find(mesh);
|
||||
if (it != m_meshCache.end()) {
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
CachedMesh cachedMesh;
|
||||
if (!UploadMesh(device, mesh, cachedMesh)) {
|
||||
ShutdownMesh(cachedMesh);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto result = m_meshCache.emplace(mesh, cachedMesh);
|
||||
return &result.first->second;
|
||||
}
|
||||
|
||||
const RenderResourceCache::CachedTexture* RenderResourceCache::GetOrCreateTexture(
|
||||
RHI::RHIDevice* device,
|
||||
const Resources::Texture* texture) {
|
||||
if (device == nullptr || texture == nullptr || !texture->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = m_textureCache.find(texture);
|
||||
if (it != m_textureCache.end()) {
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
CachedTexture cachedTexture;
|
||||
if (!UploadTexture(device, texture, cachedTexture)) {
|
||||
ShutdownTexture(cachedTexture);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto result = m_textureCache.emplace(texture, cachedTexture);
|
||||
return &result.first->second;
|
||||
}
|
||||
|
||||
bool RenderResourceCache::UploadMesh(
|
||||
RHI::RHIDevice* device,
|
||||
const Resources::Mesh* mesh,
|
||||
CachedMesh& cachedMesh) {
|
||||
if (mesh->GetVertexData() == nullptr || mesh->GetVertexDataSize() == 0 || mesh->GetVertexStride() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::BufferDesc vertexBufferDesc = {};
|
||||
vertexBufferDesc.size = static_cast<uint64_t>(mesh->GetVertexDataSize());
|
||||
vertexBufferDesc.stride = mesh->GetVertexStride();
|
||||
vertexBufferDesc.bufferType = static_cast<uint32_t>(RHI::BufferType::Vertex);
|
||||
cachedMesh.vertexBuffer = device->CreateBuffer(vertexBufferDesc);
|
||||
if (cachedMesh.vertexBuffer == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedMesh.vertexBuffer->SetData(mesh->GetVertexData(), mesh->GetVertexDataSize());
|
||||
cachedMesh.vertexBuffer->SetStride(mesh->GetVertexStride());
|
||||
cachedMesh.vertexBuffer->SetBufferType(RHI::BufferType::Vertex);
|
||||
|
||||
RHI::ResourceViewDesc vertexViewDesc = {};
|
||||
vertexViewDesc.dimension = RHI::ResourceViewDimension::Buffer;
|
||||
vertexViewDesc.structureByteStride = mesh->GetVertexStride();
|
||||
cachedMesh.vertexBufferView = device->CreateVertexBufferView(cachedMesh.vertexBuffer, vertexViewDesc);
|
||||
if (cachedMesh.vertexBufferView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedMesh.vertexCount = mesh->GetVertexCount();
|
||||
cachedMesh.vertexStride = mesh->GetVertexStride();
|
||||
|
||||
if (mesh->GetIndexData() != nullptr && mesh->GetIndexDataSize() > 0 && mesh->GetIndexCount() > 0) {
|
||||
RHI::BufferDesc indexBufferDesc = {};
|
||||
indexBufferDesc.size = static_cast<uint64_t>(mesh->GetIndexDataSize());
|
||||
indexBufferDesc.stride = mesh->IsUse32BitIndex() ? sizeof(uint32_t) : sizeof(uint16_t);
|
||||
indexBufferDesc.bufferType = static_cast<uint32_t>(RHI::BufferType::Index);
|
||||
cachedMesh.indexBuffer = device->CreateBuffer(indexBufferDesc);
|
||||
if (cachedMesh.indexBuffer == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedMesh.indexBuffer->SetData(mesh->GetIndexData(), mesh->GetIndexDataSize());
|
||||
cachedMesh.indexBuffer->SetStride(indexBufferDesc.stride);
|
||||
cachedMesh.indexBuffer->SetBufferType(RHI::BufferType::Index);
|
||||
|
||||
RHI::ResourceViewDesc indexViewDesc = {};
|
||||
indexViewDesc.dimension = RHI::ResourceViewDimension::Buffer;
|
||||
indexViewDesc.format = static_cast<uint32_t>(
|
||||
mesh->IsUse32BitIndex() ? RHI::Format::R32_UInt : RHI::Format::R16_UInt);
|
||||
cachedMesh.indexBufferView = device->CreateIndexBufferView(cachedMesh.indexBuffer, indexViewDesc);
|
||||
if (cachedMesh.indexBufferView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedMesh.indexCount = mesh->GetIndexCount();
|
||||
cachedMesh.uses32BitIndices = mesh->IsUse32BitIndex();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderResourceCache::UploadTexture(
|
||||
RHI::RHIDevice* device,
|
||||
const Resources::Texture* texture,
|
||||
CachedTexture& cachedTexture) {
|
||||
if (texture->GetPixelData() == nullptr || texture->GetPixelDataSize() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const RHI::Format format = ToRHITextureFormat(texture->GetFormat());
|
||||
if (format == RHI::Format::Unknown) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::TextureDesc textureDesc = {};
|
||||
textureDesc.width = texture->GetWidth();
|
||||
textureDesc.height = texture->GetHeight();
|
||||
textureDesc.depth = texture->GetDepth();
|
||||
textureDesc.mipLevels = texture->GetMipLevels();
|
||||
textureDesc.arraySize = 1;
|
||||
textureDesc.format = static_cast<uint32_t>(format);
|
||||
textureDesc.textureType = static_cast<uint32_t>(ToRHITextureType(texture->GetTextureType()));
|
||||
textureDesc.sampleCount = 1;
|
||||
textureDesc.sampleQuality = 0;
|
||||
textureDesc.flags = 0;
|
||||
|
||||
const uint32_t rowPitch = texture->GetWidth() * 4;
|
||||
cachedTexture.texture = device->CreateTexture(
|
||||
textureDesc,
|
||||
texture->GetPixelData(),
|
||||
texture->GetPixelDataSize(),
|
||||
rowPitch);
|
||||
if (cachedTexture.texture == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHI::ResourceViewDesc textureViewDesc = {};
|
||||
textureViewDesc.format = static_cast<uint32_t>(format);
|
||||
textureViewDesc.dimension = RHI::ResourceViewDimension::Texture2D;
|
||||
textureViewDesc.mipLevel = 0;
|
||||
cachedTexture.shaderResourceView = device->CreateShaderResourceView(cachedTexture.texture, textureViewDesc);
|
||||
if (cachedTexture.shaderResourceView == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cachedTexture.width = texture->GetWidth();
|
||||
cachedTexture.height = texture->GetHeight();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
150
engine/src/Rendering/RenderSceneExtractor.cpp
Normal file
150
engine/src/Rendering/RenderSceneExtractor.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "Rendering/RenderSceneExtractor.h"
|
||||
|
||||
#include "Components/CameraComponent.h"
|
||||
#include "Components/GameObject.h"
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
#include "Components/TransformComponent.h"
|
||||
#include "Scene/Scene.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
namespace {
|
||||
|
||||
bool IsUsableCamera(const Components::CameraComponent* camera) {
|
||||
return camera != nullptr &&
|
||||
camera->IsEnabled() &&
|
||||
camera->GetGameObject() != nullptr &&
|
||||
camera->GetGameObject()->IsActiveInHierarchy();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
RenderSceneData RenderSceneExtractor::Extract(
|
||||
const Components::Scene& scene,
|
||||
Components::CameraComponent* overrideCamera,
|
||||
uint32_t viewportWidth,
|
||||
uint32_t viewportHeight) const {
|
||||
RenderSceneData sceneData;
|
||||
sceneData.camera = SelectCamera(scene, overrideCamera);
|
||||
if (sceneData.camera == nullptr) {
|
||||
return sceneData;
|
||||
}
|
||||
|
||||
sceneData.cameraData = BuildCameraData(*sceneData.camera, viewportWidth, viewportHeight);
|
||||
|
||||
const std::vector<Components::GameObject*> rootGameObjects = scene.GetRootGameObjects();
|
||||
for (Components::GameObject* rootGameObject : rootGameObjects) {
|
||||
ExtractVisibleObjects(rootGameObject, sceneData.visibleObjects);
|
||||
}
|
||||
|
||||
return sceneData;
|
||||
}
|
||||
|
||||
Components::CameraComponent* RenderSceneExtractor::SelectCamera(
|
||||
const Components::Scene& scene,
|
||||
Components::CameraComponent* overrideCamera) const {
|
||||
if (IsUsableCamera(overrideCamera)) {
|
||||
return overrideCamera;
|
||||
}
|
||||
|
||||
const std::vector<Components::CameraComponent*> cameras = scene.FindObjectsOfType<Components::CameraComponent>();
|
||||
|
||||
Components::CameraComponent* primaryCamera = nullptr;
|
||||
for (Components::CameraComponent* camera : cameras) {
|
||||
if (!IsUsableCamera(camera)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (camera->IsPrimary()) {
|
||||
if (primaryCamera == nullptr || camera->GetDepth() > primaryCamera->GetDepth()) {
|
||||
primaryCamera = camera;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (primaryCamera != nullptr) {
|
||||
return primaryCamera;
|
||||
}
|
||||
|
||||
for (Components::CameraComponent* camera : cameras) {
|
||||
if (IsUsableCamera(camera)) {
|
||||
return camera;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
RenderCameraData RenderSceneExtractor::BuildCameraData(
|
||||
const Components::CameraComponent& camera,
|
||||
uint32_t viewportWidth,
|
||||
uint32_t viewportHeight) const {
|
||||
RenderCameraData cameraData;
|
||||
cameraData.viewportWidth = viewportWidth;
|
||||
cameraData.viewportHeight = viewportHeight;
|
||||
cameraData.worldPosition = camera.transform().GetPosition();
|
||||
cameraData.clearColor = camera.GetClearColor();
|
||||
|
||||
const Math::Matrix4x4 view = camera.transform().GetWorldToLocalMatrix();
|
||||
const float aspect = viewportHeight > 0
|
||||
? static_cast<float>(viewportWidth) / static_cast<float>(viewportHeight)
|
||||
: 1.0f;
|
||||
|
||||
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
|
||||
if (camera.GetProjectionType() == Components::CameraProjectionType::Perspective) {
|
||||
projection = Math::Matrix4x4::Perspective(
|
||||
camera.GetFieldOfView() * Math::DEG_TO_RAD,
|
||||
aspect,
|
||||
camera.GetNearClipPlane(),
|
||||
camera.GetFarClipPlane());
|
||||
} else {
|
||||
const float orthoSize = camera.GetOrthographicSize();
|
||||
projection = Math::Matrix4x4::Orthographic(
|
||||
-orthoSize * aspect,
|
||||
orthoSize * aspect,
|
||||
-orthoSize,
|
||||
orthoSize,
|
||||
camera.GetNearClipPlane(),
|
||||
camera.GetFarClipPlane());
|
||||
}
|
||||
|
||||
cameraData.view = view.Transpose();
|
||||
cameraData.projection = projection.Transpose();
|
||||
cameraData.viewProjection = (projection * view).Transpose();
|
||||
return cameraData;
|
||||
}
|
||||
|
||||
void RenderSceneExtractor::ExtractVisibleObjects(
|
||||
Components::GameObject* gameObject,
|
||||
std::vector<VisibleRenderObject>& visibleObjects) const {
|
||||
if (gameObject == nullptr || !gameObject->IsActiveInHierarchy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto* meshFilter = gameObject->GetComponent<Components::MeshFilterComponent>();
|
||||
auto* meshRenderer = gameObject->GetComponent<Components::MeshRendererComponent>();
|
||||
if (meshFilter != nullptr &&
|
||||
meshRenderer != nullptr &&
|
||||
meshFilter->IsEnabled() &&
|
||||
meshRenderer->IsEnabled()) {
|
||||
Resources::Mesh* mesh = meshFilter->GetMesh();
|
||||
if (mesh != nullptr && mesh->IsValid()) {
|
||||
VisibleRenderObject visibleObject;
|
||||
visibleObject.gameObject = gameObject;
|
||||
visibleObject.meshFilter = meshFilter;
|
||||
visibleObject.meshRenderer = meshRenderer;
|
||||
visibleObject.mesh = mesh;
|
||||
visibleObject.localToWorld = gameObject->GetTransform()->GetLocalToWorldMatrix();
|
||||
visibleObjects.push_back(visibleObject);
|
||||
}
|
||||
}
|
||||
|
||||
for (Components::GameObject* child : gameObject->GetChildren()) {
|
||||
ExtractVisibleObjects(child, visibleObjects);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
37
engine/src/Rendering/RenderSurface.cpp
Normal file
37
engine/src/Rendering/RenderSurface.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "Rendering/RenderSurface.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
RenderSurface::RenderSurface(uint32_t width, uint32_t height)
|
||||
: m_width(width)
|
||||
, m_height(height) {
|
||||
}
|
||||
|
||||
void RenderSurface::SetSize(uint32_t width, uint32_t height) {
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
}
|
||||
|
||||
void RenderSurface::SetColorAttachment(RHI::RHIResourceView* colorAttachment) {
|
||||
m_colorAttachments.clear();
|
||||
if (colorAttachment != nullptr) {
|
||||
m_colorAttachments.push_back(colorAttachment);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderSurface::SetColorAttachments(const std::vector<RHI::RHIResourceView*>& colorAttachments) {
|
||||
m_colorAttachments = colorAttachments;
|
||||
}
|
||||
|
||||
void RenderSurface::SetClearColorOverride(const Math::Color& clearColor) {
|
||||
m_hasClearColorOverride = true;
|
||||
m_clearColorOverride = clearColor;
|
||||
}
|
||||
|
||||
void RenderSurface::ClearClearColorOverride() {
|
||||
m_hasClearColorOverride = false;
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
60
engine/src/Rendering/SceneRenderer.cpp
Normal file
60
engine/src/Rendering/SceneRenderer.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "Rendering/SceneRenderer.h"
|
||||
|
||||
#include "Rendering/Pipelines/BuiltinForwardPipeline.h"
|
||||
#include "Rendering/RenderSurface.h"
|
||||
#include "Scene/Scene.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
SceneRenderer::SceneRenderer()
|
||||
: m_pipeline(std::make_unique<Pipelines::BuiltinForwardPipeline>()) {
|
||||
}
|
||||
|
||||
SceneRenderer::SceneRenderer(std::unique_ptr<RenderPipeline> pipeline)
|
||||
: m_pipeline(std::move(pipeline)) {
|
||||
if (!m_pipeline) {
|
||||
m_pipeline = std::make_unique<Pipelines::BuiltinForwardPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
SceneRenderer::~SceneRenderer() {
|
||||
if (m_pipeline) {
|
||||
m_pipeline->Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
void SceneRenderer::SetPipeline(std::unique_ptr<RenderPipeline> pipeline) {
|
||||
if (m_pipeline) {
|
||||
m_pipeline->Shutdown();
|
||||
}
|
||||
|
||||
m_pipeline = std::move(pipeline);
|
||||
if (!m_pipeline) {
|
||||
m_pipeline = std::make_unique<Pipelines::BuiltinForwardPipeline>();
|
||||
}
|
||||
}
|
||||
|
||||
bool SceneRenderer::Render(
|
||||
const Components::Scene& scene,
|
||||
Components::CameraComponent* overrideCamera,
|
||||
const RenderContext& context,
|
||||
const RenderSurface& surface) {
|
||||
if (!context.IsValid() || m_pipeline == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RenderSceneData sceneData = m_sceneExtractor.Extract(
|
||||
scene,
|
||||
overrideCamera,
|
||||
surface.GetWidth(),
|
||||
surface.GetHeight());
|
||||
if (!sceneData.HasCamera()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_pipeline->Render(context, surface, sceneData);
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user