effect.h 4.6 KB

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