diff --git a/Engine/premake5.lua b/Engine/premake5.lua index 1587989..949b4b6 100644 --- a/Engine/premake5.lua +++ b/Engine/premake5.lua @@ -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" diff --git a/Engine/src/Engine/Base.h b/Engine/src/Engine/Base.h index 3c06501..4649477 100644 --- a/Engine/src/Engine/Base.h +++ b/Engine/src/Engine/Base.h @@ -6,15 +6,21 @@ #include +#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(); } } diff --git a/Engine/src/Engine/Graphics/GraphicsContext.cpp b/Engine/src/Engine/Graphics/GraphicsContext.cpp index a0373ec..0c1c316 100644 --- a/Engine/src/Engine/Graphics/GraphicsContext.cpp +++ b/Engine/src/Engine/Graphics/GraphicsContext.cpp @@ -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::Create(windowHandle)); + s_Context->m_RenderCommand = std::unique_ptr(RenderCommand::Create(windowHandle, s_Context->m_SharedContext)); s_Context->m_UserInterface = std::unique_ptr(UserInterface::Create(windowHandle)); s_Context->m_Renderer = std::unique_ptr(Renderer::Create(s_Context->m_RenderCommand)); diff --git a/Engine/src/Engine/Graphics/GraphicsContext.h b/Engine/src/Engine/Graphics/GraphicsContext.h index d6497e1..5307016 100644 --- a/Engine/src/Engine/Graphics/GraphicsContext.h +++ b/Engine/src/Engine/Graphics/GraphicsContext.h @@ -32,6 +32,7 @@ namespace Light { protected: GraphicsAPI m_GraphicsAPI; + void* m_SharedContext = nullptr; public: static GraphicsContext* Create(GraphicsAPI api, GLFWwindow* windowHandle); diff --git a/Engine/src/Engine/Graphics/RenderCommand.cpp b/Engine/src/Engine/Graphics/RenderCommand.cpp index e75ac36..00fc8fa 100644 --- a/Engine/src/Engine/Graphics/RenderCommand.cpp +++ b/Engine/src/Engine/Graphics/RenderCommand.cpp @@ -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; diff --git a/Engine/src/Engine/Graphics/RenderCommand.h b/Engine/src/Engine/Graphics/RenderCommand.h index f079968..6a16afc 100644 --- a/Engine/src/Engine/Graphics/RenderCommand.h +++ b/Engine/src/Engine/Graphics/RenderCommand.h @@ -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; diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp new file mode 100644 index 0000000..49e8cd2 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp @@ -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 + +#define GLFW_EXPOSE_NATIVE_WIN32 +#include + +#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 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() + { + + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h new file mode 100644 index 0000000..aeed2f6 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Base.h" +#include "Graphics/GraphicsContext.h" + +#include +#include + +struct GLFWwindow; + +namespace Light { + + class dxGraphicsContext : public GraphicsContext + { + private: + GLFWwindow* m_WindowHandle; + + Microsoft::WRL::ComPtr m_Device; + Microsoft::WRL::ComPtr m_DeviceContext; + Microsoft::WRL::ComPtr m_SwapChain; + Microsoft::WRL::ComPtr m_RenderTargetView; + + public: + dxGraphicsContext(GLFWwindow* windowHandle); + + virtual void OnWindowResize(const WindowResizedEvent& event) override; + + virtual void LogDebugData() override; + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp new file mode 100644 index 0000000..a5dc21d --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp @@ -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) + { + + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h new file mode 100644 index 0000000..6ee4006 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Base.h" +#include "Graphics/RenderCommand.h" + +#include +#include + +namespace Light { + + class dxRenderCommand : public RenderCommand + { + private: + Microsoft::WRL::ComPtr m_DeviceContext; + Microsoft::WRL::ComPtr m_SwapChain; + Microsoft::WRL::ComPtr 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; + + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h new file mode 100644 index 0000000..6a6d16c --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h @@ -0,0 +1,17 @@ +#pragma once + +#include "Base.h" + +#include +#include + +namespace Light { + + struct dxSharedContext + { + Microsoft::WRL::ComPtr deviceContext; + Microsoft::WRL::ComPtr swapChain; + Microsoft::WRL::ComPtr renderTargetView; + }; + +} \ No newline at end of file