effects_rd.cpp 153 KB

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