| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/local_vector.h" |
| 34 |
#include "core/templates/rid_owner.h" |
| 35 |
#include "servers/rendering/environment/renderer_fog.h" |
| 36 |
#include "servers/rendering/renderer_rd/cluster_builder_rd.h" |
| 37 |
#include "servers/rendering/renderer_rd/environment/gi.h" |
| 38 |
#include "servers/rendering/renderer_rd/pipeline_deferred_rd.h" |
| 39 |
#include "servers/rendering/renderer_rd/shaders/environment/volumetric_fog.glsl.gen.h" |
| 40 |
#include "servers/rendering/renderer_rd/shaders/environment/volumetric_fog_process.glsl.gen.h" |
| 41 |
#include "servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h" |
| 42 |
#include "servers/rendering/storage/utilities.h" |
| 43 |
|
| 44 |
#define RB_SCOPE_FOG SNAME("Fog") |
| 45 |
|
| 46 |
namespace RendererRD { |
| 47 |
|
| 48 |
class Fog : public RendererFog { |
| 49 |
private: |
| 50 |
static Fog *singleton; |
| 51 |
|
| 52 |
static int _get_fog_shader_group(); |
| 53 |
static int _get_fog_variant(); |
| 54 |
static int _get_fog_process_variant(int p_idx); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct FogVolume { |
| 59 |
RID material; |
| 60 |
Vector3 size = Vector3(2, 2, 2); |
| 61 |
|
| 62 |
RS::FogVolumeShape shape = RS::FOG_VOLUME_SHAPE_BOX; |
| 63 |
|
| 64 |
Dependency dependency; |
| 65 |
}; |
| 66 |
|
| 67 |
mutable RID_Owner<FogVolume, true> fog_volume_owner; |
| 68 |
|
| 69 |
struct FogVolumeInstance { |
| 70 |
RID volume; |
| 71 |
Transform3D transform; |
| 72 |
bool active = false; |
| 73 |
}; |
| 74 |
|
| 75 |
mutable RID_Owner<FogVolumeInstance> fog_volume_instance_owner; |
| 76 |
|
| 77 |
const int SAMPLERS_BINDING_FIRST_INDEX = 3; |
| 78 |
|
| 79 |
|
| 80 |
struct VolumetricFogShader { |
| 81 |
enum ShaderGroup { |
| 82 |
SHADER_GROUP_BASE, |
| 83 |
SHADER_GROUP_NO_ATOMICS, |
| 84 |
SHADER_GROUP_VULKAN_MEMORY_MODEL, |
| 85 |
SHADER_GROUP_VULKAN_MEMORY_MODEL_NO_ATOMICS, |
| 86 |
}; |
| 87 |
|
| 88 |
enum FogSet { |
| 89 |
FOG_SET_BASE, |
| 90 |
FOG_SET_UNIFORMS, |
| 91 |
FOG_SET_MATERIAL, |
| 92 |
FOG_SET_MAX, |
| 93 |
}; |
| 94 |
|
| 95 |
struct FogPushConstant { |
| 96 |
float position[3]; |
| 97 |
float pad; |
| 98 |
|
| 99 |
float size[3]; |
| 100 |
float pad2; |
| 101 |
|
| 102 |
int32_t corner[3]; |
| 103 |
uint32_t shape; |
| 104 |
|
| 105 |
float transform[16]; |
| 106 |
}; |
| 107 |
|
| 108 |
struct VolumeUBO { |
| 109 |
float fog_frustum_size_begin[2]; |
| 110 |
float fog_frustum_size_end[2]; |
| 111 |
|
| 112 |
float fog_frustum_end; |
| 113 |
float z_near; |
| 114 |
float z_far; |
| 115 |
float time; |
| 116 |
|
| 117 |
int32_t fog_volume_size[3]; |
| 118 |
uint32_t directional_light_count; |
| 119 |
|
| 120 |
uint32_t use_temporal_reprojection; |
| 121 |
uint32_t temporal_frame; |
| 122 |
float detail_spread; |
| 123 |
float temporal_blend; |
| 124 |
|
| 125 |
float to_prev_view[16]; |
| 126 |
float transform[16]; |
| 127 |
}; |
| 128 |
|
| 129 |
ShaderCompiler compiler; |
| 130 |
VolumetricFogShaderRD shader; |
| 131 |
RID volume_ubo; |
| 132 |
|
| 133 |
RID default_shader; |
| 134 |
RID default_material; |
| 135 |
RID default_shader_rd; |
| 136 |
|
| 137 |
RID base_uniform_set; |
| 138 |
|
| 139 |
RID params_ubo; |
| 140 |
|
| 141 |
enum { |
| 142 |
VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY, |
| 143 |
VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY_WITH_SDFGI, |
| 144 |
VOLUMETRIC_FOG_PROCESS_SHADER_FILTER, |
| 145 |
VOLUMETRIC_FOG_PROCESS_SHADER_FOG, |
| 146 |
VOLUMETRIC_FOG_PROCESS_SHADER_COPY, |
| 147 |
VOLUMETRIC_FOG_PROCESS_SHADER_MAX, |
| 148 |
}; |
| 149 |
|
| 150 |
struct ParamsUBO { |
| 151 |
float fog_frustum_size_begin[2]; |
| 152 |
float fog_frustum_size_end[2]; |
| 153 |
|
| 154 |
float fog_frustum_end; |
| 155 |
float ambient_inject; |
| 156 |
float z_far; |
| 157 |
uint32_t filter_axis; |
| 158 |
|
| 159 |
float ambient_color[3]; |
| 160 |
float sky_contribution; |
| 161 |
|
| 162 |
int32_t fog_volume_size[3]; |
| 163 |
uint32_t directional_light_count; |
| 164 |
|
| 165 |
float base_emission[3]; |
| 166 |
float base_density; |
| 167 |
|
| 168 |
float base_scattering[3]; |
| 169 |
float phase_g; |
| 170 |
|
| 171 |
float detail_spread; |
| 172 |
float gi_inject; |
| 173 |
uint32_t max_voxel_gi_instances; |
| 174 |
uint32_t cluster_type_size; |
| 175 |
|
| 176 |
float screen_size[2]; |
| 177 |
uint32_t cluster_shift; |
| 178 |
uint32_t cluster_width; |
| 179 |
|
| 180 |
uint32_t max_cluster_element_count_div_32; |
| 181 |
uint32_t use_temporal_reprojection; |
| 182 |
uint32_t temporal_frame; |
| 183 |
float temporal_blend; |
| 184 |
|
| 185 |
float sky_border_size[2]; |
| 186 |
float pad[2]; |
| 187 |
|
| 188 |
float cam_rotation[12]; |
| 189 |
float to_prev_view[16]; |
| 190 |
float radiance_inverse_xform[12]; |
| 191 |
}; |
| 192 |
|
| 193 |
VolumetricFogProcessShaderRD process_shader; |
| 194 |
|
| 195 |
RID process_shader_version; |
| 196 |
PipelineDeferredRD process_pipelines[VOLUMETRIC_FOG_PROCESS_SHADER_MAX]; |
| 197 |
|
| 198 |
} volumetric_fog; |
| 199 |
|
| 200 |
Vector3i _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); |
| 201 |
|
| 202 |
struct FogShaderData : public RendererRD::MaterialStorage::ShaderData { |
| 203 |
bool valid = false; |
| 204 |
RID version; |
| 205 |
|
| 206 |
PipelineDeferredRD pipeline; |
| 207 |
Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms; |
| 208 |
|
| 209 |
Vector<uint32_t> ubo_offsets; |
| 210 |
uint32_t ubo_size = 0; |
| 211 |
|
| 212 |
String code; |
| 213 |
|
| 214 |
bool uses_time = false; |
| 215 |
|
| 216 |
virtual void set_code(const String &p_Code); |
| 217 |
virtual bool is_animated() const; |
| 218 |
virtual bool casts_shadows() const; |
| 219 |
virtual RS::ShaderNativeSourceCode get_native_source_code() const; |
| 220 |
virtual Pair<ShaderRD *, RID> get_native_shader_and_version() const; |
| 221 |
|
| 222 |
FogShaderData() {} |
| 223 |
virtual ~FogShaderData(); |
| 224 |
}; |
| 225 |
|
| 226 |
struct FogMaterialData : public RendererRD::MaterialStorage::MaterialData { |
| 227 |
FogShaderData *shader_data = nullptr; |
| 228 |
RID uniform_set; |
| 229 |
bool uniform_set_updated; |
| 230 |
|
| 231 |
virtual void set_render_priority(int p_priority) {} |
| 232 |
virtual void set_next_pass(RID p_pass) {} |
| 233 |
virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty); |
| 234 |
virtual ~FogMaterialData(); |
| 235 |
}; |
| 236 |
|
| 237 |
RendererRD::MaterialStorage::ShaderData *_create_fog_shader_func(); |
| 238 |
static RendererRD::MaterialStorage::ShaderData *_create_fog_shader_funcs(); |
| 239 |
|
| 240 |
RendererRD::MaterialStorage::MaterialData *_create_fog_material_func(FogShaderData *p_shader); |
| 241 |
static RendererRD::MaterialStorage::MaterialData *_create_fog_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader); |
| 242 |
|
| 243 |
public: |
| 244 |
static Fog *get_singleton() { return singleton; } |
| 245 |
|
| 246 |
Fog(); |
| 247 |
~Fog(); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
bool owns_fog_volume(RID p_rid) { return fog_volume_owner.owns(p_rid); } |
| 252 |
|
| 253 |
virtual RID fog_volume_allocate() override; |
| 254 |
virtual void fog_volume_initialize(RID p_rid) override; |
| 255 |
virtual void fog_volume_free(RID p_rid) override; |
| 256 |
Dependency *fog_volume_get_dependency(RID p_fog_volume) const; |
| 257 |
|
| 258 |
virtual void fog_volume_set_shape(RID p_fog_volume, RS::FogVolumeShape p_shape) override; |
| 259 |
virtual void fog_volume_set_size(RID p_fog_volume, const Vector3 &p_size) override; |
| 260 |
virtual void fog_volume_set_material(RID p_fog_volume, RID p_material) override; |
| 261 |
virtual RS::FogVolumeShape fog_volume_get_shape(RID p_fog_volume) const override; |
| 262 |
RID fog_volume_get_material(RID p_fog_volume) const; |
| 263 |
virtual AABB fog_volume_get_aabb(RID p_fog_volume) const override; |
| 264 |
Vector3 fog_volume_get_size(RID p_fog_volume) const; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
bool owns_fog_volume_instance(RID p_rid) { return fog_volume_instance_owner.owns(p_rid); } |
| 269 |
|
| 270 |
RID fog_volume_instance_create(RID p_fog_volume); |
| 271 |
void fog_instance_free(RID p_rid); |
| 272 |
|
| 273 |
void fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) { |
| 274 |
Fog::FogVolumeInstance *fvi = fog_volume_instance_owner.get_or_null(p_fog_volume_instance); |
| 275 |
ERR_FAIL_NULL(fvi); |
| 276 |
fvi->transform = p_transform; |
| 277 |
} |
| 278 |
|
| 279 |
void fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) { |
| 280 |
Fog::FogVolumeInstance *fvi = fog_volume_instance_owner.get_or_null(p_fog_volume_instance); |
| 281 |
ERR_FAIL_NULL(fvi); |
| 282 |
fvi->active = p_active; |
| 283 |
} |
| 284 |
|
| 285 |
RID fog_volume_instance_get_volume(RID p_fog_volume_instance) const { |
| 286 |
Fog::FogVolumeInstance *fvi = fog_volume_instance_owner.get_or_null(p_fog_volume_instance); |
| 287 |
ERR_FAIL_NULL_V(fvi, RID()); |
| 288 |
return fvi->volume; |
| 289 |
} |
| 290 |
|
| 291 |
Vector3 fog_volume_instance_get_position(RID p_fog_volume_instance) const { |
| 292 |
Fog::FogVolumeInstance *fvi = fog_volume_instance_owner.get_or_null(p_fog_volume_instance); |
| 293 |
ERR_FAIL_NULL_V(fvi, Vector3()); |
| 294 |
return fvi->transform.get_origin(); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
class VolumetricFog : public RenderBufferCustomDataRD { |
| 299 |
GDCLASS(VolumetricFog, RenderBufferCustomDataRD) |
| 300 |
|
| 301 |
public: |
| 302 |
enum { |
| 303 |
MAX_TEMPORAL_FRAMES = 16 |
| 304 |
}; |
| 305 |
|
| 306 |
uint32_t width = 0; |
| 307 |
uint32_t height = 0; |
| 308 |
uint32_t depth = 0; |
| 309 |
|
| 310 |
float length; |
| 311 |
float spread; |
| 312 |
|
| 313 |
RID light_density_map; |
| 314 |
RID prev_light_density_map; |
| 315 |
RID fog_map; |
| 316 |
RID density_map; |
| 317 |
RID light_map; |
| 318 |
RID emissive_map; |
| 319 |
|
| 320 |
RID fog_uniform_set; |
| 321 |
RID copy_uniform_set; |
| 322 |
|
| 323 |
struct { |
| 324 |
RID process_uniform_set_density; |
| 325 |
RID process_uniform_set; |
| 326 |
RID process_uniform_set2; |
| 327 |
} gi_dependent_sets; |
| 328 |
|
| 329 |
RID sdfgi_uniform_set; |
| 330 |
RID sky_uniform_set; |
| 331 |
|
| 332 |
int last_shadow_filter = -1; |
| 333 |
|
| 334 |
|
| 335 |
RD::UniformType atomic_type = RD::UNIFORM_TYPE_IMAGE; |
| 336 |
|
| 337 |
virtual void configure(RenderSceneBuffersRD *p_render_buffers) override {} |
| 338 |
virtual void free_data() override {} |
| 339 |
|
| 340 |
bool sync_gi_dependent_sets_validity(bool p_ensure_freed = false); |
| 341 |
|
| 342 |
void init(const Vector3i &fog_size, RID p_sky_shader); |
| 343 |
~VolumetricFog(); |
| 344 |
}; |
| 345 |
|
| 346 |
void init_fog_shader(uint32_t p_max_directional_lights, int p_roughness_layers, bool p_is_using_radiance_octmap_array); |
| 347 |
void free_fog_shader(); |
| 348 |
|
| 349 |
struct VolumetricFogSettings { |
| 350 |
Vector2i rb_size; |
| 351 |
double time; |
| 352 |
bool is_using_radiance_octmap_array; |
| 353 |
uint32_t max_cluster_elements; |
| 354 |
bool volumetric_fog_filter_active; |
| 355 |
RID shadow_sampler; |
| 356 |
RID voxel_gi_buffer; |
| 357 |
RID shadow_atlas_depth; |
| 358 |
RID omni_light_buffer; |
| 359 |
RID spot_light_buffer; |
| 360 |
RID directional_shadow_depth; |
| 361 |
RID directional_light_buffer; |
| 362 |
|
| 363 |
|
| 364 |
Ref<VolumetricFog> vfog; |
| 365 |
ClusterBuilderRD *cluster_builder; |
| 366 |
GI *gi; |
| 367 |
Ref<GI::SDFGI> sdfgi; |
| 368 |
Ref<GI::RenderBuffersGI> rbgi; |
| 369 |
RID env; |
| 370 |
SkyRD *sky; |
| 371 |
}; |
| 372 |
void 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); |
| 373 |
}; |
| 374 |
|
| 375 |
} |