fog.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /**************************************************************************/
  2. /* fog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "fog.h"
  31. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  33. #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
  34. #include "servers/rendering/rendering_server_default.h"
  35. using namespace RendererRD;
  36. Fog *Fog::singleton = nullptr;
  37. Fog::Fog() {
  38. singleton = this;
  39. }
  40. Fog::~Fog() {
  41. singleton = nullptr;
  42. }
  43. /* FOG VOLUMES */
  44. RID Fog::fog_volume_allocate() {
  45. return fog_volume_owner.allocate_rid();
  46. }
  47. void Fog::fog_volume_initialize(RID p_rid) {
  48. fog_volume_owner.initialize_rid(p_rid, FogVolume());
  49. }
  50. void Fog::fog_volume_free(RID p_rid) {
  51. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_rid);
  52. fog_volume->dependency.deleted_notify(p_rid);
  53. fog_volume_owner.free(p_rid);
  54. }
  55. Dependency *Fog::fog_volume_get_dependency(RID p_fog_volume) const {
  56. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  57. ERR_FAIL_NULL_V(fog_volume, nullptr);
  58. return &fog_volume->dependency;
  59. }
  60. void Fog::fog_volume_set_shape(RID p_fog_volume, RS::FogVolumeShape p_shape) {
  61. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  62. ERR_FAIL_COND(!fog_volume);
  63. if (p_shape == fog_volume->shape) {
  64. return;
  65. }
  66. fog_volume->shape = p_shape;
  67. fog_volume->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  68. }
  69. void Fog::fog_volume_set_size(RID p_fog_volume, const Vector3 &p_size) {
  70. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  71. ERR_FAIL_COND(!fog_volume);
  72. fog_volume->size = p_size;
  73. fog_volume->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  74. }
  75. void Fog::fog_volume_set_material(RID p_fog_volume, RID p_material) {
  76. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  77. ERR_FAIL_COND(!fog_volume);
  78. fog_volume->material = p_material;
  79. }
  80. RID Fog::fog_volume_get_material(RID p_fog_volume) const {
  81. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  82. ERR_FAIL_COND_V(!fog_volume, RID());
  83. return fog_volume->material;
  84. }
  85. RS::FogVolumeShape Fog::fog_volume_get_shape(RID p_fog_volume) const {
  86. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  87. ERR_FAIL_COND_V(!fog_volume, RS::FOG_VOLUME_SHAPE_BOX);
  88. return fog_volume->shape;
  89. }
  90. AABB Fog::fog_volume_get_aabb(RID p_fog_volume) const {
  91. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  92. ERR_FAIL_COND_V(!fog_volume, AABB());
  93. switch (fog_volume->shape) {
  94. case RS::FOG_VOLUME_SHAPE_ELLIPSOID:
  95. case RS::FOG_VOLUME_SHAPE_CONE:
  96. case RS::FOG_VOLUME_SHAPE_CYLINDER:
  97. case RS::FOG_VOLUME_SHAPE_BOX: {
  98. AABB aabb;
  99. aabb.position = -fog_volume->size / 2;
  100. aabb.size = fog_volume->size;
  101. return aabb;
  102. }
  103. default: {
  104. // Need some size otherwise will get culled
  105. return AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  106. }
  107. }
  108. }
  109. Vector3 Fog::fog_volume_get_size(RID p_fog_volume) const {
  110. const FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  111. ERR_FAIL_COND_V(!fog_volume, Vector3());
  112. return fog_volume->size;
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////
  115. // Fog material
  116. bool Fog::FogMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  117. uniform_set_updated = true;
  118. return update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set, Fog::get_singleton()->volumetric_fog.shader.version_get_shader(shader_data->version, 0), VolumetricFogShader::FogSet::FOG_SET_MATERIAL, true);
  119. }
  120. Fog::FogMaterialData::~FogMaterialData() {
  121. free_parameters_uniform_set(uniform_set);
  122. }
  123. RendererRD::MaterialStorage::ShaderData *Fog::_create_fog_shader_func() {
  124. FogShaderData *shader_data = memnew(FogShaderData);
  125. return shader_data;
  126. }
  127. RendererRD::MaterialStorage::ShaderData *Fog::_create_fog_shader_funcs() {
  128. return Fog::get_singleton()->_create_fog_shader_func();
  129. };
  130. RendererRD::MaterialStorage::MaterialData *Fog::_create_fog_material_func(FogShaderData *p_shader) {
  131. FogMaterialData *material_data = memnew(FogMaterialData);
  132. material_data->shader_data = p_shader;
  133. //update will happen later anyway so do nothing.
  134. return material_data;
  135. }
  136. RendererRD::MaterialStorage::MaterialData *Fog::_create_fog_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
  137. return Fog::get_singleton()->_create_fog_material_func(static_cast<FogShaderData *>(p_shader));
  138. };
  139. ////////////////////////////////////////////////////////////////////////////////
  140. // FOG VOLUMES INSTANCE
  141. RID Fog::fog_volume_instance_create(RID p_fog_volume) {
  142. FogVolumeInstance fvi;
  143. fvi.volume = p_fog_volume;
  144. return fog_volume_instance_owner.make_rid(fvi);
  145. }
  146. void Fog::fog_instance_free(RID p_rid) {
  147. fog_volume_instance_owner.free(p_rid);
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////
  150. // Volumetric Fog Shader
  151. void Fog::init_fog_shader(uint32_t p_max_directional_lights, int p_roughness_layers, bool p_is_using_radiance_cubemap_array) {
  152. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  153. {
  154. // Initialize local fog shader
  155. Vector<String> volumetric_fog_modes;
  156. volumetric_fog_modes.push_back("");
  157. volumetric_fog.shader.initialize(volumetric_fog_modes);
  158. material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_FOG, _create_fog_shader_funcs);
  159. material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_FOG, _create_fog_material_funcs);
  160. volumetric_fog.volume_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(VolumetricFogShader::VolumeUBO));
  161. }
  162. {
  163. ShaderCompiler::DefaultIdentifierActions actions;
  164. actions.renames["TIME"] = "scene_params.time";
  165. actions.renames["PI"] = _MKSTR(Math_PI);
  166. actions.renames["TAU"] = _MKSTR(Math_TAU);
  167. actions.renames["E"] = _MKSTR(Math_E);
  168. actions.renames["WORLD_POSITION"] = "world.xyz";
  169. actions.renames["OBJECT_POSITION"] = "params.position";
  170. actions.renames["UVW"] = "uvw";
  171. actions.renames["SIZE"] = "params.size";
  172. actions.renames["ALBEDO"] = "albedo";
  173. actions.renames["DENSITY"] = "density";
  174. actions.renames["EMISSION"] = "emission";
  175. actions.renames["SDF"] = "sdf";
  176. actions.usage_defines["SDF"] = "#define SDF_USED\n";
  177. actions.usage_defines["DENSITY"] = "#define DENSITY_USED\n";
  178. actions.usage_defines["ALBEDO"] = "#define ALBEDO_USED\n";
  179. actions.usage_defines["EMISSION"] = "#define EMISSION_USED\n";
  180. actions.sampler_array_name = "material_samplers";
  181. actions.base_texture_binding_index = 1;
  182. actions.texture_layout_set = VolumetricFogShader::FogSet::FOG_SET_MATERIAL;
  183. actions.base_uniform_string = "material.";
  184. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  185. actions.default_repeat = ShaderLanguage::REPEAT_DISABLE;
  186. actions.global_buffer_array_variable = "global_variables.data";
  187. volumetric_fog.compiler.initialize(actions);
  188. }
  189. {
  190. // default material and shader for fog shader
  191. volumetric_fog.default_shader = material_storage->shader_allocate();
  192. material_storage->shader_initialize(volumetric_fog.default_shader);
  193. material_storage->shader_set_code(volumetric_fog.default_shader, R"(
  194. // Default fog shader.
  195. shader_type fog;
  196. void fog() {
  197. DENSITY = 1.0;
  198. ALBEDO = vec3(1.0);
  199. }
  200. )");
  201. volumetric_fog.default_material = material_storage->material_allocate();
  202. material_storage->material_initialize(volumetric_fog.default_material);
  203. material_storage->material_set_shader(volumetric_fog.default_material, volumetric_fog.default_shader);
  204. FogMaterialData *md = static_cast<FogMaterialData *>(material_storage->material_get_data(volumetric_fog.default_material, RendererRD::MaterialStorage::SHADER_TYPE_FOG));
  205. volumetric_fog.default_shader_rd = volumetric_fog.shader.version_get_shader(md->shader_data->version, 0);
  206. Vector<RD::Uniform> uniforms;
  207. {
  208. Vector<RID> ids;
  209. ids.resize(12);
  210. RID *ids_ptr = ids.ptrw();
  211. ids_ptr[0] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  212. ids_ptr[1] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  213. ids_ptr[2] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  214. ids_ptr[3] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  215. ids_ptr[4] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  216. ids_ptr[5] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  217. ids_ptr[6] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  218. ids_ptr[7] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  219. ids_ptr[8] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  220. ids_ptr[9] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  221. ids_ptr[10] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  222. ids_ptr[11] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  223. RD::Uniform u(RD::UNIFORM_TYPE_SAMPLER, 1, ids);
  224. uniforms.push_back(u);
  225. }
  226. {
  227. RD::Uniform u;
  228. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  229. u.binding = 2;
  230. u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer());
  231. uniforms.push_back(u);
  232. }
  233. volumetric_fog.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.default_shader_rd, VolumetricFogShader::FogSet::FOG_SET_BASE);
  234. }
  235. {
  236. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(p_max_directional_lights) + "\n";
  237. defines += "\n#define MAX_SKY_LOD " + itos(p_roughness_layers - 1) + ".0\n";
  238. if (p_is_using_radiance_cubemap_array) {
  239. defines += "\n#define USE_RADIANCE_CUBEMAP_ARRAY \n";
  240. }
  241. Vector<String> volumetric_fog_modes;
  242. volumetric_fog_modes.push_back("\n#define MODE_DENSITY\n");
  243. volumetric_fog_modes.push_back("\n#define MODE_DENSITY\n#define ENABLE_SDFGI\n");
  244. volumetric_fog_modes.push_back("\n#define MODE_FILTER\n");
  245. volumetric_fog_modes.push_back("\n#define MODE_FOG\n");
  246. volumetric_fog_modes.push_back("\n#define MODE_COPY\n");
  247. volumetric_fog.process_shader.initialize(volumetric_fog_modes, defines);
  248. volumetric_fog.process_shader_version = volumetric_fog.process_shader.version_create();
  249. for (int i = 0; i < VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_MAX; i++) {
  250. volumetric_fog.process_pipelines[i] = RD::get_singleton()->compute_pipeline_create(volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, i));
  251. }
  252. volumetric_fog.params_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(VolumetricFogShader::ParamsUBO));
  253. }
  254. }
  255. void Fog::free_fog_shader() {
  256. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  257. volumetric_fog.process_shader.version_free(volumetric_fog.process_shader_version);
  258. RD::get_singleton()->free(volumetric_fog.volume_ubo);
  259. RD::get_singleton()->free(volumetric_fog.params_ubo);
  260. material_storage->shader_free(volumetric_fog.default_shader);
  261. material_storage->material_free(volumetric_fog.default_material);
  262. }
  263. void Fog::FogShaderData::set_code(const String &p_code) {
  264. //compile
  265. code = p_code;
  266. valid = false;
  267. ubo_size = 0;
  268. uniforms.clear();
  269. if (code.is_empty()) {
  270. return; //just invalid, but no error
  271. }
  272. ShaderCompiler::GeneratedCode gen_code;
  273. ShaderCompiler::IdentifierActions actions;
  274. actions.entry_point_stages["fog"] = ShaderCompiler::STAGE_COMPUTE;
  275. uses_time = false;
  276. actions.usage_flag_pointers["TIME"] = &uses_time;
  277. actions.uniforms = &uniforms;
  278. Fog *fog_singleton = Fog::get_singleton();
  279. Error err = fog_singleton->volumetric_fog.compiler.compile(RS::SHADER_FOG, code, &actions, path, gen_code);
  280. ERR_FAIL_COND_MSG(err != OK, "Fog shader compilation failed.");
  281. if (version.is_null()) {
  282. version = fog_singleton->volumetric_fog.shader.version_create();
  283. }
  284. fog_singleton->volumetric_fog.shader.version_set_compute_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_COMPUTE], gen_code.defines);
  285. ERR_FAIL_COND(!fog_singleton->volumetric_fog.shader.version_is_valid(version));
  286. ubo_size = gen_code.uniform_total_size;
  287. ubo_offsets = gen_code.uniform_offsets;
  288. texture_uniforms = gen_code.texture_uniforms;
  289. pipeline = RD::get_singleton()->compute_pipeline_create(fog_singleton->volumetric_fog.shader.version_get_shader(version, 0));
  290. valid = true;
  291. }
  292. bool Fog::FogShaderData::is_animated() const {
  293. return false;
  294. }
  295. bool Fog::FogShaderData::casts_shadows() const {
  296. return false;
  297. }
  298. RS::ShaderNativeSourceCode Fog::FogShaderData::get_native_source_code() const {
  299. Fog *fog_singleton = Fog::get_singleton();
  300. return fog_singleton->volumetric_fog.shader.version_get_native_source_code(version);
  301. }
  302. Fog::FogShaderData::~FogShaderData() {
  303. Fog *fog_singleton = Fog::get_singleton();
  304. ERR_FAIL_COND(!fog_singleton);
  305. //pipeline variants will clear themselves if shader is gone
  306. if (version.is_valid()) {
  307. fog_singleton->volumetric_fog.shader.version_free(version);
  308. }
  309. }
  310. ////////////////////////////////////////////////////////////////////////////////
  311. // Volumetric Fog
  312. void Fog::VolumetricFog::init(const Vector3i &fog_size, RID p_sky_shader) {
  313. width = fog_size.x;
  314. height = fog_size.y;
  315. depth = fog_size.z;
  316. RD::TextureFormat tf;
  317. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  318. tf.width = fog_size.x;
  319. tf.height = fog_size.y;
  320. tf.depth = fog_size.z;
  321. tf.texture_type = RD::TEXTURE_TYPE_3D;
  322. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  323. light_density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  324. RD::get_singleton()->set_resource_name(light_density_map, "Fog light-density map");
  325. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  326. prev_light_density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  327. RD::get_singleton()->set_resource_name(prev_light_density_map, "Fog previous light-density map");
  328. RD::get_singleton()->texture_clear(prev_light_density_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  329. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  330. fog_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  331. RD::get_singleton()->set_resource_name(fog_map, "Fog map");
  332. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  333. Vector<uint8_t> dm;
  334. dm.resize(fog_size.x * fog_size.y * fog_size.z * 4);
  335. dm.fill(0);
  336. density_map = RD::get_singleton()->storage_buffer_create(dm.size(), dm);
  337. RD::get_singleton()->set_resource_name(density_map, "Fog density map");
  338. light_map = RD::get_singleton()->storage_buffer_create(dm.size(), dm);
  339. RD::get_singleton()->set_resource_name(light_map, "Fog light map");
  340. emissive_map = RD::get_singleton()->storage_buffer_create(dm.size(), dm);
  341. RD::get_singleton()->set_resource_name(emissive_map, "Fog emissive map");
  342. #else
  343. tf.format = RD::DATA_FORMAT_R32_UINT;
  344. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  345. density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  346. RD::get_singleton()->set_resource_name(density_map, "Fog density map");
  347. RD::get_singleton()->texture_clear(density_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  348. light_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  349. RD::get_singleton()->set_resource_name(light_map, "Fog light map");
  350. RD::get_singleton()->texture_clear(light_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  351. emissive_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  352. RD::get_singleton()->set_resource_name(emissive_map, "Fog emissive map");
  353. RD::get_singleton()->texture_clear(emissive_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  354. #endif
  355. Vector<RD::Uniform> uniforms;
  356. {
  357. RD::Uniform u;
  358. u.binding = 0;
  359. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  360. u.append_id(fog_map);
  361. uniforms.push_back(u);
  362. }
  363. sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_sky_shader, RendererRD::SkyRD::SKY_SET_FOG);
  364. }
  365. Fog::VolumetricFog::~VolumetricFog() {
  366. RD::get_singleton()->free(prev_light_density_map);
  367. RD::get_singleton()->free(light_density_map);
  368. RD::get_singleton()->free(fog_map);
  369. RD::get_singleton()->free(density_map);
  370. RD::get_singleton()->free(light_map);
  371. RD::get_singleton()->free(emissive_map);
  372. if (fog_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(fog_uniform_set)) {
  373. RD::get_singleton()->free(fog_uniform_set);
  374. }
  375. // At this point, due to cascade deletions, the sets may no longer be valid, but still they must work as a group.
  376. gi_dependent_sets.valid = RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set_density);
  377. #ifdef DEV_ENABLED
  378. gi_dependent_sets.assert_actual_validity();
  379. #endif
  380. if (gi_dependent_sets.valid) {
  381. RD::get_singleton()->free(gi_dependent_sets.copy_uniform_set);
  382. RD::get_singleton()->free(gi_dependent_sets.process_uniform_set_density);
  383. RD::get_singleton()->free(gi_dependent_sets.process_uniform_set);
  384. RD::get_singleton()->free(gi_dependent_sets.process_uniform_set2);
  385. }
  386. if (sdfgi_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sdfgi_uniform_set)) {
  387. RD::get_singleton()->free(sdfgi_uniform_set);
  388. }
  389. if (sky_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_uniform_set)) {
  390. RD::get_singleton()->free(sky_uniform_set);
  391. }
  392. }
  393. Vector3i Fog::_point_get_position_in_froxel_volume(const Vector3 &p_point, float fog_end, const Vector2 &fog_near_size, const Vector2 &fog_far_size, float volumetric_fog_detail_spread, const Vector3 &fog_size, const Transform3D &p_cam_transform) {
  394. Vector3 view_position = p_cam_transform.affine_inverse().xform(p_point);
  395. view_position.z = MIN(view_position.z, -0.01); // Clamp to the front of camera
  396. Vector3 fog_position = Vector3(0, 0, 0);
  397. view_position.y = -view_position.y;
  398. fog_position.z = -view_position.z / fog_end;
  399. fog_position.x = (view_position.x / (2 * (fog_near_size.x * (1.0 - fog_position.z) + fog_far_size.x * fog_position.z))) + 0.5;
  400. fog_position.y = (view_position.y / (2 * (fog_near_size.y * (1.0 - fog_position.z) + fog_far_size.y * fog_position.z))) + 0.5;
  401. fog_position.z = Math::pow(float(fog_position.z), float(1.0 / volumetric_fog_detail_spread));
  402. fog_position = fog_position * fog_size - Vector3(0.5, 0.5, 0.5);
  403. fog_position.x = CLAMP(fog_position.x, 0.0, fog_size.x);
  404. fog_position.y = CLAMP(fog_position.y, 0.0, fog_size.y);
  405. fog_position.z = CLAMP(fog_position.z, 0.0, fog_size.z);
  406. return Vector3i(fog_position);
  407. }
  408. void Fog::volumetric_fog_update(const VolumetricFogSettings &p_settings, const Projection &p_cam_projection, const Transform3D &p_cam_transform, const Transform3D &p_prev_cam_inv_transform, RID p_shadow_atlas, int p_directional_light_count, bool p_use_directional_shadows, int p_positional_light_count, int p_voxel_gi_count, const PagedArray<RID> &p_fog_volumes) {
  409. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  410. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  411. RENDER_TIMESTAMP("> Volumetric Fog");
  412. RD::get_singleton()->draw_command_begin_label("Volumetric Fog");
  413. Ref<VolumetricFog> fog = p_settings.vfog;
  414. if (p_fog_volumes.size() > 0) {
  415. RD::get_singleton()->draw_command_begin_label("Render Volumetric Fog Volumes");
  416. RENDER_TIMESTAMP("Render FogVolumes");
  417. VolumetricFogShader::VolumeUBO params;
  418. Vector2 frustum_near_size = p_cam_projection.get_viewport_half_extents();
  419. Vector2 frustum_far_size = p_cam_projection.get_far_plane_half_extents();
  420. float z_near = p_cam_projection.get_z_near();
  421. float z_far = p_cam_projection.get_z_far();
  422. float fog_end = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_length(p_settings.env);
  423. Vector2 fog_far_size = frustum_near_size.lerp(frustum_far_size, (fog_end - z_near) / (z_far - z_near));
  424. Vector2 fog_near_size;
  425. if (p_cam_projection.is_orthogonal()) {
  426. fog_near_size = fog_far_size;
  427. } else {
  428. fog_near_size = Vector2();
  429. }
  430. params.fog_frustum_size_begin[0] = fog_near_size.x;
  431. params.fog_frustum_size_begin[1] = fog_near_size.y;
  432. params.fog_frustum_size_end[0] = fog_far_size.x;
  433. params.fog_frustum_size_end[1] = fog_far_size.y;
  434. params.fog_frustum_end = fog_end;
  435. params.z_near = z_near;
  436. params.z_far = z_far;
  437. params.time = p_settings.time;
  438. params.fog_volume_size[0] = fog->width;
  439. params.fog_volume_size[1] = fog->height;
  440. params.fog_volume_size[2] = fog->depth;
  441. params.use_temporal_reprojection = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env);
  442. params.temporal_frame = RSG::rasterizer->get_frame_number() % VolumetricFog::MAX_TEMPORAL_FRAMES;
  443. params.detail_spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  444. params.temporal_blend = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection_amount(p_settings.env);
  445. Transform3D to_prev_cam_view = p_prev_cam_inv_transform * p_cam_transform;
  446. RendererRD::MaterialStorage::store_transform(to_prev_cam_view, params.to_prev_view);
  447. RendererRD::MaterialStorage::store_transform(p_cam_transform, params.transform);
  448. RD::get_singleton()->buffer_update(volumetric_fog.volume_ubo, 0, sizeof(VolumetricFogShader::VolumeUBO), &params, RD::BARRIER_MASK_COMPUTE);
  449. if (fog->fog_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fog->fog_uniform_set)) {
  450. Vector<RD::Uniform> uniforms;
  451. {
  452. RD::Uniform u;
  453. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  454. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  455. #else
  456. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  457. #endif
  458. u.binding = 1;
  459. u.append_id(fog->emissive_map);
  460. uniforms.push_back(u);
  461. }
  462. {
  463. RD::Uniform u;
  464. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  465. u.binding = 2;
  466. u.append_id(volumetric_fog.volume_ubo);
  467. uniforms.push_back(u);
  468. }
  469. {
  470. RD::Uniform u;
  471. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  472. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  473. #else
  474. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  475. #endif
  476. u.binding = 3;
  477. u.append_id(fog->density_map);
  478. uniforms.push_back(u);
  479. }
  480. {
  481. RD::Uniform u;
  482. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  483. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  484. #else
  485. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  486. #endif
  487. u.binding = 4;
  488. u.append_id(fog->light_map);
  489. uniforms.push_back(u);
  490. }
  491. fog->fog_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.default_shader_rd, VolumetricFogShader::FogSet::FOG_SET_UNIFORMS);
  492. }
  493. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  494. bool any_uses_time = false;
  495. for (int i = 0; i < (int)p_fog_volumes.size(); i++) {
  496. FogVolumeInstance *fog_volume_instance = fog_volume_instance_owner.get_or_null(p_fog_volumes[i]);
  497. ERR_FAIL_COND(!fog_volume_instance);
  498. RID fog_volume = fog_volume_instance->volume;
  499. RID fog_material = RendererRD::Fog::get_singleton()->fog_volume_get_material(fog_volume);
  500. FogMaterialData *material = nullptr;
  501. if (fog_material.is_valid()) {
  502. material = static_cast<FogMaterialData *>(material_storage->material_get_data(fog_material, RendererRD::MaterialStorage::SHADER_TYPE_FOG));
  503. if (!material || !material->shader_data->valid) {
  504. material = nullptr;
  505. }
  506. }
  507. if (!material) {
  508. fog_material = volumetric_fog.default_material;
  509. material = static_cast<FogMaterialData *>(material_storage->material_get_data(fog_material, RendererRD::MaterialStorage::SHADER_TYPE_FOG));
  510. }
  511. ERR_FAIL_COND(!material);
  512. FogShaderData *shader_data = material->shader_data;
  513. ERR_FAIL_COND(!shader_data);
  514. any_uses_time |= shader_data->uses_time;
  515. Vector3i min;
  516. Vector3i max;
  517. Vector3i kernel_size;
  518. Vector3 position = fog_volume_instance->transform.get_origin();
  519. RS::FogVolumeShape volume_type = RendererRD::Fog::get_singleton()->fog_volume_get_shape(fog_volume);
  520. Vector3 extents = RendererRD::Fog::get_singleton()->fog_volume_get_size(fog_volume) / 2;
  521. if (volume_type != RS::FOG_VOLUME_SHAPE_WORLD) {
  522. // Local fog volume.
  523. Vector3i points[8];
  524. Vector3 fog_size = Vector3(fog->width, fog->height, fog->depth);
  525. float volumetric_fog_detail_spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  526. points[0] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(extents.x, extents.y, extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  527. points[1] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(-extents.x, extents.y, extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  528. points[2] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(extents.x, -extents.y, extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  529. points[3] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(-extents.x, -extents.y, extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  530. points[4] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(extents.x, extents.y, -extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  531. points[5] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(-extents.x, extents.y, -extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  532. points[6] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(extents.x, -extents.y, -extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  533. points[7] = _point_get_position_in_froxel_volume(fog_volume_instance->transform.xform(Vector3(-extents.x, -extents.y, -extents.z)), fog_end, fog_near_size, fog_far_size, volumetric_fog_detail_spread, fog_size, p_cam_transform);
  534. min = Vector3i(int32_t(fog->width) - 1, int32_t(fog->height) - 1, int32_t(fog->depth) - 1);
  535. max = Vector3i(1, 1, 1);
  536. for (int j = 0; j < 8; j++) {
  537. min = Vector3i(MIN(min.x, points[j].x), MIN(min.y, points[j].y), MIN(min.z, points[j].z));
  538. max = Vector3i(MAX(max.x, points[j].x), MAX(max.y, points[j].y), MAX(max.z, points[j].z));
  539. }
  540. kernel_size = max - min;
  541. } else {
  542. // Volume type global runs on all cells
  543. extents = Vector3(fog->width, fog->height, fog->depth);
  544. min = Vector3i(0, 0, 0);
  545. kernel_size = Vector3i(int32_t(fog->width), int32_t(fog->height), int32_t(fog->depth));
  546. }
  547. if (kernel_size.x == 0 || kernel_size.y == 0 || kernel_size.z == 0) {
  548. continue;
  549. }
  550. VolumetricFogShader::FogPushConstant push_constant;
  551. push_constant.position[0] = position.x;
  552. push_constant.position[1] = position.y;
  553. push_constant.position[2] = position.z;
  554. push_constant.size[0] = extents.x * 2;
  555. push_constant.size[1] = extents.y * 2;
  556. push_constant.size[2] = extents.z * 2;
  557. push_constant.corner[0] = min.x;
  558. push_constant.corner[1] = min.y;
  559. push_constant.corner[2] = min.z;
  560. push_constant.shape = uint32_t(RendererRD::Fog::get_singleton()->fog_volume_get_shape(fog_volume));
  561. RendererRD::MaterialStorage::store_transform(fog_volume_instance->transform.affine_inverse(), push_constant.transform);
  562. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shader_data->pipeline);
  563. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->fog_uniform_set, VolumetricFogShader::FogSet::FOG_SET_UNIFORMS);
  564. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VolumetricFogShader::FogPushConstant));
  565. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, volumetric_fog.base_uniform_set, VolumetricFogShader::FogSet::FOG_SET_BASE);
  566. if (material->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(material->uniform_set)) { // Material may not have a uniform set.
  567. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, material->uniform_set, VolumetricFogShader::FogSet::FOG_SET_MATERIAL);
  568. material->set_as_used();
  569. }
  570. RD::get_singleton()->compute_list_dispatch_threads(compute_list, kernel_size.x, kernel_size.y, kernel_size.z);
  571. }
  572. if (any_uses_time || RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env)) {
  573. RenderingServerDefault::redraw_request();
  574. }
  575. RD::get_singleton()->draw_command_end_label();
  576. RD::get_singleton()->compute_list_end();
  577. }
  578. #ifdef DEV_ENABLED
  579. fog->gi_dependent_sets.assert_actual_validity();
  580. #endif
  581. if (!fog->gi_dependent_sets.valid) {
  582. //re create uniform set if needed
  583. Vector<RD::Uniform> uniforms;
  584. Vector<RD::Uniform> copy_uniforms;
  585. {
  586. RD::Uniform u;
  587. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  588. u.binding = 1;
  589. if (p_settings.shadow_atlas_depth.is_null()) {
  590. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK));
  591. } else {
  592. u.append_id(p_settings.shadow_atlas_depth);
  593. }
  594. uniforms.push_back(u);
  595. copy_uniforms.push_back(u);
  596. }
  597. {
  598. RD::Uniform u;
  599. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  600. u.binding = 2;
  601. if (p_settings.directional_shadow_depth.is_valid()) {
  602. u.append_id(p_settings.directional_shadow_depth);
  603. } else {
  604. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK));
  605. }
  606. uniforms.push_back(u);
  607. copy_uniforms.push_back(u);
  608. }
  609. {
  610. RD::Uniform u;
  611. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  612. u.binding = 3;
  613. u.append_id(p_settings.omni_light_buffer);
  614. uniforms.push_back(u);
  615. copy_uniforms.push_back(u);
  616. }
  617. {
  618. RD::Uniform u;
  619. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  620. u.binding = 4;
  621. u.append_id(p_settings.spot_light_buffer);
  622. uniforms.push_back(u);
  623. copy_uniforms.push_back(u);
  624. }
  625. {
  626. RD::Uniform u;
  627. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  628. u.binding = 5;
  629. u.append_id(p_settings.directional_light_buffer);
  630. uniforms.push_back(u);
  631. copy_uniforms.push_back(u);
  632. }
  633. {
  634. RD::Uniform u;
  635. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  636. u.binding = 6;
  637. u.append_id(p_settings.cluster_builder->get_cluster_buffer());
  638. uniforms.push_back(u);
  639. copy_uniforms.push_back(u);
  640. }
  641. {
  642. RD::Uniform u;
  643. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  644. u.binding = 7;
  645. u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  646. uniforms.push_back(u);
  647. copy_uniforms.push_back(u);
  648. }
  649. {
  650. RD::Uniform u;
  651. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  652. u.binding = 8;
  653. u.append_id(fog->light_density_map);
  654. uniforms.push_back(u);
  655. copy_uniforms.push_back(u);
  656. }
  657. {
  658. RD::Uniform u;
  659. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  660. u.binding = 9;
  661. u.append_id(fog->fog_map);
  662. uniforms.push_back(u);
  663. }
  664. {
  665. RD::Uniform u;
  666. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  667. u.binding = 9;
  668. u.append_id(fog->prev_light_density_map);
  669. copy_uniforms.push_back(u);
  670. }
  671. {
  672. RD::Uniform u;
  673. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  674. u.binding = 10;
  675. u.append_id(p_settings.shadow_sampler);
  676. uniforms.push_back(u);
  677. copy_uniforms.push_back(u);
  678. }
  679. {
  680. RD::Uniform u;
  681. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  682. u.binding = 11;
  683. u.append_id(p_settings.voxel_gi_buffer);
  684. uniforms.push_back(u);
  685. copy_uniforms.push_back(u);
  686. }
  687. {
  688. RD::Uniform u;
  689. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  690. u.binding = 12;
  691. for (int i = 0; i < RendererRD::GI::MAX_VOXEL_GI_INSTANCES; i++) {
  692. u.append_id(p_settings.rbgi->voxel_gi_textures[i]);
  693. }
  694. uniforms.push_back(u);
  695. copy_uniforms.push_back(u);
  696. }
  697. {
  698. RD::Uniform u;
  699. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  700. u.binding = 13;
  701. u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  702. uniforms.push_back(u);
  703. copy_uniforms.push_back(u);
  704. }
  705. {
  706. RD::Uniform u;
  707. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  708. u.binding = 14;
  709. u.append_id(volumetric_fog.params_ubo);
  710. uniforms.push_back(u);
  711. copy_uniforms.push_back(u);
  712. }
  713. {
  714. RD::Uniform u;
  715. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  716. u.binding = 15;
  717. u.append_id(fog->prev_light_density_map);
  718. uniforms.push_back(u);
  719. }
  720. {
  721. RD::Uniform u;
  722. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  723. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  724. #else
  725. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  726. #endif
  727. u.binding = 16;
  728. u.append_id(fog->density_map);
  729. uniforms.push_back(u);
  730. }
  731. {
  732. RD::Uniform u;
  733. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  734. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  735. #else
  736. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  737. #endif
  738. u.binding = 17;
  739. u.append_id(fog->light_map);
  740. uniforms.push_back(u);
  741. }
  742. {
  743. RD::Uniform u;
  744. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  745. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  746. #else
  747. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  748. #endif
  749. u.binding = 18;
  750. u.append_id(fog->emissive_map);
  751. uniforms.push_back(u);
  752. }
  753. {
  754. RD::Uniform u;
  755. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  756. u.binding = 19;
  757. RID radiance_texture = texture_storage->texture_rd_get_default(p_settings.is_using_radiance_cubemap_array ? RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK : RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK);
  758. RID sky_texture = RendererSceneRenderRD::get_singleton()->environment_get_sky(p_settings.env).is_valid() ? p_settings.sky->sky_get_radiance_texture_rd(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_settings.env)) : RID();
  759. u.append_id(sky_texture.is_valid() ? sky_texture : radiance_texture);
  760. uniforms.push_back(u);
  761. }
  762. fog->gi_dependent_sets.copy_uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_COPY), 0);
  763. fog->gi_dependent_sets.process_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FOG), 0);
  764. RID aux7 = uniforms.write[7].get_id(0);
  765. RID aux8 = uniforms.write[8].get_id(0);
  766. uniforms.write[7].set_id(0, aux8);
  767. uniforms.write[8].set_id(0, aux7);
  768. fog->gi_dependent_sets.process_uniform_set2 = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FOG), 0);
  769. uniforms.remove_at(8);
  770. uniforms.write[7].set_id(0, aux7);
  771. fog->gi_dependent_sets.process_uniform_set_density = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY), 0);
  772. fog->gi_dependent_sets.valid = true;
  773. }
  774. bool using_sdfgi = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_gi_inject(p_settings.env) > 0.0001 && RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_enabled(p_settings.env) && (p_settings.sdfgi.is_valid());
  775. if (using_sdfgi) {
  776. if (fog->sdfgi_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fog->sdfgi_uniform_set)) {
  777. Vector<RD::Uniform> uniforms;
  778. {
  779. RD::Uniform u;
  780. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  781. u.binding = 0;
  782. u.append_id(p_settings.gi->sdfgi_ubo);
  783. uniforms.push_back(u);
  784. }
  785. {
  786. RD::Uniform u;
  787. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  788. u.binding = 1;
  789. u.append_id(p_settings.sdfgi->ambient_texture);
  790. uniforms.push_back(u);
  791. }
  792. {
  793. RD::Uniform u;
  794. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  795. u.binding = 2;
  796. u.append_id(p_settings.sdfgi->occlusion_texture);
  797. uniforms.push_back(u);
  798. }
  799. fog->sdfgi_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY_WITH_SDFGI), 1);
  800. }
  801. }
  802. fog->length = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_length(p_settings.env);
  803. fog->spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  804. VolumetricFogShader::ParamsUBO params;
  805. Vector2 frustum_near_size = p_cam_projection.get_viewport_half_extents();
  806. Vector2 frustum_far_size = p_cam_projection.get_far_plane_half_extents();
  807. float z_near = p_cam_projection.get_z_near();
  808. float z_far = p_cam_projection.get_z_far();
  809. float fog_end = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_length(p_settings.env);
  810. Vector2 fog_far_size = frustum_near_size.lerp(frustum_far_size, (fog_end - z_near) / (z_far - z_near));
  811. Vector2 fog_near_size;
  812. if (p_cam_projection.is_orthogonal()) {
  813. fog_near_size = fog_far_size;
  814. } else {
  815. fog_near_size = Vector2();
  816. }
  817. params.fog_frustum_size_begin[0] = fog_near_size.x;
  818. params.fog_frustum_size_begin[1] = fog_near_size.y;
  819. params.fog_frustum_size_end[0] = fog_far_size.x;
  820. params.fog_frustum_size_end[1] = fog_far_size.y;
  821. params.ambient_inject = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_ambient_inject(p_settings.env) * RendererSceneRenderRD::get_singleton()->environment_get_ambient_light_energy(p_settings.env);
  822. params.z_far = z_far;
  823. params.fog_frustum_end = fog_end;
  824. Color ambient_color = RendererSceneRenderRD::get_singleton()->environment_get_ambient_light(p_settings.env).srgb_to_linear();
  825. params.ambient_color[0] = ambient_color.r;
  826. params.ambient_color[1] = ambient_color.g;
  827. params.ambient_color[2] = ambient_color.b;
  828. params.sky_contribution = RendererSceneRenderRD::get_singleton()->environment_get_ambient_sky_contribution(p_settings.env);
  829. params.fog_volume_size[0] = fog->width;
  830. params.fog_volume_size[1] = fog->height;
  831. params.fog_volume_size[2] = fog->depth;
  832. params.directional_light_count = p_directional_light_count;
  833. Color emission = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission(p_settings.env).srgb_to_linear();
  834. params.base_emission[0] = emission.r * RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission_energy(p_settings.env);
  835. params.base_emission[1] = emission.g * RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission_energy(p_settings.env);
  836. params.base_emission[2] = emission.b * RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission_energy(p_settings.env);
  837. params.base_density = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_density(p_settings.env);
  838. Color base_scattering = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_scattering(p_settings.env).srgb_to_linear();
  839. params.base_scattering[0] = base_scattering.r;
  840. params.base_scattering[1] = base_scattering.g;
  841. params.base_scattering[2] = base_scattering.b;
  842. params.phase_g = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_anisotropy(p_settings.env);
  843. params.detail_spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  844. params.gi_inject = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_gi_inject(p_settings.env);
  845. params.cam_rotation[0] = p_cam_transform.basis[0][0];
  846. params.cam_rotation[1] = p_cam_transform.basis[1][0];
  847. params.cam_rotation[2] = p_cam_transform.basis[2][0];
  848. params.cam_rotation[3] = 0;
  849. params.cam_rotation[4] = p_cam_transform.basis[0][1];
  850. params.cam_rotation[5] = p_cam_transform.basis[1][1];
  851. params.cam_rotation[6] = p_cam_transform.basis[2][1];
  852. params.cam_rotation[7] = 0;
  853. params.cam_rotation[8] = p_cam_transform.basis[0][2];
  854. params.cam_rotation[9] = p_cam_transform.basis[1][2];
  855. params.cam_rotation[10] = p_cam_transform.basis[2][2];
  856. params.cam_rotation[11] = 0;
  857. params.filter_axis = 0;
  858. params.max_voxel_gi_instances = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_gi_inject(p_settings.env) > 0.001 ? p_voxel_gi_count : 0;
  859. params.temporal_frame = RSG::rasterizer->get_frame_number() % VolumetricFog::MAX_TEMPORAL_FRAMES;
  860. Transform3D to_prev_cam_view = p_prev_cam_inv_transform * p_cam_transform;
  861. RendererRD::MaterialStorage::store_transform(to_prev_cam_view, params.to_prev_view);
  862. params.use_temporal_reprojection = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env);
  863. params.temporal_blend = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection_amount(p_settings.env);
  864. {
  865. uint32_t cluster_size = p_settings.cluster_builder->get_cluster_size();
  866. params.cluster_shift = get_shift_from_power_of_2(cluster_size);
  867. uint32_t cluster_screen_width = (p_settings.rb_size.x - 1) / cluster_size + 1;
  868. uint32_t cluster_screen_height = (p_settings.rb_size.y - 1) / cluster_size + 1;
  869. params.max_cluster_element_count_div_32 = p_settings.max_cluster_elements / 32;
  870. params.cluster_type_size = cluster_screen_width * cluster_screen_height * (params.max_cluster_element_count_div_32 + 32);
  871. params.cluster_width = cluster_screen_width;
  872. params.screen_size[0] = p_settings.rb_size.x;
  873. params.screen_size[1] = p_settings.rb_size.y;
  874. }
  875. Basis sky_transform = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_settings.env);
  876. sky_transform = sky_transform.inverse() * p_cam_transform.basis;
  877. RendererRD::MaterialStorage::store_transform_3x3(sky_transform, params.radiance_inverse_xform);
  878. RD::get_singleton()->draw_command_begin_label("Render Volumetric Fog");
  879. RENDER_TIMESTAMP("Render Fog");
  880. RD::get_singleton()->buffer_update(volumetric_fog.params_ubo, 0, sizeof(VolumetricFogShader::ParamsUBO), &params, RD::BARRIER_MASK_COMPUTE);
  881. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  882. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[using_sdfgi ? VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY_WITH_SDFGI : VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY]);
  883. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set_density, 0);
  884. if (using_sdfgi) {
  885. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->sdfgi_uniform_set, 1);
  886. }
  887. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  888. RD::get_singleton()->compute_list_add_barrier(compute_list);
  889. // Copy fog to history buffer
  890. if (RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env)) {
  891. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_COPY]);
  892. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.copy_uniform_set, 0);
  893. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  894. RD::get_singleton()->compute_list_add_barrier(compute_list);
  895. }
  896. RD::get_singleton()->draw_command_end_label();
  897. if (p_settings.volumetric_fog_filter_active) {
  898. RD::get_singleton()->draw_command_begin_label("Filter Fog");
  899. RENDER_TIMESTAMP("Filter Fog");
  900. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FILTER]);
  901. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set, 0);
  902. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  903. RD::get_singleton()->compute_list_end();
  904. //need restart for buffer update
  905. params.filter_axis = 1;
  906. RD::get_singleton()->buffer_update(volumetric_fog.params_ubo, 0, sizeof(VolumetricFogShader::ParamsUBO), &params);
  907. compute_list = RD::get_singleton()->compute_list_begin();
  908. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FILTER]);
  909. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set2, 0);
  910. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  911. RD::get_singleton()->compute_list_add_barrier(compute_list);
  912. RD::get_singleton()->draw_command_end_label();
  913. }
  914. RENDER_TIMESTAMP("Integrate Fog");
  915. RD::get_singleton()->draw_command_begin_label("Integrate Fog");
  916. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FOG]);
  917. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set, 0);
  918. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, 1);
  919. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_RASTER);
  920. RENDER_TIMESTAMP("< Volumetric Fog");
  921. RD::get_singleton()->draw_command_end_label();
  922. RD::get_singleton()->draw_command_end_label();
  923. }