effects_rd.cpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. /*************************************************************************/
  2. /* effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  37. for (int i = 0; i < 4; i++) {
  38. for (int j = 0; j < 4; j++) {
  39. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  40. }
  41. }
  42. }
  43. RID EffectsRD::_get_uniform_set_from_image(RID p_image) {
  44. if (image_to_uniform_set_cache.has(p_image)) {
  45. RID uniform_set = image_to_uniform_set_cache[p_image];
  46. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  47. return uniform_set;
  48. }
  49. }
  50. Vector<RD::Uniform> uniforms;
  51. RD::Uniform u;
  52. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  53. u.binding = 0;
  54. u.ids.push_back(p_image);
  55. uniforms.push_back(u);
  56. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  57. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  58. image_to_uniform_set_cache[p_image] = uniform_set;
  59. return uniform_set;
  60. }
  61. RID EffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  62. if (texture_to_uniform_set_cache.has(p_texture)) {
  63. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  64. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  65. return uniform_set;
  66. }
  67. }
  68. Vector<RD::Uniform> uniforms;
  69. RD::Uniform u;
  70. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  71. u.binding = 0;
  72. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  73. u.ids.push_back(p_texture);
  74. uniforms.push_back(u);
  75. //anything with the same configuration (one texture in binding 0 for set 0), is good
  76. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, 0), 0);
  77. texture_to_uniform_set_cache[p_texture] = uniform_set;
  78. return uniform_set;
  79. }
  80. RID EffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  81. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  82. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  83. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  84. return uniform_set;
  85. }
  86. }
  87. Vector<RD::Uniform> uniforms;
  88. RD::Uniform u;
  89. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  90. u.binding = 0;
  91. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  92. u.ids.push_back(p_texture);
  93. uniforms.push_back(u);
  94. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  95. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  96. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  97. return uniform_set;
  98. }
  99. RID EffectsRD::_get_compute_uniform_set_from_texture_and_sampler(RID p_texture, RID p_sampler) {
  100. TextureSamplerPair tsp;
  101. tsp.texture = p_texture;
  102. tsp.sampler = p_sampler;
  103. if (texture_sampler_to_compute_uniform_set_cache.has(tsp)) {
  104. RID uniform_set = texture_sampler_to_compute_uniform_set_cache[tsp];
  105. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  106. return uniform_set;
  107. }
  108. }
  109. Vector<RD::Uniform> uniforms;
  110. RD::Uniform u;
  111. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  112. u.binding = 0;
  113. u.ids.push_back(p_sampler);
  114. u.ids.push_back(p_texture);
  115. uniforms.push_back(u);
  116. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  117. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0), 0);
  118. texture_sampler_to_compute_uniform_set_cache[tsp] = uniform_set;
  119. return uniform_set;
  120. }
  121. RID EffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  122. TexturePair tp;
  123. tp.texture1 = p_texture1;
  124. tp.texture2 = p_texture2;
  125. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  126. RID uniform_set = texture_pair_to_compute_uniform_set_cache[tp];
  127. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  128. return uniform_set;
  129. }
  130. }
  131. Vector<RD::Uniform> uniforms;
  132. {
  133. RD::Uniform u;
  134. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  135. u.binding = 0;
  136. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  137. u.ids.push_back(p_texture1);
  138. uniforms.push_back(u);
  139. }
  140. {
  141. RD::Uniform u;
  142. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  143. u.binding = 1;
  144. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  145. u.ids.push_back(p_texture2);
  146. uniforms.push_back(u);
  147. }
  148. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  149. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  150. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  151. return uniform_set;
  152. }
  153. RID EffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  154. TexturePair tp;
  155. tp.texture1 = p_texture1;
  156. tp.texture2 = p_texture2;
  157. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  158. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  159. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  160. return uniform_set;
  161. }
  162. }
  163. Vector<RD::Uniform> uniforms;
  164. {
  165. RD::Uniform u;
  166. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  167. u.binding = 0;
  168. u.ids.push_back(p_texture1);
  169. uniforms.push_back(u);
  170. }
  171. {
  172. RD::Uniform u;
  173. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  174. u.binding = 1;
  175. u.ids.push_back(p_texture2);
  176. uniforms.push_back(u);
  177. }
  178. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  179. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  180. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  181. return uniform_set;
  182. }
  183. void EffectsRD::copy_to_atlas_fb(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_uv_rect, RD::DrawListID p_draw_list, bool p_flip_y, bool p_panorama) {
  184. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  185. copy_to_fb.push_constant.use_section = true;
  186. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  187. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  188. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  189. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  190. if (p_flip_y) {
  191. copy_to_fb.push_constant.flip_y = true;
  192. }
  193. RD::DrawListID draw_list = p_draw_list;
  194. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  195. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  196. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  197. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  198. RD::get_singleton()->draw_list_draw(draw_list, true);
  199. }
  200. void EffectsRD::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, bool p_alpha_to_zero, bool p_srgb, RID p_secondary) {
  201. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  202. if (p_flip_y) {
  203. copy_to_fb.push_constant.flip_y = true;
  204. }
  205. if (p_force_luminance) {
  206. copy_to_fb.push_constant.force_luminance = true;
  207. }
  208. if (p_alpha_to_zero) {
  209. copy_to_fb.push_constant.alpha_to_zero = true;
  210. }
  211. if (p_srgb) {
  212. copy_to_fb.push_constant.srgb = true;
  213. }
  214. 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);
  215. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  216. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  217. if (p_secondary.is_valid()) {
  218. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_secondary), 1);
  219. }
  220. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  221. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  222. RD::get_singleton()->draw_list_draw(draw_list, true);
  223. RD::get_singleton()->draw_list_end();
  224. }
  225. void EffectsRD::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, bool p_alpha_to_one) {
  226. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  227. if (p_flip_y) {
  228. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  229. }
  230. if (p_force_luminance) {
  231. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  232. }
  233. if (p_all_source) {
  234. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  235. }
  236. if (p_alpha_to_one) {
  237. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  238. }
  239. copy.push_constant.section[0] = 0;
  240. copy.push_constant.section[1] = 0;
  241. copy.push_constant.section[2] = p_rect.size.width;
  242. copy.push_constant.section[3] = p_rect.size.height;
  243. copy.push_constant.target[0] = p_rect.position.x;
  244. copy.push_constant.target[1] = p_rect.position.y;
  245. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  246. 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]);
  247. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  248. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  249. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  250. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  251. RD::get_singleton()->compute_list_end();
  252. }
  253. void EffectsRD::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {
  254. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  255. copy.push_constant.section[0] = 0;
  256. copy.push_constant.section[1] = 0;
  257. copy.push_constant.section[2] = p_panorama_size.width;
  258. copy.push_constant.section[3] = p_panorama_size.height;
  259. copy.push_constant.target[0] = 0;
  260. copy.push_constant.target[1] = 0;
  261. copy.push_constant.camera_z_far = p_lod;
  262. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  263. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_is_array ? COPY_MODE_CUBE_ARRAY_TO_PANORAMA : COPY_MODE_CUBE_TO_PANORAMA]);
  264. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cube), 0);
  265. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_panorama), 3);
  266. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  267. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
  268. RD::get_singleton()->compute_list_end();
  269. }
  270. void EffectsRD::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) {
  271. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  272. if (p_flip_y) {
  273. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  274. }
  275. copy.push_constant.section[0] = 0;
  276. copy.push_constant.section[1] = 0;
  277. copy.push_constant.section[2] = p_rect.size.width;
  278. copy.push_constant.section[3] = p_rect.size.height;
  279. copy.push_constant.target[0] = p_rect.position.x;
  280. copy.push_constant.target[1] = p_rect.position.y;
  281. copy.push_constant.camera_z_far = p_z_far;
  282. copy.push_constant.camera_z_near = p_z_near;
  283. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  284. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_LINEARIZE_DEPTH]);
  285. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  286. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  287. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  288. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  289. RD::get_singleton()->compute_list_end();
  290. }
  291. void EffectsRD::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  292. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  293. if (p_flip_y) {
  294. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  295. }
  296. copy.push_constant.section[0] = 0;
  297. copy.push_constant.section[1] = 0;
  298. copy.push_constant.section[2] = p_rect.size.width;
  299. copy.push_constant.section[3] = p_rect.size.height;
  300. copy.push_constant.target[0] = p_rect.position.x;
  301. copy.push_constant.target[1] = p_rect.position.y;
  302. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  303. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_SIMPLY_COPY_DEPTH]);
  304. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  305. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  306. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  307. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  308. RD::get_singleton()->compute_list_end();
  309. }
  310. void EffectsRD::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  311. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  312. copy.push_constant.section[0] = 0;
  313. copy.push_constant.section[1] = 0;
  314. copy.push_constant.section[2] = p_region.size.width;
  315. copy.push_constant.section[3] = p_region.size.height;
  316. copy.push_constant.target[0] = p_region.position.x;
  317. copy.push_constant.target[1] = p_region.position.y;
  318. copy.push_constant.set_color[0] = p_color.r;
  319. copy.push_constant.set_color[1] = p_color.g;
  320. copy.push_constant.set_color[2] = p_color.b;
  321. copy.push_constant.set_color[3] = p_color.a;
  322. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  323. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR]);
  324. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  325. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  326. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  327. RD::get_singleton()->compute_list_end();
  328. }
  329. void EffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_texture, RID p_back_texture, const Rect2i &p_region, bool p_8bit_dst) {
  330. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  331. uint32_t base_flags = 0;
  332. copy.push_constant.section[0] = p_region.position.x;
  333. copy.push_constant.section[1] = p_region.position.y;
  334. copy.push_constant.section[2] = p_region.size.width;
  335. copy.push_constant.section[3] = p_region.size.height;
  336. //HORIZONTAL
  337. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  338. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY]);
  339. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  340. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  341. copy.push_constant.flags = base_flags | COPY_FLAG_HORIZONTAL;
  342. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  343. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  344. RD::get_singleton()->compute_list_add_barrier(compute_list);
  345. //VERTICAL
  346. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_back_texture), 0);
  347. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 3);
  348. copy.push_constant.flags = base_flags;
  349. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  350. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  351. RD::get_singleton()->compute_list_end();
  352. }
  353. void EffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_back_texture, const Size2i &p_size, float p_strength, bool p_high_quality, 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) {
  354. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  355. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  356. uint32_t base_flags = 0;
  357. copy.push_constant.section[2] = p_size.x;
  358. copy.push_constant.section[3] = p_size.y;
  359. copy.push_constant.glow_strength = p_strength;
  360. copy.push_constant.glow_bloom = p_bloom;
  361. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_treshold;
  362. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  363. copy.push_constant.glow_exposure = p_exposure;
  364. copy.push_constant.glow_white = 0; //actually unused
  365. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  366. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  367. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  368. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  369. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  370. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  371. if (p_auto_exposure.is_valid() && p_first_pass) {
  372. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_auto_exposure), 1);
  373. }
  374. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  375. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  376. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  377. RD::get_singleton()->compute_list_end();
  378. }
  379. void EffectsRD::screen_space_reflection(RID p_diffuse, RID p_normal_roughness, RenderingServer::EnvironmentSSRRoughnessQuality p_roughness_quality, 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) {
  380. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  381. { //scale color and depth to half
  382. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  383. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  384. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  385. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  386. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  387. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  388. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  389. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  390. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal_roughness), 1);
  391. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  392. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  393. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  394. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  395. RD::get_singleton()->compute_list_add_barrier(compute_list);
  396. }
  397. {
  398. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  399. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  400. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  401. ssr.push_constant.screen_size[0] = p_screen_size.x;
  402. ssr.push_constant.screen_size[1] = p_screen_size.y;
  403. ssr.push_constant.curve_fade_in = p_fade_in;
  404. ssr.push_constant.distance_fade = p_fade_out;
  405. ssr.push_constant.num_steps = p_max_steps;
  406. ssr.push_constant.depth_tolerance = p_tolerance;
  407. ssr.push_constant.use_half_res = true;
  408. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  409. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  410. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  411. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  412. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  413. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  414. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  415. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  416. store_camera(p_camera, ssr.push_constant.projection);
  417. 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]);
  418. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  419. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  420. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  421. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  422. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_metallic, p_normal_roughness), 3);
  423. } else {
  424. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  425. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  426. }
  427. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  428. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  429. }
  430. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  431. //blur
  432. RD::get_singleton()->compute_list_add_barrier(compute_list);
  433. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  434. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  435. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  436. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  437. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  438. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  439. ssr_filter.push_constant.vertical = 0;
  440. if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_LOW) {
  441. ssr_filter.push_constant.steps = p_max_steps / 3;
  442. ssr_filter.push_constant.increment = 3;
  443. } else if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_MEDIUM) {
  444. ssr_filter.push_constant.steps = p_max_steps / 2;
  445. ssr_filter.push_constant.increment = 2;
  446. } else {
  447. ssr_filter.push_constant.steps = p_max_steps;
  448. ssr_filter.push_constant.increment = 1;
  449. }
  450. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  451. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  452. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  453. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  454. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  455. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  456. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  457. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  458. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  459. RD::get_singleton()->compute_list_add_barrier(compute_list);
  460. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  461. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  462. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  463. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  464. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  465. ssr_filter.push_constant.vertical = 1;
  466. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  467. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  468. }
  469. RD::get_singleton()->compute_list_end();
  470. }
  471. void EffectsRD::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) {
  472. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  473. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  474. p.normal /= p.d;
  475. float unit_size = p.normal.x;
  476. { //scale color and depth to half
  477. sss.push_constant.camera_z_far = p_camera.get_z_far();
  478. sss.push_constant.camera_z_near = p_camera.get_z_near();
  479. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  480. sss.push_constant.unit_size = unit_size;
  481. sss.push_constant.screen_size[0] = p_screen_size.x;
  482. sss.push_constant.screen_size[1] = p_screen_size.y;
  483. sss.push_constant.vertical = false;
  484. sss.push_constant.scale = p_scale;
  485. sss.push_constant.depth_scale = p_depth_scale;
  486. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  487. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  488. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  489. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  490. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  491. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  492. RD::get_singleton()->compute_list_add_barrier(compute_list);
  493. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  494. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  495. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  496. sss.push_constant.vertical = true;
  497. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  498. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  499. RD::get_singleton()->compute_list_end();
  500. }
  501. }
  502. void EffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  503. 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>());
  504. if (p_reflection.is_valid()) {
  505. if (p_base.is_valid()) {
  506. 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)));
  507. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  508. } else {
  509. 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)));
  510. }
  511. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  512. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  513. } else {
  514. if (p_base.is_valid()) {
  515. 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)));
  516. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  517. } else {
  518. 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)));
  519. }
  520. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  521. }
  522. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  523. RD::get_singleton()->draw_list_draw(draw_list, true);
  524. RD::get_singleton()->draw_list_end();
  525. }
  526. void EffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  527. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  528. copy.push_constant.section[0] = 0;
  529. copy.push_constant.section[1] = 0;
  530. copy.push_constant.section[2] = p_size.width;
  531. copy.push_constant.section[3] = p_size.height;
  532. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  533. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_MIPMAP]);
  534. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  535. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  536. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  537. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  538. RD::get_singleton()->compute_list_end();
  539. }
  540. void EffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, float p_z_near, float p_z_far, bool p_dp_flip) {
  541. CopyToDPPushConstant push_constant;
  542. push_constant.screen_rect[0] = p_rect.position.x;
  543. push_constant.screen_rect[1] = p_rect.position.y;
  544. push_constant.screen_rect[2] = p_rect.size.width;
  545. push_constant.screen_rect[3] = p_rect.size.height;
  546. push_constant.z_far = p_z_far;
  547. push_constant.z_near = p_z_near;
  548. push_constant.z_flip = p_dp_flip;
  549. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ);
  550. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cube_to_dp.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
  551. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  552. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  553. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  554. RD::get_singleton()->draw_list_draw(draw_list, true);
  555. RD::get_singleton()->draw_list_end(RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_TRANSFER);
  556. }
  557. void EffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  558. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  559. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  560. tonemap.push_constant.bcs[0] = p_settings.brightness;
  561. tonemap.push_constant.bcs[1] = p_settings.contrast;
  562. tonemap.push_constant.bcs[2] = p_settings.saturation;
  563. tonemap.push_constant.use_glow = p_settings.use_glow;
  564. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  565. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  566. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  567. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  568. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  569. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  570. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  571. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  572. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  573. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  574. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  575. int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  576. if (p_settings.use_1d_color_correction) {
  577. mode += 2;
  578. }
  579. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  580. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  581. tonemap.push_constant.exposure = p_settings.exposure;
  582. tonemap.push_constant.white = p_settings.white;
  583. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  584. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  585. tonemap.push_constant.use_fxaa = p_settings.use_fxaa;
  586. tonemap.push_constant.use_debanding = p_settings.use_debanding;
  587. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  588. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  589. if (p_settings.view_count > 1) {
  590. // Use MULTIVIEW versions
  591. mode += 4;
  592. }
  593. 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);
  594. 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)));
  595. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  596. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  597. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  598. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  599. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  600. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  601. RD::get_singleton()->draw_list_draw(draw_list, true);
  602. RD::get_singleton()->draw_list_end();
  603. }
  604. 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) {
  605. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  606. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  607. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  608. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  609. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  610. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  611. for (int i = 0; i < p_reduce.size(); i++) {
  612. if (i == 0) {
  613. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  614. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  615. } else {
  616. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  617. if (i == p_reduce.size() - 1 && !p_set) {
  618. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  619. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  620. } else {
  621. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  622. }
  623. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  624. }
  625. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  626. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  627. RD::get_singleton()->compute_list_dispatch_threads(compute_list, luminance_reduce.push_constant.source_size[0], luminance_reduce.push_constant.source_size[1], 1);
  628. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  629. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  630. }
  631. RD::get_singleton()->compute_list_end();
  632. }
  633. void EffectsRD::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) {
  634. bokeh.push_constant.blur_far_active = p_dof_far;
  635. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  636. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  637. bokeh.push_constant.blur_near_active = p_dof_near;
  638. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  639. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  640. bokeh.push_constant.use_jitter = p_use_jitter;
  641. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  642. bokeh.push_constant.z_near = p_cam_znear;
  643. bokeh.push_constant.z_far = p_cam_zfar;
  644. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  645. bokeh.push_constant.blur_size = p_bokeh_size;
  646. bokeh.push_constant.second_pass = false;
  647. bokeh.push_constant.half_size = false;
  648. bokeh.push_constant.blur_scale = 0.5;
  649. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  650. /* FIRST PASS */
  651. // The alpha channel of the source color texture is filled with the expected circle size
  652. // If used for DOF far, the size is positive, if used for near, its negative.
  653. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BLUR_SIZE]);
  654. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  655. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  656. bokeh.push_constant.size[0] = p_base_texture_size.x;
  657. bokeh.push_constant.size[1] = p_base_texture_size.y;
  658. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  659. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_base_texture_size.x, p_base_texture_size.y, 1);
  660. RD::get_singleton()->compute_list_add_barrier(compute_list);
  661. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  662. //second pass
  663. 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]);
  664. static const int quality_samples[4] = { 6, 12, 12, 24 };
  665. bokeh.push_constant.steps = quality_samples[p_quality];
  666. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  667. //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)
  668. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  669. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  670. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  671. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  672. bokeh.push_constant.half_size = true;
  673. bokeh.push_constant.blur_size *= 0.5;
  674. } else {
  675. //medium and high quality use full size
  676. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 0);
  677. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  678. }
  679. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  680. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  681. RD::get_singleton()->compute_list_add_barrier(compute_list);
  682. //third pass
  683. bokeh.push_constant.second_pass = true;
  684. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  685. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture2), 0);
  686. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  687. } else {
  688. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  689. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 1);
  690. }
  691. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  692. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  693. RD::get_singleton()->compute_list_add_barrier(compute_list);
  694. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  695. //forth pass, upscale for low quality
  696. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  697. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  698. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture2), 1);
  699. bokeh.push_constant.size[0] = p_base_texture_size.x;
  700. bokeh.push_constant.size[1] = p_base_texture_size.y;
  701. bokeh.push_constant.half_size = false;
  702. bokeh.push_constant.second_pass = false;
  703. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  704. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_base_texture_size.x, p_base_texture_size.y, 1);
  705. }
  706. } else {
  707. //circle
  708. //second pass
  709. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  710. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  711. bokeh.push_constant.steps = 0;
  712. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  713. //circle always runs in half size, otherwise too expensive
  714. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  715. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  716. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  717. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  718. bokeh.push_constant.half_size = true;
  719. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  720. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  721. RD::get_singleton()->compute_list_add_barrier(compute_list);
  722. //circle is just one pass, then upscale
  723. // upscale
  724. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  725. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  726. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  727. bokeh.push_constant.size[0] = p_base_texture_size.x;
  728. bokeh.push_constant.size[1] = p_base_texture_size.y;
  729. bokeh.push_constant.half_size = false;
  730. bokeh.push_constant.second_pass = false;
  731. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  732. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_base_texture_size.x, p_base_texture_size.y, 1);
  733. }
  734. RD::get_singleton()->compute_list_end();
  735. }
  736. void EffectsRD::gather_ssao(RD::ComputeListID p_compute_list, const Vector<RID> p_ao_slices, const SSAOSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set) {
  737. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  738. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  739. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  740. }
  741. for (int i = 0; i < 4; i++) {
  742. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  743. continue;
  744. }
  745. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  746. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  747. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  748. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  749. ssao.gather_push_constant.pass = i;
  750. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 2);
  751. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  752. Size2i size = Size2i(p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1), p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  753. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  754. }
  755. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  756. }
  757. void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, RID p_depth_mipmaps_texture, const Vector<RID> &p_depth_mipmaps, RID p_ao, const Vector<RID> p_ao_slices, RID p_ao_pong, const Vector<RID> p_ao_pong_slices, RID p_upscale_buffer, RID p_importance_map, RID p_importance_map_pong, const CameraMatrix &p_projection, const SSAOSettings &p_settings, bool p_invalidate_uniform_sets, RID &r_downsample_uniform_set, RID &r_gather_uniform_set, RID &r_importance_map_uniform_set) {
  758. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  759. RD::get_singleton()->draw_command_begin_label("SSAO");
  760. /* FIRST PASS */
  761. // Downsample and deinterleave the depth buffer.
  762. {
  763. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  764. if (p_invalidate_uniform_sets) {
  765. Vector<RD::Uniform> uniforms;
  766. {
  767. RD::Uniform u;
  768. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  769. u.binding = 0;
  770. u.ids.push_back(p_depth_mipmaps[1]);
  771. uniforms.push_back(u);
  772. }
  773. {
  774. RD::Uniform u;
  775. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  776. u.binding = 1;
  777. u.ids.push_back(p_depth_mipmaps[2]);
  778. uniforms.push_back(u);
  779. }
  780. {
  781. RD::Uniform u;
  782. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  783. u.binding = 2;
  784. u.ids.push_back(p_depth_mipmaps[3]);
  785. uniforms.push_back(u);
  786. }
  787. r_downsample_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.downsample_shader.version_get_shader(ssao.downsample_shader_version, 2), 2);
  788. }
  789. float depth_linearize_mul = -p_projection.matrix[3][2];
  790. float depth_linearize_add = p_projection.matrix[2][2];
  791. if (depth_linearize_mul * depth_linearize_add < 0) {
  792. depth_linearize_add = -depth_linearize_add;
  793. }
  794. ssao.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  795. ssao.downsample_push_constant.z_near = depth_linearize_mul;
  796. ssao.downsample_push_constant.z_far = depth_linearize_add;
  797. if (ssao.downsample_push_constant.orthogonal) {
  798. ssao.downsample_push_constant.z_near = p_projection.get_z_near();
  799. ssao.downsample_push_constant.z_far = p_projection.get_z_far();
  800. }
  801. ssao.downsample_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  802. ssao.downsample_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  803. ssao.downsample_push_constant.radius_sq = p_settings.radius * p_settings.radius;
  804. int downsample_pipeline = SSAO_DOWNSAMPLE;
  805. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  806. downsample_pipeline = SSAO_DOWNSAMPLE_HALF;
  807. } else if (p_settings.quality > RS::ENV_SSAO_QUALITY_MEDIUM) {
  808. downsample_pipeline = SSAO_DOWNSAMPLE_MIPMAP;
  809. }
  810. if (p_settings.half_size) {
  811. downsample_pipeline++;
  812. }
  813. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[downsample_pipeline]);
  814. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  815. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_depth_mipmaps[0]), 1);
  816. if (p_settings.quality > RS::ENV_SSAO_QUALITY_MEDIUM) {
  817. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, r_downsample_uniform_set, 2);
  818. }
  819. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.downsample_push_constant, sizeof(SSAODownsamplePushConstant));
  820. Size2i size(MAX(1, p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1)), MAX(1, p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1)));
  821. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  822. RD::get_singleton()->compute_list_add_barrier(compute_list);
  823. RD::get_singleton()->draw_command_end_label(); // Downsample SSAO
  824. }
  825. /* SECOND PASS */
  826. // Sample SSAO
  827. {
  828. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  829. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  830. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  831. ssao.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  832. ssao.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  833. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  834. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  835. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  836. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  837. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  838. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  839. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  840. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  841. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  842. float radius_near_limit = (p_settings.radius * 1.2f);
  843. if (p_settings.quality <= RS::ENV_SSAO_QUALITY_LOW) {
  844. radius_near_limit *= 1.50f;
  845. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  846. ssao.gather_push_constant.radius *= 0.8f;
  847. }
  848. if (p_settings.half_size) {
  849. ssao.gather_push_constant.radius *= 0.5f;
  850. }
  851. }
  852. radius_near_limit /= tan_half_fov_y;
  853. ssao.gather_push_constant.radius = p_settings.radius;
  854. ssao.gather_push_constant.intensity = p_settings.intensity;
  855. ssao.gather_push_constant.shadow_power = p_settings.power;
  856. ssao.gather_push_constant.shadow_clamp = 0.98;
  857. ssao.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  858. ssao.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  859. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  860. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  861. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  862. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_settings.quarter_screen_size.x) * (p_settings.quarter_screen_size.y) * 255);
  863. ssao.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  864. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  865. ssao.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  866. ssao.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  867. if (p_invalidate_uniform_sets) {
  868. Vector<RD::Uniform> uniforms;
  869. {
  870. RD::Uniform u;
  871. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  872. u.binding = 0;
  873. u.ids.push_back(ssao.mirror_sampler);
  874. u.ids.push_back(p_depth_mipmaps_texture);
  875. uniforms.push_back(u);
  876. }
  877. {
  878. RD::Uniform u;
  879. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  880. u.binding = 1;
  881. u.ids.push_back(p_normal_buffer);
  882. uniforms.push_back(u);
  883. }
  884. {
  885. RD::Uniform u;
  886. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  887. u.binding = 2;
  888. u.ids.push_back(ssao.gather_constants_buffer);
  889. uniforms.push_back(u);
  890. }
  891. r_gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 0), 0);
  892. }
  893. if (p_invalidate_uniform_sets) {
  894. Vector<RD::Uniform> uniforms;
  895. {
  896. RD::Uniform u;
  897. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  898. u.binding = 0;
  899. u.ids.push_back(p_ao_pong);
  900. uniforms.push_back(u);
  901. }
  902. {
  903. RD::Uniform u;
  904. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  905. u.binding = 1;
  906. u.ids.push_back(default_sampler);
  907. u.ids.push_back(p_importance_map);
  908. uniforms.push_back(u);
  909. }
  910. {
  911. RD::Uniform u;
  912. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  913. u.binding = 2;
  914. u.ids.push_back(ssao.importance_map_load_counter);
  915. uniforms.push_back(u);
  916. }
  917. r_importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 2), 1);
  918. }
  919. if (p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  920. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  921. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  922. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  923. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  924. ssao.importance_map_push_constant.power = p_settings.power;
  925. //base pass
  926. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE]);
  927. gather_ssao(compute_list, p_ao_pong_slices, p_settings, true, r_gather_uniform_set, RID());
  928. //generate importance map
  929. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP]);
  930. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 0);
  931. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  932. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  933. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  934. RD::get_singleton()->compute_list_add_barrier(compute_list);
  935. //process importance map A
  936. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA]);
  937. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map), 0);
  938. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map_pong), 1);
  939. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  940. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  941. RD::get_singleton()->compute_list_add_barrier(compute_list);
  942. //process Importance Map B
  943. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB]);
  944. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map_pong), 0);
  945. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  946. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  947. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  948. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  949. RD::get_singleton()->compute_list_add_barrier(compute_list);
  950. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
  951. RD::get_singleton()->draw_command_end_label(); // Importance Map
  952. } else {
  953. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
  954. }
  955. gather_ssao(compute_list, p_ao_slices, p_settings, false, r_gather_uniform_set, r_importance_map_uniform_set);
  956. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  957. }
  958. // /* THIRD PASS */
  959. // // Blur
  960. //
  961. {
  962. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  963. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  964. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  965. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  966. int blur_passes = p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  967. for (int pass = 0; pass < blur_passes; pass++) {
  968. int blur_pipeline = SSAO_BLUR_PASS;
  969. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  970. if (pass < blur_passes - 2) {
  971. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  972. }
  973. blur_pipeline = SSAO_BLUR_PASS_SMART;
  974. }
  975. for (int i = 0; i < 4; i++) {
  976. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  977. continue;
  978. }
  979. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline]);
  980. if (pass % 2 == 0) {
  981. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  982. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_slices[i]), 0);
  983. } else {
  984. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_slices[i], ssao.mirror_sampler), 0);
  985. }
  986. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_pong_slices[i]), 1);
  987. } else {
  988. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  989. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong_slices[i]), 0);
  990. } else {
  991. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_pong_slices[i], ssao.mirror_sampler), 0);
  992. }
  993. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 1);
  994. }
  995. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  996. Size2i size(p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1), p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  997. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  998. }
  999. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1000. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1001. }
  1002. }
  1003. RD::get_singleton()->draw_command_end_label(); // Blur
  1004. }
  1005. /* FOURTH PASS */
  1006. // Interleave buffers
  1007. // back to full size
  1008. {
  1009. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1010. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1011. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1012. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1013. ssao.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  1014. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  1015. if (p_settings.quality == RS::ENV_SSAO_QUALITY_LOW) {
  1016. interleave_pipeline = SSAO_INTERLEAVE;
  1017. } else if (p_settings.quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  1018. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  1019. }
  1020. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline]);
  1021. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 0);
  1022. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  1023. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao), 1);
  1024. } else {
  1025. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 1);
  1026. }
  1027. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  1028. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1029. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1030. RD::get_singleton()->draw_command_end_label(); // Interleave
  1031. }
  1032. RD::get_singleton()->draw_command_end_label(); //SSAO
  1033. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_TRANSFER); //wait for upcoming transfer
  1034. int zero[1] = { 0 };
  1035. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  1036. }
  1037. void EffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  1038. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  1039. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  1040. roughness_limiter.push_constant.curve = p_curve;
  1041. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1042. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  1043. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  1044. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  1045. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  1046. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
  1047. RD::get_singleton()->compute_list_end();
  1048. }
  1049. void EffectsRD::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) {
  1050. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  1051. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  1052. roughness.push_constant.roughness = p_roughness;
  1053. roughness.push_constant.sample_count = p_sample_count;
  1054. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1055. roughness.push_constant.face_size = p_size;
  1056. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1057. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.pipeline);
  1058. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  1059. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_framebuffer), 1);
  1060. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  1061. int x_groups = (p_size - 1) / 8 + 1;
  1062. int y_groups = (p_size - 1) / 8 + 1;
  1063. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  1064. RD::get_singleton()->compute_list_end();
  1065. }
  1066. void EffectsRD::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  1067. cubemap_downsampler.push_constant.face_size = p_size.x;
  1068. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1069. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.pipeline);
  1070. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap), 0);
  1071. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_cubemap), 1);
  1072. int x_groups = (p_size.x - 1) / 8 + 1;
  1073. int y_groups = (p_size.y - 1) / 8 + 1;
  1074. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  1075. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  1076. RD::get_singleton()->compute_list_end();
  1077. }
  1078. void EffectsRD::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  1079. Vector<RD::Uniform> uniforms;
  1080. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  1081. RD::Uniform u;
  1082. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1083. u.binding = i;
  1084. u.ids.push_back(p_dest_cubemap[i]);
  1085. uniforms.push_back(u);
  1086. }
  1087. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1088. RD::get_singleton()->free(filter.image_uniform_set);
  1089. }
  1090. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.shader.version_get_shader(filter.shader_version, 0), 2);
  1091. int pipeline = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  1092. pipeline = filter.use_high_quality ? pipeline : pipeline + 1;
  1093. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1094. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.pipelines[pipeline]);
  1095. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap, true), 0);
  1096. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  1097. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  1098. 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
  1099. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  1100. RD::get_singleton()->compute_list_end();
  1101. }
  1102. void EffectsRD::resolve_gi(RID p_source_depth, RID p_source_normal_roughness, RID p_source_voxel_gi, RID p_dest_depth, RID p_dest_normal_roughness, RID p_dest_voxel_gi, Vector2i p_screen_size, int p_samples, uint32_t p_barrier) {
  1103. ResolvePushConstant push_constant;
  1104. push_constant.screen_size[0] = p_screen_size.x;
  1105. push_constant.screen_size[1] = p_screen_size.y;
  1106. push_constant.samples = p_samples;
  1107. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1108. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[p_source_voxel_gi.is_valid() ? RESOLVE_MODE_GI_VOXEL_GI : RESOLVE_MODE_GI]);
  1109. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_source_depth, p_source_normal_roughness), 0);
  1110. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_dest_depth, p_dest_normal_roughness), 1);
  1111. if (p_source_voxel_gi.is_valid()) {
  1112. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_voxel_gi), 2);
  1113. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_voxel_gi), 3);
  1114. }
  1115. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  1116. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
  1117. RD::get_singleton()->compute_list_end(p_barrier);
  1118. }
  1119. void EffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  1120. Sort::PushConstant push_constant;
  1121. push_constant.total_elements = p_size;
  1122. bool done = true;
  1123. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  1124. if (numThreadGroups > 1) {
  1125. done = false;
  1126. }
  1127. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1128. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  1129. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  1130. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1131. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1132. int presorted = 512;
  1133. while (!done) {
  1134. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1135. done = true;
  1136. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  1137. numThreadGroups = 0;
  1138. if (p_size > presorted) {
  1139. if (p_size > presorted * 2) {
  1140. done = false;
  1141. }
  1142. int pow2 = presorted;
  1143. while (pow2 < p_size) {
  1144. pow2 *= 2;
  1145. }
  1146. numThreadGroups = pow2 >> 9;
  1147. }
  1148. unsigned int nMergeSize = presorted * 2;
  1149. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  1150. push_constant.job_params[0] = nMergeSubSize;
  1151. if (nMergeSubSize == nMergeSize >> 1) {
  1152. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  1153. push_constant.job_params[2] = -1;
  1154. } else {
  1155. push_constant.job_params[1] = nMergeSubSize;
  1156. push_constant.job_params[2] = 1;
  1157. }
  1158. push_constant.job_params[3] = 0;
  1159. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1160. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1161. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1162. }
  1163. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  1164. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1165. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1166. presorted *= 2;
  1167. }
  1168. RD::get_singleton()->compute_list_end();
  1169. }
  1170. EffectsRD::EffectsRD() {
  1171. { // Initialize copy
  1172. Vector<String> copy_modes;
  1173. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  1174. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  1175. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  1176. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  1177. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  1178. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  1179. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  1180. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  1181. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  1182. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  1183. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  1184. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  1185. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  1186. copy.shader.initialize(copy_modes);
  1187. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  1188. copy.shader_version = copy.shader.version_create();
  1189. for (int i = 0; i < COPY_MODE_MAX; i++) {
  1190. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  1191. }
  1192. }
  1193. {
  1194. Vector<String> copy_modes;
  1195. copy_modes.push_back("\n");
  1196. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n");
  1197. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n");
  1198. copy_to_fb.shader.initialize(copy_modes);
  1199. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  1200. //use additive
  1201. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  1202. copy_to_fb.pipelines[i].setup(copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  1203. }
  1204. }
  1205. {
  1206. // Initialize roughness
  1207. Vector<String> cubemap_roughness_modes;
  1208. cubemap_roughness_modes.push_back("");
  1209. roughness.shader.initialize(cubemap_roughness_modes);
  1210. roughness.shader_version = roughness.shader.version_create();
  1211. roughness.pipeline = RD::get_singleton()->compute_pipeline_create(roughness.shader.version_get_shader(roughness.shader_version, 0));
  1212. }
  1213. {
  1214. // Initialize tonemapper
  1215. Vector<String> tonemap_modes;
  1216. tonemap_modes.push_back("\n");
  1217. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  1218. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  1219. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1220. // multiview versions of our shaders
  1221. tonemap_modes.push_back("\n#define MULTIVIEW\n");
  1222. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n");
  1223. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_1D_LUT\n");
  1224. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1225. tonemap.shader.initialize(tonemap_modes);
  1226. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  1227. tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false);
  1228. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false);
  1229. tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false);
  1230. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false);
  1231. }
  1232. tonemap.shader_version = tonemap.shader.version_create();
  1233. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  1234. if (tonemap.shader.is_variant_enabled(i)) {
  1235. 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);
  1236. } else {
  1237. tonemap.pipelines[i].clear();
  1238. }
  1239. }
  1240. }
  1241. {
  1242. // Initialize luminance_reduce
  1243. Vector<String> luminance_reduce_modes;
  1244. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  1245. luminance_reduce_modes.push_back("\n");
  1246. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  1247. luminance_reduce.shader.initialize(luminance_reduce_modes);
  1248. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  1249. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  1250. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  1251. }
  1252. }
  1253. {
  1254. // Initialize copier
  1255. Vector<String> copy_modes;
  1256. copy_modes.push_back("\n");
  1257. cube_to_dp.shader.initialize(copy_modes);
  1258. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  1259. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  1260. RD::PipelineDepthStencilState dss;
  1261. dss.enable_depth_test = true;
  1262. dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
  1263. dss.enable_depth_write = true;
  1264. cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
  1265. }
  1266. {
  1267. // Initialize bokeh
  1268. Vector<String> bokeh_modes;
  1269. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  1270. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  1271. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  1272. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n");
  1273. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  1274. bokeh.shader.initialize(bokeh_modes);
  1275. bokeh.shader_version = bokeh.shader.version_create();
  1276. for (int i = 0; i < BOKEH_MAX; i++) {
  1277. bokeh.pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.shader.version_get_shader(bokeh.shader_version, i));
  1278. }
  1279. }
  1280. {
  1281. // Initialize ssao
  1282. RD::SamplerState sampler;
  1283. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1284. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1285. sampler.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1286. sampler.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1287. sampler.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1288. sampler.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1289. sampler.max_lod = 4;
  1290. ssao.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  1291. uint32_t pipeline = 0;
  1292. {
  1293. Vector<String> ssao_modes;
  1294. ssao_modes.push_back("\n");
  1295. ssao_modes.push_back("\n#define USE_HALF_SIZE\n");
  1296. ssao_modes.push_back("\n#define GENERATE_MIPS\n");
  1297. ssao_modes.push_back("\n#define GENERATE_MIPS\n#define USE_HALF_SIZE");
  1298. ssao_modes.push_back("\n#define USE_HALF_BUFFERS\n");
  1299. ssao_modes.push_back("\n#define USE_HALF_BUFFERS\n#define USE_HALF_SIZE");
  1300. ssao.downsample_shader.initialize(ssao_modes);
  1301. ssao.downsample_shader_version = ssao.downsample_shader.version_create();
  1302. for (int i = 0; i <= SSAO_DOWNSAMPLE_HALF_RES_HALF; i++) {
  1303. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.downsample_shader.version_get_shader(ssao.downsample_shader_version, i));
  1304. pipeline++;
  1305. }
  1306. }
  1307. {
  1308. Vector<String> ssao_modes;
  1309. ssao_modes.push_back("\n");
  1310. ssao_modes.push_back("\n#define SSAO_BASE\n");
  1311. ssao_modes.push_back("\n#define ADAPTIVE\n");
  1312. ssao.gather_shader.initialize(ssao_modes);
  1313. ssao.gather_shader_version = ssao.gather_shader.version_create();
  1314. for (int i = SSAO_GATHER; i <= SSAO_GATHER_ADAPTIVE; i++) {
  1315. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i - SSAO_GATHER));
  1316. pipeline++;
  1317. }
  1318. ssao.gather_constants_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSAOGatherConstants));
  1319. SSAOGatherConstants gather_constants;
  1320. const int sub_pass_count = 5;
  1321. for (int pass = 0; pass < 4; pass++) {
  1322. for (int subPass = 0; subPass < sub_pass_count; subPass++) {
  1323. int a = pass;
  1324. int b = subPass;
  1325. int spmap[5]{ 0, 1, 4, 3, 2 };
  1326. b = spmap[subPass];
  1327. float ca, sa;
  1328. float angle0 = (float(a) + float(b) / float(sub_pass_count)) * Math_PI * 0.5f;
  1329. ca = Math::cos(angle0);
  1330. sa = Math::sin(angle0);
  1331. float scale = 1.0f + (a - 1.5f + (b - (sub_pass_count - 1.0f) * 0.5f) / float(sub_pass_count)) * 0.07f;
  1332. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 0] = scale * ca;
  1333. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 1] = scale * -sa;
  1334. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 2] = -scale * sa;
  1335. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 3] = -scale * ca;
  1336. }
  1337. }
  1338. RD::get_singleton()->buffer_update(ssao.gather_constants_buffer, 0, sizeof(SSAOGatherConstants), &gather_constants);
  1339. }
  1340. {
  1341. Vector<String> ssao_modes;
  1342. ssao_modes.push_back("\n#define GENERATE_MAP\n");
  1343. ssao_modes.push_back("\n#define PROCESS_MAPA\n");
  1344. ssao_modes.push_back("\n#define PROCESS_MAPB\n");
  1345. ssao.importance_map_shader.initialize(ssao_modes);
  1346. ssao.importance_map_shader_version = ssao.importance_map_shader.version_create();
  1347. for (int i = SSAO_GENERATE_IMPORTANCE_MAP; i <= SSAO_PROCESS_IMPORTANCE_MAPB; i++) {
  1348. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, i - SSAO_GENERATE_IMPORTANCE_MAP));
  1349. pipeline++;
  1350. }
  1351. ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  1352. int zero[1] = { 0 };
  1353. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  1354. RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");
  1355. Vector<RD::Uniform> uniforms;
  1356. {
  1357. RD::Uniform u;
  1358. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1359. u.binding = 0;
  1360. u.ids.push_back(ssao.importance_map_load_counter);
  1361. uniforms.push_back(u);
  1362. }
  1363. ssao.counter_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.importance_map_shader.version_get_shader(ssao.importance_map_shader_version, 2), 2);
  1364. RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
  1365. }
  1366. {
  1367. Vector<String> ssao_modes;
  1368. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  1369. ssao_modes.push_back("\n#define MODE_SMART\n");
  1370. ssao_modes.push_back("\n#define MODE_WIDE\n");
  1371. ssao.blur_shader.initialize(ssao_modes);
  1372. ssao.blur_shader_version = ssao.blur_shader.version_create();
  1373. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_PASS_WIDE; i++) {
  1374. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  1375. pipeline++;
  1376. }
  1377. }
  1378. {
  1379. Vector<String> ssao_modes;
  1380. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  1381. ssao_modes.push_back("\n#define MODE_SMART\n");
  1382. ssao_modes.push_back("\n#define MODE_HALF\n");
  1383. ssao.interleave_shader.initialize(ssao_modes);
  1384. ssao.interleave_shader_version = ssao.interleave_shader.version_create();
  1385. for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
  1386. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  1387. RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
  1388. pipeline++;
  1389. }
  1390. }
  1391. ERR_FAIL_COND(pipeline != SSAO_MAX);
  1392. }
  1393. {
  1394. // Initialize roughness limiter
  1395. Vector<String> shader_modes;
  1396. shader_modes.push_back("");
  1397. roughness_limiter.shader.initialize(shader_modes);
  1398. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  1399. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  1400. }
  1401. {
  1402. //Initialize cubemap downsampler
  1403. Vector<String> cubemap_downsampler_modes;
  1404. cubemap_downsampler_modes.push_back("");
  1405. cubemap_downsampler.shader.initialize(cubemap_downsampler_modes);
  1406. cubemap_downsampler.shader_version = cubemap_downsampler.shader.version_create();
  1407. cubemap_downsampler.pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  1408. }
  1409. {
  1410. // Initialize cubemap filter
  1411. filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
  1412. Vector<String> cubemap_filter_modes;
  1413. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  1414. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  1415. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1416. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1417. filter.shader.initialize(cubemap_filter_modes);
  1418. filter.shader_version = filter.shader.version_create();
  1419. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  1420. filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.shader.version_get_shader(filter.shader_version, i));
  1421. }
  1422. if (filter.use_high_quality) {
  1423. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  1424. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
  1425. } else {
  1426. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  1427. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
  1428. }
  1429. Vector<RD::Uniform> uniforms;
  1430. {
  1431. RD::Uniform u;
  1432. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1433. u.binding = 0;
  1434. u.ids.push_back(filter.coefficient_buffer);
  1435. uniforms.push_back(u);
  1436. }
  1437. 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);
  1438. }
  1439. {
  1440. Vector<String> specular_modes;
  1441. specular_modes.push_back("\n#define MODE_MERGE\n");
  1442. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  1443. specular_modes.push_back("\n");
  1444. specular_modes.push_back("\n#define MODE_SSR\n");
  1445. specular_merge.shader.initialize(specular_modes);
  1446. specular_merge.shader_version = specular_merge.shader.version_create();
  1447. //use additive
  1448. RD::PipelineColorBlendState::Attachment ba;
  1449. ba.enable_blend = true;
  1450. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1451. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1452. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1453. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1454. ba.color_blend_op = RD::BLEND_OP_ADD;
  1455. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  1456. RD::PipelineColorBlendState blend_additive;
  1457. blend_additive.attachments.push_back(ba);
  1458. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  1459. RD::PipelineColorBlendState blend_state;
  1460. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  1461. blend_state = blend_additive;
  1462. } else {
  1463. blend_state = RD::PipelineColorBlendState::create_disabled();
  1464. }
  1465. 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);
  1466. }
  1467. }
  1468. {
  1469. Vector<String> ssr_modes;
  1470. ssr_modes.push_back("\n");
  1471. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  1472. ssr.shader.initialize(ssr_modes);
  1473. ssr.shader_version = ssr.shader.version_create();
  1474. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  1475. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  1476. }
  1477. }
  1478. {
  1479. Vector<String> ssr_filter_modes;
  1480. ssr_filter_modes.push_back("\n");
  1481. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  1482. ssr_filter.shader.initialize(ssr_filter_modes);
  1483. ssr_filter.shader_version = ssr_filter.shader.version_create();
  1484. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  1485. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  1486. }
  1487. }
  1488. {
  1489. Vector<String> ssr_scale_modes;
  1490. ssr_scale_modes.push_back("\n");
  1491. ssr_scale.shader.initialize(ssr_scale_modes);
  1492. ssr_scale.shader_version = ssr_scale.shader.version_create();
  1493. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  1494. }
  1495. {
  1496. Vector<String> sss_modes;
  1497. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  1498. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  1499. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  1500. sss.shader.initialize(sss_modes);
  1501. sss.shader_version = sss.shader.version_create();
  1502. for (int i = 0; i < sss_modes.size(); i++) {
  1503. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  1504. }
  1505. }
  1506. {
  1507. Vector<String> resolve_modes;
  1508. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n");
  1509. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define VOXEL_GI_RESOLVE\n");
  1510. resolve.shader.initialize(resolve_modes);
  1511. resolve.shader_version = resolve.shader.version_create();
  1512. for (int i = 0; i < RESOLVE_MODE_MAX; i++) {
  1513. resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i));
  1514. }
  1515. }
  1516. {
  1517. Vector<String> sort_modes;
  1518. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  1519. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  1520. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  1521. sort.shader.initialize(sort_modes);
  1522. sort.shader_version = sort.shader.version_create();
  1523. for (int i = 0; i < SORT_MODE_MAX; i++) {
  1524. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  1525. }
  1526. }
  1527. RD::SamplerState sampler;
  1528. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1529. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1530. sampler.max_lod = 0;
  1531. default_sampler = RD::get_singleton()->sampler_create(sampler);
  1532. RD::get_singleton()->set_resource_name(default_sampler, "Default Linear Sampler");
  1533. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1534. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1535. sampler.max_lod = 1e20;
  1536. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  1537. RD::get_singleton()->set_resource_name(default_mipmap_sampler, "Default MipMap Sampler");
  1538. { //create index array for copy shaders
  1539. Vector<uint8_t> pv;
  1540. pv.resize(6 * 4);
  1541. {
  1542. uint8_t *w = pv.ptrw();
  1543. int *p32 = (int *)w;
  1544. p32[0] = 0;
  1545. p32[1] = 1;
  1546. p32[2] = 2;
  1547. p32[3] = 0;
  1548. p32[4] = 2;
  1549. p32[5] = 3;
  1550. }
  1551. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1552. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  1553. }
  1554. }
  1555. EffectsRD::~EffectsRD() {
  1556. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1557. RD::get_singleton()->free(filter.image_uniform_set);
  1558. }
  1559. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  1560. RD::get_singleton()->free(filter.uniform_set);
  1561. }
  1562. RD::get_singleton()->free(default_sampler);
  1563. RD::get_singleton()->free(default_mipmap_sampler);
  1564. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  1565. RD::get_singleton()->free(filter.coefficient_buffer);
  1566. RD::get_singleton()->free(ssao.mirror_sampler);
  1567. RD::get_singleton()->free(ssao.gather_constants_buffer);
  1568. RD::get_singleton()->free(ssao.importance_map_load_counter);
  1569. bokeh.shader.version_free(bokeh.shader_version);
  1570. copy.shader.version_free(copy.shader_version);
  1571. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  1572. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  1573. cubemap_downsampler.shader.version_free(cubemap_downsampler.shader_version);
  1574. filter.shader.version_free(filter.shader_version);
  1575. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  1576. resolve.shader.version_free(resolve.shader_version);
  1577. roughness.shader.version_free(roughness.shader_version);
  1578. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  1579. sort.shader.version_free(sort.shader_version);
  1580. specular_merge.shader.version_free(specular_merge.shader_version);
  1581. ssao.blur_shader.version_free(ssao.blur_shader_version);
  1582. ssao.gather_shader.version_free(ssao.gather_shader_version);
  1583. ssao.downsample_shader.version_free(ssao.downsample_shader_version);
  1584. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  1585. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  1586. ssr.shader.version_free(ssr.shader_version);
  1587. ssr_filter.shader.version_free(ssr_filter.shader_version);
  1588. ssr_scale.shader.version_free(ssr_scale.shader_version);
  1589. sss.shader.version_free(sss.shader_version);
  1590. tonemap.shader.version_free(tonemap.shader_version);
  1591. }