Texture painting:
[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
170         for (sc = bmain->screen.first; sc; sc = sc->id.next)
171                 for (sa = sc->areabase.first; sa; sa = sa->next)
172                         ED_render_engine_area_exit(sa);
173
174         RE_FreePersistentData();
175
176         for (scene = bmain->scene.first; scene; scene = scene->id.next)
177                 ED_render_id_flush_update(bmain, &scene->id);
178 }
179
180 /***************************** Updates ***********************************
181  * ED_render_id_flush_update gets called from DAG_id_tag_update, to do   *
182  * editor level updates when the ID changes. when these ID blocks are in *
183  * the dependency graph, we can get rid of the manual dependency checks  */
184
185 static void render_engine_flag_changed(Main *bmain, int update_flag)
186 {
187         bScreen *sc;
188         ScrArea *sa;
189         ARegion *ar;
190         
191         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
192                 for (sa = sc->areabase.first; sa; sa = sa->next) {
193                         if (sa->spacetype != SPACE_VIEW3D)
194                                 continue;
195                         
196                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
197                                 RegionView3D *rv3d;
198                                 
199                                 if (ar->regiontype != RGN_TYPE_WINDOW)
200                                         continue;
201                                 
202                                 rv3d = ar->regiondata;
203                                 if (rv3d->render_engine)
204                                         rv3d->render_engine->update_flag |= update_flag;
205                                 
206                         }
207                 }
208         }
209 }
210
211 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
212 {
213         int a;
214
215         if (!mtex)
216                 return 0;
217
218         for (a = 0; a < tot; a++)
219                 if (mtex[a] && mtex[a]->tex == tex)
220                         return 1;
221         
222         return 0;
223 }
224
225 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
226 {
227         bNode *node;
228
229         for (node = ntree->nodes.first; node; node = node->next) {
230                 if (node->id) {
231                         if (node->id == (ID *)tex) {
232                                 return 1;
233                         }
234                         else if (GS(node->id->name) == ID_MA) {
235                                 if (mtex_use_tex(((Material *)node->id)->mtex, MAX_MTEX, tex))
236                                         return 1;
237                         }
238                         else if (node->type == NODE_GROUP) {
239                                 if (nodes_use_tex((bNodeTree *)node->id, tex))
240                                         return 1;
241                         }
242                 }
243         }
244
245         return 0;
246 }
247
248 static int nodes_use_material(bNodeTree *ntree, Material *ma)
249 {
250         bNode *node;
251
252         for (node = ntree->nodes.first; node; node = node->next) {
253                 if (node->id) {
254                         if (node->id == (ID *)ma) {
255                                 return 1;
256                         }
257                         else if (node->type == NODE_GROUP) {
258                                 if (nodes_use_material((bNodeTree *)node->id, ma))
259                                         return 1;
260                         }
261                 }
262         }
263
264         return 0;
265 }
266
267 static void material_changed(Main *bmain, Material *ma)
268 {
269         Material *parent;
270         Object *ob;
271         Scene *scene;
272         int texture_draw = false;
273
274         /* icons */
275         BKE_icon_changed(BKE_icon_getid(&ma->id));
276
277         /* glsl */
278         if (ma->gpumaterial.first)
279                 GPU_material_free(ma);
280
281         /* find node materials using this */
282         for (parent = bmain->mat.first; parent; parent = parent->id.next) {
283                 if (parent->use_nodes && parent->nodetree && nodes_use_material(parent->nodetree, ma)) {
284                         /* pass */
285                 }
286                 else {
287                         continue;
288                 }
289
290                 BKE_icon_changed(BKE_icon_getid(&parent->id));
291
292                 if (parent->gpumaterial.first)
293                         GPU_material_free(parent);
294         }
295
296         /* find if we have a scene with textured display */
297         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
298                 if (scene->customdata_mask & CD_MASK_MTFACE) {
299                         texture_draw = true;
300                         break;
301                 }
302         }
303
304         /* find textured objects */
305         if (texture_draw && !(U.gameflags & USER_DISABLE_VBO)) {
306                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
307                         DerivedMesh *dm = ob->derivedFinal;
308                         Material ***material = give_matarar(ob);
309                         short a, *totmaterial = give_totcolp(ob);
310
311                         if (dm && totmaterial && material) {
312                                 for (a = 0; a < *totmaterial; a++) {
313                                         if ((*material)[a] == ma) {
314                                                 GPU_drawobject_free(dm);
315                                                 break;
316                                         }
317                                 }
318                         }
319                 }
320         }
321
322 }
323
324 static void lamp_changed(Main *bmain, Lamp *la)
325 {
326         Object *ob;
327         Material *ma;
328
329         /* icons */
330         BKE_icon_changed(BKE_icon_getid(&la->id));
331
332         /* glsl */
333         for (ob = bmain->object.first; ob; ob = ob->id.next)
334                 if (ob->data == la && ob->gpulamp.first)
335                         GPU_lamp_free(ob);
336
337         for (ma = bmain->mat.first; ma; ma = ma->id.next)
338                 if (ma->gpumaterial.first)
339                         GPU_material_free(ma);
340
341         if (defmaterial.gpumaterial.first)
342                 GPU_material_free(&defmaterial);
343 }
344
345 static int material_uses_texture(Material *ma, Tex *tex)
346 {
347         if (mtex_use_tex(ma->mtex, MAX_MTEX, tex))
348                 return true;
349         else if (ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex))
350                 return true;
351         
352         return false;
353 }
354
355 static void texture_changed(Main *bmain, Tex *tex)
356 {
357         Material *ma;
358         Lamp *la;
359         World *wo;
360         Scene *scene;
361         Object *ob;
362         bNode *node;
363         bool texture_draw = false;
364
365         /* icons */
366         BKE_icon_changed(BKE_icon_getid(&tex->id));
367
368         /* paint overlays */
369         for (scene = bmain->scene.first; scene; scene = scene->id.next)
370                 BKE_paint_invalidate_overlay_tex(scene, tex);
371
372         /* find materials */
373         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
374                 if (!material_uses_texture(ma, tex))
375                         continue;
376
377                 BKE_icon_changed(BKE_icon_getid(&ma->id));
378
379                 if (ma->gpumaterial.first)
380                         GPU_material_free(ma);
381         }
382
383         /* find lamps */
384         for (la = bmain->lamp.first; la; la = la->id.next) {
385                 if (mtex_use_tex(la->mtex, MAX_MTEX, tex)) {
386                         lamp_changed(bmain, la);
387                 }
388                 else if (la->nodetree && nodes_use_tex(la->nodetree, tex)) {
389                         lamp_changed(bmain, la);
390                 }
391                 else {
392                         continue;
393                 }
394         }
395
396         /* find worlds */
397         for (wo = bmain->world.first; wo; wo = wo->id.next) {
398                 if (mtex_use_tex(wo->mtex, MAX_MTEX, tex)) {
399                         /* pass */
400                 }
401                 else if (wo->nodetree && nodes_use_tex(wo->nodetree, tex)) {
402                         /* pass */
403                 }
404                 else {
405                         continue;
406                 }
407
408                 BKE_icon_changed(BKE_icon_getid(&wo->id));
409         }
410
411         /* find compositing nodes */
412         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
413                 if (scene->use_nodes && scene->nodetree) {
414                         for (node = scene->nodetree->nodes.first; node; node = node->next) {
415                                 if (node->id == &tex->id)
416                                         ED_node_tag_update_id(&scene->id);
417                         }
418                 }
419
420                 if (scene->customdata_mask & CD_MASK_MTFACE)
421                         texture_draw = true;
422         }
423
424         /* find textured objects */
425         if (texture_draw && !(U.gameflags & USER_DISABLE_VBO)) {
426                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
427                         DerivedMesh *dm = ob->derivedFinal;
428                         Material ***material = give_matarar(ob);
429                         short a, *totmaterial = give_totcolp(ob);
430
431                         if (dm && totmaterial && material) {
432                                 for (a = 0; a < *totmaterial; a++) {
433                                         if (ob->matbits && ob->matbits[a])
434                                                 ma = ob->mat[a];
435                                         else
436                                                 ma = (*material)[a];
437
438                                         if (ma && material_uses_texture(ma, tex)) {
439                                                 GPU_drawobject_free(dm);
440                                                 break;
441                                         }
442                                 }
443                         }
444                 }
445         }
446 }
447
448 static void world_changed(Main *bmain, World *wo)
449 {
450         Material *ma;
451
452         /* icons */
453         BKE_icon_changed(BKE_icon_getid(&wo->id));
454
455         /* glsl */
456         for (ma = bmain->mat.first; ma; ma = ma->id.next)
457                 if (ma->gpumaterial.first)
458                         GPU_material_free(ma);
459
460         if (defmaterial.gpumaterial.first)
461                 GPU_material_free(&defmaterial);
462 }
463
464 static void image_changed(Main *bmain, Image *ima)
465 {
466         Tex *tex;
467
468         /* icons */
469         BKE_icon_changed(BKE_icon_getid(&ima->id));
470
471         /* textures */
472         for (tex = bmain->tex.first; tex; tex = tex->id.next)
473                 if (tex->ima == ima)
474                         texture_changed(bmain, tex);
475 }
476
477 static void scene_changed(Main *bmain, Scene *scene)
478 {
479         Object *ob;
480         Material *ma;
481
482         /* glsl */
483         for (ob = bmain->object.first; ob; ob = ob->id.next) {
484                 if (ob->gpulamp.first)
485                         GPU_lamp_free(ob);
486                 
487                 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
488                         BKE_texpaint_slots_refresh_object(scene, ob);
489                         BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
490                         GPU_drawobject_free(ob->derivedFinal);                  
491                 }
492         }
493
494         for (ma = bmain->mat.first; ma; ma = ma->id.next)
495                 if (ma->gpumaterial.first)
496                         GPU_material_free(ma);
497
498         if (defmaterial.gpumaterial.first)
499                 GPU_material_free(&defmaterial);
500 }
501
502 void ED_render_id_flush_update(Main *bmain, ID *id)
503 {
504         /* this can be called from render or baking thread when a python script makes
505          * changes, in that case we don't want to do any editor updates, and making
506          * GPU changes is not possible because OpenGL only works in the main thread */
507         if (!BLI_thread_is_main())
508                 return;
509
510         switch (GS(id->name)) {
511                 case ID_MA:
512                         material_changed(bmain, (Material *)id);
513                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_MA);
514                         break;
515                 case ID_TE:
516                         texture_changed(bmain, (Tex *)id);
517                         break;
518                 case ID_WO:
519                         world_changed(bmain, (World *)id);
520                         break;
521                 case ID_LA:
522                         lamp_changed(bmain, (Lamp *)id);
523                         break;
524                 case ID_IM:
525                         image_changed(bmain, (Image *)id);
526                         break;
527                 case ID_SCE:
528                         scene_changed(bmain, (Scene *)id);
529                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
530                         break;
531                 default:
532                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
533                         break;
534         }
535         
536 }
537
538
539 void ED_render_internal_init(void)
540 {
541         RenderEngineType *ret = RE_engines_find("BLENDER_RENDER");
542         
543         ret->view_update = render_view3d_update;
544         ret->view_draw = render_view3d_draw;
545         
546 }