Blender V5.3
render_update.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#include "DNA_anim_types.h"
13#include "DNA_brush_types.h"
14#include "DNA_cachefile_types.h"
15#include "DNA_light_types.h"
16#include "DNA_material_types.h"
17#include "DNA_node_types.h"
18#include "DNA_object_types.h"
19#include "DNA_scene_types.h"
20#include "DNA_screen_types.h"
21#include "DNA_space_types.h"
22#include "DNA_view3d_types.h"
24#include "DNA_world_types.h"
25
26#include "DRW_engine.hh"
27
28#include "BLI_function_ref.hh"
29#include "BLI_listbase.hh"
30#include "BLI_threads.hh"
31
32#include "BKE_brush.hh"
33#include "BKE_context.hh"
34#include "BKE_icons.hh"
35#include "BKE_main.hh"
37#include "BKE_material.hh"
38#include "BKE_node_runtime.hh"
40#include "BKE_paint.hh"
41#include "BKE_scene.hh"
42#include "BKE_scene_runtime.hh"
43
44#include "RE_engine.h"
45#include "RE_pipeline.h"
46
47#include "SEQ_animation.hh"
48#include "SEQ_prefetch.hh"
49#include "SEQ_relations.hh"
50#include "SEQ_sequencer.hh"
51
52#include "ED_node.hh"
53#include "ED_node_preview.hh"
54#include "ED_paint.hh"
55#include "ED_render.hh"
56#include "ED_screen.hh"
57#include "ED_view3d.hh"
58
59#include "DEG_depsgraph.hh"
61
62#include "WM_api.hh"
63
64namespace blender {
65
66/* -------------------------------------------------------------------- */
69
71 Main *bmain, wmWindow *window, bScreen *screen, ScrArea *area, ARegion *region, Scene *scene)
72{
74 CTX_data_main_set(C, bmain);
75 CTX_data_scene_set(C, scene);
76 CTX_wm_manager_set(C, bmain->wm.first());
77 CTX_wm_window_set(C, window);
78 CTX_wm_screen_set(C, screen);
79 CTX_wm_area_set(C, area);
80 CTX_wm_region_set(C, region);
81 return C;
82}
83
84void ED_render_view3d_update(Depsgraph *depsgraph,
85 wmWindow *window,
86 ScrArea *area,
87 const bool updated)
88{
89 Main *bmain = DEG_get_bmain(depsgraph);
90 Scene *scene = DEG_get_input_scene(depsgraph);
91
92 for (ARegion &region : area->regionbase) {
93 if (region.regiontype != RGN_TYPE_WINDOW) {
94 continue;
95 }
96
97 RegionView3D *rv3d = static_cast<RegionView3D *>(region.regiondata);
98 RenderEngine *engine = rv3d->view_render ? RE_view_engine_get(rv3d->view_render) : nullptr;
99
100 if (engine && (updated || (engine->flag & RE_ENGINE_DO_UPDATE))) {
102 bmain, window, WM_window_get_active_screen(window), area, &region, scene);
103
104 engine->flag &= ~RE_ENGINE_DO_UPDATE;
105 engine->type->view_update(engine, C, CTX_data_depsgraph_pointer(C));
106
107 CTX_free(C);
108 }
109 }
110}
111
113 Main *bmain, const FunctionRef<void(RenderEngine *engine, bContext *C, ARegion *region)> fn)
114{
115 wmWindowManager *wm = bmain->wm.first();
116
117 for (bScreen &screen : bmain->screens) {
118 wmWindow *window = wm ? ED_screen_window_find(&screen, wm) : nullptr;
119
120 for (ScrArea &area : screen.areabase) {
121 if (area.spacetype != SPACE_VIEW3D) {
122 continue;
123 }
124
125 for (ARegion &region : area.regionbase) {
126 if (region.regiontype != RGN_TYPE_WINDOW) {
127 continue;
128 }
129
130 const RegionView3D *rv3d = static_cast<RegionView3D *>(region.regiondata);
131 RenderEngine *engine = (rv3d && rv3d->view_render) ?
133 nullptr;
134 if (!engine) {
135 continue;
136 }
137
138 Scene *scene = window ? WM_window_get_active_scene(window) : nullptr;
139 bContext *C = render_view3d_context_create(bmain, window, &screen, &area, &region, scene);
140
141 fn(engine, C, &region);
142
143 CTX_free(C);
144 }
145 }
146 }
147}
148
149void ED_render_view3d_auto_pause(Main *bmain, const bool pause)
150{
151 render_view3d_engines_foreach(bmain, [&](RenderEngine *engine, bContext *C, ARegion *region) {
152 RE_engine_view_auto_pause_set(engine, pause);
153 if (RE_engine_view_pause_notify(engine, C)) {
154 ED_region_tag_redraw(region);
155 }
156 });
157}
158
160{
161 render_view3d_engines_foreach(bmain, [&](RenderEngine *engine, bContext *C, ARegion *region) {
162 if (RE_engine_view_pause_notify(engine, C)) {
163 ED_region_tag_redraw(region);
164 }
165 });
166}
167
168static void update_compositor(const DEGEditorUpdateContext *update_context)
169{
170 const Scene *scene = DEG_get_evaluated(update_context->depsgraph, update_context->scene);
171 if (!(scene->id.recalc & ID_RECALC_COMPOSITOR)) {
172 return;
173 }
174
175 const bool is_user_modified = DEG_scene_component_is_user_modified(
176 update_context->depsgraph, scene, DEG_SCENE_COMP_COMPOSITOR);
177 if (is_user_modified) {
178 update_context->scene->runtime->compositor.cache.clear_frames();
179 }
180
182 update_context->bmain, update_context->scene, update_context->view_layer, is_user_modified);
183}
184
185void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, const bool updated)
186{
187 Main *bmain = update_ctx->bmain;
188 static bool recursive_check = false;
189
190 /* don't do this render engine update if we're updating the scene from
191 * other threads doing e.g. rendering or baking jobs */
192 if (!BLI_thread_is_main()) {
193 return;
194 }
195
196 /* don't call this recursively for frame updates */
197 if (recursive_check) {
198 return;
199 }
200
201 /* Do not call if no WM available, see #42688. */
202 if (bmain->wm.is_empty()) {
203 return;
204 }
205
206 recursive_check = true;
207
208 wmWindowManager *wm = bmain->wm.first();
209 for (wmWindow &window : wm->windows) {
210 bScreen *screen = WM_window_get_active_screen(&window);
211
212 for (ScrArea &area : screen->areabase) {
213 if (area.spacetype == SPACE_VIEW3D) {
214 ED_render_view3d_update(update_ctx->depsgraph, &window, &area, updated);
215 }
216 }
217 }
218
219 update_compositor(update_ctx);
220
221 recursive_check = false;
222}
223
225{
226 /* clear all render engines in this area */
227 wmWindowManager *wm = bmain->wm.first();
228
229 if (area->spacetype != SPACE_VIEW3D) {
230 return;
231 }
232
233 for (ARegion &region : area->regionbase) {
234 if (region.regiontype != RGN_TYPE_WINDOW || !(region.regiondata)) {
235 continue;
236 }
238 }
239}
240
241void ED_render_engine_changed(Main *bmain, const bool update_scene_data)
242{
243 /* on changing the render engine type, clear all running render engines */
244 for (bScreen *screen = bmain->screens.first(); screen;
245 screen = static_cast<bScreen *>(screen->id.next))
246 {
247 for (ScrArea &area : screen->areabase) {
248 ED_render_engine_area_exit(bmain, &area);
249 }
250 }
251 /* Stop and invalidate all shader previews. */
252 ED_preview_kill_jobs(bmain->wm.first(), bmain);
253 for (Material &ma : bmain->materials) {
255 }
256 RE_FreePersistentData(nullptr);
257 /* Inform all render engines and draw managers. */
258 DEGEditorUpdateContext update_ctx = {nullptr};
259 update_ctx.bmain = bmain;
260 for (Scene *scene = bmain->scenes.first(); scene; scene = static_cast<Scene *>(scene->id.next)) {
261 update_ctx.scene = scene;
262 for (ViewLayer &view_layer : scene->view_layers) {
263 /* TODO(sergey): Iterate over depsgraphs instead? */
264 update_ctx.depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, &view_layer);
265 update_ctx.view_layer = &view_layer;
266 ED_render_id_flush_update(&update_ctx, &scene->id);
267 }
268 if (update_scene_data) {
269 BKE_ntree_update_tag_id_changed(bmain, &scene->id);
270 BKE_ntree_update(*bmain);
271 }
272 }
274}
275
277{
278 for (ScrArea &area : screen->areabase) {
279 ED_render_engine_area_exit(bmain, &area);
280 }
281}
282
284
285/* -------------------------------------------------------------------- */
293
294static void material_changed(Main *bmain, Material *ma)
295{
296 /* icons */
298 ED_previews_tag_dirty_by_id(*bmain, ma->id);
299}
300
301static void lamp_changed(Main *bmain, Light *la)
302{
303 /* icons */
305 ED_previews_tag_dirty_by_id(*bmain, la->id);
306}
307
308static void texture_changed(Main *bmain, Tex *tex)
309{
310 /* icons */
312 ED_previews_tag_dirty_by_id(*bmain, tex->id);
313
314 for (Scene &scene : bmain->scenes) {
315 /* paint overlays */
317 }
318
319 for (Brush &brush : bmain->brushes) {
320 if (ELEM(tex, brush.mtex.tex, brush.mask_mtex.tex)) {
322 }
323 }
324}
325
326static void world_changed(Main *bmain, World *wo)
327{
328 /* icons */
330 ED_previews_tag_dirty_by_id(*bmain, wo->id);
331}
332
333static void image_changed(Main *bmain, Image *ima)
334{
335 Tex *tex;
336
337 /* icons */
339 ED_previews_tag_dirty_by_id(*bmain, ima->id);
340
341 /* textures */
342 for (tex = bmain->textures.first(); tex; tex = static_cast<Tex *>(tex->id.next)) {
343 if (tex->type == TEX_IMAGE && tex->ima == ima) {
344 texture_changed(bmain, tex);
345 }
346 }
347
348 /* Ensure downstream editors are made aware of changes to the Image data. */
350}
351
352static void scene_changed(Main *bmain, Scene *scene)
353{
354 Object *ob;
355
356 /* glsl */
357 for (ob = bmain->objects.first(); ob; ob = static_cast<Object *>(ob->id.next)) {
358 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
360 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
361 }
362 }
363}
364
365static void update_sequencer(const DEGEditorUpdateContext *update_ctx, Main *bmain, ID *id)
366{
370 if ((id->recalc & ~ignored) == 0) {
371 return;
372 }
373 Scene *changed_scene = update_ctx->scene;
374
375 /* Changed datablocks invalidate camera-input scene strips.
376 * Changed strips invalidate sequencer-input scene strips. */
377 if (id->id_type() != ID_SCE || id->recalc & ID_RECALC_SEQUENCER_STRIPS) {
378 seq::relations_invalidate_scene_strips(bmain, changed_scene);
379 }
380
381 /* Invalidate rendered VSE caches in `changed_scene`, because strip animation may have been
382 * updated. */
383 if (id->id_type() == ID_AC) {
384 Editing *ed = seq::editing_get(changed_scene);
385 if (ed != nullptr && seq::animation_keyframes_exist(changed_scene) &&
386 &changed_scene->adt->action->id == id)
387 {
388 seq::prefetch_stop(changed_scene);
391 }
392 }
393
394 /* Invalidate cache for strips that use this compositing tree. */
395 if (id->id_type() == ID_NT) {
396 const bNodeTree *node_tree = reinterpret_cast<const bNodeTree *>(id);
397 if (node_tree->type == NTREE_COMPOSIT) {
399 }
400 }
401}
402
404{
405 /* this can be called from render or baking thread when a python script makes
406 * changes, in that case we don't want to do any editor updates, and making
407 * GPU changes is not possible because OpenGL only works in the main thread */
408 if (!BLI_thread_is_main()) {
409 return;
410 }
411 Main *bmain = update_ctx->bmain;
412 /* Internal ID update handlers. */
413 switch (id->id_type()) {
414 case ID_MA:
415 material_changed(bmain, id_cast<Material *>(id));
416 break;
417 case ID_TE:
418 texture_changed(bmain, id_cast<Tex *>(id));
419 break;
420 case ID_WO:
421 world_changed(bmain, id_cast<World *>(id));
422 break;
423 case ID_LA:
424 lamp_changed(bmain, id_cast<Light *>(id));
425 break;
426 case ID_IM:
427 image_changed(bmain, id_cast<Image *>(id));
428 break;
429 case ID_SCE:
430 scene_changed(bmain, id_cast<Scene *>(id));
431 break;
432 case ID_BR:
433 BKE_brush_tag_unsaved_changes(reinterpret_cast<Brush *>(id));
434 break;
435 default:
436 break;
437 }
438
439 update_sequencer(update_ctx, bmain, id);
440}
441
443
444} // namespace blender
General operations, lookup, etc. for materials.
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define NA_EDITED
Definition WM_types.hh:634
#define NC_IMAGE
Definition WM_types.hh:397
#define ELEM(...)
void invalidate_overlay_tex(Scene &scene, const Tex *tex)
void prefetch_stop(Scene *scene)
Definition prefetch.cc:336
void relations_invalidate_scene_strips(const Main *bmain, const Scene *scene_target)
bool animation_keyframes_exist(const Scene *scene)
Definition animation.cc:25
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
void relations_invalidate_compositor_users(const Main *bmain, const bNodeTree *node_tree)
void relations_invalidate_generator_strips(Scene *scene)
void cache_cleanup(Scene *scene, CacheCleanup mode)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
static void render_view3d_engines_foreach(Main *bmain, const FunctionRef< void(RenderEngine *engine, bContext *C, ARegion *region)> fn)
void BKE_ntree_update(Main &bmain, std::optional< Span< bNodeTree * > > modified_trees=std::nullopt, const NodeTreeUpdateExtraParams &params={})
void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, bool updated)
void ED_render_view_layer_changed(Main *bmain, bScreen *screen)
void RE_engine_view_auto_pause_set(RenderEngine *engine, const bool pause)
unsigned int uint
Definition BKE_text.h:55
void BKE_ntree_update_tag_id_changed(Main *bmain, ID *id)
static void texture_changed(Main *bmain, Tex *tex)
int BKE_icon_id_ensure(ID *id)
Definition icons.cc:279
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
bool DEG_scene_component_is_user_modified(const Depsgraph *graph, const Scene *scene, eDepsSceneComponentType component_type)
void CTX_wm_window_set(bContext *C, wmWindow *win)
void RE_FreePersistentData(const Scene *scene)
static void image_changed(Main *bmain, Image *ima)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
static void update_compositor(const DEGEditorUpdateContext *update_context)
bContext * CTX_create()
void BKE_brush_tag_unsaved_changes(Brush *brush)
Definition brush.cc:843
static void material_changed(Main *bmain, Material *ma)
void ED_preview_kill_jobs(wmWindowManager *wm, Main *bmain)
void BKE_icon_changed(int icon_id)
Definition icons.cc:215
Scene * WM_window_get_active_scene(const wmWindow *win)
void CTX_wm_screen_set(bContext *C, bScreen *screen)
void ED_node_compositor_job(Main *bmain, Scene *scene, ViewLayer *view_layer, bool triggered_by_user)
void ED_render_engine_changed(Main *bmain, bool update_scene_data)
Main * DEG_get_bmain(const Depsgraph *graph)
@ OB_MODE_TEXTURE_PAINT
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
void CTX_free(bContext *C)
int BLI_thread_is_main()
Definition threads.cc:184
bool ED_paint_proj_mesh_data_check(Scene &scene, Object &ob, bool *r_has_uvs, bool *r_has_mat, bool *r_has_tex, bool *r_has_stencil)
void BKE_material_make_node_previews_dirty(Material *ma)
void ED_view3d_stop_render_preview(wmWindowManager *wm, ARegion *region)
static bContext * render_view3d_context_create(Main *bmain, wmWindow *window, bScreen *screen, ScrArea *area, ARegion *region, Scene *scene)
static void update_sequencer(const DEGEditorUpdateContext *update_ctx, Main *bmain, ID *id)
static void scene_changed(Main *bmain, Scene *scene)
static void lamp_changed(Main *bmain, Light *la)
void ED_render_view3d_update(Depsgraph *depsgraph, wmWindow *window, ScrArea *area, bool updated)
void ED_previews_tag_dirty_by_id(const Main &bmain, const ID &id)
void ED_render_id_flush_update(const DEGEditorUpdateContext *update_ctx, ID *id)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_AUDIO_LISTENER
Definition DNA_ID.h:1130
@ ID_RECALC_AUDIO_FPS
Definition DNA_ID.h:1127
@ ID_RECALC_AUDIO
Definition DNA_ID.h:1132
@ ID_RECALC_FRAME_CHANGE
Definition DNA_ID.h:1125
@ ID_RECALC_SEQUENCER_STRIPS
Definition DNA_ID.h:1122
@ ID_RECALC_COMPOSITOR
Definition DNA_ID.h:1161
@ ID_RECALC_AUDIO_MUTE
Definition DNA_ID.h:1129
@ ID_RECALC_AUDIO_VOLUME
Definition DNA_ID.h:1128
Scene * DEG_get_input_scene(const Depsgraph *graph)
void ED_render_view3d_auto_pause(Main *bmain, bool pause)
void ED_render_view3d_pause_notify(Main *bmain)
void CTX_wm_manager_set(bContext *C, wmWindowManager *wm)
RenderEngine * RE_view_engine_get(const ViewRender *view_render)
void BKE_texpaint_slots_refresh_object(Scene *scene, Object *ob)
@ RE_ENGINE_DO_UPDATE
Definition RE_engine.h:66
bool RE_engine_view_pause_notify(RenderEngine *engine, const bContext *context)
void CTX_wm_region_set(bContext *C, ARegion *region)
void CTX_data_main_set(bContext *C, Main *bmain)
Depsgraph * BKE_scene_ensure_depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer)
Definition scene.cc:3644
bScreen * WM_window_get_active_screen(const wmWindow *win)
static void world_changed(Main *bmain, World *wo)
void ED_render_engine_area_exit(Main *bmain, ScrArea *area)
wmWindow * ED_screen_window_find(const bScreen *screen, const wmWindowManager *wm)
void CTX_data_scene_set(bContext *C, Scene *scene)
void CTX_wm_area_set(bContext *C, ScrArea *area)
eRegion_Type regiontype
void * next
Definition DNA_ID.h:418
unsigned int recalc
Definition DNA_ID.h:446
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
ListBaseT< Brush > brushes
Definition BKE_main.hh:410
ListBaseT< Material > materials
Definition BKE_main.hh:392
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
ListBaseT< Tex > textures
Definition BKE_main.hh:393
ListBaseT< bScreen > screens
Definition BKE_main.hh:400
struct ViewRender * view_render
void(*) view_update(struct RenderEngine *engine, const struct bContext *context, struct Depsgraph *depsgraph)
Definition RE_engine.h:111
RenderEngineType * type
Definition RE_engine.h:141
struct AnimData * adt
bke::SceneRuntime * runtime
ListBaseT< ARegion > regionbase
struct Image * ima
eNodeTree_Type type
ListBaseT< struct ScrArea > areabase