ResourceManager
- Added ResourceManager - Removed FileManager - Shader's constructor now takes the shader source specific to the current GraphicsAPI
This commit is contained in:
parent
b50564c833
commit
6149c0da70
10 changed files with 236 additions and 82 deletions
|
@ -21,10 +21,16 @@ project "Engine"
|
|||
-- Project Files ---
|
||||
files
|
||||
{
|
||||
"%{prj.location}/src/**.h" ,
|
||||
"%{prj.location}/src/**.cpp" ,
|
||||
"%{prj.location}/**.lua" ,
|
||||
"%{prj.location}/dxgidebug.dll" ,
|
||||
-- src
|
||||
"%{prj.location}/src/**.h",
|
||||
"%{prj.location}/src/**.cpp",
|
||||
|
||||
-- premake
|
||||
"%{prj.location}/preake5*.lua",
|
||||
|
||||
"%{prj.location}/dxgidebug.dll", -- :#todo
|
||||
|
||||
"%{prj.location}/res/**"
|
||||
}
|
||||
|
||||
-- Dependencies --
|
||||
|
@ -38,11 +44,11 @@ project "Engine"
|
|||
|
||||
-- 3rd party
|
||||
(dependenciesdir .. "spdlog/include/"),
|
||||
(dependenciesdir .. "GLFW/include/" ),
|
||||
(dependenciesdir .. "GLAD/include" ),
|
||||
(dependenciesdir .. "imgui/backends" ),
|
||||
(dependenciesdir .. "imgui/" ),
|
||||
(dependenciesdir .. "glm/" ),
|
||||
(dependenciesdir .. "GLFW/include/"),
|
||||
(dependenciesdir .. "GLAD/include"),
|
||||
(dependenciesdir .. "imgui/backends"),
|
||||
(dependenciesdir .. "imgui/"),
|
||||
(dependenciesdir .. "glm/"),
|
||||
}
|
||||
|
||||
links
|
||||
|
|
53
Engine/res/Shaders/QuadShader.h
Normal file
53
Engine/res/Shaders/QuadShader.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
#define LT_ENGINE_RESOURCES_QUAD_SHADER_VS \
|
||||
R"(
|
||||
+GLSL
|
||||
#version 440 core
|
||||
|
||||
layout(location = 0) in vec3 a_Position;
|
||||
layout(location = 1) in vec4 a_Color;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(a_Position, 1.0);
|
||||
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)"
|
||||
|
||||
#define LT_ENGINE_RESOURCES_QUAD_SHADER_PS \
|
||||
R"(
|
||||
+GLSL
|
||||
#version 440 core
|
||||
|
||||
in vec4 fragColor;
|
||||
|
||||
out vec4 FragmentColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragmentColor = fragColor;
|
||||
}
|
||||
-GLSL
|
||||
+HLSL
|
||||
float4 main(float4 Color : COLOR) : SV_Target
|
||||
{
|
||||
return Color;
|
||||
}
|
||||
-HLSL
|
||||
)"
|
|
@ -12,6 +12,7 @@
|
|||
#include "RenderCommand.h"
|
||||
#include "UserInterface/UserInterface.h"
|
||||
|
||||
#include "Utility/ResourceManager.h"
|
||||
#include "Utility/Stringifier.h"
|
||||
|
||||
namespace Light {
|
||||
|
@ -61,6 +62,8 @@ namespace Light {
|
|||
}
|
||||
|
||||
// create gfx context dependent classes
|
||||
s_Context->m_ResourceManager = std::unique_ptr<ResourceManager>(ResourceManager::Create(s_Context->m_SharedContext));
|
||||
|
||||
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_SharedContext));
|
||||
s_Context->m_Renderer = std::unique_ptr<Renderer>(Renderer::Create(s_Context->m_RenderCommand, s_Context->m_SharedContext));
|
||||
|
|
|
@ -6,8 +6,12 @@
|
|||
|
||||
struct GLFWwindow;
|
||||
|
||||
#include "Utility/ResourceManager.h"
|
||||
|
||||
|
||||
namespace Light {
|
||||
|
||||
class ResourceManager;
|
||||
class Renderer;
|
||||
class RenderCommand;
|
||||
class UserInterface;
|
||||
|
@ -28,6 +32,8 @@ namespace Light {
|
|||
private:
|
||||
static GraphicsContext* s_Context;
|
||||
|
||||
std::unique_ptr<ResourceManager> m_ResourceManager;
|
||||
|
||||
std::unique_ptr<Renderer> m_Renderer;
|
||||
std::shared_ptr<RenderCommand> m_RenderCommand;
|
||||
std::unique_ptr<UserInterface> m_UserInterface;
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
|
||||
#include "GraphicsContext.h"
|
||||
|
||||
#include "Utility/ResourceManager.h"
|
||||
|
||||
#include "../res/Shaders/QuadShader.h"
|
||||
|
||||
#include "RenderCommand.h"
|
||||
|
||||
namespace Light {
|
||||
|
@ -14,8 +18,10 @@ namespace Light {
|
|||
{
|
||||
s_Context = this;
|
||||
|
||||
ResourceManager::CreateShader("QuadShader", LT_ENGINE_RESOURCES_QUAD_SHADER_VS, LT_ENGINE_RESOURCES_QUAD_SHADER_PS);
|
||||
|
||||
// QUADRENDERER //
|
||||
m_QuadRenderer.shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext));
|
||||
m_QuadRenderer.shader = ResourceManager::GetShader("QuadShader");
|
||||
m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, m_SharedContext));
|
||||
m_QuadRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 6, 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));
|
||||
|
|
|
@ -9,28 +9,17 @@
|
|||
|
||||
#include "GraphicsContext.h"
|
||||
|
||||
#include "Utility/FileManager.h"
|
||||
|
||||
namespace Light {
|
||||
|
||||
Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath, std::shared_ptr<SharedContext> sharedContext)
|
||||
Shader* Shader::Create(const std::string& vertexSource, const std::string& pixelSource, std::shared_ptr<SharedContext> sharedContext)
|
||||
{
|
||||
// load shader source
|
||||
std::string vertexSource = FileManager::ReadTXTFile(vertexPath);
|
||||
std::string pixelSource = FileManager::ReadTXTFile(pixelPath);
|
||||
|
||||
switch (GraphicsContext::GetGraphicsAPI())
|
||||
{
|
||||
case GraphicsAPI::OpenGL:
|
||||
ExtractShaderSource(vertexSource, "GLSL");
|
||||
ExtractShaderSource(pixelSource, "GLSL");
|
||||
|
||||
return new glShader(vertexSource, pixelSource);
|
||||
|
||||
case GraphicsAPI::DirectX: LT_WIN(
|
||||
ExtractShaderSource(vertexSource, "HLSL");
|
||||
ExtractShaderSource(pixelSource, "HLSL");
|
||||
|
||||
return new dxShader(vertexSource, pixelSource, std::static_pointer_cast<dxSharedContext>(sharedContext));)
|
||||
|
||||
default:
|
||||
|
@ -39,26 +28,4 @@ namespace Light {
|
|||
}
|
||||
}
|
||||
|
||||
void Shader::ExtractShaderSource(std::string& src, const std::string& delim)
|
||||
{
|
||||
size_t begDelimPos, endDelimPos;
|
||||
|
||||
// find begin and end delimiter (eg. +GLSL ... -GLSL )
|
||||
begDelimPos = src.find('+' + delim) + 5;
|
||||
endDelimPos = src.find('-' + delim);
|
||||
|
||||
// check
|
||||
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);
|
||||
|
||||
// extract the shader
|
||||
src = src.substr(begDelimPos, endDelimPos - begDelimPos);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
#include "ltpch.h"
|
||||
#include "FileManager.h"
|
||||
|
||||
namespace Light {
|
||||
|
||||
std::string FileManager::ReadTXTFile(const std::string& path)
|
||||
{
|
||||
// initialize
|
||||
std::ifstream stream(path);
|
||||
std::stringstream ss;
|
||||
std::string line;
|
||||
|
||||
// check
|
||||
LT_ENGINE_ASSERT(!path.empty(), "FileManager::ReadTXTFile: path is empty");
|
||||
LT_ENGINE_ASSERT(stream.is_open(), "FileManager::ReadTXTFile: invalid path: {}", path);
|
||||
|
||||
// read
|
||||
while (std::getline(stream, line))
|
||||
ss << line << '\n';
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
|
||||
namespace Light {
|
||||
|
||||
// TODO: optimize!!
|
||||
class FileManager
|
||||
{
|
||||
public:
|
||||
static std::string ReadTXTFile(const std::string& path);
|
||||
};
|
||||
|
||||
}
|
111
Engine/src/Engine/Utility/ResourceManager.cpp
Normal file
111
Engine/src/Engine/Utility/ResourceManager.cpp
Normal file
|
@ -0,0 +1,111 @@
|
|||
#include "ltpch.h"
|
||||
#include "ResourceManager.h"
|
||||
|
||||
#include "Graphics/GraphicsContext.h"
|
||||
#include "Graphics/Shader.h"
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>g
|
||||
|
||||
namespace Light {
|
||||
|
||||
ResourceManager* ResourceManager::s_Context = nullptr;
|
||||
|
||||
ResourceManager* ResourceManager::Create(std::shared_ptr<SharedContext> sharedContext)
|
||||
{
|
||||
return new ResourceManager(sharedContext);
|
||||
}
|
||||
|
||||
ResourceManager::ResourceManager(std::shared_ptr<SharedContext> sharedContext)
|
||||
: m_SharedContext(sharedContext)
|
||||
{
|
||||
LT_ENGINE_ASSERT(!s_Context, "ResourceManager::ResourceManager: an instance of 'resource manager' already exists, do not construct this class");
|
||||
s_Context = this;
|
||||
}
|
||||
|
||||
void ResourceManager::CreateShaderImpl(const std::string& name, const std::string& vertexSource, const std::string& pixelSource)
|
||||
{
|
||||
// delim
|
||||
std::string delim = GraphicsContext::GetGraphicsAPI() == GraphicsAPI::OpenGL ? "GLSL" :
|
||||
GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX ? "HLSL" : NULL;
|
||||
|
||||
// check
|
||||
LT_ENGINE_ASSERT(!vertexSource.empty(), "ResourceManager::CreateShader: vertex source is empty");
|
||||
LT_ENGINE_ASSERT(!vertexSource.empty(), "ResourceManager::CreateShader: pixel source is empty");
|
||||
LT_ENGINE_ASSERT(!delim.empty(), "ResourceManager::LoadShader: invalid/unsupported graphics api: {}", GraphicsContext::GetGraphicsAPI());
|
||||
|
||||
// save to string
|
||||
std::string vsSource = vertexSource;
|
||||
std::string psSource = pixelSource;
|
||||
|
||||
// extract source
|
||||
ResourceManager::ExtractShaderSource(vsSource, delim);
|
||||
ResourceManager::ExtractShaderSource(psSource, delim);
|
||||
|
||||
// create shader
|
||||
m_Shaders[name] = std::shared_ptr<Shader>(Shader::Create(vsSource, psSource, m_SharedContext));
|
||||
}
|
||||
|
||||
void ResourceManager::LoadShaderImpl(const std::string& name, const std::string& vertexPath, const std::string& pixelPath)
|
||||
{
|
||||
// check
|
||||
LT_ENGINE_ASSERT(!vertexPath.empty(), "ResourceManager::LoadShader: vertex path is empty");
|
||||
LT_ENGINE_ASSERT(!pixelPath.empty(), "ResourceManager::LoadShader: pixel path is empty");
|
||||
|
||||
// initialize
|
||||
std::ifstream vsStream(vertexPath), psStream(pixelPath);
|
||||
std::stringstream vsSS, psSS; // pss pss pss pss :D
|
||||
std::string vertexSource, pixelSource;
|
||||
std::string line;
|
||||
|
||||
// delim
|
||||
std::string delim = GraphicsContext::GetGraphicsAPI() == GraphicsAPI::OpenGL ? "GLSL" :
|
||||
GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX ? "HLSL" : NULL;
|
||||
|
||||
// check
|
||||
LT_ENGINE_ASSERT(!delim.empty(), "ResourceManager::LoadShader: invalid/unsupported graphics api: {}", GraphicsContext::GetGraphicsAPI());
|
||||
LT_ENGINE_ASSERT(vsStream.is_open(), "ResourceManager::LoadShader: invalid vertex path: {}", vertexPath);
|
||||
LT_ENGINE_ASSERT(psStream.is_open(), "ResourceManager::LoadShader: invalid pixel path: {}", pixelPath);
|
||||
|
||||
// read
|
||||
while (std::getline(vsStream, line))
|
||||
vsSS << line << '\n';
|
||||
|
||||
while (std::getline(psStream, line))
|
||||
psSS << line << '\n';
|
||||
|
||||
// save to string
|
||||
vertexSource = vsSS.str();
|
||||
pixelSource = psSS.str();
|
||||
|
||||
// extract source
|
||||
ResourceManager::ExtractShaderSource(vertexSource, delim);
|
||||
ResourceManager::ExtractShaderSource(pixelSource, delim);
|
||||
|
||||
// create shader
|
||||
m_Shaders[name] = std::shared_ptr<Shader>(Shader::Create(vertexSource, pixelSource, m_SharedContext));
|
||||
}
|
||||
|
||||
void ResourceManager::ExtractShaderSource(std::string& src, const std::string& delim)
|
||||
{
|
||||
size_t begDelimPos, endDelimPos;
|
||||
|
||||
// find begin and end delimiter (eg. +GLSL ... -GLSL )
|
||||
begDelimPos = src.find('+' + delim) + 5;
|
||||
endDelimPos = src.find('-' + delim);
|
||||
|
||||
// check
|
||||
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);
|
||||
|
||||
// extract the shader
|
||||
src = src.substr(begDelimPos, endDelimPos - begDelimPos);
|
||||
}
|
||||
|
||||
}
|
40
Engine/src/Engine/Utility/ResourceManager.h
Normal file
40
Engine/src/Engine/Utility/ResourceManager.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include "Base.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class Shader;
|
||||
class SharedContext;
|
||||
|
||||
class ResourceManager
|
||||
{
|
||||
private:
|
||||
std::unordered_map<std::string, std::shared_ptr<Shader>> m_Shaders;
|
||||
|
||||
std::shared_ptr<SharedContext> m_SharedContext;
|
||||
|
||||
static ResourceManager* s_Context;
|
||||
|
||||
public:
|
||||
static ResourceManager* Create(std::shared_ptr<SharedContext> sharedContext);
|
||||
|
||||
// #todo: add geometry shader support
|
||||
static inline void CreateShader(const std::string& name, const std::string& vertexSource, const std::string& pixelSource) { s_Context->CreateShaderImpl(name, vertexSource, pixelSource); }
|
||||
static inline void LoadShader(const std::string& name, const std::string& vertexPath, const std::string& pixelPath) { s_Context->LoadShaderImpl(name, vertexPath, pixelPath); }
|
||||
|
||||
static inline std::shared_ptr<Shader> GetShader(const std::string& name) { return s_Context->m_Shaders[name]; }
|
||||
|
||||
private:
|
||||
ResourceManager(std::shared_ptr<SharedContext> sharedContext);
|
||||
|
||||
void CreateShaderImpl(const std::string& name, const std::string& vertexSource, const std::string& pixelSource);
|
||||
void LoadShaderImpl(const std::string& name, const std::string& vertexPath, const std::string& pixelPath);
|
||||
|
||||
private:
|
||||
void ExtractShaderSource(std::string& src, const std::string& delim);
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue