st/mesa: add support for GL_RGBA + GL_UNSIGNED_INT_8_8_8_8 in st_fast_readpixels
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.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 * glReadPixels interface to pipe
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/bufferobj.h"
38 #include "main/context.h"
39 #include "main/image.h"
40 #include "main/pack.h"
41 #include "main/pbo.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_format.h"
46 #include "util/u_inlines.h"
47 #include "util/u_tile.h"
48
49 #include "st_debug.h"
50 #include "st_context.h"
51 #include "st_atom.h"
52 #include "st_cb_bitmap.h"
53 #include "st_cb_readpixels.h"
54 #include "st_cb_fbo.h"
55
56 /**
57 * Special case for reading stencil buffer.
58 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
59 */
60 void
61 st_read_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
62 GLsizei width, GLsizei height,
63 GLenum format, GLenum type,
64 const struct gl_pixelstore_attrib *packing,
65 GLvoid *pixels)
66 {
67 struct gl_framebuffer *fb = ctx->ReadBuffer;
68 struct pipe_context *pipe = st_context(ctx)->pipe;
69 struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
70 struct pipe_transfer *pt;
71 ubyte *stmap;
72 GLint j;
73
74 if (strb->Base.Wrapped) {
75 strb = st_renderbuffer(strb->Base.Wrapped);
76 }
77
78 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
79 y = ctx->DrawBuffer->Height - y - height;
80 }
81
82 /* Create a read transfer from the renderbuffer's texture */
83
84 pt = pipe_get_transfer(pipe, strb->texture,
85 0, 0,
86 PIPE_TRANSFER_READ,
87 x, y, width, height);
88
89 /* map the stencil buffer */
90 stmap = pipe_transfer_map(pipe, pt);
91
92 /* width should never be > MAX_WIDTH since we did clipping earlier */
93 ASSERT(width <= MAX_WIDTH);
94
95 /* process image row by row */
96 for (j = 0; j < height; j++) {
97 GLvoid *dest;
98 GLstencil sValues[MAX_WIDTH];
99 GLfloat zValues[MAX_WIDTH];
100 GLint srcY;
101
102 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
103 srcY = height - j - 1;
104 }
105 else {
106 srcY = j;
107 }
108
109 /* get stencil (and Z) values */
110 switch (pt->resource->format) {
111 case PIPE_FORMAT_S8_USCALED:
112 {
113 const ubyte *src = stmap + srcY * pt->stride;
114 memcpy(sValues, src, width);
115 }
116 break;
117 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
118 if (format == GL_DEPTH_STENCIL) {
119 const uint *src = (uint *) (stmap + srcY * pt->stride);
120 const GLfloat scale = 1.0f / (0xffffff);
121 GLint k;
122 for (k = 0; k < width; k++) {
123 sValues[k] = src[k] >> 24;
124 zValues[k] = (src[k] & 0xffffff) * scale;
125 }
126 }
127 else {
128 const uint *src = (uint *) (stmap + srcY * pt->stride);
129 GLint k;
130 for (k = 0; k < width; k++) {
131 sValues[k] = src[k] >> 24;
132 }
133 }
134 break;
135 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
136 if (format == GL_DEPTH_STENCIL) {
137 const uint *src = (uint *) (stmap + srcY * pt->stride);
138 const GLfloat scale = 1.0f / (0xffffff);
139 GLint k;
140 for (k = 0; k < width; k++) {
141 sValues[k] = src[k] & 0xff;
142 zValues[k] = (src[k] >> 8) * scale;
143 }
144 }
145 else {
146 const uint *src = (uint *) (stmap + srcY * pt->stride);
147 GLint k;
148 for (k = 0; k < width; k++) {
149 sValues[k] = src[k] & 0xff;
150 }
151 }
152 break;
153 default:
154 assert(0);
155 }
156
157 /* store */
158 dest = _mesa_image_address2d(packing, pixels, width, height,
159 format, type, j, 0);
160 if (format == GL_DEPTH_STENCIL) {
161 _mesa_pack_depth_stencil_span(ctx, width, dest,
162 zValues, sValues, packing);
163 }
164 else {
165 _mesa_pack_stencil_span(ctx, width, type, dest, sValues, packing);
166 }
167 }
168
169 /* unmap the stencil buffer */
170 pipe_transfer_unmap(pipe, pt);
171 pipe->transfer_destroy(pipe, pt);
172 }
173
174
175 /**
176 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
177 * commands.
178 */
179 struct st_renderbuffer *
180 st_get_color_read_renderbuffer(struct gl_context *ctx)
181 {
182 struct gl_framebuffer *fb = ctx->ReadBuffer;
183 struct st_renderbuffer *strb =
184 st_renderbuffer(fb->_ColorReadBuffer);
185
186 return strb;
187 }
188
189
190 /**
191 * Try to do glReadPixels in a fast manner for common cases.
192 * \return GL_TRUE for success, GL_FALSE for failure
193 */
194 static GLboolean
195 st_fast_readpixels(struct gl_context *ctx, struct st_renderbuffer *strb,
196 GLint x, GLint y, GLsizei width, GLsizei height,
197 GLenum format, GLenum type,
198 const struct gl_pixelstore_attrib *pack,
199 GLvoid *dest)
200 {
201 enum combination {
202 A8R8G8B8_UNORM_TO_RGBA_UBYTE,
203 A8R8G8B8_UNORM_TO_RGB_UBYTE,
204 A8R8G8B8_UNORM_TO_BGRA_UINT,
205 A8R8G8B8_UNORM_TO_RGBA_UINT
206 } combo;
207
208 if (ctx->_ImageTransferState)
209 return GL_FALSE;
210
211 if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
212 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
213 combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
214 }
215 else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
216 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
217 combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
218 }
219 else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
220 format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
221 combo = A8R8G8B8_UNORM_TO_BGRA_UINT;
222 }
223 else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
224 format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8) {
225 combo = A8R8G8B8_UNORM_TO_RGBA_UINT;
226 }
227 else {
228 return GL_FALSE;
229 }
230
231 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
232
233 {
234 struct pipe_context *pipe = st_context(ctx)->pipe;
235 struct pipe_transfer *trans;
236 const GLubyte *map;
237 GLubyte *dst;
238 GLint row, col, dy, dstStride;
239
240 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
241 /* convert GL Y to Gallium Y */
242 y = strb->texture->height0 - y - height;
243 }
244
245 trans = pipe_get_transfer(pipe, strb->texture,
246 0, 0,
247 PIPE_TRANSFER_READ,
248 x, y, width, height);
249 if (!trans) {
250 return GL_FALSE;
251 }
252
253 map = pipe_transfer_map(pipe, trans);
254 if (!map) {
255 pipe->transfer_destroy(pipe, trans);
256 return GL_FALSE;
257 }
258
259 /* We always write to the user/dest buffer from low addr to high addr
260 * but the read order depends on renderbuffer orientation
261 */
262 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
263 /* read source rows from bottom to top */
264 y = height - 1;
265 dy = -1;
266 }
267 else {
268 /* read source rows from top to bottom */
269 y = 0;
270 dy = 1;
271 }
272
273 dst = _mesa_image_address2d(pack, dest, width, height,
274 format, type, 0, 0);
275 dstStride = _mesa_image_row_stride(pack, width, format, type);
276
277 switch (combo) {
278 case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
279 for (row = 0; row < height; row++) {
280 const GLubyte *src = map + y * trans->stride;
281 for (col = 0; col < width; col++) {
282 GLuint pixel = ((GLuint *) src)[col];
283 dst[col*4+0] = (pixel >> 16) & 0xff;
284 dst[col*4+1] = (pixel >> 8) & 0xff;
285 dst[col*4+2] = (pixel >> 0) & 0xff;
286 dst[col*4+3] = (pixel >> 24) & 0xff;
287 }
288 dst += dstStride;
289 y += dy;
290 }
291 break;
292 case A8R8G8B8_UNORM_TO_RGB_UBYTE:
293 for (row = 0; row < height; row++) {
294 const GLubyte *src = map + y * trans->stride;
295 for (col = 0; col < width; col++) {
296 GLuint pixel = ((GLuint *) src)[col];
297 dst[col*3+0] = (pixel >> 16) & 0xff;
298 dst[col*3+1] = (pixel >> 8) & 0xff;
299 dst[col*3+2] = (pixel >> 0) & 0xff;
300 }
301 dst += dstStride;
302 y += dy;
303 }
304 break;
305 case A8R8G8B8_UNORM_TO_BGRA_UINT:
306 for (row = 0; row < height; row++) {
307 const GLubyte *src = map + y * trans->stride;
308 memcpy(dst, src, 4 * width);
309 dst += dstStride;
310 y += dy;
311 }
312 break;
313 case A8R8G8B8_UNORM_TO_RGBA_UINT:
314 for (row = 0; row < height; row++) {
315 const GLubyte *src = map + y * trans->stride;
316 for (col = 0; col < width; col++) {
317 GLuint pixel = ((GLuint *) src)[col];
318 dst[col*4+0] = (pixel >> 24) & 0xff;
319 dst[col*4+1] = (pixel >> 0) & 0xff;
320 dst[col*4+2] = (pixel >> 8) & 0xff;
321 dst[col*4+3] = (pixel >> 16) & 0xff;
322 }
323 dst += dstStride;
324 y += dy;
325 }
326 break;
327 default:
328 ; /* nothing */
329 }
330
331 pipe_transfer_unmap(pipe, trans);
332 pipe->transfer_destroy(pipe, trans);
333 }
334
335 return GL_TRUE;
336 }
337
338
339 /**
340 * Do glReadPixels by getting rows from the framebuffer transfer with
341 * get_tile(). Convert to requested format/type with Mesa image routines.
342 * Image transfer ops are done in software too.
343 */
344 static void
345 st_readpixels(struct gl_context *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
346 GLenum format, GLenum type,
347 const struct gl_pixelstore_attrib *pack,
348 GLvoid *dest)
349 {
350 struct st_context *st = st_context(ctx);
351 struct pipe_context *pipe = st->pipe;
352 GLfloat (*temp)[4];
353 GLbitfield transferOps = ctx->_ImageTransferState;
354 GLsizei i, j;
355 GLint yStep, dfStride;
356 GLfloat *df;
357 struct st_renderbuffer *strb;
358 struct gl_pixelstore_attrib clippedPacking = *pack;
359 struct pipe_transfer *trans;
360 enum pipe_format pformat;
361
362 assert(ctx->ReadBuffer->Width > 0);
363
364 st_validate_state(st);
365
366 /* Do all needed clipping here, so that we can forget about it later */
367 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
368 /* The ReadPixels transfer is totally outside the window bounds */
369 return;
370 }
371
372 st_flush_bitmap_cache(st);
373
374 dest = _mesa_map_pbo_dest(ctx, &clippedPacking, dest);
375 if (!dest)
376 return;
377
378 if (format == GL_STENCIL_INDEX ||
379 format == GL_DEPTH_STENCIL) {
380 st_read_stencil_pixels(ctx, x, y, width, height,
381 format, type, pack, dest);
382 return;
383 }
384 else if (format == GL_DEPTH_COMPONENT) {
385 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
386 if (strb->Base.Wrapped) {
387 strb = st_renderbuffer(strb->Base.Wrapped);
388 }
389 }
390 else {
391 /* Read color buffer */
392 strb = st_get_color_read_renderbuffer(ctx);
393 }
394
395 if (!strb)
396 return;
397
398 /* try a fast-path readpixels before anything else */
399 if (st_fast_readpixels(ctx, strb, x, y, width, height,
400 format, type, pack, dest)) {
401 /* success! */
402 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
403 return;
404 }
405
406 /* allocate temp pixel row buffer */
407 temp = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
408 if (!temp) {
409 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
410 return;
411 }
412
413 if(ctx->Color._ClampReadColor)
414 transferOps |= IMAGE_CLAMP_BIT;
415
416 if (format == GL_RGBA && type == GL_FLOAT && !transferOps) {
417 /* write tile(row) directly into user's buffer */
418 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
419 height, format, type, 0, 0);
420 dfStride = width * 4;
421 }
422 else {
423 /* write tile(row) into temp row buffer */
424 df = (GLfloat *) temp;
425 dfStride = 0;
426 }
427
428 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
429 /* convert GL Y to Gallium Y */
430 y = strb->Base.Height - y - height;
431 }
432
433 /* Create a read transfer from the renderbuffer's texture */
434 trans = pipe_get_transfer(pipe, strb->texture,
435 0, 0,
436 PIPE_TRANSFER_READ,
437 x, y, width, height);
438
439 /* determine bottom-to-top vs. top-to-bottom order */
440 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
441 y = height - 1;
442 yStep = -1;
443 }
444 else {
445 y = 0;
446 yStep = 1;
447 }
448
449 /* possibly convert sRGB format to linear RGB format */
450 pformat = util_format_linear(trans->resource->format);
451
452 if (ST_DEBUG & DEBUG_FALLBACK)
453 debug_printf("%s: fallback processing\n", __FUNCTION__);
454
455 /*
456 * Copy pixels from pipe_transfer to user memory
457 */
458 {
459 /* dest of first pixel in client memory */
460 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
461 height, format, type, 0, 0);
462 /* dest row stride */
463 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
464 format, type);
465
466 if (pformat == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
467 pformat == PIPE_FORMAT_Z24X8_UNORM) {
468 if (format == GL_DEPTH_COMPONENT) {
469 for (i = 0; i < height; i++) {
470 GLuint ztemp[MAX_WIDTH];
471 GLfloat zfloat[MAX_WIDTH];
472 const double scale = 1.0 / ((1 << 24) - 1);
473 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
474 y += yStep;
475 for (j = 0; j < width; j++) {
476 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
477 }
478 _mesa_pack_depth_span(ctx, width, dst, type,
479 zfloat, &clippedPacking);
480 dst += dstStride;
481 }
482 }
483 else {
484 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
485 assert(format == GL_DEPTH_STENCIL_EXT);
486 for (i = 0; i < height; i++) {
487 GLuint *zshort = (GLuint *)dst;
488 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
489 y += yStep;
490 /* Reverse into 24/8 */
491 for (j = 0; j < width; j++) {
492 zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24);
493 }
494 dst += dstStride;
495 }
496 }
497 }
498 else if (pformat == PIPE_FORMAT_S8_USCALED_Z24_UNORM ||
499 pformat == PIPE_FORMAT_X8Z24_UNORM) {
500 if (format == GL_DEPTH_COMPONENT) {
501 for (i = 0; i < height; i++) {
502 GLuint ztemp[MAX_WIDTH];
503 GLfloat zfloat[MAX_WIDTH];
504 const double scale = 1.0 / ((1 << 24) - 1);
505 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
506 y += yStep;
507 for (j = 0; j < width; j++) {
508 zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
509 }
510 _mesa_pack_depth_span(ctx, width, dst, type,
511 zfloat, &clippedPacking);
512 dst += dstStride;
513 }
514 }
515 else {
516 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
517 assert(format == GL_DEPTH_STENCIL_EXT);
518 for (i = 0; i < height; i++) {
519 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
520 y += yStep;
521 dst += dstStride;
522 }
523 }
524 }
525 else if (pformat == PIPE_FORMAT_Z16_UNORM) {
526 for (i = 0; i < height; i++) {
527 GLushort ztemp[MAX_WIDTH];
528 GLfloat zfloat[MAX_WIDTH];
529 const double scale = 1.0 / 0xffff;
530 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
531 y += yStep;
532 for (j = 0; j < width; j++) {
533 zfloat[j] = (float) (scale * ztemp[j]);
534 }
535 _mesa_pack_depth_span(ctx, width, dst, type,
536 zfloat, &clippedPacking);
537 dst += dstStride;
538 }
539 }
540 else if (pformat == PIPE_FORMAT_Z32_UNORM) {
541 for (i = 0; i < height; i++) {
542 GLuint ztemp[MAX_WIDTH];
543 GLfloat zfloat[MAX_WIDTH];
544 const double scale = 1.0 / 0xffffffff;
545 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
546 y += yStep;
547 for (j = 0; j < width; j++) {
548 zfloat[j] = (float) (scale * ztemp[j]);
549 }
550 _mesa_pack_depth_span(ctx, width, dst, type,
551 zfloat, &clippedPacking);
552 dst += dstStride;
553 }
554 }
555 else {
556 /* RGBA format */
557 /* Do a row at a time to flip image data vertically */
558 for (i = 0; i < height; i++) {
559 pipe_get_tile_rgba_format(pipe, trans, 0, y, width, 1,
560 pformat, df);
561 y += yStep;
562 df += dfStride;
563 if (!dfStride) {
564 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
565 &clippedPacking, transferOps);
566 dst += dstStride;
567 }
568 }
569 }
570 }
571
572 free(temp);
573
574 pipe->transfer_destroy(pipe, trans);
575
576 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
577 }
578
579
580 void st_init_readpixels_functions(struct dd_function_table *functions)
581 {
582 functions->ReadPixels = st_readpixels;
583 }