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