Blender V5.3
eevee_world.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_lib_id.hh"
10#include "BKE_node.hh"
13#include "NOD_shader.h"
14
15#include "eevee_instance.hh"
16
17namespace blender::eevee {
18
19/* -------------------------------------------------------------------- */
23
25{
26 if (default_world_ != nullptr) {
27 BKE_id_free(nullptr, default_world_);
28 }
29}
30
31blender::World *World::default_world_get()
32{
33 if (default_world_ == nullptr) {
34 default_world_ = BKE_id_new_nomain<blender::World>("EEVEE default world");
35
36 default_world_->gpumaterial.clear_no_delete();
37 }
38 return default_world_;
39}
40
41blender::World *World::scene_world_get()
42{
43 return (inst_.scene->world != nullptr) ? inst_.scene->world : default_world_get();
44}
45
47{
48 /* No sun extraction during baking. */
49 if (inst_.is_baking()) {
50 return 0.0;
51 }
52
53 float sun_threshold = scene_world_get()->sun_threshold;
54 if (inst_.use_studio_light()) {
55 /* Do not call `lookdev_world_.intensity_get()` as it might not be initialized yet. */
56 sun_threshold *= inst_.v3d->shading.studiolight_intensity;
57 }
58 return sun_threshold;
59}
60
62{
63 bool has_update = false;
64
65 WorldHandle wo_handle = {inst_.scene->world ? inst_.get_recalc_flags(*inst_.scene->world) : 0};
66 /* Detect world update before overriding it. */
67 has_update = wo_handle.recalc != 0;
68
69 bool wait_ready = true; // TODO !inst_.is_image_render;
70
71 /* Sync volume first since its result can override the surface world. */
72 sync_volume(wo_handle, wait_ready);
73
74 blender::World *bl_world;
75 if (inst_.use_studio_light()) {
76 has_update |= lookdev_world_.sync(LookdevParameters(inst_.v3d));
77 bl_world = lookdev_world_.world_get();
78 }
79 else if ((inst_.view_layer->layflag & SCE_LAY_SKY) == 0) {
80 bl_world = default_world_get();
81 }
82 else if (has_volume_absorption_) {
83 bl_world = default_world_get();
84 }
85 else {
86 bl_world = scene_world_get();
87 }
88
89 blender::World *world_override = DEG_get_evaluated(inst_.depsgraph,
90 inst_.view_layer->world_override);
91 if (world_override) {
92 bl_world = world_override;
93 }
94
95 bNodeTree *ntree = (bl_world->nodetree) ? bl_world->nodetree : default_world_get()->nodetree;
96
97 {
98 if (has_volume_absorption_) {
99 /* Replace world by black world. */
100 bl_world = default_world_get();
101 }
102 }
103
104 /* We have to manually test here because we have overrides. */
105 blender::World *orig_world = DEG_get_original(bl_world);
106 if (assign_if_different(prev_original_world, orig_world)) {
107 has_update = true;
108 }
109
110 inst_.light_probes.sync_world(bl_world, has_update);
111
112 if (inst_.is_viewport() && has_update) {
113 /* Catch lookdev viewport properties updates. */
114 inst_.sampling.reset();
115 }
116
117 GPUMaterial *gpumat = inst_.shaders.world_shader_get(
118 bl_world, ntree, MAT_PIPE_DEFERRED, !wait_ready);
119
120 if (GPU_material_status(gpumat) == GPU_MAT_FAILED) {
121 bl_world = default_world_get();
122 ntree = bl_world->nodetree;
123 gpumat = inst_.shaders.world_shader_get(bl_world, ntree, MAT_PIPE_DEFERRED, !wait_ready);
124 }
125 if (GPU_material_status(gpumat) == GPU_MAT_QUEUED) {
126 is_ready_ = false;
127 return;
128 }
129 is_ready_ = true;
130
131 inst_.manager->register_layer_attributes(gpumat);
132
133 float opacity = inst_.use_studio_light() ? lookdev_world_.background_opacity_get() :
134 inst_.film.background_opacity_get();
135 float background_blur = inst_.use_studio_light() ? lookdev_world_.background_blur_get() : 0.0;
136
137 inst_.pipelines.background.sync(gpumat, opacity, background_blur);
138 inst_.pipelines.world.sync(gpumat);
139}
140
141void World::sync_volume(const WorldHandle &world_handle, bool wait_ready)
142{
143 /* Studio lights have no volume shader. */
144 blender::World *world = inst_.use_studio_light() ? nullptr : inst_.scene->world;
145
146 GPUMaterial *gpumat = nullptr;
147
148 /* Only the scene world nodetree can have volume shader. */
149 if (world && world->nodetree) {
150 gpumat = inst_.shaders.world_shader_get(
151 world, world->nodetree, MAT_PIPE_VOLUME_MATERIAL, !wait_ready);
152 }
153
154 bool had_volume = has_volume_;
155
156 if (gpumat && (GPU_material_status(gpumat) == GPU_MAT_SUCCESS)) {
157 has_volume_ = GPU_material_has_volume_output(gpumat);
158 has_volume_scatter_ = GPU_material_flag_get(gpumat, GPU_MATFLAG_VOLUME_SCATTER);
159 has_volume_absorption_ = GPU_material_flag_get(gpumat, GPU_MATFLAG_VOLUME_ABSORPTION);
160 }
161 else {
162 has_volume_ = has_volume_absorption_ = has_volume_scatter_ = false;
163 }
164
165 /* World volume needs to be always synced for correct clearing of parameter buffers. */
166 inst_.pipelines.world_volume.sync(gpumat);
167
168 if (has_volume_ || had_volume) {
169 inst_.volume.world_sync(world_handle);
170 }
171}
172
174
175} // namespace blender::eevee
GPUMaterial * world_shader_get(blender::World *blender_world, bNodeTree *nodetree, eMaterialPipeline pipeline_type, bool deferred_compilation)
bool GPU_material_has_volume_output(GPUMaterial *mat)
bool assign_if_different(T &old_value, T &&new_value)
@ GPU_MATFLAG_VOLUME_ABSORPTION
@ GPU_MATFLAG_VOLUME_SCATTER
@ GPU_MAT_SUCCESS
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void BKE_id_free(Main *bmain, void *idv)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
T * DEG_get_original(T *id)
GPUMaterialStatus GPU_material_status(GPUMaterial *mat)
struct World * world