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