effects_rd.cpp 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /*************************************************************************/
  2. /* effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "effects_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "thirdparty/misc/cubemap_coeffs.h"
  34. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  35. p_array[0] = p_basis.elements[0][0];
  36. p_array[1] = p_basis.elements[1][0];
  37. p_array[2] = p_basis.elements[2][0];
  38. p_array[3] = 0;
  39. p_array[4] = p_basis.elements[0][1];
  40. p_array[5] = p_basis.elements[1][1];
  41. p_array[6] = p_basis.elements[2][1];
  42. p_array[7] = 0;
  43. p_array[8] = p_basis.elements[0][2];
  44. p_array[9] = p_basis.elements[1][2];
  45. p_array[10] = p_basis.elements[2][2];
  46. p_array[11] = 0;
  47. }
  48. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  49. for (int i = 0; i < 4; i++) {
  50. for (int j = 0; j < 4; j++) {
  51. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  52. }
  53. }
  54. }
  55. RID EffectsRD::_get_uniform_set_from_image(RID p_image) {
  56. if (image_to_uniform_set_cache.has(p_image)) {
  57. RID uniform_set = image_to_uniform_set_cache[p_image];
  58. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  59. return uniform_set;
  60. }
  61. }
  62. Vector<RD::Uniform> uniforms;
  63. RD::Uniform u;
  64. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  65. u.binding = 0;
  66. u.ids.push_back(p_image);
  67. uniforms.push_back(u);
  68. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  69. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  70. image_to_uniform_set_cache[p_image] = uniform_set;
  71. return uniform_set;
  72. }
  73. RID EffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  74. if (texture_to_uniform_set_cache.has(p_texture)) {
  75. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  76. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  77. return uniform_set;
  78. }
  79. }
  80. Vector<RD::Uniform> uniforms;
  81. RD::Uniform u;
  82. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  83. u.binding = 0;
  84. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  85. u.ids.push_back(p_texture);
  86. uniforms.push_back(u);
  87. //anything with the same configuration (one texture in binding 0 for set 0), is good
  88. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, 0), 0);
  89. texture_to_uniform_set_cache[p_texture] = uniform_set;
  90. return uniform_set;
  91. }
  92. RID EffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  93. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  94. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  95. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  96. return uniform_set;
  97. }
  98. }
  99. Vector<RD::Uniform> uniforms;
  100. RD::Uniform u;
  101. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  102. u.binding = 0;
  103. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  104. u.ids.push_back(p_texture);
  105. uniforms.push_back(u);
  106. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  107. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  108. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  109. return uniform_set;
  110. }
  111. RID EffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  112. TexturePair tp;
  113. tp.texture1 = p_texture1;
  114. tp.texture2 = p_texture2;
  115. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  116. RID uniform_set = texture_pair_to_compute_uniform_set_cache[tp];
  117. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  118. return uniform_set;
  119. }
  120. }
  121. Vector<RD::Uniform> uniforms;
  122. {
  123. RD::Uniform u;
  124. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  125. u.binding = 0;
  126. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  127. u.ids.push_back(p_texture1);
  128. uniforms.push_back(u);
  129. }
  130. {
  131. RD::Uniform u;
  132. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  133. u.binding = 1;
  134. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  135. u.ids.push_back(p_texture2);
  136. uniforms.push_back(u);
  137. }
  138. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  139. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  140. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  141. return uniform_set;
  142. }
  143. RID EffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  144. TexturePair tp;
  145. tp.texture1 = p_texture1;
  146. tp.texture2 = p_texture2;
  147. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  148. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  149. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  150. return uniform_set;
  151. }
  152. }
  153. Vector<RD::Uniform> uniforms;
  154. {
  155. RD::Uniform u;
  156. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  157. u.binding = 0;
  158. u.ids.push_back(p_texture1);
  159. uniforms.push_back(u);
  160. }
  161. {
  162. RD::Uniform u;
  163. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  164. u.binding = 1;
  165. u.ids.push_back(p_texture2);
  166. uniforms.push_back(u);
  167. }
  168. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  169. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  170. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  171. return uniform_set;
  172. }
  173. void 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) {
  174. zeromem(&copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  175. copy_to_fb.push_constant.use_section = true;
  176. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  177. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  178. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  179. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  180. if (p_flip_y) {
  181. copy_to_fb.push_constant.flip_y = true;
  182. }
  183. RD::DrawListID draw_list = p_draw_list;
  184. 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)));
  185. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  186. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  187. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  188. RD::get_singleton()->draw_list_draw(draw_list, true);
  189. }
  190. 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) {
  191. zeromem(&copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  192. if (p_flip_y) {
  193. copy_to_fb.push_constant.flip_y = true;
  194. }
  195. if (p_force_luminance) {
  196. copy_to_fb.push_constant.force_luminance = true;
  197. }
  198. if (p_alpha_to_zero) {
  199. copy_to_fb.push_constant.alpha_to_zero = true;
  200. }
  201. if (p_srgb) {
  202. copy_to_fb.push_constant.srgb = true;
  203. }
  204. 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);
  205. 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)));
  206. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  207. if (p_secondary.is_valid()) {
  208. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_secondary), 1);
  209. }
  210. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  211. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  212. RD::get_singleton()->draw_list_draw(draw_list, true);
  213. RD::get_singleton()->draw_list_end();
  214. }
  215. 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) {
  216. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  217. if (p_flip_y) {
  218. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  219. }
  220. if (p_force_luminance) {
  221. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  222. }
  223. if (p_all_source) {
  224. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  225. }
  226. if (p_alpha_to_one) {
  227. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  228. }
  229. copy.push_constant.section[0] = 0;
  230. copy.push_constant.section[1] = 0;
  231. copy.push_constant.section[2] = p_rect.size.width;
  232. copy.push_constant.section[3] = p_rect.size.height;
  233. copy.push_constant.target[0] = p_rect.position.x;
  234. copy.push_constant.target[1] = p_rect.position.y;
  235. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  236. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  237. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  238. 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]);
  239. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  240. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  241. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  242. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  243. RD::get_singleton()->compute_list_end();
  244. }
  245. 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) {
  246. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  247. copy.push_constant.section[0] = 0;
  248. copy.push_constant.section[1] = 0;
  249. copy.push_constant.section[2] = p_panorama_size.width;
  250. copy.push_constant.section[3] = p_panorama_size.height;
  251. copy.push_constant.target[0] = 0;
  252. copy.push_constant.target[1] = 0;
  253. copy.push_constant.camera_z_far = p_lod;
  254. int32_t x_groups = (p_panorama_size.width - 1) / 8 + 1;
  255. int32_t y_groups = (p_panorama_size.height - 1) / 8 + 1;
  256. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  257. 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]);
  258. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cube), 0);
  259. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_panorama), 3);
  260. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  261. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  262. RD::get_singleton()->compute_list_end();
  263. }
  264. 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) {
  265. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  266. if (p_flip_y) {
  267. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  268. }
  269. copy.push_constant.section[0] = 0;
  270. copy.push_constant.section[1] = 0;
  271. copy.push_constant.section[2] = p_rect.size.width;
  272. copy.push_constant.section[3] = p_rect.size.height;
  273. copy.push_constant.target[0] = p_rect.position.x;
  274. copy.push_constant.target[1] = p_rect.position.y;
  275. copy.push_constant.camera_z_far = p_z_far;
  276. copy.push_constant.camera_z_near = p_z_near;
  277. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  278. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  279. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  280. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_LINEARIZE_DEPTH]);
  281. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  282. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  283. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  284. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  285. RD::get_singleton()->compute_list_end();
  286. }
  287. void EffectsRD::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  288. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  289. if (p_flip_y) {
  290. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  291. }
  292. copy.push_constant.section[0] = 0;
  293. copy.push_constant.section[1] = 0;
  294. copy.push_constant.section[2] = p_rect.size.width;
  295. copy.push_constant.section[3] = p_rect.size.height;
  296. copy.push_constant.target[0] = p_rect.position.x;
  297. copy.push_constant.target[1] = p_rect.position.y;
  298. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  299. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  300. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  301. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_SIMPLY_COPY_DEPTH]);
  302. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  303. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  304. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  305. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  306. RD::get_singleton()->compute_list_end();
  307. }
  308. void EffectsRD::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  309. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  310. copy.push_constant.section[0] = 0;
  311. copy.push_constant.section[1] = 0;
  312. copy.push_constant.section[2] = p_region.size.width;
  313. copy.push_constant.section[3] = p_region.size.height;
  314. copy.push_constant.target[0] = p_region.position.x;
  315. copy.push_constant.target[1] = p_region.position.y;
  316. copy.push_constant.set_color[0] = p_color.r;
  317. copy.push_constant.set_color[1] = p_color.g;
  318. copy.push_constant.set_color[2] = p_color.b;
  319. copy.push_constant.set_color[3] = p_color.a;
  320. int32_t x_groups = (p_region.size.width - 1) / 8 + 1;
  321. int32_t y_groups = (p_region.size.height - 1) / 8 + 1;
  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(compute_list, x_groups, y_groups, 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. zeromem(&copy.push_constant, 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. int32_t x_groups = (p_region.size.width - 1) / 8 + 1;
  337. int32_t y_groups = (p_region.size.height - 1) / 8 + 1;
  338. //HORIZONTAL
  339. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  340. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY]);
  341. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  342. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  343. copy.push_constant.flags = base_flags | COPY_FLAG_HORIZONTAL;
  344. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  345. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  346. RD::get_singleton()->compute_list_add_barrier(compute_list);
  347. //VERTICAL
  348. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_back_texture), 0);
  349. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 3);
  350. copy.push_constant.flags = base_flags;
  351. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  352. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  353. RD::get_singleton()->compute_list_end();
  354. }
  355. 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) {
  356. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  357. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  358. uint32_t base_flags = 0;
  359. int32_t x_groups = (p_size.width + 7) / 8;
  360. int32_t y_groups = (p_size.height + 7) / 8;
  361. copy.push_constant.section[2] = p_size.x;
  362. copy.push_constant.section[3] = p_size.y;
  363. copy.push_constant.glow_strength = p_strength;
  364. copy.push_constant.glow_bloom = p_bloom;
  365. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_treshold;
  366. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  367. copy.push_constant.glow_exposure = p_exposure;
  368. copy.push_constant.glow_white = 0; //actually unused
  369. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  370. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  371. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  372. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  373. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  374. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  375. if (p_auto_exposure.is_valid() && p_first_pass) {
  376. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_auto_exposure), 1);
  377. }
  378. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  379. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  380. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  381. RD::get_singleton()->compute_list_end();
  382. }
  383. 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) {
  384. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  385. int32_t x_groups = (p_screen_size.width - 1) / 8 + 1;
  386. int32_t y_groups = (p_screen_size.height - 1) / 8 + 1;
  387. { //scale color and depth to half
  388. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  389. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  390. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  391. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  392. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  393. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  394. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  395. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  396. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal_roughness), 1);
  397. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  398. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  399. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  400. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  401. RD::get_singleton()->compute_list_add_barrier(compute_list);
  402. }
  403. {
  404. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  405. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  406. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  407. ssr.push_constant.screen_size[0] = p_screen_size.x;
  408. ssr.push_constant.screen_size[1] = p_screen_size.y;
  409. ssr.push_constant.curve_fade_in = p_fade_in;
  410. ssr.push_constant.distance_fade = p_fade_out;
  411. ssr.push_constant.num_steps = p_max_steps;
  412. ssr.push_constant.depth_tolerance = p_tolerance;
  413. ssr.push_constant.use_half_res = true;
  414. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  415. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  416. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  417. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  418. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  419. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  420. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  421. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  422. store_camera(p_camera, ssr.push_constant.projection);
  423. 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]);
  424. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  425. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  426. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  427. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  428. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_metallic, p_normal_roughness), 3);
  429. } else {
  430. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  431. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  432. }
  433. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  434. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  435. }
  436. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  437. //blur
  438. RD::get_singleton()->compute_list_add_barrier(compute_list);
  439. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  440. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  441. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  442. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  443. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  444. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  445. ssr_filter.push_constant.vertical = 0;
  446. if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_LOW) {
  447. ssr_filter.push_constant.steps = p_max_steps / 3;
  448. ssr_filter.push_constant.increment = 3;
  449. } else if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_MEDIUM) {
  450. ssr_filter.push_constant.steps = p_max_steps / 2;
  451. ssr_filter.push_constant.increment = 2;
  452. } else {
  453. ssr_filter.push_constant.steps = p_max_steps;
  454. ssr_filter.push_constant.increment = 1;
  455. }
  456. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  457. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  458. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  459. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  460. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  461. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  462. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  463. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  464. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  465. RD::get_singleton()->compute_list_add_barrier(compute_list);
  466. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  467. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  468. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  469. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  470. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  471. ssr_filter.push_constant.vertical = 1;
  472. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  473. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  474. }
  475. RD::get_singleton()->compute_list_end();
  476. }
  477. 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) {
  478. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  479. int32_t x_groups = (p_screen_size.width - 1) / 8 + 1;
  480. int32_t y_groups = (p_screen_size.height - 1) / 8 + 1;
  481. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  482. p.normal /= p.d;
  483. float unit_size = p.normal.x;
  484. { //scale color and depth to half
  485. sss.push_constant.camera_z_far = p_camera.get_z_far();
  486. sss.push_constant.camera_z_near = p_camera.get_z_near();
  487. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  488. sss.push_constant.unit_size = unit_size;
  489. sss.push_constant.screen_size[0] = p_screen_size.x;
  490. sss.push_constant.screen_size[1] = p_screen_size.y;
  491. sss.push_constant.vertical = false;
  492. sss.push_constant.scale = p_scale;
  493. sss.push_constant.depth_scale = p_depth_scale;
  494. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  495. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  496. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  497. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  498. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  499. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  500. RD::get_singleton()->compute_list_add_barrier(compute_list);
  501. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  502. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  503. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  504. sss.push_constant.vertical = true;
  505. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  506. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  507. RD::get_singleton()->compute_list_end();
  508. }
  509. }
  510. void EffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  511. 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>());
  512. if (p_reflection.is_valid()) {
  513. if (p_base.is_valid()) {
  514. 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)));
  515. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  516. } else {
  517. 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)));
  518. }
  519. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  520. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  521. } else {
  522. if (p_base.is_valid()) {
  523. 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)));
  524. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  525. } else {
  526. 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)));
  527. }
  528. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  529. }
  530. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  531. RD::get_singleton()->draw_list_draw(draw_list, true);
  532. RD::get_singleton()->draw_list_end();
  533. }
  534. void EffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  535. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  536. copy.push_constant.section[0] = 0;
  537. copy.push_constant.section[1] = 0;
  538. copy.push_constant.section[2] = p_size.width;
  539. copy.push_constant.section[3] = p_size.height;
  540. int32_t x_groups = (p_size.width - 1) / 8 + 1;
  541. int32_t y_groups = (p_size.height - 1) / 8 + 1;
  542. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  543. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_MIPMAP]);
  544. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  545. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  546. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  547. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  548. RD::get_singleton()->compute_list_end();
  549. }
  550. void EffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, float p_z_near, float p_z_far, float p_bias, bool p_dp_flip) {
  551. CopyToDPPushConstant push_constant;
  552. push_constant.screen_size[0] = p_rect.size.x;
  553. push_constant.screen_size[1] = p_rect.size.y;
  554. push_constant.dest_offset[0] = p_rect.position.x;
  555. push_constant.dest_offset[1] = p_rect.position.y;
  556. push_constant.bias = p_bias;
  557. push_constant.z_far = p_z_far;
  558. push_constant.z_near = p_z_near;
  559. push_constant.z_flip = p_dp_flip;
  560. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  561. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  562. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  563. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cube_to_dp.pipeline);
  564. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  565. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 1);
  566. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(CopyToDPPushConstant));
  567. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  568. RD::get_singleton()->compute_list_end();
  569. }
  570. void EffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  571. zeromem(&tonemap.push_constant, sizeof(TonemapPushConstant));
  572. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  573. tonemap.push_constant.bcs[0] = p_settings.brightness;
  574. tonemap.push_constant.bcs[1] = p_settings.contrast;
  575. tonemap.push_constant.bcs[2] = p_settings.saturation;
  576. tonemap.push_constant.use_glow = p_settings.use_glow;
  577. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  578. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  579. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  580. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  581. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  582. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  583. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  584. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  585. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  586. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  587. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  588. int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  589. if (p_settings.use_1d_color_correction) {
  590. mode += 2;
  591. }
  592. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  593. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  594. tonemap.push_constant.exposure = p_settings.exposure;
  595. tonemap.push_constant.white = p_settings.white;
  596. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  597. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  598. tonemap.push_constant.use_fxaa = p_settings.use_fxaa;
  599. tonemap.push_constant.use_debanding = p_settings.use_debanding;
  600. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  601. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  602. 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);
  603. 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)));
  604. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  605. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  606. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  607. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  608. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  609. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  610. RD::get_singleton()->draw_list_draw(draw_list, true);
  611. RD::get_singleton()->draw_list_end();
  612. }
  613. 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) {
  614. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  615. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  616. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  617. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  618. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  619. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  620. for (int i = 0; i < p_reduce.size(); i++) {
  621. if (i == 0) {
  622. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  623. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  624. } else {
  625. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  626. if (i == p_reduce.size() - 1 && !p_set) {
  627. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  628. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  629. } else {
  630. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  631. }
  632. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  633. }
  634. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  635. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  636. int32_t x_groups = (luminance_reduce.push_constant.source_size[0] - 1) / 8 + 1;
  637. int32_t y_groups = (luminance_reduce.push_constant.source_size[1] - 1) / 8 + 1;
  638. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  639. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  640. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  641. }
  642. RD::get_singleton()->compute_list_end();
  643. }
  644. 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) {
  645. bokeh.push_constant.blur_far_active = p_dof_far;
  646. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  647. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  648. bokeh.push_constant.blur_near_active = p_dof_near;
  649. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  650. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  651. bokeh.push_constant.use_jitter = p_use_jitter;
  652. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  653. bokeh.push_constant.z_near = p_cam_znear;
  654. bokeh.push_constant.z_far = p_cam_zfar;
  655. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  656. bokeh.push_constant.blur_size = p_bokeh_size;
  657. bokeh.push_constant.second_pass = false;
  658. bokeh.push_constant.half_size = false;
  659. bokeh.push_constant.blur_scale = 0.5;
  660. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  661. /* FIRST PASS */
  662. // The alpha channel of the source color texture is filled with the expected circle size
  663. // If used for DOF far, the size is positive, if used for near, its negative.
  664. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BLUR_SIZE]);
  665. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  666. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  667. int32_t x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  668. int32_t y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  669. bokeh.push_constant.size[0] = p_base_texture_size.x;
  670. bokeh.push_constant.size[1] = p_base_texture_size.y;
  671. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  672. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  673. RD::get_singleton()->compute_list_add_barrier(compute_list);
  674. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  675. //second pass
  676. 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]);
  677. static const int quality_samples[4] = { 6, 12, 12, 24 };
  678. bokeh.push_constant.steps = quality_samples[p_quality];
  679. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  680. //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)
  681. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  682. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  683. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  684. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  685. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  686. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  687. bokeh.push_constant.half_size = true;
  688. bokeh.push_constant.blur_size *= 0.5;
  689. } else {
  690. //medium and high quality use full size
  691. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 0);
  692. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  693. }
  694. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  695. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  696. RD::get_singleton()->compute_list_add_barrier(compute_list);
  697. //third pass
  698. bokeh.push_constant.second_pass = true;
  699. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  700. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture2), 0);
  701. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  702. } else {
  703. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  704. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 1);
  705. }
  706. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  707. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  708. RD::get_singleton()->compute_list_add_barrier(compute_list);
  709. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  710. //forth pass, upscale for low quality
  711. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  712. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  713. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture2), 1);
  714. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  715. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  716. bokeh.push_constant.size[0] = p_base_texture_size.x;
  717. bokeh.push_constant.size[1] = p_base_texture_size.y;
  718. bokeh.push_constant.half_size = false;
  719. bokeh.push_constant.second_pass = false;
  720. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  721. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  722. }
  723. } else {
  724. //circle
  725. //second pass
  726. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  727. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  728. bokeh.push_constant.steps = 0;
  729. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  730. //circle always runs in half size, otherwise too expensive
  731. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  732. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  733. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  734. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  735. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  736. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  737. bokeh.push_constant.half_size = true;
  738. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  739. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  740. RD::get_singleton()->compute_list_add_barrier(compute_list);
  741. //circle is just one pass, then upscale
  742. // upscale
  743. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  744. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  745. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  746. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  747. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  748. bokeh.push_constant.size[0] = p_base_texture_size.x;
  749. bokeh.push_constant.size[1] = p_base_texture_size.y;
  750. bokeh.push_constant.half_size = false;
  751. bokeh.push_constant.second_pass = false;
  752. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  753. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  754. }
  755. RD::get_singleton()->compute_list_end();
  756. }
  757. void EffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, const Size2i &p_depth_buffer_size, RID p_depth_mipmaps_texture, const Vector<RID> &depth_mipmaps, RID p_ao1, bool p_half_size, RID p_ao2, RID p_upscale_buffer, float p_intensity, float p_radius, float p_bias, const CameraMatrix &p_projection, RS::EnvironmentSSAOQuality p_quality, RS::EnvironmentSSAOBlur p_blur, float p_edge_sharpness) {
  758. //minify first
  759. ssao.minify_push_constant.orthogonal = p_projection.is_orthogonal();
  760. ssao.minify_push_constant.z_near = p_projection.get_z_near();
  761. ssao.minify_push_constant.z_far = p_projection.get_z_far();
  762. ssao.minify_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  763. ssao.minify_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  764. ssao.minify_push_constant.source_size[0] = p_depth_buffer_size.x;
  765. ssao.minify_push_constant.source_size[1] = p_depth_buffer_size.y;
  766. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  767. /* FIRST PASS */
  768. // Minify the depth buffer.
  769. for (int i = 0; i < depth_mipmaps.size(); i++) {
  770. if (i == 0) {
  771. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_FIRST]);
  772. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  773. } else {
  774. if (i == 1) {
  775. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_MIPMAP]);
  776. }
  777. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i - 1]), 0);
  778. }
  779. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i]), 1);
  780. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.minify_push_constant, sizeof(SSAOMinifyPushConstant));
  781. // shrink after set
  782. ssao.minify_push_constant.source_size[0] = MAX(1, ssao.minify_push_constant.source_size[0] >> 1);
  783. ssao.minify_push_constant.source_size[1] = MAX(1, ssao.minify_push_constant.source_size[1] >> 1);
  784. int x_groups = (ssao.minify_push_constant.source_size[0] - 1) / 8 + 1;
  785. int y_groups = (ssao.minify_push_constant.source_size[1] - 1) / 8 + 1;
  786. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  787. RD::get_singleton()->compute_list_add_barrier(compute_list);
  788. }
  789. /* SECOND PASS */
  790. // Gather samples
  791. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[(SSAO_GATHER_LOW + p_quality) + (p_half_size ? 4 : 0)]);
  792. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 0);
  793. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 1);
  794. if (!p_half_size) {
  795. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 2);
  796. }
  797. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_normal_buffer), 3);
  798. ssao.gather_push_constant.screen_size[0] = p_depth_buffer_size.x;
  799. ssao.gather_push_constant.screen_size[1] = p_depth_buffer_size.y;
  800. if (p_half_size) {
  801. ssao.gather_push_constant.screen_size[0] >>= 1;
  802. ssao.gather_push_constant.screen_size[1] >>= 1;
  803. }
  804. ssao.gather_push_constant.z_far = p_projection.get_z_far();
  805. ssao.gather_push_constant.z_near = p_projection.get_z_near();
  806. ssao.gather_push_constant.orthogonal = p_projection.is_orthogonal();
  807. ssao.gather_push_constant.proj_info[0] = -2.0f / (ssao.gather_push_constant.screen_size[0] * p_projection.matrix[0][0]);
  808. ssao.gather_push_constant.proj_info[1] = -2.0f / (ssao.gather_push_constant.screen_size[1] * p_projection.matrix[1][1]);
  809. ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  810. ssao.gather_push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  811. //ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  812. //ssao.gather_push_constant.proj_info[3] = -(1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  813. ssao.gather_push_constant.radius = p_radius;
  814. ssao.gather_push_constant.proj_scale = float(p_projection.get_pixels_per_meter(ssao.gather_push_constant.screen_size[0]));
  815. ssao.gather_push_constant.bias = p_bias;
  816. ssao.gather_push_constant.intensity_div_r6 = p_intensity / pow(p_radius, 6.0f);
  817. ssao.gather_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  818. ssao.gather_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  819. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  820. int x_groups = (ssao.gather_push_constant.screen_size[0] - 1) / 8 + 1;
  821. int y_groups = (ssao.gather_push_constant.screen_size[1] - 1) / 8 + 1;
  822. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  823. RD::get_singleton()->compute_list_add_barrier(compute_list);
  824. /* THIRD PASS */
  825. // Blur horizontal
  826. ssao.blur_push_constant.edge_sharpness = p_edge_sharpness;
  827. ssao.blur_push_constant.filter_scale = p_blur;
  828. ssao.blur_push_constant.screen_size[0] = ssao.gather_push_constant.screen_size[0];
  829. ssao.blur_push_constant.screen_size[1] = ssao.gather_push_constant.screen_size[1];
  830. ssao.blur_push_constant.z_far = p_projection.get_z_far();
  831. ssao.blur_push_constant.z_near = p_projection.get_z_near();
  832. ssao.blur_push_constant.orthogonal = p_projection.is_orthogonal();
  833. ssao.blur_push_constant.axis[0] = 1;
  834. ssao.blur_push_constant.axis[1] = 0;
  835. if (p_blur != RS::ENV_SSAO_BLUR_DISABLED) {
  836. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[p_half_size ? SSAO_BLUR_PASS_HALF : SSAO_BLUR_PASS]);
  837. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  838. if (p_half_size) {
  839. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 1);
  840. } else {
  841. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  842. }
  843. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao2), 3);
  844. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  845. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  846. RD::get_singleton()->compute_list_add_barrier(compute_list);
  847. /* THIRD PASS */
  848. // Blur vertical
  849. ssao.blur_push_constant.axis[0] = 0;
  850. ssao.blur_push_constant.axis[1] = 1;
  851. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao2), 0);
  852. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 3);
  853. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  854. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  855. }
  856. if (p_half_size) { //must upscale
  857. /* FOURTH PASS */
  858. // upscale if half size
  859. //back to full size
  860. ssao.blur_push_constant.screen_size[0] = p_depth_buffer_size.x;
  861. ssao.blur_push_constant.screen_size[1] = p_depth_buffer_size.y;
  862. RD::get_singleton()->compute_list_add_barrier(compute_list);
  863. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_BLUR_UPSCALE]);
  864. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  865. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 3);
  866. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  867. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 2);
  868. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant)); //not used but set anyway
  869. x_groups = (p_depth_buffer_size.x - 1) / 8 + 1;
  870. y_groups = (p_depth_buffer_size.y - 1) / 8 + 1;
  871. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  872. }
  873. RD::get_singleton()->compute_list_end();
  874. }
  875. void EffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  876. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  877. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  878. roughness_limiter.push_constant.curve = p_curve;
  879. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  880. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  881. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  882. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  883. int x_groups = (p_size.x - 1) / 8 + 1;
  884. int y_groups = (p_size.y - 1) / 8 + 1;
  885. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  886. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  887. RD::get_singleton()->compute_list_end();
  888. }
  889. 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) {
  890. zeromem(&roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  891. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  892. roughness.push_constant.roughness = p_roughness;
  893. roughness.push_constant.sample_count = p_sample_count;
  894. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  895. roughness.push_constant.face_size = p_size;
  896. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  897. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.pipeline);
  898. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  899. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_framebuffer), 1);
  900. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  901. int x_groups = (p_size - 1) / 8 + 1;
  902. int y_groups = (p_size - 1) / 8 + 1;
  903. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  904. RD::get_singleton()->compute_list_end();
  905. }
  906. void EffectsRD::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  907. cubemap_downsampler.push_constant.face_size = p_size.x;
  908. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  909. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.pipeline);
  910. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap), 0);
  911. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_cubemap), 1);
  912. int x_groups = (p_size.x - 1) / 8 + 1;
  913. int y_groups = (p_size.y - 1) / 8 + 1;
  914. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  915. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  916. RD::get_singleton()->compute_list_end();
  917. }
  918. void EffectsRD::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  919. Vector<RD::Uniform> uniforms;
  920. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  921. RD::Uniform u;
  922. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  923. u.binding = i;
  924. u.ids.push_back(p_dest_cubemap[i]);
  925. uniforms.push_back(u);
  926. }
  927. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  928. RD::get_singleton()->free(filter.image_uniform_set);
  929. }
  930. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.shader.version_get_shader(filter.shader_version, 0), 2);
  931. int pipeline = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  932. pipeline = filter.use_high_quality ? pipeline : pipeline + 1;
  933. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  934. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.pipelines[pipeline]);
  935. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap, true), 0);
  936. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  937. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  938. 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
  939. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  940. RD::get_singleton()->compute_list_end();
  941. }
  942. void EffectsRD::render_sky(RD::DrawListID p_list, float p_time, RID p_fb, RID p_samplers, RID p_fog, PipelineCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, const CameraMatrix &p_camera, const Basis &p_orientation, float p_multiplier, const Vector3 &p_position) {
  943. SkyPushConstant sky_push_constant;
  944. zeromem(&sky_push_constant, sizeof(SkyPushConstant));
  945. sky_push_constant.proj[0] = p_camera.matrix[2][0];
  946. sky_push_constant.proj[1] = p_camera.matrix[0][0];
  947. sky_push_constant.proj[2] = p_camera.matrix[2][1];
  948. sky_push_constant.proj[3] = p_camera.matrix[1][1];
  949. sky_push_constant.position[0] = p_position.x;
  950. sky_push_constant.position[1] = p_position.y;
  951. sky_push_constant.position[2] = p_position.z;
  952. sky_push_constant.multiplier = p_multiplier;
  953. sky_push_constant.time = p_time;
  954. store_transform_3x3(p_orientation, sky_push_constant.orientation);
  955. RenderingDevice::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_fb);
  956. RD::DrawListID draw_list = p_list;
  957. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, p_pipeline->get_render_pipeline(RD::INVALID_ID, fb_format));
  958. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_samplers, 0);
  959. if (p_uniform_set.is_valid()) { //material may not have uniform set
  960. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_uniform_set, 1);
  961. }
  962. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_texture_set, 2);
  963. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_fog, 3);
  964. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  965. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky_push_constant, sizeof(SkyPushConstant));
  966. RD::get_singleton()->draw_list_draw(draw_list, true);
  967. }
  968. void EffectsRD::resolve_gi(RID p_source_depth, RID p_source_normal_roughness, RID p_source_giprobe, RID p_dest_depth, RID p_dest_normal_roughness, RID p_dest_giprobe, Vector2i p_screen_size, int p_samples) {
  969. ResolvePushConstant push_constant;
  970. push_constant.screen_size[0] = p_screen_size.x;
  971. push_constant.screen_size[1] = p_screen_size.y;
  972. push_constant.samples = p_samples;
  973. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  974. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[p_source_giprobe.is_valid() ? RESOLVE_MODE_GI_GIPROBE : RESOLVE_MODE_GI]);
  975. 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);
  976. 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);
  977. if (p_source_giprobe.is_valid()) {
  978. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_giprobe), 2);
  979. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_giprobe), 3);
  980. }
  981. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  982. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1, 8, 8, 1);
  983. RD::get_singleton()->compute_list_end();
  984. }
  985. void EffectsRD::reduce_shadow(RID p_source_shadow, RID p_dest_shadow, const Size2i &p_source_size, const Rect2i &p_source_rect, int p_shrink_limit, RD::ComputeListID compute_list) {
  986. uint32_t push_constant[8] = { (uint32_t)p_source_size.x, (uint32_t)p_source_size.y, (uint32_t)p_source_rect.position.x, (uint32_t)p_source_rect.position.y, (uint32_t)p_shrink_limit, 0, 0, 0 };
  987. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shadow_reduce.pipelines[SHADOW_REDUCE_REDUCE]);
  988. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_source_shadow, p_dest_shadow), 0);
  989. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(uint32_t) * 8);
  990. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_source_rect.size.width, p_source_rect.size.height, 1, 8, 8, 1);
  991. }
  992. void EffectsRD::filter_shadow(RID p_shadow, RID p_backing_shadow, const Size2i &p_source_size, const Rect2i &p_source_rect, RenderingServer::EnvVolumetricFogShadowFilter p_filter, RD::ComputeListID compute_list, bool p_vertical, bool p_horizontal) {
  993. uint32_t push_constant[8] = { (uint32_t)p_source_size.x, (uint32_t)p_source_size.y, (uint32_t)p_source_rect.position.x, (uint32_t)p_source_rect.position.y, 0, 0, 0, 0 };
  994. switch (p_filter) {
  995. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED:
  996. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_LOW: {
  997. push_constant[5] = 0;
  998. } break;
  999. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_MEDIUM: {
  1000. push_constant[5] = 9;
  1001. } break;
  1002. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_HIGH: {
  1003. push_constant[5] = 18;
  1004. } break;
  1005. }
  1006. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shadow_reduce.pipelines[SHADOW_REDUCE_FILTER]);
  1007. if (p_vertical) {
  1008. push_constant[6] = 1;
  1009. push_constant[7] = 0;
  1010. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_shadow, p_backing_shadow), 0);
  1011. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(uint32_t) * 8);
  1012. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_source_rect.size.width, p_source_rect.size.height, 1, 8, 8, 1);
  1013. }
  1014. if (p_vertical && p_horizontal) {
  1015. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1016. }
  1017. if (p_horizontal) {
  1018. push_constant[6] = 0;
  1019. push_constant[7] = 1;
  1020. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_backing_shadow, p_shadow), 0);
  1021. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(uint32_t) * 8);
  1022. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_source_rect.size.width, p_source_rect.size.height, 1, 8, 8, 1);
  1023. }
  1024. }
  1025. void EffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  1026. Sort::PushConstant push_constant;
  1027. push_constant.total_elements = p_size;
  1028. bool done = true;
  1029. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  1030. if (numThreadGroups > 1) {
  1031. done = false;
  1032. }
  1033. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1034. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  1035. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  1036. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1037. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1038. int presorted = 512;
  1039. while (!done) {
  1040. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1041. done = true;
  1042. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  1043. numThreadGroups = 0;
  1044. if (p_size > presorted) {
  1045. if (p_size > presorted * 2) {
  1046. done = false;
  1047. }
  1048. int pow2 = presorted;
  1049. while (pow2 < p_size) {
  1050. pow2 *= 2;
  1051. }
  1052. numThreadGroups = pow2 >> 9;
  1053. }
  1054. unsigned int nMergeSize = presorted * 2;
  1055. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  1056. push_constant.job_params[0] = nMergeSubSize;
  1057. if (nMergeSubSize == nMergeSize >> 1) {
  1058. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  1059. push_constant.job_params[2] = -1;
  1060. } else {
  1061. push_constant.job_params[1] = nMergeSubSize;
  1062. push_constant.job_params[2] = 1;
  1063. }
  1064. push_constant.job_params[3] = 0;
  1065. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1066. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1067. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1068. }
  1069. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  1070. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1071. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1072. presorted *= 2;
  1073. }
  1074. RD::get_singleton()->compute_list_end();
  1075. }
  1076. EffectsRD::EffectsRD() {
  1077. { // Initialize copy
  1078. Vector<String> copy_modes;
  1079. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  1080. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  1081. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  1082. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  1083. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  1084. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  1085. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  1086. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  1087. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  1088. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  1089. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  1090. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  1091. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  1092. copy.shader.initialize(copy_modes);
  1093. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  1094. copy.shader_version = copy.shader.version_create();
  1095. for (int i = 0; i < COPY_MODE_MAX; i++) {
  1096. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  1097. }
  1098. }
  1099. {
  1100. Vector<String> copy_modes;
  1101. copy_modes.push_back("\n");
  1102. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n");
  1103. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n");
  1104. copy_to_fb.shader.initialize(copy_modes);
  1105. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  1106. //use additive
  1107. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  1108. 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);
  1109. }
  1110. }
  1111. {
  1112. // Initialize roughness
  1113. Vector<String> cubemap_roughness_modes;
  1114. cubemap_roughness_modes.push_back("");
  1115. roughness.shader.initialize(cubemap_roughness_modes);
  1116. roughness.shader_version = roughness.shader.version_create();
  1117. roughness.pipeline = RD::get_singleton()->compute_pipeline_create(roughness.shader.version_get_shader(roughness.shader_version, 0));
  1118. }
  1119. {
  1120. // Initialize tonemapper
  1121. Vector<String> tonemap_modes;
  1122. tonemap_modes.push_back("\n");
  1123. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  1124. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  1125. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1126. tonemap.shader.initialize(tonemap_modes);
  1127. tonemap.shader_version = tonemap.shader.version_create();
  1128. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  1129. 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);
  1130. }
  1131. }
  1132. {
  1133. // Initialize luminance_reduce
  1134. Vector<String> luminance_reduce_modes;
  1135. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  1136. luminance_reduce_modes.push_back("\n");
  1137. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  1138. luminance_reduce.shader.initialize(luminance_reduce_modes);
  1139. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  1140. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  1141. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  1142. }
  1143. }
  1144. {
  1145. // Initialize copier
  1146. Vector<String> copy_modes;
  1147. copy_modes.push_back("\n");
  1148. cube_to_dp.shader.initialize(copy_modes);
  1149. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  1150. cube_to_dp.pipeline = RD::get_singleton()->compute_pipeline_create(cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0));
  1151. }
  1152. {
  1153. // Initialize bokeh
  1154. Vector<String> bokeh_modes;
  1155. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  1156. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  1157. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  1158. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n");
  1159. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  1160. bokeh.shader.initialize(bokeh_modes);
  1161. bokeh.shader_version = bokeh.shader.version_create();
  1162. for (int i = 0; i < BOKEH_MAX; i++) {
  1163. bokeh.pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.shader.version_get_shader(bokeh.shader_version, i));
  1164. }
  1165. }
  1166. {
  1167. // Initialize ssao
  1168. uint32_t pipeline = 0;
  1169. {
  1170. Vector<String> ssao_modes;
  1171. ssao_modes.push_back("\n#define MINIFY_START\n");
  1172. ssao_modes.push_back("\n");
  1173. ssao.minify_shader.initialize(ssao_modes);
  1174. ssao.minify_shader_version = ssao.minify_shader.version_create();
  1175. for (int i = 0; i <= SSAO_MINIFY_MIPMAP; i++) {
  1176. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.minify_shader.version_get_shader(ssao.minify_shader_version, i));
  1177. pipeline++;
  1178. }
  1179. }
  1180. {
  1181. Vector<String> ssao_modes;
  1182. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n");
  1183. ssao_modes.push_back("\n");
  1184. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n");
  1185. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n");
  1186. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n#define USE_HALF_SIZE\n");
  1187. ssao_modes.push_back("\n#define USE_HALF_SIZE\n");
  1188. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n#define USE_HALF_SIZE\n");
  1189. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n#define USE_HALF_SIZE\n");
  1190. ssao.gather_shader.initialize(ssao_modes);
  1191. ssao.gather_shader_version = ssao.gather_shader.version_create();
  1192. for (int i = SSAO_GATHER_LOW; i <= SSAO_GATHER_ULTRA_HALF; i++) {
  1193. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i - SSAO_GATHER_LOW));
  1194. pipeline++;
  1195. }
  1196. }
  1197. {
  1198. Vector<String> ssao_modes;
  1199. ssao_modes.push_back("\n#define MODE_FULL_SIZE\n");
  1200. ssao_modes.push_back("\n");
  1201. ssao_modes.push_back("\n#define MODE_UPSCALE\n");
  1202. ssao.blur_shader.initialize(ssao_modes);
  1203. ssao.blur_shader_version = ssao.blur_shader.version_create();
  1204. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_UPSCALE; i++) {
  1205. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  1206. pipeline++;
  1207. }
  1208. }
  1209. ERR_FAIL_COND(pipeline != SSAO_MAX);
  1210. }
  1211. {
  1212. // Initialize roughness limiter
  1213. Vector<String> shader_modes;
  1214. shader_modes.push_back("");
  1215. roughness_limiter.shader.initialize(shader_modes);
  1216. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  1217. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  1218. }
  1219. {
  1220. //Initialize cubemap downsampler
  1221. Vector<String> cubemap_downsampler_modes;
  1222. cubemap_downsampler_modes.push_back("");
  1223. cubemap_downsampler.shader.initialize(cubemap_downsampler_modes);
  1224. cubemap_downsampler.shader_version = cubemap_downsampler.shader.version_create();
  1225. cubemap_downsampler.pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  1226. }
  1227. {
  1228. // Initialize cubemap filter
  1229. filter.use_high_quality = GLOBAL_GET("rendering/quality/reflections/fast_filter_high_quality");
  1230. Vector<String> cubemap_filter_modes;
  1231. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  1232. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  1233. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1234. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1235. filter.shader.initialize(cubemap_filter_modes);
  1236. filter.shader_version = filter.shader.version_create();
  1237. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  1238. filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.shader.version_get_shader(filter.shader_version, i));
  1239. }
  1240. if (filter.use_high_quality) {
  1241. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  1242. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0], false);
  1243. } else {
  1244. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  1245. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0], false);
  1246. }
  1247. Vector<RD::Uniform> uniforms;
  1248. {
  1249. RD::Uniform u;
  1250. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1251. u.binding = 0;
  1252. u.ids.push_back(filter.coefficient_buffer);
  1253. uniforms.push_back(u);
  1254. }
  1255. 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);
  1256. }
  1257. {
  1258. Vector<String> specular_modes;
  1259. specular_modes.push_back("\n#define MODE_MERGE\n");
  1260. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  1261. specular_modes.push_back("\n");
  1262. specular_modes.push_back("\n#define MODE_SSR\n");
  1263. specular_merge.shader.initialize(specular_modes);
  1264. specular_merge.shader_version = specular_merge.shader.version_create();
  1265. //use additive
  1266. RD::PipelineColorBlendState::Attachment ba;
  1267. ba.enable_blend = true;
  1268. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1269. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1270. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1271. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1272. ba.color_blend_op = RD::BLEND_OP_ADD;
  1273. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  1274. RD::PipelineColorBlendState blend_additive;
  1275. blend_additive.attachments.push_back(ba);
  1276. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  1277. RD::PipelineColorBlendState blend_state;
  1278. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  1279. blend_state = blend_additive;
  1280. } else {
  1281. blend_state = RD::PipelineColorBlendState::create_disabled();
  1282. }
  1283. 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);
  1284. }
  1285. }
  1286. {
  1287. Vector<String> ssr_modes;
  1288. ssr_modes.push_back("\n");
  1289. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  1290. ssr.shader.initialize(ssr_modes);
  1291. ssr.shader_version = ssr.shader.version_create();
  1292. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  1293. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  1294. }
  1295. }
  1296. {
  1297. Vector<String> ssr_filter_modes;
  1298. ssr_filter_modes.push_back("\n");
  1299. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  1300. ssr_filter.shader.initialize(ssr_filter_modes);
  1301. ssr_filter.shader_version = ssr_filter.shader.version_create();
  1302. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  1303. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  1304. }
  1305. }
  1306. {
  1307. Vector<String> ssr_scale_modes;
  1308. ssr_scale_modes.push_back("\n");
  1309. ssr_scale.shader.initialize(ssr_scale_modes);
  1310. ssr_scale.shader_version = ssr_scale.shader.version_create();
  1311. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  1312. }
  1313. {
  1314. Vector<String> sss_modes;
  1315. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  1316. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  1317. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  1318. sss.shader.initialize(sss_modes);
  1319. sss.shader_version = sss.shader.version_create();
  1320. for (int i = 0; i < sss_modes.size(); i++) {
  1321. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  1322. }
  1323. }
  1324. {
  1325. Vector<String> resolve_modes;
  1326. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n");
  1327. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define GIPROBE_RESOLVE\n");
  1328. resolve.shader.initialize(resolve_modes);
  1329. resolve.shader_version = resolve.shader.version_create();
  1330. for (int i = 0; i < RESOLVE_MODE_MAX; i++) {
  1331. resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i));
  1332. }
  1333. }
  1334. {
  1335. Vector<String> shadow_reduce_modes;
  1336. shadow_reduce_modes.push_back("\n#define MODE_REDUCE\n");
  1337. shadow_reduce_modes.push_back("\n#define MODE_FILTER\n");
  1338. shadow_reduce.shader.initialize(shadow_reduce_modes);
  1339. shadow_reduce.shader_version = shadow_reduce.shader.version_create();
  1340. for (int i = 0; i < SHADOW_REDUCE_MAX; i++) {
  1341. shadow_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(shadow_reduce.shader.version_get_shader(shadow_reduce.shader_version, i));
  1342. }
  1343. }
  1344. {
  1345. Vector<String> sort_modes;
  1346. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  1347. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  1348. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  1349. sort.shader.initialize(sort_modes);
  1350. sort.shader_version = sort.shader.version_create();
  1351. for (int i = 0; i < SORT_MODE_MAX; i++) {
  1352. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  1353. }
  1354. }
  1355. RD::SamplerState sampler;
  1356. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1357. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1358. sampler.max_lod = 0;
  1359. default_sampler = RD::get_singleton()->sampler_create(sampler);
  1360. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1361. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1362. sampler.max_lod = 1e20;
  1363. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  1364. { //create index array for copy shaders
  1365. Vector<uint8_t> pv;
  1366. pv.resize(6 * 4);
  1367. {
  1368. uint8_t *w = pv.ptrw();
  1369. int *p32 = (int *)w;
  1370. p32[0] = 0;
  1371. p32[1] = 1;
  1372. p32[2] = 2;
  1373. p32[3] = 0;
  1374. p32[4] = 2;
  1375. p32[5] = 3;
  1376. }
  1377. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1378. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  1379. }
  1380. }
  1381. EffectsRD::~EffectsRD() {
  1382. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1383. RD::get_singleton()->free(filter.image_uniform_set);
  1384. }
  1385. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  1386. RD::get_singleton()->free(filter.uniform_set);
  1387. }
  1388. RD::get_singleton()->free(default_sampler);
  1389. RD::get_singleton()->free(default_mipmap_sampler);
  1390. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  1391. RD::get_singleton()->free(filter.coefficient_buffer);
  1392. bokeh.shader.version_free(bokeh.shader_version);
  1393. copy.shader.version_free(copy.shader_version);
  1394. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  1395. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  1396. cubemap_downsampler.shader.version_free(cubemap_downsampler.shader_version);
  1397. filter.shader.version_free(filter.shader_version);
  1398. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  1399. resolve.shader.version_free(resolve.shader_version);
  1400. roughness.shader.version_free(roughness.shader_version);
  1401. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  1402. sort.shader.version_free(sort.shader_version);
  1403. specular_merge.shader.version_free(specular_merge.shader_version);
  1404. ssao.blur_shader.version_free(ssao.blur_shader_version);
  1405. ssao.gather_shader.version_free(ssao.gather_shader_version);
  1406. ssao.minify_shader.version_free(ssao.minify_shader_version);
  1407. ssr.shader.version_free(ssr.shader_version);
  1408. ssr_filter.shader.version_free(ssr_filter.shader_version);
  1409. ssr_scale.shader.version_free(ssr_scale.shader_version);
  1410. sss.shader.version_free(sss.shader_version);
  1411. tonemap.shader.version_free(tonemap.shader_version);
  1412. shadow_reduce.shader.version_free(shadow_reduce.shader_version);
  1413. }