diff --git a/Include/FlashlightEngine/Application.hpp b/Include/FlashlightEngine/Application.hpp index 3606582..ec85bad 100644 --- a/Include/FlashlightEngine/Application.hpp +++ b/Include/FlashlightEngine/Application.hpp @@ -11,8 +11,6 @@ #include -#include - #include #include #include @@ -28,7 +26,6 @@ namespace FlashlightEngine { virtual void OnUpdate() = 0; virtual void OnRender() = 0; bool OnWindowClose(WindowCloseEvent& e); - bool OnWindowResize(const WindowResizeEvent& e) const; inline static Application& Get(); [[nodiscard]] inline Window& GetWindow() const; @@ -38,8 +35,6 @@ namespace FlashlightEngine { std::shared_ptr m_Window; protected: - std::shared_ptr m_Renderer; - private: bool m_Running = false; static Application* m_Instance; diff --git a/Include/FlashlightEngine/EngineApplication.hpp b/Include/FlashlightEngine/EngineApplication.hpp index baeba6f..cbcbc04 100644 --- a/Include/FlashlightEngine/EngineApplication.hpp +++ b/Include/FlashlightEngine/EngineApplication.hpp @@ -11,8 +11,6 @@ #include -#include - namespace FlashlightEngine { class EngineApplication final : public Application { public: @@ -24,15 +22,6 @@ namespace FlashlightEngine { void OnRender() override; private: - ShaderCollection m_MainShaderCollection; - std::unique_ptr m_TriangleVertexBuffer; - - std::shared_ptr m_LinearSampler; - std::unique_ptr m_FrogTexture; - std::unique_ptr m_FallbackTexture; - - bool m_UseFrogTexture = true; - void OnKeyPressed(const KeyDownEvent& event); }; } diff --git a/Include/FlashlightEngine/Renderer/Buffer.hpp b/Include/FlashlightEngine/Renderer/Buffer.hpp deleted file mode 100644 index 657dbe8..0000000 --- a/Include/FlashlightEngine/Renderer/Buffer.hpp +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_BUFFER_HPP -#define FL_RENDERER_BUFFER_HPP - -#include - -#include -#include - -#include - -#include - -namespace FlashlightEngine { - enum class BufferUsage { - Default = 0, - Immutable = 1, - Dynamic = 2, - Staging = 3 - }; - - enum class BufferBindFlags { - VertexBuffer = Bit(0), - IndexBuffer = Bit(1), - ConstantBuffer = Bit(2), - ShaderResource = Bit(3), - StreamOutput = Bit(4), - RenderTarget = Bit(5), - DepthStencil = Bit(6), - UnorderedAccess = Bit(7), - Decoder = Bit(8), - VideoEncoder = Bit(9) - }; - - inline BufferBindFlags operator|(const BufferBindFlags lhs, const BufferBindFlags rhs) { - return static_cast(EnumAsInteger(lhs) | EnumAsInteger(rhs)); - } - - enum class BufferCPUAccess { - None = 0, - - Write = Bit(0), - Read = Bit(1) - }; - - inline BufferCPUAccess operator|(const BufferCPUAccess lhs, const BufferCPUAccess rhs) { - return static_cast(EnumAsInteger(lhs) | EnumAsInteger(rhs)); - } - - class Buffer { - public: - Buffer(const std::shared_ptr& device, - const void* data, - UInt32 size, - D3D11_USAGE usage, - D3D11_BIND_FLAG bindFlags, - bool enableCpuAccess = false, - D3D11_CPU_ACCESS_FLAG cpuAccess = D3D11_CPU_ACCESS_WRITE); - ~Buffer(); - - Buffer(const Buffer&) = delete; - Buffer(Buffer&& other) noexcept; - - [[nodiscard]] inline ComPtr GetBuffer() const; - - void Allocate(const void* data, - UInt32 size, - D3D11_USAGE usage, - D3D11_BIND_FLAG bindFlags, - bool enableCpuAccess, - D3D11_CPU_ACCESS_FLAG cpuAccess); - void Free(); - - Buffer& operator=(const Buffer&) = delete; - Buffer& operator=(Buffer&& other) noexcept; - - private: - ComPtr m_Buffer{nullptr}; - - std::shared_ptr m_Device{nullptr}; - }; -} - -#include - -#endif // FL_RENDERER_BUFFER_HPP diff --git a/Include/FlashlightEngine/Renderer/Buffer.inl b/Include/FlashlightEngine/Renderer/Buffer.inl deleted file mode 100644 index 09bddec..0000000 --- a/Include/FlashlightEngine/Renderer/Buffer.inl +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - inline ComPtr Buffer::GetBuffer() const { - return m_Buffer; - } -} diff --git a/Include/FlashlightEngine/Renderer/Device.hpp b/Include/FlashlightEngine/Renderer/Device.hpp deleted file mode 100644 index d293e4d..0000000 --- a/Include/FlashlightEngine/Renderer/Device.hpp +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_DEVICE_HPP -#define FL_RENDERER_DEVICE_HPP - -#include - -#include - -namespace FlashlightEngine { - class Device { - public: - Device(); - ~Device(); - - Device(const Device&) = delete; - Device(Device&& other) noexcept; - - [[nodiscard]] inline ComPtr GetDevice() const; - [[nodiscard]] inline ComPtr GetDeviceContext() const; - [[nodiscard]] inline ComPtr GetDxgiFactory() const; - - [[nodiscard]] inline bool IsValid() const; - - Device& operator=(const Device&) = delete; - Device& operator=(Device&& other) noexcept; - - private: - ComPtr m_Device{nullptr}; - -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - ComPtr m_DebugInterface{nullptr}; -#endif - - ComPtr m_Context{nullptr}; - ComPtr m_DxgiFactory{nullptr}; - }; -} - -#include - -#endif // FL_RENDERER_DEVICE_HPP diff --git a/Include/FlashlightEngine/Renderer/Device.inl b/Include/FlashlightEngine/Renderer/Device.inl deleted file mode 100644 index f073b30..0000000 --- a/Include/FlashlightEngine/Renderer/Device.inl +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - inline ComPtr Device::GetDevice() const { - return m_Device; - } - - inline ComPtr Device::GetDeviceContext() const { - return m_Context; - } - - inline ComPtr Device::GetDxgiFactory() const { - return m_DxgiFactory; - } - -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - inline bool Device::IsValid() const { - return m_Device != nullptr && m_DebugInterface != nullptr && m_Context != nullptr && m_DxgiFactory != nullptr; - } -#else - inline bool Device::IsValid() const { - return m_Device != nullptr && m_Context != nullptr && m_DxgiFactory != nullptr; - } -#endif -} diff --git a/Include/FlashlightEngine/Renderer/Enums.hpp b/Include/FlashlightEngine/Renderer/Enums.hpp deleted file mode 100644 index b4453b5..0000000 --- a/Include/FlashlightEngine/Renderer/Enums.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_ENUMS_HPP -#define FL_RENDERER_ENUMS_HPP - -#include - -namespace FlashlightEngine { - enum class PrimitiveTopology { - Undefined = 0, - PointList = 1, - LineList = 2, - LineStrip = 3, - TriangleList = 4, - TriangleStrip = 5, - LineListAdj = 10, - LineStripAdj = 11, - TriangleListAdj = 12, - TriangleStripAdj = 13 - }; -} - -#endif // FL_RENDERER_ENUMS_HPP diff --git a/Include/FlashlightEngine/Renderer/Renderer.hpp b/Include/FlashlightEngine/Renderer/Renderer.hpp deleted file mode 100644 index ff04a46..0000000 --- a/Include/FlashlightEngine/Renderer/Renderer.hpp +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_RENDERER_HPP -#define FL_RENDERER_RENDERER_HPP - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace FlashlightEngine { - class Renderer { - public: - explicit Renderer(const std::shared_ptr& window); - ~Renderer() = default; - - Renderer(const Renderer&) = delete; - Renderer(Renderer&&) noexcept = default; - - void OnResize(UInt32 width, UInt32 height) const; - - void BeginFrame() const; - void EndFrame() const; - - inline void SetClearColor(Float32 r, Float32 g, Float32 b, Float32 a); - void UseShaderCollection(const ShaderCollection& collection) const; - void BindVertexBuffer(const Buffer& buffer, VertexType vertexType, UInt32 offset = 0) const; - void SetPrimitiveTopology(PrimitiveTopology topology) const; - void Draw(UInt32 vertexCount, UInt32 firstVertex = 0) const; - - ShaderCollection CreateShaderCollection(VertexType vertexType, - const std::filesystem::path& vertexShaderPath, - const std::filesystem::path& pixelShaderPath) const; - std::unique_ptr CreateBuffer(const void* data, - UInt32 size, - D3D11_USAGE usage, - D3D11_BIND_FLAG bindFlag, - bool hasCpuAccess = false, - D3D11_CPU_ACCESS_FLAG cpuAccess = D3D11_CPU_ACCESS_WRITE) const; - std::shared_ptr CreateSampler(D3D11_FILTER filter, - D3D11_TEXTURE_ADDRESS_MODE addressModeU = D3D11_TEXTURE_ADDRESS_WRAP, - D3D11_TEXTURE_ADDRESS_MODE addressModeV = D3D11_TEXTURE_ADDRESS_WRAP, - D3D11_TEXTURE_ADDRESS_MODE addressModeW = D3D11_TEXTURE_ADDRESS_WRAP) - const; - std::unique_ptr CreateTexture(const std::filesystem::path& path) const; - - Renderer& operator=(const Renderer&) = delete; - Renderer& operator=(Renderer&&) noexcept = default; - - private: - std::shared_ptr m_Window; - - std::shared_ptr m_Device; - std::shared_ptr m_Swapchain; - - Float32 m_ClearColor[4] = {0.1f, 0.1f, 0.1f, 0.1f}; - }; -} - -#include - -#endif // FL_RENDERER_RENDERER_HPP diff --git a/Include/FlashlightEngine/Renderer/Renderer.inl b/Include/FlashlightEngine/Renderer/Renderer.inl deleted file mode 100644 index 7556393..0000000 --- a/Include/FlashlightEngine/Renderer/Renderer.inl +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - inline void Renderer::SetClearColor(const Float32 r, const Float32 g, const Float32 b, const Float32 a) { - m_ClearColor[0] = r; - m_ClearColor[1] = g; - m_ClearColor[2] = b; - m_ClearColor[3] = a; - } -} diff --git a/Include/FlashlightEngine/Renderer/Sampler.hpp b/Include/FlashlightEngine/Renderer/Sampler.hpp deleted file mode 100644 index a3b9f17..0000000 --- a/Include/FlashlightEngine/Renderer/Sampler.hpp +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_SAMPLER_HPP -#define FL_RENDERER_SAMPLER_HPP - -#include - -#include -#include - -#include - -namespace FlashlightEngine { - class Sampler { - public: - Sampler(const std::shared_ptr& device, - D3D11_FILTER filter, - D3D11_TEXTURE_ADDRESS_MODE addressModeU = D3D11_TEXTURE_ADDRESS_WRAP, - D3D11_TEXTURE_ADDRESS_MODE addressModeV = D3D11_TEXTURE_ADDRESS_WRAP, - D3D11_TEXTURE_ADDRESS_MODE addressModeW = D3D11_TEXTURE_ADDRESS_WRAP - ); - ~Sampler(); - - Sampler(const Sampler&) = delete; - Sampler(Sampler&& other) noexcept; - - [[nodiscard]] inline ComPtr GetSampler(); - - void UseSampler(UInt32 slot); - - Sampler& operator=(const Sampler&) = delete; - Sampler& operator=(Sampler&& other) noexcept; - - private: - ComPtr m_Sampler; - - std::shared_ptr m_Device; - }; -} - -#include - -#endif // FL_RENDERER_SAMPLER_HPP diff --git a/Include/FlashlightEngine/Renderer/Sampler.inl b/Include/FlashlightEngine/Renderer/Sampler.inl deleted file mode 100644 index fd63b2c..0000000 --- a/Include/FlashlightEngine/Renderer/Sampler.inl +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - inline ComPtr Sampler::GetSampler() { - return m_Sampler; - } -} diff --git a/Include/FlashlightEngine/Renderer/Shader.hpp b/Include/FlashlightEngine/Renderer/Shader.hpp deleted file mode 100644 index 2dbf05c..0000000 --- a/Include/FlashlightEngine/Renderer/Shader.hpp +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_SHADER_HPP -#define FL_RENDERER_SHADER_HPP - -#include - -#include -#include -#include - -#include -#include -#include - -namespace FlashlightEngine { - struct ShaderCollectionDescriptor { - std::filesystem::path VertexShaderPath; - std::filesystem::path PixelShaderPath; - VertexType VertexType; - }; - - class ShaderCollection { - public: - static ShaderCollection CreateShaderCollection(const ShaderCollectionDescriptor& desc, const Device& device); - static UInt32 GetLayoutByteSize(VertexType type); - - void ApplyToContext(const Device& device) const; - void Destroy(); - - private: - [[nodiscard]] static ComPtr CreateVertexShader(const Device& device, - const std::filesystem::path& filePath, - ComPtr& vertexShaderBlob); - [[nodiscard]] static ComPtr CreatePixelShader(const Device& device, - const std::filesystem::path& filePath); - - static bool CreateInputLayout(const Device& device, - VertexType layoutInfo, - const ComPtr& vertexShaderBlob, - ComPtr& inputLayout); - - static bool CompileShader(const std::filesystem::path& filePath, - const std::string& entryPoint, - const std::string& profile, - ComPtr& shaderBlob); - - ComPtr m_VertexShader{nullptr}; - ComPtr m_PixelShader{nullptr}; - ComPtr m_InputLayout{nullptr}; - D3D11_PRIMITIVE_TOPOLOGY m_PrimitiveTopology{}; - UInt32 m_VertexSize{0}; - static std::unordered_map> m_LayoutMap; - }; -} - -#include - -#endif // FL_RENDERER_SHADER_HPP diff --git a/Include/FlashlightEngine/Renderer/Shader.inl b/Include/FlashlightEngine/Renderer/Shader.inl deleted file mode 100644 index 580658e..0000000 --- a/Include/FlashlightEngine/Renderer/Shader.inl +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - -} diff --git a/Include/FlashlightEngine/Renderer/Swapchain.hpp b/Include/FlashlightEngine/Renderer/Swapchain.hpp deleted file mode 100644 index c731aaf..0000000 --- a/Include/FlashlightEngine/Renderer/Swapchain.hpp +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_SWAPCHAIN_HPP -#define FL_RENDERER_SWAPCHAIN_HPP - -#include - -#include -#include - -#include - - -namespace FlashlightEngine { - class Swapchain { - public: - Swapchain(const std::shared_ptr& window, const std::shared_ptr& device); - ~Swapchain(); - - Swapchain(const Swapchain&) = delete; - Swapchain(Swapchain&& other) noexcept; - - [[nodiscard]] inline ComPtr GetSwapchain() const; - [[nodiscard]] inline ComPtr GetRTV() const; - - void OnResize(UInt32 width, UInt32 height); - - Swapchain& operator=(const Swapchain&) = delete; - Swapchain& operator=(Swapchain&& other) noexcept; - - private: - ComPtr m_Swapchain{nullptr}; - ComPtr m_RenderTargetView{nullptr}; - - std::shared_ptr m_Window{nullptr}; - std::shared_ptr m_Device{nullptr}; - - bool CreateSwapchain(UInt32 width, UInt32 height); - bool CreateSwapchainResources(); - void DestroySwapchainResources(); - }; -} - -#include - -#endif // FL_RENDERER_SWAPCHAIN_HPP diff --git a/Include/FlashlightEngine/Renderer/Swapchain.inl b/Include/FlashlightEngine/Renderer/Swapchain.inl deleted file mode 100644 index ca43edf..0000000 --- a/Include/FlashlightEngine/Renderer/Swapchain.inl +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - inline ComPtr Swapchain::GetSwapchain() const { - return m_Swapchain; - } - - inline ComPtr Swapchain::GetRTV() const { - return m_RenderTargetView; - } -} diff --git a/Include/FlashlightEngine/Renderer/Texture.hpp b/Include/FlashlightEngine/Renderer/Texture.hpp deleted file mode 100644 index 1b22dd6..0000000 --- a/Include/FlashlightEngine/Renderer/Texture.hpp +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_TEXTURE_HPP -#define FL_RENDERER_TEXTURE_HPP - -#include - -#include -#include - -#include -#include - -namespace FlashlightEngine { - class Texture { - public: - Texture(const std::filesystem::path& path, - const std::shared_ptr& device); - ~Texture(); - - Texture(const Texture&) = delete; - Texture(Texture&& other) noexcept; - - [[nodiscard]] inline ComPtr GetTextureSRV() const; - - void UseTexture(UInt32 slot); - - Texture& operator=(const Texture&) = delete; - Texture& operator=(Texture&& other) noexcept; - - private: - ComPtr m_TextureSrv{nullptr}; - - std::shared_ptr m_Device{nullptr}; - - void CreateTextureViewFromDDS(const std::filesystem::path& path); - void CreateTextureView(const std::filesystem::path& path); - }; -} - -#include - -#endif // FL_RENDERER_TEXTURE_HPP diff --git a/Include/FlashlightEngine/Renderer/Texture.inl b/Include/FlashlightEngine/Renderer/Texture.inl deleted file mode 100644 index cae00ab..0000000 --- a/Include/FlashlightEngine/Renderer/Texture.inl +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -namespace FlashlightEngine { - inline ComPtr Texture::GetTextureSRV() const { - return m_TextureSrv; - } -} diff --git a/Include/FlashlightEngine/Renderer/VertexTypes.hpp b/Include/FlashlightEngine/Renderer/VertexTypes.hpp deleted file mode 100644 index 7245487..0000000 --- a/Include/FlashlightEngine/Renderer/VertexTypes.hpp +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#pragma once - -#ifndef FL_RENDERER_VERTEXTYPES_HPP -#define FL_RENDERER_VERTEXTYPES_HPP - -#include - -#include - -namespace FlashlightEngine { - enum class VertexType { - PositionColor, - PositionColorUv, - }; - - using Position = DirectX::XMFLOAT3; - using Color = DirectX::XMFLOAT3; - using Uv = DirectX::XMFLOAT2; - - struct VertexPositionColor { - Position Position; - Color Color; - }; - - struct VertexPositionColorUv { - Position Position; - Color Color; - Uv Uv; - }; -} - -#endif // FL_RENDERER_VERTEXTYPES_HPP diff --git a/Source/FlashlightEngine/Application.cpp b/Source/FlashlightEngine/Application.cpp index 6ecf304..f44efee 100644 --- a/Source/FlashlightEngine/Application.cpp +++ b/Source/FlashlightEngine/Application.cpp @@ -27,12 +27,9 @@ namespace FlashlightEngine { m_Window->SetEventCallback([this](Event& e) { EventDispatcher dispatcher(e); dispatcher.Dispatch(FL_BIND_EVENT(Application::OnWindowClose)); - dispatcher.Dispatch(FL_BIND_EVENT(Application::OnWindowResize)); OnEvent(e); }); - m_Renderer = std::make_shared(m_Window); - m_Running = true; } @@ -58,9 +55,4 @@ namespace FlashlightEngine { m_Running = false; return true; } - - bool Application::OnWindowResize(const WindowResizeEvent& e) const { - m_Renderer->OnResize(e.GetWidth(), e.GetHeight()); - return true; - } } diff --git a/Source/FlashlightEngine/EngineApplication.cpp b/Source/FlashlightEngine/EngineApplication.cpp index cdf70b9..a52eb7c 100644 --- a/Source/FlashlightEngine/EngineApplication.cpp +++ b/Source/FlashlightEngine/EngineApplication.cpp @@ -10,40 +10,10 @@ namespace FlashlightEngine { EngineApplication::EngineApplication(const UInt32 width, const UInt32 height) - : Application(width, height, "Flashlight Engine ") { - m_MainShaderCollection = m_Renderer->CreateShaderCollection( - VertexType::PositionColorUv, - Filesystem::GetShadersDirectory() / "Main.vs.hlsl", - Filesystem::GetShadersDirectory() / "Main.ps.hlsl" - ); - - constexpr VertexPositionColorUv vertices[] = { - {Position{0.0f, 0.5f, 0.0f}, Color{0.25f, 0.39f, 0.19f}, Uv{0.5f, 0.0f}}, - {Position{0.5f, -0.5f, 0.0f}, Color{0.44f, 0.75f, 0.35f}, Uv{1.0f, 1.0f}}, - {Position{-0.5f, -0.5f, 0.0f}, Color{0.38f, 0.55f, 0.20f}, Uv{0.0f, 1.0f}}, - }; - - // RGB triangle - //constexpr VertexPositionColorUv vertices[] = { - // {Position{0.0f, 0.5f, 0.0f}, Color{1.0f, 0.0f, 0.0f}, Uv{0.5f, 0.0f}}, - // {Position{0.5f, -0.5f, 0.0f}, Color{0.0f, 1.0f, 0.0f}, Uv{1.0f, 1.0f}}, - // {Position{-0.5f, -0.5f, 0.0f}, Color{0.0f, 0.0f, 1.0f}, Uv{0.0f, 1.0f}}, - //}; - - m_TriangleVertexBuffer = m_Renderer->CreateBuffer(vertices, sizeof(vertices), D3D11_USAGE_IMMUTABLE, - D3D11_BIND_VERTEX_BUFFER); - - m_LinearSampler = m_Renderer->CreateSampler(D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT); - - m_FallbackTexture = m_Renderer->CreateTexture(Filesystem::GetTexturesDirectory() / "Default.png"); - m_FrogTexture = m_Renderer->CreateTexture(Filesystem::GetTexturesDirectory() / "T_Froge.dds"); - - m_Renderer->SetClearColor(0.0f, 0.2f, 0.4f, 1.0f); + : Application(width, height, "Flashlight Engine ") { } - EngineApplication::~EngineApplication() { - m_MainShaderCollection.Destroy(); - } + EngineApplication::~EngineApplication() = default; void EngineApplication::OnEvent(Event& event) { EventDispatcher dispatcher(event); @@ -54,23 +24,6 @@ namespace FlashlightEngine { } void EngineApplication::OnRender() { - m_Renderer->BeginFrame(); - - m_Renderer->UseShaderCollection(m_MainShaderCollection); - - m_Renderer->BindVertexBuffer(*m_TriangleVertexBuffer, VertexType::PositionColorUv); - - m_LinearSampler->UseSampler(0); - - if (m_UseFrogTexture) { - m_FrogTexture->UseTexture(0); - } else { - m_FallbackTexture->UseTexture(0); - } - - m_Renderer->Draw(3); - - m_Renderer->EndFrame(); } void EngineApplication::OnKeyPressed(const KeyDownEvent& event) { @@ -79,9 +32,6 @@ namespace FlashlightEngine { case Key::Escape: Close(); break; - case Key::T: - m_UseFrogTexture = !m_UseFrogTexture; - break; default: break; } diff --git a/Source/FlashlightEngine/Renderer/Buffer.cpp b/Source/FlashlightEngine/Renderer/Buffer.cpp deleted file mode 100644 index 5006c1b..0000000 --- a/Source/FlashlightEngine/Renderer/Buffer.cpp +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -#include - -namespace FlashlightEngine { - Buffer::Buffer(const std::shared_ptr& device, - const void* data, - const UInt32 size, - const D3D11_USAGE usage, - const D3D11_BIND_FLAG bindFlags, - const bool enableCpuAccess, - const D3D11_CPU_ACCESS_FLAG cpuAccess) - : m_Device(device) { - Allocate(data, size, usage, bindFlags, enableCpuAccess, cpuAccess); - } - - Buffer::~Buffer() { - Free(); - } - - Buffer::Buffer(Buffer&& other) noexcept { - m_Buffer.Swap(other.m_Buffer); - m_Device = other.m_Device; - } - - Buffer& Buffer::operator=(Buffer&& other) noexcept { - m_Buffer.Swap(other.m_Buffer); - m_Device = other.m_Device; - - return *this; - } - - void Buffer::Allocate(const void* data, - const UInt32 size, - const D3D11_USAGE usage, - const D3D11_BIND_FLAG bindFlags, - const bool enableCpuAccess, - const D3D11_CPU_ACCESS_FLAG cpuAccess) { - D3D11_BUFFER_DESC bufferInfo{}; - bufferInfo.ByteWidth = size; - bufferInfo.Usage = usage; - bufferInfo.BindFlags = bindFlags; - bufferInfo.CPUAccessFlags = enableCpuAccess ? cpuAccess : 0; - - D3D11_SUBRESOURCE_DATA resourceData{}; - resourceData.pSysMem = data; - - const HRESULT hr = m_Device->GetDevice()->CreateBuffer(&bufferInfo, &resourceData, &m_Buffer); - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create buffer. Error: {}", HResultToString(hr)); - } - } - - - void Buffer::Free() { - m_Buffer.Reset(); - } -} diff --git a/Source/FlashlightEngine/Renderer/Device.cpp b/Source/FlashlightEngine/Renderer/Device.cpp deleted file mode 100644 index 4e75dcc..0000000 --- a/Source/FlashlightEngine/Renderer/Device.cpp +++ /dev/null @@ -1,134 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -#include - -#include - -#include - -namespace FlashlightEngine { - Device::Device() { - HRESULT hr = S_OK; - - hr = CreateDXGIFactory1(IID_PPV_ARGS(&m_DxgiFactory)); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create DXGI factory. Error: {}", HResultToString(hr)); - } - - UInt32 deviceFlags = 0; -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - deviceFlags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - constexpr std::array driverTypes = { - D3D_DRIVER_TYPE_HARDWARE, - D3D_DRIVER_TYPE_WARP, - D3D_DRIVER_TYPE_REFERENCE - }; - - D3D_FEATURE_LEVEL preferredFeatureLevel = D3D_FEATURE_LEVEL_11_1; - - constexpr std::array featureLevels = { - D3D_FEATURE_LEVEL_11_1, - D3D_FEATURE_LEVEL_11_0, - D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0 - }; - - D3D_DRIVER_TYPE chosenDriverType = D3D_DRIVER_TYPE_UNKNOWN; - - for (UInt32 driverTypeIndex = 0; driverTypeIndex < driverTypes.size(); driverTypeIndex++) { - chosenDriverType = driverTypes[driverTypeIndex]; - - hr = D3D11CreateDevice(nullptr, - chosenDriverType, - nullptr, - deviceFlags, - featureLevels.data(), - static_cast(featureLevels.size()), - D3D11_SDK_VERSION, - &m_Device, - &preferredFeatureLevel, - &m_Context - ); - - if (hr == E_INVALIDARG) { - spdlog::warn("[DirectX] Cannot create DirectX 11.1 compatible device. Error: {}", HResultToString(hr)); - - // DirectX 11.0 platforms will not recognize D3D_FEATURE_LEVEL_11_1 so we need to retry without it - hr = D3D11CreateDevice(nullptr, - chosenDriverType, - nullptr, - deviceFlags, - &featureLevels[1], - static_cast(featureLevels.size() - 1), - D3D11_SDK_VERSION, - &m_Device, - &preferredFeatureLevel, - &m_Context - ); - } - - if (SUCCEEDED(hr)) { - break; - } - } - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create DirectX 11 device. Error: {}", HResultToString(hr)); - } - -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - hr = m_Device.As(&m_DebugInterface); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create debug interface. Error: {}", HResultToString(hr)); - } -#endif - - spdlog::info("[DirectX] Device created successfully."); - } - - Device::~Device() { - m_DxgiFactory.Reset(); - m_Context.Reset(); -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - m_DebugInterface->ReportLiveDeviceObjects(D3D11_RLDO_IGNORE_INTERNAL); - m_DebugInterface.Reset(); -#endif - m_Device.Reset(); - - spdlog::info("[DirectX] Device destroyed."); - } - - Device::Device(Device&& other) noexcept { - m_Device.Swap(other.m_Device); - -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - m_DebugInterface.Swap(other.m_DebugInterface); -#endif - - m_Context.Swap(other.m_Context); - - m_DxgiFactory.Swap(other.m_DxgiFactory); - } - - Device& Device::operator=(Device&& other) noexcept { - m_Device.Swap(other.m_Device); - -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - m_DebugInterface.Swap(other.m_DebugInterface); -#endif - - m_Context.Swap(other.m_Context); - - m_DxgiFactory.Swap(other.m_DxgiFactory); - - return *this; - } -} diff --git a/Source/FlashlightEngine/Renderer/Renderer.cpp b/Source/FlashlightEngine/Renderer/Renderer.cpp deleted file mode 100644 index 076d11e..0000000 --- a/Source/FlashlightEngine/Renderer/Renderer.cpp +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -namespace FlashlightEngine { - Renderer::Renderer(const std::shared_ptr& window) - : m_Window(window) { - m_Device = std::make_shared(); - m_Swapchain = std::make_shared(m_Window, m_Device); - - // Set the default primitive topology to be a triangle list. - SetPrimitiveTopology(PrimitiveTopology::TriangleList); - } - - void Renderer::OnResize(const UInt32 width, const UInt32 height) const { - m_Device->GetDeviceContext()->Flush(); - - m_Swapchain->OnResize(width, height); - } - - void Renderer::BeginFrame() const { - const ComPtr deviceContext = m_Device->GetDeviceContext(); - - D3D11_VIEWPORT viewport{}; - viewport.TopLeftX = 0; - viewport.TopLeftY = 0; - viewport.Width = static_cast(m_Window->GetWidth()); - viewport.Height = static_cast(m_Window->GetHeight()); - viewport.MinDepth = 0.0f; - viewport.MaxDepth = 1.0f; - - deviceContext->ClearRenderTargetView(m_Swapchain->GetRTV().Get(), m_ClearColor); - deviceContext->RSSetViewports(1, &viewport); - deviceContext->OMSetRenderTargets(1, m_Swapchain->GetRTV().GetAddressOf(), nullptr); - } - - void Renderer::EndFrame() const { - if (const HRESULT hr = m_Swapchain->GetSwapchain()->Present(m_Window->IsVSync() ? 1 : 0, 0); FAILED(hr)) { - spdlog::error("[DirectX] Failed to present swapchain. Error: {}", HResultToString(hr)); - if (hr == DXGI_ERROR_DEVICE_REMOVED) { - spdlog::error("[DirectX] Device removed. Reason: {}", - HResultToString(m_Device->GetDevice()->GetDeviceRemovedReason())); - } - } - } - - void Renderer::UseShaderCollection(const ShaderCollection& collection) const { - collection.ApplyToContext(*m_Device); - } - - void Renderer::BindVertexBuffer(const Buffer& buffer, const VertexType vertexType, const UInt32 offset) const { - const UInt32 stride = ShaderCollection::GetLayoutByteSize(vertexType); - m_Device->GetDeviceContext()->IASetVertexBuffers(0, 1, buffer.GetBuffer().GetAddressOf(), &stride, &offset); - } - - void Renderer::SetPrimitiveTopology(const PrimitiveTopology topology) const { - m_Device->GetDeviceContext()->IASetPrimitiveTopology( - static_cast(EnumAsInteger(topology)) - ); - } - - void Renderer::Draw(const UInt32 vertexCount, const UInt32 firstVertex) const { - m_Device->GetDeviceContext()->Draw(vertexCount, firstVertex); - } - - ShaderCollection Renderer::CreateShaderCollection(const VertexType vertexType, - const std::filesystem::path& vertexShaderPath, - const std::filesystem::path& pixelShaderPath) const { - ShaderCollectionDescriptor desc{}; - desc.VertexType = vertexType; - desc.VertexShaderPath = vertexShaderPath; - desc.PixelShaderPath = pixelShaderPath; - - ShaderCollection collection = ShaderCollection::CreateShaderCollection(desc, *m_Device); - - return collection; - } - - std::unique_ptr Renderer::CreateBuffer(const void* data, - const UInt32 size, - const D3D11_USAGE usage, - const D3D11_BIND_FLAG bindFlag, - const bool hasCpuAccess, - const D3D11_CPU_ACCESS_FLAG cpuAccess) const { - auto buffer = std::make_unique(m_Device, data, size, usage, bindFlag, hasCpuAccess, cpuAccess); - - return buffer; - } - - std::shared_ptr Renderer::CreateSampler(const D3D11_FILTER filter, - const D3D11_TEXTURE_ADDRESS_MODE addressModeU, - const D3D11_TEXTURE_ADDRESS_MODE addressModeV, - const D3D11_TEXTURE_ADDRESS_MODE addressModeW) const { - return std::make_shared(m_Device, filter, addressModeU, addressModeV, addressModeW); - } - - std::unique_ptr Renderer::CreateTexture(const std::filesystem::path& path) const { - return std::make_unique(path, m_Device); - } -} diff --git a/Source/FlashlightEngine/Renderer/Sampler.cpp b/Source/FlashlightEngine/Renderer/Sampler.cpp deleted file mode 100644 index 3f10779..0000000 --- a/Source/FlashlightEngine/Renderer/Sampler.cpp +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -#include - -namespace FlashlightEngine { - Sampler::Sampler(const std::shared_ptr& device, - const D3D11_FILTER filter, - const D3D11_TEXTURE_ADDRESS_MODE addressModeU, - const D3D11_TEXTURE_ADDRESS_MODE addressModeV, - const D3D11_TEXTURE_ADDRESS_MODE addressModeW) - : m_Device(device) { - D3D11_SAMPLER_DESC samplerDesc{}; - samplerDesc.Filter = filter; - samplerDesc.AddressU = addressModeU; - samplerDesc.AddressV = addressModeV; - samplerDesc.AddressW = addressModeW; - - const HRESULT hr = device->GetDevice()->CreateSamplerState(&samplerDesc, &m_Sampler); - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create sampler state. Error: {0}", HResultToString(hr)); - } - } - - Sampler::~Sampler() { - m_Sampler.Reset(); - } - - Sampler::Sampler(Sampler&& other) noexcept { - m_Sampler.Swap(other.m_Sampler); - - m_Device = other.m_Device; - other.m_Device = nullptr; - } - - void Sampler::UseSampler(const UInt32 slot) { - if (m_Sampler != nullptr) { - m_Device->GetDeviceContext()->PSSetSamplers(slot, 1, m_Sampler.GetAddressOf()); - } - } - - Sampler& Sampler::operator=(Sampler&& other) noexcept { - m_Sampler.Swap(other.m_Sampler); - - m_Device = other.m_Device; - other.m_Device = nullptr; - - return *this; - } -} diff --git a/Source/FlashlightEngine/Renderer/Shader.cpp b/Source/FlashlightEngine/Renderer/Shader.cpp deleted file mode 100644 index d95ed9c..0000000 --- a/Source/FlashlightEngine/Renderer/Shader.cpp +++ /dev/null @@ -1,219 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -#include - -#include - -#include - -namespace FlashlightEngine { - std::unordered_map> ShaderCollection::m_LayoutMap = { - { - VertexType::PositionColor, - { - { - "POSITION", - 0, DXGI_FORMAT_R32G32B32_FLOAT, - 0, - offsetof(VertexPositionColor, Position), - D3D11_INPUT_PER_VERTEX_DATA, - 0 - }, - { - "COLOR", - 0, - DXGI_FORMAT_R32G32B32_FLOAT, - 0, - offsetof(VertexPositionColor, Color), - D3D11_INPUT_PER_VERTEX_DATA, - 0 - } - } - }, - { - VertexType::PositionColorUv, - { - { - { - "POSITION", - 0, - DXGI_FORMAT_R32G32B32_FLOAT, - 0, - offsetof(VertexPositionColorUv, Position), - D3D11_INPUT_PER_VERTEX_DATA, - 0 - }, - { - "COLOR", - 0, - DXGI_FORMAT_R32G32B32_FLOAT, - 0, - offsetof(VertexPositionColorUv, Color), - D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA, - 0 - }, - { - "TEXCOORD", - 0, - DXGI_FORMAT_R32G32_FLOAT, - 0, - offsetof(VertexPositionColorUv, Uv), - D3D11_INPUT_PER_VERTEX_DATA, - 0 - } - } - } - } - }; - - ShaderCollection ShaderCollection::CreateShaderCollection(const ShaderCollectionDescriptor& desc, - const Device& device) { - if (!Filesystem::Exists(desc.VertexShaderPath) || !Filesystem::Exists(desc.PixelShaderPath)) { - spdlog::error("[DirectX] Shader file does not exist."); - return {}; - } - - ShaderCollection collection; - - ComPtr vertexShaderBlob; - collection.m_VertexShader = CreateVertexShader(device, desc.VertexShaderPath, vertexShaderBlob); - collection.m_PixelShader = CreatePixelShader(device, desc.PixelShaderPath); - if (!CreateInputLayout(device, desc.VertexType, vertexShaderBlob, collection.m_InputLayout)) { - spdlog::error("[DirectX] Failed to create input layout for shader collection."); - return {}; - } - - return collection; - } - - UInt32 ShaderCollection::GetLayoutByteSize(const VertexType type) { - switch (type) { - case VertexType::PositionColor: - return sizeof(VertexPositionColor); - case VertexType::PositionColorUv: - return sizeof(VertexPositionColorUv); - default: - return 0; - } - } - - ComPtr ShaderCollection::CreateVertexShader(const Device& device, - const std::filesystem::path& filePath, - ComPtr& vertexShaderBlob) { - if (!CompileShader(filePath, "Main", "vs_5_0", vertexShaderBlob)) { - spdlog::error("[DirectX] Failed to compile shader: {}", filePath.string()); - return nullptr; - } - - ComPtr vertexShader; - const HRESULT hr = device.GetDevice()->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), - vertexShaderBlob->GetBufferSize(), - nullptr, - &vertexShader); - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create vertex shader. Error: {}", HResultToString(hr)); - return nullptr; - } - - return vertexShader; - } - - ComPtr ShaderCollection::CreatePixelShader(const Device& device, - const std::filesystem::path& filePath) { - ComPtr pixelShaderBlob = nullptr; - if (!CompileShader(filePath, "Main", "ps_5_0", pixelShaderBlob)) { - spdlog::error("[DirectX] Failed to compile shader: {}", filePath.string()); - return nullptr; - } - - ComPtr pixelShader; - const HRESULT hr = device.GetDevice()->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), - pixelShaderBlob->GetBufferSize(), - nullptr, - &pixelShader); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create pixel shader. Error: {}", HResultToString(hr)); - return nullptr; - } - - return pixelShader; - } - - bool ShaderCollection::CreateInputLayout(const Device& device, - const VertexType layoutInfo, - const ComPtr& vertexShaderBlob, - ComPtr& inputLayout) { - const std::vector inputLayoutDesc = m_LayoutMap[layoutInfo]; - - const HRESULT hr = device.GetDevice()->CreateInputLayout(inputLayoutDesc.data(), - static_cast(inputLayoutDesc.size()), - vertexShaderBlob->GetBufferPointer(), - vertexShaderBlob->GetBufferSize(), - &inputLayout); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create input layout. Error: {}", HResultToString(hr)); - return false; - } - - return true; - } - - bool ShaderCollection::CompileShader(const std::filesystem::path& filePath, - const std::string& entryPoint, - const std::string& profile, - ComPtr& shaderBlob) { - UInt32 compileFlags = D3DCOMPILE_ENABLE_STRICTNESS; - -#if defined(FL_DEBUG) || defined(FL_FORCE_DX_DEBUG_INTERFACE) - compileFlags |= D3DCOMPILE_DEBUG; -#endif - - ComPtr tempShaderBlob = nullptr; - ComPtr errorBlob = nullptr; - - const HRESULT hr = D3DCompileFromFile(filePath.c_str(), - nullptr, - D3D_COMPILE_STANDARD_FILE_INCLUDE, - entryPoint.c_str(), - profile.c_str(), - compileFlags, - 0, - &tempShaderBlob, - &errorBlob); - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to compile shader: {}. Error: {}", filePath.string(), - HResultToString(hr)); - - if (errorBlob) { - spdlog::error("[DirectX] Shader compilation error: {}", - static_cast(errorBlob->GetBufferPointer())); - } - - return false; - } - - shaderBlob = tempShaderBlob; - - return true; - } - - void ShaderCollection::ApplyToContext(const Device& device) const { - if (m_InputLayout != nullptr && m_VertexShader != nullptr && m_PixelShader != nullptr) { - device.GetDeviceContext()->IASetInputLayout(m_InputLayout.Get()); - device.GetDeviceContext()->VSSetShader(m_VertexShader.Get(), nullptr, 0); - device.GetDeviceContext()->PSSetShader(m_PixelShader.Get(), nullptr, 0); - } - } - - void ShaderCollection::Destroy() { - m_InputLayout.Reset(); - m_VertexShader.Reset(); - m_PixelShader.Reset(); - } -} diff --git a/Source/FlashlightEngine/Renderer/Swapchain.cpp b/Source/FlashlightEngine/Renderer/Swapchain.cpp deleted file mode 100644 index cbad432..0000000 --- a/Source/FlashlightEngine/Renderer/Swapchain.cpp +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -#define GLFW_EXPOSE_NATIVE_WIN32 -#include - -#include - -namespace FlashlightEngine { - Swapchain::Swapchain(const std::shared_ptr& window, const std::shared_ptr& device) - : m_Window(window), m_Device(device) { - if (!CreateSwapchain(window->GetWidth(), window->GetHeight())) { - spdlog::error("[DirectX] Failed to create swapchain."); - } - - if (!CreateSwapchainResources()) { - spdlog::error("[DirectX] Failed to create swapchain resources."); - } - } - - Swapchain::~Swapchain() { - m_RenderTargetView.Reset(); - m_Swapchain.Reset(); - - spdlog::info("[DirectX] Swapchain destroyed."); - } - - Swapchain::Swapchain(Swapchain&& other) noexcept { - m_Swapchain.Swap(other.m_Swapchain); - m_RenderTargetView.Swap(other.m_RenderTargetView); - } - - Swapchain& Swapchain::operator=(Swapchain&& other) noexcept { - m_Swapchain.Swap(other.m_Swapchain); - m_RenderTargetView.Swap(other.m_RenderTargetView); - - return *this; - } - - void Swapchain::OnResize(const UInt32 width, const UInt32 height) { - DestroySwapchainResources(); - - if (const auto hr = m_Swapchain->ResizeBuffers(0, width, height, DXGI_FORMAT_B8G8R8A8_UNORM, 0); FAILED(hr)) { - spdlog::error("[DirectX] Failed to resize swapchain. Error: {}", HResultToString(hr)); - } - - - CreateSwapchainResources(); - } - - bool Swapchain::CreateSwapchain(const UInt32 width, const UInt32 height) { - m_Swapchain.Reset(); - - HRESULT hr = S_OK; - - DXGI_SWAP_CHAIN_DESC1 swapChainDesc{}; - swapChainDesc.Width = width; - swapChainDesc.Height = height; - swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; - swapChainDesc.SampleDesc.Count = 1; - swapChainDesc.SampleDesc.Quality = 0; - swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swapChainDesc.BufferCount = 2; - swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; - swapChainDesc.Scaling = DXGI_SCALING_STRETCH; - swapChainDesc.Flags = 0; - - DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreenDesc{}; - fullscreenDesc.Windowed = true; - - hr = m_Device->GetDxgiFactory()->CreateSwapChainForHwnd( - m_Device->GetDevice().Get(), - glfwGetWin32Window(m_Window->GetNativeWindow()), - &swapChainDesc, - &fullscreenDesc, - nullptr, - &m_Swapchain - ); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create swapchain. Error: {}", HResultToString(hr)); - return false; - } - - spdlog::info("[DirectX] Swapchain created."); - - return true; - } - - - bool Swapchain::CreateSwapchainResources() { - ComPtr backBuffer = nullptr; - - HRESULT hr = m_Swapchain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to get swapchain back buffer. Error: {}", HResultToString(hr)); - return false; - } - - hr = m_Device->GetDevice()->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create render target. Error: {}", HResultToString(hr)); - return false; - } - - spdlog::info("[DirectX] Render target created."); - - return true; - } - - void Swapchain::DestroySwapchainResources() { - m_RenderTargetView.Reset(); - } -} diff --git a/Source/FlashlightEngine/Renderer/Texture.cpp b/Source/FlashlightEngine/Renderer/Texture.cpp deleted file mode 100644 index 998320e..0000000 --- a/Source/FlashlightEngine/Renderer/Texture.cpp +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright (C) 2024 Jean "Pixfri" Letessier -// This file is part of FlashlightEngine. -// For conditions of distribution and use, see copyright notice in LICENSE. - -#include - -#include - -#include -#include - -#include - -namespace FlashlightEngine { - Texture::Texture(const std::filesystem::path& path, - const std::shared_ptr& device) - : m_Device(device) { - if (path.extension().string() == ".dds") { - CreateTextureViewFromDDS(path); - } else { - CreateTextureView(path); - } - } - - Texture::~Texture() { - m_TextureSrv.Reset(); - } - - Texture::Texture(Texture&& other) noexcept { - m_TextureSrv.Swap(other.m_TextureSrv); - - m_Device = other.m_Device; - other.m_Device = nullptr; - } - - void Texture::UseTexture(const UInt32 slot) { - if (m_TextureSrv != nullptr) { - m_Device->GetDeviceContext()->PSSetShaderResources(slot, 1, m_TextureSrv.GetAddressOf()); - } - } - - Texture& Texture::operator=(Texture&& other) noexcept { - m_TextureSrv.Swap(other.m_TextureSrv); - - m_Device = other.m_Device; - other.m_Device = nullptr; - - return *this; - } - - void Texture::CreateTextureViewFromDDS(const std::filesystem::path& path) { - DirectX::TexMetadata metadata{}; - DirectX::ScratchImage scratchImage; - - if (!Filesystem::Exists(path)) { - spdlog::error("[DirectXTex] Texture file does not exist: {}", path.string()); - return; - } - - HRESULT hr = DirectX::LoadFromDDSFile(path.c_str(), DirectX::DDS_FLAGS_NONE, &metadata, scratchImage); - - if (FAILED(hr)) { - spdlog::error("[DirectXTex] Failed to load texture from DDS file: {}. Error: {}", path.string(), - HResultToString(hr)); - return; - } - - ComPtr texture = nullptr; - hr = DirectX::CreateTexture(m_Device->GetDevice().Get(), scratchImage.GetImages(), scratchImage.GetImageCount(), - metadata, &texture); - if (FAILED(hr)) { - spdlog::error("[DirectXTex] Failed to create texture from DDS file: {}. Error: {}", path.string(), - HResultToString(hr)); - scratchImage.Release(); - return; - } - - ID3D11ShaderResourceView* srv = nullptr; - - hr = DirectX::CreateShaderResourceView(m_Device->GetDevice().Get(), scratchImage.GetImages(), - scratchImage.GetImageCount(), metadata, &srv); - if (FAILED(hr)) { - spdlog::error("[DirectXTex] Failed to create shader resource view from DDS file: {}. Error: {}", - path.string(), HResultToString(hr)); - scratchImage.Release(); - return; - } - - m_TextureSrv = srv; - } - - void Texture::CreateTextureView(const std::filesystem::path& path) { - FIBITMAP* image = nullptr; - // Win32 methods for opening files is called "CreateFile" counterintuitively, we make sure to tell it to only read pre-existing files - - //We open a new local scope here so we don't keep the vector in memory for the entire function call, we can get rid of the memory it holds earlier this way - { - std::vector fileDataRaw = Filesystem::ReadBytes(path); - - FIMEMORY* memHandle = FreeImage_OpenMemory(reinterpret_cast(fileDataRaw.data()), - static_cast(fileDataRaw.size())); - FREE_IMAGE_FORMAT imageFormat = FreeImage_GetFileTypeFromMemory(memHandle); - if (imageFormat == FIF_UNKNOWN) { - FreeImage_CloseMemory(memHandle); - spdlog::error("Failed to determine image format: {}", path.string()); - return; - } - image = FreeImage_LoadFromMemory(imageFormat, memHandle); - - FreeImage_CloseMemory(memHandle); - } - - FreeImage_FlipVertical(image); - - UInt32 textureWidth = FreeImage_GetWidth(image); - UInt32 textureHeight = FreeImage_GetHeight(image); - UInt32 textureBpp = FreeImage_GetBPP(image); - - D3D11_TEXTURE2D_DESC textureDesc{}; - D3D11_SUBRESOURCE_DATA initialData{}; - ComPtr texture{nullptr}; - - DXGI_FORMAT textureFormat = DXGI_FORMAT_UNKNOWN; - switch (textureBpp) { - case 8: - textureFormat = DXGI_FORMAT_R8_UNORM; - break; - case 16: - textureFormat = DXGI_FORMAT_R8G8_UNORM; - break; - case 24: - // D3D11 does not support 24 bit formats for textures, we'll need to convert - { - textureBpp = 32; - FIBITMAP* newImage = FreeImage_ConvertTo32Bits(image); - FreeImage_Unload(image); - image = newImage; - textureFormat = DXGI_FORMAT::DXGI_FORMAT_R8G8B8A8_UNORM; - } - break; - case 32: - textureFormat = DXGI_FORMAT_R8G8B8A8_UNORM; - break; - default: - { - //we could try to handle some weird bit count, but these will probably be HDR or some antique format, just exit instead. - spdlog::warn("Texture {} has nontrivial bits per pixel ({}).", path.string(), textureBpp); - } - break; - } - - textureDesc.Format = textureFormat; - textureDesc.ArraySize = 1; - textureDesc.MipLevels = 1; - textureDesc.Width = textureWidth; - textureDesc.Height = textureHeight; - textureDesc.SampleDesc.Count = 1; - textureDesc.Usage = D3D11_USAGE_IMMUTABLE; - textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; - - // Populate initial data - initialData.pSysMem = FreeImage_GetBits(image); - initialData.SysMemPitch = textureWidth * (textureBpp / 8); - - HRESULT hr = m_Device->GetDevice()->CreateTexture2D(&textureDesc, &initialData, texture.GetAddressOf()); - - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create texture from file: {}. Error: {}", path.string(), - HResultToString(hr)); - FreeImage_Unload(image); - return; - } - FreeImage_Unload(image); - - ID3D11ShaderResourceView* srv = nullptr; - D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc{}; - srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; - srvDesc.Format = textureFormat; - srvDesc.Texture2D.MipLevels = textureDesc.MipLevels; - - hr = m_Device->GetDevice()->CreateShaderResourceView(texture.Get(), &srvDesc, &srv); - if (FAILED(hr)) { - spdlog::error("[DirectX] Failed to create shader resource view from file: {}. Error: {}", path.string(), - HResultToString(hr)); - return; - } - - m_TextureSrv = srv; - } -} diff --git a/xmake.lua b/xmake.lua index eaaa6b7..e6f8040 100644 --- a/xmake.lua +++ b/xmake.lua @@ -7,7 +7,7 @@ set_project(ProjectName) add_rules("mode.debug", "mode.release") option("override_runtime", {description = "Override VS runtime to MD in release and MDd in debug.", default = true}) -option("force_validation", {description = "Force Vulkan validation layers to be enabled.", default = false}) +option("force_d3d12_debug", {description = "Force D3D12 debug utilities to be enabled.", default = false}) option("profiler", {description = "Enable the Tracy profiler.", default = false}) add_includedirs("Include") @@ -31,22 +31,28 @@ set_targetdir("./bin/$(plat)_$(arch)_$(mode)") set_warnings("allextra") set_allowedplats("windows", "mingw") +add_cxflags("-Wno-missing-field-initializers -Werror=vla", {tools = {"clang", "gcc"}}) + +add_requires( + "freeimage", + "stb", + "glfw", + "spdlog", + "directxmath", + "directx-headers", + "directxshadercompiler" +) + if is_plat("windows") then if has_config("override_runtime") then set_runtimes(is_mode("debug") and "MDd" or "MD") end end -if has_config("force_validation") then - add_defines("FL_FORCE_DX_DEBUG_INTERFACE") +if has_config("force_d3d12_debug") then + add_defines("FL_FORCE_D3D12_DEBUG") end -add_cxflags("-Wno-missing-field-initializers -Werror=vla", {tools = {"clang", "gcc"}}) - -add_requires("freeimage", "stb", "glfw 3.4", "spdlog") -add_requires("imgui", {configs = {dx11 = true, glfw = true}}) -add_requires("directxmath", "directxtex") - if has_config("profiler") then add_defines("FL_PROFILER_ENABLED") add_requires("tracy") @@ -57,23 +63,54 @@ rule("cp-resources") os.cp("Resources", "./bin/$(plat)_$(arch)_$(mode)/") end) +rule("cp-d3d12") + after_build(function(target) + os.cp("Deps/D3D12", "./bin/$(plat)_$(arch)_$(mode)/") + if is_mode("debug") or has_config("force_d3d12_debug") then + os.cp("Deps/PIX/bin/WinPixEventRuntime.dll", "./bin/$(plat)_$(arch)_$(mode)/") + end + end) + target(ProjectName) set_kind("binary") - add_rules("cp-resources") + add_rules("cp-resources", "cp-d3d12") add_files("Source/**.cpp") + add_includedirs("Deps/PIX/include") + add_linkdirs("Deps/PIX/lib") + for _, ext in ipairs({".hpp", ".inl"}) do add_headerfiles("Include/**" .. ext) end - add_packages("freeimage", "stb", "glfw", "spdlog", "imgui") - add_packages("directxmath", "directxtex") + add_packages( + "freeimage", + "stb", + "glfw", + "spdlog", + "directxmath", + "directx-headers", + "directxshadercompiler" + ) + if has_config("profiler") then add_packages("tracy") end - add_syslinks("d3d11", "user32", "kernel32", "dxgi", "dxguid", "WinMM", "d3dcompiler") + add_syslinks( + "d3d12", + "user32", + "kernel32", + "dxgi", + "dxguid", + "WinMM" + ) + + if is_mode("debug") or has_config("force_d3d12_debug") then + add_syslinks("WinPixEventRuntime.lib") + add_defines("USE_PIX") + end add_rpathdirs("$ORIGIN")