24 #include <unordered_map>
26 #include "../../signals/signal.h"
27 #include "../../types.h"
28 #include "../../generic/property.h"
29 #include "../../generic/managed.h"
30 #include "../../utils/gl_thread_check.h"
31 #include "../../generic/identifiable.h"
32 #include "../../vertex_data.h"
34 #include "../glad/glad/glad.h"
36 #define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))
39 using smlt::ShaderType;
42 struct hash<ShaderType> {
43 size_t operator()(
const ShaderType& a)
const {
44 hash<int32_t> make_hash;
45 return make_hash(int32_t(a));
57 typedef sig::signal<void ()> ProgramLinkedSignal;
58 typedef sig::signal<void (ShaderType)> ShaderCompiledSignal;
74 GPUProgram(
const GPUProgramID&
id,
Renderer* renderer,
const std::string& vertex_source,
const std::string& fragment_source);
78 bool on_init()
override;
79 void on_clean_up()
override;
81 bool is_current()
const;
84 bool is_complete()
const;
85 bool is_compiled(ShaderType type)
const;
87 void compile(ShaderType type);
93 std::string md5() {
return md5_shader_hash_; }
97 bool is_compiled =
false;
101 const std::unordered_map<ShaderType, ShaderInfo> shader_infos()
const {
return shaders_; }
103 GLint locate_uniform(
const std::string& name,
bool fail_silently=
false);
104 GLint locate_attribute(
const std::string& name,
bool fail_silently=
false);
105 void set_uniform_location(
const std::string& name, GLint location);
106 void set_attribute_location(
const std::string& name, GLint location);
108 UniformInfo uniform_info(
const std::string& uniform_name);
111 uniform_cache_.clear();
114 void set_uniform_int(
const int32_t loc,
const int32_t value);
115 void set_uniform_mat4x4(
const int32_t loc,
const Mat4& values);
116 void set_uniform_color(
const int32_t loc,
const Color& values);
117 void set_uniform_vec4(
const int32_t loc,
const Vec4& values);
118 void set_uniform_float(
const int32_t loc,
const float value);
120 void set_uniform_int(
const std::string& uniform_name,
const int32_t value,
bool fail_silently=
false);
121 void set_uniform_float(
const std::string& uniform_name,
const float value,
bool fail_silently=
false);
122 void set_uniform_mat4x4(
const std::string& uniform_name,
const Mat4& values);
123 void set_uniform_mat3x3(
const std::string& uniform_name,
const Mat3& values);
124 void set_uniform_vec3(
const std::string& uniform_name,
const Vec3& values);
125 void set_uniform_vec4(
const std::string& uniform_name,
const Vec4& values);
126 void set_uniform_color(
const std::string& uniform_name,
const Color& values);
127 void set_uniform_mat4x4_array(
const std::string& uniform_name,
const std::vector<Mat4>& matrices);
132 needs_relink_ =
false;
136 GLuint program_object()
const {
return program_object_; }
137 void prepare_program();
139 void _set_renderer_specific_id(uint32_t
id) {
143 uint32_t _renderer_specific_id()
const {
148 friend class ::ShaderTest;
152 std::unordered_map<unicode, UniformInfo> uniform_info_;
154 void rebuild_uniform_info();
155 void set_shader_source(ShaderType type,
const std::string &source);
157 bool is_linked_ =
false;
158 bool needs_relink_ =
true;
160 uint32_t program_object_ = 0;
161 std::unordered_map<ShaderType, ShaderInfo> shaders_;
162 std::unordered_map<ShaderType, std::string> shader_hashes_;
164 ProgramLinkedSignal signal_linked_;
165 ShaderCompiledSignal signal_shader_compiled_;
169 std::string md5_shader_hash_;
171 std::unordered_map<std::string, GLint> uniform_cache_;
172 std::unordered_map<std::string, int32_t> attribute_cache_;
174 void link(
bool force=
false);
176 uint32_t renderer_id_ = 0;
181 #endif // GPU_PROGRAM_H