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_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_resource template;
101 /* Free the old surface and texture
103 pipe_surface_reference( &strb->surface, NULL );
104 pipe_resource_reference( &strb->texture, NULL );
105 pipe_sampler_view_reference(&strb->sampler_view, 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.bind = PIPE_BIND_DEPTH_STENCIL;
121 template.bind = (PIPE_BIND_DISPLAY_TARGET |
122 PIPE_BIND_RENDER_TARGET);
125 strb->texture = screen->resource_create(screen, &template);
130 strb->surface = screen->get_tex_surface(screen,
135 assert(strb->surface->texture);
136 assert(strb->surface->format);
137 assert(strb->surface->width == width);
138 assert(strb->surface->height == height);
141 return strb->surface != NULL;
147 * gl_renderbuffer::Delete()
150 st_renderbuffer_delete(struct gl_renderbuffer *rb)
152 struct st_renderbuffer *strb = st_renderbuffer(rb);
154 pipe_surface_reference(&strb->surface, NULL);
155 pipe_resource_reference(&strb->texture, NULL);
156 pipe_sampler_view_reference(&strb->sampler_view, NULL);
163 * gl_renderbuffer::GetPointer()
166 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
169 /* By returning NULL we force all software rendering to go through
173 assert(0); /* Should never get called with softpipe */
180 * Called via ctx->Driver.NewFramebuffer()
182 static struct gl_framebuffer *
183 st_new_framebuffer(GLcontext *ctx, GLuint name)
185 /* XXX not sure we need to subclass gl_framebuffer for pipe */
186 return _mesa_new_framebuffer(ctx, name);
191 * Called via ctx->Driver.NewRenderbuffer()
193 static struct gl_renderbuffer *
194 st_new_renderbuffer(GLcontext *ctx, GLuint name)
196 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
198 _mesa_init_renderbuffer(&strb->Base, name);
199 strb->Base.Delete = st_renderbuffer_delete;
200 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
201 strb->Base.GetPointer = null_get_pointer;
202 strb->format = PIPE_FORMAT_NONE;
210 * Allocate a renderbuffer for a an on-screen window (not a user-created
211 * renderbuffer). The window system code determines the format.
213 struct gl_renderbuffer *
214 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
216 struct st_renderbuffer *strb;
218 strb = ST_CALLOC_STRUCT(st_renderbuffer);
220 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
224 _mesa_init_renderbuffer(&strb->Base, 0);
225 strb->Base.ClassID = 0x4242; /* just a unique value */
226 strb->Base.NumSamples = samples;
227 strb->Base.Format = st_pipe_format_to_mesa_format(format);
228 strb->Base.DataType = st_format_datatype(format);
229 strb->format = format;
233 case PIPE_FORMAT_B8G8R8A8_UNORM:
234 case PIPE_FORMAT_A8R8G8B8_UNORM:
235 case PIPE_FORMAT_B8G8R8X8_UNORM:
236 case PIPE_FORMAT_X8R8G8B8_UNORM:
237 case PIPE_FORMAT_B5G5R5A1_UNORM:
238 case PIPE_FORMAT_B4G4R4A4_UNORM:
239 case PIPE_FORMAT_B5G6R5_UNORM:
240 strb->Base.InternalFormat = GL_RGBA;
242 case PIPE_FORMAT_Z16_UNORM:
243 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
245 case PIPE_FORMAT_Z32_UNORM:
246 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
248 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
249 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
250 case PIPE_FORMAT_Z24X8_UNORM:
251 case PIPE_FORMAT_X8Z24_UNORM:
252 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
254 case PIPE_FORMAT_S8_USCALED:
255 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
257 case PIPE_FORMAT_R16G16B16A16_SNORM:
258 strb->Base.InternalFormat = GL_RGBA16;
262 "Unexpected format in st_new_renderbuffer_fb");
267 /* st-specific methods */
268 strb->Base.Delete = st_renderbuffer_delete;
269 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
270 strb->Base.GetPointer = null_get_pointer;
272 /* surface is allocated in st_renderbuffer_alloc_storage() */
273 strb->surface = NULL;
282 * Called via ctx->Driver.BindFramebufferEXT().
285 st_bind_framebuffer(GLcontext *ctx, GLenum target,
286 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
292 * Called by ctx->Driver.FramebufferRenderbuffer
295 st_framebuffer_renderbuffer(GLcontext *ctx,
296 struct gl_framebuffer *fb,
298 struct gl_renderbuffer *rb)
300 /* XXX no need for derivation? */
301 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
306 * Called by ctx->Driver.RenderTexture
309 st_render_texture(GLcontext *ctx,
310 struct gl_framebuffer *fb,
311 struct gl_renderbuffer_attachment *att)
313 struct pipe_screen *screen = ctx->st->pipe->screen;
314 struct st_renderbuffer *strb;
315 struct gl_renderbuffer *rb;
316 struct pipe_resource *pt = st_get_texobj_texture(att->Texture);
317 struct st_texture_object *stObj;
318 const struct gl_texture_image *texImage;
321 /* When would this fail? Perhaps assert? */
325 /* The first gallium texture level = Mesa BaseLevel */
326 pt_level = MAX2(0, (GLint) att->TextureLevel - att->Texture->BaseLevel);
327 texImage = att->Texture->Image[att->CubeMapFace][pt_level];
329 /* create new renderbuffer which wraps the texture image */
330 rb = st_new_renderbuffer(ctx, 0);
332 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
336 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
337 assert(rb->RefCount == 1);
338 rb->AllocStorage = NULL; /* should not get called */
339 strb = st_renderbuffer(rb);
341 assert(strb->Base.RefCount > 0);
343 /* get the texture for the texture object */
344 stObj = st_texture_object(att->Texture);
346 /* point renderbuffer at texobject */
348 strb->rtt_level = pt_level;
349 strb->rtt_face = att->CubeMapFace;
350 strb->rtt_slice = att->Zoffset;
352 rb->Width = texImage->Width2;
353 rb->Height = texImage->Height2;
354 rb->_BaseFormat = texImage->_BaseFormat;
355 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
357 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
359 pipe_resource_reference( &strb->texture, pt );
361 pipe_surface_reference(&strb->surface, NULL);
363 pipe_sampler_view_reference(&strb->sampler_view, st_get_stobj_sampler_view(stObj));
365 assert(strb->rtt_level <= strb->texture->last_level);
367 /* new surface for rendering into the texture */
368 strb->surface = screen->get_tex_surface(screen,
373 PIPE_BIND_RENDER_TARGET);
375 strb->format = pt->format;
377 strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
378 strb->Base.DataType = st_format_datatype(pt->format);
381 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
382 att->Texture, pt, strb->surface, rb->Width, rb->Height);
385 /* Invalidate buffer state so that the pipe's framebuffer state
387 * That's where the new renderbuffer (which we just created) gets
388 * passed to the pipe as a (color/depth) render target.
390 st_invalidate_state(ctx, _NEW_BUFFERS);
395 * Called via ctx->Driver.FinishRenderTexture.
398 st_finish_render_texture(GLcontext *ctx,
399 struct gl_renderbuffer_attachment *att)
401 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
406 st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
411 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
414 /* restore previous framebuffer state */
415 st_invalidate_state(ctx, _NEW_BUFFERS);
420 * Validate a renderbuffer attachment for a particular usage.
424 st_validate_attachment(struct pipe_screen *screen,
425 const struct gl_renderbuffer_attachment *att,
428 const struct st_texture_object *stObj =
429 st_texture_object(att->Texture);
432 * Only validate texture attachments for now, since
433 * st_renderbuffer_alloc_storage makes sure that
434 * the format is supported.
437 if (att->Type != GL_TEXTURE)
443 return screen->is_format_supported(screen, stObj->pt->format,
449 * Check that the framebuffer configuration is valid in terms of what
450 * the driver can support.
452 * For Gallium we only supports combined Z+stencil, not separate buffers.
455 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
457 struct pipe_screen *screen = ctx->st->pipe->screen;
458 const struct gl_renderbuffer *depthRb =
459 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
460 const struct gl_renderbuffer *stencilRb =
461 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
464 if (stencilRb && depthRb && stencilRb != depthRb) {
465 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
469 if (!st_validate_attachment(screen,
470 &fb->Attachment[BUFFER_DEPTH],
471 PIPE_BIND_DEPTH_STENCIL)) {
472 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
475 if (!st_validate_attachment(screen,
476 &fb->Attachment[BUFFER_STENCIL],
477 PIPE_BIND_DEPTH_STENCIL)) {
478 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
481 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
482 if (!st_validate_attachment(screen,
483 &fb->Attachment[BUFFER_COLOR0 + i],
484 PIPE_BIND_RENDER_TARGET)) {
485 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
493 * Copy back color buffer to front color buffer.
496 copy_back_to_front(struct st_context *st,
497 struct gl_framebuffer *fb,
498 gl_buffer_index frontIndex,
499 gl_buffer_index backIndex)
502 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
503 struct pipe_surface *surf_front, *surf_back;
505 (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
506 (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
508 if (surf_front && surf_back) {
509 st->pipe->surface_copy(st->pipe,
510 surf_front, 0, 0, /* dest */
511 surf_back, 0, 0, /* src */
512 fb->Width, fb->Height);
518 * Check if we're drawing into, or read from, a front color buffer. If the
519 * front buffer is missing, create it now.
521 * The back color buffer must exist since we'll use its format/samples info
522 * for creating the front buffer.
524 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
525 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
528 check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
529 gl_buffer_index frontIndex,
530 gl_buffer_index backIndex)
532 if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
533 GLboolean create = GL_FALSE;
535 /* check if drawing to or reading from front buffer */
536 if (fb->_ColorReadBufferIndex == frontIndex) {
541 for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
542 if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
550 struct st_renderbuffer *back;
551 struct gl_renderbuffer *front;
552 enum pipe_format colorFormat;
556 _mesa_debug(ctx, "Allocate new front buffer\n");
558 /* get back renderbuffer info */
559 back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
560 colorFormat = back->format;
561 samples = back->Base.NumSamples;
563 /* create front renderbuffer */
564 front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
565 _mesa_add_renderbuffer(fb, frontIndex, front);
567 /* alloc texture/surface for new front buffer */
568 front->AllocStorage(ctx, front, front->InternalFormat,
569 fb->Width, fb->Height);
571 /* initialize the front color buffer contents by copying
574 copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
581 * If front left/right color buffers are missing, create them now.
584 check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
586 /* check if we need to create the front left buffer now */
587 check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
589 if (fb->Visual.stereoMode) {
590 check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
593 st_invalidate_state(ctx, _NEW_BUFFERS);
598 * Called via glDrawBuffer.
601 st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
603 GLframebuffer *fb = ctx->DrawBuffer;
609 /* add the renderbuffers on demand */
610 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
611 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
612 st_manager_add_color_renderbuffer(ctx->st, fb, idx);
615 check_create_front_buffers(ctx, ctx->DrawBuffer);
620 * Called via glReadBuffer.
623 st_ReadBuffer(GLcontext *ctx, GLenum buffer)
625 GLframebuffer *fb = ctx->ReadBuffer;
629 /* add the renderbuffer on demand */
630 st_manager_add_color_renderbuffer(ctx->st, fb, fb->_ColorReadBufferIndex);
631 check_create_front_buffers(ctx, fb);
635 void st_init_fbo_functions(struct dd_function_table *functions)
637 functions->NewFramebuffer = st_new_framebuffer;
638 functions->NewRenderbuffer = st_new_renderbuffer;
639 functions->BindFramebuffer = st_bind_framebuffer;
640 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
641 functions->RenderTexture = st_render_texture;
642 functions->FinishRenderTexture = st_finish_render_texture;
643 functions->ValidateFramebuffer = st_validate_framebuffer;
644 /* no longer needed by core Mesa, drivers handle resizes...
645 functions->ResizeBuffers = st_resize_buffers;
648 functions->DrawBuffers = st_DrawBuffers;
649 functions->ReadBuffer = st_ReadBuffer;
652 struct pipe_sampler_view *
653 st_renderbuffer_get_sampler_view(struct st_renderbuffer *rb,
654 struct pipe_context *pipe)
656 if (!rb->sampler_view) {
657 rb->sampler_view = st_sampler_view_from_texture(pipe, rb->texture);
660 return rb->sampler_view;