effect.h 4.7 KB

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