2.5:
[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                 if(parent) {
476                         ok = ed_preview_draw_rect(sa, sce, parent, 1, 1, rect, &newrect);
477                         ok &= ed_preview_draw_rect(sa, sce, id, 1, 0, rect, &newrect);
478                 }
479                 else
480                         ok = ed_preview_draw_rect(sa, sce, id, 0, 0, rect, &newrect);
481
482                 if(ok)
483                         *rect= newrect;
484
485                 /* check for spacetype... */
486                 if(sbuts->spacetype==SPACE_BUTS && sbuts->preview) {
487                         sbuts->preview= 0;
488                         ok= 0;
489                 }
490                 
491                 if(ok==0) {
492                         ED_preview_shader_job(C, sa, id, parent, newx, newy);
493                 }
494         }       
495 }
496
497 /* *************************** Preview for 3d window *********************** */
498
499 void view3d_previewrender_progress(RenderResult *rr, volatile rcti *renrect)
500 {
501 //      ScrArea *sa= NULL; // XXX
502 //      View3D *v3d= NULL; // XXX
503         RenderLayer *rl;
504         int ofsx=0, ofsy=0;
505         
506         if(renrect) return;
507         
508         rl= rr->layers.first;
509         
510         /* this case is when we render envmaps... */
511 //      if(rr->rectx > v3d->ri->pr_rectx || rr->recty > v3d->ri->pr_recty)
512 //              return;
513         
514 //      ofsx= v3d->ri->disprect.xmin + rr->tilerect.xmin;
515 //      ofsy= v3d->ri->disprect.ymin + rr->tilerect.ymin;
516         
517         glDrawBuffer(GL_FRONT);
518 //      glaDefine2DArea(&sa->winrct);
519         glaDrawPixelsSafe_to32(ofsx, ofsy, rr->rectx, rr->recty, rr->rectx, rl->rectf, 0);
520         bglFlush();
521         glDrawBuffer(GL_BACK);
522
523 }
524
525 void BIF_view3d_previewrender_signal(ScrArea *sa, short signal)
526 {
527 #if 0
528         View3D *v3d= sa->spacedata.first;
529         
530         /* this can be called from other window... solve! */
531         if(sa->spacetype!=SPACE_VIEW3D)
532                 return; // XXX
533            
534         if(v3d && v3d->ri) {
535                 RenderInfo *ri= v3d->ri;
536                 ri->status &= ~signal;
537                 ri->curtile= 0;
538                 //printf("preview signal %d\n", signal);
539                 if(ri->re && (signal & PR_DBASE))
540                         RE_Database_Free(ri->re);
541
542 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
543         }
544 #endif
545 }
546
547 void BIF_view3d_previewrender_free(View3D *v3d)
548 {
549 #if 0
550         if(v3d->ri) {
551                 RenderInfo *ri= v3d->ri;
552                 if(ri->re) {
553 //                      printf("free render\n");
554                         RE_Database_Free(ri->re);
555                         RE_FreeRender(ri->re);
556                         ri->re= NULL;
557                 }
558                 if (v3d->ri->rect) MEM_freeN(v3d->ri->rect);
559                 MEM_freeN(v3d->ri);
560                 v3d->ri= NULL;
561         }
562 #endif
563 }
564
565 /* returns 1 if OK, do not call while in panel space!  */
566 static int view3d_previewrender_get_rects(ScrArea *sa, rctf *viewplane, RenderInfo *ri, float *clipsta, float *clipend, int *ortho, float *pixsize)
567 {
568         View3D *v3d= NULL; // XXX
569         RegionView3D *rv3d= NULL; // XXX
570         int rectx, recty;
571 //      uiBlock *block;
572         
573 //      block= uiFindOpenPanelBlockName(&sa->uiblocks, "Preview");
574 //      if(block==NULL) return 0;
575         
576         /* calculate preview rect size */
577 //      BLI_init_rctf(viewplane, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
578 //      uiPanelPush(block);
579 //      ui_graphics_to_window_rct(sa->win, viewplane, &ri->disprect);
580 //      uiPanelPop(block);
581         
582         /* correction for gla draw */
583 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
584         
585         *ortho= get_view3d_viewplane(v3d, rv3d, sa->winx, sa->winy, viewplane, clipsta, clipend, pixsize);
586
587         rectx= ri->disprect.xmax - ri->disprect.xmin;
588         recty= ri->disprect.ymax - ri->disprect.ymin;
589         
590         if(rectx<4 || recty<4) return 0;
591         
592         if(ri->rect && (rectx!=ri->pr_rectx || recty!=ri->pr_recty)) {
593                 MEM_freeN(ri->rect);
594                 ri->rect= NULL;
595                 ri->curtile= 0;
596                 printf("changed size\n");
597         }
598         ri->pr_rectx= rectx;
599         ri->pr_recty= recty;
600         
601         return 1;
602 }
603
604 /* called before a panel gets moved/scaled, makes sure we can see through */
605 void BIF_view3d_previewrender_clear(ScrArea *sa)
606 {
607 #if 0
608         View3D *v3d= sa->spacedata.first;
609
610         if(v3d->ri) {
611                 RenderInfo *ri= v3d->ri;
612                 ri->curtile= 0;
613                 if(ri->rect)
614                         MEM_freeN(ri->rect);
615                 ri->rect= NULL;
616         }
617 #endif
618 }
619
620 /* afterqueue call */
621 void BIF_view3d_previewrender(Scene *scene, ScrArea *sa)
622 {
623         bContext *C= NULL;
624         View3D *v3d= sa->spacedata.first;
625         RegionView3D *rv3d= NULL; // XXX
626         Render *re;
627         RenderInfo *ri=NULL;    /* preview struct! */
628         RenderStats *rstats;
629         RenderData rdata;
630         rctf viewplane;
631         float clipsta, clipend, pixsize;
632         int orth;
633         
634         /* first get the render info right */
635 //      if (!v3d->ri) {
636 //              ri= v3d->ri= MEM_callocN(sizeof(RenderInfo), "butsrenderinfo");
637 //              ri->tottile= 10000;
638 //      }
639 //      ri= v3d->ri;
640         
641         if(0==view3d_previewrender_get_rects(sa, &viewplane, ri, &clipsta, &clipend, &orth, &pixsize))
642                 return;
643         
644         /* render is finished, so return */
645         if(ri->tottile && ri->curtile>=ri->tottile) return;
646
647         /* or return with a new event */
648         if(qtest()) {
649 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
650                 return;
651         }
652         //printf("Enter previewrender\n");
653         /* ok, are we rendering all over? */
654         if(ri->re==NULL) {
655                 char name[32];
656                 
657                 ri->status= 0;
658                 
659                 sprintf(name, "View3dPreview %p", sa);
660                 re= ri->re= RE_NewRender(name);
661                 //RE_display_draw_cb(re, view3d_previewrender_progress);
662                 //RE_stats_draw_cb(re, view3d_previewrender_stats);
663                 //RE_test_break_cb(re, qtest);
664                 
665                 /* no osa, blur, seq, layers, etc for preview render */
666                 rdata= scene->r;
667                 rdata.mode &= ~(R_OSA|R_MBLUR);
668                 rdata.scemode &= ~(R_DOSEQ|R_DOCOMP|R_FREE_IMAGE);
669                 rdata.layers.first= rdata.layers.last= NULL;
670                 rdata.renderer= R_INTERN;
671                  
672                 RE_InitState(re, NULL, &rdata, sa->winx, sa->winy, &ri->disprect);
673         
674                 if(orth)
675                         RE_SetOrtho(re, &viewplane, clipsta, clipend);
676                 else
677                         RE_SetWindow(re, &viewplane, clipsta, clipend);
678                 RE_SetPixelSize(re, pixsize);
679                 
680                 /* until here are no escapes */
681                 ri->status |= PR_DISPRECT;
682                 ri->curtile= 0;
683                 //printf("new render\n");
684         }
685
686         re= ri->re;
687         
688         PIL_sleep_ms(100);      /* wait 0.1 second if theres really no event... */
689         if(qtest()==0)  {
690                 
691                 /* check status */
692                 if((ri->status & PR_DISPRECT)==0) {
693                         RE_SetDispRect(ri->re, &ri->disprect);
694                         if(orth)
695                                 RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
696                         else
697                                 RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
698                         RE_SetPixelSize(re, pixsize);
699                         ri->status |= PR_DISPRECT;
700                         ri->curtile= 0;
701                         //printf("disprect update\n");
702                 }
703                 if((ri->status & PR_DBASE)==0) {
704                         unsigned int lay= scene->lay;
705                         
706                         RE_SetView(re, rv3d->viewmat);
707                         
708                         /* allow localview render for objects with lights in normal layers */
709                         if(v3d->lay & 0xFF000000)
710                                 scene->lay |= v3d->lay;
711                         else scene->lay= v3d->lay;
712                         
713                         RE_Database_FromScene(re, scene, 0);            // 0= dont use camera view
714                         scene->lay= lay;
715                         
716                         rstats= RE_GetStats(re);
717                         if(rstats->convertdone) 
718                                 ri->status |= PR_DBASE|PR_PROJECTED|PR_ROTATED;
719                         ri->curtile= 0;
720                         
721                         /* database can have created render-resol data... */
722                         if(rstats->convertdone) 
723                                 ED_anim_dag_flush_update(C); // <--- only current scene XXX
724                         
725                         //printf("dbase update\n");
726                 }
727                 if((ri->status & PR_PROJECTED)==0) {
728                         if(ri->status & PR_DBASE) {
729                                 if(orth)
730                                         RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
731                                 else
732                                         RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
733                                 RE_DataBase_ApplyWindow(re);
734                                 ri->status |= PR_PROJECTED;
735                         }
736                         ri->curtile= 0;
737                         //printf("project update\n");
738                 }
739         
740                 /* OK, can we enter render code? */
741                 if(ri->status==(PR_DISPRECT|PR_DBASE|PR_PROJECTED|PR_ROTATED)) {
742                         //printf("curtile %d tottile %d\n", ri->curtile, ri->tottile);
743                         RE_TileProcessor(ri->re, ri->curtile, 0);
744         
745                         if(ri->rect==NULL)
746                                 ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "preview view3d rect");
747                         
748                         RE_ResultGet32(ri->re, ri->rect);
749                 }
750                 
751                 rstats= RE_GetStats(ri->re);
752 //              if(rstats->totpart==rstats->partsdone && rstats->partsdone)
753 //                      addqueue(sa->win, REDRAW, 1);
754 //              else
755 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
756                 
757                 ri->curtile= rstats->partsdone;
758                 ri->tottile= rstats->totpart;
759         }
760         else {
761 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
762         }
763         
764         //printf("\n");
765 }
766
767 /* in panel space! */
768 static void view3d_previewdraw_rect(ScrArea *sa, uiBlock *block, RenderInfo *ri)
769 {
770 //      rctf dispf;
771         
772         if(ri->rect==NULL)
773                 return;
774         
775 //      BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
776 //      ui_graphics_to_window_rct(sa->win, &dispf, &ri->disprect);
777
778         /* correction for gla draw */
779 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
780         
781         /* when panel scale changed, free rect */
782         if(ri->disprect.xmax-ri->disprect.xmin != ri->pr_rectx ||
783            ri->disprect.ymax-ri->disprect.ymin != ri->pr_recty) {
784                 MEM_freeN(ri->rect);
785                 ri->rect= NULL;
786         }
787         else {
788 //              glaDefine2DArea(&sa->winrct);
789                 glaDrawPixelsSafe(ri->disprect.xmin, ri->disprect.ymin, ri->pr_rectx, ri->pr_recty, ri->pr_rectx, GL_RGBA, GL_UNSIGNED_BYTE, ri->rect);
790         }       
791 }
792
793 /* is panel callback, supposed to be called with correct panel offset matrix */
794 void BIF_view3d_previewdraw(struct ScrArea *sa, struct uiBlock *block)
795 {
796         RegionView3D *rv3d= NULL;
797
798 //      if (v3d->ri==NULL || v3d->ri->rect==NULL) 
799 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
800 //      else {
801                 view3d_previewdraw_rect(sa, block, rv3d->ri);
802 //              if(v3d->ri->curtile==0) 
803 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
804 //      }
805 }
806
807
808 /* **************************** New preview system ****************** */
809
810 /* inside thread, called by renderer, sets job update value */
811 static void shader_preview_draw(void *spv, RenderResult *rr, volatile struct rcti *rect)
812 {
813         ShaderPreview *sp= spv;
814         
815         *(sp->do_update)= 1;
816 }
817
818 /* called by renderer, checks job value */
819 static int shader_preview_break(void *spv)
820 {
821         ShaderPreview *sp= spv;
822         
823         return *(sp->stop);
824 }
825
826 /* outside thread, called before redraw notifiers, it moves finished preview over */
827 static void shader_preview_updatejob(void *spv)
828 {
829 //      ShaderPreview *sp= spv;
830         
831 }
832
833 static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
834 {
835         Render *re;
836         Scene *sce;
837         float oldlens;
838         char name[32];
839         int sizex;
840
841         /* get the stuff from the builtin preview dbase */
842         sce= preview_prepare_scene(sp->scene, id, GS(id->name), sp); // XXX sizex
843         if(sce==NULL) return;
844         
845         if(!split || first) sprintf(name, "Preview %p", sp->owner);
846         else sprintf(name, "SecondPreview %p", sp->owner);
847         re= RE_GetRender(name);
848         
849         /* full refreshed render from first tile */
850         if(re==NULL)
851                 re= RE_NewRender(name);
852                 
853         /* sce->r gets copied in RE_InitState! */
854         if(sp->pr_method==PR_DO_RENDER) {
855                 sce->r.scemode |= R_NODE_PREVIEW;
856                 sce->r.scemode &= ~R_NO_IMAGE_LOAD;
857                 sce->r.mode |= R_OSA;
858         }
859         else {  /* PR_ICON_RENDER */
860                 sce->r.scemode &= ~R_NODE_PREVIEW;
861                 sce->r.scemode |= R_NO_IMAGE_LOAD;
862         }
863
864         /* in case of split preview, use border render */
865         if(split) {
866                 if(first) sizex= sp->sizex/2;
867                 else sizex= sp->sizex - sp->sizex/2;
868         }
869         else sizex= sp->sizex;
870
871         /* allocates or re-uses render result */
872         RE_InitState(re, NULL, &sce->r, sizex, sp->sizey, NULL);
873
874         /* callbacs are cleared on GetRender() */
875         if(sp->pr_method==PR_DO_RENDER) {
876                 RE_display_draw_cb(re, sp, shader_preview_draw);
877                 RE_test_break_cb(re, sp, shader_preview_break);
878         }
879         /* lens adjust */
880         oldlens= ((Camera *)sce->camera->data)->lens;
881         if(sizex > sp->sizey)
882                 ((Camera *)sce->camera->data)->lens *= (float)sp->sizey/(float)sizex;
883
884         /* entire cycle for render engine */
885         RE_SetCamera(re, sce->camera);
886         RE_Database_FromScene(re, sce, 1);
887         RE_TileProcessor(re, 0, 1);     // actual render engine
888         RE_Database_Free(re);
889
890         ((Camera *)sce->camera->data)->lens= oldlens;
891
892         /* handle results */
893         if(sp->pr_method==PR_ICON_RENDER) {
894                 if(sp->pr_rect)
895                         RE_ResultGet32(re, sp->pr_rect);
896         }
897         else {
898                 /* validate owner */
899                 //if(ri->rect==NULL)
900                 //      ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
901                 //RE_ResultGet32(re, ri->rect);
902         }
903
904         /* unassign the pointers, reset vars */
905         preview_prepare_scene(sp->scene, NULL, GS(id->name), NULL);
906 }
907
908 /* runs inside thread for material, in foreground for icons */
909 static void shader_preview_startjob(void *customdata, short *stop, short *do_update)
910 {
911         ShaderPreview *sp= customdata;
912
913         sp->stop= stop;
914         sp->do_update= do_update;
915
916         if(sp->parent) {
917                 shader_preview_render(sp, sp->parent, 1, 1);
918                 shader_preview_render(sp, sp->id, 1, 0);
919         }
920         else
921                 shader_preview_render(sp, sp->id, 0, 0);
922
923         *do_update= 1;
924 }
925
926 static void shader_preview_free(void *customdata)
927 {
928         ShaderPreview *sp= customdata;
929         
930         MEM_freeN(sp);
931 }
932
933 void ED_preview_shader_job(const bContext *C, void *owner, ID *id, ID *parent, int sizex, int sizey)
934 {
935         wmJob *steve;
936         ShaderPreview *sp;
937
938         /* XXX ugly global still, but we can't do preview while rendering */
939         if(G.rendering)
940                 return;
941
942         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner);
943         sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
944
945         /* customdata for preview thread */
946         sp->scene= CTX_data_scene(C);
947         sp->owner= owner;
948         sp->sizex= sizex;
949         sp->sizey= sizey;
950         sp->pr_method= PR_DO_RENDER;
951         sp->id = id;
952         sp->parent= parent;
953         
954         /* setup job */
955         WM_jobs_customdata(steve, sp, shader_preview_free);
956         WM_jobs_timer(steve, 0.1, NC_MATERIAL, NC_MATERIAL);
957         WM_jobs_callbacks(steve, shader_preview_startjob, NULL, shader_preview_updatejob);
958         
959         WM_jobs_start(CTX_wm_manager(C), steve);
960         
961         /* signal to rerender icon in menus */
962         BKE_icon_changed(BKE_icon_getid(id));
963 }
964
965 /* rect should be allocated, sizex/sizy pixels, 32 bits */
966 void ED_preview_iconrender(Scene *scene, ID *id, int *rect, int sizex, int sizey)
967 {
968         ShaderPreview *sp;
969         short stop=0, do_update=0;
970
971         sp= MEM_callocN(sizeof(ShaderPreview), "ShaderPreview");
972         
973         /* customdata for preview thread */
974         sp->scene= scene;
975         sp->sizex= sizex;
976         sp->sizey= sizey;
977         sp->pr_method= PR_ICON_RENDER;
978         sp->pr_rect= rect;
979         sp->id = id;
980
981         shader_preview_startjob(sp, &stop, &do_update);
982         
983         MEM_freeN(sp);
984 }
985
986
987