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