Fix dupli-frames (and other duplis)
[blender.git] / source / blender / editors / render / render_update.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *
17  * The Original Code is Copyright (C) 2009 Blender Foundation.
18  * All rights reserved.
19  *
20  * Contributor(s): Blender Foundation
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_update.c
26  *  \ingroup edrend
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "DNA_lamp_types.h"
33 #include "DNA_material_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_view3d_types.h"
41 #include "DNA_workspace_types.h"
42 #include "DNA_world_types.h"
43 #include "DNA_windowmanager_types.h"
44
45 #include "DRW_engine.h"
46
47 #include "BLI_listbase.h"
48 #include "BLI_threads.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_context.h"
52 #include "BKE_DerivedMesh.h"
53 #include "BKE_icons.h"
54 #include "BKE_layer.h"
55 #include "BKE_main.h"
56 #include "BKE_material.h"
57 #include "BKE_node.h"
58 #include "BKE_paint.h"
59 #include "BKE_scene.h"
60 #include "BKE_workspace.h"
61
62 #include "GPU_lamp.h"
63 #include "GPU_material.h"
64 #include "GPU_buffers.h"
65
66 #include "RE_engine.h"
67 #include "RE_pipeline.h"
68
69 #include "ED_node.h"
70 #include "ED_render.h"
71 #include "ED_view3d.h"
72
73 #include "DEG_depsgraph.h"
74
75 #include "WM_api.h"
76
77 #include "render_intern.h"  // own include
78
79 extern Material defmaterial;
80
81 /***************************** Render Engines ********************************/
82
83 void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, int updated)
84 {
85         /* viewport rendering update on data changes, happens after depsgraph
86          * updates if there was any change. context is set to the 3d view */
87         Main *bmain = update_ctx->bmain;
88         Scene *scene = update_ctx->scene;
89         ViewLayer *view_layer = update_ctx->view_layer;
90         bContext *C;
91         wmWindowManager *wm;
92         wmWindow *win;
93         static bool recursive_check = false;
94
95         /* don't do this render engine update if we're updating the scene from
96          * other threads doing e.g. rendering or baking jobs */
97         if (!BLI_thread_is_main())
98                 return;
99
100         /* don't call this recursively for frame updates */
101         if (recursive_check)
102                 return;
103
104         /* Do not call if no WM available, see T42688. */
105         if (BLI_listbase_is_empty(&bmain->wm))
106                 return;
107
108         recursive_check = true;
109
110         C = CTX_create();
111         CTX_data_main_set(C, bmain);
112         CTX_data_scene_set(C, scene);
113
114         CTX_wm_manager_set(C, bmain->wm.first);
115         wm = bmain->wm.first;
116         
117         for (win = wm->windows.first; win; win = win->next) {
118                 bScreen *sc = WM_window_get_active_screen(win);
119                 ScrArea *sa;
120                 ARegion *ar;
121                 
122                 CTX_wm_window_set(C, win);
123                 WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
124                 ViewRender *view_render = BKE_viewrender_get(win->scene, workspace);
125
126                 for (sa = sc->areabase.first; sa; sa = sa->next) {
127                         if (sa->spacetype != SPACE_VIEW3D)
128                                 continue;
129
130                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
131                                 if (ar->regiontype != RGN_TYPE_WINDOW) {
132                                         continue;
133                                 }
134                                 RegionView3D *rv3d = ar->regiondata;
135                                 RenderEngine *engine = rv3d->render_engine;
136                                 /* call update if the scene changed, or if the render engine
137                                  * tagged itself for update (e.g. because it was busy at the
138                                  * time of the last update) */
139                                 if (engine && (updated || (engine->flag & RE_ENGINE_DO_UPDATE))) {
140
141                                         CTX_wm_screen_set(C, sc);
142                                         CTX_wm_area_set(C, sa);
143                                         CTX_wm_region_set(C, ar);
144
145                                         engine->flag &= ~RE_ENGINE_DO_UPDATE;
146                                         engine->type->view_update(engine, C);
147
148                                 }
149                                 else {
150                                         RenderEngineType *engine_type = RE_engines_find(view_render->engine_id);
151                                         if ((engine_type->flag & RE_USE_LEGACY_PIPELINE) == 0) {
152                                                 if (updated) {
153                                                         DRW_notify_view_update(
154                                                                 (&(DRWUpdateContext){
155                                                                     .bmain = bmain,
156                                                                     .scene = scene,
157                                                                     .view_layer = view_layer,
158                                                                     .ar = ar,
159                                                                     .v3d = (View3D *)sa->spacedata.first,
160                                                                     .engine_type = engine_type
161                                                                 }));
162                                                 }
163                                         }
164                                 }
165                         }
166                 }
167         }
168
169         CTX_free(C);
170
171         recursive_check = false;
172 }
173
174 void ED_render_engine_area_exit(Main *bmain, ScrArea *sa)
175 {
176         /* clear all render engines in this area */
177         ARegion *ar;
178         wmWindowManager *wm = bmain->wm.first;
179
180         if (sa->spacetype != SPACE_VIEW3D)
181                 return;
182
183         for (ar = sa->regionbase.first; ar; ar = ar->next) {
184                 if (ar->regiontype != RGN_TYPE_WINDOW || !(ar->regiondata))
185                         continue;
186                 ED_view3d_stop_render_preview(wm, ar);
187         }
188 }
189
190 void ED_render_engine_changed(Main *bmain)
191 {
192         /* on changing the render engine type, clear all running render engines */
193         for (bScreen *sc = bmain->screen.first; sc; sc = sc->id.next) {
194                 for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
195                         ED_render_engine_area_exit(bmain, sa);
196                 }
197         }
198         RE_FreePersistentData();
199         /* Inform all render engines and draw managers. */
200         DEGEditorUpdateContext update_ctx = {NULL};
201         update_ctx.bmain = bmain;
202         for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
203                 update_ctx.scene = scene;
204                 LINKLIST_FOREACH(ViewLayer *, view_layer, &scene->view_layers) {
205                         update_ctx.view_layer = view_layer;
206                         ED_render_id_flush_update(&update_ctx, &scene->id);
207                 }
208                 if (scene->nodetree) {
209                         ntreeCompositUpdateRLayers(scene->nodetree);
210                 }
211         }
212 }
213
214 /***************************** Updates ***********************************
215  * ED_render_id_flush_update gets called from DEG_id_tag_update, to do   *
216  * editor level updates when the ID changes. when these ID blocks are in *
217  * the dependency graph, we can get rid of the manual dependency checks  */
218
219 static void render_engine_flag_changed(Main *bmain, int update_flag)
220 {
221         bScreen *sc;
222         ScrArea *sa;
223         ARegion *ar;
224         
225         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
226                 for (sa = sc->areabase.first; sa; sa = sa->next) {
227                         if (sa->spacetype != SPACE_VIEW3D)
228                                 continue;
229                         
230                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
231                                 RegionView3D *rv3d;
232                                 
233                                 if (ar->regiontype != RGN_TYPE_WINDOW)
234                                         continue;
235                                 
236                                 rv3d = ar->regiondata;
237                                 if (rv3d->render_engine)
238                                         rv3d->render_engine->update_flag |= update_flag;
239                                 
240                         }
241                 }
242         }
243 }
244
245 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
246 {
247         int a;
248
249         if (!mtex)
250                 return 0;
251
252         for (a = 0; a < tot; a++)
253                 if (mtex[a] && mtex[a]->tex == tex)
254                         return 1;
255         
256         return 0;
257 }
258
259 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
260 {
261         bNode *node;
262
263         for (node = ntree->nodes.first; node; node = node->next) {
264                 if (node->id) {
265                         if (node->id == (ID *)tex) {
266                                 return 1;
267                         }
268                         else if (GS(node->id->name) == ID_MA) {
269                                 if (mtex_use_tex(((Material *)node->id)->mtex, MAX_MTEX, tex))
270                                         return 1;
271                         }
272                         else if (node->type == NODE_GROUP) {
273                                 if (nodes_use_tex((bNodeTree *)node->id, tex))
274                                         return 1;
275                         }
276                 }
277         }
278
279         return 0;
280 }
281
282 static int nodes_use_material(bNodeTree *ntree, Material *ma)
283 {
284         bNode *node;
285
286         for (node = ntree->nodes.first; node; node = node->next) {
287                 if (node->id) {
288                         if (node->id == (ID *)ma) {
289                                 return 1;
290                         }
291                         else if (node->type == NODE_GROUP) {
292                                 if (nodes_use_material((bNodeTree *)node->id, ma))
293                                         return 1;
294                         }
295                 }
296         }
297
298         return 0;
299 }
300
301 static void material_changed(Main *bmain, Material *ma)
302 {
303         Material *parent;
304         Object *ob;
305         Scene *scene;
306         int texture_draw = false;
307
308         /* icons */
309         BKE_icon_changed(BKE_icon_id_ensure(&ma->id));
310
311         /* glsl */
312         if (ma->id.tag & LIB_TAG_ID_RECALC) {
313                 if (!BLI_listbase_is_empty(&ma->gpumaterial)) {
314                         GPU_material_free(&ma->gpumaterial);
315                 }
316         }
317
318         /* find node materials using this */
319         for (parent = bmain->mat.first; parent; parent = parent->id.next) {
320                 if (parent->use_nodes && parent->nodetree && nodes_use_material(parent->nodetree, ma)) {
321                         /* pass */
322                 }
323                 else {
324                         continue;
325                 }
326
327                 BKE_icon_changed(BKE_icon_id_ensure(&parent->id));
328
329                 if (parent->gpumaterial.first)
330                         GPU_material_free(&parent->gpumaterial);
331         }
332
333         /* find if we have a scene with textured display */
334         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
335                 if (scene->customdata_mask & CD_MASK_MTFACE) {
336                         texture_draw = true;
337                         break;
338                 }
339         }
340
341         /* find textured objects */
342         if (texture_draw) {
343                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
344                         DerivedMesh *dm = ob->derivedFinal;
345                         Material ***material = give_matarar(ob);
346                         short a, *totmaterial = give_totcolp(ob);
347
348                         if (dm && totmaterial && material) {
349                                 for (a = 0; a < *totmaterial; a++) {
350                                         if ((*material)[a] == ma) {
351                                                 GPU_drawobject_free(dm);
352                                                 break;
353                                         }
354                                 }
355                         }
356                 }
357         }
358
359 }
360
361 static void lamp_changed(Main *bmain, Lamp *la)
362 {
363         Object *ob;
364
365         /* icons */
366         BKE_icon_changed(BKE_icon_id_ensure(&la->id));
367
368         /* glsl */
369         for (ob = bmain->object.first; ob; ob = ob->id.next)
370                 if (ob->data == la && ob->gpulamp.first)
371                         GPU_lamp_free(ob);
372
373         if (defmaterial.gpumaterial.first)
374                 GPU_material_free(&defmaterial.gpumaterial);
375 }
376
377 static int material_uses_texture(Material *ma, Tex *tex)
378 {
379         if (mtex_use_tex(ma->mtex, MAX_MTEX, tex))
380                 return true;
381         else if (ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex))
382                 return true;
383         
384         return false;
385 }
386
387 static void texture_changed(Main *bmain, Tex *tex)
388 {
389         Material *ma;
390         Lamp *la;
391         World *wo;
392         Scene *scene;
393         ViewLayer *view_layer;
394         Object *ob;
395         bNode *node;
396         bool texture_draw = false;
397
398         /* icons */
399         BKE_icon_changed(BKE_icon_id_ensure(&tex->id));
400
401         /* paint overlays */
402         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
403                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
404                         BKE_paint_invalidate_overlay_tex(scene, view_layer, tex);
405                 }
406         }
407
408         /* find materials */
409         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
410                 if (!material_uses_texture(ma, tex))
411                         continue;
412
413                 BKE_icon_changed(BKE_icon_id_ensure(&ma->id));
414
415                 if (ma->gpumaterial.first)
416                         GPU_material_free(&ma->gpumaterial);
417         }
418
419         /* find lamps */
420         for (la = bmain->lamp.first; la; la = la->id.next) {
421                 if (mtex_use_tex(la->mtex, MAX_MTEX, tex)) {
422                         lamp_changed(bmain, la);
423                 }
424                 else if (la->nodetree && nodes_use_tex(la->nodetree, tex)) {
425                         lamp_changed(bmain, la);
426                 }
427                 else {
428                         continue;
429                 }
430         }
431
432         /* find worlds */
433         for (wo = bmain->world.first; wo; wo = wo->id.next) {
434                 if (mtex_use_tex(wo->mtex, MAX_MTEX, tex)) {
435                         /* pass */
436                 }
437                 else if (wo->nodetree && nodes_use_tex(wo->nodetree, tex)) {
438                         /* pass */
439                 }
440                 else {
441                         continue;
442                 }
443
444                 BKE_icon_changed(BKE_icon_id_ensure(&wo->id));
445                 
446                 if (wo->gpumaterial.first)
447                         GPU_material_free(&wo->gpumaterial);            
448         }
449
450         /* find compositing nodes */
451         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
452                 if (scene->use_nodes && scene->nodetree) {
453                         for (node = scene->nodetree->nodes.first; node; node = node->next) {
454                                 if (node->id == &tex->id)
455                                         ED_node_tag_update_id(&scene->id);
456                         }
457                 }
458
459                 if (scene->customdata_mask & CD_MASK_MTFACE)
460                         texture_draw = true;
461         }
462
463         /* find textured objects */
464         if (texture_draw) {
465                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
466                         DerivedMesh *dm = ob->derivedFinal;
467                         Material ***material = give_matarar(ob);
468                         short a, *totmaterial = give_totcolp(ob);
469
470                         if (dm && totmaterial && material) {
471                                 for (a = 0; a < *totmaterial; a++) {
472                                         if (ob->matbits && ob->matbits[a])
473                                                 ma = ob->mat[a];
474                                         else
475                                                 ma = (*material)[a];
476
477                                         if (ma && material_uses_texture(ma, tex)) {
478                                                 GPU_drawobject_free(dm);
479                                                 break;
480                                         }
481                                 }
482                         }
483                 }
484         }
485 }
486
487 static void world_changed(Main *UNUSED(bmain), World *wo)
488 {
489         /* icons */
490         BKE_icon_changed(BKE_icon_id_ensure(&wo->id));
491
492         /* XXX temporary flag waiting for depsgraph proper tagging */
493         wo->update_flag = 1;
494
495         /* glsl */
496         if (wo->id.tag & LIB_TAG_ID_RECALC) {
497                 if (!BLI_listbase_is_empty(&defmaterial.gpumaterial)) {
498                         GPU_material_free(&defmaterial.gpumaterial);
499                 }
500                 if (!BLI_listbase_is_empty(&wo->gpumaterial)) {
501                         GPU_material_free(&wo->gpumaterial);
502                 }
503         }
504 }
505
506 static void image_changed(Main *bmain, Image *ima)
507 {
508         Tex *tex;
509
510         /* icons */
511         BKE_icon_changed(BKE_icon_id_ensure(&ima->id));
512
513         /* textures */
514         for (tex = bmain->tex.first; tex; tex = tex->id.next)
515                 if (tex->ima == ima)
516                         texture_changed(bmain, tex);
517 }
518
519 static void scene_changed(Main *bmain, Scene *scene)
520 {
521         Object *ob;
522
523         /* glsl */
524         for (ob = bmain->object.first; ob; ob = ob->id.next) {
525                 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
526                         BKE_texpaint_slots_refresh_object(scene, ob);
527                         BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
528                         GPU_drawobject_free(ob->derivedFinal);
529                 }
530         }
531 }
532
533 void ED_render_id_flush_update(const DEGEditorUpdateContext *update_ctx, ID *id)
534 {
535         /* this can be called from render or baking thread when a python script makes
536          * changes, in that case we don't want to do any editor updates, and making
537          * GPU changes is not possible because OpenGL only works in the main thread */
538         if (!BLI_thread_is_main()) {
539                 return;
540         }
541         Main *bmain = update_ctx->bmain;
542         Scene *scene = update_ctx->scene;
543         ViewLayer *view_layer = update_ctx->view_layer;
544         /* Internal ID update handlers. */
545         switch (GS(id->name)) {
546                 case ID_MA:
547                         material_changed(bmain, (Material *)id);
548                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_MA);
549                         break;
550                 case ID_TE:
551                         texture_changed(bmain, (Tex *)id);
552                         break;
553                 case ID_WO:
554                         world_changed(bmain, (World *)id);
555                         break;
556                 case ID_LA:
557                         lamp_changed(bmain, (Lamp *)id);
558                         break;
559                 case ID_IM:
560                         image_changed(bmain, (Image *)id);
561                         break;
562                 case ID_SCE:
563                         scene_changed(bmain, (Scene *)id);
564                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
565                         break;
566                 default:
567                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
568                         break;
569         }
570         /* Inform all draw managers about changes.
571          *
572          * TODO(sergey): This code is run for every updated ID, via flushing
573          * mechanism. How can we avoid iterating over the whole interface for
574          * every of those IDs? One of the ideas would be to call draw manager's
575          * ID update which is not bound to any of contexts.
576          */
577         {
578                 wmWindowManager *wm = bmain->wm.first;
579                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
580                         bScreen *sc = WM_window_get_active_screen(win);
581                         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
582                         ViewRender *view_render = BKE_viewrender_get(win->scene, workspace);
583                         for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
584                                 if (sa->spacetype != SPACE_VIEW3D) {
585                                         continue;
586                                 }
587                                 for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
588                                         if (ar->regiontype != RGN_TYPE_WINDOW) {
589                                                 continue;
590                                         }
591                                         RenderEngineType *engine_type = RE_engines_find(view_render->engine_id);
592                                         DRW_notify_id_update(
593                                                 (&(DRWUpdateContext){
594                                                     .bmain = bmain,
595                                                     .scene = scene,
596                                                     .view_layer = view_layer,
597                                                     .ar = ar,
598                                                     .v3d = (View3D *)sa->spacedata.first,
599                                                     .engine_type = engine_type
600                                                 }),
601                                                 id);
602                                 }
603                         }
604                 }
605         }
606 }
607
608
609 void ED_render_internal_init(void)
610 {
611         RenderEngineType *ret = RE_engines_find(RE_engine_id_BLENDER_RENDER);
612         
613         ret->view_update = render_view3d_update;
614         ret->render_to_view = render_view3d_draw;
615         
616 }