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