3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) Blender Foundation.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/render/render_preview.c
45 #include "MEM_guardedalloc.h"
47 #include "BLO_readfile.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_threads.h"
52 #include "BLI_utildefines.h"
54 #include "DNA_world_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_material_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_lamp_types.h"
60 #include "DNA_space_types.h"
61 #include "DNA_view3d_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_brush_types.h"
64 #include "DNA_screen_types.h"
66 #include "BKE_brush.h"
67 #include "BKE_context.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
70 #include "BKE_idprop.h"
71 #include "BKE_image.h"
72 #include "BKE_icons.h"
74 #include "BKE_library.h"
76 #include "BKE_material.h"
78 #include "BKE_object.h"
79 #include "BKE_texture.h"
80 #include "BKE_world.h"
82 #include "IMB_imbuf.h"
83 #include "IMB_imbuf_types.h"
86 #include "BIF_glutil.h"
90 #include "RE_pipeline.h"
96 #include "ED_render.h"
97 #include "ED_view3d.h"
99 #include "UI_interface.h"
101 #include "render_intern.h"
103 ImBuf* get_brush_icon(Brush *brush)
105 static const int flags = IB_rect|IB_multilayer|IB_metadata;
110 if (!(brush->icon_imbuf)) {
111 if (brush->flag & BRUSH_CUSTOM_ICON) {
113 if (brush->icon_filepath[0]) {
114 // first use the path directly to try and load the file
116 BLI_strncpy(path, brush->icon_filepath, sizeof(brush->icon_filepath));
117 BLI_path_abs(path, G.main->name);
119 brush->icon_imbuf= IMB_loadiffname(path, flags);
121 // otherwise lets try to find it in other directories
122 if (!(brush->icon_imbuf)) {
123 folder= BLI_get_folder(BLENDER_DATAFILES, "brushicons");
125 BLI_make_file_string(G.main->name, path, folder, brush->icon_filepath);
128 brush->icon_imbuf= IMB_loadiffname(path, flags);
131 if (brush->icon_imbuf)
132 BKE_icon_changed(BKE_icon_getid(&brush->id));
137 if (!(brush->icon_imbuf))
138 brush->id.icon_id = 0;
140 return brush->icon_imbuf;
143 typedef struct ShaderPreview {
146 short *stop, *do_update;
153 /* datablocks with nodes need full copy during preview render, glsl uses it too */
159 float col[4]; /* active object color */
162 unsigned int *pr_rect;
167 /* *************************** Preview for buttons *********************** */
169 static Main *pr_main= NULL;
171 void ED_preview_init_dbase(void)
173 #ifndef WITH_HEADLESS
175 extern int datatoc_preview_blend_size;
176 extern char datatoc_preview_blend[];
177 const int fileflags= G.fileflags;
179 G.fileflags |= G_FILE_NO_UI;
180 bfd= BLO_read_from_memory(datatoc_preview_blend, datatoc_preview_blend_size, NULL);
186 G.fileflags= fileflags;
190 void ED_preview_free_dbase(void)
196 static int preview_mat_has_sss(Material *mat, bNodeTree *ntree)
199 if(mat->sss_flag & MA_DIFF_SSS)
202 if( preview_mat_has_sss(NULL, mat->nodetree))
207 for(node= ntree->nodes.first; node; node= node->next) {
208 if(node->type==NODE_GROUP && node->id) {
209 if( preview_mat_has_sss(NULL, (bNodeTree *)node->id))
212 else if(node->id && ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
213 mat= (Material *)node->id;
214 if(mat->sss_flag & MA_DIFF_SSS)
222 /* call this with a pointer to initialize preview scene */
223 /* call this with NULL to restore assigned ID pointers in preview scene */
224 static Scene *preview_prepare_scene(Scene *scene, ID *id, int id_type, ShaderPreview *sp)
229 if(pr_main==NULL) return NULL;
231 sce= pr_main->scene.first;
234 /* this flag tells render to not execute depsgraph or ipos etc */
235 sce->r.scemode |= R_PREVIEWBUTS;
236 /* set world always back, is used now */
237 sce->world= pr_main->world.first;
238 /* now: exposure copy */
240 sce->world->exp= scene->world->exp;
241 sce->world->range= scene->world->range;
244 sce->r.color_mgt_flag = scene->r.color_mgt_flag;
246 /* prevent overhead for small renders and icons (32) */
247 if(id && sp->sizex < 40)
248 sce->r.xparts= sce->r.yparts= 1;
250 sce->r.xparts= sce->r.yparts= 4;
252 /* exception: don't color manage texture previews or icons */
253 if((id && sp->pr_method==PR_ICON_RENDER) || id_type == ID_TE)
254 sce->r.color_mgt_flag &= ~R_COLOR_MANAGEMENT;
256 if((id && sp->pr_method==PR_ICON_RENDER) && id_type != ID_WO)
257 sce->r.alphamode= R_ALPHAPREMUL;
259 sce->r.alphamode= R_ADDSKY;
261 sce->r.cfra= scene->r.cfra;
262 BLI_strncpy(sce->r.engine, scene->r.engine, sizeof(sce->r.engine));
265 Material *mat= NULL, *origmat= (Material *)id;
269 mat= localize_material(origmat);
271 BLI_addtail(&pr_main->mat, mat);
273 init_render_material(mat, 0, NULL); /* call that retrieves mode_l */
274 end_render_material(mat);
276 /* un-useful option */
277 if(sp->pr_method==PR_ICON_RENDER)
278 mat->shade_flag &= ~MA_OBCOLOR;
280 /* turn on raytracing if needed */
281 if(mat->mode_l & MA_RAYMIRROR)
282 sce->r.mode |= R_RAYTRACE;
283 if(mat->material_type == MA_TYPE_VOLUME)
284 sce->r.mode |= R_RAYTRACE;
285 if((mat->mode_l & MA_RAYTRANSP) && (mat->mode_l & MA_TRANSP))
286 sce->r.mode |= R_RAYTRACE;
287 if(preview_mat_has_sss(mat, NULL))
288 sce->r.mode |= R_SSS;
290 /* turn off fake shadows if needed */
291 /* this only works in a specific case where the preview.blend contains
292 * an object starting with 'c' which has a material linked to it (not the obdata)
293 * and that material has a fake shadow texture in the active texture slot */
294 for(base= sce->base.first; base; base= base->next) {
295 if(base->object->id.name[2]=='c') {
296 Material *shadmat= give_current_material(base->object, base->object->actcol);
298 if (mat->mode & MA_SHADBUF) shadmat->septex = 0;
299 else shadmat->septex |= 1;
304 /* turn off bounce lights for volume,
305 * doesn't make much visual difference and slows it down too */
306 if(mat->material_type == MA_TYPE_VOLUME) {
307 for(base= sce->base.first; base; base= base->next) {
308 if(base->object->type == OB_LAMP) {
309 /* if doesn't match 'Lamp.002' --> main key light */
310 if( strcmp(base->object->id.name+2, "Lamp.002") != 0 ) {
311 base->object->restrictflag |= OB_RESTRICT_RENDER;
318 if(sp->pr_method==PR_ICON_RENDER) {
319 if (mat->material_type == MA_TYPE_HALO) {
320 sce->lay= 1<<MA_FLAT;
323 sce->lay= 1<<MA_SPHERE_A;
327 sce->lay= 1<<mat->pr_type;
328 if(mat->nodetree && sp->pr_method==PR_NODE_RENDER) {
329 /* two previews, they get copied by wmJob */
330 ntreeInitPreview(mat->nodetree, sp->sizex, sp->sizey);
331 ntreeInitPreview(origmat->nodetree, sp->sizex, sp->sizey);
336 sce->r.mode &= ~(R_OSA|R_RAYTRACE|R_SSS);
340 for(base= sce->base.first; base; base= base->next) {
341 if(base->object->id.name[2]=='p') {
342 /* copy over object color, in case material uses it */
343 copy_v4_v4(base->object->col, sp->col);
345 if(OB_TYPE_SUPPORT_MATERIAL(base->object->type)) {
346 /* don't use assign_material, it changed mat->id.us, which shows in the UI */
347 Material ***matar= give_matarar(base->object);
348 int actcol= MAX2(base->object->actcol > 0, 1) - 1;
350 if(matar && actcol < base->object->totcol)
351 (*matar)[actcol]= mat;
352 } else if (base->object->type == OB_LAMP) {
353 base->object->restrictflag &= ~OB_RESTRICT_RENDER;
358 else if(id_type==ID_TE) {
359 Tex *tex= NULL, *origtex= (Tex *)id;
362 tex= localize_texture(origtex);
364 BLI_addtail(&pr_main->tex, tex);
366 sce->lay= 1<<MA_TEXTURE;
368 for(base= sce->base.first; base; base= base->next) {
369 if(base->object->id.name[2]=='t') {
370 Material *mat= give_current_material(base->object, base->object->actcol);
371 if(mat && mat->mtex[0]) {
372 mat->mtex[0]->tex= tex;
375 mat->mtex[0]->which_output = sp->slot->which_output;
377 /* show alpha in this case */
378 if(tex==NULL || (tex->flag & TEX_PRV_ALPHA)) {
379 mat->mtex[0]->mapto |= MAP_ALPHA;
383 mat->mtex[0]->mapto &= ~MAP_ALPHA;
390 if(tex && tex->nodetree && sp->pr_method==PR_NODE_RENDER) {
391 /* two previews, they get copied by wmJob */
392 ntreeInitPreview(origtex->nodetree, sp->sizex, sp->sizey);
393 ntreeInitPreview(tex->nodetree, sp->sizex, sp->sizey);
396 else if(id_type==ID_LA) {
397 Lamp *la= NULL, *origla= (Lamp *)id;
401 la= localize_lamp(origla);
403 BLI_addtail(&pr_main->lamp, la);
406 if(la && la->type==LA_SUN && (la->sun_effect_type & LA_SUN_EFFECT_SKY)) {
407 sce->lay= 1<<MA_ATMOS;
408 sce->world= scene->world;
409 sce->camera= (Object *)BLI_findstring(&pr_main->object, "CameraAtmo", offsetof(ID, name)+2);
412 sce->lay= 1<<MA_LAMP;
414 sce->camera= (Object *)BLI_findstring(&pr_main->object, "Camera", offsetof(ID, name)+2);
416 sce->r.mode &= ~R_SHADOW;
418 for(base= sce->base.first; base; base= base->next) {
419 if(base->object->id.name[2]=='p') {
420 if(base->object->type==OB_LAMP)
421 base->object->data= la;
425 if(la && la->nodetree && sp->pr_method==PR_NODE_RENDER) {
426 /* two previews, they get copied by wmJob */
427 ntreeInitPreview(origla->nodetree, sp->sizex, sp->sizey);
428 ntreeInitPreview(la->nodetree, sp->sizex, sp->sizey);
431 else if(id_type==ID_WO) {
432 World *wrld= NULL, *origwrld= (World *)id;
435 wrld= localize_world(origwrld);
437 BLI_addtail(&pr_main->world, wrld);
443 if(wrld && wrld->nodetree && sp->pr_method==PR_NODE_RENDER) {
444 /* two previews, they get copied by wmJob */
445 ntreeInitPreview(wrld->nodetree, sp->sizex, sp->sizey);
446 ntreeInitPreview(origwrld->nodetree, sp->sizex, sp->sizey);
456 /* new UI convention: draw is in pixel space already. */
457 /* uses ROUNDBOX button in block to get the rect */
458 static int ed_preview_draw_rect(ScrArea *sa, Scene *sce, ID *id, int split, int first, rcti *rect, rcti *newrect)
463 int do_gamma_correct=0, do_predivide=0;
464 int offx=0, newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
466 if (id && GS(id->name) != ID_TE) {
467 /* exception: don't color manage texture previews - show the raw values */
469 do_gamma_correct = sce->r.color_mgt_flag & R_COLOR_MANAGEMENT;
470 do_predivide = sce->r.color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE;
474 if(!split || first) sprintf(name, "Preview %p", (void *)sa);
475 else sprintf(name, "SecondPreview %p", (void *)sa);
488 re= RE_GetRender(name);
489 RE_AcquireResultImage(re, &rres);
493 if(ABS(rres.rectx-newx)<2 && ABS(rres.recty-newy)<2) {
495 newrect->xmax= MAX2(newrect->xmax, rect->xmin + rres.rectx + offx);
496 newrect->ymax= MAX2(newrect->ymax, rect->ymin + rres.recty);
498 if(rres.rectx && rres.recty) {
499 /* temporary conversion to byte for drawing */
500 float fx= rect->xmin + offx;
501 float fy= rect->ymin;
502 int profile_from= (do_gamma_correct)? IB_PROFILE_LINEAR_RGB: IB_PROFILE_SRGB;
504 unsigned char *rect_byte;
506 rect_byte= MEM_mallocN(rres.rectx*rres.recty*sizeof(int), "ed_preview_draw_rect");
508 IMB_buffer_byte_from_float(rect_byte, rres.rectf,
509 4, dither, IB_PROFILE_SRGB, profile_from, do_predivide,
510 rres.rectx, rres.recty, rres.rectx, rres.rectx);
512 glaDrawPixelsSafe(fx, fy, rres.rectx, rres.recty, rres.rectx, GL_RGBA, GL_UNSIGNED_BYTE, rect_byte);
514 MEM_freeN(rect_byte);
517 RE_ReleaseResultImage(re);
522 RE_ReleaseResultImage(re);
526 void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, rcti *rect)
529 ScrArea *sa= CTX_wm_area(C);
530 Scene *sce = CTX_data_scene(C);
532 ID *parent= (ID *)parentp;
533 MTex *slot= (MTex *)slotp;
534 SpaceButs *sbuts= sa->spacedata.first;
537 int newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
539 newrect.xmin= rect->xmin;
540 newrect.xmax= rect->xmin;
541 newrect.ymin= rect->ymin;
542 newrect.ymax= rect->ymin;
545 ok = ed_preview_draw_rect(sa, sce, id, 1, 1, rect, &newrect);
546 ok &= ed_preview_draw_rect(sa, sce, parent, 1, 0, rect, &newrect);
549 ok = ed_preview_draw_rect(sa, sce, id, 0, 0, rect, &newrect);
554 /* check for spacetype... */
555 if(sbuts->spacetype==SPACE_BUTS && sbuts->preview) {
561 ED_preview_shader_job(C, sa, id, parent, slot, newx, newy, PR_BUTS_RENDER);
566 /* **************************** new shader preview system ****************** */
568 /* inside thread, called by renderer, sets job update value */
569 static void shader_preview_draw(void *spv, RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect))
571 ShaderPreview *sp= spv;
576 /* called by renderer, checks job value */
577 static int shader_preview_break(void *spv)
579 ShaderPreview *sp= spv;
584 /* outside thread, called before redraw notifiers, it moves finished preview over */
585 static void shader_preview_updatejob(void *spv)
587 ShaderPreview *sp= spv;
590 if(sp->pr_method==PR_NODE_RENDER) {
591 if( GS(sp->id->name) == ID_MA) {
592 Material *mat= (Material *)sp->id;
594 if(sp->matcopy && mat->nodetree && sp->matcopy->nodetree)
595 ntreeLocalSync(sp->matcopy->nodetree, mat->nodetree);
597 else if( GS(sp->id->name) == ID_TE) {
598 Tex *tex= (Tex *)sp->id;
600 if(sp->texcopy && tex->nodetree && sp->texcopy->nodetree)
601 ntreeLocalSync(sp->texcopy->nodetree, tex->nodetree);
603 else if( GS(sp->id->name) == ID_WO) {
604 World *wrld= (World *)sp->id;
606 if(sp->worldcopy && wrld->nodetree && sp->worldcopy->nodetree)
607 ntreeLocalSync(sp->worldcopy->nodetree, wrld->nodetree);
609 else if( GS(sp->id->name) == ID_LA) {
610 Lamp *la= (Lamp *)sp->id;
612 if(sp->lampcopy && la->nodetree && sp->lampcopy->nodetree)
613 ntreeLocalSync(sp->lampcopy->nodetree, la->nodetree);
619 static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
624 short idtype= GS(id->name);
628 /* get the stuff from the builtin preview dbase */
629 sce= preview_prepare_scene(sp->scene, id, idtype, sp); // XXX sizex
630 if(sce==NULL) return;
632 if(!split || first) sprintf(name, "Preview %p", sp->owner);
633 else sprintf(name, "SecondPreview %p", sp->owner);
634 re= RE_GetRender(name);
636 /* full refreshed render from first tile */
638 re= RE_NewRender(name);
640 /* sce->r gets copied in RE_InitState! */
641 sce->r.scemode &= ~(R_MATNODE_PREVIEW|R_TEXNODE_PREVIEW);
642 sce->r.scemode &= ~R_NO_IMAGE_LOAD;
644 if(sp->pr_method==PR_ICON_RENDER) {
645 sce->r.scemode |= R_NO_IMAGE_LOAD;
646 sce->r.mode |= R_OSA;
648 else if(sp->pr_method==PR_NODE_RENDER) {
649 if(idtype == ID_MA) sce->r.scemode |= R_MATNODE_PREVIEW;
650 else if(idtype == ID_TE) sce->r.scemode |= R_TEXNODE_PREVIEW;
651 sce->r.mode &= ~R_OSA;
653 else { /* PR_BUTS_RENDER */
654 sce->r.mode |= R_OSA;
657 /* in case of split preview, use border render */
659 if(first) sizex= sp->sizex/2;
660 else sizex= sp->sizex - sp->sizex/2;
662 else sizex= sp->sizex;
664 /* allocates or re-uses render result */
666 sce->r.ysch= sp->sizey;
669 /* callbacs are cleared on GetRender() */
670 if(ELEM(sp->pr_method, PR_BUTS_RENDER, PR_NODE_RENDER)) {
671 RE_display_draw_cb(re, sp, shader_preview_draw);
673 /* set this for all previews, default is react to G.afbreek still */
674 RE_test_break_cb(re, sp, shader_preview_break);
677 oldlens= ((Camera *)sce->camera->data)->lens;
678 if(sizex > sp->sizey)
679 ((Camera *)sce->camera->data)->lens *= (float)sp->sizey/(float)sizex;
681 /* entire cycle for render engine */
682 RE_PreviewRender(re, pr_main, sce);
684 ((Camera *)sce->camera->data)->lens= oldlens;
687 if(sp->pr_method==PR_ICON_RENDER) {
688 // char *rct= (char *)(sp->pr_rect + 32*16 + 16);
691 RE_ResultGet32(re, sp->pr_rect);
696 // ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
697 //RE_ResultGet32(re, ri->rect);
700 /* unassign the pointers, reset vars */
701 preview_prepare_scene(sp->scene, NULL, GS(id->name), sp);
703 /* XXX bad exception, end-exec is not being called in render, because it uses local main */
704 // if(idtype == ID_TE) {
705 // Tex *tex= (Tex *)id;
706 // if(tex->use_nodes && tex->nodetree)
707 // ntreeEndExecTree(tex->nodetree);
712 /* runs inside thread for material and icons */
713 static void shader_preview_startjob(void *customdata, short *stop, short *do_update)
715 ShaderPreview *sp= customdata;
718 sp->do_update= do_update;
721 shader_preview_render(sp, sp->id, 1, 1);
722 shader_preview_render(sp, sp->parent, 1, 0);
725 shader_preview_render(sp, sp->id, 0, 0);
730 static void shader_preview_free(void *customdata)
732 ShaderPreview *sp= customdata;
735 struct IDProperty *properties;
739 shader_preview_updatejob(sp);
741 /* get rid of copied material */
742 BLI_remlink(&pr_main->mat, sp->matcopy);
744 /* free_material decrements texture, prevent this. hack alert! */
745 for(a=0; a<MAX_MTEX; a++) {
746 MTex *mtex= sp->matcopy->mtex[a];
747 if(mtex && mtex->tex) mtex->tex= NULL;
750 free_material(sp->matcopy);
752 properties= IDP_GetProperties((ID *)sp->matcopy, FALSE);
754 IDP_FreeProperty(properties);
755 MEM_freeN(properties);
757 MEM_freeN(sp->matcopy);
760 struct IDProperty *properties;
762 shader_preview_updatejob(sp);
764 /* get rid of copied texture */
765 BLI_remlink(&pr_main->tex, sp->texcopy);
766 free_texture(sp->texcopy);
768 properties= IDP_GetProperties((ID *)sp->texcopy, FALSE);
770 IDP_FreeProperty(properties);
771 MEM_freeN(properties);
773 MEM_freeN(sp->texcopy);
776 struct IDProperty *properties;
778 shader_preview_updatejob(sp);
780 /* get rid of copied world */
781 BLI_remlink(&pr_main->world, sp->worldcopy);
782 free_world(sp->worldcopy);
784 properties= IDP_GetProperties((ID *)sp->worldcopy, FALSE);
786 IDP_FreeProperty(properties);
787 MEM_freeN(properties);
789 MEM_freeN(sp->worldcopy);
792 struct IDProperty *properties;
794 shader_preview_updatejob(sp);
796 /* get rid of copied lamp */
797 BLI_remlink(&pr_main->lamp, sp->lampcopy);
798 free_lamp(sp->lampcopy);
800 properties= IDP_GetProperties((ID *)sp->lampcopy, FALSE);
802 IDP_FreeProperty(properties);
803 MEM_freeN(properties);
805 MEM_freeN(sp->lampcopy);
811 /* ************************* icon preview ********************** */
813 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
816 unsigned int *drect, *srect;
817 float scaledx, scaledy;
818 short ex, ey, dx, dy;
821 if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
824 /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
825 ima = IMB_dupImBuf(ibuf);
830 if (ima->x > ima->y) {
832 scaledy = ( (float)ima->y/(float)ima->x )*(float)w;
835 scaledx = ( (float)ima->x/(float)ima->y )*(float)h;
845 IMB_scalefastImBuf(ima, ex, ey);
847 /* if needed, convert to 32 bits */
849 IMB_rect_from_float(ima);
856 memcpy(drect,srect, ex * sizeof(int));
864 static void set_alpha(char *cp, int sizex, int sizey, char alpha)
866 int a, size= sizex*sizey;
868 for(a=0; a<size; a++, cp+=4)
872 static void icon_preview_startjob(void *customdata, short *stop, short *do_update)
874 ShaderPreview *sp= customdata;
876 short idtype= GS(id->name);
878 if(idtype == ID_IM) {
879 Image *ima= (Image*)id;
881 ImageUser iuser= {NULL};
883 /* ima->ok is zero when Image cannot load */
884 if(ima==NULL || ima->ok==0)
887 /* setup dummy image user */
888 iuser.ok= iuser.framenr= 1;
889 iuser.scene= sp->scene;
891 /* elubie: this needs to be changed: here image is always loaded if not
892 already there. Very expensive for large images. Need to find a way to
893 only get existing ibuf */
894 ibuf = BKE_image_get_ibuf(ima, &iuser);
895 if(ibuf==NULL || ibuf->rect==NULL)
898 icon_copy_rect(ibuf, sp->sizex, sp->sizey, sp->pr_rect);
902 else if(idtype == ID_BR) {
903 Brush *br= (Brush*)id;
905 br->icon_imbuf= get_brush_icon(br);
907 memset(sp->pr_rect, 0x888888, sp->sizex*sp->sizey*sizeof(unsigned int));
909 if(!(br->icon_imbuf) || !(br->icon_imbuf->rect))
912 icon_copy_rect(br->icon_imbuf, sp->sizex, sp->sizey, sp->pr_rect);
917 /* re-use shader job */
918 shader_preview_startjob(customdata, stop, do_update);
920 /* world is rendered with alpha=0, so it wasn't displayed
921 this could be render option for sky to, for later */
922 if(idtype == ID_WO) {
923 set_alpha((char*)sp->pr_rect, sp->sizex, sp->sizey, 255);
925 else if(idtype == ID_MA) {
926 Material* ma = (Material*)id;
928 if(ma->material_type == MA_TYPE_HALO)
929 set_alpha((char*)sp->pr_rect, sp->sizex, sp->sizey, 255);
934 /* use same function for icon & shader, so the job manager
935 does not run two of them at the same time. */
937 static void common_preview_startjob(void *customdata, short *stop, short *do_update, float *UNUSED(progress))
939 ShaderPreview *sp= customdata;
941 if(sp->pr_method == PR_ICON_RENDER)
942 icon_preview_startjob(customdata, stop, do_update);
944 shader_preview_startjob(customdata, stop, do_update);
947 static void common_preview_endjob(void *customdata)
949 ShaderPreview *sp= customdata;
951 if(sp->id && GS(sp->id->name) == ID_BR)
952 WM_main_add_notifier(NC_BRUSH|NA_EDITED, sp->id);
955 /* exported functions */
957 void ED_preview_icon_job(const bContext *C, void *owner, ID *id, unsigned int *rect, int sizex, int sizey)
962 /* suspended start means it starts after 1 timer step, see WM_jobs_timer below */
963 steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Icon Preview", WM_JOB_EXCL_RENDER|WM_JOB_SUSPEND);
964 sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
966 /* customdata for preview thread */
967 sp->scene= CTX_data_scene(C);
971 sp->pr_method= PR_ICON_RENDER;
976 WM_jobs_customdata(steve, sp, shader_preview_free);
977 WM_jobs_timer(steve, 0.25, NC_MATERIAL, NC_MATERIAL);
978 WM_jobs_callbacks(steve, common_preview_startjob, NULL, NULL, common_preview_endjob);
980 WM_jobs_start(CTX_wm_manager(C), steve);
983 void ED_preview_shader_job(const bContext *C, void *owner, ID *id, ID *parent, MTex *slot, int sizex, int sizey, int method)
985 Object *ob= CTX_data_active_object(C);
989 steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Shader Preview", WM_JOB_EXCL_RENDER);
990 sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
992 /* customdata for preview thread */
993 sp->scene= CTX_data_scene(C);
997 sp->pr_method= method;
1001 if(ob && ob->totcol) copy_v4_v4(sp->col, ob->col);
1002 else sp->col[0]= sp->col[1]= sp->col[2]= sp->col[3]= 1.0f;
1005 WM_jobs_customdata(steve, sp, shader_preview_free);
1006 WM_jobs_timer(steve, 0.1, NC_MATERIAL, NC_MATERIAL);
1007 WM_jobs_callbacks(steve, common_preview_startjob, NULL, shader_preview_updatejob, NULL);
1009 WM_jobs_start(CTX_wm_manager(C), steve);
1012 void ED_preview_kill_jobs(const struct bContext *C)
1014 wmWindowManager *wm= CTX_wm_manager(C);
1016 WM_jobs_kill(wm, NULL, common_preview_startjob);