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 * So let's find the supported number of samples closest to NumSamples.
160 if (rb
->NumSamples
> 0) {
163 for (i
= MAX2(2, rb
->NumSamples
); i
<= ctx
->Const
.MaxSamples
; i
++) {
164 format
= st_choose_renderbuffer_format(st
, internalFormat
, i
);
166 if (format
!= PIPE_FORMAT_NONE
) {
172 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0);
175 /* Not setting gl_renderbuffer::Format here will cause
176 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
178 if (format
== PIPE_FORMAT_NONE
) {
182 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
184 if (width
== 0 || height
== 0) {
185 /* if size is zero, nothing to allocate */
189 /* Setup new texture template.
191 memset(&templ
, 0, sizeof(templ
));
192 templ
.target
= st
->internal_target
;
193 templ
.format
= format
;
194 templ
.width0
= width
;
195 templ
.height0
= height
;
197 templ
.array_size
= 1;
198 templ
.nr_samples
= rb
->NumSamples
;
199 if (util_format_is_depth_or_stencil(format
)) {
200 templ
.bind
= PIPE_BIND_DEPTH_STENCIL
;
202 else if (strb
->Base
.Name
!= 0) {
203 /* this is a user-created renderbuffer */
204 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
207 /* this is a window-system buffer */
208 templ
.bind
= (PIPE_BIND_DISPLAY_TARGET
|
209 PIPE_BIND_RENDER_TARGET
);
212 strb
->texture
= screen
->resource_create(screen
, &templ
);
217 st_update_renderbuffer_surface(st
, strb
);
218 return strb
->surface
!= NULL
;
223 * gl_renderbuffer::Delete()
226 st_renderbuffer_delete(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
228 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
230 struct st_context
*st
= st_context(ctx
);
231 pipe_surface_release(st
->pipe
, &strb
->surface_srgb
);
232 pipe_surface_release(st
->pipe
, &strb
->surface_linear
);
233 strb
->surface
= NULL
;
235 pipe_resource_reference(&strb
->texture
, NULL
);
237 _mesa_delete_renderbuffer(ctx
, rb
);
242 * Called via ctx->Driver.NewRenderbuffer()
244 static struct gl_renderbuffer
*
245 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
247 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
250 _mesa_init_renderbuffer(&strb
->Base
, name
);
251 strb
->Base
.Delete
= st_renderbuffer_delete
;
252 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
260 * Allocate a renderbuffer for an on-screen window (not a user-created
261 * renderbuffer). The window system code determines the format.
263 struct gl_renderbuffer
*
264 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
266 struct st_renderbuffer
*strb
;
268 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
270 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
274 _mesa_init_renderbuffer(&strb
->Base
, 0);
275 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
276 strb
->Base
.NumSamples
= samples
;
277 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
278 strb
->Base
._BaseFormat
= _mesa_get_format_base_format(strb
->Base
.Format
);
282 case PIPE_FORMAT_R8G8B8A8_UNORM
:
283 case PIPE_FORMAT_B8G8R8A8_UNORM
:
284 case PIPE_FORMAT_A8R8G8B8_UNORM
:
285 strb
->Base
.InternalFormat
= GL_RGBA8
;
287 case PIPE_FORMAT_R8G8B8X8_UNORM
:
288 case PIPE_FORMAT_B8G8R8X8_UNORM
:
289 case PIPE_FORMAT_X8R8G8B8_UNORM
:
290 strb
->Base
.InternalFormat
= GL_RGB8
;
292 case PIPE_FORMAT_R8G8B8A8_SRGB
:
293 case PIPE_FORMAT_B8G8R8A8_SRGB
:
294 case PIPE_FORMAT_A8R8G8B8_SRGB
:
295 strb
->Base
.InternalFormat
= GL_SRGB8_ALPHA8
;
297 case PIPE_FORMAT_R8G8B8X8_SRGB
:
298 case PIPE_FORMAT_B8G8R8X8_SRGB
:
299 case PIPE_FORMAT_X8R8G8B8_SRGB
:
300 strb
->Base
.InternalFormat
= GL_SRGB8
;
302 case PIPE_FORMAT_B5G5R5A1_UNORM
:
303 strb
->Base
.InternalFormat
= GL_RGB5_A1
;
305 case PIPE_FORMAT_B4G4R4A4_UNORM
:
306 strb
->Base
.InternalFormat
= GL_RGBA4
;
308 case PIPE_FORMAT_B5G6R5_UNORM
:
309 strb
->Base
.InternalFormat
= GL_RGB565
;
311 case PIPE_FORMAT_Z16_UNORM
:
312 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
314 case PIPE_FORMAT_Z32_UNORM
:
315 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
317 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
318 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
319 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
321 case PIPE_FORMAT_Z24X8_UNORM
:
322 case PIPE_FORMAT_X8Z24_UNORM
:
323 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT24
;
325 case PIPE_FORMAT_S8_UINT
:
326 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
328 case PIPE_FORMAT_R16G16B16A16_SNORM
:
330 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
332 case PIPE_FORMAT_R16G16B16A16_UNORM
:
333 strb
->Base
.InternalFormat
= GL_RGBA16
;
335 case PIPE_FORMAT_R8_UNORM
:
336 strb
->Base
.InternalFormat
= GL_R8
;
338 case PIPE_FORMAT_R8G8_UNORM
:
339 strb
->Base
.InternalFormat
= GL_RG8
;
341 case PIPE_FORMAT_R16_UNORM
:
342 strb
->Base
.InternalFormat
= GL_R16
;
344 case PIPE_FORMAT_R16G16_UNORM
:
345 strb
->Base
.InternalFormat
= GL_RG16
;
347 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
348 strb
->Base
.InternalFormat
= GL_RGBA32F
;
350 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
351 strb
->Base
.InternalFormat
= GL_RGBA16F
;
355 "Unexpected format %s in st_new_renderbuffer_fb",
356 util_format_name(format
));
361 /* st-specific methods */
362 strb
->Base
.Delete
= st_renderbuffer_delete
;
363 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
365 /* surface is allocated in st_renderbuffer_alloc_storage() */
366 strb
->surface
= NULL
;
373 * Create or update the pipe_surface of a FBO renderbuffer.
374 * This is usually called after st_finalize_texture.
377 st_update_renderbuffer_surface(struct st_context
*st
,
378 struct st_renderbuffer
*strb
)
380 struct pipe_context
*pipe
= st
->pipe
;
381 struct pipe_resource
*resource
= strb
->texture
;
382 const struct st_texture_object
*stTexObj
= NULL
;
383 unsigned rtt_width
= strb
->Base
.Width
;
384 unsigned rtt_height
= strb
->Base
.Height
;
385 unsigned rtt_depth
= strb
->Base
.Depth
;
388 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
389 * the format of strb->texture is linear (because we have no control over
390 * the format). Check strb->Base.Format instead of strb->texture->format
391 * to determine if the rb is sRGB-capable.
393 boolean enable_srgb
= st
->ctx
->Color
.sRGBEnabled
&&
394 _mesa_get_format_color_encoding(strb
->Base
.Format
) == GL_SRGB
;
395 enum pipe_format format
= resource
->format
;
398 stTexObj
= st_texture_object(strb
->Base
.TexImage
->TexObject
);
399 if (stTexObj
->surface_based
)
400 format
= stTexObj
->surface_format
;
403 format
= enable_srgb
? util_format_srgb(format
) : util_format_linear(format
);
405 if (resource
->target
== PIPE_TEXTURE_1D_ARRAY
) {
406 rtt_depth
= rtt_height
;
410 /* find matching mipmap level size */
412 for (level
= 0; level
<= resource
->last_level
; level
++) {
413 if (u_minify(resource
->width0
, level
) == rtt_width
&&
414 u_minify(resource
->height0
, level
) == rtt_height
&&
415 (resource
->target
!= PIPE_TEXTURE_3D
||
416 u_minify(resource
->depth0
, level
) == rtt_depth
)) {
420 assert(level
<= resource
->last_level
);
422 /* determine the layer bounds */
423 unsigned first_layer
, last_layer
;
424 if (strb
->rtt_layered
) {
426 last_layer
= util_max_layer(strb
->texture
, level
);
430 last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
433 /* Adjust for texture views */
434 if (strb
->is_rtt
&& resource
->array_size
> 1 &&
435 stTexObj
->base
.Immutable
) {
436 const struct gl_texture_object
*tex
= &stTexObj
->base
;
437 first_layer
+= tex
->MinLayer
;
438 if (!strb
->rtt_layered
)
439 last_layer
+= tex
->MinLayer
;
441 last_layer
= MIN2(first_layer
+ tex
->NumLayers
- 1, last_layer
);
444 struct pipe_surface
**psurf
=
445 enable_srgb
? &strb
->surface_srgb
: &strb
->surface_linear
;
446 struct pipe_surface
*surf
= *psurf
;
449 surf
->texture
->nr_samples
!= strb
->Base
.NumSamples
||
450 surf
->format
!= format
||
451 surf
->texture
!= resource
||
452 surf
->width
!= rtt_width
||
453 surf
->height
!= rtt_height
||
454 surf
->u
.tex
.level
!= level
||
455 surf
->u
.tex
.first_layer
!= first_layer
||
456 surf
->u
.tex
.last_layer
!= last_layer
) {
457 /* create a new pipe_surface */
458 struct pipe_surface surf_tmpl
;
459 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
460 surf_tmpl
.format
= format
;
461 surf_tmpl
.u
.tex
.level
= level
;
462 surf_tmpl
.u
.tex
.first_layer
= first_layer
;
463 surf_tmpl
.u
.tex
.last_layer
= last_layer
;
465 pipe_surface_release(pipe
, psurf
);
467 *psurf
= pipe
->create_surface(pipe
, resource
, &surf_tmpl
);
469 strb
->surface
= *psurf
;
474 * Return the pipe_resource which stores a particular texture image.
476 static struct pipe_resource
*
477 get_teximage_resource(struct gl_texture_object
*texObj
,
478 unsigned face
, unsigned level
)
480 struct st_texture_image
*stImg
=
481 st_texture_image(texObj
->Image
[face
][level
]);
488 * Called by ctx->Driver.RenderTexture
491 st_render_texture(struct gl_context
*ctx
,
492 struct gl_framebuffer
*fb
,
493 struct gl_renderbuffer_attachment
*att
)
495 struct st_context
*st
= st_context(ctx
);
496 struct pipe_context
*pipe
= st
->pipe
;
497 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
498 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
499 struct pipe_resource
*pt
;
501 if (!st_finalize_texture(ctx
, pipe
, att
->Texture
, att
->CubeMapFace
))
504 pt
= get_teximage_resource(att
->Texture
,
509 /* point renderbuffer at texobject */
511 strb
->rtt_face
= att
->CubeMapFace
;
512 strb
->rtt_slice
= att
->Zoffset
;
513 strb
->rtt_layered
= att
->Layered
;
514 pipe_resource_reference(&strb
->texture
, pt
);
516 st_update_renderbuffer_surface(st
, strb
);
518 /* Invalidate buffer state so that the pipe's framebuffer state
520 * That's where the new renderbuffer (which we just created) gets
521 * passed to the pipe as a (color/depth) render target.
523 st_invalidate_buffers(st
);
526 /* Need to trigger a call to update_framebuffer() since we just
527 * attached a new renderbuffer.
529 ctx
->NewState
|= _NEW_BUFFERS
;
534 * Called via ctx->Driver.FinishRenderTexture.
537 st_finish_render_texture(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
539 struct st_context
*st
= st_context(ctx
);
540 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
545 strb
->is_rtt
= FALSE
;
547 /* restore previous framebuffer state */
548 st_invalidate_buffers(st
);
554 st_fbo_invalid(const char *reason
)
556 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
557 _mesa_debug(NULL
, "Invalid FBO: %s\n", reason
);
563 * Validate a renderbuffer attachment for a particular set of bindings.
566 st_validate_attachment(struct gl_context
*ctx
,
567 struct pipe_screen
*screen
,
568 const struct gl_renderbuffer_attachment
*att
,
571 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
572 enum pipe_format format
;
573 mesa_format texFormat
;
576 /* Sanity check: we must be binding the surface as a (color) render target
577 * or depth/stencil target.
579 assert(bindings
== PIPE_BIND_RENDER_TARGET
||
580 bindings
== PIPE_BIND_DEPTH_STENCIL
);
582 /* Only validate texture attachments for now, since
583 * st_renderbuffer_alloc_storage makes sure that
584 * the format is supported.
586 if (att
->Type
!= GL_TEXTURE
)
589 if (!stObj
|| !stObj
->pt
)
592 format
= stObj
->pt
->format
;
593 texFormat
= att
->Renderbuffer
->TexImage
->TexFormat
;
595 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
596 * check for linear format support instead.
597 * Later when we create a surface, we change the format to a linear one. */
598 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
&&
599 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
600 const mesa_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
601 format
= st_mesa_format_to_pipe_format(st_context(ctx
), linearFormat
);
604 valid
= screen
->is_format_supported(screen
, format
,
606 stObj
->pt
->nr_samples
, bindings
);
608 st_fbo_invalid("Invalid format");
616 * Check that the framebuffer configuration is valid in terms of what
617 * the driver can support.
619 * For Gallium we only supports combined Z+stencil, not separate buffers.
622 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
624 struct st_context
*st
= st_context(ctx
);
625 struct pipe_screen
*screen
= st
->pipe
->screen
;
626 const struct gl_renderbuffer_attachment
*depth
=
627 &fb
->Attachment
[BUFFER_DEPTH
];
628 const struct gl_renderbuffer_attachment
*stencil
=
629 &fb
->Attachment
[BUFFER_STENCIL
];
631 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
632 boolean mixed_formats
=
633 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
635 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
636 st_fbo_invalid("Different Depth/Stencil buffer formats");
637 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
640 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
641 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
642 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
643 st_fbo_invalid("Separate Depth/Stencil buffers");
644 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
647 if (depth
->Type
== GL_TEXTURE
&&
648 stencil
->Type
== GL_TEXTURE
&&
649 depth
->Texture
!= stencil
->Texture
) {
650 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
651 st_fbo_invalid("Different Depth/Stencil textures");
655 if (!st_validate_attachment(ctx
, screen
, depth
, PIPE_BIND_DEPTH_STENCIL
)) {
656 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
657 st_fbo_invalid("Invalid depth attachment");
660 if (!st_validate_attachment(ctx
, screen
, stencil
, PIPE_BIND_DEPTH_STENCIL
)) {
661 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
662 st_fbo_invalid("Invalid stencil attachment");
665 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
666 struct gl_renderbuffer_attachment
*att
=
667 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
668 enum pipe_format format
;
670 if (!st_validate_attachment(ctx
, screen
, att
, PIPE_BIND_RENDER_TARGET
)) {
671 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
672 st_fbo_invalid("Invalid color attachment");
676 if (!mixed_formats
) {
677 /* Disallow mixed formats. */
678 if (att
->Type
!= GL_NONE
) {
679 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
684 if (first_format
== PIPE_FORMAT_NONE
) {
685 first_format
= format
;
686 } else if (format
!= first_format
) {
687 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
688 st_fbo_invalid("Mixed color formats");
697 * Called via glDrawBuffer. We only provide this driver function so that we
698 * can check if we need to allocate a new renderbuffer. Specifically, we
699 * don't usually allocate a front color buffer when using a double-buffered
700 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
701 * that buffer. Note, this is only for window system buffers, not user-
705 st_DrawBuffers(struct gl_context
*ctx
, GLsizei count
, const GLenum
*buffers
)
707 struct st_context
*st
= st_context(ctx
);
708 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
713 if (_mesa_is_winsys_fbo(fb
)) {
715 /* add the renderbuffers on demand */
716 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
717 GLint idx
= fb
->_ColorDrawBufferIndexes
[i
];
720 st_manager_add_color_renderbuffer(st
, fb
, idx
);
728 * Called via glReadBuffer. As with st_DrawBuffers, we use this function
729 * to check if we need to allocate a renderbuffer on demand.
732 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
734 struct st_context
*st
= st_context(ctx
);
735 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
739 /* Check if we need to allocate a front color buffer.
740 * Front buffers are often allocated on demand (other color buffers are
741 * always allocated in advance).
743 if ((fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
||
744 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_RIGHT
) &&
745 fb
->Attachment
[fb
->_ColorReadBufferIndex
].Type
== GL_NONE
) {
746 assert(_mesa_is_winsys_fbo(fb
));
748 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
749 _mesa_update_state(ctx
);
750 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
757 * Called via ctx->Driver.MapRenderbuffer.
760 st_MapRenderbuffer(struct gl_context
*ctx
,
761 struct gl_renderbuffer
*rb
,
762 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
764 GLubyte
**mapOut
, GLint
*rowStrideOut
)
766 struct st_context
*st
= st_context(ctx
);
767 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
768 struct pipe_context
*pipe
= st
->pipe
;
769 const GLboolean invert
= rb
->Name
== 0;
774 if (strb
->software
) {
775 /* software-allocated renderbuffer (probably an accum buffer) */
777 GLint bpp
= _mesa_get_format_bytes(strb
->Base
.Format
);
778 GLint stride
= _mesa_format_row_stride(strb
->Base
.Format
,
780 *mapOut
= (GLubyte
*) strb
->data
+ y
* stride
+ x
* bpp
;
781 *rowStrideOut
= stride
;
791 if (mode
& GL_MAP_READ_BIT
)
792 usage
|= PIPE_TRANSFER_READ
;
793 if (mode
& GL_MAP_WRITE_BIT
)
794 usage
|= PIPE_TRANSFER_WRITE
;
795 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
796 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
798 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
799 * 'invert' will be true for window-system buffers and false for
800 * user-allocated renderbuffers and textures.
803 y2
= strb
->Base
.Height
- y
- h
;
807 map
= pipe_transfer_map(pipe
,
809 strb
->surface
->u
.tex
.level
,
810 strb
->surface
->u
.tex
.first_layer
,
811 usage
, x
, y2
, w
, h
, &strb
->transfer
);
814 *rowStrideOut
= -(int) strb
->transfer
->stride
;
815 map
+= (h
- 1) * strb
->transfer
->stride
;
818 *rowStrideOut
= strb
->transfer
->stride
;
830 * Called via ctx->Driver.UnmapRenderbuffer.
833 st_UnmapRenderbuffer(struct gl_context
*ctx
,
834 struct gl_renderbuffer
*rb
)
836 struct st_context
*st
= st_context(ctx
);
837 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
838 struct pipe_context
*pipe
= st
->pipe
;
840 if (strb
->software
) {
841 /* software-allocated renderbuffer (probably an accum buffer) */
845 pipe_transfer_unmap(pipe
, strb
->transfer
);
846 strb
->transfer
= NULL
;
852 st_init_fbo_functions(struct dd_function_table
*functions
)
854 functions
->NewFramebuffer
= _mesa_new_framebuffer
;
855 functions
->NewRenderbuffer
= st_new_renderbuffer
;
856 functions
->FramebufferRenderbuffer
= _mesa_FramebufferRenderbuffer_sw
;
857 functions
->RenderTexture
= st_render_texture
;
858 functions
->FinishRenderTexture
= st_finish_render_texture
;
859 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
861 functions
->DrawBuffers
= st_DrawBuffers
;
862 functions
->ReadBuffer
= st_ReadBuffer
;
864 functions
->MapRenderbuffer
= st_MapRenderbuffer
;
865 functions
->UnmapRenderbuffer
= st_UnmapRenderbuffer
;