effects_rd.cpp 127 KB

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