Move ID recalc flags into dedicated field in ID
[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_world_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "BLI_listbase.h"
45 #include "BLI_threads.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_context.h"
49 #include "BKE_DerivedMesh.h"
50 #include "BKE_icons.h"
51 #include "BKE_main.h"
52 #include "BKE_material.h"
53 #include "BKE_node.h"
54 #include "BKE_paint.h"
55 #include "BKE_scene.h"
56
57 #include "GPU_material.h"
58 #include "GPU_buffers.h"
59
60 #include "RE_engine.h"
61 #include "RE_pipeline.h"
62
63 #include "ED_node.h"
64 #include "ED_render.h"
65 #include "ED_view3d.h"
66
67 #include "render_intern.h"  // own include
68
69 extern Material defmaterial;
70
71 /***************************** Render Engines ********************************/
72
73 void ED_render_scene_update(Main *bmain, Scene *scene, int updated)
74 {
75         /* viewport rendering update on data changes, happens after depsgraph
76          * updates if there was any change. context is set to the 3d view */
77         bContext *C;
78         wmWindowManager *wm;
79         wmWindow *win;
80         static bool recursive_check = false;
81
82         /* don't do this render engine update if we're updating the scene from
83          * other threads doing e.g. rendering or baking jobs */
84         if (!BLI_thread_is_main())
85                 return;
86
87         /* don't call this recursively for frame updates */
88         if (recursive_check)
89                 return;
90
91         /* Do not call if no WM available, see T42688. */
92         if (BLI_listbase_is_empty(&bmain->wm))
93                 return;
94
95         recursive_check = true;
96
97         C = CTX_create();
98         CTX_data_main_set(C, bmain);
99         CTX_data_scene_set(C, scene);
100
101         CTX_wm_manager_set(C, bmain->wm.first);
102         wm = bmain->wm.first;
103         
104         for (win = wm->windows.first; win; win = win->next) {
105                 bScreen *sc = win->screen;
106                 ScrArea *sa;
107                 ARegion *ar;
108                 
109                 CTX_wm_window_set(C, win);
110                 
111                 for (sa = sc->areabase.first; sa; sa = sa->next) {
112                         if (sa->spacetype != SPACE_VIEW3D)
113                                 continue;
114
115                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
116                                 RegionView3D *rv3d;
117                                 RenderEngine *engine;
118
119                                 if (ar->regiontype != RGN_TYPE_WINDOW)
120                                         continue;
121
122                                 rv3d = ar->regiondata;
123                                 engine = rv3d->render_engine;
124
125                                 /* call update if the scene changed, or if the render engine
126                                  * tagged itself for update (e.g. because it was busy at the
127                                  * time of the last update) */
128                                 if (engine && (updated || (engine->flag & RE_ENGINE_DO_UPDATE))) {
129
130                                         CTX_wm_screen_set(C, sc);
131                                         CTX_wm_area_set(C, sa);
132                                         CTX_wm_region_set(C, ar);
133
134                                         engine->flag &= ~RE_ENGINE_DO_UPDATE;
135                                         engine->type->view_update(engine, C);
136                                 }
137                         }
138                 }
139         }
140
141         CTX_free(C);
142
143         recursive_check = false;
144 }
145
146 void ED_render_scene_update_pre(Main *bmain, Scene *scene, bool time)
147 {
148         /* Blender internal might access to the data which is gonna to be freed
149          * by the scene update functions. This applies for example to simulation
150          * data like smoke and fire.
151          */
152         if (time && !BKE_scene_use_new_shading_nodes(scene)) {
153                 bScreen *sc;
154                 ScrArea *sa;
155                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
156                         for (sa = sc->areabase.first; sa; sa = sa->next) {
157                                 ED_render_engine_area_exit(bmain, sa);
158                         }
159                 }
160         }
161 }
162
163 void ED_render_engine_area_exit(Main *bmain, ScrArea *sa)
164 {
165         /* clear all render engines in this area */
166         ARegion *ar;
167         wmWindowManager *wm = bmain->wm.first;
168
169         if (sa->spacetype != SPACE_VIEW3D)
170                 return;
171
172         for (ar = sa->regionbase.first; ar; ar = ar->next) {
173                 if (ar->regiontype != RGN_TYPE_WINDOW || !(ar->regiondata))
174                         continue;
175                 ED_view3d_stop_render_preview(wm, ar);
176         }
177 }
178
179 void ED_render_engine_changed(Main *bmain)
180 {
181         /* on changing the render engine type, clear all running render engines */
182         bScreen *sc;
183         ScrArea *sa;
184         Scene *scene;
185
186         for (sc = bmain->screen.first; sc; sc = sc->id.next)
187                 for (sa = sc->areabase.first; sa; sa = sa->next)
188                         ED_render_engine_area_exit(bmain, sa);
189
190         RE_FreePersistentData();
191
192         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
193                 ED_render_id_flush_update(bmain, &scene->id);
194                 if (scene->nodetree) {
195                         ntreeCompositUpdateRLayers(scene->nodetree);
196                 }
197         }
198 }
199
200 /***************************** Updates ***********************************
201  * ED_render_id_flush_update gets called from DAG_id_tag_update, to do   *
202  * editor level updates when the ID changes. when these ID blocks are in *
203  * the dependency graph, we can get rid of the manual dependency checks  */
204
205 static void render_engine_flag_changed(Main *bmain, int update_flag)
206 {
207         bScreen *sc;
208         ScrArea *sa;
209         ARegion *ar;
210         
211         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
212                 for (sa = sc->areabase.first; sa; sa = sa->next) {
213                         if (sa->spacetype != SPACE_VIEW3D)
214                                 continue;
215                         
216                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
217                                 RegionView3D *rv3d;
218                                 
219                                 if (ar->regiontype != RGN_TYPE_WINDOW)
220                                         continue;
221                                 
222                                 rv3d = ar->regiondata;
223                                 if (rv3d->render_engine)
224                                         rv3d->render_engine->update_flag |= update_flag;
225                                 
226                         }
227                 }
228         }
229 }
230
231 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
232 {
233         int a;
234
235         if (!mtex)
236                 return 0;
237
238         for (a = 0; a < tot; a++)
239                 if (mtex[a] && mtex[a]->tex == tex)
240                         return 1;
241         
242         return 0;
243 }
244
245 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
246 {
247         bNode *node;
248
249         for (node = ntree->nodes.first; node; node = node->next) {
250                 if (node->id) {
251                         if (node->id == (ID *)tex) {
252                                 return 1;
253                         }
254                         else if (GS(node->id->name) == ID_MA) {
255                                 if (mtex_use_tex(((Material *)node->id)->mtex, MAX_MTEX, tex))
256                                         return 1;
257                         }
258                         else if (node->type == NODE_GROUP) {
259                                 if (nodes_use_tex((bNodeTree *)node->id, tex))
260                                         return 1;
261                         }
262                 }
263         }
264
265         return 0;
266 }
267
268 static int nodes_use_material(bNodeTree *ntree, Material *ma)
269 {
270         bNode *node;
271
272         for (node = ntree->nodes.first; node; node = node->next) {
273                 if (node->id) {
274                         if (node->id == (ID *)ma) {
275                                 return 1;
276                         }
277                         else if (node->type == NODE_GROUP) {
278                                 if (nodes_use_material((bNodeTree *)node->id, ma))
279                                         return 1;
280                         }
281                 }
282         }
283
284         return 0;
285 }
286
287 static void material_changed(Main *bmain, Material *ma)
288 {
289         Material *parent;
290         Object *ob;
291         Scene *scene;
292         int texture_draw = false;
293
294         /* icons */
295         BKE_icon_changed(BKE_icon_id_ensure(&ma->id));
296
297         /* glsl */
298         if (ma->gpumaterial.first)
299                 GPU_material_free(&ma->gpumaterial);
300
301         /* find node materials using this */
302         for (parent = bmain->mat.first; parent; parent = parent->id.next) {
303                 if (parent->use_nodes && parent->nodetree && nodes_use_material(parent->nodetree, ma)) {
304                         /* pass */
305                 }
306                 else {
307                         continue;
308                 }
309
310                 BKE_icon_changed(BKE_icon_id_ensure(&parent->id));
311
312                 if (parent->gpumaterial.first)
313                         GPU_material_free(&parent->gpumaterial);
314         }
315
316         /* find if we have a scene with textured display */
317         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
318                 if (scene->customdata_mask & CD_MASK_MTFACE) {
319                         texture_draw = true;
320                         break;
321                 }
322         }
323
324         /* find textured objects */
325         if (texture_draw) {
326                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
327                         DerivedMesh *dm = ob->derivedFinal;
328                         Material ***material = give_matarar(ob);
329                         short a, *totmaterial = give_totcolp(ob);
330
331                         if (dm && totmaterial && material) {
332                                 for (a = 0; a < *totmaterial; a++) {
333                                         if ((*material)[a] == ma) {
334                                                 GPU_drawobject_free(dm);
335                                                 break;
336                                         }
337                                 }
338                         }
339                 }
340         }
341
342 }
343
344 static void lamp_changed(Main *bmain, Lamp *la)
345 {
346         Object *ob;
347         Material *ma;
348
349         /* icons */
350         BKE_icon_changed(BKE_icon_id_ensure(&la->id));
351
352         /* glsl */
353         for (ob = bmain->object.first; ob; ob = ob->id.next)
354                 if (ob->data == la && ob->gpulamp.first)
355                         GPU_lamp_free(ob);
356
357         for (ma = bmain->mat.first; ma; ma = ma->id.next)
358                 if (ma->gpumaterial.first)
359                         GPU_material_free(&ma->gpumaterial);
360
361         if (defmaterial.gpumaterial.first)
362                 GPU_material_free(&defmaterial.gpumaterial);
363 }
364
365 static int material_uses_texture(Material *ma, Tex *tex)
366 {
367         if (mtex_use_tex(ma->mtex, MAX_MTEX, tex))
368                 return true;
369         else if (ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex))
370                 return true;
371         
372         return false;
373 }
374
375 static void texture_changed(Main *bmain, Tex *tex)
376 {
377         Material *ma;
378         Lamp *la;
379         World *wo;
380         Scene *scene;
381         Object *ob;
382         bNode *node;
383         bool texture_draw = false;
384
385         /* icons */
386         BKE_icon_changed(BKE_icon_id_ensure(&tex->id));
387
388         /* paint overlays */
389         for (scene = bmain->scene.first; scene; scene = scene->id.next)
390                 BKE_paint_invalidate_overlay_tex(scene, tex);
391
392         /* find materials */
393         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
394                 if (!material_uses_texture(ma, tex))
395                         continue;
396
397                 BKE_icon_changed(BKE_icon_id_ensure(&ma->id));
398
399                 if (ma->gpumaterial.first)
400                         GPU_material_free(&ma->gpumaterial);
401         }
402
403         /* find lamps */
404         for (la = bmain->lamp.first; la; la = la->id.next) {
405                 if (mtex_use_tex(la->mtex, MAX_MTEX, tex)) {
406                         lamp_changed(bmain, la);
407                 }
408                 else if (la->nodetree && nodes_use_tex(la->nodetree, tex)) {
409                         lamp_changed(bmain, la);
410                 }
411                 else {
412                         continue;
413                 }
414         }
415
416         /* find worlds */
417         for (wo = bmain->world.first; wo; wo = wo->id.next) {
418                 if (mtex_use_tex(wo->mtex, MAX_MTEX, tex)) {
419                         /* pass */
420                 }
421                 else if (wo->nodetree && nodes_use_tex(wo->nodetree, tex)) {
422                         /* pass */
423                 }
424                 else {
425                         continue;
426                 }
427
428                 BKE_icon_changed(BKE_icon_id_ensure(&wo->id));
429                 
430                 if (wo->gpumaterial.first)
431                         GPU_material_free(&wo->gpumaterial);            
432         }
433
434         /* find compositing nodes */
435         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
436                 if (scene->use_nodes && scene->nodetree) {
437                         for (node = scene->nodetree->nodes.first; node; node = node->next) {
438                                 if (node->id == &tex->id)
439                                         ED_node_tag_update_id(&scene->id);
440                         }
441                 }
442
443                 if (scene->customdata_mask & CD_MASK_MTFACE)
444                         texture_draw = true;
445         }
446
447         /* find textured objects */
448         if (texture_draw) {
449                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
450                         DerivedMesh *dm = ob->derivedFinal;
451                         Material ***material = give_matarar(ob);
452                         short a, *totmaterial = give_totcolp(ob);
453
454                         if (dm && totmaterial && material) {
455                                 for (a = 0; a < *totmaterial; a++) {
456                                         if (ob->matbits && ob->matbits[a])
457                                                 ma = ob->mat[a];
458                                         else
459                                                 ma = (*material)[a];
460
461                                         if (ma && material_uses_texture(ma, tex)) {
462                                                 GPU_drawobject_free(dm);
463                                                 break;
464                                         }
465                                 }
466                         }
467                 }
468         }
469 }
470
471 static void world_changed(Main *bmain, World *wo)
472 {
473         Material *ma;
474
475         /* icons */
476         BKE_icon_changed(BKE_icon_id_ensure(&wo->id));
477         
478         /* glsl */
479         for (ma = bmain->mat.first; ma; ma = ma->id.next)
480                 if (ma->gpumaterial.first)
481                         GPU_material_free(&ma->gpumaterial);
482
483         if (defmaterial.gpumaterial.first)
484                 GPU_material_free(&defmaterial.gpumaterial);
485         
486         if (wo->gpumaterial.first)
487                 GPU_material_free(&wo->gpumaterial);
488 }
489
490 static void image_changed(Main *bmain, Image *ima)
491 {
492         Tex *tex;
493
494         /* icons */
495         BKE_icon_changed(BKE_icon_id_ensure(&ima->id));
496
497         /* textures */
498         for (tex = bmain->tex.first; tex; tex = tex->id.next)
499                 if (tex->ima == ima)
500                         texture_changed(bmain, tex);
501 }
502
503 static void scene_changed(Main *bmain, Scene *scene)
504 {
505         Object *ob;
506         Material *ma;
507         World *wo;
508
509         /* glsl */
510         for (ob = bmain->object.first; ob; ob = ob->id.next) {
511                 if (ob->gpulamp.first)
512                         GPU_lamp_free(ob);
513                 
514                 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
515                         BKE_texpaint_slots_refresh_object(scene, ob);
516                         BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
517                         GPU_drawobject_free(ob->derivedFinal);
518                 }
519         }
520
521         for (ma = bmain->mat.first; ma; ma = ma->id.next)
522                 if (ma->gpumaterial.first)
523                         GPU_material_free(&ma->gpumaterial);
524
525         for (wo = bmain->world.first; wo; wo = wo->id.next)
526                 if (wo->gpumaterial.first)
527                         GPU_material_free(&wo->gpumaterial);
528         
529         if (defmaterial.gpumaterial.first)
530                 GPU_material_free(&defmaterial.gpumaterial);
531 }
532
533 void ED_render_id_flush_update(Main *bmain, 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         switch (GS(id->name)) {
542                 case ID_MA:
543                         material_changed(bmain, (Material *)id);
544                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_MA);
545                         break;
546                 case ID_TE:
547                         texture_changed(bmain, (Tex *)id);
548                         break;
549                 case ID_WO:
550                         world_changed(bmain, (World *)id);
551                         break;
552                 case ID_LA:
553                         lamp_changed(bmain, (Lamp *)id);
554                         break;
555                 case ID_IM:
556                         image_changed(bmain, (Image *)id);
557                         break;
558                 case ID_SCE:
559                         scene_changed(bmain, (Scene *)id);
560                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
561                         break;
562                 default:
563                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
564                         break;
565         }
566         
567 }
568
569
570 void ED_render_internal_init(void)
571 {
572         RenderEngineType *ret = RE_engines_find(RE_engine_id_BLENDER_RENDER);
573         
574         ret->view_update = render_view3d_update;
575         ret->view_draw = render_view3d_draw;
576         
577 }