Geogram Version 1.10.2
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
50
51namespace GEO {
52
53 namespace GLSL {
54
55
61 inline void strip_trailing_null_entries(std::vector<const char*>& v) {
62 auto last = std::find_if(
63 v.rbegin(), v.rend(),
64 [](const char* p) { return p != nullptr; }
65 );
66 v.erase(last.base(), v.end());
67 for(auto p: v) {
68 geo_assert(p != nullptr);
69 }
70 }
71
77
83
90 struct GEOGRAM_GFX_API GLSLCompileError : std::exception {
91
95 const char* what() const GEO_NOEXCEPT override;
96 };
97
98
106
107 /************************************************************/
108
114 class Source {
115 public:
119 Source() : text_(nullptr) {
120 }
121
127 Source(const char* src) : text_(src) {
128 }
129
135 Source(const std::string& src) : text_string_(src) {
136 text_ = text_string_.c_str();
137 }
138
143 Source(const Source& rhs) {
144 copy(rhs);
145 }
146
152 Source& operator=(const Source& rhs) {
153 if(&rhs != this) {
154 copy(rhs);
155 }
156 return *this;
157 }
158
163 const char* text() {
164 return text_;
165 }
166
167 protected:
172 void copy(const Source& rhs) {
173 if(rhs.text_string_ != "") {
174 text_string_ = rhs.text_string_;
175 text_ = text_string_.c_str();
176 } else {
177 text_ = rhs.text_;
178 }
179 }
180
181 private:
182 const char* text_;
183 std::string text_string_;
184 };
185
193 public:
198 };
199
209 typedef void (*PseudoFile)(
210 PseudoFileProvider* provider, std::vector<Source>& sources
211 );
212
213
222 const std::string& name, const char* source
223 );
224
235 const std::string& name, PseudoFile file
236 );
237
238
248 const std::string& name
249 );
250
268 GLenum target, const char* source, PseudoFileProvider* provider
269 );
270
271
285 PseudoFileProvider* provider,
286 const std::vector<const char*>& shaders
287 );
288
301 template <typename... Args> inline
303 PseudoFileProvider* provider, Args... shaders
304 ) {
305 std::vector<const char*> shaders_vec { shaders... };
306 strip_trailing_null_entries(shaders_vec);
307 return compile_program_with_includes_no_link(provider, shaders_vec);
308 }
309
310 /************************************************************/
311
332 GLenum target, const char** sources, index_t nb_sources
333 );
334
335
355 GLenum target, const std::vector<const char*>& sources
356 );
357
358
380 template <typename... Args> inline
381 GLuint compile_shader(GLenum target, Args... args) {
382 std::vector<const char*> args_vec { args... };
384 return compile_shader(target, args_vec);
385 }
386
392 void GEOGRAM_GFX_API link_program(GLuint program);
393
407 GLuint shader, ...
408 );
409
421
443 const char* string, bool copy_string = true
444 );
445
455 const std::string& filename
456 );
457
467 template <class T> inline bool set_program_uniform_by_name(
468 GLuint shader_id, const char* name, T val
469 ) {
470 geo_argused(shader_id);
471 geo_argused(name);
472 geo_argused(val);
474 return false;
475 }
476
477 template<> inline bool set_program_uniform_by_name(
478 GLuint shader_id, const char* name, bool 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 ? 1 : 0) ;
486 glUseProgram(0);
487 return true;
488 }
489
490 template<> inline bool set_program_uniform_by_name(
491 GLuint shader_id, const char* name, float val
492 ) {
493 GLint location = glGetUniformLocation(shader_id, name) ;
494 if(location < 0) {
495 return false ;
496 }
497 glUseProgram(shader_id);
498 glUniform1f(location, val) ;
499 glUseProgram(0);
500 return true;
501 }
502
503 template<> inline bool set_program_uniform_by_name(
504 GLuint shader_id, const char* name, double val
505 ) {
506 GLint location = glGetUniformLocation(shader_id, name) ;
507 if(location < 0) {
508 return false ;
509 }
510 glUseProgram(shader_id);
511 glUniform1f(location, float(val)) ;
512 glUseProgram(0);
513 return true;
514 }
515
516 template<> inline bool set_program_uniform_by_name(
517 GLuint shader_id, const char* name, int val
518 ) {
519 GLint location = glGetUniformLocation(shader_id, name) ;
520 if(location < 0) {
521 return false ;
522 }
523 glUseProgram(shader_id);
524 glUniform1i(location, val) ;
525 glUseProgram(0);
526 return true;
527 }
528
529
530 template<> inline bool set_program_uniform_by_name(
531 GLuint shader_id, const char* name, unsigned int val
532 ) {
533 GLint location = glGetUniformLocation(shader_id, name) ;
534 if(location < 0) {
535 return false ;
536 }
537 glUseProgram(shader_id);
538#ifdef GEO_GL_140
539 glUniform1ui(location, val) ;
540#else
541 glUniform1i(location, GLint(val)) ;
542#endif
543 glUseProgram(0);
544 return true;
545 }
546
556 GLuint shader_id, const char* name, index_t count, float* values
557 ) {
558 GLint location = glGetUniformLocation(shader_id, name) ;
559 if(location < 0) {
560 return false ;
561 }
562 glUseProgram(shader_id);
563 glUniform1fv(location, GLsizei(count), values) ;
564 glUseProgram(0);
565 return true;
566 }
567
568 inline bool set_program_uniform_by_name(
569 GLuint shader_id, const char* name, float x, float y
570 ) {
571 GLint location = glGetUniformLocation(shader_id, name) ;
572 if(location < 0) {
573 return false ;
574 }
575 glUseProgram(shader_id);
576 glUniform2f(location, x, y);
577 glUseProgram(0);
578 return true;
579 }
580
590 GLuint program, const char* varname
591 );
592
602 GLuint program, const char* varname
603 );
604
611
612 }
613}
614
615#endif
GLuint GEOGRAM_GFX_API compile_program_with_includes_no_link(PseudoFileProvider *provider, const std::vector< const char * > &shaders)
Compiles a program from shader sources.
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.
double GEOGRAM_GFX_API supported_language_version()
Gets the supported GLSL language version.
void(* PseudoFile)(PseudoFileProvider *provider, std::vector< Source > &sources)
A pointer to a function registered as a pseudo file in the GLSL pseudo file system.
Definition GLSL.h:209
const char * get_GLSL_include_file(const std::string &name)
Gets a GLSL include file by file name.
void strip_trailing_null_entries(std::vector< const char * > &v)
Removes all trailing nullptr entries in a vector of strings.
Definition GLSL.h:61
void GEOGRAM_GFX_API introspect_program(GLuint program)
Outputs to the logger everything that can be queried about a program using OpenGL introspection APIs.
void GEOGRAM_GFX_API initialize()
Initializes some GLSL functions and objects.
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.
void GEOGRAM_GFX_API terminate()
Terminates GLSL functions and objects.
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:467
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:183
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:155
A class that can register functions to the GLSL pseudo file system.
Definition GLSL.h:192
virtual ~PseudoFileProvider()
PseudoFileProvider destructor.
Source & operator=(const Source &rhs)
Source assignment operator.
Definition GLSL.h:152
const char * text()
Gets the text.
Definition GLSL.h:163
void copy(const Source &rhs)
Copies a Source.
Definition GLSL.h:172
Source()
Source constructor.
Definition GLSL.h:119
Source(const Source &rhs)
Source copy constructor.
Definition GLSL.h:143
Source(const std::string &src)
Source constructor from a string.
Definition GLSL.h:135
Source(const char *src)
Source constructor from a char pointer.
Definition GLSL.h:127
#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:341
Types and functions for numbers manipulation.
Exception thrown when a GLSL shader fails to compiled.
Definition GLSL.h:90
const char * what() const GEO_NOEXCEPT override
Gets the string identifying the exception.