effects_rd.cpp 95 KB

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