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