OSDN Git Service

a7c286bcc525b607a7996e6c77e3532efc3c51f5
[android-x86/external-mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
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 VMWARE 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/glformats.h"
41 #include "main/macros.h"
42 #include "main/renderbuffer.h"
43 #include "main/state.h"
44
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_screen.h"
48 #include "st_atom.h"
49 #include "st_context.h"
50 #include "st_cb_fbo.h"
51 #include "st_cb_flush.h"
52 #include "st_cb_texture.h"
53 #include "st_format.h"
54 #include "st_texture.h"
55 #include "st_manager.h"
56
57 #include "util/u_format.h"
58 #include "util/u_inlines.h"
59 #include "util/u_surface.h"
60
61
62 static GLboolean
63 st_renderbuffer_alloc_sw_storage(struct gl_context * ctx,
64                                  struct gl_renderbuffer *rb,
65                                  GLenum internalFormat,
66                                  GLuint width, GLuint height)
67 {
68    struct st_context *st = st_context(ctx);
69    struct st_renderbuffer *strb = st_renderbuffer(rb);
70    enum pipe_format format;
71    size_t size;
72
73    free(strb->data);
74    strb->data = NULL;
75
76    if (internalFormat == GL_RGBA16_SNORM) {
77       /* Special case for software accum buffers.  Otherwise, if the
78        * call to st_choose_renderbuffer_format() fails (because the
79        * driver doesn't support signed 16-bit/channel colors) we'd
80        * just return without allocating the software accum buffer.
81        */
82       format = PIPE_FORMAT_R16G16B16A16_SNORM;
83    }
84    else {
85       format = st_choose_renderbuffer_format(st, internalFormat, 0);
86
87       /* Not setting gl_renderbuffer::Format here will cause
88        * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
89        */
90       if (format == PIPE_FORMAT_NONE) {
91          return GL_TRUE;
92       }
93    }
94
95    strb->Base.Format = st_pipe_format_to_mesa_format(format);
96
97    size = _mesa_format_image_size(strb->Base.Format, width, height, 1);
98    strb->data = malloc(size);
99    return strb->data != NULL;
100 }
101
102
103 /**
104  * gl_renderbuffer::AllocStorage()
105  * This is called to allocate the original drawing surface, and
106  * during window resize.
107  */
108 static GLboolean
109 st_renderbuffer_alloc_storage(struct gl_context * ctx,
110                               struct gl_renderbuffer *rb,
111                               GLenum internalFormat,
112                               GLuint width, GLuint height)
113 {
114    struct st_context *st = st_context(ctx);
115    struct pipe_screen *screen = st->pipe->screen;
116    struct st_renderbuffer *strb = st_renderbuffer(rb);
117    enum pipe_format format = PIPE_FORMAT_NONE;
118    struct pipe_resource templ;
119
120    /* init renderbuffer fields */
121    strb->Base.Width  = width;
122    strb->Base.Height = height;
123    strb->Base._BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
124    strb->defined = GL_FALSE;  /* undefined contents now */
125
126    if (strb->software) {
127       return st_renderbuffer_alloc_sw_storage(ctx, rb, internalFormat,
128                                               width, height);
129    }
130
131    /* Free the old surface and texture
132     */
133    pipe_surface_reference(&strb->surface_srgb, NULL);
134    pipe_surface_reference(&strb->surface_linear, NULL);
135    strb->surface = NULL;
136    pipe_resource_reference(&strb->texture, NULL);
137
138    /* If an sRGB framebuffer is unsupported, sRGB formats behave like linear
139     * formats.
140     */
141    if (!ctx->Extensions.EXT_framebuffer_sRGB) {
142       internalFormat = _mesa_get_linear_internalformat(internalFormat);
143    }
144
145    /* Handle multisample renderbuffers first.
146     *
147     * From ARB_framebuffer_object:
148     *   If <samples> is zero, then RENDERBUFFER_SAMPLES is set to zero.
149     *   Otherwise <samples> represents a request for a desired minimum
150     *   number of samples. Since different implementations may support
151     *   different sample counts for multisampled rendering, the actual
152     *   number of samples allocated for the renderbuffer image is
153     *   implementation dependent.  However, the resulting value for
154     *   RENDERBUFFER_SAMPLES is guaranteed to be greater than or equal
155     *   to <samples> and no more than the next larger sample count supported
156     *   by the implementation.
157     *
158     * Find the supported number of samples >= rb->NumSamples
159     */
160    if (rb->NumSamples > 0) {
161       unsigned start, i;
162
163       if (ctx->Const.MaxSamples > 1 &&  rb->NumSamples == 1) {
164          /* don't try num_samples = 1 with drivers that support real msaa */
165          start = 2;
166       } else {
167          start = rb->NumSamples;
168       }
169
170       for (i = start; i <= ctx->Const.MaxSamples; i++) {
171          format = st_choose_renderbuffer_format(st, internalFormat, i);
172
173          if (format != PIPE_FORMAT_NONE) {
174             rb->NumSamples = i;
175             break;
176          }
177       }
178    } else {
179       format = st_choose_renderbuffer_format(st, internalFormat, 0);
180    }
181
182    /* Not setting gl_renderbuffer::Format here will cause
183     * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
184     */
185    if (format == PIPE_FORMAT_NONE) {
186       return GL_TRUE;
187    }
188
189    strb->Base.Format = st_pipe_format_to_mesa_format(format);
190
191    if (width == 0 || height == 0) {
192       /* if size is zero, nothing to allocate */
193       return GL_TRUE;
194    }
195
196    /* Setup new texture template.
197     */
198    memset(&templ, 0, sizeof(templ));
199    templ.target = st->internal_target;
200    templ.format = format;
201    templ.width0 = width;
202    templ.height0 = height;
203    templ.depth0 = 1;
204    templ.array_size = 1;
205    templ.nr_samples = rb->NumSamples;
206    if (util_format_is_depth_or_stencil(format)) {
207       templ.bind = PIPE_BIND_DEPTH_STENCIL;
208    }
209    else if (strb->Base.Name != 0) {
210       /* this is a user-created renderbuffer */
211       templ.bind = PIPE_BIND_RENDER_TARGET;
212    }
213    else {
214       /* this is a window-system buffer */
215       templ.bind = (PIPE_BIND_DISPLAY_TARGET |
216                     PIPE_BIND_RENDER_TARGET);
217    }
218
219    strb->texture = screen->resource_create(screen, &templ);
220
221    if (!strb->texture)
222       return FALSE;
223
224    st_update_renderbuffer_surface(st, strb);
225    return strb->surface != NULL;
226 }
227
228
229 /**
230  * gl_renderbuffer::Delete()
231  */
232 static void
233 st_renderbuffer_delete(struct gl_context *ctx, struct gl_renderbuffer *rb)
234 {
235    struct st_renderbuffer *strb = st_renderbuffer(rb);
236    if (ctx) {
237       struct st_context *st = st_context(ctx);
238       pipe_surface_release(st->pipe, &strb->surface_srgb);
239       pipe_surface_release(st->pipe, &strb->surface_linear);
240       strb->surface = NULL;
241    }
242    pipe_resource_reference(&strb->texture, NULL);
243    free(strb->data);
244    _mesa_delete_renderbuffer(ctx, rb);
245 }
246
247
248 /**
249  * Called via ctx->Driver.NewRenderbuffer()
250  */
251 static struct gl_renderbuffer *
252 st_new_renderbuffer(struct gl_context *ctx, GLuint name)
253 {
254    struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
255    if (strb) {
256       assert(name != 0);
257       _mesa_init_renderbuffer(&strb->Base, name);
258       strb->Base.Delete = st_renderbuffer_delete;
259       strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
260       return &strb->Base;
261    }
262    return NULL;
263 }
264
265
266 /**
267  * Allocate a renderbuffer for an on-screen window (not a user-created
268  * renderbuffer).  The window system code determines the format.
269  */
270 struct gl_renderbuffer *
271 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
272 {
273    struct st_renderbuffer *strb;
274
275    strb = ST_CALLOC_STRUCT(st_renderbuffer);
276    if (!strb) {
277       _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
278       return NULL;
279    }
280
281    _mesa_init_renderbuffer(&strb->Base, 0);
282    strb->Base.ClassID = 0x4242; /* just a unique value */
283    strb->Base.NumSamples = samples;
284    strb->Base.Format = st_pipe_format_to_mesa_format(format);
285    strb->Base._BaseFormat = _mesa_get_format_base_format(strb->Base.Format);
286    strb->software = sw;
287
288    switch (format) {
289    case PIPE_FORMAT_R8G8B8A8_UNORM:
290    case PIPE_FORMAT_B8G8R8A8_UNORM:
291    case PIPE_FORMAT_A8R8G8B8_UNORM:
292       strb->Base.InternalFormat = GL_RGBA8;
293       break;
294    case PIPE_FORMAT_R8G8B8X8_UNORM:
295    case PIPE_FORMAT_B8G8R8X8_UNORM:
296    case PIPE_FORMAT_X8R8G8B8_UNORM:
297       strb->Base.InternalFormat = GL_RGB8;
298       break;
299    case PIPE_FORMAT_R8G8B8A8_SRGB:
300    case PIPE_FORMAT_B8G8R8A8_SRGB:
301    case PIPE_FORMAT_A8R8G8B8_SRGB:
302       strb->Base.InternalFormat = GL_SRGB8_ALPHA8;
303       break;
304    case PIPE_FORMAT_R8G8B8X8_SRGB:
305    case PIPE_FORMAT_B8G8R8X8_SRGB:
306    case PIPE_FORMAT_X8R8G8B8_SRGB:
307       strb->Base.InternalFormat = GL_SRGB8;
308       break;
309    case PIPE_FORMAT_B5G5R5A1_UNORM:
310       strb->Base.InternalFormat = GL_RGB5_A1;
311       break;
312    case PIPE_FORMAT_B4G4R4A4_UNORM:
313       strb->Base.InternalFormat = GL_RGBA4;
314       break;
315    case PIPE_FORMAT_B5G6R5_UNORM:
316       strb->Base.InternalFormat = GL_RGB565;
317       break;
318    case PIPE_FORMAT_Z16_UNORM:
319       strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
320       break;
321    case PIPE_FORMAT_Z32_UNORM:
322       strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
323       break;
324    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
325    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
326       strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
327       break;
328    case PIPE_FORMAT_Z24X8_UNORM:
329    case PIPE_FORMAT_X8Z24_UNORM:
330       strb->Base.InternalFormat = GL_DEPTH_COMPONENT24;
331       break;
332    case PIPE_FORMAT_S8_UINT:
333       strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
334       break;
335    case PIPE_FORMAT_R16G16B16A16_SNORM:
336       /* accum buffer */
337       strb->Base.InternalFormat = GL_RGBA16_SNORM;
338       break;
339    case PIPE_FORMAT_R16G16B16A16_UNORM:
340       strb->Base.InternalFormat = GL_RGBA16;
341       break;
342    case PIPE_FORMAT_R8_UNORM:
343       strb->Base.InternalFormat = GL_R8;
344       break;
345    case PIPE_FORMAT_R8G8_UNORM:
346       strb->Base.InternalFormat = GL_RG8;
347       break;
348    case PIPE_FORMAT_R16_UNORM:
349       strb->Base.InternalFormat = GL_R16;
350       break;
351    case PIPE_FORMAT_R16G16_UNORM:
352       strb->Base.InternalFormat = GL_RG16;
353       break;
354    case PIPE_FORMAT_R32G32B32A32_FLOAT:
355       strb->Base.InternalFormat = GL_RGBA32F;
356       break;
357    case PIPE_FORMAT_R16G16B16A16_FLOAT:
358       strb->Base.InternalFormat = GL_RGBA16F;
359       break;
360    default:
361       _mesa_problem(NULL,
362                     "Unexpected format %s in st_new_renderbuffer_fb",
363                     util_format_name(format));
364       free(strb);
365       return NULL;
366    }
367
368    /* st-specific methods */
369    strb->Base.Delete = st_renderbuffer_delete;
370    strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
371
372    /* surface is allocated in st_renderbuffer_alloc_storage() */
373    strb->surface = NULL;
374
375    return &strb->Base;
376 }
377
378
379 /**
380  * Create or update the pipe_surface of a FBO renderbuffer.
381  * This is usually called after st_finalize_texture.
382  */
383 void
384 st_update_renderbuffer_surface(struct st_context *st,
385                                struct st_renderbuffer *strb)
386 {
387    struct pipe_context *pipe = st->pipe;
388    struct pipe_resource *resource = strb->texture;
389    const struct st_texture_object *stTexObj = NULL;
390    unsigned rtt_width = strb->Base.Width;
391    unsigned rtt_height = strb->Base.Height;
392    unsigned rtt_depth = strb->Base.Depth;
393
394    /*
395     * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
396     * the format of strb->texture is linear (because we have no control over
397     * the format).  Check strb->Base.Format instead of strb->texture->format
398     * to determine if the rb is sRGB-capable.
399     */
400    boolean enable_srgb = st->ctx->Color.sRGBEnabled &&
401       _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB;
402    enum pipe_format format = resource->format;
403
404    if (strb->is_rtt) {
405       stTexObj = st_texture_object(strb->Base.TexImage->TexObject);
406       if (stTexObj->surface_based)
407          format = stTexObj->surface_format;
408    }
409
410    format = enable_srgb ? util_format_srgb(format) : util_format_linear(format);
411
412    if (resource->target == PIPE_TEXTURE_1D_ARRAY) {
413       rtt_depth = rtt_height;
414       rtt_height = 1;
415    }
416
417    /* find matching mipmap level size */
418    unsigned level;
419    for (level = 0; level <= resource->last_level; level++) {
420       if (u_minify(resource->width0, level) == rtt_width &&
421           u_minify(resource->height0, level) == rtt_height &&
422           (resource->target != PIPE_TEXTURE_3D ||
423            u_minify(resource->depth0, level) == rtt_depth)) {
424          break;
425       }
426    }
427    assert(level <= resource->last_level);
428
429    /* determine the layer bounds */
430    unsigned first_layer, last_layer;
431    if (strb->rtt_layered) {
432       first_layer = 0;
433       last_layer = util_max_layer(strb->texture, level);
434    }
435    else {
436       first_layer =
437       last_layer = strb->rtt_face + strb->rtt_slice;
438    }
439
440    /* Adjust for texture views */
441    if (strb->is_rtt && resource->array_size > 1 &&
442        stTexObj->base.Immutable) {
443       const struct gl_texture_object *tex = &stTexObj->base;
444       first_layer += tex->MinLayer;
445       if (!strb->rtt_layered)
446          last_layer += tex->MinLayer;
447       else
448          last_layer = MIN2(first_layer + tex->NumLayers - 1, last_layer);
449    }
450
451    struct pipe_surface **psurf =
452       enable_srgb ? &strb->surface_srgb : &strb->surface_linear;
453    struct pipe_surface *surf = *psurf;
454
455    if (!surf ||
456        surf->texture->nr_samples != strb->Base.NumSamples ||
457        surf->format != format ||
458        surf->texture != resource ||
459        surf->width != rtt_width ||
460        surf->height != rtt_height ||
461        surf->u.tex.level != level ||
462        surf->u.tex.first_layer != first_layer ||
463        surf->u.tex.last_layer != last_layer) {
464       /* create a new pipe_surface */
465       struct pipe_surface surf_tmpl;
466       memset(&surf_tmpl, 0, sizeof(surf_tmpl));
467       surf_tmpl.format = format;
468       surf_tmpl.u.tex.level = level;
469       surf_tmpl.u.tex.first_layer = first_layer;
470       surf_tmpl.u.tex.last_layer = last_layer;
471
472       pipe_surface_release(pipe, psurf);
473
474       *psurf = pipe->create_surface(pipe, resource, &surf_tmpl);
475    }
476    strb->surface = *psurf;
477 }
478
479
480 /**
481  * Return the pipe_resource which stores a particular texture image.
482  */
483 static struct pipe_resource *
484 get_teximage_resource(struct gl_texture_object *texObj,
485                       unsigned face, unsigned level)
486 {
487    struct st_texture_image *stImg =
488       st_texture_image(texObj->Image[face][level]);
489
490    return stImg->pt;
491 }
492
493
494 /**
495  * Called by ctx->Driver.RenderTexture
496  */
497 static void
498 st_render_texture(struct gl_context *ctx,
499                   struct gl_framebuffer *fb,
500                   struct gl_renderbuffer_attachment *att)
501 {
502    struct st_context *st = st_context(ctx);
503    struct pipe_context *pipe = st->pipe;
504    struct gl_renderbuffer *rb = att->Renderbuffer;
505    struct st_renderbuffer *strb = st_renderbuffer(rb);
506    struct pipe_resource *pt;
507
508    if (!st_finalize_texture(ctx, pipe, att->Texture, att->CubeMapFace))
509       return;
510
511    pt = get_teximage_resource(att->Texture,
512                               att->CubeMapFace,
513                               att->TextureLevel);
514    assert(pt);
515
516    /* point renderbuffer at texobject */
517    strb->is_rtt = TRUE;
518    strb->rtt_face = att->CubeMapFace;
519    strb->rtt_slice = att->Zoffset;
520    strb->rtt_layered = att->Layered;
521    pipe_resource_reference(&strb->texture, pt);
522
523    st_update_renderbuffer_surface(st, strb);
524
525    /* Invalidate buffer state so that the pipe's framebuffer state
526     * gets updated.
527     * That's where the new renderbuffer (which we just created) gets
528     * passed to the pipe as a (color/depth) render target.
529     */
530    st_invalidate_buffers(st);
531
532
533    /* Need to trigger a call to update_framebuffer() since we just
534     * attached a new renderbuffer.
535     */
536    ctx->NewState |= _NEW_BUFFERS;
537 }
538
539
540 /**
541  * Called via ctx->Driver.FinishRenderTexture.
542  */
543 static void
544 st_finish_render_texture(struct gl_context *ctx, struct gl_renderbuffer *rb)
545 {
546    struct st_context *st = st_context(ctx);
547    struct st_renderbuffer *strb = st_renderbuffer(rb);
548
549    if (!strb)
550       return;
551
552    strb->is_rtt = FALSE;
553
554    /* restore previous framebuffer state */
555    st_invalidate_buffers(st);
556 }
557
558
559 /** Debug helper */
560 static void
561 st_fbo_invalid(const char *reason)
562 {
563    if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
564       _mesa_debug(NULL, "Invalid FBO: %s\n", reason);
565    }
566 }
567
568
569 /**
570  * Validate a renderbuffer attachment for a particular set of bindings.
571  */
572 static GLboolean
573 st_validate_attachment(struct gl_context *ctx,
574                        struct pipe_screen *screen,
575                        const struct gl_renderbuffer_attachment *att,
576                        unsigned bindings)
577 {
578    const struct st_texture_object *stObj = st_texture_object(att->Texture);
579    enum pipe_format format;
580    mesa_format texFormat;
581    GLboolean valid;
582
583    /* Sanity check: we must be binding the surface as a (color) render target
584     * or depth/stencil target.
585     */
586    assert(bindings == PIPE_BIND_RENDER_TARGET ||
587           bindings == PIPE_BIND_DEPTH_STENCIL);
588
589    /* Only validate texture attachments for now, since
590     * st_renderbuffer_alloc_storage makes sure that
591     * the format is supported.
592     */
593    if (att->Type != GL_TEXTURE)
594       return GL_TRUE;
595
596    if (!stObj || !stObj->pt)
597       return GL_FALSE;
598
599    format = stObj->pt->format;
600    texFormat = att->Renderbuffer->TexImage->TexFormat;
601
602    /* If the encoding is sRGB and sRGB rendering cannot be enabled,
603     * check for linear format support instead.
604     * Later when we create a surface, we change the format to a linear one. */
605    if (!ctx->Extensions.EXT_framebuffer_sRGB &&
606        _mesa_get_format_color_encoding(texFormat) == GL_SRGB) {
607       const mesa_format linearFormat = _mesa_get_srgb_format_linear(texFormat);
608       format = st_mesa_format_to_pipe_format(st_context(ctx), linearFormat);
609    }
610
611    valid = screen->is_format_supported(screen, format,
612                                       PIPE_TEXTURE_2D,
613                                       stObj->pt->nr_samples, bindings);
614    if (!valid) {
615       st_fbo_invalid("Invalid format");
616    }
617
618    return valid;
619 }
620
621
622 /**
623  * Check that the framebuffer configuration is valid in terms of what
624  * the driver can support.
625  *
626  * For Gallium we only supports combined Z+stencil, not separate buffers.
627  */
628 static void
629 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
630 {
631    struct st_context *st = st_context(ctx);
632    struct pipe_screen *screen = st->pipe->screen;
633    const struct gl_renderbuffer_attachment *depth =
634          &fb->Attachment[BUFFER_DEPTH];
635    const struct gl_renderbuffer_attachment *stencil =
636          &fb->Attachment[BUFFER_STENCIL];
637    GLuint i;
638    enum pipe_format first_format = PIPE_FORMAT_NONE;
639    boolean mixed_formats =
640          screen->get_param(screen, PIPE_CAP_MIXED_COLORBUFFER_FORMATS) != 0;
641
642    if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
643       st_fbo_invalid("Different Depth/Stencil buffer formats");
644       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
645       return;
646    }
647    if (depth->Type == GL_RENDERBUFFER_EXT &&
648        stencil->Type == GL_RENDERBUFFER_EXT &&
649        depth->Renderbuffer != stencil->Renderbuffer) {
650       st_fbo_invalid("Separate Depth/Stencil buffers");
651       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
652       return;
653    }
654    if (depth->Type == GL_TEXTURE &&
655        stencil->Type == GL_TEXTURE &&
656        depth->Texture != stencil->Texture) {
657       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
658       st_fbo_invalid("Different Depth/Stencil textures");
659       return;
660    }
661
662    if (!st_validate_attachment(ctx, screen, depth, PIPE_BIND_DEPTH_STENCIL)) {
663       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
664       st_fbo_invalid("Invalid depth attachment");
665       return;
666    }
667    if (!st_validate_attachment(ctx, screen, stencil, PIPE_BIND_DEPTH_STENCIL)) {
668       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
669       st_fbo_invalid("Invalid stencil attachment");
670       return;
671    }
672    for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
673       struct gl_renderbuffer_attachment *att =
674             &fb->Attachment[BUFFER_COLOR0 + i];
675       enum pipe_format format;
676
677       if (!st_validate_attachment(ctx, screen, att, PIPE_BIND_RENDER_TARGET)) {
678          fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
679          st_fbo_invalid("Invalid color attachment");
680          return;
681       }
682
683       if (!mixed_formats) {
684          /* Disallow mixed formats. */
685          if (att->Type != GL_NONE) {
686             format = st_renderbuffer(att->Renderbuffer)->surface->format;
687          } else {
688             continue;
689          }
690
691          if (first_format == PIPE_FORMAT_NONE) {
692             first_format = format;
693          } else if (format != first_format) {
694             fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
695             st_fbo_invalid("Mixed color formats");
696             return;
697          }
698       }
699    }
700 }
701
702
703 /**
704  * Called via glDrawBuffer.  We only provide this driver function so that we
705  * can check if we need to allocate a new renderbuffer.  Specifically, we
706  * don't usually allocate a front color buffer when using a double-buffered
707  * visual.  But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
708  * that buffer.  Note, this is only for window system buffers, not user-
709  * created FBOs.
710  */
711 static void
712 st_DrawBuffers(struct gl_context *ctx, GLsizei count, const GLenum *buffers)
713 {
714    struct st_context *st = st_context(ctx);
715    struct gl_framebuffer *fb = ctx->DrawBuffer;
716
717    (void) count;
718    (void) buffers;
719
720    if (_mesa_is_winsys_fbo(fb)) {
721       GLuint i;
722       /* add the renderbuffers on demand */
723       for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
724          GLint idx = fb->_ColorDrawBufferIndexes[i];
725
726          if (idx >= 0) {
727             st_manager_add_color_renderbuffer(st, fb, idx);
728          }
729       }
730    }
731 }
732
733
734 /**
735  * Called via glReadBuffer.  As with st_DrawBuffers, we use this function
736  * to check if we need to allocate a renderbuffer on demand.
737  */
738 static void
739 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
740 {
741    struct st_context *st = st_context(ctx);
742    struct gl_framebuffer *fb = ctx->ReadBuffer;
743
744    (void) buffer;
745
746    /* Check if we need to allocate a front color buffer.
747     * Front buffers are often allocated on demand (other color buffers are
748     * always allocated in advance).
749     */
750    if ((fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT ||
751         fb->_ColorReadBufferIndex == BUFFER_FRONT_RIGHT) &&
752        fb->Attachment[fb->_ColorReadBufferIndex].Type == GL_NONE) {
753       assert(_mesa_is_winsys_fbo(fb));
754       /* add the buffer */
755       st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
756       _mesa_update_state(ctx);
757       st_validate_state(st, ST_PIPELINE_UPDATE_FRAMEBUFFER);
758    }
759 }
760
761
762
763 /**
764  * Called via ctx->Driver.MapRenderbuffer.
765  */
766 static void
767 st_MapRenderbuffer(struct gl_context *ctx,
768                    struct gl_renderbuffer *rb,
769                    GLuint x, GLuint y, GLuint w, GLuint h,
770                    GLbitfield mode,
771                    GLubyte **mapOut, GLint *rowStrideOut)
772 {
773    struct st_context *st = st_context(ctx);
774    struct st_renderbuffer *strb = st_renderbuffer(rb);
775    struct pipe_context *pipe = st->pipe;
776    const GLboolean invert = rb->Name == 0;
777    unsigned usage;
778    GLuint y2;
779    GLubyte *map;
780
781    if (strb->software) {
782       /* software-allocated renderbuffer (probably an accum buffer) */
783       if (strb->data) {
784          GLint bpp = _mesa_get_format_bytes(strb->Base.Format);
785          GLint stride = _mesa_format_row_stride(strb->Base.Format,
786                                                 strb->Base.Width);
787          *mapOut = (GLubyte *) strb->data + y * stride + x * bpp;
788          *rowStrideOut = stride;
789       }
790       else {
791          *mapOut = NULL;
792          *rowStrideOut = 0;
793       }
794       return;
795    }
796
797    usage = 0x0;
798    if (mode & GL_MAP_READ_BIT)
799       usage |= PIPE_TRANSFER_READ;
800    if (mode & GL_MAP_WRITE_BIT)
801       usage |= PIPE_TRANSFER_WRITE;
802    if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
803       usage |= PIPE_TRANSFER_DISCARD_RANGE;
804
805    /* Note: y=0=bottom of buffer while y2=0=top of buffer.
806     * 'invert' will be true for window-system buffers and false for
807     * user-allocated renderbuffers and textures.
808     */
809    if (invert)
810       y2 = strb->Base.Height - y - h;
811    else
812       y2 = y;
813
814     map = pipe_transfer_map(pipe,
815                             strb->texture,
816                             strb->surface->u.tex.level,
817                             strb->surface->u.tex.first_layer,
818                             usage, x, y2, w, h, &strb->transfer);
819    if (map) {
820       if (invert) {
821          *rowStrideOut = -(int) strb->transfer->stride;
822          map += (h - 1) * strb->transfer->stride;
823       }
824       else {
825          *rowStrideOut = strb->transfer->stride;
826       }
827       *mapOut = map;
828    }
829    else {
830       *mapOut = NULL;
831       *rowStrideOut = 0;
832    }
833 }
834
835
836 /**
837  * Called via ctx->Driver.UnmapRenderbuffer.
838  */
839 static void
840 st_UnmapRenderbuffer(struct gl_context *ctx,
841                      struct gl_renderbuffer *rb)
842 {
843    struct st_context *st = st_context(ctx);
844    struct st_renderbuffer *strb = st_renderbuffer(rb);
845    struct pipe_context *pipe = st->pipe;
846
847    if (strb->software) {
848       /* software-allocated renderbuffer (probably an accum buffer) */
849       return;
850    }
851
852    pipe_transfer_unmap(pipe, strb->transfer);
853    strb->transfer = NULL;
854 }
855
856
857
858 void
859 st_init_fbo_functions(struct dd_function_table *functions)
860 {
861    functions->NewFramebuffer = _mesa_new_framebuffer;
862    functions->NewRenderbuffer = st_new_renderbuffer;
863    functions->FramebufferRenderbuffer = _mesa_FramebufferRenderbuffer_sw;
864    functions->RenderTexture = st_render_texture;
865    functions->FinishRenderTexture = st_finish_render_texture;
866    functions->ValidateFramebuffer = st_validate_framebuffer;
867
868    functions->DrawBuffers = st_DrawBuffers;
869    functions->ReadBuffer = st_ReadBuffer;
870
871    functions->MapRenderbuffer = st_MapRenderbuffer;
872    functions->UnmapRenderbuffer = st_UnmapRenderbuffer;
873 }