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"
57 #include "st_manager.h"
59 #include "util/format/u_format.h"
60 #include "util/u_inlines.h"
61 #include "util/u_surface.h"
65 st_renderbuffer_alloc_sw_storage(struct gl_context
* ctx
,
66 struct gl_renderbuffer
*rb
,
67 GLenum internalFormat
,
68 GLuint width
, GLuint height
)
70 struct st_context
*st
= st_context(ctx
);
71 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
72 enum pipe_format format
;
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.
84 format
= PIPE_FORMAT_R16G16B16A16_SNORM
;
87 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0, 0);
89 /* Not setting gl_renderbuffer::Format here will cause
90 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
92 if (format
== PIPE_FORMAT_NONE
) {
97 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
99 size
= _mesa_format_image_size(strb
->Base
.Format
, width
, height
, 1);
100 strb
->data
= malloc(size
);
101 return strb
->data
!= NULL
;
106 * gl_renderbuffer::AllocStorage()
107 * This is called to allocate the original drawing surface, and
108 * during window resize.
111 st_renderbuffer_alloc_storage(struct gl_context
* ctx
,
112 struct gl_renderbuffer
*rb
,
113 GLenum internalFormat
,
114 GLuint width
, GLuint height
)
116 struct st_context
*st
= st_context(ctx
);
117 struct pipe_screen
*screen
= st
->pipe
->screen
;
118 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
119 enum pipe_format format
= PIPE_FORMAT_NONE
;
120 struct pipe_resource templ
;
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 */
128 if (strb
->software
) {
129 return st_renderbuffer_alloc_sw_storage(ctx
, rb
, internalFormat
,
133 /* Free the old surface and texture
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
);
140 /* If an sRGB framebuffer is unsupported, sRGB formats behave like linear
143 if (!ctx
->Extensions
.EXT_sRGB
) {
144 internalFormat
= _mesa_get_linear_internalformat(internalFormat
);
147 /* Handle multisample renderbuffers first.
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.
160 * Find the supported number of samples >= rb->NumSamples
162 if (rb
->NumSamples
> 0) {
163 unsigned start
, start_storage
;
165 if (ctx
->Const
.MaxSamples
> 1 && rb
->NumSamples
== 1) {
166 /* don't try num_samples = 1 with drivers that support real msaa */
170 start
= rb
->NumSamples
;
171 start_storage
= rb
->NumStorageSamples
;
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
;
182 format
= st_choose_renderbuffer_format(st
, internalFormat
,
185 if (format
!= PIPE_FORMAT_NONE
) {
186 rb
->NumSamples
= samples
;
187 rb
->NumStorageSamples
= samples
;
192 /* Find a supported color format, samples >= storage_samples. */
193 for (unsigned storage_samples
= start_storage
;
194 storage_samples
<= ctx
->Const
.MaxColorFramebufferStorageSamples
;
196 for (unsigned samples
= MAX2(start
, storage_samples
);
197 samples
<= ctx
->Const
.MaxColorFramebufferSamples
;
199 format
= st_choose_renderbuffer_format(st
, internalFormat
,
203 if (format
!= PIPE_FORMAT_NONE
) {
204 rb
->NumSamples
= samples
;
205 rb
->NumStorageSamples
= storage_samples
;
213 for (unsigned samples
= start
; samples
<= ctx
->Const
.MaxSamples
;
215 format
= st_choose_renderbuffer_format(st
, internalFormat
,
218 if (format
!= PIPE_FORMAT_NONE
) {
219 rb
->NumSamples
= samples
;
220 rb
->NumStorageSamples
= samples
;
226 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0, 0);
229 /* Not setting gl_renderbuffer::Format here will cause
230 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
232 if (format
== PIPE_FORMAT_NONE
) {
236 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
238 if (width
== 0 || height
== 0) {
239 /* if size is zero, nothing to allocate */
243 /* Setup new texture template.
245 memset(&templ
, 0, sizeof(templ
));
246 templ
.target
= st
->internal_target
;
247 templ
.format
= format
;
248 templ
.width0
= width
;
249 templ
.height0
= height
;
251 templ
.array_size
= 1;
252 templ
.nr_samples
= rb
->NumSamples
;
253 templ
.nr_storage_samples
= rb
->NumStorageSamples
;
255 if (util_format_is_depth_or_stencil(format
)) {
256 templ
.bind
= PIPE_BIND_DEPTH_STENCIL
;
258 else if (strb
->Base
.Name
!= 0) {
259 /* this is a user-created renderbuffer */
260 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
263 /* this is a window-system buffer */
264 templ
.bind
= (PIPE_BIND_DISPLAY_TARGET
|
265 PIPE_BIND_RENDER_TARGET
);
268 strb
->texture
= screen
->resource_create(screen
, &templ
);
273 st_update_renderbuffer_surface(st
, strb
);
274 return strb
->surface
!= NULL
;
279 * gl_renderbuffer::Delete()
282 st_renderbuffer_delete(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
284 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
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
);
290 pipe_surface_release_no_context(&strb
->surface_srgb
);
291 pipe_surface_release_no_context(&strb
->surface_linear
);
293 strb
->surface
= NULL
;
294 pipe_resource_reference(&strb
->texture
, NULL
);
296 _mesa_delete_renderbuffer(ctx
, rb
);
301 * Called via ctx->Driver.NewRenderbuffer()
303 static struct gl_renderbuffer
*
304 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
306 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
309 _mesa_init_renderbuffer(&strb
->Base
, name
);
310 strb
->Base
.Delete
= st_renderbuffer_delete
;
311 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
319 * Allocate a renderbuffer for an on-screen window (not a user-created
320 * renderbuffer). The window system code determines the format.
322 struct gl_renderbuffer
*
323 st_new_renderbuffer_fb(enum pipe_format format
, unsigned samples
, boolean sw
)
325 struct st_renderbuffer
*strb
;
327 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
329 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
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
);
342 case PIPE_FORMAT_B10G10R10A2_UNORM
:
343 case PIPE_FORMAT_R10G10B10A2_UNORM
:
344 strb
->Base
.InternalFormat
= GL_RGB10_A2
;
346 case PIPE_FORMAT_R10G10B10X2_UNORM
:
347 case PIPE_FORMAT_B10G10R10X2_UNORM
:
348 strb
->Base
.InternalFormat
= GL_RGB10
;
350 case PIPE_FORMAT_R8G8B8A8_UNORM
:
351 case PIPE_FORMAT_B8G8R8A8_UNORM
:
352 case PIPE_FORMAT_A8R8G8B8_UNORM
:
353 strb
->Base
.InternalFormat
= GL_RGBA8
;
355 case PIPE_FORMAT_R8G8B8X8_UNORM
:
356 case PIPE_FORMAT_B8G8R8X8_UNORM
:
357 case PIPE_FORMAT_X8R8G8B8_UNORM
:
358 strb
->Base
.InternalFormat
= GL_RGB8
;
360 case PIPE_FORMAT_R8G8B8A8_SRGB
:
361 case PIPE_FORMAT_B8G8R8A8_SRGB
:
362 case PIPE_FORMAT_A8R8G8B8_SRGB
:
363 strb
->Base
.InternalFormat
= GL_SRGB8_ALPHA8
;
365 case PIPE_FORMAT_R8G8B8X8_SRGB
:
366 case PIPE_FORMAT_B8G8R8X8_SRGB
:
367 case PIPE_FORMAT_X8R8G8B8_SRGB
:
368 strb
->Base
.InternalFormat
= GL_SRGB8
;
370 case PIPE_FORMAT_B5G5R5A1_UNORM
:
371 strb
->Base
.InternalFormat
= GL_RGB5_A1
;
373 case PIPE_FORMAT_B4G4R4A4_UNORM
:
374 strb
->Base
.InternalFormat
= GL_RGBA4
;
376 case PIPE_FORMAT_B5G6R5_UNORM
:
377 strb
->Base
.InternalFormat
= GL_RGB565
;
379 case PIPE_FORMAT_Z16_UNORM
:
380 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
382 case PIPE_FORMAT_Z32_UNORM
:
383 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
385 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
386 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
387 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
389 case PIPE_FORMAT_Z24X8_UNORM
:
390 case PIPE_FORMAT_X8Z24_UNORM
:
391 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT24
;
393 case PIPE_FORMAT_S8_UINT
:
394 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
396 case PIPE_FORMAT_R16G16B16A16_SNORM
:
398 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
400 case PIPE_FORMAT_R16G16B16A16_UNORM
:
401 strb
->Base
.InternalFormat
= GL_RGBA16
;
403 case PIPE_FORMAT_R8_UNORM
:
404 strb
->Base
.InternalFormat
= GL_R8
;
406 case PIPE_FORMAT_R8G8_UNORM
:
407 strb
->Base
.InternalFormat
= GL_RG8
;
409 case PIPE_FORMAT_R16_UNORM
:
410 strb
->Base
.InternalFormat
= GL_R16
;
412 case PIPE_FORMAT_R16G16_UNORM
:
413 strb
->Base
.InternalFormat
= GL_RG16
;
415 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
416 strb
->Base
.InternalFormat
= GL_RGBA32F
;
418 case PIPE_FORMAT_R32G32B32X32_FLOAT
:
419 strb
->Base
.InternalFormat
= GL_RGB32F
;
421 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
422 strb
->Base
.InternalFormat
= GL_RGBA16F
;
424 case PIPE_FORMAT_R16G16B16X16_FLOAT
:
425 strb
->Base
.InternalFormat
= GL_RGB16F
;
429 "Unexpected format %s in st_new_renderbuffer_fb",
430 util_format_name(format
));
435 /* st-specific methods */
436 strb
->Base
.Delete
= st_renderbuffer_delete
;
437 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
439 /* surface is allocated in st_renderbuffer_alloc_storage() */
440 strb
->surface
= NULL
;
447 * Create or update the pipe_surface of a FBO renderbuffer.
448 * This is usually called after st_finalize_texture.
451 st_update_renderbuffer_surface(struct st_context
*st
,
452 struct st_renderbuffer
*strb
)
454 struct pipe_context
*pipe
= st
->pipe
;
455 struct pipe_resource
*resource
= strb
->texture
;
456 const struct st_texture_object
*stTexObj
= NULL
;
457 unsigned rtt_width
= strb
->Base
.Width
;
458 unsigned rtt_height
= strb
->Base
.Height
;
459 unsigned rtt_depth
= strb
->Base
.Depth
;
462 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
463 * the format of strb->texture is linear (because we have no control over
464 * the format). Check strb->Base.Format instead of strb->texture->format
465 * to determine if the rb is sRGB-capable.
467 boolean enable_srgb
= st
->ctx
->Color
.sRGBEnabled
&&
468 _mesa_is_format_srgb(strb
->Base
.Format
);
469 enum pipe_format format
= resource
->format
;
472 stTexObj
= st_texture_object(strb
->Base
.TexImage
->TexObject
);
473 if (stTexObj
->surface_based
)
474 format
= stTexObj
->surface_format
;
477 format
= enable_srgb
? util_format_srgb(format
) : util_format_linear(format
);
479 if (resource
->target
== PIPE_TEXTURE_1D_ARRAY
) {
480 rtt_depth
= rtt_height
;
484 /* find matching mipmap level size */
486 for (level
= 0; level
<= resource
->last_level
; level
++) {
487 if (u_minify(resource
->width0
, level
) == rtt_width
&&
488 u_minify(resource
->height0
, level
) == rtt_height
&&
489 (resource
->target
!= PIPE_TEXTURE_3D
||
490 u_minify(resource
->depth0
, level
) == rtt_depth
)) {
494 assert(level
<= resource
->last_level
);
496 /* determine the layer bounds */
497 unsigned first_layer
, last_layer
;
498 if (strb
->rtt_layered
) {
500 last_layer
= util_max_layer(strb
->texture
, level
);
504 last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
507 /* Adjust for texture views */
508 if (strb
->is_rtt
&& resource
->array_size
> 1 &&
509 stTexObj
->base
.Immutable
) {
510 const struct gl_texture_object
*tex
= &stTexObj
->base
;
511 first_layer
+= tex
->MinLayer
;
512 if (!strb
->rtt_layered
)
513 last_layer
+= tex
->MinLayer
;
515 last_layer
= MIN2(first_layer
+ tex
->NumLayers
- 1, last_layer
);
518 struct pipe_surface
**psurf
=
519 enable_srgb
? &strb
->surface_srgb
: &strb
->surface_linear
;
520 struct pipe_surface
*surf
= *psurf
;
523 surf
->texture
->nr_samples
!= strb
->Base
.NumSamples
||
524 surf
->texture
->nr_storage_samples
!= strb
->Base
.NumStorageSamples
||
525 surf
->format
!= format
||
526 surf
->texture
!= resource
||
527 surf
->width
!= rtt_width
||
528 surf
->height
!= rtt_height
||
529 surf
->nr_samples
!= strb
->rtt_nr_samples
||
530 surf
->u
.tex
.level
!= level
||
531 surf
->u
.tex
.first_layer
!= first_layer
||
532 surf
->u
.tex
.last_layer
!= last_layer
) {
533 /* create a new pipe_surface */
534 struct pipe_surface surf_tmpl
;
535 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
536 surf_tmpl
.format
= format
;
537 surf_tmpl
.nr_samples
= strb
->rtt_nr_samples
;
538 surf_tmpl
.u
.tex
.level
= level
;
539 surf_tmpl
.u
.tex
.first_layer
= first_layer
;
540 surf_tmpl
.u
.tex
.last_layer
= last_layer
;
542 pipe_surface_release(pipe
, psurf
);
544 *psurf
= pipe
->create_surface(pipe
, resource
, &surf_tmpl
);
546 strb
->surface
= *psurf
;
551 * Return the pipe_resource which stores a particular texture image.
553 static struct pipe_resource
*
554 get_teximage_resource(struct gl_texture_object
*texObj
,
555 unsigned face
, unsigned level
)
557 struct st_texture_image
*stImg
=
558 st_texture_image(texObj
->Image
[face
][level
]);
565 * Called by ctx->Driver.RenderTexture
568 st_render_texture(struct gl_context
*ctx
,
569 struct gl_framebuffer
*fb
,
570 struct gl_renderbuffer_attachment
*att
)
572 struct st_context
*st
= st_context(ctx
);
573 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
574 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
575 struct pipe_resource
*pt
;
577 pt
= get_teximage_resource(att
->Texture
,
582 /* point renderbuffer at texobject */
584 strb
->rtt_face
= att
->CubeMapFace
;
585 strb
->rtt_slice
= att
->Zoffset
;
586 strb
->rtt_layered
= att
->Layered
;
587 strb
->rtt_nr_samples
= att
->NumSamples
;
588 pipe_resource_reference(&strb
->texture
, pt
);
590 st_update_renderbuffer_surface(st
, strb
);
592 /* Invalidate buffer state so that the pipe's framebuffer state
594 * That's where the new renderbuffer (which we just created) gets
595 * passed to the pipe as a (color/depth) render target.
597 st_invalidate_buffers(st
);
600 /* Need to trigger a call to update_framebuffer() since we just
601 * attached a new renderbuffer.
603 ctx
->NewState
|= _NEW_BUFFERS
;
608 * Called via ctx->Driver.FinishRenderTexture.
611 st_finish_render_texture(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
613 struct st_context
*st
= st_context(ctx
);
614 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
619 strb
->is_rtt
= FALSE
;
621 /* restore previous framebuffer state */
622 st_invalidate_buffers(st
);
628 st_fbo_invalid(const char *reason
)
630 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
631 _mesa_debug(NULL
, "Invalid FBO: %s\n", reason
);
637 * Validate a renderbuffer attachment for a particular set of bindings.
640 st_validate_attachment(struct gl_context
*ctx
,
641 struct pipe_screen
*screen
,
642 const struct gl_renderbuffer_attachment
*att
,
645 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
646 enum pipe_format format
;
647 mesa_format texFormat
;
650 /* Sanity check: we must be binding the surface as a (color) render target
651 * or depth/stencil target.
653 assert(bindings
== PIPE_BIND_RENDER_TARGET
||
654 bindings
== PIPE_BIND_DEPTH_STENCIL
);
656 /* Only validate texture attachments for now, since
657 * st_renderbuffer_alloc_storage makes sure that
658 * the format is supported.
660 if (att
->Type
!= GL_TEXTURE
)
663 if (!stObj
|| !stObj
->pt
)
666 format
= stObj
->pt
->format
;
667 texFormat
= att
->Renderbuffer
->TexImage
->TexFormat
;
669 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
670 * check for linear format support instead.
671 * Later when we create a surface, we change the format to a linear one. */
672 if (!ctx
->Extensions
.EXT_sRGB
&& _mesa_is_format_srgb(texFormat
)) {
673 const mesa_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
674 format
= st_mesa_format_to_pipe_format(st_context(ctx
), linearFormat
);
677 valid
= screen
->is_format_supported(screen
, format
,
679 stObj
->pt
->nr_samples
,
680 stObj
->pt
->nr_storage_samples
,
683 st_fbo_invalid("Invalid format");
691 * Check that the framebuffer configuration is valid in terms of what
692 * the driver can support.
694 * For Gallium we only supports combined Z+stencil, not separate buffers.
697 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
699 struct st_context
*st
= st_context(ctx
);
700 struct pipe_screen
*screen
= st
->pipe
->screen
;
701 const struct gl_renderbuffer_attachment
*depth
=
702 &fb
->Attachment
[BUFFER_DEPTH
];
703 const struct gl_renderbuffer_attachment
*stencil
=
704 &fb
->Attachment
[BUFFER_STENCIL
];
706 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
707 boolean mixed_formats
=
708 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
710 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
711 st_fbo_invalid("Different Depth/Stencil buffer formats");
712 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
715 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
716 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
717 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
718 st_fbo_invalid("Separate Depth/Stencil buffers");
719 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
722 if (depth
->Type
== GL_TEXTURE
&&
723 stencil
->Type
== GL_TEXTURE
&&
724 depth
->Texture
!= stencil
->Texture
) {
725 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
726 st_fbo_invalid("Different Depth/Stencil textures");
730 if (!st_validate_attachment(ctx
, screen
, depth
, PIPE_BIND_DEPTH_STENCIL
)) {
731 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
732 st_fbo_invalid("Invalid depth attachment");
735 if (!st_validate_attachment(ctx
, screen
, stencil
, PIPE_BIND_DEPTH_STENCIL
)) {
736 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
737 st_fbo_invalid("Invalid stencil attachment");
740 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
741 struct gl_renderbuffer_attachment
*att
=
742 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
743 enum pipe_format format
;
745 if (!st_validate_attachment(ctx
, screen
, att
, PIPE_BIND_RENDER_TARGET
)) {
746 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
747 st_fbo_invalid("Invalid color attachment");
751 if (!mixed_formats
) {
752 /* Disallow mixed formats. */
753 if (att
->Type
!= GL_NONE
) {
754 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
759 if (first_format
== PIPE_FORMAT_NONE
) {
760 first_format
= format
;
761 } else if (format
!= first_format
) {
762 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
763 st_fbo_invalid("Mixed color formats");
772 * Called by ctx->Driver.DiscardFramebuffer
775 st_discard_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
776 struct gl_renderbuffer_attachment
*att
)
778 struct st_context
*st
= st_context(ctx
);
779 struct pipe_resource
*prsc
;
781 if (!att
->Renderbuffer
|| !att
->Complete
)
784 prsc
= st_renderbuffer(att
->Renderbuffer
)->surface
->texture
;
786 /* using invalidate_resource will only work for simple 2D resources */
787 if (prsc
->depth0
!= 1 || prsc
->array_size
!= 1 || prsc
->last_level
!= 0)
790 if (st
->pipe
->invalidate_resource
)
791 st
->pipe
->invalidate_resource(st
->pipe
, prsc
);
796 * Called via glDrawBuffer. We only provide this driver function so that we
797 * can check if we need to allocate a new renderbuffer. Specifically, we
798 * don't usually allocate a front color buffer when using a double-buffered
799 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
800 * that buffer. Note, this is only for window system buffers, not user-
804 st_DrawBufferAllocate(struct gl_context
*ctx
)
806 struct st_context
*st
= st_context(ctx
);
807 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
809 if (_mesa_is_winsys_fbo(fb
)) {
811 /* add the renderbuffers on demand */
812 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
813 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
815 if (idx
!= BUFFER_NONE
) {
816 st_manager_add_color_renderbuffer(st
, fb
, idx
);
824 * Called via glReadBuffer. As with st_DrawBufferAllocate, we use this
825 * function to check if we need to allocate a renderbuffer on demand.
828 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
830 struct st_context
*st
= st_context(ctx
);
831 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
835 /* Check if we need to allocate a front color buffer.
836 * Front buffers are often allocated on demand (other color buffers are
837 * always allocated in advance).
839 if ((fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
||
840 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_RIGHT
) &&
841 fb
->Attachment
[fb
->_ColorReadBufferIndex
].Type
== GL_NONE
) {
842 assert(_mesa_is_winsys_fbo(fb
));
844 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
845 _mesa_update_state(ctx
);
846 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
853 * Called via ctx->Driver.MapRenderbuffer.
856 st_MapRenderbuffer(struct gl_context
*ctx
,
857 struct gl_renderbuffer
*rb
,
858 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
860 GLubyte
**mapOut
, GLint
*rowStrideOut
,
863 struct st_context
*st
= st_context(ctx
);
864 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
865 struct pipe_context
*pipe
= st
->pipe
;
866 const GLboolean invert
= flip_y
;
870 if (strb
->software
) {
871 /* software-allocated renderbuffer (probably an accum buffer) */
873 GLint bpp
= _mesa_get_format_bytes(strb
->Base
.Format
);
874 GLint stride
= _mesa_format_row_stride(strb
->Base
.Format
,
876 *mapOut
= (GLubyte
*) strb
->data
+ y
* stride
+ x
* bpp
;
877 *rowStrideOut
= stride
;
886 /* Check for unexpected flags */
887 assert((mode
& ~(GL_MAP_READ_BIT
|
889 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
891 const enum pipe_transfer_usage transfer_flags
=
892 st_access_flags_to_transfer_flags(mode
, false);
894 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
895 * 'invert' will be true for window-system buffers and false for
896 * user-allocated renderbuffers and textures.
899 y2
= strb
->Base
.Height
- y
- h
;
903 map
= pipe_transfer_map(pipe
,
905 strb
->surface
->u
.tex
.level
,
906 strb
->surface
->u
.tex
.first_layer
,
907 transfer_flags
, x
, y2
, w
, h
, &strb
->transfer
);
910 *rowStrideOut
= -(int) strb
->transfer
->stride
;
911 map
+= (h
- 1) * strb
->transfer
->stride
;
914 *rowStrideOut
= strb
->transfer
->stride
;
926 * Called via ctx->Driver.UnmapRenderbuffer.
929 st_UnmapRenderbuffer(struct gl_context
*ctx
,
930 struct gl_renderbuffer
*rb
)
932 struct st_context
*st
= st_context(ctx
);
933 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
934 struct pipe_context
*pipe
= st
->pipe
;
936 if (strb
->software
) {
937 /* software-allocated renderbuffer (probably an accum buffer) */
941 pipe_transfer_unmap(pipe
, strb
->transfer
);
942 strb
->transfer
= NULL
;
947 * Called via ctx->Driver.EvaluateDepthValues.
950 st_EvaluateDepthValues(struct gl_context
*ctx
)
952 struct st_context
*st
= st_context(ctx
);
954 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
956 st
->pipe
->evaluate_depth_buffer(st
->pipe
);
961 st_init_fbo_functions(struct dd_function_table
*functions
)
963 functions
->NewFramebuffer
= _mesa_new_framebuffer
;
964 functions
->NewRenderbuffer
= st_new_renderbuffer
;
965 functions
->FramebufferRenderbuffer
= _mesa_FramebufferRenderbuffer_sw
;
966 functions
->RenderTexture
= st_render_texture
;
967 functions
->FinishRenderTexture
= st_finish_render_texture
;
968 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
969 functions
->DiscardFramebuffer
= st_discard_framebuffer
;
971 functions
->DrawBufferAllocate
= st_DrawBufferAllocate
;
972 functions
->ReadBuffer
= st_ReadBuffer
;
974 functions
->MapRenderbuffer
= st_MapRenderbuffer
;
975 functions
->UnmapRenderbuffer
= st_UnmapRenderbuffer
;
976 functions
->EvaluateDepthValues
= st_EvaluateDepthValues
;