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