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