rasterizer_effects_rd.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. /*************************************************************************/
  2. /* rasterizer_effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "rasterizer_effects_rd.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "thirdparty/misc/cubemap_coeffs.h"
  34. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  35. p_array[0] = p_basis.elements[0][0];
  36. p_array[1] = p_basis.elements[1][0];
  37. p_array[2] = p_basis.elements[2][0];
  38. p_array[3] = 0;
  39. p_array[4] = p_basis.elements[0][1];
  40. p_array[5] = p_basis.elements[1][1];
  41. p_array[6] = p_basis.elements[2][1];
  42. p_array[7] = 0;
  43. p_array[8] = p_basis.elements[0][2];
  44. p_array[9] = p_basis.elements[1][2];
  45. p_array[10] = p_basis.elements[2][2];
  46. p_array[11] = 0;
  47. }
  48. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  49. for (int i = 0; i < 4; i++) {
  50. for (int j = 0; j < 4; j++) {
  51. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  52. }
  53. }
  54. }
  55. RID RasterizerEffectsRD::_get_uniform_set_from_image(RID p_image) {
  56. if (image_to_uniform_set_cache.has(p_image)) {
  57. RID uniform_set = image_to_uniform_set_cache[p_image];
  58. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  59. return uniform_set;
  60. }
  61. }
  62. Vector<RD::Uniform> uniforms;
  63. RD::Uniform u;
  64. u.type = RD::UNIFORM_TYPE_IMAGE;
  65. u.binding = 0;
  66. u.ids.push_back(p_image);
  67. uniforms.push_back(u);
  68. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  69. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  70. image_to_uniform_set_cache[p_image] = uniform_set;
  71. return uniform_set;
  72. }
  73. RID RasterizerEffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  74. if (texture_to_uniform_set_cache.has(p_texture)) {
  75. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  76. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  77. return uniform_set;
  78. }
  79. }
  80. Vector<RD::Uniform> uniforms;
  81. RD::Uniform u;
  82. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  83. u.binding = 0;
  84. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  85. u.ids.push_back(p_texture);
  86. uniforms.push_back(u);
  87. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  88. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, tonemap.shader.version_get_shader(tonemap.shader_version, 0), 0);
  89. texture_to_uniform_set_cache[p_texture] = uniform_set;
  90. return uniform_set;
  91. }
  92. RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  93. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  94. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  95. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  96. return uniform_set;
  97. }
  98. }
  99. Vector<RD::Uniform> uniforms;
  100. RD::Uniform u;
  101. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  102. u.binding = 0;
  103. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  104. u.ids.push_back(p_texture);
  105. uniforms.push_back(u);
  106. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  107. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  108. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  109. return uniform_set;
  110. }
  111. RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture_pair(RID p_texture1, RID p_texture2, bool p_use_mipmaps) {
  112. TexturePair tp;
  113. tp.texture1 = p_texture1;
  114. tp.texture2 = p_texture2;
  115. if (texture_pair_to_compute_uniform_set_cache.has(tp)) {
  116. RID uniform_set = texture_pair_to_compute_uniform_set_cache[tp];
  117. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  118. return uniform_set;
  119. }
  120. }
  121. Vector<RD::Uniform> uniforms;
  122. {
  123. RD::Uniform u;
  124. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  125. u.binding = 0;
  126. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  127. u.ids.push_back(p_texture1);
  128. uniforms.push_back(u);
  129. }
  130. {
  131. RD::Uniform u;
  132. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  133. u.binding = 1;
  134. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  135. u.ids.push_back(p_texture2);
  136. uniforms.push_back(u);
  137. }
  138. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  139. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 1);
  140. texture_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  141. return uniform_set;
  142. }
  143. RID RasterizerEffectsRD::_get_compute_uniform_set_from_image_pair(RID p_texture1, RID p_texture2) {
  144. TexturePair tp;
  145. tp.texture1 = p_texture1;
  146. tp.texture2 = p_texture2;
  147. if (image_pair_to_compute_uniform_set_cache.has(tp)) {
  148. RID uniform_set = image_pair_to_compute_uniform_set_cache[tp];
  149. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  150. return uniform_set;
  151. }
  152. }
  153. Vector<RD::Uniform> uniforms;
  154. {
  155. RD::Uniform u;
  156. u.type = RD::UNIFORM_TYPE_IMAGE;
  157. u.binding = 0;
  158. u.ids.push_back(p_texture1);
  159. uniforms.push_back(u);
  160. }
  161. {
  162. RD::Uniform u;
  163. u.type = RD::UNIFORM_TYPE_IMAGE;
  164. u.binding = 1;
  165. u.ids.push_back(p_texture2);
  166. uniforms.push_back(u);
  167. }
  168. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  169. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0), 3);
  170. image_pair_to_compute_uniform_set_cache[tp] = uniform_set;
  171. return uniform_set;
  172. }
  173. void RasterizerEffectsRD::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) {
  174. zeromem(&copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  175. copy_to_fb.push_constant.use_section = true;
  176. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  177. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  178. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  179. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  180. if (p_flip_y) {
  181. copy_to_fb.push_constant.flip_y = true;
  182. }
  183. RD::DrawListID draw_list = p_draw_list;
  184. 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)));
  185. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  186. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  187. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  188. RD::get_singleton()->draw_list_draw(draw_list, true);
  189. }
  190. void RasterizerEffectsRD::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) {
  191. zeromem(&copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  192. if (p_flip_y) {
  193. copy_to_fb.push_constant.flip_y = true;
  194. }
  195. if (p_force_luminance) {
  196. copy_to_fb.push_constant.force_luminance = true;
  197. }
  198. if (p_alpha_to_zero) {
  199. copy_to_fb.push_constant.alpha_to_zero = true;
  200. }
  201. if (p_srgb) {
  202. copy_to_fb.push_constant.srgb = true;
  203. }
  204. 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);
  205. 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)));
  206. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  207. if (p_secondary.is_valid()) {
  208. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_secondary), 1);
  209. }
  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. RD::get_singleton()->draw_list_end();
  214. }
  215. void RasterizerEffectsRD::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) {
  216. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  217. if (p_flip_y) {
  218. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  219. }
  220. if (p_force_luminance) {
  221. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  222. }
  223. if (p_all_source) {
  224. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  225. }
  226. copy.push_constant.section[0] = 0;
  227. copy.push_constant.section[1] = 0;
  228. copy.push_constant.section[2] = p_rect.size.width;
  229. copy.push_constant.section[3] = p_rect.size.height;
  230. copy.push_constant.target[0] = p_rect.position.x;
  231. copy.push_constant.target[1] = p_rect.position.y;
  232. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  233. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  234. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  235. 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]);
  236. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  237. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  238. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  239. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  240. RD::get_singleton()->compute_list_end();
  241. }
  242. void RasterizerEffectsRD::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {
  243. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  244. copy.push_constant.section[0] = 0;
  245. copy.push_constant.section[1] = 0;
  246. copy.push_constant.section[2] = p_panorama_size.width;
  247. copy.push_constant.section[3] = p_panorama_size.height;
  248. copy.push_constant.target[0] = 0;
  249. copy.push_constant.target[1] = 0;
  250. copy.push_constant.camera_z_far = p_lod;
  251. int32_t x_groups = (p_panorama_size.width - 1) / 8 + 1;
  252. int32_t y_groups = (p_panorama_size.height - 1) / 8 + 1;
  253. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  254. 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]);
  255. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cube), 0);
  256. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_panorama), 3);
  257. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  258. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  259. RD::get_singleton()->compute_list_end();
  260. }
  261. void RasterizerEffectsRD::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) {
  262. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  263. if (p_flip_y) {
  264. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  265. }
  266. copy.push_constant.section[0] = 0;
  267. copy.push_constant.section[1] = 0;
  268. copy.push_constant.section[2] = p_rect.size.width;
  269. copy.push_constant.section[3] = p_rect.size.height;
  270. copy.push_constant.target[0] = p_rect.position.x;
  271. copy.push_constant.target[1] = p_rect.position.y;
  272. copy.push_constant.camera_z_far = p_z_far;
  273. copy.push_constant.camera_z_near = p_z_near;
  274. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  275. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  276. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  277. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_LINEARIZE_DEPTH]);
  278. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  279. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  280. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  281. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  282. RD::get_singleton()->compute_list_end();
  283. }
  284. void RasterizerEffectsRD::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  285. zeromem(&copy.push_constant, 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. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  296. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  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_SIMPLY_COPY_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(compute_list, x_groups, y_groups, 1);
  303. RD::get_singleton()->compute_list_end();
  304. }
  305. void RasterizerEffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_texture, RID p_back_texture, const Rect2i &p_region, bool p_8bit_dst) {
  306. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  307. uint32_t base_flags = 0;
  308. copy.push_constant.section[0] = p_region.position.x;
  309. copy.push_constant.section[1] = p_region.position.y;
  310. copy.push_constant.section[2] = p_region.size.width;
  311. copy.push_constant.section[3] = p_region.size.height;
  312. int32_t x_groups = (p_region.size.width - 1) / 8 + 1;
  313. int32_t y_groups = (p_region.size.height - 1) / 8 + 1;
  314. //HORIZONTAL
  315. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  316. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY]);
  317. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  318. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 0);
  319. copy.push_constant.flags = base_flags | COPY_FLAG_HORIZONTAL;
  320. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  321. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  322. RD::get_singleton()->compute_list_add_barrier(compute_list);
  323. //VERTICAL
  324. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_back_texture), 0);
  325. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_texture), 0);
  326. copy.push_constant.flags = base_flags;
  327. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  328. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  329. RD::get_singleton()->compute_list_end();
  330. }
  331. void RasterizerEffectsRD::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) {
  332. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  333. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  334. uint32_t base_flags = 0;
  335. int32_t x_groups = (p_size.width + 7) / 8;
  336. int32_t y_groups = (p_size.height + 7) / 8;
  337. copy.push_constant.section[2] = p_size.x;
  338. copy.push_constant.section[3] = p_size.y;
  339. copy.push_constant.glow_strength = p_strength;
  340. copy.push_constant.glow_bloom = p_bloom;
  341. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_treshold;
  342. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  343. copy.push_constant.glow_exposure = p_exposure;
  344. copy.push_constant.glow_white = 0; //actually unused
  345. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  346. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  347. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  348. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  349. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  350. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_back_texture), 3);
  351. if (p_auto_exposure.is_valid() && p_first_pass) {
  352. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_auto_exposure), 1);
  353. }
  354. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  355. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  356. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  357. RD::get_singleton()->compute_list_end();
  358. }
  359. void RasterizerEffectsRD::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) {
  360. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  361. int32_t x_groups = (p_screen_size.width - 1) / 8 + 1;
  362. int32_t y_groups = (p_screen_size.height - 1) / 8 + 1;
  363. { //scale color and depth to half
  364. ssr_scale.push_constant.camera_z_far = p_camera.get_z_far();
  365. ssr_scale.push_constant.camera_z_near = p_camera.get_z_near();
  366. ssr_scale.push_constant.orthogonal = p_camera.is_orthogonal();
  367. ssr_scale.push_constant.filter = false; //enabling causes arctifacts
  368. ssr_scale.push_constant.screen_size[0] = p_screen_size.x;
  369. ssr_scale.push_constant.screen_size[1] = p_screen_size.y;
  370. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_scale.pipeline);
  371. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  372. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_depth, p_normal_roughness), 1);
  373. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output_blur), 2);
  374. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_scale_depth, p_scale_normal), 3);
  375. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_scale.push_constant, sizeof(ScreenSpaceReflectionScalePushConstant));
  376. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  377. RD::get_singleton()->compute_list_add_barrier(compute_list);
  378. }
  379. {
  380. ssr.push_constant.camera_z_far = p_camera.get_z_far();
  381. ssr.push_constant.camera_z_near = p_camera.get_z_near();
  382. ssr.push_constant.orthogonal = p_camera.is_orthogonal();
  383. ssr.push_constant.screen_size[0] = p_screen_size.x;
  384. ssr.push_constant.screen_size[1] = p_screen_size.y;
  385. ssr.push_constant.curve_fade_in = p_fade_in;
  386. ssr.push_constant.distance_fade = p_fade_out;
  387. ssr.push_constant.num_steps = p_max_steps;
  388. ssr.push_constant.depth_tolerance = p_tolerance;
  389. ssr.push_constant.use_half_res = true;
  390. ssr.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  391. ssr.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  392. ssr.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  393. ssr.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  394. ssr.push_constant.metallic_mask[0] = CLAMP(p_metallic_mask.r * 255.0, 0, 255);
  395. ssr.push_constant.metallic_mask[1] = CLAMP(p_metallic_mask.g * 255.0, 0, 255);
  396. ssr.push_constant.metallic_mask[2] = CLAMP(p_metallic_mask.b * 255.0, 0, 255);
  397. ssr.push_constant.metallic_mask[3] = CLAMP(p_metallic_mask.a * 255.0, 0, 255);
  398. store_camera(p_camera, ssr.push_constant.projection);
  399. 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]);
  400. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr.push_constant, sizeof(ScreenSpaceReflectionPushConstant));
  401. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_scale_depth), 0);
  402. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  403. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 1);
  404. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture_pair(p_metallic, p_normal_roughness), 3);
  405. } else {
  406. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 1);
  407. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_metallic), 3);
  408. }
  409. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 2);
  410. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  411. }
  412. if (p_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED) {
  413. //blur
  414. RD::get_singleton()->compute_list_add_barrier(compute_list);
  415. ssr_filter.push_constant.orthogonal = p_camera.is_orthogonal();
  416. ssr_filter.push_constant.edge_tolerance = Math::sin(Math::deg2rad(15.0));
  417. ssr_filter.push_constant.proj_info[0] = -2.0f / (p_screen_size.width * p_camera.matrix[0][0]);
  418. ssr_filter.push_constant.proj_info[1] = -2.0f / (p_screen_size.height * p_camera.matrix[1][1]);
  419. ssr_filter.push_constant.proj_info[2] = (1.0f - p_camera.matrix[0][2]) / p_camera.matrix[0][0];
  420. ssr_filter.push_constant.proj_info[3] = (1.0f + p_camera.matrix[1][2]) / p_camera.matrix[1][1];
  421. ssr_filter.push_constant.vertical = 0;
  422. if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_LOW) {
  423. ssr_filter.push_constant.steps = p_max_steps / 3;
  424. ssr_filter.push_constant.increment = 3;
  425. } else if (p_roughness_quality == RS::ENV_SSR_ROUGNESS_QUALITY_MEDIUM) {
  426. ssr_filter.push_constant.steps = p_max_steps / 2;
  427. ssr_filter.push_constant.increment = 2;
  428. } else {
  429. ssr_filter.push_constant.steps = p_max_steps;
  430. ssr_filter.push_constant.increment = 1;
  431. }
  432. ssr_filter.push_constant.screen_size[0] = p_screen_size.width;
  433. ssr_filter.push_constant.screen_size[1] = p_screen_size.height;
  434. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL]);
  435. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output, p_blur_radius), 0);
  436. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  437. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 2);
  438. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  439. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  440. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  441. RD::get_singleton()->compute_list_add_barrier(compute_list);
  442. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssr_filter.pipelines[SCREEN_SPACE_REFLECTION_FILTER_VERTICAL]);
  443. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_output_blur, p_blur_radius2), 0);
  444. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_normal), 1);
  445. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_output), 2);
  446. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_scale_depth), 3);
  447. ssr_filter.push_constant.vertical = 1;
  448. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssr_filter.push_constant, sizeof(ScreenSpaceReflectionFilterPushConstant));
  449. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  450. }
  451. RD::get_singleton()->compute_list_end();
  452. }
  453. void RasterizerEffectsRD::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) {
  454. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  455. int32_t x_groups = (p_screen_size.width - 1) / 8 + 1;
  456. int32_t y_groups = (p_screen_size.height - 1) / 8 + 1;
  457. Plane p = p_camera.xform4(Plane(1, 0, -1, 1));
  458. p.normal /= p.d;
  459. float unit_size = p.normal.x;
  460. { //scale color and depth to half
  461. sss.push_constant.camera_z_far = p_camera.get_z_far();
  462. sss.push_constant.camera_z_near = p_camera.get_z_near();
  463. sss.push_constant.orthogonal = p_camera.is_orthogonal();
  464. sss.push_constant.unit_size = unit_size;
  465. sss.push_constant.screen_size[0] = p_screen_size.x;
  466. sss.push_constant.screen_size[1] = p_screen_size.y;
  467. sss.push_constant.vertical = false;
  468. sss.push_constant.scale = p_scale;
  469. sss.push_constant.depth_scale = p_depth_scale;
  470. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sss.pipelines[p_quality - 1]);
  471. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse), 0);
  472. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse2), 1);
  473. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  474. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  475. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  476. RD::get_singleton()->compute_list_add_barrier(compute_list);
  477. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_diffuse2), 0);
  478. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_diffuse), 1);
  479. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth), 2);
  480. sss.push_constant.vertical = true;
  481. RD::get_singleton()->compute_list_set_push_constant(compute_list, &sss.push_constant, sizeof(SubSurfaceScatteringPushConstant));
  482. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  483. RD::get_singleton()->compute_list_end();
  484. }
  485. }
  486. void RasterizerEffectsRD::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection) {
  487. 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>());
  488. if (p_reflection.is_valid()) {
  489. if (p_base.is_valid()) {
  490. 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)));
  491. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  492. } else {
  493. 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)));
  494. }
  495. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  496. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_reflection), 1);
  497. } else {
  498. if (p_base.is_valid()) {
  499. 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)));
  500. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_base), 2);
  501. } else {
  502. 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)));
  503. }
  504. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_specular), 0);
  505. }
  506. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  507. RD::get_singleton()->draw_list_draw(draw_list, true);
  508. RD::get_singleton()->draw_list_end();
  509. }
  510. void RasterizerEffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  511. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  512. copy.push_constant.section[0] = 0;
  513. copy.push_constant.section[1] = 0;
  514. copy.push_constant.section[2] = p_size.width;
  515. copy.push_constant.section[3] = p_size.height;
  516. int32_t x_groups = (p_size.width - 1) / 8 + 1;
  517. int32_t y_groups = (p_size.height - 1) / 8 + 1;
  518. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  519. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[COPY_MODE_MIPMAP]);
  520. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  521. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 3);
  522. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  523. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  524. RD::get_singleton()->compute_list_end();
  525. }
  526. void RasterizerEffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, float p_z_near, float p_z_far, float p_bias, bool p_dp_flip) {
  527. CopyToDPPushConstant push_constant;
  528. push_constant.screen_size[0] = p_rect.size.x;
  529. push_constant.screen_size[1] = p_rect.size.y;
  530. push_constant.dest_offset[0] = p_rect.position.x;
  531. push_constant.dest_offset[1] = p_rect.position.y;
  532. push_constant.bias = p_bias;
  533. push_constant.z_far = p_z_far;
  534. push_constant.z_near = p_z_near;
  535. push_constant.z_flip = p_dp_flip;
  536. int32_t x_groups = (p_rect.size.width - 1) / 8 + 1;
  537. int32_t y_groups = (p_rect.size.height - 1) / 8 + 1;
  538. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  539. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cube_to_dp.pipeline);
  540. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  541. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_texture), 1);
  542. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(CopyToDPPushConstant));
  543. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  544. RD::get_singleton()->compute_list_end();
  545. }
  546. void RasterizerEffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  547. zeromem(&tonemap.push_constant, sizeof(TonemapPushConstant));
  548. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  549. tonemap.push_constant.bcs[0] = p_settings.brightness;
  550. tonemap.push_constant.bcs[1] = p_settings.contrast;
  551. tonemap.push_constant.bcs[2] = p_settings.saturation;
  552. tonemap.push_constant.use_glow = p_settings.use_glow;
  553. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  554. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  555. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  556. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  557. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  558. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  559. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  560. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  561. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  562. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  563. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  564. TonemapMode mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  565. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  566. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  567. tonemap.push_constant.exposure = p_settings.exposure;
  568. tonemap.push_constant.white = p_settings.white;
  569. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  570. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  571. tonemap.push_constant.use_fxaa = p_settings.use_fxaa;
  572. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  573. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  574. 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);
  575. 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)));
  576. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  577. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  578. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  579. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  580. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  581. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  582. RD::get_singleton()->draw_list_draw(draw_list, true);
  583. RD::get_singleton()->draw_list_end();
  584. }
  585. void RasterizerEffectsRD::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) {
  586. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  587. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  588. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  589. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  590. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  591. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  592. for (int i = 0; i < p_reduce.size(); i++) {
  593. if (i == 0) {
  594. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  595. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  596. } else {
  597. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  598. if (i == p_reduce.size() - 1 && !p_set) {
  599. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  600. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  601. } else {
  602. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  603. }
  604. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  605. }
  606. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  607. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  608. int32_t x_groups = (luminance_reduce.push_constant.source_size[0] - 1) / 8 + 1;
  609. int32_t y_groups = (luminance_reduce.push_constant.source_size[1] - 1) / 8 + 1;
  610. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  611. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  612. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  613. }
  614. RD::get_singleton()->compute_list_end();
  615. }
  616. void RasterizerEffectsRD::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) {
  617. bokeh.push_constant.blur_far_active = p_dof_far;
  618. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  619. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  620. bokeh.push_constant.blur_near_active = p_dof_near;
  621. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  622. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  623. bokeh.push_constant.use_jitter = p_use_jitter;
  624. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  625. bokeh.push_constant.z_near = p_cam_znear;
  626. bokeh.push_constant.z_far = p_cam_zfar;
  627. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  628. bokeh.push_constant.blur_size = p_bokeh_size;
  629. bokeh.push_constant.second_pass = false;
  630. bokeh.push_constant.half_size = false;
  631. bokeh.push_constant.blur_scale = 0.5;
  632. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  633. /* FIRST PASS */
  634. // The alpha channel of the source color texture is filled with the expected circle size
  635. // If used for DOF far, the size is positive, if used for near, its negative.
  636. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BLUR_SIZE]);
  637. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  638. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  639. int32_t x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  640. int32_t y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  641. bokeh.push_constant.size[0] = p_base_texture_size.x;
  642. bokeh.push_constant.size[1] = p_base_texture_size.y;
  643. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  644. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  645. RD::get_singleton()->compute_list_add_barrier(compute_list);
  646. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  647. //second pass
  648. 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]);
  649. static const int quality_samples[4] = { 6, 12, 12, 24 };
  650. bokeh.push_constant.steps = quality_samples[p_quality];
  651. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  652. //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)
  653. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  654. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  655. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  656. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  657. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  658. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  659. bokeh.push_constant.half_size = true;
  660. bokeh.push_constant.blur_size *= 0.5;
  661. } else {
  662. //medium and high quality use full size
  663. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 0);
  664. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  665. }
  666. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  667. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  668. RD::get_singleton()->compute_list_add_barrier(compute_list);
  669. //third pass
  670. bokeh.push_constant.second_pass = true;
  671. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  672. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture2), 0);
  673. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  674. } else {
  675. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  676. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 1);
  677. }
  678. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  679. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  680. RD::get_singleton()->compute_list_add_barrier(compute_list);
  681. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  682. //forth pass, upscale for low quality
  683. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  684. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  685. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture2), 1);
  686. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  687. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  688. bokeh.push_constant.size[0] = p_base_texture_size.x;
  689. bokeh.push_constant.size[1] = p_base_texture_size.y;
  690. bokeh.push_constant.half_size = false;
  691. bokeh.push_constant.second_pass = false;
  692. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  693. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  694. }
  695. } else {
  696. //circle
  697. //second pass
  698. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  699. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  700. bokeh.push_constant.steps = 0;
  701. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  702. //circle always runs in half size, otherwise too expensive
  703. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  704. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  705. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  706. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  707. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  708. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  709. bokeh.push_constant.half_size = true;
  710. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  711. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  712. RD::get_singleton()->compute_list_add_barrier(compute_list);
  713. //circle is just one pass, then upscale
  714. // upscale
  715. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  716. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  717. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  718. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  719. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  720. bokeh.push_constant.size[0] = p_base_texture_size.x;
  721. bokeh.push_constant.size[1] = p_base_texture_size.y;
  722. bokeh.push_constant.half_size = false;
  723. bokeh.push_constant.second_pass = false;
  724. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  725. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  726. }
  727. RD::get_singleton()->compute_list_end();
  728. }
  729. void RasterizerEffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, const Size2i &p_depth_buffer_size, RID p_depth_mipmaps_texture, const Vector<RID> &depth_mipmaps, RID p_ao1, bool p_half_size, RID p_ao2, RID p_upscale_buffer, float p_intensity, float p_radius, float p_bias, const CameraMatrix &p_projection, RS::EnvironmentSSAOQuality p_quality, RS::EnvironmentSSAOBlur p_blur, float p_edge_sharpness) {
  730. //minify first
  731. ssao.minify_push_constant.orthogonal = p_projection.is_orthogonal();
  732. ssao.minify_push_constant.z_near = p_projection.get_z_near();
  733. ssao.minify_push_constant.z_far = p_projection.get_z_far();
  734. ssao.minify_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  735. ssao.minify_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  736. ssao.minify_push_constant.source_size[0] = p_depth_buffer_size.x;
  737. ssao.minify_push_constant.source_size[1] = p_depth_buffer_size.y;
  738. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  739. /* FIRST PASS */
  740. // Minify the depth buffer.
  741. for (int i = 0; i < depth_mipmaps.size(); i++) {
  742. if (i == 0) {
  743. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_FIRST]);
  744. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  745. } else {
  746. if (i == 1) {
  747. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_MIPMAP]);
  748. }
  749. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i - 1]), 0);
  750. }
  751. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i]), 1);
  752. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.minify_push_constant, sizeof(SSAOMinifyPushConstant));
  753. // shrink after set
  754. ssao.minify_push_constant.source_size[0] = MAX(1, ssao.minify_push_constant.source_size[0] >> 1);
  755. ssao.minify_push_constant.source_size[1] = MAX(1, ssao.minify_push_constant.source_size[1] >> 1);
  756. int x_groups = (ssao.minify_push_constant.source_size[0] - 1) / 8 + 1;
  757. int y_groups = (ssao.minify_push_constant.source_size[1] - 1) / 8 + 1;
  758. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  759. RD::get_singleton()->compute_list_add_barrier(compute_list);
  760. }
  761. /* SECOND PASS */
  762. // Gather samples
  763. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[(SSAO_GATHER_LOW + p_quality) + (p_half_size ? 4 : 0)]);
  764. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 0);
  765. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 1);
  766. if (!p_half_size) {
  767. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 2);
  768. }
  769. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_normal_buffer), 3);
  770. ssao.gather_push_constant.screen_size[0] = p_depth_buffer_size.x;
  771. ssao.gather_push_constant.screen_size[1] = p_depth_buffer_size.y;
  772. if (p_half_size) {
  773. ssao.gather_push_constant.screen_size[0] >>= 1;
  774. ssao.gather_push_constant.screen_size[1] >>= 1;
  775. }
  776. ssao.gather_push_constant.z_far = p_projection.get_z_far();
  777. ssao.gather_push_constant.z_near = p_projection.get_z_near();
  778. ssao.gather_push_constant.orthogonal = p_projection.is_orthogonal();
  779. ssao.gather_push_constant.proj_info[0] = -2.0f / (ssao.gather_push_constant.screen_size[0] * p_projection.matrix[0][0]);
  780. ssao.gather_push_constant.proj_info[1] = -2.0f / (ssao.gather_push_constant.screen_size[1] * p_projection.matrix[1][1]);
  781. ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  782. ssao.gather_push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  783. //ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  784. //ssao.gather_push_constant.proj_info[3] = -(1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  785. ssao.gather_push_constant.radius = p_radius;
  786. ssao.gather_push_constant.proj_scale = float(p_projection.get_pixels_per_meter(ssao.gather_push_constant.screen_size[0]));
  787. ssao.gather_push_constant.bias = p_bias;
  788. ssao.gather_push_constant.intensity_div_r6 = p_intensity / pow(p_radius, 6.0f);
  789. ssao.gather_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  790. ssao.gather_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  791. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  792. int x_groups = (ssao.gather_push_constant.screen_size[0] - 1) / 8 + 1;
  793. int y_groups = (ssao.gather_push_constant.screen_size[1] - 1) / 8 + 1;
  794. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  795. RD::get_singleton()->compute_list_add_barrier(compute_list);
  796. /* THIRD PASS */
  797. // Blur horizontal
  798. ssao.blur_push_constant.edge_sharpness = p_edge_sharpness;
  799. ssao.blur_push_constant.filter_scale = p_blur;
  800. ssao.blur_push_constant.screen_size[0] = ssao.gather_push_constant.screen_size[0];
  801. ssao.blur_push_constant.screen_size[1] = ssao.gather_push_constant.screen_size[1];
  802. ssao.blur_push_constant.z_far = p_projection.get_z_far();
  803. ssao.blur_push_constant.z_near = p_projection.get_z_near();
  804. ssao.blur_push_constant.orthogonal = p_projection.is_orthogonal();
  805. ssao.blur_push_constant.axis[0] = 1;
  806. ssao.blur_push_constant.axis[1] = 0;
  807. if (p_blur != RS::ENV_SSAO_BLUR_DISABLED) {
  808. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[p_half_size ? SSAO_BLUR_PASS_HALF : SSAO_BLUR_PASS]);
  809. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  810. if (p_half_size) {
  811. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 1);
  812. } else {
  813. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  814. }
  815. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao2), 3);
  816. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  817. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  818. RD::get_singleton()->compute_list_add_barrier(compute_list);
  819. /* THIRD PASS */
  820. // Blur vertical
  821. ssao.blur_push_constant.axis[0] = 0;
  822. ssao.blur_push_constant.axis[1] = 1;
  823. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao2), 0);
  824. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 3);
  825. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  826. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  827. }
  828. if (p_half_size) { //must upscale
  829. /* FOURTH PASS */
  830. // upscale if half size
  831. //back to full size
  832. ssao.blur_push_constant.screen_size[0] = p_depth_buffer_size.x;
  833. ssao.blur_push_constant.screen_size[1] = p_depth_buffer_size.y;
  834. RD::get_singleton()->compute_list_add_barrier(compute_list);
  835. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_BLUR_UPSCALE]);
  836. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  837. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 3);
  838. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  839. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 2);
  840. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant)); //not used but set anyway
  841. x_groups = (p_depth_buffer_size.x - 1) / 8 + 1;
  842. y_groups = (p_depth_buffer_size.y - 1) / 8 + 1;
  843. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  844. }
  845. RD::get_singleton()->compute_list_end();
  846. }
  847. void RasterizerEffectsRD::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  848. roughness_limiter.push_constant.screen_size[0] = p_size.x;
  849. roughness_limiter.push_constant.screen_size[1] = p_size.y;
  850. roughness_limiter.push_constant.curve = p_curve;
  851. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  852. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness_limiter.pipeline);
  853. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_normal), 0);
  854. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_roughness), 1);
  855. int x_groups = (p_size.x - 1) / 8 + 1;
  856. int y_groups = (p_size.y - 1) / 8 + 1;
  857. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness_limiter.push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  858. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  859. RD::get_singleton()->compute_list_end();
  860. }
  861. void RasterizerEffectsRD::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) {
  862. zeromem(&roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  863. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  864. roughness.push_constant.roughness = p_roughness;
  865. roughness.push_constant.sample_count = p_sample_count;
  866. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  867. roughness.push_constant.face_size = p_size;
  868. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  869. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.pipeline);
  870. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_rd_texture), 0);
  871. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_framebuffer), 1);
  872. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  873. int x_groups = (p_size - 1) / 8 + 1;
  874. int y_groups = (p_size - 1) / 8 + 1;
  875. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  876. RD::get_singleton()->compute_list_end();
  877. }
  878. void RasterizerEffectsRD::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  879. cubemap_downsampler.push_constant.face_size = p_size.x;
  880. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  881. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.pipeline);
  882. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap), 0);
  883. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_cubemap), 1);
  884. int x_groups = (p_size.x - 1) / 8 + 1;
  885. int y_groups = (p_size.y - 1) / 8 + 1;
  886. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  887. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  888. RD::get_singleton()->compute_list_end();
  889. }
  890. void RasterizerEffectsRD::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  891. Vector<RD::Uniform> uniforms;
  892. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  893. RD::Uniform u;
  894. u.type = RD::UNIFORM_TYPE_IMAGE;
  895. u.binding = i;
  896. u.ids.push_back(p_dest_cubemap[i]);
  897. uniforms.push_back(u);
  898. }
  899. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  900. RD::get_singleton()->free(filter.image_uniform_set);
  901. }
  902. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.shader.version_get_shader(filter.shader_version, 0), 2);
  903. int pipeline = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  904. pipeline = filter.use_high_quality ? pipeline : pipeline + 1;
  905. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  906. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.pipelines[pipeline]);
  907. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_cubemap, true), 0);
  908. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  909. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  910. 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
  911. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  912. RD::get_singleton()->compute_list_end();
  913. }
  914. void RasterizerEffectsRD::render_sky(RD::DrawListID p_list, float p_time, RID p_fb, RID p_samplers, RID p_fog, RenderPipelineVertexFormatCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, const CameraMatrix &p_camera, const Basis &p_orientation, float p_multiplier, const Vector3 &p_position) {
  915. SkyPushConstant sky_push_constant;
  916. zeromem(&sky_push_constant, sizeof(SkyPushConstant));
  917. sky_push_constant.proj[0] = p_camera.matrix[2][0];
  918. sky_push_constant.proj[1] = p_camera.matrix[0][0];
  919. sky_push_constant.proj[2] = p_camera.matrix[2][1];
  920. sky_push_constant.proj[3] = p_camera.matrix[1][1];
  921. sky_push_constant.position[0] = p_position.x;
  922. sky_push_constant.position[1] = p_position.y;
  923. sky_push_constant.position[2] = p_position.z;
  924. sky_push_constant.multiplier = p_multiplier;
  925. sky_push_constant.time = p_time;
  926. store_transform_3x3(p_orientation, sky_push_constant.orientation);
  927. RenderingDevice::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_fb);
  928. RD::DrawListID draw_list = p_list;
  929. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, p_pipeline->get_render_pipeline(RD::INVALID_ID, fb_format));
  930. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_samplers, 0);
  931. if (p_uniform_set.is_valid()) { //material may not have uniform set
  932. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_uniform_set, 1);
  933. }
  934. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_texture_set, 2);
  935. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_fog, 3);
  936. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  937. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky_push_constant, sizeof(SkyPushConstant));
  938. RD::get_singleton()->draw_list_draw(draw_list, true);
  939. }
  940. void RasterizerEffectsRD::resolve_gi(RID p_source_depth, RID p_source_normal_roughness, RID p_source_giprobe, RID p_dest_depth, RID p_dest_normal_roughness, RID p_dest_giprobe, Vector2i p_screen_size, int p_samples) {
  941. ResolvePushConstant push_constant;
  942. push_constant.screen_size[0] = p_screen_size.x;
  943. push_constant.screen_size[1] = p_screen_size.y;
  944. push_constant.samples = p_samples;
  945. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  946. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[p_source_giprobe.is_valid() ? RESOLVE_MODE_GI_GIPROBE : RESOLVE_MODE_GI]);
  947. 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);
  948. 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);
  949. if (p_source_giprobe.is_valid()) {
  950. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_giprobe), 2);
  951. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_dest_giprobe), 3);
  952. }
  953. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
  954. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1, 8, 8, 1);
  955. RD::get_singleton()->compute_list_end();
  956. }
  957. void RasterizerEffectsRD::reduce_shadow(RID p_source_shadow, RID p_dest_shadow, const Size2i &p_source_size, const Rect2i &p_source_rect, int p_shrink_limit, RD::ComputeListID compute_list) {
  958. uint32_t push_constant[8] = { (uint32_t)p_source_size.x, (uint32_t)p_source_size.y, (uint32_t)p_source_rect.position.x, (uint32_t)p_source_rect.position.y, (uint32_t)p_shrink_limit, 0, 0, 0 };
  959. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shadow_reduce.pipelines[SHADOW_REDUCE_REDUCE]);
  960. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_source_shadow, p_dest_shadow), 0);
  961. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(uint32_t) * 8);
  962. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_source_rect.size.width, p_source_rect.size.height, 1, 8, 8, 1);
  963. }
  964. void RasterizerEffectsRD::filter_shadow(RID p_shadow, RID p_backing_shadow, const Size2i &p_source_size, const Rect2i &p_source_rect, RenderingServer::EnvVolumetricFogShadowFilter p_filter, RD::ComputeListID compute_list, bool p_vertical, bool p_horizontal) {
  965. uint32_t push_constant[8] = { (uint32_t)p_source_size.x, (uint32_t)p_source_size.y, (uint32_t)p_source_rect.position.x, (uint32_t)p_source_rect.position.y, 0, 0, 0, 0 };
  966. switch (p_filter) {
  967. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED:
  968. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_LOW: {
  969. push_constant[5] = 0;
  970. } break;
  971. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_MEDIUM: {
  972. push_constant[5] = 9;
  973. } break;
  974. case RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_HIGH: {
  975. push_constant[5] = 18;
  976. } break;
  977. }
  978. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shadow_reduce.pipelines[SHADOW_REDUCE_FILTER]);
  979. if (p_vertical) {
  980. push_constant[6] = 1;
  981. push_constant[7] = 0;
  982. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_shadow, p_backing_shadow), 0);
  983. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(uint32_t) * 8);
  984. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_source_rect.size.width, p_source_rect.size.height, 1, 8, 8, 1);
  985. }
  986. if (p_vertical && p_horizontal) {
  987. RD::get_singleton()->compute_list_add_barrier(compute_list);
  988. }
  989. if (p_horizontal) {
  990. push_constant[6] = 0;
  991. push_constant[7] = 1;
  992. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_image_pair(p_backing_shadow, p_shadow), 0);
  993. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(uint32_t) * 8);
  994. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_source_rect.size.width, p_source_rect.size.height, 1, 8, 8, 1);
  995. }
  996. }
  997. void RasterizerEffectsRD::sort_buffer(RID p_uniform_set, int p_size) {
  998. Sort::PushConstant push_constant;
  999. push_constant.total_elements = p_size;
  1000. bool done = true;
  1001. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  1002. if (numThreadGroups > 1) {
  1003. done = false;
  1004. }
  1005. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1006. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_BLOCK]);
  1007. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  1008. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1009. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1010. int presorted = 512;
  1011. while (!done) {
  1012. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1013. done = true;
  1014. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_STEP]);
  1015. numThreadGroups = 0;
  1016. if (p_size > presorted) {
  1017. if (p_size > presorted * 2) {
  1018. done = false;
  1019. }
  1020. int pow2 = presorted;
  1021. while (pow2 < p_size) {
  1022. pow2 *= 2;
  1023. }
  1024. numThreadGroups = pow2 >> 9;
  1025. }
  1026. unsigned int nMergeSize = presorted * 2;
  1027. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  1028. push_constant.job_params[0] = nMergeSubSize;
  1029. if (nMergeSubSize == nMergeSize >> 1) {
  1030. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  1031. push_constant.job_params[2] = -1;
  1032. } else {
  1033. push_constant.job_params[1] = nMergeSubSize;
  1034. push_constant.job_params[2] = 1;
  1035. }
  1036. push_constant.job_params[3] = 0;
  1037. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1038. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1039. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1040. }
  1041. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sort.pipelines[SORT_MODE_INNER]);
  1042. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(Sort::PushConstant));
  1043. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  1044. presorted *= 2;
  1045. }
  1046. RD::get_singleton()->compute_list_end();
  1047. }
  1048. RasterizerEffectsRD::RasterizerEffectsRD() {
  1049. { // Initialize copy
  1050. Vector<String> copy_modes;
  1051. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  1052. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  1053. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  1054. copy_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  1055. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  1056. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  1057. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  1058. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  1059. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  1060. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  1061. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  1062. copy.shader.initialize(copy_modes);
  1063. zeromem(&copy.push_constant, sizeof(CopyPushConstant));
  1064. copy.shader_version = copy.shader.version_create();
  1065. for (int i = 0; i < COPY_MODE_MAX; i++) {
  1066. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  1067. }
  1068. }
  1069. {
  1070. Vector<String> copy_modes;
  1071. copy_modes.push_back("\n");
  1072. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n");
  1073. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n");
  1074. copy_to_fb.shader.initialize(copy_modes);
  1075. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  1076. //use additive
  1077. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  1078. 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);
  1079. }
  1080. }
  1081. {
  1082. // Initialize roughness
  1083. Vector<String> cubemap_roughness_modes;
  1084. cubemap_roughness_modes.push_back("");
  1085. roughness.shader.initialize(cubemap_roughness_modes);
  1086. roughness.shader_version = roughness.shader.version_create();
  1087. roughness.pipeline = RD::get_singleton()->compute_pipeline_create(roughness.shader.version_get_shader(roughness.shader_version, 0));
  1088. }
  1089. {
  1090. // Initialize tonemapper
  1091. Vector<String> tonemap_modes;
  1092. tonemap_modes.push_back("\n");
  1093. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  1094. tonemap.shader.initialize(tonemap_modes);
  1095. tonemap.shader_version = tonemap.shader.version_create();
  1096. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  1097. 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);
  1098. }
  1099. }
  1100. {
  1101. // Initialize luminance_reduce
  1102. Vector<String> luminance_reduce_modes;
  1103. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  1104. luminance_reduce_modes.push_back("\n");
  1105. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  1106. luminance_reduce.shader.initialize(luminance_reduce_modes);
  1107. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  1108. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  1109. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  1110. }
  1111. }
  1112. {
  1113. // Initialize copier
  1114. Vector<String> copy_modes;
  1115. copy_modes.push_back("\n");
  1116. cube_to_dp.shader.initialize(copy_modes);
  1117. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  1118. cube_to_dp.pipeline = RD::get_singleton()->compute_pipeline_create(cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0));
  1119. }
  1120. {
  1121. // Initialize bokeh
  1122. Vector<String> bokeh_modes;
  1123. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  1124. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  1125. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  1126. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n");
  1127. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  1128. bokeh.shader.initialize(bokeh_modes);
  1129. bokeh.shader_version = bokeh.shader.version_create();
  1130. for (int i = 0; i < BOKEH_MAX; i++) {
  1131. bokeh.pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.shader.version_get_shader(bokeh.shader_version, i));
  1132. }
  1133. }
  1134. {
  1135. // Initialize ssao
  1136. uint32_t pipeline = 0;
  1137. {
  1138. Vector<String> ssao_modes;
  1139. ssao_modes.push_back("\n#define MINIFY_START\n");
  1140. ssao_modes.push_back("\n");
  1141. ssao.minify_shader.initialize(ssao_modes);
  1142. ssao.minify_shader_version = ssao.minify_shader.version_create();
  1143. for (int i = 0; i <= SSAO_MINIFY_MIPMAP; i++) {
  1144. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.minify_shader.version_get_shader(ssao.minify_shader_version, i));
  1145. pipeline++;
  1146. }
  1147. }
  1148. {
  1149. Vector<String> ssao_modes;
  1150. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n");
  1151. ssao_modes.push_back("\n");
  1152. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n");
  1153. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n");
  1154. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n#define USE_HALF_SIZE\n");
  1155. ssao_modes.push_back("\n#define USE_HALF_SIZE\n");
  1156. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n#define USE_HALF_SIZE\n");
  1157. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n#define USE_HALF_SIZE\n");
  1158. ssao.gather_shader.initialize(ssao_modes);
  1159. ssao.gather_shader_version = ssao.gather_shader.version_create();
  1160. for (int i = SSAO_GATHER_LOW; i <= SSAO_GATHER_ULTRA_HALF; i++) {
  1161. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i - SSAO_GATHER_LOW));
  1162. pipeline++;
  1163. }
  1164. }
  1165. {
  1166. Vector<String> ssao_modes;
  1167. ssao_modes.push_back("\n#define MODE_FULL_SIZE\n");
  1168. ssao_modes.push_back("\n");
  1169. ssao_modes.push_back("\n#define MODE_UPSCALE\n");
  1170. ssao.blur_shader.initialize(ssao_modes);
  1171. ssao.blur_shader_version = ssao.blur_shader.version_create();
  1172. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_UPSCALE; i++) {
  1173. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  1174. pipeline++;
  1175. }
  1176. }
  1177. ERR_FAIL_COND(pipeline != SSAO_MAX);
  1178. }
  1179. {
  1180. // Initialize roughness limiter
  1181. Vector<String> shader_modes;
  1182. shader_modes.push_back("");
  1183. roughness_limiter.shader.initialize(shader_modes);
  1184. roughness_limiter.shader_version = roughness_limiter.shader.version_create();
  1185. roughness_limiter.pipeline = RD::get_singleton()->compute_pipeline_create(roughness_limiter.shader.version_get_shader(roughness_limiter.shader_version, 0));
  1186. }
  1187. {
  1188. //Initialize cubemap downsampler
  1189. Vector<String> cubemap_downsampler_modes;
  1190. cubemap_downsampler_modes.push_back("");
  1191. cubemap_downsampler.shader.initialize(cubemap_downsampler_modes);
  1192. cubemap_downsampler.shader_version = cubemap_downsampler.shader.version_create();
  1193. cubemap_downsampler.pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  1194. }
  1195. {
  1196. // Initialize cubemap filter
  1197. filter.use_high_quality = GLOBAL_GET("rendering/quality/reflections/fast_filter_high_quality");
  1198. Vector<String> cubemap_filter_modes;
  1199. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  1200. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  1201. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1202. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  1203. filter.shader.initialize(cubemap_filter_modes);
  1204. filter.shader_version = filter.shader.version_create();
  1205. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  1206. filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.shader.version_get_shader(filter.shader_version, i));
  1207. }
  1208. if (filter.use_high_quality) {
  1209. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  1210. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0], false);
  1211. } else {
  1212. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  1213. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0], false);
  1214. }
  1215. Vector<RD::Uniform> uniforms;
  1216. {
  1217. RD::Uniform u;
  1218. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1219. u.binding = 0;
  1220. u.ids.push_back(filter.coefficient_buffer);
  1221. uniforms.push_back(u);
  1222. }
  1223. 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);
  1224. }
  1225. {
  1226. Vector<String> specular_modes;
  1227. specular_modes.push_back("\n#define MODE_MERGE\n");
  1228. specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n");
  1229. specular_modes.push_back("\n");
  1230. specular_modes.push_back("\n#define MODE_SSR\n");
  1231. specular_merge.shader.initialize(specular_modes);
  1232. specular_merge.shader_version = specular_merge.shader.version_create();
  1233. //use additive
  1234. RD::PipelineColorBlendState::Attachment ba;
  1235. ba.enable_blend = true;
  1236. ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1237. ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
  1238. ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1239. ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1240. ba.color_blend_op = RD::BLEND_OP_ADD;
  1241. ba.alpha_blend_op = RD::BLEND_OP_ADD;
  1242. RD::PipelineColorBlendState blend_additive;
  1243. blend_additive.attachments.push_back(ba);
  1244. for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
  1245. RD::PipelineColorBlendState blend_state;
  1246. if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR) {
  1247. blend_state = blend_additive;
  1248. } else {
  1249. blend_state = RD::PipelineColorBlendState::create_disabled();
  1250. }
  1251. 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);
  1252. }
  1253. }
  1254. {
  1255. Vector<String> ssr_modes;
  1256. ssr_modes.push_back("\n");
  1257. ssr_modes.push_back("\n#define MODE_ROUGH\n");
  1258. ssr.shader.initialize(ssr_modes);
  1259. ssr.shader_version = ssr.shader.version_create();
  1260. for (int i = 0; i < SCREEN_SPACE_REFLECTION_MAX; i++) {
  1261. ssr.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr.shader.version_get_shader(ssr.shader_version, i));
  1262. }
  1263. }
  1264. {
  1265. Vector<String> ssr_filter_modes;
  1266. ssr_filter_modes.push_back("\n");
  1267. ssr_filter_modes.push_back("\n#define VERTICAL_PASS\n");
  1268. ssr_filter.shader.initialize(ssr_filter_modes);
  1269. ssr_filter.shader_version = ssr_filter.shader.version_create();
  1270. for (int i = 0; i < SCREEN_SPACE_REFLECTION_FILTER_MAX; i++) {
  1271. ssr_filter.pipelines[i] = RD::get_singleton()->compute_pipeline_create(ssr_filter.shader.version_get_shader(ssr_filter.shader_version, i));
  1272. }
  1273. }
  1274. {
  1275. Vector<String> ssr_scale_modes;
  1276. ssr_scale_modes.push_back("\n");
  1277. ssr_scale.shader.initialize(ssr_scale_modes);
  1278. ssr_scale.shader_version = ssr_scale.shader.version_create();
  1279. ssr_scale.pipeline = RD::get_singleton()->compute_pipeline_create(ssr_scale.shader.version_get_shader(ssr_scale.shader_version, 0));
  1280. }
  1281. {
  1282. Vector<String> sss_modes;
  1283. sss_modes.push_back("\n#define USE_11_SAMPLES\n");
  1284. sss_modes.push_back("\n#define USE_17_SAMPLES\n");
  1285. sss_modes.push_back("\n#define USE_25_SAMPLES\n");
  1286. sss.shader.initialize(sss_modes);
  1287. sss.shader_version = sss.shader.version_create();
  1288. for (int i = 0; i < sss_modes.size(); i++) {
  1289. sss.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sss.shader.version_get_shader(sss.shader_version, i));
  1290. }
  1291. }
  1292. {
  1293. Vector<String> resolve_modes;
  1294. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n");
  1295. resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define GIPROBE_RESOLVE\n");
  1296. resolve.shader.initialize(resolve_modes);
  1297. resolve.shader_version = resolve.shader.version_create();
  1298. for (int i = 0; i < RESOLVE_MODE_MAX; i++) {
  1299. resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i));
  1300. }
  1301. }
  1302. {
  1303. Vector<String> shadow_reduce_modes;
  1304. shadow_reduce_modes.push_back("\n#define MODE_REDUCE\n");
  1305. shadow_reduce_modes.push_back("\n#define MODE_FILTER\n");
  1306. shadow_reduce.shader.initialize(shadow_reduce_modes);
  1307. shadow_reduce.shader_version = shadow_reduce.shader.version_create();
  1308. for (int i = 0; i < SHADOW_REDUCE_MAX; i++) {
  1309. shadow_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(shadow_reduce.shader.version_get_shader(shadow_reduce.shader_version, i));
  1310. }
  1311. }
  1312. {
  1313. Vector<String> sort_modes;
  1314. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  1315. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  1316. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  1317. sort.shader.initialize(sort_modes);
  1318. sort.shader_version = sort.shader.version_create();
  1319. for (int i = 0; i < SORT_MODE_MAX; i++) {
  1320. sort.pipelines[i] = RD::get_singleton()->compute_pipeline_create(sort.shader.version_get_shader(sort.shader_version, i));
  1321. }
  1322. }
  1323. RD::SamplerState sampler;
  1324. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1325. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1326. sampler.max_lod = 0;
  1327. default_sampler = RD::get_singleton()->sampler_create(sampler);
  1328. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1329. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1330. sampler.max_lod = 1e20;
  1331. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  1332. { //create index array for copy shaders
  1333. Vector<uint8_t> pv;
  1334. pv.resize(6 * 4);
  1335. {
  1336. uint8_t *w = pv.ptrw();
  1337. int *p32 = (int *)w;
  1338. p32[0] = 0;
  1339. p32[1] = 1;
  1340. p32[2] = 2;
  1341. p32[3] = 0;
  1342. p32[4] = 2;
  1343. p32[5] = 3;
  1344. }
  1345. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1346. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  1347. }
  1348. }
  1349. RasterizerEffectsRD::~RasterizerEffectsRD() {
  1350. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  1351. RD::get_singleton()->free(filter.image_uniform_set);
  1352. }
  1353. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  1354. RD::get_singleton()->free(filter.uniform_set);
  1355. }
  1356. RD::get_singleton()->free(default_sampler);
  1357. RD::get_singleton()->free(default_mipmap_sampler);
  1358. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  1359. RD::get_singleton()->free(filter.coefficient_buffer);
  1360. bokeh.shader.version_free(bokeh.shader_version);
  1361. copy.shader.version_free(copy.shader_version);
  1362. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  1363. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  1364. cubemap_downsampler.shader.version_free(cubemap_downsampler.shader_version);
  1365. filter.shader.version_free(filter.shader_version);
  1366. luminance_reduce.shader.version_free(luminance_reduce.shader_version);
  1367. resolve.shader.version_free(resolve.shader_version);
  1368. roughness.shader.version_free(roughness.shader_version);
  1369. roughness_limiter.shader.version_free(roughness_limiter.shader_version);
  1370. sort.shader.version_free(sort.shader_version);
  1371. specular_merge.shader.version_free(specular_merge.shader_version);
  1372. ssao.blur_shader.version_free(ssao.blur_shader_version);
  1373. ssao.gather_shader.version_free(ssao.gather_shader_version);
  1374. ssao.minify_shader.version_free(ssao.minify_shader_version);
  1375. ssr.shader.version_free(ssr.shader_version);
  1376. ssr_filter.shader.version_free(ssr_filter.shader_version);
  1377. ssr_scale.shader.version_free(ssr_scale.shader_version);
  1378. sss.shader.version_free(sss.shader_version);
  1379. tonemap.shader.version_free(tonemap.shader_version);
  1380. shadow_reduce.shader.version_free(shadow_reduce.shader_version);
  1381. }