4d321587fc87add3d5f99a29911f7cf82714a594
[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/mfeatures.h"
42 #include "main/renderbuffer.h"
43
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_screen.h"
47 #include "st_context.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_cb_texture.h"
51 #include "st_format.h"
52 #include "st_texture.h"
53 #include "st_manager.h"
54
55 #include "util/u_format.h"
56 #include "util/u_inlines.h"
57 #include "util/u_surface.h"
58
59
60 /**
61 * gl_renderbuffer::AllocStorage()
62 * This is called to allocate the original drawing surface, and
63 * during window resize.
64 */
65 static GLboolean
66 st_renderbuffer_alloc_storage(struct gl_context * ctx,
67 struct gl_renderbuffer *rb,
68 GLenum internalFormat,
69 GLuint width, GLuint height)
70 {
71 struct st_context *st = st_context(ctx);
72 struct pipe_context *pipe = st->pipe;
73 struct pipe_screen *screen = st->pipe->screen;
74 struct st_renderbuffer *strb = st_renderbuffer(rb);
75 enum pipe_format format;
76 struct pipe_surface surf_tmpl;
77
78 format = st_choose_renderbuffer_format(screen, internalFormat,
79 rb->NumSamples);
80
81 if (format == PIPE_FORMAT_NONE) {
82 return FALSE;
83 }
84
85 /* init renderbuffer fields */
86 strb->Base.Width = width;
87 strb->Base.Height = height;
88 strb->Base.Format = st_pipe_format_to_mesa_format(format);
89 strb->Base._BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
90 strb->Base.DataType = st_format_datatype(format);
91 strb->format = format;
92
93 strb->defined = GL_FALSE; /* undefined contents now */
94
95 if (strb->software) {
96 size_t size;
97
98 free(strb->data);
99
100 assert(strb->format != PIPE_FORMAT_NONE);
101
102 strb->stride = util_format_get_stride(strb->format, width);
103 size = util_format_get_2d_size(strb->format, strb->stride, height);
104
105 strb->data = malloc(size);
106
107 return strb->data != NULL;
108 }
109 else {
110 struct pipe_resource template;
111
112 /* Free the old surface and texture
113 */
114 pipe_surface_reference( &strb->surface, NULL );
115 pipe_resource_reference( &strb->texture, NULL );
116
117 /* Setup new texture template.
118 */
119 memset(&template, 0, sizeof(template));
120 template.target = st->internal_target;
121 template.format = format;
122 template.width0 = width;
123 template.height0 = height;
124 template.depth0 = 1;
125 template.array_size = 1;
126 template.last_level = 0;
127 template.nr_samples = rb->NumSamples;
128 if (util_format_is_depth_or_stencil(format)) {
129 template.bind = PIPE_BIND_DEPTH_STENCIL;
130 }
131 else {
132 template.bind = (PIPE_BIND_DISPLAY_TARGET |
133 PIPE_BIND_RENDER_TARGET);
134 }
135
136 strb->texture = screen->resource_create(screen, &template);
137
138 if (!strb->texture)
139 return FALSE;
140
141 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
142 u_surface_default_template(&surf_tmpl, strb->texture, template.bind);
143 strb->surface = pipe->create_surface(pipe,
144 strb->texture,
145 &surf_tmpl);
146 if (strb->surface) {
147 assert(strb->surface->texture);
148 assert(strb->surface->format);
149 assert(strb->surface->width == width);
150 assert(strb->surface->height == height);
151 }
152
153 return strb->surface != NULL;
154 }
155 }
156
157
158 /**
159 * gl_renderbuffer::Delete()
160 */
161 static void
162 st_renderbuffer_delete(struct gl_renderbuffer *rb)
163 {
164 struct st_renderbuffer *strb = st_renderbuffer(rb);
165 ASSERT(strb);
166 pipe_surface_reference(&strb->surface, NULL);
167 pipe_resource_reference(&strb->texture, NULL);
168 free(strb->data);
169 free(strb);
170 }
171
172
173 /**
174 * gl_renderbuffer::GetPointer()
175 */
176 static void *
177 null_get_pointer(struct gl_context * 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(struct gl_context *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(struct gl_context *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._BaseFormat = _mesa_get_format_base_format(strb->Base.Format);
240 strb->Base.DataType = st_format_datatype(format);
241 strb->format = format;
242 strb->software = sw;
243
244 switch (format) {
245 case PIPE_FORMAT_R8G8B8A8_UNORM:
246 case PIPE_FORMAT_B8G8R8A8_UNORM:
247 case PIPE_FORMAT_A8R8G8B8_UNORM:
248 case PIPE_FORMAT_R8G8B8X8_UNORM:
249 case PIPE_FORMAT_B8G8R8X8_UNORM:
250 case PIPE_FORMAT_X8R8G8B8_UNORM:
251 case PIPE_FORMAT_B5G5R5A1_UNORM:
252 case PIPE_FORMAT_B4G4R4A4_UNORM:
253 case PIPE_FORMAT_B5G6R5_UNORM:
254 strb->Base.InternalFormat = GL_RGBA;
255 break;
256 case PIPE_FORMAT_Z16_UNORM:
257 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
258 break;
259 case PIPE_FORMAT_Z32_UNORM:
260 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
261 break;
262 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
263 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
264 case PIPE_FORMAT_Z24X8_UNORM:
265 case PIPE_FORMAT_X8Z24_UNORM:
266 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
267 break;
268 case PIPE_FORMAT_S8_USCALED:
269 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
270 break;
271 case PIPE_FORMAT_R16G16B16A16_SNORM:
272 /* accum buffer */
273 strb->Base.InternalFormat = GL_RGBA16_SNORM;
274 break;
275 case PIPE_FORMAT_R8_UNORM:
276 strb->Base.InternalFormat = GL_R8;
277 break;
278 case PIPE_FORMAT_R8G8_UNORM:
279 strb->Base.InternalFormat = GL_RG8;
280 break;
281 case PIPE_FORMAT_R16_UNORM:
282 strb->Base.InternalFormat = GL_R16;
283 break;
284 case PIPE_FORMAT_R16G16_UNORM:
285 strb->Base.InternalFormat = GL_RG16;
286 break;
287 default:
288 _mesa_problem(NULL,
289 "Unexpected format in st_new_renderbuffer_fb");
290 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(struct gl_context *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(struct gl_context *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(struct gl_context *ctx,
337 struct gl_framebuffer *fb,
338 struct gl_renderbuffer_attachment *att)
339 {
340 struct st_context *st = st_context(ctx);
341 struct pipe_context *pipe = st->pipe;
342 struct st_renderbuffer *strb;
343 struct gl_renderbuffer *rb;
344 struct pipe_resource *pt;
345 struct st_texture_object *stObj;
346 const struct gl_texture_image *texImage;
347 struct pipe_surface surf_tmpl;
348
349 if (!st_finalize_texture(ctx, pipe, att->Texture))
350 return;
351
352 pt = st_get_texobj_resource(att->Texture);
353 assert(pt);
354
355 /* get pointer to texture image we're rendeing to */
356 texImage = _mesa_get_attachment_teximage(att);
357
358 /* create new renderbuffer which wraps the texture image */
359 rb = st_new_renderbuffer(ctx, 0);
360 if (!rb) {
361 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
362 return;
363 }
364
365 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
366 assert(rb->RefCount == 1);
367 rb->AllocStorage = NULL; /* should not get called */
368 strb = st_renderbuffer(rb);
369
370 assert(strb->Base.RefCount > 0);
371
372 /* get the texture for the texture object */
373 stObj = st_texture_object(att->Texture);
374
375 /* point renderbuffer at texobject */
376 strb->rtt = stObj;
377 strb->rtt_level = att->TextureLevel;
378 strb->rtt_face = att->CubeMapFace;
379 strb->rtt_slice = att->Zoffset;
380
381 rb->Width = texImage->Width2;
382 rb->Height = texImage->Height2;
383 rb->_BaseFormat = texImage->_BaseFormat;
384 rb->InternalFormat = texImage->InternalFormat;
385 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
386
387 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
388
389 pipe_resource_reference( &strb->texture, pt );
390
391 pipe_surface_reference(&strb->surface, NULL);
392
393 assert(strb->rtt_level <= strb->texture->last_level);
394
395 /* new surface for rendering into the texture */
396 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
397 surf_tmpl.format = ctx->Color.sRGBEnabled ? strb->texture->format : util_format_linear(strb->texture->format);
398 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
399 surf_tmpl.u.tex.level = strb->rtt_level;
400 surf_tmpl.u.tex.first_layer = strb->rtt_face + strb->rtt_slice;
401 surf_tmpl.u.tex.last_layer = strb->rtt_face + strb->rtt_slice;
402 strb->surface = pipe->create_surface(pipe,
403 strb->texture,
404 &surf_tmpl);
405
406 strb->format = pt->format;
407
408 strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
409 strb->Base.DataType = st_format_datatype(pt->format);
410
411 /*
412 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
413 att->Texture, pt, strb->surface, rb->Width, rb->Height);
414 */
415
416 /* Invalidate buffer state so that the pipe's framebuffer state
417 * gets updated.
418 * That's where the new renderbuffer (which we just created) gets
419 * passed to the pipe as a (color/depth) render target.
420 */
421 st_invalidate_state(ctx, _NEW_BUFFERS);
422 }
423
424
425 /**
426 * Called via ctx->Driver.FinishRenderTexture.
427 */
428 static void
429 st_finish_render_texture(struct gl_context *ctx,
430 struct gl_renderbuffer_attachment *att)
431 {
432 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
433
434 if (!strb)
435 return;
436
437 strb->rtt = NULL;
438
439 /*
440 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
441 */
442
443 /* restore previous framebuffer state */
444 st_invalidate_state(ctx, _NEW_BUFFERS);
445 }
446
447
448 /**
449 * Validate a renderbuffer attachment for a particular set of bindings.
450 */
451 static GLboolean
452 st_validate_attachment(struct gl_context *ctx,
453 struct pipe_screen *screen,
454 const struct gl_renderbuffer_attachment *att,
455 unsigned bindings)
456 {
457 const struct st_texture_object *stObj = st_texture_object(att->Texture);
458 enum pipe_format format;
459 gl_format texFormat;
460
461 /* Only validate texture attachments for now, since
462 * st_renderbuffer_alloc_storage makes sure that
463 * the format is supported.
464 */
465 if (att->Type != GL_TEXTURE)
466 return GL_TRUE;
467
468 if (!stObj)
469 return GL_FALSE;
470
471 format = stObj->pt->format;
472 texFormat = _mesa_get_attachment_teximage_const(att)->TexFormat;
473
474 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
475 * check for linear format support instead.
476 * Later when we create a surface, we change the format to a linear one. */
477 if (!ctx->Const.sRGBCapable &&
478 _mesa_get_format_color_encoding(texFormat) == GL_SRGB) {
479 const gl_format linearFormat = _mesa_get_srgb_format_linear(texFormat);
480 format = st_mesa_format_to_pipe_format(linearFormat);
481 }
482
483 return screen->is_format_supported(screen, format,
484 PIPE_TEXTURE_2D,
485 stObj->pt->nr_samples, bindings);
486 }
487
488
489 /**
490 * Check if two renderbuffer attachments name a combined depth/stencil
491 * renderbuffer.
492 */
493 GLboolean
494 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment *depth,
495 const struct gl_renderbuffer_attachment *stencil)
496 {
497 assert(depth && stencil);
498
499 if (depth->Type == stencil->Type) {
500 if (depth->Type == GL_RENDERBUFFER_EXT &&
501 depth->Renderbuffer == stencil->Renderbuffer)
502 return GL_TRUE;
503
504 if (depth->Type == GL_TEXTURE &&
505 depth->Texture == stencil->Texture)
506 return GL_TRUE;
507 }
508
509 return GL_FALSE;
510 }
511
512
513 /**
514 * Check that the framebuffer configuration is valid in terms of what
515 * the driver can support.
516 *
517 * For Gallium we only supports combined Z+stencil, not separate buffers.
518 */
519 static void
520 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
521 {
522 struct st_context *st = st_context(ctx);
523 struct pipe_screen *screen = st->pipe->screen;
524 const struct gl_renderbuffer_attachment *depth =
525 &fb->Attachment[BUFFER_DEPTH];
526 const struct gl_renderbuffer_attachment *stencil =
527 &fb->Attachment[BUFFER_STENCIL];
528 GLuint i;
529 enum pipe_format first_format = PIPE_FORMAT_NONE;
530 boolean mixed_formats =
531 screen->get_param(screen, PIPE_CAP_MIXED_COLORBUFFER_FORMATS) != 0;
532
533 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
534 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
535 return;
536 }
537 if (depth->Type == GL_RENDERBUFFER_EXT &&
538 stencil->Type == GL_RENDERBUFFER_EXT &&
539 depth->Renderbuffer != stencil->Renderbuffer) {
540 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
541 return;
542 }
543 if (depth->Type == GL_TEXTURE &&
544 stencil->Type == GL_TEXTURE &&
545 depth->Texture != stencil->Texture) {
546 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
547 return;
548 }
549
550 if (!st_validate_attachment(ctx,
551 screen,
552 depth,
553 PIPE_BIND_DEPTH_STENCIL)) {
554 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
555 return;
556 }
557 if (!st_validate_attachment(ctx,
558 screen,
559 stencil,
560 PIPE_BIND_DEPTH_STENCIL)) {
561 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
562 return;
563 }
564 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
565 struct gl_renderbuffer_attachment *att =
566 &fb->Attachment[BUFFER_COLOR0 + i];
567 enum pipe_format format;
568
569 if (!st_validate_attachment(ctx,
570 screen,
571 att,
572 PIPE_BIND_RENDER_TARGET)) {
573 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
574 return;
575 }
576
577 if (!mixed_formats) {
578 /* Disallow mixed formats. */
579 if (att->Type != GL_NONE) {
580 format = st_renderbuffer(att->Renderbuffer)->surface->format;
581 } else {
582 continue;
583 }
584
585 if (first_format == PIPE_FORMAT_NONE) {
586 first_format = format;
587 } else if (format != first_format) {
588 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
589 return;
590 }
591 }
592 }
593 }
594
595
596 /**
597 * Called via glDrawBuffer.
598 */
599 static void
600 st_DrawBuffers(struct gl_context *ctx, GLsizei count, const GLenum *buffers)
601 {
602 struct st_context *st = st_context(ctx);
603 struct gl_framebuffer *fb = ctx->DrawBuffer;
604 GLuint i;
605
606 (void) count;
607 (void) buffers;
608
609 /* add the renderbuffers on demand */
610 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
611 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
612 st_manager_add_color_renderbuffer(st, fb, idx);
613 }
614 }
615
616
617 /**
618 * Called via glReadBuffer.
619 */
620 static void
621 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
622 {
623 struct st_context *st = st_context(ctx);
624 struct gl_framebuffer *fb = ctx->ReadBuffer;
625
626 (void) buffer;
627
628 /* add the renderbuffer on demand */
629 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
630 }
631
632
633 void st_init_fbo_functions(struct dd_function_table *functions)
634 {
635 #if FEATURE_EXT_framebuffer_object
636 functions->NewFramebuffer = st_new_framebuffer;
637 functions->NewRenderbuffer = st_new_renderbuffer;
638 functions->BindFramebuffer = st_bind_framebuffer;
639 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
640 functions->RenderTexture = st_render_texture;
641 functions->FinishRenderTexture = st_finish_render_texture;
642 functions->ValidateFramebuffer = st_validate_framebuffer;
643 #endif
644 /* no longer needed by core Mesa, drivers handle resizes...
645 functions->ResizeBuffers = st_resize_buffers;
646 */
647
648 functions->DrawBuffers = st_DrawBuffers;
649 functions->ReadBuffer = st_ReadBuffer;
650 }
651
652