Blender V5.3
render_preview.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* global includes */
10
11#include <algorithm>
12#include <atomic>
13#include <cmath>
14#include <cstdlib>
15#include <cstring>
16
17#ifndef WIN32
18# include <unistd.h>
19#else
20# include <io.h>
21#endif
26#include "BLI_rect.hh"
27#include "BLI_set.hh"
30
31#include "BLT_translation.hh"
33#include "BLO_readfile.hh"
41#include "DNA_object_types.h"
42#include "DNA_scene_types.h"
49#include "BKE_brush.hh"
50#include "BKE_collection.hh"
51#include "BKE_colortools.hh"
52#include "BKE_context.hh"
53#include "BKE_global.hh"
54#include "BKE_icons.hh"
55#include "BKE_idprop.hh"
56#include "BKE_image.hh"
57#include "BKE_layer.hh"
58#include "BKE_lib_id.hh"
59#include "BKE_library.hh"
60#include "BKE_light.h"
61#include "BKE_main.hh"
62#include "BKE_material.hh"
63#include "BKE_node.hh"
69#include "BKE_scene.hh"
70#include "BKE_screen.hh"
71#include "BKE_texture.h"
72#include "BKE_world.h"
74#include "BLI_math_vector_c.hh"
75
80#include "IMB_imbuf.hh"
82#include "IMB_thumbs.hh"
84#include "BIF_glutil.hh"
86#include "RE_engine.h"
87#include "RE_pipeline.h"
88#include "RE_texture.h"
89
90#include "WM_api.hh"
91#include "WM_types.hh"
93#include "ED_datafiles.h"
94#include "ED_render.hh"
95#include "ED_screen.hh"
96#include "ED_view3d.hh"
100
101#include "ANIM_action.hh"
102#include "ANIM_pose.hh"
103
104namespace blender {
105
106#ifndef NDEBUG
107/* Used for database init assert(). */
108# include "BLI_threads.hh"
109#endif
110
111static void icon_copy_rect(const ImBuf *ibuf, uint w, uint h, uint *rect);
113/* -------------------------------------------------------------------- */
118 /* from wmJob */
119 const void *owner;
121
127 /* Data-blocks with nodes need full copy during preview render, GLSL uses it too. */
135 float color[4];
136
148 Depsgraph *depsgraph; /* May be nullptr (see #WM_OT_previews_ensure). */
150 void *owner;
154 /* Which icon sizes to render. */
157 /* May be nullptr, is used for rendering IDs that require some other object for it to be applied
158 * on before the ID can be represented as an image, for example when rendering an Action. */
160
161 /* Sizes the job was stopped before it finished rendering. */
163};
164
165static bool preview_kill_jobs_for_undo = false;
169/* -------------------------------------------------------------------- */
172
175#ifndef WITH_HEADLESS
176static Main *load_main_from_memory(const void *blend, int blend_size)
177{
178 const int fileflags = G.fileflags;
179 Main *bmain = nullptr;
181
182 G.fileflags |= G_FILE_NO_UI;
183 bfd = BLO_read_from_memory(blend, blend_size, BLO_READ_SKIP_NONE, nullptr);
184 if (bfd) {
185 bmain = bfd->main;
186 MEM_delete(bfd);
187 }
188 G.fileflags = fileflags;
190 return bmain;
191}
192#endif
193
194void ED_preview_ensure_dbase(const bool with_gpencil)
195{
196#ifndef WITH_HEADLESS
197 static bool base_initialized = false;
198 static bool base_initialized_gpencil = false;
200 if (!base_initialized) {
202 base_initialized = true;
204 if (!base_initialized_gpencil && with_gpencil) {
207 base_initialized_gpencil = true;
209#else
210 UNUSED_VARS(with_gpencil);
211#endif
212}
217 return (type->flag & RE_USE_PREVIEW) != 0;
219
221{
222 return ELEM(pr_method, PR_ICON_RENDER, PR_BUTS_RENDER);
227 if (G.pr_main) {
228 BKE_main_free(G.pr_main);
229 }
235
236static Scene *preview_get_scene(Main *pr_main)
237{
238 if (pr_main == nullptr) {
239 return nullptr;
240 }
242 return pr_main->scenes.first();
243}
244
245const char *ED_preview_collection_name(const ePreviewType pr_type)
246{
247 switch (pr_type) {
249 return "Flat";
251 return "Sphere";
252 case MA_CUBE:
253 return "Cube";
254 case MA_SHADERBALL:
255 return "Shader Ball";
257 return "Cloth";
258 case MA_FLUID:
259 return "Fluid";
260 case MA_SPHERE_A:
261 return "World Sphere";
263 return "Lamp";
264 case MA_SKY:
265 return "Sky";
267 return "Hair";
269 return "Atmosphere";
270 default:
271 BLI_assert_msg(0, "Unknown preview type");
272 return "";
273 }
274}
281static void switch_preview_collection_visibility(ViewLayer *view_layer, const ePreviewType pr_type)
283 /* Set appropriate layer as visible. */
284 LayerCollection *lc = view_layer->layer_collections.first();
285 const char *collection_name = ED_preview_collection_name(pr_type);
286
287 for (lc = lc->layer_collections.first(); lc; lc = lc->next) {
288 if (STREQ(lc->collection->id.name + 2, collection_name)) {
290 }
291 else {
294 }
295}
296
297static const char *preview_floor_material_name(const Scene *scene,
298 const ePreviewRenderMethod pr_method)
299{
300 if (pr_method == PR_ICON_RENDER && render_engine_supports_ray_visibility(scene)) {
301 return "FloorHidden";
303 return "Floor";
305
307 Mesh *mesh,
308 const Scene *scene,
309 const ePreviewRenderMethod pr_method)
311 if (mesh->totcol == 0) {
312 return;
313 }
314
315 const char *material_name = preview_floor_material_name(scene, pr_method);
316 Material *mat = static_cast<Material *>(
317 BLI_findstring(&pr_main->materials, material_name, offsetof(ID, name) + 2));
318 if (mat) {
319 mesh->mat[0] = mat;
320 }
321}
322
324 const Scene *scene,
325 ViewLayer *view_layer,
326 const ePreviewRenderMethod pr_method)
327{
328 /* Hide floor for icon renders. */
329 BKE_view_layer_synced_ensure(*pr_main, scene, view_layer);
330 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
331 if (STREQ(base.object->id.name + 2, "Floor")) {
332 base.object->visibility_flag &= ~OB_HIDE_RENDER;
333 if (pr_method == PR_ICON_RENDER) {
335 base.object->visibility_flag |= OB_HIDE_RENDER;
336 }
338 if (base.object->type == OB_MESH) {
340 pr_main, id_cast<Mesh *>(base.object->data), scene, pr_method);
341 }
342 }
343 }
344}
345
347 Scene *scene,
348 ViewLayer *view_layer,
349 const ePreviewType pr_type,
350 const ePreviewRenderMethod pr_method)
351{
352 switch_preview_collection_visibility(view_layer, pr_type);
353 switch_preview_floor_visibility(pr_main, scene, view_layer, pr_method);
354 BKE_layer_collection_sync(*pr_main, scene, view_layer);
355}
356
358{
359 if (world == nullptr) {
360 return nullptr;
361 }
362 if (sp->worldcopy != nullptr) {
363 return sp->worldcopy;
364 }
365
366 ID *id_copy = BKE_id_copy_ex(nullptr,
367 &world->id,
368 nullptr,
371 sp->worldcopy = id_cast<World *>(id_copy);
373 return sp->worldcopy;
374}
375
377{
378 using namespace blender::bke;
379
380 World *world = BKE_world_add(bmain, "SimpleWorld");
381 bNodeTree *ntree = world->nodetree;
382
383 bNode *background = node_add_node(nullptr, *ntree, "ShaderNodeBackground"_ustr);
384 bNode *output = node_add_node(nullptr, *ntree, "ShaderNodeOutputWorld"_ustr);
385 node_add_link(*world->nodetree,
386 *background,
387 *node_find_socket(*background, SOCK_OUT, "Background"_ustr),
388 *output,
389 *node_find_socket(*output, SOCK_IN, "Surface"_ustr));
390 node_set_active(*ntree, *output);
391
392 world->nodetree = ntree;
393
395
396 return world;
397}
398
400{
401 if (sp->world_simple == nullptr) {
403 }
404 return sp->world_simple;
405}
406
407void ED_preview_world_simple_set_rgb(World *world, const float color[4])
408{
409 BLI_assert(world != nullptr);
410
411 bNode *background = bke::node_find_node_by_name(*world->nodetree, "Background");
412 BLI_assert(background != nullptr);
413
414 auto *color_socket = static_cast<bNodeSocketValueRGBA *>(
415 bke::node_find_socket(*background, SOCK_IN, "Color"_ustr)->default_value);
416 copy_v4_v4(color_socket->value, color);
417}
418
419static ID *duplicate_ids(ID *id, const bool allow_failure)
420{
421 if (id == nullptr) {
422 /* Non-ID preview render. */
423 return nullptr;
424 }
425
426 switch (GS(id->name)) {
427 case ID_OB:
428 case ID_MA:
429 case ID_TE:
430 case ID_LA:
431 case ID_WO: {
433 ID *id_copy = BKE_id_copy_ex(nullptr,
434 id,
435 nullptr,
438 return id_copy;
439 }
440 case ID_GR: {
441 /* Doesn't really duplicate the collection. Just creates a collection instance empty. */
443 Object *instance_empty = BKE_object_add_only_object(nullptr, OB_EMPTY, nullptr);
444 instance_empty->instance_collection = id_cast<Collection *>(id);
445 instance_empty->transflag |= OB_DUPLICOLLECTION;
446 return &instance_empty->id;
447 }
448 /* These support threading, but don't need duplicating. */
449 case ID_IM:
451 return nullptr;
452 default:
453 if (!allow_failure) {
454 BLI_assert_msg(0, "ID type preview not supported.");
455 }
456 return nullptr;
457 }
458}
459
460static const char *preview_world_name(const Scene *sce,
461 const ID_Type id_type,
462 const ePreviewRenderMethod pr_method)
463{
464 /* When rendering material icons the floor will not be shown in the output. Cycles will use a
465 * material trick to show the floor in the reflections, but hide the floor for camera rays. For
466 * Eevee we use a transparent world that has a projected grid.
467 *
468 * In the future when Eevee supports VULKAN ray-tracing we can re-evaluate and perhaps remove
469 * this approximation.
470 */
471 if (id_type == ID_MA && pr_method == PR_ICON_RENDER &&
473 {
474 return "WorldFloor";
475 }
476 return "World";
477}
478
479static World *preview_get_world(Main *pr_main,
480 const Scene *sce,
481 const ID_Type id_type,
482 const ePreviewRenderMethod pr_method)
483{
484 World *result = nullptr;
485 const char *world_name = preview_world_name(sce, id_type, pr_method);
486 result = static_cast<World *>(
487 BLI_findstring(&pr_main->worlds, world_name, offsetof(ID, name) + 2));
488
489 /* No world found return first world. */
490 if (result == nullptr) {
491 result = pr_main->worlds.first();
492 }
493
494 BLI_assert_msg(result, "Preview file has no world.");
495 return result;
496}
497
498static void preview_sync_exposure(World *dst, const World *src)
499{
500 BLI_assert(dst);
501 BLI_assert(src);
502 dst->exp = src->exp;
503 dst->range = src->range;
504}
505
507 const Scene *scene,
508 const World *world,
509 const ID_Type id_type,
510 const ePreviewRenderMethod pr_method)
511{
512 World *result = preview_get_world(pr_main, scene, id_type, pr_method);
513 if (world) {
515 }
516 return result;
517}
518
519/* call this with a pointer to initialize preview scene */
520/* call this with nullptr to restore assigned ID pointers in preview scene */
522 Main *bmain, Scene *scene, ID *id, int id_type, ShaderPreview *sp)
523{
524 Scene *sce;
525 Main *pr_main = sp->pr_main;
526
527 memcpy(pr_main->filepath, BKE_main_blendfile_path(bmain), sizeof(pr_main->filepath));
528
529 sce = preview_get_scene(pr_main);
530 if (sce) {
531 ViewLayer *view_layer = sce->view_layers.first();
532
533 /* Only enable the combined render-pass. */
534 view_layer->passflag = SCE_PASS_COMBINED;
536
537 /* This flag tells render to not execute depsgraph or F-Curves etc. */
538 sce->r.scemode |= R_BUTS_PREVIEW;
539 STRNCPY_UTF8(sce->r.engine, scene->r.engine);
540
541 sce->r.color_mgt_flag = scene->r.color_mgt_flag;
543
546
547 if ((id && sp->pr_method == PR_ICON_RENDER) && id_type != ID_WO) {
549 }
550 else {
551 sce->r.alphamode = R_ADDSKY;
552 }
553
554 sce->r.cfra = scene->r.cfra;
555
556 /* Setup the world. */
558 pr_main, sce, scene->world, static_cast<ID_Type>(id_type), sp->pr_method);
559
560 if (id_type == ID_TE) {
561 /* Texture is not actually rendered with engine, just set dummy value. */
563 }
564
565 if (id_type == ID_MA) {
566 Material *mat = nullptr, *origmat = id_cast<Material *>(id);
567
568 if (origmat) {
569 /* work on a copy */
570 BLI_assert(sp->id_copy != nullptr);
571 mat = sp->matcopy = id_cast<Material *>(sp->id_copy);
572 sp->id_copy = nullptr;
573 BLI_addtail(&pr_main->materials, mat);
574
575 /* Use current scene world for lighting. */
576 if (mat->pr_flag == MA_PREVIEW_WORLD && sp->pr_method == PR_BUTS_RENDER) {
577 /* Use current scene world to light sphere. */
578 sce->world = preview_get_localized_world(sp, scene->world);
579 }
580 else if (sce->world && sp->pr_method != PR_ICON_RENDER) {
581 /* Use a default world color. Using the current
582 * scene world can be slow if it has big textures. */
584
585 /* Use brighter world color for grease pencil. */
586 if (sp->pr_main == G_pr_main_grease_pencil) {
587 const float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
589 }
590 else {
591 const float dark[4] = {0.05f, 0.05f, 0.05f, 0.05f};
593 }
594 }
595
596 const ePreviewType preview_type = static_cast<ePreviewType>(mat->pr_type);
597 ED_preview_set_visibility(pr_main, sce, view_layer, preview_type, sp->pr_method);
598 }
599 else {
601 }
602 BKE_view_layer_synced_ensure(*pr_main, sce, view_layer);
603 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
604 if (base.object->id.name[2] == 'p') {
605 /* copy over object color, in case material uses it */
606 copy_v4_v4(base.object->color, sp->color);
607
608 if (OB_TYPE_SUPPORT_MATERIAL(base.object->type)) {
609 /* don't use BKE_object_material_assign, it changed mat->id.us, which shows in the UI
610 */
611 Material ***matar = BKE_object_material_array_p(base.object);
612 int actcol = max_ii(base.object->actcol - 1, 0);
613
614 if (matar && actcol < base.object->totcol) {
615 (*matar)[actcol] = mat;
616 }
617 }
618 else if (base.object->type == OB_LAMP) {
620 }
621 }
622 }
623 }
624 else if (id_type == ID_TE) {
625 Tex *tex = nullptr, *origtex = id_cast<Tex *>(id);
626
627 if (origtex) {
628 BLI_assert(sp->id_copy != nullptr);
629 tex = sp->texcopy = id_cast<Tex *>(sp->id_copy);
630 sp->id_copy = nullptr;
631 BLI_addtail(&pr_main->textures, tex);
632 }
633 }
634 else if (id_type == ID_LA) {
635 Light *la = nullptr, *origla = id_cast<Light *>(id);
636
637 /* work on a copy */
638 if (origla) {
639 BLI_assert(sp->id_copy != nullptr);
640 la = sp->lampcopy = id_cast<Light *>(sp->id_copy);
641 sp->id_copy = nullptr;
642 BLI_addtail(&pr_main->lights, la);
643 }
644
645 ED_preview_set_visibility(pr_main, sce, view_layer, MA_LAMP, sp->pr_method);
646
647 if (sce->world) {
648 /* Only use lighting from the light. */
650 const float black[4] = {0.0f, 0.0f, 0.0f, 0.0f};
652 }
653
654 BKE_view_layer_synced_ensure(*pr_main, sce, view_layer);
655 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
656 if (base.object->id.name[2] == 'p') {
657 if (base.object->type == OB_LAMP) {
658 base.object->data = id_cast<ID *>(la);
659 }
660 }
661 }
662 }
663 else if (id_type == ID_WO) {
664 World *wrld = nullptr, *origwrld = id_cast<World *>(id);
665
666 if (origwrld) {
667 BLI_assert(sp->id_copy != nullptr);
668 wrld = sp->worldcopy = id_cast<World *>(sp->id_copy);
669 sp->id_copy = nullptr;
670 BLI_addtail(&pr_main->worlds, wrld);
671 }
672
673 ED_preview_set_visibility(pr_main, sce, view_layer, MA_SKY, sp->pr_method);
674 sce->world = wrld;
675 }
676
677 return sce;
678 }
679
680 return nullptr;
681}
682
683/* new UI convention: draw is in pixel space already. */
684/* uses ButtonType::Roundbox button in block to get the rect */
686 Scene *scene, const void *owner, int split, int first, const rcti *rect, rcti *newrect)
687{
688 Render *re;
689 RenderView *rv;
690 RenderResult rres;
691 int offx = 0;
692 int newx = BLI_rcti_size_x(rect);
693 int newy = BLI_rcti_size_y(rect);
694 const void *split_owner = (!split || first) ? owner :
695 static_cast<char *>(const_cast<void *>(owner)) + 1;
696 bool ok = false;
697
698 if (split) {
699 if (first) {
700 offx = 0;
701 newx = newx / 2;
702 }
703 else {
704 offx = newx / 2;
705 newx = newx - newx / 2;
706 }
707 }
708
709 /* test if something rendered ok */
710 re = RE_GetRender(split_owner);
711
712 if (re == nullptr) {
713 return false;
714 }
715
717
718 if (!rres.views.is_empty()) {
719 /* material preview only needs monoscopy (view 0) */
720 rv = RE_RenderViewGetById(&rres, 0);
721 }
722 else {
723 /* possible the job clears the views but we're still drawing #45496 */
724 rv = nullptr;
725 }
726
727 if (rv && rv->ibuf) {
728 if (abs(rres.rectx - newx) < 2 && abs(rres.recty - newy) < 2) {
729 newrect->xmax = max_ii(newrect->xmax, rect->xmin + rres.rectx + offx);
730 newrect->ymax = max_ii(newrect->ymax, rect->ymin + rres.recty);
731
732 if (rres.rectx && rres.recty) {
733 float fx = rect->xmin + offx;
734 float fy = rect->ymin;
735
737 rv->ibuf, fx, fy, false, &scene->view_settings, &scene->display_settings, 1.0f, 1.0f);
738
739 ok = true;
740 }
741 }
742 }
743
745
746 return ok;
747}
748
750 const bContext *C, void *idp, void *parentp, void *slotp, uiPreview *ui_preview, rcti *rect)
751{
752 if (idp) {
753 Scene *scene = CTX_data_scene(C);
755 ID *id = static_cast<ID *>(idp);
756 ID *parent = static_cast<ID *>(parentp);
757 MTex *slot = static_cast<MTex *>(slotp);
759 const void *owner = CTX_wm_area(C);
760 ShaderPreview *sp = static_cast<ShaderPreview *>(
762 rcti newrect;
763 bool ok;
764 int newx = BLI_rcti_size_x(rect);
765 int newy = BLI_rcti_size_y(rect);
766
767 newrect.xmin = rect->xmin;
768 newrect.xmax = rect->xmin;
769 newrect.ymin = rect->ymin;
770 newrect.ymax = rect->ymin;
771
772 if (parent) {
773 ok = ed_preview_draw_rect(scene, owner, 1, 1, rect, &newrect);
774 ok &= ed_preview_draw_rect(scene, owner, 1, 0, rect, &newrect);
775 }
776 else {
777 ok = ed_preview_draw_rect(scene, owner, 0, 0, rect, &newrect);
778 }
779
780 if (ok) {
781 *rect = newrect;
782 }
783
784 /* start a new preview render job if signaled through sbuts->preview,
785 * if no render result was found and no preview render job is running,
786 * or if the job is running and the size of preview changed */
787 if ((sbuts != nullptr && sbuts->preview) || (ui_preview->tag & UI_PREVIEW_TAG_DIRTY) ||
789 (sp && (abs(sp->sizex - newx) >= 2 || abs(sp->sizey - newy) > 2)))
790 {
791 if (sbuts != nullptr) {
792 sbuts->preview = 0;
793 }
794 ED_preview_shader_job(C, owner, id, parent, slot, newx, newy, PR_BUTS_RENDER);
795 ui_preview->tag &= ~UI_PREVIEW_TAG_DIRTY;
796 }
797 }
798}
799
800void ED_previews_tag_dirty_by_id(const Main &bmain, const ID &id)
801{
802 for (const bScreen &screen : bmain.screens) {
803 for (const ScrArea &area : screen.areabase) {
804 for (const ARegion &region : area.regionbase) {
805 for (uiPreview &preview : region.ui_previews) {
806 if (preview.id_session_uid == id.session_uid) {
807 preview.tag |= UI_PREVIEW_TAG_DIRTY;
808 }
809 }
810 }
811 }
812 }
813}
814
816
817/* -------------------------------------------------------------------- */
820
822 /* The main for the preview, not of the current file. */
824 /* Copy of the object to create the preview for. The copy is for thread safety (and to insert
825 * it into its own main). */
827 /* Current frame. */
828 int cfra;
829 int sizex;
830 int sizey;
831};
832
834{
835 return OB_TYPE_IS_GEOMETRY(ob->type);
836}
837
839 Scene *scene,
840 ViewLayer *view_layer,
841 Object *preview_object)
842{
843 Object *camera = BKE_object_add(preview_main, scene, view_layer, OB_CAMERA, "Preview Camera");
844
845 float rotmat[3][3];
846 float dummy_scale[3];
847 mat4_to_loc_rot_size(camera->loc, rotmat, dummy_scale, preview_object->object_to_world().ptr());
848
849 /* Camera is Y up, so needs additional rotations to obliquely face the front. */
850 float drotmat[3][3];
851 const float eul[3] = {M_PI * 0.4f, 0.0f, M_PI * 0.1f};
852 eul_to_mat3(drotmat, eul);
853 mul_m3_m3_post(rotmat, drotmat);
854
855 camera->rotmode = ROT_MODE_QUAT;
856 mat3_to_quat(camera->quat, rotmat);
857
858 /* Nice focal length for close portraiture. */
859 (id_cast<Camera *>(camera->data))->lens = 85;
860
861 return camera;
862}
863
865 Depsgraph **r_depsgraph)
866{
867 Scene *scene = BKE_scene_add(preview_data->pr_main, "Object preview scene");
868 /* Preview need to be in the current frame to get a thumbnail similar of what
869 * viewport displays. */
870 scene->r.cfra = preview_data->cfra;
871
872 ViewLayer *view_layer = scene->view_layers.first();
873 Depsgraph *depsgraph = DEG_graph_new(
874 preview_data->pr_main, scene, view_layer, DAG_EVAL_VIEWPORT);
875
876 BLI_assert(preview_data->object != nullptr);
877 BLI_addtail(&preview_data->pr_main->objects, preview_data->object);
878
879 BKE_collection_object_add(preview_data->pr_main, scene->master_collection, preview_data->object);
880
881 Object *camera_object = object_preview_camera_create(
882 preview_data->pr_main, scene, view_layer, preview_data->object);
883
884 scene->camera = camera_object;
885 scene->r.xsch = preview_data->sizex;
886 scene->r.ysch = preview_data->sizey;
887 scene->r.size = 100;
888
889 BKE_view_layer_synced_ensure(*preview_data->pr_main, scene, view_layer);
890 Base *preview_base = BKE_view_layer_base_find(view_layer, preview_data->object);
891 /* For 'view selected' below. */
892 preview_base->flag |= BASE_SELECTED;
893
895 DEG_evaluate_on_refresh(depsgraph);
896
898 preview_data->pr_main, depsgraph, scene, camera_object);
899
900 BKE_scene_graph_update_tagged(depsgraph, preview_data->pr_main);
901
902 *r_depsgraph = depsgraph;
903 return scene;
904}
905
906static void object_preview_render(const PreviewImage *prv_img,
907 IconPreview *preview,
908 const eIconSizes icon_size)
909{
910 Main *preview_main = BKE_main_new();
911 char err_out[256] = "unknown";
912
913 BLI_assert(preview->id_copy && (preview->id_copy != preview->id));
914
915 ObjectPreviewData preview_data = {};
916 preview_data.pr_main = preview_main;
917 /* Act on a copy. */
918 preview_data.object = id_cast<Object *>(preview->id_copy);
919 preview_data.cfra = preview->scene->r.cfra;
920 preview_data.sizex = prv_img->w[icon_size];
921 preview_data.sizey = prv_img->h[icon_size];
922
923 Depsgraph *depsgraph;
924 Scene *scene = object_preview_scene_create(&preview_data, &depsgraph);
925
926 /* Ownership is now ours. */
927 preview->id_copy = nullptr;
928
929 View3DShading shading;
931 /* Enable shadows, makes it a bit easier to see the shape. */
932 shading.flag |= V3D_SHADING_SHADOW;
933
935 DEG_get_evaluated_scene(depsgraph),
936 &shading,
938 DEG_get_evaluated(depsgraph, scene->camera),
939 prv_img->w[icon_size],
940 prv_img->h[icon_size],
944 nullptr,
945 nullptr,
946 nullptr,
947 err_out);
948 /* TODO: color-management? */
949
950 if (ibuf) {
951 icon_copy_rect(ibuf, prv_img->w[icon_size], prv_img->h[icon_size], prv_img->rect[icon_size]);
952 IMB_freeImBuf(ibuf);
953 }
954
955 DEG_graph_free(depsgraph);
956 BKE_main_free(preview_main);
957}
958
960
961/* -------------------------------------------------------------------- */
968
970
971/* -------------------------------------------------------------------- */
974
976{
977 Object *object = preview->active_object;
978 if (object == nullptr) {
979 WM_global_report(RPT_WARNING, "No active object, unable to apply the Action before rendering");
980 return nullptr;
981 }
982 if (object->pose == nullptr) {
984 "Object %s has no pose, unable to apply the Action before rendering",
985 object->id.name + 2);
986 return nullptr;
987 }
988
989 /* Create a backup of the current pose. */
990 animrig::Action &pose_action = reinterpret_cast<bAction *>(preview->id)->wrap();
991
992 if (pose_action.slot_array_num == 0) {
993 WM_global_report(RPT_WARNING, "Action has no data, cannot render preview");
994 return nullptr;
995 }
996
998 PoseBackup *pose_backup = BKE_pose_backup_create_all_bones({object}, &pose_action);
999
1000 /* Apply the Action as pose, so that it can be rendered. This assumes the Action represents a
1001 * single pose, and that thus the evaluation time doesn't matter. */
1002 AnimationEvalContext anim_eval_context = {preview->depsgraph, 0.0f};
1003 animrig::pose_apply_action_all_bones(object, &pose_action, slot.handle, &anim_eval_context);
1004
1005 /* Force evaluation of the new pose, before the preview is rendered. */
1008
1009 return pose_backup;
1010}
1011
1012static void action_preview_render_cleanup(IconPreview *preview, PoseBackup *pose_backup)
1013{
1014 if (pose_backup == nullptr) {
1015 return;
1016 }
1017 BKE_pose_backup_restore(pose_backup);
1018 BKE_pose_backup_free(pose_backup);
1019
1021}
1022
1030static bool action_preview_render(const PreviewImage *prv_img,
1031 IconPreview *preview,
1032 const eIconSizes icon_size)
1033{
1034 char err_out[256] = "";
1035
1036 Depsgraph *depsgraph = preview->depsgraph;
1037 /* Not all code paths that lead to this function actually provide a depsgraph.
1038 * The "Refresh Asset Preview" button (#ED_OT_lib_id_generate_preview) does,
1039 * but #WM_OT_previews_ensure does not. */
1040 BLI_assert(depsgraph != nullptr);
1041 BLI_assert(preview->scene == DEG_get_input_scene(depsgraph));
1042
1043 /* Apply the pose before getting the evaluated scene, so that the new pose is evaluated. */
1044 PoseBackup *pose_backup = action_preview_render_prepare(preview);
1045
1046 Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
1047 Object *camera_eval = scene_eval->camera;
1048 if (camera_eval == nullptr) {
1049 printf("Scene has no camera, unable to render preview of %s without it.\n",
1050 preview->id->name + 2);
1051 action_preview_render_cleanup(preview, pose_backup);
1052 return false;
1053 }
1054
1055 /* This renders with the Workbench engine settings stored on the Scene. */
1057 scene_eval,
1058 nullptr,
1059 OB_SOLID,
1060 camera_eval,
1061 prv_img->w[icon_size],
1062 prv_img->h[icon_size],
1065 R_ADDSKY,
1066 nullptr,
1067 nullptr,
1068 nullptr,
1069 err_out);
1070
1071 action_preview_render_cleanup(preview, pose_backup);
1072
1073 if (err_out[0] != '\0') {
1074 printf("Error rendering Action %s preview: %s\n", preview->id->name + 2, err_out);
1075 }
1076 if (!ibuf) {
1077 return false;
1078 }
1079
1080 icon_copy_rect(ibuf, prv_img->w[icon_size], prv_img->h[icon_size], prv_img->rect[icon_size]);
1081 IMB_freeImBuf(ibuf);
1082 return true;
1083}
1084
1086
1087/* -------------------------------------------------------------------- */
1090
1091static bool scene_preview_is_supported(const Scene *scene)
1092{
1093 return scene->camera != nullptr;
1094}
1095
1096static void scene_preview_render(const PreviewImage *prv_img,
1097 IconPreview *preview,
1098 const eIconSizes icon_size,
1099 ReportList *reports)
1100{
1101 Depsgraph *depsgraph = preview->depsgraph;
1102 /* Not all code paths that lead to this function actually provide a depsgraph.
1103 * The "Refresh Asset Preview" button (#ED_OT_lib_id_generate_preview) does,
1104 * but #WM_OT_previews_ensure does not. */
1105 BLI_assert(depsgraph != nullptr);
1106 BLI_assert(preview->id != nullptr);
1107
1108 Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
1109 Object *camera_eval = scene_eval->camera;
1110 if (camera_eval == nullptr) {
1111 BKE_reportf(reports,
1112 RPT_ERROR,
1113 "Scene has no camera, unable to render preview of %s without it.",
1114 BKE_id_name(*preview->id));
1115 return;
1116 }
1117
1118 char err_out[256] = "";
1119 /* This renders with the Workbench engine settings stored on the Scene. */
1121 scene_eval,
1122 nullptr,
1123 OB_SOLID,
1124 camera_eval,
1125 prv_img->w[icon_size],
1126 prv_img->h[icon_size],
1129 R_ADDSKY,
1130 nullptr,
1131 nullptr,
1132 nullptr,
1133 err_out);
1134
1135 if (err_out[0] != '\0') {
1136 BKE_reportf(reports,
1137 RPT_ERROR,
1138 "Error rendering Scene %s preview: %s.",
1139 BKE_id_name(*preview->id),
1140 err_out);
1141 }
1142
1143 if (ibuf) {
1144 icon_copy_rect(ibuf, prv_img->w[icon_size], prv_img->h[icon_size], prv_img->rect[icon_size]);
1145 IMB_freeImBuf(ibuf);
1146 }
1147}
1148
1150
1151/* -------------------------------------------------------------------- */
1154
1155/* inside thread, called by renderer, sets job update value */
1156static void shader_preview_update(void *spv, RenderResult * /*rr*/)
1157{
1158 ShaderPreview *sp = static_cast<ShaderPreview *>(spv);
1159
1160 *(sp->do_update) = true;
1161}
1162
1163/* called by renderer, checks job value */
1164static bool shader_preview_break(void *spv)
1165{
1166 ShaderPreview *sp = static_cast<ShaderPreview *>(spv);
1167
1168 return *(sp->stop);
1169}
1170
1171static void shader_preview_updatejob(void * /*spv*/) {}
1172
1173/* Renders texture directly to render buffer. */
1174static void shader_preview_texture(ShaderPreview *sp, Tex *tex, Scene *sce, Render *re)
1175{
1176 /* Setup output buffer. */
1177 int width = sp->sizex;
1178 int height = sp->sizey;
1179
1180 /* This is needed otherwise no RenderResult is created. */
1181 sce->r.scemode &= ~R_BUTS_PREVIEW;
1182 RE_InitState(re, nullptr, &sce->r, &sce->view_layers, nullptr, width, height, nullptr);
1183 RE_SetScene(re, sce);
1184
1185 /* Create buffer in empty RenderView created in the init step. */
1187 RenderView *rv = rr->views.first();
1188 ImBuf *rv_ibuf = RE_RenderViewEnsureImBuf(rr, rv);
1189 rv_ibuf->assign_float_data(MEM_new_array_zeroed<float>(size_t(4) * width * height, __func__));
1190 RE_ReleaseResult(re);
1191
1192 /* Get texture image pool (if any) */
1193 ImagePool *img_pool = BKE_image_pool_new();
1194 BKE_texture_fetch_images_for_pool(tex, img_pool);
1195
1196 /* Fill in image buffer. */
1197 float *rect_float = rv_ibuf->float_data_for_write();
1198 float tex_coord[3] = {0.0f, 0.0f, 0.0f};
1199
1200 for (int y = 0; y < height; y++) {
1201 /* Tex coords between -1.0f and 1.0f. */
1202 tex_coord[1] = (float(y) / float(height)) * 2.0f - 1.0f;
1203
1204 for (int x = 0; x < width; x++) {
1205 tex_coord[0] = (float(x) / float(height)) * 2.0f - 1.0f;
1206
1207 /* Evaluate texture at tex_coord. */
1208 TexResult texres = {0};
1209 BKE_texture_get_value_ex(tex, tex_coord, &texres, img_pool, true);
1210 copy_v4_fl4(rect_float,
1211 texres.trgba[0],
1212 texres.trgba[1],
1213 texres.trgba[2],
1214 texres.talpha ? texres.trgba[3] : 1.0f);
1215
1216 rect_float += 4;
1217 }
1218
1219 /* Check if we should cancel texture preview. */
1220 if (shader_preview_break(sp)) {
1221 break;
1222 }
1223 }
1224
1225 BKE_image_pool_free(img_pool);
1226}
1227
1228static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
1229{
1230 Render *re;
1231 Scene *sce;
1232 float oldlens;
1233 short idtype = GS(id->name);
1234 int sizex;
1235 Main *pr_main = sp->pr_main;
1236
1237 /* in case of split preview, use border render */
1238 if (split) {
1239 if (first) {
1240 sizex = sp->sizex / 2;
1241 }
1242 else {
1243 sizex = sp->sizex - sp->sizex / 2;
1244 }
1245 }
1246 else {
1247 sizex = sp->sizex;
1248 }
1249
1250 /* we have to set preview variables first */
1251 sce = preview_get_scene(pr_main);
1252 if (sce) {
1253 sce->r.xsch = sizex;
1254 sce->r.ysch = sp->sizey;
1255 sce->r.size = 100;
1256 }
1257
1258 /* get the stuff from the builtin preview dbase */
1259 sce = preview_prepare_scene(sp->bmain, sp->scene, id, idtype, sp);
1260 if (sce == nullptr) {
1261 return;
1262 }
1263
1264 const void *split_owner = (!split || first) ?
1265 sp->owner :
1266 static_cast<char *>(const_cast<void *>(sp->owner)) + 1;
1267 re = RE_GetRender(split_owner);
1268
1269 /* full refreshed render from first tile */
1270 if (re == nullptr) {
1271 re = RE_NewRender(split_owner);
1272 }
1273
1274 /* sce->r gets copied in RE_InitState! */
1276 sce->r.scemode &= ~R_NO_IMAGE_LOAD;
1277
1278 if (sp->pr_method == PR_ICON_RENDER) {
1279 sce->r.scemode |= R_NO_IMAGE_LOAD;
1281 }
1282 else { /* PR_BUTS_RENDER */
1284 }
1285
1286 /* Callbacks are cleared on GetRender(). */
1287 if (sp->pr_method == PR_BUTS_RENDER) {
1289 }
1290 /* set this for all previews, default is react to G.is_break still */
1292
1293 /* lens adjust */
1294 oldlens = (id_cast<Camera *>(sce->camera->data))->lens;
1295 if (sizex > sp->sizey) {
1296 (id_cast<Camera *>(sce->camera->data))->lens *= float(sp->sizey) / float(sizex);
1297 }
1298
1299 /* entire cycle for render engine */
1300 if (idtype == ID_TE) {
1301 shader_preview_texture(sp, id_cast<Tex *>(id), sce, re);
1302 }
1303 else {
1304 /* Render preview scene */
1305 RE_PreviewRender(re, pr_main, sce);
1306 }
1307
1308 (id_cast<Camera *>(sce->camera->data))->lens = oldlens;
1309
1310 /* handle results */
1311 if (sp->pr_method == PR_ICON_RENDER) {
1312 if (sp->pr_rect) {
1313 RE_ResultGet32(re, reinterpret_cast<uint8_t *>(sp->pr_rect));
1314 }
1315 }
1316
1317 /* unassign the pointers, reset vars */
1318 preview_prepare_scene(sp->bmain, sp->scene, nullptr, GS(id->name), sp);
1319
1320 /* XXX bad exception, end-exec is not being called in render, because it uses local main. */
1321#if 0
1322 if (idtype == ID_TE) {
1323 Tex *tex = (Tex *)id;
1324 if (tex->use_nodes && tex->nodetree) {
1325 ntreeEndExecTree(tex->nodetree);
1326 }
1327 }
1328#endif
1329}
1330
1331/* runs inside thread for material and icons */
1332static void shader_preview_startjob(void *customdata, bool *stop, bool *do_update)
1333{
1334 ShaderPreview *sp = static_cast<ShaderPreview *>(customdata);
1335
1336 sp->stop = stop;
1337 sp->do_update = do_update;
1338
1339 if (sp->parent) {
1340 shader_preview_render(sp, sp->id, 1, 1);
1341 shader_preview_render(sp, sp->parent, 1, 0);
1342 }
1343 else {
1344 shader_preview_render(sp, sp->id, 0, 0);
1345 }
1346
1347 *do_update = true;
1348}
1349
1350static void preview_id_copy_free(ID *id)
1351{
1353 BKE_libblock_free_data(id, false);
1354 MEM_delete(id);
1355}
1356
1357static void shader_preview_free(void *customdata)
1358{
1359 ShaderPreview *sp = static_cast<ShaderPreview *>(customdata);
1360 Main *pr_main = sp->pr_main;
1361 ID *main_id_copy = nullptr;
1362 ID *sub_id_copy = nullptr;
1363
1364 if (sp->matcopy) {
1365 main_id_copy = id_cast<ID *>(sp->matcopy);
1366 BLI_remlink(&pr_main->materials, sp->matcopy);
1367 }
1368 if (sp->texcopy) {
1369 BLI_assert(main_id_copy == nullptr);
1370 main_id_copy = id_cast<ID *>(sp->texcopy);
1371 BLI_remlink(&pr_main->textures, sp->texcopy);
1372 }
1373 if (sp->worldcopy) {
1374 /* worldcopy is also created for material with `Preview World` enabled */
1375 if (main_id_copy) {
1376 sub_id_copy = id_cast<ID *>(sp->worldcopy);
1377 }
1378 else {
1379 main_id_copy = id_cast<ID *>(sp->worldcopy);
1380 }
1381 BLI_remlink(&pr_main->worlds, sp->worldcopy);
1382 }
1383 if (sp->lampcopy) {
1384 BLI_assert(main_id_copy == nullptr);
1385 main_id_copy = id_cast<ID *>(sp->lampcopy);
1386 BLI_remlink(&pr_main->lights, sp->lampcopy);
1387 }
1388 if (sp->world_simple) {
1390 pr_main, &sp->world_simple->id, LIB_ID_FREE_NO_UI_USER | LIB_ID_FREE_NO_DEG_TAG, false);
1391 }
1392 if (sp->own_id_copy) {
1393 if (sp->id_copy) {
1395 }
1396 if (main_id_copy) {
1397 preview_id_copy_free(main_id_copy);
1398 }
1399 if (sub_id_copy) {
1400 preview_id_copy_free(sub_id_copy);
1401 }
1402 }
1403
1404 MEM_delete(sp);
1405}
1406
1408
1409/* -------------------------------------------------------------------- */
1412
1413static void icon_copy_rect(const ImBuf *ibuf, uint w, uint h, uint *rect)
1414{
1415 if (ibuf == nullptr || (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr) ||
1416 rect == nullptr)
1417 {
1418 return;
1419 }
1420
1421 float scaledx, scaledy;
1422 if (ibuf->x > ibuf->y) {
1423 scaledx = float(w);
1424 scaledy = (float(ibuf->y) / float(ibuf->x)) * float(w);
1425 }
1426 else {
1427 scaledx = (float(ibuf->x) / float(ibuf->y)) * float(h);
1428 scaledy = float(h);
1429 }
1430
1431 /* Scaling down must never assign zero width/height, see: #89868. */
1432 int ex = std::max<int>(1, scaledx);
1433 int ey = std::max<int>(1, scaledy);
1434
1435 int dx = (w - ex) / 2;
1436 int dy = (h - ey) / 2;
1437
1438 ImBuf *ima = IMB_scale_into_new(ibuf, ex, ey, IMBScaleFilter::Nearest, false);
1439 if (ima == nullptr) {
1440 return;
1441 }
1442
1443 /* if needed, convert to 32 bits */
1444 if (ima->byte_data() == nullptr) {
1446 }
1447
1448 const uint *srect = reinterpret_cast<const uint *>(ima->byte_data());
1449 uint *drect = rect;
1450
1451 drect += dy * w + dx;
1452 for (; ey > 0; ey--) {
1453 memcpy(drect, srect, ex * sizeof(int));
1454 drect += w;
1455 srect += ima->x;
1456 }
1457
1458 IMB_freeImBuf(ima);
1459}
1460
1461static void set_alpha(char *cp, int sizex, int sizey, char alpha)
1462{
1463 int a, size = sizex * sizey;
1464
1465 for (a = 0; a < size; a++, cp += 4) {
1466 cp[3] = alpha;
1467 }
1468}
1469
1470static void icon_preview_startjob(void *customdata, bool *stop, bool *do_update)
1471{
1472 ShaderPreview *sp = static_cast<ShaderPreview *>(customdata);
1473
1474 if (sp->pr_method == PR_ICON_DEFERRED) {
1476 return;
1477 }
1478
1479 ID *id = sp->id;
1480 short idtype = GS(id->name);
1481
1482 BLI_assert(id != nullptr);
1483
1484 if (idtype == ID_IM) {
1485 Image *ima = id_cast<Image *>(id);
1486 ImBuf *ibuf = nullptr;
1487 ImageUser iuser;
1488 BKE_imageuser_default(&iuser);
1489
1490 if (ima == nullptr) {
1491 return;
1492 }
1493
1494 /* setup dummy image user */
1495 iuser.framenr = 1;
1496 iuser.scene = sp->scene;
1497
1498 /* NOTE(@elubie): this needs to be changed: here image is always loaded if not
1499 * already there. Very expensive for large images. Need to find a way to
1500 * only get existing `ibuf`. */
1501 ibuf = BKE_image_acquire_ibuf(ima, &iuser, nullptr);
1502 if (ibuf == nullptr || (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr)) {
1503 BKE_image_release_ibuf(ima, ibuf, nullptr);
1504 return;
1505 }
1506
1507 icon_copy_rect(ibuf, sp->sizex, sp->sizey, sp->pr_rect);
1508
1509 *do_update = true;
1510
1511 BKE_image_release_ibuf(ima, ibuf, nullptr);
1512 }
1513 else {
1514 /* re-use shader job */
1515 shader_preview_startjob(customdata, stop, do_update);
1516
1517 /* world is rendered with alpha=0, so it wasn't displayed
1518 * this could be render option for sky to, for later */
1519 if (idtype == ID_WO) {
1520 set_alpha(reinterpret_cast<char *>(sp->pr_rect), sp->sizex, sp->sizey, 255);
1521 }
1522 }
1523}
1524
1525/* use same function for icon & shader, so the job manager
1526 * does not run two of them at the same time. */
1527
1528static void common_preview_startjob(void *customdata, wmJobWorkerStatus *worker_status)
1529{
1530 ShaderPreview *sp = static_cast<ShaderPreview *>(customdata);
1531
1533 icon_preview_startjob(customdata, &worker_status->stop, &worker_status->do_update);
1534 }
1535 else {
1536 shader_preview_startjob(customdata, &worker_status->stop, &worker_status->do_update);
1537 }
1538}
1539
1545 IconPreview *ip,
1546 const eIconSizes icon_size,
1547 const ePreviewRenderMethod pr_method,
1548 wmJobWorkerStatus *worker_status)
1549{
1550 ShaderPreview *sp = MEM_new_zeroed<ShaderPreview>("Icon ShaderPreview");
1551
1552 /* These types don't use the ShaderPreview mess, they have their own types and functions. */
1553 BLI_assert(!ip->id || !ELEM(GS(ip->id->name), ID_OB));
1554
1555 /* Construct shader preview from image size and preview custom-data. */
1556 sp->scene = ip->scene;
1557 sp->owner = ip->owner;
1558 sp->sizex = prv_img->w[icon_size];
1559 sp->sizey = prv_img->h[icon_size];
1560 sp->pr_method = pr_method;
1561 sp->pr_rect = prv_img->rect[icon_size];
1562 sp->id = ip->id;
1563 sp->id_copy = ip->id_copy;
1564 sp->bmain = ip->bmain;
1565 sp->own_id_copy = false;
1566 Material *ma = nullptr;
1567
1568 if (sp->pr_method == PR_ICON_RENDER) {
1569 BLI_assert(ip->id);
1570
1571 /* grease pencil use its own preview file */
1572 if (GS(ip->id->name) == ID_MA) {
1573 ma = id_cast<Material *>(ip->id);
1574 }
1575
1576 if ((ma == nullptr) || (ma->gp_style == nullptr)) {
1577 sp->pr_main = G.pr_main;
1578 }
1579 else {
1581 }
1582 }
1583
1584 common_preview_startjob(sp, worker_status);
1586}
1587
1588/* exported functions */
1589
1590static void icon_preview_startjob_all_sizes(void *customdata, wmJobWorkerStatus *worker_status)
1591{
1592 IconPreview *ip = static_cast<IconPreview *>(customdata);
1593
1594 for (int i = 0; i < NUM_ICON_SIZES; i++) {
1595 PreviewImage *prv = static_cast<PreviewImage *>(ip->owner);
1596 const eIconSizes icon_size = eIconSizes(i);
1597 if (ip->render_size[icon_size] == false) {
1598 continue;
1599 }
1600
1601 /* Is this a render job or a deferred loading job? */
1602 const ePreviewRenderMethod pr_method = (prv->runtime->deferred_loading_data) ?
1605
1606 if (worker_status->stop) {
1607 ip->cancelled[icon_size] = true;
1608 continue;
1609 }
1610
1611 /* Non-thread-protected reading is not an issue here, because we are only trying
1612 * to avoid unnecessary work when the preview is to be deleted. */
1613 if (prv->runtime->tag[icon_size] & PRV_TAG_DEFERRED_DELETE) {
1614 continue;
1615 }
1616
1617 /* check_engine_supports_preview() checks whether the engine supports "preview mode" (think:
1618 * Material Preview). This check is only relevant when the render function called below is
1619 * going to use such a mode. Group, Object and Action render functions use Solid mode, though,
1620 * so they can skip this test. Same is true for Images and Brushes, they can also skip this
1621 * test since their preview is just pulled from ImBuf which is not dependent on the render
1622 * engine. */
1623 /* TODO: Decouple the ID-type-specific render functions from this function, so that it's not
1624 * necessary to know here what happens inside lower-level functions. */
1625 const bool use_solid_render_mode = (ip->id != nullptr) &&
1626 ELEM(GS(ip->id->name), ID_OB, ID_AC, ID_IM, ID_GR, ID_SCE);
1627 if (!use_solid_render_mode && preview_method_is_render(pr_method) &&
1629 {
1630 continue;
1631 }
1632
1633 /* Workaround: Skip preview renders for linked IDs. Preview rendering can be slow and even
1634 * freeze the UI (e.g. on Eevee shader compilation). And since the result will never be stored
1635 * in a file, it's done every time the file is reloaded, so this becomes a frequent annoyance.
1636 */
1637 if (!use_solid_render_mode && ip->id && !ID_IS_EDITABLE(ip->id)) {
1638 continue;
1639 }
1640
1642
1643 bool rendered = false;
1644 if (ip->id != nullptr) {
1645 switch (GS(ip->id->name)) {
1646 case ID_OB:
1647 if (object_preview_is_type_supported(id_cast<Object *>(ip->id))) {
1648 /* Much simpler than the ShaderPreview mess used for other ID types. */
1649 object_preview_render(prv, ip, icon_size);
1650 }
1651 rendered = true;
1652 break;
1653 case ID_GR:
1655 reinterpret_cast<const Collection *>(ip->id)));
1656 /* A collection instance empty was created, so this can just reuse the object preview
1657 * rendering. */
1658 object_preview_render(prv, ip, icon_size);
1659 rendered = true;
1660 break;
1661 case ID_AC:
1662 if (!action_preview_render(prv, ip, icon_size)) {
1663 /* Free the preview image, as it's already been overwritten in `icon_set_image()` in
1664 * interface_icons.cc. */
1666 /* Avoid freeing the preview again in icon_preview_endjob(). */
1667 ip->owner = nullptr;
1668 }
1669 /* Always mark as 'rendered' as the call to other_id_types_preview_render() should be
1670 * skipped. */
1671 rendered = true;
1672 break;
1673 case ID_SCE:
1674 scene_preview_render(prv, ip, icon_size, worker_status->reports);
1675 rendered = true;
1676 break;
1677 default:
1678 /* Use the same code as the `ip->id == nullptr` case. */
1679 break;
1680 }
1681 }
1682 if (!rendered) {
1683 other_id_types_preview_render(prv, ip, icon_size, pr_method, worker_status);
1684 }
1685 if (worker_status->stop) {
1686 ip->cancelled[icon_size] = true;
1687 }
1688 }
1689}
1690
1691static void icon_preview_endjob(void *customdata)
1692{
1693 IconPreview *ip = static_cast<IconPreview *>(customdata);
1694
1695 if (ip->owner) {
1696 PreviewImage *prv_img = static_cast<PreviewImage *>(ip->owner);
1697
1698 for (int i = 0; i < NUM_ICON_SIZES; i++) {
1699 if (ip->render_size[i]) {
1700 /* Undo restarts previews that did not finish rendering, per size. For other
1701 * cancellations (e.g. starting an F12 render) there is not yet any mechanism
1702 * to restart them. These remain marked as finished. */
1703 const bool cancel = ip->cancelled[i] && preview_kill_jobs_for_undo;
1704
1706 eIconSizes(i),
1709
1710 if (cancel) {
1712 }
1713 }
1714 }
1715
1716 ip->owner = nullptr;
1717 }
1718}
1719
1736 enum class PreviewState : uint8_t {
1737 NotStarted,
1738 Downloading,
1739 LoadingFromDisk,
1741 Ready,
1744 Failed,
1745 };
1746
1747 struct RequestedPreview {
1748 PreviewImage *preview;
1750 eIconSizes icon_size;
1751 std::atomic<PreviewState> state = PreviewState::NotStarted;
1752
1761 bool is_queued = false;
1762
1763 RequestedPreview(PreviewImage *preview, eIconSizes icon_size)
1764 : preview(preview), icon_size(icon_size)
1765 {
1766 }
1767 RequestedPreview(RequestedPreview &&other)
1768 : preview(other.preview), icon_size(other.icon_size), state(other.state.load())
1769 {
1770 }
1771 };
1772
1777 ThreadQueue *todo_queue_; /* RequestedPreview * */
1779 std::mutex todo_queue_mutex_;
1780
1782 void todo_queue_push(RequestedPreview *request);
1784 RequestedPreview *todo_queue_pop(int wait_time_msec);
1785
1795 Map<std::pair<std::string, eIconSizes>, std::unique_ptr<RequestedPreview>> requested_previews_;
1796 std::mutex requested_previews_mutex_;
1797
1798 public:
1801
1803 static void load_jobless(PreviewImage *preview, eIconSizes icon_size);
1804 static void on_download_requested(StringRef preview_full_filepath);
1805 static void on_download_completed(wmWindowManager *wm, StringRef preview_full_filepath);
1806
1807 void push_load_request(PreviewImage *preview, eIconSizes icon_size);
1808
1809 private:
1817 static Set<std::string> &known_downloaded_previews();
1818
1819 static void run_fn(void *customdata, wmJobWorkerStatus *worker_status);
1820 static void update_fn(void *customdata);
1821 static void end_fn(void *customdata);
1822 static void free_fn(void *customdata);
1823
1825 static void finish_request(RequestedPreview &request);
1826};
1827
1829
1834
1835Set<std::string> &PreviewLoadJob::known_downloaded_previews()
1836{
1838 static Set<std::string> known_downloaded_previews;
1839 return known_downloaded_previews;
1840}
1841
1843{
1844 wmJob *wm_job = WM_jobs_get(
1845 wm, win, nullptr, "Loading previews...", WM_JOB_BACKGROUND, WM_JOB_TYPE_LOAD_PREVIEW);
1846
1847 if (!WM_jobs_is_running(wm_job)) {
1848 PreviewLoadJob *job_data = MEM_new<PreviewLoadJob>("PreviewLoadJobData");
1849
1850 WM_jobs_customdata_set(wm_job, job_data, free_fn);
1851 WM_jobs_timer(wm_job, 0.1, NC_WINDOW, NC_WINDOW);
1852 WM_jobs_callbacks(wm_job, run_fn, nullptr, update_fn, end_fn);
1853
1854 WM_jobs_start(wm, wm_job);
1855 }
1856
1857 return *static_cast<PreviewLoadJob *>(WM_jobs_customdata_get(wm_job));
1858}
1859
1861{
1862 PreviewLoadJob job_data{};
1863
1864 job_data.push_load_request(preview, icon_size);
1865
1866 wmJobWorkerStatus worker_status = {};
1867 run_fn(&job_data, &worker_status);
1868 update_fn(&job_data);
1869 end_fn(&job_data);
1870}
1871
1876{
1879 BLI_assert_msg(!BKE_previewimg_is_rendering(preview, icon_size),
1880 "Preview was already requested and is being loaded");
1881 std::optional<StringRefNull> path = BKE_previewimg_deferred_filepath_get(preview);
1882 if (!path) {
1884 return;
1885 }
1886
1887 BKE_previewimg_render_start(preview, icon_size, true);
1888
1889 const bool is_downloading = BKE_previewimg_is_online(preview) &&
1890 !PreviewLoadJob::known_downloaded_previews().contains_as(*path);
1891
1892 const std::pair key = std::make_pair(*path, icon_size);
1893
1894 RequestedPreview *request = nullptr;
1895 {
1896 std::lock_guard lock(requested_previews_mutex_);
1897
1898 /* Typically shouldn't happen, since previews are flagged with #PRV_RENDERING when loading,
1899 * which should prevent double requests. However, a #PreviewImage might be tagged for deletion
1900 * and recreated while a request is still pending. In that case, update the preview pointer.
1901 *
1902 * This happens when reloading online asset libraries with running preview downloads. The
1903 * assets are destructed then, the preview removed from the global cache (so it won't be
1904 * reused by the subsequent re-request) and tagged for freeing. */
1905 if (std::unique_ptr<RequestedPreview> *existing_request_uptr = requested_previews_.lookup_ptr(
1906 key))
1907 {
1908 RequestedPreview *existing_request = existing_request_uptr->get();
1909 if (existing_request->preview != preview) {
1910 /* This will free the preview if it's tagged with #PRV_TAG_DEFERRED_DELETE. That's
1911 * important since the global cache doesn't hold it anymore and therefore won't free it.
1912 * It's up to us here to end loading properly. */
1913 BKE_previewimg_render_end(existing_request->preview, icon_size, PRV_RENDER_STATUS_FAILED);
1914 existing_request->preview = preview;
1915 }
1916 return;
1917 }
1918
1919 std::unique_ptr<RequestedPreview> new_request = std::make_unique<RequestedPreview>(preview,
1920 icon_size);
1921 request = new_request.get();
1922
1923 if (is_downloading) {
1924 request->state = PreviewState::Downloading;
1925 }
1926 else {
1927 request->state = PreviewState::LoadingFromDisk;
1928 }
1929 requested_previews_.add(key, std::move(new_request));
1930 }
1931
1932 /* NOTE: The request gets pushed to the queue, even when state == PreviewState::Downloading, even
1933 * though PreviewLoadJob::run_fn immediately discards any queued request with that state. The
1934 * reason is that, between this push and that discard, the download may be done, and thus the
1935 * status of the request change. */
1936 this->todo_queue_push(request);
1937}
1938
1939void PreviewLoadJob::todo_queue_push(PreviewLoadJob::RequestedPreview *request)
1940{
1941 std::lock_guard lock(this->todo_queue_mutex_);
1942 if (request->is_queued) {
1943 return;
1944 }
1945
1947 request->is_queued = true;
1948}
1949
1950PreviewLoadJob::RequestedPreview *PreviewLoadJob::todo_queue_pop(const int wait_time_msec)
1951{
1952 RequestedPreview *request = static_cast<RequestedPreview *>(
1953 BLI_thread_queue_pop_timeout(this->todo_queue_, wait_time_msec));
1954
1955 /* Only acquire the lock after the waiting. In the hypothetical case that
1956 * BLI_thread_queue_pop_timeout() would be called while this lock was acquired, nobody else would
1957 * be able to acquire the lock to actually push something onto the queue (because run_fn() below
1958 * is calling this pop function in quick succession; the only thing slowing it down is the
1959 * waiting time here).
1960 *
1961 * There still is a chance of a harmless race condition, when todo_queue_push() executes between
1962 * the pop above and the lock below. This is harmless, because:
1963 *
1964 * - The request is still marked as 'is queued', even though it was just popped off.
1965 * - This means the push function will not push it again.
1966 * - This can happen any number of times, making all the pushes no-ops.
1967 * - If this pop function gets called in the mean time, the queue does not contain this request.
1968 * - Once the lock below is acquired, the request is marked as 'not queued', and returned to the
1969 * caller.
1970 *
1971 * The end state is that the request was still only returned by a pop function once, even though
1972 * its 'is queued' status was slightly misleading in the mean time. This is why that field is
1973 * documented as "do not use for anything else".
1974 */
1975 std::lock_guard lock(this->todo_queue_mutex_);
1976
1977 if (!request) {
1978 return nullptr;
1979 }
1980 request->is_queued = false;
1981 return request;
1982}
1983
1988 const StringRef preview_full_filepath)
1989{
1991 "This function is meant to be called from external code, not from the job");
1992
1993 PreviewLoadJob *load_job = static_cast<PreviewLoadJob *>(
1995 if (!load_job) {
1996 PreviewLoadJob::known_downloaded_previews().add_as(preview_full_filepath);
1997 return;
1998 }
1999
2000 bool has_request = false;
2001 /* Transition each preview request that uses this filepath from 'Downloading' to
2002 * 'LoadingFromDisk' and push it to the TODO queue, to trigger the actual loading from disk. */
2003 {
2004 std::lock_guard lock(load_job->requested_previews_mutex_);
2005 /* See if this download can be matched to one or more preview requests (from the UI). */
2006 for (int size = 0; size < NUM_ICON_SIZES; size++) {
2007 const std::pair key = std::make_pair(preview_full_filepath, eIconSizes(size));
2008 std::unique_ptr<RequestedPreview> *request_uptr = load_job->requested_previews_.lookup_ptr(
2009 key);
2010 if (!request_uptr) {
2011 continue;
2012 }
2013 has_request = true;
2014 RequestedPreview *request = request_uptr->get();
2015
2016 if (request->state != PreviewState::Downloading) {
2017 continue;
2018 }
2019
2020 /* Ensure the request from the UI gets answered, this is done in PreviewLoadJob::run_fn. */
2021 request->state = PreviewState::LoadingFromDisk;
2022 load_job->todo_queue_push(request);
2023 }
2024 }
2025
2026 if (!has_request) {
2027 /* No pending request, but one might still be coming. Add it to the "known" downloaded
2028 * previews. */
2029 PreviewLoadJob::known_downloaded_previews().add_as(preview_full_filepath);
2030 }
2031}
2032
2033void PreviewLoadJob::on_download_requested(const StringRef preview_full_filepath)
2034{
2035 /* Preview was requested. Allow the system to detect it as being downloaded by removing it from
2036 * the files known as "already downloaded". This way downloaded previews don't linger around
2037 * as "already downloaded" forever, and their downloading state can be recognized correctly. */
2038 PreviewLoadJob::known_downloaded_previews().remove_as(preview_full_filepath);
2039}
2040
2041void PreviewLoadJob::run_fn(void *customdata, wmJobWorkerStatus *worker_status)
2042{
2043 PreviewLoadJob *job_data = static_cast<PreviewLoadJob *>(customdata);
2044
2046
2047 bool has_work = true;
2048 /* Keep this loop running while there are any requests in the 'Downloading' or 'LoadingFromDisk'
2049 * state. This way previews that are done downloading don't need to be re-requested to actually
2050 * show up. */
2051 while (has_work && !worker_status->stop) {
2052 RequestedPreview *request = job_data->todo_queue_pop(100);
2053
2054 if (worker_status->stop) {
2055 break;
2056 }
2057
2058 has_work = request != nullptr;
2059 if (!has_work) {
2060 /* No immediate work; check if any previews are still pending. */
2061 std::lock_guard lock(job_data->requested_previews_mutex_);
2062 for (std::unique_ptr<RequestedPreview> &check_request :
2063 job_data->requested_previews_.values())
2064 {
2065 const PreviewState state = check_request->state.load();
2066 if (ELEM(state, PreviewState::Downloading, PreviewState::LoadingFromDisk)) {
2067 has_work = true;
2068 break;
2069 }
2070 }
2071
2072 continue;
2073 }
2074
2075 BLI_assert(request);
2076 /* Should never happen, because preview requests are only queued once they are 'started'. */
2077 BLI_assert(request->state != PreviewState::NotStarted);
2078 /* Should never happen, because the request only goes to these states at the end of this loop
2079 * body, in which case the request has already been popped off the queue. So it shouldn't be
2080 * seen here. */
2081 BLI_assert(!ELEM(request->state, PreviewState::Ready, PreviewState::Failed));
2082
2083 if (request->state == PreviewState::Downloading) {
2084 /* This request can be safely remain popped off the queue, as the on_download_completed()
2085 * function will push it back on once the download is complete. This job loop will be kept
2086 * alive while things are downloading anyway, independent of todo_queue_ (see the `has_work`
2087 * check above). */
2088 continue;
2089 }
2090
2091 PreviewImage *preview = request->preview;
2092
2093 const std::optional<int> source = BKE_previewimg_deferred_thumb_source_get(preview);
2094 const std::optional<StringRefNull> filepath = BKE_previewimg_deferred_filepath_get(preview);
2095
2096 if (!source || !filepath) {
2097 continue;
2098 }
2099
2100 // printf("loading deferred %dx%d preview for %s\n", request->sizex, request->sizey, filepath);
2101
2102 IMB_thumb_path_lock(filepath->c_str());
2103 ImBuf *thumb = IMB_thumb_manage(filepath->c_str(), THB_LARGE, ThumbSource(*source));
2104 IMB_thumb_path_unlock(filepath->c_str());
2105
2106 if (thumb) {
2107 /* PreviewImage assumes premultiplied alpha. */
2108 IMB_premultiply_alpha(thumb);
2109
2110 if (ED_preview_use_image_size(preview, request->icon_size)) {
2111 preview->w[request->icon_size] = thumb->x;
2112 preview->h[request->icon_size] = thumb->y;
2113 BLI_assert(preview->rect[request->icon_size] == nullptr);
2114 preview->rect[request->icon_size] = reinterpret_cast<uint *>(
2115 MEM_dupalloc(thumb->byte_data()));
2116 }
2117 else {
2118 icon_copy_rect(thumb,
2119 preview->w[request->icon_size],
2120 preview->h[request->icon_size],
2121 preview->rect[request->icon_size]);
2122 }
2123 IMB_freeImBuf(thumb);
2124 }
2125
2126 request->state = thumb ? PreviewState::Ready : PreviewState::Failed;
2127 worker_status->do_update = true;
2128 }
2129
2131}
2132
2133/* Only execute on the main thread! */
2134void PreviewLoadJob::finish_request(RequestedPreview &request)
2135{
2137
2138 PreviewImage *preview = request.preview;
2139
2141 request.icon_size,
2142 request.state == PreviewState::Failed ? PRV_RENDER_STATUS_FAILED :
2144}
2145
2146void PreviewLoadJob::update_fn(void *customdata)
2147{
2148 PreviewLoadJob *job_data = static_cast<PreviewLoadJob *>(customdata);
2149
2150 Vector<std::pair<StringRef, eIconSizes>> finished_requests;
2151
2152 std::lock_guard lock(job_data->requested_previews_mutex_);
2153 for (const auto item : job_data->requested_previews_.items()) {
2154 std::unique_ptr<RequestedPreview> &requested = item.value;
2155
2156 /* Skip items that are not done loading yet. */
2157 if (ELEM(requested->state, PreviewState::Ready, PreviewState::Failed)) {
2158 finish_request(*requested);
2159 finished_requests.append(item.key);
2160 }
2161 }
2162
2163 for (auto &key : finished_requests) {
2164 job_data->requested_previews_.remove(key);
2165 }
2166}
2167
2168void PreviewLoadJob::end_fn(void *customdata)
2169{
2170 PreviewLoadJob *job_data = static_cast<PreviewLoadJob *>(customdata);
2171
2172 std::lock_guard lock(job_data->requested_previews_mutex_);
2173 /* Finish any possibly remaining queued previews. */
2174 for (std::unique_ptr<RequestedPreview> &request : job_data->requested_previews_.values()) {
2175 finish_request(*request);
2176 }
2177 job_data->requested_previews_.clear();
2178}
2179
2180void PreviewLoadJob::free_fn(void *customdata)
2181{
2182 MEM_delete(static_cast<PreviewLoadJob *>(customdata));
2183}
2184
2185static void icon_preview_free(void *customdata)
2186{
2187 IconPreview *ip = static_cast<IconPreview *>(customdata);
2188
2189 for (int i = 0; i < NUM_ICON_SIZES; i++) {
2190 ip->cancelled[i] = true;
2191 }
2192 icon_preview_endjob(customdata);
2193
2194 if (ip->id_copy) {
2196 }
2197
2198 MEM_delete(ip);
2199}
2200
2202{
2203 return size == ICON_SIZE_PREVIEW && preview->runtime->deferred_loading_data;
2204}
2205
2206bool ED_preview_id_render_is_supported(const ID *id, const char **r_disabled_hint)
2207{
2208 if (id == nullptr) {
2209 return false;
2210 }
2211
2212 /* Get both the result and the "potential" disabled hint. After that we can decide if the
2213 * disabled hint needs to be returned to the caller. */
2214 const auto [result, disabled_hint] = [id]() -> std::pair<bool, const char *> {
2215 switch (GS(id->name)) {
2216 case ID_NT:
2217 return {false, RPT_("Node groups do not support automatic previews")};
2218 case ID_OB:
2219 return {object_preview_is_type_supported(id_cast<const Object *>(id)),
2220 RPT_("Object type does not support automatic previews")};
2221 case ID_GR:
2222 return {
2223 BKE_collection_contains_geometry_recursive(reinterpret_cast<const Collection *>(id)),
2224 RPT_("Collection does not contain object types that can be rendered for the automatic "
2225 "preview")};
2226 case ID_SCE:
2227 return {scene_preview_is_supported(id_cast<const Scene *>(id)),
2228 RPT_("Scenes without a camera do not support previews")};
2229 case ID_BR:
2230 return {false, RPT_("Brushes do not support automatic previews")};
2231 case ID_MA:
2232 return {true, ""};
2233 case ID_TE:
2234 return {true, ""};
2235 case ID_WO:
2236 return {true, ""};
2237 case ID_LA:
2238 return {true, ""};
2239 case ID_IM:
2240 return {true, ""};
2241 case ID_AC:
2242 return {true, ""};
2243 case ID_SCR:
2244 return {false, RPT_("Screens do not support automatic previews")};
2245 default:
2247 return {false, RPT_("Data-block type does not support automatic previews")};
2248 }
2249 }();
2250
2251 if (result == false && disabled_hint && r_disabled_hint) {
2252 *r_disabled_hint = disabled_hint;
2253 }
2254
2255 return result;
2256}
2257
2259 const bContext *C, Scene *scene, PreviewImage *prv_img, ID *id, eIconSizes icon_size)
2260{
2261 /* Deferred loading of previews from the file system. */
2262 if (prv_img->runtime->deferred_loading_data) {
2263 if (BKE_previewimg_is_rendering(prv_img, icon_size)) {
2264 /* Already in the queue, don't add it again. */
2265 return;
2266 }
2267
2268 PreviewLoadJob::load_jobless(prv_img, icon_size);
2269 return;
2270 }
2271
2272 /* Check if the ID supports the auto-generated previews at all. */
2274 return;
2275 }
2276
2277 IconPreview ip = {nullptr};
2278
2280
2281 ip.bmain = CTX_data_main(C);
2282 if (GS(id->name) == ID_SCE) {
2283 Scene *icon_scene = reinterpret_cast<Scene *>(id);
2284 ip.scene = icon_scene;
2287 ip.active_object = nullptr;
2288 }
2289 else {
2290 ip.scene = scene;
2292 /* Control isn't given back to the caller until the preview is done. So we don't need to copy
2293 * the ID to avoid thread races. */
2294 ip.id_copy = duplicate_ids(id, true);
2296 }
2298 ip.id = id;
2299
2300 BKE_previewimg_render_start(prv_img, icon_size, false);
2301 ip.render_size[icon_size] = true;
2302
2303 wmJobWorkerStatus worker_status = {};
2304 icon_preview_startjob_all_sizes(&ip, &worker_status);
2305
2307
2308 if (ip.id_copy != nullptr) {
2310 }
2311}
2312
2314 const bContext *C, PreviewImage *prv_img, ID *id, eIconSizes icon_size, const bool delay)
2315{
2316 /* Deferred loading of previews from the file system. */
2317 if (prv_img->runtime->deferred_loading_data) {
2318 if (BKE_previewimg_is_rendering(prv_img, icon_size)) {
2319 /* Already in the queue, don't add it again. */
2320 return;
2321 }
2323 load_job.push_load_request(prv_img, icon_size);
2324
2325 return;
2326 }
2327
2328 /* Check if the ID supports the auto-generated previews at all. */
2330 return;
2331 }
2332
2333 IconPreview *ip, *old_ip;
2334
2336
2337 /* suspended start means it starts after 1 timer step, see WM_jobs_timer below */
2338 wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
2340 prv_img,
2341 "Generating icon preview...",
2344
2345 ip = MEM_new_zeroed<IconPreview>("icon preview");
2346
2347 /* render all resolutions from suspended job too */
2348 old_ip = static_cast<IconPreview *>(WM_jobs_customdata_get(wm_job));
2349 if (old_ip) {
2350 for (int i = 0; i < NUM_ICON_SIZES; i++) {
2351 ip->render_size[i] = old_ip->render_size[i];
2352 old_ip->render_size[i] = false;
2353 }
2354 }
2355
2356 /* customdata for preview thread */
2357 ip->bmain = CTX_data_main(C);
2358 if (GS(id->name) == ID_SCE) {
2359 Scene *icon_scene = reinterpret_cast<Scene *>(id);
2360 ip->scene = icon_scene;
2363 ip->active_object = nullptr;
2364 }
2365 else {
2368 ip->id_copy = duplicate_ids(id, false);
2370 }
2371 ip->owner = prv_img;
2372 ip->id = id;
2373
2374 BKE_previewimg_render_start(prv_img, icon_size, true);
2375 ip->render_size[icon_size] = true;
2376
2377 /* setup job */
2379 WM_jobs_timer(wm_job, 0.1, NC_WINDOW, NC_WINDOW);
2380 /* Wait 2s to start rendering icon previews, to not bog down user interaction.
2381 * Particularly important for heavy scenes and Eevee using OpenGL that blocks
2382 * the user interface drawing. */
2383 WM_jobs_delay_start(wm_job, (delay) ? 2.0 : 0.0);
2385 wm_job, icon_preview_startjob_all_sizes, nullptr, nullptr, icon_preview_endjob);
2386
2387 WM_jobs_start(CTX_wm_manager(C), wm_job);
2388}
2389
2391 const void *owner,
2392 ID *id,
2393 ID *parent,
2394 MTex *slot,
2395 int sizex,
2396 int sizey,
2397 ePreviewRenderMethod method)
2398{
2400 wmJob *wm_job;
2401 ShaderPreview *sp;
2402 Scene *scene = CTX_data_scene(C);
2403 const ID_Type id_type = GS(id->name);
2404
2406
2407 /* Use workspace render only for buttons Window,
2408 * since the other previews are related to the datablock. */
2409
2411 return;
2412 }
2413
2415
2416 wm_job = WM_jobs_get(CTX_wm_manager(C),
2418 owner,
2419 "Generating shader preview...",
2422 sp = MEM_new_zeroed<ShaderPreview>("shader preview");
2423
2424 /* customdata for preview thread */
2425 sp->scene = scene;
2426 sp->owner = owner;
2427 sp->sizex = sizex;
2428 sp->sizey = sizey;
2429 sp->pr_method = method;
2430 sp->id = id;
2431 sp->id_copy = duplicate_ids(id, false);
2432 sp->own_id_copy = true;
2433 sp->parent = parent;
2434 sp->slot = slot;
2435 sp->bmain = CTX_data_main(C);
2436 Material *ma = nullptr;
2437
2438 /* hardcoded preview .blend for Eevee + Cycles, this should be solved
2439 * once with custom preview .blend path for external engines */
2440
2441 /* grease pencil use its own preview file */
2442 if (id_type == ID_MA) {
2443 ma = id_cast<Material *>(id);
2444 }
2445
2446 if ((ma == nullptr) || (ma->gp_style == nullptr)) {
2447 sp->pr_main = G.pr_main;
2448 }
2449 else {
2451 }
2452
2453 if (ob && ob->totcol) {
2454 copy_v4_v4(sp->color, ob->color);
2455 }
2456 else {
2457 ARRAY_SET_ITEMS(sp->color, 0.0f, 0.0f, 0.0f, 1.0f);
2458 }
2459
2460 /* setup job */
2462 WM_jobs_timer(wm_job, 0.1, NC_MATERIAL, NC_MATERIAL);
2464
2465 WM_jobs_start(CTX_wm_manager(C), wm_job);
2466}
2467
2469{
2470 if (wm) {
2471 /* This is called to stop all preview jobs before scene data changes, to
2472 * avoid invalid memory access. */
2474 }
2475}
2476
2483
2485{
2486 const PreviewImage *preview = BKE_previewimg_id_get(id);
2487 if (wm && preview) {
2489 }
2490}
2491
2492void ED_preview_online_download_requested(const StringRef preview_full_filepath)
2493{
2494 PreviewLoadJob::on_download_requested(preview_full_filepath);
2495}
2496
2498 const StringRef preview_full_filepath)
2499{
2500 PreviewLoadJob::on_download_completed(wm, preview_full_filepath);
2501}
2502
2504{
2505 Main *bmain = CTX_data_main(C);
2506
2507 if (!bmain->need_preview_render_restart) {
2508 return;
2509 }
2510
2511 ID *id = nullptr;
2512 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2513 PreviewImage *preview = BKE_previewimg_id_get(id);
2514 if (!preview) {
2515 continue;
2516 }
2517
2518 for (int i = 0; i < NUM_ICON_SIZES; i++) {
2519 if (BKE_previewimg_render_restart(preview, i)) {
2521 ui::icon_render_id(C, nullptr, id, eIconSizes(i), true);
2522 }
2523 }
2524 }
2526
2527 bmain->need_preview_render_restart = false;
2528}
2529
2531
2532} // namespace blender
Functions and classes to work with Actions.
Functions to work with animation poses.
General operations, lookup, etc. for blender lights.
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define M_PI
#define STRNCPY_UTF8(dst, src)
external readfile function prototypes.
#define RPT_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define OB_TYPE_IS_GEOMETRY(_type)
#define OB_TYPE_SUPPORT_MATERIAL(_type)
const char datatoc_preview_grease_pencil_blend[]
int datatoc_preview_grease_pencil_blend_size
const char datatoc_preview_blend[]
int datatoc_preview_blend_size
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_WINDOW
Definition WM_types.hh:388
#define NC_MATERIAL
Definition WM_types.hh:393
volatile int lock
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define ARRAY_SET_ITEMS(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define output
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void clear()
Definition BLI_map.hh:1038
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
ValueIterator values() const &
Definition BLI_map.hh:884
bool remove(const Key &key)
Definition BLI_map.hh:368
ItemIterator items() const &
Definition BLI_map.hh:902
static void load_jobless(PreviewImage *preview, eIconSizes icon_size)
static PreviewLoadJob & ensure_job(wmWindowManager *wm, wmWindow *win)
static void on_download_requested(StringRef preview_full_filepath)
void push_load_request(PreviewImage *preview, eIconSizes icon_size)
static void on_download_completed(wmWindowManager *wm, StringRef preview_full_filepath)
void append(const T &value)
#define offsetof(t, d)
#define abs
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
const int state
void pose_apply_action_all_bones(Object *ob, bAction *action, slot_handle_t slot_handle, const AnimationEvalContext *anim_eval_context)
Slot & get_best_pose_slot_for_id(const ID &id, Action &pose_data)
Definition pose.cc:471
bNode * node_find_node_by_name(bNodeTree &ntree, StringRefNull name)
Definition node.cc:4047
bNode * node_add_node(const bContext *C, bNodeTree &ntree, UString idname, std::optional< int > unique_identifier=std::nullopt)
Definition node.cc:4260
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
BLI_INLINE Texture * wrap(MTLTexture *texture)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void icon_render_id(const bContext *C, Scene *scene, ID *id, enum eIconSizes size, bool use_job)
BMesh Iterators.
static void switch_preview_floor_visibility(Main *pr_main, const Scene *scene, ViewLayer *view_layer, const ePreviewRenderMethod pr_method)
bool ED_preview_id_render_is_supported(const ID *id, const char **r_disabled_hint=nullptr)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Render * RE_GetRender(const void *owner)
Object * BKE_object_add(Main *bmain, Scene *scene, ViewLayer *view_layer, ObjectType type, const char *name) ATTR_NONNULL(1
Definition object.cc:2296
void WM_global_reportf(eReportType type, const char *format,...)
static bool ed_preview_draw_rect(Scene *scene, const void *owner, int split, int first, const rcti *rect, rcti *newrect)
static void object_preview_render(const PreviewImage *prv_img, IconPreview *preview, const eIconSizes icon_size)
ScrArea * CTX_wm_area(const bContext *C)
void eul_to_mat3(float mat[3][3], const float eul[3])
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
void BKE_previewimg_id_free(ID *id)
RenderView * RE_RenderViewGetById(RenderResult *rr, const int view_id)
void IMB_thumb_locks_release()
Definition thumbs.cc:807
void ED_preview_kill_jobs_for_id(wmWindowManager *wm, const ID *id)
static bool preview_kill_jobs_for_undo
bool BKE_previewimg_render_restart(PreviewImage *prv, int size)
@ NUM_ICON_SIZES
@ ICON_SIZE_PREVIEW
@ RPT_WARNING
Definition BKE_report.hh:40
void ED_preview_restart_work(const bContext *C)
void DEG_graph_build_from_view_layer(Depsgraph *graph)
@ BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL
void RE_ResultGet32(Render *re, uint8_t *dst)
static bool shader_preview_break(void *spv)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
static ID * duplicate_ids(ID *id, const bool allow_failure)
void WM_jobs_delay_start(wmJob *wm_job, double delay_time)
Definition wm_jobs.cc:402
static void shader_preview_update(void *spv, RenderResult *)
unsigned int uint
Definition BKE_text.h:55
void BKE_previewimg_clear_single(PreviewImage *prv, enum eIconSizes size)
void RE_test_break_cb(Render *re, void *handle, bool(*f)(void *handle))
bool BKE_previewimg_is_online(const PreviewImage *prv)
void IMB_thumb_path_unlock(const char *path)
Definition thumbs.cc:836
static bool preview_method_is_render(const ePreviewRenderMethod pr_method)
void * BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms)
Definition threads.cc:787
Material *** BKE_object_material_array_p(Object *ob)
void DEG_graph_free(Depsgraph *graph)
Definition depsgraph.cc:307
void ED_preview_kill_jobs_for_undo(wmWindowManager *wm, Main *bmain)
@ PRV_TAG_DEFERRED_DELETE
Definition DNA_ID.h:630
void BKE_screen_view3d_shading_init(View3DShading *shading)
Definition screen.cc:1262
@ BASE_ENABLED_AND_MAYBE_VISIBLE_IN_VIEWPORT
void BKE_previewimg_render_end(PreviewImage *prv, int size, PreviewImageRenderEndStatus status)
void mul_m3_m3_post(float R[3][3], const float B[3][3])
static World * preview_get_world_simple(ShaderPreview *sp)
std::optional< int > BKE_previewimg_deferred_thumb_source_get(const PreviewImage *prv)
bool ED_preview_use_image_size(const PreviewImage *preview, eIconSizes size)
void BKE_pose_backup_restore(const struct PoseBackup *pbd)
static void shader_preview_updatejob(void *)
@ WM_JOB_TYPE_LOAD_PREVIEW
Definition WM_api.hh:2014
@ WM_JOB_TYPE_RENDER_PREVIEW
Definition WM_api.hh:2012
static void common_preview_startjob(void *customdata, wmJobWorkerStatus *worker_status)
static Object * object_preview_camera_create(Main *preview_main, Scene *scene, ViewLayer *view_layer, Object *preview_object)
void ED_draw_imbuf(const ImBuf *ibuf, float x, float y, bool use_filter, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, float zoom_x, float zoom_y)
Definition glutil.cc:121
void IMB_thumb_locks_acquire()
Definition thumbs.cc:793
Main * BKE_main_new()
Definition main.cc:93
@ RE_USE_PREVIEW
Definition RE_engine.h:46
static void icon_copy_rect(const ImBuf *ibuf, uint w, uint h, uint *rect)
void BKE_id_free_ex(Main *bmain, void *idv, int flag_orig, bool use_flag_from_idtag)
Depsgraph * DEG_graph_new(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
Definition depsgraph.cc:279
void BKE_texture_get_value_ex(struct Tex *texture, const float *tex_co, struct TexResult *texres, struct ImagePool *pool, bool use_color_management)
Definition texture.cc:618
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
@ V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS
void RE_SetScene(Render *re, Scene *sce)
World * ED_preview_prepare_world_simple(Main *bmain)
wmWindow * CTX_wm_window(const bContext *C)
static bool object_preview_is_type_supported(const Object *ob)
MINLINE int max_ii(int a, int b)
MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
@ LIB_ID_FREE_NO_DEG_TAG
@ LIB_ID_FREE_NO_UI_USER
static void switch_preview_floor_material(Main *pr_main, Mesh *mesh, const Scene *scene, const ePreviewRenderMethod pr_method)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
void BKE_texture_fetch_images_for_pool(struct Tex *texture, struct ImagePool *pool)
Definition texture.cc:663
const char * BKE_id_name(const ID &id)
@ WM_JOB_EXCL_RENDER
Definition WM_api.hh:1995
@ WM_JOB_BACKGROUND
Definition WM_api.hh:1999
static void other_id_types_preview_render(const PreviewImage *prv_img, IconPreview *ip, const eIconSizes icon_size, const ePreviewRenderMethod pr_method, wmJobWorkerStatus *worker_status)
void DEG_evaluate_on_refresh(Depsgraph *graph, DepsgraphEvaluateSyncWriteback sync_writeback=DEG_EVALUATE_SYNC_WRITEBACK_NO)
static const char * preview_world_name(const Scene *sce, const ID_Type id_type, const ePreviewRenderMethod pr_method)
Render * RE_NewRender(const void *owner)
void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings, const ColorManagedViewSettings *settings)
World * ED_preview_prepare_world(Main *pr_main, const Scene *scene, const World *world, ID_Type id_type, ePreviewRenderMethod pr_method)
static Scene * preview_prepare_scene(Main *bmain, Scene *scene, ID *id, int id_type, ShaderPreview *sp)
static bool action_preview_render(const PreviewImage *prv_img, IconPreview *preview, const eIconSizes icon_size)
static void shader_preview_free(void *customdata)
@ LIB_ID_COPY_LOCALIZE
@ LIB_ID_COPY_NO_ANIMDATA
@ LIB_ID_CREATE_LOCAL
bool BKE_previewimg_id_supports_jobs(const ID *id)
void BKE_previewimg_render_start(PreviewImage *prv, int size, bool using_job)
void IMB_thumb_path_lock(const char *path)
Definition thumbs.cc:822
PreviewImage ** BKE_previewimg_id_get_p(const ID *id)
static void icon_preview_endjob(void *customdata)
const char * RE_engine_id_BLENDER_EEVEE
Definition scene.cc:1717
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
void BKE_libblock_free_data(ID *id, bool do_id_user) ATTR_NONNULL()
static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void * WM_jobs_customdata_get(wmJob *wm_job)
Definition wm_jobs.cc:369
static void set_alpha(char *cp, int sizex, int sizey, char alpha)
void ED_preview_kill_jobs(wmWindowManager *wm, Main *bmain)
static World * preview_get_localized_world(ShaderPreview *sp, World *world)
static void icon_preview_free(void *customdata)
void ED_preview_world_simple_set_rgb(World *world, const float color[4])
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void IMB_freeImBuf(ImBuf *ibuf)
static void preview_sync_exposure(World *dst, const World *src)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings, const ColorManagedDisplaySettings *settings)
RenderResult * RE_AcquireResultWrite(Render *re)
@ PRV_RENDER_STATUS_CANCELLED
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
static bool scene_preview_is_supported(const Scene *scene)
static Main * G_pr_main_grease_pencil
bool WM_jobs_has_running(const wmWindowManager *window_manager, const void *owner, const eWM_JobType type, const eWM_JobFlag exclude_flags)
Definition wm_jobs.cc:809
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool BKE_collection_contains_geometry_recursive(const Collection *collection)
const char * ED_preview_collection_name(ePreviewType pr_type)
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static void action_preview_render_cleanup(IconPreview *preview, PoseBackup *pose_backup)
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams &params={})
unsigned char uint8_t
Definition BKE_text.h:79
PreviewImage * BKE_previewimg_id_ensure(ID *id)
void BKE_scene_graph_update_tagged(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2867
Scene * BKE_scene_add(Main *bmain, const char *name)
Definition scene.cc:2200
static void shader_preview_startjob(void *customdata, bool *stop, bool *do_update)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static void switch_preview_collection_visibility(ViewLayer *view_layer, const ePreviewType pr_type)
static void icon_preview_startjob(void *customdata, bool *stop, bool *do_update)
@ DAG_EVAL_VIEWPORT
int BLI_thread_is_main()
Definition threads.cc:184
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
bool ED_check_engine_supports_preview(const Scene *scene)
void BLI_thread_queue_free(ThreadQueue *queue)
Definition threads.cc:638
static void icon_preview_startjob_all_sizes(void *customdata, wmJobWorkerStatus *worker_status)
void WM_global_report(eReportType type, const char *message)
ThreadQueue * BLI_thread_queue_init()
Definition threads.cc:627
void ED_preview_set_visibility(Main *pr_main, Scene *scene, ViewLayer *view_layer, ePreviewType pr_type, ePreviewRenderMethod pr_method)
void ED_preview_online_download_finished(wmWindowManager *wm, StringRef preview_full_filepath)
void BKE_image_pool_free(ImagePool *pool)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
PreviewImage * BKE_previewimg_id_get(const ID *id)
void ED_preview_free_dbase()
RenderEngineType * RE_engines_find(const char *idname)
@ BLO_READ_SKIP_NONE
void BKE_main_free(Main *bmain)
Definition main.cc:202
struct World * BKE_world_add(struct Main *bmain, const char *name)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void ED_previews_tag_dirty_by_id(const Main &bmain, const ID &id)
void RE_InitState(Render *re, Render *source, const RenderData *rd, ListBaseT< ViewLayer > *, ViewLayer *single_layer, int winx, int winy, const rcti *disprect)
void WM_jobs_kill_type(wmWindowManager *wm, const void *owner, int job_type)
Definition wm_jobs.cc:642
bool BKE_previewimg_is_rendering(const PreviewImage *prv, int size)
struct PoseBackup * BKE_pose_backup_create_all_bones(Span< Object * > objects, const struct bAction *action) ATTR_WARN_UNUSED_RESULT
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
ImBuf * IMB_thumb_manage(const char *file_or_lib_path, ThumbSize size, ThumbSource source, const ThumbCancellationToken *cancel_token=nullptr)
Definition thumbs.cc:601
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
void IMB_byte_from_float(ImBuf *ibuf)
void RE_display_update_cb(Render *re, void *handle, void(*f)(void *handle, RenderResult *rr))
void BKE_pose_backup_free(struct PoseBackup *pbd)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
bool ED_view3d_camera_to_view_selected_with_set_clipping(Main *bmain, Depsgraph *depsgraph, const Scene *scene, Object *camera_ob)
std::optional< StringRefNull > BKE_previewimg_deferred_filepath_get(const PreviewImage *prv)
static bool render_engine_supports_ray_visibility(const Scene *sce)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Scene * DEG_get_input_scene(const Depsgraph *graph)
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
static void scene_preview_render(const PreviewImage *prv_img, IconPreview *preview, const eIconSizes icon_size, ReportList *reports)
static World * preview_get_world(Main *pr_main, const Scene *sce, const ID_Type id_type, const ePreviewRenderMethod pr_method)
BlendFileData * BLO_read_from_memory(const void *mem, int memsize, eBLOReadSkip skip_flags, ReportList *reports)
void BKE_color_managed_view_settings_free(ColorManagedViewSettings *settings)
Scene * CTX_data_scene(const bContext *C)
Depsgraph * BKE_scene_ensure_depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer)
Definition scene.cc:3644
MINLINE void copy_v4_v4(float r[4], const float a[4])
void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
ImBuf * RE_RenderViewEnsureImBuf(const RenderResult *render_result, RenderView *render_view)
void BKE_libblock_free_datablock(ID *id, int flag) ATTR_NONNULL()
void RE_AcquireResultImageViews(Render *re, RenderResult *rr)
static PoseBackup * action_preview_render_prepare(IconPreview *preview)
@ SCE_DISPLAY_AA_SAMPLES_8
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
static Scene * object_preview_scene_create(const ObjectPreviewData *preview_data, Depsgraph **r_depsgraph)
ImBuf * ED_view3d_draw_offscreen_imbuf_simple(Depsgraph *depsgraph, Scene *scene, View3DShading *shading_override, eDrawType drawtype, Object *camera, int width, int height, ImBufFlags imbuf_flags, eV3DOffscreenDrawFlag draw_flags, int alpha_mode, const char *viewname, GPUOffScreen *ofs, GPUViewport *viewport, char err_out[256])
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
void ED_preview_online_download_requested(StringRef preview_full_filepath)
ImBuf * IMB_scale_into_new(const ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:531
uint64_t BLI_thread_queue_push(ThreadQueue *queue, void *work, ThreadQueueWorkPriority priority)
Definition threads.cc:648
void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, uiPreview *ui_preview, rcti *rect)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
ViewLayer * BKE_view_layer_default_render(const Scene *scene)
SpaceProperties * CTX_wm_space_properties(const bContext *C)
void RE_ReleaseResultImageViews(Render *re, RenderResult *rr)
static void shader_preview_texture(ShaderPreview *sp, Tex *tex, Scene *sce, Render *re)
void BKE_imageuser_default(ImageUser *iuser)
static Main * load_main_from_memory(const void *blend, int blend_size)
Object * CTX_data_active_object(const bContext *C)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
static Scene * preview_get_scene(Main *pr_main)
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
void mat3_to_quat(float q[4], const float mat[3][3])
void ED_preview_shader_job(const bContext *C, const void *owner, ID *id, ID *parent, MTex *slot, int sizex, int sizey, ePreviewRenderMethod method)
void ED_preview_icon_render(const bContext *C, Scene *scene, PreviewImage *prv_img, ID *id, enum eIconSizes icon_size)
void ED_preview_ensure_dbase(bool with_gpencil)
ePreviewRenderMethod
Definition ED_render.hh:72
@ PR_ICON_DEFERRED
Definition ED_render.hh:78
@ PR_BUTS_RENDER
Definition ED_render.hh:74
@ PR_ICON_RENDER
Definition ED_render.hh:76
void ED_preview_icon_job(const bContext *C, PreviewImage *prv_img, ID *id, enum eIconSizes icon_size, bool delay)
bool WM_jobs_is_running(const wmJob *wm_job)
Definition wm_jobs.cc:358
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void IMB_premultiply_alpha(ImBuf *ibuf)
Definition filter.cc:382
static const char * preview_floor_material_name(const Scene *scene, const ePreviewRenderMethod pr_method)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
static void preview_id_copy_free(ID *id)
void * WM_jobs_customdata_from_type(wmWindowManager *wm, const void *owner, int job_type)
Definition wm_jobs.cc:347
bool BKE_layer_collection_sync(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< uiPreview > ui_previews
char name[258]
Definition DNA_ID.h:433
bool render_size[NUM_ICON_SIZES]
bool cancelled[NUM_ICON_SIZES]
const uint8_t * byte_data() const
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79
void assign_float_data(float *data)
struct Scene * scene
struct Collection * collection
ListBaseT< LayerCollection > layer_collections
struct LayerCollection * next
char filepath[1024]
Definition BKE_main.hh:267
bool need_preview_render_restart
Definition BKE_main.hh:347
ListBaseT< Light > lights
Definition BKE_main.hh:396
ListBaseT< World > worlds
Definition BKE_main.hh:399
ListBaseT< Material > materials
Definition BKE_main.hh:392
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
ListBaseT< Tex > textures
Definition BKE_main.hh:393
ListBaseT< bScreen > screens
Definition BKE_main.hh:400
eObject_TransFlag transflag
struct Collection * instance_collection
unsigned int w[2]
Definition DNA_ID.h:645
bke::PreviewImageRuntime * runtime
Definition DNA_ID.h:651
unsigned int * rect[2]
Definition DNA_ID.h:649
unsigned int h[2]
Definition DNA_ID.h:646
ListBaseT< RenderView > views
struct ImBuf * ibuf
Definition RE_pipeline.h:54
eSceneDisplay_AA render_aa
ListBaseT< ViewLayer > view_layers
struct World * world
struct SceneDisplay display
struct Collection * master_collection
ColorManagedViewSettings view_settings
struct Object * camera
ColorManagedDisplaySettings display_settings
struct RenderData r
ListBaseT< ARegion > regionbase
ePreviewRenderMethod pr_method
struct bNodeTree * nodetree
eView3DShading_Flag flag
eViewLayerEEVEEPassType render_passes
ListBaseT< LayerCollection > layer_collections
struct ViewLayerEEVEE eevee
ListBaseT< struct ScrArea > areabase
std::unique_ptr< PreviewDeferredLoadingData > deferred_loading_data
unsigned int id_session_uid