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
);
107 pipe_sampler_view_reference(&strb
->sampler_view
, NULL
);
109 /* Setup new texture template.
111 memset(&template, 0, sizeof(template));
112 template.target
= PIPE_TEXTURE_2D
;
113 template.format
= format
;
114 template.width0
= width
;
115 template.height0
= height
;
117 template.last_level
= 0;
118 template.nr_samples
= rb
->NumSamples
;
119 if (util_format_is_depth_or_stencil(format
)) {
120 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
123 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
124 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
127 /* Probably need dedicated flags for surface usage too:
129 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
130 PIPE_BUFFER_USAGE_GPU_WRITE
);
132 PIPE_BUFFER_USAGE_CPU_READ
|
133 PIPE_BUFFER_USAGE_CPU_WRITE
);
136 strb
->texture
= screen
->texture_create(screen
, &template);
141 strb
->surface
= screen
->get_tex_surface(screen
,
146 assert(strb
->surface
->texture
);
147 assert(strb
->surface
->format
);
148 assert(strb
->surface
->width
== width
);
149 assert(strb
->surface
->height
== height
);
152 return strb
->surface
!= NULL
;
158 * gl_renderbuffer::Delete()
161 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
163 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
165 pipe_surface_reference(&strb
->surface
, NULL
);
166 pipe_texture_reference(&strb
->texture
, NULL
);
167 pipe_sampler_view_reference(&strb
->sampler_view
, NULL
);
174 * gl_renderbuffer::GetPointer()
177 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
180 /* By returning NULL we force all software rendering to go through
184 assert(0); /* Should never get called with softpipe */
191 * Called via ctx->Driver.NewFramebuffer()
193 static struct gl_framebuffer
*
194 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
196 /* XXX not sure we need to subclass gl_framebuffer for pipe */
197 return _mesa_new_framebuffer(ctx
, name
);
202 * Called via ctx->Driver.NewRenderbuffer()
204 static struct gl_renderbuffer
*
205 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
207 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
209 _mesa_init_renderbuffer(&strb
->Base
, name
);
210 strb
->Base
.Delete
= st_renderbuffer_delete
;
211 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
212 strb
->Base
.GetPointer
= null_get_pointer
;
213 strb
->format
= PIPE_FORMAT_NONE
;
221 * Allocate a renderbuffer for a an on-screen window (not a user-created
222 * renderbuffer). The window system code determines the format.
224 struct gl_renderbuffer
*
225 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
227 struct st_renderbuffer
*strb
;
229 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
231 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
235 _mesa_init_renderbuffer(&strb
->Base
, 0);
236 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
237 strb
->Base
.NumSamples
= samples
;
238 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
239 strb
->Base
.DataType
= st_format_datatype(format
);
240 strb
->format
= format
;
244 case PIPE_FORMAT_B8G8R8A8_UNORM
:
245 case PIPE_FORMAT_A8R8G8B8_UNORM
:
246 case PIPE_FORMAT_B8G8R8X8_UNORM
:
247 case PIPE_FORMAT_X8R8G8B8_UNORM
:
248 case PIPE_FORMAT_B5G5R5A1_UNORM
:
249 case PIPE_FORMAT_B4G4R4A4_UNORM
:
250 case PIPE_FORMAT_B5G6R5_UNORM
:
251 strb
->Base
.InternalFormat
= GL_RGBA
;
253 case PIPE_FORMAT_Z16_UNORM
:
254 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
256 case PIPE_FORMAT_Z32_UNORM
:
257 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
259 case PIPE_FORMAT_Z24S8_UNORM
:
260 case PIPE_FORMAT_S8Z24_UNORM
:
261 case PIPE_FORMAT_Z24X8_UNORM
:
262 case PIPE_FORMAT_X8Z24_UNORM
:
263 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
265 case PIPE_FORMAT_S8_UNORM
:
266 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
268 case PIPE_FORMAT_R16G16B16A16_SNORM
:
269 strb
->Base
.InternalFormat
= GL_RGBA16
;
273 "Unexpected format in st_new_renderbuffer_fb");
278 /* st-specific methods */
279 strb
->Base
.Delete
= st_renderbuffer_delete
;
280 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
281 strb
->Base
.GetPointer
= null_get_pointer
;
283 /* surface is allocated in st_renderbuffer_alloc_storage() */
284 strb
->surface
= NULL
;
293 * Called via ctx->Driver.BindFramebufferEXT().
296 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
297 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
303 * Called by ctx->Driver.FramebufferRenderbuffer
306 st_framebuffer_renderbuffer(GLcontext
*ctx
,
307 struct gl_framebuffer
*fb
,
309 struct gl_renderbuffer
*rb
)
311 /* XXX no need for derivation? */
312 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
317 * Called by ctx->Driver.RenderTexture
320 st_render_texture(GLcontext
*ctx
,
321 struct gl_framebuffer
*fb
,
322 struct gl_renderbuffer_attachment
*att
)
324 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
325 struct st_renderbuffer
*strb
;
326 struct gl_renderbuffer
*rb
;
327 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
328 struct st_texture_object
*stObj
;
329 const struct gl_texture_image
*texImage
;
332 /* When would this fail? Perhaps assert? */
336 /* The first gallium texture level = Mesa BaseLevel */
337 pt_level
= MAX2(0, (GLint
) att
->TextureLevel
- att
->Texture
->BaseLevel
);
338 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][pt_level
];
340 /* create new renderbuffer which wraps the texture image */
341 rb
= st_new_renderbuffer(ctx
, 0);
343 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
347 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
348 assert(rb
->RefCount
== 1);
349 rb
->AllocStorage
= NULL
; /* should not get called */
350 strb
= st_renderbuffer(rb
);
352 assert(strb
->Base
.RefCount
> 0);
354 /* get the texture for the texture object */
355 stObj
= st_texture_object(att
->Texture
);
357 /* point renderbuffer at texobject */
359 strb
->rtt_level
= pt_level
;
360 strb
->rtt_face
= att
->CubeMapFace
;
361 strb
->rtt_slice
= att
->Zoffset
;
363 rb
->Width
= texImage
->Width2
;
364 rb
->Height
= texImage
->Height2
;
365 rb
->_BaseFormat
= texImage
->_BaseFormat
;
366 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
368 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
370 pipe_texture_reference( &strb
->texture
, pt
);
372 pipe_surface_reference(&strb
->surface
, NULL
);
374 pipe_sampler_view_reference(&strb
->sampler_view
, st_get_stobj_sampler_view(stObj
));
376 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
378 /* new surface for rendering into the texture */
379 strb
->surface
= screen
->get_tex_surface(screen
,
384 PIPE_BUFFER_USAGE_GPU_READ
|
385 PIPE_BUFFER_USAGE_GPU_WRITE
);
387 strb
->format
= pt
->format
;
389 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
390 strb
->Base
.DataType
= st_format_datatype(pt
->format
);
393 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
394 att->Texture, pt, strb->surface, rb->Width, rb->Height);
397 /* Invalidate buffer state so that the pipe's framebuffer state
399 * That's where the new renderbuffer (which we just created) gets
400 * passed to the pipe as a (color/depth) render target.
402 st_invalidate_state(ctx
, _NEW_BUFFERS
);
407 * Called via ctx->Driver.FinishRenderTexture.
410 st_finish_render_texture(GLcontext
*ctx
,
411 struct gl_renderbuffer_attachment
*att
)
413 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
418 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
423 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
426 /* restore previous framebuffer state */
427 st_invalidate_state(ctx
, _NEW_BUFFERS
);
432 * Validate a renderbuffer attachment for a particular usage.
436 st_validate_attachment(struct pipe_screen
*screen
,
437 const struct gl_renderbuffer_attachment
*att
,
440 const struct st_texture_object
*stObj
=
441 st_texture_object(att
->Texture
);
444 * Only validate texture attachments for now, since
445 * st_renderbuffer_alloc_storage makes sure that
446 * the format is supported.
449 if (att
->Type
!= GL_TEXTURE
)
455 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
461 * Check that the framebuffer configuration is valid in terms of what
462 * the driver can support.
464 * For Gallium we only supports combined Z+stencil, not separate buffers.
467 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
469 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
470 const struct gl_renderbuffer
*depthRb
=
471 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
472 const struct gl_renderbuffer
*stencilRb
=
473 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
476 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
477 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
481 if (!st_validate_attachment(screen
,
482 &fb
->Attachment
[BUFFER_DEPTH
],
483 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
484 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
487 if (!st_validate_attachment(screen
,
488 &fb
->Attachment
[BUFFER_STENCIL
],
489 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)) {
490 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
493 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
494 if (!st_validate_attachment(screen
,
495 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
496 PIPE_TEXTURE_USAGE_RENDER_TARGET
)) {
497 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
505 * Copy back color buffer to front color buffer.
508 copy_back_to_front(struct st_context
*st
,
509 struct gl_framebuffer
*fb
,
510 gl_buffer_index frontIndex
,
511 gl_buffer_index backIndex
)
514 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
515 struct pipe_surface
*surf_front
, *surf_back
;
517 (void) st_get_framebuffer_surface(stfb
, frontIndex
, &surf_front
);
518 (void) st_get_framebuffer_surface(stfb
, backIndex
, &surf_back
);
520 if (surf_front
&& surf_back
) {
521 if (st
->pipe
->surface_copy
) {
522 st
->pipe
->surface_copy(st
->pipe
,
523 surf_front
, 0, 0, /* dest */
524 surf_back
, 0, 0, /* src */
525 fb
->Width
, fb
->Height
);
527 util_surface_copy(st
->pipe
, FALSE
,
530 fb
->Width
, fb
->Height
);
537 * Check if we're drawing into, or read from, a front color buffer. If the
538 * front buffer is missing, create it now.
540 * The back color buffer must exist since we'll use its format/samples info
541 * for creating the front buffer.
543 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
544 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
547 check_create_front_buffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
548 gl_buffer_index frontIndex
,
549 gl_buffer_index backIndex
)
551 if (fb
->Attachment
[frontIndex
].Renderbuffer
== NULL
) {
552 GLboolean create
= GL_FALSE
;
554 /* check if drawing to or reading from front buffer */
555 if (fb
->_ColorReadBufferIndex
== frontIndex
) {
560 for (b
= 0; b
< fb
->_NumColorDrawBuffers
; b
++) {
561 if (fb
->_ColorDrawBufferIndexes
[b
] == frontIndex
) {
569 struct st_renderbuffer
*back
;
570 struct gl_renderbuffer
*front
;
571 enum pipe_format colorFormat
;
575 _mesa_debug(ctx
, "Allocate new front buffer\n");
577 /* get back renderbuffer info */
578 back
= st_renderbuffer(fb
->Attachment
[backIndex
].Renderbuffer
);
579 colorFormat
= back
->format
;
580 samples
= back
->Base
.NumSamples
;
582 /* create front renderbuffer */
583 front
= st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
584 _mesa_add_renderbuffer(fb
, frontIndex
, front
);
586 /* alloc texture/surface for new front buffer */
587 front
->AllocStorage(ctx
, front
, front
->InternalFormat
,
588 fb
->Width
, fb
->Height
);
590 /* initialize the front color buffer contents by copying
593 copy_back_to_front(ctx
->st
, fb
, frontIndex
, backIndex
);
600 * If front left/right color buffers are missing, create them now.
603 check_create_front_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
605 /* check if we need to create the front left buffer now */
606 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_LEFT
, BUFFER_BACK_LEFT
);
608 if (fb
->Visual
.stereoMode
) {
609 check_create_front_buffer(ctx
, fb
, BUFFER_FRONT_RIGHT
, BUFFER_BACK_RIGHT
);
612 st_invalidate_state(ctx
, _NEW_BUFFERS
);
617 * Called via glDrawBuffer.
620 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
622 GLframebuffer
*fb
= ctx
->DrawBuffer
;
628 /* add the renderbuffers on demand */
629 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
630 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
631 st_manager_add_color_renderbuffer(ctx
->st
, fb
, idx
);
634 check_create_front_buffers(ctx
, ctx
->DrawBuffer
);
639 * Called via glReadBuffer.
642 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
644 GLframebuffer
*fb
= ctx
->ReadBuffer
;
648 /* add the renderbuffer on demand */
649 st_manager_add_color_renderbuffer(ctx
->st
, fb
, fb
->_ColorReadBufferIndex
);
650 check_create_front_buffers(ctx
, fb
);
654 void st_init_fbo_functions(struct dd_function_table
*functions
)
656 functions
->NewFramebuffer
= st_new_framebuffer
;
657 functions
->NewRenderbuffer
= st_new_renderbuffer
;
658 functions
->BindFramebuffer
= st_bind_framebuffer
;
659 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
660 functions
->RenderTexture
= st_render_texture
;
661 functions
->FinishRenderTexture
= st_finish_render_texture
;
662 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
663 /* no longer needed by core Mesa, drivers handle resizes...
664 functions->ResizeBuffers = st_resize_buffers;
667 functions
->DrawBuffers
= st_DrawBuffers
;
668 functions
->ReadBuffer
= st_ReadBuffer
;
671 struct pipe_sampler_view
*
672 st_renderbuffer_get_sampler_view(struct st_renderbuffer
*rb
,
673 struct pipe_context
*pipe
)
675 if (!rb
->sampler_view
) {
676 rb
->sampler_view
= st_sampler_view_from_texture(pipe
, rb
->texture
);
679 return rb
->sampler_view
;