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/renderbuffer.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_screen.h"
46 #include "st_context.h"
47 #include "st_cb_fbo.h"
48 #include "st_format.h"
49 #include "st_public.h"
50 #include "st_texture.h"
52 #include "util/u_format.h"
53 #include "util/u_rect.h"
54 #include "util/u_inlines.h"
58 * gl_renderbuffer::AllocStorage()
59 * This is called to allocate the original drawing surface, and
60 * during window resize.
63 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
64 GLenum internalFormat
,
65 GLuint width
, GLuint height
)
67 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
68 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
69 enum pipe_format format
;
71 if (strb
->format
!= PIPE_FORMAT_NONE
)
72 format
= strb
->format
;
74 format
= st_choose_renderbuffer_format(screen
, internalFormat
);
76 /* init renderbuffer fields */
77 strb
->Base
.Width
= width
;
78 strb
->Base
.Height
= height
;
79 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
80 strb
->Base
.DataType
= st_format_datatype(format
);
82 strb
->defined
= GL_FALSE
; /* undefined contents now */
89 assert(strb
->format
!= PIPE_FORMAT_NONE
);
91 strb
->stride
= util_format_get_stride(strb
->format
, width
);
92 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
94 strb
->data
= malloc(size
);
96 return strb
->data
!= NULL
;
99 struct pipe_texture
template;
100 unsigned surface_usage
;
102 /* Free the old surface and texture
104 pipe_surface_reference( &strb
->surface
, NULL
);
105 pipe_texture_reference( &strb
->texture
, NULL
);
107 /* Setup new texture template.
109 memset(&template, 0, sizeof(template));
110 template.target
= PIPE_TEXTURE_2D
;
111 template.format
= format
;
112 template.width0
= width
;
113 template.height0
= height
;
115 template.last_level
= 0;
116 template.nr_samples
= rb
->NumSamples
;
117 if (util_format_is_depth_or_stencil(format
)) {
118 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
121 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
122 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
125 /* Probably need dedicated flags for surface usage too:
127 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
128 PIPE_BUFFER_USAGE_GPU_WRITE
);
130 PIPE_BUFFER_USAGE_CPU_READ
|
131 PIPE_BUFFER_USAGE_CPU_WRITE
);
134 strb
->texture
= screen
->texture_create(screen
, &template);
139 strb
->surface
= screen
->get_tex_surface(screen
,
144 assert(strb
->surface
->texture
);
145 assert(strb
->surface
->format
);
146 assert(strb
->surface
->width
== width
);
147 assert(strb
->surface
->height
== height
);
150 return strb
->surface
!= NULL
;
156 * gl_renderbuffer::Delete()
159 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
161 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
163 pipe_surface_reference(&strb
->surface
, NULL
);
164 pipe_texture_reference(&strb
->texture
, NULL
);
171 * gl_renderbuffer::GetPointer()
174 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
177 /* By returning NULL we force all software rendering to go through
181 assert(0); /* Should never get called with softpipe */
188 * Called via ctx->Driver.NewFramebuffer()
190 static struct gl_framebuffer
*
191 st_new_framebuffer(GLcontext
*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(GLcontext
*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
->Base
.GetPointer
= null_get_pointer
;
210 strb
->format
= PIPE_FORMAT_NONE
;
218 * Allocate a renderbuffer for a an on-screen window (not a user-created
219 * renderbuffer). The window system code determines the format.
221 struct gl_renderbuffer
*
222 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
224 struct st_renderbuffer
*strb
;
226 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
228 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
232 _mesa_init_renderbuffer(&strb
->Base
, 0);
233 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
234 strb
->Base
.NumSamples
= samples
;
235 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
236 strb
->Base
.DataType
= st_format_datatype(format
);
237 strb
->format
= format
;
241 case PIPE_FORMAT_B8G8R8A8_UNORM
:
242 case PIPE_FORMAT_A8R8G8B8_UNORM
:
243 case PIPE_FORMAT_B8G8R8X8_UNORM
:
244 case PIPE_FORMAT_X8R8G8B8_UNORM
:
245 case PIPE_FORMAT_B5G5R5A1_UNORM
:
246 case PIPE_FORMAT_B4G4R4A4_UNORM
:
247 case PIPE_FORMAT_B5G6R5_UNORM
:
248 strb
->Base
.InternalFormat
= GL_RGBA
;
250 case PIPE_FORMAT_Z16_UNORM
:
251 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
253 case PIPE_FORMAT_Z32_UNORM
:
254 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
256 case PIPE_FORMAT_Z24S8_UNORM
:
257 case PIPE_FORMAT_S8Z24_UNORM
:
258 case PIPE_FORMAT_Z24X8_UNORM
:
259 case PIPE_FORMAT_X8Z24_UNORM
:
260 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
262 case PIPE_FORMAT_S8_UNORM
:
263 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
265 case PIPE_FORMAT_R16G16B16A16_SNORM
:
266 strb
->Base
.InternalFormat
= GL_RGBA16
;
270 "Unexpected format in st_new_renderbuffer_fb");
275 /* st-specific methods */
276 strb
->Base
.Delete
= st_renderbuffer_delete
;
277 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
278 strb
->Base
.GetPointer
= null_get_pointer
;
280 /* surface is allocated in st_renderbuffer_alloc_storage() */
281 strb
->surface
= NULL
;
290 * Called via ctx->Driver.BindFramebufferEXT().
293 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
294 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
300 * Called by ctx->Driver.FramebufferRenderbuffer
303 st_framebuffer_renderbuffer(GLcontext
*ctx
,
304 struct gl_framebuffer
*fb
,
306 struct gl_renderbuffer
*rb
)
308 /* XXX no need for derivation? */
309 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
314 * Called by ctx->Driver.RenderTexture
317 st_render_texture(GLcontext
*ctx
,
318 struct gl_framebuffer
*fb
,
319 struct gl_renderbuffer_attachment
*att
)
321 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
322 struct st_renderbuffer
*strb
;
323 struct gl_renderbuffer
*rb
;
324 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
325 struct st_texture_object
*stObj
;
326 const struct gl_texture_image
*texImage
;
329 /* When would this fail? Perhaps assert? */
333 /* The first gallium texture level = Mesa BaseLevel */
334 pt_level
= MAX2(0, (GLint
) att
->TextureLevel
- att
->Texture
->BaseLevel
);
335 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][pt_level
];
337 /* create new renderbuffer which wraps the texture image */
338 rb
= st_new_renderbuffer(ctx
, 0);
340 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
344 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
345 assert(rb
->RefCount
== 1);
346 rb
->AllocStorage
= NULL
; /* should not get called */
347 strb
= st_renderbuffer(rb
);
349 assert(strb
->Base
.RefCount
> 0);
351 /* get the texture for the texture object */
352 stObj
= st_texture_object(att
->Texture
);
354 /* point renderbuffer at texobject */
356 strb
->rtt_level
= pt_level
;
357 strb
->rtt_face
= att
->CubeMapFace
;
358 strb
->rtt_slice
= att
->Zoffset
;
360 rb
->Width
= texImage
->Width2
;
361 rb
->Height
= texImage
->Height2
;
362 rb
->_BaseFormat
= texImage
->_BaseFormat
;
363 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
365 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
367 pipe_texture_reference( &strb
->texture
, pt
);
369 pipe_surface_reference(&strb
->surface
, NULL
);
371 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
373 /* new surface for rendering into the texture */
374 strb
->surface
= screen
->get_tex_surface(screen
,
379 PIPE_BUFFER_USAGE_GPU_READ
|
380 PIPE_BUFFER_USAGE_GPU_WRITE
);
382 strb
->format
= pt
->format
;
384 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
385 strb
->Base
.DataType
= st_format_datatype(pt
->format
);
388 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
389 att->Texture, pt, strb->surface, rb->Width, rb->Height);
392 /* Invalidate buffer state so that the pipe's framebuffer state
394 * That's where the new renderbuffer (which we just created) gets
395 * passed to the pipe as a (color/depth) render target.
397 st_invalidate_state(ctx
, _NEW_BUFFERS
);
402 * Called via ctx->Driver.FinishRenderTexture.
405 st_finish_render_texture(GLcontext
*ctx
,
406 struct gl_renderbuffer_attachment
*att
)
408 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
413 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
418 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
421 /* restore previous framebuffer state */
422 st_invalidate_state(ctx
, _NEW_BUFFERS
);
427 * Validate a renderbuffer attachment for a particular usage.
431 st_validate_attachment(struct pipe_screen
*screen
,
432 const struct gl_renderbuffer_attachment
*att
,
435 const struct st_texture_object
*stObj
=
436 st_texture_object(att
->Texture
);
439 * Only validate texture attachments for now, since
440 * st_renderbuffer_alloc_storage makes sure that
441 * the format is supported.
444 if (att
->Type
!= GL_TEXTURE
)
450 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
456 * Check that the framebuffer configuration is valid in terms of what
457 * the driver can support.
459 * For Gallium we only supports combined Z+stencil, not separate buffers.
462 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
464 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
465 const struct gl_renderbuffer
*depthRb
=
466 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
467 const struct gl_renderbuffer
*stencilRb
=
468 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
471 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
472 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
476 if (!st_validate_attachment(screen
,
477 &fb
->Attachment
[BUFFER_DEPTH
],
478 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
479 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
482 if (!st_validate_attachment(screen
,
483 &fb
->Attachment
[BUFFER_STENCIL
],
484 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
485 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
488 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
489 if (!st_validate_attachment(screen
,
490 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
491 PIPE_TEXTURE_USAGE_RENDER_TARGET
)) {
492 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
500 * Copy back color buffer to front color buffer.
503 copy_back_to_front(struct st_context
*st
,
504 struct gl_framebuffer
*fb
,
505 gl_buffer_index frontIndex
,
506 gl_buffer_index backIndex
)
509 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
510 struct pipe_surface
*surf_front
, *surf_back
;
512 (void) st_get_framebuffer_surface(stfb
, frontIndex
, &surf_front
);
513 (void) st_get_framebuffer_surface(stfb
, backIndex
, &surf_back
);
515 if (surf_front
&& surf_back
) {
516 if (st
->pipe
->surface_copy
) {
517 st
->pipe
->surface_copy(st
->pipe
,
518 surf_front
, 0, 0, /* dest */
519 surf_back
, 0, 0, /* src */
520 fb
->Width
, fb
->Height
);
522 util_surface_copy(st
->pipe
, FALSE
,
525 fb
->Width
, fb
->Height
);
532 * Check if we're drawing into, or read from, a front color buffer. If the
533 * front buffer is missing, create it now.
535 * The back color buffer must exist since we'll use its format/samples info
536 * for creating the front buffer.
538 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
539 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
542 check_create_front_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
543 gl_buffer_index frontIndex
,
544 gl_buffer_index backIndex
)
546 if (fb
->Attachment
[frontIndex
].Renderbuffer
== NULL
) {
547 GLboolean create
= GL_FALSE
;
549 /* check if drawing to or reading from front buffer */
550 if (fb
->_ColorReadBufferIndex
== frontIndex
) {
555 for (b
= 0; b
< fb
->_NumColorDrawBuffers
; b
++) {
556 if (fb
->_ColorDrawBufferIndexes
[b
] == frontIndex
) {
564 struct st_renderbuffer
*back
;
565 struct gl_renderbuffer
*front
;
566 enum pipe_format colorFormat
;
570 _mesa_debug(ctx
, "Allocate new front buffer\n");
572 /* get back renderbuffer info */
573 back
= st_renderbuffer(fb
->Attachment
[backIndex
].Renderbuffer
);
574 colorFormat
= back
->format
;
575 samples
= back
->Base
.NumSamples
;
577 /* create front renderbuffer */
578 front
= st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
579 _mesa_add_renderbuffer(fb
, frontIndex
, front
);
581 /* alloc texture/surface for new front buffer */
582 front
->AllocStorage(ctx
, front
, front
->InternalFormat
,
583 fb
->Width
, fb
->Height
);
585 /* initialize the front color buffer contents by copying
588 copy_back_to_front(ctx
->st
, fb
, frontIndex
, backIndex
);
595 * If front left/right color buffers are missing, create them now.
598 check_create_front_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
600 /* check if we need to create the front left buffer now */
601 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_LEFT
, BUFFER_BACK_LEFT
);
603 if (fb
->Visual
.stereoMode
) {
604 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_RIGHT
, BUFFER_BACK_RIGHT
);
607 st_invalidate_state(ctx
, _NEW_BUFFERS
);
612 * Called via glDrawBuffer.
615 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
619 check_create_front_buffers(ctx
, ctx
->DrawBuffer
);
624 * Called via glReadBuffer.
627 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
630 check_create_front_buffers(ctx
, ctx
->ReadBuffer
);
634 void st_init_fbo_functions(struct dd_function_table
*functions
)
636 functions
->NewFramebuffer
= st_new_framebuffer
;
637 functions
->NewRenderbuffer
= st_new_renderbuffer
;
638 functions
->BindFramebuffer
= st_bind_framebuffer
;
639 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
640 functions
->RenderTexture
= st_render_texture
;
641 functions
->FinishRenderTexture
= st_finish_render_texture
;
642 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
643 /* no longer needed by core Mesa, drivers handle resizes...
644 functions->ResizeBuffers = st_resize_buffers;
647 functions
->DrawBuffers
= st_DrawBuffers
;
648 functions
->ReadBuffer
= st_ReadBuffer
;