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