Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
shader_base.h
1#ifndef OE_RENDER_SHADER_BASE_H
2#define OE_RENDER_SHADER_BASE_H
3
4#include <vector>
5#include <string>
6#include <map>
7#include <span>
8#include <unordered_map>
9#include <glm/mat4x4.hpp>
10
11#include "passes.h"
12#include "../scene/material.h"
13
14namespace oe::scene
15{
16 class Camera;
17 class Node;
18 class Material;
19}
20
21namespace oe::render
22{
23 class Texture;
24 class Cubemap;
25
32 {
33 public:
39 ShaderBase(const std::string& name = "");
40
41 ShaderBase(const ShaderBase&) = delete;
42 ShaderBase& operator=(const ShaderBase&) = delete;
43
44 ShaderBase(ShaderBase&& other) noexcept:
45 _program_handle(other._program_handle),
46 _name(other._name),
47 _defines(other._defines),
48 _locations(other._locations),
49 _pass_locations(other._pass_locations),
50 _pass_programs(other._pass_programs)
51 {
52 other._program_handle = 0;
53 other._pass_programs = {};
54 }
55
56 ~ShaderBase();
57
68 template <typename T>
69 void define(const std::string& name, const T& value)
70 {
71 _defines[name] = std::to_string(value);
72 }
73
79 void define(const std::string& name, const char* value)
80 {
81 _defines[name] = std::string(value);
82 }
83
89 void define(const std::string& name, const std::string& value)
90 {
91 _defines[name] = value;
92 }
93
101 void define(const std::string& name)
102 {
103 _defines[name] = "";
104 }
105
110 const std::string& getName() const
111 {
112 return _name;
113 }
114
121 void compile(const GeometryPass vertex_pass, const RenderingPass fragment_pass);
122
128 void bind(const GeometryPass vertex_pass, const RenderingPass fragment_pass);
129
138 void fillFromCamera(const scene::Camera& camera) noexcept;
139
143 void fillTextures(std::span<const scene::TextureProperty> textures);
144
150 void fillTextures(const std::map<std::string, std::pair<uint32_t, Texture*>>& textures);
151
155 void fillCubemaps(const std::map<std::string, std::pair<uint32_t, Cubemap*>>& cubemaps);
156
170 void fillFromMaterial(const scene::Material* material);
171
175 void unbind();
176
177 /*
178 *
179 */
180 //std::string getShaderContent(const VertexPass vertex_pass, const RenderingPass fragment_pass) const;
181
188 void setInt(const std::string& name, const int32_t value);
189
196 void setUInt(const std::string& name, const uint32_t value);
197
204 void setFloat(const std::string& name, const float value);
205
212 void setDouble(const std::string& name, const double value);
213
220 void setMat3(const std::string& name, const glm::mat3& value);
221
228 void setMat4(const std::string& name, const glm::mat4& value);
229
236 void setVec2i(const std::string& name, const glm::ivec2& value);
237
244 void setVec2(const std::string& name, const glm::vec2& value);
245
252 void setVec3(const std::string& name, const glm::vec3& value);
253
260 void setVec4(const std::string& name, const glm::vec4& value);
261
268 void setIntArray(const std::string& name, const std::vector<int32_t>& values);
269
276 void setUIntArray(const std::string& name, const std::vector<uint32_t>& values);
277
284 void setFloatArray(const std::string& name, const std::vector<float>& values);
285
292 void setDoubleArray(const std::string& name, const std::vector<double>& values);
293
300 void setVec2iArray(const std::string& name, const std::vector<glm::ivec2>& values);
301
308 void setVec2Array(const std::string& name, const std::vector<glm::vec2>& values);
309
316 void setVec3Array(const std::string& name, const std::vector<glm::vec3>& values);
317
324 void setVec4Array(const std::string& name, const std::vector<glm::vec4>& values);
325
332 void setMat4Array(const std::string& name, const std::vector<glm::mat4>& values);
333
334 protected:
335 std::unordered_map<GeometryPass, std::string> _vertex_shader_sources;
336 std::unordered_map<RenderingPass, std::string> _fragment_shader_sources;
337
338 std::string _header;
339
340 private:
341
342 struct PassPairHash
343 {
344 std::size_t operator ()(const std::pair<GeometryPass, RenderingPass> &p) const
345 {
346 auto h1 = std::hash<GeometryPass>{}(p.first);
347 auto h2 = std::hash<RenderingPass>{}(p.second);
348
349 return h1 ^ h2;
350 }
351 };
352
353 std::string _generate_header() const noexcept;
354
355 uint32_t _program_handle = 0;
356
357 std::string _name;
358
359 std::unordered_map<std::string, std::string> _defines;
360
361 std::unordered_map<std::string, int32_t> _locations;
362
363 std::unordered_map<std::pair<GeometryPass, RenderingPass>, std::unordered_map<std::string, int32_t>, PassPairHash> _pass_locations;
364 std::unordered_map<std::pair<GeometryPass, RenderingPass>, uint32_t, PassPairHash> _pass_programs;
365
366 static uint32_t _last_bound_shader;
367 };
368}
369
370#endif
Definition cubemap.h:9
void setVec4(const std::string &name, const glm::vec4 &value)
Send a vec4 uniform to the shader.
void compile(const GeometryPass vertex_pass, const RenderingPass fragment_pass)
Compile / link the shader using the graphic API.
void setUIntArray(const std::string &name, const std::vector< uint32_t > &values)
Send a array of uint32_t uniforms to the shader.
void setFloatArray(const std::string &name, const std::vector< float > &values)
Send an array of float uniforms to the shader.
void fillCubemaps(const std::map< std::string, std::pair< uint32_t, Cubemap * > > &cubemaps)
Fill cubemaps uniforms + bind cubemaps.
const std::string & getName() const
Get shader name.
Definition shader_base.h:110
void fillTextures(std::span< const scene::TextureProperty > textures)
Fill textures uniforms + bind textures.
void bind(const GeometryPass vertex_pass, const RenderingPass fragment_pass)
Use the shader in graphic API.
void fillFromCamera(const scene::Camera &camera) noexcept
Fill uniforms from camera (mainly position + transform matrices).
void setFloat(const std::string &name, const float value)
Send a float uniform to the shader.
void setMat3(const std::string &name, const glm::mat3 &value)
Send a 3x3 matrix uniform to the shader.
void setVec2(const std::string &name, const glm::vec2 &value)
Send a vec2 uniform to the shader.
void setMat4(const std::string &name, const glm::mat4 &value)
Send a 4x4 matrix uniform to the shader.
void define(const std::string &name, const char *value)
Define a shader constant.
Definition shader_base.h:79
void fillFromMaterial(const scene::Material *material)
Fill uniforms from material.
void setMat4Array(const std::string &name, const std::vector< glm::mat4 > &values)
Send an array of matrix uniforms to the shader.
void unbind()
Tell the graphic API to release this shader.
void setVec2iArray(const std::string &name, const std::vector< glm::ivec2 > &values)
Send an array of ivec2 uniforms to the shader.
ShaderBase(const std::string &name="")
Create a new shader instance.
void fillTextures(const std::map< std::string, std::pair< uint32_t, Texture * > > &textures)
Fill textures uniforms + bind textures.
void setVec3Array(const std::string &name, const std::vector< glm::vec3 > &values)
Send an array of vec3 uniforms to the shader.
void setDouble(const std::string &name, const double value)
Send a double uniform to the shader.
void define(const std::string &name, const T &value)
Define a shader constant.
Definition shader_base.h:69
void define(const std::string &name)
Define a shader constant without value.
Definition shader_base.h:101
void setVec4Array(const std::string &name, const std::vector< glm::vec4 > &values)
Send an array of vec4 uniforms to the shader.
void setUInt(const std::string &name, const uint32_t value)
Send an uint32_t uniform to the shader.
void setInt(const std::string &name, const int32_t value)
Send an int uniform to the shader.
void setVec2Array(const std::string &name, const std::vector< glm::vec2 > &values)
Send an array of vec2 uniforms to the shader.
void setVec3(const std::string &name, const glm::vec3 &value)
Send a vec3 uniform to the shader.
void setDoubleArray(const std::string &name, const std::vector< double > &values)
Send an array of double uniforms to the shader.
void setVec2i(const std::string &name, const glm::ivec2 &value)
Send an ivec2 uniform to the shader.
void setIntArray(const std::string &name, const std::vector< int32_t > &values)
Send an array of int uniforms to the shader.
void define(const std::string &name, const std::string &value)
Define a shader constant.
Definition shader_base.h:89
Definition texture.h:31
The "eye of the scene".
Definition camera.h:31
Render agnostic material.
Definition material.h:90
Scene node.
Definition node.h:30
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures).
Definition opengl.h:12
RenderingPass
Pipeline rendering passes.
Definition passes.h:24
GeometryPass
Vertex rendering passes.
Definition passes.h:12
Scene related management (Render-agnostic Geometry, Manger, etc...).
Definition debug.h:19