mesa/st: wire up DiscardFramebuffer
[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, 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_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, start_storage;
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 start_storage = 2;
168 } else {
169 start = rb->NumSamples;
170 start_storage = rb->NumStorageSamples;
171 }
172
173 if (ctx->Extensions.AMD_framebuffer_multisample_advanced) {
174 if (rb->_BaseFormat == GL_DEPTH_COMPONENT ||
175 rb->_BaseFormat == GL_DEPTH_STENCIL ||
176 rb->_BaseFormat == GL_STENCIL_INDEX) {
177 /* Find a supported depth-stencil format. */
178 for (unsigned samples = start;
179 samples <= ctx->Const.MaxDepthStencilFramebufferSamples;
180 samples++) {
181 format = st_choose_renderbuffer_format(st, internalFormat,
182 samples, samples);
183
184 if (format != PIPE_FORMAT_NONE) {
185 rb->NumSamples = samples;
186 rb->NumStorageSamples = samples;
187 break;
188 }
189 }
190 } else {
191 /* Find a supported color format, samples >= storage_samples. */
192 for (unsigned storage_samples = start_storage;
193 storage_samples <= ctx->Const.MaxColorFramebufferStorageSamples;
194 storage_samples++) {
195 for (unsigned samples = MAX2(start, storage_samples);
196 samples <= ctx->Const.MaxColorFramebufferSamples;
197 samples++) {
198 format = st_choose_renderbuffer_format(st, internalFormat,
199 samples,
200 storage_samples);
201
202 if (format != PIPE_FORMAT_NONE) {
203 rb->NumSamples = samples;
204 rb->NumStorageSamples = storage_samples;
205 goto found;
206 }
207 }
208 }
209 found:;
210 }
211 } else {
212 for (unsigned samples = start; samples <= ctx->Const.MaxSamples;
213 samples++) {
214 format = st_choose_renderbuffer_format(st, internalFormat,
215 samples, samples);
216
217 if (format != PIPE_FORMAT_NONE) {
218 rb->NumSamples = samples;
219 rb->NumStorageSamples = samples;
220 break;
221 }
222 }
223 }
224 } else {
225 format = st_choose_renderbuffer_format(st, internalFormat, 0, 0);
226 }
227
228 /* Not setting gl_renderbuffer::Format here will cause
229 * FRAMEBUFFER_UNSUPPORTED and ValidateFramebuffer will not be called.
230 */
231 if (format == PIPE_FORMAT_NONE) {
232 return GL_TRUE;
233 }
234
235 strb->Base.Format = st_pipe_format_to_mesa_format(format);
236
237 if (width == 0 || height == 0) {
238 /* if size is zero, nothing to allocate */
239 return GL_TRUE;
240 }
241
242 /* Setup new texture template.
243 */
244 memset(&templ, 0, sizeof(templ));
245 templ.target = st->internal_target;
246 templ.format = format;
247 templ.width0 = width;
248 templ.height0 = height;
249 templ.depth0 = 1;
250 templ.array_size = 1;
251 templ.nr_samples = rb->NumSamples;
252 templ.nr_storage_samples = rb->NumStorageSamples;
253
254 if (util_format_is_depth_or_stencil(format)) {
255 templ.bind = PIPE_BIND_DEPTH_STENCIL;
256 }
257 else if (strb->Base.Name != 0) {
258 /* this is a user-created renderbuffer */
259 templ.bind = PIPE_BIND_RENDER_TARGET;
260 }
261 else {
262 /* this is a window-system buffer */
263 templ.bind = (PIPE_BIND_DISPLAY_TARGET |
264 PIPE_BIND_RENDER_TARGET);
265 }
266
267 strb->texture = screen->resource_create(screen, &templ);
268
269 if (!strb->texture)
270 return FALSE;
271
272 st_update_renderbuffer_surface(st, strb);
273 return strb->surface != NULL;
274 }
275
276
277 /**
278 * gl_renderbuffer::Delete()
279 */
280 static void
281 st_renderbuffer_delete(struct gl_context *ctx, struct gl_renderbuffer *rb)
282 {
283 struct st_renderbuffer *strb = st_renderbuffer(rb);
284 if (ctx) {
285 struct st_context *st = st_context(ctx);
286 pipe_surface_release(st->pipe, &strb->surface_srgb);
287 pipe_surface_release(st->pipe, &strb->surface_linear);
288 } else {
289 pipe_surface_release_no_context(&strb->surface_srgb);
290 pipe_surface_release_no_context(&strb->surface_linear);
291 }
292 strb->surface = NULL;
293 pipe_resource_reference(&strb->texture, NULL);
294 free(strb->data);
295 _mesa_delete_renderbuffer(ctx, rb);
296 }
297
298
299 /**
300 * Called via ctx->Driver.NewRenderbuffer()
301 */
302 static struct gl_renderbuffer *
303 st_new_renderbuffer(struct gl_context *ctx, GLuint name)
304 {
305 struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
306 if (strb) {
307 assert(name != 0);
308 _mesa_init_renderbuffer(&strb->Base, name);
309 strb->Base.Delete = st_renderbuffer_delete;
310 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
311 return &strb->Base;
312 }
313 return NULL;
314 }
315
316
317 /**
318 * Allocate a renderbuffer for an on-screen window (not a user-created
319 * renderbuffer). The window system code determines the format.
320 */
321 struct gl_renderbuffer *
322 st_new_renderbuffer_fb(enum pipe_format format, unsigned samples, boolean sw)
323 {
324 struct st_renderbuffer *strb;
325
326 strb = ST_CALLOC_STRUCT(st_renderbuffer);
327 if (!strb) {
328 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
329 return NULL;
330 }
331
332 _mesa_init_renderbuffer(&strb->Base, 0);
333 strb->Base.ClassID = 0x4242; /* just a unique value */
334 strb->Base.NumSamples = samples;
335 strb->Base.NumStorageSamples = samples;
336 strb->Base.Format = st_pipe_format_to_mesa_format(format);
337 strb->Base._BaseFormat = _mesa_get_format_base_format(strb->Base.Format);
338 strb->software = sw;
339
340 switch (format) {
341 case PIPE_FORMAT_B10G10R10A2_UNORM:
342 case PIPE_FORMAT_R10G10B10A2_UNORM:
343 strb->Base.InternalFormat = GL_RGB10_A2;
344 break;
345 case PIPE_FORMAT_R10G10B10X2_UNORM:
346 case PIPE_FORMAT_B10G10R10X2_UNORM:
347 strb->Base.InternalFormat = GL_RGB10;
348 break;
349 case PIPE_FORMAT_R8G8B8A8_UNORM:
350 case PIPE_FORMAT_B8G8R8A8_UNORM:
351 case PIPE_FORMAT_A8R8G8B8_UNORM:
352 strb->Base.InternalFormat = GL_RGBA8;
353 break;
354 case PIPE_FORMAT_R8G8B8X8_UNORM:
355 case PIPE_FORMAT_B8G8R8X8_UNORM:
356 case PIPE_FORMAT_X8R8G8B8_UNORM:
357 strb->Base.InternalFormat = GL_RGB8;
358 break;
359 case PIPE_FORMAT_R8G8B8A8_SRGB:
360 case PIPE_FORMAT_B8G8R8A8_SRGB:
361 case PIPE_FORMAT_A8R8G8B8_SRGB:
362 strb->Base.InternalFormat = GL_SRGB8_ALPHA8;
363 break;
364 case PIPE_FORMAT_R8G8B8X8_SRGB:
365 case PIPE_FORMAT_B8G8R8X8_SRGB:
366 case PIPE_FORMAT_X8R8G8B8_SRGB:
367 strb->Base.InternalFormat = GL_SRGB8;
368 break;
369 case PIPE_FORMAT_B5G5R5A1_UNORM:
370 strb->Base.InternalFormat = GL_RGB5_A1;
371 break;
372 case PIPE_FORMAT_B4G4R4A4_UNORM:
373 strb->Base.InternalFormat = GL_RGBA4;
374 break;
375 case PIPE_FORMAT_B5G6R5_UNORM:
376 strb->Base.InternalFormat = GL_RGB565;
377 break;
378 case PIPE_FORMAT_Z16_UNORM:
379 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
380 break;
381 case PIPE_FORMAT_Z32_UNORM:
382 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
383 break;
384 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
385 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
386 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
387 break;
388 case PIPE_FORMAT_Z24X8_UNORM:
389 case PIPE_FORMAT_X8Z24_UNORM:
390 strb->Base.InternalFormat = GL_DEPTH_COMPONENT24;
391 break;
392 case PIPE_FORMAT_S8_UINT:
393 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
394 break;
395 case PIPE_FORMAT_R16G16B16A16_SNORM:
396 /* accum buffer */
397 strb->Base.InternalFormat = GL_RGBA16_SNORM;
398 break;
399 case PIPE_FORMAT_R16G16B16A16_UNORM:
400 strb->Base.InternalFormat = GL_RGBA16;
401 break;
402 case PIPE_FORMAT_R8_UNORM:
403 strb->Base.InternalFormat = GL_R8;
404 break;
405 case PIPE_FORMAT_R8G8_UNORM:
406 strb->Base.InternalFormat = GL_RG8;
407 break;
408 case PIPE_FORMAT_R16_UNORM:
409 strb->Base.InternalFormat = GL_R16;
410 break;
411 case PIPE_FORMAT_R16G16_UNORM:
412 strb->Base.InternalFormat = GL_RG16;
413 break;
414 case PIPE_FORMAT_R32G32B32A32_FLOAT:
415 strb->Base.InternalFormat = GL_RGBA32F;
416 break;
417 case PIPE_FORMAT_R16G16B16A16_FLOAT:
418 strb->Base.InternalFormat = GL_RGBA16F;
419 break;
420 default:
421 _mesa_problem(NULL,
422 "Unexpected format %s in st_new_renderbuffer_fb",
423 util_format_name(format));
424 free(strb);
425 return NULL;
426 }
427
428 /* st-specific methods */
429 strb->Base.Delete = st_renderbuffer_delete;
430 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
431
432 /* surface is allocated in st_renderbuffer_alloc_storage() */
433 strb->surface = NULL;
434
435 return &strb->Base;
436 }
437
438
439 /**
440 * Create or update the pipe_surface of a FBO renderbuffer.
441 * This is usually called after st_finalize_texture.
442 */
443 void
444 st_update_renderbuffer_surface(struct st_context *st,
445 struct st_renderbuffer *strb)
446 {
447 struct pipe_context *pipe = st->pipe;
448 struct pipe_resource *resource = strb->texture;
449 const struct st_texture_object *stTexObj = NULL;
450 unsigned rtt_width = strb->Base.Width;
451 unsigned rtt_height = strb->Base.Height;
452 unsigned rtt_depth = strb->Base.Depth;
453
454 /*
455 * For winsys fbo, it is possible that the renderbuffer is sRGB-capable but
456 * the format of strb->texture is linear (because we have no control over
457 * the format). Check strb->Base.Format instead of strb->texture->format
458 * to determine if the rb is sRGB-capable.
459 */
460 boolean enable_srgb = st->ctx->Color.sRGBEnabled &&
461 _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB;
462 enum pipe_format format = resource->format;
463
464 if (strb->is_rtt) {
465 stTexObj = st_texture_object(strb->Base.TexImage->TexObject);
466 if (stTexObj->surface_based)
467 format = stTexObj->surface_format;
468 }
469
470 format = enable_srgb ? util_format_srgb(format) : util_format_linear(format);
471
472 if (resource->target == PIPE_TEXTURE_1D_ARRAY) {
473 rtt_depth = rtt_height;
474 rtt_height = 1;
475 }
476
477 /* find matching mipmap level size */
478 unsigned level;
479 for (level = 0; level <= resource->last_level; level++) {
480 if (u_minify(resource->width0, level) == rtt_width &&
481 u_minify(resource->height0, level) == rtt_height &&
482 (resource->target != PIPE_TEXTURE_3D ||
483 u_minify(resource->depth0, level) == rtt_depth)) {
484 break;
485 }
486 }
487 assert(level <= resource->last_level);
488
489 /* determine the layer bounds */
490 unsigned first_layer, last_layer;
491 if (strb->rtt_layered) {
492 first_layer = 0;
493 last_layer = util_max_layer(strb->texture, level);
494 }
495 else {
496 first_layer =
497 last_layer = strb->rtt_face + strb->rtt_slice;
498 }
499
500 /* Adjust for texture views */
501 if (strb->is_rtt && resource->array_size > 1 &&
502 stTexObj->base.Immutable) {
503 const struct gl_texture_object *tex = &stTexObj->base;
504 first_layer += tex->MinLayer;
505 if (!strb->rtt_layered)
506 last_layer += tex->MinLayer;
507 else
508 last_layer = MIN2(first_layer + tex->NumLayers - 1, last_layer);
509 }
510
511 struct pipe_surface **psurf =
512 enable_srgb ? &strb->surface_srgb : &strb->surface_linear;
513 struct pipe_surface *surf = *psurf;
514
515 if (!surf ||
516 surf->texture->nr_samples != strb->Base.NumSamples ||
517 surf->texture->nr_storage_samples != strb->Base.NumStorageSamples ||
518 surf->format != format ||
519 surf->texture != resource ||
520 surf->width != rtt_width ||
521 surf->height != rtt_height ||
522 surf->nr_samples != strb->rtt_nr_samples ||
523 surf->u.tex.level != level ||
524 surf->u.tex.first_layer != first_layer ||
525 surf->u.tex.last_layer != last_layer) {
526 /* create a new pipe_surface */
527 struct pipe_surface surf_tmpl;
528 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
529 surf_tmpl.format = format;
530 surf_tmpl.nr_samples = strb->rtt_nr_samples;
531 surf_tmpl.u.tex.level = level;
532 surf_tmpl.u.tex.first_layer = first_layer;
533 surf_tmpl.u.tex.last_layer = last_layer;
534
535 pipe_surface_release(pipe, psurf);
536
537 *psurf = pipe->create_surface(pipe, resource, &surf_tmpl);
538 }
539 strb->surface = *psurf;
540 }
541
542
543 /**
544 * Return the pipe_resource which stores a particular texture image.
545 */
546 static struct pipe_resource *
547 get_teximage_resource(struct gl_texture_object *texObj,
548 unsigned face, unsigned level)
549 {
550 struct st_texture_image *stImg =
551 st_texture_image(texObj->Image[face][level]);
552
553 return stImg->pt;
554 }
555
556
557 /**
558 * Called by ctx->Driver.RenderTexture
559 */
560 static void
561 st_render_texture(struct gl_context *ctx,
562 struct gl_framebuffer *fb,
563 struct gl_renderbuffer_attachment *att)
564 {
565 struct st_context *st = st_context(ctx);
566 struct gl_renderbuffer *rb = att->Renderbuffer;
567 struct st_renderbuffer *strb = st_renderbuffer(rb);
568 struct pipe_resource *pt;
569
570 pt = get_teximage_resource(att->Texture,
571 att->CubeMapFace,
572 att->TextureLevel);
573 assert(pt);
574
575 /* point renderbuffer at texobject */
576 strb->is_rtt = TRUE;
577 strb->rtt_face = att->CubeMapFace;
578 strb->rtt_slice = att->Zoffset;
579 strb->rtt_layered = att->Layered;
580 strb->rtt_nr_samples = att->NumSamples;
581 pipe_resource_reference(&strb->texture, pt);
582
583 st_update_renderbuffer_surface(st, strb);
584
585 /* Invalidate buffer state so that the pipe's framebuffer state
586 * gets updated.
587 * That's where the new renderbuffer (which we just created) gets
588 * passed to the pipe as a (color/depth) render target.
589 */
590 st_invalidate_buffers(st);
591
592
593 /* Need to trigger a call to update_framebuffer() since we just
594 * attached a new renderbuffer.
595 */
596 ctx->NewState |= _NEW_BUFFERS;
597 }
598
599
600 /**
601 * Called via ctx->Driver.FinishRenderTexture.
602 */
603 static void
604 st_finish_render_texture(struct gl_context *ctx, struct gl_renderbuffer *rb)
605 {
606 struct st_context *st = st_context(ctx);
607 struct st_renderbuffer *strb = st_renderbuffer(rb);
608
609 if (!strb)
610 return;
611
612 strb->is_rtt = FALSE;
613
614 /* restore previous framebuffer state */
615 st_invalidate_buffers(st);
616 }
617
618
619 /** Debug helper */
620 static void
621 st_fbo_invalid(const char *reason)
622 {
623 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
624 _mesa_debug(NULL, "Invalid FBO: %s\n", reason);
625 }
626 }
627
628
629 /**
630 * Validate a renderbuffer attachment for a particular set of bindings.
631 */
632 static GLboolean
633 st_validate_attachment(struct gl_context *ctx,
634 struct pipe_screen *screen,
635 const struct gl_renderbuffer_attachment *att,
636 unsigned bindings)
637 {
638 const struct st_texture_object *stObj = st_texture_object(att->Texture);
639 enum pipe_format format;
640 mesa_format texFormat;
641 GLboolean valid;
642
643 /* Sanity check: we must be binding the surface as a (color) render target
644 * or depth/stencil target.
645 */
646 assert(bindings == PIPE_BIND_RENDER_TARGET ||
647 bindings == PIPE_BIND_DEPTH_STENCIL);
648
649 /* Only validate texture attachments for now, since
650 * st_renderbuffer_alloc_storage makes sure that
651 * the format is supported.
652 */
653 if (att->Type != GL_TEXTURE)
654 return GL_TRUE;
655
656 if (!stObj || !stObj->pt)
657 return GL_FALSE;
658
659 format = stObj->pt->format;
660 texFormat = att->Renderbuffer->TexImage->TexFormat;
661
662 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
663 * check for linear format support instead.
664 * Later when we create a surface, we change the format to a linear one. */
665 if (!ctx->Extensions.EXT_sRGB &&
666 _mesa_get_format_color_encoding(texFormat) == GL_SRGB) {
667 const mesa_format linearFormat = _mesa_get_srgb_format_linear(texFormat);
668 format = st_mesa_format_to_pipe_format(st_context(ctx), linearFormat);
669 }
670
671 valid = screen->is_format_supported(screen, format,
672 PIPE_TEXTURE_2D,
673 stObj->pt->nr_samples,
674 stObj->pt->nr_storage_samples,
675 bindings);
676 if (!valid) {
677 st_fbo_invalid("Invalid format");
678 }
679
680 return valid;
681 }
682
683
684 /**
685 * Check that the framebuffer configuration is valid in terms of what
686 * the driver can support.
687 *
688 * For Gallium we only supports combined Z+stencil, not separate buffers.
689 */
690 static void
691 st_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
692 {
693 struct st_context *st = st_context(ctx);
694 struct pipe_screen *screen = st->pipe->screen;
695 const struct gl_renderbuffer_attachment *depth =
696 &fb->Attachment[BUFFER_DEPTH];
697 const struct gl_renderbuffer_attachment *stencil =
698 &fb->Attachment[BUFFER_STENCIL];
699 GLuint i;
700 enum pipe_format first_format = PIPE_FORMAT_NONE;
701 boolean mixed_formats =
702 screen->get_param(screen, PIPE_CAP_MIXED_COLORBUFFER_FORMATS) != 0;
703
704 if (depth->Type && stencil->Type && depth->Type != stencil->Type) {
705 st_fbo_invalid("Different Depth/Stencil buffer formats");
706 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
707 return;
708 }
709 if (depth->Type == GL_RENDERBUFFER_EXT &&
710 stencil->Type == GL_RENDERBUFFER_EXT &&
711 depth->Renderbuffer != stencil->Renderbuffer) {
712 st_fbo_invalid("Separate Depth/Stencil buffers");
713 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
714 return;
715 }
716 if (depth->Type == GL_TEXTURE &&
717 stencil->Type == GL_TEXTURE &&
718 depth->Texture != stencil->Texture) {
719 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
720 st_fbo_invalid("Different Depth/Stencil textures");
721 return;
722 }
723
724 if (!st_validate_attachment(ctx, screen, depth, PIPE_BIND_DEPTH_STENCIL)) {
725 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
726 st_fbo_invalid("Invalid depth attachment");
727 return;
728 }
729 if (!st_validate_attachment(ctx, screen, stencil, PIPE_BIND_DEPTH_STENCIL)) {
730 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
731 st_fbo_invalid("Invalid stencil attachment");
732 return;
733 }
734 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
735 struct gl_renderbuffer_attachment *att =
736 &fb->Attachment[BUFFER_COLOR0 + i];
737 enum pipe_format format;
738
739 if (!st_validate_attachment(ctx, screen, att, PIPE_BIND_RENDER_TARGET)) {
740 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
741 st_fbo_invalid("Invalid color attachment");
742 return;
743 }
744
745 if (!mixed_formats) {
746 /* Disallow mixed formats. */
747 if (att->Type != GL_NONE) {
748 format = st_renderbuffer(att->Renderbuffer)->surface->format;
749 } else {
750 continue;
751 }
752
753 if (first_format == PIPE_FORMAT_NONE) {
754 first_format = format;
755 } else if (format != first_format) {
756 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
757 st_fbo_invalid("Mixed color formats");
758 return;
759 }
760 }
761 }
762 }
763
764
765 /**
766 * Called by ctx->Driver.DiscardFramebuffer
767 */
768 static void
769 st_discard_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
770 struct gl_renderbuffer_attachment *att)
771 {
772 struct st_context *st = st_context(ctx);
773 struct pipe_resource *prsc;
774
775 if (!att->Renderbuffer)
776 return;
777
778 prsc = st_renderbuffer(att->Renderbuffer)->surface->texture;
779
780 /* using invalidate_resource will only work for simple 2D resources */
781 if (prsc->depth0 != 1 || prsc->array_size != 1 || prsc->last_level != 0)
782 return;
783
784 if (st->pipe->invalidate_resource)
785 st->pipe->invalidate_resource(st->pipe, prsc);
786 }
787
788
789 /**
790 * Called via glDrawBuffer. We only provide this driver function so that we
791 * can check if we need to allocate a new renderbuffer. Specifically, we
792 * don't usually allocate a front color buffer when using a double-buffered
793 * visual. But if the app calls glDrawBuffer(GL_FRONT) we need to allocate
794 * that buffer. Note, this is only for window system buffers, not user-
795 * created FBOs.
796 */
797 static void
798 st_DrawBufferAllocate(struct gl_context *ctx)
799 {
800 struct st_context *st = st_context(ctx);
801 struct gl_framebuffer *fb = ctx->DrawBuffer;
802
803 if (_mesa_is_winsys_fbo(fb)) {
804 GLuint i;
805 /* add the renderbuffers on demand */
806 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
807 gl_buffer_index idx = fb->_ColorDrawBufferIndexes[i];
808
809 if (idx != BUFFER_NONE) {
810 st_manager_add_color_renderbuffer(st, fb, idx);
811 }
812 }
813 }
814 }
815
816
817 /**
818 * Called via glReadBuffer. As with st_DrawBufferAllocate, we use this
819 * function to check if we need to allocate a renderbuffer on demand.
820 */
821 static void
822 st_ReadBuffer(struct gl_context *ctx, GLenum buffer)
823 {
824 struct st_context *st = st_context(ctx);
825 struct gl_framebuffer *fb = ctx->ReadBuffer;
826
827 (void) buffer;
828
829 /* Check if we need to allocate a front color buffer.
830 * Front buffers are often allocated on demand (other color buffers are
831 * always allocated in advance).
832 */
833 if ((fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT ||
834 fb->_ColorReadBufferIndex == BUFFER_FRONT_RIGHT) &&
835 fb->Attachment[fb->_ColorReadBufferIndex].Type == GL_NONE) {
836 assert(_mesa_is_winsys_fbo(fb));
837 /* add the buffer */
838 st_manager_add_color_renderbuffer(st, fb, fb->_ColorReadBufferIndex);
839 _mesa_update_state(ctx);
840 st_validate_state(st, ST_PIPELINE_UPDATE_FRAMEBUFFER);
841 }
842 }
843
844
845
846 /**
847 * Called via ctx->Driver.MapRenderbuffer.
848 */
849 static void
850 st_MapRenderbuffer(struct gl_context *ctx,
851 struct gl_renderbuffer *rb,
852 GLuint x, GLuint y, GLuint w, GLuint h,
853 GLbitfield mode,
854 GLubyte **mapOut, GLint *rowStrideOut,
855 bool flip_y)
856 {
857 struct st_context *st = st_context(ctx);
858 struct st_renderbuffer *strb = st_renderbuffer(rb);
859 struct pipe_context *pipe = st->pipe;
860 const GLboolean invert = rb->Name == 0;
861 GLuint y2;
862 GLubyte *map;
863
864 /* driver does not support GL_FRAMEBUFFER_FLIP_Y_MESA */
865 assert((rb->Name == 0) == flip_y);
866
867 if (strb->software) {
868 /* software-allocated renderbuffer (probably an accum buffer) */
869 if (strb->data) {
870 GLint bpp = _mesa_get_format_bytes(strb->Base.Format);
871 GLint stride = _mesa_format_row_stride(strb->Base.Format,
872 strb->Base.Width);
873 *mapOut = (GLubyte *) strb->data + y * stride + x * bpp;
874 *rowStrideOut = stride;
875 }
876 else {
877 *mapOut = NULL;
878 *rowStrideOut = 0;
879 }
880 return;
881 }
882
883 /* Check for unexpected flags */
884 assert((mode & ~(GL_MAP_READ_BIT |
885 GL_MAP_WRITE_BIT |
886 GL_MAP_INVALIDATE_RANGE_BIT)) == 0);
887
888 const enum pipe_transfer_usage transfer_flags =
889 st_access_flags_to_transfer_flags(mode, false);
890
891 /* Note: y=0=bottom of buffer while y2=0=top of buffer.
892 * 'invert' will be true for window-system buffers and false for
893 * user-allocated renderbuffers and textures.
894 */
895 if (invert)
896 y2 = strb->Base.Height - y - h;
897 else
898 y2 = y;
899
900 map = pipe_transfer_map(pipe,
901 strb->texture,
902 strb->surface->u.tex.level,
903 strb->surface->u.tex.first_layer,
904 transfer_flags, x, y2, w, h, &strb->transfer);
905 if (map) {
906 if (invert) {
907 *rowStrideOut = -(int) strb->transfer->stride;
908 map += (h - 1) * strb->transfer->stride;
909 }
910 else {
911 *rowStrideOut = strb->transfer->stride;
912 }
913 *mapOut = map;
914 }
915 else {
916 *mapOut = NULL;
917 *rowStrideOut = 0;
918 }
919 }
920
921
922 /**
923 * Called via ctx->Driver.UnmapRenderbuffer.
924 */
925 static void
926 st_UnmapRenderbuffer(struct gl_context *ctx,
927 struct gl_renderbuffer *rb)
928 {
929 struct st_context *st = st_context(ctx);
930 struct st_renderbuffer *strb = st_renderbuffer(rb);
931 struct pipe_context *pipe = st->pipe;
932
933 if (strb->software) {
934 /* software-allocated renderbuffer (probably an accum buffer) */
935 return;
936 }
937
938 pipe_transfer_unmap(pipe, strb->transfer);
939 strb->transfer = NULL;
940 }
941
942
943 /**
944 * Called via ctx->Driver.EvaluateDepthValues.
945 */
946 static void
947 st_EvaluateDepthValues(struct gl_context *ctx)
948 {
949 struct st_context *st = st_context(ctx);
950
951 st_validate_state(st, ST_PIPELINE_UPDATE_FRAMEBUFFER);
952
953 st->pipe->evaluate_depth_buffer(st->pipe);
954 }
955
956
957 void
958 st_init_fbo_functions(struct dd_function_table *functions)
959 {
960 functions->NewFramebuffer = _mesa_new_framebuffer;
961 functions->NewRenderbuffer = st_new_renderbuffer;
962 functions->FramebufferRenderbuffer = _mesa_FramebufferRenderbuffer_sw;
963 functions->RenderTexture = st_render_texture;
964 functions->FinishRenderTexture = st_finish_render_texture;
965 functions->ValidateFramebuffer = st_validate_framebuffer;
966 functions->DiscardFramebuffer = st_discard_framebuffer;
967
968 functions->DrawBufferAllocate = st_DrawBufferAllocate;
969 functions->ReadBuffer = st_ReadBuffer;
970
971 functions->MapRenderbuffer = st_MapRenderbuffer;
972 functions->UnmapRenderbuffer = st_UnmapRenderbuffer;
973 functions->EvaluateDepthValues = st_EvaluateDepthValues;
974 }