light/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp

161 lines
5.3 KiB
C++
Raw Normal View History

2022-03-08 21:19:19 +03:30
#include "dxGraphicsContext.hpp"
#include "Events/WindowEvents.hpp"
#include "Graphics/Blender.hpp" // required for forward declaration
#include "Graphics/Buffers.hpp" // required for forward declaration
#include "Graphics/RenderCommand.hpp" // required for forward declaration
#include "Graphics/Renderer.hpp" // required for forward declaration
#include "UserInterface/UserInterface.hpp" // required for forward declaration
#include "Utility/ResourceManager.hpp" // required for forward declaration
#include "dxSharedContext.hpp"
2021-06-02 09:07:45 +04:30
#define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw/glfw3.h>
2021-06-02 09:07:45 +04:30
#include <glfw/glfw3native.h>
namespace Light {
2022-03-07 21:57:00 +03:30
dxGraphicsContext::dxGraphicsContext(GLFWwindow* windowHandle)
: m_WindowHandle(windowHandle), m_DebugInterface(nullptr)
{
// set 'GraphicsAPI';
m_GraphicsAPI = GraphicsAPI::DirectX;
m_SharedContext = std::make_shared<dxSharedContext>();
// setup stuff
SetupDeviceAndSwapChain(windowHandle);
SetupRenderTargets();
SetupDebugInterface();
}
void dxGraphicsContext::SetupDeviceAndSwapChain(GLFWwindow* windowHandle)
{
Ref<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
// swap chain desc
DXGI_SWAP_CHAIN_DESC sd = { 0 };
// buffer desc
sd.BufferDesc.Width = 1u;
sd.BufferDesc.Height = 1u;
sd.BufferDesc.RefreshRate.Numerator = NULL; // :#todo
sd.BufferDesc.RefreshRate.Denominator = NULL; // :#todo
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
// sample desc (for multi sampling) #todo: implement multi-samplingz
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<HWND>(glfwGetWin32Window(windowHandle));
sd.Windowed = true;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = NULL;
// determine device flags
UINT flags = NULL;
2021-06-15 09:39:11 +04:30
#ifdef LIGHT_DEBUG
2022-03-07 21:57:00 +03:30
flags = D3D11_CREATE_DEVICE_DEBUG;
2021-06-15 09:39:11 +04:30
#endif
2021-06-02 09:07:45 +04:30
2022-03-07 21:57:00 +03:30
// create device and swap chain
DXC(D3D11CreateDeviceAndSwapChain(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
flags,
nullptr,
NULL,
D3D11_SDK_VERSION,
&sd,
&context->GetSwapChainRef(),
&context->GetDeviceRef(),
nullptr,
&context->GetDeviceContextRef()));
}
void dxGraphicsContext::SetupRenderTargets()
{
Ref<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
// set primitive topology
context->GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// create render target view
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer;
DXC(context->GetSwapChain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer));
DXC(context->GetDevice()->CreateRenderTargetView(backBuffer.Get(), nullptr, &context->GetRenderTargetViewRef()));
// set render target view
context->GetDeviceContext()->OMSetRenderTargets(1u, context->GetRenderTargetView().GetAddressOf(), nullptr);
}
void dxGraphicsContext::SetupDebugInterface()
{
#ifdef LIGHT_DEBUG
2022-03-07 21:57:00 +03:30
Ref<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
2021-06-15 09:39:11 +04:30
2022-03-07 21:57:00 +03:30
HRESULT hr;
Microsoft::WRL::ComPtr<ID3D11Debug> debugInterface = nullptr;
DXC(context->GetDevice()->QueryInterface(__uuidof(ID3D11Debug), &debugInterface));
2022-03-07 21:57:00 +03:30
Microsoft::WRL::ComPtr<ID3D11InfoQueue> infoQueue = nullptr;
DXC(debugInterface->QueryInterface(__uuidof(ID3D11InfoQueue), &infoQueue));
2022-03-07 21:57:00 +03:30
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
2021-06-15 09:39:11 +04:30
2022-03-07 21:57:00 +03:30
D3D11_MESSAGE_ID hide[] = {
D3D11_MESSAGE_ID_UNKNOWN,
// #todo: add more messages here as needed
};
2021-06-15 09:39:11 +04:30
2022-03-07 21:57:00 +03:30
D3D11_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumIDs = _countof(hide);
filter.DenyList.pIDList = hide;
infoQueue->AddStorageFilterEntries(&filter);
infoQueue->Release();
#endif
2022-03-07 21:57:00 +03:30
}
void dxGraphicsContext::LogDebugData()
{
Ref<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
// locals
IDXGIDevice* DXGIDevice;
IDXGIAdapter* DXGIAdapter;
DXGI_ADAPTER_DESC DXGIAdapterDesc;
context->GetDevice()->QueryInterface(__uuidof(IDXGIDevice), (void**)&DXGIDevice);
DXGIDevice->GetAdapter(&DXGIAdapter);
DXGIAdapter->GetDesc(&DXGIAdapterDesc);
// get the adapter's description
char DefChar = ' ';
char ch[180];
WideCharToMultiByte(CP_ACP, 0, DXGIAdapterDesc.Description, -1, ch, 180, &DefChar, NULL);
std::string adapterDesc(ch);
// release memory
DXGIDevice->Release();
DXGIAdapter->Release();
// #todo: log more information
LOG(info, "________________________________________");
LOG(info, "dxGraphicsContext:");
LOG(info, " Renderer: {}", adapterDesc);
LOG(info, "________________________________________");
}
} // namespace Light