Drop GLcontext typedef and use struct gl_context instead
[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/bufferobj.h"
28 #include "main/colormac.h"
29 #include "main/feedback.h"
30 #include "main/formats.h"
31 #include "main/image.h"
32 #include "main/macros.h"
33 #include "main/imports.h"
34 #include "main/state.h"
35
36 #include "s_context.h"
37 #include "s_depth.h"
38 #include "s_span.h"
39 #include "s_stencil.h"
40
41
42 /**
43 * Read pixels for format=GL_DEPTH_COMPONENT.
44 */
45 static void
46 read_depth_pixels( struct gl_context *ctx,
47 GLint x, GLint y,
48 GLsizei width, GLsizei height,
49 GLenum type, GLvoid *pixels,
50 const struct gl_pixelstore_attrib *packing )
51 {
52 struct gl_framebuffer *fb = ctx->ReadBuffer;
53 struct gl_renderbuffer *rb = fb->_DepthBuffer;
54 const GLboolean biasOrScale
55 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
56
57 if (!rb)
58 return;
59
60 /* clipping should have been done already */
61 ASSERT(x >= 0);
62 ASSERT(y >= 0);
63 ASSERT(x + width <= (GLint) rb->Width);
64 ASSERT(y + height <= (GLint) rb->Height);
65 /* width should never be > MAX_WIDTH since we did clipping earlier */
66 ASSERT(width <= MAX_WIDTH);
67
68 if (type == GL_UNSIGNED_SHORT && fb->Visual.depthBits == 16
69 && !biasOrScale && !packing->SwapBytes) {
70 /* Special case: directly read 16-bit unsigned depth values. */
71 GLint j;
72 ASSERT(rb->Format == MESA_FORMAT_Z16);
73 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
74 for (j = 0; j < height; j++, y++) {
75 void *dest =_mesa_image_address2d(packing, pixels, width, height,
76 GL_DEPTH_COMPONENT, type, j, 0);
77 rb->GetRow(ctx, rb, width, x, y, dest);
78 }
79 }
80 else if (type == GL_UNSIGNED_INT && fb->Visual.depthBits == 24
81 && !biasOrScale && !packing->SwapBytes) {
82 /* Special case: directly read 24-bit unsigned depth values. */
83 GLint j;
84 ASSERT(rb->Format == MESA_FORMAT_X8_Z24 ||
85 rb->Format == MESA_FORMAT_S8_Z24 ||
86 rb->Format == MESA_FORMAT_Z24_X8 ||
87 rb->Format == MESA_FORMAT_Z24_S8);
88 ASSERT(rb->DataType == GL_UNSIGNED_INT ||
89 rb->DataType == GL_UNSIGNED_INT_24_8);
90 for (j = 0; j < height; j++, y++) {
91 GLuint *dest = (GLuint *)
92 _mesa_image_address2d(packing, pixels, width, height,
93 GL_DEPTH_COMPONENT, type, j, 0);
94 GLint k;
95 rb->GetRow(ctx, rb, width, x, y, dest);
96 /* convert range from 24-bit to 32-bit */
97 if (rb->Format == MESA_FORMAT_X8_Z24 ||
98 rb->Format == MESA_FORMAT_S8_Z24) {
99 for (k = 0; k < width; k++) {
100 /* Note: put MSByte of 24-bit value into LSByte */
101 dest[k] = (dest[k] << 8) | ((dest[k] >> 16) & 0xff);
102 }
103 }
104 else {
105 for (k = 0; k < width; k++) {
106 /* Note: fill in LSByte by replication */
107 dest[k] = dest[k] | ((dest[k] >> 8) & 0xff);
108 }
109 }
110 }
111 }
112 else if (type == GL_UNSIGNED_INT && fb->Visual.depthBits == 32
113 && !biasOrScale && !packing->SwapBytes) {
114 /* Special case: directly read 32-bit unsigned depth values. */
115 GLint j;
116 ASSERT(rb->Format == MESA_FORMAT_Z32);
117 ASSERT(rb->DataType == GL_UNSIGNED_INT);
118 for (j = 0; j < height; j++, y++) {
119 void *dest = _mesa_image_address2d(packing, pixels, width, height,
120 GL_DEPTH_COMPONENT, type, j, 0);
121 rb->GetRow(ctx, rb, width, x, y, dest);
122 }
123 }
124 else {
125 /* General case (slower) */
126 GLint j;
127 for (j = 0; j < height; j++, y++) {
128 GLfloat depthValues[MAX_WIDTH];
129 GLvoid *dest = _mesa_image_address2d(packing, pixels, width, height,
130 GL_DEPTH_COMPONENT, type, j, 0);
131 _swrast_read_depth_span_float(ctx, rb, width, x, y, depthValues);
132 _mesa_pack_depth_span(ctx, width, dest, type, depthValues, packing);
133 }
134 }
135 }
136
137
138 /**
139 * Read pixels for format=GL_STENCIL_INDEX.
140 */
141 static void
142 read_stencil_pixels( struct gl_context *ctx,
143 GLint x, GLint y,
144 GLsizei width, GLsizei height,
145 GLenum type, GLvoid *pixels,
146 const struct gl_pixelstore_attrib *packing )
147 {
148 struct gl_framebuffer *fb = ctx->ReadBuffer;
149 struct gl_renderbuffer *rb = fb->_StencilBuffer;
150 GLint j;
151
152 if (!rb)
153 return;
154
155 /* width should never be > MAX_WIDTH since we did clipping earlier */
156 ASSERT(width <= MAX_WIDTH);
157
158 /* process image row by row */
159 for (j=0;j<height;j++,y++) {
160 GLvoid *dest;
161 GLstencil stencil[MAX_WIDTH];
162
163 _swrast_read_stencil_span(ctx, rb, width, x, y, stencil);
164
165 dest = _mesa_image_address2d(packing, pixels, width, height,
166 GL_STENCIL_INDEX, type, j, 0);
167
168 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
169 }
170 }
171
172
173
174 /**
175 * Optimized glReadPixels for particular pixel formats when pixel
176 * scaling, biasing, mapping, etc. are disabled.
177 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
178 */
179 static GLboolean
180 fast_read_rgba_pixels( struct gl_context *ctx,
181 GLint x, GLint y,
182 GLsizei width, GLsizei height,
183 GLenum format, GLenum type,
184 GLvoid *pixels,
185 const struct gl_pixelstore_attrib *packing,
186 GLbitfield transferOps)
187 {
188 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
189
190 if (!rb)
191 return GL_FALSE;
192
193 ASSERT(rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB ||
194 rb->_BaseFormat == GL_ALPHA);
195
196 /* clipping should have already been done */
197 ASSERT(x + width <= (GLint) rb->Width);
198 ASSERT(y + height <= (GLint) rb->Height);
199
200 /* check for things we can't handle here */
201 if (transferOps ||
202 packing->SwapBytes ||
203 packing->LsbFirst) {
204 return GL_FALSE;
205 }
206
207 if (format == GL_RGBA && rb->DataType == type) {
208 const GLint dstStride = _mesa_image_row_stride(packing, width,
209 format, type);
210 GLubyte *dest
211 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
212 format, type, 0, 0);
213 GLint row;
214 ASSERT(rb->GetRow);
215 for (row = 0; row < height; row++) {
216 rb->GetRow(ctx, rb, width, x, y + row, dest);
217 dest += dstStride;
218 }
219 return GL_TRUE;
220 }
221
222 if (format == GL_RGB &&
223 rb->DataType == GL_UNSIGNED_BYTE &&
224 type == GL_UNSIGNED_BYTE) {
225 const GLint dstStride = _mesa_image_row_stride(packing, width,
226 format, type);
227 GLubyte *dest
228 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
229 format, type, 0, 0);
230 GLint row;
231 ASSERT(rb->GetRow);
232 for (row = 0; row < height; row++) {
233 GLubyte tempRow[MAX_WIDTH][4];
234 GLint col;
235 rb->GetRow(ctx, rb, width, x, y + row, tempRow);
236 /* convert RGBA to RGB */
237 for (col = 0; col < width; col++) {
238 dest[col * 3 + 0] = tempRow[col][0];
239 dest[col * 3 + 1] = tempRow[col][1];
240 dest[col * 3 + 2] = tempRow[col][2];
241 }
242 dest += dstStride;
243 }
244 return GL_TRUE;
245 }
246
247 /* not handled */
248 return GL_FALSE;
249 }
250
251
252 /**
253 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
254 * we have to adjust our color values a bit to pass conformance.
255 * The problem is when a 5 or 6-bit color value is converted to an 8-bit
256 * value and then a floating point value, the floating point values don't
257 * increment uniformly as the 5 or 6-bit value is incremented.
258 *
259 * This function adjusts floating point values to compensate.
260 */
261 static void
262 adjust_colors(const struct gl_framebuffer *fb, GLuint n, GLfloat rgba[][4])
263 {
264 const GLuint rShift = 8 - fb->Visual.redBits;
265 const GLuint gShift = 8 - fb->Visual.greenBits;
266 const GLuint bShift = 8 - fb->Visual.blueBits;
267 GLfloat rScale = 1.0F / (GLfloat) ((1 << fb->Visual.redBits ) - 1);
268 GLfloat gScale = 1.0F / (GLfloat) ((1 << fb->Visual.greenBits) - 1);
269 GLfloat bScale = 1.0F / (GLfloat) ((1 << fb->Visual.blueBits ) - 1);
270 GLuint i;
271
272 if (fb->Visual.redBits == 0)
273 rScale = 0;
274 if (fb->Visual.greenBits == 0)
275 gScale = 0;
276 if (fb->Visual.blueBits == 0)
277 bScale = 0;
278
279 for (i = 0; i < n; i++) {
280 GLint r, g, b;
281 /* convert float back to ubyte */
282 CLAMPED_FLOAT_TO_UBYTE(r, rgba[i][RCOMP]);
283 CLAMPED_FLOAT_TO_UBYTE(g, rgba[i][GCOMP]);
284 CLAMPED_FLOAT_TO_UBYTE(b, rgba[i][BCOMP]);
285 /* using only the N most significant bits of the ubyte value, convert to
286 * float in [0,1].
287 */
288 rgba[i][RCOMP] = (GLfloat) (r >> rShift) * rScale;
289 rgba[i][GCOMP] = (GLfloat) (g >> gShift) * gScale;
290 rgba[i][BCOMP] = (GLfloat) (b >> bShift) * bScale;
291 }
292 }
293
294
295
296 /*
297 * Read R, G, B, A, RGB, L, or LA pixels.
298 */
299 static void
300 read_rgba_pixels( struct gl_context *ctx,
301 GLint x, GLint y,
302 GLsizei width, GLsizei height,
303 GLenum format, GLenum type, GLvoid *pixels,
304 const struct gl_pixelstore_attrib *packing )
305 {
306 SWcontext *swrast = SWRAST_CONTEXT(ctx);
307 GLbitfield transferOps = ctx->_ImageTransferState;
308 struct gl_framebuffer *fb = ctx->ReadBuffer;
309 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
310
311 if (!rb)
312 return;
313
314 if (type == GL_FLOAT && ((ctx->Color.ClampReadColor == GL_TRUE) ||
315 (ctx->Color.ClampReadColor == GL_FIXED_ONLY_ARB &&
316 rb->DataType != GL_FLOAT)))
317 transferOps |= IMAGE_CLAMP_BIT;
318
319 /* Try optimized path first */
320 if (fast_read_rgba_pixels(ctx, x, y, width, height,
321 format, type, pixels, packing, transferOps)) {
322 return; /* done! */
323 }
324
325 /* width should never be > MAX_WIDTH since we did clipping earlier */
326 ASSERT(width <= MAX_WIDTH);
327
328 do {
329 const GLint dstStride
330 = _mesa_image_row_stride(packing, width, format, type);
331 GLfloat (*rgba)[4] = swrast->SpanArrays->attribs[FRAG_ATTRIB_COL0];
332 GLint row;
333 GLubyte *dst
334 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
335 format, type, 0, 0);
336
337 for (row = 0; row < height; row++, y++) {
338
339 /* Get float rgba pixels */
340 _swrast_read_rgba_span(ctx, rb, width, x, y, GL_FLOAT, rgba);
341
342 /* apply fudge factor for shallow color buffers */
343 if (fb->Visual.redBits < 8 ||
344 fb->Visual.greenBits < 8 ||
345 fb->Visual.blueBits < 8) {
346 adjust_colors(fb, width, rgba);
347 }
348
349 /* pack the row of RGBA pixels into user's buffer */
350 _mesa_pack_rgba_span_float(ctx, width, rgba, format, type, dst,
351 packing, transferOps);
352
353 dst += dstStride;
354 }
355 } while (0);
356 }
357
358
359 /**
360 * Read combined depth/stencil values.
361 * We'll have already done error checking to be sure the expected
362 * depth and stencil buffers really exist.
363 */
364 static void
365 read_depth_stencil_pixels(struct gl_context *ctx,
366 GLint x, GLint y,
367 GLsizei width, GLsizei height,
368 GLenum type, GLvoid *pixels,
369 const struct gl_pixelstore_attrib *packing )
370 {
371 const GLboolean scaleOrBias
372 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
373 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
374 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
375 struct gl_renderbuffer *depthRb, *stencilRb;
376
377 depthRb = ctx->ReadBuffer->_DepthBuffer;
378 stencilRb = ctx->ReadBuffer->_StencilBuffer;
379
380 if (!depthRb || !stencilRb)
381 return;
382
383 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
384 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
385
386 if (depthRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
387 stencilRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
388 depthRb == stencilRb &&
389 !scaleOrBias &&
390 !stencilTransfer) {
391 /* This is the ideal case.
392 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
393 * Plus, no pixel transfer ops to worry about!
394 */
395 GLint i;
396 GLint dstStride = _mesa_image_row_stride(packing, width,
397 GL_DEPTH_STENCIL_EXT, type);
398 GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
399 width, height,
400 GL_DEPTH_STENCIL_EXT,
401 type, 0, 0);
402 for (i = 0; i < height; i++) {
403 depthRb->GetRow(ctx, depthRb, width, x, y + i, dst);
404 dst += dstStride;
405 }
406 }
407 else {
408 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
409 * or we need pixel transfer.
410 */
411 GLint i;
412 depthRb = ctx->ReadBuffer->_DepthBuffer;
413 stencilRb = ctx->ReadBuffer->_StencilBuffer;
414
415 for (i = 0; i < height; i++) {
416 GLstencil stencilVals[MAX_WIDTH];
417
418 GLuint *depthStencilDst = (GLuint *)
419 _mesa_image_address2d(packing, pixels, width, height,
420 GL_DEPTH_STENCIL_EXT, type, i, 0);
421
422 _swrast_read_stencil_span(ctx, stencilRb, width,
423 x, y + i, stencilVals);
424
425 if (!scaleOrBias && !stencilTransfer
426 && ctx->ReadBuffer->Visual.depthBits == 24) {
427 /* ideal case */
428 GLuint zVals[MAX_WIDTH]; /* 24-bit values! */
429 GLint j;
430 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
431 /* note, we've already been clipped */
432 depthRb->GetRow(ctx, depthRb, width, x, y + i, zVals);
433 for (j = 0; j < width; j++) {
434 depthStencilDst[j] = (zVals[j] << 8) | (stencilVals[j] & 0xff);
435 }
436 }
437 else {
438 /* general case */
439 GLfloat depthVals[MAX_WIDTH];
440 _swrast_read_depth_span_float(ctx, depthRb, width, x, y + i,
441 depthVals);
442 _mesa_pack_depth_stencil_span(ctx, width, depthStencilDst,
443 depthVals, stencilVals, packing);
444 }
445 }
446 }
447 }
448
449
450
451 /**
452 * Software fallback routine for ctx->Driver.ReadPixels().
453 * By time we get here, all error checking will have been done.
454 */
455 void
456 _swrast_ReadPixels( struct gl_context *ctx,
457 GLint x, GLint y, GLsizei width, GLsizei height,
458 GLenum format, GLenum type,
459 const struct gl_pixelstore_attrib *packing,
460 GLvoid *pixels )
461 {
462 SWcontext *swrast = SWRAST_CONTEXT(ctx);
463 struct gl_pixelstore_attrib clippedPacking = *packing;
464
465 if (ctx->NewState)
466 _mesa_update_state(ctx);
467
468 /* Need to do swrast_render_start() before clipping or anything else
469 * since this is where a driver may grab the hw lock and get an updated
470 * window size.
471 */
472 swrast_render_start(ctx);
473
474 if (swrast->NewState)
475 _swrast_validate_derived( ctx );
476
477 /* Do all needed clipping here, so that we can forget about it later */
478 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
479 /* The ReadPixels region is totally outside the window bounds */
480 swrast_render_finish(ctx);
481 return;
482 }
483
484 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
485 if (!pixels)
486 return;
487
488 switch (format) {
489 case GL_STENCIL_INDEX:
490 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
491 &clippedPacking);
492 break;
493 case GL_DEPTH_COMPONENT:
494 read_depth_pixels(ctx, x, y, width, height, type, pixels,
495 &clippedPacking);
496 break;
497 case GL_RED:
498 case GL_GREEN:
499 case GL_BLUE:
500 case GL_ALPHA:
501 case GL_RGB:
502 case GL_LUMINANCE:
503 case GL_LUMINANCE_ALPHA:
504 case GL_RGBA:
505 case GL_BGR:
506 case GL_BGRA:
507 case GL_ABGR_EXT:
508 read_rgba_pixels(ctx, x, y, width, height,
509 format, type, pixels, &clippedPacking);
510 break;
511 case GL_DEPTH_STENCIL_EXT:
512 read_depth_stencil_pixels(ctx, x, y, width, height,
513 type, pixels, &clippedPacking);
514 break;
515 default:
516 _mesa_problem(ctx, "unexpected format in _swrast_ReadPixels");
517 /* don't return yet, clean-up */
518 }
519
520 swrast_render_finish(ctx);
521
522 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
523 }