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