Easy3D 2.6.1
Loading...
Searching...
No Matches
shader_program.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
35
36#ifndef EASY3D_RENDERER_SHADER_PROGRAM_H
37#define EASY3D_RENDERER_SHADER_PROGRAM_H
38
39#include <unordered_map>
40#include <vector>
41#include <string>
42
43
44namespace easy3d {
45
46
75 {
76 public:
84
85 typedef std::pair<AttribType, std::string> Attribute;
86
97
102 static bool is_supported();
103
109 explicit ShaderProgram(const std::string& name = "unknown");
112
117 void set_name(const std::string& name) { name_ = name; }
122 const std::string& name() const { return name_; }
123
128 void set_verbose(bool v) { verbose_ = v; }
129
130 // ---------------------------------------------------------------
131
136 unsigned int get_program() const;
137
141 void clear();
142
143 //---------------------- Creation ---------------------------
144
152 bool load_shader_from_file(ShaderType st, const std::string& file_name, const std::string& inc_id = "#include");
153
160 bool load_shader_from_code(ShaderType st, const std::string& code);
161
170 void set_attrib_name(ShaderProgram::AttribType at, const std::string& name) const;
175 void set_attrib_names(const std::vector<ShaderProgram::Attribute>& attributes) const;
176
181 bool link_program();
182
189 void set_program_output(int index, const std::string& name) const;
190
197 int program_output(const std::string& name) const;
198
199 //---------------------- Rendering ---------------------------
200
204 void bind() const;
205
215 ShaderProgram* set_uniform(const std::string& name, const void *value);
216
223 ShaderProgram* set_uniform(const std::string& name, int value);
224
231 ShaderProgram* set_uniform(const std::string& name, unsigned int value);
232
239 ShaderProgram* set_uniform(const std::string& name, float value);
240
247 ShaderProgram* set_block(const std::string& name, const void *value);
248
264 ShaderProgram* set_block_uniform(const std::string& blockName, const std::string& uniformName, const void *value);
265
274 ShaderProgram* set_block_uniform_array_element(const std::string& blockName, const std::string& uniformName, int arrayIndex, const void* value);
275
285 // default value is GL_TEXTURE_2D (0x0DE1, just to eliminate the inclusion of gl header file).
286 ShaderProgram* bind_texture(const std::string& name, unsigned int tex_id, int unit, unsigned int tex_target = 0x0DE1);
292 ShaderProgram* release_texture(unsigned int tex_target = 0x0DE1);
293
297 void release() const;
298
299 // ---------------------- Other info -------------------------------
300
306 int get_attribute_location(const std::string& name) const;
307
315 bool is_uniform_used(const std::string& name);
316
322 bool is_attribute_used(const std::string& name);
323
331 bool is_program_valid() const;
332
337 bool is_program_linked() const;
338
343 bool is_bound() const;
344
351 bool shader_info_log(std::string& log, unsigned int shader) const;
352
358 bool program_info_log(std::string& log) const;
359
375
376 // ---------------------- Load/Save binary -------------------------------
383 bool load_binary(const std::string& file_name);
390 bool save_binary(const std::string& file_name);
391
398 static std::string load_shader_source(const std::string& file_name, const std::string& inc_id = "#include");
399
400 protected:
401
402 // AUX STRUCTURES
403
404 // stores information for uniforms
405 struct Uniform {
406 std::string name;
407 unsigned int type;
408 int location;
409 int size;
410 unsigned int stride;
411 };
412
413 // stores information for block uniforms
414 struct BlockUniform {
415 std::string name;
416 unsigned int type;
417 unsigned int offset;
418 unsigned int size;
419 unsigned int arrayStride;
420 };
421
422 // Uniform blocks are a very convenient feature for two reasons :
423 // (1) Allow uniform sharing between programs �C set once, use many times
424 // (2) Allow setting multiple values at once
425 struct UniformBlock { // stores information for a block and its uniforms
426 std::string name;
427 // size of the uniform block
428 int size;
429 // buffer bound to the index point
430 unsigned int buffer;
431 // binding index
432 unsigned int bindingIndex;
433 // uniforms information
434 // maps of std::string can be super slow when calling find with a string literal or const char*
435 // as find forces construction/copy/destruction of a std::sting copy of the const char*.
436 std::unordered_map<std::string, BlockUniform> uniformOffsets;
437 };
438
439 // VARIABLES
440
441 // blockCount is used to assign binding indexes
442 static int spBlockCount;
443
444 // Stores info on all blocks found
445 // maps of std::string can be super slow when calling find with a string literal or const char*
446 // as find forces construction/copy/destruction of a std::sting copy of the const char*.
447 static std::unordered_map<std::string, UniformBlock> spBlocks;
448
449 // stores the OpenGL shader types
450 static unsigned int spGLShaderTypes[ShaderProgram::NUM_SHADER_TYPES];
451
452 // stores the text string related to each type
453 static std::string spStringShaderTypes[ShaderProgram::NUM_SHADER_TYPES];
454
455 // the program handle
456 unsigned int program_;
457
458 // stores info on the uniforms
459 // maps of std::string can be super slow when calling find with a string literal or const char*
460 // as find forces construction/copy/destruction of a std::sting copy of the const char*.
461 std::unordered_map<std::string, Uniform> pUniforms;
462
463 // AUX FUNCTIONS
464
465 // aux function to get info on the uniforms referenced by the shaders
466 void _add_uniforms();
467
468 // aux function to store the info of a uniform
469 void _add_uniform(const std::string& name, unsigned int type, unsigned int size);
470
471 // aux function to get info on the blocks referenced by the shaders
472 void _add_blocks();
473
474 // aux function to determine the size in bytes based on the OpenGL type
475 int _type_size(unsigned int type) const;
476
477 std::string _type_string(unsigned int type) const;
478
479 // aux function to read a previously compiled and linked program code from a file
480 std::string _read_linked_program(const std::string& file);
481
482 private:
483 std::string name_;
484 bool verbose_; // log any issues found
485
486 //copying disabled
488 ShaderProgram& operator=(const ShaderProgram&);
489
490 private:
491 friend class ShaderManager;
492 };
493
494}
495
496
497#endif // EASY3D_RENDERER_SHADER_PROGRAM_H
OpenGL Shader Compilation.
Definition shader_program.h:75
bool program_info_log(std::string &log) const
Get the program info log.
Definition shader_program.cpp:294
bool load_shader_from_file(ShaderType st, const std::string &file_name, const std::string &inc_id="#include")
Load the text in the file to the source of the specified shader.
Definition shader_program.cpp:199
void print_active_uniform_blocks()
Print active uniform blocks.
Definition shader_program.cpp:803
bool load_shader_from_code(ShaderType st, const std::string &code)
Load the code to the source of the specified shader.
Definition shader_program.cpp:209
~ShaderProgram()
Destructor.
Definition shader_program.cpp:108
void bind() const
Start using the program.
Definition shader_program.cpp:678
bool is_program_valid() const
Check if the program is valid.
Definition shader_program.cpp:639
const std::string & name() const
Get the name of the shader program.
Definition shader_program.h:122
unsigned int get_program() const
Get the program index.
Definition shader_program.cpp:337
ShaderProgram * set_uniform(const std::string &name, const void *value)
Set the uniform to value.
Definition shader_program.cpp:436
void set_program_output(int index, const std::string &name) const
Bind a user-defined varying out variable to a fragment shader color number.
Definition shader_program.cpp:311
void set_attrib_name(ShaderProgram::AttribType at, const std::string &name) const
Define semantics for the input vertex attributes. This is required for other libraries to know how to...
Definition shader_program.cpp:321
bool is_program_linked() const
Check if the program is linked.
Definition shader_program.cpp:669
void release() const
End using the program.
Definition shader_program.cpp:689
int get_attribute_location(const std::string &name) const
Get the location of an attribute.
Definition shader_program.cpp:332
ShaderType
Types of Shaders.
Definition shader_program.h:88
@ VERTEX
Vertex shader.
Definition shader_program.h:89
@ COMPUTE
Compute shader.
Definition shader_program.h:94
@ TESS_EVALUATION
Tessellation evaluation shader.
Definition shader_program.h:93
@ NUM_SHADER_TYPES
Number of shader types.
Definition shader_program.h:95
@ GEOMETRY
Geometry shader.
Definition shader_program.h:91
@ TESS_CONTROL
Tessellation control shader.
Definition shader_program.h:92
@ FRAGMENT
Fragment shader.
Definition shader_program.h:90
void print_active_uniforms()
Print active uniforms.
Definition shader_program.cpp:772
static bool is_supported()
Is shader program supported?
Definition shader_program.cpp:78
bool shader_info_log(std::string &log, unsigned int shader) const
Get the shader info log.
Definition shader_program.cpp:276
bool link_program()
Prepare program for usage. Links it and collects information about uniform variables and uniform bloc...
Definition shader_program.cpp:247
ShaderProgram(const std::string &name="unknown")
Definition shader_program.cpp:91
void set_attrib_names(const std::vector< ShaderProgram::Attribute > &attributes) const
Define semantics for multiple input vertex attributes.
Definition shader_program.cpp:326
void set_verbose(bool v)
Set verbose mode.
Definition shader_program.h:128
void print_active_attributes()
Print active attributes.
Definition shader_program.cpp:742
AttribType
Types of vertex attributes.
Definition shader_program.h:78
@ TEXCOORD
Texture coordinates.
Definition shader_program.h:82
@ NORMAL
Normal.
Definition shader_program.h:81
@ COLOR
Color.
Definition shader_program.h:80
@ POSITION
Position.
Definition shader_program.h:79
ShaderProgram * set_block(const std::string &name, const void *value)
Set a uniform block as a whole.
Definition shader_program.cpp:342
static std::string load_shader_source(const std::string &file_name, const std::string &inc_id="#include")
Auxiliary function that loads the shader source code from a file.
Definition shader_program.cpp:124
ShaderProgram * set_block_uniform_array_element(const std::string &blockName, const std::string &uniformName, int arrayIndex, const void *value)
Set an element of an array of uniforms inside a block.
Definition shader_program.cpp:376
bool is_attribute_used(const std::string &name)
Check if an attribute is used by the shader.
Definition shader_program.cpp:710
ShaderProgram * release_texture(unsigned int tex_target=0x0DE1)
Release a texture from the shader program.
Definition shader_program.cpp:703
int program_output(const std::string &name) const
Get the fragment shader color number bound to a user-defined varying out variable.
Definition shader_program.cpp:316
bool load_binary(const std::string &file_name)
Load a binary (i.e., compiled and linked) program.
Definition shader_program.cpp:1399
bool save_binary(const std::string &file_name)
Save the program to a binary file.
Definition shader_program.cpp:1447
void clear()
Remove (delete) all shaders.
Definition shader_program.cpp:113
ShaderProgram * bind_texture(const std::string &name, unsigned int tex_id, int unit, unsigned int tex_target=0x0DE1)
Bind a texture to the shader program.
Definition shader_program.cpp:694
void set_name(const std::string &name)
Set the name of the shader program.
Definition shader_program.h:117
std::pair< AttribType, std::string > Attribute
Attribute: a pair of attribute type and attribute name.
Definition shader_program.h:85
bool is_uniform_used(const std::string &name)
Check if a uniform is used by the shader.
Definition shader_program.cpp:726
bool is_bound() const
Check if the program is bound.
Definition shader_program.cpp:662
ShaderProgram * set_block_uniform(const std::string &blockName, const std::string &uniformName, const void *value)
Set a uniform inside a named block.
Definition shader_program.cpp:355
Definition collider.cpp:182