DirectX
This commit is contained in:
parent
ce1aa29e0e
commit
2f560239cb
32 changed files with 464 additions and 72 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -52,8 +52,8 @@ namespace Light {
|
|||
|
||||
// create gfx context dependent classes
|
||||
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));
|
||||
s_Context->m_UserInterface = std::unique_ptr<UserInterface>(UserInterface::Create(windowHandle, s_Context->m_SharedContext));
|
||||
s_Context->m_Renderer = std::unique_ptr<Renderer>(Renderer::Create(s_Context->m_RenderCommand, s_Context->m_SharedContext));
|
||||
|
||||
// sanity check
|
||||
LT_ENGINE_ASSERT(s_Context->m_RenderCommand, "GraphicsContext::Create: RenderCommand creation failed");
|
||||
|
|
|
@ -9,48 +9,27 @@ namespace Light {
|
|||
|
||||
Renderer* Renderer::m_Context;
|
||||
|
||||
Renderer::Renderer(std::shared_ptr<RenderCommand> renderCommand)
|
||||
: m_RenderCommand(renderCommand)
|
||||
Renderer::Renderer(std::shared_ptr<RenderCommand> renderCommand, void* sharedContext)
|
||||
: m_RenderCommand(renderCommand), m_SharedContext(sharedContext)
|
||||
{
|
||||
m_Context = this;
|
||||
|
||||
m_Shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment"));
|
||||
|
||||
// m_Shader = std::unique_ptr<Shader>(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>(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>(VertexBuffer::Create((2 + 4) * 3, vertices));
|
||||
m_VertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_VertexBuffer.get(), { VertexElementType::Float2, VertexElementType::Float4 }));
|
||||
m_VertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(6 * sizeof(float), (2 + 4) * 3, vertices, m_SharedContext));
|
||||
m_VertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_VertexBuffer.get(), m_Shader.get(), { { "POSITION", VertexElementType::Float2 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext));
|
||||
}
|
||||
|
||||
Renderer* Renderer::Create(std::shared_ptr<RenderCommand> renderCommand)
|
||||
Renderer* Renderer::Create(std::shared_ptr<RenderCommand> 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,13 +22,15 @@ namespace Light {
|
|||
std::unique_ptr<Shader> m_Shader;
|
||||
std::unique_ptr<VertexBuffer> m_VertexBuffer;
|
||||
std::unique_ptr<VertexLayout> m_VertexLayout;
|
||||
|
||||
void* m_SharedContext;
|
||||
public:
|
||||
static Renderer* Create(std::shared_ptr<RenderCommand> renderCommand);
|
||||
static Renderer* Create(std::shared_ptr<RenderCommand> renderCommand, void* sharedContext);
|
||||
|
||||
void Draw();
|
||||
|
||||
private:
|
||||
Renderer(std::shared_ptr<RenderCommand> renderCommand);
|
||||
Renderer(std::shared_ptr<RenderCommand> renderCommand, void* sharedContext);
|
||||
};
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<VertexElementType>& elements)
|
||||
VertexLayout* VertexLayout::Create(VertexBuffer* buffer, Shader* shader, const std::vector<std::pair<std::string, VertexElementType>>& 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;
|
||||
|
|
|
@ -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<VertexElementType>& elements);
|
||||
static VertexLayout* Create(VertexBuffer* buffer, Shader* shader, const std::vector<std::pair<std::string, VertexElementType>>& elements, void* sharedContext);
|
||||
|
||||
virtual ~VertexLayout() = default;;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
6
Engine/src/Platform/GraphicsAPI/DirectX/dxBase.h
Normal file
6
Engine/src/Platform/GraphicsAPI/DirectX/dxBase.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
|
||||
// DirectX Call
|
||||
#define DXC(x) hr = x; if(FAILED(x)) __debugbreak()
|
49
Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp
Normal file
49
Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp
Normal file
|
@ -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<dxSharedContext*>(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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
29
Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h
Normal file
29
Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
#include "dxBase.h"
|
||||
#include "Graphics/Buffers.h"
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <wrl.h>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class dxVertexBuffer : public VertexBuffer
|
||||
{
|
||||
private:
|
||||
Microsoft::WRL::ComPtr<ID3D11Buffer> m_Buffer;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> 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;
|
||||
};
|
||||
|
||||
}
|
|
@ -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<ID3D11Resource> 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;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
#include "dxBase.h"
|
||||
#include "Graphics/GraphicsContext.h"
|
||||
|
||||
#include <d3d11.h>
|
||||
|
|
|
@ -31,7 +31,7 @@ namespace Light {
|
|||
|
||||
void dxRenderCommand::Draw(unsigned int count)
|
||||
{
|
||||
|
||||
m_DeviceContext->Draw(count, 0u);
|
||||
}
|
||||
|
||||
void dxRenderCommand::DrawIndexed(unsigned int count)
|
||||
|
|
47
Engine/src/Platform/GraphicsAPI/DirectX/dxShader.cpp
Normal file
47
Engine/src/Platform/GraphicsAPI/DirectX/dxShader.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "ltpch.h"
|
||||
#include "dxShader.h"
|
||||
|
||||
#include "dxSharedContext.h"
|
||||
|
||||
#include <d3dcompiler.h>
|
||||
|
||||
namespace Light {
|
||||
|
||||
|
||||
dxShader::dxShader(const std::string& vertexSource, const std::string& pixelSource, void* sharedContext)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
dxSharedContext* dxContext = static_cast<dxSharedContext*>(sharedContext);
|
||||
LT_ENGINE_ASSERT(dxContext, "dxShader::dxShader: invalid dxContext");
|
||||
|
||||
m_Device = dxContext->device;
|
||||
m_DeviceContext = dxContext->deviceContext;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3DBlob> 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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
33
Engine/src/Platform/GraphicsAPI/DirectX/dxShader.h
Normal file
33
Engine/src/Platform/GraphicsAPI/DirectX/dxShader.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
#include "dxBase.h"
|
||||
#include "Graphics/Shader.h"
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <wrl.h>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class dxShader : public Shader
|
||||
{
|
||||
private:
|
||||
Microsoft::WRL::ComPtr<ID3D11VertexShader> m_VertexShader;
|
||||
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_PixelShader;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3DBlob> m_VertexBlob;
|
||||
public:
|
||||
dxShader(const std::string& vertexSource, const std::string& pixelSource, void* sharedContext);
|
||||
~dxShader();
|
||||
|
||||
void Bind() override;
|
||||
void UnBind() override;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3DBlob> GetVertexBlob() { return m_VertexBlob; }
|
||||
};
|
||||
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
#include "dxBase.h"
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <wrl.h>
|
||||
|
@ -12,6 +13,7 @@ namespace Light {
|
|||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext;
|
||||
Microsoft::WRL::ComPtr<IDXGISwapChain> swapChain;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> renderTargetView;
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> device;
|
||||
};
|
||||
|
||||
}
|
69
Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp
Normal file
69
Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include "ltpch.h"
|
||||
#include "dxUserInterface.h"
|
||||
|
||||
#include "dxSharedContext.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_win32.h>
|
||||
#include <imgui_impl_dx11.h>
|
||||
|
||||
#define GLFW_EXPOSE_NATIVE_WIN32
|
||||
#include <glfw/glfw3.h>
|
||||
#include <glfw/glfw3native.h>
|
||||
|
||||
namespace Light {
|
||||
|
||||
dxUserInterface::dxUserInterface(GLFWwindow* windowHandle, void* sharedContext)
|
||||
{
|
||||
// set dxContext
|
||||
dxSharedContext* dxContext = static_cast<dxSharedContext*>(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("________________________________________");
|
||||
}
|
||||
|
||||
}
|
30
Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h
Normal file
30
Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
#include "dxBase.h"
|
||||
#include "UserInterface/UserInterface.h"
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <wrl.h>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class dxUserInterface : public UserInterface
|
||||
{
|
||||
private:
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
|
||||
|
||||
public:
|
||||
dxUserInterface(GLFWwindow* windowHandle, void* sharedContext);
|
||||
~dxUserInterface();
|
||||
|
||||
void Begin() override;
|
||||
void End() override;
|
||||
|
||||
void LogDebugData() override;
|
||||
};
|
||||
|
||||
}
|
81
Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp
Normal file
81
Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.cpp
Normal file
|
@ -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<std::pair<std::string, VertexElementType>>& elements, void* sharedContext)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
dxSharedContext* dxContext = static_cast<dxSharedContext*>(sharedContext);
|
||||
LT_ENGINE_ASSERT(dxContext, "dxShader::dxShader: invalid dxContext");
|
||||
|
||||
m_Device = dxContext->device;
|
||||
m_DeviceContext = dxContext->deviceContext;
|
||||
|
||||
std::vector<D3D11_INPUT_ELEMENT_DESC> 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<dxShader*>(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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
33
Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.h
Normal file
33
Engine/src/Platform/GraphicsAPI/DirectX/dxVertexLayout.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
#include "dxBase.h"
|
||||
#include "Graphics/VertexLayout.h"
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <wrl.h>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class Shader;
|
||||
|
||||
class dxVertexLayout : public VertexLayout
|
||||
{
|
||||
private:
|
||||
Microsoft::WRL::ComPtr<ID3D11InputLayout> m_InputLayout;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
|
||||
|
||||
public:
|
||||
dxVertexLayout(Shader* shader, const std::vector<std::pair<std::string, VertexElementType>>& elements, void* sharedContext);
|
||||
~dxVertexLayout();
|
||||
|
||||
void Bind() override;
|
||||
void UnBind() override;
|
||||
|
||||
private:
|
||||
DXGI_FORMAT GetDxgiFormat(VertexElementType type);
|
||||
};
|
||||
|
||||
}
|
|
@ -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("________________________________________");
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace Light {
|
|||
void Begin() override;
|
||||
void End() override;
|
||||
|
||||
virtual void LogDebugData() override;
|
||||
void LogDebugData() override;
|
||||
};
|
||||
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace Light {
|
||||
|
||||
glVertexLayout::glVertexLayout(VertexBuffer* buffer, std::vector<VertexElementType> elements)
|
||||
glVertexLayout::glVertexLayout(VertexBuffer* buffer, const std::vector<std::pair<std::string, VertexElementType>>& elements)
|
||||
{
|
||||
// sanity check
|
||||
LT_ENGINE_ASSERT(dynamic_cast<glVertexBuffer*>(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;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Light {
|
|||
unsigned int m_ArrayID;
|
||||
|
||||
public:
|
||||
glVertexLayout(VertexBuffer* buffer, std::vector<VertexElementType> elements);
|
||||
glVertexLayout(VertexBuffer* buffer, const std::vector<std::pair<std::string, VertexElementType>>& elements);
|
||||
~glVertexLayout();
|
||||
|
||||
void Bind() override;
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace Light {
|
|||
glfwSetWindowUserPointer(m_Handle, &m_EventCallback);
|
||||
BindGlfwEvents();
|
||||
|
||||
m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle));
|
||||
m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::DirectX, m_Handle));
|
||||
LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed");
|
||||
}
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Add table
Reference in a new issue