Raito 0.1
Loading...
Searching...
No Matches
OpenGLShader.h
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2023 Víctor Falcón Zaro
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#pragma once
26
29#include <Renderer/Shader.h>
30
31#include <Math/MathTypes.h>
32#include <glm/gtc/type_ptr.hpp>
33
35{
37 {
38 VERTEX = (1u << 0),
39 TESSELATION = (1u << 1),
40 GEOMETRY = (1u << 2),
41 FRAGMENT = (1u << 3),
42 COMPUTE = (1u << 4),
43
44 MAX = (1u << 5)
45 };
46
47 enum class UniformType : u16
48 {
49 NONE = 0,
50 INT,
51 FLOAT,
52 VEC2,
53 VEC3,
54 VEC4,
55 MAT3,
56 MAT4,
60 };
61
63 {
64 size_t Size = 0;
65 GLint Id = 0;
67 };
68
69 class OpenGLShader final : public Shader
70 {
71 public:
72 OpenGLShader(u32 id);
73 virtual ~OpenGLShader();
74
75
77
78 void Bind() const;
79 void UnBind() const;
80
81
82 template<typename T>
83 void SetUniform(u32 id, T value)
84 {
85 ASSERT(false);
86 }
87 template<typename T>
88 void SetUniformRef(u32 id, const T& value)
89 {
90 ASSERT(false);
91 }
92
93 template<typename T>
94 void SetUniform(const char* uniformName, T value)
95 {
96 SetUniform<T>(GetUniformLocation(uniformName), value);
97 }
98 template<typename T>
99 void SetUniformRef(const char* uniformName, const T& value)
100 {
101 SetUniformRef<T>(GetUniformLocation(uniformName), value);
102 }
103
104 NODISCARD OpenGLShaderType ShaderType() const { return m_ShaderType; }
105 NODISCARD size_t UniformCount() const { return m_Uniforms.size(); }
106 NODISCARD size_t AttributeCount() const { return m_Attributes.size(); }
107 NODISCARD const std::unordered_map<std::string, ShaderValue>& Uniforms() const { return m_Uniforms; }
108 NODISCARD const std::unordered_map<std::string, ShaderValue>& Attributes() const { return m_Attributes; }
109 NODISCARD u32 Id() const { return m_ShaderId; }
110
111
112 NODISCARD GLint GetUniformLocation(const std::string& uniformName) const
113 {
114 return GetUniformLocation(uniformName.c_str());
115 }
116
117 NODISCARD GLint GetUniformLocation(const char* uniformName) const
118 {
119 if (m_Uniforms.contains(uniformName))
120 {
121 return m_Uniforms.at(uniformName).Id;
122 }
123
124 return 0;
125 }
126
127 private:
128 u32 m_ShaderId = 0;
130
131 std::unordered_map<std::string, ShaderValue> m_Uniforms{};
132 std::unordered_map<std::string, ShaderValue> m_Attributes{};
133
135 };
136
137 template<>
138 inline void OpenGLShader::SetUniform<bool>(u32 id, bool value)
139 {
140 glUniform1i(id, value);
141 }
142
143 template<>
144 inline void OpenGLShader::SetUniform<i32>(u32 id, i32 value)
145 {
146 glUniform1i(id, value);
147 }
148 template<>
149 inline void OpenGLShader::SetUniform<u32>(u32 id, u32 value)
150 {
151 glUniform1i(id, value);
152 }
153 template<>
154 inline void OpenGLShader::SetUniform<float>(u32 id, float value)
155 {
156 glUniform1f(id, value);
157 }
158 template<>
159 inline void OpenGLShader::SetUniformRef<Iv2>(u32 id, const Iv2& value)
160 {
161 glUniform2i(id, value.x, value.y);
162 }
163 template<>
164 inline void OpenGLShader::SetUniformRef<V2>(u32 id, const V2& value)
165 {
166 glUniform2f(id, value.x, value.y);
167 }
168 template<>
169 inline void OpenGLShader::SetUniformRef<V3>(u32 id, const V3& value)
170 {
171 glUniform3f(id, value.x, value.y, value.z);
172 }
173 template<>
174 inline void OpenGLShader::SetUniformRef<V4>(u32 id, const V4& value)
175 {
176 glUniform4f(id, value.x, value.y, value.z, value.w);
177 }
178 template<>
179 inline void OpenGLShader::SetUniformRef<Mat3>(u32 id, const Mat3& value)
180 {
181 glUniformMatrix3fv(id, 1, GL_FALSE, glm::value_ptr(value));
182 }
183 template<>
184 inline void OpenGLShader::SetUniformRef<Mat4>(u32 id, const Mat4& value)
185 {
186 glUniformMatrix4fv(id, 1, GL_FALSE, glm::value_ptr(value));
187 }
188
189}
#define ASSERT(x)
Definition Assert.h:37
unsigned __int32 u32
Unsigned integer 32 bit type.
Definition BasicTypes.h:55
unsigned __int16 u16
Unsigned integer 16 bit type.
Definition BasicTypes.h:53
signed __int32 i32
Singed integer 32 bit type.
Definition BasicTypes.h:40
#define NODISCARD
Definition Common.h:80
Definition OpenGLShader.h:70
void UnBind() const
Definition OpenGLShader.cpp:165
OpenGLShader(u32 id)
Definition OpenGLShader.cpp:30
NODISCARD size_t AttributeCount() const
Definition OpenGLShader.h:106
virtual ~OpenGLShader()
Definition OpenGLShader.cpp:154
NODISCARD GLint GetUniformLocation(const std::string &uniformName) const
Definition OpenGLShader.h:112
NODISCARD const std::unordered_map< std::string, ShaderValue > & Uniforms() const
Definition OpenGLShader.h:107
NODISCARD const std::unordered_map< std::string, ShaderValue > & Attributes() const
Definition OpenGLShader.h:108
NODISCARD u32 Id() const
Definition OpenGLShader.h:109
void SetUniformRef(u32 id, const T &value)
Definition OpenGLShader.h:88
void SetUniform(const char *uniformName, T value)
Definition OpenGLShader.h:94
void Bind() const
Definition OpenGLShader.cpp:160
void SetUniform(u32 id, T value)
Definition OpenGLShader.h:83
NODISCARD GLint GetUniformLocation(const char *uniformName) const
Definition OpenGLShader.h:117
NODISCARD OpenGLShaderType ShaderType() const
Definition OpenGLShader.h:104
void SetUniformRef(const char *uniformName, const T &value)
Definition OpenGLShader.h:99
NODISCARD size_t UniformCount() const
Definition OpenGLShader.h:105
Definition Shader.h:68
u32 CompileShader(const ShaderFileData &data)
Definition OpenGLShaderCompiler.cpp:124
Definition OpenGLCommon.h:47
OpenGLShaderType
Definition OpenGLShader.h:37
@ MAX
Definition OpenGLShader.h:44
@ VERTEX
Definition OpenGLShader.h:38
@ FRAGMENT
Definition OpenGLShader.h:41
@ TESSELATION
Definition OpenGLShader.h:39
@ GEOMETRY
Definition OpenGLShader.h:40
@ COMPUTE
Definition OpenGLShader.h:42
UniformType
Definition OpenGLShader.h:48
glm::vec4 V4
4D Vector; 32 bit floating point components
Definition MathTypes.h:53
glm::mat4x4 Mat4
Definition MathTypes.h:80
glm::ivec2 Iv2
2D Vector; 32 bit integer components
Definition MathTypes.h:44
glm::mat3x3 Mat3
3x3 Matrix: 32 bit floating point components
Definition MathTypes.h:77
glm::vec2 V2
2D Vector; 32 bit floating point components
Definition MathTypes.h:41
glm::vec3 V3
3D Vector; 32 bit floating point components
Definition MathTypes.h:47
Definition OpenGLShader.h:63
UniformType Type
Definition OpenGLShader.h:66
size_t Size
Definition OpenGLShader.h:64
GLint Id
Definition OpenGLShader.h:65
Definition Shader.h:60