From 5cc82f1558a12ffd42a633922df896958afefaa9 Mon Sep 17 00:00:00 2001 From: Light Date: Thu, 22 Jul 2021 13:00:41 +0430 Subject: [PATCH] Maintenance - Added 'Get*Ref()' to 'dxSharedContext' - Fixed 'dxFramebuffer::Resize' not resizing :/ - Fixed 'dxFrameBuffer::BindAsTarget' not setting the viewport - Fixed 'dxRenderCommand::SetViewport()' not resizing the swapchain's buffer - Improved 'dxGraphicsContext''s debug interface - Removed most of the 'ComPtr's in 'dxGraphicsContext', they can be accessed with the 'm_SharedContext' --- Engine/src/Engine/Base.h | 24 ++++++--- .../GraphicsAPI/DirectX/dxFramebuffer.cpp | 22 +++++++- .../GraphicsAPI/DirectX/dxGraphicsContext.cpp | 52 +++++++++++-------- .../GraphicsAPI/DirectX/dxGraphicsContext.h | 6 --- .../GraphicsAPI/DirectX/dxRenderCommand.cpp | 23 ++++++++ .../GraphicsAPI/DirectX/dxRenderCommand.h | 3 ++ .../GraphicsAPI/DirectX/dxSharedContext.h | 21 ++++---- 7 files changed, 104 insertions(+), 47 deletions(-) diff --git a/Engine/src/Engine/Base.h b/Engine/src/Engine/Base.h index bbfe9ea..691164d 100644 --- a/Engine/src/Engine/Base.h +++ b/Engine/src/Engine/Base.h @@ -11,10 +11,10 @@ // version -#define LT_VERSION "0.7.4" +#define LT_VERSION "0.7.5b" ///*** [ CHANGE_LOG ] ***/// // -------------------------------------------------------------------- -// Note: change log starts from 2021-07-21, the starting version is 0.7.0, +// Note: change log starts from 2021-07-21, the starting version is 0.7.0a, // I came up with that version because of: // projects: 'Engine', 'Sandbox', 'Mirror' [+0.3] // graphics apis: 'OpenGL', 'DirectX11' [+0.2] @@ -22,16 +22,16 @@ // -------------------------------------------------------------------- // // -// 0.7.0: started the change log +// 0.7.0a: started the change log // -// 0.7.1: [ LT_BREAK ] +// 0.7.1a: [ LT_BREAK ] // - Added the 'LT_BERAK' macro, a portable debug-trap // -// 0.7.2: [ Failed engine/client assertion ] +// 0.7.2a: [ Failed engine/client assertion ] // - Separated 'FailedAssertion' into 'FailedEngineAssertion' and 'FailedClientAssertion' // - Minor adjustment to the change log // -// 0.7.3: [ Layer Improvements ] +// 0.7.3a: [ Layer Improvements ] // - Added 'Layer::OnEvent()', 'Layer' now handles an event by itself and doesn't need another class to determine the event's type // // - Added reverse iterators 'rend()' and 'rbegin()' to 'LayerStack' @@ -44,11 +44,21 @@ // // - Fixed a typo where in 'Mirror' where the name of the 'MirrorLayer' was "SandboxLayer" instead of "MirrorLayer" // -// 0.7.4 [ Input ] +// 0.7.4a: [ Input ] // - Added 'Input' // - - Added // - The 'MirrorLayer''s ImGuiWindow, "GameView" will not receive/react to input events if the window is not focused // +// 0.7.4b [ Maintenance ] +// - Added 'Get*Ref()' to 'dxSharedContext' +// +// - Fixed 'dxFramebuffer::Resize' not resizing : / +// - Fixed 'dxFrameBuffer::BindAsTarget' not setting the viewport +// - Fixed 'dxRenderCommand::SetViewport()' not resizing the swapchain's buffer +// +// - Improved 'dxGraphicsContext''s debug interface +// +// - Removed most of the 'ComPtr's in 'dxGraphicsContext', they can be accessed with the 'm_SharedContext' // ///*** [ CHANGE_LOG ] ***/// diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxFramebuffer.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxFramebuffer.cpp index e27b865..71a4d06 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxFramebuffer.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxFramebuffer.cpp @@ -47,6 +47,20 @@ namespace Light { m_Context->GetDeviceContext()->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr); m_Context->GetDeviceContext()->ClearRenderTargetView(m_RenderTargetView.Get(), color); + + D3D11_VIEWPORT viewport; + + viewport.TopLeftX = 0; + viewport.TopLeftY = 0; + + viewport.Width = m_Specification.width; + viewport.Height = m_Specification.height; + + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; + + // set viewport + m_Context->GetDeviceContext()->RSSetViewports(1u, &viewport); } void dxFramebuffer::BindAsResource() @@ -56,14 +70,20 @@ namespace Light { void dxFramebuffer::Resize(const glm::vec2& size) { + m_Specification.width = std::clamp(size.x, 1.0f, 16384.0f); + m_Specification.height= std::clamp(size.y, 1.0f, 16384.0f); + D3D11_TEXTURE2D_DESC textureDesc; D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; - D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; m_ColorAttachment->GetDesc(&textureDesc); m_RenderTargetView->GetDesc(&rtvDesc); m_ResourceView->GetDesc(&srvDesc); + textureDesc.Width = m_Specification.width; + textureDesc.Height = m_Specification.height; + HRESULT hr; DXC(m_Context->GetDevice()->CreateTexture2D(&textureDesc, nullptr, &m_ColorAttachment)); DXC(m_Context->GetDevice()->CreateRenderTargetView(m_ColorAttachment.Get(), &rtvDesc, &m_RenderTargetView)); diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp index 03ddd8a..51b1fdb 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp @@ -21,18 +21,19 @@ namespace Light { { // set 'GraphicsAPI'; m_GraphicsAPI = GraphicsAPI::DirectX; + + m_SharedContext = std::make_shared(); // setup stuff SetupDeviceAndSwapChain(windowHandle); SetupRenderTargets(); SetupDebugInterface(); - - // create 'dxSharedContext' - m_SharedContext = std::make_shared(m_Device, m_DeviceContext, m_SwapChain, m_RenderTargetView); } void dxGraphicsContext::SetupDeviceAndSwapChain(GLFWwindow* windowHandle) { + std::shared_ptr context = std::static_pointer_cast(m_SharedContext); + // swap chain desc DXGI_SWAP_CHAIN_DESC sd = { 0 }; @@ -77,61 +78,70 @@ namespace Light { NULL, D3D11_SDK_VERSION, &sd, - &m_SwapChain, - &m_Device, + &context->GetSwapChainRef(), + &context->GetDeviceRef(), nullptr, - &m_DeviceContext)); + &context->GetDeviceContextRef())); + } void dxGraphicsContext::SetupRenderTargets() { + std::shared_ptr context = std::static_pointer_cast(m_SharedContext); + // set primitive topology - m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + context->GetDeviceContext()->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)); + DXC(context->GetSwapChain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer)); + DXC(context->GetDevice()->CreateRenderTargetView(backBuffer.Get(), nullptr, &context->GetRenderTargetViewRef())); // set render target view - m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr); + context->GetDeviceContext()->OMSetRenderTargets(1u, context->GetRenderTargetView().GetAddressOf(), nullptr); } void dxGraphicsContext::SetupDebugInterface() { #ifdef LIGHT_DEBUG - // configure the debug interface - Microsoft::WRL::ComPtr infoQueue; + std::shared_ptr context = std::static_pointer_cast(m_SharedContext); HRESULT hr; - DXC(m_Device.As(&m_DebugInterface)); - DXC(m_DebugInterface.As(&infoQueue)); + Microsoft::WRL::ComPtr debugInterface = nullptr; + DXC(context->GetDevice()->QueryInterface(__uuidof(ID3D11Debug), &debugInterface)); + + Microsoft::WRL::ComPtr infoQueue = nullptr; + DXC(debugInterface->QueryInterface(__uuidof(ID3D11InfoQueue), &infoQueue)); + + infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true); + infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true); D3D11_MESSAGE_ID hide[] = { - D3D11_MESSAGE_ID_DEVICE_DRAW_SAMPLER_NOT_SET, - - // #todo: add more message ids here as needed + D3D11_MESSAGE_ID_UNKNOWN, + // #todo: add more messages here as needed }; - D3D11_INFO_QUEUE_FILTER filter = { 0 }; + D3D11_INFO_QUEUE_FILTER filter = { }; filter.DenyList.NumIDs = _countof(hide); filter.DenyList.pIDList = hide; - - DXC(infoQueue->AddStorageFilterEntries(&filter)); + infoQueue->AddStorageFilterEntries(&filter); + infoQueue->Release(); #endif } void dxGraphicsContext::LogDebugData() { + std::shared_ptr context = std::static_pointer_cast(m_SharedContext); + // locals IDXGIDevice* DXGIDevice; IDXGIAdapter* DXGIAdapter; DXGI_ADAPTER_DESC DXGIAdapterDesc; - m_Device->QueryInterface(__uuidof(IDXGIDevice), (void**)&DXGIDevice); + context->GetDevice()->QueryInterface(__uuidof(IDXGIDevice), (void**)&DXGIDevice); DXGIDevice->GetAdapter(&DXGIAdapter); DXGIAdapter->GetDesc(&DXGIAdapterDesc); diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h index 1c46cce..9f368ec 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h @@ -14,12 +14,6 @@ namespace Light { { private: GLFWwindow* m_WindowHandle; - - Microsoft::WRL::ComPtr m_Device; - Microsoft::WRL::ComPtr m_DeviceContext; - Microsoft::WRL::ComPtr m_SwapChain; - Microsoft::WRL::ComPtr m_RenderTargetView; - Microsoft::WRL::ComPtr m_DebugInterface; public: diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp index e54ec0f..e213f56 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp @@ -49,6 +49,9 @@ namespace Light { void dxRenderCommand::SetViewport(unsigned int x, unsigned int y, unsigned int width, unsigned int height) { + // #todo: maybe call this somewhere else?? + SetResolution(width, height); + // create viewport D3D11_VIEWPORT viewport; @@ -65,4 +68,24 @@ namespace Light { m_Context->GetDeviceContext()->RSSetViewports(1u, &viewport); } + void dxRenderCommand::SetResolution(unsigned int width, unsigned int height) + { + HRESULT hr; + + // remove render target + ID3D11RenderTargetView* nullViews[] = { nullptr }; + m_Context->GetDeviceContext()->OMSetRenderTargets(1u, nullViews, nullptr); + m_Context->GetRenderTargetViewRef().Reset(); + + // resize buffer + DXC(m_Context->GetSwapChain()->ResizeBuffers(0u, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, NULL)); + + // create render target + Microsoft::WRL::ComPtr backBuffer = nullptr; + DXC(m_Context->GetSwapChain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer)); + DXC(m_Context->GetDevice()->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_Context->GetRenderTargetViewRef())); + // set render target + m_Context->GetDeviceContext()->OMSetRenderTargets(1, m_Context->GetRenderTargetView().GetAddressOf(), nullptr); + } + } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h index 9ade258..b6597d5 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h @@ -27,6 +27,9 @@ namespace Light { virtual void DefaultTargetFramebuffer() override; virtual void SetViewport(unsigned int x, unsigned int y, unsigned int width, unsigned int height) override; + + private: + void SetResolution(unsigned int width, unsigned int height); }; } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h index 14a1924..d57c9ae 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h @@ -12,24 +12,21 @@ namespace Light { class dxSharedContext : public SharedContext { private: - Microsoft::WRL::ComPtr m_Device; - Microsoft::WRL::ComPtr m_DeviceContext; - Microsoft::WRL::ComPtr m_SwapChain; - Microsoft::WRL::ComPtr m_RenderTargetView; + Microsoft::WRL::ComPtr m_Device = nullptr; + Microsoft::WRL::ComPtr m_DeviceContext = nullptr; + Microsoft::WRL::ComPtr m_SwapChain = nullptr; + Microsoft::WRL::ComPtr m_RenderTargetView = nullptr; public: - dxSharedContext(Microsoft::WRL::ComPtr device, - Microsoft::WRL::ComPtr deviceContext, - Microsoft::WRL::ComPtr swapChain, - Microsoft::WRL::ComPtr renderTargetView) - : m_Device(device), m_DeviceContext(deviceContext), m_SwapChain(swapChain), m_RenderTargetView(renderTargetView) - { - } - inline Microsoft::WRL::ComPtr GetDevice () { return m_Device; } inline Microsoft::WRL::ComPtr GetDeviceContext () { return m_DeviceContext; } inline Microsoft::WRL::ComPtr GetSwapChain () { return m_SwapChain; } inline Microsoft::WRL::ComPtr GetRenderTargetView() { return m_RenderTargetView; } + + inline Microsoft::WRL::ComPtr& GetDeviceRef () { return m_Device; } + inline Microsoft::WRL::ComPtr& GetDeviceContextRef () { return m_DeviceContext; } + inline Microsoft::WRL::ComPtr& GetSwapChainRef () { return m_SwapChain; } + inline Microsoft::WRL::ComPtr& GetRenderTargetViewRef() { return m_RenderTargetView; } }; } \ No newline at end of file