Stripped back old shader code for now.

This commit is contained in:
YourWishes committed 2024-12-24 12:27:55 -06:00
1 parent 2b36a12335
commit e10aea20a1
81 files changed
+321 -896

No files matched your search

File renamed without changes.
File renamed without changes.
+74
View File
@@ -0,0 +1,74 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assertgl.hpp"
#include "assert/assert.hpp"
#include "ShaderStage.hpp"
using namespace Dawn;
ShaderStage::ShaderStage(
const enum ShaderStageType type,
const std::string source
) : IShaderStage(type) {
// Get OpenGL Shader Type
GLenum shaderType;
switch(this->type) {
case ShaderStageType::VERTEX:
shaderType = GL_VERTEX_SHADER;
break;
case ShaderStageType::FRAGMENT:
shaderType = GL_FRAGMENT_SHADER;
break;
// case ShaderStageType::COMPUTE:
// shaderType = GL_COMPUTE;
// break;
default:
assertUnreachable("Unknown ShaderStageType");
}
// Initialize the shader
this->id = glCreateShader(shaderType);
assertNoGLError();
// Compile the shader
auto cSource = source.c_str();
glShaderSource(this->id, 1, &cSource, NULL);
assertNoGLError();
glCompileShader(this->id);
assertNoGLError();
// glShaderBinary(1, &this->id, GL_SHADER_BINARY_FORMAT_SPIR_V, source.data(), source.size());
// assertNoGLError();
// glSpecializeShader(this->id, "main", 0, NULL, NULL);
// assertNoGLError();
// Validate
GLint status;
glGetShaderiv(this->id, GL_COMPILE_STATUS, &status);
assertNoGLError();
if(!status) {
// Failed to compile
GLint logLength;
glGetShaderiv(this->id, GL_INFO_LOG_LENGTH, &logLength);
assertNoGLError();
GLchar *log = new GLchar[logLength];
glGetShaderInfoLog(this->id, logLength, NULL, log);
assertNoGLError();
assertUnreachable("Failed to compile shader stage %i:\n%s", type, log);
}
}
ShaderStage::~ShaderStage() {
if(this->id != -1) {
glDeleteShader(this->id);
assertNoGLError();
}
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/shader/IShaderStage.hpp"
namespace Dawn {
class ShaderStage : public IShaderStage {
public:
GLuint id = -1;
/**
* Constructs a new ShaderStage.
*
* @param type The type of shader this is.
* @param source The source code to compile.
*/
ShaderStage(const enum ShaderStageType type, const std::string source);
/**
* Disposes of the shader stage.
*/
~ShaderStage();
};
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
-1
View File
@@ -33,7 +33,6 @@ add_subdirectory(scene)
add_subdirectory(settings) add_subdirectory(settings)
add_subdirectory(time) add_subdirectory(time)
add_subdirectory(util) add_subdirectory(util)
add_subdirectory(ui)
# Assets # Assets
+7 -7
View File
@@ -18,7 +18,7 @@ ShaderLoader::ShaderLoader(
) : ) :
AssetLoader(assetManager, name), AssetLoader(assetManager, name),
state(ShaderLoaderState::INITIAL), state(ShaderLoaderState::INITIAL),
shader(std::make_shared<ShaderProgram2>()) shader(std::make_shared<ShaderProgram>())
{ {
} }
@@ -76,7 +76,7 @@ void ShaderLoader::updateSync() {
// Create the shader program. // Create the shader program.
Slang::ComPtr<IBlob> blob; Slang::ComPtr<IBlob> blob;
slang::ProgramLayout* layout = program->getLayout(); slang::ProgramLayout* layout = program->getLayout();
std::vector<std::shared_ptr<ShaderEntry>> shaderEntries; std::vector<std::shared_ptr<ShaderStage>> shaderStages;
for(auto i = 0; i < definedEntryPointCount; i++) { for(auto i = 0; i < definedEntryPointCount; i++) {
// Get the code // Get the code
auto result = linkedProgram->getEntryPointCode( auto result = linkedProgram->getEntryPointCode(
@@ -95,18 +95,18 @@ void ShaderLoader::updateSync() {
auto stage = entryPointReflection->getStage(); auto stage = entryPointReflection->getStage();
// Create the shader entry // Create the shader entry
auto shaderEntry = std::make_shared<ShaderEntry>(); auto shaderStage = std::make_shared<ShaderStage>();
shaderEntry->init( shaderStage->init(
stage, stage,
std::string((const char*)blob->getBufferPointer()) std::string((const char*)blob->getBufferPointer())
); );
// Add to the list // Add to the list
shaderEntries.push_back(shaderEntry); shaderStages.push_back(shaderStage);
} }
// Create the shader program. // Create the shader program.
shader->init(shaderEntries); shader->init(shaderStages);
// Finished loading. // Finished loading.
delete [] components; delete [] components;
@@ -118,7 +118,7 @@ std::string ShaderLoader::getAssetType() const {
return ShaderLoader::ASSET_TYPE; return ShaderLoader::ASSET_TYPE;
} }
std::shared_ptr<IShaderProgram2> ShaderLoader::getShader() { std::shared_ptr<ShaderProgram> ShaderLoader::getShader() {
assertNotNull(shader, "ShaderLoader shader is null."); assertNotNull(shader, "ShaderLoader shader is null.");
return shader; return shader;
} }
+4 -4
View File
@@ -7,7 +7,7 @@
#include "asset/AssetLoader.hpp" #include "asset/AssetLoader.hpp"
#include "asset/AssetDataLoader.hpp" #include "asset/AssetDataLoader.hpp"
#include "display/shader/ShaderManager.hpp" #include "display/shader/ShaderManager.hpp"
#include "display/shader/ShaderProgram2.hpp" #include "display/shader/ShaderProgram.hpp"
namespace Dawn { namespace Dawn {
enum class ShaderLoaderState { enum class ShaderLoaderState {
@@ -19,7 +19,7 @@ namespace Dawn {
class ShaderLoader : public AssetLoader { class ShaderLoader : public AssetLoader {
protected: protected:
enum ShaderLoaderState state; enum ShaderLoaderState state;
std::shared_ptr<ShaderProgram2> shader; std::shared_ptr<ShaderProgram> shader;
public: public:
const static std::string ASSET_TYPE; const static std::string ASSET_TYPE;
@@ -40,9 +40,9 @@ namespace Dawn {
/** /**
* Retreives the shader program for this loader. * Retreives the shader program for this loader.
* *
* @return std::shared_ptr<IShaderProgram2> * @return The shader program that this loader is managing.
*/ */
std::shared_ptr<IShaderProgram2> getShader(); std::shared_ptr<ShaderProgram> getShader();
~ShaderLoader(); ~ShaderLoader();
}; };
-1
View File
@@ -10,4 +10,3 @@ target_sources(${DAWN_TARGET_NAME}
# Subdirs # Subdirs
add_subdirectory(display) add_subdirectory(display)
add_subdirectory(ui)
@@ -17,7 +17,7 @@ namespace Dawn {
* @param ctx Context for the render pass. * @param ctx Context for the render pass.
* @return List of render passes. * @return List of render passes.
*/ */
virtual std::vector<std::shared_ptr<IRenderPass>> getPasses( virtual std::vector<std::shared_ptr<RenderPass>> getPasses(
struct RenderPassContext &ctx struct RenderPassContext &ctx
) = 0; ) = 0;
}; };
@@ -31,26 +31,26 @@ namespace Dawn {
* @param data Data to use for the render pass. * @param data Data to use for the render pass.
* @return Created render pass. * @return Created render pass.
*/ */
template<class S, typename D> // template<class S, typename D>
std::shared_ptr<IRenderPass> createRenderPass( // std::shared_ptr<IRenderPass> createRenderPass(
SceneComponent &self, // SceneComponent &self,
const D data, // const D data,
const std::unordered_map< // const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture> // shadertexturebinding_t, std::shared_ptr<Texture>
> textures = {}, // > textures = {},
const std::shared_ptr<Mesh> mesh = nullptr, // const std::shared_ptr<Mesh> mesh = nullptr,
const enum MeshDrawMode drawMode = MeshDrawMode::TRIANGLES, // const enum MeshDrawMode drawMode = MeshDrawMode::TRIANGLES,
int32_t indiceStart = 0, // int32_t indiceStart = 0,
int32_t indiceCount = -1 // int32_t indiceCount = -1
) { // ) {
return std::make_shared<RenderPass<S,D>>( // return std::make_shared<RenderPass<S,D>>(
self, // self,
data, // data,
textures, // textures,
mesh, // mesh,
drawMode, // drawMode,
indiceStart, // indiceStart,
indiceCount // indiceCount
); // );
} // }
} }
@@ -9,11 +9,7 @@
using namespace Dawn; using namespace Dawn;
void Material::onInit() { void Material::onInit() {
this->lockedShaders = this->getLockedShaders(
getGame()->renderHost->shaderManager
);
} }
void Material::onDispose() { void Material::onDispose() {
this->lockedShaders.clear();
} }
@@ -13,18 +13,8 @@ namespace Dawn {
public IRenderableComponent public IRenderableComponent
{ {
private: private:
std::vector<std::shared_ptr<IShaderBase>> lockedShaders;
protected: protected:
/**
* Locks the shaders to be used for rendering.
*
* @param shaderManager Shader manager to use.
* @return List of shaders to be used.
*/
virtual std::vector<std::shared_ptr<IShaderBase>> getLockedShaders(
ShaderManager &shaderManager
) = 0;
public: public:
void onInit() override; void onInit() override;
@@ -9,14 +9,6 @@
using namespace Dawn; using namespace Dawn;
std::vector<
std::shared_ptr<IShaderBase>
> SimpleTexturedMaterial::getLockedShaders(ShaderManager &shaderManager) {
return {
shaderManager.getShader<SimpleTexturedShader>()
};
}
void SimpleTexturedMaterial::load(std::shared_ptr<SceneLoadContext> ctx) { void SimpleTexturedMaterial::load(std::shared_ptr<SceneLoadContext> ctx) {
if(ctx->data.contains("color")) { if(ctx->data.contains("color")) {
this->setColor(JSON::color(ctx->data["color"])); this->setColor(JSON::color(ctx->data["color"]));
@@ -31,7 +23,7 @@ void SimpleTexturedMaterial::load(std::shared_ptr<SceneLoadContext> ctx) {
} }
struct Color SimpleTexturedMaterial::getColor() { struct Color SimpleTexturedMaterial::getColor() {
return this->data.data.color; return this->data.color;
} }
std::shared_ptr<Texture> SimpleTexturedMaterial::getTexture() { std::shared_ptr<Texture> SimpleTexturedMaterial::getTexture() {
@@ -45,32 +37,32 @@ void SimpleTexturedMaterial::setTexture(
} }
void SimpleTexturedMaterial::setColor(const struct Color color) { void SimpleTexturedMaterial::setColor(const struct Color color) {
this->data.data.color = color; this->data.color = color;
} }
std::vector<std::shared_ptr<IRenderPass>> SimpleTexturedMaterial::getPasses( std::vector<std::shared_ptr<RenderPass>> SimpleTexturedMaterial::getPasses(
struct RenderPassContext &ctx struct RenderPassContext &ctx
) { ) {
this->data.data.model = this->getItem()->getWorldTransform(); // this->data.model = this->getItem()->getWorldTransform();
this->data.data.projection = ctx.camera->getProjection(); // this->data.projection = ctx.camera->getProjection();
this->data.data.view = ctx.camera->getItem()->getWorldTransform(); // this->data.view = ctx.camera->getItem()->getWorldTransform();
auto textures = std::unordered_map< // auto textures = std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture> // shadertexturebinding_t, std::shared_ptr<Texture>
>(); // >();
if(this->texture) { // if(this->texture) {
this->data.data.hasTexture = true; // this->data.hasTexture = true;
this->data.texture = 0; // this->data.texture = 0;
textures[this->data.texture] = this->texture; // textures[this->data.texture] = this->texture;
} else { // } else {
this->data.data.hasTexture = false; // this->data.data.hasTexture = false;
} // }
return { return {
createRenderPass<SimpleTexturedShader, struct SimpleTexturedShaderData>( // createRenderPass<SimpleTexturedShader, struct SimpleTexturedShaderData>(
*this, // *this,
data, // data,
textures // textures
) // )
}; };
} }
@@ -5,19 +5,24 @@
#pragma once #pragma once
#include "component/display/material/Material.hpp" #include "component/display/material/Material.hpp"
#include "display/shader/SimpleTexturedShader.hpp"
#include "display/Texture.hpp" #include "display/Texture.hpp"
namespace Dawn { namespace Dawn {
struct SimpleTexturedMaterialShaderData {
int32_t t;
struct Color color;
glm::mat4 model;
glm::mat4 projection;
glm::mat4 view;
bool hasTexture;
};
class SimpleTexturedMaterial : public Material { class SimpleTexturedMaterial : public Material {
private: private:
struct SimpleTexturedShaderData data; struct SimpleTexturedMaterialShaderData data;
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
protected: protected:
std::vector<std::shared_ptr<IShaderBase>> getLockedShaders(
ShaderManager &shaderManager
) override;
public: public:
void load(std::shared_ptr<SceneLoadContext> ctx) override; void load(std::shared_ptr<SceneLoadContext> ctx) override;
@@ -48,7 +53,7 @@ namespace Dawn {
*/ */
void setColor(const struct Color color); void setColor(const struct Color color);
std::vector<std::shared_ptr<IRenderPass>> getPasses( std::vector<std::shared_ptr<RenderPass>> getPasses(
struct RenderPassContext &ctx struct RenderPassContext &ctx
) override; ) override;
}; };
+1
View File
@@ -16,3 +16,4 @@ add_subdirectory(mesh)
add_subdirectory(shader) add_subdirectory(shader)
add_subdirectory(font) add_subdirectory(font)
add_subdirectory(tileset) add_subdirectory(tileset)
add_subdirectory(pass)
-2
View File
@@ -7,7 +7,6 @@
#include "dawn.hpp" #include "dawn.hpp"
#include "display/RenderTarget.hpp" #include "display/RenderTarget.hpp"
#include "display/RenderPipeline.hpp" #include "display/RenderPipeline.hpp"
#include "display/shader/ShaderManager.hpp"
namespace Dawn { namespace Dawn {
class Game; class Game;
@@ -15,7 +14,6 @@ namespace Dawn {
class IRenderHost { class IRenderHost {
public: public:
RenderPipeline renderPipeline; RenderPipeline renderPipeline;
ShaderManager shaderManager;
/** /**
* Creates a render host. * Creates a render host.
+1 -4
View File
@@ -78,7 +78,7 @@ void RenderPipeline::renderSceneCamera(
}; };
// Get list of renderables // Get list of renderables
std::vector<std::shared_ptr<IRenderPass>> renderPasses; std::vector<std::shared_ptr<RenderPass>> renderPasses;
auto renderables = scene->findComponents<IRenderableComponent>(); auto renderables = scene->findComponents<IRenderableComponent>();
for(auto renderable : renderables) { for(auto renderable : renderables) {
auto rp = renderable->getPasses(ctx); auto rp = renderable->getPasses(ctx);
@@ -95,9 +95,6 @@ void RenderPipeline::renderSceneCamera(
); );
for(auto renderPass : renderPasses) { for(auto renderPass : renderPasses) {
renderPass->bind();
renderPass->setData();
renderPass->upload();
renderPass->draw(); renderPass->draw();
} }
} }
@@ -5,5 +5,5 @@
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
MapShader.cpp RenderPass.cpp
) )
-40
View File
@@ -1,40 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class IRenderPass {
public:
std::shared_ptr<Mesh> mesh;
/**
* Binds the shader for this render pass.
*/
virtual void bind() = 0;
/**
* Sets the data for this render pass to the shader.
*/
virtual void setData() = 0;
/**
* Uploads the data to the GPU.
*/
virtual void upload() = 0;
/**
* Draws the mesh for this render pass.
*/
virtual void draw() = 0;
/**
* Cleans up the render pass.
*/
virtual ~IRenderPass() {
}
};
}
+32
View File
@@ -0,0 +1,32 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "RenderPass.hpp"
#include "component/display/MeshRenderer.hpp"
using namespace Dawn;
RenderPass::RenderPass(
SceneComponent &self,
const std::shared_ptr<Mesh> mesh,
const enum MeshDrawMode drawMode,
const int32_t indiceStart,
const int32_t indiceCount
) :
mesh(mesh),
drawMode(drawMode),
indiceStart(indiceStart),
indiceCount(indiceCount)
{
// Need mesh?
if(!this->mesh) {
auto meshRenderer = self.getItem()->getComponent<MeshRenderer>();
if(meshRenderer) this->mesh = meshRenderer->mesh;
}
}
void RenderPass::draw() {
mesh->draw(drawMode, indiceStart, indiceCount);
}
+11 -58
View File
@@ -4,25 +4,16 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "game/Game.hpp" #include "display/mesh/Mesh.hpp"
#include "display/pass/IRenderPass.hpp" #include "scene/SceneComponent.hpp"
#include "display/shader/Shader.hpp"
#include "display/Texture.hpp"
#include "component/display/MeshRenderer.hpp"
namespace Dawn { namespace Dawn {
template<class S, typename D> class RenderPass {
class RenderPass : public IRenderPass {
private: private:
std::shared_ptr<S> shader;
const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
> textures;
std::shared_ptr<Mesh> mesh; std::shared_ptr<Mesh> mesh;
const enum MeshDrawMode drawMode; const enum MeshDrawMode drawMode;
const int32_t indiceStart; const int32_t indiceStart;
const int32_t indiceCount; const int32_t indiceCount;
const D data;
public: public:
/** /**
@@ -37,58 +28,20 @@ namespace Dawn {
*/ */
RenderPass( RenderPass(
SceneComponent &self, SceneComponent &self,
const D d,
const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
> textures,
const std::shared_ptr<Mesh> mesh, const std::shared_ptr<Mesh> mesh,
const enum MeshDrawMode drawMode, const enum MeshDrawMode drawMode,
const int32_t indiceStart, const int32_t indiceStart,
const int32_t indiceCount const int32_t indiceCount
) :
data(d),
textures(textures),
mesh(mesh),
drawMode(drawMode),
indiceStart(indiceStart),
indiceCount(indiceCount)
{
//Get the shader
shader = (
self.getGame()->renderHost->shaderManager.getShader<S>()
); );
assertNotNull(shader, "Shader cannot be null!");
// Need mesh? /**
if(!this->mesh) { * Draws the mesh for this render pass.
auto meshRenderer = self.getItem()->getComponent<MeshRenderer>(); */
if(meshRenderer) this->mesh = meshRenderer->mesh; void draw();
}
}
void bind() override { /**
shader->bind(); * Cleans up the render pass.
} */
~RenderPass();
void setData() override {
shader->setData(data);
}
void upload() override {
for(auto &pair : textures) {
if(!pair.second->isReady()) continue;
pair.second->bind(pair.first);
}
shader->upload();
}
void draw() override {
if(mesh) {
mesh->draw(drawMode, indiceStart, indiceCount);
}
}
~RenderPass() override {
}
}; };
} }
+2 -2
View File
@@ -6,9 +6,9 @@
# Sources # Sources
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
IShader.cpp IShaderProgram.cpp
IShaderStage.cpp IShaderStage.cpp
ShaderManager.cpp ShaderManager.cpp
ShaderManagerSlangFileSystem.cpp ShaderManagerSlangFileSystem.cpp
IShaderProgram2.cpp IShaderProgram.cpp
) )
-46
View File
@@ -1,46 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/Shader.hpp"
#include "assert/assert.hpp"
#include "display/Color.hpp"
#include "display/Texture.hpp"
using namespace Dawn;
size_t shaderParameterTypeGetSize(const ShaderParameterType type) {
switch(type) {
case ShaderParameterType::VEC2:
return sizeof(glm::vec2);
case ShaderParameterType::VEC3:
return sizeof(glm::vec3);
case ShaderParameterType::VEC4:
return sizeof(glm::vec4);
case ShaderParameterType::MAT3:
return sizeof(glm::mat3);
case ShaderParameterType::MAT4:
return sizeof(glm::mat4);
case ShaderParameterType::COLOR:
return sizeof(struct Color);
case ShaderParameterType::FLOAT:
return sizeof(float);
case ShaderParameterType::INT:
return sizeof(int32_t);
case ShaderParameterType::TEXTURE:
return sizeof(shadertexturebinding_t);
default:
assertUnreachable("Unknown ShaderParameterType");
return 0;
}
}
-86
View File
@@ -1,86 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawn.hpp"
namespace Dawn {
enum class ShaderParameterType {
VEC2,
VEC3,
VEC4,
MAT3,
MAT4,
COLOR,
FLOAT,
INT,
TEXTURE,
BOOLEAN
};
class IShaderBase {
public:
virtual ~IShaderBase() {
}
};
template<typename T>
class IShader : public IShaderBase {
protected:
T data;
public:
/**
* Returns the currently uploaded data on the Shader.
*
* @return The uploaded data.
*/
T getData() {
return data;
}
/**
* Sets the entire data to be uploaded.
*
* @param data Data to be uploaded.
*/
void setData(const T data) {
this->data = data;
}
/**
* Initializes the shader, this needs to be called before the shader can
* be used.
*/
virtual void init() = 0;
/**
* Binds the shader as the current one, does not upload any data, somewhat
* relies on something else uploading the data.
*/
virtual void bind() = 0;
/**
* Uploads the data to the GPU.
*/
virtual void upload() = 0;
/**
* Disposes of the shader.
*/
virtual ~IShader() {
}
};
}
/**
* Returns the size of the ShaderParameterType.
*
* @param type The type to get the size of.
* @return Size of the type.
*/
size_t shaderParameterTypeGetSize(const Dawn::ShaderParameterType type);
@@ -0,0 +1,17 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "IShaderProgram.hpp"
using namespace Dawn;
void IShaderProgram::init(
const std::vector<std::shared_ptr<ShaderStage>> &stages
) {
this->stages = stages;
}
IShaderProgram::~IShaderProgram() {
}
@@ -0,0 +1,30 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/ShaderStage.hpp"
namespace Dawn {
class IShaderProgram {
protected:
std::vector<std::shared_ptr<ShaderStage>> stages;
public:
/**
* Initialize the IShaderProgram2 object
*
* @param stages The list of shader stages to initialize with.
*/
virtual void init(
const std::vector<std::shared_ptr<ShaderStage>> &stages
) = 0;
/**
* Destroy the IShaderProgram2 object
*/
virtual ~IShaderProgram();
};
}
@@ -1,21 +0,0 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "IShaderProgram2.hpp"
using namespace Dawn;
IShaderEntry::~IShaderEntry() {
}
void IShaderProgram2::init(
const std::vector<std::shared_ptr<ShaderEntry>> &entries
) {
this->entries = entries;
}
IShaderProgram2::~IShaderProgram2() {
}
@@ -1,53 +0,0 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawn.hpp"
#include "slang.h"
using namespace slang;
namespace Dawn {
class ShaderEntry;
class IShaderEntry {
public:
/**
* Initialize the IShaderEntry object
*
* @param stage The stage of the shader entry.
* @param code The code of the shader entry.
*/
virtual void init(
const SlangStage &stage,
const std::string &code
) = 0;
/**
* Destroy the IShaderEntry object
*/
virtual ~IShaderEntry();
};
class IShaderProgram2 {
protected:
std::vector<std::shared_ptr<ShaderEntry>> entries;
public:
/**
* Initialize the IShaderProgram2 object
*
* @param entries The list of shader entries to initialize with.
*/
virtual void init(
const std::vector<std::shared_ptr<ShaderEntry>> &entries
) = 0;
/**
* Destroy the IShaderProgram2 object
*/
virtual ~IShaderProgram2();
};
}
+1 -8
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Dominic Masters // Copyright (c) 2024 Dominic Masters
// //
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
@@ -7,12 +7,5 @@
using namespace Dawn; using namespace Dawn;
IShaderStage::IShaderStage(const ShaderStageType type) :
type(type)
{
}
IShaderStage::~IShaderStage() { IShaderStage::~IShaderStage() {
} }
+12 -13
View File
@@ -1,31 +1,30 @@
// Copyright (c) 2023 Dominic Masters // Copyright (c) 2024 Dominic Masters
// //
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "dawn.hpp" #include "dawn.hpp"
#include "slang.h"
using namespace slang;
namespace Dawn { namespace Dawn {
enum class ShaderStageType {
VERTEX,
FRAGMENT,
// COMPUTE
};
class IShaderStage { class IShaderStage {
public: public:
const enum ShaderStageType type;
/** /**
* Constructs a new Shader Stage. * Initialize the IShaderEntry object
* *
* @param type Type of shader stage. * @param stage The stage of the shader entry.
* @param code The code of the shader entry.
*/ */
IShaderStage(const ShaderStageType type); virtual void init(
const SlangStage &stage,
const std::string &code
) = 0;
/** /**
* Destroy the IShaderStage object * Destroy the IShaderEntry object
*/ */
virtual ~IShaderStage(); virtual ~IShaderStage();
}; };
@@ -47,6 +47,4 @@ std::shared_ptr<Game> ShaderManager::getGame() {
} }
ShaderManager::~ShaderManager() { ShaderManager::~ShaderManager() {
// Clear all shaders
shaders.clear();
} }
-32
View File
@@ -4,7 +4,6 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "display/shader/Shader.hpp"
#include "ShaderManagerSlangFileSystem.hpp" #include "ShaderManagerSlangFileSystem.hpp"
using namespace slang; using namespace slang;
@@ -14,11 +13,8 @@ namespace Dawn {
class ShaderManager : public std::enable_shared_from_this<ShaderManager> { class ShaderManager : public std::enable_shared_from_this<ShaderManager> {
private: private:
std::vector<std::weak_ptr<IShaderBase>> shaders;
std::weak_ptr<Game> game; std::weak_ptr<Game> game;
ShaderManagerSlangFileSystem fileSystem; ShaderManagerSlangFileSystem fileSystem;
Slang::ComPtr<IGlobalSession> globalSession; Slang::ComPtr<IGlobalSession> globalSession;
TargetDesc targetDescription; TargetDesc targetDescription;
SessionDesc sessionDescription; SessionDesc sessionDescription;
@@ -44,34 +40,6 @@ namespace Dawn {
*/ */
std::shared_ptr<Game> getGame(); std::shared_ptr<Game> getGame();
/**
* Retreives an instance of the shader from the shader manager. If the
* shader does not exist it will be created.
*
* @tparam T Type of shader to retreive.
* @return Shader instance.
*/
template<class T>
std::shared_ptr<T> getShader() {
auto itShaders = shaders.begin();
while(itShaders != shaders.end()) {
auto shader = itShaders->lock();
if(!shader) {
itShaders = shaders.erase(itShaders);
continue;
}
std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(shader);
if(casted) return casted;
itShaders++;
}
auto newShader = std::make_shared<T>();
shaders.push_back(newShader);
newShader->init();
return newShader;
}
/** /**
* Disposes of all shaders. * Disposes of all shaders.
*/ */
+1 -5
View File
@@ -6,10 +6,6 @@
# Sources # Sources
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
Shader.cpp ShaderProgram.cpp
ShaderStage.cpp ShaderStage.cpp
SimpleTexturedShader.cpp
UIShader.cpp
ShaderParameter.cpp
ShaderProgram2.cpp
) )
@@ -0,0 +1,17 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ShaderProgram.hpp"
using namespace Dawn;
void ShaderProgram::init(
const std::vector<std::shared_ptr<ShaderStage>> &stages
) {
IShaderProgram::init(stages);
}
ShaderProgram::~ShaderProgram() {
}
@@ -0,0 +1,17 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/IShaderProgram.hpp"
namespace Dawn {
class ShaderProgram : public IShaderProgram {
public:
void init(
const std::vector<std::shared_ptr<ShaderStage>> &stages
) override;
~ShaderProgram();
};
}
@@ -1,28 +0,0 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ShaderProgram2.hpp"
using namespace Dawn;
void ShaderEntry::init(
const SlangStage &stage,
const std::string &code
) {
}
ShaderEntry::~ShaderEntry() {
}
// // //
void ShaderProgram2::init(
const std::vector<std::shared_ptr<ShaderEntry>> &entries
) {
IShaderProgram2::init(entries);
}
ShaderProgram2::~ShaderProgram2() {
}
@@ -1,28 +0,0 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/IShaderProgram2.hpp"
namespace Dawn {
class ShaderEntry {
protected:
public:
void init(
const SlangStage &stage,
const std::string &code
);
~ShaderEntry();
};
class ShaderProgram2 : public IShaderProgram2 {
public:
void init(
const std::vector<std::shared_ptr<ShaderEntry>> &entries
) override;
~ShaderProgram2();
};
}
+5 -62
View File
@@ -1,74 +1,17 @@
// Copyright (c) 2023 Dominic Masters // Copyright (c) 2024 Dominic Masters
// //
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "assert/assertgl.hpp"
#include "assert/assert.hpp"
#include "ShaderStage.hpp" #include "ShaderStage.hpp"
using namespace Dawn; using namespace Dawn;
ShaderStage::ShaderStage( void ShaderStage::init(
const enum ShaderStageType type, const SlangStage &stage,
const std::string source const std::string &code
) : IShaderStage(type) { ) {
// Get OpenGL Shader Type
GLenum shaderType;
switch(this->type) {
case ShaderStageType::VERTEX:
shaderType = GL_VERTEX_SHADER;
break;
case ShaderStageType::FRAGMENT:
shaderType = GL_FRAGMENT_SHADER;
break;
// case ShaderStageType::COMPUTE:
// shaderType = GL_COMPUTE;
// break;
default:
assertUnreachable("Unknown ShaderStageType");
}
// Initialize the shader
this->id = glCreateShader(shaderType);
assertNoGLError();
// Compile the shader
auto cSource = source.c_str();
glShaderSource(this->id, 1, &cSource, NULL);
assertNoGLError();
glCompileShader(this->id);
assertNoGLError();
// glShaderBinary(1, &this->id, GL_SHADER_BINARY_FORMAT_SPIR_V, source.data(), source.size());
// assertNoGLError();
// glSpecializeShader(this->id, "main", 0, NULL, NULL);
// assertNoGLError();
// Validate
GLint status;
glGetShaderiv(this->id, GL_COMPILE_STATUS, &status);
assertNoGLError();
if(!status) {
// Failed to compile
GLint logLength;
glGetShaderiv(this->id, GL_INFO_LOG_LENGTH, &logLength);
assertNoGLError();
GLchar *log = new GLchar[logLength];
glGetShaderInfoLog(this->id, logLength, NULL, log);
assertNoGLError();
assertUnreachable("Failed to compile shader stage %i:\n%s", type, log);
}
} }
ShaderStage::~ShaderStage() { ShaderStage::~ShaderStage() {
if(this->id != -1) {
glDeleteShader(this->id);
assertNoGLError();
}
} }
+5 -15
View File
@@ -1,28 +1,18 @@
// Copyright (c) 2023 Dominic Masters // Copyright (c) 2024 Dominic Masters
// //
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "dawnopengl.hpp"
#include "display/shader/IShaderStage.hpp" #include "display/shader/IShaderStage.hpp"
namespace Dawn { namespace Dawn {
class ShaderStage : public IShaderStage { class ShaderStage : public IShaderStage {
public: public:
GLuint id = -1; void init(
const SlangStage &stage,
/** const std::string &code
* Constructs a new ShaderStage. ) override;
*
* @param type The type of shader this is.
* @param source The source code to compile.
*/
ShaderStage(const enum ShaderStageType type, const std::string source);
/**
* Disposes of the shader stage.
*/
~ShaderStage(); ~ShaderStage();
}; };
} }
-1
View File
@@ -11,7 +11,6 @@ target_include_directories(${DAWN_TARGET_NAME}
# Subdirs # Subdirs
add_subdirectory(component) add_subdirectory(component)
add_subdirectory(display)
add_subdirectory(game) add_subdirectory(game)
add_subdirectory(rpg) add_subdirectory(rpg)
-1
View File
@@ -11,4 +11,3 @@ target_sources(${DAWN_TARGET_NAME}
) )
# Subdirs # Subdirs
add_subdirectory(display)
@@ -1,7 +0,0 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(material)
@@ -1,9 +0,0 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DAWN_TARGET_NAME}
PRIVATE
MapMaterial.cpp
)
@@ -1,76 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "MapMaterial.hpp"
#include "util/JSON.hpp"
#include "asset/loader/TextureLoader.hpp"
using namespace Dawn;
std::vector<
std::shared_ptr<IShaderBase>
> MapMaterial::getLockedShaders(ShaderManager &shaderManager) {
return {
shaderManager.getShader<MapShader>()
};
}
void MapMaterial::load(std::shared_ptr<SceneLoadContext> ctx) {
if(ctx->data.contains("color")) {
this->setColor(JSON::color(ctx->data["color"]));
}
if(ctx->data.contains("texture")) {
auto asset = ctx->getAsset<TextureLoader>(
ctx->data["texture"].get<std::string>()
);
this->setTexture(asset->getTexture());
}
}
struct Color MapMaterial::getColor() {
return this->data.color;
}
std::shared_ptr<Texture> MapMaterial::getTexture() {
return this->texture;
}
void MapMaterial::setTexture(
const std::shared_ptr<Texture> texture
) {
this->texture = texture;
}
void MapMaterial::setColor(const struct Color color) {
this->data.color = color;
}
std::vector<std::shared_ptr<IRenderPass>> MapMaterial::getPasses(
struct RenderPassContext &ctx
) {
this->data.model = this->getItem()->getWorldTransform();
this->data.projection = ctx.camera->getProjection();
this->data.view = ctx.camera->getItem()->getWorldTransform();
auto textures = std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
>();
if(this->texture) {
this->data.hasTexture = true;
this->data.texture = 0;
textures[this->data.texture] = this->texture;
} else {
this->data.hasTexture = false;
}
return {
createRenderPass<MapShader, struct MapShaderData>(
*this,
data,
textures
)
};
}
@@ -1,55 +0,0 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "component/display/material/Material.hpp"
#include "display/shader/MapShader.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class MapMaterial : public Material {
protected:
std::vector<std::shared_ptr<IShaderBase>> getLockedShaders(
ShaderManager &shaderManager
) override;
private:
struct MapShaderData data;
std::shared_ptr<Texture> texture;
public:
void load(std::shared_ptr<SceneLoadContext> ctx) override;
/**
* Returns the color of this material.
*/
struct Color getColor();
/**
* Returns the texture of this material.
*
* @return The texture of this material.
*/
std::shared_ptr<Texture> getTexture();
/**
* Sets the texture of this material.
*
* @param texture The texture to set.
*/
void setTexture(const std::shared_ptr<Texture> texture);
/**
* Sets the color of this material.
*
* @param color The color to set.
*/
void setColor(const struct Color color);
std::vector<std::shared_ptr<IRenderPass>> getPasses(
struct RenderPassContext &ctx
) override;
};
}
-7
View File
@@ -1,7 +0,0 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(shader)
-106
View File
@@ -1,106 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/MapShader.hpp"
using namespace Dawn;
void MapShader::getStages(
const enum ShaderOpenGLVariant variant,
const struct MapShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderParameter> &parameters,
std::vector<struct IShaderStructure> &structures
) {
// Stages
std::shared_ptr<ShaderStage> vertex;
std::shared_ptr<ShaderStage> fragment;
std::cout << "MapShader::getStages" << std::endl;
switch(variant) {
case ShaderOpenGLVariant::GLSL_330_CORE:
vertex = std::make_shared<ShaderStage>(
ShaderStageType::VERTEX,R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
uniform mat4 u_Projection;
uniform mat4 u_View;
uniform mat4 u_Model;
out vec2 o_TextCoord;
void main() {
gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);
o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);
}
)"
);
fragment = std::make_shared<ShaderStage>(
ShaderStageType::FRAGMENT,R"(
#version 330 core
in vec2 o_TextCoord;
out vec4 o_Color;
uniform vec4 u_Color;
uniform bool u_HasTexture;
uniform sampler2D u_Texture;
void main() {
if(u_HasTexture) {
o_Color = texture(u_Texture, o_TextCoord);
} else {
o_Color = u_Color;
}
if(o_Color.a == 0) discard;
}
)"
);
break;
default:
assertUnreachable("Unsupported ShaderOpenGLVariant");
}
// Add stages
stages.push_back(vertex);
stages.push_back(fragment);
// Parameters
parameters.push_back(ShaderParameter(
"u_Projection",
&rel->projection,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_View",
&rel->view,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Model",
&rel->model,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Color",
&rel->color,
ShaderParameterType::COLOR
));
parameters.push_back(ShaderParameter(
"u_HasTexture",
&rel->hasTexture,
ShaderParameterType::BOOLEAN
));
parameters.push_back(ShaderParameter(
"u_Texture",
&rel->texture,
ShaderParameterType::TEXTURE
));
}
-29
View File
@@ -1,29 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/Shader.hpp"
namespace Dawn {
struct MapShaderData {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model;
struct Color color = COLOR_WHITE;
bool hasTexture = false;
shadertexturebinding_t texture = 0;
};
class MapShader : public Shader<MapShaderData> {
protected:
void getStages(
const enum ShaderOpenGLVariant variant,
const struct MapShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderParameter> &parameters,
std::vector<struct IShaderStructure> &structures
) override;
};
}
-14
View File
@@ -6,13 +6,10 @@
#include "Game.hpp" #include "Game.hpp"
#include "component/SceneComponentRegistry.hpp" #include "component/SceneComponentRegistry.hpp"
#include "component/display/material/MapMaterial.hpp"
#include "component/RPGEntity.hpp" #include "component/RPGEntity.hpp"
#include "component/RPGPlayer.hpp" #include "component/RPGPlayer.hpp"
#include "component/RPGMap.hpp" #include "component/RPGMap.hpp"
#include "asset/loader/ShaderLoader.hpp" #include "asset/loader/ShaderLoader.hpp"
#include <fstream>
using namespace Dawn; using namespace Dawn;
@@ -20,7 +17,6 @@ Game::Game() : IGame() {
SceneComponentRegistry::reg<RPGEntity>("RPGEntity"); SceneComponentRegistry::reg<RPGEntity>("RPGEntity");
SceneComponentRegistry::reg<RPGPlayer>("RPGPlayer"); SceneComponentRegistry::reg<RPGPlayer>("RPGPlayer");
SceneComponentRegistry::reg<RPGMap>("RPGMap"); SceneComponentRegistry::reg<RPGMap>("RPGMap");
SceneComponentRegistry::reg<MapMaterial>("MapMaterial");
} }
std::string Game::getInitialScene() { std::string Game::getInitialScene() {
@@ -33,16 +29,6 @@ void Game::initManagers() {
auto sl = assetManager->get<ShaderLoader>("shaders/hello-world.slang"); auto sl = assetManager->get<ShaderLoader>("shaders/hello-world.slang");
sl->loadImmediately(); sl->loadImmediately();
// auto code1 = sl->getEntryPointCode("vertexMain");
// std::fstream file("vertexMain.glsl", std::ios::out);
// file << code1;
// file.close();
// auto code2 = sl->getEntryPointCode("fragmentMain");
// std::fstream file2("fragmentMain.glsl", std::ios::out);
// file2 << code2;
// file2.close();
} }
Game::~Game() { Game::~Game() {