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