effects_rd.cpp 97 KB

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