smk::Shader Class Reference

#include <Shader.hpp>

A Shader is a little program that rest on the GPU. They are run on a specific section of the graphic pipeline. More...

Description

A Shader is a little program that rest on the GPU. They are run on a specific section of the graphic pipeline.

This class is movable and copyable (via internal ref-count).

See also
https://learnopengl.com/Getting-started/Shaders

This class is independant from SMK.

Example:

auto vertex_shader = smk::Shader::FromString(R"(
layout(location = 0) in vec2 space_position;
layout(location = 1) in vec2 texture_position;
uniform mat4 projection;
uniform mat4 view;
out vec2 f_texture_position;
void main() {
f_texture_position = texture_position;
gl_Position = projection * view * vec4(space_position, 0.0, 1.0);
}
)", GL_VERTEX_SHADER);
auto fragment_shader = smk::Shader::FromString(R"(
in vec2 f_texture_position;
uniform sampler2D texture_0;
uniform vec4 color;
out vec4 out_color;
void main() {
vec4 color = texture(texture_0, f_texture_position) * color;
vec3 inverted_color = vec3(1.0) - color.rgb;
out_color = vec4(inverted_color, color.a);
}
)", GL_FRAGMENT_SHADER);
auto shader_program = smk::ShaderProgram();
shader_program.AddShader(vertex_shader);
shader_program.AddShader(fragment_shader);
shader_program.Link();
window.SetShaderProgram(shader_program);
A shader program is a set of shader (for instance vertex shader + pixel shader) defining the renderin...
Definition: Shader.hpp:114
static Shader FromString(const std::string &content, GLenum type)
Load a shader from a std::string.
Definition: Shader.cpp:60
See also
ShaderProgram

Definition at line 66 of file Shader.hpp.

Public Member Functions

bool IsReady () const
 Check the status of a Shader. Linking shader is an asynchronous process. Using the shader can causes the CPU to wait until its completion. If you need to do some work before the completion, you can use this function and use the Shader only after it becomes ready. More...
 
bool CompileStatus () const
 Wait until the Shader to be ready. More...
 
GLuint id () const
 The GPU shader id. More...
 
 Shader (Shader &&) noexcept
 
 Shader (const Shader &) noexcept
 
Shaderoperator= (Shader &&) noexcept
 
Shaderoperator= (const Shader &) noexcept
 

Static Public Member Functions

static Shader FromFile (const std::string &filename, GLenum type)
 Load a shader from a file. More...
 
static Shader FromString (const std::string &content, GLenum type)
 Load a shader from a std::string. More...
 

Constructor & Destructor Documentation

◆ ~Shader()

smk::Shader::~Shader ( )

Definition at line 132 of file Shader.cpp.

132 {
133 Release();
134}

◆ Shader() [1/2]

smk::Shader::Shader ( Shader &&  other)
noexcept

Definition at line 140 of file Shader.cpp.

140 {
141 this->operator=(std::move(other));
142}

◆ Shader() [2/2]

smk::Shader::Shader ( const Shader other)
noexcept

Definition at line 136 of file Shader.cpp.

136 {
137 this->operator=(other);
138}

Member Function Documentation

◆ CompileStatus()

bool smk::Shader::CompileStatus ( ) const

Wait until the Shader to be ready.

Returns
True if it suceeded, false otherwise.

Definition at line 113 of file Shader.cpp.

113 {
114 GLint compile_status = {};
115 glGetShaderiv(id_, GL_COMPILE_STATUS, &compile_status);
116 if (compile_status == GL_TRUE) {
117 return true;
118 }
119
120 GLsizei logsize = 0;
121 glGetShaderiv(id_, GL_INFO_LOG_LENGTH, &logsize);
122
123 std::vector<char> log(logsize + 1);
124 glGetShaderInfoLog(id_, logsize, &logsize, log.data());
125
126 std::cout << "[Error] compilation error: " << std::endl;
127 std::cout << log.data() << std::endl;
128
129 return false;
130}

◆ FromFile()

Shader smk::Shader::FromFile ( const std::string &  filename,
GLenum  type 
)
static

Load a shader from a file.

Parameters
filenameThe text filename where the shader is written.
typeEither GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. It can also be any other shader type defined by OpenGL. See https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glCreateShader.xhtml
See also
Shader::FromString
Shader::FromFile

Definition at line 42 of file Shader.cpp.

42 {
43 std::ifstream file(filename);
44 return Shader::FromString(std::string(std::istreambuf_iterator<char>(file),
45 std::istreambuf_iterator<char>()),
46 type);
47}

◆ FromString()

Shader smk::Shader::FromString ( const std::string &  content,
GLenum  type 
)
static

Load a shader from a std::string.

Parameters
contentThe string representing the shader.
typeEither GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. It can also be any other shader type defined by OpenGL. See https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glCreateShader.xhtml
See also
Shader::FromString
Shader::FromFile

Definition at line 60 of file Shader.cpp.

60 {
61 std::vector<char> buffer;
62 for (const auto& c : kShaderHeader) {
63 buffer.push_back(c);
64 }
65 for (const auto& c : content) {
66 buffer.push_back(c);
67 }
68 buffer.push_back('\0');
69 return Shader(buffer, type);
70}

◆ id()

GLuint smk::Shader::id ( ) const

The GPU shader id.

Returns
The OpenGL shader id. If the Shader is invalid, returns zero.

Definition at line 74 of file Shader.cpp.

74 {
75 return id_;
76}

◆ IsReady()

bool smk::Shader::IsReady ( ) const

Check the status of a Shader. Linking shader is an asynchronous process. Using the shader can causes the CPU to wait until its completion. If you need to do some work before the completion, you can use this function and use the Shader only after it becomes ready.

Definition at line 100 of file Shader.cpp.

100 {
101 if (g_khr_parallel_shader) {
102 GLint completion_status = {};
103 glGetShaderiv(id_, GL_COMPLETION_STATUS_KHR, &completion_status);
104 return completion_status == GL_TRUE;
105 }
106
107 std::cerr << "Used bad path" << std::endl;
108 return true;
109}

◆ operator=() [1/2]

Shader & smk::Shader::operator= ( const Shader other)
noexcept

Definition at line 144 of file Shader.cpp.

144 {
145 if (this == &other) {
146 return *this;
147 }
148 Release();
149 if (!other.id_) {
150 return *this;
151 }
152
153 if (!other.ref_count_) {
154 other.ref_count_ = new int(1); // NOLINT
155 }
156
157 id_ = other.id_;
158 ref_count_ = other.ref_count_;
159
160 (*ref_count_)++;
161 return *this;
162}

◆ operator=() [2/2]

Shader & smk::Shader::operator= ( Shader &&  other)
noexcept

Definition at line 164 of file Shader.cpp.

164 {
165 std::swap(id_, other.id_);
166 std::swap(ref_count_, other.ref_count_);
167 return *this;
168}

The documentation for this class was generated from the following files: