DirectX Context

This commit is contained in:
Light3039 2021-06-02 09:07:45 +04:30
parent cefe62be9f
commit ce1aa29e0e
11 changed files with 255 additions and 6 deletions

View file

@ -49,11 +49,23 @@ project "Engine"
--- Filters ---
-- windows
filter "system:windows"
defines "LIGHT_PLATFORM_WINDOWS"
systemversion "latest"
staticruntime "On"
links
{
"d3d11.lib" ,
"dxguid.lib" ,
"D3DCompiler.lib" ,
}
filter "system:not windows"
excludes "%{prj.location}/src/Platform/GraphicsAPI/DirectX**"
excludes "%{prj.location}/src/Platform/OS/Windows**"
-- debug
filter "configurations:Debug"
defines "LT_DEBUG"

View file

@ -6,15 +6,21 @@
#include <memory>
#define LT_WIN(x)
#define LT_LIN(x)
#define LT_MAC(x)
#if defined(LIGHT_PLATFORM_WINDOWS)
#define LT_BUILD_PLATFORM "Windows"
#define LT_WIN(x) x
#elif defined(LT_PLATFORM_LINUX)
#error "Unsupported platform: UNIX"
#define LT_LIN(x)
#else
#error "Unsupported platform: Unknown"
#define LT_MAC(x) x
#endif
#define BIT(x) 1 << x
#define LT_ENGINE_ASSERT(x, ...) { if(!(x)) { LT_ENGINE_CRITICAL(__VA_ARGS__); __debugbreak(); } }

View file

@ -2,6 +2,10 @@
#include "GraphicsContext.h"
#include "OpenGL/glGraphicsContext.h"
#ifdef LIGHT_PLATFORM_WINDOWS
#include "DirectX/dxGraphicsContext.h"
#endif
#include "Buffers.h"
#include "Renderer.h"
@ -35,17 +39,19 @@ namespace Light {
// create gfx context
switch (api)
{
case Light::GraphicsAPI::OpenGL:
case GraphicsAPI::OpenGL:
s_Context = new glGraphicsContext(windowHandle);
break;
case GraphicsAPI::DirectX: LT_WIN(
s_Context = new dxGraphicsContext(windowHandle);
break;)
default:
LT_ENGINE_ASSERT(false, "GraphicsContext::Create: invalid/unsupported GraphicsAPI {}", api);
return nullptr;
}
// create gfx context dependent classes
s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle));
s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle, s_Context->m_SharedContext));
s_Context->m_UserInterface = std::unique_ptr<UserInterface>(UserInterface::Create(windowHandle));
s_Context->m_Renderer = std::unique_ptr<Renderer>(Renderer::Create(s_Context->m_RenderCommand));

View file

@ -32,6 +32,7 @@ namespace Light {
protected:
GraphicsAPI m_GraphicsAPI;
void* m_SharedContext = nullptr;
public:
static GraphicsContext* Create(GraphicsAPI api, GLFWwindow* windowHandle);

View file

@ -2,17 +2,24 @@
#include "RenderCommand.h"
#include "OpenGL/glRenderCommand.h"
#ifdef LIGHT_PLATFORM_WINDOWS
#include "DirectX/dxRenderCommand.h"
#endif
#include "GraphicsContext.h"
namespace Light {
RenderCommand* RenderCommand::Create(GLFWwindow* windowHandle)
RenderCommand* RenderCommand::Create(GLFWwindow* windowHandle, void* sharedContext)
{
switch (GraphicsContext::GetGraphicsAPI())
{
case GraphicsAPI::OpenGL:
return new glRenderCommand(windowHandle);
case GraphicsAPI::DirectX: LT_WIN(
return new dxRenderCommand(sharedContext);)
default:
LT_ENGINE_ASSERT(false, "RenderCommand::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
return nullptr;

View file

@ -20,7 +20,7 @@ namespace Light {
virtual void Draw(unsigned int count) = 0;
virtual void DrawIndexed(unsigned int count) = 0;
static RenderCommand* Create(GLFWwindow* windowHandle);
static RenderCommand* Create(GLFWwindow* windowHandle, void* sharedContext);
protected:
RenderCommand() = default;

View file

@ -0,0 +1,97 @@
#include "ltpch.h"
#include "dxGraphicsContext.h"
// Required for forward declaration
#include "Graphics/Renderer.h"
#include "Graphics/RenderCommand.h"
#include "Graphics/Shader.h"
#include "Graphics/Buffers.h"
#include "Graphics/VertexLayout.h"
#include "UserInterface/UserInterface.h"
#include <glfw/glfw3.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw/glfw3native.h>
#include "dxSharedContext.h"
namespace Light {
dxGraphicsContext::dxGraphicsContext(GLFWwindow* windowHandle)
: m_WindowHandle(windowHandle)
{
m_GraphicsAPI = GraphicsAPI::DirectX;
DXGI_SWAP_CHAIN_DESC sd = { 0 };
sd.BufferCount = 1u;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = glfwGetWin32Window(m_WindowHandle);
sd.SampleDesc.Count = 1u;
sd.Windowed = true;
D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE,
NULL, NULL, NULL, NULL, D3D11_SDK_VERSION,
&sd, &m_SwapChain, &m_Device, NULL, &m_DeviceContext);
m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer;
m_SwapChain->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer);
m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView);
m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr);
D3D11_VIEWPORT viewport;
viewport.Width = 800.0f;
viewport.Height = 600.0f;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
m_DeviceContext->RSSetViewports(1u, &viewport);
dxSharedContext* sharedContext = new dxSharedContext({m_DeviceContext, m_SwapChain, m_RenderTargetView});
m_SharedContext = sharedContext;
// log some information about dx context //
// locals
IDXGIDevice* DXGIDevice;
IDXGIAdapter* DXGIAdapter;
DXGI_ADAPTER_DESC DXGIAdapterDesc;
// initialize Locals
m_Device->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();
// log info // #todo: log more information
LT_ENGINE_INFO("dxGraphicsContext:");
LT_ENGINE_INFO(" Renderer: {}", adapterDesc);
}
void dxGraphicsContext::OnWindowResize(const WindowResizedEvent& event)
{
}
void dxGraphicsContext::LogDebugData()
{
}
}

View file

@ -0,0 +1,31 @@
#pragma once
#include "Base.h"
#include "Graphics/GraphicsContext.h"
#include <d3d11.h>
#include <wrl.h>
struct GLFWwindow;
namespace Light {
class dxGraphicsContext : public GraphicsContext
{
private:
GLFWwindow* m_WindowHandle;
Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
Microsoft::WRL::ComPtr<IDXGISwapChain> m_SwapChain;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_RenderTargetView;
public:
dxGraphicsContext(GLFWwindow* windowHandle);
virtual void OnWindowResize(const WindowResizedEvent& event) override;
virtual void LogDebugData() override;
};
}

View file

@ -0,0 +1,42 @@
#include "ltpch.h"
#include "dxRenderCommand.h"
#include "dxSharedContext.h"
namespace Light {
dxRenderCommand::dxRenderCommand(void* sharedContext)
{
dxSharedContext* dxContext = (dxSharedContext*)sharedContext;
m_DeviceContext = dxContext->deviceContext;
m_SwapChain = dxContext->swapChain;
m_RenderTargetView = dxContext->renderTargetView;
}
dxRenderCommand::~dxRenderCommand()
{
}
void dxRenderCommand::SwapBuffers()
{
m_SwapChain->Present(0, 0);
}
void dxRenderCommand::ClearBackBuffer()
{
float colors[] = { 1.2f, 0.4f, 0.9f, 1.0f };
m_DeviceContext->ClearRenderTargetView(m_RenderTargetView.Get(), colors);
}
void dxRenderCommand::Draw(unsigned int count)
{
}
void dxRenderCommand::DrawIndexed(unsigned int count)
{
}
}

View file

@ -0,0 +1,30 @@
#pragma once
#include "Base.h"
#include "Graphics/RenderCommand.h"
#include <d3d11.h>
#include <wrl.h>
namespace Light {
class dxRenderCommand : public RenderCommand
{
private:
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
Microsoft::WRL::ComPtr<IDXGISwapChain> m_SwapChain;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_RenderTargetView;
public:
dxRenderCommand(void* sharedContext);
~dxRenderCommand();
virtual void SwapBuffers() override;
virtual void ClearBackBuffer() override;
virtual void Draw(unsigned int count) override;
virtual void DrawIndexed(unsigned int count) override;
};
}

View file

@ -0,0 +1,17 @@
#pragma once
#include "Base.h"
#include <d3d11.h>
#include <wrl.h>
namespace Light {
struct dxSharedContext
{
Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext;
Microsoft::WRL::ComPtr<IDXGISwapChain> swapChain;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> renderTargetView;
};
}