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