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