Chira Engine
A customizable MIT-licensed game engine.
Shader.h
1 #pragma once
2 
3 #include <glm/glm.hpp>
4 #include <entity/component/LightComponents.h>
5 #include <math/Types.h>
6 #include <render/backend/RenderBackend.h>
7 #include <resource/Resource.h>
8 #include <utility/Serial.h>
9 
10 namespace chira {
11 
12 constexpr std::string_view SHADER_PREPROCESSOR_DEFAULT_PREFIX = "#";
13 constexpr std::string_view SHADER_PREPROCESSOR_DEFAULT_SUFFIX = "#";
14 
15 class Shader : public Resource {
16 public:
17  explicit Shader(std::string identifier_);
18  void compile(const byte buffer[], std::size_t bufferLength) override;
19  void use() const;
20  ~Shader() override;
21 
22  inline void setUniform(std::string_view name, bool value) {
23  Renderer::setShaderUniform1b(this->handle, name, value);
24  }
25  inline void setUniform(std::string_view name, unsigned int value) {
26  Renderer::setShaderUniform1u(this->handle, name, value);
27  }
28  inline void setUniform(std::string_view name, int value) {
29  Renderer::setShaderUniform1i(this->handle, name, value);
30  }
31  inline void setUniform(std::string_view name, float value) {
32  Renderer::setShaderUniform1f(this->handle, name, value);
33  }
34  inline void setUniform(std::string_view name, glm::vec2b value) {
35  Renderer::setShaderUniform2b(this->handle, name, value);
36  }
37  inline void setUniform(std::string_view name, glm::vec2u value) {
38  Renderer::setShaderUniform2u(this->handle, name, value);
39  }
40  inline void setUniform(std::string_view name, glm::vec2i value) {
41  Renderer::setShaderUniform2i(this->handle, name, value);
42  }
43  inline void setUniform(std::string_view name, glm::vec2f value) {
44  Renderer::setShaderUniform2f(this->handle, name, value);
45  }
46  inline void setUniform(std::string_view name, glm::vec3b value) {
47  Renderer::setShaderUniform3b(this->handle, name, value);
48  }
49  inline void setUniform(std::string_view name, glm::vec3u value) {
50  Renderer::setShaderUniform3u(this->handle, name, value);
51  }
52  inline void setUniform(std::string_view name, glm::vec3i value) {
53  Renderer::setShaderUniform3i(this->handle, name, value);
54  }
55  inline void setUniform(std::string_view name, glm::vec3f value) {
56  Renderer::setShaderUniform3f(this->handle, name, value);
57  }
58  inline void setUniform(std::string_view name, glm::vec4b value) {
59  Renderer::setShaderUniform4b(this->handle, name, value);
60  }
61  inline void setUniform(std::string_view name, glm::vec4u value) {
62  Renderer::setShaderUniform4u(this->handle, name, value);
63  }
64  inline void setUniform(std::string_view name, glm::vec4i value) {
65  Renderer::setShaderUniform4i(this->handle, name, value);
66  }
67  inline void setUniform(std::string_view name, glm::vec4f value) {
68  Renderer::setShaderUniform4f(this->handle, name, value);
69  }
70  inline void setUniform(std::string_view name, glm::mat4 value) {
71  Renderer::setShaderUniform4m(this->handle, name, value);
72  }
73 
74  [[nodiscard]] inline bool usesPVMatrices() const {
75  return this->usesPV;
76  }
77  [[nodiscard]] inline bool usesModelMatrix() const {
78  return this->usesM;
79  }
80  [[nodiscard]] inline bool isLit() const {
81  return this->lit;
82  }
83 
84  static void addPreprocessorSymbol(const std::string& name, const std::string& value);
85  static void setPreprocessorPrefix(const std::string& prefix);
86  static void setPreprocessorSuffix(const std::string& suffix);
87 
88 private:
89  static inline std::unordered_map<std::string, std::string> preprocessorSymbols{
90  {"DIRECTIONAL_LIGHT_MAX", std::to_string(DIRECTIONAL_LIGHT_MAX)},
91  {"POINT_LIGHT_MAX", std::to_string(POINT_LIGHT_MAX)},
92  {"SPOT_LIGHT_MAX", std::to_string(SPOT_LIGHT_MAX)},
93  };
94  static inline std::string preprocessorPrefix = std::string{SHADER_PREPROCESSOR_DEFAULT_PREFIX};
95  static inline std::string preprocessorSuffix = std::string{SHADER_PREPROCESSOR_DEFAULT_SUFFIX};
96 
97  static std::string replaceMacros(const std::string&, const std::string&);
98 
99  Renderer::ShaderHandle handle{};
100  bool usesPV = true;
101  bool usesM = true;
102  bool lit = true;
103  std::string vertexPath{"file://shaders/unlitTextured.vsh"};
104  std::string fragmentPath{"file://shaders/unlitTextured.fsh"};
105 
106 public:
107  template<typename Archive>
108  void serialize(Archive& ar) {
109  ar(
110  cereal::make_nvp("usesPV", this->usesPV),
111  cereal::make_nvp("usesM", this->usesM),
112  cereal::make_nvp("lit", this->lit),
113  cereal::make_nvp("vertex", this->vertexPath),
114  cereal::make_nvp("fragment", this->fragmentPath)
115  );
116  }
117 };
118 
119 } // namespace chira
A chunk of data, usually a file. Is typically cached and shared.
Definition: Resource.h:19