Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Framebuffer/renderbuffer functions.
31 *
32 * \author Brian Paul
33 */
34
35
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"
41
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_format.h"
48 #include "st_public.h"
49 #include "st_texture.h"
50
51 #include "util/u_format.h"
52 #include "util/u_rect.h"
53
54
55 /**
56 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
57 */
58 static int
59 init_renderbuffer_bits(struct st_renderbuffer *strb,
60 enum pipe_format pipeFormat)
61 {
62 struct pipe_format_info info;
63
64 if (!st_get_format_info( pipeFormat, &info )) {
65 assert( 0 );
66 }
67
68 strb->Base.Format = info.mesa_format;
69 strb->Base.DataType = st_format_datatype(pipeFormat);
70
71 return info.size;
72 }
73
74 /**
75 * gl_renderbuffer::AllocStorage()
76 * This is called to allocate the original drawing surface, and
77 * during window resize.
78 */
79 static GLboolean
80 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
81 GLenum internalFormat,
82 GLuint width, GLuint height)
83 {
84 struct pipe_context *pipe = ctx->st->pipe;
85 struct st_renderbuffer *strb = st_renderbuffer(rb);
86 enum pipe_format format;
87
88 if (strb->format != PIPE_FORMAT_NONE)
89 format = strb->format;
90 else
91 format = st_choose_renderbuffer_format(pipe->screen, internalFormat);
92
93 /* init renderbuffer fields */
94 strb->Base.Width = width;
95 strb->Base.Height = height;
96 init_renderbuffer_bits(strb, format);
97
98 strb->defined = GL_FALSE; /* undefined contents now */
99
100 if(strb->software) {
101 size_t size;
102
103 _mesa_free(strb->data);
104
105 assert(strb->format != PIPE_FORMAT_NONE);
106
107 strb->stride = util_format_get_stride(strb->format, width);
108 size = util_format_get_2d_size(strb->format, strb->stride, height);
109
110 strb->data = _mesa_malloc(size);
111
112 return strb->data != NULL;
113 }
114 else {
115 struct pipe_texture template;
116 unsigned surface_usage;
117
118 /* Free the old surface and texture
119 */
120 pipe_surface_reference( &strb->surface, NULL );
121 pipe_texture_reference( &strb->texture, NULL );
122
123 /* Setup new texture template.
124 */
125 memset(&template, 0, sizeof(template));
126 template.target = PIPE_TEXTURE_2D;
127 template.format = format;
128 template.width0 = width;
129 template.height0 = height;
130 template.depth0 = 1;
131 template.last_level = 0;
132 template.nr_samples = rb->NumSamples;
133 if (util_format_is_depth_or_stencil(format)) {
134 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
135 }
136 else {
137 template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
138 PIPE_TEXTURE_USAGE_RENDER_TARGET);
139 }
140
141 /* Probably need dedicated flags for surface usage too:
142 */
143 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
144 PIPE_BUFFER_USAGE_GPU_WRITE);
145 #if 0
146 PIPE_BUFFER_USAGE_CPU_READ |
147 PIPE_BUFFER_USAGE_CPU_WRITE);
148 #endif
149
150 strb->texture = pipe->screen->texture_create( pipe->screen,
151 &template );
152
153 if (!strb->texture)
154 return FALSE;
155
156 strb->surface = pipe->screen->get_tex_surface( pipe->screen,
157 strb->texture,
158 0, 0, 0,
159 surface_usage );
160 if (strb->surface) {
161 assert(strb->surface->texture);
162 assert(strb->surface->format);
163 assert(strb->surface->width == width);
164 assert(strb->surface->height == height);
165 }
166
167 return strb->surface != NULL;
168 }
169 }
170
171
172 /**
173 * gl_renderbuffer::Delete()
174 */
175 static void
176 st_renderbuffer_delete(struct gl_renderbuffer *rb)
177 {
178 struct st_renderbuffer *strb = st_renderbuffer(rb);
179 ASSERT(strb);
180 pipe_surface_reference(&strb->surface, NULL);
181 pipe_texture_reference(&strb->texture, NULL);
182 _mesa_free(strb->data);
183 _mesa_free(strb);
184 }
185
186
187 /**
188 * gl_renderbuffer::GetPointer()
189 */
190 static void *
191 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
192 GLint x, GLint y)
193 {
194 /* By returning NULL we force all software rendering to go through
195 * the span routines.
196 */
197 #if 0
198 assert(0); /* Should never get called with softpipe */
199 #endif
200 return NULL;
201 }
202
203
204 /**
205 * Called via ctx->Driver.NewFramebuffer()
206 */
207 static struct gl_framebuffer *
208 st_new_framebuffer(GLcontext *ctx, GLuint name)
209 {
210 /* XXX not sure we need to subclass gl_framebuffer for pipe */
211 return _mesa_new_framebuffer(ctx, name);
212 }
213
214
215 /**
216 * Called via ctx->Driver.NewRenderbuffer()
217 */
218 static struct gl_renderbuffer *
219 st_new_renderbuffer(GLcontext *ctx, GLuint name)
220 {
221 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
222 if (strb) {
223 _mesa_init_renderbuffer(&strb->Base, name);
224 strb->Base.Delete = st_renderbuffer_delete;
225 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
226 strb->Base.GetPointer = null_get_pointer;
227 strb->format = PIPE_FORMAT_NONE;
228 return &strb->Base;
229 }
230 return NULL;
231 }
232
233
234 /**
235 * Allocate a renderbuffer for a an on-screen window (not a user-created
236 * renderbuffer). The window system code determines the format.
237 */
238 struct gl_renderbuffer *
239 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
240 {
241 struct st_renderbuffer *strb;
242
243 strb = ST_CALLOC_STRUCT(st_renderbuffer);
244 if (!strb) {
245 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
246 return NULL;
247 }
248
249 _mesa_init_renderbuffer(&strb->Base, 0);
250 strb->Base.ClassID = 0x4242; /* just a unique value */
251 strb->Base.NumSamples = samples;
252 strb->format = format;
253 init_renderbuffer_bits(strb, format);
254 strb->software = sw;
255
256 switch (format) {
257 case PIPE_FORMAT_A8R8G8B8_UNORM:
258 case PIPE_FORMAT_B8G8R8A8_UNORM:
259 case PIPE_FORMAT_X8R8G8B8_UNORM:
260 case PIPE_FORMAT_B8G8R8X8_UNORM:
261 case PIPE_FORMAT_A1R5G5B5_UNORM:
262 case PIPE_FORMAT_A4R4G4B4_UNORM:
263 case PIPE_FORMAT_R5G6B5_UNORM:
264 strb->Base.InternalFormat = GL_RGBA;
265 break;
266 case PIPE_FORMAT_Z16_UNORM:
267 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
268 break;
269 case PIPE_FORMAT_Z32_UNORM:
270 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
271 break;
272 case PIPE_FORMAT_S8Z24_UNORM:
273 case PIPE_FORMAT_Z24S8_UNORM:
274 case PIPE_FORMAT_X8Z24_UNORM:
275 case PIPE_FORMAT_Z24X8_UNORM:
276 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
277 break;
278 case PIPE_FORMAT_S8_UNORM:
279 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
280 break;
281 case PIPE_FORMAT_R16G16B16A16_SNORM:
282 strb->Base.InternalFormat = GL_RGBA16;
283 break;
284 default:
285 _mesa_problem(NULL,
286 "Unexpected format in st_new_renderbuffer_fb");
287 _mesa_free(strb);
288 return NULL;
289 }
290
291 /* st-specific methods */
292 strb->Base.Delete = st_renderbuffer_delete;
293 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
294 strb->Base.GetPointer = null_get_pointer;
295
296 /* surface is allocated in st_renderbuffer_alloc_storage() */
297 strb->surface = NULL;
298
299 return &strb->Base;
300 }
301
302
303
304
305 /**
306 * Called via ctx->Driver.BindFramebufferEXT().
307 */
308 static void
309 st_bind_framebuffer(GLcontext *ctx, GLenum target,
310 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
311 {
312
313 }
314
315 /**
316 * Called by ctx->Driver.FramebufferRenderbuffer
317 */
318 static void
319 st_framebuffer_renderbuffer(GLcontext *ctx,
320 struct gl_framebuffer *fb,
321 GLenum attachment,
322 struct gl_renderbuffer *rb)
323 {
324 /* XXX no need for derivation? */
325 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
326 }
327
328
329 /**
330 * Called by ctx->Driver.RenderTexture
331 */
332 static void
333 st_render_texture(GLcontext *ctx,
334 struct gl_framebuffer *fb,
335 struct gl_renderbuffer_attachment *att)
336 {
337 struct pipe_screen *screen = ctx->st->pipe->screen;
338 struct st_renderbuffer *strb;
339 struct gl_renderbuffer *rb;
340 struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
341 struct st_texture_object *stObj;
342 const struct gl_texture_image *texImage =
343 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
344
345 if (!pt)
346 return;
347
348 /* create new renderbuffer which wraps the texture image */
349 rb = st_new_renderbuffer(ctx, 0);
350 if (!rb) {
351 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
352 return;
353 }
354
355 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
356 assert(rb->RefCount == 1);
357 rb->AllocStorage = NULL; /* should not get called */
358 strb = st_renderbuffer(rb);
359
360 assert(strb->Base.RefCount > 0);
361
362 /* get the texture for the texture object */
363 stObj = st_texture_object(att->Texture);
364
365 /* point renderbuffer at texobject */
366 strb->rtt = stObj;
367 strb->rtt_level = att->TextureLevel;
368 strb->rtt_face = att->CubeMapFace;
369 strb->rtt_slice = att->Zoffset;
370
371 rb->Width = texImage->Width2;
372 rb->Height = texImage->Height2;
373 rb->_BaseFormat = texImage->_BaseFormat;
374 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
375
376 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
377
378 pipe_texture_reference( &strb->texture, pt );
379
380 pipe_surface_reference(&strb->surface, NULL);
381
382 /* new surface for rendering into the texture */
383 strb->surface = screen->get_tex_surface(screen,
384 strb->texture,
385 strb->rtt_face,
386 strb->rtt_level,
387 strb->rtt_slice,
388 PIPE_BUFFER_USAGE_GPU_READ |
389 PIPE_BUFFER_USAGE_GPU_WRITE);
390
391 init_renderbuffer_bits(strb, pt->format);
392
393 /*
394 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
395 att->Texture, pt, strb->surface, rb->Width, rb->Height);
396 */
397
398 /* Invalidate buffer state so that the pipe's framebuffer state
399 * gets updated.
400 * That's where the new renderbuffer (which we just created) gets
401 * passed to the pipe as a (color/depth) render target.
402 */
403 st_invalidate_state(ctx, _NEW_BUFFERS);
404 }
405
406
407 /**
408 * Called via ctx->Driver.FinishRenderTexture.
409 */
410 static void
411 st_finish_render_texture(GLcontext *ctx,
412 struct gl_renderbuffer_attachment *att)
413 {
414 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
415
416 if (!strb)
417 return;
418
419 st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
420
421 if (strb->surface)
422 pipe_surface_reference( &strb->surface, NULL );
423
424 strb->rtt = NULL;
425
426 /*
427 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
428 */
429
430 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
431
432 /* restore previous framebuffer state */
433 st_invalidate_state(ctx, _NEW_BUFFERS);
434 }
435
436
437 /**
438 * Validate a renderbuffer attachment for a particular usage.
439 */
440
441 static GLboolean
442 st_validate_attachment(struct pipe_screen *screen,
443 const struct gl_renderbuffer_attachment *att,
444 GLuint usage)
445 {
446 const struct st_texture_object *stObj =
447 st_texture_object(att->Texture);
448
449 /**
450 * Only validate texture attachments for now, since
451 * st_renderbuffer_alloc_storage makes sure that
452 * the format is supported.
453 */
454
455 if (att->Type != GL_TEXTURE)
456 return GL_TRUE;
457
458 if (!stObj)
459 return GL_FALSE;
460
461 return screen->is_format_supported(screen, stObj->pt->format,
462 PIPE_TEXTURE_2D,
463 usage, 0);
464 }
465
466 /**
467 * Check that the framebuffer configuration is valid in terms of what
468 * the driver can support.
469 *
470 * For Gallium we only supports combined Z+stencil, not separate buffers.
471 */
472 static void
473 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
474 {
475 struct pipe_screen *screen = ctx->st->pipe->screen;
476 const struct gl_renderbuffer *depthRb =
477 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
478 const struct gl_renderbuffer *stencilRb =
479 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
480 GLuint i;
481
482 if (stencilRb && depthRb && stencilRb != depthRb) {
483 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
484 return;
485 }
486
487 if (!st_validate_attachment(screen,
488 &fb->Attachment[BUFFER_DEPTH],
489 PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
490 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
491 return;
492 }
493 if (!st_validate_attachment(screen,
494 &fb->Attachment[BUFFER_STENCIL],
495 PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
496 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
497 return;
498 }
499 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
500 if (!st_validate_attachment(screen,
501 &fb->Attachment[BUFFER_COLOR0 + i],
502 PIPE_TEXTURE_USAGE_RENDER_TARGET)) {
503 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
504 return;
505 }
506 }
507 }
508
509
510 /**
511 * Copy back color buffer to front color buffer.
512 */
513 static void
514 copy_back_to_front(struct st_context *st,
515 struct gl_framebuffer *fb,
516 gl_buffer_index frontIndex,
517 gl_buffer_index backIndex)
518
519 {
520 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
521 struct pipe_surface *surf_front, *surf_back;
522
523 (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
524 (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
525
526 if (surf_front && surf_back) {
527 if (st->pipe->surface_copy) {
528 st->pipe->surface_copy(st->pipe,
529 surf_front, 0, 0, /* dest */
530 surf_back, 0, 0, /* src */
531 fb->Width, fb->Height);
532 } else {
533 util_surface_copy(st->pipe, FALSE,
534 surf_front, 0, 0,
535 surf_back, 0, 0,
536 fb->Width, fb->Height);
537 }
538 }
539 }
540
541
542 /**
543 * Check if we're drawing into, or read from, a front color buffer. If the
544 * front buffer is missing, create it now.
545 *
546 * The back color buffer must exist since we'll use its format/samples info
547 * for creating the front buffer.
548 *
549 * \param frontIndex either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
550 * \param backIndex either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
551 */
552 static void
553 check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
554 gl_buffer_index frontIndex,
555 gl_buffer_index backIndex)
556 {
557 if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
558 GLboolean create = GL_FALSE;
559
560 /* check if drawing to or reading from front buffer */
561 if (fb->_ColorReadBufferIndex == frontIndex) {
562 create = GL_TRUE;
563 }
564 else {
565 GLuint b;
566 for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
567 if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
568 create = GL_TRUE;
569 break;
570 }
571 }
572 }
573
574 if (create) {
575 struct st_renderbuffer *back;
576 struct gl_renderbuffer *front;
577 enum pipe_format colorFormat;
578 uint samples;
579
580 if (0)
581 _mesa_debug(ctx, "Allocate new front buffer\n");
582
583 /* get back renderbuffer info */
584 back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
585 colorFormat = back->format;
586 samples = back->Base.NumSamples;
587
588 /* create front renderbuffer */
589 front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
590 _mesa_add_renderbuffer(fb, frontIndex, front);
591
592 /* alloc texture/surface for new front buffer */
593 front->AllocStorage(ctx, front, front->InternalFormat,
594 fb->Width, fb->Height);
595
596 /* initialize the front color buffer contents by copying
597 * the back buffer.
598 */
599 copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
600 }
601 }
602 }
603
604
605 /**
606 * If front left/right color buffers are missing, create them now.
607 */
608 static void
609 check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
610 {
611 /* check if we need to create the front left buffer now */
612 check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
613
614 if (fb->Visual.stereoMode) {
615 check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
616 }
617
618 st_invalidate_state(ctx, _NEW_BUFFERS);
619 }
620
621
622 /**
623 * Called via glDrawBuffer.
624 */
625 static void
626 st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
627 {
628 (void) count;
629 (void) buffers;
630 check_create_front_buffers(ctx, ctx->DrawBuffer);
631 }
632
633
634 /**
635 * Called via glReadBuffer.
636 */
637 static void
638 st_ReadBuffer(GLcontext *ctx, GLenum buffer)
639 {
640 (void) buffer;
641 check_create_front_buffers(ctx, ctx->ReadBuffer);
642 }
643
644
645 void st_init_fbo_functions(struct dd_function_table *functions)
646 {
647 functions->NewFramebuffer = st_new_framebuffer;
648 functions->NewRenderbuffer = st_new_renderbuffer;
649 functions->BindFramebuffer = st_bind_framebuffer;
650 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
651 functions->RenderTexture = st_render_texture;
652 functions->FinishRenderTexture = st_finish_render_texture;
653 functions->ValidateFramebuffer = st_validate_framebuffer;
654 /* no longer needed by core Mesa, drivers handle resizes...
655 functions->ResizeBuffers = st_resize_buffers;
656 */
657
658 functions->DrawBuffers = st_DrawBuffers;
659 functions->ReadBuffer = st_ReadBuffer;
660 }