program_object.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Harry Storbacka
28 ** Kenneth Gangstoe
29 */
30 
31 #pragma once
32 
33 #include <memory>
34 #include "../../Core/IOData/file_system.h"
35 #include "graphic_context.h"
36 #include "uniform_vector.h"
37 
38 namespace clan
39 {
42 
43  class ShaderObject;
44  class ProgramObject_Impl;
45  class GraphicContext;
46  class GraphicContextProvider;
47  class XMLResourceDocument;
48  class ProgramObjectProvider;
49 
69  {
70  public:
72  ProgramObject();
73 
78 
83 
88 
96  static ProgramObject load(GraphicContext &gc, const std::string &resource_id, const XMLResourceDocument &resman);
97 
105  static ProgramObject load(GraphicContext &gc, const std::string &vertex_fullname, const std::string &fragment_fullname);
106 
115  static ProgramObject load(GraphicContext &gc, const std::string &vertex_fullname, const std::string &geometry_fullname, const std::string &fragment_fullname);
116 
125  static ProgramObject load(GraphicContext &gc, const std::string &vertex_filename, const std::string &fragment_filename, const FileSystem &fs);
126 
136  static ProgramObject load(GraphicContext &gc, const std::string &vertex_filename, const std::string &geometry_filename, const std::string &fragment_filename, const FileSystem &fs);
137 
145  static ProgramObject load(GraphicContext &gc, IODevice &vertex_file, IODevice &fragment_file);
146 
155  static ProgramObject load(GraphicContext &gc, IODevice &vertex_file, IODevice &geometry_file, IODevice &fragment_file);
156 
164  static ProgramObject load_and_link(GraphicContext &gc, const std::string &vertex_fullname, const std::string &fragment_fullname);
165 
174  static ProgramObject load_and_link(GraphicContext &gc, const std::string &vertex_fullname, const std::string &geometry_fullname, const std::string &fragment_fullname);
175 
184  static ProgramObject load_and_link(GraphicContext &gc, const std::string &vertex_filename, const std::string &fragment_filename, const FileSystem &fs);
185 
195  static ProgramObject load_and_link(GraphicContext &gc, const std::string &vertex_filename, const std::string &geometry_filename, const std::string &fragment_filename, const FileSystem &fs);
196 
204  static ProgramObject load_and_link(GraphicContext &gc, IODevice &vertex_file, IODevice &fragment_file);
205 
214  static ProgramObject load_and_link(GraphicContext &gc, IODevice &vertex_file, IODevice &geometry_file, IODevice &fragment_file);
215 
216  virtual ~ProgramObject();
217 
219  bool is_null() const { return !impl; }
220  explicit operator bool() const { return bool(impl); }
221 
223  void throw_if_null() const;
224 
227 
229  unsigned int get_handle() const;
230 
232  std::vector<ShaderObject> get_shaders() const;
233 
235  std::string get_info_log() const;
236 
240  int get_attribute_location(const std::string &name) const;
241 
245  int get_uniform_location(const std::string &name) const;
246 
252  int get_uniform_buffer_size(const std::string &block_name) const;
253 
255  int get_uniform_buffer_size(int block_index) const;
256 
260  int get_uniform_buffer_index(const std::string &block_name) const;
261 
265  int get_storage_buffer_index(const std::string &name) const;
266 
268  bool operator==(const ProgramObject &other) const;
269 
271  void attach(const ShaderObject &obj);
272 
274  void detach(const ShaderObject &obj);
275 
277 
278  void bind_attribute_location(int index, const std::string &name);
279 
281 
282  void bind_frag_data_location(int color_number, const std::string &name);
283 
285 
286  bool link();
287 
289 
290  bool validate();
291 
292  void set_uniform1i(int location, int value_a);
293  void set_uniform2i(int location, int value_a, int value_b);
294  void set_uniform3i(int location, int value_a, int value_b, int value_c);
295  void set_uniform4i(int location, int value_a, int value_b, int value_c, int value_d);
296  void set_uniformiv(int location, int size, int count, const int *data);
297  void set_uniform2i(int location, const Vec2i &vec) { set_uniform2i(location, vec.x, vec.y); }
298  void set_uniform3i(int location, const Vec3i &vec) { set_uniform3i(location, vec.x, vec.y, vec.z); }
299  void set_uniform4i(int location, const Vec4i &vec) { set_uniform4i(location, vec.x, vec.y, vec.z, vec.w); }
300  void set_uniform2s(int location, const Vec2s &vec) { set_uniform2i(location, vec.x, vec.y); }
301  void set_uniform3s(int location, const Vec3s &vec) { set_uniform3i(location, vec.x, vec.y, vec.z); }
302  void set_uniform4s(int location, const Vec4s &vec) { set_uniform4i(location, vec.x, vec.y, vec.z, vec.w); }
303  void set_uniform2b(int location, const Vec2b &vec) { set_uniform2i(location, vec.x, vec.y); }
304  void set_uniform3b(int location, const Vec3b &vec) { set_uniform3i(location, vec.x, vec.y, vec.z); }
305  void set_uniform4b(int location, const Vec4b &vec) { set_uniform4i(location, vec.x, vec.y, vec.z, vec.w); }
306  void set_uniformiv(int location, int count, const Vec2i *data) { set_uniformiv(location, 2, count, &data->x); }
307  void set_uniformiv(int location, int count, const Vec3i *data) { set_uniformiv(location, 3, count, &data->x); }
308  void set_uniformiv(int location, int count, const Vec4i *data) { set_uniformiv(location, 4, count, &data->x); }
309  void set_uniform1f(int location, float value_a);
310  void set_uniform2f(int location, float value_a, float value_b);
311  void set_uniform3f(int location, float value_a, float value_b, float value_c);
312  void set_uniform4f(int location, float value_a, float value_b, float value_c, float value_d);
313  void set_uniformfv(int location, int size, int count, const float *data);
314  void set_uniform2f(int location, const Vec2f &vec) { set_uniform2f(location, vec.x, vec.y); }
315  void set_uniform3f(int location, const Vec3f &vec) { set_uniform3f(location, vec.x, vec.y, vec.z); }
316  void set_uniform4f(int location, const Vec4f &vec) { set_uniform4f(location, vec.x, vec.y, vec.z, vec.w); }
317  void set_uniformfv(int location, int count, const Vec2f *data) { set_uniformfv(location, 2, count, &data->x); }
318  void set_uniformfv(int location, int count, const Vec3f *data) { set_uniformfv(location, 3, count, &data->x); }
319  void set_uniformfv(int location, int count, const Vec4f *data) { set_uniformfv(location, 4, count, &data->x); }
320  void set_uniform_matrix(int location, int size, int count, bool transpose, const float *data);
321  void set_uniform_matrix(int location, const Mat2f &matrix) { set_uniform_matrix(location, 2, 1, false, matrix.matrix); }
322  void set_uniform_matrix(int location, const Mat3f &matrix) { set_uniform_matrix(location, 3, 1, false, matrix.matrix); }
323  void set_uniform_matrix(int location, const Mat4f &matrix) { set_uniform_matrix(location, 4, 1, false, matrix.matrix); }
324  void set_uniform_matrix(int location, int count, const Mat2f *matrix) { set_uniform_matrix(location, 2, count, false, matrix->matrix); }
325  void set_uniform_matrix(int location, int count, const Mat3f *matrix) { set_uniform_matrix(location, 3, count, false, matrix->matrix); }
326  void set_uniform_matrix(int location, int count, const Mat4f *matrix) { set_uniform_matrix(location, 4, count, false, matrix->matrix); }
327 
328  void set_uniform1i(const std::string &name, int value_a) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform1i(loc, value_a); }
329  void set_uniform2i(const std::string &name, int value_a, int value_b) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform2i(loc, value_a, value_b); }
330  void set_uniform3i(const std::string &name, int value_a, int value_b, int value_c) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform3i(loc, value_a, value_b, value_c); }
331  void set_uniform4i(const std::string &name, int value_a, int value_b, int value_c, int value_d) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform4i(loc, value_a, value_b, value_c, value_d); }
332  void set_uniformiv(const std::string &name, int size, int count, const int *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformiv(loc, size, count, data); }
333  void set_uniform2i(const std::string &name, const Vec2i &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform2i(loc, vec); }
334  void set_uniform3i(const std::string &name, const Vec3i &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform3i(loc, vec); }
335  void set_uniform4i(const std::string &name, const Vec4i &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform4i(loc, vec); }
336  void set_uniform2s(const std::string &name, const Vec2s &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform2s(loc, vec); }
337  void set_uniform3s(const std::string &name, const Vec3s &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform3s(loc, vec); }
338  void set_uniform4s(const std::string &name, const Vec4s &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform4s(loc, vec); }
339  void set_uniform2b(const std::string &name, const Vec2b &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform2b(loc, vec); }
340  void set_uniform3b(const std::string &name, const Vec3b &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform3b(loc, vec); }
341  void set_uniform4b(const std::string &name, const Vec4b &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform4b(loc, vec); }
342  void set_uniformiv(const std::string &name, int count, const Vec2i *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformiv(loc, count, data); }
343  void set_uniformiv(const std::string &name, int count, const Vec3i *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformiv(loc, count, data); }
344  void set_uniformiv(const std::string &name, int count, const Vec4i *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformiv(loc, count, data); }
345  void set_uniform1f(const std::string &name, float value_a) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform1f(loc, value_a); }
346  void set_uniform2f(const std::string &name, float value_a, float value_b) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform2f(loc, value_a, value_b); }
347  void set_uniform3f(const std::string &name, float value_a, float value_b, float value_c) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform3f(loc, value_a, value_b, value_c); }
348  void set_uniform4f(const std::string &name, float value_a, float value_b, float value_c, float value_d) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform4f(loc, value_a, value_b, value_c, value_d); }
349  void set_uniformfv(const std::string &name, int size, int count, const float *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformfv(loc, size, count, data); }
350  void set_uniform2f(const std::string &name, const Vec2f &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform2f(loc, vec); }
351  void set_uniform3f(const std::string &name, const Vec3f &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform3f(loc, vec); }
352  void set_uniform4f(const std::string &name, const Vec4f &vec) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform4f(loc, vec); }
353  void set_uniformfv(const std::string &name, int count, const Vec2f *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformfv(loc, count, data); }
354  void set_uniformfv(const std::string &name, int count, const Vec3f *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformfv(loc, count, data); }
355  void set_uniformfv(const std::string &name, int count, const Vec4f *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniformfv(loc, count, data); }
356  void set_uniform_matrix(const std::string &name, int size, int count, bool transpose, const float *data) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, size, count, transpose, data); }
357  void set_uniform_matrix(const std::string &name, const Mat2f &matrix) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, matrix); }
358  void set_uniform_matrix(const std::string &name, const Mat3f &matrix) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, matrix); }
359  void set_uniform_matrix(const std::string &name, const Mat4f &matrix) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, matrix); }
360  void set_uniform_matrix(const std::string &name, int count, const Mat2f *matrix) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, count, matrix); }
361  void set_uniform_matrix(const std::string &name, int count, const Mat3f *matrix) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, count, matrix); }
362  void set_uniform_matrix(const std::string &name, int count, const Mat4f *matrix) { int loc = get_uniform_location(name); if (loc >= 0) set_uniform_matrix(loc, count, matrix); }
363 
365  void set_uniform_buffer_index(const std::string &block_name, int bind_index);
366 
368  void set_uniform_buffer_index(int block_index, int bind_index);
369 
371  void set_storage_buffer_index(const std::string &block_name, int bind_index);
372 
374  void set_storage_buffer_index(int block_index, int bind_index);
375 
376  private:
377  std::shared_ptr<ProgramObject_Impl> impl;
378  };
379 
381 }
void attach(const ShaderObject &obj)
Add shader to program object.
void set_uniform_matrix(const std::string &name, int count, const Mat3f *matrix)
Definition: program_object.h:361
void set_uniform2b(const std::string &name, const Vec2b &vec)
Definition: program_object.h:339
void set_uniform_matrix(int location, const Mat3f &matrix)
Definition: program_object.h:322
void set_uniform4f(int location, const Vec4f &vec)
Definition: program_object.h:316
Shader Object.
Definition: shader_object.h:67
Definition: clanapp.h:35
Program Object provider.
Definition: program_object_provider.h:42
void set_uniform2i(int location, int value_a, int value_b)
void set_uniform3i(const std::string &name, const Vec3i &vec)
Definition: program_object.h:334
void set_uniform4f(const std::string &name, const Vec4f &vec)
Definition: program_object.h:352
void throw_if_null() const
Throw an exception if this object is invalid.
void set_uniform4i(int location, const Vec4i &vec)
Definition: program_object.h:299
4D vector
Definition: size.h:47
void set_uniform2s(int location, const Vec2s &vec)
Definition: program_object.h:300
void set_uniformfv(const std::string &name, int count, const Vec3f *data)
Definition: program_object.h:354
void set_uniform_matrix(int location, int count, const Mat4f *matrix)
Definition: program_object.h:326
void set_uniform1i(int location, int value_a)
I/O Device interface.
Definition: iodevice.h:49
Interface for implementing a GraphicContext target.
Definition: graphic_context_provider.h:85
void set_uniform2i(int location, const Vec2i &vec)
Definition: program_object.h:297
Type z
Definition: vec4.h:81
void set_uniform_matrix(const std::string &name, const Mat3f &matrix)
Definition: program_object.h:358
void set_uniform2f(const std::string &name, float value_a, float value_b)
Definition: program_object.h:346
static ProgramObject load_and_link(GraphicContext &gc, const std::string &vertex_fullname, const std::string &fragment_fullname)
Load and link.
ProgramObjectProvider * get_provider() const
Retrieves the provider.
void set_uniformiv(int location, int count, const Vec2i *data)
Definition: program_object.h:306
int get_uniform_location(const std::string &name) const
Returns the location of a named uniform variable.
void set_uniform3f(const std::string &name, float value_a, float value_b, float value_c)
Definition: program_object.h:347
bool validate()
Validate program.
void set_uniform3f(const std::string &name, const Vec3f &vec)
Definition: program_object.h:351
void set_uniform_matrix(const std::string &name, const Mat2f &matrix)
Definition: program_object.h:357
void set_uniform_matrix(const std::string &name, int count, const Mat2f *matrix)
Definition: program_object.h:360
void set_uniform3b(const std::string &name, const Vec3b &vec)
Definition: program_object.h:340
void set_uniform_matrix(int location, int count, const Mat2f *matrix)
Definition: program_object.h:324
void set_uniform4b(const std::string &name, const Vec4b &vec)
Definition: program_object.h:341
void set_uniformfv(int location, int size, int count, const float *data)
void set_uniform2f(int location, float value_a, float value_b)
void set_uniform3s(const std::string &name, const Vec3s &vec)
Definition: program_object.h:337
virtual ~ProgramObject()
Type y
Definition: vec4.h:80
void set_uniformiv(const std::string &name, int count, const Vec2i *data)
Definition: program_object.h:342
void set_uniform_matrix(int location, int count, const Mat3f *matrix)
Definition: program_object.h:325
Type y
Definition: vec3.h:80
void set_uniform2f(int location, const Vec2f &vec)
Definition: program_object.h:314
void set_uniformfv(const std::string &name, int size, int count, const float *data)
Definition: program_object.h:349
void set_uniformiv(int location, int size, int count, const int *data)
void set_uniform3i(int location, const Vec3i &vec)
Definition: program_object.h:298
Type matrix[4]
The matrix (in column-major format)
Definition: mat2.h:177
int get_attribute_location(const std::string &name) const
Returns the location of a named active attribute.
int get_uniform_buffer_index(const std::string &block_name) const
Get the uniform block index.
void set_uniformfv(const std::string &name, int count, const Vec4f *data)
Definition: program_object.h:355
bool operator==(const ProgramObject &other) const
Equality operator.
void set_uniform2i(const std::string &name, int value_a, int value_b)
Definition: program_object.h:329
void detach(const ShaderObject &obj)
Remove shader from program object.
4D matrix
Definition: mat2.h:50
2D matrix
Definition: mat2.h:44
void set_uniform4s(int location, const Vec4s &vec)
Definition: program_object.h:302
Type y
Definition: vec2.h:81
Virtual File System (VFS).
Definition: file_system.h:46
Type z
Definition: vec3.h:81
void set_uniform4f(const std::string &name, float value_a, float value_b, float value_c, float value_d)
Definition: program_object.h:348
Type w
Definition: vec4.h:82
void bind_attribute_location(int index, const std::string &name)
Bind attribute to specific location.
void set_uniform3f(int location, float value_a, float value_b, float value_c)
void set_uniform2s(const std::string &name, const Vec2s &vec)
Definition: program_object.h:336
int get_storage_buffer_index(const std::string &name) const
Get the storage buffer index.
void set_uniformiv(const std::string &name, int count, const Vec4i *data)
Definition: program_object.h:344
void set_uniform_matrix(const std::string &name, int size, int count, bool transpose, const float *data)
Definition: program_object.h:356
void set_uniform1f(int location, float value_a)
void set_uniform1i(const std::string &name, int value_a)
Definition: program_object.h:328
void set_uniform4b(int location, const Vec4b &vec)
Definition: program_object.h:305
void set_uniform3s(int location, const Vec3s &vec)
Definition: program_object.h:301
void set_uniform_matrix(const std::string &name, int count, const Mat4f *matrix)
Definition: program_object.h:362
void set_uniformiv(int location, int count, const Vec4i *data)
Definition: program_object.h:308
bool link()
Link program.
void set_uniform2f(const std::string &name, const Vec2f &vec)
Definition: program_object.h:350
Type matrix[16]
The matrix (in column-major format)
Definition: mat4.h:343
void set_uniform4f(int location, float value_a, float value_b, float value_c, float value_d)
std::vector< ShaderObject > get_shaders() const
Returns the shaders attached to the program object.
Interface to drawing graphics.
Definition: graphic_context.h:256
void set_uniform3f(int location, const Vec3f &vec)
Definition: program_object.h:315
bool is_null() const
Returns true if this object is invalid.
Definition: program_object.h:219
void set_uniform1f(const std::string &name, float value_a)
Definition: program_object.h:345
void set_uniform2b(int location, const Vec2b &vec)
Definition: program_object.h:303
Type x
Definition: vec3.h:79
Program Object.
Definition: program_object.h:68
void set_uniformiv(const std::string &name, int size, int count, const int *data)
Definition: program_object.h:332
void set_uniform4i(const std::string &name, const Vec4i &vec)
Definition: program_object.h:335
void set_uniform4s(const std::string &name, const Vec4s &vec)
Definition: program_object.h:338
void set_uniformfv(int location, int count, const Vec3f *data)
Definition: program_object.h:318
void set_uniformfv(int location, int count, const Vec4f *data)
Definition: program_object.h:319
void set_storage_buffer_index(const std::string &block_name, int bind_index)
Sets the UniformBuffer.
void set_uniform3i(const std::string &name, int value_a, int value_b, int value_c)
Definition: program_object.h:330
void set_uniformfv(const std::string &name, int count, const Vec2f *data)
Definition: program_object.h:353
Type x
Definition: vec4.h:79
unsigned int get_handle() const
Returns the OpenGL program object handle.
XML Resource Document.
Definition: xml_resource_document.h:47
void set_uniform2i(const std::string &name, const Vec2i &vec)
Definition: program_object.h:333
void set_uniform_matrix(int location, const Mat2f &matrix)
Definition: program_object.h:321
Type x
Definition: vec2.h:80
void set_uniform3b(int location, const Vec3b &vec)
Definition: program_object.h:304
void set_uniformiv(int location, int count, const Vec3i *data)
Definition: program_object.h:307
int get_uniform_buffer_size(const std::string &block_name) const
Get the uniform block size.
void set_uniform_matrix(int location, const Mat4f &matrix)
Definition: program_object.h:323
void set_uniformiv(const std::string &name, int count, const Vec3i *data)
Definition: program_object.h:343
ProgramObject()
Construct a null instance.
void set_uniform_matrix(const std::string &name, const Mat4f &matrix)
Definition: program_object.h:359
Type matrix[9]
The matrix (in column-major format)
Definition: mat3.h:270
std::string get_info_log() const
Returns the current info log for the program object.
void set_uniform4i(const std::string &name, int value_a, int value_b, int value_c, int value_d)
Definition: program_object.h:331
void set_uniform3i(int location, int value_a, int value_b, int value_c)
void set_uniform_matrix(int location, int size, int count, bool transpose, const float *data)
void set_uniform4i(int location, int value_a, int value_b, int value_c, int value_d)
static ProgramObject load(GraphicContext &gc, const std::string &resource_id, const XMLResourceDocument &resman)
Load.
void set_uniform_buffer_index(const std::string &block_name, int bind_index)
Sets the UniformBuffer.
void set_uniformfv(int location, int count, const Vec2f *data)
Definition: program_object.h:317
void bind_frag_data_location(int color_number, const std::string &name)
Bind shader out variable a specific color buffer location.