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/renderbuffer.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_screen.h"
45 #include "st_context.h"
46 #include "st_cb_fbo.h"
47 #include "st_format.h"
48 #include "st_public.h"
49 #include "st_texture.h"
51 #include "util/u_format.h"
52 #include "util/u_rect.h"
56 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
59 init_renderbuffer_bits(struct st_renderbuffer
*strb
,
60 enum pipe_format pipeFormat
)
62 struct pipe_format_info info
;
64 if (!st_get_format_info( pipeFormat
, &info
)) {
68 strb
->Base
.Format
= info
.mesa_format
;
69 strb
->Base
.DataType
= st_format_datatype(pipeFormat
);
75 * gl_renderbuffer::AllocStorage()
76 * This is called to allocate the original drawing surface, and
77 * during window resize.
80 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
81 GLenum internalFormat
,
82 GLuint width
, GLuint height
)
84 struct pipe_context
*pipe
= ctx
->st
->pipe
;
85 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
86 enum pipe_format format
;
88 if (strb
->format
!= PIPE_FORMAT_NONE
)
89 format
= strb
->format
;
91 format
= st_choose_renderbuffer_format(pipe
->screen
, internalFormat
);
93 /* init renderbuffer fields */
94 strb
->Base
.Width
= width
;
95 strb
->Base
.Height
= height
;
96 init_renderbuffer_bits(strb
, format
);
98 strb
->defined
= GL_FALSE
; /* undefined contents now */
103 _mesa_free(strb
->data
);
105 assert(strb
->format
!= PIPE_FORMAT_NONE
);
107 strb
->stride
= util_format_get_stride(strb
->format
, width
);
108 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
110 strb
->data
= _mesa_malloc(size
);
112 return strb
->data
!= NULL
;
115 struct pipe_texture
template;
116 unsigned surface_usage
;
118 /* Free the old surface and texture
120 pipe_surface_reference( &strb
->surface
, NULL
);
121 pipe_texture_reference( &strb
->texture
, NULL
);
123 /* Setup new texture template.
125 memset(&template, 0, sizeof(template));
126 template.target
= PIPE_TEXTURE_2D
;
127 template.format
= format
;
128 template.width0
= width
;
129 template.height0
= height
;
131 template.last_level
= 0;
132 template.nr_samples
= rb
->NumSamples
;
133 if (util_format_is_depth_or_stencil(format
)) {
134 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
137 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
138 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
141 /* Probably need dedicated flags for surface usage too:
143 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
144 PIPE_BUFFER_USAGE_GPU_WRITE
);
146 PIPE_BUFFER_USAGE_CPU_READ
|
147 PIPE_BUFFER_USAGE_CPU_WRITE
);
150 strb
->texture
= pipe
->screen
->texture_create( pipe
->screen
,
156 strb
->surface
= pipe
->screen
->get_tex_surface( pipe
->screen
,
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_texture_reference(&strb
->texture
, NULL
);
182 _mesa_free(strb
->data
);
188 * gl_renderbuffer::GetPointer()
191 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
194 /* By returning NULL we force all software rendering to go through
198 assert(0); /* Should never get called with softpipe */
205 * Called via ctx->Driver.NewFramebuffer()
207 static struct gl_framebuffer
*
208 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
210 /* XXX not sure we need to subclass gl_framebuffer for pipe */
211 return _mesa_new_framebuffer(ctx
, name
);
216 * Called via ctx->Driver.NewRenderbuffer()
218 static struct gl_renderbuffer
*
219 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
221 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
223 _mesa_init_renderbuffer(&strb
->Base
, name
);
224 strb
->Base
.Delete
= st_renderbuffer_delete
;
225 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
226 strb
->Base
.GetPointer
= null_get_pointer
;
227 strb
->format
= PIPE_FORMAT_NONE
;
235 * Allocate a renderbuffer for a an on-screen window (not a user-created
236 * renderbuffer). The window system code determines the format.
238 struct gl_renderbuffer
*
239 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
241 struct st_renderbuffer
*strb
;
243 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
245 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
249 _mesa_init_renderbuffer(&strb
->Base
, 0);
250 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
251 strb
->Base
.NumSamples
= samples
;
252 strb
->format
= format
;
253 init_renderbuffer_bits(strb
, format
);
257 case PIPE_FORMAT_A8R8G8B8_UNORM
:
258 case PIPE_FORMAT_B8G8R8A8_UNORM
:
259 case PIPE_FORMAT_X8R8G8B8_UNORM
:
260 case PIPE_FORMAT_B8G8R8X8_UNORM
:
261 case PIPE_FORMAT_A1R5G5B5_UNORM
:
262 case PIPE_FORMAT_A4R4G4B4_UNORM
:
263 case PIPE_FORMAT_R5G6B5_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_S8Z24_UNORM
:
273 case PIPE_FORMAT_Z24S8_UNORM
:
274 case PIPE_FORMAT_X8Z24_UNORM
:
275 case PIPE_FORMAT_Z24X8_UNORM
:
276 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
278 case PIPE_FORMAT_S8_UNORM
:
279 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
281 case PIPE_FORMAT_R16G16B16A16_SNORM
:
282 strb
->Base
.InternalFormat
= GL_RGBA16
;
286 "Unexpected format in st_new_renderbuffer_fb");
291 /* st-specific methods */
292 strb
->Base
.Delete
= st_renderbuffer_delete
;
293 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
294 strb
->Base
.GetPointer
= null_get_pointer
;
296 /* surface is allocated in st_renderbuffer_alloc_storage() */
297 strb
->surface
= NULL
;
306 * Called via ctx->Driver.BindFramebufferEXT().
309 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
310 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
316 * Called by ctx->Driver.FramebufferRenderbuffer
319 st_framebuffer_renderbuffer(GLcontext
*ctx
,
320 struct gl_framebuffer
*fb
,
322 struct gl_renderbuffer
*rb
)
324 /* XXX no need for derivation? */
325 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
330 * Called by ctx->Driver.RenderTexture
333 st_render_texture(GLcontext
*ctx
,
334 struct gl_framebuffer
*fb
,
335 struct gl_renderbuffer_attachment
*att
)
337 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
338 struct st_renderbuffer
*strb
;
339 struct gl_renderbuffer
*rb
;
340 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
341 struct st_texture_object
*stObj
;
342 const struct gl_texture_image
*texImage
=
343 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
348 /* create new renderbuffer which wraps the texture image */
349 rb
= st_new_renderbuffer(ctx
, 0);
351 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
355 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
356 assert(rb
->RefCount
== 1);
357 rb
->AllocStorage
= NULL
; /* should not get called */
358 strb
= st_renderbuffer(rb
);
360 assert(strb
->Base
.RefCount
> 0);
362 /* get the texture for the texture object */
363 stObj
= st_texture_object(att
->Texture
);
365 /* point renderbuffer at texobject */
367 strb
->rtt_level
= att
->TextureLevel
;
368 strb
->rtt_face
= att
->CubeMapFace
;
369 strb
->rtt_slice
= att
->Zoffset
;
371 rb
->Width
= texImage
->Width2
;
372 rb
->Height
= texImage
->Height2
;
373 rb
->_BaseFormat
= texImage
->_BaseFormat
;
374 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
376 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
378 pipe_texture_reference( &strb
->texture
, pt
);
380 pipe_surface_reference(&strb
->surface
, NULL
);
382 /* new surface for rendering into the texture */
383 strb
->surface
= screen
->get_tex_surface(screen
,
388 PIPE_BUFFER_USAGE_GPU_READ
|
389 PIPE_BUFFER_USAGE_GPU_WRITE
);
391 init_renderbuffer_bits(strb
, pt
->format
);
394 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
395 att->Texture, pt, strb->surface, rb->Width, rb->Height);
398 /* Invalidate buffer state so that the pipe's framebuffer state
400 * That's where the new renderbuffer (which we just created) gets
401 * passed to the pipe as a (color/depth) render target.
403 st_invalidate_state(ctx
, _NEW_BUFFERS
);
408 * Called via ctx->Driver.FinishRenderTexture.
411 st_finish_render_texture(GLcontext
*ctx
,
412 struct gl_renderbuffer_attachment
*att
)
414 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
419 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
422 pipe_surface_reference( &strb
->surface
, NULL
);
427 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
430 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
432 /* restore previous framebuffer state */
433 st_invalidate_state(ctx
, _NEW_BUFFERS
);
438 * Validate a renderbuffer attachment for a particular usage.
442 st_validate_attachment(struct pipe_screen
*screen
,
443 const struct gl_renderbuffer_attachment
*att
,
446 const struct st_texture_object
*stObj
=
447 st_texture_object(att
->Texture
);
450 * Only validate texture attachments for now, since
451 * st_renderbuffer_alloc_storage makes sure that
452 * the format is supported.
455 if (att
->Type
!= GL_TEXTURE
)
461 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
467 * Check that the framebuffer configuration is valid in terms of what
468 * the driver can support.
470 * For Gallium we only supports combined Z+stencil, not separate buffers.
473 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
475 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
476 const struct gl_renderbuffer
*depthRb
=
477 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
478 const struct gl_renderbuffer
*stencilRb
=
479 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
482 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
483 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
487 if (!st_validate_attachment(screen
,
488 &fb
->Attachment
[BUFFER_DEPTH
],
489 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
490 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
493 if (!st_validate_attachment(screen
,
494 &fb
->Attachment
[BUFFER_STENCIL
],
495 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
496 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
499 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
500 if (!st_validate_attachment(screen
,
501 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
502 PIPE_TEXTURE_USAGE_RENDER_TARGET
)) {
503 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
511 * Copy back color buffer to front color buffer.
514 copy_back_to_front(struct st_context
*st
,
515 struct gl_framebuffer
*fb
,
516 gl_buffer_index frontIndex
,
517 gl_buffer_index backIndex
)
520 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
521 struct pipe_surface
*surf_front
, *surf_back
;
523 (void) st_get_framebuffer_surface(stfb
, frontIndex
, &surf_front
);
524 (void) st_get_framebuffer_surface(stfb
, backIndex
, &surf_back
);
526 if (surf_front
&& surf_back
) {
527 if (st
->pipe
->surface_copy
) {
528 st
->pipe
->surface_copy(st
->pipe
,
529 surf_front
, 0, 0, /* dest */
530 surf_back
, 0, 0, /* src */
531 fb
->Width
, fb
->Height
);
533 util_surface_copy(st
->pipe
, FALSE
,
536 fb
->Width
, fb
->Height
);
543 * Check if we're drawing into, or read from, a front color buffer. If the
544 * front buffer is missing, create it now.
546 * The back color buffer must exist since we'll use its format/samples info
547 * for creating the front buffer.
549 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
550 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
553 check_create_front_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
554 gl_buffer_index frontIndex
,
555 gl_buffer_index backIndex
)
557 if (fb
->Attachment
[frontIndex
].Renderbuffer
== NULL
) {
558 GLboolean create
= GL_FALSE
;
560 /* check if drawing to or reading from front buffer */
561 if (fb
->_ColorReadBufferIndex
== frontIndex
) {
566 for (b
= 0; b
< fb
->_NumColorDrawBuffers
; b
++) {
567 if (fb
->_ColorDrawBufferIndexes
[b
] == frontIndex
) {
575 struct st_renderbuffer
*back
;
576 struct gl_renderbuffer
*front
;
577 enum pipe_format colorFormat
;
581 _mesa_debug(ctx
, "Allocate new front buffer\n");
583 /* get back renderbuffer info */
584 back
= st_renderbuffer(fb
->Attachment
[backIndex
].Renderbuffer
);
585 colorFormat
= back
->format
;
586 samples
= back
->Base
.NumSamples
;
588 /* create front renderbuffer */
589 front
= st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
590 _mesa_add_renderbuffer(fb
, frontIndex
, front
);
592 /* alloc texture/surface for new front buffer */
593 front
->AllocStorage(ctx
, front
, front
->InternalFormat
,
594 fb
->Width
, fb
->Height
);
596 /* initialize the front color buffer contents by copying
599 copy_back_to_front(ctx
->st
, fb
, frontIndex
, backIndex
);
606 * If front left/right color buffers are missing, create them now.
609 check_create_front_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
611 /* check if we need to create the front left buffer now */
612 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_LEFT
, BUFFER_BACK_LEFT
);
614 if (fb
->Visual
.stereoMode
) {
615 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_RIGHT
, BUFFER_BACK_RIGHT
);
618 st_invalidate_state(ctx
, _NEW_BUFFERS
);
623 * Called via glDrawBuffer.
626 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
630 check_create_front_buffers(ctx
, ctx
->DrawBuffer
);
635 * Called via glReadBuffer.
638 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
641 check_create_front_buffers(ctx
, ctx
->ReadBuffer
);
645 void st_init_fbo_functions(struct dd_function_table
*functions
)
647 functions
->NewFramebuffer
= st_new_framebuffer
;
648 functions
->NewRenderbuffer
= st_new_renderbuffer
;
649 functions
->BindFramebuffer
= st_bind_framebuffer
;
650 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
651 functions
->RenderTexture
= st_render_texture
;
652 functions
->FinishRenderTexture
= st_finish_render_texture
;
653 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
654 /* no longer needed by core Mesa, drivers handle resizes...
655 functions->ResizeBuffers = st_resize_buffers;
658 functions
->DrawBuffers
= st_DrawBuffers
;
659 functions
->ReadBuffer
= st_ReadBuffer
;