effects_rd.cpp 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  1. /*************************************************************************/
  2. /* effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  102. TexturePair tp;
  103. tp.texture1 = p_texture1;
  104. tp.texture2 = p_texture2;
  105. if (texture_pair_to_uniform_set_cache.has(tp)) {
  106. RID uniform_set = texture_pair_to_uniform_set_cache[tp];
  107. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  108. return uniform_set;
  109. }
  110. }
  111. Vector<RD::Uniform> uniforms;
  112. {
  113. RD::Uniform u;
  114. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  115. u.binding = 0;
  116. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  117. u.ids.push_back(p_texture1);
  118. uniforms.push_back(u);
  119. }
  120. {
  121. RD::Uniform u;
  122. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  123. u.binding = 1;
  124. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  125. u.ids.push_back(p_texture2);
  126. uniforms.push_back(u);
  127. }
  128. // anything with the same configuration (one texture in binding 0 for set 0), is good
  129. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, 0), 2);
  130. texture_pair_to_uniform_set_cache[tp] = uniform_set;
  131. return uniform_set;
  132. }
  133. RID EffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  134. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  135. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  136. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  137. return uniform_set;
  138. }
  139. }
  140. Vector<RD::Uniform> uniforms;
  141. RD::Uniform u;
  142. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  143. u.binding = 0;
  144. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  145. u.ids.push_back(p_texture);
  146. uniforms.push_back(u);
  147. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  148. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  149. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  150. return uniform_set;
  151. }
  152. RID EffectsRD::_get_compute_uniform_set_from_texture_and_sampler(RID p_texture, RID p_sampler) {
  153. TextureSamplerPair tsp;
  154. tsp.texture = p_texture;
  155. tsp.sampler = p_sampler;
  156. if (texture_sampler_to_compute_uniform_set_cache.has(tsp)) {
  157. RID uniform_set = texture_sampler_to_compute_uniform_set_cache[tsp];
  158. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  159. return uniform_set;
  160. }
  161. }
  162. Vector<RD::Uniform> uniforms;
  163. RD::Uniform u;
  164. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  165. u.binding = 0;
  166. u.ids.push_back(p_sampler);
  167. u.ids.push_back(p_texture);
  168. uniforms.push_back(u);
  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, ssao.blur_shader.version_get_shader(ssao.blur_shader_version, 0), 0);
  171. texture_sampler_to_compute_uniform_set_cache[tsp] = uniform_set;
  172. return uniform_set;
  173. }
  174. RID EffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  175. TexturePair tp;
  176. tp.texture1 = p_texture1;
  177. tp.texture2 = p_texture2;
  178. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  179. RID uniform_set = texture_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_SAMPLER_WITH_TEXTURE;
  188. u.binding = 0;
  189. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  190. u.ids.push_back(p_texture1);
  191. uniforms.push_back(u);
  192. }
  193. {
  194. RD::Uniform u;
  195. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  196. u.binding = 1;
  197. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  198. u.ids.push_back(p_texture2);
  199. uniforms.push_back(u);
  200. }
  201. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  202. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  203. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  204. return uniform_set;
  205. }
  206. RID EffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  207. TexturePair tp;
  208. tp.texture1 = p_texture1;
  209. tp.texture2 = p_texture2;
  210. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  211. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  212. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  213. return uniform_set;
  214. }
  215. }
  216. Vector<RD::Uniform> uniforms;
  217. {
  218. RD::Uniform u;
  219. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  220. u.binding = 0;
  221. u.ids.push_back(p_texture1);
  222. uniforms.push_back(u);
  223. }
  224. {
  225. RD::Uniform u;
  226. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  227. u.binding = 1;
  228. u.ids.push_back(p_texture2);
  229. uniforms.push_back(u);
  230. }
  231. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  232. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  233. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  234. return uniform_set;
  235. }
  236. void EffectsRD::fsr_upscale(RID p_source_rd_texture, RID p_secondary_texture, RID p_destination_texture, const Size2i &p_internal_size, const Size2i &p_size, float p_fsr_upscale_sharpness) {
  237. memset(&FSR_upscale.push_constant, 0, sizeof(FSRUpscalePushConstant));
  238. int dispatch_x = (p_size.x + 15) / 16;
  239. int dispatch_y = (p_size.y + 15) / 16;
  240. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  241. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, FSR_upscale.pipeline);
  242. FSR_upscale.push_constant.resolution_width = p_internal_size.width;
  243. FSR_upscale.push_constant.resolution_height = p_internal_size.height;
  244. FSR_upscale.push_constant.upscaled_width = p_size.width;
  245. FSR_upscale.push_constant.upscaled_height = p_size.height;
  246. FSR_upscale.push_constant.sharpness = p_fsr_upscale_sharpness;
  247. //FSR Easc
  248. FSR_upscale.push_constant.pass = FSR_UPSCALE_PASS_EASU;
  249. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  250. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 1);
  251. RD::get_singleton()->compute_list_set_push_constant(compute_list, &FSR_upscale.push_constant, sizeof(FSRUpscalePushConstant));
  252. RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1);
  253. RD::get_singleton()->compute_list_add_barrier(compute_list);
  254. //FSR Rcas
  255. FSR_upscale.push_constant.pass = FSR_UPSCALE_PASS_RCAS;
  256. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 0);
  257. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_destination_texture), 1);
  258. RD::get_singleton()->compute_list_set_push_constant(compute_list, &FSR_upscale.push_constant, sizeof(FSRUpscalePushConstant));
  259. RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1);
  260. RD::get_singleton()->compute_list_end(compute_list);
  261. }
  262. 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) {
  263. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  264. copy_to_fb.push_constant.use_section = true;
  265. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  266. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  267. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  268. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  269. if (p_flip_y) {
  270. copy_to_fb.push_constant.flip_y = true;
  271. }
  272. RD::DrawListID draw_list = p_draw_list;
  273. 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)));
  274. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  275. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  276. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  277. RD::get_singleton()->draw_list_draw(draw_list, true);
  278. }
  279. 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) {
  280. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  281. if (p_flip_y) {
  282. copy_to_fb.push_constant.flip_y = true;
  283. }
  284. if (p_force_luminance) {
  285. copy_to_fb.push_constant.force_luminance = true;
  286. }
  287. if (p_alpha_to_zero) {
  288. copy_to_fb.push_constant.alpha_to_zero = true;
  289. }
  290. if (p_srgb) {
  291. copy_to_fb.push_constant.srgb = true;
  292. }
  293. 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);
  294. 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)));
  295. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  296. if (p_secondary.is_valid()) {
  297. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_secondary), 1);
  298. }
  299. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  300. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  301. RD::get_singleton()->draw_list_draw(draw_list, true);
  302. RD::get_singleton()->draw_list_end();
  303. }
  304. 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) {
  305. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  306. if (p_flip_y) {
  307. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  308. }
  309. if (p_force_luminance) {
  310. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  311. }
  312. if (p_all_source) {
  313. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  314. }
  315. if (p_alpha_to_one) {
  316. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  317. }
  318. copy.push_constant.section[0] = 0;
  319. copy.push_constant.section[1] = 0;
  320. copy.push_constant.section[2] = p_rect.size.width;
  321. copy.push_constant.section[3] = p_rect.size.height;
  322. copy.push_constant.target[0] = p_rect.position.x;
  323. copy.push_constant.target[1] = p_rect.position.y;
  324. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  325. 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]);
  326. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  327. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  328. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  329. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  330. RD::get_singleton()->compute_list_end();
  331. }
  332. 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) {
  333. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  334. copy.push_constant.section[0] = 0;
  335. copy.push_constant.section[1] = 0;
  336. copy.push_constant.section[2] = p_panorama_size.width;
  337. copy.push_constant.section[3] = p_panorama_size.height;
  338. copy.push_constant.target[0] = 0;
  339. copy.push_constant.target[1] = 0;
  340. copy.push_constant.camera_z_far = p_lod;
  341. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  342. 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]);
  343. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cube), 0);
  344. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_panorama), 3);
  345. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  346. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
  347. RD::get_singleton()->compute_list_end();
  348. }
  349. 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) {
  350. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  351. if (p_flip_y) {
  352. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  353. }
  354. copy.push_constant.section[0] = 0;
  355. copy.push_constant.section[1] = 0;
  356. copy.push_constant.section[2] = p_rect.size.width;
  357. copy.push_constant.section[3] = p_rect.size.height;
  358. copy.push_constant.target[0] = p_rect.position.x;
  359. copy.push_constant.target[1] = p_rect.position.y;
  360. copy.push_constant.camera_z_far = p_z_far;
  361. copy.push_constant.camera_z_near = p_z_near;
  362. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  363. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_LINEARIZE_DEPTH]);
  364. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  365. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  366. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  367. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  368. RD::get_singleton()->compute_list_end();
  369. }
  370. void EffectsRD::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  371. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  372. if (p_flip_y) {
  373. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  374. }
  375. copy.push_constant.section[0] = 0;
  376. copy.push_constant.section[1] = 0;
  377. copy.push_constant.section[2] = p_rect.size.width;
  378. copy.push_constant.section[3] = p_rect.size.height;
  379. copy.push_constant.target[0] = p_rect.position.x;
  380. copy.push_constant.target[1] = p_rect.position.y;
  381. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  382. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_SIMPLY_COPY_DEPTH]);
  383. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  384. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  385. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  386. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  387. RD::get_singleton()->compute_list_end();
  388. }
  389. void EffectsRD::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  390. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  391. copy.push_constant.section[0] = 0;
  392. copy.push_constant.section[1] = 0;
  393. copy.push_constant.section[2] = p_region.size.width;
  394. copy.push_constant.section[3] = p_region.size.height;
  395. copy.push_constant.target[0] = p_region.position.x;
  396. copy.push_constant.target[1] = p_region.position.y;
  397. copy.push_constant.set_color[0] = p_color.r;
  398. copy.push_constant.set_color[1] = p_color.g;
  399. copy.push_constant.set_color[2] = p_color.b;
  400. copy.push_constant.set_color[3] = p_color.a;
  401. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  402. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR]);
  403. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  404. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  405. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  406. RD::get_singleton()->compute_list_end();
  407. }
  408. void EffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, bool p_8bit_dst) {
  409. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");
  410. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  411. copy.push_constant.section[0] = p_region.position.x;
  412. copy.push_constant.section[1] = p_region.position.y;
  413. copy.push_constant.section[2] = p_region.size.width;
  414. copy.push_constant.section[3] = p_region.size.height;
  415. //HORIZONTAL
  416. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  417. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY]);
  418. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  419. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 3);
  420. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  421. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  422. RD::get_singleton()->compute_list_end();
  423. }
  424. 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_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  425. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");
  426. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  427. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  428. uint32_t base_flags = 0;
  429. copy.push_constant.section[2] = p_size.x;
  430. copy.push_constant.section[3] = p_size.y;
  431. copy.push_constant.glow_strength = p_strength;
  432. copy.push_constant.glow_bloom = p_bloom;
  433. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  434. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  435. copy.push_constant.glow_exposure = p_exposure;
  436. copy.push_constant.glow_white = 0; //actually unused
  437. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  438. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  439. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  440. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  441. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  442. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  443. if (p_auto_exposure.is_valid() && p_first_pass) {
  444. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_auto_exposure), 1);
  445. }
  446. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  447. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  448. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  449. RD::get_singleton()->compute_list_end();
  450. }
  451. 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_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  452. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");
  453. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  454. BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  455. uint32_t base_flags = 0;
  456. blur_raster.push_constant.pixel_size[0] = p_pixel_size.x;
  457. blur_raster.push_constant.pixel_size[1] = p_pixel_size.y;
  458. blur_raster.push_constant.glow_strength = p_strength;
  459. blur_raster.push_constant.glow_bloom = p_bloom;
  460. blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  461. blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  462. blur_raster.push_constant.glow_exposure = p_exposure;
  463. blur_raster.push_constant.glow_white = 0; //actually unused
  464. blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
  465. blur_raster.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  466. //HORIZONTAL
  467. 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);
  468. 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)));
  469. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  470. if (p_auto_exposure.is_valid() && p_first_pass) {
  471. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_auto_exposure), 1);
  472. }
  473. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  474. blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  475. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  476. RD::get_singleton()->draw_list_draw(draw_list, true);
  477. RD::get_singleton()->draw_list_end();
  478. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  479. //VERTICAL
  480. 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);
  481. 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)));
  482. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_rd_texture_half), 0);
  483. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  484. blur_raster.push_constant.flags = base_flags;
  485. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  486. RD::get_singleton()->draw_list_draw(draw_list, true);
  487. RD::get_singleton()->draw_list_end();
  488. }
  489. 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) {
  490. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  491. { //scale color and depth to half
  492. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  493. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  494. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  495. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  496. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  497. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  498. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  499. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  500. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal_roughness), 1);
  501. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  502. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  503. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  504. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  505. RD::get_singleton()->compute_list_add_barrier(compute_list);
  506. }
  507. {
  508. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  509. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  510. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  511. ssr.push_constant.screen_size[0] = p_screen_size.x;
  512. ssr.push_constant.screen_size[1] = p_screen_size.y;
  513. ssr.push_constant.curve_fade_in = p_fade_in;
  514. ssr.push_constant.distance_fade = p_fade_out;
  515. ssr.push_constant.num_steps = p_max_steps;
  516. ssr.push_constant.depth_tolerance = p_tolerance;
  517. ssr.push_constant.use_half_res = true;
  518. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  519. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  520. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  521. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  522. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  523. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  524. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  525. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  526. store_camera(p_camera, ssr.push_constant.projection);
  527. 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]);
  528. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  529. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  530. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  531. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  532. } else {
  533. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  534. }
  535. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  536. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  537. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  538. }
  539. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  540. //blur
  541. RD::get_singleton()->compute_list_add_barrier(compute_list);
  542. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  543. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  544. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  545. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  546. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  547. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  548. ssr_filter.push_constant.vertical = 0;
  549. if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_LOW) {
  550. ssr_filter.push_constant.steps = p_max_steps / 3;
  551. ssr_filter.push_constant.increment = 3;
  552. } else if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_MEDIUM) {
  553. ssr_filter.push_constant.steps = p_max_steps / 2;
  554. ssr_filter.push_constant.increment = 2;
  555. } else {
  556. ssr_filter.push_constant.steps = p_max_steps;
  557. ssr_filter.push_constant.increment = 1;
  558. }
  559. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  560. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  561. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  562. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  563. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  564. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  565. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  566. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  567. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  568. RD::get_singleton()->compute_list_add_barrier(compute_list);
  569. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  570. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  571. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  572. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  573. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  574. ssr_filter.push_constant.vertical = 1;
  575. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  576. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  577. }
  578. RD::get_singleton()->compute_list_end();
  579. }
  580. 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) {
  581. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  582. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  583. p.normal /= p.d;
  584. float unit_size = p.normal.x;
  585. { //scale color and depth to half
  586. sss.push_constant.camera_z_far = p_camera.get_z_far();
  587. sss.push_constant.camera_z_near = p_camera.get_z_near();
  588. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  589. sss.push_constant.unit_size = unit_size;
  590. sss.push_constant.screen_size[0] = p_screen_size.x;
  591. sss.push_constant.screen_size[1] = p_screen_size.y;
  592. sss.push_constant.vertical = false;
  593. sss.push_constant.scale = p_scale;
  594. sss.push_constant.depth_scale = p_depth_scale;
  595. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  596. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  597. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  598. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  599. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  600. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  601. RD::get_singleton()->compute_list_add_barrier(compute_list);
  602. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  603. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  604. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  605. sss.push_constant.vertical = true;
  606. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  607. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.width, p_screen_size.height, 1);
  608. RD::get_singleton()->compute_list_end();
  609. }
  610. }
  611. void EffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  612. 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>());
  613. if (p_reflection.is_valid()) {
  614. if (p_base.is_valid()) {
  615. 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)));
  616. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  617. } else {
  618. 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)));
  619. }
  620. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  621. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  622. } else {
  623. if (p_base.is_valid()) {
  624. 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)));
  625. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  626. } else {
  627. 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)));
  628. }
  629. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  630. }
  631. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  632. RD::get_singleton()->draw_list_draw(draw_list, true);
  633. RD::get_singleton()->draw_list_end();
  634. }
  635. void EffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  636. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  637. copy.push_constant.section[0] = 0;
  638. copy.push_constant.section[1] = 0;
  639. copy.push_constant.section[2] = p_size.width;
  640. copy.push_constant.section[3] = p_size.height;
  641. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  642. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_MIPMAP]);
  643. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  644. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  645. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  646. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  647. RD::get_singleton()->compute_list_end();
  648. }
  649. void EffectsRD::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_framebuffer, const Size2i &p_size) {
  650. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");
  651. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  652. BlurRasterMode mode = BLUR_MIPMAP;
  653. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  654. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  655. 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);
  656. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  657. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  658. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  659. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  660. RD::get_singleton()->draw_list_draw(draw_list, true);
  661. RD::get_singleton()->draw_list_end();
  662. }
  663. void EffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, const Vector2 &p_dst_size, float p_z_near, float p_z_far, bool p_dp_flip) {
  664. CopyToDPPushConstant push_constant;
  665. push_constant.screen_rect[0] = p_rect.position.x;
  666. push_constant.screen_rect[1] = p_rect.position.y;
  667. push_constant.screen_rect[2] = p_rect.size.width;
  668. push_constant.screen_rect[3] = p_rect.size.height;
  669. push_constant.z_far = p_z_far;
  670. push_constant.z_near = p_z_near;
  671. push_constant.texel_size[0] = 1.0f / p_dst_size.x;
  672. push_constant.texel_size[1] = 1.0f / p_dst_size.y;
  673. push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
  674. 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);
  675. 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)));
  676. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  677. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  678. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  679. RD::get_singleton()->draw_list_draw(draw_list, true);
  680. RD::get_singleton()->draw_list_end(RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_TRANSFER);
  681. }
  682. void EffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  683. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  684. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  685. tonemap.push_constant.bcs[0] = p_settings.brightness;
  686. tonemap.push_constant.bcs[1] = p_settings.contrast;
  687. tonemap.push_constant.bcs[2] = p_settings.saturation;
  688. tonemap.push_constant.use_glow = p_settings.use_glow;
  689. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  690. tonemap.push_constant.glow_map_strength = p_settings.glow_map_strength;
  691. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  692. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  693. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  694. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  695. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  696. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  697. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  698. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  699. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  700. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  701. int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  702. if (p_settings.use_1d_color_correction) {
  703. mode += 2;
  704. }
  705. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  706. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  707. tonemap.push_constant.exposure = p_settings.exposure;
  708. tonemap.push_constant.white = p_settings.white;
  709. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  710. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  711. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  712. tonemap.push_constant.use_fxaa = p_settings.use_fxaa;
  713. tonemap.push_constant.use_debanding = p_settings.use_debanding;
  714. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  715. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  716. if (p_settings.view_count > 1) {
  717. // Use MULTIVIEW versions
  718. mode += 6;
  719. }
  720. 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);
  721. 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()));
  722. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  723. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  724. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture_pair(p_settings.glow_texture, p_settings.glow_map, true), 2);
  725. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  726. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  727. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  728. RD::get_singleton()->draw_list_draw(draw_list, true);
  729. RD::get_singleton()->draw_list_end();
  730. }
  731. void EffectsRD::tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings) {
  732. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  733. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  734. tonemap.push_constant.bcs[0] = p_settings.brightness;
  735. tonemap.push_constant.bcs[1] = p_settings.contrast;
  736. tonemap.push_constant.bcs[2] = p_settings.saturation;
  737. ERR_FAIL_COND_MSG(p_settings.use_glow, "Glow is not supported when using subpasses.");
  738. tonemap.push_constant.use_glow = p_settings.use_glow;
  739. int mode = p_settings.use_1d_color_correction ? TONEMAP_MODE_SUBPASS_1D_LUT : TONEMAP_MODE_SUBPASS;
  740. if (p_settings.view_count > 1) {
  741. // Use MULTIVIEW versions
  742. mode += 6;
  743. }
  744. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  745. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  746. tonemap.push_constant.exposure = p_settings.exposure;
  747. tonemap.push_constant.white = p_settings.white;
  748. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  749. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  750. tonemap.push_constant.use_debanding = p_settings.use_debanding;
  751. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  752. 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()));
  753. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_for_input(p_source_color), 0);
  754. 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
  755. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_from_texture_pair(p_settings.glow_texture, p_settings.glow_map, true), 2); // should be set to a default texture, it's ignored
  756. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  757. RD::get_singleton()->draw_list_bind_index_array(p_subpass_draw_list, index_array);
  758. RD::get_singleton()->draw_list_set_push_constant(p_subpass_draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  759. RD::get_singleton()->draw_list_draw(p_subpass_draw_list, true);
  760. }
  761. 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) {
  762. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of luminance reduction with the mobile renderer.");
  763. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  764. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  765. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  766. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  767. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  768. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  769. for (int i = 0; i < p_reduce.size(); i++) {
  770. if (i == 0) {
  771. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  772. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  773. } else {
  774. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  775. if (i == p_reduce.size() - 1 && !p_set) {
  776. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  777. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  778. } else {
  779. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  780. }
  781. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  782. }
  783. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  784. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  785. RD::get_singleton()->compute_list_dispatch_threads(compute_list, luminance_reduce.push_constant.source_size[0], luminance_reduce.push_constant.source_size[1], 1);
  786. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  787. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  788. }
  789. RD::get_singleton()->compute_list_end();
  790. }
  791. 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) {
  792. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster version of luminance reduction with the clustered renderer.");
  793. ERR_FAIL_COND_MSG(p_reduce.size() != p_fb.size(), "Incorrect frame buffer account for luminance reduction.");
  794. luminance_reduce_raster.push_constant.max_luminance = p_max_luminance;
  795. luminance_reduce_raster.push_constant.min_luminance = p_min_luminance;
  796. luminance_reduce_raster.push_constant.exposure_adjust = p_adjust;
  797. for (int i = 0; i < p_reduce.size(); i++) {
  798. luminance_reduce_raster.push_constant.source_size[0] = i == 0 ? p_source_size.x : luminance_reduce_raster.push_constant.dest_size[0];
  799. luminance_reduce_raster.push_constant.source_size[1] = i == 0 ? p_source_size.y : luminance_reduce_raster.push_constant.dest_size[1];
  800. luminance_reduce_raster.push_constant.dest_size[0] = MAX(luminance_reduce_raster.push_constant.source_size[0] / 8, 1);
  801. luminance_reduce_raster.push_constant.dest_size[1] = MAX(luminance_reduce_raster.push_constant.source_size[1] / 8, 1);
  802. bool final = !p_set && (luminance_reduce_raster.push_constant.dest_size[0] == 1) && (luminance_reduce_raster.push_constant.dest_size[1] == 1);
  803. LuminanceReduceRasterMode mode = final ? LUMINANCE_REDUCE_FRAGMENT_FINAL : (i == 0 ? LUMINANCE_REDUCE_FRAGMENT_FIRST : LUMINANCE_REDUCE_FRAGMENT);
  804. 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);
  805. 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])));
  806. 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);
  807. if (final) {
  808. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_prev_luminance), 1);
  809. }
  810. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  811. RD::get_singleton()->draw_list_set_push_constant(draw_list, &luminance_reduce_raster.push_constant, sizeof(LuminanceReduceRasterPushConstant));
  812. RD::get_singleton()->draw_list_draw(draw_list, true);
  813. RD::get_singleton()->draw_list_end();
  814. }
  815. }
  816. 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) {
  817. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of BOKEH DOF with the mobile renderer.");
  818. bokeh.push_constant.blur_far_active = p_dof_far;
  819. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  820. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  821. bokeh.push_constant.blur_near_active = p_dof_near;
  822. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  823. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  824. bokeh.push_constant.use_jitter = p_use_jitter;
  825. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  826. bokeh.push_constant.z_near = p_cam_znear;
  827. bokeh.push_constant.z_far = p_cam_zfar;
  828. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  829. bokeh.push_constant.blur_size = p_bokeh_size;
  830. bokeh.push_constant.second_pass = false;
  831. bokeh.push_constant.half_size = false;
  832. bokeh.push_constant.blur_scale = 0.5;
  833. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  834. /* FIRST PASS */
  835. // The alpha channel of the source color texture is filled with the expected circle size
  836. // If used for DOF far, the size is positive, if used for near, its negative.
  837. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE]);
  838. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  839. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.depth_texture), 1);
  840. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  841. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  842. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  843. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  844. RD::get_singleton()->compute_list_add_barrier(compute_list);
  845. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  846. //second pass
  847. 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]);
  848. static const int quality_samples[4] = { 6, 12, 12, 24 };
  849. bokeh.push_constant.steps = quality_samples[p_quality];
  850. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  851. //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)
  852. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.half_texture[0]), 0);
  853. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.base_texture), 1);
  854. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  855. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  856. bokeh.push_constant.half_size = true;
  857. bokeh.push_constant.blur_size *= 0.5;
  858. } else {
  859. //medium and high quality use full size
  860. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.secondary_texture), 0);
  861. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.base_texture), 1);
  862. }
  863. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  864. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  865. RD::get_singleton()->compute_list_add_barrier(compute_list);
  866. //third pass
  867. bokeh.push_constant.second_pass = true;
  868. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  869. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.half_texture[1]), 0);
  870. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.half_texture[0]), 1);
  871. } else {
  872. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  873. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.secondary_texture), 1);
  874. }
  875. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  876. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  877. RD::get_singleton()->compute_list_add_barrier(compute_list);
  878. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  879. //forth pass, upscale for low quality
  880. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
  881. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  882. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.half_texture[1]), 1);
  883. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  884. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  885. bokeh.push_constant.half_size = false;
  886. bokeh.push_constant.second_pass = false;
  887. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  888. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  889. }
  890. } else {
  891. //circle
  892. //second pass
  893. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  894. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  895. bokeh.push_constant.steps = 0;
  896. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  897. //circle always runs in half size, otherwise too expensive
  898. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.half_texture[0]), 0);
  899. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.base_texture), 1);
  900. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  901. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  902. bokeh.push_constant.half_size = true;
  903. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  904. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  905. RD::get_singleton()->compute_list_add_barrier(compute_list);
  906. //circle is just one pass, then upscale
  907. // upscale
  908. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
  909. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_buffers.base_texture), 0);
  910. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_buffers.half_texture[0]), 1);
  911. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  912. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  913. bokeh.push_constant.half_size = false;
  914. bokeh.push_constant.second_pass = false;
  915. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  916. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  917. }
  918. RD::get_singleton()->compute_list_end();
  919. }
  920. 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) {
  921. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use blur DOF with the clustered renderer.");
  922. memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
  923. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  924. bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;
  925. bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;
  926. bokeh.push_constant.z_far = p_cam_zfar;
  927. bokeh.push_constant.z_near = p_cam_znear;
  928. bokeh.push_constant.second_pass = false;
  929. bokeh.push_constant.half_size = false;
  930. bokeh.push_constant.blur_size = p_dof_blur_amount;
  931. if (p_dof_far || p_dof_near) {
  932. if (p_dof_far) {
  933. bokeh.push_constant.blur_far_active = true;
  934. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  935. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  936. }
  937. if (p_dof_near) {
  938. bokeh.push_constant.blur_near_active = true;
  939. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  940. bokeh.push_constant.blur_near_end = p_dof_near_begin - p_dof_near_size;
  941. }
  942. {
  943. // generate our depth data
  944. RID framebuffer = p_buffers.base_weight_fb;
  945. 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);
  946. 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)));
  947. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.depth_texture), 0);
  948. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  949. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  950. RD::get_singleton()->draw_list_draw(draw_list, true);
  951. RD::get_singleton()->draw_list_end();
  952. }
  953. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  954. // double pass approach
  955. BokehMode mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
  956. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  957. //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)
  958. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  959. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  960. bokeh.push_constant.half_size = true;
  961. bokeh.push_constant.blur_size *= 0.5;
  962. }
  963. static const int quality_samples[4] = { 6, 12, 12, 24 };
  964. bokeh.push_constant.blur_scale = 0.5;
  965. bokeh.push_constant.steps = quality_samples[p_quality];
  966. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  967. // Pass 1
  968. 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);
  969. 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)));
  970. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.base_texture), 0);
  971. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 1);
  972. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  973. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  974. RD::get_singleton()->draw_list_draw(draw_list, true);
  975. RD::get_singleton()->draw_list_end();
  976. // Pass 2
  977. if (!bokeh.push_constant.half_size) {
  978. // do not output weight, we're writing back into our base buffer
  979. mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;
  980. }
  981. bokeh.push_constant.second_pass = true;
  982. framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;
  983. RID texture = bokeh.push_constant.half_size ? p_buffers.half_texture[0] : p_buffers.secondary_texture;
  984. RID weight = bokeh.push_constant.half_size ? p_buffers.weight_texture[2] : p_buffers.weight_texture[1];
  985. 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);
  986. 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)));
  987. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(texture), 0);
  988. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(weight), 1);
  989. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  990. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  991. RD::get_singleton()->draw_list_draw(draw_list, true);
  992. RD::get_singleton()->draw_list_end();
  993. if (bokeh.push_constant.half_size) {
  994. // Compose pass
  995. mode = BOKEH_COMPOSITE;
  996. framebuffer = p_buffers.base_fb;
  997. 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);
  998. 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)));
  999. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.half_texture[1]), 0);
  1000. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[3]), 1);
  1001. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 2);
  1002. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1003. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  1004. RD::get_singleton()->draw_list_draw(draw_list, true);
  1005. RD::get_singleton()->draw_list_end();
  1006. }
  1007. } else {
  1008. // circular is a single pass approach
  1009. BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;
  1010. {
  1011. // circle always runs in half size, otherwise too expensive (though the code below does support making this optional)
  1012. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  1013. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  1014. bokeh.push_constant.half_size = true;
  1015. // bokeh.push_constant.blur_size *= 0.5;
  1016. }
  1017. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  1018. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  1019. bokeh.push_constant.steps = 0.0;
  1020. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  1021. 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);
  1022. 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)));
  1023. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.base_texture), 0);
  1024. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 1);
  1025. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1026. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  1027. RD::get_singleton()->draw_list_draw(draw_list, true);
  1028. RD::get_singleton()->draw_list_end();
  1029. if (bokeh.push_constant.half_size) {
  1030. // Compose
  1031. mode = BOKEH_COMPOSITE;
  1032. framebuffer = p_buffers.base_fb;
  1033. 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);
  1034. 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)));
  1035. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.half_texture[0]), 0);
  1036. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[2]), 1);
  1037. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.weight_texture[0]), 2);
  1038. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1039. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  1040. RD::get_singleton()->draw_list_draw(draw_list, true);
  1041. RD::get_singleton()->draw_list_end();
  1042. } else {
  1043. // Just copy it back (we use our blur raster shader here)..
  1044. 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);
  1045. 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)));
  1046. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_buffers.secondary_texture), 0);
  1047. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1048. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  1049. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  1050. RD::get_singleton()->draw_list_draw(draw_list, true);
  1051. RD::get_singleton()->draw_list_end();
  1052. }
  1053. }
  1054. }
  1055. }
  1056. void EffectsRD::downsample_depth(RID p_depth_buffer, const Vector<RID> &p_depth_mipmaps, RS::EnvironmentSSAOQuality p_ssao_quality, RS::EnvironmentSSILQuality p_ssil_quality, bool p_invalidate_uniform_set, bool p_ssao_half_size, bool p_ssil_half_size, Size2i p_full_screen_size, const CameraMatrix &p_projection) {
  1057. // Downsample and deinterleave the depth buffer for SSAO and SSIL
  1058. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1059. int downsample_pipeline = SS_EFFECTS_DOWNSAMPLE;
  1060. bool use_mips = p_ssao_quality > RS::ENV_SSAO_QUALITY_MEDIUM || p_ssil_quality > RS::ENV_SSIL_QUALITY_MEDIUM;
  1061. if (p_ssao_quality == RS::ENV_SSAO_QUALITY_VERY_LOW && p_ssil_quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  1062. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_HALF;
  1063. } else if (use_mips) {
  1064. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  1065. }
  1066. bool use_half_size = false;
  1067. bool use_full_mips = false;
  1068. if (p_ssao_half_size && p_ssil_half_size) {
  1069. downsample_pipeline++;
  1070. use_half_size = true;
  1071. } else if (p_ssao_half_size != p_ssil_half_size) {
  1072. if (use_mips) {
  1073. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_FULL_MIPS;
  1074. use_full_mips = true;
  1075. } else {
  1076. // Only need the first two mipmaps, but the cost to generate the next two is trivial
  1077. // TODO investigate the benefit of a shader version to generate only 2 mips
  1078. downsample_pipeline = SS_EFFECTS_DOWNSAMPLE_MIPMAP;
  1079. use_mips = true;
  1080. }
  1081. }
  1082. int depth_index = use_half_size ? 1 : 0;
  1083. RD::get_singleton()->draw_command_begin_label("Downsample Depth");
  1084. if (p_invalidate_uniform_set || use_full_mips != ss_effects.used_full_mips_last_frame || use_half_size != ss_effects.used_half_size_last_frame || use_mips != ss_effects.used_mips_last_frame) {
  1085. Vector<RD::Uniform> uniforms;
  1086. {
  1087. RD::Uniform u;
  1088. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1089. u.binding = 0;
  1090. u.ids.push_back(p_depth_mipmaps[depth_index + 1]);
  1091. uniforms.push_back(u);
  1092. }
  1093. {
  1094. RD::Uniform u;
  1095. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1096. u.binding = 1;
  1097. u.ids.push_back(p_depth_mipmaps[depth_index + 2]);
  1098. uniforms.push_back(u);
  1099. }
  1100. {
  1101. RD::Uniform u;
  1102. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1103. u.binding = 2;
  1104. u.ids.push_back(p_depth_mipmaps[depth_index + 3]);
  1105. uniforms.push_back(u);
  1106. }
  1107. if (use_full_mips) {
  1108. RD::Uniform u;
  1109. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1110. u.binding = 3;
  1111. u.ids.push_back(p_depth_mipmaps[4]);
  1112. uniforms.push_back(u);
  1113. }
  1114. ss_effects.downsample_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, use_full_mips ? 6 : 2), 2);
  1115. }
  1116. float depth_linearize_mul = -p_projection.matrix[3][2];
  1117. float depth_linearize_add = p_projection.matrix[2][2];
  1118. if (depth_linearize_mul * depth_linearize_add < 0) {
  1119. depth_linearize_add = -depth_linearize_add;
  1120. }
  1121. ss_effects.downsample_push_constant.orthogonal = p_projection.is_orthogonal();
  1122. ss_effects.downsample_push_constant.z_near = depth_linearize_mul;
  1123. ss_effects.downsample_push_constant.z_far = depth_linearize_add;
  1124. if (ss_effects.downsample_push_constant.orthogonal) {
  1125. ss_effects.downsample_push_constant.z_near = p_projection.get_z_near();
  1126. ss_effects.downsample_push_constant.z_far = p_projection.get_z_far();
  1127. }
  1128. ss_effects.downsample_push_constant.pixel_size[0] = 1.0 / p_full_screen_size.x;
  1129. ss_effects.downsample_push_constant.pixel_size[1] = 1.0 / p_full_screen_size.y;
  1130. ss_effects.downsample_push_constant.radius_sq = 1.0;
  1131. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ss_effects.pipelines[downsample_pipeline]);
  1132. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  1133. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_depth_mipmaps[depth_index + 0]), 1);
  1134. if (use_mips) {
  1135. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ss_effects.downsample_uniform_set, 2);
  1136. }
  1137. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ss_effects.downsample_push_constant, sizeof(SSEffectsDownsamplePushConstant));
  1138. Size2i size(MAX(1, p_full_screen_size.x >> (use_half_size ? 2 : 1)), MAX(1, p_full_screen_size.y >> (use_half_size ? 2 : 1)));
  1139. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  1140. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1141. RD::get_singleton()->draw_command_end_label();
  1142. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_COMPUTE);
  1143. ss_effects.used_full_mips_last_frame = use_mips;
  1144. ss_effects.used_full_mips_last_frame = use_full_mips;
  1145. ss_effects.used_half_size_last_frame = use_half_size;
  1146. }
  1147. 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) {
  1148. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  1149. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  1150. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  1151. }
  1152. for (int i = 0; i < 4; i++) {
  1153. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1154. continue;
  1155. }
  1156. ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
  1157. ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
  1158. ssao.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  1159. ssao.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  1160. ssao.gather_push_constant.pass = i;
  1161. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 2);
  1162. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  1163. 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));
  1164. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  1165. }
  1166. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  1167. }
  1168. void EffectsRD::generate_ssao(RID p_normal_buffer, RID p_depth_mipmaps_texture, 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_gather_uniform_set, RID &r_importance_map_uniform_set) {
  1169. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1170. memset(&ssao.gather_push_constant, 0, sizeof(SSAOGatherPushConstant));
  1171. /* FIRST PASS */
  1172. RD::get_singleton()->draw_command_begin_label("Process Screen Space Ambient Occlusion");
  1173. /* SECOND PASS */
  1174. // Sample SSAO
  1175. {
  1176. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  1177. ssao.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  1178. ssao.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  1179. ssao.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1180. ssao.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1181. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  1182. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  1183. ssao.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  1184. ssao.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  1185. ssao.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  1186. ssao.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  1187. ssao.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  1188. ssao.gather_push_constant.half_screen_pixel_size_x025[0] = ssao.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  1189. ssao.gather_push_constant.half_screen_pixel_size_x025[1] = ssao.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  1190. ssao.gather_push_constant.radius = p_settings.radius;
  1191. float radius_near_limit = (p_settings.radius * 1.2f);
  1192. if (p_settings.quality <= RS::ENV_SSAO_QUALITY_LOW) {
  1193. radius_near_limit *= 1.50f;
  1194. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1195. ssao.gather_push_constant.radius *= 0.8f;
  1196. }
  1197. }
  1198. radius_near_limit /= tan_half_fov_y;
  1199. ssao.gather_push_constant.intensity = p_settings.intensity;
  1200. ssao.gather_push_constant.shadow_power = p_settings.power;
  1201. ssao.gather_push_constant.shadow_clamp = 0.98;
  1202. ssao.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  1203. ssao.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  1204. ssao.gather_push_constant.horizon_angle_threshold = p_settings.horizon;
  1205. ssao.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  1206. ssao.gather_push_constant.neg_inv_radius = -1.0 / ssao.gather_push_constant.radius;
  1207. ssao.gather_push_constant.load_counter_avg_div = 9.0 / float((p_settings.quarter_screen_size.x) * (p_settings.quarter_screen_size.y) * 255);
  1208. ssao.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  1209. ssao.gather_push_constant.detail_intensity = p_settings.detail;
  1210. ssao.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  1211. ssao.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  1212. if (p_invalidate_uniform_sets) {
  1213. Vector<RD::Uniform> uniforms;
  1214. {
  1215. RD::Uniform u;
  1216. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1217. u.binding = 0;
  1218. u.ids.push_back(default_sampler);
  1219. u.ids.push_back(p_depth_mipmaps_texture);
  1220. uniforms.push_back(u);
  1221. }
  1222. {
  1223. RD::Uniform u;
  1224. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1225. u.binding = 1;
  1226. u.ids.push_back(p_normal_buffer);
  1227. uniforms.push_back(u);
  1228. }
  1229. {
  1230. RD::Uniform u;
  1231. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1232. u.binding = 2;
  1233. u.ids.push_back(ss_effects.gather_constants_buffer);
  1234. uniforms.push_back(u);
  1235. }
  1236. r_gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 0), 0);
  1237. }
  1238. if (p_invalidate_uniform_sets) {
  1239. Vector<RD::Uniform> uniforms;
  1240. {
  1241. RD::Uniform u;
  1242. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1243. u.binding = 0;
  1244. u.ids.push_back(p_ao_pong);
  1245. uniforms.push_back(u);
  1246. }
  1247. {
  1248. RD::Uniform u;
  1249. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1250. u.binding = 1;
  1251. u.ids.push_back(default_sampler);
  1252. u.ids.push_back(p_importance_map);
  1253. uniforms.push_back(u);
  1254. }
  1255. {
  1256. RD::Uniform u;
  1257. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1258. u.binding = 2;
  1259. u.ids.push_back(ssao.importance_map_load_counter);
  1260. uniforms.push_back(u);
  1261. }
  1262. r_importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssao.gather_shader.version_get_shader(ssao.gather_shader_version, 2), 1);
  1263. }
  1264. if (p_settings.quality == RS::ENV_SSAO_QUALITY_ULTRA) {
  1265. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  1266. ssao.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1267. ssao.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1268. ssao.importance_map_push_constant.intensity = p_settings.intensity;
  1269. ssao.importance_map_push_constant.power = p_settings.power;
  1270. //base pass
  1271. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_BASE]);
  1272. gather_ssao(compute_list, p_ao_pong_slices, p_settings, true, r_gather_uniform_set, RID());
  1273. //generate importance map
  1274. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GENERATE_IMPORTANCE_MAP]);
  1275. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 0);
  1276. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  1277. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1278. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1279. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1280. //process importance map A
  1281. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPA]);
  1282. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map), 0);
  1283. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map_pong), 1);
  1284. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1285. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1286. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1287. //process Importance Map B
  1288. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_PROCESS_IMPORTANCE_MAPB]);
  1289. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map_pong), 0);
  1290. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  1291. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssao.counter_uniform_set, 2);
  1292. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.importance_map_push_constant, sizeof(SSAOImportanceMapPushConstant));
  1293. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1294. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1295. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER_ADAPTIVE]);
  1296. RD::get_singleton()->draw_command_end_label(); // Importance Map
  1297. } else {
  1298. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_GATHER]);
  1299. }
  1300. gather_ssao(compute_list, p_ao_slices, p_settings, false, r_gather_uniform_set, r_importance_map_uniform_set);
  1301. RD::get_singleton()->draw_command_end_label(); // Gather SSAO
  1302. }
  1303. // /* THIRD PASS */
  1304. // // Blur
  1305. //
  1306. {
  1307. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  1308. ssao.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  1309. ssao.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1310. ssao.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1311. int blur_passes = p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  1312. for (int pass = 0; pass < blur_passes; pass++) {
  1313. int blur_pipeline = SSAO_BLUR_PASS;
  1314. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1315. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1316. if (pass < blur_passes - 2) {
  1317. blur_pipeline = SSAO_BLUR_PASS_WIDE;
  1318. } else {
  1319. blur_pipeline = SSAO_BLUR_PASS_SMART;
  1320. }
  1321. }
  1322. for (int i = 0; i < 4; i++) {
  1323. if ((p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1324. continue;
  1325. }
  1326. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[blur_pipeline]);
  1327. if (pass % 2 == 0) {
  1328. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1329. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_slices[i]), 0);
  1330. } else {
  1331. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_slices[i], ss_effects.mirror_sampler), 0);
  1332. }
  1333. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_pong_slices[i]), 1);
  1334. } else {
  1335. if (p_settings.quality == RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1336. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong_slices[i]), 0);
  1337. } else {
  1338. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ao_pong_slices[i], ss_effects.mirror_sampler), 0);
  1339. }
  1340. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao_slices[i]), 1);
  1341. }
  1342. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  1343. 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));
  1344. RD::get_singleton()->compute_list_dispatch_threads(compute_list, size.x, size.y, 1);
  1345. }
  1346. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW) {
  1347. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1348. }
  1349. }
  1350. RD::get_singleton()->draw_command_end_label(); // Blur
  1351. }
  1352. /* FOURTH PASS */
  1353. // Interleave buffers
  1354. // back to full size
  1355. {
  1356. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1357. ssao.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1358. ssao.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1359. ssao.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1360. ssao.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  1361. int interleave_pipeline = SSAO_INTERLEAVE_HALF;
  1362. if (p_settings.quality == RS::ENV_SSAO_QUALITY_LOW) {
  1363. interleave_pipeline = SSAO_INTERLEAVE;
  1364. } else if (p_settings.quality >= RS::ENV_SSAO_QUALITY_MEDIUM) {
  1365. interleave_pipeline = SSAO_INTERLEAVE_SMART;
  1366. }
  1367. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[interleave_pipeline]);
  1368. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 0);
  1369. if (p_settings.quality > RS::ENV_SSAO_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  1370. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao), 1);
  1371. } else {
  1372. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao_pong), 1);
  1373. }
  1374. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.interleave_push_constant, sizeof(SSAOInterleavePushConstant));
  1375. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1376. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1377. RD::get_singleton()->draw_command_end_label(); // Interleave
  1378. }
  1379. RD::get_singleton()->draw_command_end_label(); //SSAO
  1380. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER); //wait for upcoming transfer
  1381. int zero[1] = { 0 };
  1382. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  1383. }
  1384. void EffectsRD::gather_ssil(RD::ComputeListID p_compute_list, const Vector<RID> p_ssil_slices, const Vector<RID> p_edges_slices, const SSILSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set, RID p_projection_uniform_set) {
  1385. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_gather_uniform_set, 0);
  1386. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_ULTRA) && !p_adaptive_base_pass) {
  1387. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_importance_map_uniform_set, 1);
  1388. }
  1389. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, p_projection_uniform_set, 3);
  1390. for (int i = 0; i < 4; i++) {
  1391. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1392. continue;
  1393. }
  1394. ssil.gather_push_constant.pass_coord_offset[0] = i % 2;
  1395. ssil.gather_push_constant.pass_coord_offset[1] = i / 2;
  1396. ssil.gather_push_constant.pass_uv_offset[0] = ((i % 2) - 0.0) / p_settings.full_screen_size.x;
  1397. ssil.gather_push_constant.pass_uv_offset[1] = ((i / 2) - 0.0) / p_settings.full_screen_size.y;
  1398. ssil.gather_push_constant.pass = i;
  1399. RD::get_singleton()->compute_list_bind_uniform_set(p_compute_list, _get_compute_uniform_set_from_image_pair(p_ssil_slices[i], p_edges_slices[i]), 2);
  1400. RD::get_singleton()->compute_list_set_push_constant(p_compute_list, &ssil.gather_push_constant, sizeof(SSILGatherPushConstant));
  1401. 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));
  1402. RD::get_singleton()->compute_list_dispatch_threads(p_compute_list, size.x, size.y, 1);
  1403. }
  1404. RD::get_singleton()->compute_list_add_barrier(p_compute_list);
  1405. }
  1406. void EffectsRD::screen_space_indirect_lighting(RID p_diffuse, RID p_destination, RID p_normal_buffer, RID p_depth_mipmaps_texture, RID p_ssil, const Vector<RID> p_ssil_slices, RID p_ssil_pong, const Vector<RID> p_ssil_pong_slices, RID p_importance_map, RID p_importance_map_pong, RID p_edges, const Vector<RID> p_edges_slices, const CameraMatrix &p_projection, const CameraMatrix &p_last_projection, const SSILSettings &p_settings, bool p_invalidate_uniform_sets, RID &r_gather_uniform_set, RID &r_importance_map_uniform_set, RID &r_projection_uniform_set) {
  1407. RD::get_singleton()->draw_command_begin_label("Process Screen Space Indirect Lighting");
  1408. //Store projection info before starting the compute list
  1409. SSILProjectionUniforms projection_uniforms;
  1410. store_camera(p_last_projection, projection_uniforms.inv_last_frame_projection_matrix);
  1411. RD::get_singleton()->buffer_update(ssil.projection_uniform_buffer, 0, sizeof(SSILProjectionUniforms), &projection_uniforms);
  1412. memset(&ssil.gather_push_constant, 0, sizeof(SSILGatherPushConstant));
  1413. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1414. {
  1415. RD::get_singleton()->draw_command_begin_label("Gather Samples");
  1416. ssil.gather_push_constant.screen_size[0] = p_settings.full_screen_size.x;
  1417. ssil.gather_push_constant.screen_size[1] = p_settings.full_screen_size.y;
  1418. ssil.gather_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1419. ssil.gather_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1420. float tan_half_fov_x = 1.0 / p_projection.matrix[0][0];
  1421. float tan_half_fov_y = 1.0 / p_projection.matrix[1][1];
  1422. ssil.gather_push_constant.NDC_to_view_mul[0] = tan_half_fov_x * 2.0;
  1423. ssil.gather_push_constant.NDC_to_view_mul[1] = tan_half_fov_y * -2.0;
  1424. ssil.gather_push_constant.NDC_to_view_add[0] = tan_half_fov_x * -1.0;
  1425. ssil.gather_push_constant.NDC_to_view_add[1] = tan_half_fov_y;
  1426. ssil.gather_push_constant.z_near = p_projection.get_z_near();
  1427. ssil.gather_push_constant.z_far = p_projection.get_z_far();
  1428. ssil.gather_push_constant.is_orthogonal = p_projection.is_orthogonal();
  1429. ssil.gather_push_constant.half_screen_pixel_size_x025[0] = ssil.gather_push_constant.half_screen_pixel_size[0] * 0.25;
  1430. ssil.gather_push_constant.half_screen_pixel_size_x025[1] = ssil.gather_push_constant.half_screen_pixel_size[1] * 0.25;
  1431. ssil.gather_push_constant.radius = p_settings.radius;
  1432. float radius_near_limit = (p_settings.radius * 1.2f);
  1433. if (p_settings.quality <= RS::ENV_SSIL_QUALITY_LOW) {
  1434. radius_near_limit *= 1.50f;
  1435. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  1436. ssil.gather_push_constant.radius *= 0.8f;
  1437. }
  1438. }
  1439. radius_near_limit /= tan_half_fov_y;
  1440. ssil.gather_push_constant.intensity = p_settings.intensity * Math_PI;
  1441. ssil.gather_push_constant.fade_out_mul = -1.0 / (p_settings.fadeout_to - p_settings.fadeout_from);
  1442. ssil.gather_push_constant.fade_out_add = p_settings.fadeout_from / (p_settings.fadeout_to - p_settings.fadeout_from) + 1.0;
  1443. ssil.gather_push_constant.inv_radius_near_limit = 1.0f / radius_near_limit;
  1444. ssil.gather_push_constant.neg_inv_radius = -1.0 / ssil.gather_push_constant.radius;
  1445. ssil.gather_push_constant.normal_rejection_amount = p_settings.normal_rejection;
  1446. ssil.gather_push_constant.load_counter_avg_div = 9.0 / float((p_settings.quarter_screen_size.x) * (p_settings.quarter_screen_size.y) * 255);
  1447. ssil.gather_push_constant.adaptive_sample_limit = p_settings.adaptive_target;
  1448. ssil.gather_push_constant.quality = MAX(0, p_settings.quality - 1);
  1449. ssil.gather_push_constant.size_multiplier = p_settings.half_size ? 2 : 1;
  1450. if (p_invalidate_uniform_sets) {
  1451. Vector<RD::Uniform> uniforms;
  1452. {
  1453. RD::Uniform u;
  1454. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1455. u.binding = 0;
  1456. u.ids.push_back(default_mipmap_sampler);
  1457. u.ids.push_back(p_diffuse);
  1458. uniforms.push_back(u);
  1459. }
  1460. {
  1461. RD::Uniform u;
  1462. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1463. u.binding = 1;
  1464. u.ids.push_back(ssil.projection_uniform_buffer);
  1465. uniforms.push_back(u);
  1466. }
  1467. r_projection_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0), 3);
  1468. }
  1469. if (p_invalidate_uniform_sets) {
  1470. Vector<RD::Uniform> uniforms;
  1471. {
  1472. RD::Uniform u;
  1473. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1474. u.binding = 0;
  1475. u.ids.push_back(default_sampler);
  1476. u.ids.push_back(p_depth_mipmaps_texture);
  1477. uniforms.push_back(u);
  1478. }
  1479. {
  1480. RD::Uniform u;
  1481. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1482. u.binding = 1;
  1483. u.ids.push_back(p_normal_buffer);
  1484. uniforms.push_back(u);
  1485. }
  1486. {
  1487. RD::Uniform u;
  1488. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1489. u.binding = 2;
  1490. u.ids.push_back(ss_effects.gather_constants_buffer);
  1491. uniforms.push_back(u);
  1492. }
  1493. r_gather_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 0), 0);
  1494. }
  1495. if (p_invalidate_uniform_sets) {
  1496. Vector<RD::Uniform> uniforms;
  1497. {
  1498. RD::Uniform u;
  1499. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1500. u.binding = 0;
  1501. u.ids.push_back(p_ssil_pong);
  1502. uniforms.push_back(u);
  1503. }
  1504. {
  1505. RD::Uniform u;
  1506. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  1507. u.binding = 1;
  1508. u.ids.push_back(default_sampler);
  1509. u.ids.push_back(p_importance_map);
  1510. uniforms.push_back(u);
  1511. }
  1512. {
  1513. RD::Uniform u;
  1514. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1515. u.binding = 2;
  1516. u.ids.push_back(ssil.importance_map_load_counter);
  1517. uniforms.push_back(u);
  1518. }
  1519. r_importance_map_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.gather_shader.version_get_shader(ssil.gather_shader_version, 2), 1);
  1520. }
  1521. if (p_settings.quality == RS::ENV_SSIL_QUALITY_ULTRA) {
  1522. RD::get_singleton()->draw_command_begin_label("Generate Importance Map");
  1523. ssil.importance_map_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1524. ssil.importance_map_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1525. ssil.importance_map_push_constant.intensity = p_settings.intensity * Math_PI;
  1526. //base pass
  1527. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_BASE]);
  1528. gather_ssil(compute_list, p_ssil_pong_slices, p_edges_slices, p_settings, true, r_gather_uniform_set, r_importance_map_uniform_set, r_projection_uniform_set);
  1529. //generate importance map
  1530. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GENERATE_IMPORTANCE_MAP]);
  1531. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_pong), 0);
  1532. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  1533. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  1534. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1535. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1536. // process Importance Map A
  1537. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPA]);
  1538. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map), 0);
  1539. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map_pong), 1);
  1540. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  1541. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1542. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1543. // process Importance Map B
  1544. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_PROCESS_IMPORTANCE_MAPB]);
  1545. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_importance_map_pong), 0);
  1546. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_importance_map), 1);
  1547. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, ssil.counter_uniform_set, 2);
  1548. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.importance_map_push_constant, sizeof(SSILImportanceMapPushConstant));
  1549. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.quarter_screen_size.x, p_settings.quarter_screen_size.y, 1);
  1550. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1551. RD::get_singleton()->draw_command_end_label(); // Importance Map
  1552. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER_ADAPTIVE]);
  1553. } else {
  1554. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[SSIL_GATHER]);
  1555. }
  1556. gather_ssil(compute_list, p_ssil_slices, p_edges_slices, p_settings, false, r_gather_uniform_set, r_importance_map_uniform_set, r_projection_uniform_set);
  1557. RD::get_singleton()->draw_command_end_label(); //Gather
  1558. }
  1559. {
  1560. RD::get_singleton()->draw_command_begin_label("Edge Aware Blur");
  1561. ssil.blur_push_constant.edge_sharpness = 1.0 - p_settings.sharpness;
  1562. ssil.blur_push_constant.half_screen_pixel_size[0] = 1.0 / p_settings.half_screen_size.x;
  1563. ssil.blur_push_constant.half_screen_pixel_size[1] = 1.0 / p_settings.half_screen_size.y;
  1564. int blur_passes = p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW ? p_settings.blur_passes : 1;
  1565. for (int pass = 0; pass < blur_passes; pass++) {
  1566. int blur_pipeline = SSIL_BLUR_PASS;
  1567. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  1568. blur_pipeline = SSIL_BLUR_PASS_SMART;
  1569. if (pass < blur_passes - 2) {
  1570. blur_pipeline = SSIL_BLUR_PASS_WIDE;
  1571. }
  1572. }
  1573. for (int i = 0; i < 4; i++) {
  1574. if ((p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) && ((i == 1) || (i == 2))) {
  1575. continue;
  1576. }
  1577. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[blur_pipeline]);
  1578. if (pass % 2 == 0) {
  1579. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  1580. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_slices[i]), 0);
  1581. } else {
  1582. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ssil_slices[i], ss_effects.mirror_sampler), 0);
  1583. }
  1584. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ssil_pong_slices[i]), 1);
  1585. } else {
  1586. if (p_settings.quality == RS::ENV_SSIL_QUALITY_VERY_LOW) {
  1587. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_pong_slices[i]), 0);
  1588. } else {
  1589. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_and_sampler(p_ssil_pong_slices[i], ss_effects.mirror_sampler), 0);
  1590. }
  1591. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ssil_slices[i]), 1);
  1592. }
  1593. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_edges_slices[i]), 2);
  1594. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.blur_push_constant, sizeof(SSILBlurPushConstant));
  1595. int x_groups = (p_settings.full_screen_size.x >> (p_settings.half_size ? 2 : 1));
  1596. int y_groups = (p_settings.full_screen_size.y >> (p_settings.half_size ? 2 : 1));
  1597. RD::get_singleton()->compute_list_dispatch_threads(compute_list, x_groups, y_groups, 1);
  1598. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW) {
  1599. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1600. }
  1601. }
  1602. }
  1603. RD::get_singleton()->draw_command_end_label(); // Blur
  1604. }
  1605. {
  1606. RD::get_singleton()->draw_command_begin_label("Interleave Buffers");
  1607. ssil.interleave_push_constant.inv_sharpness = 1.0 - p_settings.sharpness;
  1608. ssil.interleave_push_constant.pixel_size[0] = 1.0 / p_settings.full_screen_size.x;
  1609. ssil.interleave_push_constant.pixel_size[1] = 1.0 / p_settings.full_screen_size.y;
  1610. ssil.interleave_push_constant.size_modifier = uint32_t(p_settings.half_size ? 4 : 2);
  1611. int interleave_pipeline = SSIL_INTERLEAVE_HALF;
  1612. if (p_settings.quality == RS::ENV_SSIL_QUALITY_LOW) {
  1613. interleave_pipeline = SSIL_INTERLEAVE;
  1614. } else if (p_settings.quality >= RS::ENV_SSIL_QUALITY_MEDIUM) {
  1615. interleave_pipeline = SSIL_INTERLEAVE_SMART;
  1616. }
  1617. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssil.pipelines[interleave_pipeline]);
  1618. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_destination), 0);
  1619. if (p_settings.quality > RS::ENV_SSIL_QUALITY_VERY_LOW && p_settings.blur_passes % 2 == 0) {
  1620. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil), 1);
  1621. } else {
  1622. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ssil_pong), 1);
  1623. }
  1624. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_edges), 2);
  1625. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssil.interleave_push_constant, sizeof(SSILInterleavePushConstant));
  1626. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_settings.full_screen_size.x, p_settings.full_screen_size.y, 1);
  1627. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1628. RD::get_singleton()->draw_command_end_label(); // Interleave
  1629. }
  1630. RD::get_singleton()->draw_command_end_label(); // SSIL
  1631. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_NO_BARRIER);
  1632. int zero[1] = { 0 };
  1633. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero, 0); //no barrier
  1634. }
  1635. void EffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  1636. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  1637. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  1638. roughness_limiter.push_constant.curve = p_curve;
  1639. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1640. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  1641. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  1642. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  1643. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  1644. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
  1645. RD::get_singleton()->compute_list_end();
  1646. }
  1647. 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) {
  1648. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");
  1649. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  1650. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  1651. roughness.push_constant.roughness = p_roughness;
  1652. roughness.push_constant.sample_count = p_sample_count;
  1653. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1654. roughness.push_constant.face_size = p_size;
  1655. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1656. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);
  1657. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  1658. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 1);
  1659. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  1660. int x_groups = (p_size - 1) / 8 + 1;
  1661. int y_groups = (p_size - 1) / 8 + 1;
  1662. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  1663. RD::get_singleton()->compute_list_end();
  1664. }
  1665. 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) {
  1666. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");
  1667. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");
  1668. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  1669. roughness.push_constant.face_id = p_face_id;
  1670. roughness.push_constant.roughness = p_roughness;
  1671. roughness.push_constant.sample_count = p_sample_count;
  1672. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  1673. roughness.push_constant.face_size = p_size;
  1674. 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);
  1675. 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)));
  1676. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  1677. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1678. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  1679. RD::get_singleton()->draw_list_draw(draw_list, true);
  1680. RD::get_singleton()->draw_list_end();
  1681. }
  1682. void EffectsRD::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  1683. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");
  1684. cubemap_downsampler.push_constant.face_size = p_size.x;
  1685. cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call
  1686. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1687. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);
  1688. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap), 0);
  1689. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_cubemap), 1);
  1690. int x_groups = (p_size.x - 1) / 8 + 1;
  1691. int y_groups = (p_size.y - 1) / 8 + 1;
  1692. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  1693. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  1694. RD::get_singleton()->compute_list_end();
  1695. }
  1696. void EffectsRD::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {
  1697. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");
  1698. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");
  1699. cubemap_downsampler.push_constant.face_size = p_size.x;
  1700. cubemap_downsampler.push_constant.face_id = p_face_id;
  1701. 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);
  1702. 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)));
  1703. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_cubemap), 0);
  1704. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1705. RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  1706. RD::get_singleton()->draw_list_draw(draw_list, true);
  1707. RD::get_singleton()->draw_list_end();
  1708. }
  1709. void EffectsRD::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  1710. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");
  1711. Vector<RD::Uniform> uniforms;
  1712. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  1713. RD::Uniform u;
  1714. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1715. u.binding = i;
  1716. u.ids.push_back(p_dest_cubemap[i]);
  1717. uniforms.push_back(u);
  1718. }
  1719. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1720. RD::get_singleton()->free(filter.image_uniform_set);
  1721. }
  1722. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
  1723. int pipeline = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  1724. pipeline = filter.use_high_quality ? pipeline : pipeline + 1;
  1725. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1726. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[pipeline]);
  1727. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap, true), 0);
  1728. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  1729. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  1730. 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
  1731. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  1732. RD::get_singleton()->compute_list_end();
  1733. }
  1734. void EffectsRD::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {
  1735. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");
  1736. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");
  1737. // TODO implement!
  1738. CubemapFilterRasterPushConstant push_constant;
  1739. push_constant.mip_level = p_mip_level;
  1740. push_constant.face_id = p_face_id;
  1741. CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;
  1742. 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);
  1743. 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)));
  1744. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_cubemap), 0);
  1745. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
  1746. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  1747. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));
  1748. RD::get_singleton()->draw_list_draw(draw_list, true);
  1749. RD::get_singleton()->draw_list_end();
  1750. }
  1751. 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) {
  1752. ResolvePushConstant push_constant;
  1753. push_constant.screen_size[0] = p_screen_size.x;
  1754. push_constant.screen_size[1] = p_screen_size.y;
  1755. push_constant.samples = p_samples;
  1756. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1757. 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]);
  1758. 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);
  1759. 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);
  1760. if (p_source_voxel_gi.is_valid()) {
  1761. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_voxel_gi), 2);
  1762. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_voxel_gi), 3);
  1763. }
  1764. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  1765. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
  1766. RD::get_singleton()->compute_list_end(p_barrier);
  1767. }
  1768. void EffectsRD::resolve_depth(RID p_source_depth, RID p_dest_depth, Vector2i p_screen_size, int p_samples, uint32_t p_barrier) {
  1769. ResolvePushConstant push_constant;
  1770. push_constant.screen_size[0] = p_screen_size.x;
  1771. push_constant.screen_size[1] = p_screen_size.y;
  1772. push_constant.samples = p_samples;
  1773. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1774. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[RESOLVE_MODE_DEPTH]);
  1775. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_depth), 0);
  1776. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_depth), 1);
  1777. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  1778. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
  1779. RD::get_singleton()->compute_list_end(p_barrier);
  1780. }
  1781. void EffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  1782. Sort::PushConstant push_constant;
  1783. push_constant.total_elements = p_size;
  1784. bool done = true;
  1785. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  1786. if (numThreadGroups > 1) {
  1787. done = false;
  1788. }
  1789. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1790. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  1791. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  1792. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1793. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1794. int presorted = 512;
  1795. while (!done) {
  1796. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1797. done = true;
  1798. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  1799. numThreadGroups = 0;
  1800. if (p_size > presorted) {
  1801. if (p_size > presorted * 2) {
  1802. done = false;
  1803. }
  1804. int pow2 = presorted;
  1805. while (pow2 < p_size) {
  1806. pow2 *= 2;
  1807. }
  1808. numThreadGroups = pow2 >> 9;
  1809. }
  1810. unsigned int nMergeSize = presorted * 2;
  1811. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  1812. push_constant.job_params[0] = nMergeSubSize;
  1813. if (nMergeSubSize == nMergeSize >> 1) {
  1814. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  1815. push_constant.job_params[2] = -1;
  1816. } else {
  1817. push_constant.job_params[1] = nMergeSubSize;
  1818. push_constant.job_params[2] = 1;
  1819. }
  1820. push_constant.job_params[3] = 0;
  1821. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1822. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1823. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1824. }
  1825. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  1826. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1827. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1828. presorted *= 2;
  1829. }
  1830. RD::get_singleton()->compute_list_end();
  1831. }
  1832. EffectsRD::EffectsRD(bool p_prefer_raster_effects) {
  1833. {
  1834. Vector<String> FSR_upscale_modes;
  1835. #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
  1836. // MoltenVK does not support some of the operations used by the normal mode of FSR. Fallback works just fine though.
  1837. FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n");
  1838. #else
  1839. // Everyone else can use normal mode when available.
  1840. if (RD::get_singleton()->get_device_capabilities()->supports_fsr_half_float) {
  1841. FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_NORMAL\n");
  1842. } else {
  1843. FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n");
  1844. }
  1845. #endif
  1846. FSR_upscale.shader.initialize(FSR_upscale_modes);
  1847. FSR_upscale.shader_version = FSR_upscale.shader.version_create();
  1848. FSR_upscale.pipeline = RD::get_singleton()->compute_pipeline_create(FSR_upscale.shader.version_get_shader(FSR_upscale.shader_version, 0));
  1849. }
  1850. prefer_raster_effects = p_prefer_raster_effects;
  1851. if (prefer_raster_effects) {
  1852. // init blur shader (on compute use copy shader)
  1853. Vector<String> blur_modes;
  1854. blur_modes.push_back("\n#define MODE_MIPMAP\n"); // BLUR_MIPMAP
  1855. blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n"); // BLUR_MODE_GAUSSIAN_BLUR
  1856. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n"); // BLUR_MODE_GAUSSIAN_GLOW
  1857. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n"); // BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE
  1858. blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY
  1859. blur_raster.shader.initialize(blur_modes);
  1860. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  1861. blur_raster.shader_version = blur_raster.shader.version_create();
  1862. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  1863. 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);
  1864. }
  1865. } else {
  1866. // not used in clustered
  1867. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  1868. blur_raster.pipelines[i].clear();
  1869. }
  1870. }
  1871. if (!prefer_raster_effects) { // Initialize copy
  1872. Vector<String> copy_modes;
  1873. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  1874. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  1875. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n");
  1876. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  1877. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  1878. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  1879. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  1880. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  1881. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  1882. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  1883. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  1884. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  1885. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  1886. copy.shader.initialize(copy_modes);
  1887. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  1888. if (prefer_raster_effects) {
  1889. // disable shaders we can't use
  1890. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_COPY, false);
  1891. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_COPY_8BIT, false);
  1892. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_GLOW, false);
  1893. copy.shader.set_variant_enabled(COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE, false);
  1894. }
  1895. copy.shader_version = copy.shader.version_create();
  1896. for (int i = 0; i < COPY_MODE_MAX; i++) {
  1897. if (copy.shader.is_variant_enabled(i)) {
  1898. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  1899. }
  1900. }
  1901. }
  1902. {
  1903. Vector<String> copy_modes;
  1904. copy_modes.push_back("\n");
  1905. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n");
  1906. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n");
  1907. copy_to_fb.shader.initialize(copy_modes);
  1908. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  1909. //use additive
  1910. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  1911. 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);
  1912. }
  1913. }
  1914. {
  1915. // Initialize roughness
  1916. Vector<String> cubemap_roughness_modes;
  1917. cubemap_roughness_modes.push_back("");
  1918. if (prefer_raster_effects) {
  1919. roughness.raster_shader.initialize(cubemap_roughness_modes);
  1920. roughness.shader_version = roughness.raster_shader.version_create();
  1921. 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);
  1922. } else {
  1923. roughness.compute_shader.initialize(cubemap_roughness_modes);
  1924. roughness.shader_version = roughness.compute_shader.version_create();
  1925. roughness.compute_pipeline = RD::get_singleton()->compute_pipeline_create(roughness.compute_shader.version_get_shader(roughness.shader_version, 0));
  1926. roughness.raster_pipeline.clear();
  1927. }
  1928. }
  1929. {
  1930. // Initialize tonemapper
  1931. Vector<String> tonemap_modes;
  1932. tonemap_modes.push_back("\n");
  1933. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  1934. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  1935. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1936. tonemap_modes.push_back("\n#define SUBPASS\n");
  1937. tonemap_modes.push_back("\n#define SUBPASS\n#define USE_1D_LUT\n");
  1938. // multiview versions of our shaders
  1939. tonemap_modes.push_back("\n#define MULTIVIEW\n");
  1940. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n");
  1941. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_1D_LUT\n");
  1942. tonemap_modes.push_back("\n#define MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  1943. tonemap_modes.push_back("\n#define MULTIVIEW\n#define SUBPASS\n");
  1944. tonemap_modes.push_back("\n#define MULTIVIEW\n#define SUBPASS\n#define USE_1D_LUT\n");
  1945. tonemap.shader.initialize(tonemap_modes);
  1946. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  1947. tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false);
  1948. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false);
  1949. tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false);
  1950. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false);
  1951. tonemap.shader.set_variant_enabled(TONEMAP_MODE_SUBPASS_MULTIVIEW, false);
  1952. tonemap.shader.set_variant_enabled(TONEMAP_MODE_SUBPASS_1D_LUT_MULTIVIEW, false);
  1953. }
  1954. tonemap.shader_version = tonemap.shader.version_create();
  1955. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  1956. if (tonemap.shader.is_variant_enabled(i)) {
  1957. 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);
  1958. } else {
  1959. tonemap.pipelines[i].clear();
  1960. }
  1961. }
  1962. }
  1963. if (prefer_raster_effects) {
  1964. Vector<String> luminance_reduce_modes;
  1965. luminance_reduce_modes.push_back("\n#define FIRST_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FIRST
  1966. luminance_reduce_modes.push_back("\n"); // LUMINANCE_REDUCE_FRAGMENT
  1967. luminance_reduce_modes.push_back("\n#define FINAL_PASS\n"); // LUMINANCE_REDUCE_FRAGMENT_FINAL
  1968. luminance_reduce_raster.shader.initialize(luminance_reduce_modes);
  1969. memset(&luminance_reduce_raster.push_constant, 0, sizeof(LuminanceReduceRasterPushConstant));
  1970. luminance_reduce_raster.shader_version = luminance_reduce_raster.shader.version_create();
  1971. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  1972. 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);
  1973. }
  1974. } else {
  1975. // Initialize luminance_reduce
  1976. Vector<String> luminance_reduce_modes;
  1977. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  1978. luminance_reduce_modes.push_back("\n");
  1979. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  1980. luminance_reduce.shader.initialize(luminance_reduce_modes);
  1981. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  1982. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  1983. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  1984. }
  1985. for (int i = 0; i < LUMINANCE_REDUCE_FRAGMENT_MAX; i++) {
  1986. luminance_reduce_raster.pipelines[i].clear();
  1987. }
  1988. }
  1989. {
  1990. // Initialize copier
  1991. Vector<String> copy_modes;
  1992. copy_modes.push_back("\n");
  1993. cube_to_dp.shader.initialize(copy_modes);
  1994. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  1995. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  1996. RD::PipelineDepthStencilState dss;
  1997. dss.enable_depth_test = true;
  1998. dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
  1999. dss.enable_depth_write = true;
  2000. cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
  2001. }
  2002. // Initialize bokeh
  2003. Vector<String> bokeh_modes;
  2004. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  2005. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n#define OUTPUT_WEIGHT\n");
  2006. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  2007. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n#define OUTPUT_WEIGHT\n");
  2008. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  2009. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n#define OUTPUT_WEIGHT\n");
  2010. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  2011. if (prefer_raster_effects) {
  2012. bokeh.raster_shader.initialize(bokeh_modes);
  2013. bokeh.shader_version = bokeh.raster_shader.version_create();
  2014. const int att_count[BOKEH_MAX] = { 1, 2, 1, 2, 1, 2, 1 };
  2015. for (int i = 0; i < BOKEH_MAX; i++) {
  2016. RD::PipelineColorBlendState blend_state = (i == BOKEH_COMPOSITE) ? RD::PipelineColorBlendState::create_blend(att_count[i]) : RD::PipelineColorBlendState::create_disabled(att_count[i]);
  2017. 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);
  2018. }
  2019. } else {
  2020. bokeh.compute_shader.initialize(bokeh_modes);
  2021. bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_BOX_NOWEIGHT, false);
  2022. bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT, false);
  2023. bokeh.shader_version = bokeh.compute_shader.version_create();
  2024. for (int i = 0; i < BOKEH_MAX; i++) {
  2025. if (bokeh.compute_shader.is_variant_enabled(i)) {
  2026. bokeh.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.compute_shader.version_get_shader(bokeh.shader_version, i));
  2027. }
  2028. }
  2029. for (int i = 0; i < BOKEH_MAX; i++) {
  2030. bokeh.raster_pipelines[i].clear();
  2031. }
  2032. }
  2033. if (!prefer_raster_effects) {
  2034. {
  2035. // Initialize depth buffer for screen space effects
  2036. Vector<String> downsampler_modes;
  2037. downsampler_modes.push_back("\n");
  2038. downsampler_modes.push_back("\n#define USE_HALF_SIZE\n");
  2039. downsampler_modes.push_back("\n#define GENERATE_MIPS\n");
  2040. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define USE_HALF_SIZE\n");
  2041. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n");
  2042. downsampler_modes.push_back("\n#define USE_HALF_BUFFERS\n#define USE_HALF_SIZE\n");
  2043. downsampler_modes.push_back("\n#define GENERATE_MIPS\n#define GENERATE_FULL_MIPS");
  2044. ss_effects.downsample_shader.initialize(downsampler_modes);
  2045. ss_effects.downsample_shader_version = ss_effects.downsample_shader.version_create();
  2046. for (int i = 0; i < SS_EFFECTS_MAX; i++) {
  2047. ss_effects.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ss_effects.downsample_shader.version_get_shader(ss_effects.downsample_shader_version, i));
  2048. }
  2049. ss_effects.gather_constants_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSEffectsGatherConstants));
  2050. SSEffectsGatherConstants gather_constants;
  2051. const int sub_pass_count = 5;
  2052. for (int pass = 0; pass < 4; pass++) {
  2053. for (int subPass = 0; subPass < sub_pass_count; subPass++) {
  2054. int a = pass;
  2055. int b = subPass;
  2056. int spmap[5]{ 0, 1, 4, 3, 2 };
  2057. b = spmap[subPass];
  2058. float ca, sa;
  2059. float angle0 = (float(a) + float(b) / float(sub_pass_count)) * Math_PI * 0.5f;
  2060. ca = Math::cos(angle0);
  2061. sa = Math::sin(angle0);
  2062. float scale = 1.0f + (a - 1.5f + (b - (sub_pass_count - 1.0f) * 0.5f) / float(sub_pass_count)) * 0.07f;
  2063. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 0] = scale * ca;
  2064. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 1] = scale * -sa;
  2065. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 2] = -scale * sa;
  2066. gather_constants.rotation_matrices[pass * 20 + subPass * 4 + 3] = -scale * ca;
  2067. }
  2068. }
  2069. RD::get_singleton()->buffer_update(ss_effects.gather_constants_buffer, 0, sizeof(SSEffectsGatherConstants), &gather_constants);
  2070. }
  2071. {
  2072. // Initialize ssao
  2073. RD::SamplerState sampler;
  2074. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  2075. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  2076. sampler.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  2077. sampler.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  2078. sampler.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  2079. sampler.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  2080. sampler.max_lod = 4;
  2081. ss_effects.mirror_sampler = RD::get_singleton()->sampler_create(sampler);
  2082. uint32_t pipeline = 0;
  2083. {
  2084. Vector<String> ssao_modes;
  2085. ssao_modes.push_back("\n");
  2086. ssao_modes.push_back("\n#define SSAO_BASE\n");
  2087. ssao_modes.push_back("\n#define ADAPTIVE\n");
  2088. ssao.gather_shader.initialize(ssao_modes);
  2089. ssao.gather_shader_version = ssao.gather_shader.version_create();
  2090. for (int i = 0; i <= SSAO_GATHER_ADAPTIVE; i++) {
  2091. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i));
  2092. pipeline++;
  2093. }
  2094. }
  2095. {
  2096. Vector<String> ssao_modes;
  2097. ssao_modes.push_back("\n#define GENERATE_MAP\n");
  2098. ssao_modes.push_back("\n#define PROCESS_MAPA\n");
  2099. ssao_modes.push_back("\n#define PROCESS_MAPB\n");
  2100. ssao.importance_map_shader.initialize(ssao_modes);
  2101. ssao.importance_map_shader_version = ssao.importance_map_shader.version_create();
  2102. for (int i = SSAO_GENERATE_IMPORTANCE_MAP; i <= SSAO_PROCESS_IMPORTANCE_MAPB; i++) {
  2103. 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));
  2104. pipeline++;
  2105. }
  2106. ssao.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  2107. int zero[1] = { 0 };
  2108. RD::get_singleton()->buffer_update(ssao.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  2109. RD::get_singleton()->set_resource_name(ssao.importance_map_load_counter, "Importance Map Load Counter");
  2110. Vector<RD::Uniform> uniforms;
  2111. {
  2112. RD::Uniform u;
  2113. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2114. u.binding = 0;
  2115. u.ids.push_back(ssao.importance_map_load_counter);
  2116. uniforms.push_back(u);
  2117. }
  2118. 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);
  2119. RD::get_singleton()->set_resource_name(ssao.counter_uniform_set, "Load Counter Uniform Set");
  2120. }
  2121. {
  2122. Vector<String> ssao_modes;
  2123. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  2124. ssao_modes.push_back("\n#define MODE_SMART\n");
  2125. ssao_modes.push_back("\n#define MODE_WIDE\n");
  2126. ssao.blur_shader.initialize(ssao_modes);
  2127. ssao.blur_shader_version = ssao.blur_shader.version_create();
  2128. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_PASS_WIDE; i++) {
  2129. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  2130. pipeline++;
  2131. }
  2132. }
  2133. {
  2134. Vector<String> ssao_modes;
  2135. ssao_modes.push_back("\n#define MODE_NON_SMART\n");
  2136. ssao_modes.push_back("\n#define MODE_SMART\n");
  2137. ssao_modes.push_back("\n#define MODE_HALF\n");
  2138. ssao.interleave_shader.initialize(ssao_modes);
  2139. ssao.interleave_shader_version = ssao.interleave_shader.version_create();
  2140. for (int i = SSAO_INTERLEAVE; i <= SSAO_INTERLEAVE_HALF; i++) {
  2141. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.interleave_shader.version_get_shader(ssao.interleave_shader_version, i - SSAO_INTERLEAVE));
  2142. RD::get_singleton()->set_resource_name(ssao.pipelines[pipeline], "Interleave Pipeline " + itos(i));
  2143. pipeline++;
  2144. }
  2145. }
  2146. ERR_FAIL_COND(pipeline != SSAO_MAX);
  2147. }
  2148. }
  2149. if (!prefer_raster_effects) {
  2150. // Initialize roughness limiter
  2151. Vector<String> shader_modes;
  2152. shader_modes.push_back("");
  2153. roughness_limiter.shader.initialize(shader_modes);
  2154. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  2155. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  2156. }
  2157. {
  2158. //Initialize cubemap downsampler
  2159. Vector<String> cubemap_downsampler_modes;
  2160. cubemap_downsampler_modes.push_back("");
  2161. if (prefer_raster_effects) {
  2162. cubemap_downsampler.raster_shader.initialize(cubemap_downsampler_modes);
  2163. cubemap_downsampler.shader_version = cubemap_downsampler.raster_shader.version_create();
  2164. 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);
  2165. } else {
  2166. cubemap_downsampler.compute_shader.initialize(cubemap_downsampler_modes);
  2167. cubemap_downsampler.shader_version = cubemap_downsampler.compute_shader.version_create();
  2168. cubemap_downsampler.compute_pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  2169. cubemap_downsampler.raster_pipeline.clear();
  2170. }
  2171. }
  2172. {
  2173. // Initialize cubemap filter
  2174. filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
  2175. Vector<String> cubemap_filter_modes;
  2176. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  2177. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  2178. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  2179. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  2180. if (filter.use_high_quality) {
  2181. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  2182. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
  2183. } else {
  2184. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  2185. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
  2186. }
  2187. if (prefer_raster_effects) {
  2188. filter.raster_shader.initialize(cubemap_filter_modes);
  2189. // array variants are not supported in raster
  2190. filter.raster_shader.set_variant_enabled(FILTER_MODE_HIGH_QUALITY_ARRAY, false);
  2191. filter.raster_shader.set_variant_enabled(FILTER_MODE_LOW_QUALITY_ARRAY, false);
  2192. filter.shader_version = filter.raster_shader.version_create();
  2193. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  2194. if (filter.raster_shader.is_variant_enabled(i)) {
  2195. 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);
  2196. } else {
  2197. filter.raster_pipelines[i].clear();
  2198. }
  2199. }
  2200. Vector<RD::Uniform> uniforms;
  2201. {
  2202. RD::Uniform u;
  2203. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2204. u.binding = 0;
  2205. u.ids.push_back(filter.coefficient_buffer);
  2206. uniforms.push_back(u);
  2207. }
  2208. 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);
  2209. } else {
  2210. filter.compute_shader.initialize(cubemap_filter_modes);
  2211. filter.shader_version = filter.compute_shader.version_create();
  2212. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  2213. filter.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.compute_shader.version_get_shader(filter.shader_version, i));
  2214. filter.raster_pipelines[i].clear();
  2215. }
  2216. Vector<RD::Uniform> uniforms;
  2217. {
  2218. RD::Uniform u;
  2219. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2220. u.binding = 0;
  2221. u.ids.push_back(filter.coefficient_buffer);
  2222. uniforms.push_back(u);
  2223. }
  2224. 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);
  2225. }
  2226. }
  2227. if (!prefer_raster_effects) {
  2228. Vector<String> specular_modes;
  2229. specular_modes.push_back("\n#define MODE_MERGE\n");
  2230. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  2231. specular_modes.push_back("\n");
  2232. specular_modes.push_back("\n#define MODE_SSR\n");
  2233. specular_merge.shader.initialize(specular_modes);
  2234. specular_merge.shader_version = specular_merge.shader.version_create();
  2235. //use additive
  2236. RD::PipelineColorBlendState::Attachment ba;
  2237. ba.enable_blend = true;
  2238. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  2239. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  2240. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  2241. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  2242. ba.color_blend_op = RD::BLEND_OP_ADD;
  2243. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  2244. RD::PipelineColorBlendState blend_additive;
  2245. blend_additive.attachments.push_back(ba);
  2246. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  2247. RD::PipelineColorBlendState blend_state;
  2248. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  2249. blend_state = blend_additive;
  2250. } else {
  2251. blend_state = RD::PipelineColorBlendState::create_disabled();
  2252. }
  2253. 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);
  2254. }
  2255. }
  2256. if (!prefer_raster_effects) {
  2257. {
  2258. Vector<String> ssr_modes;
  2259. ssr_modes.push_back("\n");
  2260. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  2261. ssr.shader.initialize(ssr_modes);
  2262. ssr.shader_version = ssr.shader.version_create();
  2263. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  2264. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  2265. }
  2266. }
  2267. {
  2268. Vector<String> ssr_filter_modes;
  2269. ssr_filter_modes.push_back("\n");
  2270. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  2271. ssr_filter.shader.initialize(ssr_filter_modes);
  2272. ssr_filter.shader_version = ssr_filter.shader.version_create();
  2273. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  2274. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  2275. }
  2276. }
  2277. {
  2278. Vector<String> ssr_scale_modes;
  2279. ssr_scale_modes.push_back("\n");
  2280. ssr_scale.shader.initialize(ssr_scale_modes);
  2281. ssr_scale.shader_version = ssr_scale.shader.version_create();
  2282. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  2283. }
  2284. {
  2285. Vector<String> sss_modes;
  2286. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  2287. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  2288. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  2289. sss.shader.initialize(sss_modes);
  2290. sss.shader_version = sss.shader.version_create();
  2291. for (int i = 0; i < sss_modes.size(); i++) {
  2292. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  2293. }
  2294. }
  2295. {
  2296. Vector<String> ssil_modes;
  2297. ssil_modes.push_back("\n");
  2298. ssil_modes.push_back("\n#define SSIL_BASE\n");
  2299. ssil_modes.push_back("\n#define ADAPTIVE\n");
  2300. ssil.gather_shader.initialize(ssil_modes);
  2301. ssil.gather_shader_version = ssil.gather_shader.version_create();
  2302. for (int i = SSIL_GATHER; i <= SSIL_GATHER_ADAPTIVE; i++) {
  2303. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.gather_shader.version_get_shader(ssil.gather_shader_version, i));
  2304. }
  2305. ssil.projection_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SSILProjectionUniforms));
  2306. }
  2307. {
  2308. Vector<String> ssil_modes;
  2309. ssil_modes.push_back("\n#define GENERATE_MAP\n");
  2310. ssil_modes.push_back("\n#define PROCESS_MAPA\n");
  2311. ssil_modes.push_back("\n#define PROCESS_MAPB\n");
  2312. ssil.importance_map_shader.initialize(ssil_modes);
  2313. ssil.importance_map_shader_version = ssil.importance_map_shader.version_create();
  2314. for (int i = SSIL_GENERATE_IMPORTANCE_MAP; i <= SSIL_PROCESS_IMPORTANCE_MAPB; i++) {
  2315. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, i - SSIL_GENERATE_IMPORTANCE_MAP));
  2316. }
  2317. ssil.importance_map_load_counter = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t));
  2318. int zero[1] = { 0 };
  2319. RD::get_singleton()->buffer_update(ssil.importance_map_load_counter, 0, sizeof(uint32_t), &zero);
  2320. RD::get_singleton()->set_resource_name(ssil.importance_map_load_counter, "Importance Map Load Counter");
  2321. Vector<RD::Uniform> uniforms;
  2322. {
  2323. RD::Uniform u;
  2324. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2325. u.binding = 0;
  2326. u.ids.push_back(ssil.importance_map_load_counter);
  2327. uniforms.push_back(u);
  2328. }
  2329. ssil.counter_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssil.importance_map_shader.version_get_shader(ssil.importance_map_shader_version, 2), 2);
  2330. RD::get_singleton()->set_resource_name(ssil.counter_uniform_set, "Load Counter Uniform Set");
  2331. }
  2332. {
  2333. Vector<String> ssil_modes;
  2334. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  2335. ssil_modes.push_back("\n#define MODE_SMART\n");
  2336. ssil_modes.push_back("\n#define MODE_WIDE\n");
  2337. ssil.blur_shader.initialize(ssil_modes);
  2338. ssil.blur_shader_version = ssil.blur_shader.version_create();
  2339. for (int i = SSIL_BLUR_PASS; i <= SSIL_BLUR_PASS_WIDE; i++) {
  2340. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.blur_shader.version_get_shader(ssil.blur_shader_version, i - SSIL_BLUR_PASS));
  2341. }
  2342. }
  2343. {
  2344. Vector<String> ssil_modes;
  2345. ssil_modes.push_back("\n#define MODE_NON_SMART\n");
  2346. ssil_modes.push_back("\n#define MODE_SMART\n");
  2347. ssil_modes.push_back("\n#define MODE_HALF\n");
  2348. ssil.interleave_shader.initialize(ssil_modes);
  2349. ssil.interleave_shader_version = ssil.interleave_shader.version_create();
  2350. for (int i = SSIL_INTERLEAVE; i <= SSIL_INTERLEAVE_HALF; i++) {
  2351. ssil.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssil.interleave_shader.version_get_shader(ssil.interleave_shader_version, i - SSIL_INTERLEAVE));
  2352. }
  2353. }
  2354. {
  2355. Vector<String> resolve_modes;
  2356. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n");
  2357. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define VOXEL_GI_RESOLVE\n");
  2358. resolve_modes.push_back("\n#define MODE_RESOLVE_DEPTH\n");
  2359. resolve.shader.initialize(resolve_modes);
  2360. resolve.shader_version = resolve.shader.version_create();
  2361. for (int i = 0; i < RESOLVE_MODE_MAX; i++) {
  2362. resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i));
  2363. }
  2364. }
  2365. }
  2366. {
  2367. Vector<String> sort_modes;
  2368. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  2369. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  2370. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  2371. sort.shader.initialize(sort_modes);
  2372. sort.shader_version = sort.shader.version_create();
  2373. for (int i = 0; i < SORT_MODE_MAX; i++) {
  2374. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  2375. }
  2376. }
  2377. RD::SamplerState sampler;
  2378. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  2379. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  2380. sampler.max_lod = 0;
  2381. default_sampler = RD::get_singleton()->sampler_create(sampler);
  2382. RD::get_singleton()->set_resource_name(default_sampler, "Default Linear Sampler");
  2383. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  2384. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  2385. sampler.max_lod = 1e20;
  2386. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  2387. RD::get_singleton()->set_resource_name(default_mipmap_sampler, "Default MipMap Sampler");
  2388. { //create index array for copy shaders
  2389. Vector<uint8_t> pv;
  2390. pv.resize(6 * 4);
  2391. {
  2392. uint8_t *w = pv.ptrw();
  2393. int *p32 = (int *)w;
  2394. p32[0] = 0;
  2395. p32[1] = 1;
  2396. p32[2] = 2;
  2397. p32[3] = 0;
  2398. p32[4] = 2;
  2399. p32[5] = 3;
  2400. }
  2401. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  2402. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  2403. }
  2404. }
  2405. EffectsRD::~EffectsRD() {
  2406. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  2407. RD::get_singleton()->free(filter.image_uniform_set);
  2408. }
  2409. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  2410. RD::get_singleton()->free(filter.uniform_set);
  2411. }
  2412. RD::get_singleton()->free(default_sampler);
  2413. RD::get_singleton()->free(default_mipmap_sampler);
  2414. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  2415. RD::get_singleton()->free(filter.coefficient_buffer);
  2416. FSR_upscale.shader.version_free(FSR_upscale.shader_version);
  2417. if (prefer_raster_effects) {
  2418. blur_raster.shader.version_free(blur_raster.shader_version);
  2419. bokeh.raster_shader.version_free(blur_raster.shader_version);
  2420. luminance_reduce_raster.shader.version_free(luminance_reduce_raster.shader_version);
  2421. roughness.raster_shader.version_free(roughness.shader_version);
  2422. cubemap_downsampler.raster_shader.version_free(cubemap_downsampler.shader_version);
  2423. filter.raster_shader.version_free(filter.shader_version);
  2424. } else {
  2425. bokeh.compute_shader.version_free(bokeh.shader_version);
  2426. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  2427. roughness.compute_shader.version_free(roughness.shader_version);
  2428. cubemap_downsampler.compute_shader.version_free(cubemap_downsampler.shader_version);
  2429. filter.compute_shader.version_free(filter.shader_version);
  2430. }
  2431. if (!prefer_raster_effects) {
  2432. copy.shader.version_free(copy.shader_version);
  2433. resolve.shader.version_free(resolve.shader_version);
  2434. specular_merge.shader.version_free(specular_merge.shader_version);
  2435. ss_effects.downsample_shader.version_free(ss_effects.downsample_shader_version);
  2436. ssao.blur_shader.version_free(ssao.blur_shader_version);
  2437. ssao.gather_shader.version_free(ssao.gather_shader_version);
  2438. ssao.interleave_shader.version_free(ssao.interleave_shader_version);
  2439. ssao.importance_map_shader.version_free(ssao.importance_map_shader_version);
  2440. ssil.blur_shader.version_free(ssil.blur_shader_version);
  2441. ssil.gather_shader.version_free(ssil.gather_shader_version);
  2442. ssil.interleave_shader.version_free(ssil.interleave_shader_version);
  2443. ssil.importance_map_shader.version_free(ssil.importance_map_shader_version);
  2444. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  2445. ssr.shader.version_free(ssr.shader_version);
  2446. ssr_filter.shader.version_free(ssr_filter.shader_version);
  2447. ssr_scale.shader.version_free(ssr_scale.shader_version);
  2448. sss.shader.version_free(sss.shader_version);
  2449. RD::get_singleton()->free(ss_effects.mirror_sampler);
  2450. RD::get_singleton()->free(ss_effects.gather_constants_buffer);
  2451. RD::get_singleton()->free(ssao.importance_map_load_counter);
  2452. RD::get_singleton()->free(ssil.importance_map_load_counter);
  2453. RD::get_singleton()->free(ssil.projection_uniform_buffer);
  2454. }
  2455. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  2456. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  2457. sort.shader.version_free(sort.shader_version);
  2458. tonemap.shader.version_free(tonemap.shader_version);
  2459. }