OSDN Git Service

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