Blender V5.3
eevee_lightcache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "DRW_engine.hh"
12#include "DRW_render.hh"
13
14#include "BKE_global.hh"
15#include "BKE_lightprobe.h"
16
18
19#include "BLI_mutex.hh"
20#include "BLI_threads.hh"
21#include "BLI_time.hh"
22
25
26#include "GPU_capabilities.hh"
27#include "GPU_context.hh"
28
29#include "WM_api.hh"
30#include "WM_types.hh"
31
32#include "wm_window.hh"
33
34#include "eevee_instance.hh"
35
36#include "eevee_lightcache.hh"
37
38namespace blender {
39
40/* -------------------------------------------------------------------- */
43
44namespace eevee {
45
46class LightBake {
47 private:
48 Depsgraph *depsgraph_;
49
51 int frame_;
53 int delay_ms_;
59 std::string &report_;
60
65 GHOST_IContext *gl_context_ = nullptr;
67 GPUContext *gpu_context_ = nullptr;
68
70 Instance *instance_ = nullptr;
72 draw::Manager *manager_ = nullptr;
73
75 Vector<Object *> original_probes_;
78 Mutex result_mutex_;
79
80 public:
82 ViewLayer *view_layer,
83 Scene *scene,
84 Span<Object *> probes,
85 bool run_as_job,
86 std::string &report,
87 int frame,
88 int delay_ms = 0)
89 : depsgraph_(DEG_graph_new(bmain, scene, view_layer, DAG_EVAL_RENDER)),
90 frame_(frame),
91 delay_ms_(delay_ms),
92 report_(report),
93 original_probes_(probes)
94 {
96 bake_result_.resize(probes.size());
97 bake_result_.fill(nullptr);
98
99 if (run_as_job && !GPU_use_main_context_workaround()) {
100 /* This needs to happen in main thread. */
101 gl_context_ = WM_system_gpu_context_create();
103 }
104 }
105
107 {
109 DEG_graph_free(depsgraph_);
110 }
111
118 void update()
119 {
121
122 for (auto i : bake_result_.index_range()) {
123 if (bake_result_[i] == nullptr) {
124 continue;
125 }
126 Object *orig_ob = original_probes_[i];
127
128 {
129 std::scoped_lock lock(result_mutex_);
130
131 LightProbeObjectCache *cache = orig_ob->lightprobe_cache;
132 /* Delete any existing cache. */
133 if (cache->grid_static_cache != nullptr) {
135 }
136 /* Pass ownership to original object. */
137 cache->grid_static_cache = bake_result_[i];
138 bake_result_[i] = nullptr;
139 }
140 /* Propagate the cache to evaluated object. */
142 }
143 }
144
148 void run(const bool *stop = nullptr, bool *do_update = nullptr, float *progress = nullptr)
149 {
150 DEG_graph_relations_update(depsgraph_);
151 DEG_evaluate_on_framechange(depsgraph_, frame_);
152
153 if (delay_ms_ > 0) {
154 BLI_time_sleep_ms(delay_ms_);
155 }
156
157 context_enable();
158 manager_ = new draw::Manager();
159 instance_ = new eevee::Instance();
160 instance_->init_light_bake(depsgraph_, manager_);
161 context_disable();
162
163 for (auto i : original_probes_.index_range()) {
164 Object *eval_ob = DEG_get_evaluated(depsgraph_, original_probes_[i]);
165
166 instance_->light_bake_irradiance(
167 *eval_ob,
168 [this]() { context_enable(); },
169 [this]() { context_disable(); },
170 [&]() { return (G.is_break == true) || ((stop != nullptr) ? *stop : false); },
171 [&](LightProbeGridCacheFrame *cache_frame, float grid_progress) {
172 {
173 std::scoped_lock lock(result_mutex_);
174 /* Delete any existing cache that wasn't transferred to the original object. */
175 if (bake_result_[i] != nullptr) {
177 }
178 bake_result_[i] = cache_frame;
179 }
180
181 if (do_update) {
182 *do_update = true;
183 }
184
185 if (progress) {
186 *progress = (i + grid_progress) / original_probes_.size();
187 }
188 });
189
190 if (StringRefNull(instance_->info_get()) != "") {
191 /* Pipe report to operator. */
192 report_ = instance_->info_get();
193 }
194
195 if ((G.is_break == true) || (stop != nullptr && *stop == true)) {
196 break;
197 }
198 }
199
200 delete_resources();
201 }
202
203 private:
204 void context_enable(bool render_begin = true)
205 {
207 /* Reuse main draw context. */
210 }
211 else if (gl_context_ == nullptr) {
212 /* Main thread case. */
214 }
215 else {
216 /* Worker thread case. */
218 if (gpu_context_ == nullptr) {
219 /* Create GPUContext in worker thread as it needs the correct gl context bound (which can
220 * only be bound in worker thread because of some GL driver requirements). */
221 gpu_context_ = GPU_context_create(nullptr, gl_context_);
222 }
224 }
225
226 if (render_begin) {
228 }
229 }
230
231 void context_disable()
232 {
234 /* Reuse main draw context. */
238 }
239 else if (gl_context_ == nullptr) {
240 /* Main thread case. */
243 }
244 else {
245 /* Worker thread case. */
249 }
250 }
251
258 void delete_resources()
259 {
260 /* Bind context without GPU_render_begin(). */
261 context_enable(false);
262
263 /* Free GPU data (Textures, Frame-buffers, etc...). */
264 delete instance_;
265 delete manager_;
266
267 /* Delete / unbind the GL & GPU context. Assumes it is currently bound. */
269 /* Reuse main draw context. */
272 }
273 else if (gl_context_ == nullptr) {
274 /* Main thread case. */
276 }
277 else {
278 /* Worker thread case. */
279 if (gpu_context_ != nullptr) {
280 GPU_context_discard(gpu_context_);
281 }
284 }
285 }
286};
287
288} // namespace eevee
289
291
292/* -------------------------------------------------------------------- */
295
296using namespace blender::eevee;
297
299 wmWindow *win,
300 Main *bmain,
301 ViewLayer *view_layer,
302 Scene *scene,
303 Vector<Object *> original_probes,
304 std::string &report,
305 int delay_ms,
306 int frame)
307{
308 /* Do not bake if there is a render going on. */
310 return nullptr;
311 }
312
313 /* Stop existing baking job. */
315
316 wmJob *wm_job = WM_jobs_get(wm,
317 win,
318 scene,
319 "Baking lighting...",
322
323 LightBake *bake = new LightBake(
324 bmain, view_layer, scene, std::move(original_probes), true, report, frame, delay_ms);
325
327 WM_jobs_timer(wm_job, 0.4, NC_SCENE | NA_EDITED, 0);
330
331 G.is_break = false;
332
333 return wm_job;
334}
335
337 ViewLayer *view_layer,
338 Scene *scene,
339 Vector<Object *> original_probes,
340 std::string &report,
341 int frame)
342{
343 LightBake *bake = new LightBake(
344 bmain, view_layer, scene, std::move(original_probes), false, report, frame);
345 /* TODO(fclem): Can remove this cast once we remove the previous EEVEE light cache. */
346 return reinterpret_cast<void *>(bake);
347}
348
350{
351 delete static_cast<LightBake *>(job_data);
352}
353
354void EEVEE_lightbake_update(void *job_data)
355{
356 static_cast<LightBake *>(job_data)->update();
357}
358
359void EEVEE_lightbake_job(void *job_data, wmJobWorkerStatus *worker_status)
360{
361 static_cast<LightBake *>(job_data)->run(
362 &worker_status->stop, &worker_status->do_update, &worker_status->progress);
363}
364
366
367} // namespace blender
General operations for probes.
Platform independent time functions.
#define NC_SCENE
Definition WM_types.hh:391
#define NA_EDITED
Definition WM_types.hh:634
volatile int lock
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr StringRefNull()
A running instance of the engine.
void run(const bool *stop=nullptr, bool *do_update=nullptr, float *progress=nullptr)
LightBake(Main *bmain, ViewLayer *view_layer, Scene *scene, Span< Object * > probes, bool run_as_job, std::string &report, int frame, int delay_ms=0)
BMesh Iterators.
void DRW_gpu_context_enable()
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void DRW_blender_gpu_render_context_enable(void *re_gpu_context)
void EEVEE_lightbake_update(void *job_data)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
void WM_jobs_stop_type(wmWindowManager *wm, const void *owner, eWM_JobType job_type)
Definition wm_jobs.cc:666
void DRW_system_gpu_render_context_disable(GHOST_IContext *re_system_gpu_context)
void DEG_graph_free(Depsgraph *graph)
Definition depsgraph.cc:307
GPUContext * GPU_context_create(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
void BLI_time_sleep_ms(int ms)
Definition time.cc:91
void GPU_context_main_unlock()
@ WM_JOB_TYPE_LIGHT_BAKE
Definition WM_api.hh:2042
@ WM_JOB_TYPE_RENDER
Definition WM_api.hh:2011
bool GPU_use_main_context_workaround()
Depsgraph * DEG_graph_new(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
Definition depsgraph.cc:279
void DRW_system_gpu_render_context_enable(GHOST_IContext *re_system_gpu_context)
void GPU_render_begin()
void DRW_blender_gpu_render_context_disable(void *re_gpu_context)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
@ WM_JOB_PRIORITY
Definition WM_api.hh:1990
@ WM_JOB_EXCL_RENDER
Definition WM_api.hh:1995
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
GHOST_IContext * WM_system_gpu_context_create()
void wm_window_reset_drawable()
Global G
Definition blender.cc:57
void DRW_gpu_context_disable()
void WM_system_gpu_context_dispose(GHOST_IContext *context)
void GPU_render_end()
void DEG_id_tag_update(ID *id, unsigned int flags)
bool WM_jobs_has_running(const wmWindowManager *window_manager, const void *owner, const eWM_JobType type, const eWM_JobFlag exclude_flags)
Definition wm_jobs.cc:809
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
wmJob * EEVEE_lightbake_job_create(wmWindowManager *wm, wmWindow *win, Main *bmain, ViewLayer *view_layer, Scene *scene, Vector< Object * > original_probes, std::string &report, int delay_ms, int frame)
int BLI_thread_is_main()
Definition threads.cc:184
void EEVEE_lightbake_job_data_free(void *job_data)
void BKE_lightprobe_grid_cache_frame_free(struct LightProbeGridCacheFrame *cache)
void * EEVEE_lightbake_job_data_alloc(Main *bmain, ViewLayer *view_layer, Scene *scene, Vector< Object * > original_probes, std::string &report, int frame)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
std::mutex Mutex
Definition BLI_mutex.hh:51
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
void DEG_graph_relations_update(Depsgraph *graph)
void DEG_evaluate_on_framechange(Depsgraph *graph, float frame, DepsgraphEvaluateSyncWriteback sync_writeback=DEG_EVALUATE_SYNC_WRITEBACK_NO)
void GPU_context_main_lock()
void EEVEE_lightbake_job(void *job_data, wmJobWorkerStatus *worker_status)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
void GPU_context_discard(GPUContext *)
struct LightProbeGridCacheFrame * grid_static_cache
struct LightProbeObjectCache * lightprobe_cache