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_format.h"
53 #include "util/u_rect.h"
57 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
60 init_renderbuffer_bits(struct st_renderbuffer
*strb
,
61 enum pipe_format pipeFormat
)
63 struct pipe_format_info info
;
65 if (!st_get_format_info( pipeFormat
, &info
)) {
69 strb
->Base
.Format
= info
.mesa_format
;
70 strb
->Base
.DataType
= st_format_datatype(pipeFormat
);
76 * gl_renderbuffer::AllocStorage()
77 * This is called to allocate the original drawing surface, and
78 * during window resize.
81 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
82 GLenum internalFormat
,
83 GLuint width
, GLuint height
)
85 struct pipe_context
*pipe
= ctx
->st
->pipe
;
86 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
87 enum pipe_format format
;
89 if (strb
->format
!= PIPE_FORMAT_NONE
)
90 format
= strb
->format
;
92 format
= st_choose_renderbuffer_format(pipe
->screen
, internalFormat
);
94 /* init renderbuffer fields */
95 strb
->Base
.Width
= width
;
96 strb
->Base
.Height
= height
;
97 init_renderbuffer_bits(strb
, format
);
99 strb
->defined
= GL_FALSE
; /* undefined contents now */
102 struct pipe_format_block block
;
105 _mesa_free(strb
->data
);
107 assert(strb
->format
!= PIPE_FORMAT_NONE
);
108 pf_get_block(strb
->format
, &block
);
110 strb
->stride
= pf_get_stride(&block
, width
);
111 size
= pf_get_2d_size(&block
, strb
->stride
, height
);
113 strb
->data
= _mesa_malloc(size
);
115 return strb
->data
!= NULL
;
118 struct pipe_texture
template;
119 unsigned surface_usage
;
121 /* Free the old surface and texture
123 pipe_surface_reference( &strb
->surface
, NULL
);
124 pipe_texture_reference( &strb
->texture
, NULL
);
126 /* Setup new texture template.
128 memset(&template, 0, sizeof(template));
129 template.target
= PIPE_TEXTURE_2D
;
130 template.format
= format
;
131 pf_get_block(format
, &template.block
);
132 template.width0
= width
;
133 template.height0
= height
;
135 template.last_level
= 0;
136 template.nr_samples
= rb
->NumSamples
;
137 if (util_format_is_depth_or_stencil(format
)) {
138 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
141 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
142 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
145 /* Probably need dedicated flags for surface usage too:
147 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
148 PIPE_BUFFER_USAGE_GPU_WRITE
);
150 PIPE_BUFFER_USAGE_CPU_READ
|
151 PIPE_BUFFER_USAGE_CPU_WRITE
);
154 strb
->texture
= pipe
->screen
->texture_create( pipe
->screen
,
160 strb
->surface
= pipe
->screen
->get_tex_surface( pipe
->screen
,
165 assert(strb
->surface
->texture
);
166 assert(strb
->surface
->format
);
167 assert(strb
->surface
->width
== width
);
168 assert(strb
->surface
->height
== height
);
171 return strb
->surface
!= NULL
;
177 * gl_renderbuffer::Delete()
180 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
182 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
184 pipe_surface_reference(&strb
->surface
, NULL
);
185 pipe_texture_reference(&strb
->texture
, NULL
);
186 _mesa_free(strb
->data
);
192 * gl_renderbuffer::GetPointer()
195 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
198 /* By returning NULL we force all software rendering to go through
202 assert(0); /* Should never get called with softpipe */
209 * Called via ctx->Driver.NewFramebuffer()
211 static struct gl_framebuffer
*
212 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
214 /* XXX not sure we need to subclass gl_framebuffer for pipe */
215 return _mesa_new_framebuffer(ctx
, name
);
220 * Called via ctx->Driver.NewRenderbuffer()
222 static struct gl_renderbuffer
*
223 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
225 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
227 _mesa_init_renderbuffer(&strb
->Base
, name
);
228 strb
->Base
.Delete
= st_renderbuffer_delete
;
229 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
230 strb
->Base
.GetPointer
= null_get_pointer
;
231 strb
->format
= PIPE_FORMAT_NONE
;
239 * Allocate a renderbuffer for a an on-screen window (not a user-created
240 * renderbuffer). The window system code determines the format.
242 struct gl_renderbuffer
*
243 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
245 struct st_renderbuffer
*strb
;
247 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
249 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
253 _mesa_init_renderbuffer(&strb
->Base
, 0);
254 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
255 strb
->Base
.NumSamples
= samples
;
256 strb
->format
= format
;
257 init_renderbuffer_bits(strb
, format
);
261 case PIPE_FORMAT_A8R8G8B8_UNORM
:
262 case PIPE_FORMAT_B8G8R8A8_UNORM
:
263 case PIPE_FORMAT_X8R8G8B8_UNORM
:
264 case PIPE_FORMAT_B8G8R8X8_UNORM
:
265 case PIPE_FORMAT_A1R5G5B5_UNORM
:
266 case PIPE_FORMAT_A4R4G4B4_UNORM
:
267 case PIPE_FORMAT_R5G6B5_UNORM
:
268 strb
->Base
.InternalFormat
= GL_RGBA
;
270 case PIPE_FORMAT_Z16_UNORM
:
271 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
273 case PIPE_FORMAT_Z32_UNORM
:
274 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
276 case PIPE_FORMAT_S8Z24_UNORM
:
277 case PIPE_FORMAT_Z24S8_UNORM
:
278 case PIPE_FORMAT_X8Z24_UNORM
:
279 case PIPE_FORMAT_Z24X8_UNORM
:
280 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
282 case PIPE_FORMAT_S8_UNORM
:
283 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
285 case PIPE_FORMAT_R16G16B16A16_SNORM
:
286 strb
->Base
.InternalFormat
= GL_RGBA16
;
290 "Unexpected format in st_new_renderbuffer_fb");
295 /* st-specific methods */
296 strb
->Base
.Delete
= st_renderbuffer_delete
;
297 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
298 strb
->Base
.GetPointer
= null_get_pointer
;
300 /* surface is allocated in st_renderbuffer_alloc_storage() */
301 strb
->surface
= NULL
;
310 * Called via ctx->Driver.BindFramebufferEXT().
313 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
314 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
320 * Called by ctx->Driver.FramebufferRenderbuffer
323 st_framebuffer_renderbuffer(GLcontext
*ctx
,
324 struct gl_framebuffer
*fb
,
326 struct gl_renderbuffer
*rb
)
328 /* XXX no need for derivation? */
329 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
334 * Called by ctx->Driver.RenderTexture
337 st_render_texture(GLcontext
*ctx
,
338 struct gl_framebuffer
*fb
,
339 struct gl_renderbuffer_attachment
*att
)
341 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
342 struct st_renderbuffer
*strb
;
343 struct gl_renderbuffer
*rb
;
344 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
345 struct st_texture_object
*stObj
;
346 const struct gl_texture_image
*texImage
=
347 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
352 /* create new renderbuffer which wraps the texture image */
353 rb
= st_new_renderbuffer(ctx
, 0);
355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
359 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
360 assert(rb
->RefCount
== 1);
361 rb
->AllocStorage
= NULL
; /* should not get called */
362 strb
= st_renderbuffer(rb
);
364 assert(strb
->Base
.RefCount
> 0);
366 /* get the texture for the texture object */
367 stObj
= st_texture_object(att
->Texture
);
369 /* point renderbuffer at texobject */
371 strb
->rtt_level
= att
->TextureLevel
;
372 strb
->rtt_face
= att
->CubeMapFace
;
373 strb
->rtt_slice
= att
->Zoffset
;
375 rb
->Width
= texImage
->Width2
;
376 rb
->Height
= texImage
->Height2
;
377 rb
->_BaseFormat
= texImage
->_BaseFormat
;
378 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
380 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
382 pipe_texture_reference( &strb
->texture
, pt
);
384 pipe_surface_reference(&strb
->surface
, NULL
);
386 /* new surface for rendering into the texture */
387 strb
->surface
= screen
->get_tex_surface(screen
,
392 PIPE_BUFFER_USAGE_GPU_READ
|
393 PIPE_BUFFER_USAGE_GPU_WRITE
);
395 init_renderbuffer_bits(strb
, pt
->format
);
398 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
399 att->Texture, pt, strb->surface, rb->Width, rb->Height);
402 /* Invalidate buffer state so that the pipe's framebuffer state
404 * That's where the new renderbuffer (which we just created) gets
405 * passed to the pipe as a (color/depth) render target.
407 st_invalidate_state(ctx
, _NEW_BUFFERS
);
412 * Called via ctx->Driver.FinishRenderTexture.
415 st_finish_render_texture(GLcontext
*ctx
,
416 struct gl_renderbuffer_attachment
*att
)
418 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
423 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
426 pipe_surface_reference( &strb
->surface
, NULL
);
431 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
434 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
436 /* restore previous framebuffer state */
437 st_invalidate_state(ctx
, _NEW_BUFFERS
);
442 * Validate a renderbuffer attachment for a particular usage.
446 st_validate_attachment(struct pipe_screen
*screen
,
447 const struct gl_renderbuffer_attachment
*att
,
450 const struct st_texture_object
*stObj
=
451 st_texture_object(att
->Texture
);
454 * Only validate texture attachments for now, since
455 * st_renderbuffer_alloc_storage makes sure that
456 * the format is supported.
459 if (att
->Type
!= GL_TEXTURE
)
465 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
471 * Check that the framebuffer configuration is valid in terms of what
472 * the driver can support.
474 * For Gallium we only supports combined Z+stencil, not separate buffers.
477 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
479 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
480 const struct gl_renderbuffer
*depthRb
=
481 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
482 const struct gl_renderbuffer
*stencilRb
=
483 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
486 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
487 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
491 if (!st_validate_attachment(screen
,
492 &fb
->Attachment
[BUFFER_DEPTH
],
493 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
494 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
497 if (!st_validate_attachment(screen
,
498 &fb
->Attachment
[BUFFER_STENCIL
],
499 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
500 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
503 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
504 if (!st_validate_attachment(screen
,
505 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
506 PIPE_TEXTURE_USAGE_RENDER_TARGET
)) {
507 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
515 * Copy back color buffer to front color buffer.
518 copy_back_to_front(struct st_context
*st
,
519 struct gl_framebuffer
*fb
,
520 gl_buffer_index frontIndex
,
521 gl_buffer_index backIndex
)
524 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
525 struct pipe_surface
*surf_front
, *surf_back
;
527 (void) st_get_framebuffer_surface(stfb
, frontIndex
, &surf_front
);
528 (void) st_get_framebuffer_surface(stfb
, backIndex
, &surf_back
);
530 if (surf_front
&& surf_back
) {
531 if (st
->pipe
->surface_copy
) {
532 st
->pipe
->surface_copy(st
->pipe
,
533 surf_front
, 0, 0, /* dest */
534 surf_back
, 0, 0, /* src */
535 fb
->Width
, fb
->Height
);
537 util_surface_copy(st
->pipe
, FALSE
,
540 fb
->Width
, fb
->Height
);
547 * Check if we're drawing into, or read from, a front color buffer. If the
548 * front buffer is missing, create it now.
550 * The back color buffer must exist since we'll use its format/samples info
551 * for creating the front buffer.
553 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
554 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
557 check_create_front_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
558 gl_buffer_index frontIndex
,
559 gl_buffer_index backIndex
)
561 if (fb
->Attachment
[frontIndex
].Renderbuffer
== NULL
) {
562 GLboolean create
= GL_FALSE
;
564 /* check if drawing to or reading from front buffer */
565 if (fb
->_ColorReadBufferIndex
== frontIndex
) {
570 for (b
= 0; b
< fb
->_NumColorDrawBuffers
; b
++) {
571 if (fb
->_ColorDrawBufferIndexes
[b
] == frontIndex
) {
579 struct st_renderbuffer
*back
;
580 struct gl_renderbuffer
*front
;
581 enum pipe_format colorFormat
;
585 _mesa_debug(ctx
, "Allocate new front buffer\n");
587 /* get back renderbuffer info */
588 back
= st_renderbuffer(fb
->Attachment
[backIndex
].Renderbuffer
);
589 colorFormat
= back
->format
;
590 samples
= back
->Base
.NumSamples
;
592 /* create front renderbuffer */
593 front
= st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
594 _mesa_add_renderbuffer(fb
, frontIndex
, front
);
596 /* alloc texture/surface for new front buffer */
597 front
->AllocStorage(ctx
, front
, front
->InternalFormat
,
598 fb
->Width
, fb
->Height
);
600 /* initialize the front color buffer contents by copying
603 copy_back_to_front(ctx
->st
, fb
, frontIndex
, backIndex
);
610 * If front left/right color buffers are missing, create them now.
613 check_create_front_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
615 /* check if we need to create the front left buffer now */
616 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_LEFT
, BUFFER_BACK_LEFT
);
618 if (fb
->Visual
.stereoMode
) {
619 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_RIGHT
, BUFFER_BACK_RIGHT
);
622 st_invalidate_state(ctx
, _NEW_BUFFERS
);
627 * Called via glDrawBuffer.
630 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
634 check_create_front_buffers(ctx
, ctx
->DrawBuffer
);
639 * Called via glReadBuffer.
642 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
645 check_create_front_buffers(ctx
, ctx
->ReadBuffer
);
649 void st_init_fbo_functions(struct dd_function_table
*functions
)
651 functions
->NewFramebuffer
= st_new_framebuffer
;
652 functions
->NewRenderbuffer
= st_new_renderbuffer
;
653 functions
->BindFramebuffer
= st_bind_framebuffer
;
654 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
655 functions
->RenderTexture
= st_render_texture
;
656 functions
->FinishRenderTexture
= st_finish_render_texture
;
657 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
658 /* no longer needed by core Mesa, drivers handle resizes...
659 functions->ResizeBuffers = st_resize_buffers;
662 functions
->DrawBuffers
= st_DrawBuffers
;
663 functions
->ReadBuffer
= st_ReadBuffer
;