effect.h 4.8 KB

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