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