542ab38a50e324d0540d94db9231814da3c2c050
[profile/ivi/mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2  * 
3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28
29 /**
30  * Framebuffer/renderbuffer functions.
31  *
32  * \author Brian Paul
33  */
34
35
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/fbobject.h"
39 #include "main/framebuffer.h"
40 #include "main/macros.h"
41 #include "main/renderbuffer.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_screen.h"
46 #include "st_context.h"
47 #include "st_cb_fbo.h"
48 #include "st_format.h"
49 #include "st_public.h"
50 #include "st_texture.h"
51 #include "st_manager.h"
52
53 #include "util/u_format.h"
54 #include "util/u_inlines.h"
55
56
57 /**
58  * gl_renderbuffer::AllocStorage()
59  * This is called to allocate the original drawing surface, and
60  * during window resize.
61  */
62 static GLboolean
63 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
64                               GLenum internalFormat,
65                               GLuint width, GLuint height)
66 {
67    struct pipe_screen *screen = ctx->st->pipe->screen;
68    struct st_renderbuffer *strb = st_renderbuffer(rb);
69    enum pipe_format format;
70
71    if (strb->format != PIPE_FORMAT_NONE)
72       format = strb->format;
73    else
74       format = st_choose_renderbuffer_format(screen, internalFormat);
75       
76    /* init renderbuffer fields */
77    strb->Base.Width  = width;
78    strb->Base.Height = height;
79    strb->Base.Format = st_pipe_format_to_mesa_format(format);
80    strb->Base.DataType = st_format_datatype(format);
81
82    strb->defined = GL_FALSE;  /* undefined contents now */
83
84    if (strb->software) {
85       size_t size;
86       
87       free(strb->data);
88
89       assert(strb->format != PIPE_FORMAT_NONE);
90       
91       strb->stride = util_format_get_stride(strb->format, width);
92       size = util_format_get_2d_size(strb->format, strb->stride, height);
93       
94       strb->data = malloc(size);
95       
96       return strb->data != NULL;
97    }
98    else {
99       struct pipe_texture template;
100       unsigned surface_usage;
101     
102       /* Free the old surface and texture
103        */
104       pipe_surface_reference( &strb->surface, NULL );
105       pipe_texture_reference( &strb->texture, NULL );
106       pipe_sampler_view_reference(&strb->sampler_view, NULL);
107
108       /* Setup new texture template.
109        */
110       memset(&template, 0, sizeof(template));
111       template.target = PIPE_TEXTURE_2D;
112       template.format = format;
113       template.width0 = width;
114       template.height0 = height;
115       template.depth0 = 1;
116       template.last_level = 0;
117       template.nr_samples = rb->NumSamples;
118       if (util_format_is_depth_or_stencil(format)) {
119          template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
120       }
121       else {
122          template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
123                                PIPE_TEXTURE_USAGE_RENDER_TARGET);
124       }
125
126       /* Probably need dedicated flags for surface usage too: 
127        */
128       surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
129                        PIPE_BUFFER_USAGE_GPU_WRITE);
130 #if 0
131                        PIPE_BUFFER_USAGE_CPU_READ |
132                        PIPE_BUFFER_USAGE_CPU_WRITE);
133 #endif
134
135       strb->texture = screen->texture_create(screen, &template);
136
137       if (!strb->texture) 
138          return FALSE;
139
140       strb->surface = screen->get_tex_surface(screen,
141                                               strb->texture,
142                                               0, 0, 0,
143                                               surface_usage);
144       if (strb->surface) {
145          assert(strb->surface->texture);
146          assert(strb->surface->format);
147          assert(strb->surface->width == width);
148          assert(strb->surface->height == height);
149       }
150
151       return strb->surface != NULL;
152    }
153 }
154
155
156 /**
157  * gl_renderbuffer::Delete()
158  */
159 static void
160 st_renderbuffer_delete(struct gl_renderbuffer *rb)
161 {
162    struct st_renderbuffer *strb = st_renderbuffer(rb);
163    ASSERT(strb);
164    pipe_surface_reference(&strb->surface, NULL);
165    pipe_texture_reference(&strb->texture, NULL);
166    pipe_sampler_view_reference(&strb->sampler_view, NULL);
167    free(strb->data);
168    free(strb);
169 }
170
171
172 /**
173  * gl_renderbuffer::GetPointer()
174  */
175 static void *
176 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
177                  GLint x, GLint y)
178 {
179    /* By returning NULL we force all software rendering to go through
180     * the span routines.
181     */
182 #if 0
183    assert(0);  /* Should never get called with softpipe */
184 #endif
185    return NULL;
186 }
187
188
189 /**
190  * Called via ctx->Driver.NewFramebuffer()
191  */
192 static struct gl_framebuffer *
193 st_new_framebuffer(GLcontext *ctx, GLuint name)
194 {
195    /* XXX not sure we need to subclass gl_framebuffer for pipe */
196    return _mesa_new_framebuffer(ctx, name);
197 }
198
199
200 /**
201  * Called via ctx->Driver.NewRenderbuffer()
202  */
203 static struct gl_renderbuffer *
204 st_new_renderbuffer(GLcontext *ctx, GLuint name)
205 {
206    struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
207    if (strb) {
208       _mesa_init_renderbuffer(&strb->Base, name);
209       strb->Base.Delete = st_renderbuffer_delete;
210       strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
211       strb->Base.GetPointer = null_get_pointer;
212       strb->format = PIPE_FORMAT_NONE;
213       return &strb->Base;
214    }
215    return NULL;
216 }
217
218
219 /**
220  * Allocate a renderbuffer for a an on-screen window (not a user-created
221  * renderbuffer).  The window system code determines the format.
222  */
223 struct gl_renderbuffer *
224 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
225 {
226    struct st_renderbuffer *strb;
227
228    strb = ST_CALLOC_STRUCT(st_renderbuffer);
229    if (!strb) {
230       _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
231       return NULL;
232    }
233
234    _mesa_init_renderbuffer(&strb->Base, 0);
235    strb->Base.ClassID = 0x4242; /* just a unique value */
236    strb->Base.NumSamples = samples;
237    strb->Base.Format = st_pipe_format_to_mesa_format(format);
238    strb->Base.DataType = st_format_datatype(format);
239    strb->format = format;
240    strb->software = sw;
241    
242    switch (format) {
243    case PIPE_FORMAT_B8G8R8A8_UNORM:
244    case PIPE_FORMAT_A8R8G8B8_UNORM:
245    case PIPE_FORMAT_B8G8R8X8_UNORM:
246    case PIPE_FORMAT_X8R8G8B8_UNORM:
247    case PIPE_FORMAT_B5G5R5A1_UNORM:
248    case PIPE_FORMAT_B4G4R4A4_UNORM:
249    case PIPE_FORMAT_B5G6R5_UNORM:
250       strb->Base.InternalFormat = GL_RGBA;
251       break;
252    case PIPE_FORMAT_Z16_UNORM:
253       strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
254       break;
255    case PIPE_FORMAT_Z32_UNORM:
256       strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
257       break;
258    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
259    case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
260    case PIPE_FORMAT_Z24X8_UNORM:
261    case PIPE_FORMAT_X8Z24_UNORM:
262       strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
263       break;
264    case PIPE_FORMAT_S8_USCALED:
265       strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
266       break;
267    case PIPE_FORMAT_R16G16B16A16_SNORM:
268       strb->Base.InternalFormat = GL_RGBA16;
269       break;
270    default:
271       _mesa_problem(NULL,
272                     "Unexpected format in st_new_renderbuffer_fb");
273       free(strb);
274       return NULL;
275    }
276
277    /* st-specific methods */
278    strb->Base.Delete = st_renderbuffer_delete;
279    strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
280    strb->Base.GetPointer = null_get_pointer;
281
282    /* surface is allocated in st_renderbuffer_alloc_storage() */
283    strb->surface = NULL;
284
285    return &strb->Base;
286 }
287
288
289
290
291 /**
292  * Called via ctx->Driver.BindFramebufferEXT().
293  */
294 static void
295 st_bind_framebuffer(GLcontext *ctx, GLenum target,
296                     struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
297 {
298
299 }
300
301 /**
302  * Called by ctx->Driver.FramebufferRenderbuffer
303  */
304 static void
305 st_framebuffer_renderbuffer(GLcontext *ctx, 
306                             struct gl_framebuffer *fb,
307                             GLenum attachment,
308                             struct gl_renderbuffer *rb)
309 {
310    /* XXX no need for derivation? */
311    _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
312 }
313
314
315 /**
316  * Called by ctx->Driver.RenderTexture
317  */
318 static void
319 st_render_texture(GLcontext *ctx,
320                   struct gl_framebuffer *fb,
321                   struct gl_renderbuffer_attachment *att)
322 {
323    struct pipe_screen *screen = ctx->st->pipe->screen;
324    struct st_renderbuffer *strb;
325    struct gl_renderbuffer *rb;
326    struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
327    struct st_texture_object *stObj;
328    const struct gl_texture_image *texImage;
329    GLint pt_level;
330
331    /* When would this fail?  Perhaps assert? */
332    if (!pt) 
333       return;
334
335    /* The first gallium texture level = Mesa BaseLevel */
336    pt_level = MAX2(0, (GLint) att->TextureLevel - att->Texture->BaseLevel);
337    texImage = att->Texture->Image[att->CubeMapFace][pt_level];
338
339    /* create new renderbuffer which wraps the texture image */
340    rb = st_new_renderbuffer(ctx, 0);
341    if (!rb) {
342       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
343       return;
344    }
345
346    _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
347    assert(rb->RefCount == 1);
348    rb->AllocStorage = NULL; /* should not get called */
349    strb = st_renderbuffer(rb);
350
351    assert(strb->Base.RefCount > 0);
352
353    /* get the texture for the texture object */
354    stObj = st_texture_object(att->Texture);
355
356    /* point renderbuffer at texobject */
357    strb->rtt = stObj;
358    strb->rtt_level = pt_level;
359    strb->rtt_face = att->CubeMapFace;
360    strb->rtt_slice = att->Zoffset;
361
362    rb->Width = texImage->Width2;
363    rb->Height = texImage->Height2;
364    rb->_BaseFormat = texImage->_BaseFormat;
365    /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
366
367    /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
368
369    pipe_texture_reference( &strb->texture, pt );
370
371    pipe_surface_reference(&strb->surface, NULL);
372
373    pipe_sampler_view_reference(&strb->sampler_view, st_get_stobj_sampler_view(stObj));
374
375    assert(strb->rtt_level <= strb->texture->last_level);
376
377    /* new surface for rendering into the texture */
378    strb->surface = screen->get_tex_surface(screen,
379                                            strb->texture,
380                                            strb->rtt_face,
381                                            strb->rtt_level,
382                                            strb->rtt_slice,
383                                            PIPE_BUFFER_USAGE_GPU_READ |
384                                            PIPE_BUFFER_USAGE_GPU_WRITE);
385
386    strb->format = pt->format;
387
388    strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
389    strb->Base.DataType = st_format_datatype(pt->format);
390
391    /*
392    printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p  %d x %d\n",
393           att->Texture, pt, strb->surface, rb->Width, rb->Height);
394    */
395
396    /* Invalidate buffer state so that the pipe's framebuffer state
397     * gets updated.
398     * That's where the new renderbuffer (which we just created) gets
399     * passed to the pipe as a (color/depth) render target.
400     */
401    st_invalidate_state(ctx, _NEW_BUFFERS);
402 }
403
404
405 /**
406  * Called via ctx->Driver.FinishRenderTexture.
407  */
408 static void
409 st_finish_render_texture(GLcontext *ctx,
410                          struct gl_renderbuffer_attachment *att)
411 {
412    struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
413
414    if (!strb)
415       return;
416
417    st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
418
419    strb->rtt = NULL;
420
421    /*
422    printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
423    */
424
425    /* restore previous framebuffer state */
426    st_invalidate_state(ctx, _NEW_BUFFERS);
427 }
428
429
430 /**
431  * Validate a renderbuffer attachment for a particular usage.
432  */
433
434 static GLboolean
435 st_validate_attachment(struct pipe_screen *screen,
436                        const struct gl_renderbuffer_attachment *att,
437                        GLuint usage)
438 {
439    const struct st_texture_object *stObj =
440       st_texture_object(att->Texture);
441
442    /**
443     * Only validate texture attachments for now, since
444     * st_renderbuffer_alloc_storage makes sure that
445     * the format is supported.
446     */
447
448    if (att->Type != GL_TEXTURE)
449       return GL_TRUE;
450
451    if (!stObj)
452       return GL_FALSE;
453
454    return screen->is_format_supported(screen, stObj->pt->format,
455                                       PIPE_TEXTURE_2D,
456                                       usage, 0);
457 }
458
459 /**
460  * Check that the framebuffer configuration is valid in terms of what
461  * the driver can support.
462  *
463  * For Gallium we only supports combined Z+stencil, not separate buffers.
464  */
465 static void
466 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
467 {
468    struct pipe_screen *screen = ctx->st->pipe->screen;
469    const struct gl_renderbuffer *depthRb =
470       fb->Attachment[BUFFER_DEPTH].Renderbuffer;
471    const struct gl_renderbuffer *stencilRb =
472       fb->Attachment[BUFFER_STENCIL].Renderbuffer;
473    GLuint i;
474
475    if (stencilRb && depthRb && stencilRb != depthRb) {
476       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
477       return;
478    }
479
480    if (!st_validate_attachment(screen,
481                                &fb->Attachment[BUFFER_DEPTH],
482                                PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
483       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
484       return;
485    }
486    if (!st_validate_attachment(screen,
487                                &fb->Attachment[BUFFER_STENCIL],
488                                PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
489       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
490       return;
491    }
492    for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
493       if (!st_validate_attachment(screen,
494                                   &fb->Attachment[BUFFER_COLOR0 + i],
495                                   PIPE_TEXTURE_USAGE_RENDER_TARGET)) {
496          fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
497          return;
498       }
499    }
500 }
501
502
503 /**
504  * Copy back color buffer to front color buffer.
505  */
506 static void
507 copy_back_to_front(struct st_context *st,
508                    struct gl_framebuffer *fb,
509                    gl_buffer_index frontIndex,
510                    gl_buffer_index backIndex)
511
512 {
513    struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
514    struct pipe_surface *surf_front, *surf_back;
515
516    (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
517    (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
518
519    if (surf_front && surf_back) {
520       st->pipe->surface_copy(st->pipe,
521                              surf_front, 0, 0,  /* dest */
522                              surf_back, 0, 0,   /* src */
523                              fb->Width, fb->Height);
524    }
525 }
526
527
528 /**
529  * Check if we're drawing into, or read from, a front color buffer.  If the
530  * front buffer is missing, create it now.
531  *
532  * The back color buffer must exist since we'll use its format/samples info
533  * for creating the front buffer.
534  *
535  * \param frontIndex  either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
536  * \param backIndex  either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
537  */
538 static void
539 check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
540                           gl_buffer_index frontIndex,
541                           gl_buffer_index backIndex)
542 {
543    if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
544       GLboolean create = GL_FALSE;
545
546       /* check if drawing to or reading from front buffer */
547       if (fb->_ColorReadBufferIndex == frontIndex) {
548          create = GL_TRUE;
549       }
550       else {
551          GLuint b;
552          for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
553             if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
554                create = GL_TRUE;
555                break;
556             }
557          }
558       }
559
560       if (create) {
561          struct st_renderbuffer *back;
562          struct gl_renderbuffer *front;
563          enum pipe_format colorFormat;
564          uint samples;
565
566          if (0)
567             _mesa_debug(ctx, "Allocate new front buffer\n");
568
569          /* get back renderbuffer info */
570          back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
571          colorFormat = back->format;
572          samples = back->Base.NumSamples;
573
574          /* create front renderbuffer */
575          front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
576          _mesa_add_renderbuffer(fb, frontIndex, front);
577
578          /* alloc texture/surface for new front buffer */
579          front->AllocStorage(ctx, front, front->InternalFormat,
580                              fb->Width, fb->Height);
581
582          /* initialize the front color buffer contents by copying
583           * the back buffer.
584           */
585          copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
586       }
587    }
588 }
589
590
591 /**
592  * If front left/right color buffers are missing, create them now.
593  */
594 static void
595 check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
596 {
597    /* check if we need to create the front left buffer now */
598    check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
599
600    if (fb->Visual.stereoMode) {
601       check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
602    }
603
604    st_invalidate_state(ctx, _NEW_BUFFERS);
605 }
606
607
608 /**
609  * Called via glDrawBuffer.
610  */
611 static void
612 st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
613 {
614    GLframebuffer *fb = ctx->DrawBuffer;
615    GLuint i;
616
617    (void) count;
618    (void) buffers;
619
620    /* add the renderbuffers on demand */
621    for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
622       gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
623       st_manager_add_color_renderbuffer(ctx->st, fb, idx);
624    }
625
626    check_create_front_buffers(ctx, ctx->DrawBuffer);
627 }
628
629
630 /**
631  * Called via glReadBuffer.
632  */
633 static void
634 st_ReadBuffer(GLcontext *ctx, GLenum buffer)
635 {
636    GLframebuffer *fb = ctx->ReadBuffer;
637
638    (void) buffer;
639
640    /* add the renderbuffer on demand */
641    st_manager_add_color_renderbuffer(ctx->st, fb, fb->_ColorReadBufferIndex);
642    check_create_front_buffers(ctx, fb);
643 }
644
645
646 void st_init_fbo_functions(struct dd_function_table *functions)
647 {
648    functions->NewFramebuffer = st_new_framebuffer;
649    functions->NewRenderbuffer = st_new_renderbuffer;
650    functions->BindFramebuffer = st_bind_framebuffer;
651    functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
652    functions->RenderTexture = st_render_texture;
653    functions->FinishRenderTexture = st_finish_render_texture;
654    functions->ValidateFramebuffer = st_validate_framebuffer;
655    /* no longer needed by core Mesa, drivers handle resizes...
656    functions->ResizeBuffers = st_resize_buffers;
657    */
658
659    functions->DrawBuffers = st_DrawBuffers;
660    functions->ReadBuffer = st_ReadBuffer;
661 }
662
663 struct pipe_sampler_view *
664 st_renderbuffer_get_sampler_view(struct st_renderbuffer *rb,
665                                  struct pipe_context *pipe)
666 {
667    if (!rb->sampler_view) {
668       rb->sampler_view = st_sampler_view_from_texture(pipe, rb->texture);
669    }
670
671    return rb->sampler_view;
672 }