Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
ShaderProgram.h
Go to the documentation of this file.
1/**
2 * @file ShaderProgram.h
3 * @ingroup mnd_graphics
4 * @brief Wrapper around a linked OpenGL GLSL program with uniform caching.
5 *
6 * ShaderProgram objects are created by GraphicsAPI::CreateShaderProgram() from
7 * GLSL source strings (loaded from .vert / .frag files at runtime). Once built,
8 * they are owned by shared_ptr and shared by Material instances.
9 *
10 * ## Uniform upload flow
11 * @code
12 * shader->Bind();
13 * shader->SetUniform("uModel", modelMatrix); // cached location lookup
14 * shader->SetTexture("uDiffuse", texture); // binds to next texture unit
15 * @endcode
16 *
17 * Uniform locations are cached after the first lookup to avoid repeated
18 * glGetUniformLocation calls, which are expensive if issued every frame.
19 *
20 * @see GraphicsAPI::CreateShaderProgram, Material
21 */
22
23#pragma once
24#include <string>
25#include <unordered_map>
26
27#include "Types.h"
28#include "graphics/GLForward.h"
29
30namespace mnd
31{
32class Texture;
33
34/**
35 * @brief Linked GLSL program with cached uniform locations.
36 *
37 * Non-copyable — each GL program handle is unique. Created exclusively by
38 * GraphicsAPI and typically stored in a std::shared_ptr<ShaderProgram>.
39 */
41{
42public:
43 ShaderProgram() = delete;
44 ShaderProgram(const ShaderProgram &) = delete;
46
47 /**
48 * @brief Wraps an existing linked GL program.
49 * @param shaderProgramID A GL program handle returned by glCreateProgram().
50 */
51 explicit ShaderProgram(GLuint shaderProgramID);
52
53 /// Calls glDeleteProgram() to release the GPU resource.
55
56 /// Activate this program for subsequent draw calls (glUseProgram).
57 void Bind();
58
59 /**
60 * @brief Look up a uniform location, caching the result for future calls.
61 * @param name The GLSL uniform variable name.
62 * @return The GL uniform location, or -1 if not found.
63 */
64 GLint GetUniformLocation(const std::string &name);
65
66 /// @name Uniform Setters
67 /// Upload typed values to named GLSL uniforms.
68 /// @{
69 void SetUniform(const std::string &name, float value); ///< Upload a float uniform.
70 void SetUniform(const std::string &name, float v0, float v1); ///< Upload a vec2 uniform.
71 void SetUniform(const std::string &name, float v0, float v1, float v2); ///< Upload a vec3 uniform.
72 void SetUniform(const std::string &name, float v0, float v1, float v2, float v3); ///< Upload a vec4 uniform.
73 void SetUniform(const std::string &name, const mat4 &mat); ///< Upload a 4×4 matrix (column-major).
74 void SetUniform(const std::string &name, const vec3 &value); ///< Upload a vec3 from a glm::vec3.
75 void SetUniform(const std::string &name, const mat4 *matrices, std::size_t count); ///< Upload an array of 4×4 matrices.
76
77 /**
78 * @brief Bind a texture to the next available texture unit and set the sampler uniform.
79 * @param name GLSL sampler uniform name (e.g. "uDiffuse").
80 * @param texture Texture to bind; must not be nullptr.
81 */
82 void SetTexture(const std::string &name, Texture *texture);
83 /// @}
84
85private:
86 std::unordered_map<std::string, GLint> m_uniformLocationCache; ///< Name → GL location cache.
87 GLuint m_shaderProgramID = 0; ///< Underlying GL program handle.
88 int m_currentTextureUnit = 0; ///< Next free texture unit index.
89};
90
91} // namespace mnd
int GLint
Definition GLForward.h:13
unsigned int GLuint
Definition GLForward.h:11
Engine-wide primitive type aliases and GLM math imports.
Linked GLSL program with cached uniform locations.
void SetTexture(const std::string &name, Texture *texture)
Bind a texture to the next available texture unit and set the sampler uniform.
ShaderProgram()=delete
void Bind()
Activate this program for subsequent draw calls (glUseProgram).
void SetUniform(const std::string &name, float value)
Upload a float uniform.
~ShaderProgram()
Calls glDeleteProgram() to release the GPU resource.
ShaderProgram & operator=(const ShaderProgram &)=delete
ShaderProgram(const ShaderProgram &)=delete
GLint GetUniformLocation(const std::string &name)
Look up a uniform location, caching the result for future calls.
2D OpenGL texture object wrapping a GL_TEXTURE_2D handle.
Definition Texture.h:41
glm::mat4 mat4
4×4 column-major float matrix (model/view/projection).
Definition Types.h:63
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51