effect.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. typedef DARRAY(struct gs_effect_param) gs_effect_param_array_t;
  21. typedef DARRAY(struct pass_shaderparam) pass_shaderparam_array_t;
  22. /*
  23. * Effects introduce a means of bundling together shader text into one
  24. * file with shared functions and parameters. This is done because often
  25. * shaders must be duplicated when you need to alter minor aspects of the code
  26. * that cannot be done via constants. Effects allow developers to easily
  27. * switch shaders and set constants that can be used between shaders.
  28. *
  29. * Effects are built via the effect parser, and shaders are automatically
  30. * generated for each technique's pass.
  31. */
  32. /* ------------------------------------------------------------------------- */
  33. enum effect_section {
  34. EFFECT_PARAM,
  35. EFFECT_TECHNIQUE,
  36. EFFECT_SAMPLER,
  37. EFFECT_PASS,
  38. EFFECT_ANNOTATION
  39. };
  40. /* ------------------------------------------------------------------------- */
  41. struct gs_effect_param {
  42. char *name;
  43. enum effect_section section;
  44. enum gs_shader_param_type type;
  45. bool changed;
  46. DARRAY(uint8_t) cur_val;
  47. DARRAY(uint8_t) default_val;
  48. gs_effect_t *effect;
  49. gs_samplerstate_t *next_sampler;
  50. /*char *full_name;
  51. float scroller_min, scroller_max, scroller_inc, scroller_mul;*/
  52. gs_effect_param_array_t annotations;
  53. };
  54. static inline void effect_param_init(struct gs_effect_param *param)
  55. {
  56. memset(param, 0, sizeof(struct gs_effect_param));
  57. da_init(param->annotations);
  58. }
  59. static inline void effect_param_free(struct gs_effect_param *param)
  60. {
  61. bfree(param->name);
  62. //bfree(param->full_name);
  63. da_free(param->cur_val);
  64. da_free(param->default_val);
  65. size_t i;
  66. for (i = 0; i < param->annotations.num; i++)
  67. effect_param_free(param->annotations.array + i);
  68. da_free(param->annotations);
  69. }
  70. EXPORT void effect_param_parse_property(gs_eparam_t *param,
  71. const char *property);
  72. /* ------------------------------------------------------------------------- */
  73. struct pass_shaderparam {
  74. struct gs_effect_param *eparam;
  75. gs_sparam_t *sparam;
  76. };
  77. struct gs_effect_pass {
  78. char *name;
  79. enum effect_section section;
  80. gs_shader_t *vertshader;
  81. gs_shader_t *pixelshader;
  82. pass_shaderparam_array_t vertshader_params;
  83. pass_shaderparam_array_t pixelshader_params;
  84. };
  85. static inline void effect_pass_init(struct gs_effect_pass *pass)
  86. {
  87. memset(pass, 0, sizeof(struct gs_effect_pass));
  88. }
  89. static inline void effect_pass_free(struct gs_effect_pass *pass)
  90. {
  91. bfree(pass->name);
  92. da_free(pass->vertshader_params);
  93. da_free(pass->pixelshader_params);
  94. gs_shader_destroy(pass->vertshader);
  95. gs_shader_destroy(pass->pixelshader);
  96. }
  97. /* ------------------------------------------------------------------------- */
  98. struct gs_effect_technique {
  99. char *name;
  100. enum effect_section section;
  101. struct gs_effect *effect;
  102. DARRAY(struct gs_effect_pass) passes;
  103. };
  104. static inline void effect_technique_init(struct gs_effect_technique *t)
  105. {
  106. memset(t, 0, sizeof(struct gs_effect_technique));
  107. }
  108. static inline void effect_technique_free(struct gs_effect_technique *t)
  109. {
  110. size_t i;
  111. for (i = 0; i < t->passes.num; i++)
  112. effect_pass_free(t->passes.array + i);
  113. da_free(t->passes);
  114. bfree(t->name);
  115. }
  116. /* ------------------------------------------------------------------------- */
  117. struct gs_effect {
  118. bool processing;
  119. bool cached;
  120. char *effect_path, *effect_dir;
  121. gs_effect_param_array_t params;
  122. DARRAY(struct gs_effect_technique) techniques;
  123. struct gs_effect_technique *cur_technique;
  124. struct gs_effect_pass *cur_pass;
  125. gs_eparam_t *view_proj, *world, *scale;
  126. graphics_t *graphics;
  127. struct gs_effect *next;
  128. size_t loop_pass;
  129. bool looping;
  130. };
  131. static inline void effect_init(gs_effect_t *effect)
  132. {
  133. memset(effect, 0, sizeof(struct gs_effect));
  134. }
  135. static inline void effect_free(gs_effect_t *effect)
  136. {
  137. size_t i;
  138. for (i = 0; i < effect->params.num; i++)
  139. effect_param_free(effect->params.array + i);
  140. for (i = 0; i < effect->techniques.num; i++)
  141. effect_technique_free(effect->techniques.array + i);
  142. da_free(effect->params);
  143. da_free(effect->techniques);
  144. bfree(effect->effect_path);
  145. bfree(effect->effect_dir);
  146. effect->effect_path = NULL;
  147. effect->effect_dir = NULL;
  148. }
  149. #ifdef __cplusplus
  150. }
  151. #endif