diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp index 9dca730..504fc6e 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp @@ -1,6 +1,8 @@ #include "ltpch.h" #include "dxGraphicsContext.h" +#include "dxSharedContext.h" + // Required for forward declaration #include "Graphics/Renderer.h" #include "Graphics/RenderCommand.h" @@ -16,65 +18,100 @@ #define GLFW_EXPOSE_NATIVE_WIN32 #include -#include "dxSharedContext.h" - namespace Light { dxGraphicsContext::dxGraphicsContext(GLFWwindow* windowHandle) : m_WindowHandle(windowHandle) { + // DirectX API m_GraphicsAPI = GraphicsAPI::DirectX; - // swap chain desc + // setup + SetupDeviceAndSwapChain(windowHandle); + SetupRenderTargets(); + SetupDebugInterface(); + + // create shared context + m_SharedContext = std::make_shared(m_Device, m_DeviceContext, m_SwapChain, m_RenderTargetView); + } + + + void dxGraphicsContext::SetupDeviceAndSwapChain(GLFWwindow* windowHandle) + { + //* swap chain desc *// DXGI_SWAP_CHAIN_DESC sd = { 0 }; - sd.OutputWindow = static_cast(glfwGetWin32Window(windowHandle)); - - sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - sd.BufferCount = 1u; + // buffer desc sd.BufferDesc.Width = 800u; sd.BufferDesc.Height = 600u; - sd.BufferDesc.RefreshRate.Denominator = NULL; - sd.BufferDesc.RefreshRate.Numerator = NULL; + sd.BufferDesc.RefreshRate.Numerator = NULL; // :#todo + sd.BufferDesc.RefreshRate.Denominator = NULL; // :#todo sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; + sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; - sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; - + // sample desc (for multi sampling) + // #todo: implement multi-sampling sd.SampleDesc.Count = 1u; sd.SampleDesc.Quality = 0u; + // #todo: support swap chains with more than 1 back-buffer + sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + sd.BufferCount = 1u; + + // #todo: don't handle Windows's window with glfw, create it yourself + sd.OutputWindow = static_cast(glfwGetWin32Window(windowHandle)); sd.Windowed = true; + + sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; + sd.Flags = NULL; - UINT flags = NULL; + UINT flags = NULL; #ifdef LIGHT_DEBUG flags = D3D11_CREATE_DEVICE_DEBUG; #endif // create device and swap chain HRESULT hr; - DXC(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, - NULL, flags, NULL, NULL, D3D11_SDK_VERSION, - &sd, &m_SwapChain, &m_Device, NULL, &m_DeviceContext)); + DXC(D3D11CreateDeviceAndSwapChain(nullptr, + D3D_DRIVER_TYPE_HARDWARE, + NULL, + flags, + nullptr, + NULL, + D3D11_SDK_VERSION, + &sd, + &m_SwapChain, + &m_Device, + nullptr, + &m_DeviceContext)); + } + void dxGraphicsContext::SetupRenderTargets() + { // set primitive topology m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // create render target view Microsoft::WRL::ComPtr backBuffer; + + HRESULT hr; DXC(m_SwapChain->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer)); DXC(m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView)); // set render target view m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr); + } + void dxGraphicsContext::SetupDebugInterface() + { #ifdef LIGHT_DEBUG // configure the debug interface Microsoft::WRL::ComPtr infoQueue; + HRESULT hr; DXC(m_Device.As(&m_DebugInterface)); DXC(m_DebugInterface.As(&infoQueue)); @@ -85,20 +122,12 @@ namespace Light { // #todo: add more message IDs here as needed }; - D3D11_INFO_QUEUE_FILTER filter; - memset(&filter, 0, sizeof(filter)); + D3D11_INFO_QUEUE_FILTER filter = { 0 }; filter.DenyList.NumIDs = _countof(hide); filter.DenyList.pIDList = hide; - infoQueue->AddStorageFilterEntries(&filter); + + DXC(infoQueue->AddStorageFilterEntries(&filter)); #endif - - // create shared context - m_SharedContext = std::make_shared(m_Device, m_DeviceContext, m_SwapChain, m_RenderTargetView); - } - - void dxGraphicsContext::OnWindowResize(const WindowResizedEvent& event) - { - SetResolution(event.GetSize()); } void dxGraphicsContext::SetResolution(const glm::uvec2& resolution) diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h index 8c04456..5235776 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h @@ -32,6 +32,11 @@ namespace Light { virtual void LogDebugData() override; private: + void SetupDeviceAndSwapChain(GLFWwindow* windowHandle); + void SetupRenderTargets(); + void SetupDebugInterface(); + + void SetResolution(const glm::uvec2& resolution); }; diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp index 952d790..4fd4933 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp @@ -10,10 +10,6 @@ namespace Light { { } - dxRenderCommand::~dxRenderCommand() - { - } - void dxRenderCommand::SwapBuffers() { #ifdef LIGHT_DEBUG diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h index 858ec04..c3b22c2 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h @@ -17,7 +17,6 @@ namespace Light { public: dxRenderCommand(std::shared_ptr sharedContext); - ~dxRenderCommand(); virtual void SwapBuffers() override; virtual void ClearBackBuffer() override; diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp index 45ff374..9a395a6 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp @@ -9,7 +9,6 @@ namespace Light { dxVertexLayout::dxVertexLayout(Shader* shader, const std::vector>& elements, std::shared_ptr sharedContext) : m_Context(sharedContext) { - // local std::vector inputElementsDesc; inputElementsDesc.reserve(elements.size()); @@ -27,12 +26,11 @@ namespace Light { 0u }); } - // #todo: take in shared_ptr dxShader* dxpShader = static_cast(shader); LT_ENGINE_ASSERT(dxpShader, "dxVertexLayout::dxVertexLayout: failed to cast Shader to dxShader"); - // create input layout ( vertex layout ) + // create input layout (vertex layout) HRESULT hr; DXC(m_Context->device->CreateInputLayout(&inputElementsDesc[0], inputElementsDesc.size(), dxpShader->GetVertexBlob().Get()->GetBufferPointer(), dxpShader->GetVertexBlob().Get()->GetBufferSize(), &m_InputLayout)); } diff --git a/Engine/src/Platform/OS/Windows/wWindow.h b/Engine/src/Platform/OS/Windows/wWindow.h index d45818b..cbc8875 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.h +++ b/Engine/src/Platform/OS/Windows/wWindow.h @@ -12,6 +12,7 @@ namespace Light { class wWindow : public Window { private: + // #todo: don't handle Windows's window with glfw, create it yourself GLFWwindow* m_Handle = nullptr; std::function m_EventCallback;