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"
51 #include "st_manager.h"
53 #include "util/u_format.h"
54 #include "util/u_rect.h"
55 #include "util/u_inlines.h"
59 * gl_renderbuffer::AllocStorage()
60 * This is called to allocate the original drawing surface, and
61 * during window resize.
64 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
65 GLenum internalFormat
,
66 GLuint width
, GLuint height
)
68 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
69 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
70 enum pipe_format format
;
72 if (strb
->format
!= PIPE_FORMAT_NONE
)
73 format
= strb
->format
;
75 format
= st_choose_renderbuffer_format(screen
, internalFormat
);
77 /* init renderbuffer fields */
78 strb
->Base
.Width
= width
;
79 strb
->Base
.Height
= height
;
80 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
81 strb
->Base
.DataType
= st_format_datatype(format
);
83 strb
->defined
= GL_FALSE
; /* undefined contents now */
90 assert(strb
->format
!= PIPE_FORMAT_NONE
);
92 strb
->stride
= util_format_get_stride(strb
->format
, width
);
93 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
95 strb
->data
= malloc(size
);
97 return strb
->data
!= NULL
;
100 struct pipe_texture
template;
101 unsigned surface_usage
;
103 /* Free the old surface and texture
105 pipe_surface_reference( &strb
->surface
, NULL
);
106 pipe_texture_reference( &strb
->texture
, NULL
);
108 /* Setup new texture template.
110 memset(&template, 0, sizeof(template));
111 template.target
= PIPE_TEXTURE_2D
;
112 template.format
= format
;
113 template.width0
= width
;
114 template.height0
= height
;
116 template.last_level
= 0;
117 template.nr_samples
= rb
->NumSamples
;
118 if (util_format_is_depth_or_stencil(format
)) {
119 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
122 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
123 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
126 /* Probably need dedicated flags for surface usage too:
128 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
129 PIPE_BUFFER_USAGE_GPU_WRITE
);
131 PIPE_BUFFER_USAGE_CPU_READ
|
132 PIPE_BUFFER_USAGE_CPU_WRITE
);
135 strb
->texture
= screen
->texture_create(screen
, &template);
140 strb
->surface
= screen
->get_tex_surface(screen
,
145 assert(strb
->surface
->texture
);
146 assert(strb
->surface
->format
);
147 assert(strb
->surface
->width
== width
);
148 assert(strb
->surface
->height
== height
);
151 return strb
->surface
!= NULL
;
157 * gl_renderbuffer::Delete()
160 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
162 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
164 pipe_surface_reference(&strb
->surface
, NULL
);
165 pipe_texture_reference(&strb
->texture
, NULL
);
172 * gl_renderbuffer::GetPointer()
175 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
178 /* By returning NULL we force all software rendering to go through
182 assert(0); /* Should never get called with softpipe */
189 * Called via ctx->Driver.NewFramebuffer()
191 static struct gl_framebuffer
*
192 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
194 /* XXX not sure we need to subclass gl_framebuffer for pipe */
195 return _mesa_new_framebuffer(ctx
, name
);
200 * Called via ctx->Driver.NewRenderbuffer()
202 static struct gl_renderbuffer
*
203 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
205 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
207 _mesa_init_renderbuffer(&strb
->Base
, name
);
208 strb
->Base
.Delete
= st_renderbuffer_delete
;
209 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
210 strb
->Base
.GetPointer
= null_get_pointer
;
211 strb
->format
= PIPE_FORMAT_NONE
;
219 * Allocate a renderbuffer for a an on-screen window (not a user-created
220 * renderbuffer). The window system code determines the format.
222 struct gl_renderbuffer
*
223 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
225 struct st_renderbuffer
*strb
;
227 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
229 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
233 _mesa_init_renderbuffer(&strb
->Base
, 0);
234 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
235 strb
->Base
.NumSamples
= samples
;
236 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
237 strb
->Base
.DataType
= st_format_datatype(format
);
238 strb
->format
= format
;
242 case PIPE_FORMAT_B8G8R8A8_UNORM
:
243 case PIPE_FORMAT_A8R8G8B8_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_Z24S8_UNORM
:
258 case PIPE_FORMAT_S8Z24_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_UNORM
:
264 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
266 case PIPE_FORMAT_R16G16B16A16_SNORM
:
267 strb
->Base
.InternalFormat
= GL_RGBA16
;
271 "Unexpected format in st_new_renderbuffer_fb");
276 /* st-specific methods */
277 strb
->Base
.Delete
= st_renderbuffer_delete
;
278 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
279 strb
->Base
.GetPointer
= null_get_pointer
;
281 /* surface is allocated in st_renderbuffer_alloc_storage() */
282 strb
->surface
= NULL
;
291 * Called via ctx->Driver.BindFramebufferEXT().
294 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
295 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
301 * Called by ctx->Driver.FramebufferRenderbuffer
304 st_framebuffer_renderbuffer(GLcontext
*ctx
,
305 struct gl_framebuffer
*fb
,
307 struct gl_renderbuffer
*rb
)
309 /* XXX no need for derivation? */
310 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
315 * Called by ctx->Driver.RenderTexture
318 st_render_texture(GLcontext
*ctx
,
319 struct gl_framebuffer
*fb
,
320 struct gl_renderbuffer_attachment
*att
)
322 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
323 struct st_renderbuffer
*strb
;
324 struct gl_renderbuffer
*rb
;
325 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
326 struct st_texture_object
*stObj
;
327 const struct gl_texture_image
*texImage
;
330 /* When would this fail? Perhaps assert? */
334 /* The first gallium texture level = Mesa BaseLevel */
335 pt_level
= MAX2(0, (GLint
) att
->TextureLevel
- att
->Texture
->BaseLevel
);
336 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][pt_level
];
338 /* create new renderbuffer which wraps the texture image */
339 rb
= st_new_renderbuffer(ctx
, 0);
341 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
345 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
346 assert(rb
->RefCount
== 1);
347 rb
->AllocStorage
= NULL
; /* should not get called */
348 strb
= st_renderbuffer(rb
);
350 assert(strb
->Base
.RefCount
> 0);
352 /* get the texture for the texture object */
353 stObj
= st_texture_object(att
->Texture
);
355 /* point renderbuffer at texobject */
357 strb
->rtt_level
= pt_level
;
358 strb
->rtt_face
= att
->CubeMapFace
;
359 strb
->rtt_slice
= att
->Zoffset
;
361 rb
->Width
= texImage
->Width2
;
362 rb
->Height
= texImage
->Height2
;
363 rb
->_BaseFormat
= texImage
->_BaseFormat
;
364 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
366 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
368 pipe_texture_reference( &strb
->texture
, pt
);
370 pipe_surface_reference(&strb
->surface
, NULL
);
372 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
374 /* new surface for rendering into the texture */
375 strb
->surface
= screen
->get_tex_surface(screen
,
380 PIPE_BUFFER_USAGE_GPU_READ
|
381 PIPE_BUFFER_USAGE_GPU_WRITE
);
383 strb
->format
= pt
->format
;
385 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
386 strb
->Base
.DataType
= st_format_datatype(pt
->format
);
389 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
390 att->Texture, pt, strb->surface, rb->Width, rb->Height);
393 /* Invalidate buffer state so that the pipe's framebuffer state
395 * That's where the new renderbuffer (which we just created) gets
396 * passed to the pipe as a (color/depth) render target.
398 st_invalidate_state(ctx
, _NEW_BUFFERS
);
403 * Called via ctx->Driver.FinishRenderTexture.
406 st_finish_render_texture(GLcontext
*ctx
,
407 struct gl_renderbuffer_attachment
*att
)
409 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
414 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
419 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
422 /* restore previous framebuffer state */
423 st_invalidate_state(ctx
, _NEW_BUFFERS
);
428 * Validate a renderbuffer attachment for a particular usage.
432 st_validate_attachment(struct pipe_screen
*screen
,
433 const struct gl_renderbuffer_attachment
*att
,
436 const struct st_texture_object
*stObj
=
437 st_texture_object(att
->Texture
);
440 * Only validate texture attachments for now, since
441 * st_renderbuffer_alloc_storage makes sure that
442 * the format is supported.
445 if (att
->Type
!= GL_TEXTURE
)
451 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
457 * Check that the framebuffer configuration is valid in terms of what
458 * the driver can support.
460 * For Gallium we only supports combined Z+stencil, not separate buffers.
463 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
465 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
466 const struct gl_renderbuffer
*depthRb
=
467 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
468 const struct gl_renderbuffer
*stencilRb
=
469 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
472 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
473 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
477 if (!st_validate_attachment(screen
,
478 &fb
->Attachment
[BUFFER_DEPTH
],
479 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
480 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
483 if (!st_validate_attachment(screen
,
484 &fb
->Attachment
[BUFFER_STENCIL
],
485 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
486 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
489 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
490 if (!st_validate_attachment(screen
,
491 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
492 PIPE_TEXTURE_USAGE_RENDER_TARGET
)) {
493 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
501 * Copy back color buffer to front color buffer.
504 copy_back_to_front(struct st_context
*st
,
505 struct gl_framebuffer
*fb
,
506 gl_buffer_index frontIndex
,
507 gl_buffer_index backIndex
)
510 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
511 struct pipe_surface
*surf_front
, *surf_back
;
513 (void) st_get_framebuffer_surface(stfb
, frontIndex
, &surf_front
);
514 (void) st_get_framebuffer_surface(stfb
, backIndex
, &surf_back
);
516 if (surf_front
&& surf_back
) {
517 if (st
->pipe
->surface_copy
) {
518 st
->pipe
->surface_copy(st
->pipe
,
519 surf_front
, 0, 0, /* dest */
520 surf_back
, 0, 0, /* src */
521 fb
->Width
, fb
->Height
);
523 util_surface_copy(st
->pipe
, FALSE
,
526 fb
->Width
, fb
->Height
);
533 * Check if we're drawing into, or read from, a front color buffer. If the
534 * front buffer is missing, create it now.
536 * The back color buffer must exist since we'll use its format/samples info
537 * for creating the front buffer.
539 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
540 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
543 check_create_front_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
544 gl_buffer_index frontIndex
,
545 gl_buffer_index backIndex
)
547 if (fb
->Attachment
[frontIndex
].Renderbuffer
== NULL
) {
548 GLboolean create
= GL_FALSE
;
550 /* check if drawing to or reading from front buffer */
551 if (fb
->_ColorReadBufferIndex
== frontIndex
) {
556 for (b
= 0; b
< fb
->_NumColorDrawBuffers
; b
++) {
557 if (fb
->_ColorDrawBufferIndexes
[b
] == frontIndex
) {
565 struct st_renderbuffer
*back
;
566 struct gl_renderbuffer
*front
;
567 enum pipe_format colorFormat
;
571 _mesa_debug(ctx
, "Allocate new front buffer\n");
573 /* get back renderbuffer info */
574 back
= st_renderbuffer(fb
->Attachment
[backIndex
].Renderbuffer
);
575 colorFormat
= back
->format
;
576 samples
= back
->Base
.NumSamples
;
578 /* create front renderbuffer */
579 front
= st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
580 _mesa_add_renderbuffer(fb
, frontIndex
, front
);
582 /* alloc texture/surface for new front buffer */
583 front
->AllocStorage(ctx
, front
, front
->InternalFormat
,
584 fb
->Width
, fb
->Height
);
586 /* initialize the front color buffer contents by copying
589 copy_back_to_front(ctx
->st
, fb
, frontIndex
, backIndex
);
596 * If front left/right color buffers are missing, create them now.
599 check_create_front_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
601 /* check if we need to create the front left buffer now */
602 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_LEFT
, BUFFER_BACK_LEFT
);
604 if (fb
->Visual
.stereoMode
) {
605 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_RIGHT
, BUFFER_BACK_RIGHT
);
608 st_invalidate_state(ctx
, _NEW_BUFFERS
);
613 * Called via glDrawBuffer.
616 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
618 GLframebuffer
*fb
= ctx
->DrawBuffer
;
624 /* add the renderbuffers on demand */
625 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
626 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
627 st_manager_add_color_renderbuffer(ctx
->st
, fb
, idx
);
630 check_create_front_buffers(ctx
, ctx
->DrawBuffer
);
635 * Called via glReadBuffer.
638 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
640 GLframebuffer
*fb
= ctx
->ReadBuffer
;
644 /* add the renderbuffer on demand */
645 st_manager_add_color_renderbuffer(ctx
->st
, fb
, fb
->_ColorReadBufferIndex
);
646 check_create_front_buffers(ctx
, fb
);
650 void st_init_fbo_functions(struct dd_function_table
*functions
)
652 functions
->NewFramebuffer
= st_new_framebuffer
;
653 functions
->NewRenderbuffer
= st_new_renderbuffer
;
654 functions
->BindFramebuffer
= st_bind_framebuffer
;
655 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
656 functions
->RenderTexture
= st_render_texture
;
657 functions
->FinishRenderTexture
= st_finish_render_texture
;
658 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
659 /* no longer needed by core Mesa, drivers handle resizes...
660 functions->ResizeBuffers = st_resize_buffers;
663 functions
->DrawBuffers
= st_DrawBuffers
;
664 functions
->ReadBuffer
= st_ReadBuffer
;