Merge branch '7.8'
[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_readpixels.h"
50 #include "st_cb_fbo.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 = st_context(ctx)->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->resource->format) {
105 case PIPE_FORMAT_S8_USCALED:
106 {
107 const ubyte *src = stmap + srcY * pt->stride;
108 memcpy(sValues, src, width);
109 }
110 break;
111 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
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_S8_USCALED_Z24_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->transfer_destroy(pipe, pt);
166 }
167
168
169 /**
170 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
171 * commands.
172 */
173 struct st_renderbuffer *
174 st_get_color_read_renderbuffer(GLcontext *ctx)
175 {
176 struct gl_framebuffer *fb = ctx->ReadBuffer;
177 struct st_renderbuffer *strb =
178 st_renderbuffer(fb->_ColorReadBuffer);
179
180 return strb;
181 }
182
183
184 /**
185 * Try to do glReadPixels in a fast manner for common cases.
186 * \return GL_TRUE for success, GL_FALSE for failure
187 */
188 static GLboolean
189 st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
190 GLint x, GLint y, GLsizei width, GLsizei height,
191 GLenum format, GLenum type,
192 const struct gl_pixelstore_attrib *pack,
193 GLvoid *dest)
194 {
195 enum combination {
196 A8R8G8B8_UNORM_TO_RGBA_UBYTE,
197 A8R8G8B8_UNORM_TO_RGB_UBYTE,
198 A8R8G8B8_UNORM_TO_BGRA_UINT
199 } combo;
200
201 if (ctx->_ImageTransferState)
202 return GL_FALSE;
203
204 if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
205 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
206 combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
207 }
208 else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
209 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
210 combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
211 }
212 else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
213 format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
214 combo = A8R8G8B8_UNORM_TO_BGRA_UINT;
215 }
216 else {
217 return GL_FALSE;
218 }
219
220 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
221
222 {
223 struct pipe_context *pipe = st_context(ctx)->pipe;
224 struct pipe_transfer *trans;
225 const GLubyte *map;
226 GLubyte *dst;
227 GLint row, col, dy, dstStride;
228
229 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
230 /* convert GL Y to Gallium Y */
231 y = strb->texture->height0 - y - height;
232 }
233
234 trans = st_cond_flush_get_tex_transfer(st_context(ctx), strb->texture,
235 0, 0, 0,
236 PIPE_TRANSFER_READ, x, y,
237 width, height);
238 if (!trans) {
239 return GL_FALSE;
240 }
241
242 map = pipe_transfer_map(pipe, trans);
243 if (!map) {
244 pipe->transfer_destroy(pipe, trans);
245 return GL_FALSE;
246 }
247
248 /* We always write to the user/dest buffer from low addr to high addr
249 * but the read order depends on renderbuffer orientation
250 */
251 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
252 /* read source rows from bottom to top */
253 y = height - 1;
254 dy = -1;
255 }
256 else {
257 /* read source rows from top to bottom */
258 y = 0;
259 dy = 1;
260 }
261
262 dst = _mesa_image_address2d(pack, dest, width, height,
263 format, type, 0, 0);
264 dstStride = _mesa_image_row_stride(pack, width, format, type);
265
266 switch (combo) {
267 case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
268 for (row = 0; row < height; row++) {
269 const GLubyte *src = map + y * trans->stride;
270 for (col = 0; col < width; col++) {
271 GLuint pixel = ((GLuint *) src)[col];
272 dst[col*4+0] = (pixel >> 16) & 0xff;
273 dst[col*4+1] = (pixel >> 8) & 0xff;
274 dst[col*4+2] = (pixel >> 0) & 0xff;
275 dst[col*4+3] = (pixel >> 24) & 0xff;
276 }
277 dst += dstStride;
278 y += dy;
279 }
280 break;
281 case A8R8G8B8_UNORM_TO_RGB_UBYTE:
282 for (row = 0; row < height; row++) {
283 const GLubyte *src = map + y * trans->stride;
284 for (col = 0; col < width; col++) {
285 GLuint pixel = ((GLuint *) src)[col];
286 dst[col*3+0] = (pixel >> 16) & 0xff;
287 dst[col*3+1] = (pixel >> 8) & 0xff;
288 dst[col*3+2] = (pixel >> 0) & 0xff;
289 }
290 dst += dstStride;
291 y += dy;
292 }
293 break;
294 case A8R8G8B8_UNORM_TO_BGRA_UINT:
295 for (row = 0; row < height; row++) {
296 const GLubyte *src = map + y * trans->stride;
297 memcpy(dst, src, 4 * width);
298 dst += dstStride;
299 y += dy;
300 }
301 break;
302 default:
303 ; /* nothing */
304 }
305
306 pipe_transfer_unmap(pipe, trans);
307 pipe->transfer_destroy(pipe, trans);
308 }
309
310 return GL_TRUE;
311 }
312
313
314 /**
315 * Do glReadPixels by getting rows from the framebuffer transfer with
316 * get_tile(). Convert to requested format/type with Mesa image routines.
317 * Image transfer ops are done in software too.
318 */
319 static void
320 st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
321 GLenum format, GLenum type,
322 const struct gl_pixelstore_attrib *pack,
323 GLvoid *dest)
324 {
325 struct st_context *st = st_context(ctx);
326 struct pipe_context *pipe = st->pipe;
327 GLfloat temp[MAX_WIDTH][4];
328 const GLbitfield transferOps = ctx->_ImageTransferState;
329 GLsizei i, j;
330 GLint yStep, dfStride;
331 GLfloat *df;
332 struct st_renderbuffer *strb;
333 struct gl_pixelstore_attrib clippedPacking = *pack;
334 struct pipe_transfer *trans;
335
336 assert(ctx->ReadBuffer->Width > 0);
337
338 /* XXX convolution not done yet */
339 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
340
341 st_validate_state(st);
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_pbo_dest(ctx, &clippedPacking, dest);
350 if (!dest)
351 return;
352
353 st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL);
354
355 if (format == GL_STENCIL_INDEX ||
356 format == GL_DEPTH_STENCIL) {
357 st_read_stencil_pixels(ctx, x, y, width, height,
358 format, type, pack, dest);
359 return;
360 }
361 else if (format == GL_DEPTH_COMPONENT) {
362 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
363 }
364 else {
365 /* Read color buffer */
366 strb = st_get_color_read_renderbuffer(ctx);
367 }
368
369 if (!strb)
370 return;
371
372 /* try a fast-path readpixels before anything else */
373 if (st_fast_readpixels(ctx, strb, x, y, width, height,
374 format, type, pack, dest)) {
375 /* success! */
376 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
377 return;
378 }
379
380 if (format == GL_RGBA && type == GL_FLOAT) {
381 /* write tile(row) directly into user's buffer */
382 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
383 height, format, type, 0, 0);
384 dfStride = width * 4;
385 }
386 else {
387 /* write tile(row) into temp row buffer */
388 df = (GLfloat *) temp;
389 dfStride = 0;
390 }
391
392 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
393 /* convert GL Y to Gallium Y */
394 y = strb->Base.Height - y - height;
395 }
396
397 /* Create a read transfer from the renderbuffer's texture */
398 trans = st_cond_flush_get_tex_transfer(st_context(ctx), strb->texture,
399 0, 0, 0,
400 PIPE_TRANSFER_READ, x, y,
401 width, height);
402
403 /* determine bottom-to-top vs. top-to-bottom order */
404 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
405 y = height - 1;
406 yStep = -1;
407 }
408 else {
409 y = 0;
410 yStep = 1;
411 }
412
413 if (ST_DEBUG & DEBUG_FALLBACK)
414 debug_printf("%s: fallback processing\n", __FUNCTION__);
415
416 /*
417 * Copy pixels from pipe_transfer to user memory
418 */
419 {
420 /* dest of first pixel in client memory */
421 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
422 height, format, type, 0, 0);
423 /* dest row stride */
424 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
425 format, type);
426
427 if (trans->resource->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
428 trans->resource->format == PIPE_FORMAT_Z24X8_UNORM) {
429 if (format == GL_DEPTH_COMPONENT) {
430 for (i = 0; i < height; i++) {
431 GLuint ztemp[MAX_WIDTH];
432 GLfloat zfloat[MAX_WIDTH];
433 const double scale = 1.0 / ((1 << 24) - 1);
434 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
435 y += yStep;
436 for (j = 0; j < width; j++) {
437 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
438 }
439 _mesa_pack_depth_span(ctx, width, dst, type,
440 zfloat, &clippedPacking);
441 dst += dstStride;
442 }
443 }
444 else {
445 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
446 assert(format == GL_DEPTH_STENCIL_EXT);
447 for (i = 0; i < height; i++) {
448 GLuint *zshort = (GLuint *)dst;
449 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
450 y += yStep;
451 /* Reverse into 24/8 */
452 for (j = 0; j < width; j++) {
453 zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24);
454 }
455 dst += dstStride;
456 }
457 }
458 }
459 else if (trans->resource->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM ||
460 trans->resource->format == PIPE_FORMAT_X8Z24_UNORM) {
461 if (format == GL_DEPTH_COMPONENT) {
462 for (i = 0; i < height; i++) {
463 GLuint ztemp[MAX_WIDTH];
464 GLfloat zfloat[MAX_WIDTH];
465 const double scale = 1.0 / ((1 << 24) - 1);
466 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
467 y += yStep;
468 for (j = 0; j < width; j++) {
469 zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
470 }
471 _mesa_pack_depth_span(ctx, width, dst, type,
472 zfloat, &clippedPacking);
473 dst += dstStride;
474 }
475 }
476 else {
477 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
478 assert(format == GL_DEPTH_STENCIL_EXT);
479 for (i = 0; i < height; i++) {
480 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
481 y += yStep;
482 dst += dstStride;
483 }
484 }
485 }
486 else if (trans->resource->format == PIPE_FORMAT_Z16_UNORM) {
487 for (i = 0; i < height; i++) {
488 GLushort ztemp[MAX_WIDTH];
489 GLfloat zfloat[MAX_WIDTH];
490 const double scale = 1.0 / 0xffff;
491 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
492 y += yStep;
493 for (j = 0; j < width; j++) {
494 zfloat[j] = (float) (scale * ztemp[j]);
495 }
496 _mesa_pack_depth_span(ctx, width, dst, type,
497 zfloat, &clippedPacking);
498 dst += dstStride;
499 }
500 }
501 else if (trans->resource->format == PIPE_FORMAT_Z32_UNORM) {
502 for (i = 0; i < height; i++) {
503 GLuint ztemp[MAX_WIDTH];
504 GLfloat zfloat[MAX_WIDTH];
505 const double scale = 1.0 / 0xffffffff;
506 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
507 y += yStep;
508 for (j = 0; j < width; j++) {
509 zfloat[j] = (float) (scale * ztemp[j]);
510 }
511 _mesa_pack_depth_span(ctx, width, dst, type,
512 zfloat, &clippedPacking);
513 dst += dstStride;
514 }
515 }
516 else {
517 /* RGBA format */
518 /* Do a row at a time to flip image data vertically */
519 for (i = 0; i < height; i++) {
520 pipe_get_tile_rgba(pipe, trans, 0, y, width, 1, df);
521 y += yStep;
522 df += dfStride;
523 if (!dfStride) {
524 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
525 &clippedPacking, transferOps);
526 dst += dstStride;
527 }
528 }
529 }
530 }
531
532 pipe->transfer_destroy(pipe, trans);
533
534 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
535 }
536
537
538 void st_init_readpixels_functions(struct dd_function_table *functions)
539 {
540 functions->ReadPixels = st_readpixels;
541 }