replace inline string searches with BLI_findstring(), strcmp(..., ""), with char...
[blender-staging.git] / source / blender / editors / render / render_preview.c
1 /* 
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/render/render_preview.c
31  *  \ingroup edrend
32  */
33
34
35 /* global includes */
36
37 #include <stdlib.h>
38 #include <math.h>
39 #include <string.h>
40
41 #ifndef WIN32
42 #include <unistd.h>
43 #else
44 #include <io.h>
45 #endif   
46 #include "MEM_guardedalloc.h"
47
48 #include "BLO_readfile.h" 
49
50 #include "BLI_math.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_threads.h"
53 #include "BLI_utildefines.h"
54
55 #include "DNA_world_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_node_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_lamp_types.h"
61 #include "DNA_space_types.h"
62 #include "DNA_view3d_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_brush_types.h"
65 #include "DNA_screen_types.h"
66
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"
73 #include "BKE_library.h"
74 #include "BKE_main.h"
75 #include "BKE_material.h"
76 #include "BKE_node.h"
77 #include "BKE_texture.h"
78
79 #include "IMB_imbuf.h"
80 #include "IMB_imbuf_types.h"
81
82 #include "BIF_gl.h"
83 #include "BIF_glutil.h"
84
85 #include "PIL_time.h"
86
87 #include "RE_pipeline.h"
88
89
90 #include "WM_api.h"
91 #include "WM_types.h"
92
93 #include "ED_render.h"
94 #include "ED_view3d.h"
95
96 #include "UI_interface.h"
97
98 #include "render_intern.h"
99
100 #define PR_XMIN         10
101 #define PR_YMIN         5
102 #define PR_XMAX         200
103 #define PR_YMAX         195
104
105 /* XXX */
106 static int qtest(void) {return 0;}
107 /* XXX */
108
109 ImBuf* get_brush_icon(Brush *brush)
110 {
111         static const int flags = IB_rect|IB_multilayer|IB_metadata;
112
113         char path[240];
114         char *folder;
115
116         if (!(brush->icon_imbuf)) {
117                 if (brush->flag & BRUSH_CUSTOM_ICON) {
118
119                         if (brush->icon_filepath[0]) {
120                                 // first use the path directly to try and load the file
121
122                                 BLI_strncpy(path, brush->icon_filepath, sizeof(brush->icon_filepath));
123                                 BLI_path_abs(path, G.main->name);
124
125                                 brush->icon_imbuf= IMB_loadiffname(path, flags);
126
127                                 // otherwise lets try to find it in other directories
128                                 if (!(brush->icon_imbuf)) {
129                                         folder= BLI_get_folder(BLENDER_DATAFILES, "brushicons");
130
131                                         path[0]= 0;
132
133                                         BLI_make_file_string(G.main->name, path, folder, brush->icon_filepath);
134
135                                         if (path[0])
136                                                 brush->icon_imbuf= IMB_loadiffname(path, flags);
137                                 }
138
139                                 if (brush->icon_imbuf)
140                                         BKE_icon_changed(BKE_icon_getid(&brush->id));
141                         }
142                 }
143         }
144
145         if (!(brush->icon_imbuf))
146                 brush->id.icon_id = 0;
147
148         return brush->icon_imbuf;
149 }
150
151 typedef struct ShaderPreview {
152         /* from wmJob */
153         void *owner;
154         short *stop, *do_update;
155         
156         Scene *scene;
157         ID *id;
158         ID *parent;
159         MTex *slot;
160         
161         /* node materials/texture need full copy during preview render, glsl uses it too */
162         Material *matcopy;
163         Tex *texcopy;
164         
165         float col[4];           /* active object color */
166         
167         int sizex, sizey;
168         unsigned int *pr_rect;
169         int pr_method;
170         
171 } ShaderPreview;
172
173
174
175 /* unused now */
176 void draw_tex_crop(Tex *tex)
177 {
178         rcti rct;
179         int ret= 0;
180         
181         if(tex==NULL) return;
182         
183         if(tex->type==TEX_IMAGE) {
184                 if(tex->cropxmin==0.0f) ret++;
185                 if(tex->cropymin==0.0f) ret++;
186                 if(tex->cropxmax==1.0f) ret++;
187                 if(tex->cropymax==1.0f) ret++;
188                 if(ret==4) return;
189                 
190                 rct.xmin= PR_XMIN+2+tex->cropxmin*(PR_XMAX-PR_XMIN-4);
191                 rct.xmax= PR_XMIN+2+tex->cropxmax*(PR_XMAX-PR_XMIN-4);
192                 rct.ymin= PR_YMIN+2+tex->cropymin*(PR_YMAX-PR_YMIN-4);
193                 rct.ymax= PR_YMIN+2+tex->cropymax*(PR_YMAX-PR_YMIN-4);
194
195                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
196
197                 glColor3ub(0, 0, 0);
198                 glRecti(rct.xmin+1,  rct.ymin-1,  rct.xmax+1,  rct.ymax-1); 
199
200                 glColor3ub(255, 255, 255);
201                 glRecti(rct.xmin,  rct.ymin,  rct.xmax,  rct.ymax);
202
203                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);                      
204         }
205         
206 }
207
208 /* temporal abuse; if id_code is -1 it only does texture.... solve! */
209 void BIF_preview_changed(short UNUSED(id_code))
210 {
211 #if 0   
212         ScrArea *sa;
213         
214         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
215                 if(sa->spacetype==SPACE_BUTS) {
216                         SpaceButs *sbuts= sa->spacedata.first;
217                         if(sbuts->mainb==CONTEXT_SHADING) {
218                                 int tab= sbuts->tab[CONTEXT_SHADING];
219                                 if(tab==TAB_SHADING_MAT && (id_code==ID_MA || id_code==ID_TE)) {
220                                         if (sbuts->ri) sbuts->ri->curtile= 0;
221                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
222                                 }
223                                 else if(tab==TAB_SHADING_TEX && (id_code==ID_TE || id_code==-1)) {
224                                         if (sbuts->ri) sbuts->ri->curtile= 0;
225                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
226                                 }
227                                 else if(tab==TAB_SHADING_LAMP && (id_code==ID_LA || id_code==ID_TE)) {
228                                         if (sbuts->ri) sbuts->ri->curtile= 0;
229                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
230                                 }
231                                 else if(tab==TAB_SHADING_WORLD && (id_code==ID_WO || id_code==ID_TE)) {
232                                         if (sbuts->ri) sbuts->ri->curtile= 0;
233                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
234                                 }
235                         }
236                         else if (sbuts->ri) 
237                                 sbuts->ri->curtile= 0;  /* ensure changes always result in re-render when context is restored */
238                 }
239                 else if(sa->spacetype==SPACE_NODE) {
240                         SpaceNode *snode= sa->spacedata.first;
241                         if(snode->treetype==NTREE_SHADER && (id_code==ID_MA || id_code==ID_TE)) {
242                                 snode_tag_dirty(snode);
243                         }
244                 }
245                 else if(sa->spacetype==SPACE_VIEW3D) {
246                         View3D *vd= sa->spacedata.first;
247                         /* if is has a renderinfo, we consider that reason for signalling */
248                         if (vd->ri) {
249                                 vd->ri->curtile= 0;
250                                 addafterqueue(sa->win, RENDERPREVIEW, 1);
251                         }
252                 }
253         }
254
255         if(ELEM4(id_code, ID_MA, ID_TE, ID_LA, ID_WO)) {
256                 Object *ob;
257                 Material *ma;
258
259                 if(id_code == ID_WO) {
260                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
261                                 if(ma->gpumaterial.first) {
262                                         GPU_material_free(ma);
263                                 }
264                         }
265                 }
266                 else if(id_code == ID_LA) {
267                         for(ob=G.main->object.first; ob; ob=ob->id.next) {
268                                 if(ob->gpulamp.first) {
269                                         GPU_lamp_free(ob);
270                                 }
271                         }
272
273                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
274                                 if(ma->gpumaterial.first) {
275                                         GPU_material_free(ma);
276                                 }
277                         }
278                 } else if(OBACT) {
279                         Object *ob = OBACT;
280
281                         ma= give_current_material(ob, ob->actcol);
282                         if(ma && ma->gpumaterial.first) {
283                                 GPU_material_free(ma);
284                         }
285                 }
286         }
287 #endif
288 }
289
290 /* *************************** Preview for buttons *********************** */
291
292 static Main *pr_main= NULL;
293
294 void ED_preview_init_dbase(void)
295 {
296         BlendFileData *bfd;
297         extern int datatoc_preview_blend_size;
298         extern char datatoc_preview_blend[];
299         const int fileflags= G.fileflags;
300         
301         G.fileflags |= G_FILE_NO_UI;
302         bfd= BLO_read_from_memory(datatoc_preview_blend, datatoc_preview_blend_size, NULL);
303         if (bfd) {
304                 pr_main= bfd->main;
305                 
306                 MEM_freeN(bfd);
307         }
308         G.fileflags= fileflags;
309 }
310
311 void ED_preview_free_dbase(void)
312 {
313         if(pr_main)
314                 free_main(pr_main);
315 }
316
317 static int preview_mat_has_sss(Material *mat, bNodeTree *ntree)
318 {
319         if(mat) {
320                 if(mat->sss_flag & MA_DIFF_SSS)
321                         return 1;
322                 if(mat->nodetree)
323                         if( preview_mat_has_sss(NULL, mat->nodetree))
324                                 return 1;
325         }
326         else if(ntree) {
327                 bNode *node;
328                 for(node= ntree->nodes.first; node; node= node->next) {
329                         if(node->type==NODE_GROUP && node->id) {
330                                 if( preview_mat_has_sss(NULL, (bNodeTree *)node->id))
331                                         return 1;
332                         }
333                         else if(node->id && ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
334                                 mat= (Material *)node->id;
335                                 if(mat->sss_flag & MA_DIFF_SSS)
336                                         return 1;
337                         }
338                 }
339         }
340         return 0;
341 }
342
343 /* call this with a pointer to initialize preview scene */
344 /* call this with NULL to restore assigned ID pointers in preview scene */
345 static Scene *preview_prepare_scene(Scene *scene, ID *id, int id_type, ShaderPreview *sp)
346 {
347         Scene *sce;
348         Base *base;
349         
350         if(pr_main==NULL) return NULL;
351         
352         sce= pr_main->scene.first;
353         if(sce) {
354                 
355                 /* this flag tells render to not execute depsgraph or ipos etc */
356                 sce->r.scemode |= R_PREVIEWBUTS;
357                 /* set world always back, is used now */
358                 sce->world= pr_main->world.first;
359                 /* now: exposure copy */
360                 if(scene->world) {
361                         sce->world->exp= scene->world->exp;
362                         sce->world->range= scene->world->range;
363                 }
364                 
365                 sce->r.color_mgt_flag = scene->r.color_mgt_flag;
366                 
367                 /* prevent overhead for small renders and icons (32) */
368                 if(id && sp->sizex < 40)
369                         sce->r.xparts= sce->r.yparts= 1;
370                 else
371                         sce->r.xparts= sce->r.yparts= 4;
372                 
373                 /* exception: don't color manage texture previews or icons */
374                 if((id && sp->pr_method==PR_ICON_RENDER) || id_type == ID_TE)
375                         sce->r.color_mgt_flag &= ~R_COLOR_MANAGEMENT;
376                 
377                 if((id && sp->pr_method==PR_ICON_RENDER) && id_type != ID_WO)
378                         sce->r.alphamode= R_ALPHAPREMUL;
379                 else
380                         sce->r.alphamode= R_ADDSKY;
381
382                 sce->r.cfra= scene->r.cfra;
383                 strcpy(sce->r.engine, scene->r.engine);
384                 
385                 if(id_type==ID_MA) {
386                         Material *mat= NULL, *origmat= (Material *)id;
387                         
388                         if(id) {
389                                 /* work on a copy */
390                                 mat= localize_material(origmat);
391                                 sp->matcopy= mat;
392                                 BLI_addtail(&pr_main->mat, mat);
393                                 
394                                 init_render_material(mat, 0, NULL);             /* call that retrieves mode_l */
395                                 end_render_material(mat);
396                                 
397                                 /* un-useful option */
398                                 if(sp->pr_method==PR_ICON_RENDER)
399                                         mat->shade_flag &= ~MA_OBCOLOR;
400
401                                 /* turn on raytracing if needed */
402                                 if(mat->mode_l & MA_RAYMIRROR)
403                                         sce->r.mode |= R_RAYTRACE;
404                                 if(mat->material_type == MA_TYPE_VOLUME)
405                                         sce->r.mode |= R_RAYTRACE;
406                                 if((mat->mode_l & MA_RAYTRANSP) && (mat->mode_l & MA_TRANSP))
407                                         sce->r.mode |= R_RAYTRACE;
408                                 if(preview_mat_has_sss(mat, NULL))
409                                         sce->r.mode |= R_SSS;
410                                 
411                                 /* turn off fake shadows if needed */
412                                 /* this only works in a specific case where the preview.blend contains
413                                  * an object starting with 'c' which has a material linked to it (not the obdata)
414                                  * and that material has a fake shadow texture in the active texture slot */
415                                 for(base= sce->base.first; base; base= base->next) {
416                                         if(base->object->id.name[2]=='c') {
417                                                 Material *shadmat= give_current_material(base->object, base->object->actcol);
418                                                 if(shadmat) {
419                                                         if (mat->mode & MA_SHADBUF) shadmat->septex = 0;
420                                                         else shadmat->septex |= 1;
421                                                 }
422                                         }
423                                 }
424                                 
425                                 /* turn off bounce lights for volume, 
426                                  * doesn't make much visual difference and slows it down too */
427                                 if(mat->material_type == MA_TYPE_VOLUME) {
428                                         for(base= sce->base.first; base; base= base->next) {
429                                                 if(base->object->type == OB_LAMP) {
430                                                         /* if doesn't match 'Lamp.002' --> main key light */
431                                                         if( strcmp(base->object->id.name+2, "Lamp.002") != 0 ) {
432                                                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
433                                                         }
434                                                 }
435                                         }
436                                 }
437
438                                 
439                                 if(sp->pr_method==PR_ICON_RENDER) {
440                                         if (mat->material_type == MA_TYPE_HALO) {
441                                                 sce->lay= 1<<MA_FLAT;
442                                         } 
443                                         else {
444                                                 sce->lay= 1<<MA_SPHERE_A;
445                                         }
446                                 }
447                                 else {
448                                         sce->lay= 1<<mat->pr_type;
449                                         if(mat->nodetree && sp->pr_method==PR_NODE_RENDER) {
450                                                 /* two previews, they get copied by wmJob */
451                                                 ntreeInitPreview(mat->nodetree, sp->sizex, sp->sizey);
452                                                 ntreeInitPreview(origmat->nodetree, sp->sizex, sp->sizey);
453                                         }
454                                 }
455                         }
456                         else {
457                                 sce->r.mode &= ~(R_OSA|R_RAYTRACE|R_SSS);
458                                 
459                         }
460                         
461                         for(base= sce->base.first; base; base= base->next) {
462                                 if(base->object->id.name[2]=='p') {
463                                         /* copy over object color, in case material uses it */
464                                         copy_v4_v4(base->object->col, sp->col);
465                                         
466                                         if(ELEM4(base->object->type, OB_MESH, OB_CURVE, OB_SURF, OB_MBALL)) {
467                                                 /* don't use assign_material, it changed mat->id.us, which shows in the UI */
468                                                 Material ***matar= give_matarar(base->object);
469                                                 int actcol= MAX2(base->object->actcol > 0, 1) - 1;
470
471                                                 if(matar && actcol < base->object->totcol)
472                                                         (*matar)[actcol]= mat;
473                                         } else if (base->object->type == OB_LAMP) {
474                                                 base->object->restrictflag &= ~OB_RESTRICT_RENDER;
475                                         }
476                                 }
477                         }
478                 }
479                 else if(id_type==ID_TE) {
480                         Tex *tex= NULL, *origtex= (Tex *)id;
481                         
482                         if(origtex) {
483                                 tex= localize_texture(origtex);
484                                 sp->texcopy= tex;
485                                 BLI_addtail(&pr_main->tex, tex);
486                         }                       
487                         sce->lay= 1<<MA_TEXTURE;
488                         
489                         for(base= sce->base.first; base; base= base->next) {
490                                 if(base->object->id.name[2]=='t') {
491                                         Material *mat= give_current_material(base->object, base->object->actcol);
492                                         if(mat && mat->mtex[0]) {
493                                                 mat->mtex[0]->tex= tex;
494                                                 
495                                                 if(tex && sp->slot)
496                                                         mat->mtex[0]->which_output = sp->slot->which_output;
497                                                 
498                                                 /* show alpha in this case */
499                                                 if(tex==NULL || (tex->flag & TEX_PRV_ALPHA)) {
500                                                         mat->mtex[0]->mapto |= MAP_ALPHA;
501                                                         mat->alpha= 0.0f;
502                                                 }
503                                                 else {
504                                                         mat->mtex[0]->mapto &= ~MAP_ALPHA;
505                                                         mat->alpha= 1.0f;
506                                                 }
507                                         }
508                                 }
509                         }
510
511                         if(tex && tex->nodetree && sp->pr_method==PR_NODE_RENDER) {
512                                 /* two previews, they get copied by wmJob */
513                                 ntreeInitPreview(origtex->nodetree, sp->sizex, sp->sizey);
514                                 ntreeInitPreview(tex->nodetree, sp->sizex, sp->sizey);
515                         }
516                 }
517                 else if(id_type==ID_LA) {
518                         Lamp *la= (Lamp *)id;
519                         
520                         if(la && la->type==LA_SUN && (la->sun_effect_type & LA_SUN_EFFECT_SKY)) {
521                                 sce->lay= 1<<MA_ATMOS;
522                                 sce->world= scene->world;
523                                 sce->camera= (Object *)BLI_findstring(&pr_main->object, "CameraAtmo", offsetof(ID, name)+2);
524                         }
525                         else {
526                                 sce->lay= 1<<MA_LAMP;
527                                 sce->world= NULL;
528                                 sce->camera= (Object *)BLI_findstring(&pr_main->object, "Camera", offsetof(ID, name)+2);
529                         }
530                         sce->r.mode &= ~R_SHADOW;
531                         
532                         for(base= sce->base.first; base; base= base->next) {
533                                 if(base->object->id.name[2]=='p') {
534                                         if(base->object->type==OB_LAMP)
535                                                 base->object->data= la;
536                                 }
537                         }
538                 }
539                 else if(id_type==ID_WO) {
540                         sce->lay= 1<<MA_SKY;
541                         sce->world= (World *)id;
542                 }
543                 
544                 return sce;
545         }
546         
547         return NULL;
548 }
549
550 /* new UI convention: draw is in pixel space already. */
551 /* uses ROUNDBOX button in block to get the rect */
552 static int ed_preview_draw_rect(ScrArea *sa, Scene *sce, ID *id, int split, int first, rcti *rect, rcti *newrect)
553 {
554         Render *re;
555         RenderResult rres;
556         char name[32];
557         int do_gamma_correct=0;
558         int offx=0, newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
559
560         if (id && GS(id->name) != ID_TE) {
561                 /* exception: don't color manage texture previews - show the raw values */
562                 if (sce) do_gamma_correct = sce->r.color_mgt_flag & R_COLOR_MANAGEMENT;
563         }
564
565         if(!split || first) sprintf(name, "Preview %p", (void *)sa);
566         else sprintf(name, "SecondPreview %p", (void *)sa);
567
568         if(split) {
569                 if(first) {
570                         offx= 0;
571                         newx= newx/2;
572                 }
573                 else {
574                         offx= newx/2;
575                         newx= newx - newx/2;
576                 }
577         }
578
579         re= RE_GetRender(name);
580         RE_AcquireResultImage(re, &rres);
581
582         if(rres.rectf) {
583                 
584                 if(ABS(rres.rectx-newx)<2 && ABS(rres.recty-newy)<2) {
585                         newrect->xmax= MAX2(newrect->xmax, rect->xmin + rres.rectx + offx);
586                         newrect->ymax= MAX2(newrect->ymax, rect->ymin + rres.recty);
587
588                         glaDrawPixelsSafe_to32(rect->xmin+offx, rect->ymin, rres.rectx, rres.recty, rres.rectx, rres.rectf, do_gamma_correct);
589
590                         RE_ReleaseResultImage(re);
591                         return 1;
592                 }
593         }
594
595         RE_ReleaseResultImage(re);
596         return 0;
597 }
598
599 void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, rcti *rect)
600 {
601         if(idp) {
602                 ScrArea *sa= CTX_wm_area(C);
603                 Scene *sce = CTX_data_scene(C);
604                 ID *id = (ID *)idp;
605                 ID *parent= (ID *)parentp;
606                 MTex *slot= (MTex *)slotp;
607                 SpaceButs *sbuts= sa->spacedata.first;
608                 rcti newrect;
609                 int ok;
610                 int newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
611
612                 newrect.xmin= rect->xmin;
613                 newrect.xmax= rect->xmin;
614                 newrect.ymin= rect->ymin;
615                 newrect.ymax= rect->ymin;
616
617                 if(parent) {
618                         ok = ed_preview_draw_rect(sa, sce, id, 1, 1, rect, &newrect);
619                         ok &= ed_preview_draw_rect(sa, sce, parent, 1, 0, rect, &newrect);
620                 }
621                 else
622                         ok = ed_preview_draw_rect(sa, sce, id, 0, 0, rect, &newrect);
623
624                 if(ok)
625                         *rect= newrect;
626
627                 /* check for spacetype... */
628                 if(sbuts->spacetype==SPACE_BUTS && sbuts->preview) {
629                         sbuts->preview= 0;
630                         ok= 0;
631                 }
632         
633                 if(ok==0) {
634                         ED_preview_shader_job(C, sa, id, parent, slot, newx, newy, PR_BUTS_RENDER);
635                 }
636         }       
637 }
638
639 /* ******************************** Icon Preview **************************** */
640
641 void ED_preview_icon_draw(const bContext *UNUSED(C), void *UNUSED(idp), void *UNUSED(arg1), void *UNUSED(arg2), rcti *UNUSED(rect))
642 {
643 }
644
645 /* *************************** Preview for 3d window *********************** */
646
647 void view3d_previewrender_progress(RenderResult *rr, volatile rcti *renrect)
648 {
649 //      ScrArea *sa= NULL; // XXX
650 //      View3D *v3d= NULL; // XXX
651         RenderLayer *rl;
652         int ofsx=0, ofsy=0;
653         
654         if(renrect) return;
655         
656         rl= rr->layers.first;
657         
658         /* this case is when we render envmaps... */
659 //      if(rr->rectx > v3d->ri->pr_rectx || rr->recty > v3d->ri->pr_recty)
660 //              return;
661         
662 //      ofsx= v3d->ri->disprect.xmin + rr->tilerect.xmin;
663 //      ofsy= v3d->ri->disprect.ymin + rr->tilerect.ymin;
664         
665         glDrawBuffer(GL_FRONT);
666 //      glaDefine2DArea(&sa->winrct);
667         glaDrawPixelsSafe_to32(ofsx, ofsy, rr->rectx, rr->recty, rr->rectx, rl->rectf, 0);
668         bglFlush();
669         glDrawBuffer(GL_BACK);
670
671 }
672
673 void BIF_view3d_previewrender_signal(ScrArea *UNUSED(sa), short UNUSED(signal))
674 {
675 #if 0
676         View3D *v3d= sa->spacedata.first;
677         
678         /* this can be called from other window... solve! */
679         if(sa->spacetype!=SPACE_VIEW3D)
680                 return; // XXX
681            
682         if(v3d && v3d->ri) {
683                 RenderInfo *ri= v3d->ri;
684                 ri->status &= ~signal;
685                 ri->curtile= 0;
686                 //printf("preview signal %d\n", signal);
687                 if(ri->re && (signal & PR_DBASE))
688                         RE_Database_Free(ri->re);
689
690 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
691         }
692 #endif
693 }
694
695 void BIF_view3d_previewrender_free(View3D *UNUSED(v3d))
696 {
697 #if 0
698         if(v3d->ri) {
699                 RenderInfo *ri= v3d->ri;
700                 if(ri->re) {
701 //                      printf("free render\n");
702                         RE_Database_Free(ri->re);
703                         RE_FreeRender(ri->re);
704                         ri->re= NULL;
705                 }
706                 if (v3d->ri->rect) MEM_freeN(v3d->ri->rect);
707                 MEM_freeN(v3d->ri);
708                 v3d->ri= NULL;
709         }
710 #endif
711 }
712
713 /* returns 1 if OK, do not call while in panel space!  */
714 static int view3d_previewrender_get_rects(ScrArea *sa, rctf *viewplane, RenderInfo *ri, float *clipsta, float *clipend, int *ortho, float *pixsize)
715 {
716         View3D *v3d= NULL; // XXX
717         RegionView3D *rv3d= NULL; // XXX
718         int rectx, recty;
719 //      uiBlock *block;
720         
721 //      block= uiFindOpenPanelBlockName(&sa->uiblocks, "Preview");
722 //      if(block==NULL) return 0;
723         
724         /* calculate preview rect size */
725 //      BLI_init_rctf(viewplane, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
726 //      uiPanelPush(block);
727 //      ui_graphics_to_window_rct(sa->win, viewplane, &ri->disprect);
728 //      uiPanelPop(block);
729         
730         /* correction for gla draw */
731 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
732         
733         *ortho= get_view3d_viewplane(v3d, rv3d, sa->winx, sa->winy, viewplane, clipsta, clipend, pixsize);
734
735         rectx= ri->disprect.xmax - ri->disprect.xmin;
736         recty= ri->disprect.ymax - ri->disprect.ymin;
737         
738         if(rectx<4 || recty<4) return 0;
739         
740         if(ri->rect && (rectx!=ri->pr_rectx || recty!=ri->pr_recty)) {
741                 MEM_freeN(ri->rect);
742                 ri->rect= NULL;
743                 ri->curtile= 0;
744                 printf("changed size\n");
745         }
746         ri->pr_rectx= rectx;
747         ri->pr_recty= recty;
748         
749         return 1;
750 }
751
752 /* called before a panel gets moved/scaled, makes sure we can see through */
753 void BIF_view3d_previewrender_clear(ScrArea *UNUSED(sa))
754 {
755 #if 0
756         View3D *v3d= sa->spacedata.first;
757
758         if(v3d->ri) {
759                 RenderInfo *ri= v3d->ri;
760                 ri->curtile= 0;
761                 if(ri->rect)
762                         MEM_freeN(ri->rect);
763                 ri->rect= NULL;
764         }
765 #endif
766 }
767
768 /* afterqueue call */
769 void BIF_view3d_previewrender(Main *bmain, Scene *scene, ScrArea *sa)
770 {
771         View3D *v3d= sa->spacedata.first;
772         RegionView3D *rv3d= NULL; // XXX
773         Render *re;
774         RenderInfo *ri=NULL;    /* preview struct! */
775         RenderStats *rstats;
776         RenderData rdata;
777         rctf viewplane;
778         float clipsta, clipend, pixsize;
779         int orth;
780         
781         /* first get the render info right */
782 //      if (!v3d->ri) {
783 //              ri= v3d->ri= MEM_callocN(sizeof(RenderInfo), "butsrenderinfo");
784 //              ri->tottile= 10000;
785 //      }
786 //      ri= v3d->ri;
787         
788         if(0==view3d_previewrender_get_rects(sa, &viewplane, ri, &clipsta, &clipend, &orth, &pixsize))
789                 return;
790         
791         /* render is finished, so return */
792         if(ri->tottile && ri->curtile>=ri->tottile) return;
793
794         /* or return with a new event */
795         if(qtest()) {
796 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
797                 return;
798         }
799         //printf("Enter previewrender\n");
800         /* ok, are we rendering all over? */
801         if(ri->re==NULL) {
802                 char name[32];
803                 
804                 ri->status= 0;
805                 
806                 sprintf(name, "View3dPreview %p", (void *)sa);
807                 re= ri->re= RE_NewRender(name);
808                 //RE_display_draw_cb(re, view3d_previewrender_progress);
809                 //RE_stats_draw_cb(re, view3d_previewrender_stats);
810                 //RE_test_break_cb(re, qtest);
811                 
812                 /* no osa, blur, seq, layers, etc for preview render */
813                 rdata= scene->r;
814                 rdata.mode &= ~(R_OSA|R_MBLUR);
815                 rdata.scemode &= ~(R_DOSEQ|R_DOCOMP|R_FREE_IMAGE);
816                 rdata.layers.first= rdata.layers.last= NULL;
817                 rdata.renderer= R_INTERN;
818                  
819                 RE_InitState(re, NULL, &rdata, NULL, sa->winx, sa->winy, &ri->disprect);
820         
821                 if(orth)
822                         RE_SetOrtho(re, &viewplane, clipsta, clipend);
823                 else
824                         RE_SetWindow(re, &viewplane, clipsta, clipend);
825                 RE_SetPixelSize(re, pixsize);
826                 
827                 /* until here are no escapes */
828                 ri->status |= PR_DISPRECT;
829                 ri->curtile= 0;
830                 //printf("new render\n");
831         }
832
833         re= ri->re;
834         
835         PIL_sleep_ms(100);      /* wait 0.1 second if theres really no event... */
836         if(qtest()==0)  {
837                 
838                 /* check status */
839                 if((ri->status & PR_DISPRECT)==0) {
840                         RE_SetDispRect(ri->re, &ri->disprect);
841                         if(orth)
842                                 RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
843                         else
844                                 RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
845                         RE_SetPixelSize(re, pixsize);
846                         ri->status |= PR_DISPRECT;
847                         ri->curtile= 0;
848                         //printf("disprect update\n");
849                 }
850                 if((ri->status & PR_DBASE)==0) {
851                         unsigned int lay= scene->lay;
852                         
853                         RE_SetView(re, rv3d->viewmat);
854                         
855                         /* allow localview render for objects with lights in normal layers */
856                         if(v3d->lay & 0xFF000000)
857                                 lay |= v3d->lay;
858                         else lay= v3d->lay;
859                         
860                         RE_Database_FromScene(re, bmain, scene, lay, 0);                // 0= dont use camera view
861                         
862                         rstats= RE_GetStats(re);
863                         if(rstats->convertdone) 
864                                 ri->status |= PR_DBASE|PR_PROJECTED|PR_ROTATED;
865                         ri->curtile= 0;
866                         
867                         /* database can have created render-resol data... */
868                         if(rstats->convertdone) 
869                                 DAG_scene_flush_update(bmain, scene, scene->lay, 0);
870                         
871                         //printf("dbase update\n");
872                 }
873                 if((ri->status & PR_PROJECTED)==0) {
874                         if(ri->status & PR_DBASE) {
875                                 if(orth)
876                                         RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
877                                 else
878                                         RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
879                                 RE_DataBase_ApplyWindow(re);
880                                 ri->status |= PR_PROJECTED;
881                         }
882                         ri->curtile= 0;
883                         //printf("project update\n");
884                 }
885         
886                 /* OK, can we enter render code? */
887                 if(ri->status==(PR_DISPRECT|PR_DBASE|PR_PROJECTED|PR_ROTATED)) {
888                         //printf("curtile %d tottile %d\n", ri->curtile, ri->tottile);
889                         RE_TileProcessor(ri->re); //, ri->curtile, 0);
890         
891                         if(ri->rect==NULL)
892                                 ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "preview view3d rect");
893                         
894                         RE_ResultGet32(ri->re, ri->rect);
895                 }
896                 
897                 rstats= RE_GetStats(ri->re);
898 //              if(rstats->totpart==rstats->partsdone && rstats->partsdone)
899 //                      addqueue(sa->win, REDRAW, 1);
900 //              else
901 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
902                 
903                 ri->curtile= rstats->partsdone;
904                 ri->tottile= rstats->totpart;
905         }
906         else {
907 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
908         }
909         
910         //printf("\n");
911 }
912
913 /* in panel space! */
914 static void view3d_previewdraw_rect(ScrArea *UNUSED(sa), uiBlock *UNUSED(block), RenderInfo *ri)
915 {
916 //      rctf dispf;
917         
918         if(ri->rect==NULL)
919                 return;
920         
921 //      BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
922 //      ui_graphics_to_window_rct(sa->win, &dispf, &ri->disprect);
923
924         /* correction for gla draw */
925 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
926         
927         /* when panel scale changed, free rect */
928         if(ri->disprect.xmax-ri->disprect.xmin != ri->pr_rectx ||
929            ri->disprect.ymax-ri->disprect.ymin != ri->pr_recty) {
930                 MEM_freeN(ri->rect);
931                 ri->rect= NULL;
932         }
933         else {
934 //              glaDefine2DArea(&sa->winrct);
935                 glaDrawPixelsSafe(ri->disprect.xmin, ri->disprect.ymin, ri->pr_rectx, ri->pr_recty, ri->pr_rectx, GL_RGBA, GL_UNSIGNED_BYTE, ri->rect);
936         }       
937 }
938
939 /* is panel callback, supposed to be called with correct panel offset matrix */
940 void BIF_view3d_previewdraw(struct ScrArea *sa, struct uiBlock *block)
941 {
942         RegionView3D *rv3d= NULL;
943
944 //      if (v3d->ri==NULL || v3d->ri->rect==NULL) 
945 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
946 //      else {
947                 view3d_previewdraw_rect(sa, block, rv3d->ri);
948 //              if(v3d->ri->curtile==0) 
949 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
950 //      }
951 }
952
953
954 /* **************************** new shader preview system ****************** */
955
956 /* inside thread, called by renderer, sets job update value */
957 static void shader_preview_draw(void *spv, RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect))
958 {
959         ShaderPreview *sp= spv;
960         
961         *(sp->do_update)= 1;
962 }
963
964 /* called by renderer, checks job value */
965 static int shader_preview_break(void *spv)
966 {
967         ShaderPreview *sp= spv;
968
969         return *(sp->stop);
970 }
971
972 /* outside thread, called before redraw notifiers, it moves finished preview over */
973 static void shader_preview_updatejob(void *spv)
974 {
975         ShaderPreview *sp= spv;
976         
977         if(sp->id) {
978                 if(sp->pr_method==PR_NODE_RENDER) {
979                         if( GS(sp->id->name) == ID_MA) {
980                                 Material *mat= (Material *)sp->id;
981                                 
982                                 if(sp->matcopy && mat->nodetree && sp->matcopy->nodetree)
983                                         ntreeLocalSync(sp->matcopy->nodetree, mat->nodetree);
984                         }
985                         else if( GS(sp->id->name) == ID_TE) {
986                                 Tex *tex= (Tex *)sp->id;
987                                 
988                                 if(sp->texcopy && tex->nodetree && sp->texcopy->nodetree)
989                                         ntreeLocalSync(sp->texcopy->nodetree, tex->nodetree);
990                         }
991                 }               
992         }
993 }
994
995 static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
996 {
997         Render *re;
998         Scene *sce;
999         float oldlens;
1000         short idtype= GS(id->name);
1001         char name[32];
1002         int sizex;
1003         
1004         /* get the stuff from the builtin preview dbase */
1005         sce= preview_prepare_scene(sp->scene, id, idtype, sp); // XXX sizex
1006         if(sce==NULL) return;
1007         
1008         if(!split || first) sprintf(name, "Preview %p", sp->owner);
1009         else sprintf(name, "SecondPreview %p", sp->owner);
1010         re= RE_GetRender(name);
1011         
1012         /* full refreshed render from first tile */
1013         if(re==NULL)
1014                 re= RE_NewRender(name);
1015                 
1016         /* sce->r gets copied in RE_InitState! */
1017         sce->r.scemode &= ~(R_MATNODE_PREVIEW|R_TEXNODE_PREVIEW);
1018         sce->r.scemode &= ~R_NO_IMAGE_LOAD;
1019
1020         if(sp->pr_method==PR_ICON_RENDER) {
1021                 sce->r.scemode |= R_NO_IMAGE_LOAD;
1022                 sce->r.mode |= R_OSA;
1023         }
1024         else if(sp->pr_method==PR_NODE_RENDER) {
1025                 if(idtype == ID_MA) sce->r.scemode |= R_MATNODE_PREVIEW;
1026                 else if(idtype == ID_TE) sce->r.scemode |= R_TEXNODE_PREVIEW;
1027                 sce->r.mode &= ~R_OSA;
1028         }
1029         else {  /* PR_BUTS_RENDER */
1030                 sce->r.mode |= R_OSA;
1031         }
1032
1033         /* in case of split preview, use border render */
1034         if(split) {
1035                 if(first) sizex= sp->sizex/2;
1036                 else sizex= sp->sizex - sp->sizex/2;
1037         }
1038         else sizex= sp->sizex;
1039
1040         /* allocates or re-uses render result */
1041         sce->r.xsch= sizex;
1042         sce->r.ysch= sp->sizey;
1043         sce->r.size= 100;
1044
1045         /* callbacs are cleared on GetRender() */
1046         if(ELEM(sp->pr_method, PR_BUTS_RENDER, PR_NODE_RENDER)) {
1047                 RE_display_draw_cb(re, sp, shader_preview_draw);
1048         }
1049         /* set this for all previews, default is react to G.afbreek still */
1050         RE_test_break_cb(re, sp, shader_preview_break);
1051         
1052         /* lens adjust */
1053         oldlens= ((Camera *)sce->camera->data)->lens;
1054         if(sizex > sp->sizey)
1055                 ((Camera *)sce->camera->data)->lens *= (float)sp->sizey/(float)sizex;
1056
1057         /* entire cycle for render engine */
1058         RE_PreviewRender(re, pr_main, sce);
1059
1060         ((Camera *)sce->camera->data)->lens= oldlens;
1061
1062         /* handle results */
1063         if(sp->pr_method==PR_ICON_RENDER) {
1064                 // char *rct= (char *)(sp->pr_rect + 32*16 + 16);
1065                 
1066                 if(sp->pr_rect)
1067                         RE_ResultGet32(re, sp->pr_rect);
1068         }
1069         else {
1070                 /* validate owner */
1071                 //if(ri->rect==NULL)
1072                 //      ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
1073                 //RE_ResultGet32(re, ri->rect);
1074         }
1075
1076         /* unassign the pointers, reset vars */
1077         preview_prepare_scene(sp->scene, NULL, GS(id->name), sp);
1078         
1079         /* XXX bad exception, end-exec is not being called in render, because it uses local main */
1080 //      if(idtype == ID_TE) {
1081 //              Tex *tex= (Tex *)id;
1082 //              if(tex->use_nodes && tex->nodetree)
1083 //                      ntreeEndExecTree(tex->nodetree);
1084 //      }
1085
1086 }
1087
1088 /* runs inside thread for material and icons */
1089 static void shader_preview_startjob(void *customdata, short *stop, short *do_update)
1090 {
1091         ShaderPreview *sp= customdata;
1092
1093         sp->stop= stop;
1094         sp->do_update= do_update;
1095
1096         if(sp->parent) {
1097                 shader_preview_render(sp, sp->id, 1, 1);
1098                 shader_preview_render(sp, sp->parent, 1, 0);
1099         }
1100         else
1101                 shader_preview_render(sp, sp->id, 0, 0);
1102
1103         *do_update= 1;
1104 }
1105
1106 static void shader_preview_free(void *customdata)
1107 {
1108         ShaderPreview *sp= customdata;
1109         
1110         if(sp->matcopy) {
1111                 struct IDProperty *properties;
1112                 int a;
1113                 
1114                 /* node previews */
1115                 shader_preview_updatejob(sp);
1116                 
1117                 /* get rid of copied material */
1118                 BLI_remlink(&pr_main->mat, sp->matcopy);
1119                 
1120                 /* free_material decrements texture, prevent this. hack alert! */
1121                 for(a=0; a<MAX_MTEX; a++) {
1122                         MTex *mtex= sp->matcopy->mtex[a];
1123                         if(mtex && mtex->tex) mtex->tex= NULL;
1124                 }
1125                 
1126                 free_material(sp->matcopy);
1127
1128                 properties= IDP_GetProperties((ID *)sp->matcopy, FALSE);
1129                 if (properties) {
1130                         IDP_FreeProperty(properties);
1131                         MEM_freeN(properties);
1132                 }
1133                 MEM_freeN(sp->matcopy);
1134         }
1135         if(sp->texcopy) {
1136                 struct IDProperty *properties;
1137                 /* node previews */
1138                 shader_preview_updatejob(sp);
1139                 
1140                 /* get rid of copied texture */
1141                 BLI_remlink(&pr_main->tex, sp->texcopy);
1142                 free_texture(sp->texcopy);
1143                 
1144                 properties= IDP_GetProperties((ID *)sp->texcopy, FALSE);
1145                 if (properties) {
1146                         IDP_FreeProperty(properties);
1147                         MEM_freeN(properties);
1148                 }
1149                 MEM_freeN(sp->texcopy);
1150         }
1151         
1152         MEM_freeN(sp);
1153 }
1154
1155 /* ************************* icon preview ********************** */
1156
1157 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
1158 {
1159         struct ImBuf *ima;
1160         unsigned int *drect, *srect;
1161         float scaledx, scaledy;
1162         short ex, ey, dx, dy;
1163
1164         /* paranoia test */
1165         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
1166                 return;
1167         
1168         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
1169         ima = IMB_dupImBuf(ibuf);
1170         
1171         if (!ima) 
1172                 return;
1173         
1174         if (ima->x > ima->y) {
1175                 scaledx = (float)w;
1176                 scaledy =  ( (float)ima->y/(float)ima->x )*(float)w;
1177         }
1178         else {                  
1179                 scaledx =  ( (float)ima->x/(float)ima->y )*(float)h;
1180                 scaledy = (float)h;
1181         }
1182         
1183         ex = (short)scaledx;
1184         ey = (short)scaledy;
1185         
1186         dx = (w - ex) / 2;
1187         dy = (h - ey) / 2;
1188         
1189         IMB_scalefastImBuf(ima, ex, ey);
1190         
1191         /* if needed, convert to 32 bits */
1192         if(ima->rect==NULL)
1193                 IMB_rect_from_float(ima);
1194
1195         srect = ima->rect;
1196         drect = rect;
1197
1198         drect+= dy*w+dx;
1199         for (;ey > 0; ey--){            
1200                 memcpy(drect,srect, ex * sizeof(int));
1201                 drect += w;
1202                 srect += ima->x;
1203         }
1204
1205         IMB_freeImBuf(ima);
1206 }
1207
1208 static void set_alpha(char *cp, int sizex, int sizey, char alpha) 
1209 {
1210         int a, size= sizex*sizey;
1211
1212         for(a=0; a<size; a++, cp+=4)
1213                 cp[3]= alpha;
1214 }
1215
1216 static void icon_preview_startjob(void *customdata, short *stop, short *do_update)
1217 {
1218         ShaderPreview *sp= customdata;
1219         ID *id= sp->id;
1220         short idtype= GS(id->name);
1221
1222         if(idtype == ID_IM) {
1223                 Image *ima= (Image*)id;
1224                 ImBuf *ibuf= NULL;
1225                 ImageUser iuser= {NULL};
1226
1227                 /* ima->ok is zero when Image cannot load */
1228                 if(ima==NULL || ima->ok==0)
1229                         return;
1230
1231                 /* setup dummy image user */
1232                 iuser.ok= iuser.framenr= 1;
1233                 iuser.scene= sp->scene;
1234                 
1235                 /* elubie: this needs to be changed: here image is always loaded if not
1236                    already there. Very expensive for large images. Need to find a way to 
1237                    only get existing ibuf */
1238                 ibuf = BKE_image_get_ibuf(ima, &iuser);
1239                 if(ibuf==NULL || ibuf->rect==NULL)
1240                         return;
1241                 
1242                 icon_copy_rect(ibuf, sp->sizex, sp->sizey, sp->pr_rect);
1243
1244                 *do_update= 1;
1245         }
1246         else if(idtype == ID_BR) {
1247                 Brush *br= (Brush*)id;
1248
1249                 br->icon_imbuf= get_brush_icon(br);
1250
1251                 memset(sp->pr_rect, 0x888888, sp->sizex*sp->sizey*sizeof(unsigned int));
1252
1253                 if(!(br->icon_imbuf) || !(br->icon_imbuf->rect))
1254                         return;
1255
1256                 icon_copy_rect(br->icon_imbuf, sp->sizex, sp->sizey, sp->pr_rect);
1257
1258                 *do_update= 1;
1259         }
1260         else {
1261                 /* re-use shader job */
1262                 shader_preview_startjob(customdata, stop, do_update);
1263
1264                 /* world is rendered with alpha=0, so it wasn't displayed 
1265                    this could be render option for sky to, for later */
1266                 if(idtype == ID_WO) {
1267                         set_alpha((char*)sp->pr_rect, sp->sizex, sp->sizey, 255);
1268                 }
1269                 else if(idtype == ID_MA) {
1270                         Material* ma = (Material*)id;
1271
1272                         if(ma->material_type == MA_TYPE_HALO)
1273                                 set_alpha((char*)sp->pr_rect, sp->sizex, sp->sizey, 255);
1274                 }
1275         }
1276 }
1277
1278 /* use same function for icon & shader, so the job manager
1279    does not run two of them at the same time. */
1280
1281 static void common_preview_startjob(void *customdata, short *stop, short *do_update, float *UNUSED(progress))
1282 {
1283         ShaderPreview *sp= customdata;
1284
1285         if(sp->pr_method == PR_ICON_RENDER)
1286                 icon_preview_startjob(customdata, stop, do_update);
1287         else
1288                 shader_preview_startjob(customdata, stop, do_update);
1289 }
1290
1291 static void common_preview_endjob(void *customdata)
1292 {
1293         ShaderPreview *sp= customdata;
1294
1295         if(sp->id && GS(sp->id->name) == ID_BR)
1296                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, sp->id);
1297 }
1298
1299 /* exported functions */
1300
1301 void ED_preview_icon_job(const bContext *C, void *owner, ID *id, unsigned int *rect, int sizex, int sizey)
1302 {
1303         wmJob *steve;
1304         ShaderPreview *sp;
1305         
1306         /* suspended start means it starts after 1 timer step, see WM_jobs_timer below */
1307         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Icon Preview", WM_JOB_EXCL_RENDER|WM_JOB_SUSPEND);
1308         sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
1309
1310         /* customdata for preview thread */
1311         sp->scene= CTX_data_scene(C);
1312         sp->owner= id;
1313         sp->sizex= sizex;
1314         sp->sizey= sizey;
1315         sp->pr_method= PR_ICON_RENDER;
1316         sp->pr_rect= rect;
1317         sp->id = id;
1318
1319         /* setup job */
1320         WM_jobs_customdata(steve, sp, shader_preview_free);
1321         WM_jobs_timer(steve, 0.25, NC_MATERIAL, NC_MATERIAL);
1322         WM_jobs_callbacks(steve, common_preview_startjob, NULL, NULL, common_preview_endjob);
1323
1324         WM_jobs_start(CTX_wm_manager(C), steve);
1325 }
1326
1327 void ED_preview_shader_job(const bContext *C, void *owner, ID *id, ID *parent, MTex *slot, int sizex, int sizey, int method)
1328 {
1329         Object *ob= CTX_data_active_object(C);
1330         wmJob *steve;
1331         ShaderPreview *sp;
1332
1333         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Shader Preview", WM_JOB_EXCL_RENDER);
1334         sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
1335
1336         /* customdata for preview thread */
1337         sp->scene= CTX_data_scene(C);
1338         sp->owner= owner;
1339         sp->sizex= sizex;
1340         sp->sizey= sizey;
1341         sp->pr_method= method;
1342         sp->id = id;
1343         sp->parent= parent;
1344         sp->slot= slot;
1345         if(ob && ob->totcol) copy_v4_v4(sp->col, ob->col);
1346         else sp->col[0]= sp->col[1]= sp->col[2]= sp->col[3]= 1.0f;
1347         
1348         /* setup job */
1349         WM_jobs_customdata(steve, sp, shader_preview_free);
1350         WM_jobs_timer(steve, 0.1, NC_MATERIAL, NC_MATERIAL);
1351         WM_jobs_callbacks(steve, common_preview_startjob, NULL, shader_preview_updatejob, NULL);
1352         
1353         WM_jobs_start(CTX_wm_manager(C), steve);
1354 }
1355
1356 void ED_preview_kill_jobs(const struct bContext *C)
1357 {
1358         wmWindowManager *wm= CTX_wm_manager(C);
1359         if(wm)
1360                 WM_jobs_kill(wm, NULL, common_preview_startjob);
1361 }
1362