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