swrast: new fast_draw_depth_stencil() for glDrawPixels(GL_DEPTH_STENCIL)
[mesa.git] / src / mesa / swrast / s_drawpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
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/condrender.h"
29 #include "main/context.h"
30 #include "main/image.h"
31 #include "main/imports.h"
32 #include "main/macros.h"
33 #include "main/pack.h"
34 #include "main/pbo.h"
35 #include "main/pixeltransfer.h"
36 #include "main/state.h"
37
38 #include "s_context.h"
39 #include "s_span.h"
40 #include "s_stencil.h"
41 #include "s_zoom.h"
42
43
44
45 /**
46 * Try to do a fast and simple RGB(a) glDrawPixels.
47 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
48 */
49 static GLboolean
50 fast_draw_rgba_pixels(struct gl_context *ctx, GLint x, GLint y,
51 GLsizei width, GLsizei height,
52 GLenum format, GLenum type,
53 const struct gl_pixelstore_attrib *userUnpack,
54 const GLvoid *pixels)
55 {
56 const GLint imgX = x, imgY = y;
57 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
58 GLenum rbType;
59 SWcontext *swrast = SWRAST_CONTEXT(ctx);
60 SWspan span;
61 GLboolean simpleZoom;
62 GLint yStep; /* +1 or -1 */
63 struct gl_pixelstore_attrib unpack;
64 GLint destX, destY, drawWidth, drawHeight; /* post clipping */
65
66 if (!rb)
67 return GL_TRUE; /* no-op */
68
69 rbType = rb->DataType;
70
71 if ((swrast->_RasterMask & ~CLIP_BIT) ||
72 ctx->Texture._EnabledCoordUnits ||
73 userUnpack->SwapBytes ||
74 ctx->_ImageTransferState) {
75 /* can't handle any of those conditions */
76 return GL_FALSE;
77 }
78
79 INIT_SPAN(span, GL_BITMAP);
80 span.arrayMask = SPAN_RGBA;
81 span.arrayAttribs = FRAG_BIT_COL0;
82 _swrast_span_default_attribs(ctx, &span);
83
84 /* copy input params since clipping may change them */
85 unpack = *userUnpack;
86 destX = x;
87 destY = y;
88 drawWidth = width;
89 drawHeight = height;
90
91 /* check for simple zooming and clipping */
92 if (ctx->Pixel.ZoomX == 1.0F &&
93 (ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F)) {
94 if (!_mesa_clip_drawpixels(ctx, &destX, &destY,
95 &drawWidth, &drawHeight, &unpack)) {
96 /* image was completely clipped: no-op, all done */
97 return GL_TRUE;
98 }
99 simpleZoom = GL_TRUE;
100 yStep = (GLint) ctx->Pixel.ZoomY;
101 ASSERT(yStep == 1 || yStep == -1);
102 }
103 else {
104 /* non-simple zooming */
105 simpleZoom = GL_FALSE;
106 yStep = 1;
107 if (unpack.RowLength == 0)
108 unpack.RowLength = width;
109 }
110
111 /*
112 * Ready to draw!
113 */
114
115 if (format == GL_RGBA && type == rbType) {
116 const GLubyte *src
117 = (const GLubyte *) _mesa_image_address2d(&unpack, pixels, width,
118 height, format, type, 0, 0);
119 const GLint srcStride = _mesa_image_row_stride(&unpack, width,
120 format, type);
121 if (simpleZoom) {
122 GLint row;
123 for (row = 0; row < drawHeight; row++) {
124 rb->PutRow(ctx, rb, drawWidth, destX, destY, src, NULL);
125 src += srcStride;
126 destY += yStep;
127 }
128 }
129 else {
130 /* with zooming */
131 GLint row;
132 for (row = 0; row < drawHeight; row++) {
133 span.x = destX;
134 span.y = destY + row;
135 span.end = drawWidth;
136 span.array->ChanType = rbType;
137 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, src);
138 src += srcStride;
139 }
140 span.array->ChanType = CHAN_TYPE;
141 }
142 return GL_TRUE;
143 }
144
145 if (format == GL_RGB && type == rbType) {
146 const GLubyte *src
147 = (const GLubyte *) _mesa_image_address2d(&unpack, pixels, width,
148 height, format, type, 0, 0);
149 const GLint srcStride = _mesa_image_row_stride(&unpack, width,
150 format, type);
151 if (simpleZoom) {
152 GLint row;
153 for (row = 0; row < drawHeight; row++) {
154 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, src, NULL);
155 src += srcStride;
156 destY += yStep;
157 }
158 }
159 else {
160 /* with zooming */
161 GLint row;
162 for (row = 0; row < drawHeight; row++) {
163 span.x = destX;
164 span.y = destY;
165 span.end = drawWidth;
166 span.array->ChanType = rbType;
167 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, src);
168 src += srcStride;
169 destY++;
170 }
171 span.array->ChanType = CHAN_TYPE;
172 }
173 return GL_TRUE;
174 }
175
176 /* Remaining cases haven't been tested with alignment != 1 */
177 if (userUnpack->Alignment != 1)
178 return GL_FALSE;
179
180 if (format == GL_LUMINANCE && type == CHAN_TYPE && rbType == CHAN_TYPE) {
181 const GLchan *src = (const GLchan *) pixels
182 + (unpack.SkipRows * unpack.RowLength + unpack.SkipPixels);
183 if (simpleZoom) {
184 /* no zooming */
185 GLint row;
186 ASSERT(drawWidth <= MAX_WIDTH);
187 for (row = 0; row < drawHeight; row++) {
188 GLchan rgb[MAX_WIDTH][3];
189 GLint i;
190 for (i = 0;i<drawWidth;i++) {
191 rgb[i][0] = src[i];
192 rgb[i][1] = src[i];
193 rgb[i][2] = src[i];
194 }
195 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, rgb, NULL);
196 src += unpack.RowLength;
197 destY += yStep;
198 }
199 }
200 else {
201 /* with zooming */
202 GLint row;
203 ASSERT(drawWidth <= MAX_WIDTH);
204 for (row = 0; row < drawHeight; row++) {
205 GLchan rgb[MAX_WIDTH][3];
206 GLint i;
207 for (i = 0;i<drawWidth;i++) {
208 rgb[i][0] = src[i];
209 rgb[i][1] = src[i];
210 rgb[i][2] = src[i];
211 }
212 span.x = destX;
213 span.y = destY;
214 span.end = drawWidth;
215 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, rgb);
216 src += unpack.RowLength;
217 destY++;
218 }
219 }
220 return GL_TRUE;
221 }
222
223 if (format == GL_LUMINANCE_ALPHA && type == CHAN_TYPE && rbType == CHAN_TYPE) {
224 const GLchan *src = (const GLchan *) pixels
225 + (unpack.SkipRows * unpack.RowLength + unpack.SkipPixels)*2;
226 if (simpleZoom) {
227 GLint row;
228 ASSERT(drawWidth <= MAX_WIDTH);
229 for (row = 0; row < drawHeight; row++) {
230 GLint i;
231 const GLchan *ptr = src;
232 for (i = 0;i<drawWidth;i++) {
233 span.array->rgba[i][0] = *ptr;
234 span.array->rgba[i][1] = *ptr;
235 span.array->rgba[i][2] = *ptr++;
236 span.array->rgba[i][3] = *ptr++;
237 }
238 rb->PutRow(ctx, rb, drawWidth, destX, destY,
239 span.array->rgba, NULL);
240 src += unpack.RowLength*2;
241 destY += yStep;
242 }
243 }
244 else {
245 /* with zooming */
246 GLint row;
247 ASSERT(drawWidth <= MAX_WIDTH);
248 for (row = 0; row < drawHeight; row++) {
249 const GLchan *ptr = src;
250 GLint i;
251 for (i = 0;i<drawWidth;i++) {
252 span.array->rgba[i][0] = *ptr;
253 span.array->rgba[i][1] = *ptr;
254 span.array->rgba[i][2] = *ptr++;
255 span.array->rgba[i][3] = *ptr++;
256 }
257 span.x = destX;
258 span.y = destY;
259 span.end = drawWidth;
260 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
261 span.array->rgba);
262 src += unpack.RowLength*2;
263 destY++;
264 }
265 }
266 return GL_TRUE;
267 }
268
269 if (format == GL_COLOR_INDEX && type == GL_UNSIGNED_BYTE) {
270 const GLubyte *src = (const GLubyte *) pixels
271 + unpack.SkipRows * unpack.RowLength + unpack.SkipPixels;
272 if (rbType == GL_UNSIGNED_BYTE) {
273 /* convert ubyte/CI data to ubyte/RGBA */
274 if (simpleZoom) {
275 GLint row;
276 for (row = 0; row < drawHeight; row++) {
277 ASSERT(drawWidth <= MAX_WIDTH);
278 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
279 span.array->rgba8);
280 rb->PutRow(ctx, rb, drawWidth, destX, destY,
281 span.array->rgba8, NULL);
282 src += unpack.RowLength;
283 destY += yStep;
284 }
285 }
286 else {
287 /* ubyte/CI to ubyte/RGBA with zooming */
288 GLint row;
289 for (row = 0; row < drawHeight; row++) {
290 ASSERT(drawWidth <= MAX_WIDTH);
291 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
292 span.array->rgba8);
293 span.x = destX;
294 span.y = destY;
295 span.end = drawWidth;
296 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
297 span.array->rgba8);
298 src += unpack.RowLength;
299 destY++;
300 }
301 }
302 return GL_TRUE;
303 }
304 }
305
306 /* can't handle this pixel format and/or data type */
307 return GL_FALSE;
308 }
309
310
311
312 /*
313 * Draw stencil image.
314 */
315 static void
316 draw_stencil_pixels( struct gl_context *ctx, GLint x, GLint y,
317 GLsizei width, GLsizei height,
318 GLenum type,
319 const struct gl_pixelstore_attrib *unpack,
320 const GLvoid *pixels )
321 {
322 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
323 const GLenum destType = GL_UNSIGNED_BYTE;
324 GLint skipPixels;
325
326 /* if width > MAX_WIDTH, have to process image in chunks */
327 skipPixels = 0;
328 while (skipPixels < width) {
329 const GLint spanX = x + skipPixels;
330 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
331 GLint row;
332 for (row = 0; row < height; row++) {
333 const GLint spanY = y + row;
334 GLubyte values[MAX_WIDTH];
335 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
336 width, height,
337 GL_STENCIL_INDEX, type,
338 row, skipPixels);
339 _mesa_unpack_stencil_span(ctx, spanWidth, destType, values,
340 type, source, unpack,
341 ctx->_ImageTransferState);
342 if (zoom) {
343 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
344 spanX, spanY, values);
345 }
346 else {
347 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
348 }
349 }
350 skipPixels += spanWidth;
351 }
352 }
353
354
355 /*
356 * Draw depth image.
357 */
358 static void
359 draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
360 GLsizei width, GLsizei height,
361 GLenum type,
362 const struct gl_pixelstore_attrib *unpack,
363 const GLvoid *pixels )
364 {
365 const GLboolean scaleOrBias
366 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
367 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
368 SWspan span;
369
370 INIT_SPAN(span, GL_BITMAP);
371 span.arrayMask = SPAN_Z;
372 _swrast_span_default_attribs(ctx, &span);
373
374 if (type == GL_UNSIGNED_SHORT
375 && ctx->DrawBuffer->Visual.depthBits == 16
376 && !scaleOrBias
377 && !zoom
378 && width <= MAX_WIDTH
379 && !unpack->SwapBytes) {
380 /* Special case: directly write 16-bit depth values */
381 GLint row;
382 for (row = 0; row < height; row++) {
383 const GLushort *zSrc = (const GLushort *)
384 _mesa_image_address2d(unpack, pixels, width, height,
385 GL_DEPTH_COMPONENT, type, row, 0);
386 GLint i;
387 for (i = 0; i < width; i++)
388 span.array->z[i] = zSrc[i];
389 span.x = x;
390 span.y = y + row;
391 span.end = width;
392 _swrast_write_rgba_span(ctx, &span);
393 }
394 }
395 else if (type == GL_UNSIGNED_INT
396 && !scaleOrBias
397 && !zoom
398 && width <= MAX_WIDTH
399 && !unpack->SwapBytes) {
400 /* Special case: shift 32-bit values down to Visual.depthBits */
401 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
402 GLint row;
403 for (row = 0; row < height; row++) {
404 const GLuint *zSrc = (const GLuint *)
405 _mesa_image_address2d(unpack, pixels, width, height,
406 GL_DEPTH_COMPONENT, type, row, 0);
407 if (shift == 0) {
408 memcpy(span.array->z, zSrc, width * sizeof(GLuint));
409 }
410 else {
411 GLint col;
412 for (col = 0; col < width; col++)
413 span.array->z[col] = zSrc[col] >> shift;
414 }
415 span.x = x;
416 span.y = y + row;
417 span.end = width;
418 _swrast_write_rgba_span(ctx, &span);
419 }
420 }
421 else {
422 /* General case */
423 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
424 GLint skipPixels = 0;
425
426 /* in case width > MAX_WIDTH do the copy in chunks */
427 while (skipPixels < width) {
428 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
429 GLint row;
430 ASSERT(span.end <= MAX_WIDTH);
431 for (row = 0; row < height; row++) {
432 const GLvoid *zSrc = _mesa_image_address2d(unpack,
433 pixels, width, height,
434 GL_DEPTH_COMPONENT, type,
435 row, skipPixels);
436
437 /* Set these for each row since the _swrast_write_* function may
438 * change them while clipping.
439 */
440 span.x = x + skipPixels;
441 span.y = y + row;
442 span.end = spanWidth;
443
444 _mesa_unpack_depth_span(ctx, spanWidth,
445 GL_UNSIGNED_INT, span.array->z, depthMax,
446 type, zSrc, unpack);
447 if (zoom) {
448 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
449 }
450 else {
451 _swrast_write_rgba_span(ctx, &span);
452 }
453 }
454 skipPixels += spanWidth;
455 }
456 }
457 }
458
459
460
461 /**
462 * Draw RGBA image.
463 */
464 static void
465 draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
466 GLsizei width, GLsizei height,
467 GLenum format, GLenum type,
468 const struct gl_pixelstore_attrib *unpack,
469 const GLvoid *pixels )
470 {
471 const GLint imgX = x, imgY = y;
472 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
473 GLfloat *convImage = NULL;
474 GLbitfield transferOps = ctx->_ImageTransferState;
475 SWspan span;
476
477 /* Try an optimized glDrawPixels first */
478 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
479 unpack, pixels)) {
480 return;
481 }
482
483 INIT_SPAN(span, GL_BITMAP);
484 _swrast_span_default_attribs(ctx, &span);
485 span.arrayMask = SPAN_RGBA;
486 span.arrayAttribs = FRAG_BIT_COL0; /* we're fill in COL0 attrib values */
487
488 if (ctx->DrawBuffer->_NumColorDrawBuffers > 0 &&
489 ctx->DrawBuffer->_ColorDrawBuffers[0]->DataType != GL_FLOAT &&
490 ctx->Color.ClampFragmentColor != GL_FALSE) {
491 /* need to clamp colors before applying fragment ops */
492 transferOps |= IMAGE_CLAMP_BIT;
493 }
494
495 /*
496 * General solution
497 */
498 {
499 const GLbitfield interpMask = span.interpMask;
500 const GLbitfield arrayMask = span.arrayMask;
501 const GLint srcStride
502 = _mesa_image_row_stride(unpack, width, format, type);
503 GLint skipPixels = 0;
504 /* use span array for temp color storage */
505 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
506
507 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
508 while (skipPixels < width) {
509 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
510 const GLubyte *source
511 = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
512 width, height, format,
513 type, 0, skipPixels);
514 GLint row;
515
516 for (row = 0; row < height; row++) {
517 /* get image row as float/RGBA */
518 _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
519 format, type, source, unpack,
520 transferOps);
521 /* Set these for each row since the _swrast_write_* functions
522 * may change them while clipping/rendering.
523 */
524 span.array->ChanType = GL_FLOAT;
525 span.x = x + skipPixels;
526 span.y = y + row;
527 span.end = spanWidth;
528 span.arrayMask = arrayMask;
529 span.interpMask = interpMask;
530 if (zoom) {
531 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, rgba);
532 }
533 else {
534 _swrast_write_rgba_span(ctx, &span);
535 }
536
537 source += srcStride;
538 } /* for row */
539
540 skipPixels += spanWidth;
541 } /* while skipPixels < width */
542
543 /* XXX this is ugly/temporary, to undo above change */
544 span.array->ChanType = CHAN_TYPE;
545 }
546
547 if (convImage) {
548 free(convImage);
549 }
550 }
551
552
553 /**
554 * Draw depth+stencil values into a MESA_FORAMT_Z24_S8 or MESA_FORMAT_S8_Z24
555 * renderbuffer. No masking, zooming, scaling, etc.
556 */
557 static void
558 fast_draw_depth_stencil(struct gl_context *ctx, GLint x, GLint y,
559 GLsizei width, GLsizei height,
560 const struct gl_pixelstore_attrib *unpack,
561 const GLvoid *pixels)
562 {
563 const GLenum format = GL_DEPTH_STENCIL_EXT;
564 const GLenum type = GL_UNSIGNED_INT_24_8;
565 struct gl_renderbuffer *rb =
566 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
567 GLubyte *src, *dst;
568 GLint srcRowStride, dstRowStride;
569 GLint i;
570
571 src = _mesa_image_address2d(unpack, pixels, width, height,
572 format, type, 0, 0);
573 srcRowStride = _mesa_image_row_stride(unpack, width, format, type);
574
575 dst = _swrast_pixel_address(rb, x, y);
576 dstRowStride = rb->RowStride * 4;
577
578 for (i = 0; i < height; i++) {
579 _mesa_pack_uint_24_8_depth_stencil_row(rb->Format, width,
580 (const GLuint *) src, dst);
581 dst += dstRowStride;
582 src += srcRowStride;
583 }
584 }
585
586
587
588 /**
589 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
590 * The only per-pixel operations that apply are depth scale/bias,
591 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
592 * and pixel zoom.
593 * Also, only the depth buffer and stencil buffers are touched, not the
594 * color buffer(s).
595 */
596 static void
597 draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
598 GLsizei width, GLsizei height, GLenum type,
599 const struct gl_pixelstore_attrib *unpack,
600 const GLvoid *pixels)
601 {
602 const GLint imgX = x, imgY = y;
603 const GLboolean scaleOrBias
604 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
605 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
606 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
607 const GLenum stencilType = GL_UNSIGNED_BYTE;
608 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
609 struct gl_renderbuffer *depthRb, *stencilRb;
610 struct gl_pixelstore_attrib clippedUnpack = *unpack;
611
612 if (!zoom) {
613 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
614 &clippedUnpack)) {
615 /* totally clipped */
616 return;
617 }
618 }
619
620 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
621 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
622 ASSERT(depthRb);
623 ASSERT(stencilRb);
624
625 if (depthRb == stencilRb &&
626 (depthRb->Format == MESA_FORMAT_Z24_S8 ||
627 depthRb->Format == MESA_FORMAT_S8_Z24) &&
628 type == GL_UNSIGNED_INT_24_8 &&
629 !scaleOrBias &&
630 !zoom &&
631 ctx->Depth.Mask &&
632 (stencilMask & 0xff) == 0xff) {
633 fast_draw_depth_stencil(ctx, x, y, width, height,
634 &clippedUnpack, pixels);
635 }
636 else {
637 /* sub-optimal cases:
638 * Separate depth/stencil buffers, or pixel transfer ops required.
639 */
640 /* XXX need to handle very wide images (skippixels) */
641 GLint i;
642
643 depthRb = ctx->DrawBuffer->_DepthBuffer;
644
645 for (i = 0; i < height; i++) {
646 const GLuint *depthStencilSrc = (const GLuint *)
647 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
648 GL_DEPTH_STENCIL_EXT, type, i, 0);
649
650 if (ctx->Depth.Mask) {
651 if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 24 &&
652 type == GL_UNSIGNED_INT_24_8) {
653 /* fast path 24-bit zbuffer */
654 GLuint zValues[MAX_WIDTH];
655 GLint j;
656 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
657 for (j = 0; j < width; j++) {
658 zValues[j] = depthStencilSrc[j] >> 8;
659 }
660 if (zoom)
661 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
662 x, y + i, zValues);
663 else
664 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
665 }
666 else if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 16 &&
667 type == GL_UNSIGNED_INT_24_8) {
668 /* fast path 16-bit zbuffer */
669 GLushort zValues[MAX_WIDTH];
670 GLint j;
671 ASSERT(depthRb->DataType == GL_UNSIGNED_SHORT);
672 for (j = 0; j < width; j++) {
673 zValues[j] = depthStencilSrc[j] >> 16;
674 }
675 if (zoom)
676 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
677 x, y + i, zValues);
678 else
679 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
680 }
681 else {
682 /* general case */
683 GLuint zValues[MAX_WIDTH]; /* 16 or 32-bit Z value storage */
684 _mesa_unpack_depth_span(ctx, width,
685 depthRb->DataType, zValues, depthMax,
686 type, depthStencilSrc, &clippedUnpack);
687 if (zoom) {
688 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
689 y + i, zValues);
690 }
691 else {
692 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
693 }
694 }
695 }
696
697 if (stencilMask != 0x0) {
698 GLubyte stencilValues[MAX_WIDTH];
699 /* get stencil values, with shift/offset/mapping */
700 _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
701 type, depthStencilSrc, &clippedUnpack,
702 ctx->_ImageTransferState);
703 if (zoom)
704 _swrast_write_zoomed_stencil_span(ctx, imgX, imgY, width,
705 x, y + i, stencilValues);
706 else
707 _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
708 }
709 }
710 }
711 }
712
713
714 /**
715 * Execute software-based glDrawPixels.
716 * By time we get here, all error checking will have been done.
717 */
718 void
719 _swrast_DrawPixels( struct gl_context *ctx,
720 GLint x, GLint y,
721 GLsizei width, GLsizei height,
722 GLenum format, GLenum type,
723 const struct gl_pixelstore_attrib *unpack,
724 const GLvoid *pixels )
725 {
726 SWcontext *swrast = SWRAST_CONTEXT(ctx);
727 GLboolean save_vp_override = ctx->VertexProgram._Overriden;
728
729 if (!_mesa_check_conditional_render(ctx))
730 return; /* don't draw */
731
732 /* We are creating fragments directly, without going through vertex
733 * programs.
734 *
735 * This override flag tells the fragment processing code that its input
736 * comes from a non-standard source, and it may therefore not rely on
737 * optimizations that assume e.g. constant color if there is no color
738 * vertex array.
739 */
740 _mesa_set_vp_override(ctx, GL_TRUE);
741
742 swrast_render_start(ctx);
743
744 if (ctx->NewState)
745 _mesa_update_state(ctx);
746
747 if (swrast->NewState)
748 _swrast_validate_derived( ctx );
749
750 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
751 if (!pixels) {
752 swrast_render_finish(ctx);
753 _mesa_set_vp_override(ctx, save_vp_override);
754 return;
755 }
756
757 /*
758 * By time we get here, all error checking should have been done.
759 */
760 switch (format) {
761 case GL_STENCIL_INDEX:
762 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
763 break;
764 case GL_DEPTH_COMPONENT:
765 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
766 break;
767 case GL_DEPTH_STENCIL_EXT:
768 draw_depth_stencil_pixels(ctx, x, y, width, height, type, unpack, pixels);
769 break;
770 default:
771 /* all other formats should be color formats */
772 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
773 }
774
775 swrast_render_finish(ctx);
776 _mesa_set_vp_override(ctx, save_vp_override);
777
778 _mesa_unmap_pbo_source(ctx, unpack);
779 }