effects_rd.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* effects_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef EFFECTS_RD_H
  31. #define EFFECTS_RD_H
  32. #include "core/math/projection.h"
  33. #include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
  34. #include "servers/rendering/renderer_rd/shaders/luminance_reduce.glsl.gen.h"
  35. #include "servers/rendering/renderer_rd/shaders/luminance_reduce_raster.glsl.gen.h"
  36. #include "servers/rendering/renderer_rd/shaders/roughness_limiter.glsl.gen.h"
  37. #include "servers/rendering/renderer_rd/shaders/sort.glsl.gen.h"
  38. #include "servers/rendering/renderer_scene_render.h"
  39. #include "servers/rendering_server.h"
  40. class EffectsRD {
  41. private:
  42. bool prefer_raster_effects;
  43. enum LuminanceReduceMode {
  44. LUMINANCE_REDUCE_READ,
  45. LUMINANCE_REDUCE,
  46. LUMINANCE_REDUCE_WRITE,
  47. LUMINANCE_REDUCE_MAX
  48. };
  49. struct LuminanceReducePushConstant {
  50. int32_t source_size[2];
  51. float max_luminance;
  52. float min_luminance;
  53. float exposure_adjust;
  54. float pad[3];
  55. };
  56. struct LuminanceReduce {
  57. LuminanceReducePushConstant push_constant;
  58. LuminanceReduceShaderRD shader;
  59. RID shader_version;
  60. RID pipelines[LUMINANCE_REDUCE_MAX];
  61. } luminance_reduce;
  62. enum LuminanceReduceRasterMode {
  63. LUMINANCE_REDUCE_FRAGMENT_FIRST,
  64. LUMINANCE_REDUCE_FRAGMENT,
  65. LUMINANCE_REDUCE_FRAGMENT_FINAL,
  66. LUMINANCE_REDUCE_FRAGMENT_MAX
  67. };
  68. struct LuminanceReduceRasterPushConstant {
  69. int32_t source_size[2];
  70. int32_t dest_size[2];
  71. float exposure_adjust;
  72. float min_luminance;
  73. float max_luminance;
  74. uint32_t pad1;
  75. };
  76. struct LuminanceReduceFragment {
  77. LuminanceReduceRasterPushConstant push_constant;
  78. LuminanceReduceRasterShaderRD shader;
  79. RID shader_version;
  80. PipelineCacheRD pipelines[LUMINANCE_REDUCE_FRAGMENT_MAX];
  81. } luminance_reduce_raster;
  82. struct RoughnessLimiterPushConstant {
  83. int32_t screen_size[2];
  84. float curve;
  85. uint32_t pad;
  86. };
  87. struct RoughnessLimiter {
  88. RoughnessLimiterPushConstant push_constant;
  89. RoughnessLimiterShaderRD shader;
  90. RID shader_version;
  91. RID pipeline;
  92. } roughness_limiter;
  93. enum SortMode {
  94. SORT_MODE_BLOCK,
  95. SORT_MODE_STEP,
  96. SORT_MODE_INNER,
  97. SORT_MODE_MAX
  98. };
  99. struct Sort {
  100. struct PushConstant {
  101. uint32_t total_elements;
  102. uint32_t pad[3];
  103. int32_t job_params[4];
  104. };
  105. SortShaderRD shader;
  106. RID shader_version;
  107. RID pipelines[SORT_MODE_MAX];
  108. } sort;
  109. RID default_sampler;
  110. RID default_mipmap_sampler;
  111. RID index_buffer;
  112. RID index_array;
  113. HashMap<RID, RID> texture_to_uniform_set_cache;
  114. HashMap<RID, RID> input_to_uniform_set_cache;
  115. HashMap<RID, RID> image_to_uniform_set_cache;
  116. struct TexturePair {
  117. RID texture1;
  118. RID texture2;
  119. _FORCE_INLINE_ bool operator<(const TexturePair &p_pair) const {
  120. if (texture1 == p_pair.texture1) {
  121. return texture2 < p_pair.texture2;
  122. } else {
  123. return texture1 < p_pair.texture1;
  124. }
  125. }
  126. };
  127. struct TextureSamplerPair {
  128. RID texture;
  129. RID sampler;
  130. _FORCE_INLINE_ bool operator<(const TextureSamplerPair &p_pair) const {
  131. if (texture == p_pair.texture) {
  132. return sampler < p_pair.sampler;
  133. } else {
  134. return texture < p_pair.texture;
  135. }
  136. }
  137. };
  138. RBMap<TexturePair, RID> texture_pair_to_uniform_set_cache;
  139. RBMap<RID, RID> texture_to_compute_uniform_set_cache;
  140. RBMap<TexturePair, RID> texture_pair_to_compute_uniform_set_cache;
  141. RBMap<TexturePair, RID> image_pair_to_compute_uniform_set_cache;
  142. RBMap<TextureSamplerPair, RID> texture_sampler_to_compute_uniform_set_cache;
  143. RID _get_uniform_set_from_image(RID p_texture);
  144. RID _get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps = false);
  145. RID _get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps = false);
  146. public:
  147. bool get_prefer_raster_effects();
  148. void luminance_reduction(RID p_source_texture, const Size2i p_source_size, const Vector<RID> p_reduce, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set = false);
  149. void luminance_reduction_raster(RID p_source_texture, const Size2i p_source_size, const Vector<RID> p_reduce, Vector<RID> p_fb, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set = false);
  150. void roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve);
  151. void sort_buffer(RID p_uniform_set, int p_size);
  152. EffectsRD(bool p_prefer_raster_effects);
  153. ~EffectsRD();
  154. };
  155. #endif // EFFECTS_RD_H