❖
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
30
namespace
mnd
31
{
32
class
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
*/
40
class
ShaderProgram
41
{
42
public
:
43
ShaderProgram
() =
delete
;
44
ShaderProgram
(
const
ShaderProgram
&) =
delete
;
45
ShaderProgram
&
operator=
(
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.
54
~ShaderProgram
();
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
85
private
:
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
GLForward.h
GLint
int GLint
Definition
GLForward.h:13
GLuint
unsigned int GLuint
Definition
GLForward.h:11
Types.h
Engine-wide primitive type aliases and GLM math imports.
mnd::ShaderProgram
Linked GLSL program with cached uniform locations.
Definition
ShaderProgram.h:41
mnd::ShaderProgram::SetTexture
void SetTexture(const std::string &name, Texture *texture)
Bind a texture to the next available texture unit and set the sampler uniform.
Definition
ShaderProgram.cpp:86
mnd::ShaderProgram::ShaderProgram
ShaderProgram()=delete
mnd::ShaderProgram::Bind
void Bind()
Activate this program for subsequent draw calls (glUseProgram).
Definition
ShaderProgram.cpp:20
mnd::ShaderProgram::SetUniform
void SetUniform(const std::string &name, float value)
Upload a float uniform.
Definition
ShaderProgram.cpp:40
mnd::ShaderProgram::~ShaderProgram
~ShaderProgram()
Calls glDeleteProgram() to release the GPU resource.
Definition
ShaderProgram.cpp:14
mnd::ShaderProgram::operator=
ShaderProgram & operator=(const ShaderProgram &)=delete
mnd::ShaderProgram::ShaderProgram
ShaderProgram(const ShaderProgram &)=delete
mnd::ShaderProgram::GetUniformLocation
GLint GetUniformLocation(const std::string &name)
Look up a uniform location, caching the result for future calls.
Definition
ShaderProgram.cpp:26
mnd::Texture
2D OpenGL texture object wrapping a GL_TEXTURE_2D handle.
Definition
Texture.h:41
mnd::mat4
glm::mat4 mat4
4×4 column-major float matrix (model/view/projection).
Definition
Types.h:63
mnd::vec3
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition
Types.h:51
mnd
Definition
IdleClip.cpp:13
engine
source
graphics
ShaderProgram.h
Generated by
1.9.8