1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Framebuffer/renderbuffer functions.
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"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_screen.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"
57 #include "util/u_format.h"
58 #include "util/u_inlines.h"
59 #include "util/u_surface.h"
63 st_renderbuffer_alloc_sw_storage(struct gl_context
* ctx
,
64 struct gl_renderbuffer
*rb
,
65 GLenum internalFormat
,
66 GLuint width
, GLuint height
)
68 struct st_context
*st
= st_context(ctx
);
69 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
70 enum pipe_format format
;
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.
82 format
= PIPE_FORMAT_R16G16B16A16_SNORM
;
85 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0);
87 /* Not setting gl_renderbuffer::Format here will cause
88 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
90 if (format
== PIPE_FORMAT_NONE
) {
95 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
97 size
= _mesa_format_image_size(strb
->Base
.Format
, width
, height
, 1);
98 strb
->data
= malloc(size
);
99 return strb
->data
!= NULL
;
104 * gl_renderbuffer::AllocStorage()
105 * This is called to allocate the original drawing surface, and
106 * during window resize.
109 st_renderbuffer_alloc_storage(struct gl_context
* ctx
,
110 struct gl_renderbuffer
*rb
,
111 GLenum internalFormat
,
112 GLuint width
, GLuint height
)
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
;
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 */
126 if (strb
->software
) {
127 return st_renderbuffer_alloc_sw_storage(ctx
, rb
, internalFormat
,
131 /* Free the old surface and texture
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
);
138 /* If an sRGB framebuffer is unsupported, sRGB formats behave like linear
141 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
) {
142 internalFormat
= _mesa_get_linear_internalformat(internalFormat
);
145 /* Handle multisample renderbuffers first.
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.
158 * Find the supported number of samples >= rb->NumSamples
160 if (rb
->NumSamples
> 0) {
163 if (ctx
->Const
.MaxSamples
> 1 && rb
->NumSamples
== 1) {
164 /* don't try num_samples = 1 with drivers that support real msaa */
167 start
= rb
->NumSamples
;
170 for (i
= start
; i
<= ctx
->Const
.MaxSamples
; i
++) {
171 format
= st_choose_renderbuffer_format(st
, internalFormat
, i
);
173 if (format
!= PIPE_FORMAT_NONE
) {
179 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0);
182 /* Not setting gl_renderbuffer::Format here will cause
183 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
185 if (format
== PIPE_FORMAT_NONE
) {
189 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
191 if (width
== 0 || height
== 0) {
192 /* if size is zero, nothing to allocate */
196 /* Setup new texture template.
198 memset(&templ
, 0, sizeof(templ
));
199 templ
.target
= st
->internal_target
;
200 templ
.format
= format
;
201 templ
.width0
= width
;
202 templ
.height0
= height
;
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
;
209 else if (strb
->Base
.Name
!= 0) {
210 /* this is a user-created renderbuffer */
211 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
214 /* this is a window-system buffer */
215 templ
.bind
= (PIPE_BIND_DISPLAY_TARGET
|
216 PIPE_BIND_RENDER_TARGET
);
219 strb
->texture
= screen
->resource_create(screen
, &templ
);
224 st_update_renderbuffer_surface(st
, strb
);
225 return strb
->surface
!= NULL
;
230 * gl_renderbuffer::Delete()
233 st_renderbuffer_delete(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
235 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
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
;
242 pipe_resource_reference(&strb
->texture
, NULL
);
244 _mesa_delete_renderbuffer(ctx
, rb
);
249 * Called via ctx->Driver.NewRenderbuffer()
251 static struct gl_renderbuffer
*
252 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
254 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
257 _mesa_init_renderbuffer(&strb
->Base
, name
);
258 strb
->Base
.Delete
= st_renderbuffer_delete
;
259 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
267 * Allocate a renderbuffer for an on-screen window (not a user-created
268 * renderbuffer). The window system code determines the format.
270 struct gl_renderbuffer
*
271 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
273 struct st_renderbuffer
*strb
;
275 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
277 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
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
);
289 case PIPE_FORMAT_B10G10R10A2_UNORM
:
290 strb
->Base
.InternalFormat
= GL_RGB10_A2
;
292 case PIPE_FORMAT_B10G10R10X2_UNORM
:
293 strb
->Base
.InternalFormat
= GL_RGB10
;
295 case PIPE_FORMAT_R8G8B8A8_UNORM
:
296 case PIPE_FORMAT_B8G8R8A8_UNORM
:
297 case PIPE_FORMAT_A8R8G8B8_UNORM
:
298 strb
->Base
.InternalFormat
= GL_RGBA8
;
300 case PIPE_FORMAT_R8G8B8X8_UNORM
:
301 case PIPE_FORMAT_B8G8R8X8_UNORM
:
302 case PIPE_FORMAT_X8R8G8B8_UNORM
:
303 strb
->Base
.InternalFormat
= GL_RGB8
;
305 case PIPE_FORMAT_R8G8B8A8_SRGB
:
306 case PIPE_FORMAT_B8G8R8A8_SRGB
:
307 case PIPE_FORMAT_A8R8G8B8_SRGB
:
308 strb
->Base
.InternalFormat
= GL_SRGB8_ALPHA8
;
310 case PIPE_FORMAT_R8G8B8X8_SRGB
:
311 case PIPE_FORMAT_B8G8R8X8_SRGB
:
312 case PIPE_FORMAT_X8R8G8B8_SRGB
:
313 strb
->Base
.InternalFormat
= GL_SRGB8
;
315 case PIPE_FORMAT_B5G5R5A1_UNORM
:
316 strb
->Base
.InternalFormat
= GL_RGB5_A1
;
318 case PIPE_FORMAT_B4G4R4A4_UNORM
:
319 strb
->Base
.InternalFormat
= GL_RGBA4
;
321 case PIPE_FORMAT_B5G6R5_UNORM
:
322 strb
->Base
.InternalFormat
= GL_RGB565
;
324 case PIPE_FORMAT_Z16_UNORM
:
325 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
327 case PIPE_FORMAT_Z32_UNORM
:
328 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
330 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
331 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
332 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
334 case PIPE_FORMAT_Z24X8_UNORM
:
335 case PIPE_FORMAT_X8Z24_UNORM
:
336 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT24
;
338 case PIPE_FORMAT_S8_UINT
:
339 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
341 case PIPE_FORMAT_R16G16B16A16_SNORM
:
343 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
345 case PIPE_FORMAT_R16G16B16A16_UNORM
:
346 strb
->Base
.InternalFormat
= GL_RGBA16
;
348 case PIPE_FORMAT_R8_UNORM
:
349 strb
->Base
.InternalFormat
= GL_R8
;
351 case PIPE_FORMAT_R8G8_UNORM
:
352 strb
->Base
.InternalFormat
= GL_RG8
;
354 case PIPE_FORMAT_R16_UNORM
:
355 strb
->Base
.InternalFormat
= GL_R16
;
357 case PIPE_FORMAT_R16G16_UNORM
:
358 strb
->Base
.InternalFormat
= GL_RG16
;
360 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
361 strb
->Base
.InternalFormat
= GL_RGBA32F
;
363 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
364 strb
->Base
.InternalFormat
= GL_RGBA16F
;
368 "Unexpected format %s in st_new_renderbuffer_fb",
369 util_format_name(format
));
374 /* st-specific methods */
375 strb
->Base
.Delete
= st_renderbuffer_delete
;
376 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
378 /* surface is allocated in st_renderbuffer_alloc_storage() */
379 strb
->surface
= NULL
;
386 * Create or update the pipe_surface of a FBO renderbuffer.
387 * This is usually called after st_finalize_texture.
390 st_update_renderbuffer_surface(struct st_context
*st
,
391 struct st_renderbuffer
*strb
)
393 struct pipe_context
*pipe
= st
->pipe
;
394 struct pipe_resource
*resource
= strb
->texture
;
395 const struct st_texture_object
*stTexObj
= NULL
;
396 unsigned rtt_width
= strb
->Base
.Width
;
397 unsigned rtt_height
= strb
->Base
.Height
;
398 unsigned rtt_depth
= strb
->Base
.Depth
;
401 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
402 * the format of strb->texture is linear (because we have no control over
403 * the format). Check strb->Base.Format instead of strb->texture->format
404 * to determine if the rb is sRGB-capable.
406 boolean enable_srgb
= st
->ctx
->Color
.sRGBEnabled
&&
407 _mesa_get_format_color_encoding(strb
->Base
.Format
) == GL_SRGB
;
408 enum pipe_format format
= resource
->format
;
411 stTexObj
= st_texture_object(strb
->Base
.TexImage
->TexObject
);
412 if (stTexObj
->surface_based
)
413 format
= stTexObj
->surface_format
;
416 format
= enable_srgb
? util_format_srgb(format
) : util_format_linear(format
);
418 if (resource
->target
== PIPE_TEXTURE_1D_ARRAY
) {
419 rtt_depth
= rtt_height
;
423 /* find matching mipmap level size */
425 for (level
= 0; level
<= resource
->last_level
; level
++) {
426 if (u_minify(resource
->width0
, level
) == rtt_width
&&
427 u_minify(resource
->height0
, level
) == rtt_height
&&
428 (resource
->target
!= PIPE_TEXTURE_3D
||
429 u_minify(resource
->depth0
, level
) == rtt_depth
)) {
433 assert(level
<= resource
->last_level
);
435 /* determine the layer bounds */
436 unsigned first_layer
, last_layer
;
437 if (strb
->rtt_layered
) {
439 last_layer
= util_max_layer(strb
->texture
, level
);
443 last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
446 /* Adjust for texture views */
447 if (strb
->is_rtt
&& resource
->array_size
> 1 &&
448 stTexObj
->base
.Immutable
) {
449 const struct gl_texture_object
*tex
= &stTexObj
->base
;
450 first_layer
+= tex
->MinLayer
;
451 if (!strb
->rtt_layered
)
452 last_layer
+= tex
->MinLayer
;
454 last_layer
= MIN2(first_layer
+ tex
->NumLayers
- 1, last_layer
);
457 struct pipe_surface
**psurf
=
458 enable_srgb
? &strb
->surface_srgb
: &strb
->surface_linear
;
459 struct pipe_surface
*surf
= *psurf
;
462 surf
->texture
->nr_samples
!= strb
->Base
.NumSamples
||
463 surf
->format
!= format
||
464 surf
->texture
!= resource
||
465 surf
->width
!= rtt_width
||
466 surf
->height
!= rtt_height
||
467 surf
->u
.tex
.level
!= level
||
468 surf
->u
.tex
.first_layer
!= first_layer
||
469 surf
->u
.tex
.last_layer
!= last_layer
) {
470 /* create a new pipe_surface */
471 struct pipe_surface surf_tmpl
;
472 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
473 surf_tmpl
.format
= format
;
474 surf_tmpl
.u
.tex
.level
= level
;
475 surf_tmpl
.u
.tex
.first_layer
= first_layer
;
476 surf_tmpl
.u
.tex
.last_layer
= last_layer
;
478 pipe_surface_release(pipe
, psurf
);
480 *psurf
= pipe
->create_surface(pipe
, resource
, &surf_tmpl
);
482 strb
->surface
= *psurf
;
487 * Return the pipe_resource which stores a particular texture image.
489 static struct pipe_resource
*
490 get_teximage_resource(struct gl_texture_object
*texObj
,
491 unsigned face
, unsigned level
)
493 struct st_texture_image
*stImg
=
494 st_texture_image(texObj
->Image
[face
][level
]);
501 * Called by ctx->Driver.RenderTexture
504 st_render_texture(struct gl_context
*ctx
,
505 struct gl_framebuffer
*fb
,
506 struct gl_renderbuffer_attachment
*att
)
508 struct st_context
*st
= st_context(ctx
);
509 struct pipe_context
*pipe
= st
->pipe
;
510 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
511 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
512 struct pipe_resource
*pt
;
514 if (!st_finalize_texture(ctx
, pipe
, att
->Texture
, att
->CubeMapFace
))
517 pt
= get_teximage_resource(att
->Texture
,
522 /* point renderbuffer at texobject */
524 strb
->rtt_face
= att
->CubeMapFace
;
525 strb
->rtt_slice
= att
->Zoffset
;
526 strb
->rtt_layered
= att
->Layered
;
527 pipe_resource_reference(&strb
->texture
, pt
);
529 st_update_renderbuffer_surface(st
, strb
);
531 /* Invalidate buffer state so that the pipe's framebuffer state
533 * That's where the new renderbuffer (which we just created) gets
534 * passed to the pipe as a (color/depth) render target.
536 st_invalidate_buffers(st
);
539 /* Need to trigger a call to update_framebuffer() since we just
540 * attached a new renderbuffer.
542 ctx
->NewState
|= _NEW_BUFFERS
;
547 * Called via ctx->Driver.FinishRenderTexture.
550 st_finish_render_texture(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
552 struct st_context
*st
= st_context(ctx
);
553 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
558 strb
->is_rtt
= FALSE
;
560 /* restore previous framebuffer state */
561 st_invalidate_buffers(st
);
567 st_fbo_invalid(const char *reason
)
569 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
570 _mesa_debug(NULL
, "Invalid FBO: %s\n", reason
);
576 * Validate a renderbuffer attachment for a particular set of bindings.
579 st_validate_attachment(struct gl_context
*ctx
,
580 struct pipe_screen
*screen
,
581 const struct gl_renderbuffer_attachment
*att
,
584 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
585 enum pipe_format format
;
586 mesa_format texFormat
;
589 /* Sanity check: we must be binding the surface as a (color) render target
590 * or depth/stencil target.
592 assert(bindings
== PIPE_BIND_RENDER_TARGET
||
593 bindings
== PIPE_BIND_DEPTH_STENCIL
);
595 /* Only validate texture attachments for now, since
596 * st_renderbuffer_alloc_storage makes sure that
597 * the format is supported.
599 if (att
->Type
!= GL_TEXTURE
)
602 if (!stObj
|| !stObj
->pt
)
605 format
= stObj
->pt
->format
;
606 texFormat
= att
->Renderbuffer
->TexImage
->TexFormat
;
608 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
609 * check for linear format support instead.
610 * Later when we create a surface, we change the format to a linear one. */
611 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
&&
612 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
613 const mesa_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
614 format
= st_mesa_format_to_pipe_format(st_context(ctx
), linearFormat
);
617 valid
= screen
->is_format_supported(screen
, format
,
619 stObj
->pt
->nr_samples
, bindings
);
621 st_fbo_invalid("Invalid format");
629 * Check that the framebuffer configuration is valid in terms of what
630 * the driver can support.
632 * For Gallium we only supports combined Z+stencil, not separate buffers.
635 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
637 struct st_context
*st
= st_context(ctx
);
638 struct pipe_screen
*screen
= st
->pipe
->screen
;
639 const struct gl_renderbuffer_attachment
*depth
=
640 &fb
->Attachment
[BUFFER_DEPTH
];
641 const struct gl_renderbuffer_attachment
*stencil
=
642 &fb
->Attachment
[BUFFER_STENCIL
];
644 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
645 boolean mixed_formats
=
646 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
648 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
649 st_fbo_invalid("Different Depth/Stencil buffer formats");
650 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
653 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
654 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
655 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
656 st_fbo_invalid("Separate Depth/Stencil buffers");
657 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
660 if (depth
->Type
== GL_TEXTURE
&&
661 stencil
->Type
== GL_TEXTURE
&&
662 depth
->Texture
!= stencil
->Texture
) {
663 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
664 st_fbo_invalid("Different Depth/Stencil textures");
668 if (!st_validate_attachment(ctx
, screen
, depth
, PIPE_BIND_DEPTH_STENCIL
)) {
669 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
670 st_fbo_invalid("Invalid depth attachment");
673 if (!st_validate_attachment(ctx
, screen
, stencil
, PIPE_BIND_DEPTH_STENCIL
)) {
674 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
675 st_fbo_invalid("Invalid stencil attachment");
678 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
679 struct gl_renderbuffer_attachment
*att
=
680 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
681 enum pipe_format format
;
683 if (!st_validate_attachment(ctx
, screen
, att
, PIPE_BIND_RENDER_TARGET
)) {
684 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
685 st_fbo_invalid("Invalid color attachment");
689 if (!mixed_formats
) {
690 /* Disallow mixed formats. */
691 if (att
->Type
!= GL_NONE
) {
692 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
697 if (first_format
== PIPE_FORMAT_NONE
) {
698 first_format
= format
;
699 } else if (format
!= first_format
) {
700 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
701 st_fbo_invalid("Mixed color formats");
710 * Called via glDrawBuffer. We only provide this driver function so that we
711 * can check if we need to allocate a new renderbuffer. Specifically, we
712 * don't usually allocate a front color buffer when using a double-buffered
713 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
714 * that buffer. Note, this is only for window system buffers, not user-
718 st_DrawBuffers(struct gl_context
*ctx
, GLsizei count
, const GLenum
*buffers
)
720 struct st_context
*st
= st_context(ctx
);
721 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
726 if (_mesa_is_winsys_fbo(fb
)) {
728 /* add the renderbuffers on demand */
729 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
730 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
732 if (idx
!= BUFFER_NONE
) {
733 st_manager_add_color_renderbuffer(st
, fb
, idx
);
741 * Called via glReadBuffer. As with st_DrawBuffers, we use this function
742 * to check if we need to allocate a renderbuffer on demand.
745 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
747 struct st_context
*st
= st_context(ctx
);
748 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
752 /* Check if we need to allocate a front color buffer.
753 * Front buffers are often allocated on demand (other color buffers are
754 * always allocated in advance).
756 if ((fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
||
757 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_RIGHT
) &&
758 fb
->Attachment
[fb
->_ColorReadBufferIndex
].Type
== GL_NONE
) {
759 assert(_mesa_is_winsys_fbo(fb
));
761 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
762 _mesa_update_state(ctx
);
763 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
770 * Called via ctx->Driver.MapRenderbuffer.
773 st_MapRenderbuffer(struct gl_context
*ctx
,
774 struct gl_renderbuffer
*rb
,
775 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
777 GLubyte
**mapOut
, GLint
*rowStrideOut
)
779 struct st_context
*st
= st_context(ctx
);
780 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
781 struct pipe_context
*pipe
= st
->pipe
;
782 const GLboolean invert
= rb
->Name
== 0;
787 if (strb
->software
) {
788 /* software-allocated renderbuffer (probably an accum buffer) */
790 GLint bpp
= _mesa_get_format_bytes(strb
->Base
.Format
);
791 GLint stride
= _mesa_format_row_stride(strb
->Base
.Format
,
793 *mapOut
= (GLubyte
*) strb
->data
+ y
* stride
+ x
* bpp
;
794 *rowStrideOut
= stride
;
804 if (mode
& GL_MAP_READ_BIT
)
805 usage
|= PIPE_TRANSFER_READ
;
806 if (mode
& GL_MAP_WRITE_BIT
)
807 usage
|= PIPE_TRANSFER_WRITE
;
808 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
809 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
811 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
812 * 'invert' will be true for window-system buffers and false for
813 * user-allocated renderbuffers and textures.
816 y2
= strb
->Base
.Height
- y
- h
;
820 map
= pipe_transfer_map(pipe
,
822 strb
->surface
->u
.tex
.level
,
823 strb
->surface
->u
.tex
.first_layer
,
824 usage
, x
, y2
, w
, h
, &strb
->transfer
);
827 *rowStrideOut
= -(int) strb
->transfer
->stride
;
828 map
+= (h
- 1) * strb
->transfer
->stride
;
831 *rowStrideOut
= strb
->transfer
->stride
;
843 * Called via ctx->Driver.UnmapRenderbuffer.
846 st_UnmapRenderbuffer(struct gl_context
*ctx
,
847 struct gl_renderbuffer
*rb
)
849 struct st_context
*st
= st_context(ctx
);
850 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
851 struct pipe_context
*pipe
= st
->pipe
;
853 if (strb
->software
) {
854 /* software-allocated renderbuffer (probably an accum buffer) */
858 pipe_transfer_unmap(pipe
, strb
->transfer
);
859 strb
->transfer
= NULL
;
865 st_init_fbo_functions(struct dd_function_table
*functions
)
867 functions
->NewFramebuffer
= _mesa_new_framebuffer
;
868 functions
->NewRenderbuffer
= st_new_renderbuffer
;
869 functions
->FramebufferRenderbuffer
= _mesa_FramebufferRenderbuffer_sw
;
870 functions
->RenderTexture
= st_render_texture
;
871 functions
->FinishRenderTexture
= st_finish_render_texture
;
872 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
874 functions
->DrawBuffers
= st_DrawBuffers
;
875 functions
->ReadBuffer
= st_ReadBuffer
;
877 functions
->MapRenderbuffer
= st_MapRenderbuffer
;
878 functions
->UnmapRenderbuffer
= st_UnmapRenderbuffer
;