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