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