9d06a232bfa8673326471a110c5f2df40e95d983
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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/glformats.h"
41 #include "main/macros.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 static GLboolean
61 st_renderbuffer_alloc_sw_storage(struct gl_context * ctx,
62 struct gl_renderbuffer *rb,
63 GLenum internalFormat,
64 GLuint width, GLuint height)
65 {
66 struct st_context *st = st_context(ctx);
67 struct st_renderbuffer *strb = st_renderbuffer(rb);
68 enum pipe_format format;
69 size_t size;
70
71 free(strb->data);
72 strb->data = NULL;
73
74 if (internalFormat == GL_RGBA16_SNORM) {
75 /* Special case for software accum buffers. Otherwise, if the
76 * call to st_choose_renderbuffer_format() fails (because the
77 * driver doesn't support signed 16-bit/channel colors) we'd
78 * just return without allocating the software accum buffer.
79 */
80 format = PIPE_FORMAT_R16G16B16A16_SNORM;
81 }
82 else {
83 format = st_choose_renderbuffer_format(st, internalFormat, 0);
84
85 /* Not setting gl_renderbuffer::Format here will cause
86 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
87 */
88 if (format == PIPE_FORMAT_NONE) {
89 return GL_TRUE;
90 }
91 }
92
93 strb->Base.Format = st_pipe_format_to_mesa_format(format);
94
95 size = _mesa_format_image_size(strb->Base.Format, width, height, 1);
96 strb->data = malloc(size);
97 return strb->data != NULL;
98 }
99
100
101 /**
102 * gl_renderbuffer::AllocStorage()
103 * This is called to allocate the original drawing surface, and
104 * during window resize.
105 */
106 static GLboolean
107 st_renderbuffer_alloc_storage(struct gl_context * ctx,
108 struct gl_renderbuffer *rb,
109 GLenum internalFormat,
110 GLuint width, GLuint height)
111 {
112 struct st_context *st = st_context(ctx);
113 struct pipe_context *pipe = st->pipe;
114 struct pipe_screen *screen = st->pipe->screen;
115 struct st_renderbuffer *strb = st_renderbuffer(rb);
116 enum pipe_format format = PIPE_FORMAT_NONE;
117 struct pipe_surface surf_tmpl;
118 struct pipe_resource templ;
119
120 /* init renderbuffer fields */
121 strb->Base.Width = width;
122 strb->Base.Height = height;
123 strb->Base._BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
124 strb->defined = GL_FALSE; /* undefined contents now */
125
126 if (strb->software) {
127 return st_renderbuffer_alloc_sw_storage(ctx, rb, internalFormat,
128 width, height);
129 }
130
131 /* Free the old surface and texture
132 */
133 pipe_surface_reference( &strb->surface, NULL );
134 pipe_resource_reference( &strb->texture, NULL );
135
136 /* If an sRGB framebuffer is unsupported, sRGB formats behave like linear
137 * formats.
138 */
139 if (!ctx->Extensions.EXT_framebuffer_sRGB) {
140 internalFormat = _mesa_get_linear_internalformat(internalFormat);
141 }
142
143 /* Handle multisample renderbuffers first.
144 *
145 * From ARB_framebuffer_object:
146 * If <samples> is zero, then RENDERBUFFER_SAMPLES is set to zero.
147 * Otherwise <samples> represents a request for a desired minimum
148 * number of samples. Since different implementations may support
149 * different sample counts for multisampled rendering, the actual
150 * number of samples allocated for the renderbuffer image is
151 * implementation dependent. However, the resulting value for
152 * RENDERBUFFER_SAMPLES is guaranteed to be greater than or equal
153 * to <samples> and no more than the next larger sample count supported
154 * by the implementation.
155 *
156 * So let's find the supported number of samples closest to NumSamples.
157 * (NumSamples == 1) is treated the same as (NumSamples == 0).
158 */
159 if (rb->NumSamples > 1) {
160 unsigned i;
161
162 for (i = rb->NumSamples; i <= ctx->Const.MaxSamples; i++) {
163 format = st_choose_renderbuffer_format(st, internalFormat, i);
164
165 if (format != PIPE_FORMAT_NONE) {
166 rb->NumSamples = i;
167 break;
168 }
169 }
170 } else {
171 format = st_choose_renderbuffer_format(st, internalFormat, 0);
172 }
173
174 /* Not setting gl_renderbuffer::Format here will cause
175 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
176 */
177 if (format == PIPE_FORMAT_NONE) {
178 return GL_TRUE;
179 }
180
181 strb->Base.Format = st_pipe_format_to_mesa_format(format);
182
183 if (width == 0 || height == 0) {
184 /* if size is zero, nothing to allocate */
185 return GL_TRUE;
186 }
187
188 /* Setup new texture template.
189 */
190 memset(&templ, 0, sizeof(templ));
191 templ.target = st->internal_target;
192 templ.format = format;
193 templ.width0 = width;
194 templ.height0 = height;
195 templ.depth0 = 1;
196 templ.array_size = 1;
197 templ.nr_samples = rb->NumSamples;
198 if (util_format_is_depth_or_stencil(format)) {
199 templ.bind = PIPE_BIND_DEPTH_STENCIL;
200 }
201 else if (strb->Base.Name != 0) {
202 /* this is a user-created renderbuffer */
203 templ.bind = PIPE_BIND_RENDER_TARGET;
204 }
205 else {
206 /* this is a window-system buffer */
207 templ.bind = (PIPE_BIND_DISPLAY_TARGET |
208 PIPE_BIND_RENDER_TARGET);
209 }
210
211 strb->texture = screen->resource_create(screen, &templ);
212
213 if (!strb->texture)
214 return FALSE;
215
216 u_surface_default_template(&surf_tmpl, strb->texture);
217 strb->surface = pipe->create_surface(pipe,
218 strb->texture,
219 &surf_tmpl);
220 if (strb->surface) {
221 assert(strb->surface->texture);
222 assert(strb->surface->format);
223 assert(strb->surface->width == width);
224 assert(strb->surface->height == height);
225 }
226
227 return strb->surface != NULL;
228 }
229
230
231 /**
232 * gl_renderbuffer::Delete()
233 */
234 static void
235 st_renderbuffer_delete(struct gl_context *ctx, struct gl_renderbuffer *rb)
236 {
237 struct st_renderbuffer *strb = st_renderbuffer(rb);
238 if (ctx) {
239 struct st_context *st = st_context(ctx);
240 pipe_surface_release(st->pipe, &strb->surface);
241 }
242 pipe_resource_reference(&strb->texture, NULL);
243 free(strb->data);
244 _mesa_delete_renderbuffer(ctx, rb);
245 }
246
247
248 /**
249 * Called via ctx->Driver.NewFramebuffer()
250 */
251 static struct gl_framebuffer *
252 st_new_framebuffer(struct gl_context *ctx, GLuint name)
253 {
254 /* XXX not sure we need to subclass gl_framebuffer for pipe */
255 return _mesa_new_framebuffer(ctx, name);
256 }
257
258
259 /**
260 * Called via ctx->Driver.NewRenderbuffer()
261 */
262 static struct gl_renderbuffer *
263 st_new_renderbuffer(struct gl_context *ctx, GLuint name)
264 {
265 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
266 if (strb) {
267 assert(name != 0);
268 _mesa_init_renderbuffer(&strb->Base, name);
269 strb->Base.Delete = st_renderbuffer_delete;
270 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
271 return &strb->Base;
272 }
273 return NULL;
274 }
275
276
277 /**
278 * Allocate a renderbuffer for a an on-screen window (not a user-created
279 * renderbuffer). The window system code determines the format.
280 */
281 struct gl_renderbuffer *
282 st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
283 {
284 struct st_renderbuffer *strb;
285
286 strb = ST_CALLOC_STRUCT(st_renderbuffer);
287 if (!strb) {
288 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
289 return NULL;
290 }
291
292 _mesa_init_renderbuffer(&strb->Base, 0);
293 strb->Base.ClassID = 0x4242; /* just a unique value */
294 strb->Base.NumSamples = samples;
295 strb->Base.Format = st_pipe_format_to_mesa_format(format);
296 strb->Base._BaseFormat = _mesa_get_format_base_format(strb->Base.Format);
297 strb->software = sw;
298
299 switch (format) {
300 case PIPE_FORMAT_R8G8B8A8_UNORM:
301 case PIPE_FORMAT_B8G8R8A8_UNORM:
302 case PIPE_FORMAT_A8R8G8B8_UNORM:
303 strb->Base.InternalFormat = GL_RGBA8;
304 break;
305 case PIPE_FORMAT_R8G8B8X8_UNORM:
306 case PIPE_FORMAT_B8G8R8X8_UNORM:
307 case PIPE_FORMAT_X8R8G8B8_UNORM:
308 strb->Base.InternalFormat = GL_RGB8;
309 break;
310 case PIPE_FORMAT_R8G8B8A8_SRGB:
311 case PIPE_FORMAT_B8G8R8A8_SRGB:
312 case PIPE_FORMAT_A8R8G8B8_SRGB:
313 strb->Base.InternalFormat = GL_SRGB8_ALPHA8;
314 break;
315 case PIPE_FORMAT_R8G8B8X8_SRGB:
316 case PIPE_FORMAT_B8G8R8X8_SRGB:
317 case PIPE_FORMAT_X8R8G8B8_SRGB:
318 strb->Base.InternalFormat = GL_SRGB8;
319 break;
320 case PIPE_FORMAT_B5G5R5A1_UNORM:
321 strb->Base.InternalFormat = GL_RGB5_A1;
322 break;
323 case PIPE_FORMAT_B4G4R4A4_UNORM:
324 strb->Base.InternalFormat = GL_RGBA4;
325 break;
326 case PIPE_FORMAT_B5G6R5_UNORM:
327 strb->Base.InternalFormat = GL_RGB565;
328 break;
329 case PIPE_FORMAT_Z16_UNORM:
330 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
331 break;
332 case PIPE_FORMAT_Z32_UNORM:
333 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
334 break;
335 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
336 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
337 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
338 break;
339 case PIPE_FORMAT_Z24X8_UNORM:
340 case PIPE_FORMAT_X8Z24_UNORM:
341 strb->Base.InternalFormat = GL_DEPTH_COMPONENT24;
342 break;
343 case PIPE_FORMAT_S8_UINT:
344 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
345 break;
346 case PIPE_FORMAT_R16G16B16A16_SNORM:
347 /* accum buffer */
348 strb->Base.InternalFormat = GL_RGBA16_SNORM;
349 break;
350 case PIPE_FORMAT_R16G16B16A16_UNORM:
351 strb->Base.InternalFormat = GL_RGBA16;
352 break;
353 case PIPE_FORMAT_R8_UNORM:
354 strb->Base.InternalFormat = GL_R8;
355 break;
356 case PIPE_FORMAT_R8G8_UNORM:
357 strb->Base.InternalFormat = GL_RG8;
358 break;
359 case PIPE_FORMAT_R16_UNORM:
360 strb->Base.InternalFormat = GL_R16;
361 break;
362 case PIPE_FORMAT_R16G16_UNORM:
363 strb->Base.InternalFormat = GL_RG16;
364 break;
365 case PIPE_FORMAT_R32G32B32A32_FLOAT:
366 strb->Base.InternalFormat = GL_RGBA32F;
367 break;
368 case PIPE_FORMAT_R16G16B16A16_FLOAT:
369 strb->Base.InternalFormat = GL_RGBA16F;
370 break;
371 default:
372 _mesa_problem(NULL,
373 "Unexpected format %s in st_new_renderbuffer_fb",
374 util_format_name(format));
375 free(strb);
376 return NULL;
377 }
378
379 /* st-specific methods */
380 strb->Base.Delete = st_renderbuffer_delete;
381 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
382
383 /* surface is allocated in st_renderbuffer_alloc_storage() */
384 strb->surface = NULL;
385
386 return &strb->Base;
387 }
388
389
390 /**
391 * Create or update the pipe_surface of a FBO renderbuffer.
392 * This is usually called after st_finalize_texture.
393 */
394 void
395 st_update_renderbuffer_surface(struct st_context *st,
396 struct st_renderbuffer *strb)
397 {
398 struct pipe_context *pipe = st->pipe;
399 struct pipe_resource *resource = strb->texture;
400 unsigned rtt_width = strb->Base.Width;
401 unsigned rtt_height = strb->Base.Height;
402 unsigned rtt_depth = strb->Base.Depth;
403 /*
404 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
405 * the format of strb->texture is linear (because we have no control over
406 * the format). Check strb->Base.Format instead of strb->texture->format
407 * to determine if the rb is sRGB-capable.
408 */
409 boolean enable_srgb = (st->ctx->Color.sRGBEnabled &&
410 _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB);
411 enum pipe_format format = (enable_srgb) ?
412 util_format_srgb(resource->format) :
413 util_format_linear(resource->format);
414 unsigned first_layer, last_layer, level;
415
416 if (resource->target == PIPE_TEXTURE_1D_ARRAY) {
417 rtt_depth = rtt_height;
418 rtt_height = 1;
419 }
420
421 /* find matching mipmap level size */
422 for (level = 0; level <= resource->last_level; level++) {
423 if (u_minify(resource->width0, level) == rtt_width &&
424 u_minify(resource->height0, level) == rtt_height &&
425 (resource->target != PIPE_TEXTURE_3D ||
426 u_minify(resource->depth0, level) == rtt_depth)) {
427 break;
428 }
429 }
430 assert(level <= resource->last_level);
431
432 /* determine the layer bounds */
433 if (strb->rtt_layered) {
434 first_layer = 0;
435 last_layer = util_max_layer(strb->texture, level);
436 }
437 else {
438 first_layer =
439 last_layer = strb->rtt_face + strb->rtt_slice;
440 }
441
442 /* Adjust for texture views */
443 if (strb->is_rtt && resource->array_size > 1 &&
444 strb->Base.TexImage->TexObject->Immutable) {
445 struct gl_texture_object *tex = strb->Base.TexImage->TexObject;
446 first_layer += tex->MinLayer;
447 if (!strb->rtt_layered)
448 last_layer += tex->MinLayer;
449 else
450 last_layer = MIN2(first_layer + tex->NumLayers - 1, last_layer);
451 }
452
453 if (!strb->surface ||
454 strb->surface->texture->nr_samples != strb->Base.NumSamples ||
455 strb->surface->format != format ||
456 strb->surface->texture != resource ||
457 strb->surface->width != rtt_width ||
458 strb->surface->height != rtt_height ||
459 strb->surface->u.tex.level != level ||
460 strb->surface->u.tex.first_layer != first_layer ||
461 strb->surface->u.tex.last_layer != last_layer) {
462 /* create a new pipe_surface */
463 struct pipe_surface surf_tmpl;
464 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
465 surf_tmpl.format = format;
466 surf_tmpl.u.tex.level = level;
467 surf_tmpl.u.tex.first_layer = first_layer;
468 surf_tmpl.u.tex.last_layer = last_layer;
469
470 pipe_surface_reference(&strb->surface, NULL);
471
472 strb->surface = pipe->create_surface(pipe, resource, &surf_tmpl);
473 }
474 }
475
476 /**
477 * Called by ctx->Driver.RenderTexture
478 */
479 static void
480 st_render_texture(struct gl_context *ctx,
481 struct gl_framebuffer *fb,
482 struct gl_renderbuffer_attachment *att)
483 {
484 struct st_context *st = st_context(ctx);
485 struct pipe_context *pipe = st->pipe;
486 struct gl_renderbuffer *rb = att->Renderbuffer;
487 struct st_renderbuffer *strb = st_renderbuffer(rb);
488 struct pipe_resource *pt;
489
490 if (!st_finalize_texture(ctx, pipe, att->Texture))
491 return;
492
493 pt = st_get_texobj_resource(att->Texture);
494 assert(pt);
495
496 /* point renderbuffer at texobject */
497 strb->is_rtt = TRUE;
498 strb->rtt_face = att->CubeMapFace;
499 strb->rtt_slice = att->Zoffset;
500 strb->rtt_layered = att->Layered;
501 pipe_resource_reference(&strb->texture, pt);
502
503 st_update_renderbuffer_surface(st, strb);
504
505 strb->Base.Format = st_pipe_format_to_mesa_format(pt->format);
506
507 /* Invalidate buffer state so that the pipe's framebuffer state
508 * gets updated.
509 * That's where the new renderbuffer (which we just created) gets
510 * passed to the pipe as a (color/depth) render target.
511 */
512 st_invalidate_state(ctx, _NEW_BUFFERS);
513
514
515 /* Need to trigger a call to update_framebuffer() since we just
516 * attached a new renderbuffer.
517 */
518 ctx->NewState |= _NEW_BUFFERS;
519 }
520
521
522 /**
523 * Called via ctx->Driver.FinishRenderTexture.
524 */
525 static void
526 st_finish_render_texture(struct gl_context *ctx, struct gl_renderbuffer *rb)
527 {
528 struct st_renderbuffer *strb = st_renderbuffer(rb);
529
530 if (!strb)
531 return;
532
533 strb->is_rtt = FALSE;
534
535 /* restore previous framebuffer state */
536 st_invalidate_state(ctx, _NEW_BUFFERS);
537 }
538
539
540 /** Debug helper */
541 static void
542 st_fbo_invalid(const char *reason)
543 {
544 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
545 _mesa_debug(NULL, "Invalid FBO: %s\n", reason);
546 }
547 }
548
549
550 /**
551 * Validate a renderbuffer attachment for a particular set of bindings.
552 */
553 static GLboolean
554 st_validate_attachment(struct gl_context *ctx,
555 struct pipe_screen *screen,
556 const struct gl_renderbuffer_attachment *att,
557 unsigned bindings)
558 {
559 const struct st_texture_object *stObj = st_texture_object(att->Texture);
560 enum pipe_format format;
561 mesa_format texFormat;
562 GLboolean valid;
563
564 /* Sanity check: we must be binding the surface as a (color) render target
565 * or depth/stencil target.
566 */
567 assert(bindings == PIPE_BIND_RENDER_TARGET ||
568 bindings == PIPE_BIND_DEPTH_STENCIL);
569
570 /* Only validate texture attachments for now, since
571 * st_renderbuffer_alloc_storage makes sure that
572 * the format is supported.
573 */
574 if (att->Type != GL_TEXTURE)
575 return GL_TRUE;
576
577 if (!stObj || !stObj->pt)
578 return GL_FALSE;
579
580 format = stObj->pt->format;
581 texFormat = att->Renderbuffer->TexImage->TexFormat;
582
583 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
584 * check for linear format support instead.
585 * Later when we create a surface, we change the format to a linear one. */
586 if (!ctx->Extensions.EXT_framebuffer_sRGB &&
587 _mesa_get_format_color_encoding(texFormat) == GL_SRGB) {
588 const mesa_format linearFormat = _mesa_get_srgb_format_linear(texFormat);
589 format = st_mesa_format_to_pipe_format(st_context(ctx), linearFormat);
590 }
591
592 valid = screen->is_format_supported(screen, format,
593 PIPE_TEXTURE_2D,
594 stObj->pt->nr_samples, bindings);
595 if (!valid) {
596 st_fbo_invalid("Invalid format");
597 }
598
599 return valid;
600 }
601
602
603 /**
604 * Check that the framebuffer configuration is valid in terms of what
605 * the driver can support.
606 *
607 * For Gallium we only supports combined Z+stencil, not separate buffers.
608 */
609 static void
610 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
611 {
612 struct st_context *st = st_context(ctx);
613 struct pipe_screen *screen = st->pipe->screen;
614 const struct gl_renderbuffer_attachment *depth =
615 &fb->Attachment[BUFFER_DEPTH];
616 const struct gl_renderbuffer_attachment *stencil =
617 &fb->Attachment[BUFFER_STENCIL];
618 GLuint i;
619 enum pipe_format first_format = PIPE_FORMAT_NONE;
620 boolean mixed_formats =
621 screen->get_param(screen, PIPE_CAP_MIXED_COLORBUFFER_FORMATS) != 0;
622
623 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
624 st_fbo_invalid("Different Depth/Stencil buffer formats");
625 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
626 return;
627 }
628 if (depth->Type == GL_RENDERBUFFER_EXT &&
629 stencil->Type == GL_RENDERBUFFER_EXT &&
630 depth->Renderbuffer != stencil->Renderbuffer) {
631 st_fbo_invalid("Separate Depth/Stencil buffers");
632 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
633 return;
634 }
635 if (depth->Type == GL_TEXTURE &&
636 stencil->Type == GL_TEXTURE &&
637 depth->Texture != stencil->Texture) {
638 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
639 st_fbo_invalid("Different Depth/Stencil textures");
640 return;
641 }
642
643 if (!st_validate_attachment(ctx,
644 screen,
645 depth,
646 PIPE_BIND_DEPTH_STENCIL)) {
647 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
648 return;
649 }
650 if (!st_validate_attachment(ctx,
651 screen,
652 stencil,
653 PIPE_BIND_DEPTH_STENCIL)) {
654 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
655 return;
656 }
657 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
658 struct gl_renderbuffer_attachment *att =
659 &fb->Attachment[BUFFER_COLOR0 + i];
660 enum pipe_format format;
661
662 if (!st_validate_attachment(ctx,
663 screen,
664 att,
665 PIPE_BIND_RENDER_TARGET)) {
666 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
667 return;
668 }
669
670 if (!mixed_formats) {
671 /* Disallow mixed formats. */
672 if (att->Type != GL_NONE) {
673 format = st_renderbuffer(att->Renderbuffer)->surface->format;
674 } else {
675 continue;
676 }
677
678 if (first_format == PIPE_FORMAT_NONE) {
679 first_format = format;
680 } else if (format != first_format) {
681 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
682 st_fbo_invalid("Mixed color formats");
683 return;
684 }
685 }
686 }
687 }
688
689
690 /**
691 * Called via glDrawBuffer.
692 */
693 static void
694 st_DrawBuffers(struct gl_context *ctx, GLsizei count, const GLenum *buffers)
695 {
696 struct st_context *st = st_context(ctx);
697 struct gl_framebuffer *fb = ctx->DrawBuffer;
698 GLuint i;
699
700 (void) count;
701 (void) buffers;
702
703 /* add the renderbuffers on demand */
704 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
705 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
706
707 if (idx >= 0) {
708 st_manager_add_color_renderbuffer(st, fb, idx);
709 }
710 }
711 }
712
713
714 /**
715 * Called via glReadBuffer.
716 */
717 static void
718 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
719 {
720 struct st_context *st = st_context(ctx);
721 struct gl_framebuffer *fb = ctx->ReadBuffer;
722
723 (void) buffer;
724
725 /* add the renderbuffer on demand */
726 if (fb->_ColorReadBufferIndex >= 0)
727 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
728 }
729
730
731
732 /**
733 * Called via ctx->Driver.MapRenderbuffer.
734 */
735 static void
736 st_MapRenderbuffer(struct gl_context *ctx,
737 struct gl_renderbuffer *rb,
738 GLuint x, GLuint y, GLuint w, GLuint h,
739 GLbitfield mode,
740 GLubyte **mapOut, GLint *rowStrideOut)
741 {
742 struct st_context *st = st_context(ctx);
743 struct st_renderbuffer *strb = st_renderbuffer(rb);
744 struct pipe_context *pipe = st->pipe;
745 const GLboolean invert = rb->Name == 0;
746 unsigned usage;
747 GLuint y2;
748 GLubyte *map;
749
750 if (strb->software) {
751 /* software-allocated renderbuffer (probably an accum buffer) */
752 if (strb->data) {
753 GLint bpp = _mesa_get_format_bytes(strb->Base.Format);
754 GLint stride = _mesa_format_row_stride(strb->Base.Format,
755 strb->Base.Width);
756 *mapOut = (GLubyte *) strb->data + y * stride + x * bpp;
757 *rowStrideOut = stride;
758 }
759 else {
760 *mapOut = NULL;
761 *rowStrideOut = 0;
762 }
763 return;
764 }
765
766 usage = 0x0;
767 if (mode & GL_MAP_READ_BIT)
768 usage |= PIPE_TRANSFER_READ;
769 if (mode & GL_MAP_WRITE_BIT)
770 usage |= PIPE_TRANSFER_WRITE;
771 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
772 usage |= PIPE_TRANSFER_DISCARD_RANGE;
773
774 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
775 * 'invert' will be true for window-system buffers and false for
776 * user-allocated renderbuffers and textures.
777 */
778 if (invert)
779 y2 = strb->Base.Height - y - h;
780 else
781 y2 = y;
782
783 map = pipe_transfer_map(pipe,
784 strb->texture,
785 strb->surface->u.tex.level,
786 strb->surface->u.tex.first_layer,
787 usage, x, y2, w, h, &strb->transfer);
788 if (map) {
789 if (invert) {
790 *rowStrideOut = -(int) strb->transfer->stride;
791 map += (h - 1) * strb->transfer->stride;
792 }
793 else {
794 *rowStrideOut = strb->transfer->stride;
795 }
796 *mapOut = map;
797 }
798 else {
799 *mapOut = NULL;
800 *rowStrideOut = 0;
801 }
802 }
803
804
805 /**
806 * Called via ctx->Driver.UnmapRenderbuffer.
807 */
808 static void
809 st_UnmapRenderbuffer(struct gl_context *ctx,
810 struct gl_renderbuffer *rb)
811 {
812 struct st_context *st = st_context(ctx);
813 struct st_renderbuffer *strb = st_renderbuffer(rb);
814 struct pipe_context *pipe = st->pipe;
815
816 if (strb->software) {
817 /* software-allocated renderbuffer (probably an accum buffer) */
818 return;
819 }
820
821 pipe_transfer_unmap(pipe, strb->transfer);
822 strb->transfer = NULL;
823 }
824
825
826
827 void st_init_fbo_functions(struct dd_function_table *functions)
828 {
829 functions->NewFramebuffer = st_new_framebuffer;
830 functions->NewRenderbuffer = st_new_renderbuffer;
831 functions->FramebufferRenderbuffer = _mesa_FramebufferRenderbuffer_sw;
832 functions->RenderTexture = st_render_texture;
833 functions->FinishRenderTexture = st_finish_render_texture;
834 functions->ValidateFramebuffer = st_validate_framebuffer;
835
836 functions->DrawBuffers = st_DrawBuffers;
837 functions->ReadBuffer = st_ReadBuffer;
838
839 functions->MapRenderbuffer = st_MapRenderbuffer;
840 functions->UnmapRenderbuffer = st_UnmapRenderbuffer;
841 }
842
843