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