svn merge -r 22628:22714 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender-staging.git] / source / blender / editors / preview / previewrender.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_arithb.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_threads.h"
48
49 #include "DNA_texture_types.h"
50 #include "DNA_world_types.h"
51 #include "DNA_camera_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_node_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_space_types.h"
58 #include "DNA_view3d_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_screen_types.h"
61
62 #include "BKE_context.h"
63 #include "BKE_depsgraph.h"
64 #include "BKE_global.h"
65 #include "BKE_image.h"
66 #include "BKE_icons.h"
67 #include "BKE_library.h"
68 #include "BKE_main.h"
69 #include "BKE_texture.h"
70 #include "BKE_material.h"
71 #include "BKE_node.h"
72 #include "BKE_world.h"
73 #include "BKE_texture.h"
74 #include "BKE_utildefines.h"
75
76 #include "IMB_imbuf.h"
77 #include "IMB_imbuf_types.h"
78
79 #include "BIF_gl.h"
80 #include "BIF_glutil.h"
81
82 #include "PIL_time.h"
83
84 #include "RE_pipeline.h"
85
86 #include "GPU_material.h"
87
88 #include "WM_api.h"
89 #include "WM_types.h"
90
91 #include "ED_anim_api.h"
92 #include "ED_previewrender.h"
93 #include "ED_view3d.h"
94
95 #include "UI_interface.h"
96
97 #include "previewrender_intern.h"
98
99 #define PR_XMIN         10
100 #define PR_YMIN         5
101 #define PR_XMAX         200
102 #define PR_YMAX         195
103
104 /* XXX */
105 static int qtest() {return 0;}
106 /* XXX */
107
108 typedef struct ShaderPreview {
109         /* from wmJob */
110         void *owner;
111         short *stop, *do_update;
112         
113         Scene *scene;
114         ID *id;
115         ID *parent;
116         MTex *slot;
117         
118         int sizex, sizey;
119         int *pr_rect;
120         int pr_method;
121         
122 } ShaderPreview;
123
124
125
126 /* unused now */
127 void draw_tex_crop(Tex *tex)
128 {
129         rcti rct;
130         int ret= 0;
131         
132         if(tex==0) return;
133         
134         if(tex->type==TEX_IMAGE) {
135                 if(tex->cropxmin==0.0f) ret++;
136                 if(tex->cropymin==0.0f) ret++;
137                 if(tex->cropxmax==1.0f) ret++;
138                 if(tex->cropymax==1.0f) ret++;
139                 if(ret==4) return;
140                 
141                 rct.xmin= PR_XMIN+2+tex->cropxmin*(PR_XMAX-PR_XMIN-4);
142                 rct.xmax= PR_XMIN+2+tex->cropxmax*(PR_XMAX-PR_XMIN-4);
143                 rct.ymin= PR_YMIN+2+tex->cropymin*(PR_YMAX-PR_YMIN-4);
144                 rct.ymax= PR_YMIN+2+tex->cropymax*(PR_YMAX-PR_YMIN-4);
145
146                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
147
148                 glColor3ub(0, 0, 0);
149                 glRecti(rct.xmin+1,  rct.ymin-1,  rct.xmax+1,  rct.ymax-1); 
150
151                 glColor3ub(255, 255, 255);
152                 glRecti(rct.xmin,  rct.ymin,  rct.xmax,  rct.ymax);
153
154                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);                      
155         }
156         
157 }
158
159 /* temporal abuse; if id_code is -1 it only does texture.... solve! */
160 void BIF_preview_changed(short id_code)
161 {
162 #if 0   
163         ScrArea *sa;
164         
165         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
166                 if(sa->spacetype==SPACE_BUTS) {
167                         SpaceButs *sbuts= sa->spacedata.first;
168                         if(sbuts->mainb==CONTEXT_SHADING) {
169                                 int tab= sbuts->tab[CONTEXT_SHADING];
170                                 if(tab==TAB_SHADING_MAT && (id_code==ID_MA || id_code==ID_TE)) {
171                                         if (sbuts->ri) sbuts->ri->curtile= 0;
172                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
173                                 }
174                                 else if(tab==TAB_SHADING_TEX && (id_code==ID_TE || id_code==-1)) {
175                                         if (sbuts->ri) sbuts->ri->curtile= 0;
176                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
177                                 }
178                                 else if(tab==TAB_SHADING_LAMP && (id_code==ID_LA || id_code==ID_TE)) {
179                                         if (sbuts->ri) sbuts->ri->curtile= 0;
180                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
181                                 }
182                                 else if(tab==TAB_SHADING_WORLD && (id_code==ID_WO || id_code==ID_TE)) {
183                                         if (sbuts->ri) sbuts->ri->curtile= 0;
184                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
185                                 }
186                         }
187                         else if (sbuts->ri) 
188                                 sbuts->ri->curtile= 0;  /* ensure changes always result in re-render when context is restored */
189                 }
190                 else if(sa->spacetype==SPACE_NODE) {
191                         SpaceNode *snode= sa->spacedata.first;
192                         if(snode->treetype==NTREE_SHADER && (id_code==ID_MA || id_code==ID_TE)) {
193                                 snode_tag_dirty(snode);
194                         }
195                 }
196                 else if(sa->spacetype==SPACE_VIEW3D) {
197                         View3D *vd= sa->spacedata.first;
198                         /* if is has a renderinfo, we consider that reason for signalling */
199                         if (vd->ri) {
200                                 vd->ri->curtile= 0;
201                                 addafterqueue(sa->win, RENDERPREVIEW, 1);
202                         }
203                 }
204         }
205
206         if(ELEM4(id_code, ID_MA, ID_TE, ID_LA, ID_WO)) {
207                 Object *ob;
208                 Material *ma;
209
210                 if(id_code == ID_WO) {
211                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
212                                 if(ma->gpumaterial.first) {
213                                         GPU_material_free(ma);
214                                 }
215                         }
216                 }
217                 else if(id_code == ID_LA) {
218                         for(ob=G.main->object.first; ob; ob=ob->id.next) {
219                                 if(ob->gpulamp.first) {
220                                         GPU_lamp_free(ob);
221                                 }
222                         }
223
224                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
225                                 if(ma->gpumaterial.first) {
226                                         GPU_material_free(ma);
227                                 }
228                         }
229                 } else if(OBACT) {
230                         Object *ob = OBACT;
231
232                         ma= give_current_material(ob, ob->actcol);
233                         if(ma && ma->gpumaterial.first) {
234                                 GPU_material_free(ma);
235                         }
236                 }
237         }
238 #endif
239 }
240
241 /* *************************** Preview for buttons *********************** */
242
243 static Main *pr_main= NULL;
244
245 void ED_preview_init_dbase(void)
246 {
247         BlendFileData *bfd;
248         extern int datatoc_preview_blend_size;
249         extern char datatoc_preview_blend[];
250         
251         G.fileflags |= G_FILE_NO_UI;
252         bfd= BLO_read_from_memory(datatoc_preview_blend, datatoc_preview_blend_size, NULL);
253         if (bfd) {
254                 pr_main= bfd->main;
255                 
256                 MEM_freeN(bfd);
257         }
258         G.fileflags &= ~G_FILE_NO_UI;
259 }
260
261 void ED_preview_free_dbase(void)
262 {
263         if(pr_main)
264                 free_main(pr_main);
265 }
266
267 static Object *find_object(ListBase *lb, const char *name)
268 {
269         Object *ob;
270         for(ob= lb->first; ob; ob= ob->id.next)
271                 if(strcmp(ob->id.name+2, name)==0)
272                         break;
273         return ob;
274 }
275
276 /* call this with a pointer to initialize preview scene */
277 /* call this with NULL to restore assigned ID pointers in preview scene */
278 static Scene *preview_prepare_scene(Scene *scene, ID *id, int id_type, ShaderPreview *sp)
279 {
280         Scene *sce;
281         Base *base;
282         
283         if(pr_main==NULL) return NULL;
284         
285         sce= pr_main->scene.first;
286         if(sce) {
287                 /* this flag tells render to not execute depsgraph or ipos etc */
288                 sce->r.scemode |= R_PREVIEWBUTS;
289                 /* set world always back, is used now */
290                 sce->world= pr_main->world.first;
291                 /* now: exposure copy */
292                 if(scene->world) {
293                         sce->world->exp= scene->world->exp;
294                         sce->world->range= scene->world->range;
295                 }
296                 
297                 sce->r.color_mgt_flag = scene->r.color_mgt_flag;
298                 sce->r.cfra= scene->r.cfra;
299                 
300                 if(id_type==ID_MA) {
301                         Material *mat= (Material *)id;
302                         
303                         if(id) {
304                                 init_render_material(mat, 0, NULL);             /* call that retrieves mode_l */
305                                 end_render_material(mat);
306                                 
307                                 /* turn on raytracing if needed */
308                                 if(mat->mode_l & MA_RAYMIRROR)
309                                         sce->r.mode |= R_RAYTRACE;
310                                 if(mat->material_type == MA_TYPE_VOLUME)
311                                         sce->r.mode |= R_RAYTRACE;
312                                 if((mat->mode_l & MA_RAYTRANSP) && (mat->mode_l & MA_TRANSP))
313                                         sce->r.mode |= R_RAYTRACE;
314                                 if(mat->sss_flag & MA_DIFF_SSS)
315                                         sce->r.mode |= R_SSS;
316                                 
317                                 /* turn off fake shadows if needed */
318                                 /* this only works in a specific case where the preview.blend contains
319                                  * an object starting with 'c' which has a material linked to it (not the obdata)
320                                  * and that material has a fake shadow texture in the active texture slot */
321                                 for(base= sce->base.first; base; base= base->next) {
322                                         if(base->object->id.name[2]=='c') {
323                                                 Material *shadmat= give_current_material(base->object, base->object->actcol);
324                                                 if(shadmat) {
325                                                         if (mat->mode & MA_SHADBUF) shadmat->septex = 0;
326                                                         else shadmat->septex |= 1;
327                                                 }
328                                         }
329                                 }
330
331                                 
332                                 if(sp->pr_method==PR_ICON_RENDER) {
333                                         if (mat->material_type == MA_TYPE_HALO) {
334                                                 sce->lay= 1<<MA_FLAT;
335                                         } 
336                                         else {
337                                                 sce->lay= 1<<MA_SPHERE_A;
338                                         }
339                                 }
340                                 else {
341                                         sce->lay= 1<<mat->pr_type;
342                                         if(mat->nodetree)
343                                                 ntreeInitPreview(mat->nodetree, sp->sizex, sp->sizey);
344                                 }
345                         }
346                         else {
347                                 sce->r.mode &= ~(R_OSA|R_RAYTRACE|R_SSS);
348                         }
349                         
350                         for(base= sce->base.first; base; base= base->next) {
351                                 if(base->object->id.name[2]=='p') {
352                                         if(ELEM4(base->object->type, OB_MESH, OB_CURVE, OB_SURF, OB_MBALL))
353                                                 assign_material(base->object, mat, base->object->actcol);
354                                 }
355                         }
356                 }
357                 else if(id_type==ID_TE) {
358                         Tex *tex= (Tex *)id;
359                         
360                         sce->lay= 1<<MA_TEXTURE;
361                         
362                         /* exception: don't color manage texture previews */
363                         sce->r.color_mgt_flag &= ~R_COLOR_MANAGEMENT;
364                         
365                         for(base= sce->base.first; base; base= base->next) {
366                                 if(base->object->id.name[2]=='t') {
367                                         Material *mat= give_current_material(base->object, base->object->actcol);
368                                         if(mat && mat->mtex[0]) {
369                                                 mat->mtex[0]->tex= tex;
370                                                 
371                                                 if(sp && sp->slot)
372                                                         mat->mtex[0]->which_output = sp->slot->which_output;
373                                                 
374                                                 /* show alpha in this case */
375                                                 if(tex==NULL || (tex->flag & TEX_PRV_ALPHA)) {
376                                                         mat->mtex[0]->mapto |= MAP_ALPHA;
377                                                         mat->alpha= 0.0f;
378                                                 }
379                                                 else {
380                                                         mat->mtex[0]->mapto &= ~MAP_ALPHA;
381                                                         mat->alpha= 1.0f;
382                                                 }
383                                         }
384                                 }
385                         }
386                 }
387                 else if(id_type==ID_LA) {
388                         Lamp *la= (Lamp *)id;
389                         
390                         if(la && la->type==LA_SUN && (la->sun_effect_type & LA_SUN_EFFECT_SKY)) {
391                                 sce->lay= 1<<MA_ATMOS;
392                                 sce->world= scene->world;
393                                 sce->camera= (Object *)find_object(&pr_main->object, "CameraAtmo");
394                         }
395                         else {
396                                 sce->lay= 1<<MA_LAMP;
397                                 sce->world= NULL;
398                                 sce->camera= (Object *)find_object(&pr_main->object, "Camera");
399                         }
400                         sce->r.mode &= ~R_SHADOW;
401                         
402                         for(base= sce->base.first; base; base= base->next) {
403                                 if(base->object->id.name[2]=='p') {
404                                         if(base->object->type==OB_LAMP)
405                                                 base->object->data= la;
406                                 }
407                         }
408                 }
409                 else if(id_type==ID_WO) {
410                         sce->lay= 1<<MA_SKY;
411                         sce->world= (World *)id;
412                 }
413                 
414                 return sce;
415         }
416         
417         return NULL;
418 }
419
420 /* new UI convention: draw is in pixel space already. */
421 /* uses ROUNDBOX button in block to get the rect */
422 static int ed_preview_draw_rect(ScrArea *sa, Scene *sce, ID *id, int split, int first, rcti *rect, rcti *newrect)
423 {
424         RenderResult rres;
425         char name[32];
426         int gamma_correct=0;
427         int offx=0, newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
428
429         if (id && GS(id->name) != ID_TE) {
430                 /* exception: don't color manage texture previews - show the raw values */
431                 if (sce) gamma_correct = sce->r.color_mgt_flag & R_COLOR_MANAGEMENT;
432         }
433
434         if(!split || first) sprintf(name, "Preview %p", sa);
435         else sprintf(name, "SecondPreview %p", sa);
436
437         if(split) {
438                 if(first) {
439                         offx= 0;
440                         newx= newx/2;
441                 }
442                 else {
443                         offx= newx/2;
444                         newx= newx - newx/2;
445                 }
446         }
447
448         RE_GetResultImage(RE_GetRender(name), &rres);
449
450         if(rres.rectf) {
451                 
452                 if(ABS(rres.rectx-newx)<2 && ABS(rres.recty-newy)<2) {
453                         newrect->xmax= MAX2(newrect->xmax, rect->xmin + rres.rectx + offx);
454                         newrect->ymax= MAX2(newrect->ymax, rect->ymin + rres.recty);
455
456                         glPushMatrix();
457                         glTranslatef(offx, 0, 0);
458                         glaDrawPixelsSafe_to32(rect->xmin, rect->ymin, rres.rectx, rres.recty, rres.rectx, rres.rectf, gamma_correct);
459                         glPopMatrix();
460                         return 1;
461                 }
462         }
463
464         return 0;
465 }
466
467 void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, rcti *rect)
468 {
469         if(idp) {
470                 ScrArea *sa= CTX_wm_area(C);
471                 Scene *sce = CTX_data_scene(C);
472                 ID *id = (ID *)idp;
473                 ID *parent= (ID *)parentp;
474                 MTex *slot= (MTex *)slotp;
475                 SpaceButs *sbuts= sa->spacedata.first;
476                 rcti newrect;
477                 int ok;
478                 int newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
479
480                 newrect.xmin= rect->xmin;
481                 newrect.xmax= rect->xmin;
482                 newrect.ymin= rect->ymin;
483                 newrect.ymax= rect->ymin;
484
485                 if(parent) {
486                         ok = ed_preview_draw_rect(sa, sce, parent, 1, 1, rect, &newrect);
487                         ok &= ed_preview_draw_rect(sa, sce, id, 1, 0, rect, &newrect);
488                 }
489                 else
490                         ok = ed_preview_draw_rect(sa, sce, id, 0, 0, rect, &newrect);
491
492                 if(ok)
493                         *rect= newrect;
494
495                 /* check for spacetype... */
496                 if(sbuts->spacetype==SPACE_BUTS && sbuts->preview) {
497                         sbuts->preview= 0;
498                         ok= 0;
499                 }
500                 
501                 if(ok==0) {
502                         ED_preview_shader_job(C, sa, id, parent, slot, newx, newy);
503                 }
504         }       
505 }
506
507 /* *************************** Preview for 3d window *********************** */
508
509 void view3d_previewrender_progress(RenderResult *rr, volatile rcti *renrect)
510 {
511 //      ScrArea *sa= NULL; // XXX
512 //      View3D *v3d= NULL; // XXX
513         RenderLayer *rl;
514         int ofsx=0, ofsy=0;
515         
516         if(renrect) return;
517         
518         rl= rr->layers.first;
519         
520         /* this case is when we render envmaps... */
521 //      if(rr->rectx > v3d->ri->pr_rectx || rr->recty > v3d->ri->pr_recty)
522 //              return;
523         
524 //      ofsx= v3d->ri->disprect.xmin + rr->tilerect.xmin;
525 //      ofsy= v3d->ri->disprect.ymin + rr->tilerect.ymin;
526         
527         glDrawBuffer(GL_FRONT);
528 //      glaDefine2DArea(&sa->winrct);
529         glaDrawPixelsSafe_to32(ofsx, ofsy, rr->rectx, rr->recty, rr->rectx, rl->rectf, 0);
530         bglFlush();
531         glDrawBuffer(GL_BACK);
532
533 }
534
535 void BIF_view3d_previewrender_signal(ScrArea *sa, short signal)
536 {
537 #if 0
538         View3D *v3d= sa->spacedata.first;
539         
540         /* this can be called from other window... solve! */
541         if(sa->spacetype!=SPACE_VIEW3D)
542                 return; // XXX
543            
544         if(v3d && v3d->ri) {
545                 RenderInfo *ri= v3d->ri;
546                 ri->status &= ~signal;
547                 ri->curtile= 0;
548                 //printf("preview signal %d\n", signal);
549                 if(ri->re && (signal & PR_DBASE))
550                         RE_Database_Free(ri->re);
551
552 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
553         }
554 #endif
555 }
556
557 void BIF_view3d_previewrender_free(View3D *v3d)
558 {
559 #if 0
560         if(v3d->ri) {
561                 RenderInfo *ri= v3d->ri;
562                 if(ri->re) {
563 //                      printf("free render\n");
564                         RE_Database_Free(ri->re);
565                         RE_FreeRender(ri->re);
566                         ri->re= NULL;
567                 }
568                 if (v3d->ri->rect) MEM_freeN(v3d->ri->rect);
569                 MEM_freeN(v3d->ri);
570                 v3d->ri= NULL;
571         }
572 #endif
573 }
574
575 /* returns 1 if OK, do not call while in panel space!  */
576 static int view3d_previewrender_get_rects(ScrArea *sa, rctf *viewplane, RenderInfo *ri, float *clipsta, float *clipend, int *ortho, float *pixsize)
577 {
578         View3D *v3d= NULL; // XXX
579         RegionView3D *rv3d= NULL; // XXX
580         int rectx, recty;
581 //      uiBlock *block;
582         
583 //      block= uiFindOpenPanelBlockName(&sa->uiblocks, "Preview");
584 //      if(block==NULL) return 0;
585         
586         /* calculate preview rect size */
587 //      BLI_init_rctf(viewplane, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
588 //      uiPanelPush(block);
589 //      ui_graphics_to_window_rct(sa->win, viewplane, &ri->disprect);
590 //      uiPanelPop(block);
591         
592         /* correction for gla draw */
593 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
594         
595         *ortho= get_view3d_viewplane(v3d, rv3d, sa->winx, sa->winy, viewplane, clipsta, clipend, pixsize);
596
597         rectx= ri->disprect.xmax - ri->disprect.xmin;
598         recty= ri->disprect.ymax - ri->disprect.ymin;
599         
600         if(rectx<4 || recty<4) return 0;
601         
602         if(ri->rect && (rectx!=ri->pr_rectx || recty!=ri->pr_recty)) {
603                 MEM_freeN(ri->rect);
604                 ri->rect= NULL;
605                 ri->curtile= 0;
606                 printf("changed size\n");
607         }
608         ri->pr_rectx= rectx;
609         ri->pr_recty= recty;
610         
611         return 1;
612 }
613
614 /* called before a panel gets moved/scaled, makes sure we can see through */
615 void BIF_view3d_previewrender_clear(ScrArea *sa)
616 {
617 #if 0
618         View3D *v3d= sa->spacedata.first;
619
620         if(v3d->ri) {
621                 RenderInfo *ri= v3d->ri;
622                 ri->curtile= 0;
623                 if(ri->rect)
624                         MEM_freeN(ri->rect);
625                 ri->rect= NULL;
626         }
627 #endif
628 }
629
630 /* afterqueue call */
631 void BIF_view3d_previewrender(Scene *scene, ScrArea *sa)
632 {
633         bContext *C= NULL;
634         View3D *v3d= sa->spacedata.first;
635         RegionView3D *rv3d= NULL; // XXX
636         Render *re;
637         RenderInfo *ri=NULL;    /* preview struct! */
638         RenderStats *rstats;
639         RenderData rdata;
640         rctf viewplane;
641         float clipsta, clipend, pixsize;
642         int orth;
643         
644         /* first get the render info right */
645 //      if (!v3d->ri) {
646 //              ri= v3d->ri= MEM_callocN(sizeof(RenderInfo), "butsrenderinfo");
647 //              ri->tottile= 10000;
648 //      }
649 //      ri= v3d->ri;
650         
651         if(0==view3d_previewrender_get_rects(sa, &viewplane, ri, &clipsta, &clipend, &orth, &pixsize))
652                 return;
653         
654         /* render is finished, so return */
655         if(ri->tottile && ri->curtile>=ri->tottile) return;
656
657         /* or return with a new event */
658         if(qtest()) {
659 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
660                 return;
661         }
662         //printf("Enter previewrender\n");
663         /* ok, are we rendering all over? */
664         if(ri->re==NULL) {
665                 char name[32];
666                 
667                 ri->status= 0;
668                 
669                 sprintf(name, "View3dPreview %p", sa);
670                 re= ri->re= RE_NewRender(name);
671                 //RE_display_draw_cb(re, view3d_previewrender_progress);
672                 //RE_stats_draw_cb(re, view3d_previewrender_stats);
673                 //RE_test_break_cb(re, qtest);
674                 
675                 /* no osa, blur, seq, layers, etc for preview render */
676                 rdata= scene->r;
677                 rdata.mode &= ~(R_OSA|R_MBLUR);
678                 rdata.scemode &= ~(R_DOSEQ|R_DOCOMP|R_FREE_IMAGE);
679                 rdata.layers.first= rdata.layers.last= NULL;
680                 rdata.renderer= R_INTERN;
681                  
682                 RE_InitState(re, NULL, &rdata, sa->winx, sa->winy, &ri->disprect);
683         
684                 if(orth)
685                         RE_SetOrtho(re, &viewplane, clipsta, clipend);
686                 else
687                         RE_SetWindow(re, &viewplane, clipsta, clipend);
688                 RE_SetPixelSize(re, pixsize);
689                 
690                 /* until here are no escapes */
691                 ri->status |= PR_DISPRECT;
692                 ri->curtile= 0;
693                 //printf("new render\n");
694         }
695
696         re= ri->re;
697         
698         PIL_sleep_ms(100);      /* wait 0.1 second if theres really no event... */
699         if(qtest()==0)  {
700                 
701                 /* check status */
702                 if((ri->status & PR_DISPRECT)==0) {
703                         RE_SetDispRect(ri->re, &ri->disprect);
704                         if(orth)
705                                 RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
706                         else
707                                 RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
708                         RE_SetPixelSize(re, pixsize);
709                         ri->status |= PR_DISPRECT;
710                         ri->curtile= 0;
711                         //printf("disprect update\n");
712                 }
713                 if((ri->status & PR_DBASE)==0) {
714                         unsigned int lay= scene->lay;
715                         
716                         RE_SetView(re, rv3d->viewmat);
717                         
718                         /* allow localview render for objects with lights in normal layers */
719                         if(v3d->lay & 0xFF000000)
720                                 scene->lay |= v3d->lay;
721                         else scene->lay= v3d->lay;
722                         
723                         RE_Database_FromScene(re, scene, 0);            // 0= dont use camera view
724                         scene->lay= lay;
725                         
726                         rstats= RE_GetStats(re);
727                         if(rstats->convertdone) 
728                                 ri->status |= PR_DBASE|PR_PROJECTED|PR_ROTATED;
729                         ri->curtile= 0;
730                         
731                         /* database can have created render-resol data... */
732                         if(rstats->convertdone) 
733                                 ED_anim_dag_flush_update(C); // <--- only current scene XXX
734                         
735                         //printf("dbase update\n");
736                 }
737                 if((ri->status & PR_PROJECTED)==0) {
738                         if(ri->status & PR_DBASE) {
739                                 if(orth)
740                                         RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
741                                 else
742                                         RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
743                                 RE_DataBase_ApplyWindow(re);
744                                 ri->status |= PR_PROJECTED;
745                         }
746                         ri->curtile= 0;
747                         //printf("project update\n");
748                 }
749         
750                 /* OK, can we enter render code? */
751                 if(ri->status==(PR_DISPRECT|PR_DBASE|PR_PROJECTED|PR_ROTATED)) {
752                         //printf("curtile %d tottile %d\n", ri->curtile, ri->tottile);
753                         RE_TileProcessor(ri->re, ri->curtile, 0);
754         
755                         if(ri->rect==NULL)
756                                 ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "preview view3d rect");
757                         
758                         RE_ResultGet32(ri->re, ri->rect);
759                 }
760                 
761                 rstats= RE_GetStats(ri->re);
762 //              if(rstats->totpart==rstats->partsdone && rstats->partsdone)
763 //                      addqueue(sa->win, REDRAW, 1);
764 //              else
765 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
766                 
767                 ri->curtile= rstats->partsdone;
768                 ri->tottile= rstats->totpart;
769         }
770         else {
771 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
772         }
773         
774         //printf("\n");
775 }
776
777 /* in panel space! */
778 static void view3d_previewdraw_rect(ScrArea *sa, uiBlock *block, RenderInfo *ri)
779 {
780 //      rctf dispf;
781         
782         if(ri->rect==NULL)
783                 return;
784         
785 //      BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
786 //      ui_graphics_to_window_rct(sa->win, &dispf, &ri->disprect);
787
788         /* correction for gla draw */
789 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
790         
791         /* when panel scale changed, free rect */
792         if(ri->disprect.xmax-ri->disprect.xmin != ri->pr_rectx ||
793            ri->disprect.ymax-ri->disprect.ymin != ri->pr_recty) {
794                 MEM_freeN(ri->rect);
795                 ri->rect= NULL;
796         }
797         else {
798 //              glaDefine2DArea(&sa->winrct);
799                 glaDrawPixelsSafe(ri->disprect.xmin, ri->disprect.ymin, ri->pr_rectx, ri->pr_recty, ri->pr_rectx, GL_RGBA, GL_UNSIGNED_BYTE, ri->rect);
800         }       
801 }
802
803 /* is panel callback, supposed to be called with correct panel offset matrix */
804 void BIF_view3d_previewdraw(struct ScrArea *sa, struct uiBlock *block)
805 {
806         RegionView3D *rv3d= NULL;
807
808 //      if (v3d->ri==NULL || v3d->ri->rect==NULL) 
809 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
810 //      else {
811                 view3d_previewdraw_rect(sa, block, rv3d->ri);
812 //              if(v3d->ri->curtile==0) 
813 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
814 //      }
815 }
816
817
818 /* **************************** New preview system ****************** */
819
820 /* inside thread, called by renderer, sets job update value */
821 static void shader_preview_draw(void *spv, RenderResult *rr, volatile struct rcti *rect)
822 {
823         ShaderPreview *sp= spv;
824         
825         *(sp->do_update)= 1;
826 }
827
828 /* called by renderer, checks job value */
829 static int shader_preview_break(void *spv)
830 {
831         ShaderPreview *sp= spv;
832         
833         return *(sp->stop);
834 }
835
836 /* outside thread, called before redraw notifiers, it moves finished preview over */
837 static void shader_preview_updatejob(void *spv)
838 {
839 //      ShaderPreview *sp= spv;
840         
841 }
842
843 static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
844 {
845         Render *re;
846         Scene *sce;
847         float oldlens;
848         char name[32];
849         int sizex;
850
851         /* get the stuff from the builtin preview dbase */
852         sce= preview_prepare_scene(sp->scene, id, GS(id->name), sp); // XXX sizex
853         if(sce==NULL) return;
854         
855         if(!split || first) sprintf(name, "Preview %p", sp->owner);
856         else sprintf(name, "SecondPreview %p", sp->owner);
857         re= RE_GetRender(name);
858         
859         /* full refreshed render from first tile */
860         if(re==NULL)
861                 re= RE_NewRender(name);
862                 
863         /* sce->r gets copied in RE_InitState! */
864         if(sp->pr_method==PR_DO_RENDER) {
865                 sce->r.scemode |= R_NODE_PREVIEW;
866                 sce->r.scemode &= ~R_NO_IMAGE_LOAD;
867                 sce->r.mode |= R_OSA;
868         }
869         else {  /* PR_ICON_RENDER */
870                 sce->r.scemode &= ~R_NODE_PREVIEW;
871                 sce->r.scemode |= R_NO_IMAGE_LOAD;
872         }
873
874         /* in case of split preview, use border render */
875         if(split) {
876                 if(first) sizex= sp->sizex/2;
877                 else sizex= sp->sizex - sp->sizex/2;
878         }
879         else sizex= sp->sizex;
880
881         /* allocates or re-uses render result */
882         RE_InitState(re, NULL, &sce->r, sizex, sp->sizey, NULL);
883
884         /* callbacs are cleared on GetRender() */
885         if(sp->pr_method==PR_DO_RENDER) {
886                 RE_display_draw_cb(re, sp, shader_preview_draw);
887                 RE_test_break_cb(re, sp, shader_preview_break);
888         }
889         /* lens adjust */
890         oldlens= ((Camera *)sce->camera->data)->lens;
891         if(sizex > sp->sizey)
892                 ((Camera *)sce->camera->data)->lens *= (float)sp->sizey/(float)sizex;
893
894         /* entire cycle for render engine */
895         RE_SetCamera(re, sce->camera);
896         RE_Database_FromScene(re, sce, 1);
897         RE_TileProcessor(re, 0, 1);     // actual render engine
898         RE_Database_Free(re);
899
900         ((Camera *)sce->camera->data)->lens= oldlens;
901
902         /* handle results */
903         if(sp->pr_method==PR_ICON_RENDER) {
904                 if(sp->pr_rect)
905                         RE_ResultGet32(re, (unsigned int *)sp->pr_rect);
906         }
907         else {
908                 /* validate owner */
909                 //if(ri->rect==NULL)
910                 //      ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
911                 //RE_ResultGet32(re, ri->rect);
912         }
913
914         /* unassign the pointers, reset vars */
915         preview_prepare_scene(sp->scene, NULL, GS(id->name), NULL);
916 }
917
918 /* runs inside thread for material, in foreground for icons */
919 static void shader_preview_startjob(void *customdata, short *stop, short *do_update)
920 {
921         ShaderPreview *sp= customdata;
922
923         sp->stop= stop;
924         sp->do_update= do_update;
925
926         if(sp->parent) {
927                 shader_preview_render(sp, sp->parent, 1, 1);
928                 shader_preview_render(sp, sp->id, 1, 0);
929         }
930         else
931                 shader_preview_render(sp, sp->id, 0, 0);
932
933         *do_update= 1;
934 }
935
936 static void shader_preview_free(void *customdata)
937 {
938         ShaderPreview *sp= customdata;
939         
940         MEM_freeN(sp);
941 }
942
943 void ED_preview_shader_job(const bContext *C, void *owner, ID *id, ID *parent, MTex *slot, int sizex, int sizey)
944 {
945         wmJob *steve;
946         ShaderPreview *sp;
947
948         /* XXX ugly global still, but we can't do preview while rendering */
949         if(G.rendering)
950                 return;
951         
952         if(GS(id->name) == ID_TE) {
953                 ntreeTexSetPreviewFlag(1);
954         }
955         
956         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner);
957         sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
958
959         /* customdata for preview thread */
960         sp->scene= CTX_data_scene(C);
961         sp->owner= owner;
962         sp->sizex= sizex;
963         sp->sizey= sizey;
964         sp->pr_method= PR_DO_RENDER;
965         sp->id = id;
966         sp->parent= parent;
967         sp->slot= slot;
968         
969         /* setup job */
970         WM_jobs_customdata(steve, sp, shader_preview_free);
971         WM_jobs_timer(steve, 0.1, NC_MATERIAL, NC_MATERIAL);
972         WM_jobs_callbacks(steve, shader_preview_startjob, NULL, shader_preview_updatejob);
973         
974         WM_jobs_start(CTX_wm_manager(C), steve);
975         
976         /* signal to rerender icon in menus */
977         BKE_icon_changed(BKE_icon_getid(id));
978 }
979
980 /* rect should be allocated, sizex/sizy pixels, 32 bits */
981 void ED_preview_iconrender(Scene *scene, ID *id, unsigned int *rect, int sizex, int sizey)
982 {
983         ShaderPreview *sp;
984         short stop=0, do_update=0;
985
986         sp= MEM_callocN(sizeof(ShaderPreview), "ShaderPreview");
987         
988         /* customdata for preview thread */
989         sp->scene= scene;
990         sp->sizex= sizex;
991         sp->sizey= sizey;
992         sp->pr_method= PR_ICON_RENDER;
993         sp->pr_rect= (int *)rect; /* shouldnt pr_rect be unsigned int also? - Campbell */
994         sp->id = id;
995
996         shader_preview_startjob(sp, &stop, &do_update);
997         
998         MEM_freeN(sp);
999 }
1000
1001
1002