effects_rd.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*************************************************************************/
  2. /* effects_rd.cpp */
  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. #include "effects_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/math/math_defs.h"
  33. #include "core/os/os.h"
  34. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  35. #include "thirdparty/misc/cubemap_coeffs.h"
  36. bool EffectsRD::get_prefer_raster_effects() {
  37. return prefer_raster_effects;
  38. }
  39. RID EffectsRD::_get_uniform_set_from_image(RID p_image) {
  40. if (image_to_uniform_set_cache.has(p_image)) {
  41. RID uniform_set = image_to_uniform_set_cache[p_image];
  42. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  43. return uniform_set;
  44. }
  45. }
  46. Vector<RD::Uniform> uniforms;
  47. RD::Uniform u;
  48. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  49. u.binding = 0;
  50. u.append_id(p_image);
  51. uniforms.push_back(u);
  52. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  53. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  54. image_to_uniform_set_cache[p_image] = uniform_set;
  55. return uniform_set;
  56. }
  57. RID EffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  58. if (texture_to_uniform_set_cache.has(p_texture)) {
  59. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  60. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  61. return uniform_set;
  62. }
  63. }
  64. Vector<RD::Uniform> uniforms;
  65. RD::Uniform u;
  66. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  67. u.binding = 0;
  68. u.append_id(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  69. u.append_id(p_texture);
  70. uniforms.push_back(u);
  71. // anything with the same configuration (one texture in binding 0 for set 0), is good
  72. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce_raster.shader.version_get_shader(luminance_reduce_raster.shader_version, 0), 0);
  73. texture_to_uniform_set_cache[p_texture] = uniform_set;
  74. return uniform_set;
  75. }
  76. RID EffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  77. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  78. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  79. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  80. return uniform_set;
  81. }
  82. }
  83. Vector<RD::Uniform> uniforms;
  84. RD::Uniform u;
  85. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  86. u.binding = 0;
  87. u.append_id(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  88. u.append_id(p_texture);
  89. uniforms.push_back(u);
  90. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  91. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  92. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  93. return uniform_set;
  94. }
  95. void EffectsRD::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) {
  96. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of luminance reduction with the mobile renderer.");
  97. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  98. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  99. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  100. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  101. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  102. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  103. for (int i = 0; i < p_reduce.size(); i++) {
  104. if (i == 0) {
  105. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  106. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  107. } else {
  108. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  109. if (i == p_reduce.size() - 1 && !p_set) {
  110. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  111. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  112. } else {
  113. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  114. }
  115. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  116. }
  117. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  118. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  119. RD::get_singleton()->compute_list_dispatch_threads(compute_list, luminance_reduce.push_constant.source_size[0], luminance_reduce.push_constant.source_size[1], 1);
  120. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  121. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  122. }
  123. RD::get_singleton()->compute_list_end();
  124. }
  125. void EffectsRD::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) {
  126. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster version of luminance reduction with the clustered renderer.");
  127. ERR_FAIL_COND_MSG(p_reduce.size() != p_fb.size(), "Incorrect frame buffer account for luminance reduction.");
  128. luminance_reduce_raster.push_constant.max_luminance = p_max_luminance;
  129. luminance_reduce_raster.push_constant.min_luminance = p_min_luminance;
  130. luminance_reduce_raster.push_constant.exposure_adjust = p_adjust;
  131. for (int i = 0; i < p_reduce.size(); i++) {
  132. luminance_reduce_raster.push_constant.source_size[0] = i == 0 ? p_source_size.x : luminance_reduce_raster.push_constant.dest_size[0];
  133. luminance_reduce_raster.push_constant.source_size[1] = i == 0 ? p_source_size.y : luminance_reduce_raster.push_constant.dest_size[1];
  134. luminance_reduce_raster.push_constant.dest_size[0] = MAX(luminance_reduce_raster.push_constant.source_size[0] / 8, 1);
  135. luminance_reduce_raster.push_constant.dest_size[1] = MAX(luminance_reduce_raster.push_constant.source_size[1] / 8, 1);
  136. bool final = !p_set && (luminance_reduce_raster.push_constant.dest_size[0] == 1) && (luminance_reduce_raster.push_constant.dest_size[1] == 1);
  137. LuminanceReduceRasterMode mode = final ? LUMINANCE_REDUCE_FRAGMENT_FINAL : (i == 0 ? LUMINANCE_REDUCE_FRAGMENT_FIRST : LUMINANCE_REDUCE_FRAGMENT);
  138. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_fb[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  139. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, luminance_reduce_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_fb[i])));
  140. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(i == 0 ? p_source_texture : p_reduce[i - 1]), 0);
  141. if (final) {
  142. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_prev_luminance), 1);
  143. }
  144. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  145. RD::get_singleton()->draw_list_set_push_constant(draw_list, &luminance_reduce_raster.push_constant, sizeof(LuminanceReduceRasterPushConstant));
  146. RD::get_singleton()->draw_list_draw(draw_list, true);
  147. RD::get_singleton()->draw_list_end();
  148. }
  149. }
  150. void EffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  151. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  152. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  153. roughness_limiter.push_constant.curve = p_curve;
  154. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  155. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  156. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  157. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  158. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  159. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
  160. RD::get_singleton()->compute_list_end();
  161. }
  162. void EffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  163. Sort::PushConstant push_constant;
  164. push_constant.total_elements = p_size;
  165. bool done = true;
  166. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  167. if (numThreadGroups > 1) {
  168. done = false;
  169. }
  170. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  171. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  172. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  173. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  174. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  175. int presorted = 512;
  176. while (!done) {
  177. RD::get_singleton()->compute_list_add_barrier(compute_list);
  178. done = true;
  179. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  180. numThreadGroups = 0;
  181. if (p_size > presorted) {
  182. if (p_size > presorted * 2) {
  183. done = false;
  184. }
  185. int pow2 = presorted;
  186. while (pow2 < p_size) {
  187. pow2 *= 2;
  188. }
  189. numThreadGroups = pow2 >> 9;
  190. }
  191. unsigned int nMergeSize = presorted * 2;
  192. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  193. push_constant.job_params[0] = nMergeSubSize;
  194. if (nMergeSubSize == nMergeSize >> 1) {
  195. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  196. push_constant.job_params[2] = -1;
  197. } else {
  198. push_constant.job_params[1] = nMergeSubSize;
  199. push_constant.job_params[2] = 1;
  200. }
  201. push_constant.job_params[3] = 0;
  202. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  203. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  204. RD::get_singleton()->compute_list_add_barrier(compute_list);
  205. }
  206. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  207. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  208. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  209. presorted *= 2;
  210. }
  211. RD::get_singleton()->compute_list_end();
  212. }
  213. EffectsRD::EffectsRD(bool p_prefer_raster_effects) {
  214. prefer_raster_effects = p_prefer_raster_effects;
  215. if (prefer_raster_effects) {
  216. Vector<String> luminance_reduce_modes;
  217. luminance_reduce_modes.push_back("\n#define FIRST_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FIRST
  218. luminance_reduce_modes.push_back("\n"); // LUMINANCE_REDUCE_FRAGMENT
  219. luminance_reduce_modes.push_back("\n#define FINAL_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FINAL
  220. luminance_reduce_raster.shader.initialize(luminance_reduce_modes);
  221. memset(&luminance_reduce_raster.push_constant, 0, sizeof(LuminanceReduceRasterPushConstant));
  222. luminance_reduce_raster.shader_version = luminance_reduce_raster.shader.version_create();
  223. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  224. luminance_reduce_raster.pipelines[i].setup(luminance_reduce_raster.shader.version_get_shader(luminance_reduce_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  225. }
  226. } else {
  227. // Initialize luminance_reduce
  228. Vector<String> luminance_reduce_modes;
  229. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  230. luminance_reduce_modes.push_back("\n");
  231. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  232. luminance_reduce.shader.initialize(luminance_reduce_modes);
  233. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  234. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  235. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  236. }
  237. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  238. luminance_reduce_raster.pipelines[i].clear();
  239. }
  240. }
  241. if (!prefer_raster_effects) {
  242. // Initialize roughness limiter
  243. Vector<String> shader_modes;
  244. shader_modes.push_back("");
  245. roughness_limiter.shader.initialize(shader_modes);
  246. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  247. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  248. }
  249. {
  250. Vector<String> sort_modes;
  251. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  252. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  253. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  254. sort.shader.initialize(sort_modes);
  255. sort.shader_version = sort.shader.version_create();
  256. for (int i = 0; i < SORT_MODE_MAX; i++) {
  257. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  258. }
  259. }
  260. RD::SamplerState sampler;
  261. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  262. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  263. sampler.max_lod = 0;
  264. default_sampler = RD::get_singleton()->sampler_create(sampler);
  265. RD::get_singleton()->set_resource_name(default_sampler, "Default Linear Sampler");
  266. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  267. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  268. sampler.max_lod = 1e20;
  269. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  270. RD::get_singleton()->set_resource_name(default_mipmap_sampler, "Default MipMap Sampler");
  271. { //create index array for copy shaders
  272. Vector<uint8_t> pv;
  273. pv.resize(6 * 4);
  274. {
  275. uint8_t *w = pv.ptrw();
  276. int *p32 = (int *)w;
  277. p32[0] = 0;
  278. p32[1] = 1;
  279. p32[2] = 2;
  280. p32[3] = 0;
  281. p32[4] = 2;
  282. p32[5] = 3;
  283. }
  284. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  285. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  286. }
  287. }
  288. EffectsRD::~EffectsRD() {
  289. RD::get_singleton()->free(default_sampler);
  290. RD::get_singleton()->free(default_mipmap_sampler);
  291. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  292. if (prefer_raster_effects) {
  293. luminance_reduce_raster.shader.version_free(luminance_reduce_raster.shader_version);
  294. } else {
  295. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  296. }
  297. if (!prefer_raster_effects) {
  298. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  299. }
  300. sort.shader.version_free(sort.shader_version);
  301. }