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