Major changes

This commit is contained in:
Light 2021-06-15 09:39:11 +04:30
parent 0510712d6e
commit bb604e9540
21 changed files with 172 additions and 62 deletions

BIN
Engine/dxgidebug.dll Normal file

Binary file not shown.

View file

@ -68,15 +68,15 @@ project "Engine"
-- debug -- debug
filter "configurations:Debug" filter "configurations:Debug"
defines "LT_DEBUG" defines "LIGHT_DEBUG"
symbols "on" symbols "on"
-- release -- release
filter "configurations:Release" filter "configurations:Release"
defines "LT_RELEASE" defines "LIGHT_RELEASE"
optimize "on" optimize "on"
-- distribution -- distribution
filter "configurations:Distribution" filter "configurations:Distribution"
defines "LT_DIST" defines "LIGHT_DIST"
optimize "on" optimize "on"

View file

@ -13,12 +13,14 @@
#if defined(LIGHT_PLATFORM_WINDOWS) #if defined(LIGHT_PLATFORM_WINDOWS)
#define LT_BUILD_PLATFORM "Windows" #define LT_BUILD_PLATFORM "Windows"
#define LT_WIN(x) x #define LT_WIN(x) x
#elif defined(LT_PLATFORM_LINUX) #elif defined(LIGHT_PLATFORM_LINUX)
#error "Unsupported platform: UNIX" #error "Unsupported platform: UNIX"
#define LT_LIN(x) #define LT_LIN(x)
#elif defined(LIGHT_PLATFORM_MAC)
#error "Unsupported platform: MAC"
#define LT_MAC(x) x
#else #else
#error "Unsupported platform: Unknown" #error "Unsupported platform: Unknown"
#define LT_MAC(x) x
#endif #endif
#define BIT(x) 1 << x #define BIT(x) 1 << x

View file

@ -31,6 +31,7 @@ namespace Light {
LT_ENGINE_ASSERT(!m_LayerStack.IsEmpty(), "Application::GameLoop: Layerstack is empty"); LT_ENGINE_ASSERT(!m_LayerStack.IsEmpty(), "Application::GameLoop: Layerstack is empty");
// Log window data // Log window data
LogDebugData();
m_Window->GetGfxContext()->LogDebugData(); m_Window->GetGfxContext()->LogDebugData();
m_Window->GetGfxContext()->GetUserInterface()->LogDebugData(); m_Window->GetGfxContext()->GetUserInterface()->LogDebugData();
@ -78,4 +79,12 @@ namespace Light {
m_LayerStack.OnEvent(event); m_LayerStack.OnEvent(event);
} }
void Application::LogDebugData()
{
LT_ENGINE_INFO("________________________________________");
LT_ENGINE_INFO("Platform::");
LT_ENGINE_INFO(" OS: {}", LT_BUILD_PLATFORM);
LT_ENGINE_INFO("________________________________________");
}
} }

View file

@ -33,6 +33,8 @@ namespace Light {
private: private:
void OnEvent(const Event& event); void OnEvent(const Event& event);
void LogDebugData();
}; };
} }

View file

@ -5,7 +5,7 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
// LOGGER MACROS // // LOGGER MACROS //
#ifndef LT_DIST #ifndef LIGHT_DIST
// Engine // Engine
#define LT_ENGINE_TRACE(...) ::Light::Logger::GetEngineLogger()->log(spdlog::level::trace , __VA_ARGS__) #define LT_ENGINE_TRACE(...) ::Light::Logger::GetEngineLogger()->log(spdlog::level::trace , __VA_ARGS__)
#define LT_ENGINE_INFO(...) ::Light::Logger::GetEngineLogger()->log(spdlog::level::info , __VA_ARGS__) #define LT_ENGINE_INFO(...) ::Light::Logger::GetEngineLogger()->log(spdlog::level::info , __VA_ARGS__)

View file

@ -18,7 +18,7 @@ namespace Light {
m_QuadRenderer.shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext)); m_QuadRenderer.shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext));
m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, m_SharedContext)); m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, m_SharedContext));
m_QuadRenderer.vertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_QuadRenderer.vertexBuffer.get(), m_QuadRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext)); m_QuadRenderer.vertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_QuadRenderer.vertexBuffer.get(), m_QuadRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext));
m_QuadRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 3, m_SharedContext)); m_QuadRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 6, m_SharedContext));
// QUADRENDERER // // QUADRENDERER //
} }
@ -49,27 +49,21 @@ namespace Light {
const float xMax = position.x + size.x; const float xMax = position.x + size.x;
const float yMax = position.y + size.y; const float yMax = position.y + size.y;
// TOP LEFT
m_QuadRenderer.mapCurrent->position = { xMin, yMin, position.z };
m_QuadRenderer.mapCurrent->tint = glm::vec4(1.0f, 0.1f, 0.1f, 1.0f);
m_QuadRenderer.mapCurrent++;
// TOP RIGHT m_QuadRenderer.mapCurrent[0].position = { xMin, yMin, position.z };
m_QuadRenderer.mapCurrent->position = { xMax, yMin, position.z }; m_QuadRenderer.mapCurrent[0].tint = glm::vec4(0.1f, 0.1f, 1.0f, 1.0f);
m_QuadRenderer.mapCurrent->tint = glm::vec4(0.1f, 1.0f, 0.1f, 1.0f);
m_QuadRenderer.mapCurrent++;
// BOTTOM RIGHT m_QuadRenderer.mapCurrent[1].position = { xMax, yMin, position.z };
m_QuadRenderer.mapCurrent->position = { xMax, yMax, position.z }; m_QuadRenderer.mapCurrent[1].tint = glm::vec4(0.3f, 0.3f, 0.3f, 1.0f);
m_QuadRenderer.mapCurrent->tint = glm::vec4(0.3f, 0.3f, 0.3f, 1.0f);
m_QuadRenderer.mapCurrent++;
// BOTTOM LEFT m_QuadRenderer.mapCurrent[2].position = { xMax, yMax, position.z };
m_QuadRenderer.mapCurrent->position = { xMin, yMax, position.z }; m_QuadRenderer.mapCurrent[2].tint = glm::vec4(0.1f, 1.0f, 0.1f, 1.0f);
m_QuadRenderer.mapCurrent->tint = glm::vec4(0.1f, 0.1f, 1.0f, 1.0f);
m_QuadRenderer.mapCurrent++; m_QuadRenderer.mapCurrent[3].position = { xMin, yMax, position.z };
m_QuadRenderer.mapCurrent[3].tint = glm::vec4(1.0f, 0.1f, 0.1f, 1.0f);
// advance // advance
m_QuadRenderer.mapCurrent += 4;
m_QuadRenderer.quadCount++; m_QuadRenderer.quadCount++;
} }
@ -80,10 +74,14 @@ namespace Light {
} }
void Renderer::EndScene() void Renderer::EndScene()
{
if (m_QuadRenderer.quadCount)
{ {
m_QuadRenderer.Bind(); m_QuadRenderer.Bind();
m_RenderCommand->DrawIndexed(m_QuadRenderer.quadCount * 6); m_RenderCommand->DrawIndexed(m_QuadRenderer.quadCount * 6);
m_QuadRenderer.quadCount = 0; m_QuadRenderer.quadCount = 0;
} }
}
} }

View file

@ -15,16 +15,22 @@ namespace Light {
Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath, void* sharedContext) Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath, void* sharedContext)
{ {
// load shader source // load shader source
const std::string vertexSource = FileManager::ReadTXTFile(vertexPath); std::string vertexSource = FileManager::ReadTXTFile(vertexPath);
const std::string pixelSource = FileManager::ReadTXTFile(pixelPath); std::string pixelSource = FileManager::ReadTXTFile(pixelPath);
switch (GraphicsContext::GetGraphicsAPI()) switch (GraphicsContext::GetGraphicsAPI())
{ {
case GraphicsAPI::OpenGL: case GraphicsAPI::OpenGL:
ExtractShaderSource(vertexSource, "GLSL");
ExtractShaderSource(pixelSource, "GLSL");
return new glShader(vertexSource, pixelSource); return new glShader(vertexSource, pixelSource);
case GraphicsAPI::DirectX: case GraphicsAPI::DirectX: LT_WIN(
return new dxShader(vertexSource, pixelSource, sharedContext); ExtractShaderSource(vertexSource, "HLSL");
ExtractShaderSource(pixelSource, "HLSL");
return new dxShader(vertexSource, pixelSource, sharedContext);)
default: default:
LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
@ -32,4 +38,23 @@ namespace Light {
} }
} }
void Shader::ExtractShaderSource(std::string& src, const std::string& delim)
{
size_t begDelimPos, endDelimPos;
begDelimPos = src.find('+' + delim) + 5;
endDelimPos = src.find('-' + delim);
LT_ENGINE_ASSERT(begDelimPos != std::string::npos + 5,
"Shader::ExtractShaderSource: failed to find the start delimeter in shader source, delim: {}, shader:\n{}",
delim, src);
LT_ENGINE_ASSERT(endDelimPos != std::string::npos,
"Shader::ExtractShaderSource: failed to find the end delimeter in shader source, delim: {}, shader:\n{}",
delim, src);
src = src.substr(begDelimPos, endDelimPos - begDelimPos);
}
} }

View file

@ -16,6 +16,9 @@ namespace Light {
protected: protected:
Shader() = default; Shader() = default;
private:
static void ExtractShaderSource(std::string& src, const std::string& delim);
}; };
} }

View file

@ -7,13 +7,10 @@ namespace Light {
class Layer; class Layer;
class Event; class Event;
template <typename T>
using Raw = T*;
class LayerStack class LayerStack
{ {
private: private:
static Raw<LayerStack> s_Context; static LayerStack* s_Context;
std::vector<Layer*> m_Layers; std::vector<Layer*> m_Layers;

View file

@ -4,7 +4,7 @@
namespace Light { namespace Light {
// TODO: optimize // TODO: optimize!!
class FileManager class FileManager
{ {
public: public:

View file

@ -53,7 +53,6 @@ namespace Light {
{ {
} }
dxIndexBuffer::dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext) dxIndexBuffer::dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext)
{ {
HRESULT hr; HRESULT hr;
@ -69,7 +68,7 @@ namespace Light {
{ {
if (count % 6 != 0) if (count % 6 != 0)
{ {
LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: count should be divisible by 6 when no indices is provided"); LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: indices can only be null if count is multiple of 6");
LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6))); LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
count = count + (6 - (count % 6)); count = count + (6 - (count % 6));
} }
@ -79,11 +78,11 @@ namespace Light {
for (unsigned int i = 0; i < count; i += 6) for (unsigned int i = 0; i < count; i += 6)
{ {
indices[i + 0] = offset + 0; indices[i + 0] = offset + 0;
indices[i + 1] = offset + 1; indices[i + 1] = offset + 3;
indices[i + 2] = offset + 2; indices[i + 2] = offset + 2;
indices[i + 3] = offset + 2; indices[i + 3] = offset + 2;
indices[i + 4] = offset + 3; indices[i + 4] = offset + 1;
indices[i + 5] = offset + 0; indices[i + 5] = offset + 0;
offset += 4; offset += 4;
@ -103,7 +102,10 @@ namespace Light {
DXC(m_Device->CreateBuffer(&bufferDesc, &sd, &m_Buffer)); DXC(m_Device->CreateBuffer(&bufferDesc, &sd, &m_Buffer));
if (!hasIndices) if (!hasIndices)
{
delete[] indices; delete[] indices;
indices = nullptr;
}
} }
dxIndexBuffer::~dxIndexBuffer() dxIndexBuffer::~dxIndexBuffer()

View file

@ -26,17 +26,36 @@ namespace Light {
HRESULT hr; HRESULT hr;
DXGI_SWAP_CHAIN_DESC sd = { 0 }; DXGI_SWAP_CHAIN_DESC sd = { 0 };
sd.BufferCount = 1u; sd.OutputWindow = static_cast<HWND>(glfwGetWin32Window(windowHandle));
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = glfwGetWin32Window(m_WindowHandle); sd.BufferCount = 1u;
sd.BufferDesc.Width = 800u;
sd.BufferDesc.Height = 600u;
sd.BufferDesc.RefreshRate.Denominator = NULL;
sd.BufferDesc.RefreshRate.Numerator = NULL;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.SampleDesc.Count = 1u; sd.SampleDesc.Count = 1u;
sd.SampleDesc.Quality = 0u;
sd.Windowed = true; sd.Windowed = true;
sd.Flags = NULL;
UINT flags = NULL;
#ifdef LIGHT_DEBUG
flags = D3D11_CREATE_DEVICE_DEBUG;
#endif
DXC(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, DXC(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE,
NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, NULL, flags, 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); m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer; Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer;
@ -44,6 +63,24 @@ namespace Light {
DXC(m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView)); DXC(m_Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &m_RenderTargetView));
m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr); m_DeviceContext->OMSetRenderTargets(1u, m_RenderTargetView.GetAddressOf(), nullptr);
Microsoft::WRL::ComPtr<ID3D11InfoQueue> infoQueue;
DXC(m_Device.As(&debugInterface));
DXC(debugInterface.As(&infoQueue));
D3D11_MESSAGE_ID hide[] =
{
D3D11_MESSAGE_ID_DEVICE_DRAW_SAMPLER_NOT_SET,
// #todo: add more message IDs here as needed
};
D3D11_INFO_QUEUE_FILTER filter;
memset(&filter, 0, sizeof(filter));
filter.DenyList.NumIDs = _countof(hide);
filter.DenyList.pIDList = hide;
infoQueue->AddStorageFilterEntries(&filter);
D3D11_VIEWPORT viewport; D3D11_VIEWPORT viewport;
@ -60,6 +97,16 @@ namespace Light {
dxSharedContext* sharedContext = new dxSharedContext({m_DeviceContext, m_SwapChain, m_RenderTargetView, m_Device}); dxSharedContext* sharedContext = new dxSharedContext({m_DeviceContext, m_SwapChain, m_RenderTargetView, m_Device});
m_SharedContext = sharedContext; m_SharedContext = sharedContext;
}
void dxGraphicsContext::OnWindowResize(const WindowResizedEvent& event)
{
}
void dxGraphicsContext::LogDebugData()
{
// log some information about dx context // // log some information about dx context //
// locals // locals
IDXGIDevice* DXGIDevice; IDXGIDevice* DXGIDevice;
@ -82,18 +129,10 @@ namespace Light {
DXGIAdapter->Release(); DXGIAdapter->Release();
// log info // #todo: log more information // log info // #todo: log more information
LT_ENGINE_INFO("________________________________________");
LT_ENGINE_INFO("dxGraphicsContext:"); LT_ENGINE_INFO("dxGraphicsContext:");
LT_ENGINE_INFO(" Renderer: {}", adapterDesc); LT_ENGINE_INFO(" Renderer: {}", adapterDesc);
} LT_ENGINE_INFO("________________________________________");
void dxGraphicsContext::OnWindowResize(const WindowResizedEvent& event)
{
}
void dxGraphicsContext::LogDebugData()
{
} }
} }

View file

@ -21,6 +21,8 @@ namespace Light {
Microsoft::WRL::ComPtr<IDXGISwapChain> m_SwapChain; Microsoft::WRL::ComPtr<IDXGISwapChain> m_SwapChain;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_RenderTargetView; Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_RenderTargetView;
Microsoft::WRL::ComPtr<ID3D11Debug> debugInterface;
public: public:
dxGraphicsContext(GLFWwindow* windowHandle); dxGraphicsContext(GLFWwindow* windowHandle);

View file

@ -36,7 +36,7 @@ namespace Light {
void dxRenderCommand::DrawIndexed(unsigned int count) void dxRenderCommand::DrawIndexed(unsigned int count)
{ {
m_DeviceContext->DrawIndexed(count, 0u, 0u);
} }
} }

View file

@ -39,7 +39,6 @@ namespace Light {
dxVertexLayout::~dxVertexLayout() dxVertexLayout::~dxVertexLayout()
{ {
} }
void dxVertexLayout::Bind() void dxVertexLayout::Bind()

View file

@ -32,6 +32,12 @@ namespace Light {
void glGraphicsContext::OnWindowResize(const WindowResizedEvent& event) void glGraphicsContext::OnWindowResize(const WindowResizedEvent& event)
{ {
if (event.GetSize().x < 0 || event.GetSize().y < 0)
{
LT_ENGINE_ERROR("glGraphicsContext::OnWindowResize: width/height cannot be negative: [{}x{}]", event.GetSize().x, event.GetSize().y);
return;
}
glViewport(0, 0, event.GetSize().x, event.GetSize().y); glViewport(0, 0, event.GetSize().x, event.GetSize().y);
} }
@ -71,6 +77,7 @@ namespace Light {
Stringifier::glDebugMsgSource(source), Stringifier::glDebugMsgSource(source),
Stringifier::glDebugMsgType(type), Stringifier::glDebugMsgType(type),
id); id);
__debugbreak();
LT_ENGINE_CRITICAL(" {}", message); LT_ENGINE_CRITICAL(" {}", message);
return; return;
case GL_DEBUG_SEVERITY_MEDIUM: case GL_DEBUG_SEVERITY_LOW: case GL_DEBUG_SEVERITY_MEDIUM: case GL_DEBUG_SEVERITY_LOW:

View file

@ -30,7 +30,7 @@ namespace Light {
glfwSetWindowUserPointer(m_Handle, &m_EventCallback); glfwSetWindowUserPointer(m_Handle, &m_EventCallback);
BindGlfwEvents(); 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"); LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: graphics context creation failed");
} }

View file

@ -53,15 +53,15 @@ project "Sandbox"
-- debug -- debug
filter "configurations:Debug" filter "configurations:Debug"
defines "LT_DEBUG" defines "LIGHT_DEBUG"
symbols "on" symbols "on"
-- release -- release
filter "configurations:Release" filter "configurations:Release"
defines "LT_RELEASE" defines "LIGHT_RELEASE"
optimize "on" optimize "on"
-- distribution -- distribution
filter "configurations:Distribution" filter "configurations:Distribution"
defines "LT_DIST" defines "LIGHT_DIST"
optimize "on" optimize "on"

View file

@ -1,3 +1,4 @@
+GLSL
#version 440 core #version 440 core
in vec4 fragColor; in vec4 fragColor;
@ -8,3 +9,10 @@ void main()
{ {
FragmentColor = fragColor; FragmentColor = fragColor;
} }
-GLSL
+HLSL
float4 main(float4 Color : COLOR) : SV_Target
{
return Color;
}
-HLSL

View file

@ -1,3 +1,4 @@
+GLSL
#version 440 core #version 440 core
layout(location = 0) in vec3 a_Position; layout(location = 0) in vec3 a_Position;
@ -10,3 +11,19 @@ void main()
gl_Position = vec4(a_Position, 1.0); gl_Position = vec4(a_Position, 1.0);
fragColor = a_Color; fragColor = a_Color;
} }
-GLSL
+HLSL
struct VertexOut
{
float4 Color : COLOR;
float4 Position : SV_Position;
};
VertexOut main(float3 InPosition : POSITION, float4 InColor : COLOR)
{
VertexOut vso;
vso.Position = float4(InPosition.x, InPosition.y, InPosition.z, 1.0);
vso.Color = InColor;
return vso;
}
-HLSL