Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GLSL.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_GFX_BASIC_GLSL
41 #define GEOGRAM_GFX_BASIC_GLSL
42 
44 #include <geogram/basic/numeric.h>
45 
51 namespace GEO {
52 
53  namespace GLSL {
54 
60 
66 
73  struct GEOGRAM_GFX_API GLSLCompileError : std::exception {
74 
78  const char* what() const GEO_NOEXCEPT override;
79  };
80 
81 
88  double GEOGRAM_GFX_API supported_language_version();
89 
90  /************************************************************/
91 
97  class Source {
98  public:
102  Source() : text_(nullptr) {
103  }
104 
110  Source(const char* src) : text_(src) {
111  }
112 
118  Source(const std::string& src) : text_string_(src) {
119  text_ = text_string_.c_str();
120  }
121 
126  Source(const Source& rhs) {
127  copy(rhs);
128  }
129 
135  Source& operator=(const Source& rhs) {
136  if(&rhs != this) {
137  copy(rhs);
138  }
139  return *this;
140  }
141 
146  const char* text() {
147  return text_;
148  }
149 
150  protected:
155  void copy(const Source& rhs) {
156  if(rhs.text_string_ != "") {
157  text_string_ = rhs.text_string_;
158  text_ = text_string_.c_str();
159  } else {
160  text_ = rhs.text_;
161  }
162  }
163 
164  private:
165  const char* text_;
166  std::string text_string_;
167  };
168 
176  public:
181  };
182 
192  typedef void (*PseudoFile)(
193  PseudoFileProvider* provider, std::vector<Source>& sources
194  );
195 
196 
205  const std::string& name, const char* source
206  );
207 
218  const std::string& name, PseudoFile file
219  );
220 
221 
231  const std::string& name
232  );
233 
251  GLenum target, const char* source, PseudoFileProvider* provider
252  );
253 
254 
267  PseudoFileProvider* provider,
268  const char* shader1, const char* shader2 = nullptr, const char* shader3 = nullptr,
269  const char* shader4 = nullptr, const char* shader5 = nullptr, const char* shader6 = nullptr
270  );
271 
272 
273  /************************************************************/
274 
295  GLenum target, const char** sources, index_t nb_sources
296  );
297 
321  GLenum target,
322  const char* source1,
323  const char* source2,
324  const char* source3 = nullptr,
325  const char* source4 = nullptr,
326  const char* source5 = nullptr,
327  const char* source6 = nullptr,
328  const char* source7 = nullptr,
329  const char* source8 = nullptr,
330  const char* source9 = nullptr,
331  const char* source10 = nullptr,
332  const char* source11 = nullptr,
333  const char* source12 = nullptr,
334  const char* source13 = nullptr,
335  const char* source14 = nullptr,
336  const char* source15 = nullptr,
337  const char* source16 = nullptr,
338  const char* source17 = nullptr,
339  const char* source18 = nullptr,
340  const char* source19 = nullptr,
341  const char* source20 = nullptr
342  );
343 
344 
350  void GEOGRAM_GFX_API link_program(GLuint program);
351 
365  GLuint shader, ...
366  );
367 
378  GLuint GEOGRAM_GFX_API create_program_from_shaders(GLuint shader, ...);
379 
401  const char* string, bool copy_string = true
402  );
403 
413  const std::string& filename
414  );
415 
425  template <class T> inline bool set_program_uniform_by_name(
426  GLuint shader_id, const char* name, T val
427  ) {
428  geo_argused(shader_id);
429  geo_argused(name);
430  geo_argused(val);
432  return false;
433  }
434 
435  template<> inline bool set_program_uniform_by_name(
436  GLuint shader_id, const char* name, bool val
437  ) {
438  GLint location = glGetUniformLocation(shader_id, name) ;
439  if(location < 0) {
440  return false ;
441  }
442  glUseProgram(shader_id);
443  glUniform1i(location, val ? 1 : 0) ;
444  glUseProgram(0);
445  return true;
446  }
447 
448  template<> inline bool set_program_uniform_by_name(
449  GLuint shader_id, const char* name, float val
450  ) {
451  GLint location = glGetUniformLocation(shader_id, name) ;
452  if(location < 0) {
453  return false ;
454  }
455  glUseProgram(shader_id);
456  glUniform1f(location, val) ;
457  glUseProgram(0);
458  return true;
459  }
460 
461  template<> inline bool set_program_uniform_by_name(
462  GLuint shader_id, const char* name, double val
463  ) {
464  GLint location = glGetUniformLocation(shader_id, name) ;
465  if(location < 0) {
466  return false ;
467  }
468  glUseProgram(shader_id);
469  glUniform1f(location, float(val)) ;
470  glUseProgram(0);
471  return true;
472  }
473 
474  template<> inline bool set_program_uniform_by_name(
475  GLuint shader_id, const char* name, int val
476  ) {
477  GLint location = glGetUniformLocation(shader_id, name) ;
478  if(location < 0) {
479  return false ;
480  }
481  glUseProgram(shader_id);
482  glUniform1i(location, val) ;
483  glUseProgram(0);
484  return true;
485  }
486 
487 
488  template<> inline bool set_program_uniform_by_name(
489  GLuint shader_id, const char* name, unsigned int val
490  ) {
491  GLint location = glGetUniformLocation(shader_id, name) ;
492  if(location < 0) {
493  return false ;
494  }
495  glUseProgram(shader_id);
496 #ifdef GEO_GL_150
497  glUniform1ui(location, val) ;
498 #else
499  glUniform1i(location, GLint(val)) ;
500 #endif
501  glUseProgram(0);
502  return true;
503  }
504 
514  GLuint shader_id, const char* name, index_t count, float* values
515  ) {
516  GLint location = glGetUniformLocation(shader_id, name) ;
517  if(location < 0) {
518  return false ;
519  }
520  glUseProgram(shader_id);
521  glUniform1fv(location, GLsizei(count), values) ;
522  glUseProgram(0);
523  return true;
524  }
525 
526  inline bool set_program_uniform_by_name(
527  GLuint shader_id, const char* name, float x, float y
528  ) {
529  GLint location = glGetUniformLocation(shader_id, name) ;
530  if(location < 0) {
531  return false ;
532  }
533  glUseProgram(shader_id);
534  glUniform2f(location, x, y);
535  glUseProgram(0);
536  return true;
537  }
538 
548  GLuint program, const char* varname
549  );
550 
560  GLuint program, const char* varname
561  );
562 
568  void GEOGRAM_GFX_API introspect_program(GLuint program);
569 
570  }
571 }
572 
573 #endif
GLuint GEOGRAM_GFX_API create_program_from_shaders(GLuint shader,...)
Creates a GLSL program from a zero-terminated list of shaders.
GLuint GEOGRAM_GFX_API create_program_from_file_no_link(const std::string &filename)
Creates a GLSL program from a file.
const char * get_GLSL_include_file(const std::string &name)
Gets a GLSL include file by file name.
void GEOGRAM_GFX_API introspect_program(GLuint program)
Outputs to the logger everything that can be queried about a program using OpenGL introspection APIs.
GLuint GEOGRAM_GFX_API compile_shader(GLenum target, const char **sources, index_t nb_sources)
Compiles a shader for a specific target.
GLuint GEOGRAM_GFX_API compile_shader_with_includes(GLenum target, const char *source, PseudoFileProvider *provider)
Compiles a shader for a specific target.
GLuint GEOGRAM_GFX_API create_program_from_shaders_no_link(GLuint shader,...)
Creates a GLSL program from a zero-terminated list of shaders.
GLuint GEOGRAM_GFX_API create_program_from_string_no_link(const char *string, bool copy_string=true)
Creates a GLSL program from a string.
GLuint GEOGRAM_GFX_API compile_program_with_includes_no_link(PseudoFileProvider *provider, const char *shader1, const char *shader2=nullptr, const char *shader3=nullptr, const char *shader4=nullptr, const char *shader5=nullptr, const char *shader6=nullptr)
Compiles a program from shader sources.
size_t GEOGRAM_GFX_API get_uniform_variable_array_stride(GLuint program, const char *varname)
Queries array stride for a variable in a GLSL program using introspection.
void GEOGRAM_GFX_API link_program(GLuint program)
Links a program.
GLint GEOGRAM_GFX_API get_uniform_variable_offset(GLuint program, const char *varname)
Gets the offset of a uniform variable relative to the uniform block it is declared in.
bool set_program_uniform_by_name(GLuint shader_id, const char *name, T val)
Sets a uniform variable in a shader by name.
Definition: GLSL.h:425
void GEOGRAM_GFX_API register_GLSL_include_file(const std::string &name, const char *source)
Registers a file in the GLSL pseudo file system.
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition: assert.h:177
A class that can register functions to the GLSL pseudo file system.
Definition: GLSL.h:175
virtual ~PseudoFileProvider()
PseudoFileProvider destructor.
A GLSL source.
Definition: GLSL.h:97
void copy(const Source &rhs)
Copies a Source.
Definition: GLSL.h:155
const char * text()
Gets the text.
Definition: GLSL.h:146
Source()
Source constructor.
Definition: GLSL.h:102
Source & operator=(const Source &rhs)
Source assignment operator.
Definition: GLSL.h:135
Source(const Source &rhs)
Source copy constructor.
Definition: GLSL.h:126
Source(const std::string &src)
Source constructor from a string.
Definition: GLSL.h:118
Source(const char *src)
Source constructor from a char pointer.
Definition: GLSL.h:110
#define GEOGRAM_GFX_API
Linkage declaration for geogram symbols.
Definition: defs.h:55
Common include file, providing basic definitions. Should be included before anything else by all head...
void copy(void *to, const void *from, size_t size)
Copies a memory block.
Definition: memory.h:129
Global Vorpaline namespace.
Definition: basic.h:55
void terminate()
Cleans up Geogram.
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition: argused.h:60
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
void initialize(int flags=GEOGRAM_INSTALL_NONE)
Initialize Geogram.
Types and functions for numbers manipulation.
Exception thrown when a GLSL shader fails to compiled.
Definition: GLSL.h:73
const char * what() const GEO_NOEXCEPT override
Gets the string identifying the exception.