Geogram Version 1.9.6-rc
A programming library of geometric algorithms
Loading...
Searching...
No Matches
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
45
51namespace GEO {
52
53 namespace GLSL {
54
59 void GEOGRAM_GFX_API initialize();
60
65 void GEOGRAM_GFX_API terminate();
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
269 PseudoFileProvider* provider,
270 const char* shader1, const char* shader2 = nullptr,
271 const char* shader3 = nullptr, const char* shader4 = nullptr,
272 const char* shader5 = nullptr, const char* shader6 = nullptr
273 );
274
275
276 /************************************************************/
277
298 GLenum target, const char** sources, index_t nb_sources
299 );
300
324 GLenum target,
325 const char* source1,
326 const char* source2,
327 const char* source3 = nullptr,
328 const char* source4 = nullptr,
329 const char* source5 = nullptr,
330 const char* source6 = nullptr,
331 const char* source7 = nullptr,
332 const char* source8 = nullptr,
333 const char* source9 = nullptr,
334 const char* source10 = nullptr,
335 const char* source11 = nullptr,
336 const char* source12 = nullptr,
337 const char* source13 = nullptr,
338 const char* source14 = nullptr,
339 const char* source15 = nullptr,
340 const char* source16 = nullptr,
341 const char* source17 = nullptr,
342 const char* source18 = nullptr,
343 const char* source19 = nullptr,
344 const char* source20 = nullptr
345 );
346
347
353 void GEOGRAM_GFX_API link_program(GLuint program);
354
368 GLuint shader, ...
369 );
370
382
404 const char* string, bool copy_string = true
405 );
406
416 const std::string& filename
417 );
418
428 template <class T> inline bool set_program_uniform_by_name(
429 GLuint shader_id, const char* name, T val
430 ) {
431 geo_argused(shader_id);
432 geo_argused(name);
433 geo_argused(val);
435 return false;
436 }
437
438 template<> inline bool set_program_uniform_by_name(
439 GLuint shader_id, const char* name, bool val
440 ) {
441 GLint location = glGetUniformLocation(shader_id, name) ;
442 if(location < 0) {
443 return false ;
444 }
445 glUseProgram(shader_id);
446 glUniform1i(location, val ? 1 : 0) ;
447 glUseProgram(0);
448 return true;
449 }
450
451 template<> inline bool set_program_uniform_by_name(
452 GLuint shader_id, const char* name, float val
453 ) {
454 GLint location = glGetUniformLocation(shader_id, name) ;
455 if(location < 0) {
456 return false ;
457 }
458 glUseProgram(shader_id);
459 glUniform1f(location, val) ;
460 glUseProgram(0);
461 return true;
462 }
463
464 template<> inline bool set_program_uniform_by_name(
465 GLuint shader_id, const char* name, double val
466 ) {
467 GLint location = glGetUniformLocation(shader_id, name) ;
468 if(location < 0) {
469 return false ;
470 }
471 glUseProgram(shader_id);
472 glUniform1f(location, float(val)) ;
473 glUseProgram(0);
474 return true;
475 }
476
477 template<> inline bool set_program_uniform_by_name(
478 GLuint shader_id, const char* name, int val
479 ) {
480 GLint location = glGetUniformLocation(shader_id, name) ;
481 if(location < 0) {
482 return false ;
483 }
484 glUseProgram(shader_id);
485 glUniform1i(location, val) ;
486 glUseProgram(0);
487 return true;
488 }
489
490
491 template<> inline bool set_program_uniform_by_name(
492 GLuint shader_id, const char* name, unsigned int val
493 ) {
494 GLint location = glGetUniformLocation(shader_id, name) ;
495 if(location < 0) {
496 return false ;
497 }
498 glUseProgram(shader_id);
499#ifdef GEO_GL_150
500 glUniform1ui(location, val) ;
501#else
502 glUniform1i(location, GLint(val)) ;
503#endif
504 glUseProgram(0);
505 return true;
506 }
507
517 GLuint shader_id, const char* name, index_t count, float* values
518 ) {
519 GLint location = glGetUniformLocation(shader_id, name) ;
520 if(location < 0) {
521 return false ;
522 }
523 glUseProgram(shader_id);
524 glUniform1fv(location, GLsizei(count), values) ;
525 glUseProgram(0);
526 return true;
527 }
528
529 inline bool set_program_uniform_by_name(
530 GLuint shader_id, const char* name, float x, float y
531 ) {
532 GLint location = glGetUniformLocation(shader_id, name) ;
533 if(location < 0) {
534 return false ;
535 }
536 glUseProgram(shader_id);
537 glUniform2f(location, x, y);
538 glUseProgram(0);
539 return true;
540 }
541
551 GLuint program, const char* varname
552 );
553
563 GLuint program, const char* varname
564 );
565
572
573 }
574}
575
576#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:428
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
Source & operator=(const Source &rhs)
Source assignment operator.
Definition GLSL.h:135
const char * text()
Gets the text.
Definition GLSL.h:146
void copy(const Source &rhs)
Copies a Source.
Definition GLSL.h:155
Source()
Source constructor.
Definition GLSL.h:102
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...
Global Vorpaline namespace.
Definition basic.h:55
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
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.