putting shaders changes on a new branch to avoid merge nightmare

This commit is contained in:
Clément Le Bihan
2021-06-08 12:41:32 +02:00
parent 6ff8a20030
commit 4ee45c9c28
6 changed files with 168 additions and 2 deletions

View File

@@ -97,7 +97,7 @@ set(SRC
sources/Model/ModelAnimations.cpp
sources/Vector/Vector2.cpp
sources/Vector/Vector3.cpp
)
sources/Shaders/Shaders.cpp sources/Shaders/Shaders.hpp)
find_package(raylib QUIET)
if (NOT raylib_FOUND)

View File

@@ -21,3 +21,8 @@ RAY::Exception::NotCompatibleError::NotCompatibleError(const std::string &expect
RayError(expectionMessage)
{
}
RAY::Exception::WrongInputError::WrongInputError(const std::string &what):
RayError(what)
{
}

View File

@@ -59,6 +59,23 @@ namespace RAY::Exception {
//! @brief A default assignment operator
NotSupportedError &operator=(const NotSupportedError &) = default;
};
//! @brief exception used when an non-supported operation is done
class WrongInputError: public RayError {
public:
//! @brief Create a new exception instance
explicit WrongInputError(const std::string &what = "The input can't be proceed");
//! @brief A default destructor
~WrongInputError() override = default;
//! @brief An exception is copy constructable
WrongInputError(const WrongInputError &) = default;
//! @brief A default assignment operator
WrongInputError &operator=(const WrongInputError &) = default;
};
}
#endif /* !RAYERROR_HPP_ */

View File

@@ -0,0 +1,49 @@
//
// Created by cbihan on 04/06/2021.
//
#include "Shaders.hpp"
#include <utility>
#include "Exceptions/RayError.hpp"
namespace RAY
{
Cache<::Shader> Shader::_shadersCache(LoadShader, UnloadShader);
Shader::Shader(const std::string &vertexFile, const std::string &fragmentFile)
: _vertexFile(vertexFile),
_fragmentFile(fragmentFile),
_rayLibShader(_shadersCache.fetch(vertexFile, fragmentFile))
{
}
const std::shared_ptr<::Shader> &Shader::getShaderPtr() const
{
return this->_rayLibShader;
}
void Shader::setShaderUniformVar(const std::string &varName, float value)
{
if (this->_shaderIndexVars.find(varName) == this->_shaderIndexVars.end()) {
int varShaderIndex = GetShaderLocation(*this->_rayLibShader, varName.c_str());
if (varShaderIndex < 0) {
throw Exception::WrongInputError("The loaded shader doesn't have a variable called: " + varName);
}
this->_shaderIndexVars[varName] = varShaderIndex;
}
SetShaderValue(*this->_rayLibShader, this->_shaderIndexVars[varName], &value, SHADER_UNIFORM_FLOAT);
}
void Shader::BeginUsingCustomShader(Shader &shader)
{
BeginShaderMode(*shader.getShaderPtr());
}
void Shader::EndUsingCustomShader()
{
EndShaderMode();
}
}

View File

@@ -0,0 +1,55 @@
//
// Created by cbihan on 04/06/2021.
//
#pragma once
#include <string>
#include <memory>
#include <map>
#include <raylib.h>
#include "Utils/Cache.hpp"
namespace RAY
{
class Shader
{
private:
//! @brief The vertex file of the shader
std::string _vertexFile;
//! @brief The fragment file of the shader
std::string _fragmentFile;
//! @brief Raylib shader struct
std::shared_ptr<::Shader> _rayLibShader;
//! @brief Fetch and cache the index of the shader variable on the first set of the var
std::map<std::string, int> _shaderIndexVars = {};
static Cache<::Shader> _shadersCache;
public:
//INTERNAL:
const std::shared_ptr<::Shader> &getShaderPtr() const;
public:
//! @brief start using a custom shader when drawing
static void BeginUsingCustomShader(RAY::Shader &shader);
//! @brief stop using the custom shader
static void EndUsingCustomShader();
//! @brief The set var for float values
//! @note Throw if the var is not found
void setShaderUniformVar(const std::string &varName, float value);
//! @brief ctor if no vertexfile in needed set it to nullptr
Shader(const std::string &vertexFile, const std::string &fragmentFile);
//! @brief Default copy ctor
Shader(const Shader &) = default;
//! @brief dtor
~Shader() = default;
//! @brief Assignment operator
Shader &operator=(const Shader &) = default;
};
}

View File

@@ -9,7 +9,9 @@
#include <memory>
#include <unordered_map>
#include <functional>
#include <utility>
#include <raylib.h>
#include "Exceptions/RayError.hpp"
namespace RAY {
//! @brief A templated class used to cache ressources, indexed with a string
@@ -57,7 +59,7 @@ namespace RAY {
class Cache<::ModelAnimation> {
public:
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
_dataLoader(dataLoader), _dataUnloader(dataUnloader)
_dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
{};
std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
{
@@ -83,4 +85,42 @@ namespace RAY {
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<::ModelAnimation>> _cache;
};
template<>
class Cache<::Shader>
{
public:
Cache(std::function<::Shader(const char *, const char *)> dataLoader,
std::function<void(::Shader)> dataUnloader) :
_dataLoader(std::move(dataLoader)), _dataUnloader(std::move(dataUnloader))
{};
std::shared_ptr<::Shader> fetch(const std::string &vertexFile, const std::string &fragmentFile)
{
const std::string index = vertexFile + fragmentFile;
if (vertexFile.empty() && fragmentFile.empty()) {
throw RAY::Exception::WrongInputError();
}
if (this->_cache.find(index) != this->_cache.end())
return this->_cache[index];
this->_cache.emplace(index, std::shared_ptr<::Shader>(
new ::Shader(
this->_dataLoader(vertexFile.empty() ? nullptr : vertexFile.c_str(), fragmentFile.c_str())),
[this](::Shader *p) {
this->_dataUnloader(*p);
}));
return this->_cache[index];
};
private:
//! @brief function to call to load data
std::function<::Shader(const char *, const char *)> _dataLoader;
//! @brief function to call when the ray data will be unloaded
std::function<void(::Shader)> _dataUnloader;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<::Shader>> _cache;
};
}