Maintanance

- Some tidying
- dxGraphicsContext's constructor been split up into multiple Setup functions.
This commit is contained in:
Light 2021-06-21 18:08:25 +04:30
parent 0fc1896854
commit f8b35c9572
6 changed files with 63 additions and 35 deletions

View file

@ -1,6 +1,8 @@
#include "ltpch.h" #include "ltpch.h"
#include "dxGraphicsContext.h" #include "dxGraphicsContext.h"
#include "dxSharedContext.h"
// Required for forward declaration // Required for forward declaration
#include "Graphics/Renderer.h" #include "Graphics/Renderer.h"
#include "Graphics/RenderCommand.h" #include "Graphics/RenderCommand.h"
@ -16,65 +18,100 @@
#define GLFW_EXPOSE_NATIVE_WIN32 #define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw/glfw3native.h> #include <glfw/glfw3native.h>
#include "dxSharedContext.h"
namespace Light { namespace Light {
dxGraphicsContext::dxGraphicsContext(GLFWwindow* windowHandle) dxGraphicsContext::dxGraphicsContext(GLFWwindow* windowHandle)
: m_WindowHandle(windowHandle) : m_WindowHandle(windowHandle)
{ {
// DirectX API
m_GraphicsAPI = GraphicsAPI::DirectX; m_GraphicsAPI = GraphicsAPI::DirectX;
// swap chain desc // setup
SetupDeviceAndSwapChain(windowHandle);
SetupRenderTargets();
SetupDebugInterface();
// create shared context
m_SharedContext = std::make_shared<dxSharedContext>(m_Device, m_DeviceContext, m_SwapChain, m_RenderTargetView);
}
void dxGraphicsContext::SetupDeviceAndSwapChain(GLFWwindow* windowHandle)
{
//* swap chain desc *//
DXGI_SWAP_CHAIN_DESC sd = { 0 }; DXGI_SWAP_CHAIN_DESC sd = { 0 };
sd.OutputWindow = static_cast<HWND>(glfwGetWin32Window(windowHandle));
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1u;
// buffer desc
sd.BufferDesc.Width = 800u; sd.BufferDesc.Width = 800u;
sd.BufferDesc.Height = 600u; sd.BufferDesc.Height = 600u;
sd.BufferDesc.RefreshRate.Denominator = NULL; sd.BufferDesc.RefreshRate.Numerator = NULL; // :#todo
sd.BufferDesc.RefreshRate.Numerator = NULL; sd.BufferDesc.RefreshRate.Denominator = NULL; // :#todo
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_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.Count = 1u;
sd.SampleDesc.Quality = 0u; 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<HWND>(glfwGetWin32Window(windowHandle));
sd.Windowed = true; sd.Windowed = true;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = NULL; sd.Flags = NULL;
UINT flags = NULL;
UINT flags = NULL;
#ifdef LIGHT_DEBUG #ifdef LIGHT_DEBUG
flags = D3D11_CREATE_DEVICE_DEBUG; flags = D3D11_CREATE_DEVICE_DEBUG;
#endif #endif
// create device and swap chain // create device and swap chain
HRESULT hr; HRESULT hr;
DXC(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, DXC(D3D11CreateDeviceAndSwapChain(nullptr,
NULL, flags, NULL, NULL, D3D11_SDK_VERSION, D3D_DRIVER_TYPE_HARDWARE,
&sd, &m_SwapChain, &m_Device, NULL, &m_DeviceContext)); NULL,
flags,
nullptr,
NULL,
D3D11_SDK_VERSION,
&sd,
&m_SwapChain,
&m_Device,
nullptr,
&m_DeviceContext));
}
void dxGraphicsContext::SetupRenderTargets()
{
// set primitive topology // set primitive topology
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// create render target view // create render target view
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer; Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer;
HRESULT hr;
DXC(m_SwapChain->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer)); DXC(m_SwapChain->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer));
DXC(m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView)); DXC(m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView));
// set render target view // set render target view
m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr); m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr);
}
void dxGraphicsContext::SetupDebugInterface()
{
#ifdef LIGHT_DEBUG #ifdef LIGHT_DEBUG
// configure the debug interface // configure the debug interface
Microsoft::WRL::ComPtr<ID3D11InfoQueue> infoQueue; Microsoft::WRL::ComPtr<ID3D11InfoQueue> infoQueue;
HRESULT hr;
DXC(m_Device.As(&m_DebugInterface)); DXC(m_Device.As(&m_DebugInterface));
DXC(m_DebugInterface.As(&infoQueue)); DXC(m_DebugInterface.As(&infoQueue));
@ -85,20 +122,12 @@ namespace Light {
// #todo: add more message IDs here as needed // #todo: add more message IDs here as needed
}; };
D3D11_INFO_QUEUE_FILTER filter; D3D11_INFO_QUEUE_FILTER filter = { 0 };
memset(&filter, 0, sizeof(filter));
filter.DenyList.NumIDs = _countof(hide); filter.DenyList.NumIDs = _countof(hide);
filter.DenyList.pIDList = hide; filter.DenyList.pIDList = hide;
infoQueue->AddStorageFilterEntries(&filter);
DXC(infoQueue->AddStorageFilterEntries(&filter));
#endif #endif
// create shared context
m_SharedContext = std::make_shared<dxSharedContext>(m_Device, m_DeviceContext, m_SwapChain, m_RenderTargetView);
}
void dxGraphicsContext::OnWindowResize(const WindowResizedEvent& event)
{
SetResolution(event.GetSize());
} }
void dxGraphicsContext::SetResolution(const glm::uvec2& resolution) void dxGraphicsContext::SetResolution(const glm::uvec2& resolution)

View file

@ -32,6 +32,11 @@ namespace Light {
virtual void LogDebugData() override; virtual void LogDebugData() override;
private: private:
void SetupDeviceAndSwapChain(GLFWwindow* windowHandle);
void SetupRenderTargets();
void SetupDebugInterface();
void SetResolution(const glm::uvec2& resolution); void SetResolution(const glm::uvec2& resolution);
}; };

View file

@ -10,10 +10,6 @@ namespace Light {
{ {
} }
dxRenderCommand::~dxRenderCommand()
{
}
void dxRenderCommand::SwapBuffers() void dxRenderCommand::SwapBuffers()
{ {
#ifdef LIGHT_DEBUG #ifdef LIGHT_DEBUG

View file

@ -17,7 +17,6 @@ namespace Light {
public: public:
dxRenderCommand(std::shared_ptr<dxSharedContext> sharedContext); dxRenderCommand(std::shared_ptr<dxSharedContext> sharedContext);
~dxRenderCommand();
virtual void SwapBuffers() override; virtual void SwapBuffers() override;
virtual void ClearBackBuffer() override; virtual void ClearBackBuffer() override;

View file

@ -9,7 +9,6 @@ namespace Light {
dxVertexLayout::dxVertexLayout(Shader* shader, const std::vector<std::pair<std::string, VertexElementType>>& elements, std::shared_ptr<dxSharedContext> sharedContext) dxVertexLayout::dxVertexLayout(Shader* shader, const std::vector<std::pair<std::string, VertexElementType>>& elements, std::shared_ptr<dxSharedContext> sharedContext)
: m_Context(sharedContext) : m_Context(sharedContext)
{ {
// local // local
std::vector<D3D11_INPUT_ELEMENT_DESC> inputElementsDesc; std::vector<D3D11_INPUT_ELEMENT_DESC> inputElementsDesc;
inputElementsDesc.reserve(elements.size()); inputElementsDesc.reserve(elements.size());
@ -27,12 +26,11 @@ namespace Light {
0u }); 0u });
} }
// #todo: take in shared_ptr // #todo: take in shared_ptr
dxShader* dxpShader = static_cast<dxShader*>(shader); dxShader* dxpShader = static_cast<dxShader*>(shader);
LT_ENGINE_ASSERT(dxpShader, "dxVertexLayout::dxVertexLayout: failed to cast Shader to dxShader"); LT_ENGINE_ASSERT(dxpShader, "dxVertexLayout::dxVertexLayout: failed to cast Shader to dxShader");
// create input layout ( vertex layout ) // create input layout (vertex layout)
HRESULT hr; HRESULT hr;
DXC(m_Context->device->CreateInputLayout(&inputElementsDesc[0], inputElementsDesc.size(), dxpShader->GetVertexBlob().Get()->GetBufferPointer(), dxpShader->GetVertexBlob().Get()->GetBufferSize(), &m_InputLayout)); DXC(m_Context->device->CreateInputLayout(&inputElementsDesc[0], inputElementsDesc.size(), dxpShader->GetVertexBlob().Get()->GetBufferPointer(), dxpShader->GetVertexBlob().Get()->GetBufferSize(), &m_InputLayout));
} }

View file

@ -12,6 +12,7 @@ namespace Light {
class wWindow : public Window class wWindow : public Window
{ {
private: private:
// #todo: don't handle Windows's window with glfw, create it yourself
GLFWwindow* m_Handle = nullptr; GLFWwindow* m_Handle = nullptr;
std::function<void(Event&)> m_EventCallback; std::function<void(Event&)> m_EventCallback;