mesa: Remove EXT_convolution.
[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 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 st_flush_bitmap_cache(st);
350
351 dest = _mesa_map_pbo_dest(ctx, &clippedPacking, dest);
352 if (!dest)
353 return;
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 if (strb->Base.Wrapped) {
364 strb = st_renderbuffer(strb->Base.Wrapped);
365 }
366 }
367 else {
368 /* Read color buffer */
369 strb = st_get_color_read_renderbuffer(ctx);
370 }
371
372 if (!strb)
373 return;
374
375 /* try a fast-path readpixels before anything else */
376 if (st_fast_readpixels(ctx, strb, x, y, width, height,
377 format, type, pack, dest)) {
378 /* success! */
379 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
380 return;
381 }
382
383 if (format == GL_RGBA && type == GL_FLOAT) {
384 /* write tile(row) directly into user's buffer */
385 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
386 height, format, type, 0, 0);
387 dfStride = width * 4;
388 }
389 else {
390 /* write tile(row) into temp row buffer */
391 df = (GLfloat *) temp;
392 dfStride = 0;
393 }
394
395 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
396 /* convert GL Y to Gallium Y */
397 y = strb->Base.Height - y - height;
398 }
399
400 /* Create a read transfer from the renderbuffer's texture */
401 trans = pipe_get_transfer(pipe, strb->texture,
402 0, 0, 0, /* face, level, zslice */
403 PIPE_TRANSFER_READ,
404 x, y, width, height);
405
406 /* determine bottom-to-top vs. top-to-bottom order */
407 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
408 y = height - 1;
409 yStep = -1;
410 }
411 else {
412 y = 0;
413 yStep = 1;
414 }
415
416 if (ST_DEBUG & DEBUG_FALLBACK)
417 debug_printf("%s: fallback processing\n", __FUNCTION__);
418
419 /*
420 * Copy pixels from pipe_transfer to user memory
421 */
422 {
423 /* dest of first pixel in client memory */
424 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
425 height, format, type, 0, 0);
426 /* dest row stride */
427 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
428 format, type);
429
430 if (trans->resource->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
431 trans->resource->format == PIPE_FORMAT_Z24X8_UNORM) {
432 if (format == GL_DEPTH_COMPONENT) {
433 for (i = 0; i < height; i++) {
434 GLuint ztemp[MAX_WIDTH];
435 GLfloat zfloat[MAX_WIDTH];
436 const double scale = 1.0 / ((1 << 24) - 1);
437 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
438 y += yStep;
439 for (j = 0; j < width; j++) {
440 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
441 }
442 _mesa_pack_depth_span(ctx, width, dst, type,
443 zfloat, &clippedPacking);
444 dst += dstStride;
445 }
446 }
447 else {
448 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
449 assert(format == GL_DEPTH_STENCIL_EXT);
450 for (i = 0; i < height; i++) {
451 GLuint *zshort = (GLuint *)dst;
452 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
453 y += yStep;
454 /* Reverse into 24/8 */
455 for (j = 0; j < width; j++) {
456 zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24);
457 }
458 dst += dstStride;
459 }
460 }
461 }
462 else if (trans->resource->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM ||
463 trans->resource->format == PIPE_FORMAT_X8Z24_UNORM) {
464 if (format == GL_DEPTH_COMPONENT) {
465 for (i = 0; i < height; i++) {
466 GLuint ztemp[MAX_WIDTH];
467 GLfloat zfloat[MAX_WIDTH];
468 const double scale = 1.0 / ((1 << 24) - 1);
469 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
470 y += yStep;
471 for (j = 0; j < width; j++) {
472 zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
473 }
474 _mesa_pack_depth_span(ctx, width, dst, type,
475 zfloat, &clippedPacking);
476 dst += dstStride;
477 }
478 }
479 else {
480 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
481 assert(format == GL_DEPTH_STENCIL_EXT);
482 for (i = 0; i < height; i++) {
483 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
484 y += yStep;
485 dst += dstStride;
486 }
487 }
488 }
489 else if (trans->resource->format == PIPE_FORMAT_Z16_UNORM) {
490 for (i = 0; i < height; i++) {
491 GLushort ztemp[MAX_WIDTH];
492 GLfloat zfloat[MAX_WIDTH];
493 const double scale = 1.0 / 0xffff;
494 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
495 y += yStep;
496 for (j = 0; j < width; j++) {
497 zfloat[j] = (float) (scale * ztemp[j]);
498 }
499 _mesa_pack_depth_span(ctx, width, dst, type,
500 zfloat, &clippedPacking);
501 dst += dstStride;
502 }
503 }
504 else if (trans->resource->format == PIPE_FORMAT_Z32_UNORM) {
505 for (i = 0; i < height; i++) {
506 GLuint ztemp[MAX_WIDTH];
507 GLfloat zfloat[MAX_WIDTH];
508 const double scale = 1.0 / 0xffffffff;
509 pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
510 y += yStep;
511 for (j = 0; j < width; j++) {
512 zfloat[j] = (float) (scale * ztemp[j]);
513 }
514 _mesa_pack_depth_span(ctx, width, dst, type,
515 zfloat, &clippedPacking);
516 dst += dstStride;
517 }
518 }
519 else {
520 /* RGBA format */
521 /* Do a row at a time to flip image data vertically */
522 for (i = 0; i < height; i++) {
523 pipe_get_tile_rgba(pipe, trans, 0, y, width, 1, df);
524 y += yStep;
525 df += dfStride;
526 if (!dfStride) {
527 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
528 &clippedPacking, transferOps);
529 dst += dstStride;
530 }
531 }
532 }
533 }
534
535 pipe->transfer_destroy(pipe, trans);
536
537 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
538 }
539
540
541 void st_init_readpixels_functions(struct dd_function_table *functions)
542 {
543 functions->ReadPixels = st_readpixels;
544 }