copy_effects.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*************************************************************************/
  2. /* copy_effects.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "copy_effects.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  33. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  34. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  35. #include "thirdparty/misc/cubemap_coeffs.h"
  36. using namespace RendererRD;
  37. CopyEffects *CopyEffects::singleton = nullptr;
  38. CopyEffects *CopyEffects::get_singleton() {
  39. return singleton;
  40. }
  41. CopyEffects::CopyEffects(bool p_prefer_raster_effects) {
  42. singleton = this;
  43. prefer_raster_effects = p_prefer_raster_effects;
  44. if (prefer_raster_effects) {
  45. // init blur shader (on compute use copy shader)
  46. Vector<String> blur_modes;
  47. blur_modes.push_back("\n#define MODE_MIPMAP\n"); // BLUR_MIPMAP
  48. blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n"); // BLUR_MODE_GAUSSIAN_BLUR
  49. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n"); // BLUR_MODE_GAUSSIAN_GLOW
  50. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n"); // BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE
  51. blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY
  52. blur_raster.shader.initialize(blur_modes);
  53. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  54. blur_raster.shader_version = blur_raster.shader.version_create();
  55. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  56. blur_raster.pipelines[i].setup(blur_raster.shader.version_get_shader(blur_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  57. }
  58. } else {
  59. // not used in clustered
  60. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  61. blur_raster.pipelines[i].clear();
  62. }
  63. Vector<String> copy_modes;
  64. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  65. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
  66. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n");
  67. copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  68. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  69. copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
  70. copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
  71. copy_modes.push_back("\n#define MODE_SET_COLOR\n");
  72. copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
  73. copy_modes.push_back("\n#define MODE_MIPMAP\n");
  74. copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
  75. copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
  76. copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
  77. copy.shader.initialize(copy_modes);
  78. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  79. copy.shader_version = copy.shader.version_create();
  80. for (int i = 0; i < COPY_MODE_MAX; i++) {
  81. if (copy.shader.is_variant_enabled(i)) {
  82. copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
  83. }
  84. }
  85. }
  86. {
  87. Vector<String> copy_modes;
  88. copy_modes.push_back("\n");
  89. copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n");
  90. copy_modes.push_back("\n#define MODE_TWO_SOURCES\n");
  91. copy_modes.push_back("\n#define MULTIVIEW\n");
  92. copy_modes.push_back("\n#define MULTIVIEW\n#define MODE_TWO_SOURCES\n");
  93. copy_to_fb.shader.initialize(copy_modes);
  94. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  95. copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW, false);
  96. copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW_WITH_DEPTH, false);
  97. }
  98. copy_to_fb.shader_version = copy_to_fb.shader.version_create();
  99. //use additive
  100. for (int i = 0; i < COPY_TO_FB_MAX; i++) {
  101. if (copy_to_fb.shader.is_variant_enabled(i)) {
  102. 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);
  103. } else {
  104. copy_to_fb.pipelines[i].clear();
  105. }
  106. }
  107. }
  108. {
  109. // Initialize copier
  110. Vector<String> copy_modes;
  111. copy_modes.push_back("\n");
  112. cube_to_dp.shader.initialize(copy_modes);
  113. cube_to_dp.shader_version = cube_to_dp.shader.version_create();
  114. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  115. RD::PipelineDepthStencilState dss;
  116. dss.enable_depth_test = true;
  117. dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
  118. dss.enable_depth_write = true;
  119. cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
  120. }
  121. {
  122. //Initialize cubemap downsampler
  123. Vector<String> cubemap_downsampler_modes;
  124. cubemap_downsampler_modes.push_back("");
  125. if (prefer_raster_effects) {
  126. cubemap_downsampler.raster_shader.initialize(cubemap_downsampler_modes);
  127. cubemap_downsampler.shader_version = cubemap_downsampler.raster_shader.version_create();
  128. cubemap_downsampler.raster_pipeline.setup(cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  129. } else {
  130. cubemap_downsampler.compute_shader.initialize(cubemap_downsampler_modes);
  131. cubemap_downsampler.shader_version = cubemap_downsampler.compute_shader.version_create();
  132. cubemap_downsampler.compute_pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0));
  133. cubemap_downsampler.raster_pipeline.clear();
  134. }
  135. }
  136. {
  137. // Initialize cubemap filter
  138. filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
  139. Vector<String> cubemap_filter_modes;
  140. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
  141. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
  142. cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  143. cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
  144. if (filter.use_high_quality) {
  145. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
  146. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
  147. } else {
  148. filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
  149. RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
  150. }
  151. if (prefer_raster_effects) {
  152. filter.raster_shader.initialize(cubemap_filter_modes);
  153. // array variants are not supported in raster
  154. filter.raster_shader.set_variant_enabled(FILTER_MODE_HIGH_QUALITY_ARRAY, false);
  155. filter.raster_shader.set_variant_enabled(FILTER_MODE_LOW_QUALITY_ARRAY, false);
  156. filter.shader_version = filter.raster_shader.version_create();
  157. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  158. if (filter.raster_shader.is_variant_enabled(i)) {
  159. filter.raster_pipelines[i].setup(filter.raster_shader.version_get_shader(filter.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  160. } else {
  161. filter.raster_pipelines[i].clear();
  162. }
  163. }
  164. Vector<RD::Uniform> uniforms;
  165. {
  166. RD::Uniform u;
  167. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  168. u.binding = 0;
  169. u.append_id(filter.coefficient_buffer);
  170. uniforms.push_back(u);
  171. }
  172. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.raster_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  173. } else {
  174. filter.compute_shader.initialize(cubemap_filter_modes);
  175. filter.shader_version = filter.compute_shader.version_create();
  176. for (int i = 0; i < FILTER_MODE_MAX; i++) {
  177. filter.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.compute_shader.version_get_shader(filter.shader_version, i));
  178. filter.raster_pipelines[i].clear();
  179. }
  180. Vector<RD::Uniform> uniforms;
  181. {
  182. RD::Uniform u;
  183. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  184. u.binding = 0;
  185. u.append_id(filter.coefficient_buffer);
  186. uniforms.push_back(u);
  187. }
  188. filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
  189. }
  190. }
  191. {
  192. // Initialize roughness
  193. Vector<String> cubemap_roughness_modes;
  194. cubemap_roughness_modes.push_back("");
  195. if (prefer_raster_effects) {
  196. roughness.raster_shader.initialize(cubemap_roughness_modes);
  197. roughness.shader_version = roughness.raster_shader.version_create();
  198. roughness.raster_pipeline.setup(roughness.raster_shader.version_get_shader(roughness.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  199. } else {
  200. roughness.compute_shader.initialize(cubemap_roughness_modes);
  201. roughness.shader_version = roughness.compute_shader.version_create();
  202. roughness.compute_pipeline = RD::get_singleton()->compute_pipeline_create(roughness.compute_shader.version_get_shader(roughness.shader_version, 0));
  203. roughness.raster_pipeline.clear();
  204. }
  205. }
  206. }
  207. CopyEffects::~CopyEffects() {
  208. if (prefer_raster_effects) {
  209. blur_raster.shader.version_free(blur_raster.shader_version);
  210. cubemap_downsampler.raster_shader.version_free(cubemap_downsampler.shader_version);
  211. filter.raster_shader.version_free(filter.shader_version);
  212. roughness.raster_shader.version_free(roughness.shader_version);
  213. } else {
  214. copy.shader.version_free(copy.shader_version);
  215. cubemap_downsampler.compute_shader.version_free(cubemap_downsampler.shader_version);
  216. filter.compute_shader.version_free(filter.shader_version);
  217. roughness.compute_shader.version_free(roughness.shader_version);
  218. }
  219. RD::get_singleton()->free(filter.coefficient_buffer);
  220. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  221. RD::get_singleton()->free(filter.image_uniform_set);
  222. }
  223. if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
  224. RD::get_singleton()->free(filter.uniform_set);
  225. }
  226. copy_to_fb.shader.version_free(copy_to_fb.shader_version);
  227. cube_to_dp.shader.version_free(cube_to_dp.shader_version);
  228. singleton = nullptr;
  229. }
  230. void CopyEffects::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) {
  231. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_to_rect shader with the mobile renderer.");
  232. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  233. ERR_FAIL_NULL(uniform_set_cache);
  234. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  235. ERR_FAIL_NULL(material_storage);
  236. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  237. if (p_flip_y) {
  238. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  239. }
  240. if (p_force_luminance) {
  241. copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
  242. }
  243. if (p_all_source) {
  244. copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
  245. }
  246. if (p_alpha_to_one) {
  247. copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
  248. }
  249. copy.push_constant.section[0] = 0;
  250. copy.push_constant.section[1] = 0;
  251. copy.push_constant.section[2] = p_rect.size.width;
  252. copy.push_constant.section[3] = p_rect.size.height;
  253. copy.push_constant.target[0] = p_rect.position.x;
  254. copy.push_constant.target[1] = p_rect.position.y;
  255. // setup our uniforms
  256. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  257. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  258. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  259. CopyMode mode = p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY;
  260. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  261. ERR_FAIL_COND(shader.is_null());
  262. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  263. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  264. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  265. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  266. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  267. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  268. RD::get_singleton()->compute_list_end();
  269. }
  270. void CopyEffects::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {
  271. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_cubemap_to_panorama shader with the mobile renderer.");
  272. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  273. ERR_FAIL_NULL(uniform_set_cache);
  274. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  275. ERR_FAIL_NULL(material_storage);
  276. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  277. copy.push_constant.section[0] = 0;
  278. copy.push_constant.section[1] = 0;
  279. copy.push_constant.section[2] = p_panorama_size.width;
  280. copy.push_constant.section[3] = p_panorama_size.height;
  281. copy.push_constant.target[0] = 0;
  282. copy.push_constant.target[1] = 0;
  283. copy.push_constant.camera_z_far = p_lod;
  284. // setup our uniforms
  285. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  286. RD::Uniform u_source_cube(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cube }));
  287. RD::Uniform u_dest_panorama(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_panorama);
  288. CopyMode mode = p_is_array ? COPY_MODE_CUBE_ARRAY_TO_PANORAMA : COPY_MODE_CUBE_TO_PANORAMA;
  289. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  290. ERR_FAIL_COND(shader.is_null());
  291. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  292. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  293. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cube), 0);
  294. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_panorama), 3);
  295. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  296. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
  297. RD::get_singleton()->compute_list_end();
  298. }
  299. void CopyEffects::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
  300. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_depth_to_rect shader with the mobile renderer.");
  301. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  302. ERR_FAIL_NULL(uniform_set_cache);
  303. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  304. ERR_FAIL_NULL(material_storage);
  305. memset(&copy.push_constant, 0, 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. // setup our uniforms
  316. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  317. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  318. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  319. CopyMode mode = COPY_MODE_SIMPLY_COPY_DEPTH;
  320. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  321. ERR_FAIL_COND(shader.is_null());
  322. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  323. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  324. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  325. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  326. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  327. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  328. RD::get_singleton()->compute_list_end();
  329. }
  330. void CopyEffects::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) {
  331. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_depth_to_rect_and_linearize shader with the mobile renderer.");
  332. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  333. ERR_FAIL_NULL(uniform_set_cache);
  334. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  335. ERR_FAIL_NULL(material_storage);
  336. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  337. if (p_flip_y) {
  338. copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
  339. }
  340. copy.push_constant.section[0] = 0;
  341. copy.push_constant.section[1] = 0;
  342. copy.push_constant.section[2] = p_rect.size.width;
  343. copy.push_constant.section[3] = p_rect.size.height;
  344. copy.push_constant.target[0] = p_rect.position.x;
  345. copy.push_constant.target[1] = p_rect.position.y;
  346. copy.push_constant.camera_z_far = p_z_far;
  347. copy.push_constant.camera_z_near = p_z_near;
  348. // setup our uniforms
  349. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  350. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  351. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  352. CopyMode mode = COPY_MODE_LINEARIZE_DEPTH;
  353. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  354. ERR_FAIL_COND(shader.is_null());
  355. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  356. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  357. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  358. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  359. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  360. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
  361. RD::get_singleton()->compute_list_end();
  362. }
  363. void CopyEffects::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) {
  364. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  365. ERR_FAIL_NULL(uniform_set_cache);
  366. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  367. ERR_FAIL_NULL(material_storage);
  368. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  369. copy_to_fb.push_constant.use_section = true;
  370. copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
  371. copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
  372. copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
  373. copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
  374. if (p_flip_y) {
  375. copy_to_fb.push_constant.flip_y = true;
  376. }
  377. // setup our uniforms
  378. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  379. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  380. CopyToFBMode mode = p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY;
  381. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  382. ERR_FAIL_COND(shader.is_null());
  383. RD::DrawListID draw_list = p_draw_list;
  384. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  385. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  386. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  387. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  388. RD::get_singleton()->draw_list_draw(draw_list, true);
  389. }
  390. void CopyEffects::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, bool p_multiview) {
  391. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  392. ERR_FAIL_NULL(uniform_set_cache);
  393. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  394. ERR_FAIL_NULL(material_storage);
  395. memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
  396. if (p_flip_y) {
  397. copy_to_fb.push_constant.flip_y = true;
  398. }
  399. if (p_force_luminance) {
  400. copy_to_fb.push_constant.force_luminance = true;
  401. }
  402. if (p_alpha_to_zero) {
  403. copy_to_fb.push_constant.alpha_to_zero = true;
  404. }
  405. if (p_srgb) {
  406. copy_to_fb.push_constant.srgb = true;
  407. }
  408. // setup our uniforms
  409. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  410. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  411. CopyToFBMode mode;
  412. if (p_multiview) {
  413. mode = p_secondary.is_valid() ? COPY_TO_FB_MULTIVIEW_WITH_DEPTH : COPY_TO_FB_MULTIVIEW;
  414. } else {
  415. mode = p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY;
  416. }
  417. RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
  418. ERR_FAIL_COND(shader.is_null());
  419. 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);
  420. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  421. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  422. if (p_secondary.is_valid()) {
  423. // TODO may need to do this differently when reading from depth buffer for multiview
  424. RD::Uniform u_secondary(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_secondary }));
  425. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_secondary), 1);
  426. }
  427. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  428. RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
  429. RD::get_singleton()->draw_list_draw(draw_list, true);
  430. RD::get_singleton()->draw_list_end();
  431. }
  432. void CopyEffects::copy_raster(RID p_source_texture, RID p_dest_framebuffer) {
  433. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the copy with the clustered renderer.");
  434. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  435. ERR_FAIL_NULL(uniform_set_cache);
  436. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  437. ERR_FAIL_NULL(material_storage);
  438. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  439. // setup our uniforms
  440. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  441. RD::Uniform u_source_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_texture }));
  442. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, BLUR_MODE_COPY);
  443. ERR_FAIL_COND(shader.is_null());
  444. // Just copy it back (we use our blur raster shader here)..
  445. 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);
  446. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[BLUR_MODE_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  447. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_texture), 0);
  448. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  449. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  450. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  451. RD::get_singleton()->draw_list_draw(draw_list, true);
  452. RD::get_singleton()->draw_list_end();
  453. }
  454. void CopyEffects::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, bool p_8bit_dst) {
  455. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");
  456. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  457. ERR_FAIL_NULL(uniform_set_cache);
  458. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  459. ERR_FAIL_NULL(material_storage);
  460. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  461. copy.push_constant.section[0] = p_region.position.x;
  462. copy.push_constant.section[1] = p_region.position.y;
  463. copy.push_constant.section[2] = p_region.size.width;
  464. copy.push_constant.section[3] = p_region.size.height;
  465. // setup our uniforms
  466. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  467. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  468. RD::Uniform u_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_texture);
  469. CopyMode mode = p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY;
  470. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  471. ERR_FAIL_COND(shader.is_null());
  472. //HORIZONTAL
  473. RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
  474. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  475. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  476. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_texture), 3);
  477. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  478. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  479. RD::get_singleton()->compute_list_end();
  480. }
  481. void CopyEffects::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_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  482. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");
  483. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  484. ERR_FAIL_NULL(uniform_set_cache);
  485. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  486. ERR_FAIL_NULL(material_storage);
  487. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  488. CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
  489. uint32_t base_flags = 0;
  490. copy.push_constant.section[2] = p_size.x;
  491. copy.push_constant.section[3] = p_size.y;
  492. copy.push_constant.glow_strength = p_strength;
  493. copy.push_constant.glow_bloom = p_bloom;
  494. copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  495. copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  496. copy.push_constant.glow_exposure = p_exposure;
  497. copy.push_constant.glow_white = 0; //actually unused
  498. copy.push_constant.glow_luminance_cap = p_luminance_cap;
  499. copy.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  500. // setup our uniforms
  501. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  502. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  503. RD::Uniform u_back_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_back_texture);
  504. RID shader = copy.shader.version_get_shader(copy.shader_version, copy_mode);
  505. ERR_FAIL_COND(shader.is_null());
  506. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  507. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
  508. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  509. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_back_texture), 3);
  510. if (p_auto_exposure.is_valid() && p_first_pass) {
  511. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  512. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  513. }
  514. copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0) | (p_high_quality ? COPY_FLAG_HIGH_QUALITY_GLOW : 0);
  515. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  516. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  517. RD::get_singleton()->compute_list_end();
  518. }
  519. void CopyEffects::gaussian_glow_raster(RID p_source_rd_texture, float p_luminance_multiplier, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, 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_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  520. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");
  521. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  522. ERR_FAIL_NULL(uniform_set_cache);
  523. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  524. ERR_FAIL_NULL(material_storage);
  525. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  526. BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  527. uint32_t base_flags = 0;
  528. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  529. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  530. blur_raster.push_constant.glow_strength = p_strength;
  531. blur_raster.push_constant.glow_bloom = p_bloom;
  532. blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
  533. blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  534. blur_raster.push_constant.glow_exposure = p_exposure;
  535. blur_raster.push_constant.glow_white = 0; //actually unused
  536. blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
  537. blur_raster.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  538. blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;
  539. // setup our uniforms
  540. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  541. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  542. RD::Uniform u_rd_texture_half(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_rd_texture_half }));
  543. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  544. ERR_FAIL_COND(shader.is_null());
  545. //HORIZONTAL
  546. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer_half, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  547. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer_half)));
  548. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  549. if (p_auto_exposure.is_valid() && p_first_pass) {
  550. RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
  551. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
  552. }
  553. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  554. blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  555. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  556. RD::get_singleton()->draw_list_draw(draw_list, true);
  557. RD::get_singleton()->draw_list_end();
  558. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  559. shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
  560. ERR_FAIL_COND(shader.is_null());
  561. //VERTICAL
  562. 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);
  563. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  564. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_rd_texture_half), 0);
  565. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  566. blur_raster.push_constant.flags = base_flags;
  567. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  568. RD::get_singleton()->draw_list_draw(draw_list, true);
  569. RD::get_singleton()->draw_list_end();
  570. }
  571. void CopyEffects::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
  572. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the make_mipmap shader with the mobile renderer.");
  573. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  574. ERR_FAIL_NULL(uniform_set_cache);
  575. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  576. ERR_FAIL_NULL(material_storage);
  577. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  578. copy.push_constant.section[0] = 0;
  579. copy.push_constant.section[1] = 0;
  580. copy.push_constant.section[2] = p_size.width;
  581. copy.push_constant.section[3] = p_size.height;
  582. // setup our uniforms
  583. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  584. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  585. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  586. CopyMode mode = COPY_MODE_MIPMAP;
  587. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  588. ERR_FAIL_COND(shader.is_null());
  589. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  590. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  591. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  592. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  593. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  594. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
  595. RD::get_singleton()->compute_list_end();
  596. }
  597. void CopyEffects::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_framebuffer, const Size2i &p_size) {
  598. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");
  599. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  600. ERR_FAIL_NULL(uniform_set_cache);
  601. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  602. ERR_FAIL_NULL(material_storage);
  603. memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
  604. BlurRasterMode mode = BLUR_MIPMAP;
  605. blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
  606. blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
  607. // setup our uniforms
  608. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  609. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  610. RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, mode);
  611. ERR_FAIL_COND(shader.is_null());
  612. 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);
  613. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  614. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  615. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  616. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
  617. RD::get_singleton()->draw_list_draw(draw_list, true);
  618. RD::get_singleton()->draw_list_end();
  619. }
  620. void CopyEffects::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
  621. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the set_color shader with the mobile renderer.");
  622. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  623. ERR_FAIL_NULL(uniform_set_cache);
  624. memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
  625. copy.push_constant.section[0] = 0;
  626. copy.push_constant.section[1] = 0;
  627. copy.push_constant.section[2] = p_region.size.width;
  628. copy.push_constant.section[3] = p_region.size.height;
  629. copy.push_constant.target[0] = p_region.position.x;
  630. copy.push_constant.target[1] = p_region.position.y;
  631. copy.push_constant.set_color[0] = p_color.r;
  632. copy.push_constant.set_color[1] = p_color.g;
  633. copy.push_constant.set_color[2] = p_color.b;
  634. copy.push_constant.set_color[3] = p_color.a;
  635. // setup our uniforms
  636. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
  637. CopyMode mode = p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR;
  638. RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
  639. ERR_FAIL_COND(shader.is_null());
  640. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  641. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
  642. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
  643. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
  644. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
  645. RD::get_singleton()->compute_list_end();
  646. }
  647. void CopyEffects::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, const Vector2 &p_dst_size, float p_z_near, float p_z_far, bool p_dp_flip) {
  648. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  649. ERR_FAIL_NULL(uniform_set_cache);
  650. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  651. ERR_FAIL_NULL(material_storage);
  652. CopyToDPPushConstant push_constant;
  653. push_constant.screen_rect[0] = p_rect.position.x;
  654. push_constant.screen_rect[1] = p_rect.position.y;
  655. push_constant.screen_rect[2] = p_rect.size.width;
  656. push_constant.screen_rect[3] = p_rect.size.height;
  657. push_constant.z_far = p_z_far;
  658. push_constant.z_near = p_z_near;
  659. push_constant.texel_size[0] = 1.0f / p_dst_size.x;
  660. push_constant.texel_size[1] = 1.0f / p_dst_size.y;
  661. push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
  662. // setup our uniforms
  663. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  664. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  665. RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
  666. ERR_FAIL_COND(shader.is_null());
  667. 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);
  668. 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)));
  669. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  670. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  671. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  672. RD::get_singleton()->draw_list_draw(draw_list, true);
  673. RD::get_singleton()->draw_list_end(RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_TRANSFER);
  674. }
  675. void CopyEffects::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
  676. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");
  677. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  678. ERR_FAIL_NULL(uniform_set_cache);
  679. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  680. ERR_FAIL_NULL(material_storage);
  681. cubemap_downsampler.push_constant.face_size = p_size.x;
  682. cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call
  683. // setup our uniforms
  684. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  685. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
  686. RD::Uniform u_dest_cubemap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_cubemap }));
  687. RID shader = cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
  688. ERR_FAIL_COND(shader.is_null());
  689. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  690. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);
  691. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  692. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_cubemap), 1);
  693. int x_groups = (p_size.x - 1) / 8 + 1;
  694. int y_groups = (p_size.y - 1) / 8 + 1;
  695. RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  696. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
  697. RD::get_singleton()->compute_list_end();
  698. }
  699. void CopyEffects::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {
  700. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");
  701. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");
  702. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  703. ERR_FAIL_NULL(uniform_set_cache);
  704. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  705. ERR_FAIL_NULL(material_storage);
  706. cubemap_downsampler.push_constant.face_size = p_size.x;
  707. cubemap_downsampler.push_constant.face_id = p_face_id;
  708. // setup our uniforms
  709. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  710. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
  711. RID shader = cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
  712. ERR_FAIL_COND(shader.is_null());
  713. 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);
  714. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cubemap_downsampler.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  715. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  716. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  717. RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
  718. RD::get_singleton()->draw_list_draw(draw_list, true);
  719. RD::get_singleton()->draw_list_end();
  720. }
  721. void CopyEffects::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
  722. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");
  723. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  724. ERR_FAIL_NULL(uniform_set_cache);
  725. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  726. ERR_FAIL_NULL(material_storage);
  727. Vector<RD::Uniform> uniforms;
  728. for (int i = 0; i < p_dest_cubemap.size(); i++) {
  729. RD::Uniform u;
  730. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  731. u.binding = i;
  732. u.append_id(p_dest_cubemap[i]);
  733. uniforms.push_back(u);
  734. }
  735. if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
  736. RD::get_singleton()->free(filter.image_uniform_set);
  737. }
  738. filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
  739. // setup our uniforms
  740. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  741. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
  742. int mode = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
  743. mode = filter.use_high_quality ? mode : mode + 1;
  744. RID shader = filter.compute_shader.version_get_shader(filter.shader_version, mode);
  745. ERR_FAIL_COND(shader.is_null());
  746. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  747. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[mode]);
  748. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  749. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
  750. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
  751. 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
  752. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
  753. RD::get_singleton()->compute_list_end();
  754. }
  755. void CopyEffects::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {
  756. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");
  757. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");
  758. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  759. ERR_FAIL_NULL(uniform_set_cache);
  760. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  761. ERR_FAIL_NULL(material_storage);
  762. // TODO implement!
  763. CubemapFilterRasterPushConstant push_constant;
  764. push_constant.mip_level = p_mip_level;
  765. push_constant.face_id = p_face_id;
  766. // setup our uniforms
  767. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  768. RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
  769. CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;
  770. RID shader = filter.raster_shader.version_get_shader(filter.shader_version, mode);
  771. ERR_FAIL_COND(shader.is_null());
  772. 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);
  773. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, filter.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  774. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
  775. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
  776. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  777. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));
  778. RD::get_singleton()->draw_list_draw(draw_list, true);
  779. RD::get_singleton()->draw_list_end();
  780. }
  781. void CopyEffects::cubemap_roughness(RID p_source_rd_texture, RID p_dest_texture, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
  782. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");
  783. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  784. ERR_FAIL_NULL(uniform_set_cache);
  785. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  786. ERR_FAIL_NULL(material_storage);
  787. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  788. roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
  789. roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
  790. roughness.push_constant.sample_count = p_sample_count;
  791. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  792. roughness.push_constant.face_size = p_size;
  793. // setup our uniforms
  794. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  795. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_rd_texture }));
  796. RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_texture }));
  797. RID shader = roughness.compute_shader.version_get_shader(filter.shader_version, 0);
  798. ERR_FAIL_COND(shader.is_null());
  799. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  800. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);
  801. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  802. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_texture), 1);
  803. RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  804. int x_groups = (p_size - 1) / 8 + 1;
  805. int y_groups = (p_size - 1) / 8 + 1;
  806. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
  807. RD::get_singleton()->compute_list_end();
  808. }
  809. void CopyEffects::cubemap_roughness_raster(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) {
  810. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");
  811. ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");
  812. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  813. ERR_FAIL_NULL(uniform_set_cache);
  814. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  815. ERR_FAIL_NULL(material_storage);
  816. memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
  817. roughness.push_constant.face_id = p_face_id;
  818. roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
  819. roughness.push_constant.sample_count = p_sample_count;
  820. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  821. roughness.push_constant.face_size = p_size;
  822. // setup our uniforms
  823. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  824. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  825. RID shader = roughness.raster_shader.version_get_shader(filter.shader_version, 0);
  826. ERR_FAIL_COND(shader.is_null());
  827. 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);
  828. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, roughness.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  829. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  830. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  831. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  832. RD::get_singleton()->draw_list_draw(draw_list, true);
  833. RD::get_singleton()->draw_list_end();
  834. }