2023-06-30 20:41:02 +02:00
|
|
|
#ifndef PARADISO_SHADER_HPP
|
|
|
|
#define PARADISO_SHADER_HPP
|
|
|
|
|
|
|
|
#include "globals.hpp"
|
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
2023-06-30 20:41:02 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <variant>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
namespace paradiso {
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
struct Shader final {
|
|
|
|
Shader();
|
|
|
|
~Shader();
|
|
|
|
Shader(const Shader&) = delete;
|
|
|
|
Shader(Shader&&) = default;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
enum class Type { Vertex, Fragment, Geometry, Compute };
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
void set_source(Type t, const std::string& c) { source_[t] = c; }
|
|
|
|
std::string source(Type t) const { return source_.at(t); }
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
bool ready() const;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
bool build();
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
void use();
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
Shader& set_uniform_at_location(int location,
|
|
|
|
float v); //!< sets a float in a shader
|
|
|
|
Shader&
|
|
|
|
set_uniform_at_location(int location,
|
|
|
|
uint32_t v); //!< sets a 32bit unsigned in a shader
|
|
|
|
Shader&
|
|
|
|
set_uniform_at_location(int location,
|
|
|
|
int32_t v); //!< sets a 32bit signed in a shader
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
/**
|
|
|
|
* @brief retrieves the position of a uniform
|
|
|
|
* @param name of the uniform
|
|
|
|
* @return position of the uniform or negative if it doesn't exist
|
|
|
|
*/
|
|
|
|
int uniform_location(std::string const& name) const;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
/**
|
|
|
|
* @brief check if a uniform with the given name exists
|
|
|
|
* @param name of the uniform
|
|
|
|
* @return true if found
|
|
|
|
*/
|
|
|
|
bool has_uniform(std::string const& name) const {
|
|
|
|
return uniform_location(name) >= 0;
|
|
|
|
}
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
/**
|
|
|
|
* sets data of the
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
Shader& set_uniform(std::string const& name, T&& value) {
|
|
|
|
return set_uniform_at_location(uniform_location(name),
|
|
|
|
std::forward<T>(value));
|
|
|
|
}
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
using uniform_t =
|
|
|
|
std::variant<bool, int, float,
|
|
|
|
double /*,vector2f,vector3f,vector4f,matrix4x4f*/>;
|
|
|
|
using uniform_entry_t = std::tuple<std::string, uniform_t, int>;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
using uniform_cache_t = std::vector<uniform_entry_t>;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
void set_uniforms(uniform_cache_t c);
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
uint32_t native_handle() const;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
private:
|
|
|
|
std::unordered_map<Type, std::string> source_;
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
struct impl;
|
|
|
|
std::unique_ptr<impl> impl_;
|
|
|
|
};
|
2023-06-30 20:41:02 +02:00
|
|
|
|
2023-06-30 22:05:23 +02:00
|
|
|
} // namespace paradiso
|
2023-06-30 20:41:02 +02:00
|
|
|
|
|
|
|
#endif
|