rasterizer_effects_rd.cpp 75 KB

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