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