DI-Guy SDK Documentation  13.7.1
diguyGraphicsShaderProgram.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (c) 2022 MAK Technologies, Inc.
3  * All rights reserved.
4  ****************************************************************************/
5 
6 /*********************************************************************/
13 #pragma once
14 
15 #ifdef SWIG
17 #else
18 #define CPLUSPLUS_ONLY
19 #endif
20 
21 #ifdef CPLUSPLUS_ONLY
22 #include <declspec_diguy.h>
23 #endif
24 #include <diguy_constants.h>
25 #include <stdlib.h>
26 
27 class diguyGraphicsLink;
30 class bdiShaderProgram;
32 class diguyViewFog;
35 
36 
37 /****************************************************************************/
38 /*l
39  *b Description:
40  **
41  ** This enumeration lists the quality level of various shader programs
42  ** that DI-Guy might create.
43  **
44  ** int diguyApp::set_max_character_shader_quality_level(int quality_level);
45  ** int diguyApp::set_max_scene_object_shader_quality_level(int quality_level);
46  */
47 typedef enum
48 {
56 
57 
58 /****************************************************************************/
59 /*l
60  *b Description:
61  **
62  ** This enumeration lists the types of shader programs that DI-Guy
63  ** might request.
64  **
65  *i DIGUY_GRAPHICS_SHADER_PROGRAM_TYPE_GENERAL
66  **
67  *i DIGUY_GRAPHICS_SHADER_PROGRAM_TYPE_SKIN
68  **
69  *i DIGUY_GRAPHICS_SHADER_PROGRAM_TYPE_REFLECTOR
70  **
71  */
72 typedef enum
73 {
78 
80 
81 
100 class BDI_DECLSPEC_diguy diguyGraphicsShaderProgram
101 {
102 
103 public:
104 
105 /*****************************************************************************/
118  /*l
119  *b Returns:
120  **
121  ** name of the shader This pointer will never be NULL.
122  */
123  const char* get_name();
124 
125  /*l
126  *b Returns:
127  **
128  ** the vertex filename of the shader This pointer will never be NULL.
129  */
130  const char* get_vertex_shader_filename();
131 
132  /*l
133  *b Returns:
134  **
135  ** the pixel shader filename of the shader This pointer will never be NULL.
136  */
137  const char* get_pixel_shader_filename();
138 
139  /*l
140  *b Returns:
141  **
142  ** program type of the shader; see diguyGraphicsShaderProgramType
143  */
144  diguyGraphicsShaderProgramType get_shader_type();
145 
146  /*l
147  *b Returns:
148  **
149  ** shader version
150  */
151  const char* get_shader_version();
152 
153  /*l
154  *b Returns:
155  **
156  ** the quality level type of the shader; see
157  ** diguyGraphicsShaderQualityLevel
158  */
159  diguyGraphicsShaderQualityLevel get_quality_level();
160 
161  /*l
162  *b Description:
163  **
164  ** This function derives filenames for shader programs from the
165  ** provided information.
166  **
167  *b Arguments:
168  **
169  *a result - string buffer in which result should be placed
170  *a result_max_len - size of supplied string buffer; too small will
171  *a result in a truncated filename
172  *a shader_base_name - base name of shader program; e.g. "diguy_character"
173  *a shader_language - language of shader program; e.g. "glsl" or
174  *a "hlsl"
175  *a shader_type - shader program type; e.g. "vert" or "frag"
176  *a include_diguy_default_shader_directory - pass 1 to include the
177  *a default DI-Guy shader directory in the filename;
178  *a pass 0 to only return the filepath relative to
179  *a the shader directory (this will include
180  *a shader_subdirectory, if given).
181  **
182  ** The constructed filename has the format
183  ** "(shader_base_name)_(shader_language).(shader_type)"; e.g.,
184  ** "diguy_character_glsl.vert".
185  **
186  ** The DI-Guy shader directory defaults to $DIGUY/geometry/shaders. This
187  ** can be overridden by setting the DIGUY_GEOMETRY_SHADERS environment variable.
188  **
189  *b Returns:
190  **
191  ** filename result in the result argument
192  */
193  static void derive_diguy_shader_source_filename( char* result, int result_max_len,
194  const char* shader_base_name, const char* shader_language, const char* shader_type,
195  int include_diguy_default_shader_directory, const char* shader_subdirectory = NULL );
196 
197  /*l
198  *b Description:
199  **
200  ** Prints information about the shader to the log.
201  */
202  void print_shader_info_to_log();
203 
204 /*****************************************************************************/
209  /*l
210  *b Description:
211  ** Forces the underlying shader to load. Returns 0 on success.
212  */
213  int load();
214 
216  virtual int reload();
217 
221  int get_use_shared_library();
222  void set_use_shared_library(int value);
223 
226  void set_vertex_shader_source_code(const char * shader_source);
227 
230  void set_pixel_shader_source_code(const char * shader_source);
231 
232 /*****************************************************************************/
237  /*l
238  *b Description:
239  **
240  ** Sets a bit mask consisting of diguyTextureMapMasks or'd together
241  ** to specify what textures should bind when this shader is active.
242  **
243  ** See $DIGUY/programming_examples/diguy_graphics_api/ogl_examples/common/diguyOglGraphicsShaderProgram.cpp
244  ** for an example of usage. By default in the graphics API we attempt
245  ** to bind all textures.
246  */
247  int set_textures_used_bitmask(int texture_bitmask);
248 
249  /*l
250  *b Returns:
251  **
252  ** the current or'd together list of diguyTextureMapMasks that this
253  ** shader attempts to bind. Defaults to
254  ** DIGUY_TEXTURES_MAP_MASK_ALL (0xffffffff)
255  */
256  int get_textures_used_bitmask();
257 
258 
259 /*****************************************************************************/
271 #ifdef CPLUSPLUS_ONLY
272 
273  /*l
274  *b Description:
275  **
276  ** This function will be called by DI-Guy when it is time for the
277  ** vertex part of this shader to be loaded and compiled. The utility
278  ** function derive_diguy_shader_source_filename() can be used to
279  ** convert the passed filename_base to an actual filename.
280  **
281  ** For example, assume that m_vertex_shader_filename is a character
282  ** array of 512 bytes. The following function call would derive an
283  ** appropriate vertex shader filename:
284  **
285  *e derive_diguy_shader_source_filename(m_vertex_shader_filename,
286  *e 511,
287  *e filename_base,
288  *e "glsl",
289  *e "vert",
290  *e 1);
291  **
292  ** This function is called during the Build Stage.
293  **
294  *b Returns:
295  **
296  ** 0 to indicate load was successful, -1 otherwise
297  **
298  *i Immediate Mode:
299  **
300  ** Immediate mode renderers usually do override this function.
301  **
302  *i Scene Graph:
303  **
304  ** Scene graph renderers usually do override this function.
305  **
306  *b Callable From:
307  **
308  *- - N/A (automatically called by DI-Guy Graphics API during
309  *- the Build Stage)
310  */
311  virtual int load_vertex_shader_source(const char* filename_base);
312 
313  /*l
314  *b Description:
315  **
316  ** This function is the pixel shader (AKA fragment shader in OpenGL)
317  ** equivalent of load_vertex_shader_source().
318  */
319  virtual int load_pixel_shader_source(const char* filename_base);
320 
321 
322  /*l
323  *b Description:
324  **
325  ** This function will be called by DI-Guy when it is time for the
326  ** shader program to be linked.
327  **
328  ** This function is called during the Build Stage after the
329  ** load_vertex_shader_source() and load_pixel_shader_source()
330  ** functions.
331  **
332  ** Once the link has successfully completed shader variable bind
333  ** indices can be looked up.
334  **
335  *b Returns:
336  **
337  ** 0 to indicate link was successful, -1 otherwise
338  **
339  *i Immediate Mode:
340  **
341  ** Immediate mode renderers usually do override this function.
342  **
343  *i Scene Graph:
344  **
345  ** Scene graph renderers can override this function.
346  **
347  *b Callable From:
348  **
349  *- - N/A (automatically called by DI-Guy Graphics API during
350  *- the Build Stage)
351  */
352  virtual int link();
353 
354  /*l
355  *b Description:
356  **
357  ** Returns whether the shader has been loaded. Note that because a
358  ** shader has been loaded doesn't mean that it is valid. See
359  ** get_valid().
360  **
361  *b Returns:
362  **
363  ** 1 if shader has been loaded; 0 if not
364  */
365  virtual int get_loaded();
366 
367  /*l
368  *b Description:
369  **
370  ** Returns whether the shader has been linked. Note that this only
371  ** means that it is been attempted to link the shader. The link may
372  ** have failed, and the shader not be valid. See get_valid().
373  **
374  *b Returns:
375  **
376  ** 1 if shader has been linked; 0 if not
377  */
378  virtual int get_linked();
379 
380  /*l
381  *b Description:
382  **
383  ** Returns whether shader is valid. An invalid shader will not work
384  ** correctly, leaving graphics that use this shader unshaded. What
385  ** this will look like visually in undetermined.
386  **
387  *b Returns:
388  **
389  ** 1 if shader is valid; 0 if not
390  */
391  virtual int get_valid();
392 
393  /*l
394  *b Description:
395  **
396  ** This function will be called by DI-Guy when this program is being
397  ** bound for rendering.
398  **
399  ** It is during this function call that program level shader
400  ** variables should be set. Program level variables will affect
401  ** *all* shader operations in which this program is active.
402  ** Variables that should affect specific instances of a shader should
403  ** be set in diguyGraphicsShaderInstance::bind().
404  **
405  ** One example in the DI-Guy reference shaders is the
406  ** ufrm_textures_enabled variable. Setting it to 0 will prevent
407  ** textures from being applied during the shader program run.
408  **
409  ** This happens during the Draw Stage.
410  **
411  *i Immediate Mode:
412  **
413  ** Immediate mode renderers can override this function.
414  **
415  *i Scene Graph:
416  **
417  ** Scene graph renderers usually do not override this function; the
418  ** renderer will typically do this internally. If shader variables
419  ** need to be bound, however, this function should be overridden.
420  **
421  *b Callable From:
422  **
423  *- - N/A (automatically called by DI-Guy Graphics API during
424  *- the Draw Stage)
425  */
426  virtual int bind();
427 
428  /*l
429  *b Description:
430  **
431  ** In general variables that were set in bind() should be unset or
432  ** unbound here.
433  **
434  ** This happens during the Draw Stage.
435  **
436  *i Immediate Mode:
437  **
438  ** Immediate mode renderers can override this function.
439  **
440  *i Scene Graph:
441  **
442  ** Scene graph renderers usually do not override this function; the
443  ** renderer will typically do this internally. If the bind()
444  ** function was overridden, however, this function probably should
445  ** be, too.
446  **
447  *b Callable From:
448  **
449  *- - N/A (automatically called by DI-Guy Graphics API during
450  *- the Draw Stage)
451  */
452  virtual int unbind();
453 
454 
455  /*l
456  *b Description:
457  **
458  ** This function will be called by DI-Guy when the matrix data of
459  ** a character's links needs to be sent to the graphics card.
460  **
461  ** For GLSL-based shaders, these values are typically passed into
462  ** the vertex shader via uniform variables.
463  **
464  ** Note that the format of the data in mat_data will be affected by
465  ** the bdiGraphicsInitGraphicsAPI variable
466  ** transpose_shader_matrices value.
467  **
468  *i Immediate Mode:
469  **
470  ** Immediate mode renderers usually do override this function.
471  **
472  *i Scene Graph:
473  **
474  ** Scene graph renderers usually do not override this function,
475  ** but instead use diguyGraphicsShape::get_shader_matrix_data() during the Update Stage.
476  **
477  *b Callable From:
478  **
479  *- - N/A (automatically called by DI-Guy Graphics API during
480  *- the Draw Stage)
481  */
482  virtual int bind_link_matrices(int num_matrices,
483  const float* mat_data);
484 
485  /*l
486  *b Description:
487  **
488  ** This function will be called by DI-Guy when the color data of
489  ** a character's links needs to be sent to the graphics card.
490  **
491  ** For GLSL-based shaders, these values are typically passed into
492  ** the vertex shader via uniform variables.
493  **
494  *b Callable From:
495  **
496  *- - N/A (automatically called by DI-Guy Graphics API during
497  *- the Draw Stage)
498  */
499  virtual int bind_color_array(int num_colors,
500  const float* color_data);
501 
502  /*l
503  *b Description:
504  **
505  ** These functions will be called by DI-Guy when there is a shader
506  ** uniform that needs to be updated and sent to the graphics card.
507  **
508  ** For GLSL-based shaders, these values are typically passed into the
509  ** shader via uniform variables. This function will get repeatedly
510  ** invoked if a character is using per shape materials. See
511  ** diguyGraphicsShape::set_material_parameter_4f() for more
512  ** details.
513  **
514  *b Callable From:
515  **
516  *- - N/A (automatically called by DI-Guy Graphics API during
517  *- the Draw Stage)
518  **
519  *b Arguments:
520  **
521  *a location - the shader uniform to feed this data to, this is the
522  *a value that was retrieved via a call to get_ufrm_bind_location()
523  *a name - string representation of the uniform, useful for debugging
524  *a value_size - number of ints or floats to send to the video card
525  *a values - pointer to ints or floats to send to the video card
526  */
527  virtual int bind_uniform_with_location(int location,
528  const char* name,
529  int value_size,
530  float* float_values);
531 
532  virtual int bind_uniform_with_location(int location,
533  const char* name,
534  int value_size,
535  int* int_values);
536 
537 
538  /*l
539  *b Description:
540  **
541  ** This function will be called by DI-Guy when core code is attempting
542  ** to identify the capabilities of the end users shader. Currently
543  ** attribute locations are unused. Return -1 to disable any
544  ** functionality.
545  **
546  */
547  virtual int get_attrib_bind_location(const char* name);
548 
549  /*l
550  *b Description:
551  **
552  ** This function will be called by DI-Guy when core code is attempting
553  ** to identify the capabilities of the end users shader. This needs
554  ** to be implemented to enable per shape advanced materials. Return
555  ** -1 to disable any functionality.
556  **
557  */
558  virtual int get_uniform_bind_location(const char* name);
559 
560 
561  /*l
562  *b Description:
563  **
564  ** This function will be called by DI-Guy when core code is attempting
565  ** to identify send shader low level data Return
566  ** -1 to disable any functionality.
567  **
568  */
569  virtual int get_gl_program_object();
570 
572  virtual int get_vertex_attrib_location() const;
573  virtual int get_color_attrib_location() const;
574  virtual int get_normal_attrib_location() const;
575  virtual int get_texture_attrib_location() const;
576 
577 
578 #endif
579 
580 
581 /*****************************************************************************/
590  int get_num_additional_feature_names();
591 
593  const char * get_additional_feature_name(int index);
594 
596  int has_additional_feature(const char * name);
597 
598 /*****************************************************************************/
603  /*l
604  *b Returns:
605  **
606  ** The value specified in bdiGraphicsInitGraphicsAPI.transpose_shader_matrices.
607  ** Indicates if DI-Guy will attempt to transpose 4x4 matrices before sending to
608  ** video card.
609  */
610  static int get_transposed_matrix_data();
611 
612  /*l
613  *b Returns:
614  **
615  ** The value specified in bdiGraphicsInitGraphicsAPI.use_4x3_shader_matrices.
616  ** Indicates if DI-Guy is sending 4x3 matrix data instead of 4x4 data.
617  */
618  static int get_use_4x3_shader_matrix_data();
619 
620 /*****************************************************************************/
636  int get_num_variables();
637  const char* get_variable_name_at_index(int index);
638  int has_variable(const char* name);
639  const char* get_variable_type(const char* name);
640 
641  int set_variable_float(const char* name, float value, int set_current = 1, int set_initial = 0, int create = 0);
642  int set_variable_vec2(const char* name, float x, float y, int set_current = 1, int set_initial = 0, int create = 0);
643  int set_variable_vec3(const char* name, float x, float y, float z, int set_current = 1, int set_initial = 0, int create = 0);
644  int set_variable_vec4(const char* name, float x, float y, float z, float w, int set_current = 1, int set_initial = 0, int create = 0);
645  int set_variable_int(const char* name, int value, int set_current = 1, int set_initial = 0, int create = 0);
646 
647  int get_variable_float(const char* name, float* value, int get_current = 1);
648  int get_variable_vec2(const char* name, float* x, float* y, int get_current = 1);
649  int get_variable_vec3(const char* name, float* x, float* y, float* z, int get_current = 1);
650  int get_variable_vec4(const char* name, float* r, float* g, float* b, float* a, int get_current = 1);
651  int get_variable_int(const char* name, int* value, int get_current = 1);
652 
653 
654 /****************************************************************************/
655 /****************************************************************************/
656 /****************************************************************************/
663 /****************************************************************************/
664  virtual void set_apply_matrices_flag(int apply_matrices_flag){;}
665  virtual void set_textures_enabled(int enabled){;}
667  virtual int bind_matrix(int bind_location, const float* mat){ return 0; }
668 
669 /****************************************************************************/
670 /****************************************************************************/
671 
672 /****************************************************************************/
673 /****************************************************************************/
674 
680 #ifdef CPLUSPLUS_ONLY
681 
682  bdiShaderProgram* get_internal_data() {return m_internal_data;}
683 
684 protected:
685 
686  /*l
687  ** A protected constructor. Constructors are called automatically
688  ** by DI-Guy.
689  */
690  diguyGraphicsShaderProgram(void* internal_data);
691 
692  /*l
693  ** A protected destructor. Destructors are called automatically
694  ** by DI-Guy.
695  */
696  virtual ~diguyGraphicsShaderProgram();
697 
698 private:
699 
700  /*l
701  ** A pointer to internal data.
702  */
703  bdiShaderProgram* m_internal_data;
704 
705  friend class bdiShaderProgram;
706  friend class bdiGeometryFactory;
707 #endif
708 };
This class is an abstract wrapper around shader constants for use with DI-Guy rendering.
Definition: diguyUniformBufferUpdater.h:61
A class that represents the unique per-character shader object.
Definition: diguyGraphicsShaderInstance.h:55
Definition: diguyGraphicsShaderProgram.h:47
Definition: diguyGraphicsShaderProgram.h:50
Definition: diguyGraphicsShaderProgram.h:46
Definition: diguyGraphicsShaderProgram.h:70
Definition: diguyGraphicsShaderProgram.h:51
Definition: diguyGraphicsShaderProgram.h:69
Definition: diguyGraphicsShaderProgram.h:48
A class that represents a shared material.
Definition: diguyGraphicsMaterial.h:42
Definition: diguyGraphicsShaderProgram.h:71
diguyGraphicsShaderProgramType
This enumeration lists the types of shader programs that DI-Guy might request.
Definition: diguyGraphicsShaderProgram.h:67
diguyGraphicsShaderQualityLevel
This enumeration lists the quality level of various shader programs that DI-Guy might create...
Definition: diguyGraphicsShaderProgram.h:44
This class is a simple structure for transmitting diguy lights down to the uniform buffer system this...
Definition: diguyViewLight.h:34
Describes basic fog model that is used by DI-Guy Scenario and our default ogl renderer Most users wil...
Definition: diguyViewFog.h:42
This class implements shader programs for use with DI-Guy character graphics.
Definition: diguyGraphicsShaderProgram.h:94
Definition: diguyGraphicsShaderProgram.h:72
Definition: diguyGraphicsShaderProgram.h:49