1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/macros.h"
41 #include "main/mfeatures.h"
42 #include "main/renderbuffer.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_screen.h"
47 #include "st_context.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_cb_texture.h"
51 #include "st_format.h"
52 #include "st_texture.h"
53 #include "st_manager.h"
55 #include "util/u_format.h"
56 #include "util/u_inlines.h"
57 #include "util/u_surface.h"
61 * gl_renderbuffer::AllocStorage()
62 * This is called to allocate the original drawing surface, and
63 * during window resize.
66 st_renderbuffer_alloc_storage(struct gl_context
* ctx
,
67 struct gl_renderbuffer
*rb
,
68 GLenum internalFormat
,
69 GLuint width
, GLuint height
)
71 struct st_context
*st
= st_context(ctx
);
72 struct pipe_context
*pipe
= st
->pipe
;
73 struct pipe_screen
*screen
= st
->pipe
->screen
;
74 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
75 enum pipe_format format
;
76 struct pipe_surface surf_tmpl
;
78 if (internalFormat
== GL_RGBA16_SNORM
&& strb
->software
) {
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(screen
, internalFormat
,
91 if (format
== PIPE_FORMAT_NONE
) {
95 /* init renderbuffer fields */
96 strb
->Base
.Width
= width
;
97 strb
->Base
.Height
= height
;
98 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
99 strb
->Base
._BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
100 strb
->format
= format
;
102 strb
->defined
= GL_FALSE
; /* undefined contents now */
104 if (strb
->software
) {
109 assert(strb
->format
!= PIPE_FORMAT_NONE
);
111 strb
->stride
= util_format_get_stride(strb
->format
, width
);
112 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
114 strb
->data
= malloc(size
);
116 return strb
->data
!= NULL
;
119 struct pipe_resource
template;
121 /* Free the old surface and texture
123 pipe_surface_reference( &strb
->surface
, NULL
);
124 pipe_resource_reference( &strb
->texture
, NULL
);
126 if (width
== 0 || height
== 0) {
127 /* if size is zero, nothing to allocate */
131 /* Setup new texture template.
133 memset(&template, 0, sizeof(template));
134 template.target
= st
->internal_target
;
135 template.format
= format
;
136 template.width0
= width
;
137 template.height0
= height
;
139 template.array_size
= 1;
140 template.last_level
= 0;
141 template.nr_samples
= rb
->NumSamples
;
142 if (util_format_is_depth_or_stencil(format
)) {
143 template.bind
= PIPE_BIND_DEPTH_STENCIL
;
146 template.bind
= (PIPE_BIND_DISPLAY_TARGET
|
147 PIPE_BIND_RENDER_TARGET
);
150 strb
->texture
= screen
->resource_create(screen
, &template);
155 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
156 u_surface_default_template(&surf_tmpl
, strb
->texture
, template.bind
);
157 strb
->surface
= pipe
->create_surface(pipe
,
161 assert(strb
->surface
->texture
);
162 assert(strb
->surface
->format
);
163 assert(strb
->surface
->width
== width
);
164 assert(strb
->surface
->height
== height
);
167 return strb
->surface
!= NULL
;
173 * gl_renderbuffer::Delete()
176 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
178 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
180 pipe_surface_reference(&strb
->surface
, NULL
);
181 pipe_resource_reference(&strb
->texture
, NULL
);
188 * Called via ctx->Driver.NewFramebuffer()
190 static struct gl_framebuffer
*
191 st_new_framebuffer(struct gl_context
*ctx
, GLuint name
)
193 /* XXX not sure we need to subclass gl_framebuffer for pipe */
194 return _mesa_new_framebuffer(ctx
, name
);
199 * Called via ctx->Driver.NewRenderbuffer()
201 static struct gl_renderbuffer
*
202 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
204 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
206 _mesa_init_renderbuffer(&strb
->Base
, name
);
207 strb
->Base
.Delete
= st_renderbuffer_delete
;
208 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
209 strb
->format
= PIPE_FORMAT_NONE
;
217 * Allocate a renderbuffer for a an on-screen window (not a user-created
218 * renderbuffer). The window system code determines the format.
220 struct gl_renderbuffer
*
221 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
223 struct st_renderbuffer
*strb
;
225 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
227 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
231 _mesa_init_renderbuffer(&strb
->Base
, 0);
232 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
233 strb
->Base
.NumSamples
= samples
;
234 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
235 strb
->Base
._BaseFormat
= _mesa_get_format_base_format(strb
->Base
.Format
);
236 strb
->format
= format
;
240 case PIPE_FORMAT_R8G8B8A8_UNORM
:
241 case PIPE_FORMAT_B8G8R8A8_UNORM
:
242 case PIPE_FORMAT_A8R8G8B8_UNORM
:
243 case PIPE_FORMAT_R8G8B8X8_UNORM
:
244 case PIPE_FORMAT_B8G8R8X8_UNORM
:
245 case PIPE_FORMAT_X8R8G8B8_UNORM
:
246 case PIPE_FORMAT_B5G5R5A1_UNORM
:
247 case PIPE_FORMAT_B4G4R4A4_UNORM
:
248 case PIPE_FORMAT_B5G6R5_UNORM
:
249 strb
->Base
.InternalFormat
= GL_RGBA
;
251 case PIPE_FORMAT_Z16_UNORM
:
252 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
254 case PIPE_FORMAT_Z32_UNORM
:
255 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
257 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
258 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
259 case PIPE_FORMAT_Z24X8_UNORM
:
260 case PIPE_FORMAT_X8Z24_UNORM
:
261 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
263 case PIPE_FORMAT_S8_UINT
:
264 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
266 case PIPE_FORMAT_R16G16B16A16_SNORM
:
268 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
270 case PIPE_FORMAT_R8_UNORM
:
271 strb
->Base
.InternalFormat
= GL_R8
;
273 case PIPE_FORMAT_R8G8_UNORM
:
274 strb
->Base
.InternalFormat
= GL_RG8
;
276 case PIPE_FORMAT_R16_UNORM
:
277 strb
->Base
.InternalFormat
= GL_R16
;
279 case PIPE_FORMAT_R16G16_UNORM
:
280 strb
->Base
.InternalFormat
= GL_RG16
;
284 "Unexpected format in st_new_renderbuffer_fb");
289 /* st-specific methods */
290 strb
->Base
.Delete
= st_renderbuffer_delete
;
291 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
293 /* surface is allocated in st_renderbuffer_alloc_storage() */
294 strb
->surface
= NULL
;
303 * Called via ctx->Driver.BindFramebufferEXT().
306 st_bind_framebuffer(struct gl_context
*ctx
, GLenum target
,
307 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
313 * Called by ctx->Driver.FramebufferRenderbuffer
316 st_framebuffer_renderbuffer(struct gl_context
*ctx
,
317 struct gl_framebuffer
*fb
,
319 struct gl_renderbuffer
*rb
)
321 /* XXX no need for derivation? */
322 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
327 * Called by ctx->Driver.RenderTexture
330 st_render_texture(struct gl_context
*ctx
,
331 struct gl_framebuffer
*fb
,
332 struct gl_renderbuffer_attachment
*att
)
334 struct st_context
*st
= st_context(ctx
);
335 struct pipe_context
*pipe
= st
->pipe
;
336 struct st_renderbuffer
*strb
;
337 struct gl_renderbuffer
*rb
;
338 struct pipe_resource
*pt
;
339 struct st_texture_object
*stObj
;
340 const struct gl_texture_image
*texImage
;
341 struct pipe_surface surf_tmpl
;
343 if (!st_finalize_texture(ctx
, pipe
, att
->Texture
))
346 pt
= st_get_texobj_resource(att
->Texture
);
349 /* get pointer to texture image we're rendeing to */
350 texImage
= _mesa_get_attachment_teximage(att
);
352 /* create new renderbuffer which wraps the texture image.
353 * Use the texture's name as the renderbuffer's name so that we have
354 * something that's non-zero (to determine vertical orientation) and
355 * possibly helpful for debugging.
357 rb
= st_new_renderbuffer(ctx
, att
->Texture
->Name
);
359 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
363 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
364 assert(rb
->RefCount
== 1);
365 rb
->AllocStorage
= NULL
; /* should not get called */
366 strb
= st_renderbuffer(rb
);
368 assert(strb
->Base
.RefCount
> 0);
370 /* get the texture for the texture object */
371 stObj
= st_texture_object(att
->Texture
);
373 /* point renderbuffer at texobject */
375 strb
->rtt_level
= att
->TextureLevel
;
376 strb
->rtt_face
= att
->CubeMapFace
;
377 strb
->rtt_slice
= att
->Zoffset
;
379 rb
->Width
= texImage
->Width2
;
380 rb
->Height
= texImage
->Height2
;
381 rb
->_BaseFormat
= texImage
->_BaseFormat
;
382 rb
->InternalFormat
= texImage
->InternalFormat
;
383 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
385 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
387 pipe_resource_reference( &strb
->texture
, pt
);
389 pipe_surface_reference(&strb
->surface
, NULL
);
391 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
393 /* new surface for rendering into the texture */
394 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
395 surf_tmpl
.format
= ctx
->Color
.sRGBEnabled
? strb
->texture
->format
: util_format_linear(strb
->texture
->format
);
396 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
397 surf_tmpl
.u
.tex
.level
= strb
->rtt_level
;
398 surf_tmpl
.u
.tex
.first_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
399 surf_tmpl
.u
.tex
.last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
400 strb
->surface
= pipe
->create_surface(pipe
,
404 strb
->format
= pt
->format
;
406 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
409 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
410 att->Texture, pt, strb->surface, rb->Width, rb->Height);
413 /* Invalidate buffer state so that the pipe's framebuffer state
415 * That's where the new renderbuffer (which we just created) gets
416 * passed to the pipe as a (color/depth) render target.
418 st_invalidate_state(ctx
, _NEW_BUFFERS
);
423 * Called via ctx->Driver.FinishRenderTexture.
426 st_finish_render_texture(struct gl_context
*ctx
,
427 struct gl_renderbuffer_attachment
*att
)
429 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
437 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
440 /* restore previous framebuffer state */
441 st_invalidate_state(ctx
, _NEW_BUFFERS
);
446 * Validate a renderbuffer attachment for a particular set of bindings.
449 st_validate_attachment(struct gl_context
*ctx
,
450 struct pipe_screen
*screen
,
451 const struct gl_renderbuffer_attachment
*att
,
454 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
455 enum pipe_format format
;
458 /* Only validate texture attachments for now, since
459 * st_renderbuffer_alloc_storage makes sure that
460 * the format is supported.
462 if (att
->Type
!= GL_TEXTURE
)
468 format
= stObj
->pt
->format
;
469 texFormat
= _mesa_get_attachment_teximage_const(att
)->TexFormat
;
471 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
472 * check for linear format support instead.
473 * Later when we create a surface, we change the format to a linear one. */
474 if (!ctx
->Extensions
.EXT_framebuffer_sRGB
&&
475 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
476 const gl_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
477 format
= st_mesa_format_to_pipe_format(linearFormat
);
480 return screen
->is_format_supported(screen
, format
,
482 stObj
->pt
->nr_samples
, bindings
);
487 * Check if two renderbuffer attachments name a combined depth/stencil
491 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment
*depth
,
492 const struct gl_renderbuffer_attachment
*stencil
)
494 assert(depth
&& stencil
);
496 if (depth
->Type
== stencil
->Type
) {
497 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
498 depth
->Renderbuffer
== stencil
->Renderbuffer
)
501 if (depth
->Type
== GL_TEXTURE
&&
502 depth
->Texture
== stencil
->Texture
)
511 * Check that the framebuffer configuration is valid in terms of what
512 * the driver can support.
514 * For Gallium we only supports combined Z+stencil, not separate buffers.
517 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
519 struct st_context
*st
= st_context(ctx
);
520 struct pipe_screen
*screen
= st
->pipe
->screen
;
521 const struct gl_renderbuffer_attachment
*depth
=
522 &fb
->Attachment
[BUFFER_DEPTH
];
523 const struct gl_renderbuffer_attachment
*stencil
=
524 &fb
->Attachment
[BUFFER_STENCIL
];
526 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
527 boolean mixed_formats
=
528 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
530 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
531 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
534 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
535 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
536 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
537 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
540 if (depth
->Type
== GL_TEXTURE
&&
541 stencil
->Type
== GL_TEXTURE
&&
542 depth
->Texture
!= stencil
->Texture
) {
543 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
547 if (!st_validate_attachment(ctx
,
550 PIPE_BIND_DEPTH_STENCIL
)) {
551 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
554 if (!st_validate_attachment(ctx
,
557 PIPE_BIND_DEPTH_STENCIL
)) {
558 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
561 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
562 struct gl_renderbuffer_attachment
*att
=
563 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
564 enum pipe_format format
;
566 if (!st_validate_attachment(ctx
,
569 PIPE_BIND_RENDER_TARGET
)) {
570 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
574 if (!mixed_formats
) {
575 /* Disallow mixed formats. */
576 if (att
->Type
!= GL_NONE
) {
577 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
582 if (first_format
== PIPE_FORMAT_NONE
) {
583 first_format
= format
;
584 } else if (format
!= first_format
) {
585 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
594 * Called via glDrawBuffer.
597 st_DrawBuffers(struct gl_context
*ctx
, GLsizei count
, const GLenum
*buffers
)
599 struct st_context
*st
= st_context(ctx
);
600 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
606 /* add the renderbuffers on demand */
607 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
608 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
609 st_manager_add_color_renderbuffer(st
, fb
, idx
);
615 * Called via glReadBuffer.
618 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
620 struct st_context
*st
= st_context(ctx
);
621 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
625 /* add the renderbuffer on demand */
626 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
632 * Called via ctx->Driver.MapRenderbuffer.
635 st_MapRenderbuffer(struct gl_context
*ctx
,
636 struct gl_renderbuffer
*rb
,
637 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
639 GLubyte
**mapOut
, GLint
*rowStrideOut
)
641 struct st_context
*st
= st_context(ctx
);
642 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
643 struct pipe_context
*pipe
= st
->pipe
;
644 const GLboolean invert
= rb
->Name
== 0;
648 if (strb
->software
) {
649 /* software-allocated renderbuffer (probably an accum buffer) */
650 GLubyte
*map
= (GLubyte
*) strb
->data
;
652 map
+= strb
->stride
* y
;
653 map
+= util_format_get_blocksize(strb
->format
) * x
;
655 *rowStrideOut
= strb
->stride
;
665 if (mode
& GL_MAP_READ_BIT
)
666 usage
|= PIPE_TRANSFER_READ
;
667 if (mode
& GL_MAP_WRITE_BIT
)
668 usage
|= PIPE_TRANSFER_WRITE
;
669 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
670 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
672 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
673 * 'invert' will be true for window-system buffers and false for
674 * user-allocated renderbuffers and textures.
677 y2
= strb
->Base
.Height
- y
- h
;
681 strb
->transfer
= pipe_get_transfer(pipe
,
684 strb
->rtt_face
+ strb
->rtt_slice
,
686 if (strb
->transfer
) {
687 GLubyte
*map
= pipe_transfer_map(pipe
, strb
->transfer
);
689 *rowStrideOut
= -strb
->transfer
->stride
;
690 map
+= (h
- 1) * strb
->transfer
->stride
;
693 *rowStrideOut
= strb
->transfer
->stride
;
705 * Called via ctx->Driver.UnmapRenderbuffer.
708 st_UnmapRenderbuffer(struct gl_context
*ctx
,
709 struct gl_renderbuffer
*rb
)
711 struct st_context
*st
= st_context(ctx
);
712 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
713 struct pipe_context
*pipe
= st
->pipe
;
715 if (strb
->software
) {
716 /* software-allocated renderbuffer (probably an accum buffer) */
720 pipe_transfer_unmap(pipe
, strb
->transfer
);
721 pipe
->transfer_destroy(pipe
, strb
->transfer
);
722 strb
->transfer
= NULL
;
727 void st_init_fbo_functions(struct dd_function_table
*functions
)
729 #if FEATURE_EXT_framebuffer_object
730 functions
->NewFramebuffer
= st_new_framebuffer
;
731 functions
->NewRenderbuffer
= st_new_renderbuffer
;
732 functions
->BindFramebuffer
= st_bind_framebuffer
;
733 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
734 functions
->RenderTexture
= st_render_texture
;
735 functions
->FinishRenderTexture
= st_finish_render_texture
;
736 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
738 /* no longer needed by core Mesa, drivers handle resizes...
739 functions->ResizeBuffers = st_resize_buffers;
742 functions
->DrawBuffers
= st_DrawBuffers
;
743 functions
->ReadBuffer
= st_ReadBuffer
;
745 functions
->MapRenderbuffer
= st_MapRenderbuffer
;
746 functions
->UnmapRenderbuffer
= st_UnmapRenderbuffer
;