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