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