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_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_manager.h"
58 #include "util/u_format.h"
59 #include "util/u_inlines.h"
60 #include "util/u_surface.h"
64 st_renderbuffer_alloc_sw_storage(struct gl_context
* ctx
,
65 struct gl_renderbuffer
*rb
,
66 GLenum internalFormat
,
67 GLuint width
, GLuint height
)
69 struct st_context
*st
= st_context(ctx
);
70 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
71 enum pipe_format format
;
77 if (internalFormat
== GL_RGBA16_SNORM
) {
78 /* Special case for software accum buffers. Otherwise, if the
79 * call to st_choose_renderbuffer_format() fails (because the
80 * driver doesn't support signed 16-bit/channel colors) we'd
81 * just return without allocating the software accum buffer.
83 format
= PIPE_FORMAT_R16G16B16A16_SNORM
;
86 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0);
88 /* Not setting gl_renderbuffer::Format here will cause
89 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
91 if (format
== PIPE_FORMAT_NONE
) {
96 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
98 size
= _mesa_format_image_size(strb
->Base
.Format
, width
, height
, 1);
99 strb
->data
= malloc(size
);
100 return strb
->data
!= NULL
;
105 * gl_renderbuffer::AllocStorage()
106 * This is called to allocate the original drawing surface, and
107 * during window resize.
110 st_renderbuffer_alloc_storage(struct gl_context
* ctx
,
111 struct gl_renderbuffer
*rb
,
112 GLenum internalFormat
,
113 GLuint width
, GLuint height
)
115 struct st_context
*st
= st_context(ctx
);
116 struct pipe_screen
*screen
= st
->pipe
->screen
;
117 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
118 enum pipe_format format
= PIPE_FORMAT_NONE
;
119 struct pipe_resource templ
;
121 /* init renderbuffer fields */
122 strb
->Base
.Width
= width
;
123 strb
->Base
.Height
= height
;
124 strb
->Base
._BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
125 strb
->defined
= GL_FALSE
; /* undefined contents now */
127 if (strb
->software
) {
128 return st_renderbuffer_alloc_sw_storage(ctx
, rb
, internalFormat
,
132 /* Free the old surface and texture
134 pipe_surface_reference(&strb
->surface_srgb
, NULL
);
135 pipe_surface_reference(&strb
->surface_linear
, NULL
);
136 strb
->surface
= NULL
;
137 pipe_resource_reference(&strb
->texture
, NULL
);
139 /* If an sRGB framebuffer is unsupported, sRGB formats behave like linear
142 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
) {
143 internalFormat
= _mesa_get_linear_internalformat(internalFormat
);
146 /* Handle multisample renderbuffers first.
148 * From ARB_framebuffer_object:
149 * If <samples> is zero, then RENDERBUFFER_SAMPLES is set to zero.
150 * Otherwise <samples> represents a request for a desired minimum
151 * number of samples. Since different implementations may support
152 * different sample counts for multisampled rendering, the actual
153 * number of samples allocated for the renderbuffer image is
154 * implementation dependent. However, the resulting value for
155 * RENDERBUFFER_SAMPLES is guaranteed to be greater than or equal
156 * to <samples> and no more than the next larger sample count supported
157 * by the implementation.
159 * Find the supported number of samples >= rb->NumSamples
161 if (rb
->NumSamples
> 0) {
164 if (ctx
->Const
.MaxSamples
> 1 && rb
->NumSamples
== 1) {
165 /* don't try num_samples = 1 with drivers that support real msaa */
168 start
= rb
->NumSamples
;
171 for (i
= start
; i
<= ctx
->Const
.MaxSamples
; i
++) {
172 format
= st_choose_renderbuffer_format(st
, internalFormat
, i
);
174 if (format
!= PIPE_FORMAT_NONE
) {
180 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0);
183 /* Not setting gl_renderbuffer::Format here will cause
184 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
186 if (format
== PIPE_FORMAT_NONE
) {
190 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
192 if (width
== 0 || height
== 0) {
193 /* if size is zero, nothing to allocate */
197 /* Setup new texture template.
199 memset(&templ
, 0, sizeof(templ
));
200 templ
.target
= st
->internal_target
;
201 templ
.format
= format
;
202 templ
.width0
= width
;
203 templ
.height0
= height
;
205 templ
.array_size
= 1;
206 templ
.nr_samples
= rb
->NumSamples
;
207 if (util_format_is_depth_or_stencil(format
)) {
208 templ
.bind
= PIPE_BIND_DEPTH_STENCIL
;
210 else if (strb
->Base
.Name
!= 0) {
211 /* this is a user-created renderbuffer */
212 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
215 /* this is a window-system buffer */
216 templ
.bind
= (PIPE_BIND_DISPLAY_TARGET
|
217 PIPE_BIND_RENDER_TARGET
);
220 strb
->texture
= screen
->resource_create(screen
, &templ
);
225 st_update_renderbuffer_surface(st
, strb
);
226 return strb
->surface
!= NULL
;
231 * gl_renderbuffer::Delete()
234 st_renderbuffer_delete(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
236 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
238 struct st_context
*st
= st_context(ctx
);
239 pipe_surface_release(st
->pipe
, &strb
->surface_srgb
);
240 pipe_surface_release(st
->pipe
, &strb
->surface_linear
);
241 strb
->surface
= NULL
;
243 pipe_resource_reference(&strb
->texture
, NULL
);
245 _mesa_delete_renderbuffer(ctx
, rb
);
250 * Called via ctx->Driver.NewRenderbuffer()
252 static struct gl_renderbuffer
*
253 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
255 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
258 _mesa_init_renderbuffer(&strb
->Base
, name
);
259 strb
->Base
.Delete
= st_renderbuffer_delete
;
260 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
268 * Allocate a renderbuffer for an on-screen window (not a user-created
269 * renderbuffer). The window system code determines the format.
271 struct gl_renderbuffer
*
272 st_new_renderbuffer_fb(enum pipe_format format
, unsigned samples
, boolean sw
)
274 struct st_renderbuffer
*strb
;
276 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
278 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
282 _mesa_init_renderbuffer(&strb
->Base
, 0);
283 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
284 strb
->Base
.NumSamples
= samples
;
285 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
286 strb
->Base
._BaseFormat
= _mesa_get_format_base_format(strb
->Base
.Format
);
290 case PIPE_FORMAT_B10G10R10A2_UNORM
:
291 strb
->Base
.InternalFormat
= GL_RGB10_A2
;
293 case PIPE_FORMAT_B10G10R10X2_UNORM
:
294 strb
->Base
.InternalFormat
= GL_RGB10
;
296 case PIPE_FORMAT_R8G8B8A8_UNORM
:
297 case PIPE_FORMAT_B8G8R8A8_UNORM
:
298 case PIPE_FORMAT_A8R8G8B8_UNORM
:
299 strb
->Base
.InternalFormat
= GL_RGBA8
;
301 case PIPE_FORMAT_R8G8B8X8_UNORM
:
302 case PIPE_FORMAT_B8G8R8X8_UNORM
:
303 case PIPE_FORMAT_X8R8G8B8_UNORM
:
304 strb
->Base
.InternalFormat
= GL_RGB8
;
306 case PIPE_FORMAT_R8G8B8A8_SRGB
:
307 case PIPE_FORMAT_B8G8R8A8_SRGB
:
308 case PIPE_FORMAT_A8R8G8B8_SRGB
:
309 strb
->Base
.InternalFormat
= GL_SRGB8_ALPHA8
;
311 case PIPE_FORMAT_R8G8B8X8_SRGB
:
312 case PIPE_FORMAT_B8G8R8X8_SRGB
:
313 case PIPE_FORMAT_X8R8G8B8_SRGB
:
314 strb
->Base
.InternalFormat
= GL_SRGB8
;
316 case PIPE_FORMAT_B5G5R5A1_UNORM
:
317 strb
->Base
.InternalFormat
= GL_RGB5_A1
;
319 case PIPE_FORMAT_B4G4R4A4_UNORM
:
320 strb
->Base
.InternalFormat
= GL_RGBA4
;
322 case PIPE_FORMAT_B5G6R5_UNORM
:
323 strb
->Base
.InternalFormat
= GL_RGB565
;
325 case PIPE_FORMAT_Z16_UNORM
:
326 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
328 case PIPE_FORMAT_Z32_UNORM
:
329 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
331 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
332 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
333 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
335 case PIPE_FORMAT_Z24X8_UNORM
:
336 case PIPE_FORMAT_X8Z24_UNORM
:
337 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT24
;
339 case PIPE_FORMAT_S8_UINT
:
340 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
342 case PIPE_FORMAT_R16G16B16A16_SNORM
:
344 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
346 case PIPE_FORMAT_R16G16B16A16_UNORM
:
347 strb
->Base
.InternalFormat
= GL_RGBA16
;
349 case PIPE_FORMAT_R8_UNORM
:
350 strb
->Base
.InternalFormat
= GL_R8
;
352 case PIPE_FORMAT_R8G8_UNORM
:
353 strb
->Base
.InternalFormat
= GL_RG8
;
355 case PIPE_FORMAT_R16_UNORM
:
356 strb
->Base
.InternalFormat
= GL_R16
;
358 case PIPE_FORMAT_R16G16_UNORM
:
359 strb
->Base
.InternalFormat
= GL_RG16
;
361 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
362 strb
->Base
.InternalFormat
= GL_RGBA32F
;
364 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
365 strb
->Base
.InternalFormat
= GL_RGBA16F
;
369 "Unexpected format %s in st_new_renderbuffer_fb",
370 util_format_name(format
));
375 /* st-specific methods */
376 strb
->Base
.Delete
= st_renderbuffer_delete
;
377 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
379 /* surface is allocated in st_renderbuffer_alloc_storage() */
380 strb
->surface
= NULL
;
387 * Create or update the pipe_surface of a FBO renderbuffer.
388 * This is usually called after st_finalize_texture.
391 st_update_renderbuffer_surface(struct st_context
*st
,
392 struct st_renderbuffer
*strb
)
394 struct pipe_context
*pipe
= st
->pipe
;
395 struct pipe_resource
*resource
= strb
->texture
;
396 const struct st_texture_object
*stTexObj
= NULL
;
397 unsigned rtt_width
= strb
->Base
.Width
;
398 unsigned rtt_height
= strb
->Base
.Height
;
399 unsigned rtt_depth
= strb
->Base
.Depth
;
402 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
403 * the format of strb->texture is linear (because we have no control over
404 * the format). Check strb->Base.Format instead of strb->texture->format
405 * to determine if the rb is sRGB-capable.
407 boolean enable_srgb
= st
->ctx
->Color
.sRGBEnabled
&&
408 _mesa_get_format_color_encoding(strb
->Base
.Format
) == GL_SRGB
;
409 enum pipe_format format
= resource
->format
;
412 stTexObj
= st_texture_object(strb
->Base
.TexImage
->TexObject
);
413 if (stTexObj
->surface_based
)
414 format
= stTexObj
->surface_format
;
417 format
= enable_srgb
? util_format_srgb(format
) : util_format_linear(format
);
419 if (resource
->target
== PIPE_TEXTURE_1D_ARRAY
) {
420 rtt_depth
= rtt_height
;
424 /* find matching mipmap level size */
426 for (level
= 0; level
<= resource
->last_level
; level
++) {
427 if (u_minify(resource
->width0
, level
) == rtt_width
&&
428 u_minify(resource
->height0
, level
) == rtt_height
&&
429 (resource
->target
!= PIPE_TEXTURE_3D
||
430 u_minify(resource
->depth0
, level
) == rtt_depth
)) {
434 assert(level
<= resource
->last_level
);
436 /* determine the layer bounds */
437 unsigned first_layer
, last_layer
;
438 if (strb
->rtt_layered
) {
440 last_layer
= util_max_layer(strb
->texture
, level
);
444 last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
447 /* Adjust for texture views */
448 if (strb
->is_rtt
&& resource
->array_size
> 1 &&
449 stTexObj
->base
.Immutable
) {
450 const struct gl_texture_object
*tex
= &stTexObj
->base
;
451 first_layer
+= tex
->MinLayer
;
452 if (!strb
->rtt_layered
)
453 last_layer
+= tex
->MinLayer
;
455 last_layer
= MIN2(first_layer
+ tex
->NumLayers
- 1, last_layer
);
458 struct pipe_surface
**psurf
=
459 enable_srgb
? &strb
->surface_srgb
: &strb
->surface_linear
;
460 struct pipe_surface
*surf
= *psurf
;
463 surf
->texture
->nr_samples
!= strb
->Base
.NumSamples
||
464 surf
->format
!= format
||
465 surf
->texture
!= resource
||
466 surf
->width
!= rtt_width
||
467 surf
->height
!= rtt_height
||
468 surf
->u
.tex
.level
!= level
||
469 surf
->u
.tex
.first_layer
!= first_layer
||
470 surf
->u
.tex
.last_layer
!= last_layer
) {
471 /* create a new pipe_surface */
472 struct pipe_surface surf_tmpl
;
473 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
474 surf_tmpl
.format
= format
;
475 surf_tmpl
.u
.tex
.level
= level
;
476 surf_tmpl
.u
.tex
.first_layer
= first_layer
;
477 surf_tmpl
.u
.tex
.last_layer
= last_layer
;
479 pipe_surface_release(pipe
, psurf
);
481 *psurf
= pipe
->create_surface(pipe
, resource
, &surf_tmpl
);
483 strb
->surface
= *psurf
;
488 * Return the pipe_resource which stores a particular texture image.
490 static struct pipe_resource
*
491 get_teximage_resource(struct gl_texture_object
*texObj
,
492 unsigned face
, unsigned level
)
494 struct st_texture_image
*stImg
=
495 st_texture_image(texObj
->Image
[face
][level
]);
502 * Called by ctx->Driver.RenderTexture
505 st_render_texture(struct gl_context
*ctx
,
506 struct gl_framebuffer
*fb
,
507 struct gl_renderbuffer_attachment
*att
)
509 struct st_context
*st
= st_context(ctx
);
510 struct pipe_context
*pipe
= st
->pipe
;
511 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
512 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
513 struct pipe_resource
*pt
;
515 if (!st_finalize_texture(ctx
, pipe
, att
->Texture
, att
->CubeMapFace
))
518 pt
= get_teximage_resource(att
->Texture
,
523 /* point renderbuffer at texobject */
525 strb
->rtt_face
= att
->CubeMapFace
;
526 strb
->rtt_slice
= att
->Zoffset
;
527 strb
->rtt_layered
= att
->Layered
;
528 pipe_resource_reference(&strb
->texture
, pt
);
530 st_update_renderbuffer_surface(st
, strb
);
532 /* Invalidate buffer state so that the pipe's framebuffer state
534 * That's where the new renderbuffer (which we just created) gets
535 * passed to the pipe as a (color/depth) render target.
537 st_invalidate_buffers(st
);
540 /* Need to trigger a call to update_framebuffer() since we just
541 * attached a new renderbuffer.
543 ctx
->NewState
|= _NEW_BUFFERS
;
548 * Called via ctx->Driver.FinishRenderTexture.
551 st_finish_render_texture(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
553 struct st_context
*st
= st_context(ctx
);
554 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
559 strb
->is_rtt
= FALSE
;
561 /* restore previous framebuffer state */
562 st_invalidate_buffers(st
);
568 st_fbo_invalid(const char *reason
)
570 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
571 _mesa_debug(NULL
, "Invalid FBO: %s\n", reason
);
577 * Validate a renderbuffer attachment for a particular set of bindings.
580 st_validate_attachment(struct gl_context
*ctx
,
581 struct pipe_screen
*screen
,
582 const struct gl_renderbuffer_attachment
*att
,
585 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
586 enum pipe_format format
;
587 mesa_format texFormat
;
590 /* Sanity check: we must be binding the surface as a (color) render target
591 * or depth/stencil target.
593 assert(bindings
== PIPE_BIND_RENDER_TARGET
||
594 bindings
== PIPE_BIND_DEPTH_STENCIL
);
596 /* Only validate texture attachments for now, since
597 * st_renderbuffer_alloc_storage makes sure that
598 * the format is supported.
600 if (att
->Type
!= GL_TEXTURE
)
603 if (!stObj
|| !stObj
->pt
)
606 format
= stObj
->pt
->format
;
607 texFormat
= att
->Renderbuffer
->TexImage
->TexFormat
;
609 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
610 * check for linear format support instead.
611 * Later when we create a surface, we change the format to a linear one. */
612 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
&&
613 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
614 const mesa_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
615 format
= st_mesa_format_to_pipe_format(st_context(ctx
), linearFormat
);
618 valid
= screen
->is_format_supported(screen
, format
,
620 stObj
->pt
->nr_samples
, bindings
);
622 st_fbo_invalid("Invalid format");
630 * Check that the framebuffer configuration is valid in terms of what
631 * the driver can support.
633 * For Gallium we only supports combined Z+stencil, not separate buffers.
636 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
638 struct st_context
*st
= st_context(ctx
);
639 struct pipe_screen
*screen
= st
->pipe
->screen
;
640 const struct gl_renderbuffer_attachment
*depth
=
641 &fb
->Attachment
[BUFFER_DEPTH
];
642 const struct gl_renderbuffer_attachment
*stencil
=
643 &fb
->Attachment
[BUFFER_STENCIL
];
645 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
646 boolean mixed_formats
=
647 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
649 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
650 st_fbo_invalid("Different Depth/Stencil buffer formats");
651 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
654 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
655 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
656 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
657 st_fbo_invalid("Separate Depth/Stencil buffers");
658 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
661 if (depth
->Type
== GL_TEXTURE
&&
662 stencil
->Type
== GL_TEXTURE
&&
663 depth
->Texture
!= stencil
->Texture
) {
664 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
665 st_fbo_invalid("Different Depth/Stencil textures");
669 if (!st_validate_attachment(ctx
, screen
, depth
, PIPE_BIND_DEPTH_STENCIL
)) {
670 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
671 st_fbo_invalid("Invalid depth attachment");
674 if (!st_validate_attachment(ctx
, screen
, stencil
, PIPE_BIND_DEPTH_STENCIL
)) {
675 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
676 st_fbo_invalid("Invalid stencil attachment");
679 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
680 struct gl_renderbuffer_attachment
*att
=
681 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
682 enum pipe_format format
;
684 if (!st_validate_attachment(ctx
, screen
, att
, PIPE_BIND_RENDER_TARGET
)) {
685 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
686 st_fbo_invalid("Invalid color attachment");
690 if (!mixed_formats
) {
691 /* Disallow mixed formats. */
692 if (att
->Type
!= GL_NONE
) {
693 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
698 if (first_format
== PIPE_FORMAT_NONE
) {
699 first_format
= format
;
700 } else if (format
!= first_format
) {
701 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
702 st_fbo_invalid("Mixed color formats");
711 * Called via glDrawBuffer. We only provide this driver function so that we
712 * can check if we need to allocate a new renderbuffer. Specifically, we
713 * don't usually allocate a front color buffer when using a double-buffered
714 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
715 * that buffer. Note, this is only for window system buffers, not user-
719 st_DrawBuffers(struct gl_context
*ctx
, GLsizei count
, const GLenum
*buffers
)
721 struct st_context
*st
= st_context(ctx
);
722 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
727 if (_mesa_is_winsys_fbo(fb
)) {
729 /* add the renderbuffers on demand */
730 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
731 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
733 if (idx
!= BUFFER_NONE
) {
734 st_manager_add_color_renderbuffer(st
, fb
, idx
);
742 * Called via glReadBuffer. As with st_DrawBuffers, we use this function
743 * to check if we need to allocate a renderbuffer on demand.
746 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
748 struct st_context
*st
= st_context(ctx
);
749 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
753 /* Check if we need to allocate a front color buffer.
754 * Front buffers are often allocated on demand (other color buffers are
755 * always allocated in advance).
757 if ((fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
||
758 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_RIGHT
) &&
759 fb
->Attachment
[fb
->_ColorReadBufferIndex
].Type
== GL_NONE
) {
760 assert(_mesa_is_winsys_fbo(fb
));
762 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
763 _mesa_update_state(ctx
);
764 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
771 * Called via ctx->Driver.MapRenderbuffer.
774 st_MapRenderbuffer(struct gl_context
*ctx
,
775 struct gl_renderbuffer
*rb
,
776 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
778 GLubyte
**mapOut
, GLint
*rowStrideOut
)
780 struct st_context
*st
= st_context(ctx
);
781 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
782 struct pipe_context
*pipe
= st
->pipe
;
783 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
;
803 /* Check for unexpected flags */
804 assert((mode
& ~(GL_MAP_READ_BIT
|
806 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
808 const enum pipe_transfer_usage transfer_flags
=
809 st_access_flags_to_transfer_flags(mode
, false);
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 transfer_flags
, 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
;