SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
OpenGLShader.hpp
Go to the documentation of this file.
1#ifndef OPENGLSHADER_HPP_
2#define OPENGLSHADER_HPP_
3
4#include <glad/glad.h>
5
6#include <string>
7
9#ifdef SLEAK_HOT_RELOAD
10#include <unordered_set>
11#endif
12
13namespace Sleak {
14namespace RenderEngine {
15
16/// GLSL shader program compiled from either a combined source file or separate vertex/fragment files.
17class ENGINE_API OpenGLShader : public Shader {
18public:
20 ~OpenGLShader() override;
21 OpenGLShader(const OpenGLShader&) = delete;
23
24 /// Splits a combined shader file into vertex/fragment stages and links them.
25 bool compile(const std::string& shaderPath) override;
26 /// Compiles and links separate vertex and fragment shader files.
27 bool compile(const std::string& vert, const std::string& frag) override;
28 void bind() override;
29
30 GLuint GetProgram() const { return m_program; }
31
32 /// Rebuilds the program from its files, keeping the old one on failure.
33 bool Reload();
34 /// Returns the stem of the vertex source, e.g. gbuffer for gbuffer_gl.vert.
35 const std::string& GetStem() const { return m_vertStem; }
36 /// Returns true when the vertex or fragment source has the given stem.
37 bool UsesStem(const std::string& stem) const {
38 return stem == m_vertStem || stem == m_fragStem;
39 }
40
41#ifdef SLEAK_HOT_RELOAD
42 /// Returns every live OpenGLShader instance.
43 static const std::unordered_set<OpenGLShader*>& GetLiveShaders();
44#endif
45
46 private:
47 /// Compiles a single shader stage from source text.
48 bool CompileShaderSource(const std::string& source, GLenum type,
49 const std::string& path, GLuint& shader);
50 /// Links a new program from the stored paths and swaps it in on success.
51 bool BuildProgram();
52 std::string ReadFile(const std::string& path);
53
54 GLuint m_program = 0;
55 std::string m_vertPath;
56 std::string m_fragPath;
57 std::string m_vertStem;
58 std::string m_fragStem;
59};
60
61} // namespace RenderEngine
62} // namespace Sleak
63
64#endif // OPENGLSHADER_HPP_
bool UsesStem(const std::string &stem) const
Returns true when the vertex or fragment source has the given stem.
const std::string & GetStem() const
Returns the stem of the vertex source, e.g. gbuffer for gbuffer_gl.vert.
void bind() override
Binds this program as the active shader for subsequent draws.
OpenGLShader(const OpenGLShader &)=delete
OpenGLShader & operator=(const OpenGLShader &)=delete
bool compile(const std::string &shaderPath) override
Splits a combined shader file into vertex/fragment stages and links them.
Backend-agnostic compiled shader program.
Definition Shader.hpp:11
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10