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