effects_rd.cpp 109 KB

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