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