Merge remote branch 'origin/master' into gallium_draw_llvm
[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_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_context *pipe = ctx->st->pipe;
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 = pipe->transfer_map(pipe, 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_Z24S8_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_S8Z24_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 pipe->transfer_unmap(pipe, pt);
165 pipe->tex_transfer_destroy(pipe, 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_B8G8R8A8_UNORM &&
218 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
219 combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
220 }
221 else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
222 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
223 combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
224 }
225 else if (strb->format == PIPE_FORMAT_B8G8R8A8_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_transfer *trans;
238 const GLubyte *map;
239 GLubyte *dst;
240 GLint row, col, dy, dstStride;
241
242 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
243 /* convert GL Y to Gallium Y */
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 = pipe->transfer_map(pipe, trans);
256 if (!map) {
257 pipe->tex_transfer_destroy(pipe, trans);
258 return GL_FALSE;
259 }
260
261 /* We always write to the user/dest buffer from low addr to high addr
262 * but the read order depends on renderbuffer orientation
263 */
264 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
265 /* read source rows from bottom to top */
266 y = height - 1;
267 dy = -1;
268 }
269 else {
270 /* read source rows from top to bottom */
271 y = 0;
272 dy = 1;
273 }
274
275 dst = _mesa_image_address2d(pack, dest, width, height,
276 format, type, 0, 0);
277 dstStride = _mesa_image_row_stride(pack, width, format, type);
278
279 switch (combo) {
280 case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
281 for (row = 0; row < height; row++) {
282 const GLubyte *src = map + y * trans->stride;
283 for (col = 0; col < width; col++) {
284 GLuint pixel = ((GLuint *) src)[col];
285 dst[col*4+0] = (pixel >> 16) & 0xff;
286 dst[col*4+1] = (pixel >> 8) & 0xff;
287 dst[col*4+2] = (pixel >> 0) & 0xff;
288 dst[col*4+3] = (pixel >> 24) & 0xff;
289 }
290 dst += dstStride;
291 y += dy;
292 }
293 break;
294 case A8R8G8B8_UNORM_TO_RGB_UBYTE:
295 for (row = 0; row < height; row++) {
296 const GLubyte *src = map + y * trans->stride;
297 for (col = 0; col < width; col++) {
298 GLuint pixel = ((GLuint *) src)[col];
299 dst[col*3+0] = (pixel >> 16) & 0xff;
300 dst[col*3+1] = (pixel >> 8) & 0xff;
301 dst[col*3+2] = (pixel >> 0) & 0xff;
302 }
303 dst += dstStride;
304 y += dy;
305 }
306 break;
307 case A8R8G8B8_UNORM_TO_BGRA_UINT:
308 for (row = 0; row < height; row++) {
309 const GLubyte *src = map + y * trans->stride;
310 memcpy(dst, src, 4 * width);
311 dst += dstStride;
312 y += dy;
313 }
314 break;
315 default:
316 ; /* nothing */
317 }
318
319 pipe->transfer_unmap(pipe, trans);
320 pipe->tex_transfer_destroy(pipe, trans);
321 }
322
323 return GL_TRUE;
324 }
325
326
327 /**
328 * Do glReadPixels by getting rows from the framebuffer transfer with
329 * get_tile(). Convert to requested format/type with Mesa image routines.
330 * Image transfer ops are done in software too.
331 */
332 static void
333 st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
334 GLenum format, GLenum type,
335 const struct gl_pixelstore_attrib *pack,
336 GLvoid *dest)
337 {
338 struct pipe_context *pipe = ctx->st->pipe;
339 GLfloat temp[MAX_WIDTH][4];
340 const GLbitfield transferOps = ctx->_ImageTransferState;
341 GLsizei i, j;
342 GLint yStep, dfStride;
343 GLfloat *df;
344 struct st_renderbuffer *strb;
345 struct gl_pixelstore_attrib clippedPacking = *pack;
346 struct pipe_transfer *trans;
347
348 assert(ctx->ReadBuffer->Width > 0);
349
350 /* XXX convolution not done yet */
351 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
352
353 /* Do all needed clipping here, so that we can forget about it later */
354 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
355 /* The ReadPixels transfer is totally outside the window bounds */
356 return;
357 }
358
359 dest = _mesa_map_pbo_dest(ctx, &clippedPacking, dest);
360 if (!dest)
361 return;
362
363 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
364
365 if (format == GL_STENCIL_INDEX ||
366 format == GL_DEPTH_STENCIL) {
367 st_read_stencil_pixels(ctx, x, y, width, height,
368 format, type, pack, dest);
369 return;
370 }
371 else if (format == GL_DEPTH_COMPONENT) {
372 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
373 }
374 else {
375 /* Read color buffer */
376 strb = st_get_color_read_renderbuffer(ctx);
377 }
378
379 if (!strb)
380 return;
381
382 /* try a fast-path readpixels before anything else */
383 if (st_fast_readpixels(ctx, strb, x, y, width, height,
384 format, type, pack, dest)) {
385 /* success! */
386 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
387 return;
388 }
389
390 if (format == GL_RGBA && type == GL_FLOAT) {
391 /* write tile(row) directly into user's buffer */
392 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
393 height, format, type, 0, 0);
394 dfStride = width * 4;
395 }
396 else {
397 /* write tile(row) into temp row buffer */
398 df = (GLfloat *) temp;
399 dfStride = 0;
400 }
401
402 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
403 /* convert GL Y to Gallium Y */
404 y = strb->Base.Height - y - height;
405 }
406
407 /* Create a read transfer from the renderbuffer's texture */
408 trans = st_cond_flush_get_tex_transfer(st_context(ctx), strb->texture,
409 0, 0, 0,
410 PIPE_TRANSFER_READ, x, y,
411 width, height);
412
413 /* determine bottom-to-top vs. top-to-bottom order */
414 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
415 y = height - 1;
416 yStep = -1;
417 }
418 else {
419 y = 0;
420 yStep = 1;
421 }
422
423 if (ST_DEBUG & DEBUG_FALLBACK)
424 debug_printf("%s: fallback processing\n", __FUNCTION__);
425
426 /*
427 * Copy pixels from pipe_transfer to user memory
428 */
429 {
430 /* dest of first pixel in client memory */
431 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
432 height, format, type, 0, 0);
433 /* dest row stride */
434 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
435 format, type);
436
437 if (trans->texture->format == PIPE_FORMAT_Z24S8_UNORM ||
438 trans->texture->format == PIPE_FORMAT_Z24X8_UNORM) {
439 if (format == GL_DEPTH_COMPONENT) {
440 for (i = 0; i < height; i++) {
441 GLuint ztemp[MAX_WIDTH];
442 GLfloat zfloat[MAX_WIDTH];
443 const double scale = 1.0 / ((1 << 24) - 1);
444 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
445 y += yStep;
446 for (j = 0; j < width; j++) {
447 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
448 }
449 _mesa_pack_depth_span(ctx, width, dst, type,
450 zfloat, &clippedPacking);
451 dst += dstStride;
452 }
453 }
454 else {
455 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
456 assert(format == GL_DEPTH_STENCIL_EXT);
457 for (i = 0; i < height; i++) {
458 GLuint *zshort = (GLuint *)dst;
459 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
460 y += yStep;
461 /* Reverse into 24/8 */
462 for (j = 0; j < width; j++) {
463 zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24);
464 }
465 dst += dstStride;
466 }
467 }
468 }
469 else if (trans->texture->format == PIPE_FORMAT_S8Z24_UNORM ||
470 trans->texture->format == PIPE_FORMAT_X8Z24_UNORM) {
471 if (format == GL_DEPTH_COMPONENT) {
472 for (i = 0; i < height; i++) {
473 GLuint ztemp[MAX_WIDTH];
474 GLfloat zfloat[MAX_WIDTH];
475 const double scale = 1.0 / ((1 << 24) - 1);
476 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
477 y += yStep;
478 for (j = 0; j < width; j++) {
479 zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
480 }
481 _mesa_pack_depth_span(ctx, width, dst, type,
482 zfloat, &clippedPacking);
483 dst += dstStride;
484 }
485 }
486 else {
487 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
488 assert(format == GL_DEPTH_STENCIL_EXT);
489 for (i = 0; i < height; i++) {
490 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
491 y += yStep;
492 dst += dstStride;
493 }
494 }
495 }
496 else if (trans->texture->format == PIPE_FORMAT_Z16_UNORM) {
497 for (i = 0; i < height; i++) {
498 GLushort ztemp[MAX_WIDTH];
499 GLfloat zfloat[MAX_WIDTH];
500 const double scale = 1.0 / 0xffff;
501 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
502 y += yStep;
503 for (j = 0; j < width; j++) {
504 zfloat[j] = (float) (scale * ztemp[j]);
505 }
506 _mesa_pack_depth_span(ctx, width, dst, type,
507 zfloat, &clippedPacking);
508 dst += dstStride;
509 }
510 }
511 else if (trans->texture->format == PIPE_FORMAT_Z32_UNORM) {
512 for (i = 0; i < height; i++) {
513 GLuint ztemp[MAX_WIDTH];
514 GLfloat zfloat[MAX_WIDTH];
515 const double scale = 1.0 / 0xffffffff;
516 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
517 y += yStep;
518 for (j = 0; j < width; j++) {
519 zfloat[j] = (float) (scale * ztemp[j]);
520 }
521 _mesa_pack_depth_span(ctx, width, dst, type,
522 zfloat, &clippedPacking);
523 dst += dstStride;
524 }
525 }
526 else {
527 /* RGBA format */
528 /* Do a row at a time to flip image data vertically */
529 for (i = 0; i < height; i++) {
530 pipe_get_tile_rgba(pipe, trans, 0, y, width, 1, df);
531 y += yStep;
532 df += dfStride;
533 if (!dfStride) {
534 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
535 &clippedPacking, transferOps);
536 dst += dstStride;
537 }
538 }
539 }
540 }
541
542 pipe->tex_transfer_destroy(pipe, trans);
543
544 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
545 }
546
547
548 void st_init_readpixels_functions(struct dd_function_table *functions)
549 {
550 functions->ReadPixels = st_readpixels;
551 }