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_cb_texture.h"
48 #include "st_format.h"
49 #include "st_public.h"
50 #include "st_texture.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 */
101 struct pipe_format_block block
;
104 _mesa_free(strb
->data
);
106 assert(strb
->format
!= PIPE_FORMAT_NONE
);
107 pf_get_block(strb
->format
, &block
);
109 strb
->stride
= pf_get_stride(&block
, width
);
110 size
= pf_get_2d_size(&block
, strb
->stride
, height
);
112 strb
->data
= _mesa_malloc(size
);
114 return strb
->data
!= NULL
;
117 struct pipe_texture
template;
118 unsigned surface_usage
;
120 /* Free the old surface and texture
122 pipe_surface_reference( &strb
->surface
, NULL
);
123 pipe_texture_reference( &strb
->texture
, NULL
);
125 /* Setup new texture template.
127 memset(&template, 0, sizeof(template));
128 template.target
= PIPE_TEXTURE_2D
;
129 template.format
= format
;
130 pf_get_block(format
, &template.block
);
131 template.width0
= width
;
132 template.height0
= height
;
134 template.last_level
= 0;
135 template.nr_samples
= rb
->NumSamples
;
136 if (pf_is_depth_stencil(format
)) {
137 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
140 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
141 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
144 /* Probably need dedicated flags for surface usage too:
146 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
147 PIPE_BUFFER_USAGE_GPU_WRITE
);
149 PIPE_BUFFER_USAGE_CPU_READ
|
150 PIPE_BUFFER_USAGE_CPU_WRITE
);
153 strb
->texture
= pipe
->screen
->texture_create( pipe
->screen
,
159 strb
->surface
= pipe
->screen
->get_tex_surface( pipe
->screen
,
164 assert(strb
->surface
->texture
);
165 assert(strb
->surface
->format
);
166 assert(strb
->surface
->width
== width
);
167 assert(strb
->surface
->height
== height
);
170 return strb
->surface
!= NULL
;
176 * gl_renderbuffer::Delete()
179 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
181 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
183 pipe_surface_reference(&strb
->surface
, NULL
);
184 pipe_texture_reference(&strb
->texture
, NULL
);
185 _mesa_free(strb
->data
);
191 * gl_renderbuffer::GetPointer()
194 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
197 /* By returning NULL we force all software rendering to go through
201 assert(0); /* Should never get called with softpipe */
208 * Called via ctx->Driver.NewFramebuffer()
210 static struct gl_framebuffer
*
211 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
213 /* XXX not sure we need to subclass gl_framebuffer for pipe */
214 return _mesa_new_framebuffer(ctx
, name
);
219 * Called via ctx->Driver.NewRenderbuffer()
221 static struct gl_renderbuffer
*
222 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
224 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
226 _mesa_init_renderbuffer(&strb
->Base
, name
);
227 strb
->Base
.Delete
= st_renderbuffer_delete
;
228 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
229 strb
->Base
.GetPointer
= null_get_pointer
;
230 strb
->format
= PIPE_FORMAT_NONE
;
238 * Allocate a renderbuffer for a an on-screen window (not a user-created
239 * renderbuffer). The window system code determines the format.
241 struct gl_renderbuffer
*
242 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
244 struct st_renderbuffer
*strb
;
246 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
248 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
252 _mesa_init_renderbuffer(&strb
->Base
, 0);
253 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
254 strb
->Base
.NumSamples
= samples
;
255 strb
->format
= format
;
256 init_renderbuffer_bits(strb
, format
);
260 case PIPE_FORMAT_A8R8G8B8_UNORM
:
261 case PIPE_FORMAT_B8G8R8A8_UNORM
:
262 case PIPE_FORMAT_X8R8G8B8_UNORM
:
263 case PIPE_FORMAT_B8G8R8X8_UNORM
:
264 case PIPE_FORMAT_A1R5G5B5_UNORM
:
265 case PIPE_FORMAT_A4R4G4B4_UNORM
:
266 case PIPE_FORMAT_R5G6B5_UNORM
:
267 strb
->Base
.InternalFormat
= GL_RGBA
;
269 case PIPE_FORMAT_Z16_UNORM
:
270 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
272 case PIPE_FORMAT_Z32_UNORM
:
273 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
275 case PIPE_FORMAT_S8Z24_UNORM
:
276 case PIPE_FORMAT_Z24S8_UNORM
:
277 case PIPE_FORMAT_X8Z24_UNORM
:
278 case PIPE_FORMAT_Z24X8_UNORM
:
279 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
281 case PIPE_FORMAT_S8_UNORM
:
282 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
284 case PIPE_FORMAT_R16G16B16A16_SNORM
:
285 strb
->Base
.InternalFormat
= GL_RGBA16
;
289 "Unexpected format in st_new_renderbuffer_fb");
294 /* st-specific methods */
295 strb
->Base
.Delete
= st_renderbuffer_delete
;
296 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
297 strb
->Base
.GetPointer
= null_get_pointer
;
299 /* surface is allocated in st_renderbuffer_alloc_storage() */
300 strb
->surface
= NULL
;
309 * Called via ctx->Driver.BindFramebufferEXT().
312 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
313 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
319 * Called by ctx->Driver.FramebufferRenderbuffer
322 st_framebuffer_renderbuffer(GLcontext
*ctx
,
323 struct gl_framebuffer
*fb
,
325 struct gl_renderbuffer
*rb
)
327 /* XXX no need for derivation? */
328 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
333 * Called by ctx->Driver.RenderTexture
336 st_render_texture(GLcontext
*ctx
,
337 struct gl_framebuffer
*fb
,
338 struct gl_renderbuffer_attachment
*att
)
340 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
341 struct st_renderbuffer
*strb
;
342 struct gl_renderbuffer
*rb
;
343 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
344 struct st_texture_object
*stObj
;
345 const struct gl_texture_image
*texImage
=
346 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
351 /* create new renderbuffer which wraps the texture image */
352 rb
= st_new_renderbuffer(ctx
, 0);
354 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
358 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
359 assert(rb
->RefCount
== 1);
360 rb
->AllocStorage
= NULL
; /* should not get called */
361 strb
= st_renderbuffer(rb
);
363 assert(strb
->Base
.RefCount
> 0);
365 /* get the texture for the texture object */
366 stObj
= st_texture_object(att
->Texture
);
368 /* point renderbuffer at texobject */
370 strb
->rtt_level
= att
->TextureLevel
;
371 strb
->rtt_face
= att
->CubeMapFace
;
372 strb
->rtt_slice
= att
->Zoffset
;
374 rb
->Width
= texImage
->Width2
;
375 rb
->Height
= texImage
->Height2
;
376 rb
->_BaseFormat
= texImage
->_BaseFormat
;
377 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
379 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
381 pipe_texture_reference( &strb
->texture
, pt
);
383 pipe_surface_reference(&strb
->surface
, NULL
);
385 /* new surface for rendering into the texture */
386 strb
->surface
= screen
->get_tex_surface(screen
,
391 PIPE_BUFFER_USAGE_GPU_READ
|
392 PIPE_BUFFER_USAGE_GPU_WRITE
);
394 init_renderbuffer_bits(strb
, pt
->format
);
397 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
398 att->Texture, pt, strb->surface, rb->Width, rb->Height);
401 /* Invalidate buffer state so that the pipe's framebuffer state
403 * That's where the new renderbuffer (which we just created) gets
404 * passed to the pipe as a (color/depth) render target.
406 st_invalidate_state(ctx
, _NEW_BUFFERS
);
411 * Called via ctx->Driver.FinishRenderTexture.
414 st_finish_render_texture(GLcontext
*ctx
,
415 struct gl_renderbuffer_attachment
*att
)
417 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
422 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
425 pipe_surface_reference( &strb
->surface
, NULL
);
430 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
433 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
435 /* restore previous framebuffer state */
436 st_invalidate_state(ctx
, _NEW_BUFFERS
);
441 * Validate a renderbuffer attachment for a particular usage.
445 st_validate_attachment(struct pipe_screen
*screen
,
446 const struct gl_renderbuffer_attachment
*att
,
449 const struct st_texture_object
*stObj
=
450 st_texture_object(att
->Texture
);
453 * Only validate texture attachments for now, since
454 * st_renderbuffer_alloc_storage makes sure that
455 * the format is supported.
458 if (att
->Type
!= GL_TEXTURE
)
464 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
470 * Check that the framebuffer configuration is valid in terms of what
471 * the driver can support.
473 * For Gallium we only supports combined Z+stencil, not separate buffers.
476 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
478 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
479 const struct gl_renderbuffer
*depthRb
=
480 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
481 const struct gl_renderbuffer
*stencilRb
=
482 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
485 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
486 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
490 if (!st_validate_attachment(screen
,
491 &fb
->Attachment
[BUFFER_DEPTH
],
492 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
493 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
496 if (!st_validate_attachment(screen
,
497 &fb
->Attachment
[BUFFER_STENCIL
],
498 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
499 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
502 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
503 if (!st_validate_attachment(screen
,
504 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
505 PIPE_TEXTURE_USAGE_RENDER_TARGET
)) {
506 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
514 * Copy back color buffer to front color buffer.
517 copy_back_to_front(struct st_context
*st
,
518 struct gl_framebuffer
*fb
,
519 gl_buffer_index frontIndex
,
520 gl_buffer_index backIndex
)
523 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
524 struct pipe_surface
*surf_front
, *surf_back
;
526 (void) st_get_framebuffer_surface(stfb
, frontIndex
, &surf_front
);
527 (void) st_get_framebuffer_surface(stfb
, backIndex
, &surf_back
);
529 if (surf_front
&& surf_back
) {
530 if (st
->pipe
->surface_copy
) {
531 st
->pipe
->surface_copy(st
->pipe
,
532 surf_front
, 0, 0, /* dest */
533 surf_back
, 0, 0, /* src */
534 fb
->Width
, fb
->Height
);
536 util_surface_copy(st
->pipe
, FALSE
,
539 fb
->Width
, fb
->Height
);
546 * Check if we're drawing into, or read from, a front color buffer. If the
547 * front buffer is missing, create it now.
549 * The back color buffer must exist since we'll use its format/samples info
550 * for creating the front buffer.
552 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
553 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
556 check_create_front_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
557 gl_buffer_index frontIndex
,
558 gl_buffer_index backIndex
)
560 if (fb
->Attachment
[frontIndex
].Renderbuffer
== NULL
) {
561 GLboolean create
= GL_FALSE
;
563 /* check if drawing to or reading from front buffer */
564 if (fb
->_ColorReadBufferIndex
== frontIndex
) {
569 for (b
= 0; b
< fb
->_NumColorDrawBuffers
; b
++) {
570 if (fb
->_ColorDrawBufferIndexes
[b
] == frontIndex
) {
578 struct st_renderbuffer
*back
;
579 struct gl_renderbuffer
*front
;
580 enum pipe_format colorFormat
;
584 _mesa_debug(ctx
, "Allocate new front buffer\n");
586 /* get back renderbuffer info */
587 back
= st_renderbuffer(fb
->Attachment
[backIndex
].Renderbuffer
);
588 colorFormat
= back
->format
;
589 samples
= back
->Base
.NumSamples
;
591 /* create front renderbuffer */
592 front
= st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
593 _mesa_add_renderbuffer(fb
, frontIndex
, front
);
595 /* alloc texture/surface for new front buffer */
596 front
->AllocStorage(ctx
, front
, front
->InternalFormat
,
597 fb
->Width
, fb
->Height
);
599 /* initialize the front color buffer contents by copying
602 copy_back_to_front(ctx
->st
, fb
, frontIndex
, backIndex
);
609 * If front left/right color buffers are missing, create them now.
612 check_create_front_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
614 /* check if we need to create the front left buffer now */
615 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_LEFT
, BUFFER_BACK_LEFT
);
617 if (fb
->Visual
.stereoMode
) {
618 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_RIGHT
, BUFFER_BACK_RIGHT
);
621 st_invalidate_state(ctx
, _NEW_BUFFERS
);
626 * Called via glDrawBuffer.
629 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
633 check_create_front_buffers(ctx
, ctx
->DrawBuffer
);
638 * Called via glReadBuffer.
641 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
644 check_create_front_buffers(ctx
, ctx
->ReadBuffer
);
648 void st_init_fbo_functions(struct dd_function_table
*functions
)
650 functions
->NewFramebuffer
= st_new_framebuffer
;
651 functions
->NewRenderbuffer
= st_new_renderbuffer
;
652 functions
->BindFramebuffer
= st_bind_framebuffer
;
653 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
654 functions
->RenderTexture
= st_render_texture
;
655 functions
->FinishRenderTexture
= st_finish_render_texture
;
656 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
657 /* no longer needed by core Mesa, drivers handle resizes...
658 functions->ResizeBuffers = st_resize_buffers;
661 functions
->DrawBuffers
= st_DrawBuffers
;
662 functions
->ReadBuffer
= st_ReadBuffer
;