2021-06-02 09:07:45 +04:30
|
|
|
#include "ltpch.h"
|
|
|
|
#include "dxGraphicsContext.h"
|
2021-06-21 18:08:25 +04:30
|
|
|
#include "dxSharedContext.h"
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
#include "Events/WindowEvents.h"
|
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
// forward declaration
|
|
|
|
#include "Graphics/Renderer.h"
|
|
|
|
#include "Graphics/RenderCommand.h"
|
|
|
|
#include "UserInterface/UserInterface.h"
|
|
|
|
#include "Utility/ResourceManager.h"
|
2021-06-02 09:07:45 +04:30
|
|
|
|
|
|
|
#define GLFW_EXPOSE_NATIVE_WIN32
|
2021-07-01 19:25:46 +04:30
|
|
|
#include <glfw/glfw3.h>
|
2021-06-02 09:07:45 +04:30
|
|
|
#include <glfw/glfw3native.h>
|
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
|
|
|
dxGraphicsContext::dxGraphicsContext(GLFWwindow* windowHandle)
|
|
|
|
: m_WindowHandle(windowHandle)
|
|
|
|
{
|
2021-07-01 19:25:46 +04:30
|
|
|
// set 'GraphicsAPI';
|
2021-06-02 09:07:45 +04:30
|
|
|
m_GraphicsAPI = GraphicsAPI::DirectX;
|
2021-07-22 13:00:41 +04:30
|
|
|
|
|
|
|
m_SharedContext = std::make_shared<dxSharedContext>();
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
// setup stuff
|
2021-06-21 18:08:25 +04:30
|
|
|
SetupDeviceAndSwapChain(windowHandle);
|
|
|
|
SetupRenderTargets();
|
|
|
|
SetupDebugInterface();
|
|
|
|
}
|
2021-06-15 09:39:11 +04:30
|
|
|
|
2021-06-21 18:08:25 +04:30
|
|
|
void dxGraphicsContext::SetupDeviceAndSwapChain(GLFWwindow* windowHandle)
|
|
|
|
{
|
2021-07-22 13:00:41 +04:30
|
|
|
std::shared_ptr<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
|
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
// swap chain desc
|
2021-06-21 18:08:25 +04:30
|
|
|
DXGI_SWAP_CHAIN_DESC sd = { 0 };
|
|
|
|
|
|
|
|
// buffer desc
|
2021-06-15 09:39:11 +04:30
|
|
|
sd.BufferDesc.Width = 800u;
|
|
|
|
sd.BufferDesc.Height = 600u;
|
2021-06-21 18:08:25 +04:30
|
|
|
sd.BufferDesc.RefreshRate.Numerator = NULL; // :#todo
|
|
|
|
sd.BufferDesc.RefreshRate.Denominator = NULL; // :#todo
|
2021-06-02 09:07:45 +04:30
|
|
|
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2021-06-15 09:39:11 +04:30
|
|
|
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
|
2021-06-21 18:08:25 +04:30
|
|
|
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
|
2021-06-15 09:39:11 +04:30
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
// sample desc (for multi sampling) #todo: implement multi-samplingz
|
2021-06-02 09:07:45 +04:30
|
|
|
sd.SampleDesc.Count = 1u;
|
2021-06-15 09:39:11 +04:30
|
|
|
sd.SampleDesc.Quality = 0u;
|
|
|
|
|
2021-06-21 18:08:25 +04:30
|
|
|
// #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));
|
2021-06-02 09:07:45 +04:30
|
|
|
sd.Windowed = true;
|
2021-06-21 18:08:25 +04:30
|
|
|
|
|
|
|
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
|
|
|
|
2021-06-15 09:39:11 +04:30
|
|
|
sd.Flags = NULL;
|
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
// determine device flags
|
2021-06-21 18:08:25 +04:30
|
|
|
UINT flags = NULL;
|
2021-06-15 09:39:11 +04:30
|
|
|
#ifdef LIGHT_DEBUG
|
|
|
|
flags = D3D11_CREATE_DEVICE_DEBUG;
|
|
|
|
#endif
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create device and swap chain
|
|
|
|
HRESULT hr;
|
2021-06-21 18:08:25 +04:30
|
|
|
DXC(D3D11CreateDeviceAndSwapChain(nullptr,
|
|
|
|
D3D_DRIVER_TYPE_HARDWARE,
|
|
|
|
NULL,
|
|
|
|
flags,
|
|
|
|
nullptr,
|
|
|
|
NULL,
|
|
|
|
D3D11_SDK_VERSION,
|
|
|
|
&sd,
|
2021-07-22 13:00:41 +04:30
|
|
|
&context->GetSwapChainRef(),
|
|
|
|
&context->GetDeviceRef(),
|
2021-06-21 18:08:25 +04:30
|
|
|
nullptr,
|
2021-07-22 13:00:41 +04:30
|
|
|
&context->GetDeviceContextRef()));
|
|
|
|
|
2021-06-21 18:08:25 +04:30
|
|
|
}
|
2021-06-19 15:12:42 +04:30
|
|
|
|
2021-06-21 18:08:25 +04:30
|
|
|
void dxGraphicsContext::SetupRenderTargets()
|
|
|
|
{
|
2021-07-22 13:00:41 +04:30
|
|
|
std::shared_ptr<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// set primitive topology
|
2021-07-22 13:00:41 +04:30
|
|
|
context->GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create render target view
|
2021-06-02 09:07:45 +04:30
|
|
|
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer;
|
2021-06-21 18:08:25 +04:30
|
|
|
|
|
|
|
HRESULT hr;
|
2021-07-22 13:00:41 +04:30
|
|
|
DXC(context->GetSwapChain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer));
|
|
|
|
DXC(context->GetDevice()->CreateRenderTargetView(backBuffer.Get(), nullptr, &context->GetRenderTargetViewRef()));
|
2021-06-19 15:12:42 +04:30
|
|
|
|
|
|
|
// set render target view
|
2021-07-22 13:00:41 +04:30
|
|
|
context->GetDeviceContext()->OMSetRenderTargets(1u, context->GetRenderTargetView().GetAddressOf(), nullptr);
|
2021-06-21 18:08:25 +04:30
|
|
|
}
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2021-06-21 18:08:25 +04:30
|
|
|
void dxGraphicsContext::SetupDebugInterface()
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
#ifdef LIGHT_DEBUG
|
2021-07-22 13:00:41 +04:30
|
|
|
std::shared_ptr<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
|
2021-06-15 09:39:11 +04:30
|
|
|
|
2021-06-21 18:08:25 +04:30
|
|
|
HRESULT hr;
|
2021-07-22 13:00:41 +04:30
|
|
|
Microsoft::WRL::ComPtr<ID3D11Debug> debugInterface = nullptr;
|
|
|
|
DXC(context->GetDevice()->QueryInterface(__uuidof(ID3D11Debug), &debugInterface));
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID3D11InfoQueue> infoQueue = nullptr;
|
|
|
|
DXC(debugInterface->QueryInterface(__uuidof(ID3D11InfoQueue), &infoQueue));
|
|
|
|
|
|
|
|
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
|
|
|
|
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
|
2021-06-15 09:39:11 +04:30
|
|
|
|
|
|
|
D3D11_MESSAGE_ID hide[] =
|
|
|
|
{
|
2021-07-22 13:00:41 +04:30
|
|
|
D3D11_MESSAGE_ID_UNKNOWN,
|
|
|
|
// #todo: add more messages here as needed
|
2021-06-15 09:39:11 +04:30
|
|
|
};
|
|
|
|
|
2021-07-22 13:00:41 +04:30
|
|
|
D3D11_INFO_QUEUE_FILTER filter = { };
|
2021-06-15 09:39:11 +04:30
|
|
|
filter.DenyList.NumIDs = _countof(hide);
|
|
|
|
filter.DenyList.pIDList = hide;
|
2021-07-22 13:00:41 +04:30
|
|
|
infoQueue->AddStorageFilterEntries(&filter);
|
|
|
|
infoQueue->Release();
|
2021-06-21 18:08:25 +04:30
|
|
|
#endif
|
2021-06-19 15:12:42 +04:30
|
|
|
}
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2021-06-15 09:39:11 +04:30
|
|
|
void dxGraphicsContext::LogDebugData()
|
|
|
|
{
|
2021-07-22 13:00:41 +04:30
|
|
|
std::shared_ptr<dxSharedContext> context = std::static_pointer_cast<dxSharedContext>(m_SharedContext);
|
|
|
|
|
2021-06-02 17:20:15 +04:30
|
|
|
// locals
|
2021-06-02 09:07:45 +04:30
|
|
|
IDXGIDevice* DXGIDevice;
|
|
|
|
IDXGIAdapter* DXGIAdapter;
|
|
|
|
DXGI_ADAPTER_DESC DXGIAdapterDesc;
|
|
|
|
|
2021-07-22 13:00:41 +04:30
|
|
|
context->GetDevice()->QueryInterface(__uuidof(IDXGIDevice), (void**)&DXGIDevice);
|
2021-06-02 09:07:45 +04:30
|
|
|
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();
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// #todo: log more information
|
2021-06-15 09:39:11 +04:30
|
|
|
LT_ENGINE_INFO("________________________________________");
|
2021-06-02 09:07:45 +04:30
|
|
|
LT_ENGINE_INFO("dxGraphicsContext:");
|
|
|
|
LT_ENGINE_INFO(" Renderer: {}", adapterDesc);
|
2021-06-15 09:39:11 +04:30
|
|
|
LT_ENGINE_INFO("________________________________________");
|
2021-06-02 09:07:45 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|