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
->Base
.DataType
= st_format_datatype(format
);
101 strb
->format
= format
;
103 strb
->defined
= GL_FALSE
; /* undefined contents now */
105 if (strb
->software
) {
110 assert(strb
->format
!= PIPE_FORMAT_NONE
);
112 strb
->stride
= util_format_get_stride(strb
->format
, width
);
113 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
115 strb
->data
= malloc(size
);
117 return strb
->data
!= NULL
;
120 struct pipe_resource
template;
122 /* Free the old surface and texture
124 pipe_surface_reference( &strb
->surface
, NULL
);
125 pipe_resource_reference( &strb
->texture
, NULL
);
127 /* Setup new texture template.
129 memset(&template, 0, sizeof(template));
130 template.target
= st
->internal_target
;
131 template.format
= format
;
132 template.width0
= width
;
133 template.height0
= height
;
135 template.array_size
= 1;
136 template.last_level
= 0;
137 template.nr_samples
= rb
->NumSamples
;
138 if (util_format_is_depth_or_stencil(format
)) {
139 template.bind
= PIPE_BIND_DEPTH_STENCIL
;
142 template.bind
= (PIPE_BIND_DISPLAY_TARGET
|
143 PIPE_BIND_RENDER_TARGET
);
146 strb
->texture
= screen
->resource_create(screen
, &template);
151 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
152 u_surface_default_template(&surf_tmpl
, strb
->texture
, template.bind
);
153 strb
->surface
= pipe
->create_surface(pipe
,
157 assert(strb
->surface
->texture
);
158 assert(strb
->surface
->format
);
159 assert(strb
->surface
->width
== width
);
160 assert(strb
->surface
->height
== height
);
163 return strb
->surface
!= NULL
;
169 * gl_renderbuffer::Delete()
172 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
174 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
176 pipe_surface_reference(&strb
->surface
, NULL
);
177 pipe_resource_reference(&strb
->texture
, NULL
);
184 * gl_renderbuffer::GetPointer()
187 null_get_pointer(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
190 /* By returning NULL we force all software rendering to go through
194 assert(0); /* Should never get called with softpipe */
201 * Called via ctx->Driver.NewFramebuffer()
203 static struct gl_framebuffer
*
204 st_new_framebuffer(struct gl_context
*ctx
, GLuint name
)
206 /* XXX not sure we need to subclass gl_framebuffer for pipe */
207 return _mesa_new_framebuffer(ctx
, name
);
212 * Called via ctx->Driver.NewRenderbuffer()
214 static struct gl_renderbuffer
*
215 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
217 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
219 _mesa_init_renderbuffer(&strb
->Base
, name
);
220 strb
->Base
.Delete
= st_renderbuffer_delete
;
221 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
222 strb
->Base
.GetPointer
= null_get_pointer
;
223 strb
->format
= PIPE_FORMAT_NONE
;
231 * Allocate a renderbuffer for a an on-screen window (not a user-created
232 * renderbuffer). The window system code determines the format.
234 struct gl_renderbuffer
*
235 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
237 struct st_renderbuffer
*strb
;
239 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
241 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
245 _mesa_init_renderbuffer(&strb
->Base
, 0);
246 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
247 strb
->Base
.NumSamples
= samples
;
248 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
249 strb
->Base
._BaseFormat
= _mesa_get_format_base_format(strb
->Base
.Format
);
250 strb
->Base
.DataType
= st_format_datatype(format
);
251 strb
->format
= format
;
255 case PIPE_FORMAT_R8G8B8A8_UNORM
:
256 case PIPE_FORMAT_B8G8R8A8_UNORM
:
257 case PIPE_FORMAT_A8R8G8B8_UNORM
:
258 case PIPE_FORMAT_R8G8B8X8_UNORM
:
259 case PIPE_FORMAT_B8G8R8X8_UNORM
:
260 case PIPE_FORMAT_X8R8G8B8_UNORM
:
261 case PIPE_FORMAT_B5G5R5A1_UNORM
:
262 case PIPE_FORMAT_B4G4R4A4_UNORM
:
263 case PIPE_FORMAT_B5G6R5_UNORM
:
264 strb
->Base
.InternalFormat
= GL_RGBA
;
266 case PIPE_FORMAT_Z16_UNORM
:
267 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
269 case PIPE_FORMAT_Z32_UNORM
:
270 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
272 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
273 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
274 case PIPE_FORMAT_Z24X8_UNORM
:
275 case PIPE_FORMAT_X8Z24_UNORM
:
276 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
278 case PIPE_FORMAT_S8_UINT
:
279 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
281 case PIPE_FORMAT_R16G16B16A16_SNORM
:
283 strb
->Base
.InternalFormat
= GL_RGBA16_SNORM
;
285 case PIPE_FORMAT_R8_UNORM
:
286 strb
->Base
.InternalFormat
= GL_R8
;
288 case PIPE_FORMAT_R8G8_UNORM
:
289 strb
->Base
.InternalFormat
= GL_RG8
;
291 case PIPE_FORMAT_R16_UNORM
:
292 strb
->Base
.InternalFormat
= GL_R16
;
294 case PIPE_FORMAT_R16G16_UNORM
:
295 strb
->Base
.InternalFormat
= GL_RG16
;
299 "Unexpected format in st_new_renderbuffer_fb");
304 /* st-specific methods */
305 strb
->Base
.Delete
= st_renderbuffer_delete
;
306 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
307 strb
->Base
.GetPointer
= null_get_pointer
;
309 /* surface is allocated in st_renderbuffer_alloc_storage() */
310 strb
->surface
= NULL
;
319 * Called via ctx->Driver.BindFramebufferEXT().
322 st_bind_framebuffer(struct gl_context
*ctx
, GLenum target
,
323 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
329 * Called by ctx->Driver.FramebufferRenderbuffer
332 st_framebuffer_renderbuffer(struct gl_context
*ctx
,
333 struct gl_framebuffer
*fb
,
335 struct gl_renderbuffer
*rb
)
337 /* XXX no need for derivation? */
338 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
343 * Called by ctx->Driver.RenderTexture
346 st_render_texture(struct gl_context
*ctx
,
347 struct gl_framebuffer
*fb
,
348 struct gl_renderbuffer_attachment
*att
)
350 struct st_context
*st
= st_context(ctx
);
351 struct pipe_context
*pipe
= st
->pipe
;
352 struct st_renderbuffer
*strb
;
353 struct gl_renderbuffer
*rb
;
354 struct pipe_resource
*pt
;
355 struct st_texture_object
*stObj
;
356 const struct gl_texture_image
*texImage
;
357 struct pipe_surface surf_tmpl
;
359 if (!st_finalize_texture(ctx
, pipe
, att
->Texture
))
362 pt
= st_get_texobj_resource(att
->Texture
);
365 /* get pointer to texture image we're rendeing to */
366 texImage
= _mesa_get_attachment_teximage(att
);
368 /* create new renderbuffer which wraps the texture image.
369 * Use the texture's name as the renderbuffer's name so that we have
370 * something that's non-zero (to determine vertical orientation) and
371 * possibly helpful for debugging.
373 rb
= st_new_renderbuffer(ctx
, att
->Texture
->Name
);
375 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
379 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
380 assert(rb
->RefCount
== 1);
381 rb
->AllocStorage
= NULL
; /* should not get called */
382 strb
= st_renderbuffer(rb
);
384 assert(strb
->Base
.RefCount
> 0);
386 /* get the texture for the texture object */
387 stObj
= st_texture_object(att
->Texture
);
389 /* point renderbuffer at texobject */
391 strb
->rtt_level
= att
->TextureLevel
;
392 strb
->rtt_face
= att
->CubeMapFace
;
393 strb
->rtt_slice
= att
->Zoffset
;
395 rb
->Width
= texImage
->Width2
;
396 rb
->Height
= texImage
->Height2
;
397 rb
->_BaseFormat
= texImage
->_BaseFormat
;
398 rb
->InternalFormat
= texImage
->InternalFormat
;
399 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
401 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
403 pipe_resource_reference( &strb
->texture
, pt
);
405 pipe_surface_reference(&strb
->surface
, NULL
);
407 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
409 /* new surface for rendering into the texture */
410 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
411 surf_tmpl
.format
= ctx
->Color
.sRGBEnabled
? strb
->texture
->format
: util_format_linear(strb
->texture
->format
);
412 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
413 surf_tmpl
.u
.tex
.level
= strb
->rtt_level
;
414 surf_tmpl
.u
.tex
.first_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
415 surf_tmpl
.u
.tex
.last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
416 strb
->surface
= pipe
->create_surface(pipe
,
420 strb
->format
= pt
->format
;
422 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
423 strb
->Base
.DataType
= st_format_datatype(pt
->format
);
426 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
427 att->Texture, pt, strb->surface, rb->Width, rb->Height);
430 /* Invalidate buffer state so that the pipe's framebuffer state
432 * That's where the new renderbuffer (which we just created) gets
433 * passed to the pipe as a (color/depth) render target.
435 st_invalidate_state(ctx
, _NEW_BUFFERS
);
440 * Called via ctx->Driver.FinishRenderTexture.
443 st_finish_render_texture(struct gl_context
*ctx
,
444 struct gl_renderbuffer_attachment
*att
)
446 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
454 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
457 /* restore previous framebuffer state */
458 st_invalidate_state(ctx
, _NEW_BUFFERS
);
463 * Validate a renderbuffer attachment for a particular set of bindings.
466 st_validate_attachment(struct gl_context
*ctx
,
467 struct pipe_screen
*screen
,
468 const struct gl_renderbuffer_attachment
*att
,
471 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
472 enum pipe_format format
;
475 /* Only validate texture attachments for now, since
476 * st_renderbuffer_alloc_storage makes sure that
477 * the format is supported.
479 if (att
->Type
!= GL_TEXTURE
)
485 format
= stObj
->pt
->format
;
486 texFormat
= _mesa_get_attachment_teximage_const(att
)->TexFormat
;
488 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
489 * check for linear format support instead.
490 * Later when we create a surface, we change the format to a linear one. */
491 if (!ctx
->Const
.sRGBCapable
&&
492 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
493 const gl_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
494 format
= st_mesa_format_to_pipe_format(linearFormat
);
497 return screen
->is_format_supported(screen
, format
,
499 stObj
->pt
->nr_samples
, bindings
);
504 * Check if two renderbuffer attachments name a combined depth/stencil
508 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment
*depth
,
509 const struct gl_renderbuffer_attachment
*stencil
)
511 assert(depth
&& stencil
);
513 if (depth
->Type
== stencil
->Type
) {
514 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
515 depth
->Renderbuffer
== stencil
->Renderbuffer
)
518 if (depth
->Type
== GL_TEXTURE
&&
519 depth
->Texture
== stencil
->Texture
)
528 * Check that the framebuffer configuration is valid in terms of what
529 * the driver can support.
531 * For Gallium we only supports combined Z+stencil, not separate buffers.
534 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
536 struct st_context
*st
= st_context(ctx
);
537 struct pipe_screen
*screen
= st
->pipe
->screen
;
538 const struct gl_renderbuffer_attachment
*depth
=
539 &fb
->Attachment
[BUFFER_DEPTH
];
540 const struct gl_renderbuffer_attachment
*stencil
=
541 &fb
->Attachment
[BUFFER_STENCIL
];
543 enum pipe_format first_format
= PIPE_FORMAT_NONE
;
544 boolean mixed_formats
=
545 screen
->get_param(screen
, PIPE_CAP_MIXED_COLORBUFFER_FORMATS
) != 0;
547 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
548 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
551 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
552 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
553 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
554 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
557 if (depth
->Type
== GL_TEXTURE
&&
558 stencil
->Type
== GL_TEXTURE
&&
559 depth
->Texture
!= stencil
->Texture
) {
560 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
564 if (!st_validate_attachment(ctx
,
567 PIPE_BIND_DEPTH_STENCIL
)) {
568 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
571 if (!st_validate_attachment(ctx
,
574 PIPE_BIND_DEPTH_STENCIL
)) {
575 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
578 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
579 struct gl_renderbuffer_attachment
*att
=
580 &fb
->Attachment
[BUFFER_COLOR0
+ i
];
581 enum pipe_format format
;
583 if (!st_validate_attachment(ctx
,
586 PIPE_BIND_RENDER_TARGET
)) {
587 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
591 if (!mixed_formats
) {
592 /* Disallow mixed formats. */
593 if (att
->Type
!= GL_NONE
) {
594 format
= st_renderbuffer(att
->Renderbuffer
)->surface
->format
;
599 if (first_format
== PIPE_FORMAT_NONE
) {
600 first_format
= format
;
601 } else if (format
!= first_format
) {
602 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
611 * Called via glDrawBuffer.
614 st_DrawBuffers(struct gl_context
*ctx
, GLsizei count
, const GLenum
*buffers
)
616 struct st_context
*st
= st_context(ctx
);
617 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
623 /* add the renderbuffers on demand */
624 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
625 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
626 st_manager_add_color_renderbuffer(st
, fb
, idx
);
632 * Called via glReadBuffer.
635 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
637 struct st_context
*st
= st_context(ctx
);
638 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
642 /* add the renderbuffer on demand */
643 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
649 * Called via ctx->Driver.MapRenderbuffer.
652 st_MapRenderbuffer(struct gl_context
*ctx
,
653 struct gl_renderbuffer
*rb
,
654 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
656 GLubyte
**mapOut
, GLint
*rowStrideOut
)
658 struct st_context
*st
= st_context(ctx
);
659 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
660 struct pipe_context
*pipe
= st
->pipe
;
661 const GLboolean invert
= rb
->Name
== 0;
666 if (mode
& GL_MAP_READ_BIT
)
667 usage
|= PIPE_TRANSFER_READ
;
668 if (mode
& GL_MAP_WRITE_BIT
)
669 usage
|= PIPE_TRANSFER_WRITE
;
671 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
672 * 'invert' will be true for window-system buffers and false for
673 * user-allocated renderbuffers and textures.
676 y2
= strb
->Base
.Height
- y
- h
;
680 strb
->transfer
= pipe_get_transfer(pipe
,
683 strb
->rtt_face
+ strb
->rtt_slice
,
685 if (strb
->transfer
) {
686 GLubyte
*map
= pipe_transfer_map(pipe
, strb
->transfer
);
688 *rowStrideOut
= -strb
->transfer
->stride
;
689 map
+= (h
- 1) * strb
->transfer
->stride
;
692 *rowStrideOut
= strb
->transfer
->stride
;
704 * Called via ctx->Driver.UnmapRenderbuffer.
707 st_UnmapRenderbuffer(struct gl_context
*ctx
,
708 struct gl_renderbuffer
*rb
)
710 struct st_context
*st
= st_context(ctx
);
711 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
712 struct pipe_context
*pipe
= st
->pipe
;
714 pipe_transfer_unmap(pipe
, strb
->transfer
);
715 pipe
->transfer_destroy(pipe
, strb
->transfer
);
716 strb
->transfer
= NULL
;
721 void st_init_fbo_functions(struct dd_function_table
*functions
)
723 #if FEATURE_EXT_framebuffer_object
724 functions
->NewFramebuffer
= st_new_framebuffer
;
725 functions
->NewRenderbuffer
= st_new_renderbuffer
;
726 functions
->BindFramebuffer
= st_bind_framebuffer
;
727 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
728 functions
->RenderTexture
= st_render_texture
;
729 functions
->FinishRenderTexture
= st_finish_render_texture
;
730 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
732 /* no longer needed by core Mesa, drivers handle resizes...
733 functions->ResizeBuffers = st_resize_buffers;
736 functions
->DrawBuffers
= st_DrawBuffers
;
737 functions
->ReadBuffer
= st_ReadBuffer
;
739 functions
->MapRenderbuffer
= st_MapRenderbuffer
;
740 functions
->UnmapRenderbuffer
= st_UnmapRenderbuffer
;