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