swrast: Add a readpixels fast-path based on memcpy and MapRenderbuffer.
[mesa.git] / src / mesa / swrast / s_readpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/feedback.h"
29 #include "main/formats.h"
30 #include "main/format_unpack.h"
31 #include "main/image.h"
32 #include "main/imports.h"
33 #include "main/macros.h"
34 #include "main/pack.h"
35 #include "main/pbo.h"
36 #include "main/state.h"
37
38 #include "s_context.h"
39 #include "s_depth.h"
40 #include "s_span.h"
41 #include "s_stencil.h"
42
43 /**
44 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
45 * mapping.
46 */
47 static GLboolean
48 fast_read_depth_pixels( struct gl_context *ctx,
49 GLint x, GLint y,
50 GLsizei width, GLsizei height,
51 GLenum type, GLvoid *pixels,
52 const struct gl_pixelstore_attrib *packing )
53 {
54 struct gl_framebuffer *fb = ctx->ReadBuffer;
55 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
56 GLubyte *map, *dst;
57 int stride, dstStride, j;
58
59 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0)
60 return GL_FALSE;
61
62 if (packing->SwapBytes)
63 return GL_FALSE;
64
65 if (_mesa_get_format_datatype(rb->Format) != GL_UNSIGNED_INT)
66 return GL_FALSE;
67
68 if (!((type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16) ||
69 type == GL_UNSIGNED_INT))
70 return GL_FALSE;
71
72 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
73 &map, &stride);
74
75 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
76 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
77 GL_DEPTH_COMPONENT, type, 0, 0);
78
79 for (j = 0; j < height; j++) {
80 if (type == GL_UNSIGNED_INT) {
81 _mesa_unpack_uint_z_row(rb->Format, width, map, (GLuint *)dst);
82 } else {
83 ASSERT(type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16);
84 memcpy(dst, map, width * 2);
85 }
86
87 map += stride;
88 dst += dstStride;
89 }
90 ctx->Driver.UnmapRenderbuffer(ctx, rb);
91
92 return GL_TRUE;
93 }
94
95 /**
96 * Read pixels for format=GL_DEPTH_COMPONENT.
97 */
98 static void
99 read_depth_pixels( struct gl_context *ctx,
100 GLint x, GLint y,
101 GLsizei width, GLsizei height,
102 GLenum type, GLvoid *pixels,
103 const struct gl_pixelstore_attrib *packing )
104 {
105 struct gl_framebuffer *fb = ctx->ReadBuffer;
106 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
107 GLint j;
108 GLubyte *dst, *map;
109 int dstStride, stride;
110
111 if (!rb)
112 return;
113
114 /* clipping should have been done already */
115 ASSERT(x >= 0);
116 ASSERT(y >= 0);
117 ASSERT(x + width <= (GLint) rb->Width);
118 ASSERT(y + height <= (GLint) rb->Height);
119 /* width should never be > MAX_WIDTH since we did clipping earlier */
120 ASSERT(width <= MAX_WIDTH);
121
122 if (fast_read_depth_pixels(ctx, x, y, width, height, type, pixels, packing))
123 return;
124
125 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
126 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
127 GL_DEPTH_COMPONENT, type, 0, 0);
128
129 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
130 &map, &stride);
131
132 /* General case (slower) */
133 for (j = 0; j < height; j++, y++) {
134 GLfloat depthValues[MAX_WIDTH];
135 _mesa_unpack_float_z_row(rb->Format, width, map, depthValues);
136 _mesa_pack_depth_span(ctx, width, dst, type, depthValues, packing);
137
138 dst += dstStride;
139 map += stride;
140 }
141
142 ctx->Driver.UnmapRenderbuffer(ctx, rb);
143 }
144
145
146 /**
147 * Read pixels for format=GL_STENCIL_INDEX.
148 */
149 static void
150 read_stencil_pixels( struct gl_context *ctx,
151 GLint x, GLint y,
152 GLsizei width, GLsizei height,
153 GLenum type, GLvoid *pixels,
154 const struct gl_pixelstore_attrib *packing )
155 {
156 struct gl_framebuffer *fb = ctx->ReadBuffer;
157 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
158 GLint j;
159 GLubyte *map;
160 GLint stride;
161
162 if (!rb)
163 return;
164
165 /* width should never be > MAX_WIDTH since we did clipping earlier */
166 ASSERT(width <= MAX_WIDTH);
167
168 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
169 &map, &stride);
170
171 /* process image row by row */
172 for (j = 0; j < height; j++) {
173 GLvoid *dest;
174 GLstencil stencil[MAX_WIDTH];
175
176 _mesa_unpack_ubyte_stencil_row(rb->Format, width, map, stencil);
177 dest = _mesa_image_address2d(packing, pixels, width, height,
178 GL_STENCIL_INDEX, type, j, 0);
179
180 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
181
182 map += stride;
183 }
184
185 ctx->Driver.UnmapRenderbuffer(ctx, rb);
186 }
187
188
189
190 /**
191 * Optimized glReadPixels for particular pixel formats when pixel
192 * scaling, biasing, mapping, etc. are disabled.
193 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
194 */
195 static GLboolean
196 fast_read_rgba_pixels( struct gl_context *ctx,
197 GLint x, GLint y,
198 GLsizei width, GLsizei height,
199 GLenum format, GLenum type,
200 GLvoid *pixels,
201 const struct gl_pixelstore_attrib *packing,
202 GLbitfield transferOps)
203 {
204 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
205
206 if (!rb)
207 return GL_FALSE;
208
209 ASSERT(rb->_BaseFormat == GL_RGBA ||
210 rb->_BaseFormat == GL_RGB ||
211 rb->_BaseFormat == GL_RG ||
212 rb->_BaseFormat == GL_RED ||
213 rb->_BaseFormat == GL_LUMINANCE ||
214 rb->_BaseFormat == GL_INTENSITY ||
215 rb->_BaseFormat == GL_LUMINANCE_ALPHA ||
216 rb->_BaseFormat == GL_ALPHA);
217
218 /* clipping should have already been done */
219 ASSERT(x + width <= (GLint) rb->Width);
220 ASSERT(y + height <= (GLint) rb->Height);
221
222 /* check for things we can't handle here */
223 if (transferOps ||
224 packing->SwapBytes ||
225 packing->LsbFirst) {
226 return GL_FALSE;
227 }
228
229 if (format == GL_RGBA && rb->DataType == type) {
230 const GLint dstStride = _mesa_image_row_stride(packing, width,
231 format, type);
232 GLubyte *dest
233 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
234 format, type, 0, 0);
235 GLint row;
236 ASSERT(rb->GetRow);
237 for (row = 0; row < height; row++) {
238 rb->GetRow(ctx, rb, width, x, y + row, dest);
239 dest += dstStride;
240 }
241 return GL_TRUE;
242 }
243
244 if (format == GL_RGB &&
245 rb->DataType == GL_UNSIGNED_BYTE &&
246 type == GL_UNSIGNED_BYTE) {
247 const GLint dstStride = _mesa_image_row_stride(packing, width,
248 format, type);
249 GLubyte *dest
250 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
251 format, type, 0, 0);
252 GLint row;
253 ASSERT(rb->GetRow);
254 for (row = 0; row < height; row++) {
255 GLubyte tempRow[MAX_WIDTH][4];
256 GLint col;
257 rb->GetRow(ctx, rb, width, x, y + row, tempRow);
258 /* convert RGBA to RGB */
259 for (col = 0; col < width; col++) {
260 dest[col * 3 + 0] = tempRow[col][0];
261 dest[col * 3 + 1] = tempRow[col][1];
262 dest[col * 3 + 2] = tempRow[col][2];
263 }
264 dest += dstStride;
265 }
266 return GL_TRUE;
267 }
268
269 /* not handled */
270 return GL_FALSE;
271 }
272
273 static GLboolean
274 fast_read_rgba_pixels_memcpy( struct gl_context *ctx,
275 GLint x, GLint y,
276 GLsizei width, GLsizei height,
277 GLenum format, GLenum type,
278 GLvoid *pixels,
279 const struct gl_pixelstore_attrib *packing,
280 GLbitfield transferOps )
281 {
282 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
283 GLubyte *dst, *map;
284 int dstStride, stride, j, texelBytes;
285
286 if (!_mesa_format_matches_format_and_type(rb->Format, format, type))
287 return GL_FALSE;
288
289 /* check for things we can't handle here */
290 if (packing->SwapBytes ||
291 packing->LsbFirst) {
292 return GL_FALSE;
293 }
294
295 dstStride = _mesa_image_row_stride(packing, width, format, type);
296 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
297 format, type, 0, 0);
298
299 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
300 &map, &stride);
301
302 texelBytes = _mesa_get_format_bytes(rb->Format);
303 for (j = 0; j < height; j++) {
304 memcpy(dst, map, width * texelBytes);
305 dst += dstStride;
306 map += stride;
307 }
308
309 ctx->Driver.UnmapRenderbuffer(ctx, rb);
310
311 return GL_TRUE;
312 }
313
314
315 /**
316 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
317 * we have to adjust our color values a bit to pass conformance.
318 * The problem is when a 5 or 6-bit color value is converted to an 8-bit
319 * value and then a floating point value, the floating point values don't
320 * increment uniformly as the 5 or 6-bit value is incremented.
321 *
322 * This function adjusts floating point values to compensate.
323 */
324 static void
325 adjust_colors(const struct gl_framebuffer *fb, GLuint n, GLfloat rgba[][4])
326 {
327 const GLuint rShift = 8 - fb->Visual.redBits;
328 const GLuint gShift = 8 - fb->Visual.greenBits;
329 const GLuint bShift = 8 - fb->Visual.blueBits;
330 GLfloat rScale = 1.0F / (GLfloat) ((1 << fb->Visual.redBits ) - 1);
331 GLfloat gScale = 1.0F / (GLfloat) ((1 << fb->Visual.greenBits) - 1);
332 GLfloat bScale = 1.0F / (GLfloat) ((1 << fb->Visual.blueBits ) - 1);
333 GLuint i;
334
335 if (fb->Visual.redBits == 0)
336 rScale = 0;
337 if (fb->Visual.greenBits == 0)
338 gScale = 0;
339 if (fb->Visual.blueBits == 0)
340 bScale = 0;
341
342 for (i = 0; i < n; i++) {
343 GLint r, g, b;
344 /* convert float back to ubyte */
345 CLAMPED_FLOAT_TO_UBYTE(r, rgba[i][RCOMP]);
346 CLAMPED_FLOAT_TO_UBYTE(g, rgba[i][GCOMP]);
347 CLAMPED_FLOAT_TO_UBYTE(b, rgba[i][BCOMP]);
348 /* using only the N most significant bits of the ubyte value, convert to
349 * float in [0,1].
350 */
351 rgba[i][RCOMP] = (GLfloat) (r >> rShift) * rScale;
352 rgba[i][GCOMP] = (GLfloat) (g >> gShift) * gScale;
353 rgba[i][BCOMP] = (GLfloat) (b >> bShift) * bScale;
354 }
355 }
356
357
358
359 /*
360 * Read R, G, B, A, RGB, L, or LA pixels.
361 */
362 static void
363 read_rgba_pixels( struct gl_context *ctx,
364 GLint x, GLint y,
365 GLsizei width, GLsizei height,
366 GLenum format, GLenum type, GLvoid *pixels,
367 const struct gl_pixelstore_attrib *packing )
368 {
369 SWcontext *swrast = SWRAST_CONTEXT(ctx);
370 GLbitfield transferOps = ctx->_ImageTransferState;
371 struct gl_framebuffer *fb = ctx->ReadBuffer;
372 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
373
374 if (!rb)
375 return;
376
377 if ((ctx->Color._ClampReadColor == GL_TRUE || type != GL_FLOAT) &&
378 !_mesa_is_integer_format(format)) {
379 transferOps |= IMAGE_CLAMP_BIT;
380 }
381
382 if (!transferOps) {
383 /* Try the optimized paths first. */
384 if (fast_read_rgba_pixels_memcpy(ctx, x, y, width, height,
385 format, type, pixels, packing,
386 transferOps)) {
387 return;
388 }
389
390 if (fast_read_rgba_pixels(ctx, x, y, width, height,
391 format, type, pixels, packing, transferOps)) {
392 return;
393 }
394 }
395
396 /* width should never be > MAX_WIDTH since we did clipping earlier */
397 ASSERT(width <= MAX_WIDTH);
398
399 {
400 const GLint dstStride
401 = _mesa_image_row_stride(packing, width, format, type);
402 GLfloat (*rgba)[4] = swrast->SpanArrays->attribs[FRAG_ATTRIB_COL0];
403 GLint row;
404 GLubyte *dst
405 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
406 format, type, 0, 0);
407
408 for (row = 0; row < height; row++, y++) {
409
410 /* Get float rgba pixels */
411 _swrast_read_rgba_span(ctx, rb, width, x, y, GL_FLOAT, rgba);
412
413 /* apply fudge factor for shallow color buffers */
414 if ((fb->Visual.redBits < 8 && fb->Visual.redBits != 0) ||
415 (fb->Visual.greenBits < 8 && fb->Visual.greenBits != 0) ||
416 (fb->Visual.blueBits < 8 && fb->Visual.blueBits != 0)) {
417 adjust_colors(fb, width, rgba);
418 }
419
420 /* pack the row of RGBA pixels into user's buffer */
421 _mesa_pack_rgba_span_float(ctx, width, rgba, format, type, dst,
422 packing, transferOps);
423
424 dst += dstStride;
425 }
426 }
427 }
428
429 /**
430 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
431 * data (possibly swapping 8/24 vs 24/8 as we go).
432 */
433 static GLboolean
434 fast_read_depth_stencil_pixels(struct gl_context *ctx,
435 GLint x, GLint y,
436 GLsizei width, GLsizei height,
437 GLvoid *dst, int dstStride)
438 {
439 struct gl_framebuffer *fb = ctx->ReadBuffer;
440 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
441 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
442 GLubyte *map;
443 int stride, i;
444
445 if (rb != stencilRb)
446 return GL_FALSE;
447
448 if (rb->Format != MESA_FORMAT_Z24_S8 &&
449 rb->Format != MESA_FORMAT_S8_Z24)
450 return GL_FALSE;
451
452 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
453 &map, &stride);
454
455 for (i = 0; i < height; i++) {
456 _mesa_unpack_uint_24_8_depth_stencil_row(rb->Format, width,
457 map, (GLuint *)dst);
458 map += stride;
459 dst += dstStride;
460 }
461
462 ctx->Driver.UnmapRenderbuffer(ctx, rb);
463
464 return GL_TRUE;
465 }
466
467
468 /**
469 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
470 * copy the integer data directly instead of converting depth to float and
471 * re-packing.
472 */
473 static GLboolean
474 fast_read_depth_stencil_pixels_separate(struct gl_context *ctx,
475 GLint x, GLint y,
476 GLsizei width, GLsizei height,
477 uint32_t *dst, int dstStride)
478 {
479 struct gl_framebuffer *fb = ctx->ReadBuffer;
480 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
481 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
482 GLubyte *depthMap, *stencilMap;
483 int depthStride, stencilStride, i, j;
484
485 if (_mesa_get_format_datatype(depthRb->Format) != GL_UNSIGNED_INT)
486 return GL_FALSE;
487
488 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
489 GL_MAP_READ_BIT, &depthMap, &depthStride);
490 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
491 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
492
493 for (j = 0; j < height; j++) {
494 GLstencil stencilVals[MAX_WIDTH];
495
496 _mesa_unpack_uint_z_row(depthRb->Format, width, depthMap, dst);
497 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
498 stencilMap, stencilVals);
499
500 for (i = 0; i < width; i++) {
501 dst[i] = (dst[i] & 0xffffff00) | stencilVals[i];
502 }
503
504 depthMap += depthStride;
505 stencilMap += stencilStride;
506 dst += dstStride / 4;
507 }
508
509 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
510 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
511
512 return GL_TRUE;
513 }
514
515 static void
516 slow_read_depth_stencil_pixels_separate(struct gl_context *ctx,
517 GLint x, GLint y,
518 GLsizei width, GLsizei height,
519 GLenum type,
520 const struct gl_pixelstore_attrib *packing,
521 GLubyte *dst, int dstStride)
522 {
523 struct gl_framebuffer *fb = ctx->ReadBuffer;
524 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
525 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
526 GLubyte *depthMap, *stencilMap;
527 int depthStride, stencilStride, j;
528
529 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
530 GL_MAP_READ_BIT, &depthMap, &depthStride);
531 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
532 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
533
534 for (j = 0; j < height; j++) {
535 GLstencil stencilVals[MAX_WIDTH];
536 GLfloat depthVals[MAX_WIDTH];
537
538 _mesa_unpack_float_z_row(depthRb->Format, width, depthMap, depthVals);
539 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
540 stencilMap, stencilVals);
541
542 _mesa_pack_depth_stencil_span(ctx, width, type, (GLuint *)dst,
543 depthVals, stencilVals, packing);
544
545 depthMap += depthStride;
546 stencilMap += stencilStride;
547 dst += dstStride;
548 }
549
550 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
551 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
552 }
553
554
555 /**
556 * Read combined depth/stencil values.
557 * We'll have already done error checking to be sure the expected
558 * depth and stencil buffers really exist.
559 */
560 static void
561 read_depth_stencil_pixels(struct gl_context *ctx,
562 GLint x, GLint y,
563 GLsizei width, GLsizei height,
564 GLenum type, GLvoid *pixels,
565 const struct gl_pixelstore_attrib *packing )
566 {
567 const GLboolean scaleOrBias
568 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
569 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
570 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
571 GLubyte *dst;
572 int dstStride;
573
574 dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
575 width, height,
576 GL_DEPTH_STENCIL_EXT,
577 type, 0, 0);
578 dstStride = _mesa_image_row_stride(packing, width,
579 GL_DEPTH_STENCIL_EXT, type);
580
581 /* Fast 24/8 reads. */
582 if (type == GL_UNSIGNED_INT_24_8 &&
583 !scaleOrBias && !stencilTransfer && !packing->SwapBytes) {
584 if (fast_read_depth_stencil_pixels(ctx, x, y, width, height,
585 dst, dstStride))
586 return;
587
588 if (fast_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
589 (uint32_t *)dst, dstStride))
590 return;
591 }
592
593 slow_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
594 type, packing,
595 dst, dstStride);
596 }
597
598
599
600 /**
601 * Software fallback routine for ctx->Driver.ReadPixels().
602 * By time we get here, all error checking will have been done.
603 */
604 void
605 _swrast_ReadPixels( struct gl_context *ctx,
606 GLint x, GLint y, GLsizei width, GLsizei height,
607 GLenum format, GLenum type,
608 const struct gl_pixelstore_attrib *packing,
609 GLvoid *pixels )
610 {
611 SWcontext *swrast = SWRAST_CONTEXT(ctx);
612 struct gl_pixelstore_attrib clippedPacking = *packing;
613
614 if (ctx->NewState)
615 _mesa_update_state(ctx);
616
617 /* Need to do swrast_render_start() before clipping or anything else
618 * since this is where a driver may grab the hw lock and get an updated
619 * window size.
620 */
621 swrast_render_start(ctx);
622
623 if (swrast->NewState)
624 _swrast_validate_derived( ctx );
625
626 /* Do all needed clipping here, so that we can forget about it later */
627 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
628
629 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
630
631 if (pixels) {
632 switch (format) {
633 case GL_STENCIL_INDEX:
634 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
635 &clippedPacking);
636 break;
637 case GL_DEPTH_COMPONENT:
638 read_depth_pixels(ctx, x, y, width, height, type, pixels,
639 &clippedPacking);
640 break;
641 case GL_DEPTH_STENCIL_EXT:
642 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
643 &clippedPacking);
644 break;
645 default:
646 /* all other formats should be color formats */
647 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
648 &clippedPacking);
649 }
650
651 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
652 }
653 }
654
655 swrast_render_finish(ctx);
656 }