effect.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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 { EFFECT_PARAM, EFFECT_TECHNIQUE, EFFECT_SAMPLER, EFFECT_PASS, EFFECT_ANNOTATION };
  34. /* ------------------------------------------------------------------------- */
  35. struct gs_effect_param {
  36. char *name;
  37. enum effect_section section;
  38. enum gs_shader_param_type type;
  39. bool changed;
  40. DARRAY(uint8_t) cur_val;
  41. DARRAY(uint8_t) default_val;
  42. gs_effect_t *effect;
  43. gs_samplerstate_t *next_sampler;
  44. /*char *full_name;
  45. float scroller_min, scroller_max, scroller_inc, scroller_mul;*/
  46. gs_effect_param_array_t annotations;
  47. };
  48. static inline void effect_param_init(struct gs_effect_param *param)
  49. {
  50. memset(param, 0, sizeof(struct gs_effect_param));
  51. da_init(param->annotations);
  52. }
  53. static inline void effect_param_free(struct gs_effect_param *param)
  54. {
  55. bfree(param->name);
  56. //bfree(param->full_name);
  57. da_free(param->cur_val);
  58. da_free(param->default_val);
  59. size_t i;
  60. for (i = 0; i < param->annotations.num; i++)
  61. effect_param_free(param->annotations.array + i);
  62. da_free(param->annotations);
  63. }
  64. EXPORT void effect_param_parse_property(gs_eparam_t *param, const char *property);
  65. /* ------------------------------------------------------------------------- */
  66. struct pass_shaderparam {
  67. struct gs_effect_param *eparam;
  68. gs_sparam_t *sparam;
  69. };
  70. struct gs_effect_pass {
  71. char *name;
  72. enum effect_section section;
  73. gs_shader_t *vertshader;
  74. gs_shader_t *pixelshader;
  75. pass_shaderparam_array_t vertshader_params;
  76. pass_shaderparam_array_t pixelshader_params;
  77. };
  78. static inline void effect_pass_init(struct gs_effect_pass *pass)
  79. {
  80. memset(pass, 0, sizeof(struct gs_effect_pass));
  81. }
  82. static inline void effect_pass_free(struct gs_effect_pass *pass)
  83. {
  84. bfree(pass->name);
  85. da_free(pass->vertshader_params);
  86. da_free(pass->pixelshader_params);
  87. gs_shader_destroy(pass->vertshader);
  88. gs_shader_destroy(pass->pixelshader);
  89. }
  90. /* ------------------------------------------------------------------------- */
  91. struct gs_effect_technique {
  92. char *name;
  93. enum effect_section section;
  94. struct gs_effect *effect;
  95. DARRAY(struct gs_effect_pass) passes;
  96. };
  97. static inline void effect_technique_init(struct gs_effect_technique *t)
  98. {
  99. memset(t, 0, sizeof(struct gs_effect_technique));
  100. }
  101. static inline void effect_technique_free(struct gs_effect_technique *t)
  102. {
  103. size_t i;
  104. for (i = 0; i < t->passes.num; i++)
  105. effect_pass_free(t->passes.array + i);
  106. da_free(t->passes);
  107. bfree(t->name);
  108. }
  109. /* ------------------------------------------------------------------------- */
  110. struct gs_effect {
  111. bool processing;
  112. bool cached;
  113. char *effect_path, *effect_dir;
  114. gs_effect_param_array_t params;
  115. DARRAY(struct gs_effect_technique) techniques;
  116. struct gs_effect_technique *cur_technique;
  117. struct gs_effect_pass *cur_pass;
  118. gs_eparam_t *view_proj, *world, *scale;
  119. graphics_t *graphics;
  120. struct gs_effect *next;
  121. size_t loop_pass;
  122. bool looping;
  123. };
  124. static inline void effect_init(gs_effect_t *effect)
  125. {
  126. memset(effect, 0, sizeof(struct gs_effect));
  127. }
  128. static inline void effect_free(gs_effect_t *effect)
  129. {
  130. size_t i;
  131. for (i = 0; i < effect->params.num; i++)
  132. effect_param_free(effect->params.array + i);
  133. for (i = 0; i < effect->techniques.num; i++)
  134. effect_technique_free(effect->techniques.array + i);
  135. da_free(effect->params);
  136. da_free(effect->techniques);
  137. bfree(effect->effect_path);
  138. bfree(effect->effect_dir);
  139. effect->effect_path = NULL;
  140. effect->effect_dir = NULL;
  141. }
  142. #ifdef __cplusplus
  143. }
  144. #endif