From 2f560239cbfa131f87979c464e6ef5c1ffcccc52 Mon Sep 17 00:00:00 2001 From: Light3039 Date: Wed, 2 Jun 2021 17:20:15 +0430 Subject: [PATCH] DirectX --- Engine/src/Engine/Graphics/Buffers.cpp | 10 ++- Engine/src/Engine/Graphics/Buffers.h | 2 +- .../src/Engine/Graphics/GraphicsContext.cpp | 4 +- Engine/src/Engine/Graphics/Renderer.cpp | 40 +++------ Engine/src/Engine/Graphics/Renderer.h | 6 +- Engine/src/Engine/Graphics/Shader.cpp | 9 ++- Engine/src/Engine/Graphics/Shader.h | 2 +- Engine/src/Engine/Graphics/VertexLayout.cpp | 9 ++- Engine/src/Engine/Graphics/VertexLayout.h | 3 +- .../Engine/UserInterface/UserInterface.cpp | 9 ++- .../src/Engine/UserInterface/UserInterface.h | 2 +- .../src/Platform/GraphicsAPI/DirectX/dxBase.h | 6 ++ .../GraphicsAPI/DirectX/dxBuffers.cpp | 49 +++++++++++ .../Platform/GraphicsAPI/DirectX/dxBuffers.h | 29 +++++++ .../GraphicsAPI/DirectX/dxGraphicsContext.cpp | 14 ++-- .../GraphicsAPI/DirectX/dxGraphicsContext.h | 1 + .../GraphicsAPI/DirectX/dxRenderCommand.cpp | 2 +- .../Platform/GraphicsAPI/DirectX/dxShader.cpp | 47 +++++++++++ .../Platform/GraphicsAPI/DirectX/dxShader.h | 33 ++++++++ .../GraphicsAPI/DirectX/dxSharedContext.h | 2 + .../GraphicsAPI/DirectX/dxUserInterface.cpp | 69 ++++++++++++++++ .../GraphicsAPI/DirectX/dxUserInterface.h | 30 +++++++ .../GraphicsAPI/DirectX/dxVertexLayout.cpp | 81 +++++++++++++++++++ .../GraphicsAPI/DirectX/dxVertexLayout.h | 33 ++++++++ .../GraphicsAPI/OpenGL/glUserInterface.cpp | 2 + .../GraphicsAPI/OpenGL/glUserInterface.h | 2 +- .../GraphicsAPI/OpenGL/glVertexLayout.cpp | 4 +- .../GraphicsAPI/OpenGL/glVertexLayout.h | 2 +- Engine/src/Platform/OS/Windows/wWindow.cpp | 2 +- Sandbox/imgui.ini | 2 +- Sandbox/res/fragment.fragment | 9 +-- Sandbox/res/vertex.vertex | 21 ++--- 32 files changed, 464 insertions(+), 72 deletions(-) create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxBase.h create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxShader.cpp create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxShader.h create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp create mode 100644 Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.h diff --git a/Engine/src/Engine/Graphics/Buffers.cpp b/Engine/src/Engine/Graphics/Buffers.cpp index 947ea50..fbb974c 100644 --- a/Engine/src/Engine/Graphics/Buffers.cpp +++ b/Engine/src/Engine/Graphics/Buffers.cpp @@ -2,17 +2,25 @@ #include "Buffers.h" #include "OpenGL/glBuffers.h" +#ifdef LIGHT_PLATFORM_WINDOWS + #include "DirectX/dxBuffers.h" +#endif + #include "GraphicsContext.h" namespace Light { - VertexBuffer* VertexBuffer::Create(unsigned int count, float* vertices) + VertexBuffer* VertexBuffer::Create(unsigned int stride, unsigned int count, float* vertices, void* sharedContext) { switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: return new glVertexBuffer(count, vertices); + + case GraphicsAPI::DirectX: + return new dxVertexBuffer(count, stride, vertices, sharedContext); + default: LT_ENGINE_ASSERT(false, "VertexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); return nullptr; diff --git a/Engine/src/Engine/Graphics/Buffers.h b/Engine/src/Engine/Graphics/Buffers.h index 33245d5..e49f1a8 100644 --- a/Engine/src/Engine/Graphics/Buffers.h +++ b/Engine/src/Engine/Graphics/Buffers.h @@ -7,7 +7,7 @@ namespace Light { class VertexBuffer { public: - static VertexBuffer* Create(unsigned int count, float* vertices); + static VertexBuffer* Create(unsigned int stride, unsigned int count, float* vertices, void* sharedContext); virtual void Bind() = 0; virtual void UnBind() = 0; diff --git a/Engine/src/Engine/Graphics/GraphicsContext.cpp b/Engine/src/Engine/Graphics/GraphicsContext.cpp index 0c1c316..37a81d8 100644 --- a/Engine/src/Engine/Graphics/GraphicsContext.cpp +++ b/Engine/src/Engine/Graphics/GraphicsContext.cpp @@ -52,8 +52,8 @@ namespace Light { // create gfx context dependent classes 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)); + s_Context->m_UserInterface = std::unique_ptr(UserInterface::Create(windowHandle, s_Context->m_SharedContext)); + s_Context->m_Renderer = std::unique_ptr(Renderer::Create(s_Context->m_RenderCommand, s_Context->m_SharedContext)); // sanity check LT_ENGINE_ASSERT(s_Context->m_RenderCommand, "GraphicsContext::Create: RenderCommand creation failed"); diff --git a/Engine/src/Engine/Graphics/Renderer.cpp b/Engine/src/Engine/Graphics/Renderer.cpp index 1815d2b..1655557 100644 --- a/Engine/src/Engine/Graphics/Renderer.cpp +++ b/Engine/src/Engine/Graphics/Renderer.cpp @@ -9,48 +9,27 @@ namespace Light { Renderer* Renderer::m_Context; - Renderer::Renderer(std::shared_ptr renderCommand) - : m_RenderCommand(renderCommand) + Renderer::Renderer(std::shared_ptr renderCommand, void* sharedContext) + : m_RenderCommand(renderCommand), m_SharedContext(sharedContext) { m_Context = this; - m_Shader = std::unique_ptr(Shader::Create("res/vertex.vertex", "res/fragment.fragment")); - -// m_Shader = std::unique_ptr(Shader::Create( -// R"( -// #version 450 core -// -// layout(location = 0) in vec2 a_Position; -// -// void main() -// { -// gl_Position = vec4(a_Position, 0.0, 1.0); -// })", -// R"( -// #version 450 core -// -// out vec4 FragColor; -// -// void main() -// { -// FragColor = vec4(1.0, 0.0, 0.0, 1.0); -// } -// )")); + m_Shader = std::unique_ptr(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext)); float vertices[] = { - -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, }; - m_VertexBuffer = std::unique_ptr(VertexBuffer::Create((2 + 4) * 3, vertices)); - m_VertexLayout = std::unique_ptr(VertexLayout::Create(m_VertexBuffer.get(), { VertexElementType::Float2, VertexElementType::Float4 })); + m_VertexBuffer = std::unique_ptr(VertexBuffer::Create(6 * sizeof(float), (2 + 4) * 3, vertices, m_SharedContext)); + m_VertexLayout = std::unique_ptr(VertexLayout::Create(m_VertexBuffer.get(), m_Shader.get(), { { "POSITION", VertexElementType::Float2 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext)); } - Renderer* Renderer::Create(std::shared_ptr renderCommand) + Renderer* Renderer::Create(std::shared_ptr renderCommand, void* sharedContext) { - return new Renderer(renderCommand); + return new Renderer(renderCommand, sharedContext); } void Renderer::Draw() @@ -58,6 +37,7 @@ namespace Light { m_Shader->Bind(); m_VertexBuffer->Bind(); m_VertexLayout->Bind(); + m_RenderCommand->Draw(3u); } diff --git a/Engine/src/Engine/Graphics/Renderer.h b/Engine/src/Engine/Graphics/Renderer.h index 90340ba..beb9f0a 100644 --- a/Engine/src/Engine/Graphics/Renderer.h +++ b/Engine/src/Engine/Graphics/Renderer.h @@ -22,13 +22,15 @@ namespace Light { std::unique_ptr m_Shader; std::unique_ptr m_VertexBuffer; std::unique_ptr m_VertexLayout; + + void* m_SharedContext; public: - static Renderer* Create(std::shared_ptr renderCommand); + static Renderer* Create(std::shared_ptr renderCommand, void* sharedContext); void Draw(); private: - Renderer(std::shared_ptr renderCommand); + Renderer(std::shared_ptr renderCommand, void* sharedContext); }; } \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/Shader.cpp b/Engine/src/Engine/Graphics/Shader.cpp index 1c584ee..7d91648 100644 --- a/Engine/src/Engine/Graphics/Shader.cpp +++ b/Engine/src/Engine/Graphics/Shader.cpp @@ -2,13 +2,17 @@ #include "Shader.h" #include "OpenGL/glShader.h" +#ifdef LIGHT_PLATFORM_WINDOWS + #include "DirectX/dxShader.h" +#endif + #include "GraphicsContext.h" #include "Utility/FileManager.h" namespace Light { - Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath) + Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath, void* sharedContext) { // load shader source const std::string vertexSource = FileManager::ReadTXTFile(vertexPath); @@ -19,6 +23,9 @@ namespace Light { case GraphicsAPI::OpenGL: return new glShader(vertexSource, pixelSource); + case GraphicsAPI::DirectX: + return new dxShader(vertexSource, pixelSource, sharedContext); + default: LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); return nullptr; diff --git a/Engine/src/Engine/Graphics/Shader.h b/Engine/src/Engine/Graphics/Shader.h index b40a320..6b13b83 100644 --- a/Engine/src/Engine/Graphics/Shader.h +++ b/Engine/src/Engine/Graphics/Shader.h @@ -7,7 +7,7 @@ namespace Light { class Shader { public: - static Shader* Create(const std::string& vertexPath, const std::string& pixelPath); + static Shader* Create(const std::string& vertexPath, const std::string& pixelPath, void* sharedContext); virtual ~Shader() = default; diff --git a/Engine/src/Engine/Graphics/VertexLayout.cpp b/Engine/src/Engine/Graphics/VertexLayout.cpp index afd5f87..26a61e8 100644 --- a/Engine/src/Engine/Graphics/VertexLayout.cpp +++ b/Engine/src/Engine/Graphics/VertexLayout.cpp @@ -2,17 +2,24 @@ #include "VertexLayout.h" #include "OpenGL/glVertexLayout.h" +#ifdef LIGHT_PLATFORM_WINDOWS + #include "DirectX/dxVertexLayout.h" +#endif + #include "GraphicsContext.h" namespace Light { - VertexLayout* VertexLayout::Create(VertexBuffer* buffer, const std::vector& elements) + VertexLayout* VertexLayout::Create(VertexBuffer* buffer, Shader* shader, const std::vector>& elements, void* sharedContext) { switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: return new glVertexLayout(buffer, elements); + case GraphicsAPI::DirectX: + return new dxVertexLayout(shader, elements, sharedContext); + default: LT_ENGINE_ASSERT(false, "VertexLayout::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); return nullptr; diff --git a/Engine/src/Engine/Graphics/VertexLayout.h b/Engine/src/Engine/Graphics/VertexLayout.h index 87d935f..37765b3 100644 --- a/Engine/src/Engine/Graphics/VertexLayout.h +++ b/Engine/src/Engine/Graphics/VertexLayout.h @@ -7,6 +7,7 @@ namespace Light { class VertexBuffer; + class Shader; enum class VertexElementType { @@ -19,7 +20,7 @@ namespace Light { class VertexLayout { public: - static VertexLayout* Create(VertexBuffer* buffer, const std::vector& elements); + static VertexLayout* Create(VertexBuffer* buffer, Shader* shader, const std::vector>& elements, void* sharedContext); virtual ~VertexLayout() = default;; diff --git a/Engine/src/Engine/UserInterface/UserInterface.cpp b/Engine/src/Engine/UserInterface/UserInterface.cpp index 235f24e..3cde7cc 100644 --- a/Engine/src/Engine/UserInterface/UserInterface.cpp +++ b/Engine/src/Engine/UserInterface/UserInterface.cpp @@ -2,6 +2,10 @@ #include "UserInterface.h" #include "OpenGL/glUserInterface.h" +#ifdef LIGHT_PLATFORM_WINDOWS + #include "DirectX/dxUserInterface.h" +#endif + #include "Graphics/GraphicsContext.h" #include "Events/Event.h" @@ -12,13 +16,16 @@ namespace Light { - UserInterface* UserInterface::Create(GLFWwindow* windowHandle) + UserInterface* UserInterface::Create(GLFWwindow* windowHandle, void* sharedContext) { switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: return new glUserInterface(windowHandle); + case GraphicsAPI::DirectX: LT_WIN( + return new dxUserInterface(windowHandle, sharedContext);) + default: LT_ENGINE_ASSERT(false, "UserInterface::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); return nullptr; diff --git a/Engine/src/Engine/UserInterface/UserInterface.h b/Engine/src/Engine/UserInterface/UserInterface.h index 9546e3c..7f0f2ff 100644 --- a/Engine/src/Engine/UserInterface/UserInterface.h +++ b/Engine/src/Engine/UserInterface/UserInterface.h @@ -11,7 +11,7 @@ namespace Light { class UserInterface { public: - static UserInterface* Create(GLFWwindow* windowHandle); + static UserInterface* Create(GLFWwindow* windowHandle, void* sharedContext); UserInterface(const UserInterface&) = delete; UserInterface operator=(const UserInterface&) = delete; diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxBase.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxBase.h new file mode 100644 index 0000000..3e856ee --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxBase.h @@ -0,0 +1,6 @@ +#pragma once + +#include "Base.h" + +// DirectX Call +#define DXC(x) hr = x; if(FAILED(x)) __debugbreak() \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp new file mode 100644 index 0000000..78554ff --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp @@ -0,0 +1,49 @@ +#include "ltpch.h" +#include "dxBuffers.h" + +#include "dxSharedContext.h" + +namespace Light { + + dxVertexBuffer::dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext) + : m_Stride(stride) + { + HRESULT hr; + + dxSharedContext* dxContext = static_cast(sharedContext); + LT_ENGINE_ASSERT(dxContext, "dxShader::dxShader: invalid dxContext"); + + m_Device = dxContext->device; + m_DeviceContext = dxContext->deviceContext; + + D3D11_BUFFER_DESC desc = { 0 }; + D3D11_SUBRESOURCE_DATA sd = { 0 }; + + + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + desc.Usage = D3D11_USAGE_DYNAMIC; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + + desc.ByteWidth = count * stride; + desc.StructureByteStride = stride; + + sd.pSysMem = vertices; + + DXC(m_Device->CreateBuffer(&desc, &sd, &m_Buffer)); + } + + dxVertexBuffer::~dxVertexBuffer() + { + } + + void dxVertexBuffer::Bind() + { + static const unsigned int offset = 0u; + m_DeviceContext->IASetVertexBuffers(0u, 1u, m_Buffer.GetAddressOf(), &m_Stride, &offset); + } + + void dxVertexBuffer::UnBind() + { + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h new file mode 100644 index 0000000..26a9c25 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h @@ -0,0 +1,29 @@ +#pragma once + +#include "Base.h" +#include "dxBase.h" +#include "Graphics/Buffers.h" + +#include +#include + +namespace Light { + + class dxVertexBuffer : public VertexBuffer + { + private: + Microsoft::WRL::ComPtr m_Buffer; + + Microsoft::WRL::ComPtr m_Device; + Microsoft::WRL::ComPtr m_DeviceContext; + + unsigned int m_Stride; + public: + dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext); + ~dxVertexBuffer(); + + void Bind() override; + void UnBind() override; + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp index 49e8cd2..5358dff 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.cpp @@ -23,6 +23,8 @@ namespace Light { { m_GraphicsAPI = GraphicsAPI::DirectX; + HRESULT hr; + DXGI_SWAP_CHAIN_DESC sd = { 0 }; sd.BufferCount = 1u; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; @@ -31,15 +33,15 @@ namespace Light { sd.SampleDesc.Count = 1u; sd.Windowed = true; - D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, + DXC(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, - &sd, &m_SwapChain, &m_Device, NULL, &m_DeviceContext); + &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); + DXC(m_SwapChain->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer)); + DXC(m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView)); m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr); @@ -55,11 +57,11 @@ namespace Light { m_DeviceContext->RSSetViewports(1u, &viewport); - dxSharedContext* sharedContext = new dxSharedContext({m_DeviceContext, m_SwapChain, m_RenderTargetView}); + dxSharedContext* sharedContext = new dxSharedContext({m_DeviceContext, m_SwapChain, m_RenderTargetView, m_Device}); m_SharedContext = sharedContext; // log some information about dx context // -// locals + // locals IDXGIDevice* DXGIDevice; IDXGIAdapter* DXGIAdapter; DXGI_ADAPTER_DESC DXGIAdapterDesc; diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h index aeed2f6..afb9fac 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxGraphicsContext.h @@ -1,6 +1,7 @@ #pragma once #include "Base.h" +#include "dxBase.h" #include "Graphics/GraphicsContext.h" #include diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp index a5dc21d..66a27d5 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxRenderCommand.cpp @@ -31,7 +31,7 @@ namespace Light { void dxRenderCommand::Draw(unsigned int count) { - + m_DeviceContext->Draw(count, 0u); } void dxRenderCommand::DrawIndexed(unsigned int count) diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxShader.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxShader.cpp new file mode 100644 index 0000000..7003249 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxShader.cpp @@ -0,0 +1,47 @@ +#include "ltpch.h" +#include "dxShader.h" + +#include "dxSharedContext.h" + +#include + +namespace Light { + + + dxShader::dxShader(const std::string& vertexSource, const std::string& pixelSource, void* sharedContext) + { + HRESULT hr; + + dxSharedContext* dxContext = static_cast(sharedContext); + LT_ENGINE_ASSERT(dxContext, "dxShader::dxShader: invalid dxContext"); + + m_Device = dxContext->device; + m_DeviceContext = dxContext->deviceContext; + + Microsoft::WRL::ComPtr ps = nullptr, vsErr = nullptr, psErr = nullptr; + + DXC(D3DCompile(vertexSource.c_str(), vertexSource.length(), NULL, nullptr, nullptr, "main", "vs_4_0", NULL, NULL, &m_VertexBlob, &vsErr)); + DXC(D3DCompile(pixelSource.c_str(), pixelSource.length(), NULL, nullptr, nullptr, "main", "ps_4_0", NULL, NULL, &ps, &psErr)); + + LT_ENGINE_ASSERT(!vsErr.Get(), "dxShader::dxShader: vertex shader compile error: {}", (char*)vsErr->GetBufferPointer()); + LT_ENGINE_ASSERT(!psErr.Get(), "dxShader::dxShader: vertex shader compile error: {}", (char*)psErr->GetBufferPointer()); + + DXC(m_Device->CreateVertexShader(m_VertexBlob->GetBufferPointer(), m_VertexBlob->GetBufferSize(), NULL, &m_VertexShader)); + DXC(m_Device->CreatePixelShader(ps->GetBufferPointer(), ps->GetBufferSize(), NULL, &m_PixelShader)); + } + + dxShader::~dxShader() + { + } + + void dxShader::Bind() + { + m_DeviceContext->VSSetShader(m_VertexShader.Get(), nullptr, 0u); + m_DeviceContext->PSSetShader(m_PixelShader.Get(), nullptr, 0u); + } + + void dxShader::UnBind() + { + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxShader.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxShader.h new file mode 100644 index 0000000..ec89f79 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxShader.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Base.h" +#include "dxBase.h" +#include "Graphics/Shader.h" + +#include +#include + +namespace Light { + + class dxShader : public Shader + { + private: + Microsoft::WRL::ComPtr m_VertexShader; + Microsoft::WRL::ComPtr m_PixelShader; + + Microsoft::WRL::ComPtr m_Device; + Microsoft::WRL::ComPtr m_DeviceContext; + + Microsoft::WRL::ComPtr m_VertexBlob; + public: + dxShader(const std::string& vertexSource, const std::string& pixelSource, void* sharedContext); + ~dxShader(); + + void Bind() override; + void UnBind() override; + + Microsoft::WRL::ComPtr GetVertexBlob() { return m_VertexBlob; } + }; + + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h index 6a6d16c..7dc4324 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxSharedContext.h @@ -1,6 +1,7 @@ #pragma once #include "Base.h" +#include "dxBase.h" #include #include @@ -12,6 +13,7 @@ namespace Light { Microsoft::WRL::ComPtr deviceContext; Microsoft::WRL::ComPtr swapChain; Microsoft::WRL::ComPtr renderTargetView; + Microsoft::WRL::ComPtr device; }; } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp new file mode 100644 index 0000000..8b0bb7c --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp @@ -0,0 +1,69 @@ +#include "ltpch.h" +#include "dxUserInterface.h" + +#include "dxSharedContext.h" + +#include +#include +#include + +#define GLFW_EXPOSE_NATIVE_WIN32 +#include +#include + +namespace Light { + + dxUserInterface::dxUserInterface(GLFWwindow* windowHandle, void* sharedContext) + { + // set dxContext + dxSharedContext* dxContext = static_cast(sharedContext); + LT_ENGINE_ASSERT(dxContext, "dxUserInterface::dxUserInterface: invalid sharedContext"); + + m_DeviceContext = dxContext->deviceContext; + m_Device = dxContext->device; + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + + ImGui::StyleColorsDark(); + + ImGui_ImplWin32_Init(glfwGetWin32Window(windowHandle)); + ImGui_ImplDX11_Init(m_Device.Get(), m_DeviceContext.Get()); + } + + dxUserInterface::~dxUserInterface() + { + ImGui_ImplDX11_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); + } + + void dxUserInterface::Begin() + { + ImGui_ImplDX11_NewFrame(); + ImGui_ImplWin32_NewFrame(); + ImGui::NewFrame(); + + // TEMP + ImGui::ShowDemoWindow(); + } + + void dxUserInterface::End() + { + ImGui::Render(); + ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); + } + + void dxUserInterface::LogDebugData() + { + LT_ENGINE_INFO("________________________________________"); + LT_ENGINE_INFO("UserInterface::"); + LT_ENGINE_INFO(" API : ImGui"); + LT_ENGINE_INFO(" Version: {}", ImGui::GetVersion()); + LT_ENGINE_INFO(" GfxAPI : DirectX"); + LT_ENGINE_INFO("________________________________________"); + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h new file mode 100644 index 0000000..5109441 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Base.h" +#include "dxBase.h" +#include "UserInterface/UserInterface.h" + +struct GLFWwindow; + +#include +#include + +namespace Light { + + class dxUserInterface : public UserInterface + { + private: + Microsoft::WRL::ComPtr m_Device; + Microsoft::WRL::ComPtr m_DeviceContext; + + public: + dxUserInterface(GLFWwindow* windowHandle, void* sharedContext); + ~dxUserInterface(); + + void Begin() override; + void End() override; + + void LogDebugData() override; + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp new file mode 100644 index 0000000..d704c3b --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp @@ -0,0 +1,81 @@ +#include "ltpch.h" +#include "dxVertexLayout.h" + +#include "dxShader.h" +#include "dxSharedContext.h" + +namespace Light { + + dxVertexLayout::dxVertexLayout(Shader* shader, const std::vector>& elements, void* sharedContext) + { + HRESULT hr; + + dxSharedContext* dxContext = static_cast(sharedContext); + LT_ENGINE_ASSERT(dxContext, "dxShader::dxShader: invalid dxContext"); + + m_Device = dxContext->device; + m_DeviceContext = dxContext->deviceContext; + + std::vector inputElementsDesc; + inputElementsDesc.reserve(elements.size()); + + for (const auto& element : elements) + { + inputElementsDesc.emplace_back(D3D11_INPUT_ELEMENT_DESC{ + element.first.c_str(), + 0u, + GetDxgiFormat(element.second), + 0u, + D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_VERTEX_DATA, + 0u }); + } + + dxShader* dxpShader = static_cast(shader); + LT_ENGINE_ASSERT(dxpShader, "dxVertexLayout::dxVertexLayout: failed to cast Shader to dxShader"); + + DXC(m_Device->CreateInputLayout(&inputElementsDesc[0], inputElementsDesc.size(), dxpShader->GetVertexBlob().Get()->GetBufferPointer(), dxpShader->GetVertexBlob().Get()->GetBufferSize(), &m_InputLayout)); + } + + dxVertexLayout::~dxVertexLayout() + { + + } + + void dxVertexLayout::Bind() + { + m_DeviceContext->IASetInputLayout(m_InputLayout.Get()); + } + + void dxVertexLayout::UnBind() + { + } + + DXGI_FORMAT dxVertexLayout::GetDxgiFormat(VertexElementType type) + { + switch (type) + { + case Light::VertexElementType::Int1: return DXGI_FORMAT_R32_SINT; + case Light::VertexElementType::Int2: return DXGI_FORMAT_R32G32_SINT; + case Light::VertexElementType::Int3: return DXGI_FORMAT_R32G32B32_SINT; + case Light::VertexElementType::Int4: return DXGI_FORMAT_R32G32B32A32_SINT; + case Light::VertexElementType::UInt1: return DXGI_FORMAT_R32_UINT; + case Light::VertexElementType::UInt2: return DXGI_FORMAT_R32G32_UINT; + case Light::VertexElementType::UInt3: return DXGI_FORMAT_R32G32B32_UINT; + case Light::VertexElementType::UInt4: return DXGI_FORMAT_R32G32B32A32_UINT; + case Light::VertexElementType::Float1: return DXGI_FORMAT_R32_FLOAT; + case Light::VertexElementType::Float2: return DXGI_FORMAT_R32G32_FLOAT; + case Light::VertexElementType::Float3: return DXGI_FORMAT_R32G32B32_FLOAT; + case Light::VertexElementType::Float4: return DXGI_FORMAT_R32G32B32A32_FLOAT; + + // TODO: + case Light::VertexElementType::Double1: + case Light::VertexElementType::Double2: + case Light::VertexElementType::Double3: + case Light::VertexElementType::Double4: + + default: LT_ENGINE_ASSERT(false, "dxVertexLayout::GetDxgiFormat: invalid type"); + } + } + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.h new file mode 100644 index 0000000..41accc0 --- /dev/null +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Base.h" +#include "dxBase.h" +#include "Graphics/VertexLayout.h" + +#include +#include + +namespace Light { + + class Shader; + + class dxVertexLayout : public VertexLayout + { + private: + Microsoft::WRL::ComPtr m_InputLayout; + + Microsoft::WRL::ComPtr m_Device; + Microsoft::WRL::ComPtr m_DeviceContext; + + public: + dxVertexLayout(Shader* shader, const std::vector>& elements, void* sharedContext); + ~dxVertexLayout(); + + void Bind() override; + void UnBind() override; + + private: + DXGI_FORMAT GetDxgiFormat(VertexElementType type); + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp index ccbc4d8..08866d9 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp @@ -33,6 +33,7 @@ namespace Light { ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); + // TEMP ImGui::ShowDemoWindow(); } @@ -48,6 +49,7 @@ namespace Light { LT_ENGINE_INFO("UserInterface::"); LT_ENGINE_INFO(" API : ImGui"); LT_ENGINE_INFO(" Version: {}", ImGui::GetVersion()); + LT_ENGINE_INFO(" GfxAPI : OpenGL"); LT_ENGINE_INFO("________________________________________"); } diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h index 4776b0a..cab0cbe 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h @@ -14,7 +14,7 @@ namespace Light { void Begin() override; void End() override; - virtual void LogDebugData() override; + void LogDebugData() override; }; } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp index 755fa6b..40415cf 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.cpp @@ -7,7 +7,7 @@ namespace Light { - glVertexLayout::glVertexLayout(VertexBuffer* buffer, std::vector elements) + glVertexLayout::glVertexLayout(VertexBuffer* buffer, const std::vector>& elements) { // sanity check LT_ENGINE_ASSERT(dynamic_cast(buffer), "glVertexLayout::glVertexLayout: failed to cast VertexBuffer to glVertexBuffer"); @@ -18,7 +18,7 @@ namespace Light { unsigned int stride = 0u; for(const auto& element : elements) { - elementsDesc.push_back(GetElementDesc(element, stride)); + elementsDesc.push_back(GetElementDesc(element.second, stride)); stride += elementsDesc.back().typeSize * elementsDesc.back().count; } diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.h index fe44432..b63f691 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.h +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glVertexLayout.h @@ -19,7 +19,7 @@ namespace Light { unsigned int m_ArrayID; public: - glVertexLayout(VertexBuffer* buffer, std::vector elements); + glVertexLayout(VertexBuffer* buffer, const std::vector>& elements); ~glVertexLayout(); void Bind() override; diff --git a/Engine/src/Platform/OS/Windows/wWindow.cpp b/Engine/src/Platform/OS/Windows/wWindow.cpp index ee77820..d822e96 100644 --- a/Engine/src/Platform/OS/Windows/wWindow.cpp +++ b/Engine/src/Platform/OS/Windows/wWindow.cpp @@ -28,7 +28,7 @@ namespace Light { glfwSetWindowUserPointer(m_Handle, &m_EventCallback); BindGlfwEvents(); - m_GraphicsContext = std::unique_ptr(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle)); + m_GraphicsContext = std::unique_ptr(GraphicsContext::Create(GraphicsAPI::DirectX, m_Handle)); LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed"); } diff --git a/Sandbox/imgui.ini b/Sandbox/imgui.ini index 886a46e..f150ffd 100644 --- a/Sandbox/imgui.ini +++ b/Sandbox/imgui.ini @@ -5,7 +5,7 @@ Collapsed=0 [Window][Dear ImGui Demo] Pos=-3,-1 -Size=243,295 +Size=244,247 Collapsed=0 [Window][Dear ImGui Metrics/Debugger] diff --git a/Sandbox/res/fragment.fragment b/Sandbox/res/fragment.fragment index 7a5fdd9..5ffca78 100644 --- a/Sandbox/res/fragment.fragment +++ b/Sandbox/res/fragment.fragment @@ -1,9 +1,4 @@ -#version 450 core - -out vec4 FragColor; -in vec4 vsout_Color; - -void main() +float4 main(float4 Color : COLOR) : SV_Target { - FragColor = vsout_Color; + return Color; } \ No newline at end of file diff --git a/Sandbox/res/vertex.vertex b/Sandbox/res/vertex.vertex index 2d0f490..bc40102 100644 --- a/Sandbox/res/vertex.vertex +++ b/Sandbox/res/vertex.vertex @@ -1,12 +1,13 @@ -#version 450 core - -layout(location = 0) in vec2 a_Position; -layout(location = 1) in vec4 a_Color; - -out vec4 vsout_Color; - -void main() +struct VertexOut { - gl_Position = vec4(a_Position, 0.0, 1.0); - vsout_Color = a_Color; + float4 Color : COLOR; + float4 Position : SV_Position; +}; + +VertexOut main(float2 InPosition : POSITION, float4 InColor : COLOR) +{ + VertexOut vso; + vso.Position = float4(InPosition.x, InPosition.y, 0.0f, 1.0f); + vso.Color = InColor; + return vso; } \ No newline at end of file