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