Make image drawing code working with core profile
[blender.git] / source / blender / editors / render / render_preview.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  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/render/render_preview.c
29  *  \ingroup edrend
30  */
31
32
33 /* global includes */
34
35 #include <stdlib.h>
36 #include <math.h>
37 #include <string.h>
38
39 #ifndef WIN32
40 #include <unistd.h>
41 #else
42 #include <io.h>
43 #endif   
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_math.h"
47 #include "BLI_blenlib.h"
48 #include "BLI_utildefines.h"
49
50 #include "BLO_readfile.h"
51
52 #include "DNA_world_types.h"
53 #include "DNA_camera_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_node_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_lamp_types.h"
58 #include "DNA_space_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_brush_types.h"
61 #include "DNA_screen_types.h"
62
63 #include "BKE_appdir.h"
64 #include "BKE_brush.h"
65 #include "BKE_context.h"
66 #include "BKE_colortools.h"
67 #include "BKE_global.h"
68 #include "BKE_idprop.h"
69 #include "BKE_image.h"
70 #include "BKE_icons.h"
71 #include "BKE_lamp.h"
72 #include "BKE_layer.h"
73 #include "BKE_library.h"
74 #include "BKE_library_remap.h"
75 #include "BKE_main.h"
76 #include "BKE_material.h"
77 #include "BKE_node.h"
78 #include "BKE_scene.h"
79 #include "BKE_texture.h"
80 #include "BKE_world.h"
81
82 #include "IMB_imbuf.h"
83 #include "IMB_imbuf_types.h"
84 #include "IMB_thumbs.h"
85
86 #include "BIF_gl.h"
87 #include "BIF_glutil.h"
88
89 #include "GPU_shader.h"
90
91 #include "RE_pipeline.h"
92 #include "RE_engine.h"
93
94 #include "WM_api.h"
95 #include "WM_types.h"
96
97 #include "ED_datafiles.h"
98 #include "ED_render.h"
99 #include "ED_screen.h"
100
101 #ifndef NDEBUG
102 /* Used for database init assert(). */
103 #  include "BLI_threads.h"
104 #endif
105
106 ImBuf *get_brush_icon(Brush *brush)
107 {
108         static const int flags = IB_rect | IB_multilayer | IB_metadata;
109
110         char path[FILE_MAX];
111         const char *folder;
112
113         if (!(brush->icon_imbuf)) {
114                 if (brush->flag & BRUSH_CUSTOM_ICON) {
115
116                         if (brush->icon_filepath[0]) {
117                                 // first use the path directly to try and load the file
118
119                                 BLI_strncpy(path, brush->icon_filepath, sizeof(brush->icon_filepath));
120                                 BLI_path_abs(path, G.main->name);
121
122                                 /* use default colorspaces for brushes */
123                                 brush->icon_imbuf = IMB_loadiffname(path, flags, NULL);
124
125                                 // otherwise lets try to find it in other directories
126                                 if (!(brush->icon_imbuf)) {
127                                         folder = BKE_appdir_folder_id(BLENDER_DATAFILES, "brushicons");
128
129                                         BLI_make_file_string(G.main->name, path, folder, brush->icon_filepath);
130
131                                         if (path[0]) {
132                                                 /* use fefault color spaces */
133                                                 brush->icon_imbuf = IMB_loadiffname(path, flags, NULL);
134                                         }
135                                 }
136
137                                 if (brush->icon_imbuf)
138                                         BKE_icon_changed(BKE_icon_id_ensure(&brush->id));
139                         }
140                 }
141         }
142
143         if (!(brush->icon_imbuf))
144                 brush->id.icon_id = 0;
145
146         return brush->icon_imbuf;
147 }
148
149 typedef struct ShaderPreview {
150         /* from wmJob */
151         void *owner;
152         short *stop, *do_update;
153         
154         Scene *scene;
155         ID *id;
156         ID *parent;
157         MTex *slot;
158         
159         /* datablocks with nodes need full copy during preview render, glsl uses it too */
160         Material *matcopy;
161         Tex *texcopy;
162         Lamp *lampcopy;
163         World *worldcopy;
164         
165         float col[4];       /* active object color */
166         
167         int sizex, sizey;
168         unsigned int *pr_rect;
169         int pr_method;
170
171         Main *bmain;
172         Main *pr_main;
173 } ShaderPreview;
174
175 typedef struct IconPreviewSize {
176         struct IconPreviewSize *next, *prev;
177         int sizex, sizey;
178         unsigned int *rect;
179 } IconPreviewSize;
180
181 typedef struct IconPreview {
182         Main *bmain;
183         Scene *scene;
184         void *owner;
185         ID *id;
186         ListBase sizes;
187 } IconPreview;
188
189 /* *************************** Preview for buttons *********************** */
190
191 static Main *G_pr_main = NULL;
192 static Main *G_pr_main_cycles = NULL;
193
194 #ifndef WITH_HEADLESS
195 static Main *load_main_from_memory(const void *blend, int blend_size)
196 {
197         const int fileflags = G.fileflags;
198         Main *bmain = NULL;
199         BlendFileData *bfd;
200
201         G.fileflags |= G_FILE_NO_UI;
202         bfd = BLO_read_from_memory(blend, blend_size, NULL, BLO_READ_SKIP_NONE);
203         if (bfd) {
204                 bmain = bfd->main;
205
206                 MEM_freeN(bfd);
207         }
208         G.fileflags = fileflags;
209
210         return bmain;
211 }
212 #endif
213
214 void ED_preview_ensure_dbase(void)
215 {
216 #ifndef WITH_HEADLESS
217         static bool base_initialized = false;
218         BLI_assert(BLI_thread_is_main());
219         if (!base_initialized) {
220                 G_pr_main = load_main_from_memory(datatoc_preview_blend, datatoc_preview_blend_size);
221                 G_pr_main_cycles = load_main_from_memory(datatoc_preview_cycles_blend, datatoc_preview_cycles_blend_size);
222                 base_initialized = true;
223         }
224 #endif
225 }
226
227 static bool check_engine_supports_textures(Scene *scene)
228 {
229         RenderEngineType *type = RE_engines_find(scene->r.engine);
230         return type->flag & RE_USE_TEXTURE_PREVIEW;
231 }
232
233 void ED_preview_free_dbase(void)
234 {
235         if (G_pr_main)
236                 BKE_main_free(G_pr_main);
237
238         if (G_pr_main_cycles)
239                 BKE_main_free(G_pr_main_cycles);
240 }
241
242 static int preview_mat_has_sss(Material *mat, bNodeTree *ntree)
243 {
244         if (mat) {
245                 if (mat->sss_flag & MA_DIFF_SSS)
246                         return 1;
247                 if (mat->nodetree)
248                         if (preview_mat_has_sss(NULL, mat->nodetree))
249                                 return 1;
250         }
251         else if (ntree) {
252                 bNode *node;
253                 for (node = ntree->nodes.first; node; node = node->next) {
254                         if (node->type == NODE_GROUP && node->id) {
255                                 if (preview_mat_has_sss(NULL, (bNodeTree *)node->id))
256                                         return 1;
257                         }
258                         else if (node->id && ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
259                                 mat = (Material *)node->id;
260                                 if (mat->sss_flag & MA_DIFF_SSS)
261                                         return 1;
262                         }
263                 }
264         }
265         return 0;
266 }
267
268 static Scene *preview_get_scene(Main *pr_main)
269 {
270         if (pr_main == NULL) return NULL;
271         
272         return pr_main->scene.first;
273 }
274
275
276 /* call this with a pointer to initialize preview scene */
277 /* call this with NULL to restore assigned ID pointers in preview scene */
278 static Scene *preview_prepare_scene(Main *bmain, Scene *scene, ID *id, int id_type, ShaderPreview *sp)
279 {
280         Scene *sce;
281         BaseLegacy *base;
282         Main *pr_main = sp->pr_main;
283
284         memcpy(pr_main->name, bmain->name, sizeof(pr_main->name));
285
286         sce = preview_get_scene(pr_main);
287         if (sce) {
288                 
289                 /* this flag tells render to not execute depsgraph or ipos etc */
290                 sce->r.scemode |= R_BUTS_PREVIEW;
291                 /* set world always back, is used now */
292                 sce->world = pr_main->world.first;
293                 /* now: exposure copy */
294                 if (scene->world) {
295                         sce->world->exp = scene->world->exp;
296                         sce->world->range = scene->world->range;
297                 }
298                 
299                 sce->r.color_mgt_flag = scene->r.color_mgt_flag;
300                 BKE_color_managed_display_settings_copy(&sce->display_settings, &scene->display_settings);
301
302                 BKE_color_managed_view_settings_free(&sce->view_settings);
303                 BKE_color_managed_view_settings_copy(&sce->view_settings, &scene->view_settings);
304                 
305                 /* prevent overhead for small renders and icons (32) */
306                 if (id && sp->sizex < 40) {
307                         sce->r.tilex = sce->r.tiley = 64;
308                 }
309                 else {
310                         sce->r.tilex = sce->r.xsch / 4;
311                         sce->r.tiley = sce->r.ysch / 4;
312                 }
313                 
314                 if ((id && sp->pr_method == PR_ICON_RENDER) && id_type != ID_WO)
315                         sce->r.alphamode = R_ALPHAPREMUL;
316                 else
317                         sce->r.alphamode = R_ADDSKY;
318
319                 sce->r.cfra = scene->r.cfra;
320
321                 if (id_type == ID_TE && !check_engine_supports_textures(scene)) {
322                         /* Force blender internal for texture icons and nodes render,
323                          * seems commonly used render engines does not support
324                          * such kind of rendering.
325                          */
326                         BLI_strncpy(sce->r.engine, RE_engine_id_BLENDER_RENDER, sizeof(sce->r.engine));
327                 }
328                 else {
329                         BLI_strncpy(sce->r.engine, scene->r.engine, sizeof(sce->r.engine));
330                 }
331                 
332                 if (id_type == ID_MA) {
333                         Material *mat = NULL, *origmat = (Material *)id;
334                         
335                         if (origmat) {
336                                 /* work on a copy */
337                                 mat = localize_material(origmat);
338                                 sp->matcopy = mat;
339                                 BLI_addtail(&pr_main->mat, mat);
340                                 
341                                 if (!BKE_scene_use_new_shading_nodes(scene)) {
342                                         init_render_material(mat, 0, NULL);     /* call that retrieves mode_l */
343                                         end_render_material(mat);
344                                         
345                                         /* un-useful option */
346                                         if (sp->pr_method == PR_ICON_RENDER)
347                                                 mat->shade_flag &= ~MA_OBCOLOR;
348
349                                         /* turn on raytracing if needed */
350                                         if (mat->mode_l & MA_RAYMIRROR)
351                                                 sce->r.mode |= R_RAYTRACE;
352                                         if (mat->material_type == MA_TYPE_VOLUME)
353                                                 sce->r.mode |= R_RAYTRACE;
354                                         if ((mat->mode_l & MA_RAYTRANSP) && (mat->mode_l & MA_TRANSP))
355                                                 sce->r.mode |= R_RAYTRACE;
356                                         if (preview_mat_has_sss(mat, NULL))
357                                                 sce->r.mode |= R_SSS;
358                                         
359                                         /* turn off fake shadows if needed */
360                                         /* this only works in a specific case where the preview.blend contains
361                                          * an object starting with 'c' which has a material linked to it (not the obdata)
362                                          * and that material has a fake shadow texture in the active texture slot */
363                                         FOREACH_SCENE_OBJECT(sce, ob)
364                                         {
365                                                 if (ob->id.name[2] == 'c') {
366                                                         Material *shadmat = give_current_material(ob, ob->actcol);
367                                                         if (shadmat) {
368                                                                 if (mat->mode2 & MA_CASTSHADOW) shadmat->septex = 0;
369                                                                 else shadmat->septex |= 1;
370                                                         }
371                                                 }
372                                         }
373                                         FOREACH_SCENE_OBJECT_END
374                                         
375                                         /* turn off bounce lights for volume, 
376                                          * doesn't make much visual difference and slows it down too */
377                                         FOREACH_SCENE_OBJECT(sce, ob)
378                                         {
379                                                 if (ob->type == OB_LAMP) {
380                                                         /* if doesn't match 'Lamp.002' --> main key light */
381                                                         if (!STREQ(ob->id.name + 2, "Lamp.002")) {
382                                                                 if (mat->material_type == MA_TYPE_VOLUME)
383                                                                         ob->restrictflag |= OB_RESTRICT_RENDER;
384                                                                 else
385                                                                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
386                                                         }
387                                                 }
388                                         }
389                                         FOREACH_SCENE_OBJECT_END
390                                 }
391                                 else {
392                                         /* use current scene world to light sphere */
393                                         if (mat->pr_type == MA_SPHERE_A)
394                                                 sce->world = scene->world;
395                                 }
396                                 
397                                 if (sp->pr_method == PR_ICON_RENDER) {
398                                         if (mat->material_type == MA_TYPE_HALO) {
399                                                 sce->lay = 1 << MA_FLAT;
400                                         }
401                                         else {
402                                                 sce->lay = 1 << MA_SPHERE_A;
403
404                                                 /* same as above, use current scene world to light sphere */
405                                                 if (BKE_scene_use_new_shading_nodes(scene))
406                                                         sce->world = scene->world;
407                                         }
408                                 }
409                                 else {
410                                         sce->lay = 1 << mat->pr_type;
411                                         if (mat->nodetree && sp->pr_method == PR_NODE_RENDER) {
412                                                 /* two previews, they get copied by wmJob */
413                                                 BKE_node_preview_init_tree(mat->nodetree, sp->sizex, sp->sizey, true);
414                                                 BKE_node_preview_init_tree(origmat->nodetree, sp->sizex, sp->sizey, true);
415                                         }
416                                 }
417                         }
418                         else {
419                                 sce->r.mode &= ~(R_OSA | R_RAYTRACE | R_SSS);
420                                 
421                         }
422                         
423                         for (base = sce->base.first; base; base = base->next) {
424                                 if (base->object->id.name[2] == 'p') {
425                                         /* copy over object color, in case material uses it */
426                                         copy_v4_v4(base->object->col, sp->col);
427                                         
428                                         if (OB_TYPE_SUPPORT_MATERIAL(base->object->type)) {
429                                                 /* don't use assign_material, it changed mat->id.us, which shows in the UI */
430                                                 Material ***matar = give_matarar(base->object);
431                                                 int actcol = max_ii(base->object->actcol - 1, 0);
432
433                                                 if (matar && actcol < base->object->totcol)
434                                                         (*matar)[actcol] = mat;
435                                         }
436                                         else if (base->object->type == OB_LAMP) {
437                                                 base->object->restrictflag &= ~OB_RESTRICT_RENDER;
438                                         }
439                                 }
440                         }
441                 }
442                 else if (id_type == ID_TE) {
443                         Tex *tex = NULL, *origtex = (Tex *)id;
444                         
445                         if (origtex) {
446                                 tex = BKE_texture_localize(origtex);
447                                 sp->texcopy = tex;
448                                 BLI_addtail(&pr_main->tex, tex);
449                         }
450                         sce->lay = 1 << MA_TEXTURE;
451                         
452                         for (base = sce->base.first; base; base = base->next) {
453                                 if (base->object->id.name[2] == 't') {
454                                         Material *mat = give_current_material(base->object, base->object->actcol);
455                                         if (mat && mat->mtex[0]) {
456                                                 mat->mtex[0]->tex = tex;
457                                                 
458                                                 if (tex && sp->slot)
459                                                         mat->mtex[0]->which_output = sp->slot->which_output;
460
461                                                 mat->mtex[0]->mapto &= ~MAP_ALPHA;
462                                                 mat->alpha = 1.0f;
463
464                                                 /* show alpha in this case */
465                                                 if (tex == NULL || (tex->flag & TEX_PRV_ALPHA)) {
466                                                         if (!(tex && tex->type == TEX_IMAGE && (tex->imaflag & (TEX_USEALPHA | TEX_CALCALPHA)) == 0)) {
467                                                                 mat->mtex[0]->mapto |= MAP_ALPHA;
468                                                                 mat->alpha = 0.0f;
469                                                         }
470                                                 }
471                                         }
472                                 }
473                         }
474
475                         if (tex && tex->nodetree && sp->pr_method == PR_NODE_RENDER) {
476                                 /* two previews, they get copied by wmJob */
477                                 BKE_node_preview_init_tree(origtex->nodetree, sp->sizex, sp->sizey, true);
478                                 BKE_node_preview_init_tree(tex->nodetree, sp->sizex, sp->sizey, true);
479                         }
480                 }
481                 else if (id_type == ID_LA) {
482                         Lamp *la = NULL, *origla = (Lamp *)id;
483
484                         /* work on a copy */
485                         if (origla) {
486                                 la = localize_lamp(origla);
487                                 sp->lampcopy = la;
488                                 BLI_addtail(&pr_main->lamp, la);
489                         }
490
491                         sce->lay = 1 << MA_LAMP;
492
493                         if (!BKE_scene_use_new_shading_nodes(scene)) {
494                                 if (la && la->type == LA_SUN && (la->sun_effect_type & LA_SUN_EFFECT_SKY)) {
495                                         sce->lay = 1 << MA_ATMOS;
496                                         sce->world = scene->world;
497                                         sce->camera = (Object *)BLI_findstring(&pr_main->object, "CameraAtmo", offsetof(ID, name) + 2);
498                                 }
499                                 else {
500                                         sce->world = NULL;
501                                         sce->camera = (Object *)BLI_findstring(&pr_main->object, "Camera", offsetof(ID, name) + 2);
502                                 }
503                         }
504                                 
505                         for (base = sce->base.first; base; base = base->next) {
506                                 if (base->object->id.name[2] == 'p') {
507                                         if (base->object->type == OB_LAMP)
508                                                 base->object->data = la;
509                                 }
510                         }
511
512                         if (la && la->nodetree && sp->pr_method == PR_NODE_RENDER) {
513                                 /* two previews, they get copied by wmJob */
514                                 BKE_node_preview_init_tree(origla->nodetree, sp->sizex, sp->sizey, true);
515                                 BKE_node_preview_init_tree(la->nodetree, sp->sizex, sp->sizey, true);
516                         }
517                 }
518                 else if (id_type == ID_WO) {
519                         World *wrld = NULL, *origwrld = (World *)id;
520
521                         if (origwrld) {
522                                 wrld = localize_world(origwrld);
523                                 sp->worldcopy = wrld;
524                                 BLI_addtail(&pr_main->world, wrld);
525                         }
526
527                         sce->lay = 1 << MA_SKY;
528                         sce->world = wrld;
529
530                         if (wrld && wrld->nodetree && sp->pr_method == PR_NODE_RENDER) {
531                                 /* two previews, they get copied by wmJob */
532                                 BKE_node_preview_init_tree(wrld->nodetree, sp->sizex, sp->sizey, true);
533                                 BKE_node_preview_init_tree(origwrld->nodetree, sp->sizex, sp->sizey, true);
534                         }
535                 }
536
537                 return sce;
538         }
539         
540         return NULL;
541 }
542
543 /* new UI convention: draw is in pixel space already. */
544 /* uses UI_BTYPE_ROUNDBOX button in block to get the rect */
545 static bool ed_preview_draw_rect(ScrArea *sa, int split, int first, rcti *rect, rcti *newrect)
546 {
547         Render *re;
548         RenderView *rv;
549         RenderResult rres;
550         char name[32];
551         int offx = 0;
552         int newx = BLI_rcti_size_x(rect);
553         int newy = BLI_rcti_size_y(rect);
554         bool ok = false;
555
556         if (!split || first) sprintf(name, "Preview %p", (void *)sa);
557         else sprintf(name, "SecondPreview %p", (void *)sa);
558
559         if (split) {
560                 if (first) {
561                         offx = 0;
562                         newx = newx / 2;
563                 }
564                 else {
565                         offx = newx / 2;
566                         newx = newx - newx / 2;
567                 }
568         }
569
570         /* test if something rendered ok */
571         re = RE_GetRender(name);
572
573         if (re == NULL)
574                 return false;
575
576         RE_AcquireResultImageViews(re, &rres);
577
578         if (!BLI_listbase_is_empty(&rres.views)) {
579                 /* material preview only needs monoscopy (view 0) */
580                 rv = RE_RenderViewGetById(&rres, 0);
581         }
582         else {
583                 /* possible the job clears the views but we're still drawing T45496 */
584                 rv = NULL;
585         }
586
587         if (rv && rv->rectf) {
588                 
589                 if (ABS(rres.rectx - newx) < 2 && ABS(rres.recty - newy) < 2) {
590
591                         newrect->xmax = max_ii(newrect->xmax, rect->xmin + rres.rectx + offx);
592                         newrect->ymax = max_ii(newrect->ymax, rect->ymin + rres.recty);
593
594                         if (rres.rectx && rres.recty) {
595                                 unsigned char *rect_byte = MEM_mallocN(rres.rectx * rres.recty * sizeof(int), "ed_preview_draw_rect");
596                                 float fx = rect->xmin + offx;
597                                 float fy = rect->ymin;
598
599                                 /* material preview only needs monoscopy (view 0) */
600                                 if (re)
601                                         RE_AcquiredResultGet32(re, &rres, (unsigned int *)rect_byte, 0);
602
603                                 IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
604                                 immDrawPixelsTex(&state, fx, fy, rres.rectx, rres.recty, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect_byte,
605                                                  1.0f, 1.0f, NULL);
606                                 
607                                 MEM_freeN(rect_byte);
608                                 
609                                 ok = 1;
610                         }
611                 }
612         }
613
614         RE_ReleaseResultImageViews(re, &rres);
615
616         return ok;
617 }
618
619 void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, rcti *rect)
620 {
621         if (idp) {
622                 wmWindowManager *wm = CTX_wm_manager(C);
623                 ScrArea *sa = CTX_wm_area(C);
624                 ID *id = (ID *)idp;
625                 ID *parent = (ID *)parentp;
626                 MTex *slot = (MTex *)slotp;
627                 SpaceButs *sbuts = CTX_wm_space_buts(C);
628                 ShaderPreview *sp = WM_jobs_customdata(wm, sa);
629                 rcti newrect;
630                 int ok;
631                 int newx = BLI_rcti_size_x(rect);
632                 int newy = BLI_rcti_size_y(rect);
633
634                 newrect.xmin = rect->xmin;
635                 newrect.xmax = rect->xmin;
636                 newrect.ymin = rect->ymin;
637                 newrect.ymax = rect->ymin;
638
639                 if (parent) {
640                         ok = ed_preview_draw_rect(sa, 1, 1, rect, &newrect);
641                         ok &= ed_preview_draw_rect(sa, 1, 0, rect, &newrect);
642                 }
643                 else
644                         ok = ed_preview_draw_rect(sa, 0, 0, rect, &newrect);
645
646                 if (ok)
647                         *rect = newrect;
648
649                 /* start a new preview render job if signalled through sbuts->preview,
650                  * if no render result was found and no preview render job is running,
651                  * or if the job is running and the size of preview changed */
652                 if ((sbuts != NULL && sbuts->preview) ||
653                     (!ok && !WM_jobs_test(wm, sa, WM_JOB_TYPE_RENDER_PREVIEW)) ||
654                     (sp && (ABS(sp->sizex - newx) >= 2 || ABS(sp->sizey - newy) > 2)))
655                 {
656                         if (sbuts != NULL) {
657                                 sbuts->preview = 0;
658                         }
659                         ED_preview_shader_job(C, sa, id, parent, slot, newx, newy, PR_BUTS_RENDER);
660                 }
661         }
662 }
663
664 /* **************************** new shader preview system ****************** */
665
666 /* inside thread, called by renderer, sets job update value */
667 static void shader_preview_update(void *spv, RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect))
668 {
669         ShaderPreview *sp = spv;
670         
671         *(sp->do_update) = true;
672 }
673
674 /* called by renderer, checks job value */
675 static int shader_preview_break(void *spv)
676 {
677         ShaderPreview *sp = spv;
678
679         return *(sp->stop);
680 }
681
682 /* outside thread, called before redraw notifiers, it moves finished preview over */
683 static void shader_preview_updatejob(void *spv)
684 {
685         ShaderPreview *sp = spv;
686         
687         if (sp->id) {
688                 if (sp->pr_method == PR_NODE_RENDER) {
689                         if (GS(sp->id->name) == ID_MA) {
690                                 Material *mat = (Material *)sp->id;
691                                 
692                                 if (sp->matcopy && mat->nodetree && sp->matcopy->nodetree)
693                                         ntreeLocalSync(sp->matcopy->nodetree, mat->nodetree);
694                         }
695                         else if (GS(sp->id->name) == ID_TE) {
696                                 Tex *tex = (Tex *)sp->id;
697                                 
698                                 if (sp->texcopy && tex->nodetree && sp->texcopy->nodetree)
699                                         ntreeLocalSync(sp->texcopy->nodetree, tex->nodetree);
700                         }
701                         else if (GS(sp->id->name) == ID_WO) {
702                                 World *wrld = (World *)sp->id;
703                                 
704                                 if (sp->worldcopy && wrld->nodetree && sp->worldcopy->nodetree)
705                                         ntreeLocalSync(sp->worldcopy->nodetree, wrld->nodetree);
706                         }
707                         else if (GS(sp->id->name) == ID_LA) {
708                                 Lamp *la = (Lamp *)sp->id;
709                                 
710                                 if (sp->lampcopy && la->nodetree && sp->lampcopy->nodetree)
711                                         ntreeLocalSync(sp->lampcopy->nodetree, la->nodetree);
712                         }
713                 }
714         }
715 }
716
717 static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
718 {
719         Render *re;
720         Scene *sce;
721         float oldlens;
722         short idtype = GS(id->name);
723         char name[32];
724         int sizex;
725         Main *pr_main = sp->pr_main;
726         
727         /* in case of split preview, use border render */
728         if (split) {
729                 if (first) sizex = sp->sizex / 2;
730                 else sizex = sp->sizex - sp->sizex / 2;
731         }
732         else {
733                 sizex = sp->sizex;
734         }
735
736         /* we have to set preview variables first */
737         sce = preview_get_scene(pr_main);
738         if (sce) {
739                 sce->r.xsch = sizex;
740                 sce->r.ysch = sp->sizey;
741                 sce->r.size = 100;
742         }
743         
744         /* get the stuff from the builtin preview dbase */
745         sce = preview_prepare_scene(sp->bmain, sp->scene, id, idtype, sp);
746         if (sce == NULL) return;
747         
748         if (!split || first) sprintf(name, "Preview %p", sp->owner);
749         else sprintf(name, "SecondPreview %p", sp->owner);
750         re = RE_GetRender(name);
751         
752         /* full refreshed render from first tile */
753         if (re == NULL)
754                 re = RE_NewRender(name);
755                 
756         /* sce->r gets copied in RE_InitState! */
757         sce->r.scemode &= ~(R_MATNODE_PREVIEW | R_TEXNODE_PREVIEW);
758         sce->r.scemode &= ~R_NO_IMAGE_LOAD;
759
760         if (sp->pr_method == PR_ICON_RENDER) {
761                 sce->r.scemode |= R_NO_IMAGE_LOAD;
762                 sce->r.mode |= R_OSA;
763         }
764         else if (sp->pr_method == PR_NODE_RENDER) {
765                 if (idtype == ID_MA) sce->r.scemode |= R_MATNODE_PREVIEW;
766                 else if (idtype == ID_TE) sce->r.scemode |= R_TEXNODE_PREVIEW;
767                 sce->r.mode &= ~R_OSA;
768         }
769         else {  /* PR_BUTS_RENDER */
770                 sce->r.mode |= R_OSA;
771         }
772
773
774         /* callbacs are cleared on GetRender() */
775         if (ELEM(sp->pr_method, PR_BUTS_RENDER, PR_NODE_RENDER)) {
776                 RE_display_update_cb(re, sp, shader_preview_update);
777         }
778         /* set this for all previews, default is react to G.is_break still */
779         RE_test_break_cb(re, sp, shader_preview_break);
780         
781         /* lens adjust */
782         oldlens = ((Camera *)sce->camera->data)->lens;
783         if (sizex > sp->sizey)
784                 ((Camera *)sce->camera->data)->lens *= (float)sp->sizey / (float)sizex;
785
786         /* entire cycle for render engine */
787         RE_PreviewRender(re, pr_main, sce);
788
789         ((Camera *)sce->camera->data)->lens = oldlens;
790
791         /* handle results */
792         if (sp->pr_method == PR_ICON_RENDER) {
793                 // char *rct= (char *)(sp->pr_rect + 32*16 + 16);
794                 
795                 if (sp->pr_rect)
796                         RE_ResultGet32(re, sp->pr_rect);
797         }
798
799         /* unassign the pointers, reset vars */
800         preview_prepare_scene(sp->bmain, sp->scene, NULL, GS(id->name), sp);
801         
802         /* XXX bad exception, end-exec is not being called in render, because it uses local main */
803 //      if (idtype == ID_TE) {
804 //              Tex *tex= (Tex *)id;
805 //              if (tex->use_nodes && tex->nodetree)
806 //                      ntreeEndExecTree(tex->nodetree);
807 //      }
808
809 }
810
811 /* runs inside thread for material and icons */
812 static void shader_preview_startjob(void *customdata, short *stop, short *do_update)
813 {
814         ShaderPreview *sp = customdata;
815
816         sp->stop = stop;
817         sp->do_update = do_update;
818
819         if (sp->parent) {
820                 shader_preview_render(sp, sp->id, 1, 1);
821                 shader_preview_render(sp, sp->parent, 1, 0);
822         }
823         else
824                 shader_preview_render(sp, sp->id, 0, 0);
825
826         *do_update = true;
827 }
828
829 static void shader_preview_free(void *customdata)
830 {
831         ShaderPreview *sp = customdata;
832         Main *pr_main = sp->pr_main;
833         
834         if (sp->matcopy) {
835                 struct IDProperty *properties;
836                 
837                 /* node previews */
838                 shader_preview_updatejob(sp);
839                 
840                 /* get rid of copied material */
841                 BLI_remlink(&pr_main->mat, sp->matcopy);
842                 
843                 BKE_material_free(sp->matcopy);
844
845                 properties = IDP_GetProperties((ID *)sp->matcopy, false);
846                 if (properties) {
847                         IDP_FreeProperty(properties);
848                         MEM_freeN(properties);
849                 }
850                 MEM_freeN(sp->matcopy);
851         }
852         if (sp->texcopy) {
853                 struct IDProperty *properties;
854                 /* node previews */
855                 shader_preview_updatejob(sp);
856                 
857                 /* get rid of copied texture */
858                 BLI_remlink(&pr_main->tex, sp->texcopy);
859                 BKE_texture_free(sp->texcopy);
860                 
861                 properties = IDP_GetProperties((ID *)sp->texcopy, false);
862                 if (properties) {
863                         IDP_FreeProperty(properties);
864                         MEM_freeN(properties);
865                 }
866                 MEM_freeN(sp->texcopy);
867         }
868         if (sp->worldcopy) {
869                 struct IDProperty *properties;
870                 /* node previews */
871                 shader_preview_updatejob(sp);
872                 
873                 /* get rid of copied world */
874                 BLI_remlink(&pr_main->world, sp->worldcopy);
875                 BKE_world_free(sp->worldcopy);
876                 
877                 properties = IDP_GetProperties((ID *)sp->worldcopy, false);
878                 if (properties) {
879                         IDP_FreeProperty(properties);
880                         MEM_freeN(properties);
881                 }
882                 MEM_freeN(sp->worldcopy);
883         }
884         if (sp->lampcopy) {
885                 struct IDProperty *properties;
886                 /* node previews */
887                 shader_preview_updatejob(sp);
888                 
889                 /* get rid of copied lamp */
890                 BLI_remlink(&pr_main->lamp, sp->lampcopy);
891                 BKE_lamp_free(sp->lampcopy);
892                 
893                 properties = IDP_GetProperties((ID *)sp->lampcopy, false);
894                 if (properties) {
895                         IDP_FreeProperty(properties);
896                         MEM_freeN(properties);
897                 }
898                 MEM_freeN(sp->lampcopy);
899         }
900         
901         MEM_freeN(sp);
902 }
903
904 /* ************************* icon preview ********************** */
905
906 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
907 {
908         struct ImBuf *ima;
909         unsigned int *drect, *srect;
910         float scaledx, scaledy;
911         short ex, ey, dx, dy;
912
913         /* paranoia test */
914         if (ibuf == NULL || (ibuf->rect == NULL && ibuf->rect_float == NULL))
915                 return;
916         
917         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
918         ima = IMB_dupImBuf(ibuf);
919         
920         if (!ima) 
921                 return;
922         
923         if (ima->x > ima->y) {
924                 scaledx = (float)w;
925                 scaledy =  ( (float)ima->y / (float)ima->x) * (float)w;
926         }
927         else {
928                 scaledx =  ( (float)ima->x / (float)ima->y) * (float)h;
929                 scaledy = (float)h;
930         }
931         
932         ex = (short)scaledx;
933         ey = (short)scaledy;
934         
935         dx = (w - ex) / 2;
936         dy = (h - ey) / 2;
937         
938         IMB_scalefastImBuf(ima, ex, ey);
939         
940         /* if needed, convert to 32 bits */
941         if (ima->rect == NULL)
942                 IMB_rect_from_float(ima);
943
944         srect = ima->rect;
945         drect = rect;
946
947         drect += dy * w + dx;
948         for (; ey > 0; ey--) {
949                 memcpy(drect, srect, ex * sizeof(int));
950                 drect += w;
951                 srect += ima->x;
952         }
953
954         IMB_freeImBuf(ima);
955 }
956
957 static void set_alpha(char *cp, int sizex, int sizey, char alpha) 
958 {
959         int a, size = sizex * sizey;
960
961         for (a = 0; a < size; a++, cp += 4)
962                 cp[3] = alpha;
963 }
964
965 static void icon_preview_startjob(void *customdata, short *stop, short *do_update)
966 {
967         ShaderPreview *sp = customdata;
968
969         if (sp->pr_method == PR_ICON_DEFERRED) {
970                 PreviewImage *prv = sp->owner;
971                 ImBuf *thumb;
972                 char *deferred_data = PRV_DEFERRED_DATA(prv);
973                 int source =  deferred_data[0];
974                 char *path = &deferred_data[1];
975
976 //              printf("generating deferred %d×%d preview for %s\n", sp->sizex, sp->sizey, path);
977
978                 thumb = IMB_thumb_manage(path, THB_LARGE, source);
979
980                 if (thumb) {
981                         /* PreviewImage assumes premultiplied alhpa... */
982                         IMB_premultiply_alpha(thumb);
983
984                         icon_copy_rect(thumb, sp->sizex, sp->sizey, sp->pr_rect);
985                         IMB_freeImBuf(thumb);
986                 }
987         }
988         else {
989                 ID *id = sp->id;
990                 short idtype = GS(id->name);
991
992                 if (idtype == ID_IM) {
993                         Image *ima = (Image *)id;
994                         ImBuf *ibuf = NULL;
995                         ImageUser iuser = {NULL};
996
997                         /* ima->ok is zero when Image cannot load */
998                         if (ima == NULL || ima->ok == 0)
999                                 return;
1000
1001                         /* setup dummy image user */
1002                         iuser.ok = iuser.framenr = 1;
1003                         iuser.scene = sp->scene;
1004
1005                         /* elubie: this needs to be changed: here image is always loaded if not
1006                          * already there. Very expensive for large images. Need to find a way to
1007                          * only get existing ibuf */
1008                         ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
1009                         if (ibuf == NULL || ibuf->rect == NULL) {
1010                                 BKE_image_release_ibuf(ima, ibuf, NULL);
1011                                 return;
1012                         }
1013
1014                         icon_copy_rect(ibuf, sp->sizex, sp->sizey, sp->pr_rect);
1015
1016                         *do_update = true;
1017
1018                         BKE_image_release_ibuf(ima, ibuf, NULL);
1019                 }
1020                 else if (idtype == ID_BR) {
1021                         Brush *br = (Brush *)id;
1022
1023                         br->icon_imbuf = get_brush_icon(br);
1024
1025                         memset(sp->pr_rect, 0x88, sp->sizex * sp->sizey * sizeof(unsigned int));
1026
1027                         if (!(br->icon_imbuf) || !(br->icon_imbuf->rect))
1028                                 return;
1029
1030                         icon_copy_rect(br->icon_imbuf, sp->sizex, sp->sizey, sp->pr_rect);
1031
1032                         *do_update = true;
1033                 }
1034                 else if (idtype == ID_SCR) {
1035                         bScreen *screen = (bScreen *)id;
1036
1037                         ED_screen_preview_render(screen, sp->sizex, sp->sizey, sp->pr_rect);
1038                         *do_update = true;
1039                 }
1040                 else {
1041                         /* re-use shader job */
1042                         shader_preview_startjob(customdata, stop, do_update);
1043
1044                         /* world is rendered with alpha=0, so it wasn't displayed
1045                          * this could be render option for sky to, for later */
1046                         if (idtype == ID_WO) {
1047                                 set_alpha((char *)sp->pr_rect, sp->sizex, sp->sizey, 255);
1048                         }
1049                         else if (idtype == ID_MA) {
1050                                 Material *ma = (Material *)id;
1051
1052                                 if (ma->material_type == MA_TYPE_HALO)
1053                                         set_alpha((char *)sp->pr_rect, sp->sizex, sp->sizey, 255);
1054                         }
1055                 }
1056         }
1057 }
1058
1059 /* use same function for icon & shader, so the job manager
1060  * does not run two of them at the same time. */
1061
1062 static void common_preview_startjob(void *customdata, short *stop, short *do_update, float *UNUSED(progress))
1063 {
1064         ShaderPreview *sp = customdata;
1065
1066         if (ELEM(sp->pr_method, PR_ICON_RENDER, PR_ICON_DEFERRED))
1067                 icon_preview_startjob(customdata, stop, do_update);
1068         else
1069                 shader_preview_startjob(customdata, stop, do_update);
1070 }
1071
1072 /* exported functions */
1073
1074 static void icon_preview_add_size(IconPreview *ip, unsigned int *rect, int sizex, int sizey)
1075 {
1076         IconPreviewSize *cur_size = ip->sizes.first, *new_size;
1077
1078         while (cur_size) {
1079                 if (cur_size->sizex == sizex && cur_size->sizey == sizey) {
1080                         /* requested size is already in list, no need to add it again */
1081                         return;
1082                 }
1083
1084                 cur_size = cur_size->next;
1085         }
1086
1087         new_size = MEM_callocN(sizeof(IconPreviewSize), "IconPreviewSize");
1088         new_size->sizex = sizex;
1089         new_size->sizey = sizey;
1090         new_size->rect = rect;
1091
1092         BLI_addtail(&ip->sizes, new_size);
1093 }
1094
1095 static void icon_preview_startjob_all_sizes(void *customdata, short *stop, short *do_update, float *progress)
1096 {
1097         IconPreview *ip = (IconPreview *)customdata;
1098         IconPreviewSize *cur_size;
1099         const bool use_new_shading = BKE_scene_use_new_shading_nodes(ip->scene);
1100
1101         for (cur_size = ip->sizes.first; cur_size; cur_size = cur_size->next) {
1102                 PreviewImage *prv = ip->owner;
1103
1104                 if (prv->tag & PRV_TAG_DEFFERED_DELETE) {
1105                         /* Non-thread-protected reading is not an issue here. */
1106                         continue;
1107                 }
1108
1109                 ShaderPreview *sp = MEM_callocN(sizeof(ShaderPreview), "Icon ShaderPreview");
1110                 const bool is_render = !(prv->tag & PRV_TAG_DEFFERED);
1111
1112                 /* construct shader preview from image size and previewcustomdata */
1113                 sp->scene = ip->scene;
1114                 sp->owner = ip->owner;
1115                 sp->sizex = cur_size->sizex;
1116                 sp->sizey = cur_size->sizey;
1117                 sp->pr_method = is_render ? PR_ICON_RENDER : PR_ICON_DEFERRED;
1118                 sp->pr_rect = cur_size->rect;
1119                 sp->id = ip->id;
1120                 sp->bmain = ip->bmain;
1121
1122                 if (is_render) {
1123                         BLI_assert(ip->id);
1124                         if (use_new_shading) {
1125                                 /* texture icon rendering is hardcoded to use BI,
1126                                  * so don't even think of using cycle's bmain for
1127                                  * texture icons
1128                                  */
1129                                 if (GS(ip->id->name) != ID_TE)
1130                                         sp->pr_main = G_pr_main_cycles;
1131                                 else
1132                                         sp->pr_main = G_pr_main;
1133                         }
1134                         else {
1135                                 sp->pr_main = G_pr_main;
1136                         }
1137                 }
1138
1139                 common_preview_startjob(sp, stop, do_update, progress);
1140                 shader_preview_free(sp);
1141         }
1142 }
1143
1144 static void icon_preview_endjob(void *customdata)
1145 {
1146         IconPreview *ip = customdata;
1147
1148         if (ip->id) {
1149
1150                 if (GS(ip->id->name) == ID_BR)
1151                         WM_main_add_notifier(NC_BRUSH | NA_EDITED, ip->id);
1152 #if 0           
1153                 if (GS(ip->id->name) == ID_MA) {
1154                         Material *ma = (Material *)ip->id;
1155                         PreviewImage *prv_img = ma->preview;
1156                         int i;
1157
1158                         /* signal to gpu texture */
1159                         for (i = 0; i < NUM_ICON_SIZES; ++i) {
1160                                 if (prv_img->gputexture[i]) {
1161                                         GPU_texture_free(prv_img->gputexture[i]);
1162                                         prv_img->gputexture[i] = NULL;
1163                                         WM_main_add_notifier(NC_MATERIAL|ND_SHADING_DRAW, ip->id);
1164                                 }
1165                         }
1166                 }
1167 #endif
1168         }
1169
1170         if (ip->owner) {
1171                 PreviewImage *prv_img = ip->owner;
1172                 prv_img->tag &= ~PRV_TAG_DEFFERED_RENDERING;
1173                 if (prv_img->tag & PRV_TAG_DEFFERED_DELETE) {
1174                         BLI_assert(prv_img->tag & PRV_TAG_DEFFERED);
1175                         BKE_previewimg_cached_release_pointer(prv_img);
1176                 }
1177         }
1178 }
1179
1180 static void icon_preview_free(void *customdata)
1181 {
1182         IconPreview *ip = (IconPreview *)customdata;
1183
1184         BLI_freelistN(&ip->sizes);
1185         MEM_freeN(ip);
1186 }
1187
1188 void ED_preview_icon_render(Main *bmain, Scene *scene, ID *id, unsigned int *rect, int sizex, int sizey)
1189 {
1190         IconPreview ip = {NULL};
1191         short stop = false, update = false;
1192         float progress = 0.0f;
1193
1194         ED_preview_ensure_dbase();
1195
1196         ip.bmain = bmain;
1197         ip.scene = scene;
1198         ip.owner = id;
1199         ip.id = id;
1200
1201         icon_preview_add_size(&ip, rect, sizex, sizey);
1202
1203         icon_preview_startjob_all_sizes(&ip, &stop, &update, &progress);
1204
1205         icon_preview_endjob(&ip);
1206
1207         BLI_freelistN(&ip.sizes);
1208 }
1209
1210 void ED_preview_icon_job(const bContext *C, void *owner, ID *id, unsigned int *rect, int sizex, int sizey)
1211 {
1212         wmJob *wm_job;
1213         IconPreview *ip, *old_ip;
1214
1215         ED_preview_ensure_dbase();
1216
1217         /* suspended start means it starts after 1 timer step, see WM_jobs_timer below */
1218         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Icon Preview",
1219                              WM_JOB_EXCL_RENDER | WM_JOB_SUSPEND, WM_JOB_TYPE_RENDER_PREVIEW);
1220
1221         ip = MEM_callocN(sizeof(IconPreview), "icon preview");
1222
1223         /* render all resolutions from suspended job too */
1224         old_ip = WM_jobs_customdata_get(wm_job);
1225         if (old_ip)
1226                 BLI_movelisttolist(&ip->sizes, &old_ip->sizes);
1227
1228         /* customdata for preview thread */
1229         ip->bmain = CTX_data_main(C);
1230         ip->scene = CTX_data_scene(C);
1231         ip->owner = owner;
1232         ip->id = id;
1233
1234         icon_preview_add_size(ip, rect, sizex, sizey);
1235
1236         /* Special threading hack: warn main code that this preview is being rendered and cannot be freed... */
1237         {
1238                 PreviewImage *prv_img = owner;
1239                 if (prv_img->tag & PRV_TAG_DEFFERED) {
1240                         prv_img->tag |= PRV_TAG_DEFFERED_RENDERING;
1241                 }
1242         }
1243
1244         /* setup job */
1245         WM_jobs_customdata_set(wm_job, ip, icon_preview_free);
1246         WM_jobs_timer(wm_job, 0.1, NC_WINDOW, NC_WINDOW);
1247         WM_jobs_callbacks(wm_job, icon_preview_startjob_all_sizes, NULL, NULL, icon_preview_endjob);
1248
1249         WM_jobs_start(CTX_wm_manager(C), wm_job);
1250 }
1251
1252 void ED_preview_shader_job(const bContext *C, void *owner, ID *id, ID *parent, MTex *slot, int sizex, int sizey, int method)
1253 {
1254         Object *ob = CTX_data_active_object(C);
1255         wmJob *wm_job;
1256         ShaderPreview *sp;
1257         Scene *scene = CTX_data_scene(C);
1258         short id_type = GS(id->name);
1259         bool use_new_shading = BKE_scene_use_new_shading_nodes(scene);
1260
1261         /* Only texture node preview is supported with Cycles. */
1262         if (use_new_shading && method == PR_NODE_RENDER && id_type != ID_TE) {
1263                 return;
1264         }
1265
1266         ED_preview_ensure_dbase();
1267
1268         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Shader Preview",
1269                             WM_JOB_EXCL_RENDER, WM_JOB_TYPE_RENDER_PREVIEW);
1270         sp = MEM_callocN(sizeof(ShaderPreview), "shader preview");
1271
1272         /* customdata for preview thread */
1273         sp->scene = scene;
1274         sp->owner = owner;
1275         sp->sizex = sizex;
1276         sp->sizey = sizey;
1277         sp->pr_method = method;
1278         sp->id = id;
1279         sp->parent = parent;
1280         sp->slot = slot;
1281         sp->bmain = CTX_data_main(C);
1282
1283         /* hardcoded preview .blend for cycles/internal, this should be solved
1284          * once with custom preview .blend path for external engines */
1285         if ((method != PR_NODE_RENDER) && id_type != ID_TE && use_new_shading) {
1286                 sp->pr_main = G_pr_main_cycles;
1287         }
1288         else {
1289                 sp->pr_main = G_pr_main;
1290         }
1291
1292         if (ob && ob->totcol) copy_v4_v4(sp->col, ob->col);
1293         else sp->col[0] = sp->col[1] = sp->col[2] = sp->col[3] = 1.0f;
1294         
1295         /* setup job */
1296         WM_jobs_customdata_set(wm_job, sp, shader_preview_free);
1297         WM_jobs_timer(wm_job, 0.1, NC_MATERIAL, NC_MATERIAL);
1298         WM_jobs_callbacks(wm_job, common_preview_startjob, NULL, shader_preview_updatejob, NULL);
1299         
1300         WM_jobs_start(CTX_wm_manager(C), wm_job);
1301 }
1302
1303 void ED_preview_kill_jobs(wmWindowManager *wm, Main *bmain)
1304 {
1305         if (wm)
1306                 WM_jobs_kill(wm, NULL, common_preview_startjob);
1307
1308         ED_viewport_render_kill_jobs(wm, bmain, false);
1309 }
1310