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