Easy3D 2.5.3
shader_manager.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
27#ifndef EASY3D_RENDERER_SHADER_MANAGER_H
28#define EASY3D_RENDERER_SHADER_MANAGER_H
29
30#include <string>
31#include <unordered_map>
32
33#include <easy3d/renderer/shader_program.h>
34
35
36namespace easy3d {
37
38 class ShaderProgram;
39
46 {
47 public:
48 // return the shader program if it exists and is working, otherwise return 0.
49 // shader_name: the base name of the program's source file.
50 static ShaderProgram* get_program(const std::string& shader_name);
51
52 // create a shader program from shader source files specified by the shader file's base name.
53 static ShaderProgram* create_program_from_files(
54 const std::string& file_base_name,
55 const std::vector<ShaderProgram::Attribute>& attributes = std::vector<ShaderProgram::Attribute>(),
56 const std::vector<std::string>& outputs = std::vector<std::string>(),
57 bool geom_shader = false
58 );
59
60 // create a shader program from shader source files specified by individual file names.
61 // This function allows user providing extra code.
62 static ShaderProgram* create_program_from_files(
63 const std::string& vert_file_name,
64 const std::string& frag_file_name,
65 const std::string& geom_file_name = "",
66 const std::string& extra_vert_code = "",
67 const std::string& extra_frag_code = "",
68 const std::string& extra_geom_code = "",
69 const std::vector<ShaderProgram::Attribute>& attributes = std::vector<ShaderProgram::Attribute>(),
70 const std::vector<std::string>& outputs = std::vector<std::string>()
71 );
72
73 // create a shader program from completed shader source codes
74 static ShaderProgram* create_program_from_codes(
75 const std::string& vert_code,
76 const std::string& frag_code,
77 const std::string& geom_code = "",
78 const std::vector<ShaderProgram::Attribute>& attributes = std::vector<ShaderProgram::Attribute>(),
79 const std::vector<std::string>& outputs = std::vector<std::string>()
80 );
81
82 static std::vector<ShaderProgram*> all_programs();
83
84 // destroy all shader programs.
85 static void terminate();
86
87 static void reload();
88
89 private:
90 // maps of std::string can be super slow when calling find with a string literal or const char*
91 // as find forces construction/copy/destruction of a std::sting copy of the const char*.
92 static std::unordered_map<std::string, ShaderProgram*> programs_;
93 static std::unordered_map<std::string, bool> attempt_load_program_; // avoid multiple attempt
94 };
95
96}
97
98
99#endif // EASY3D_RENDERER_SHADER_MANAGER_H
Management of shader programs.
Definition: shader_manager.h:46
OpenGL Shader Compilation.
Definition: shader_program.h:78
Definition: collider.cpp:182