73414fdfa15f97bbd2d2d5c9efa741682c689318
[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 templ.nr_storage_samples = rb->NumSamples;
208
209 if (util_format_is_depth_or_stencil(format)) {
210 templ.bind = PIPE_BIND_DEPTH_STENCIL;
211 }
212 else if (strb->Base.Name != 0) {
213 /* this is a user-created renderbuffer */
214 templ.bind = PIPE_BIND_RENDER_TARGET;
215 }
216 else {
217 /* this is a window-system buffer */
218 templ.bind = (PIPE_BIND_DISPLAY_TARGET |
219 PIPE_BIND_RENDER_TARGET);
220 }
221
222 strb->texture = screen->resource_create(screen, &templ);
223
224 if (!strb->texture)
225 return FALSE;
226
227 st_update_renderbuffer_surface(st, strb);
228 return strb->surface != NULL;
229 }
230
231
232 /**
233 * gl_renderbuffer::Delete()
234 */
235 static void
236 st_renderbuffer_delete(struct gl_context *ctx, struct gl_renderbuffer *rb)
237 {
238 struct st_renderbuffer *strb = st_renderbuffer(rb);
239 if (ctx) {
240 struct st_context *st = st_context(ctx);
241 pipe_surface_release(st->pipe, &strb->surface_srgb);
242 pipe_surface_release(st->pipe, &strb->surface_linear);
243 strb->surface = NULL;
244 }
245 pipe_resource_reference(&strb->texture, NULL);
246 free(strb->data);
247 _mesa_delete_renderbuffer(ctx, rb);
248 }
249
250
251 /**
252 * Called via ctx->Driver.NewRenderbuffer()
253 */
254 static struct gl_renderbuffer *
255 st_new_renderbuffer(struct gl_context *ctx, GLuint name)
256 {
257 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
258 if (strb) {
259 assert(name != 0);
260 _mesa_init_renderbuffer(&strb->Base, name);
261 strb->Base.Delete = st_renderbuffer_delete;
262 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
263 return &strb->Base;
264 }
265 return NULL;
266 }
267
268
269 /**
270 * Allocate a renderbuffer for an on-screen window (not a user-created
271 * renderbuffer). The window system code determines the format.
272 */
273 struct gl_renderbuffer *
274 st_new_renderbuffer_fb(enum pipe_format format, unsigned samples, boolean sw)
275 {
276 struct st_renderbuffer *strb;
277
278 strb = ST_CALLOC_STRUCT(st_renderbuffer);
279 if (!strb) {
280 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
281 return NULL;
282 }
283
284 _mesa_init_renderbuffer(&strb->Base, 0);
285 strb->Base.ClassID = 0x4242; /* just a unique value */
286 strb->Base.NumSamples = samples;
287 strb->Base.Format = st_pipe_format_to_mesa_format(format);
288 strb->Base._BaseFormat = _mesa_get_format_base_format(strb->Base.Format);
289 strb->software = sw;
290
291 switch (format) {
292 case PIPE_FORMAT_B10G10R10A2_UNORM:
293 case PIPE_FORMAT_R10G10B10A2_UNORM:
294 strb->Base.InternalFormat = GL_RGB10_A2;
295 break;
296 case PIPE_FORMAT_R10G10B10X2_UNORM:
297 case PIPE_FORMAT_B10G10R10X2_UNORM:
298 strb->Base.InternalFormat = GL_RGB10;
299 break;
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 const struct st_texture_object *stTexObj = NULL;
401 unsigned rtt_width = strb->Base.Width;
402 unsigned rtt_height = strb->Base.Height;
403 unsigned rtt_depth = strb->Base.Depth;
404
405 /*
406 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
407 * the format of strb->texture is linear (because we have no control over
408 * the format). Check strb->Base.Format instead of strb->texture->format
409 * to determine if the rb is sRGB-capable.
410 */
411 boolean enable_srgb = st->ctx->Color.sRGBEnabled &&
412 _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB;
413 enum pipe_format format = resource->format;
414
415 if (strb->is_rtt) {
416 stTexObj = st_texture_object(strb->Base.TexImage->TexObject);
417 if (stTexObj->surface_based)
418 format = stTexObj->surface_format;
419 }
420
421 format = enable_srgb ? util_format_srgb(format) : util_format_linear(format);
422
423 if (resource->target == PIPE_TEXTURE_1D_ARRAY) {
424 rtt_depth = rtt_height;
425 rtt_height = 1;
426 }
427
428 /* find matching mipmap level size */
429 unsigned level;
430 for (level = 0; level <= resource->last_level; level++) {
431 if (u_minify(resource->width0, level) == rtt_width &&
432 u_minify(resource->height0, level) == rtt_height &&
433 (resource->target != PIPE_TEXTURE_3D ||
434 u_minify(resource->depth0, level) == rtt_depth)) {
435 break;
436 }
437 }
438 assert(level <= resource->last_level);
439
440 /* determine the layer bounds */
441 unsigned first_layer, last_layer;
442 if (strb->rtt_layered) {
443 first_layer = 0;
444 last_layer = util_max_layer(strb->texture, level);
445 }
446 else {
447 first_layer =
448 last_layer = strb->rtt_face + strb->rtt_slice;
449 }
450
451 /* Adjust for texture views */
452 if (strb->is_rtt && resource->array_size > 1 &&
453 stTexObj->base.Immutable) {
454 const struct gl_texture_object *tex = &stTexObj->base;
455 first_layer += tex->MinLayer;
456 if (!strb->rtt_layered)
457 last_layer += tex->MinLayer;
458 else
459 last_layer = MIN2(first_layer + tex->NumLayers - 1, last_layer);
460 }
461
462 struct pipe_surface **psurf =
463 enable_srgb ? &strb->surface_srgb : &strb->surface_linear;
464 struct pipe_surface *surf = *psurf;
465
466 if (!surf ||
467 surf->texture->nr_samples != strb->Base.NumSamples ||
468 surf->format != format ||
469 surf->texture != resource ||
470 surf->width != rtt_width ||
471 surf->height != rtt_height ||
472 surf->u.tex.level != level ||
473 surf->u.tex.first_layer != first_layer ||
474 surf->u.tex.last_layer != last_layer) {
475 /* create a new pipe_surface */
476 struct pipe_surface surf_tmpl;
477 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
478 surf_tmpl.format = format;
479 surf_tmpl.u.tex.level = level;
480 surf_tmpl.u.tex.first_layer = first_layer;
481 surf_tmpl.u.tex.last_layer = last_layer;
482
483 pipe_surface_release(pipe, psurf);
484
485 *psurf = pipe->create_surface(pipe, resource, &surf_tmpl);
486 }
487 strb->surface = *psurf;
488 }
489
490
491 /**
492 * Return the pipe_resource which stores a particular texture image.
493 */
494 static struct pipe_resource *
495 get_teximage_resource(struct gl_texture_object *texObj,
496 unsigned face, unsigned level)
497 {
498 struct st_texture_image *stImg =
499 st_texture_image(texObj->Image[face][level]);
500
501 return stImg->pt;
502 }
503
504
505 /**
506 * Called by ctx->Driver.RenderTexture
507 */
508 static void
509 st_render_texture(struct gl_context *ctx,
510 struct gl_framebuffer *fb,
511 struct gl_renderbuffer_attachment *att)
512 {
513 struct st_context *st = st_context(ctx);
514 struct gl_renderbuffer *rb = att->Renderbuffer;
515 struct st_renderbuffer *strb = st_renderbuffer(rb);
516 struct pipe_resource *pt;
517
518 pt = get_teximage_resource(att->Texture,
519 att->CubeMapFace,
520 att->TextureLevel);
521 assert(pt);
522
523 /* point renderbuffer at texobject */
524 strb->is_rtt = TRUE;
525 strb->rtt_face = att->CubeMapFace;
526 strb->rtt_slice = att->Zoffset;
527 strb->rtt_layered = att->Layered;
528 pipe_resource_reference(&strb->texture, pt);
529
530 st_update_renderbuffer_surface(st, strb);
531
532 /* Invalidate buffer state so that the pipe's framebuffer state
533 * gets updated.
534 * That's where the new renderbuffer (which we just created) gets
535 * passed to the pipe as a (color/depth) render target.
536 */
537 st_invalidate_buffers(st);
538
539
540 /* Need to trigger a call to update_framebuffer() since we just
541 * attached a new renderbuffer.
542 */
543 ctx->NewState |= _NEW_BUFFERS;
544 }
545
546
547 /**
548 * Called via ctx->Driver.FinishRenderTexture.
549 */
550 static void
551 st_finish_render_texture(struct gl_context *ctx, struct gl_renderbuffer *rb)
552 {
553 struct st_context *st = st_context(ctx);
554 struct st_renderbuffer *strb = st_renderbuffer(rb);
555
556 if (!strb)
557 return;
558
559 strb->is_rtt = FALSE;
560
561 /* restore previous framebuffer state */
562 st_invalidate_buffers(st);
563 }
564
565
566 /** Debug helper */
567 static void
568 st_fbo_invalid(const char *reason)
569 {
570 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
571 _mesa_debug(NULL, "Invalid FBO: %s\n", reason);
572 }
573 }
574
575
576 /**
577 * Validate a renderbuffer attachment for a particular set of bindings.
578 */
579 static GLboolean
580 st_validate_attachment(struct gl_context *ctx,
581 struct pipe_screen *screen,
582 const struct gl_renderbuffer_attachment *att,
583 unsigned bindings)
584 {
585 const struct st_texture_object *stObj = st_texture_object(att->Texture);
586 enum pipe_format format;
587 mesa_format texFormat;
588 GLboolean valid;
589
590 /* Sanity check: we must be binding the surface as a (color) render target
591 * or depth/stencil target.
592 */
593 assert(bindings == PIPE_BIND_RENDER_TARGET ||
594 bindings == PIPE_BIND_DEPTH_STENCIL);
595
596 /* Only validate texture attachments for now, since
597 * st_renderbuffer_alloc_storage makes sure that
598 * the format is supported.
599 */
600 if (att->Type != GL_TEXTURE)
601 return GL_TRUE;
602
603 if (!stObj || !stObj->pt)
604 return GL_FALSE;
605
606 format = stObj->pt->format;
607 texFormat = att->Renderbuffer->TexImage->TexFormat;
608
609 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
610 * check for linear format support instead.
611 * Later when we create a surface, we change the format to a linear one. */
612 if (!ctx->Extensions.EXT_framebuffer_sRGB &&
613 _mesa_get_format_color_encoding(texFormat) == GL_SRGB) {
614 const mesa_format linearFormat = _mesa_get_srgb_format_linear(texFormat);
615 format = st_mesa_format_to_pipe_format(st_context(ctx), linearFormat);
616 }
617
618 valid = screen->is_format_supported(screen, format,
619 PIPE_TEXTURE_2D,
620 stObj->pt->nr_samples,
621 stObj->pt->nr_storage_samples,
622 bindings);
623 if (!valid) {
624 st_fbo_invalid("Invalid format");
625 }
626
627 return valid;
628 }
629
630
631 /**
632 * Check that the framebuffer configuration is valid in terms of what
633 * the driver can support.
634 *
635 * For Gallium we only supports combined Z+stencil, not separate buffers.
636 */
637 static void
638 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
639 {
640 struct st_context *st = st_context(ctx);
641 struct pipe_screen *screen = st->pipe->screen;
642 const struct gl_renderbuffer_attachment *depth =
643 &fb->Attachment[BUFFER_DEPTH];
644 const struct gl_renderbuffer_attachment *stencil =
645 &fb->Attachment[BUFFER_STENCIL];
646 GLuint i;
647 enum pipe_format first_format = PIPE_FORMAT_NONE;
648 boolean mixed_formats =
649 screen->get_param(screen, PIPE_CAP_MIXED_COLORBUFFER_FORMATS) != 0;
650
651 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
652 st_fbo_invalid("Different Depth/Stencil buffer formats");
653 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
654 return;
655 }
656 if (depth->Type == GL_RENDERBUFFER_EXT &&
657 stencil->Type == GL_RENDERBUFFER_EXT &&
658 depth->Renderbuffer != stencil->Renderbuffer) {
659 st_fbo_invalid("Separate Depth/Stencil buffers");
660 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
661 return;
662 }
663 if (depth->Type == GL_TEXTURE &&
664 stencil->Type == GL_TEXTURE &&
665 depth->Texture != stencil->Texture) {
666 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
667 st_fbo_invalid("Different Depth/Stencil textures");
668 return;
669 }
670
671 if (!st_validate_attachment(ctx, screen, depth, PIPE_BIND_DEPTH_STENCIL)) {
672 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
673 st_fbo_invalid("Invalid depth attachment");
674 return;
675 }
676 if (!st_validate_attachment(ctx, screen, stencil, PIPE_BIND_DEPTH_STENCIL)) {
677 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
678 st_fbo_invalid("Invalid stencil attachment");
679 return;
680 }
681 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
682 struct gl_renderbuffer_attachment *att =
683 &fb->Attachment[BUFFER_COLOR0 + i];
684 enum pipe_format format;
685
686 if (!st_validate_attachment(ctx, screen, att, PIPE_BIND_RENDER_TARGET)) {
687 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
688 st_fbo_invalid("Invalid color attachment");
689 return;
690 }
691
692 if (!mixed_formats) {
693 /* Disallow mixed formats. */
694 if (att->Type != GL_NONE) {
695 format = st_renderbuffer(att->Renderbuffer)->surface->format;
696 } else {
697 continue;
698 }
699
700 if (first_format == PIPE_FORMAT_NONE) {
701 first_format = format;
702 } else if (format != first_format) {
703 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
704 st_fbo_invalid("Mixed color formats");
705 return;
706 }
707 }
708 }
709 }
710
711
712 /**
713 * Called via glDrawBuffer. We only provide this driver function so that we
714 * can check if we need to allocate a new renderbuffer. Specifically, we
715 * don't usually allocate a front color buffer when using a double-buffered
716 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
717 * that buffer. Note, this is only for window system buffers, not user-
718 * created FBOs.
719 */
720 static void
721 st_DrawBufferAllocate(struct gl_context *ctx)
722 {
723 struct st_context *st = st_context(ctx);
724 struct gl_framebuffer *fb = ctx->DrawBuffer;
725
726 if (_mesa_is_winsys_fbo(fb)) {
727 GLuint i;
728 /* add the renderbuffers on demand */
729 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
730 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
731
732 if (idx != BUFFER_NONE) {
733 st_manager_add_color_renderbuffer(st, fb, idx);
734 }
735 }
736 }
737 }
738
739
740 /**
741 * Called via glReadBuffer. As with st_DrawBufferAllocate, we use this
742 * function to check if we need to allocate a renderbuffer on demand.
743 */
744 static void
745 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
746 {
747 struct st_context *st = st_context(ctx);
748 struct gl_framebuffer *fb = ctx->ReadBuffer;
749
750 (void) buffer;
751
752 /* Check if we need to allocate a front color buffer.
753 * Front buffers are often allocated on demand (other color buffers are
754 * always allocated in advance).
755 */
756 if ((fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT ||
757 fb->_ColorReadBufferIndex == BUFFER_FRONT_RIGHT) &&
758 fb->Attachment[fb->_ColorReadBufferIndex].Type == GL_NONE) {
759 assert(_mesa_is_winsys_fbo(fb));
760 /* add the buffer */
761 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
762 _mesa_update_state(ctx);
763 st_validate_state(st, ST_PIPELINE_UPDATE_FRAMEBUFFER);
764 }
765 }
766
767
768
769 /**
770 * Called via ctx->Driver.MapRenderbuffer.
771 */
772 static void
773 st_MapRenderbuffer(struct gl_context *ctx,
774 struct gl_renderbuffer *rb,
775 GLuint x, GLuint y, GLuint w, GLuint h,
776 GLbitfield mode,
777 GLubyte **mapOut, GLint *rowStrideOut,
778 bool flip_y)
779 {
780 struct st_context *st = st_context(ctx);
781 struct st_renderbuffer *strb = st_renderbuffer(rb);
782 struct pipe_context *pipe = st->pipe;
783 const GLboolean invert = rb->Name == 0;
784 GLuint y2;
785 GLubyte *map;
786
787 /* driver does not support GL_FRAMEBUFFER_FLIP_Y_MESA */
788 assert((rb->Name == 0) == flip_y);
789
790 if (strb->software) {
791 /* software-allocated renderbuffer (probably an accum buffer) */
792 if (strb->data) {
793 GLint bpp = _mesa_get_format_bytes(strb->Base.Format);
794 GLint stride = _mesa_format_row_stride(strb->Base.Format,
795 strb->Base.Width);
796 *mapOut = (GLubyte *) strb->data + y * stride + x * bpp;
797 *rowStrideOut = stride;
798 }
799 else {
800 *mapOut = NULL;
801 *rowStrideOut = 0;
802 }
803 return;
804 }
805
806 /* Check for unexpected flags */
807 assert((mode & ~(GL_MAP_READ_BIT |
808 GL_MAP_WRITE_BIT |
809 GL_MAP_INVALIDATE_RANGE_BIT)) == 0);
810
811 const enum pipe_transfer_usage transfer_flags =
812 st_access_flags_to_transfer_flags(mode, false);
813
814 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
815 * 'invert' will be true for window-system buffers and false for
816 * user-allocated renderbuffers and textures.
817 */
818 if (invert)
819 y2 = strb->Base.Height - y - h;
820 else
821 y2 = y;
822
823 map = pipe_transfer_map(pipe,
824 strb->texture,
825 strb->surface->u.tex.level,
826 strb->surface->u.tex.first_layer,
827 transfer_flags, x, y2, w, h, &strb->transfer);
828 if (map) {
829 if (invert) {
830 *rowStrideOut = -(int) strb->transfer->stride;
831 map += (h - 1) * strb->transfer->stride;
832 }
833 else {
834 *rowStrideOut = strb->transfer->stride;
835 }
836 *mapOut = map;
837 }
838 else {
839 *mapOut = NULL;
840 *rowStrideOut = 0;
841 }
842 }
843
844
845 /**
846 * Called via ctx->Driver.UnmapRenderbuffer.
847 */
848 static void
849 st_UnmapRenderbuffer(struct gl_context *ctx,
850 struct gl_renderbuffer *rb)
851 {
852 struct st_context *st = st_context(ctx);
853 struct st_renderbuffer *strb = st_renderbuffer(rb);
854 struct pipe_context *pipe = st->pipe;
855
856 if (strb->software) {
857 /* software-allocated renderbuffer (probably an accum buffer) */
858 return;
859 }
860
861 pipe_transfer_unmap(pipe, strb->transfer);
862 strb->transfer = NULL;
863 }
864
865
866 /**
867 * Called via ctx->Driver.EvaluateDepthValues.
868 */
869 static void
870 st_EvaluateDepthValues(struct gl_context *ctx)
871 {
872 struct st_context *st = st_context(ctx);
873
874 st_validate_state(st, ST_PIPELINE_UPDATE_FRAMEBUFFER);
875
876 st->pipe->evaluate_depth_buffer(st->pipe);
877 }
878
879
880 void
881 st_init_fbo_functions(struct dd_function_table *functions)
882 {
883 functions->NewFramebuffer = _mesa_new_framebuffer;
884 functions->NewRenderbuffer = st_new_renderbuffer;
885 functions->FramebufferRenderbuffer = _mesa_FramebufferRenderbuffer_sw;
886 functions->RenderTexture = st_render_texture;
887 functions->FinishRenderTexture = st_finish_render_texture;
888 functions->ValidateFramebuffer = st_validate_framebuffer;
889
890 functions->DrawBufferAllocate = st_DrawBufferAllocate;
891 functions->ReadBuffer = st_ReadBuffer;
892
893 functions->MapRenderbuffer = st_MapRenderbuffer;
894 functions->UnmapRenderbuffer = st_UnmapRenderbuffer;
895 functions->EvaluateDepthValues = st_EvaluateDepthValues;
896 }