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