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