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