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