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