Blender  V3.3
render_update.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Blender Foundation. All rights reserved. */
3 
8 #include <cstdlib>
9 #include <cstring>
10 
11 #include "DNA_cachefile_types.h"
12 #include "DNA_light_types.h"
13 #include "DNA_material_types.h"
14 #include "DNA_node_types.h"
15 #include "DNA_object_types.h"
16 #include "DNA_scene_types.h"
17 #include "DNA_screen_types.h"
18 #include "DNA_space_types.h"
19 #include "DNA_view3d_types.h"
21 #include "DNA_world_types.h"
22 
23 #include "DRW_engine.h"
24 
25 #include "BLI_listbase.h"
26 #include "BLI_threads.h"
27 #include "BLI_utildefines.h"
28 
29 #include "BKE_context.h"
30 #include "BKE_icons.h"
31 #include "BKE_main.h"
32 #include "BKE_material.h"
33 #include "BKE_node.h"
34 #include "BKE_paint.h"
35 #include "BKE_scene.h"
36 
37 #include "NOD_composite.h"
38 
39 #include "RE_engine.h"
40 #include "RE_pipeline.h"
41 
42 #include "ED_node.h"
43 #include "ED_paint.h"
44 #include "ED_render.h"
45 #include "ED_view3d.h"
46 
47 #include "DEG_depsgraph.h"
48 #include "DEG_depsgraph_query.h"
49 
50 #include "WM_api.h"
51 
52 #include <cstdio>
53 
54 /* -------------------------------------------------------------------- */
59  wmWindow *window,
60  ScrArea *area,
61  const bool updated)
62 {
63  Main *bmain = DEG_get_bmain(depsgraph);
66 
67  LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
68  if (region->regiontype != RGN_TYPE_WINDOW) {
69  continue;
70  }
71 
72  View3D *v3d = static_cast<View3D *>(area->spacedata.first);
73  RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
74  RenderEngine *engine = rv3d->render_engine;
75 
76  /* call update if the scene changed, or if the render engine
77  * tagged itself for update (e.g. because it was busy at the
78  * time of the last update) */
79  if (engine && (updated || (engine->flag & RE_ENGINE_DO_UPDATE))) {
80  /* Create temporary context to execute callback in. */
81  bContext *C = CTX_create();
82  CTX_data_main_set(C, bmain);
84  CTX_wm_manager_set(C, static_cast<wmWindowManager *>(bmain->wm.first));
85  CTX_wm_window_set(C, window);
88  CTX_wm_region_set(C, region);
89 
90  engine->flag &= ~RE_ENGINE_DO_UPDATE;
91  /* NOTE: Important to pass non-updated depsgraph, This is because this function is called
92  * from inside dependency graph evaluation. Additionally, if we pass fully evaluated one
93  * we will lose updates stored in the graph. */
94  engine->type->view_update(engine, C, CTX_data_depsgraph_pointer(C));
95 
96  CTX_free(C);
97  }
98  else {
100  if (updated) {
101  DRWUpdateContext drw_context = {nullptr};
102  drw_context.bmain = bmain;
103  drw_context.depsgraph = depsgraph;
104  drw_context.scene = scene;
105  drw_context.view_layer = view_layer;
106  drw_context.region = region;
107  drw_context.v3d = v3d;
108  drw_context.engine_type = engine_type;
109  DRW_notify_view_update(&drw_context);
110  }
111  }
112  }
113 }
114 
115 void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, const bool updated)
116 {
117  Main *bmain = update_ctx->bmain;
118  static bool recursive_check = false;
119 
120  /* don't do this render engine update if we're updating the scene from
121  * other threads doing e.g. rendering or baking jobs */
122  if (!BLI_thread_is_main()) {
123  return;
124  }
125 
126  /* don't call this recursively for frame updates */
127  if (recursive_check) {
128  return;
129  }
130 
131  /* Do not call if no WM available, see T42688. */
132  if (BLI_listbase_is_empty(&bmain->wm)) {
133  return;
134  }
135 
136  recursive_check = true;
137 
138  wmWindowManager *wm = static_cast<wmWindowManager *>(bmain->wm.first);
139  LISTBASE_FOREACH (wmWindow *, window, &wm->windows) {
140  bScreen *screen = WM_window_get_active_screen(window);
141 
142  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
143  if (area->spacetype == SPACE_VIEW3D) {
144  ED_render_view3d_update(update_ctx->depsgraph, window, area, updated);
145  }
146  }
147  }
148 
149  recursive_check = false;
150 }
151 
153 {
154  /* clear all render engines in this area */
155  ARegion *region;
156  wmWindowManager *wm = static_cast<wmWindowManager *>(bmain->wm.first);
157 
158  if (area->spacetype != SPACE_VIEW3D) {
159  return;
160  }
161 
162  for (region = static_cast<ARegion *>(area->regionbase.first); region; region = region->next) {
163  if (region->regiontype != RGN_TYPE_WINDOW || !(region->regiondata)) {
164  continue;
165  }
166  ED_view3d_stop_render_preview(wm, region);
167  }
168 }
169 
170 void ED_render_engine_changed(Main *bmain, const bool update_scene_data)
171 {
172  /* on changing the render engine type, clear all running render engines */
173  for (bScreen *screen = static_cast<bScreen *>(bmain->screens.first); screen;
174  screen = static_cast<bScreen *>(screen->id.next)) {
175  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
177  }
178  }
179  RE_FreePersistentData(nullptr);
180  /* Inform all render engines and draw managers. */
181  DEGEditorUpdateContext update_ctx = {nullptr};
182  update_ctx.bmain = bmain;
183  for (Scene *scene = static_cast<Scene *>(bmain->scenes.first); scene;
184  scene = static_cast<Scene *>(scene->id.next)) {
185  update_ctx.scene = scene;
186  LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
187  /* TDODO(sergey): Iterate over depsgraphs instead? */
188  update_ctx.depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, view_layer);
189  update_ctx.view_layer = view_layer;
190  ED_render_id_flush_update(&update_ctx, &scene->id);
191  }
192  if (scene->nodetree && update_scene_data) {
194  }
195  }
196 
197  /* Update #CacheFiles to ensure that procedurals are properly taken into account. */
198  LISTBASE_FOREACH (CacheFile *, cachefile, &bmain->cachefiles) {
199  /* Only update cache-files which are set to use a render procedural.
200  * We do not use #BKE_cachefile_uses_render_procedural here as we need to update regardless of
201  * the current engine or its settings. */
202  if (cachefile->use_render_procedural) {
204  /* Rebuild relations so that modifiers are reconnected to or disconnected from the
205  * cache-file. */
207  }
208  }
209 }
210 
212 {
213  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
215  }
216 }
217 
220 /* -------------------------------------------------------------------- */
229 static void material_changed(Main *UNUSED(bmain), Material *ma)
230 {
231  /* icons */
233 }
234 
235 static void lamp_changed(Main *UNUSED(bmain), Light *la)
236 {
237  /* icons */
239 }
240 
241 static void texture_changed(Main *bmain, Tex *tex)
242 {
243  Scene *scene;
244  ViewLayer *view_layer;
245  bNode *node;
246 
247  /* icons */
249 
250  for (scene = static_cast<Scene *>(bmain->scenes.first); scene;
251  scene = static_cast<Scene *>(scene->id.next)) {
252  /* paint overlays */
253  for (view_layer = static_cast<ViewLayer *>(scene->view_layers.first); view_layer;
254  view_layer = view_layer->next) {
256  }
257  /* find compositing nodes */
258  if (scene->use_nodes && scene->nodetree) {
259  for (node = static_cast<bNode *>(scene->nodetree->nodes.first); node; node = node->next) {
260  if (node->id == &tex->id) {
262  }
263  }
264  }
265  }
266 }
267 
268 static void world_changed(Main *UNUSED(bmain), World *wo)
269 {
270  /* icons */
272 }
273 
274 static void image_changed(Main *bmain, Image *ima)
275 {
276  Tex *tex;
277 
278  /* icons */
280 
281  /* textures */
282  for (tex = static_cast<Tex *>(bmain->textures.first); tex;
283  tex = static_cast<Tex *>(tex->id.next)) {
284  if (tex->type == TEX_IMAGE && tex->ima == ima) {
285  texture_changed(bmain, tex);
286  }
287  }
288 }
289 
290 static void scene_changed(Main *bmain, Scene *scene)
291 {
292  Object *ob;
293 
294  /* glsl */
295  for (ob = static_cast<Object *>(bmain->objects.first); ob;
296  ob = static_cast<Object *>(ob->id.next)) {
297  if (ob->mode & OB_MODE_TEXTURE_PAINT) {
299  ED_paint_proj_mesh_data_check(scene, ob, nullptr, nullptr, nullptr, nullptr);
300  }
301  }
302 }
303 
305 {
306  /* this can be called from render or baking thread when a python script makes
307  * changes, in that case we don't want to do any editor updates, and making
308  * GPU changes is not possible because OpenGL only works in the main thread */
309  if (!BLI_thread_is_main()) {
310  return;
311  }
312  Main *bmain = update_ctx->bmain;
313  /* Internal ID update handlers. */
314  switch (GS(id->name)) {
315  case ID_MA:
316  material_changed(bmain, (Material *)id);
317  break;
318  case ID_TE:
319  texture_changed(bmain, (Tex *)id);
320  break;
321  case ID_WO:
322  world_changed(bmain, (World *)id);
323  break;
324  case ID_LA:
325  lamp_changed(bmain, (Light *)id);
326  break;
327  case ID_IM:
328  image_changed(bmain, (Image *)id);
329  break;
330  case ID_SCE:
331  scene_changed(bmain, (Scene *)id);
332  break;
333  default:
334  break;
335  }
336 }
337 
void CTX_wm_region_set(bContext *C, struct ARegion *region)
Definition: context.c:1009
void CTX_data_scene_set(bContext *C, struct Scene *scene)
Definition: context.c:1271
bContext * CTX_create(void)
Definition: context.c:102
void CTX_free(bContext *C)
Definition: context.c:118
void CTX_wm_screen_set(bContext *C, struct bScreen *screen)
Definition: context.c:984
void CTX_wm_window_set(bContext *C, struct wmWindow *win)
Definition: context.c:966
void CTX_data_main_set(bContext *C, struct Main *bmain)
Definition: context.c:1084
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Definition: context.c:1505
void CTX_wm_area_set(bContext *C, struct ScrArea *area)
Definition: context.c:997
void CTX_wm_manager_set(bContext *C, struct wmWindowManager *wm)
Definition: context.c:950
void BKE_icon_changed(int icon_id)
Definition: icons.cc:637
int BKE_icon_id_ensure(struct ID *id)
Definition: icons.cc:699
General operations, lookup, etc. for materials.
void BKE_texpaint_slots_refresh_object(struct Scene *scene, struct Object *ob)
Definition: material.c:1564
void BKE_paint_invalidate_overlay_tex(struct Scene *scene, struct ViewLayer *view_layer, const struct Tex *tex)
struct Depsgraph * BKE_scene_ensure_depsgraph(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
Definition: scene.cc:3456
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
int BLI_thread_is_main(void)
Definition: threads.cc:207
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
struct Scene * DEG_get_input_scene(const Depsgraph *graph)
struct Main * DEG_get_bmain(const Depsgraph *graph)
struct ViewLayer * DEG_get_input_view_layer(const Depsgraph *graph)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
@ ID_TE
Definition: DNA_ID_enums.h:52
@ ID_IM
Definition: DNA_ID_enums.h:53
@ ID_LA
Definition: DNA_ID_enums.h:55
@ ID_SCE
Definition: DNA_ID_enums.h:45
@ ID_WO
Definition: DNA_ID_enums.h:59
@ ID_MA
Definition: DNA_ID_enums.h:51
@ OB_MODE_TEXTURE_PAINT
Object is a sort of wrapper for general info.
@ RGN_TYPE_WINDOW
@ SPACE_VIEW3D
void DRW_notify_view_update(const DRWUpdateContext *update_ctx)
void ED_node_tag_update_id(struct ID *id)
Definition: node_draw.cc:117
bool ED_paint_proj_mesh_data_check(struct Scene *scene, struct Object *ob, bool *uvs, bool *mat, bool *tex, bool *stencil)
void ED_view3d_stop_render_preview(struct wmWindowManager *wm, struct ARegion *region)
Definition: space_view3d.c:200
struct RenderEngineType * ED_view3d_engine_type(const struct Scene *scene, int drawtype)
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block TEX_IMAGE
#define RE_ENGINE_DO_UPDATE
Definition: RE_engine.h:61
#define C
Definition: RandGen.cpp:25
OperationNode * node
Scene scene
const Depsgraph * depsgraph
#define GS(x)
Definition: iris.c:225
static void area(int d1, int d2, int e1, int e2, float weights[2])
void ntreeCompositUpdateRLayers(bNodeTree *ntree)
void RE_FreePersistentData(const Scene *scene)
Definition: pipeline.c:660
static void material_changed(Main *UNUSED(bmain), Material *ma)
static void lamp_changed(Main *UNUSED(bmain), Light *la)
static void texture_changed(Main *bmain, Tex *tex)
static void scene_changed(Main *bmain, Scene *scene)
void ED_render_engine_area_exit(Main *bmain, ScrArea *area)
void ED_render_view3d_update(Depsgraph *depsgraph, wmWindow *window, ScrArea *area, const bool updated)
static void world_changed(Main *UNUSED(bmain), World *wo)
void ED_render_id_flush_update(const DEGEditorUpdateContext *update_ctx, ID *id)
void ED_render_view_layer_changed(Main *bmain, bScreen *screen)
void ED_render_engine_changed(Main *bmain, const bool update_scene_data)
static void image_changed(Main *bmain, Image *ima)
void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, const bool updated)
void * regiondata
struct ARegion * next
short regiontype
struct Depsgraph * depsgraph
struct ViewLayer * view_layer
struct Scene * scene
struct Scene * scene
Definition: DRW_engine.h:52
struct View3D * v3d
Definition: DRW_engine.h:55
struct ARegion * region
Definition: DRW_engine.h:54
struct RenderEngineType * engine_type
Definition: DRW_engine.h:56
struct ViewLayer * view_layer
Definition: DRW_engine.h:53
struct Main * bmain
Definition: DRW_engine.h:50
struct Depsgraph * depsgraph
Definition: DRW_engine.h:51
Definition: DNA_ID.h:368
void * next
Definition: DNA_ID.h:369
char name[66]
Definition: DNA_ID.h:378
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
ListBase scenes
Definition: BKE_main.h:168
ListBase wm
Definition: BKE_main.h:197
ListBase textures
Definition: BKE_main.h:175
ListBase screens
Definition: BKE_main.h:183
ListBase objects
Definition: BKE_main.h:170
ListBase cachefiles
Definition: BKE_main.h:202
struct RenderEngine * render_engine
void(* view_update)(struct RenderEngine *engine, const struct bContext *context, struct Depsgraph *depsgraph)
Definition: RE_engine.h:99
RenderEngineType * type
Definition: RE_engine.h:128
struct bNodeTree * nodetree
ListBase view_layers
char use_nodes
short type
struct Image * ima
View3DShading shading
struct ViewLayer * next
ListBase nodes
ListBase areabase
bScreen * WM_window_get_active_screen(const wmWindow *win)
Definition: wm_window.c:2300