fog.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. if (volumetric_fog.process_shader_version.is_valid()) {
  258. volumetric_fog.process_shader.version_free(volumetric_fog.process_shader_version);
  259. }
  260. if (volumetric_fog.volume_ubo.is_valid()) {
  261. RD::get_singleton()->free(volumetric_fog.volume_ubo);
  262. }
  263. if (volumetric_fog.params_ubo.is_valid()) {
  264. RD::get_singleton()->free(volumetric_fog.params_ubo);
  265. }
  266. if (volumetric_fog.default_shader.is_valid()) {
  267. material_storage->shader_free(volumetric_fog.default_shader);
  268. }
  269. if (volumetric_fog.default_material.is_valid()) {
  270. material_storage->material_free(volumetric_fog.default_material);
  271. }
  272. }
  273. void Fog::FogShaderData::set_code(const String &p_code) {
  274. //compile
  275. code = p_code;
  276. valid = false;
  277. ubo_size = 0;
  278. uniforms.clear();
  279. if (code.is_empty()) {
  280. return; //just invalid, but no error
  281. }
  282. ShaderCompiler::GeneratedCode gen_code;
  283. ShaderCompiler::IdentifierActions actions;
  284. actions.entry_point_stages["fog"] = ShaderCompiler::STAGE_COMPUTE;
  285. uses_time = false;
  286. actions.usage_flag_pointers["TIME"] = &uses_time;
  287. actions.uniforms = &uniforms;
  288. Fog *fog_singleton = Fog::get_singleton();
  289. Error err = fog_singleton->volumetric_fog.compiler.compile(RS::SHADER_FOG, code, &actions, path, gen_code);
  290. ERR_FAIL_COND_MSG(err != OK, "Fog shader compilation failed.");
  291. if (version.is_null()) {
  292. version = fog_singleton->volumetric_fog.shader.version_create();
  293. }
  294. 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);
  295. ERR_FAIL_COND(!fog_singleton->volumetric_fog.shader.version_is_valid(version));
  296. ubo_size = gen_code.uniform_total_size;
  297. ubo_offsets = gen_code.uniform_offsets;
  298. texture_uniforms = gen_code.texture_uniforms;
  299. pipeline = RD::get_singleton()->compute_pipeline_create(fog_singleton->volumetric_fog.shader.version_get_shader(version, 0));
  300. valid = true;
  301. }
  302. bool Fog::FogShaderData::is_animated() const {
  303. return false;
  304. }
  305. bool Fog::FogShaderData::casts_shadows() const {
  306. return false;
  307. }
  308. RS::ShaderNativeSourceCode Fog::FogShaderData::get_native_source_code() const {
  309. Fog *fog_singleton = Fog::get_singleton();
  310. return fog_singleton->volumetric_fog.shader.version_get_native_source_code(version);
  311. }
  312. Fog::FogShaderData::~FogShaderData() {
  313. Fog *fog_singleton = Fog::get_singleton();
  314. ERR_FAIL_COND(!fog_singleton);
  315. //pipeline variants will clear themselves if shader is gone
  316. if (version.is_valid()) {
  317. fog_singleton->volumetric_fog.shader.version_free(version);
  318. }
  319. }
  320. ////////////////////////////////////////////////////////////////////////////////
  321. // Volumetric Fog
  322. bool Fog::VolumetricFog::sync_gi_dependent_sets_validity(bool p_ensure_freed) {
  323. bool null = gi_dependent_sets.process_uniform_set_density.is_null();
  324. bool valid = !null && RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set_density);
  325. #ifdef DEV_ENABLED
  326. // It's all-or-nothing, or something else has changed that requires dev attention.
  327. DEV_ASSERT(null == gi_dependent_sets.process_uniform_set.is_null());
  328. DEV_ASSERT(null == gi_dependent_sets.process_uniform_set2.is_null());
  329. DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set));
  330. DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set2));
  331. #endif
  332. if (valid) {
  333. if (p_ensure_freed) {
  334. RD::get_singleton()->free(gi_dependent_sets.process_uniform_set_density);
  335. RD::get_singleton()->free(gi_dependent_sets.process_uniform_set);
  336. RD::get_singleton()->free(gi_dependent_sets.process_uniform_set2);
  337. valid = false;
  338. }
  339. }
  340. if (!valid && !null) {
  341. gi_dependent_sets = {};
  342. }
  343. return valid;
  344. }
  345. void Fog::VolumetricFog::init(const Vector3i &fog_size, RID p_sky_shader) {
  346. width = fog_size.x;
  347. height = fog_size.y;
  348. depth = fog_size.z;
  349. RD::TextureFormat tf;
  350. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  351. tf.width = fog_size.x;
  352. tf.height = fog_size.y;
  353. tf.depth = fog_size.z;
  354. tf.texture_type = RD::TEXTURE_TYPE_3D;
  355. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  356. light_density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  357. RD::get_singleton()->set_resource_name(light_density_map, "Fog light-density map");
  358. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  359. prev_light_density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  360. RD::get_singleton()->set_resource_name(prev_light_density_map, "Fog previous light-density map");
  361. RD::get_singleton()->texture_clear(prev_light_density_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  362. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  363. fog_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  364. RD::get_singleton()->set_resource_name(fog_map, "Fog map");
  365. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  366. Vector<uint8_t> dm;
  367. dm.resize(fog_size.x * fog_size.y * fog_size.z * 4);
  368. dm.fill(0);
  369. density_map = RD::get_singleton()->storage_buffer_create(dm.size(), dm);
  370. RD::get_singleton()->set_resource_name(density_map, "Fog density map");
  371. light_map = RD::get_singleton()->storage_buffer_create(dm.size(), dm);
  372. RD::get_singleton()->set_resource_name(light_map, "Fog light map");
  373. emissive_map = RD::get_singleton()->storage_buffer_create(dm.size(), dm);
  374. RD::get_singleton()->set_resource_name(emissive_map, "Fog emissive map");
  375. #else
  376. tf.format = RD::DATA_FORMAT_R32_UINT;
  377. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  378. density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  379. RD::get_singleton()->set_resource_name(density_map, "Fog density map");
  380. RD::get_singleton()->texture_clear(density_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  381. light_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  382. RD::get_singleton()->set_resource_name(light_map, "Fog light map");
  383. RD::get_singleton()->texture_clear(light_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  384. emissive_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  385. RD::get_singleton()->set_resource_name(emissive_map, "Fog emissive map");
  386. RD::get_singleton()->texture_clear(emissive_map, Color(0, 0, 0, 0), 0, 1, 0, 1);
  387. #endif
  388. Vector<RD::Uniform> uniforms;
  389. {
  390. RD::Uniform u;
  391. u.binding = 0;
  392. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  393. u.append_id(fog_map);
  394. uniforms.push_back(u);
  395. }
  396. sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_sky_shader, RendererRD::SkyRD::SKY_SET_FOG);
  397. }
  398. Fog::VolumetricFog::~VolumetricFog() {
  399. RD::get_singleton()->free(prev_light_density_map);
  400. RD::get_singleton()->free(light_density_map);
  401. RD::get_singleton()->free(fog_map);
  402. RD::get_singleton()->free(density_map);
  403. RD::get_singleton()->free(light_map);
  404. RD::get_singleton()->free(emissive_map);
  405. if (fog_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(fog_uniform_set)) {
  406. RD::get_singleton()->free(fog_uniform_set);
  407. }
  408. if (copy_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(copy_uniform_set)) {
  409. RD::get_singleton()->free(copy_uniform_set);
  410. }
  411. sync_gi_dependent_sets_validity(true);
  412. if (sdfgi_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sdfgi_uniform_set)) {
  413. RD::get_singleton()->free(sdfgi_uniform_set);
  414. }
  415. if (sky_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_uniform_set)) {
  416. RD::get_singleton()->free(sky_uniform_set);
  417. }
  418. }
  419. 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) {
  420. Vector3 view_position = p_cam_transform.affine_inverse().xform(p_point);
  421. view_position.z = MIN(view_position.z, -0.01); // Clamp to the front of camera
  422. Vector3 fog_position = Vector3(0, 0, 0);
  423. view_position.y = -view_position.y;
  424. fog_position.z = -view_position.z / fog_end;
  425. 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;
  426. 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;
  427. fog_position.z = Math::pow(float(fog_position.z), float(1.0 / volumetric_fog_detail_spread));
  428. fog_position = fog_position * fog_size - Vector3(0.5, 0.5, 0.5);
  429. fog_position.x = CLAMP(fog_position.x, 0.0, fog_size.x);
  430. fog_position.y = CLAMP(fog_position.y, 0.0, fog_size.y);
  431. fog_position.z = CLAMP(fog_position.z, 0.0, fog_size.z);
  432. return Vector3i(fog_position);
  433. }
  434. 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) {
  435. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  436. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  437. RENDER_TIMESTAMP("> Volumetric Fog");
  438. RD::get_singleton()->draw_command_begin_label("Volumetric Fog");
  439. Ref<VolumetricFog> fog = p_settings.vfog;
  440. if (p_fog_volumes.size() > 0) {
  441. RD::get_singleton()->draw_command_begin_label("Render Volumetric Fog Volumes");
  442. RENDER_TIMESTAMP("Render FogVolumes");
  443. VolumetricFogShader::VolumeUBO params;
  444. Vector2 frustum_near_size = p_cam_projection.get_viewport_half_extents();
  445. Vector2 frustum_far_size = p_cam_projection.get_far_plane_half_extents();
  446. float z_near = p_cam_projection.get_z_near();
  447. float z_far = p_cam_projection.get_z_far();
  448. float fog_end = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_length(p_settings.env);
  449. Vector2 fog_far_size = frustum_near_size.lerp(frustum_far_size, (fog_end - z_near) / (z_far - z_near));
  450. Vector2 fog_near_size;
  451. if (p_cam_projection.is_orthogonal()) {
  452. fog_near_size = fog_far_size;
  453. } else {
  454. fog_near_size = Vector2();
  455. }
  456. params.fog_frustum_size_begin[0] = fog_near_size.x;
  457. params.fog_frustum_size_begin[1] = fog_near_size.y;
  458. params.fog_frustum_size_end[0] = fog_far_size.x;
  459. params.fog_frustum_size_end[1] = fog_far_size.y;
  460. params.fog_frustum_end = fog_end;
  461. params.z_near = z_near;
  462. params.z_far = z_far;
  463. params.time = p_settings.time;
  464. params.fog_volume_size[0] = fog->width;
  465. params.fog_volume_size[1] = fog->height;
  466. params.fog_volume_size[2] = fog->depth;
  467. params.use_temporal_reprojection = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env);
  468. params.temporal_frame = RSG::rasterizer->get_frame_number() % VolumetricFog::MAX_TEMPORAL_FRAMES;
  469. params.detail_spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  470. params.temporal_blend = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection_amount(p_settings.env);
  471. Transform3D to_prev_cam_view = p_prev_cam_inv_transform * p_cam_transform;
  472. RendererRD::MaterialStorage::store_transform(to_prev_cam_view, params.to_prev_view);
  473. RendererRD::MaterialStorage::store_transform(p_cam_transform, params.transform);
  474. RD::get_singleton()->buffer_update(volumetric_fog.volume_ubo, 0, sizeof(VolumetricFogShader::VolumeUBO), &params, RD::BARRIER_MASK_COMPUTE);
  475. if (fog->fog_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fog->fog_uniform_set)) {
  476. Vector<RD::Uniform> uniforms;
  477. {
  478. RD::Uniform u;
  479. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  480. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  481. #else
  482. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  483. #endif
  484. u.binding = 1;
  485. u.append_id(fog->emissive_map);
  486. uniforms.push_back(u);
  487. }
  488. {
  489. RD::Uniform u;
  490. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  491. u.binding = 2;
  492. u.append_id(volumetric_fog.volume_ubo);
  493. uniforms.push_back(u);
  494. }
  495. {
  496. RD::Uniform u;
  497. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  498. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  499. #else
  500. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  501. #endif
  502. u.binding = 3;
  503. u.append_id(fog->density_map);
  504. uniforms.push_back(u);
  505. }
  506. {
  507. RD::Uniform u;
  508. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  509. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  510. #else
  511. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  512. #endif
  513. u.binding = 4;
  514. u.append_id(fog->light_map);
  515. uniforms.push_back(u);
  516. }
  517. fog->fog_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.default_shader_rd, VolumetricFogShader::FogSet::FOG_SET_UNIFORMS);
  518. }
  519. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  520. bool any_uses_time = false;
  521. for (int i = 0; i < (int)p_fog_volumes.size(); i++) {
  522. FogVolumeInstance *fog_volume_instance = fog_volume_instance_owner.get_or_null(p_fog_volumes[i]);
  523. ERR_FAIL_COND(!fog_volume_instance);
  524. RID fog_volume = fog_volume_instance->volume;
  525. RID fog_material = RendererRD::Fog::get_singleton()->fog_volume_get_material(fog_volume);
  526. FogMaterialData *material = nullptr;
  527. if (fog_material.is_valid()) {
  528. material = static_cast<FogMaterialData *>(material_storage->material_get_data(fog_material, RendererRD::MaterialStorage::SHADER_TYPE_FOG));
  529. if (!material || !material->shader_data->valid) {
  530. material = nullptr;
  531. }
  532. }
  533. if (!material) {
  534. fog_material = volumetric_fog.default_material;
  535. material = static_cast<FogMaterialData *>(material_storage->material_get_data(fog_material, RendererRD::MaterialStorage::SHADER_TYPE_FOG));
  536. }
  537. ERR_FAIL_COND(!material);
  538. FogShaderData *shader_data = material->shader_data;
  539. ERR_FAIL_COND(!shader_data);
  540. any_uses_time |= shader_data->uses_time;
  541. Vector3i min;
  542. Vector3i max;
  543. Vector3i kernel_size;
  544. Vector3 position = fog_volume_instance->transform.get_origin();
  545. RS::FogVolumeShape volume_type = RendererRD::Fog::get_singleton()->fog_volume_get_shape(fog_volume);
  546. Vector3 extents = RendererRD::Fog::get_singleton()->fog_volume_get_size(fog_volume) / 2;
  547. if (volume_type != RS::FOG_VOLUME_SHAPE_WORLD) {
  548. // Local fog volume.
  549. Vector3i points[8];
  550. Vector3 fog_size = Vector3(fog->width, fog->height, fog->depth);
  551. float volumetric_fog_detail_spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  552. 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);
  553. 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);
  554. 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);
  555. 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);
  556. 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);
  557. 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);
  558. 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);
  559. 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);
  560. min = Vector3i(int32_t(fog->width) - 1, int32_t(fog->height) - 1, int32_t(fog->depth) - 1);
  561. max = Vector3i(1, 1, 1);
  562. for (int j = 0; j < 8; j++) {
  563. min = Vector3i(MIN(min.x, points[j].x), MIN(min.y, points[j].y), MIN(min.z, points[j].z));
  564. max = Vector3i(MAX(max.x, points[j].x), MAX(max.y, points[j].y), MAX(max.z, points[j].z));
  565. }
  566. kernel_size = max - min;
  567. } else {
  568. // Volume type global runs on all cells
  569. extents = Vector3(fog->width, fog->height, fog->depth);
  570. min = Vector3i(0, 0, 0);
  571. kernel_size = Vector3i(int32_t(fog->width), int32_t(fog->height), int32_t(fog->depth));
  572. }
  573. if (kernel_size.x == 0 || kernel_size.y == 0 || kernel_size.z == 0) {
  574. continue;
  575. }
  576. VolumetricFogShader::FogPushConstant push_constant;
  577. push_constant.position[0] = position.x;
  578. push_constant.position[1] = position.y;
  579. push_constant.position[2] = position.z;
  580. push_constant.size[0] = extents.x * 2;
  581. push_constant.size[1] = extents.y * 2;
  582. push_constant.size[2] = extents.z * 2;
  583. push_constant.corner[0] = min.x;
  584. push_constant.corner[1] = min.y;
  585. push_constant.corner[2] = min.z;
  586. push_constant.shape = uint32_t(RendererRD::Fog::get_singleton()->fog_volume_get_shape(fog_volume));
  587. RendererRD::MaterialStorage::store_transform(fog_volume_instance->transform.affine_inverse(), push_constant.transform);
  588. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shader_data->pipeline);
  589. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->fog_uniform_set, VolumetricFogShader::FogSet::FOG_SET_UNIFORMS);
  590. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VolumetricFogShader::FogPushConstant));
  591. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, volumetric_fog.base_uniform_set, VolumetricFogShader::FogSet::FOG_SET_BASE);
  592. if (material->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(material->uniform_set)) { // Material may not have a uniform set.
  593. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, material->uniform_set, VolumetricFogShader::FogSet::FOG_SET_MATERIAL);
  594. material->set_as_used();
  595. }
  596. RD::get_singleton()->compute_list_dispatch_threads(compute_list, kernel_size.x, kernel_size.y, kernel_size.z);
  597. }
  598. if (any_uses_time || RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env)) {
  599. RenderingServerDefault::redraw_request();
  600. }
  601. RD::get_singleton()->draw_command_end_label();
  602. RD::get_singleton()->compute_list_end();
  603. }
  604. bool gi_dependent_sets_valid = fog->sync_gi_dependent_sets_validity();
  605. if (!fog->copy_uniform_set.is_null() && !RD::get_singleton()->uniform_set_is_valid(fog->copy_uniform_set)) {
  606. fog->copy_uniform_set = RID();
  607. }
  608. if (!gi_dependent_sets_valid || fog->copy_uniform_set.is_null()) {
  609. //re create uniform set if needed
  610. Vector<RD::Uniform> uniforms;
  611. Vector<RD::Uniform> copy_uniforms;
  612. {
  613. RD::Uniform u;
  614. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  615. u.binding = 1;
  616. if (p_settings.shadow_atlas_depth.is_null()) {
  617. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK));
  618. } else {
  619. u.append_id(p_settings.shadow_atlas_depth);
  620. }
  621. uniforms.push_back(u);
  622. copy_uniforms.push_back(u);
  623. }
  624. {
  625. RD::Uniform u;
  626. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  627. u.binding = 2;
  628. if (p_settings.directional_shadow_depth.is_valid()) {
  629. u.append_id(p_settings.directional_shadow_depth);
  630. } else {
  631. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_BLACK));
  632. }
  633. uniforms.push_back(u);
  634. copy_uniforms.push_back(u);
  635. }
  636. {
  637. RD::Uniform u;
  638. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  639. u.binding = 3;
  640. u.append_id(p_settings.omni_light_buffer);
  641. uniforms.push_back(u);
  642. copy_uniforms.push_back(u);
  643. }
  644. {
  645. RD::Uniform u;
  646. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  647. u.binding = 4;
  648. u.append_id(p_settings.spot_light_buffer);
  649. uniforms.push_back(u);
  650. copy_uniforms.push_back(u);
  651. }
  652. {
  653. RD::Uniform u;
  654. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  655. u.binding = 5;
  656. u.append_id(p_settings.directional_light_buffer);
  657. uniforms.push_back(u);
  658. copy_uniforms.push_back(u);
  659. }
  660. {
  661. RD::Uniform u;
  662. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  663. u.binding = 6;
  664. u.append_id(p_settings.cluster_builder->get_cluster_buffer());
  665. uniforms.push_back(u);
  666. copy_uniforms.push_back(u);
  667. }
  668. {
  669. RD::Uniform u;
  670. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  671. u.binding = 7;
  672. u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  673. uniforms.push_back(u);
  674. copy_uniforms.push_back(u);
  675. }
  676. {
  677. RD::Uniform u;
  678. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  679. u.binding = 8;
  680. u.append_id(fog->light_density_map);
  681. uniforms.push_back(u);
  682. copy_uniforms.push_back(u);
  683. }
  684. {
  685. RD::Uniform u;
  686. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  687. u.binding = 9;
  688. u.append_id(fog->fog_map);
  689. uniforms.push_back(u);
  690. }
  691. {
  692. RD::Uniform u;
  693. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  694. u.binding = 9;
  695. u.append_id(fog->prev_light_density_map);
  696. copy_uniforms.push_back(u);
  697. }
  698. {
  699. RD::Uniform u;
  700. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  701. u.binding = 10;
  702. u.append_id(p_settings.shadow_sampler);
  703. uniforms.push_back(u);
  704. copy_uniforms.push_back(u);
  705. }
  706. {
  707. RD::Uniform u;
  708. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  709. u.binding = 11;
  710. u.append_id(p_settings.voxel_gi_buffer);
  711. uniforms.push_back(u);
  712. copy_uniforms.push_back(u);
  713. }
  714. {
  715. RD::Uniform u;
  716. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  717. u.binding = 12;
  718. for (int i = 0; i < RendererRD::GI::MAX_VOXEL_GI_INSTANCES; i++) {
  719. u.append_id(p_settings.rbgi->voxel_gi_textures[i]);
  720. }
  721. uniforms.push_back(u);
  722. copy_uniforms.push_back(u);
  723. }
  724. {
  725. RD::Uniform u;
  726. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  727. u.binding = 13;
  728. u.append_id(material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  729. uniforms.push_back(u);
  730. copy_uniforms.push_back(u);
  731. }
  732. {
  733. RD::Uniform u;
  734. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  735. u.binding = 14;
  736. u.append_id(volumetric_fog.params_ubo);
  737. uniforms.push_back(u);
  738. copy_uniforms.push_back(u);
  739. }
  740. {
  741. RD::Uniform u;
  742. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  743. u.binding = 15;
  744. u.append_id(fog->prev_light_density_map);
  745. uniforms.push_back(u);
  746. }
  747. {
  748. RD::Uniform u;
  749. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  750. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  751. #else
  752. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  753. #endif
  754. u.binding = 16;
  755. u.append_id(fog->density_map);
  756. uniforms.push_back(u);
  757. }
  758. {
  759. RD::Uniform u;
  760. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  761. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  762. #else
  763. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  764. #endif
  765. u.binding = 17;
  766. u.append_id(fog->light_map);
  767. uniforms.push_back(u);
  768. }
  769. {
  770. RD::Uniform u;
  771. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  772. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  773. #else
  774. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  775. #endif
  776. u.binding = 18;
  777. u.append_id(fog->emissive_map);
  778. uniforms.push_back(u);
  779. }
  780. {
  781. RD::Uniform u;
  782. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  783. u.binding = 19;
  784. 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);
  785. 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();
  786. u.append_id(sky_texture.is_valid() ? sky_texture : radiance_texture);
  787. uniforms.push_back(u);
  788. }
  789. if (fog->copy_uniform_set.is_null()) {
  790. fog->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);
  791. }
  792. if (!gi_dependent_sets_valid) {
  793. 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);
  794. RID aux7 = uniforms.write[7].get_id(0);
  795. RID aux8 = uniforms.write[8].get_id(0);
  796. uniforms.write[7].set_id(0, aux8);
  797. uniforms.write[8].set_id(0, aux7);
  798. 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);
  799. uniforms.remove_at(8);
  800. uniforms.write[7].set_id(0, aux7);
  801. 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);
  802. }
  803. }
  804. 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());
  805. if (using_sdfgi) {
  806. if (fog->sdfgi_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fog->sdfgi_uniform_set)) {
  807. Vector<RD::Uniform> uniforms;
  808. {
  809. RD::Uniform u;
  810. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  811. u.binding = 0;
  812. u.append_id(p_settings.gi->sdfgi_ubo);
  813. uniforms.push_back(u);
  814. }
  815. {
  816. RD::Uniform u;
  817. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  818. u.binding = 1;
  819. u.append_id(p_settings.sdfgi->ambient_texture);
  820. uniforms.push_back(u);
  821. }
  822. {
  823. RD::Uniform u;
  824. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  825. u.binding = 2;
  826. u.append_id(p_settings.sdfgi->occlusion_texture);
  827. uniforms.push_back(u);
  828. }
  829. 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);
  830. }
  831. }
  832. fog->length = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_length(p_settings.env);
  833. fog->spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  834. VolumetricFogShader::ParamsUBO params;
  835. Vector2 frustum_near_size = p_cam_projection.get_viewport_half_extents();
  836. Vector2 frustum_far_size = p_cam_projection.get_far_plane_half_extents();
  837. float z_near = p_cam_projection.get_z_near();
  838. float z_far = p_cam_projection.get_z_far();
  839. float fog_end = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_length(p_settings.env);
  840. Vector2 fog_far_size = frustum_near_size.lerp(frustum_far_size, (fog_end - z_near) / (z_far - z_near));
  841. Vector2 fog_near_size;
  842. if (p_cam_projection.is_orthogonal()) {
  843. fog_near_size = fog_far_size;
  844. } else {
  845. fog_near_size = Vector2();
  846. }
  847. params.fog_frustum_size_begin[0] = fog_near_size.x;
  848. params.fog_frustum_size_begin[1] = fog_near_size.y;
  849. params.fog_frustum_size_end[0] = fog_far_size.x;
  850. params.fog_frustum_size_end[1] = fog_far_size.y;
  851. 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);
  852. params.z_far = z_far;
  853. params.fog_frustum_end = fog_end;
  854. Color ambient_color = RendererSceneRenderRD::get_singleton()->environment_get_ambient_light(p_settings.env).srgb_to_linear();
  855. params.ambient_color[0] = ambient_color.r;
  856. params.ambient_color[1] = ambient_color.g;
  857. params.ambient_color[2] = ambient_color.b;
  858. params.sky_contribution = RendererSceneRenderRD::get_singleton()->environment_get_ambient_sky_contribution(p_settings.env);
  859. params.fog_volume_size[0] = fog->width;
  860. params.fog_volume_size[1] = fog->height;
  861. params.fog_volume_size[2] = fog->depth;
  862. params.directional_light_count = p_directional_light_count;
  863. Color emission = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission(p_settings.env).srgb_to_linear();
  864. params.base_emission[0] = emission.r * RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission_energy(p_settings.env);
  865. params.base_emission[1] = emission.g * RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission_energy(p_settings.env);
  866. params.base_emission[2] = emission.b * RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_emission_energy(p_settings.env);
  867. params.base_density = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_density(p_settings.env);
  868. Color base_scattering = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_scattering(p_settings.env).srgb_to_linear();
  869. params.base_scattering[0] = base_scattering.r;
  870. params.base_scattering[1] = base_scattering.g;
  871. params.base_scattering[2] = base_scattering.b;
  872. params.phase_g = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_anisotropy(p_settings.env);
  873. params.detail_spread = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_detail_spread(p_settings.env);
  874. params.gi_inject = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_gi_inject(p_settings.env);
  875. params.cam_rotation[0] = p_cam_transform.basis[0][0];
  876. params.cam_rotation[1] = p_cam_transform.basis[1][0];
  877. params.cam_rotation[2] = p_cam_transform.basis[2][0];
  878. params.cam_rotation[3] = 0;
  879. params.cam_rotation[4] = p_cam_transform.basis[0][1];
  880. params.cam_rotation[5] = p_cam_transform.basis[1][1];
  881. params.cam_rotation[6] = p_cam_transform.basis[2][1];
  882. params.cam_rotation[7] = 0;
  883. params.cam_rotation[8] = p_cam_transform.basis[0][2];
  884. params.cam_rotation[9] = p_cam_transform.basis[1][2];
  885. params.cam_rotation[10] = p_cam_transform.basis[2][2];
  886. params.cam_rotation[11] = 0;
  887. params.filter_axis = 0;
  888. params.max_voxel_gi_instances = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_gi_inject(p_settings.env) > 0.001 ? p_voxel_gi_count : 0;
  889. params.temporal_frame = RSG::rasterizer->get_frame_number() % VolumetricFog::MAX_TEMPORAL_FRAMES;
  890. Transform3D to_prev_cam_view = p_prev_cam_inv_transform * p_cam_transform;
  891. RendererRD::MaterialStorage::store_transform(to_prev_cam_view, params.to_prev_view);
  892. params.use_temporal_reprojection = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env);
  893. params.temporal_blend = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection_amount(p_settings.env);
  894. {
  895. uint32_t cluster_size = p_settings.cluster_builder->get_cluster_size();
  896. params.cluster_shift = get_shift_from_power_of_2(cluster_size);
  897. uint32_t cluster_screen_width = (p_settings.rb_size.x - 1) / cluster_size + 1;
  898. uint32_t cluster_screen_height = (p_settings.rb_size.y - 1) / cluster_size + 1;
  899. params.max_cluster_element_count_div_32 = p_settings.max_cluster_elements / 32;
  900. params.cluster_type_size = cluster_screen_width * cluster_screen_height * (params.max_cluster_element_count_div_32 + 32);
  901. params.cluster_width = cluster_screen_width;
  902. params.screen_size[0] = p_settings.rb_size.x;
  903. params.screen_size[1] = p_settings.rb_size.y;
  904. }
  905. Basis sky_transform = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_settings.env);
  906. sky_transform = sky_transform.inverse() * p_cam_transform.basis;
  907. RendererRD::MaterialStorage::store_transform_3x3(sky_transform, params.radiance_inverse_xform);
  908. RD::get_singleton()->draw_command_begin_label("Render Volumetric Fog");
  909. RENDER_TIMESTAMP("Render Fog");
  910. RD::get_singleton()->buffer_update(volumetric_fog.params_ubo, 0, sizeof(VolumetricFogShader::ParamsUBO), &params, RD::BARRIER_MASK_COMPUTE);
  911. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  912. 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]);
  913. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set_density, 0);
  914. if (using_sdfgi) {
  915. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->sdfgi_uniform_set, 1);
  916. }
  917. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  918. RD::get_singleton()->compute_list_add_barrier(compute_list);
  919. // Copy fog to history buffer
  920. if (RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_temporal_reprojection(p_settings.env)) {
  921. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_COPY]);
  922. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->copy_uniform_set, 0);
  923. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  924. RD::get_singleton()->compute_list_add_barrier(compute_list);
  925. }
  926. RD::get_singleton()->draw_command_end_label();
  927. if (p_settings.volumetric_fog_filter_active) {
  928. RD::get_singleton()->draw_command_begin_label("Filter Fog");
  929. RENDER_TIMESTAMP("Filter Fog");
  930. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FILTER]);
  931. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set, 0);
  932. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  933. RD::get_singleton()->compute_list_end();
  934. //need restart for buffer update
  935. params.filter_axis = 1;
  936. RD::get_singleton()->buffer_update(volumetric_fog.params_ubo, 0, sizeof(VolumetricFogShader::ParamsUBO), &params);
  937. compute_list = RD::get_singleton()->compute_list_begin();
  938. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FILTER]);
  939. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set2, 0);
  940. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, fog->depth);
  941. RD::get_singleton()->compute_list_add_barrier(compute_list);
  942. RD::get_singleton()->draw_command_end_label();
  943. }
  944. RENDER_TIMESTAMP("Integrate Fog");
  945. RD::get_singleton()->draw_command_begin_label("Integrate Fog");
  946. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.process_pipelines[VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_FOG]);
  947. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, fog->gi_dependent_sets.process_uniform_set, 0);
  948. RD::get_singleton()->compute_list_dispatch_threads(compute_list, fog->width, fog->height, 1);
  949. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_RASTER);
  950. RENDER_TIMESTAMP("< Volumetric Fog");
  951. RD::get_singleton()->draw_command_end_label();
  952. RD::get_singleton()->draw_command_end_label();
  953. }