effects_rd.cpp 100 KB

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