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_texture template;
100 unsigned surface_usage;
102 /* Free the old surface and texture
104 pipe_surface_reference( &strb->surface, NULL );
105 pipe_texture_reference( &strb->texture, NULL );
106 pipe_sampler_view_reference(&strb->sampler_view, 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);
166 pipe_sampler_view_reference(&strb->sampler_view, NULL);
173 * gl_renderbuffer::GetPointer()
176 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
179 /* By returning NULL we force all software rendering to go through
183 assert(0); /* Should never get called with softpipe */
190 * Called via ctx->Driver.NewFramebuffer()
192 static struct gl_framebuffer *
193 st_new_framebuffer(GLcontext *ctx, GLuint name)
195 /* XXX not sure we need to subclass gl_framebuffer for pipe */
196 return _mesa_new_framebuffer(ctx, name);
201 * Called via ctx->Driver.NewRenderbuffer()
203 static struct gl_renderbuffer *
204 st_new_renderbuffer(GLcontext *ctx, GLuint name)
206 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
208 _mesa_init_renderbuffer(&strb->Base, name);
209 strb->Base.Delete = st_renderbuffer_delete;
210 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
211 strb->Base.GetPointer = null_get_pointer;
212 strb->format = PIPE_FORMAT_NONE;
220 * Allocate a renderbuffer for a an on-screen window (not a user-created
221 * renderbuffer). The window system code determines the format.
223 struct gl_renderbuffer *
224 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
226 struct st_renderbuffer *strb;
228 strb = ST_CALLOC_STRUCT(st_renderbuffer);
230 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
234 _mesa_init_renderbuffer(&strb->Base, 0);
235 strb->Base.ClassID = 0x4242; /* just a unique value */
236 strb->Base.NumSamples = samples;
237 strb->Base.Format = st_pipe_format_to_mesa_format(format);
238 strb->Base.DataType = st_format_datatype(format);
239 strb->format = format;
243 case PIPE_FORMAT_B8G8R8A8_UNORM:
244 case PIPE_FORMAT_A8R8G8B8_UNORM:
245 case PIPE_FORMAT_B8G8R8X8_UNORM:
246 case PIPE_FORMAT_X8R8G8B8_UNORM:
247 case PIPE_FORMAT_B5G5R5A1_UNORM:
248 case PIPE_FORMAT_B4G4R4A4_UNORM:
249 case PIPE_FORMAT_B5G6R5_UNORM:
250 strb->Base.InternalFormat = GL_RGBA;
252 case PIPE_FORMAT_Z16_UNORM:
253 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
255 case PIPE_FORMAT_Z32_UNORM:
256 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
258 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
259 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
260 case PIPE_FORMAT_Z24X8_UNORM:
261 case PIPE_FORMAT_X8Z24_UNORM:
262 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
264 case PIPE_FORMAT_S8_USCALED:
265 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
267 case PIPE_FORMAT_R16G16B16A16_SNORM:
268 strb->Base.InternalFormat = GL_RGBA16;
272 "Unexpected format in st_new_renderbuffer_fb");
277 /* st-specific methods */
278 strb->Base.Delete = st_renderbuffer_delete;
279 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
280 strb->Base.GetPointer = null_get_pointer;
282 /* surface is allocated in st_renderbuffer_alloc_storage() */
283 strb->surface = NULL;
292 * Called via ctx->Driver.BindFramebufferEXT().
295 st_bind_framebuffer(GLcontext *ctx, GLenum target,
296 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
302 * Called by ctx->Driver.FramebufferRenderbuffer
305 st_framebuffer_renderbuffer(GLcontext *ctx,
306 struct gl_framebuffer *fb,
308 struct gl_renderbuffer *rb)
310 /* XXX no need for derivation? */
311 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
316 * Called by ctx->Driver.RenderTexture
319 st_render_texture(GLcontext *ctx,
320 struct gl_framebuffer *fb,
321 struct gl_renderbuffer_attachment *att)
323 struct pipe_screen *screen = ctx->st->pipe->screen;
324 struct st_renderbuffer *strb;
325 struct gl_renderbuffer *rb;
326 struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
327 struct st_texture_object *stObj;
328 const struct gl_texture_image *texImage;
331 /* When would this fail? Perhaps assert? */
335 /* The first gallium texture level = Mesa BaseLevel */
336 pt_level = MAX2(0, (GLint) att->TextureLevel - att->Texture->BaseLevel);
337 texImage = att->Texture->Image[att->CubeMapFace][pt_level];
339 /* create new renderbuffer which wraps the texture image */
340 rb = st_new_renderbuffer(ctx, 0);
342 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
346 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
347 assert(rb->RefCount == 1);
348 rb->AllocStorage = NULL; /* should not get called */
349 strb = st_renderbuffer(rb);
351 assert(strb->Base.RefCount > 0);
353 /* get the texture for the texture object */
354 stObj = st_texture_object(att->Texture);
356 /* point renderbuffer at texobject */
358 strb->rtt_level = pt_level;
359 strb->rtt_face = att->CubeMapFace;
360 strb->rtt_slice = att->Zoffset;
362 rb->Width = texImage->Width2;
363 rb->Height = texImage->Height2;
364 rb->_BaseFormat = texImage->_BaseFormat;
365 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
367 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
369 pipe_texture_reference( &strb->texture, pt );
371 pipe_surface_reference(&strb->surface, NULL);
373 pipe_sampler_view_reference(&strb->sampler_view, st_get_stobj_sampler_view(stObj));
375 assert(strb->rtt_level <= strb->texture->last_level);
377 /* new surface for rendering into the texture */
378 strb->surface = screen->get_tex_surface(screen,
383 PIPE_BUFFER_USAGE_GPU_READ |
384 PIPE_BUFFER_USAGE_GPU_WRITE);
386 strb->format = pt->format;
388 strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
389 strb->Base.DataType = st_format_datatype(pt->format);
392 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
393 att->Texture, pt, strb->surface, rb->Width, rb->Height);
396 /* Invalidate buffer state so that the pipe's framebuffer state
398 * That's where the new renderbuffer (which we just created) gets
399 * passed to the pipe as a (color/depth) render target.
401 st_invalidate_state(ctx, _NEW_BUFFERS);
406 * Called via ctx->Driver.FinishRenderTexture.
409 st_finish_render_texture(GLcontext *ctx,
410 struct gl_renderbuffer_attachment *att)
412 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
417 st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
422 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
425 /* restore previous framebuffer state */
426 st_invalidate_state(ctx, _NEW_BUFFERS);
431 * Validate a renderbuffer attachment for a particular usage.
435 st_validate_attachment(struct pipe_screen *screen,
436 const struct gl_renderbuffer_attachment *att,
439 const struct st_texture_object *stObj =
440 st_texture_object(att->Texture);
443 * Only validate texture attachments for now, since
444 * st_renderbuffer_alloc_storage makes sure that
445 * the format is supported.
448 if (att->Type != GL_TEXTURE)
454 return screen->is_format_supported(screen, stObj->pt->format,
460 * Check that the framebuffer configuration is valid in terms of what
461 * the driver can support.
463 * For Gallium we only supports combined Z+stencil, not separate buffers.
466 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
468 struct pipe_screen *screen = ctx->st->pipe->screen;
469 const struct gl_renderbuffer *depthRb =
470 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
471 const struct gl_renderbuffer *stencilRb =
472 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
475 if (stencilRb && depthRb && stencilRb != depthRb) {
476 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
480 if (!st_validate_attachment(screen,
481 &fb->Attachment[BUFFER_DEPTH],
482 PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
483 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
486 if (!st_validate_attachment(screen,
487 &fb->Attachment[BUFFER_STENCIL],
488 PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
489 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
492 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
493 if (!st_validate_attachment(screen,
494 &fb->Attachment[BUFFER_COLOR0 + i],
495 PIPE_TEXTURE_USAGE_RENDER_TARGET)) {
496 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
504 * Copy back color buffer to front color buffer.
507 copy_back_to_front(struct st_context *st,
508 struct gl_framebuffer *fb,
509 gl_buffer_index frontIndex,
510 gl_buffer_index backIndex)
513 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
514 struct pipe_surface *surf_front, *surf_back;
516 (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
517 (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
519 if (surf_front && surf_back) {
520 st->pipe->surface_copy(st->pipe,
521 surf_front, 0, 0, /* dest */
522 surf_back, 0, 0, /* src */
523 fb->Width, fb->Height);
529 * Check if we're drawing into, or read from, a front color buffer. If the
530 * front buffer is missing, create it now.
532 * The back color buffer must exist since we'll use its format/samples info
533 * for creating the front buffer.
535 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
536 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
539 check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
540 gl_buffer_index frontIndex,
541 gl_buffer_index backIndex)
543 if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
544 GLboolean create = GL_FALSE;
546 /* check if drawing to or reading from front buffer */
547 if (fb->_ColorReadBufferIndex == frontIndex) {
552 for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
553 if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
561 struct st_renderbuffer *back;
562 struct gl_renderbuffer *front;
563 enum pipe_format colorFormat;
567 _mesa_debug(ctx, "Allocate new front buffer\n");
569 /* get back renderbuffer info */
570 back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
571 colorFormat = back->format;
572 samples = back->Base.NumSamples;
574 /* create front renderbuffer */
575 front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
576 _mesa_add_renderbuffer(fb, frontIndex, front);
578 /* alloc texture/surface for new front buffer */
579 front->AllocStorage(ctx, front, front->InternalFormat,
580 fb->Width, fb->Height);
582 /* initialize the front color buffer contents by copying
585 copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
592 * If front left/right color buffers are missing, create them now.
595 check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
597 /* check if we need to create the front left buffer now */
598 check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
600 if (fb->Visual.stereoMode) {
601 check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
604 st_invalidate_state(ctx, _NEW_BUFFERS);
609 * Called via glDrawBuffer.
612 st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
614 GLframebuffer *fb = ctx->DrawBuffer;
620 /* add the renderbuffers on demand */
621 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
622 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
623 st_manager_add_color_renderbuffer(ctx->st, fb, idx);
626 check_create_front_buffers(ctx, ctx->DrawBuffer);
631 * Called via glReadBuffer.
634 st_ReadBuffer(GLcontext *ctx, GLenum buffer)
636 GLframebuffer *fb = ctx->ReadBuffer;
640 /* add the renderbuffer on demand */
641 st_manager_add_color_renderbuffer(ctx->st, fb, fb->_ColorReadBufferIndex);
642 check_create_front_buffers(ctx, fb);
646 void st_init_fbo_functions(struct dd_function_table *functions)
648 functions->NewFramebuffer = st_new_framebuffer;
649 functions->NewRenderbuffer = st_new_renderbuffer;
650 functions->BindFramebuffer = st_bind_framebuffer;
651 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
652 functions->RenderTexture = st_render_texture;
653 functions->FinishRenderTexture = st_finish_render_texture;
654 functions->ValidateFramebuffer = st_validate_framebuffer;
655 /* no longer needed by core Mesa, drivers handle resizes...
656 functions->ResizeBuffers = st_resize_buffers;
659 functions->DrawBuffers = st_DrawBuffers;
660 functions->ReadBuffer = st_ReadBuffer;
663 struct pipe_sampler_view *
664 st_renderbuffer_get_sampler_view(struct st_renderbuffer *rb,
665 struct pipe_context *pipe)
667 if (!rb->sampler_view) {
668 rb->sampler_view = st_sampler_view_from_texture(pipe, rb->texture);
671 return rb->sampler_view;