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