1
0

effect.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "effect-parser.h"
  16. #include "graphics.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*
  21. * Effects introduce a means of bundling together shader text into one
  22. * file with shared functions and parameters. This is done because often
  23. * shaders must be duplicated when you need to alter minor aspects of the code
  24. * that cannot be done via constants. Effects allow developers to easily
  25. * switch shaders and set constants that can be used between shaders.
  26. *
  27. * Effects are built via the effect parser, and shaders are automatically
  28. * generated for each technique's pass.
  29. */
  30. /* ------------------------------------------------------------------------- */
  31. enum effect_section {
  32. EFFECT_PARAM,
  33. EFFECT_TECHNIQUE,
  34. EFFECT_SAMPLER,
  35. EFFECT_PASS,
  36. EFFECT_ANNOTATION
  37. };
  38. /* ------------------------------------------------------------------------- */
  39. struct gs_effect_param {
  40. char *name;
  41. enum effect_section section;
  42. enum gs_shader_param_type type;
  43. bool changed;
  44. DARRAY(uint8_t) cur_val;
  45. DARRAY(uint8_t) default_val;
  46. gs_effect_t *effect;
  47. gs_samplerstate_t *next_sampler;
  48. /*char *full_name;
  49. float scroller_min, scroller_max, scroller_inc, scroller_mul;*/
  50. DARRAY(struct gs_effect_param) annotations;
  51. };
  52. static inline void effect_param_init(struct gs_effect_param *param)
  53. {
  54. memset(param, 0, sizeof(struct gs_effect_param));
  55. da_init(param->annotations);
  56. }
  57. static inline void effect_param_free(struct gs_effect_param *param)
  58. {
  59. bfree(param->name);
  60. //bfree(param->full_name);
  61. da_free(param->cur_val);
  62. da_free(param->default_val);
  63. size_t i;
  64. for (i = 0; i < param->annotations.num; i++)
  65. effect_param_free(param->annotations.array + i);
  66. da_free(param->annotations);
  67. }
  68. EXPORT void effect_param_parse_property(gs_eparam_t *param,
  69. const char *property);
  70. /* ------------------------------------------------------------------------- */
  71. struct pass_shaderparam {
  72. struct gs_effect_param *eparam;
  73. gs_sparam_t *sparam;
  74. };
  75. struct gs_effect_pass {
  76. char *name;
  77. enum effect_section section;
  78. gs_shader_t *vertshader;
  79. gs_shader_t *pixelshader;
  80. DARRAY(struct pass_shaderparam) vertshader_params;
  81. DARRAY(struct pass_shaderparam) pixelshader_params;
  82. };
  83. static inline void effect_pass_init(struct gs_effect_pass *pass)
  84. {
  85. memset(pass, 0, sizeof(struct gs_effect_pass));
  86. }
  87. static inline void effect_pass_free(struct gs_effect_pass *pass)
  88. {
  89. bfree(pass->name);
  90. da_free(pass->vertshader_params);
  91. da_free(pass->pixelshader_params);
  92. gs_shader_destroy(pass->vertshader);
  93. gs_shader_destroy(pass->pixelshader);
  94. }
  95. /* ------------------------------------------------------------------------- */
  96. struct gs_effect_technique {
  97. char *name;
  98. enum effect_section section;
  99. struct gs_effect *effect;
  100. DARRAY(struct gs_effect_pass) passes;
  101. };
  102. static inline void effect_technique_init(struct gs_effect_technique *t)
  103. {
  104. memset(t, 0, sizeof(struct gs_effect_technique));
  105. }
  106. static inline void effect_technique_free(struct gs_effect_technique *t)
  107. {
  108. size_t i;
  109. for (i = 0; i < t->passes.num; i++)
  110. effect_pass_free(t->passes.array + i);
  111. da_free(t->passes);
  112. bfree(t->name);
  113. }
  114. /* ------------------------------------------------------------------------- */
  115. struct gs_effect {
  116. bool processing;
  117. bool cached;
  118. char *effect_path, *effect_dir;
  119. DARRAY(struct gs_effect_param) params;
  120. DARRAY(struct gs_effect_technique) techniques;
  121. struct gs_effect_technique *cur_technique;
  122. struct gs_effect_pass *cur_pass;
  123. gs_eparam_t *view_proj, *world, *scale;
  124. graphics_t *graphics;
  125. struct gs_effect *next;
  126. size_t loop_pass;
  127. bool looping;
  128. };
  129. static inline void effect_init(gs_effect_t *effect)
  130. {
  131. memset(effect, 0, sizeof(struct gs_effect));
  132. }
  133. static inline void effect_free(gs_effect_t *effect)
  134. {
  135. size_t i;
  136. for (i = 0; i < effect->params.num; i++)
  137. effect_param_free(effect->params.array + i);
  138. for (i = 0; i < effect->techniques.num; i++)
  139. effect_technique_free(effect->techniques.array + i);
  140. da_free(effect->params);
  141. da_free(effect->techniques);
  142. bfree(effect->effect_path);
  143. bfree(effect->effect_dir);
  144. effect->effect_path = NULL;
  145. effect->effect_dir = NULL;
  146. }
  147. EXPORT void effect_upload_params(gs_effect_t *effect, bool changed_only);
  148. EXPORT void effect_upload_shader_params(gs_effect_t *effect,
  149. gs_shader_t *shader,
  150. struct darray *pass_params,
  151. bool changed_only);
  152. #ifdef __cplusplus
  153. }
  154. #endif