effects_rd.cpp 98 KB

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