rasterizer_effects_rd.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*************************************************************************/
  2. /* rasterizer_effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "rasterizer_effects_rd.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "thirdparty/misc/cubemap_coeffs.h"
  34. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  35. p_array[0] = p_basis.elements[0][0];
  36. p_array[1] = p_basis.elements[1][0];
  37. p_array[2] = p_basis.elements[2][0];
  38. p_array[3] = 0;
  39. p_array[4] = p_basis.elements[0][1];
  40. p_array[5] = p_basis.elements[1][1];
  41. p_array[6] = p_basis.elements[2][1];
  42. p_array[7] = 0;
  43. p_array[8] = p_basis.elements[0][2];
  44. p_array[9] = p_basis.elements[1][2];
  45. p_array[10] = p_basis.elements[2][2];
  46. p_array[11] = 0;
  47. }
  48. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  49. for (int i = 0; i < 4; i++) {
  50. for (int j = 0; j < 4; j++) {
  51. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  52. }
  53. }
  54. }
  55. RID RasterizerEffectsRD::_get_uniform_set_from_image(RID p_image) {
  56. if (image_to_uniform_set_cache.has(p_image)) {
  57. RID uniform_set = image_to_uniform_set_cache[p_image];
  58. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  59. return uniform_set;
  60. }
  61. }
  62. Vector<RD::Uniform> uniforms;
  63. RD::Uniform u;
  64. u.type = RD::UNIFORM_TYPE_IMAGE;
  65. u.binding = 0;
  66. u.ids.push_back(p_image);
  67. uniforms.push_back(u);
  68. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  69. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  70. image_to_uniform_set_cache[p_image] = uniform_set;
  71. return uniform_set;
  72. }
  73. RID RasterizerEffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  74. if (texture_to_uniform_set_cache.has(p_texture)) {
  75. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  76. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  77. return uniform_set;
  78. }
  79. }
  80. Vector<RD::Uniform> uniforms;
  81. RD::Uniform u;
  82. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  83. u.binding = 0;
  84. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  85. u.ids.push_back(p_texture);
  86. uniforms.push_back(u);
  87. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  88. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, 0), 0);
  89. texture_to_uniform_set_cache[p_texture] = uniform_set;
  90. return uniform_set;
  91. }
  92. RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  93. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  94. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  95. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  96. return uniform_set;
  97. }
  98. }
  99. Vector<RD::Uniform> uniforms;
  100. RD::Uniform u;
  101. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  102. u.binding = 0;
  103. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  104. u.ids.push_back(p_texture);
  105. uniforms.push_back(u);
  106. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  107. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  108. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  109. return uniform_set;
  110. }
  111. RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  112. TexturePair tp;
  113. tp.texture1 = p_texture1;
  114. tp.texture2 = p_texture2;
  115. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  116. RID uniform_set = texture_pair_to_compute_uniform_set_cache[tp];
  117. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  118. return uniform_set;
  119. }
  120. }
  121. Vector<RD::Uniform> uniforms;
  122. {
  123. RD::Uniform u;
  124. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  125. u.binding = 0;
  126. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  127. u.ids.push_back(p_texture1);
  128. uniforms.push_back(u);
  129. }
  130. {
  131. RD::Uniform u;
  132. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  133. u.binding = 1;
  134. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  135. u.ids.push_back(p_texture2);
  136. uniforms.push_back(u);
  137. }
  138. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  139. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  140. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  141. return uniform_set;
  142. }
  143. RID RasterizerEffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  144. TexturePair tp;
  145. tp.texture1 = p_texture1;
  146. tp.texture2 = p_texture2;
  147. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  148. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  149. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  150. return uniform_set;
  151. }
  152. }
  153. Vector<RD::Uniform> uniforms;
  154. {
  155. RD::Uniform u;
  156. u.type = RD::UNIFORM_TYPE_IMAGE;
  157. u.binding = 0;
  158. u.ids.push_back(p_texture1);
  159. uniforms.push_back(u);
  160. }
  161. {
  162. RD::Uniform u;
  163. u.type = RD::UNIFORM_TYPE_IMAGE;
  164. u.binding = 1;
  165. u.ids.push_back(p_texture2);
  166. uniforms.push_back(u);
  167. }
  168. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  169. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  170. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  171. return uniform_set;
  172. }
  173. void RasterizerEffectsRD::copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance) {
  174. zeromem(&copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  175. if (p_flip_y) {
  176. copy_to_fb.push_constant.flip_y = true;
  177. }
  178. if (p_force_luminance) {
  179. copy_to_fb.push_constant.force_luminance = true;
  180. }
  181. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, p_rect);
  182. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  183. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  184. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  185. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  186. RD::get_singleton()->draw_list_draw(draw_list, true);
  187. RD::get_singleton()->draw_list_end();
  188. }
  189. void RasterizerEffectsRD::copy_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_all_source, bool p_8_bit_dst) {
  190. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  191. if (p_flip_y) {
  192. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  193. }
  194. if (p_force_luminance) {
  195. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  196. }
  197. if (p_all_source) {
  198. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  199. }
  200. copy.push_constant.section[0] = 0;
  201. copy.push_constant.section[1] = 0;
  202. copy.push_constant.section[2] = p_rect.size.width;
  203. copy.push_constant.section[3] = p_rect.size.height;
  204. copy.push_constant.target[0] = p_rect.position.x;
  205. copy.push_constant.target[1] = p_rect.position.y;
  206. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  207. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  208. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  209. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY]);
  210. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  211. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  212. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  213. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  214. RD::get_singleton()->compute_list_end();
  215. }
  216. void RasterizerEffectsRD::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far) {
  217. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  218. if (p_flip_y) {
  219. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  220. }
  221. copy.push_constant.section[0] = 0;
  222. copy.push_constant.section[1] = 0;
  223. copy.push_constant.section[2] = p_rect.size.width;
  224. copy.push_constant.section[3] = p_rect.size.height;
  225. copy.push_constant.target[0] = p_rect.position.x;
  226. copy.push_constant.target[1] = p_rect.position.y;
  227. copy.push_constant.camera_z_far = p_z_far;
  228. copy.push_constant.camera_z_near = p_z_near;
  229. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  230. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  231. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  232. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_LINEARIZE_DEPTH]);
  233. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  234. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  235. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  236. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  237. RD::get_singleton()->compute_list_end();
  238. }
  239. void RasterizerEffectsRD::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  240. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  241. if (p_flip_y) {
  242. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  243. }
  244. copy.push_constant.section[0] = 0;
  245. copy.push_constant.section[1] = 0;
  246. copy.push_constant.section[2] = p_rect.size.width;
  247. copy.push_constant.section[3] = p_rect.size.height;
  248. copy.push_constant.target[0] = p_rect.position.x;
  249. copy.push_constant.target[1] = p_rect.position.y;
  250. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  251. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  252. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  253. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_SIMPLY_COPY_DEPTH]);
  254. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  255. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  256. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  257. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  258. RD::get_singleton()->compute_list_end();
  259. }
  260. void RasterizerEffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_texture, RID p_back_texture, const Rect2i &p_region, bool p_8bit_dst) {
  261. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  262. uint32_t base_flags = 0;
  263. copy.push_constant.section[0] = p_region.position.x;
  264. copy.push_constant.section[1] = p_region.position.y;
  265. copy.push_constant.section[2] = p_region.size.width;
  266. copy.push_constant.section[3] = p_region.size.height;
  267. int32_t x_groups = (p_region.size.width - 1) / 8 + 1;
  268. int32_t y_groups = (p_region.size.height - 1) / 8 + 1;
  269. //HORIZONTAL
  270. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  271. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY]);
  272. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  273. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 0);
  274. copy.push_constant.flags = base_flags | COPY_FLAG_HORIZONTAL;
  275. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  276. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  277. RD::get_singleton()->compute_list_add_barrier(compute_list);
  278. //VERTICAL
  279. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_back_texture), 0);
  280. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 0);
  281. copy.push_constant.flags = base_flags;
  282. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  283. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  284. RD::get_singleton()->compute_list_end();
  285. }
  286. void RasterizerEffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_texture, RID p_back_texture, const Size2i &p_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  287. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  288. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  289. uint32_t base_flags = 0;
  290. int32_t x_groups = (p_size.width - 1) / 8 + 1;
  291. int32_t y_groups = (p_size.height - 1) / 8 + 1;
  292. copy.push_constant.section[2] = p_size.x;
  293. copy.push_constant.section[3] = p_size.y;
  294. copy.push_constant.glow_strength = p_strength;
  295. copy.push_constant.glow_bloom = p_bloom;
  296. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_treshold;
  297. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  298. copy.push_constant.glow_exposure = p_exposure;
  299. copy.push_constant.glow_white = 0; //actually unused
  300. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  301. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  302. //HORIZONTAL
  303. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  304. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  305. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  306. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  307. if (p_auto_exposure.is_valid() && p_first_pass) {
  308. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_auto_exposure), 1);
  309. }
  310. copy.push_constant.flags = base_flags | COPY_FLAG_HORIZONTAL | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0);
  311. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  312. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  313. RD::get_singleton()->compute_list_add_barrier(compute_list);
  314. copy_mode = COPY_MODE_GAUSSIAN_GLOW;
  315. //VERTICAL
  316. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  317. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_back_texture), 0);
  318. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 3);
  319. copy.push_constant.flags = base_flags;
  320. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  321. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  322. RD::get_singleton()->compute_list_end();
  323. }
  324. void RasterizerEffectsRD::screen_space_reflection(RID p_diffuse, RID p_normal, RenderingServer::EnvironmentSSRRoughnessQuality p_roughness_quality, RID p_roughness, RID p_blur_radius, RID p_blur_radius2, RID p_metallic, const Color &p_metallic_mask, RID p_depth, RID p_scale_depth, RID p_scale_normal, RID p_output, RID p_output_blur, const Size2i &p_screen_size, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const CameraMatrix &p_camera) {
  325. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  326. int32_t x_groups = (p_screen_size.width - 1) / 8 + 1;
  327. int32_t y_groups = (p_screen_size.height - 1) / 8 + 1;
  328. { //scale color and depth to half
  329. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  330. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  331. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  332. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  333. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  334. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  335. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  336. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  337. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal), 1);
  338. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  339. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  340. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  341. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  342. RD::get_singleton()->compute_list_add_barrier(compute_list);
  343. }
  344. {
  345. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  346. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  347. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  348. ssr.push_constant.screen_size[0] = p_screen_size.x;
  349. ssr.push_constant.screen_size[1] = p_screen_size.y;
  350. ssr.push_constant.curve_fade_in = p_fade_in;
  351. ssr.push_constant.distance_fade = p_fade_out;
  352. ssr.push_constant.num_steps = p_max_steps;
  353. ssr.push_constant.depth_tolerance = p_tolerance;
  354. ssr.push_constant.use_half_res = true;
  355. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  356. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  357. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  358. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  359. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  360. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  361. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  362. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  363. store_camera(p_camera, ssr.push_constant.projection);
  364. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr.pipelines[(p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) ? SCREEN_SPACE_REFLECTION_ROUGH : SCREEN_SPACE_REFLECTION_NORMAL]);
  365. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  366. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  367. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  368. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  369. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_metallic, p_roughness), 3);
  370. } else {
  371. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  372. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  373. }
  374. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  375. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  376. }
  377. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  378. //blurr
  379. RD::get_singleton()->compute_list_add_barrier(compute_list);
  380. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  381. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  382. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  383. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  384. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  385. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  386. ssr_filter.push_constant.vertical = 0;
  387. if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_LOW) {
  388. ssr_filter.push_constant.steps = p_max_steps / 3;
  389. ssr_filter.push_constant.increment = 3;
  390. } else if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_MEDIUM) {
  391. ssr_filter.push_constant.steps = p_max_steps / 2;
  392. ssr_filter.push_constant.increment = 2;
  393. } else {
  394. ssr_filter.push_constant.steps = p_max_steps;
  395. ssr_filter.push_constant.increment = 1;
  396. }
  397. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  398. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  399. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  400. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  401. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  402. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  403. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  404. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  405. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  406. RD::get_singleton()->compute_list_add_barrier(compute_list);
  407. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  408. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  409. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  410. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  411. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  412. ssr_filter.push_constant.vertical = 1;
  413. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  414. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  415. }
  416. RD::get_singleton()->compute_list_end();
  417. }
  418. void RasterizerEffectsRD::sub_surface_scattering(RID p_diffuse, RID p_diffuse2, RID p_depth, const CameraMatrix &p_camera, const Size2i &p_screen_size, float p_scale, float p_depth_scale, RenderingServer::SubSurfaceScatteringQuality p_quality) {
  419. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  420. int32_t x_groups = (p_screen_size.width - 1) / 8 + 1;
  421. int32_t y_groups = (p_screen_size.height - 1) / 8 + 1;
  422. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  423. p.normal /= p.d;
  424. float unit_size = p.normal.x;
  425. { //scale color and depth to half
  426. sss.push_constant.camera_z_far = p_camera.get_z_far();
  427. sss.push_constant.camera_z_near = p_camera.get_z_near();
  428. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  429. sss.push_constant.unit_size = unit_size;
  430. sss.push_constant.screen_size[0] = p_screen_size.x;
  431. sss.push_constant.screen_size[1] = p_screen_size.y;
  432. sss.push_constant.vertical = false;
  433. sss.push_constant.scale = p_scale;
  434. sss.push_constant.depth_scale = p_depth_scale;
  435. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  436. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  437. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  438. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  439. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  440. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  441. RD::get_singleton()->compute_list_add_barrier(compute_list);
  442. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  443. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  444. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  445. sss.push_constant.vertical = true;
  446. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  447. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  448. RD::get_singleton()->compute_list_end();
  449. }
  450. }
  451. void RasterizerEffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  452. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>());
  453. if (p_reflection.is_valid()) {
  454. if (p_base.is_valid()) {
  455. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_SSR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  456. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  457. } else {
  458. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_ADDITIVE_SSR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  459. }
  460. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  461. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  462. } else {
  463. if (p_base.is_valid()) {
  464. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_ADD].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  465. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  466. } else {
  467. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[SPECULAR_MERGE_ADDITIVE_ADD].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  468. }
  469. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  470. }
  471. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  472. RD::get_singleton()->draw_list_draw(draw_list, true);
  473. RD::get_singleton()->draw_list_end();
  474. }
  475. void RasterizerEffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  476. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  477. copy.push_constant.section[0] = 0;
  478. copy.push_constant.section[1] = 0;
  479. copy.push_constant.section[2] = p_size.width;
  480. copy.push_constant.section[3] = p_size.height;
  481. int32_t x_groups = (p_size.width - 1) / 8 + 1;
  482. int32_t y_groups = (p_size.height - 1) / 8 + 1;
  483. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  484. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_MIPMAP]);
  485. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  486. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  487. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  488. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  489. RD::get_singleton()->compute_list_end();
  490. }
  491. void RasterizerEffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, float p_z_near, float p_z_far, float p_bias, bool p_dp_flip) {
  492. CopyToDPPushConstant push_constant;
  493. push_constant.screen_size[0] = p_rect.size.x;
  494. push_constant.screen_size[1] = p_rect.size.y;
  495. push_constant.dest_offset[0] = p_rect.position.x;
  496. push_constant.dest_offset[1] = p_rect.position.y;
  497. push_constant.bias = p_bias;
  498. push_constant.z_far = p_z_far;
  499. push_constant.z_near = p_z_near;
  500. push_constant.z_flip = p_dp_flip;
  501. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  502. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  503. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  504. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cube_to_dp.pipeline);
  505. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  506. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 1);
  507. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(CopyToDPPushConstant));
  508. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  509. RD::get_singleton()->compute_list_end();
  510. }
  511. void RasterizerEffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  512. zeromem(&tonemap.push_constant, sizeof(TonemapPushConstant));
  513. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  514. tonemap.push_constant.bcs[0] = p_settings.brightness;
  515. tonemap.push_constant.bcs[1] = p_settings.contrast;
  516. tonemap.push_constant.bcs[2] = p_settings.saturation;
  517. tonemap.push_constant.use_glow = p_settings.use_glow;
  518. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  519. tonemap.push_constant.glow_level_flags = p_settings.glow_level_flags;
  520. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  521. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  522. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  523. TonemapMode mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  524. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  525. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  526. tonemap.push_constant.exposure = p_settings.exposure;
  527. tonemap.push_constant.white = p_settings.white;
  528. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  529. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  530. tonemap.push_constant.use_fxaa = p_settings.use_fxaa;
  531. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  532. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  533. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD);
  534. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
  535. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  536. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  537. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  538. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  539. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  540. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  541. RD::get_singleton()->draw_list_draw(draw_list, true);
  542. RD::get_singleton()->draw_list_end();
  543. }
  544. void RasterizerEffectsRD::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) {
  545. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  546. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  547. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  548. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  549. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  550. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  551. for (int i = 0; i < p_reduce.size(); i++) {
  552. if (i == 0) {
  553. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  554. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  555. } else {
  556. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  557. if (i == p_reduce.size() - 1 && !p_set) {
  558. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  559. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  560. } else {
  561. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  562. }
  563. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  564. }
  565. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  566. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  567. int32_t x_groups = (luminance_reduce.push_constant.source_size[0] - 1) / 8 + 1;
  568. int32_t y_groups = (luminance_reduce.push_constant.source_size[1] - 1) / 8 + 1;
  569. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  570. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  571. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  572. }
  573. RD::get_singleton()->compute_list_end();
  574. }
  575. void RasterizerEffectsRD::bokeh_dof(RID p_base_texture, RID p_depth_texture, const Size2i &p_base_texture_size, RID p_secondary_texture, RID p_halfsize_texture1, RID p_halfsize_texture2, bool p_dof_far, float p_dof_far_begin, float p_dof_far_size, bool p_dof_near, float p_dof_near_begin, float p_dof_near_size, float p_bokeh_size, RenderingServer::DOFBokehShape p_bokeh_shape, RS::DOFBlurQuality p_quality, bool p_use_jitter, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  576. bokeh.push_constant.blur_far_active = p_dof_far;
  577. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  578. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  579. bokeh.push_constant.blur_near_active = p_dof_near;
  580. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  581. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  582. bokeh.push_constant.use_jitter = p_use_jitter;
  583. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  584. bokeh.push_constant.z_near = p_cam_znear;
  585. bokeh.push_constant.z_far = p_cam_zfar;
  586. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  587. bokeh.push_constant.blur_size = p_bokeh_size;
  588. bokeh.push_constant.second_pass = false;
  589. bokeh.push_constant.half_size = false;
  590. bokeh.push_constant.blur_scale = 0.5;
  591. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  592. /* FIRST PASS */
  593. // The alpha channel of the source color texture is filled with the expected circle size
  594. // If used for DOF far, the size is positive, if used for near, its negative.
  595. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BLUR_SIZE]);
  596. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  597. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  598. int32_t x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  599. int32_t y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  600. bokeh.push_constant.size[0] = p_base_texture_size.x;
  601. bokeh.push_constant.size[1] = p_base_texture_size.y;
  602. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  603. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  604. RD::get_singleton()->compute_list_add_barrier(compute_list);
  605. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  606. //second pass
  607. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL]);
  608. static const int quality_samples[4] = { 6, 12, 12, 24 };
  609. bokeh.push_constant.steps = quality_samples[p_quality];
  610. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  611. //box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
  612. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  613. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  614. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  615. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  616. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  617. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  618. bokeh.push_constant.half_size = true;
  619. bokeh.push_constant.blur_size *= 0.5;
  620. } else {
  621. //medium and high quality use full size
  622. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 0);
  623. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  624. }
  625. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  626. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  627. RD::get_singleton()->compute_list_add_barrier(compute_list);
  628. //third pass
  629. bokeh.push_constant.second_pass = true;
  630. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  631. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture2), 0);
  632. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  633. } else {
  634. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  635. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 1);
  636. }
  637. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  638. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  639. RD::get_singleton()->compute_list_add_barrier(compute_list);
  640. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  641. //forth pass, upscale for low quality
  642. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  643. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  644. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture2), 1);
  645. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  646. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  647. bokeh.push_constant.size[0] = p_base_texture_size.x;
  648. bokeh.push_constant.size[1] = p_base_texture_size.y;
  649. bokeh.push_constant.half_size = false;
  650. bokeh.push_constant.second_pass = false;
  651. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  652. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  653. }
  654. } else {
  655. //circle
  656. //second pass
  657. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  658. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  659. bokeh.push_constant.steps = 0;
  660. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  661. //circle always runs in half size, otherwise too expensive
  662. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  663. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  664. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  665. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  666. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  667. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  668. bokeh.push_constant.half_size = true;
  669. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  670. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  671. RD::get_singleton()->compute_list_add_barrier(compute_list);
  672. //circle is just one pass, then upscale
  673. // upscale
  674. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  675. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  676. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  677. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  678. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  679. bokeh.push_constant.size[0] = p_base_texture_size.x;
  680. bokeh.push_constant.size[1] = p_base_texture_size.y;
  681. bokeh.push_constant.half_size = false;
  682. bokeh.push_constant.second_pass = false;
  683. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  684. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  685. }
  686. RD::get_singleton()->compute_list_end();
  687. }
  688. void RasterizerEffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, const Size2i &p_depth_buffer_size, RID p_depth_mipmaps_texture, const Vector<RID> &depth_mipmaps, RID p_ao1, bool p_half_size, RID p_ao2, RID p_upscale_buffer, float p_intensity, float p_radius, float p_bias, const CameraMatrix &p_projection, RS::EnvironmentSSAOQuality p_quality, RS::EnvironmentSSAOBlur p_blur, float p_edge_sharpness) {
  689. //minify first
  690. ssao.minify_push_constant.orthogonal = p_projection.is_orthogonal();
  691. ssao.minify_push_constant.z_near = p_projection.get_z_near();
  692. ssao.minify_push_constant.z_far = p_projection.get_z_far();
  693. ssao.minify_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  694. ssao.minify_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  695. ssao.minify_push_constant.source_size[0] = p_depth_buffer_size.x;
  696. ssao.minify_push_constant.source_size[1] = p_depth_buffer_size.y;
  697. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  698. /* FIRST PASS */
  699. // Minify the depth buffer.
  700. for (int i = 0; i < depth_mipmaps.size(); i++) {
  701. if (i == 0) {
  702. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_FIRST]);
  703. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  704. } else {
  705. if (i == 1) {
  706. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_MIPMAP]);
  707. }
  708. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i - 1]), 0);
  709. }
  710. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i]), 1);
  711. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.minify_push_constant, sizeof(SSAOMinifyPushConstant));
  712. // shrink after set
  713. ssao.minify_push_constant.source_size[0] = MAX(1, ssao.minify_push_constant.source_size[0] >> 1);
  714. ssao.minify_push_constant.source_size[1] = MAX(1, ssao.minify_push_constant.source_size[1] >> 1);
  715. int x_groups = (ssao.minify_push_constant.source_size[0] - 1) / 8 + 1;
  716. int y_groups = (ssao.minify_push_constant.source_size[1] - 1) / 8 + 1;
  717. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  718. RD::get_singleton()->compute_list_add_barrier(compute_list);
  719. }
  720. /* SECOND PASS */
  721. // Gather samples
  722. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[(SSAO_GATHER_LOW + p_quality) + (p_half_size ? 4 : 0)]);
  723. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 0);
  724. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 1);
  725. if (!p_half_size) {
  726. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 2);
  727. }
  728. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_normal_buffer), 3);
  729. ssao.gather_push_constant.screen_size[0] = p_depth_buffer_size.x;
  730. ssao.gather_push_constant.screen_size[1] = p_depth_buffer_size.y;
  731. if (p_half_size) {
  732. ssao.gather_push_constant.screen_size[0] >>= 1;
  733. ssao.gather_push_constant.screen_size[1] >>= 1;
  734. }
  735. ssao.gather_push_constant.z_far = p_projection.get_z_far();
  736. ssao.gather_push_constant.z_near = p_projection.get_z_near();
  737. ssao.gather_push_constant.orthogonal = p_projection.is_orthogonal();
  738. ssao.gather_push_constant.proj_info[0] = -2.0f / (ssao.gather_push_constant.screen_size[0] * p_projection.matrix[0][0]);
  739. ssao.gather_push_constant.proj_info[1] = -2.0f / (ssao.gather_push_constant.screen_size[1] * p_projection.matrix[1][1]);
  740. ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  741. ssao.gather_push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  742. //ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  743. //ssao.gather_push_constant.proj_info[3] = -(1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  744. ssao.gather_push_constant.radius = p_radius;
  745. ssao.gather_push_constant.proj_scale = float(p_projection.get_pixels_per_meter(ssao.gather_push_constant.screen_size[0]));
  746. ssao.gather_push_constant.bias = p_bias;
  747. ssao.gather_push_constant.intensity_div_r6 = p_intensity / pow(p_radius, 6.0f);
  748. ssao.gather_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  749. ssao.gather_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  750. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  751. int x_groups = (ssao.gather_push_constant.screen_size[0] - 1) / 8 + 1;
  752. int y_groups = (ssao.gather_push_constant.screen_size[1] - 1) / 8 + 1;
  753. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  754. RD::get_singleton()->compute_list_add_barrier(compute_list);
  755. /* THIRD PASS */
  756. // Blur horizontal
  757. ssao.blur_push_constant.edge_sharpness = p_edge_sharpness;
  758. ssao.blur_push_constant.filter_scale = p_blur;
  759. ssao.blur_push_constant.screen_size[0] = ssao.gather_push_constant.screen_size[0];
  760. ssao.blur_push_constant.screen_size[1] = ssao.gather_push_constant.screen_size[1];
  761. ssao.blur_push_constant.z_far = p_projection.get_z_far();
  762. ssao.blur_push_constant.z_near = p_projection.get_z_near();
  763. ssao.blur_push_constant.orthogonal = p_projection.is_orthogonal();
  764. ssao.blur_push_constant.axis[0] = 1;
  765. ssao.blur_push_constant.axis[1] = 0;
  766. if (p_blur != RS::ENV_SSAO_BLUR_DISABLED) {
  767. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[p_half_size ? SSAO_BLUR_PASS_HALF : SSAO_BLUR_PASS]);
  768. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  769. if (p_half_size) {
  770. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 1);
  771. } else {
  772. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  773. }
  774. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao2), 3);
  775. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  776. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  777. RD::get_singleton()->compute_list_add_barrier(compute_list);
  778. /* THIRD PASS */
  779. // Blur vertical
  780. ssao.blur_push_constant.axis[0] = 0;
  781. ssao.blur_push_constant.axis[1] = 1;
  782. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao2), 0);
  783. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 3);
  784. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  785. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  786. }
  787. if (p_half_size) { //must upscale
  788. /* FOURTH PASS */
  789. // upscale if half size
  790. //back to full size
  791. ssao.blur_push_constant.screen_size[0] = p_depth_buffer_size.x;
  792. ssao.blur_push_constant.screen_size[1] = p_depth_buffer_size.y;
  793. RD::get_singleton()->compute_list_add_barrier(compute_list);
  794. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_BLUR_UPSCALE]);
  795. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  796. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 3);
  797. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  798. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 2);
  799. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant)); //not used but set anyway
  800. x_groups = (p_depth_buffer_size.x - 1) / 8 + 1;
  801. y_groups = (p_depth_buffer_size.y - 1) / 8 + 1;
  802. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  803. }
  804. RD::get_singleton()->compute_list_end();
  805. }
  806. void RasterizerEffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  807. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  808. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  809. roughness_limiter.push_constant.curve = p_curve;
  810. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  811. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  812. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  813. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  814. int x_groups = (p_size.x - 1) / 8 + 1;
  815. int y_groups = (p_size.y - 1) / 8 + 1;
  816. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  817. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  818. RD::get_singleton()->compute_list_end();
  819. }
  820. void RasterizerEffectsRD::cubemap_roughness(RID p_source_rd_texture, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
  821. zeromem(&roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  822. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  823. roughness.push_constant.roughness = p_roughness;
  824. roughness.push_constant.sample_count = p_sample_count;
  825. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  826. roughness.push_constant.face_size = p_size;
  827. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  828. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.pipeline);
  829. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  830. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_framebuffer), 1);
  831. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  832. int x_groups = (p_size - 1) / 8 + 1;
  833. int y_groups = (p_size - 1) / 8 + 1;
  834. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  835. RD::get_singleton()->compute_list_end();
  836. }
  837. void RasterizerEffectsRD::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  838. cubemap_downsampler.push_constant.face_size = p_size.x;
  839. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  840. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.pipeline);
  841. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap), 0);
  842. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_cubemap), 1);
  843. int x_groups = (p_size.x - 1) / 8 + 1;
  844. int y_groups = (p_size.y - 1) / 8 + 1;
  845. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  846. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  847. RD::get_singleton()->compute_list_end();
  848. }
  849. void RasterizerEffectsRD::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  850. Vector<RD::Uniform> uniforms;
  851. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  852. RD::Uniform u;
  853. u.type = RD::UNIFORM_TYPE_IMAGE;
  854. u.binding = i;
  855. u.ids.push_back(p_dest_cubemap[i]);
  856. uniforms.push_back(u);
  857. }
  858. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  859. RD::get_singleton()->free(filter.image_uniform_set);
  860. }
  861. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.shader.version_get_shader(filter.shader_version, 0), 2);
  862. int pipeline = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  863. pipeline = filter.use_high_quality ? pipeline : pipeline + 1;
  864. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  865. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.pipelines[pipeline]);
  866. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap, true), 0);
  867. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  868. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  869. int x_groups = p_use_array ? 1792 : 342; // (128 * 128 * 7) / 64 : (128*128 + 64*64 + 32*32 + 16*16 + 8*8 + 4*4 + 2*2) / 64
  870. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  871. RD::get_singleton()->compute_list_end();
  872. }
  873. void RasterizerEffectsRD::render_sky(RD::DrawListID p_list, float p_time, RID p_fb, RID p_samplers, RID p_lights, RenderPipelineVertexFormatCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, const CameraMatrix &p_camera, const Basis &p_orientation, float p_multiplier, const Vector3 &p_position) {
  874. SkyPushConstant sky_push_constant;
  875. zeromem(&sky_push_constant, sizeof(SkyPushConstant));
  876. sky_push_constant.proj[0] = p_camera.matrix[2][0];
  877. sky_push_constant.proj[1] = p_camera.matrix[0][0];
  878. sky_push_constant.proj[2] = p_camera.matrix[2][1];
  879. sky_push_constant.proj[3] = p_camera.matrix[1][1];
  880. sky_push_constant.position[0] = p_position.x;
  881. sky_push_constant.position[1] = p_position.y;
  882. sky_push_constant.position[2] = p_position.z;
  883. sky_push_constant.multiplier = p_multiplier;
  884. sky_push_constant.time = p_time;
  885. store_transform_3x3(p_orientation, sky_push_constant.orientation);
  886. RenderingDevice::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_fb);
  887. RD::DrawListID draw_list = p_list;
  888. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, p_pipeline->get_render_pipeline(RD::INVALID_ID, fb_format));
  889. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_samplers, 0);
  890. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_uniform_set, 1);
  891. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_texture_set, 2);
  892. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_lights, 3);
  893. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  894. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky_push_constant, sizeof(SkyPushConstant));
  895. RD::get_singleton()->draw_list_draw(draw_list, true);
  896. }
  897. RasterizerEffectsRD::RasterizerEffectsRD() {
  898. { // Initialize copy
  899. Vector<String> copy_modes;
  900. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  901. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  902. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  903. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  904. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  905. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  906. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  907. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  908. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  909. copy.shader.initialize(copy_modes);
  910. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  911. copy.shader_version = copy.shader.version_create();
  912. for (int i = 0; i < COPY_MODE_MAX; i++) {
  913. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  914. }
  915. }
  916. {
  917. Vector<String> copy_modes;
  918. copy_modes.push_back("\n");
  919. copy_to_fb.shader.initialize(copy_modes);
  920. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  921. //use additive
  922. copy_to_fb.pipeline.setup(copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  923. }
  924. {
  925. // Initialize roughness
  926. Vector<String> cubemap_roughness_modes;
  927. cubemap_roughness_modes.push_back("");
  928. roughness.shader.initialize(cubemap_roughness_modes);
  929. roughness.shader_version = roughness.shader.version_create();
  930. roughness.pipeline = RD::get_singleton()->compute_pipeline_create(roughness.shader.version_get_shader(roughness.shader_version, 0));
  931. }
  932. {
  933. // Initialize tonemapper
  934. Vector<String> tonemap_modes;
  935. tonemap_modes.push_back("\n");
  936. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  937. tonemap.shader.initialize(tonemap_modes);
  938. tonemap.shader_version = tonemap.shader.version_create();
  939. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  940. tonemap.pipelines[i].setup(tonemap.shader.version_get_shader(tonemap.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  941. }
  942. }
  943. {
  944. // Initialize luminance_reduce
  945. Vector<String> luminance_reduce_modes;
  946. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  947. luminance_reduce_modes.push_back("\n");
  948. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  949. luminance_reduce.shader.initialize(luminance_reduce_modes);
  950. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  951. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  952. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  953. }
  954. }
  955. {
  956. // Initialize copier
  957. Vector<String> copy_modes;
  958. copy_modes.push_back("\n");
  959. cube_to_dp.shader.initialize(copy_modes);
  960. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  961. cube_to_dp.pipeline = RD::get_singleton()->compute_pipeline_create(cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0));
  962. }
  963. {
  964. // Initialize bokeh
  965. Vector<String> bokeh_modes;
  966. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  967. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  968. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  969. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n");
  970. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  971. bokeh.shader.initialize(bokeh_modes);
  972. bokeh.shader_version = bokeh.shader.version_create();
  973. for (int i = 0; i < BOKEH_MAX; i++) {
  974. bokeh.pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.shader.version_get_shader(bokeh.shader_version, i));
  975. }
  976. }
  977. {
  978. // Initialize ssao
  979. uint32_t pipeline = 0;
  980. {
  981. Vector<String> ssao_modes;
  982. ssao_modes.push_back("\n#define MINIFY_START\n");
  983. ssao_modes.push_back("\n");
  984. ssao.minify_shader.initialize(ssao_modes);
  985. ssao.minify_shader_version = ssao.minify_shader.version_create();
  986. for (int i = 0; i <= SSAO_MINIFY_MIPMAP; i++) {
  987. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.minify_shader.version_get_shader(ssao.minify_shader_version, i));
  988. pipeline++;
  989. }
  990. }
  991. {
  992. Vector<String> ssao_modes;
  993. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n");
  994. ssao_modes.push_back("\n");
  995. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n");
  996. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n");
  997. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n#define USE_HALF_SIZE\n");
  998. ssao_modes.push_back("\n#define USE_HALF_SIZE\n");
  999. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n#define USE_HALF_SIZE\n");
  1000. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n#define USE_HALF_SIZE\n");
  1001. ssao.gather_shader.initialize(ssao_modes);
  1002. ssao.gather_shader_version = ssao.gather_shader.version_create();
  1003. for (int i = SSAO_GATHER_LOW; i <= SSAO_GATHER_ULTRA_HALF; i++) {
  1004. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i - SSAO_GATHER_LOW));
  1005. pipeline++;
  1006. }
  1007. }
  1008. {
  1009. Vector<String> ssao_modes;
  1010. ssao_modes.push_back("\n#define MODE_FULL_SIZE\n");
  1011. ssao_modes.push_back("\n");
  1012. ssao_modes.push_back("\n#define MODE_UPSCALE\n");
  1013. ssao.blur_shader.initialize(ssao_modes);
  1014. ssao.blur_shader_version = ssao.blur_shader.version_create();
  1015. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_UPSCALE; i++) {
  1016. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  1017. pipeline++;
  1018. }
  1019. }
  1020. ERR_FAIL_COND(pipeline != SSAO_MAX);
  1021. }
  1022. {
  1023. // Initialize roughness limiter
  1024. Vector<String> shader_modes;
  1025. shader_modes.push_back("");
  1026. roughness_limiter.shader.initialize(shader_modes);
  1027. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  1028. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  1029. }
  1030. {
  1031. //Initialize cubemap downsampler
  1032. Vector<String> cubemap_downsampler_modes;
  1033. cubemap_downsampler_modes.push_back("");
  1034. cubemap_downsampler.shader.initialize(cubemap_downsampler_modes);
  1035. cubemap_downsampler.shader_version = cubemap_downsampler.shader.version_create();
  1036. cubemap_downsampler.pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  1037. }
  1038. {
  1039. // Initialize cubemap filter
  1040. filter.use_high_quality = GLOBAL_GET("rendering/quality/reflections/fast_filter_high_quality");
  1041. Vector<String> cubemap_filter_modes;
  1042. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  1043. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  1044. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1045. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1046. filter.shader.initialize(cubemap_filter_modes);
  1047. filter.shader_version = filter.shader.version_create();
  1048. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  1049. filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.shader.version_get_shader(filter.shader_version, i));
  1050. }
  1051. if (filter.use_high_quality) {
  1052. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  1053. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0], false);
  1054. } else {
  1055. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  1056. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0], false);
  1057. }
  1058. Vector<RD::Uniform> uniforms;
  1059. {
  1060. RD::Uniform u;
  1061. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1062. u.binding = 0;
  1063. u.ids.push_back(filter.coefficient_buffer);
  1064. uniforms.push_back(u);
  1065. }
  1066. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  1067. }
  1068. {
  1069. Vector<String> specular_modes;
  1070. specular_modes.push_back("\n#define MODE_MERGE\n");
  1071. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  1072. specular_modes.push_back("\n");
  1073. specular_modes.push_back("\n#define MODE_SSR\n");
  1074. specular_merge.shader.initialize(specular_modes);
  1075. specular_merge.shader_version = specular_merge.shader.version_create();
  1076. //use additive
  1077. RD::PipelineColorBlendState::Attachment ba;
  1078. ba.enable_blend = true;
  1079. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1080. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1081. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1082. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1083. ba.color_blend_op = RD::BLEND_OP_ADD;
  1084. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  1085. RD::PipelineColorBlendState blend_additive;
  1086. blend_additive.attachments.push_back(ba);
  1087. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  1088. RD::PipelineColorBlendState blend_state;
  1089. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  1090. blend_state = blend_additive;
  1091. } else {
  1092. blend_state = RD::PipelineColorBlendState::create_disabled();
  1093. }
  1094. specular_merge.pipelines[i].setup(specular_merge.shader.version_get_shader(specular_merge.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
  1095. }
  1096. }
  1097. {
  1098. Vector<String> ssr_modes;
  1099. ssr_modes.push_back("\n");
  1100. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  1101. ssr.shader.initialize(ssr_modes);
  1102. ssr.shader_version = ssr.shader.version_create();
  1103. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  1104. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  1105. }
  1106. }
  1107. {
  1108. Vector<String> ssr_filter_modes;
  1109. ssr_filter_modes.push_back("\n");
  1110. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  1111. ssr_filter.shader.initialize(ssr_filter_modes);
  1112. ssr_filter.shader_version = ssr_filter.shader.version_create();
  1113. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  1114. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  1115. }
  1116. }
  1117. {
  1118. Vector<String> ssr_scale_modes;
  1119. ssr_scale_modes.push_back("\n");
  1120. ssr_scale.shader.initialize(ssr_scale_modes);
  1121. ssr_scale.shader_version = ssr_scale.shader.version_create();
  1122. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  1123. }
  1124. {
  1125. Vector<String> sss_modes;
  1126. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  1127. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  1128. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  1129. sss.shader.initialize(sss_modes);
  1130. sss.shader_version = sss.shader.version_create();
  1131. for (int i = 0; i < sss_modes.size(); i++) {
  1132. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  1133. }
  1134. }
  1135. RD::SamplerState sampler;
  1136. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1137. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1138. sampler.max_lod = 0;
  1139. default_sampler = RD::get_singleton()->sampler_create(sampler);
  1140. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1141. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1142. sampler.max_lod = 1e20;
  1143. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  1144. { //create index array for copy shaders
  1145. Vector<uint8_t> pv;
  1146. pv.resize(6 * 4);
  1147. {
  1148. uint8_t *w = pv.ptrw();
  1149. int *p32 = (int *)w;
  1150. p32[0] = 0;
  1151. p32[1] = 1;
  1152. p32[2] = 2;
  1153. p32[3] = 0;
  1154. p32[4] = 2;
  1155. p32[5] = 3;
  1156. }
  1157. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1158. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  1159. }
  1160. }
  1161. RasterizerEffectsRD::~RasterizerEffectsRD() {
  1162. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1163. RD::get_singleton()->free(filter.image_uniform_set);
  1164. }
  1165. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  1166. RD::get_singleton()->free(filter.uniform_set);
  1167. }
  1168. RD::get_singleton()->free(default_sampler);
  1169. RD::get_singleton()->free(default_mipmap_sampler);
  1170. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  1171. RD::get_singleton()->free(filter.coefficient_buffer);
  1172. bokeh.shader.version_free(bokeh.shader_version);
  1173. copy.shader.version_free(copy.shader_version);
  1174. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  1175. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  1176. cubemap_downsampler.shader.version_free(cubemap_downsampler.shader_version);
  1177. filter.shader.version_free(filter.shader_version);
  1178. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  1179. roughness.shader.version_free(roughness.shader_version);
  1180. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  1181. specular_merge.shader.version_free(specular_merge.shader_version);
  1182. ssao.blur_shader.version_free(ssao.blur_shader_version);
  1183. ssao.gather_shader.version_free(ssao.gather_shader_version);
  1184. ssao.minify_shader.version_free(ssao.minify_shader_version);
  1185. ssr.shader.version_free(ssr.shader_version);
  1186. ssr_filter.shader.version_free(ssr_filter.shader_version);
  1187. ssr_scale.shader.version_free(ssr_scale.shader_version);
  1188. sss.shader.version_free(sss.shader_version);
  1189. tonemap.shader.version_free(tonemap.shader_version);
  1190. }