OSDN Git Service

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