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_workspace_types.h"
42 #include "DNA_world_types.h"
43 #include "DNA_windowmanager_types.h"
44
45 #include "DRW_engine.h"
46
47 #include "BLI_listbase.h"
48 #include "BLI_threads.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_context.h"
52 #include "BKE_DerivedMesh.h"
53 #include "BKE_icons.h"
54 #include "BKE_layer.h"
55 #include "BKE_main.h"
56 #include "BKE_material.h"
57 #include "BKE_node.h"
58 #include "BKE_paint.h"
59 #include "BKE_scene.h"
60 #include "BKE_workspace.h"
61
62 #include "GPU_material.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 "DEG_depsgraph.h"
72
73 #include "WM_api.h"
74
75 #include "render_intern.h"  // own include
76
77 extern Material defmaterial;
78
79 /***************************** Render Engines ********************************/
80
81 void ED_render_scene_update(const DEGEditorUpdateContext *update_ctx, int updated)
82 {
83         /* viewport rendering update on data changes, happens after depsgraph
84          * updates if there was any change. context is set to the 3d view */
85         Main *bmain = update_ctx->bmain;
86         Scene *scene = update_ctx->scene;
87         ViewLayer *view_layer = update_ctx->view_layer;
88         bContext *C;
89         wmWindowManager *wm;
90         wmWindow *win;
91         static bool recursive_check = false;
92
93         /* don't do this render engine update if we're updating the scene from
94          * other threads doing e.g. rendering or baking jobs */
95         if (!BLI_thread_is_main())
96                 return;
97
98         /* don't call this recursively for frame updates */
99         if (recursive_check)
100                 return;
101
102         /* Do not call if no WM available, see T42688. */
103         if (BLI_listbase_is_empty(&bmain->wm))
104                 return;
105
106         recursive_check = true;
107
108         C = CTX_create();
109         CTX_data_main_set(C, bmain);
110         CTX_data_scene_set(C, scene);
111
112         CTX_wm_manager_set(C, bmain->wm.first);
113         wm = bmain->wm.first;
114
115         for (win = wm->windows.first; win; win = win->next) {
116                 bScreen *sc = WM_window_get_active_screen(win);
117                 ScrArea *sa;
118                 ARegion *ar;
119
120                 CTX_wm_window_set(C, win);
121
122                 for (sa = sc->areabase.first; sa; sa = sa->next) {
123                         if (sa->spacetype != SPACE_VIEW3D) {
124                                 continue;
125                         }
126                         View3D *v3d = sa->spacedata.first;
127                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
128                                 if (ar->regiontype != RGN_TYPE_WINDOW) {
129                                         continue;
130                                 }
131                                 RegionView3D *rv3d = ar->regiondata;
132                                 RenderEngine *engine = rv3d->render_engine;
133                                 /* call update if the scene changed, or if the render engine
134                                  * tagged itself for update (e.g. because it was busy at the
135                                  * time of the last update) */
136                                 if (engine && (updated || (engine->flag & RE_ENGINE_DO_UPDATE))) {
137
138                                         CTX_wm_screen_set(C, sc);
139                                         CTX_wm_area_set(C, sa);
140                                         CTX_wm_region_set(C, ar);
141
142                                         engine->flag &= ~RE_ENGINE_DO_UPDATE;
143                                         engine->type->view_update(engine, C);
144
145                                 }
146                                 else {
147                                         RenderEngineType *engine_type =
148                                                 ED_view3d_engine_type(scene, v3d->drawtype);
149                                         if (updated) {
150                                                 DRW_notify_view_update(
151                                                         (&(DRWUpdateContext){
152                                                             .bmain = bmain,
153                                                             .depsgraph = update_ctx->depsgraph,
154                                                             .scene = scene,
155                                                             .view_layer = view_layer,
156                                                             .ar = ar,
157                                                             .v3d = (View3D *)sa->spacedata.first,
158                                                             .engine_type = engine_type
159                                                         }));
160                                         }
161                                 }
162                         }
163                 }
164         }
165
166         CTX_free(C);
167
168         recursive_check = false;
169 }
170
171 void ED_render_engine_area_exit(Main *bmain, ScrArea *sa)
172 {
173         /* clear all render engines in this area */
174         ARegion *ar;
175         wmWindowManager *wm = bmain->wm.first;
176
177         if (sa->spacetype != SPACE_VIEW3D)
178                 return;
179
180         for (ar = sa->regionbase.first; ar; ar = ar->next) {
181                 if (ar->regiontype != RGN_TYPE_WINDOW || !(ar->regiondata))
182                         continue;
183                 ED_view3d_stop_render_preview(wm, ar);
184         }
185 }
186
187 void ED_render_engine_changed(Main *bmain)
188 {
189         /* on changing the render engine type, clear all running render engines */
190         for (bScreen *sc = bmain->screen.first; sc; sc = sc->id.next) {
191                 for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
192                         ED_render_engine_area_exit(bmain, sa);
193                 }
194         }
195         RE_FreePersistentData();
196         /* Inform all render engines and draw managers. */
197         DEGEditorUpdateContext update_ctx = {NULL};
198         update_ctx.bmain = bmain;
199         for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
200                 update_ctx.scene = scene;
201                 LISTBASE_FOREACH(ViewLayer *, view_layer, &scene->view_layers) {
202                         /* TDODO(sergey): Iterate over depsgraphs instead? */
203                         update_ctx.depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
204                         update_ctx.view_layer = view_layer;
205                         ED_render_id_flush_update(&update_ctx, &scene->id);
206                 }
207                 if (scene->nodetree) {
208                         ntreeCompositUpdateRLayers(scene->nodetree);
209                 }
210         }
211 }
212
213 /***************************** Updates ***********************************
214  * ED_render_id_flush_update gets called from DEG_id_tag_update, to do   *
215  * editor level updates when the ID changes. when these ID blocks are in *
216  * the dependency graph, we can get rid of the manual dependency checks  */
217
218 static void render_engine_flag_changed(Main *bmain, int update_flag)
219 {
220         bScreen *sc;
221         ScrArea *sa;
222         ARegion *ar;
223
224         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
225                 for (sa = sc->areabase.first; sa; sa = sa->next) {
226                         if (sa->spacetype != SPACE_VIEW3D)
227                                 continue;
228
229                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
230                                 RegionView3D *rv3d;
231
232                                 if (ar->regiontype != RGN_TYPE_WINDOW)
233                                         continue;
234
235                                 rv3d = ar->regiondata;
236                                 if (rv3d->render_engine)
237                                         rv3d->render_engine->update_flag |= update_flag;
238
239                         }
240                 }
241         }
242 }
243
244 static void material_changed(Main *UNUSED(bmain), Material *ma)
245 {
246         /* icons */
247         BKE_icon_changed(BKE_icon_id_ensure(&ma->id));
248
249         /* glsl */
250         if (ma->id.recalc & ID_RECALC) {
251                 if (!BLI_listbase_is_empty(&ma->gpumaterial)) {
252                         GPU_material_free(&ma->gpumaterial);
253                 }
254         }
255 }
256
257 static void lamp_changed(Main *UNUSED(bmain), Lamp *la)
258 {
259         /* icons */
260         BKE_icon_changed(BKE_icon_id_ensure(&la->id));
261
262         if (defmaterial.gpumaterial.first)
263                 GPU_material_free(&defmaterial.gpumaterial);
264 }
265
266 static void texture_changed(Main *bmain, Tex *tex)
267 {
268         Scene *scene;
269         ViewLayer *view_layer;
270         bNode *node;
271
272         /* icons */
273         BKE_icon_changed(BKE_icon_id_ensure(&tex->id));
274
275         /* paint overlays */
276         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
277                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
278                         BKE_paint_invalidate_overlay_tex(scene, view_layer, tex);
279                 }
280         }
281
282         /* find compositing nodes */
283         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
284                 if (scene->use_nodes && scene->nodetree) {
285                         for (node = scene->nodetree->nodes.first; node; node = node->next) {
286                                 if (node->id == &tex->id)
287                                         ED_node_tag_update_id(&scene->id);
288                         }
289                 }
290         }
291 }
292
293 static void world_changed(Main *UNUSED(bmain), World *wo)
294 {
295         /* icons */
296         BKE_icon_changed(BKE_icon_id_ensure(&wo->id));
297
298         /* XXX temporary flag waiting for depsgraph proper tagging */
299         wo->update_flag = 1;
300
301         /* glsl */
302         if (wo->id.recalc & ID_RECALC) {
303                 if (!BLI_listbase_is_empty(&defmaterial.gpumaterial)) {
304                         GPU_material_free(&defmaterial.gpumaterial);
305                 }
306                 if (!BLI_listbase_is_empty(&wo->gpumaterial)) {
307                         GPU_material_free(&wo->gpumaterial);
308                 }
309         }
310 }
311
312 static void image_changed(Main *bmain, Image *ima)
313 {
314         Tex *tex;
315
316         /* icons */
317         BKE_icon_changed(BKE_icon_id_ensure(&ima->id));
318
319         /* textures */
320         for (tex = bmain->tex.first; tex; tex = tex->id.next)
321                 if (tex->ima == ima)
322                         texture_changed(bmain, tex);
323 }
324
325 static void scene_changed(Main *bmain, Scene *scene)
326 {
327         Object *ob;
328
329         /* glsl */
330         for (ob = bmain->object.first; ob; ob = ob->id.next) {
331                 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
332                         BKE_texpaint_slots_refresh_object(scene, ob);
333                         BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
334                 }
335         }
336 }
337
338 void ED_render_id_flush_update(const DEGEditorUpdateContext *update_ctx, ID *id)
339 {
340         /* this can be called from render or baking thread when a python script makes
341          * changes, in that case we don't want to do any editor updates, and making
342          * GPU changes is not possible because OpenGL only works in the main thread */
343         if (!BLI_thread_is_main()) {
344                 return;
345         }
346         Main *bmain = update_ctx->bmain;
347         /* Internal ID update handlers. */
348         switch (GS(id->name)) {
349                 case ID_MA:
350                         material_changed(bmain, (Material *)id);
351                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_MA);
352                         break;
353                 case ID_TE:
354                         texture_changed(bmain, (Tex *)id);
355                         break;
356                 case ID_WO:
357                         world_changed(bmain, (World *)id);
358                         break;
359                 case ID_LA:
360                         lamp_changed(bmain, (Lamp *)id);
361                         break;
362                 case ID_IM:
363                         image_changed(bmain, (Image *)id);
364                         break;
365                 case ID_SCE:
366                         scene_changed(bmain, (Scene *)id);
367                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
368                         break;
369                 default:
370                         render_engine_flag_changed(bmain, RE_ENGINE_UPDATE_OTHER);
371                         break;
372         }
373 }
374