Base refactor
This commit is contained in:
@ -1,10 +0,0 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
ShaderManager.cpp
|
||||
)
|
@ -1,112 +0,0 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/Texture.hpp"
|
||||
#include "display/shader/ShaderParameterBuffer.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<typename T>
|
||||
class IShader {
|
||||
public:
|
||||
int32_t shaderId = -1;
|
||||
int_fast16_t renderId = 0;
|
||||
|
||||
/**
|
||||
* Compile all programs for this shader.
|
||||
*/
|
||||
virtual void compile() = 0;
|
||||
|
||||
/**
|
||||
* Attaches the supplied shader as the current shader.
|
||||
*/
|
||||
virtual void bind() = 0;
|
||||
|
||||
/**
|
||||
* Binds a shader buffer to a specific slot.
|
||||
*
|
||||
* @param slot Slot to bind the buffer to.
|
||||
* @param buffer Buffer to bind.
|
||||
*/
|
||||
template<typename J>
|
||||
void setParameterBuffer(
|
||||
const shaderbufferslot_t slot,
|
||||
const ShaderParameterBuffer<J> &buffer
|
||||
);
|
||||
|
||||
/**
|
||||
* Set's a specific shader parameter to a matrix.
|
||||
*
|
||||
* @param parameter parameter on the shader to set.
|
||||
* @param matrix Matrix to apply.
|
||||
*/
|
||||
virtual void setMatrix(
|
||||
const T parameter,
|
||||
const glm::mat4 matrix
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* Attaches a boolean to a shader.
|
||||
*
|
||||
* @param parameter parameter to set.
|
||||
* @param value Value to set.
|
||||
*/
|
||||
virtual void setBoolean(
|
||||
const T parameter,
|
||||
const bool_t value
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* Set a color on to the shader.
|
||||
*
|
||||
* @param parameter parameter to set the color to.
|
||||
* @param color Color to set.
|
||||
*/
|
||||
virtual void setColor(
|
||||
const T parameter,
|
||||
const struct Color color
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* Set a 3D vector on to the shader.
|
||||
*
|
||||
* @param parameter parameter to set the vector to.
|
||||
* @param vector Vector to set.
|
||||
*/
|
||||
virtual void setVector3(
|
||||
const T parameter,
|
||||
const glm::vec3 vector
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* Attaches a texture to the currently bound shader.
|
||||
*
|
||||
* @param parameter parameter to set the texture on to.
|
||||
* @param texture Texture slot to bind to the parameter.
|
||||
*/
|
||||
virtual void setTexture(
|
||||
const T parameter,
|
||||
const textureslot_t texture
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* Sets a floating point value to the shader.
|
||||
*
|
||||
* @param parameter Paramater to set the float ont o.
|
||||
* @param Float to bind.
|
||||
*/
|
||||
virtual void setFloat(
|
||||
const T parameter,
|
||||
const float_t value
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* Destroys/Cleans up the shader.
|
||||
*/
|
||||
virtual ~IShader() {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<typename L>
|
||||
class IShaderParameterBuffer {
|
||||
public:
|
||||
/**
|
||||
* Initializes this shader parameter buffer.
|
||||
*/
|
||||
virtual void init() = 0;
|
||||
|
||||
/**
|
||||
* Bind this shader buffer to the supplied location.
|
||||
*
|
||||
* @param location Location to bind this buffer to.
|
||||
*/
|
||||
virtual void bind(const L location) = 0;
|
||||
};
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "ShaderManager.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
ShaderManager::ShaderManager() {
|
||||
this->nextId = 0;
|
||||
this->nextLock = 0;
|
||||
}
|
||||
|
||||
ShaderManager::~ShaderManager() {
|
||||
}
|
@ -1,106 +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 {
|
||||
typedef int64_t shaderlock_t;
|
||||
typedef int16_t shaderid_t;
|
||||
|
||||
class ShaderManager {
|
||||
private:
|
||||
int32_t nextId;
|
||||
shaderlock_t nextLock;
|
||||
std::map<shaderid_t, std::shared_ptr<Shader>> shaders;
|
||||
std::map<shaderlock_t, shaderid_t> shaderLocks;
|
||||
std::map<shaderid_t, std::vector<shaderlock_t>> shaderLocksByShader;
|
||||
|
||||
/**
|
||||
* Returns the shader id for the given shader type, or -1 if it is not
|
||||
* loaded.
|
||||
*
|
||||
* @return The shader id for the shader, or -1 if it is not loaded.
|
||||
*/
|
||||
template<class T>
|
||||
shaderid_t getShaderId() {
|
||||
auto it = shaders.begin();
|
||||
while(it != shaders.end()) {
|
||||
auto asT = std::dynamic_pointer_cast<T>(it->second);
|
||||
if(asT != nullptr) return asT->shaderId;
|
||||
++it;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new shader manager.
|
||||
*/
|
||||
ShaderManager();
|
||||
|
||||
/**
|
||||
* Locks a shader of the given type. If the shader is not already loaded,
|
||||
* it will be loaded. If the shader is already loaded, it will be
|
||||
* returned.
|
||||
*
|
||||
* @return The shader lock for the shader of the given type.
|
||||
*/
|
||||
template<class T>
|
||||
shaderlock_t lockShader() {
|
||||
auto shaderId = this->getShaderId<T>();
|
||||
if(shaderId == -1) {
|
||||
auto shader = std::make_shared<T>();
|
||||
shader->compile();
|
||||
shader->shaderId = this->nextId++;
|
||||
this->shaders[shader->shaderId] = shader;
|
||||
shaderId = shader->shaderId;
|
||||
}
|
||||
|
||||
shaderlock_t lock = this->nextLock++;
|
||||
this->shaderLocks[lock] = shaderId;
|
||||
this->shaderLocksByShader[shaderId].push_back(lock);
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the shader for the given lock.
|
||||
*
|
||||
* @param lock The shader lock.
|
||||
* @return The shader for the given lock.
|
||||
*/
|
||||
template<class T>
|
||||
std::shared_ptr<T> getShader(shaderlock_t lock) {
|
||||
auto shaderId = this->shaderLocks[lock];
|
||||
return std::static_pointer_cast<T>(this->shaders[shaderId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the shader for the given lock. This will unload any shader
|
||||
* that is no longer in use.
|
||||
*
|
||||
* @param lock Lock to release.
|
||||
*/
|
||||
template<class T>
|
||||
void releaseShader(shaderlock_t lock) {
|
||||
auto shaderId = this->shaderLocks[lock];
|
||||
this->shaderLocks.erase(lock);
|
||||
|
||||
auto& locks = this->shaderLocksByShader[shaderId];
|
||||
auto it = std::find(locks.begin(), locks.end(), lock);
|
||||
if(it != locks.end()) locks.erase(it);
|
||||
|
||||
if(locks.size() == 0) {
|
||||
this->shaderLocksByShader.erase(shaderId);
|
||||
this->shaders.erase(shaderId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the shader manager.
|
||||
*/
|
||||
~ShaderManager();
|
||||
};
|
||||
}
|
@ -1,37 +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"
|
||||
#include "display/mesh/Mesh.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct ShaderPassItem;
|
||||
|
||||
struct ShaderPassItem {
|
||||
std::shared_ptr<Shader> shader;
|
||||
int32_t priority = 0;
|
||||
std::vector<struct ShaderPassItem>::iterator index;
|
||||
|
||||
Mesh *mesh;
|
||||
int32_t start = 0;
|
||||
int32_t count = -1;
|
||||
float_t w = 0;
|
||||
flag_t renderFlags = RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST;
|
||||
enum MeshDrawMode drawMode = MESH_DRAW_MODE_TRIANGLES;
|
||||
|
||||
// Parameters
|
||||
std::map<shaderparameter_t, struct Color> colorValues;
|
||||
std::map<shaderparameter_t, bool_t> boolValues;
|
||||
std::map<shaderparameter_t, glm::mat4> matrixValues;
|
||||
std::map<shaderparameter_t, glm::vec3> vec3Values;
|
||||
std::map<shaderparameter_t, textureslot_t> textureValues;
|
||||
std::map<shaderparameter_t, float_t> floatValues;
|
||||
std::map<shaderbufferlocation_t, IShaderParameterBuffer<shaderbufferslot_t>*> parameterBuffers;
|
||||
|
||||
// Textures
|
||||
std::map<textureslot_t, Texture*> textureSlots;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user