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