Blender  V2.93
render_update.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14  *
15  * The Original Code is Copyright (C) 2009 Blender Foundation.
16  * All rights reserved.
17  */
18 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "DNA_light_types.h"
27 #include "DNA_material_types.h"
28 #include "DNA_node_types.h"
29 #include "DNA_object_types.h"
30 #include "DNA_scene_types.h"
31 #include "DNA_screen_types.h"
32 #include "DNA_space_types.h"
33 #include "DNA_view3d_types.h"
35 #include "DNA_world_types.h"
36 
37 #include "DRW_engine.h"
38 
39 #include "BLI_listbase.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
42 
43 #include "BKE_context.h"
44 #include "BKE_icons.h"
45 #include "BKE_main.h"
46 #include "BKE_material.h"
47 #include "BKE_node.h"
48 #include "BKE_paint.h"
49 #include "BKE_scene.h"
50 
51 #include "RE_engine.h"
52 #include "RE_pipeline.h"
53 
54 #include "ED_node.h"
55 #include "ED_paint.h"
56 #include "ED_render.h"
57 #include "ED_view3d.h"
58 
59 #include "DEG_depsgraph.h"
60 #include "DEG_depsgraph_query.h"
61 
62 #include "WM_api.h"
63 
64 #include <stdio.h>
65 
66 /***************************** Render Engines ********************************/
67 
68 /* Update 3D viewport render or draw engine on changes to the scene or view settings . */
70  wmWindow *window,
71  ScrArea *area,
72  const bool updated)
73 {
74  Main *bmain = DEG_get_bmain(depsgraph);
77 
78  LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
79  if (region->regiontype != RGN_TYPE_WINDOW) {
80  continue;
81  }
82 
83  View3D *v3d = area->spacedata.first;
84  RegionView3D *rv3d = region->regiondata;
85  RenderEngine *engine = rv3d->render_engine;
86 
87  /* call update if the scene changed, or if the render engine
88  * tagged itself for update (e.g. because it was busy at the
89  * time of the last update) */
90  if (engine && (updated || (engine->flag & RE_ENGINE_DO_UPDATE))) {
91  /* Create temporary context to execute callback in. */
92  bContext *C = CTX_create();
93  CTX_data_main_set(C, bmain);
95  CTX_wm_manager_set(C, bmain->wm.first);
96  CTX_wm_window_set(C, window);
99  CTX_wm_region_set(C, region);
100 
101  engine->flag &= ~RE_ENGINE_DO_UPDATE;
102  /* NOTE: Important to pass non-updated depsgraph, This is because this function is called
103  * from inside dependency graph evaluation. Additionally, if we pass fully evaluated one
104  * we will lose updates stored in the graph. */
105  engine->type->view_update(engine, C, CTX_data_depsgraph_pointer(C));
106 
107  CTX_free(C);
108  }
109  else {
111  if (updated) {
113  .bmain = bmain,
114  .depsgraph = depsgraph,
115  .scene = scene,
116  .view_layer = view_layer,
117  .region = region,
118  .v3d = v3d,
119  .engine_type = engine_type,
120  }));
121  }
122  }
123  }
124 }
125 
126 /* Update all 3D viewport render and draw engines on changes to the scene.
127  * This is called by the dependency graph when it detects changes. */
128 void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, const bool updated)
129 {
130  Main *bmain = update_ctx->bmain;
131  static bool recursive_check = false;
132 
133  /* don't do this render engine update if we're updating the scene from
134  * other threads doing e.g. rendering or baking jobs */
135  if (!BLI_thread_is_main()) {
136  return;
137  }
138 
139  /* don't call this recursively for frame updates */
140  if (recursive_check) {
141  return;
142  }
143 
144  /* Do not call if no WM available, see T42688. */
145  if (BLI_listbase_is_empty(&bmain->wm)) {
146  return;
147  }
148 
149  recursive_check = true;
150 
151  wmWindowManager *wm = bmain->wm.first;
152  LISTBASE_FOREACH (wmWindow *, window, &wm->windows) {
153  bScreen *screen = WM_window_get_active_screen(window);
154 
155  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
156  if (area->spacetype == SPACE_VIEW3D) {
157  ED_render_view3d_update(update_ctx->depsgraph, window, area, updated);
158  }
159  }
160  }
161 
162  recursive_check = false;
163 }
164 
166 {
167  /* clear all render engines in this area */
168  ARegion *region;
169  wmWindowManager *wm = bmain->wm.first;
170 
171  if (area->spacetype != SPACE_VIEW3D) {
172  return;
173  }
174 
175  for (region = area->regionbase.first; region; region = region->next) {
176  if (region->regiontype != RGN_TYPE_WINDOW || !(region->regiondata)) {
177  continue;
178  }
179  ED_view3d_stop_render_preview(wm, region);
180  }
181 }
182 
183 void ED_render_engine_changed(Main *bmain, const bool update_scene_data)
184 {
185  /* on changing the render engine type, clear all running render engines */
186  for (bScreen *screen = bmain->screens.first; screen; screen = screen->id.next) {
187  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
189  }
190  }
192  /* Inform all render engines and draw managers. */
193  DEGEditorUpdateContext update_ctx = {NULL};
194  update_ctx.bmain = bmain;
195  for (Scene *scene = bmain->scenes.first; scene; scene = scene->id.next) {
196  update_ctx.scene = scene;
197  LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
198  /* TDODO(sergey): Iterate over depsgraphs instead? */
199  update_ctx.depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, view_layer);
200  update_ctx.view_layer = view_layer;
201  ED_render_id_flush_update(&update_ctx, &scene->id);
202  }
203  if (scene->nodetree && update_scene_data) {
205  }
206  }
207 }
208 
210 {
211  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
213  }
214 }
215 
216 /***************************** Updates ***********************************
217  * ED_render_id_flush_update gets called from DEG_id_tag_update, to do *
218  * editor level updates when the ID changes. when these ID blocks are in *
219  * the dependency graph, we can get rid of the manual dependency checks */
220 
221 static void material_changed(Main *UNUSED(bmain), Material *ma)
222 {
223  /* icons */
225 }
226 
227 static void lamp_changed(Main *UNUSED(bmain), Light *la)
228 {
229  /* icons */
231 }
232 
233 static void texture_changed(Main *bmain, Tex *tex)
234 {
235  Scene *scene;
236  ViewLayer *view_layer;
237  bNode *node;
238 
239  /* icons */
241 
242  for (scene = bmain->scenes.first; scene; scene = scene->id.next) {
243  /* paint overlays */
244  for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
246  }
247  /* find compositing nodes */
248  if (scene->use_nodes && scene->nodetree) {
249  for (node = scene->nodetree->nodes.first; node; node = node->next) {
250  if (node->id == &tex->id) {
252  }
253  }
254  }
255  }
256 }
257 
258 static void world_changed(Main *UNUSED(bmain), World *wo)
259 {
260  /* icons */
262 }
263 
264 static void image_changed(Main *bmain, Image *ima)
265 {
266  Tex *tex;
267 
268  /* icons */
270 
271  /* textures */
272  for (tex = bmain->textures.first; tex; tex = tex->id.next) {
273  if (tex->type == TEX_IMAGE && tex->ima == ima) {
274  texture_changed(bmain, tex);
275  }
276  }
277 }
278 
279 static void scene_changed(Main *bmain, Scene *scene)
280 {
281  Object *ob;
282 
283  /* glsl */
284  for (ob = bmain->objects.first; ob; ob = ob->id.next) {
285  if (ob->mode & OB_MODE_TEXTURE_PAINT) {
288  }
289  }
290 }
291 
293 {
294  /* this can be called from render or baking thread when a python script makes
295  * changes, in that case we don't want to do any editor updates, and making
296  * GPU changes is not possible because OpenGL only works in the main thread */
297  if (!BLI_thread_is_main()) {
298  return;
299  }
300  Main *bmain = update_ctx->bmain;
301  /* Internal ID update handlers. */
302  switch (GS(id->name)) {
303  case ID_MA:
304  material_changed(bmain, (Material *)id);
305  break;
306  case ID_TE:
307  texture_changed(bmain, (Tex *)id);
308  break;
309  case ID_WO:
310  world_changed(bmain, (World *)id);
311  break;
312  case ID_LA:
313  lamp_changed(bmain, (Light *)id);
314  break;
315  case ID_IM:
316  image_changed(bmain, (Image *)id);
317  break;
318  case ID_SCE:
319  scene_changed(bmain, (Scene *)id);
320  break;
321  default:
322  break;
323  }
324 }
void CTX_wm_region_set(bContext *C, struct ARegion *region)
Definition: context.c:985
void CTX_data_scene_set(bContext *C, struct Scene *scene)
Definition: context.c:1197
bContext * CTX_create(void)
Definition: context.c:105
void CTX_free(bContext *C)
Definition: context.c:119
void CTX_wm_screen_set(bContext *C, struct bScreen *screen)
Definition: context.c:960
void CTX_wm_window_set(bContext *C, struct wmWindow *win)
Definition: context.c:942
void CTX_data_main_set(bContext *C, struct Main *bmain)
Definition: context.c:1028
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Definition: context.c:1401
void CTX_wm_area_set(bContext *C, struct ScrArea *area)
Definition: context.c:973
void CTX_wm_manager_set(bContext *C, struct wmWindowManager *wm)
Definition: context.c:926
void BKE_icon_changed(const int icon_id)
Definition: icons.cc:678
int BKE_icon_id_ensure(struct ID *id)
Definition: icons.cc:740
General operations, lookup, etc. for materials.
void BKE_texpaint_slots_refresh_object(struct Scene *scene, struct Object *ob)
Definition: material.c:1361
void ntreeCompositUpdateRLayers(struct bNodeTree *ntree)
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.c:3526
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
int BLI_thread_is_main(void)
Definition: threads.cc:234
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
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_TE
Definition: DNA_ID_enums.h:64
@ ID_IM
Definition: DNA_ID_enums.h:65
@ ID_LA
Definition: DNA_ID_enums.h:67
@ ID_SCE
Definition: DNA_ID_enums.h:57
@ ID_WO
Definition: DNA_ID_enums.h:71
@ ID_MA
Definition: DNA_ID_enums.h:63
@ 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:124
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:225
struct RenderEngineType * ED_view3d_engine_type(const struct Scene *scene, int drawtype)
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume TEX_IMAGE
#define RE_ENGINE_DO_UPDATE
Definition: RE_engine.h:73
#define C
Definition: RandGen.cpp:39
OperationNode * node
Scene scene
const Depsgraph * depsgraph
#define GS(x)
Definition: iris.c:241
static void area(int d1, int d2, int e1, int e2, float weights[2])
void RE_FreePersistentData(const Scene *scene)
Definition: pipeline.c:700
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)
Definition: render_update.c:69
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
Definition: DNA_ID.h:273
void * next
Definition: DNA_ID.h:274
char name[66]
Definition: DNA_ID.h:283
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase scenes
Definition: BKE_main.h:146
ListBase wm
Definition: BKE_main.h:175
ListBase textures
Definition: BKE_main.h:153
ListBase screens
Definition: BKE_main.h:161
ListBase objects
Definition: BKE_main.h:148
struct RenderEngine * render_engine
void(* view_update)(struct RenderEngine *engine, const struct bContext *context, struct Depsgraph *depsgraph)
Definition: RE_engine.h:97
RenderEngineType * type
Definition: RE_engine.h:126
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:2372