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, 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) {
162 unsigned start
, start_storage
;
164 if (ctx
->Const
.MaxSamples
> 1 && rb
->NumSamples
== 1) {
165 /* don't try num_samples = 1 with drivers that support real msaa */
169 start
= rb
->NumSamples
;
170 start_storage
= rb
->NumStorageSamples
;
173 if (ctx
->Extensions
.AMD_framebuffer_multisample_advanced
) {
174 if (rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
175 rb
->_BaseFormat
== GL_DEPTH_STENCIL
||
176 rb
->_BaseFormat
== GL_STENCIL_INDEX
) {
177 /* Find a supported depth-stencil format. */
178 for (unsigned samples
= start
;
179 samples
<= ctx
->Const
.MaxDepthStencilFramebufferSamples
;
181 format
= st_choose_renderbuffer_format(st
, internalFormat
,
184 if (format
!= PIPE_FORMAT_NONE
) {
185 rb
->NumSamples
= samples
;
186 rb
->NumStorageSamples
= samples
;
191 /* Find a supported color format, samples >= storage_samples. */
192 for (unsigned storage_samples
= start_storage
;
193 storage_samples
<= ctx
->Const
.MaxColorFramebufferStorageSamples
;
195 for (unsigned samples
= MAX2(start
, storage_samples
);
196 samples
<= ctx
->Const
.MaxColorFramebufferSamples
;
198 format
= st_choose_renderbuffer_format(st
, internalFormat
,
202 if (format
!= PIPE_FORMAT_NONE
) {
203 rb
->NumSamples
= samples
;
204 rb
->NumStorageSamples
= storage_samples
;
212 for (unsigned samples
= start
; samples
<= ctx
->Const
.MaxSamples
;
214 format
= st_choose_renderbuffer_format(st
, internalFormat
,
217 if (format
!= PIPE_FORMAT_NONE
) {
218 rb
->NumSamples
= samples
;
219 rb
->NumStorageSamples
= samples
;
225 format
= st_choose_renderbuffer_format(st
, internalFormat
, 0, 0);
228 /* Not setting gl_renderbuffer::Format here will cause
229 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
231 if (format
== PIPE_FORMAT_NONE
) {
235 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
237 if (width
== 0 || height
== 0) {
238 /* if size is zero, nothing to allocate */
242 /* Setup new texture template.
244 memset(&templ
, 0, sizeof(templ
));
245 templ
.target
= st
->internal_target
;
246 templ
.format
= format
;
247 templ
.width0
= width
;
248 templ
.height0
= height
;
250 templ
.array_size
= 1;
251 templ
.nr_samples
= rb
->NumSamples
;
252 templ
.nr_storage_samples
= rb
->NumStorageSamples
;
254 if (util_format_is_depth_or_stencil(format
)) {
255 templ
.bind
= PIPE_BIND_DEPTH_STENCIL
;
257 else if (strb
->Base
.Name
!= 0) {
258 /* this is a user-created renderbuffer */
259 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
262 /* this is a window-system buffer */
263 templ
.bind
= (PIPE_BIND_DISPLAY_TARGET
|
264 PIPE_BIND_RENDER_TARGET
);
267 strb
->texture
= screen
->resource_create(screen
, &templ
);
272 st_update_renderbuffer_surface(st
, strb
);
273 return strb
->surface
!= NULL
;
278 * gl_renderbuffer::Delete()
281 st_renderbuffer_delete(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
283 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
285 struct st_context
*st
= st_context(ctx
);
286 pipe_surface_release(st
->pipe
, &strb
->surface_srgb
);
287 pipe_surface_release(st
->pipe
, &strb
->surface_linear
);
288 strb
->surface
= NULL
;
290 pipe_resource_reference(&strb
->texture
, NULL
);
292 _mesa_delete_renderbuffer(ctx
, rb
);
297 * Called via ctx->Driver.NewRenderbuffer()
299 static struct gl_renderbuffer
*
300 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
302 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
305 _mesa_init_renderbuffer(&strb
->Base
, name
);
306 strb
->Base
.Delete
= st_renderbuffer_delete
;
307 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
315 * Allocate a renderbuffer for an on-screen window (not a user-created
316 * renderbuffer). The window system code determines the format.
318 struct gl_renderbuffer
*
319 st_new_renderbuffer_fb(enum pipe_format format
, unsigned samples
, boolean sw
)
321 struct st_renderbuffer
*strb
;
323 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
325 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
329 _mesa_init_renderbuffer(&strb
->Base
, 0);
330 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
331 strb
->Base
.NumSamples
= samples
;
332 strb
->Base
.NumStorageSamples
= samples
;
333 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
334 strb
->Base
._BaseFormat
= _mesa_get_format_base_format(strb
->Base
.Format
);
338 case PIPE_FORMAT_B10G10R10A2_UNORM
:
339 case PIPE_FORMAT_R10G10B10A2_UNORM
:
340 strb
->Base
.InternalFormat
= GL_RGB10_A2
;
342 case PIPE_FORMAT_R10G10B10X2_UNORM
:
343 case PIPE_FORMAT_B10G10R10X2_UNORM
:
344 strb
->Base
.InternalFormat
= GL_RGB10
;
346 case PIPE_FORMAT_R8G8B8A8_UNORM
:
347 case PIPE_FORMAT_B8G8R8A8_UNORM
:
348 case PIPE_FORMAT_A8R8G8B8_UNORM
:
349 strb
->Base
.InternalFormat
= GL_RGBA8
;
351 case PIPE_FORMAT_R8G8B8X8_UNORM
:
352 case PIPE_FORMAT_B8G8R8X8_UNORM
:
353 case PIPE_FORMAT_X8R8G8B8_UNORM
:
354 strb
->Base
.InternalFormat
= GL_RGB8
;
356 case PIPE_FORMAT_R8G8B8A8_SRGB
:
357 case PIPE_FORMAT_B8G8R8A8_SRGB
:
358 case PIPE_FORMAT_A8R8G8B8_SRGB
:
359 strb
->Base
.InternalFormat
= GL_SRGB8_ALPHA8
;
361 case PIPE_FORMAT_R8G8B8X8_SRGB
:
362 case PIPE_FORMAT_B8G8R8X8_SRGB
:
363 case PIPE_FORMAT_X8R8G8B8_SRGB
:
364 strb
->Base
.InternalFormat
= GL_SRGB8
;
366 case PIPE_FORMAT_B5G5R5A1_UNORM
:
367 strb
->Base
.InternalFormat
= GL_RGB5_A1
;
369 case PIPE_FORMAT_B4G4R4A4_UNORM
:
370 strb
->Base
.InternalFormat
= GL_RGBA4
;
372 case PIPE_FORMAT_B5G6R5_UNORM
:
373 strb
->Base
.InternalFormat
= GL_RGB565
;
375 case PIPE_FORMAT_Z16_UNORM
:
376 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
378 case PIPE_FORMAT_Z32_UNORM
:
379 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
381 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
382 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
383 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
385 case PIPE_FORMAT_Z24X8_UNORM
:
386 case PIPE_FORMAT_X8Z24_UNORM
:
387 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT24
;
389 case PIPE_FORMAT_S8_UINT
:
390 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
392 case PIPE_FORMAT_R16G16B16A16_SNORM
:
394 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
396 case PIPE_FORMAT_R16G16B16A16_UNORM
:
397 strb
->Base
.InternalFormat
= GL_RGBA16
;
399 case PIPE_FORMAT_R8_UNORM
:
400 strb
->Base
.InternalFormat
= GL_R8
;
402 case PIPE_FORMAT_R8G8_UNORM
:
403 strb
->Base
.InternalFormat
= GL_RG8
;
405 case PIPE_FORMAT_R16_UNORM
:
406 strb
->Base
.InternalFormat
= GL_R16
;
408 case PIPE_FORMAT_R16G16_UNORM
:
409 strb
->Base
.InternalFormat
= GL_RG16
;
411 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
412 strb
->Base
.InternalFormat
= GL_RGBA32F
;
414 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
415 strb
->Base
.InternalFormat
= GL_RGBA16F
;
419 "Unexpected format %s in st_new_renderbuffer_fb",
420 util_format_name(format
));
425 /* st-specific methods */
426 strb
->Base
.Delete
= st_renderbuffer_delete
;
427 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
429 /* surface is allocated in st_renderbuffer_alloc_storage() */
430 strb
->surface
= NULL
;
437 * Create or update the pipe_surface of a FBO renderbuffer.
438 * This is usually called after st_finalize_texture.
441 st_update_renderbuffer_surface(struct st_context
*st
,
442 struct st_renderbuffer
*strb
)
444 struct pipe_context
*pipe
= st
->pipe
;
445 struct pipe_resource
*resource
= strb
->texture
;
446 const struct st_texture_object
*stTexObj
= NULL
;
447 unsigned rtt_width
= strb
->Base
.Width
;
448 unsigned rtt_height
= strb
->Base
.Height
;
449 unsigned rtt_depth
= strb
->Base
.Depth
;
452 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
453 * the format of strb->texture is linear (because we have no control over
454 * the format). Check strb->Base.Format instead of strb->texture->format
455 * to determine if the rb is sRGB-capable.
457 boolean enable_srgb
= st
->ctx
->Color
.sRGBEnabled
&&
458 _mesa_get_format_color_encoding(strb
->Base
.Format
) == GL_SRGB
;
459 enum pipe_format format
= resource
->format
;
462 stTexObj
= st_texture_object(strb
->Base
.TexImage
->TexObject
);
463 if (stTexObj
->surface_based
)
464 format
= stTexObj
->surface_format
;
467 format
= enable_srgb
? util_format_srgb(format
) : util_format_linear(format
);
469 if (resource
->target
== PIPE_TEXTURE_1D_ARRAY
) {
470 rtt_depth
= rtt_height
;
474 /* find matching mipmap level size */
476 for (level
= 0; level
<= resource
->last_level
; level
++) {
477 if (u_minify(resource
->width0
, level
) == rtt_width
&&
478 u_minify(resource
->height0
, level
) == rtt_height
&&
479 (resource
->target
!= PIPE_TEXTURE_3D
||
480 u_minify(resource
->depth0
, level
) == rtt_depth
)) {
484 assert(level
<= resource
->last_level
);
486 /* determine the layer bounds */
487 unsigned first_layer
, last_layer
;
488 if (strb
->rtt_layered
) {
490 last_layer
= util_max_layer(strb
->texture
, level
);
494 last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
497 /* Adjust for texture views */
498 if (strb
->is_rtt
&& resource
->array_size
> 1 &&
499 stTexObj
->base
.Immutable
) {
500 const struct gl_texture_object
*tex
= &stTexObj
->base
;
501 first_layer
+= tex
->MinLayer
;
502 if (!strb
->rtt_layered
)
503 last_layer
+= tex
->MinLayer
;
505 last_layer
= MIN2(first_layer
+ tex
->NumLayers
- 1, last_layer
);
508 struct pipe_surface
**psurf
=
509 enable_srgb
? &strb
->surface_srgb
: &strb
->surface_linear
;
510 struct pipe_surface
*surf
= *psurf
;
513 surf
->texture
->nr_samples
!= strb
->Base
.NumSamples
||
514 surf
->texture
->nr_storage_samples
!= strb
->Base
.NumStorageSamples
||
515 surf
->format
!= format
||
516 surf
->texture
!= resource
||
517 surf
->width
!= rtt_width
||
518 surf
->height
!= rtt_height
||
519 surf
->u
.tex
.level
!= level
||
520 surf
->u
.tex
.first_layer
!= first_layer
||
521 surf
->u
.tex
.last_layer
!= last_layer
) {
522 /* create a new pipe_surface */
523 struct pipe_surface surf_tmpl
;
524 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
525 surf_tmpl
.format
= format
;
526 surf_tmpl
.u
.tex
.level
= level
;
527 surf_tmpl
.u
.tex
.first_layer
= first_layer
;
528 surf_tmpl
.u
.tex
.last_layer
= last_layer
;
530 pipe_surface_release(pipe
, psurf
);
532 *psurf
= pipe
->create_surface(pipe
, resource
, &surf_tmpl
);
534 strb
->surface
= *psurf
;
539 * Return the pipe_resource which stores a particular texture image.
541 static struct pipe_resource
*
542 get_teximage_resource(struct gl_texture_object
*texObj
,
543 unsigned face
, unsigned level
)
545 struct st_texture_image
*stImg
=
546 st_texture_image(texObj
->Image
[face
][level
]);
553 * Called by ctx->Driver.RenderTexture
556 st_render_texture(struct gl_context
*ctx
,
557 struct gl_framebuffer
*fb
,
558 struct gl_renderbuffer_attachment
*att
)
560 struct st_context
*st
= st_context(ctx
);
561 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
562 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
563 struct pipe_resource
*pt
;
565 pt
= get_teximage_resource(att
->Texture
,
570 /* point renderbuffer at texobject */
572 strb
->rtt_face
= att
->CubeMapFace
;
573 strb
->rtt_slice
= att
->Zoffset
;
574 strb
->rtt_layered
= att
->Layered
;
575 pipe_resource_reference(&strb
->texture
, pt
);
577 st_update_renderbuffer_surface(st
, strb
);
579 /* Invalidate buffer state so that the pipe's framebuffer state
581 * That's where the new renderbuffer (which we just created) gets
582 * passed to the pipe as a (color/depth) render target.
584 st_invalidate_buffers(st
);
587 /* Need to trigger a call to update_framebuffer() since we just
588 * attached a new renderbuffer.
590 ctx
->NewState
|= _NEW_BUFFERS
;
595 * Called via ctx->Driver.FinishRenderTexture.
598 st_finish_render_texture(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
600 struct st_context
*st
= st_context(ctx
);
601 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
606 strb
->is_rtt
= FALSE
;
608 /* restore previous framebuffer state */
609 st_invalidate_buffers(st
);
615 st_fbo_invalid(const char *reason
)
617 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
618 _mesa_debug(NULL
, "Invalid FBO: %s\n", reason
);
624 * Validate a renderbuffer attachment for a particular set of bindings.
627 st_validate_attachment(struct gl_context
*ctx
,
628 struct pipe_screen
*screen
,
629 const struct gl_renderbuffer_attachment
*att
,
632 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
633 enum pipe_format format
;
634 mesa_format texFormat
;
637 /* Sanity check: we must be binding the surface as a (color) render target
638 * or depth/stencil target.
640 assert(bindings
== PIPE_BIND_RENDER_TARGET
||
641 bindings
== PIPE_BIND_DEPTH_STENCIL
);
643 /* Only validate texture attachments for now, since
644 * st_renderbuffer_alloc_storage makes sure that
645 * the format is supported.
647 if (att
->Type
!= GL_TEXTURE
)
650 if (!stObj
|| !stObj
->pt
)
653 format
= stObj
->pt
->format
;
654 texFormat
= att
->Renderbuffer
->TexImage
->TexFormat
;
656 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
657 * check for linear format support instead.
658 * Later when we create a surface, we change the format to a linear one. */
659 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
&&
660 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
661 const mesa_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
662 format
= st_mesa_format_to_pipe_format(st_context(ctx
), linearFormat
);
665 valid
= screen
->is_format_supported(screen
, format
,
667 stObj
->pt
->nr_samples
,
668 stObj
->pt
->nr_storage_samples
,
671 st_fbo_invalid("Invalid format");
679 * Check that the framebuffer configuration is valid in terms of what
680 * the driver can support.
682 * For Gallium we only supports combined Z+stencil, not separate buffers.
685 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
687 struct st_context
*st
= st_context(ctx
);
688 struct pipe_screen
*screen
= st
->pipe
->screen
;
689 const struct gl_renderbuffer_attachment
*depth
=
690 &fb
->Attachment
[BUFFER_DEPTH
];
691 const struct gl_renderbuffer_attachment
*stencil
=
692 &fb
->Attachment
[BUFFER_STENCIL
];
694 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
695 boolean mixed_formats
=
696 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
698 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
699 st_fbo_invalid("Different Depth/Stencil buffer formats");
700 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
703 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
704 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
705 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
706 st_fbo_invalid("Separate Depth/Stencil buffers");
707 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
710 if (depth
->Type
== GL_TEXTURE
&&
711 stencil
->Type
== GL_TEXTURE
&&
712 depth
->Texture
!= stencil
->Texture
) {
713 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
714 st_fbo_invalid("Different Depth/Stencil textures");
718 if (!st_validate_attachment(ctx
, screen
, depth
, PIPE_BIND_DEPTH_STENCIL
)) {
719 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
720 st_fbo_invalid("Invalid depth attachment");
723 if (!st_validate_attachment(ctx
, screen
, stencil
, PIPE_BIND_DEPTH_STENCIL
)) {
724 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
725 st_fbo_invalid("Invalid stencil attachment");
728 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
729 struct gl_renderbuffer_attachment
*att
=
730 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
731 enum pipe_format format
;
733 if (!st_validate_attachment(ctx
, screen
, att
, PIPE_BIND_RENDER_TARGET
)) {
734 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
735 st_fbo_invalid("Invalid color attachment");
739 if (!mixed_formats
) {
740 /* Disallow mixed formats. */
741 if (att
->Type
!= GL_NONE
) {
742 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
747 if (first_format
== PIPE_FORMAT_NONE
) {
748 first_format
= format
;
749 } else if (format
!= first_format
) {
750 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
751 st_fbo_invalid("Mixed color formats");
760 * Called via glDrawBuffer. We only provide this driver function so that we
761 * can check if we need to allocate a new renderbuffer. Specifically, we
762 * don't usually allocate a front color buffer when using a double-buffered
763 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
764 * that buffer. Note, this is only for window system buffers, not user-
768 st_DrawBufferAllocate(struct gl_context
*ctx
)
770 struct st_context
*st
= st_context(ctx
);
771 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
773 if (_mesa_is_winsys_fbo(fb
)) {
775 /* add the renderbuffers on demand */
776 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
777 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
779 if (idx
!= BUFFER_NONE
) {
780 st_manager_add_color_renderbuffer(st
, fb
, idx
);
788 * Called via glReadBuffer. As with st_DrawBufferAllocate, we use this
789 * function to check if we need to allocate a renderbuffer on demand.
792 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
794 struct st_context
*st
= st_context(ctx
);
795 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
799 /* Check if we need to allocate a front color buffer.
800 * Front buffers are often allocated on demand (other color buffers are
801 * always allocated in advance).
803 if ((fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
||
804 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_RIGHT
) &&
805 fb
->Attachment
[fb
->_ColorReadBufferIndex
].Type
== GL_NONE
) {
806 assert(_mesa_is_winsys_fbo(fb
));
808 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
809 _mesa_update_state(ctx
);
810 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
817 * Called via ctx->Driver.MapRenderbuffer.
820 st_MapRenderbuffer(struct gl_context
*ctx
,
821 struct gl_renderbuffer
*rb
,
822 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
824 GLubyte
**mapOut
, GLint
*rowStrideOut
,
827 struct st_context
*st
= st_context(ctx
);
828 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
829 struct pipe_context
*pipe
= st
->pipe
;
830 const GLboolean invert
= rb
->Name
== 0;
834 /* driver does not support GL_FRAMEBUFFER_FLIP_Y_MESA */
835 assert((rb
->Name
== 0) == flip_y
);
837 if (strb
->software
) {
838 /* software-allocated renderbuffer (probably an accum buffer) */
840 GLint bpp
= _mesa_get_format_bytes(strb
->Base
.Format
);
841 GLint stride
= _mesa_format_row_stride(strb
->Base
.Format
,
843 *mapOut
= (GLubyte
*) strb
->data
+ y
* stride
+ x
* bpp
;
844 *rowStrideOut
= stride
;
853 /* Check for unexpected flags */
854 assert((mode
& ~(GL_MAP_READ_BIT
|
856 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
858 const enum pipe_transfer_usage transfer_flags
=
859 st_access_flags_to_transfer_flags(mode
, false);
861 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
862 * 'invert' will be true for window-system buffers and false for
863 * user-allocated renderbuffers and textures.
866 y2
= strb
->Base
.Height
- y
- h
;
870 map
= pipe_transfer_map(pipe
,
872 strb
->surface
->u
.tex
.level
,
873 strb
->surface
->u
.tex
.first_layer
,
874 transfer_flags
, x
, y2
, w
, h
, &strb
->transfer
);
877 *rowStrideOut
= -(int) strb
->transfer
->stride
;
878 map
+= (h
- 1) * strb
->transfer
->stride
;
881 *rowStrideOut
= strb
->transfer
->stride
;
893 * Called via ctx->Driver.UnmapRenderbuffer.
896 st_UnmapRenderbuffer(struct gl_context
*ctx
,
897 struct gl_renderbuffer
*rb
)
899 struct st_context
*st
= st_context(ctx
);
900 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
901 struct pipe_context
*pipe
= st
->pipe
;
903 if (strb
->software
) {
904 /* software-allocated renderbuffer (probably an accum buffer) */
908 pipe_transfer_unmap(pipe
, strb
->transfer
);
909 strb
->transfer
= NULL
;
914 * Called via ctx->Driver.EvaluateDepthValues.
917 st_EvaluateDepthValues(struct gl_context
*ctx
)
919 struct st_context
*st
= st_context(ctx
);
921 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
923 st
->pipe
->evaluate_depth_buffer(st
->pipe
);
928 st_init_fbo_functions(struct dd_function_table
*functions
)
930 functions
->NewFramebuffer
= _mesa_new_framebuffer
;
931 functions
->NewRenderbuffer
= st_new_renderbuffer
;
932 functions
->FramebufferRenderbuffer
= _mesa_FramebufferRenderbuffer_sw
;
933 functions
->RenderTexture
= st_render_texture
;
934 functions
->FinishRenderTexture
= st_finish_render_texture
;
935 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
937 functions
->DrawBufferAllocate
= st_DrawBufferAllocate
;
938 functions
->ReadBuffer
= st_ReadBuffer
;
940 functions
->MapRenderbuffer
= st_MapRenderbuffer
;
941 functions
->UnmapRenderbuffer
= st_UnmapRenderbuffer
;
942 functions
->EvaluateDepthValues
= st_EvaluateDepthValues
;