merge from master
[mesa.git] / src / mesa / swrast / s_drawpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 "glheader.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "convolve.h"
30 #include "image.h"
31 #include "macros.h"
32 #include "imports.h"
33 #include "pixel.h"
34 #include "state.h"
35
36 #include "s_context.h"
37 #include "s_drawpix.h"
38 #include "s_span.h"
39 #include "s_stencil.h"
40 #include "s_zoom.h"
41
42
43
44 /**
45 * Try to do a fast and simple RGB(a) glDrawPixels.
46 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
47 */
48 static GLboolean
49 fast_draw_rgba_pixels(GLcontext *ctx, GLint x, GLint y,
50 GLsizei width, GLsizei height,
51 GLenum format, GLenum type,
52 const struct gl_pixelstore_attrib *userUnpack,
53 const GLvoid *pixels)
54 {
55 const GLint imgX = x, imgY = y;
56 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
57 const GLenum rbType = rb->DataType;
58 SWcontext *swrast = SWRAST_CONTEXT(ctx);
59 SWspan span;
60 GLboolean simpleZoom;
61 GLint yStep; /* +1 or -1 */
62 struct gl_pixelstore_attrib unpack;
63 GLint destX, destY, drawWidth, drawHeight; /* post clipping */
64
65 if ((swrast->_RasterMask & ~CLIP_BIT) ||
66 ctx->Texture._EnabledCoordUnits ||
67 userUnpack->SwapBytes ||
68 ctx->_ImageTransferState) {
69 /* can't handle any of those conditions */
70 return GL_FALSE;
71 }
72
73 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
74 if (ctx->Depth.Test)
75 _swrast_span_default_z(ctx, &span);
76 if (swrast->_FogEnabled)
77 _swrast_span_default_fog(ctx, &span);
78 if (ctx->Texture._EnabledCoordUnits)
79 _swrast_span_default_texcoords(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 (ctx->Visual.rgbMode && 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->color.sz1.rgba);
277 rb->PutRow(ctx, rb, drawWidth, destX, destY,
278 span.array->color.sz1.rgba, 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->color.sz1.rgba);
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->color.sz1.rgba);
295 src += unpack.RowLength;
296 destY++;
297 }
298 }
299 return GL_TRUE;
300 }
301 else if (!ctx->Visual.rgbMode && rbType == GL_UNSIGNED_INT) {
302 /* write CI data to CI frame buffer */
303 GLint row;
304 if (simpleZoom) {
305 for (row = 0; row < drawHeight; row++) {
306 GLuint index32[MAX_WIDTH];
307 GLint col;
308 for (col = 0; col < drawWidth; col++)
309 index32[col] = src[col];
310 rb->PutRow(ctx, rb, drawWidth, destX, destY, index32, NULL);
311 src += unpack.RowLength;
312 destY += yStep;
313 }
314 return GL_TRUE;
315 }
316 }
317 }
318
319 /* can't handle this pixel format and/or data type */
320 return GL_FALSE;
321 }
322
323
324
325 /*
326 * Draw color index image.
327 */
328 static void
329 draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
330 GLsizei width, GLsizei height,
331 GLenum type,
332 const struct gl_pixelstore_attrib *unpack,
333 const GLvoid *pixels )
334 {
335 SWcontext *swrast = SWRAST_CONTEXT(ctx);
336 const GLint imgX = x, imgY = y;
337 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
338 GLint row, skipPixels;
339 SWspan span;
340
341 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
342
343 if (ctx->Depth.Test)
344 _swrast_span_default_z(ctx, &span);
345 if (swrast->_FogEnabled)
346 _swrast_span_default_fog(ctx, &span);
347
348 /*
349 * General solution
350 */
351 skipPixels = 0;
352 while (skipPixels < width) {
353 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
354 ASSERT(spanWidth <= MAX_WIDTH);
355 for (row = 0; row < height; row++) {
356 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
357 width, height,
358 GL_COLOR_INDEX, type,
359 row, skipPixels);
360 _mesa_unpack_index_span(ctx, spanWidth, GL_UNSIGNED_INT,
361 span.array->index, type, source, unpack,
362 ctx->_ImageTransferState);
363
364 /* These may get changed during writing/clipping */
365 span.x = x + skipPixels;
366 span.y = y + row;
367 span.end = spanWidth;
368
369 if (zoom)
370 _swrast_write_zoomed_index_span(ctx, imgX, imgY, &span);
371 else
372 _swrast_write_index_span(ctx, &span);
373 }
374 skipPixels += spanWidth;
375 }
376 }
377
378
379
380 /*
381 * Draw stencil image.
382 */
383 static void
384 draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
385 GLsizei width, GLsizei height,
386 GLenum type,
387 const struct gl_pixelstore_attrib *unpack,
388 const GLvoid *pixels )
389 {
390 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
391 GLint skipPixels;
392
393 /* if width > MAX_WIDTH, have to process image in chunks */
394 skipPixels = 0;
395 while (skipPixels < width) {
396 const GLint spanX = x + skipPixels;
397 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
398 GLint row;
399 for (row = 0; row < height; row++) {
400 const GLint spanY = y + row;
401 GLstencil values[MAX_WIDTH];
402 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
403 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
404 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
405 width, height,
406 GL_COLOR_INDEX, type,
407 row, skipPixels);
408 _mesa_unpack_index_span(ctx, spanWidth, destType, values,
409 type, source, unpack,
410 ctx->_ImageTransferState);
411 _mesa_apply_stencil_transfer_ops(ctx, spanWidth, values);
412 if (zoom) {
413 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
414 spanX, spanY, values);
415 }
416 else {
417 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
418 }
419 }
420 skipPixels += spanWidth;
421 }
422 }
423
424
425 /*
426 * Draw depth image.
427 */
428 static void
429 draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
430 GLsizei width, GLsizei height,
431 GLenum type,
432 const struct gl_pixelstore_attrib *unpack,
433 const GLvoid *pixels )
434 {
435 SWcontext *swrast = SWRAST_CONTEXT(ctx);
436 const GLboolean scaleOrBias
437 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
438 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
439 SWspan span;
440
441 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
442
443 _swrast_span_default_color(ctx, &span);
444
445 if (swrast->_FogEnabled)
446 _swrast_span_default_fog(ctx, &span);
447 if (ctx->Texture._EnabledCoordUnits)
448 _swrast_span_default_texcoords(ctx, &span);
449
450 if (type == GL_UNSIGNED_SHORT
451 && ctx->DrawBuffer->Visual.depthBits == 16
452 && !scaleOrBias
453 && !zoom
454 && ctx->Visual.rgbMode
455 && width <= MAX_WIDTH
456 && !unpack->SwapBytes) {
457 /* Special case: directly write 16-bit depth values */
458 GLint row;
459 for (row = 0; row < height; row++) {
460 const GLushort *zSrc = (const GLushort *)
461 _mesa_image_address2d(unpack, pixels, width, height,
462 GL_DEPTH_COMPONENT, type, row, 0);
463 GLint i;
464 for (i = 0; i < width; i++)
465 span.array->z[i] = zSrc[i];
466 span.x = x;
467 span.y = y + row;
468 span.end = width;
469 _swrast_write_rgba_span(ctx, &span);
470 }
471 }
472 else if (type == GL_UNSIGNED_INT
473 && !scaleOrBias
474 && !zoom
475 && ctx->Visual.rgbMode
476 && width <= MAX_WIDTH
477 && !unpack->SwapBytes) {
478 /* Special case: shift 32-bit values down to Visual.depthBits */
479 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
480 GLint row;
481 for (row = 0; row < height; row++) {
482 const GLuint *zSrc = (const GLuint *)
483 _mesa_image_address2d(unpack, pixels, width, height,
484 GL_DEPTH_COMPONENT, type, row, 0);
485 if (shift == 0) {
486 _mesa_memcpy(span.array->z, zSrc, width * sizeof(GLuint));
487 }
488 else {
489 GLint col;
490 for (col = 0; col < width; col++)
491 span.array->z[col] = zSrc[col] >> shift;
492 }
493 span.x = x;
494 span.y = y + row;
495 span.end = width;
496 _swrast_write_rgba_span(ctx, &span);
497 }
498 }
499 else {
500 /* General case */
501 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
502 GLint skipPixels = 0;
503
504 /* in case width > MAX_WIDTH do the copy in chunks */
505 while (skipPixels < width) {
506 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
507 GLint row;
508 ASSERT(span.end <= MAX_WIDTH);
509 for (row = 0; row < height; row++) {
510 const GLvoid *zSrc = _mesa_image_address2d(unpack,
511 pixels, width, height,
512 GL_DEPTH_COMPONENT, type,
513 row, skipPixels);
514
515 /* Set these for each row since the _swrast_write_* function may
516 * change them while clipping.
517 */
518 span.x = x + skipPixels;
519 span.y = y + row;
520 span.end = spanWidth;
521
522 _mesa_unpack_depth_span(ctx, spanWidth,
523 GL_UNSIGNED_INT, span.array->z, depthMax,
524 type, zSrc, unpack);
525 if (zoom) {
526 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
527 }
528 else if (ctx->Visual.rgbMode) {
529 _swrast_write_rgba_span(ctx, &span);
530 }
531 else {
532 _swrast_write_index_span(ctx, &span);
533 }
534 }
535 skipPixels += spanWidth;
536 }
537 }
538 }
539
540
541
542 /**
543 * Draw RGBA image.
544 */
545 static void
546 draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
547 GLsizei width, GLsizei height,
548 GLenum format, GLenum type,
549 const struct gl_pixelstore_attrib *unpack,
550 const GLvoid *pixels )
551 {
552 SWcontext *swrast = SWRAST_CONTEXT(ctx);
553 const GLint imgX = x, imgY = y;
554 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
555 GLfloat *convImage = NULL;
556 GLbitfield transferOps = ctx->_ImageTransferState;
557 SWspan span;
558
559 /* Try an optimized glDrawPixels first */
560 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
561 unpack, pixels))
562 return;
563
564 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
565 if (ctx->Depth.Test)
566 _swrast_span_default_z(ctx, &span);
567 if (swrast->_FogEnabled)
568 _swrast_span_default_fog(ctx, &span);
569 if (ctx->Texture._EnabledCoordUnits)
570 _swrast_span_default_texcoords(ctx, &span);
571
572 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
573 /* Convolution has to be handled specially. We'll create an
574 * intermediate image, applying all pixel transfer operations
575 * up to convolution. Then we'll convolve the image. Then
576 * we'll proceed with the rest of the transfer operations and
577 * rasterize the image.
578 */
579 GLint row;
580 GLfloat *dest, *tmpImage;
581
582 tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
583 if (!tmpImage) {
584 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
585 return;
586 }
587 convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
588 if (!convImage) {
589 _mesa_free(tmpImage);
590 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
591 return;
592 }
593
594 /* Unpack the image and apply transfer ops up to convolution */
595 dest = tmpImage;
596 for (row = 0; row < height; row++) {
597 const GLvoid *source = _mesa_image_address2d(unpack,
598 pixels, width, height, format, type, row, 0);
599 _mesa_unpack_color_span_float(ctx, width, GL_RGBA, (GLfloat *) dest,
600 format, type, source, unpack,
601 transferOps & IMAGE_PRE_CONVOLUTION_BITS);
602 dest += width * 4;
603 }
604
605 /* do convolution */
606 if (ctx->Pixel.Convolution2DEnabled) {
607 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
608 }
609 else {
610 ASSERT(ctx->Pixel.Separable2DEnabled);
611 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
612 }
613 _mesa_free(tmpImage);
614
615 /* continue transfer ops and draw the convolved image */
616 unpack = &ctx->DefaultPacking;
617 pixels = convImage;
618 format = GL_RGBA;
619 type = GL_FLOAT;
620 transferOps &= IMAGE_POST_CONVOLUTION_BITS;
621 }
622 else if (ctx->Pixel.Convolution1DEnabled) {
623 /* we only want to apply 1D convolution to glTexImage1D */
624 transferOps &= ~(IMAGE_CONVOLUTION_BIT |
625 IMAGE_POST_CONVOLUTION_SCALE_BIAS);
626 }
627
628 if (ctx->DrawBuffer->_NumColorDrawBuffers[0] > 0 &&
629 ctx->DrawBuffer->_ColorDrawBuffers[0][0]->DataType != GL_FLOAT &&
630 ctx->Color.ClampFragmentColor != GL_FALSE) {
631 /* need to clamp colors before applying fragment ops */
632 transferOps |= IMAGE_CLAMP_BIT;
633 }
634
635 /*
636 * General solution
637 */
638 {
639 const GLboolean sink = (ctx->Pixel.MinMaxEnabled && ctx->MinMax.Sink)
640 || (ctx->Pixel.HistogramEnabled && ctx->Histogram.Sink);
641 const GLbitfield interpMask = span.interpMask;
642 const GLbitfield arrayMask = span.arrayMask;
643 const GLint srcStride
644 = _mesa_image_row_stride(unpack, width, format, type);
645 GLint skipPixels = 0;
646 /* use span array for temp color storage */
647 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
648
649 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
650 while (skipPixels < width) {
651 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
652 const GLubyte *source
653 = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
654 width, height, format,
655 type, 0, skipPixels);
656 GLint row;
657
658 for (row = 0; row < height; row++) {
659 /* get image row as float/RGBA */
660 _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
661 format, type, source, unpack,
662 transferOps);
663 /* draw the span */
664 if (!sink) {
665 /* Set these for each row since the _swrast_write_* functions
666 * may change them while clipping/rendering.
667 */
668 span.array->ChanType = GL_FLOAT;
669 span.x = x + skipPixels;
670 span.y = y + row;
671 span.end = spanWidth;
672 span.arrayMask = arrayMask;
673 span.interpMask = interpMask;
674 if (zoom) {
675 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, rgba);
676 }
677 else {
678 _swrast_write_rgba_span(ctx, &span);
679 }
680 }
681
682 source += srcStride;
683 } /* for row */
684
685 skipPixels += spanWidth;
686 } /* while skipPixels < width */
687
688 /* XXX this is ugly/temporary, to undo above change */
689 span.array->ChanType = CHAN_TYPE;
690 }
691
692 if (convImage) {
693 _mesa_free(convImage);
694 }
695 }
696
697
698 /**
699 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
700 * The only per-pixel operations that apply are depth scale/bias,
701 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
702 * and pixel zoom.
703 * Also, only the depth buffer and stencil buffers are touched, not the
704 * color buffer(s).
705 */
706 static void
707 draw_depth_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
708 GLsizei width, GLsizei height, GLenum type,
709 const struct gl_pixelstore_attrib *unpack,
710 const GLvoid *pixels)
711 {
712 const GLint imgX = x, imgY = y;
713 const GLboolean scaleOrBias
714 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
715 const GLfloat depthScale = ctx->DrawBuffer->_DepthMaxF;
716 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
717 const GLuint stencilType = (STENCIL_BITS == 8) ?
718 GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
719 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
720 struct gl_renderbuffer *depthRb, *stencilRb;
721 struct gl_pixelstore_attrib clippedUnpack = *unpack;
722
723 if (!zoom) {
724 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
725 &clippedUnpack)) {
726 /* totally clipped */
727 return;
728 }
729 }
730
731 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
732 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
733 ASSERT(depthRb);
734 ASSERT(stencilRb);
735
736 if (depthRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
737 stencilRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
738 depthRb == stencilRb &&
739 !scaleOrBias &&
740 !zoom &&
741 ctx->Depth.Mask &&
742 (stencilMask & 0xff) == 0xff) {
743 /* This is the ideal case.
744 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
745 * Plus, no pixel transfer ops, zooming, or masking needed.
746 */
747 GLint i;
748 for (i = 0; i < height; i++) {
749 const GLuint *src = (const GLuint *)
750 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
751 GL_DEPTH_STENCIL_EXT, type, i, 0);
752 depthRb->PutRow(ctx, depthRb, width, x, y + i, src, NULL);
753 }
754 }
755 else {
756 /* sub-optimal cases:
757 * Separate depth/stencil buffers, or pixel transfer ops required.
758 */
759 /* XXX need to handle very wide images (skippixels) */
760 GLint i;
761
762 depthRb = ctx->DrawBuffer->_DepthBuffer;
763 stencilRb = ctx->DrawBuffer->_StencilBuffer;
764
765 for (i = 0; i < height; i++) {
766 const GLuint *depthStencilSrc = (const GLuint *)
767 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
768 GL_DEPTH_STENCIL_EXT, type, i, 0);
769
770 if (ctx->Depth.Mask) {
771 if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 24) {
772 /* fast path 24-bit zbuffer */
773 GLuint zValues[MAX_WIDTH];
774 GLint j;
775 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
776 for (j = 0; j < width; j++) {
777 zValues[j] = depthStencilSrc[j] >> 8;
778 }
779 if (zoom)
780 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
781 x, y + i, zValues);
782 else
783 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
784 }
785 else if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 16) {
786 /* fast path 16-bit zbuffer */
787 GLushort zValues[MAX_WIDTH];
788 GLint j;
789 ASSERT(depthRb->DataType == GL_UNSIGNED_SHORT);
790 for (j = 0; j < width; j++) {
791 zValues[j] = depthStencilSrc[j] >> 16;
792 }
793 if (zoom)
794 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
795 x, y + i, zValues);
796 else
797 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
798 }
799 else {
800 /* general case */
801 GLuint zValues[MAX_WIDTH]; /* 16 or 32-bit Z value storage */
802 _mesa_unpack_depth_span(ctx, width,
803 depthRb->DataType, zValues, depthScale,
804 type, depthStencilSrc, &clippedUnpack);
805 if (zoom) {
806 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
807 y + i, zValues);
808 }
809 else {
810 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
811 }
812 }
813 }
814
815 if (stencilMask != 0x0) {
816 GLstencil stencilValues[MAX_WIDTH];
817 /* get stencil values, with shift/offset/mapping */
818 _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
819 type, depthStencilSrc, &clippedUnpack,
820 ctx->_ImageTransferState);
821 if (zoom)
822 _swrast_write_zoomed_stencil_span(ctx, imgX, imgY, width,
823 x, y + i, stencilValues);
824 else
825 _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
826 }
827 }
828 }
829 }
830
831
832
833 /**
834 * Execute software-based glDrawPixels.
835 * By time we get here, all error checking will have been done.
836 */
837 void
838 _swrast_DrawPixels( GLcontext *ctx,
839 GLint x, GLint y,
840 GLsizei width, GLsizei height,
841 GLenum format, GLenum type,
842 const struct gl_pixelstore_attrib *unpack,
843 const GLvoid *pixels )
844 {
845 SWcontext *swrast = SWRAST_CONTEXT(ctx);
846
847 RENDER_START(swrast,ctx);
848
849 if (ctx->NewState)
850 _mesa_update_state(ctx);
851
852 if (swrast->NewState)
853 _swrast_validate_derived( ctx );
854
855 if (unpack->BufferObj->Name) {
856 /* unpack from PBO */
857 GLubyte *buf;
858 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
859 format, type, pixels)) {
860 _mesa_error(ctx, GL_INVALID_OPERATION,
861 "glDrawPixels(invalid PBO access)");
862 goto end;
863 }
864 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
865 GL_READ_ONLY_ARB,
866 unpack->BufferObj);
867 if (!buf) {
868 /* buffer is already mapped - that's an error */
869 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(PBO is mapped)");
870 goto end;
871 }
872 pixels = ADD_POINTERS(buf, pixels);
873 }
874
875 switch (format) {
876 case GL_STENCIL_INDEX:
877 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
878 break;
879 case GL_DEPTH_COMPONENT:
880 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
881 break;
882 case GL_COLOR_INDEX:
883 if (ctx->Visual.rgbMode)
884 draw_rgba_pixels(ctx, x,y, width, height, format, type, unpack, pixels);
885 else
886 draw_index_pixels(ctx, x, y, width, height, type, unpack, pixels);
887 break;
888 case GL_RED:
889 case GL_GREEN:
890 case GL_BLUE:
891 case GL_ALPHA:
892 case GL_LUMINANCE:
893 case GL_LUMINANCE_ALPHA:
894 case GL_RGB:
895 case GL_BGR:
896 case GL_RGBA:
897 case GL_BGRA:
898 case GL_ABGR_EXT:
899 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
900 break;
901 case GL_DEPTH_STENCIL_EXT:
902 draw_depth_stencil_pixels(ctx, x, y, width, height,
903 type, unpack, pixels);
904 break;
905 default:
906 _mesa_problem(ctx, "unexpected format in _swrast_DrawPixels");
907 /* don't return yet, clean-up */
908 }
909
910 end:
911
912 RENDER_FINISH(swrast,ctx);
913
914 if (unpack->BufferObj->Name) {
915 /* done with PBO so unmap it now */
916 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
917 unpack->BufferObj);
918 }
919 }
920
921
922
923 #if 0 /* experimental */
924 /*
925 * Execute glDrawDepthPixelsMESA().
926 */
927 void
928 _swrast_DrawDepthPixelsMESA( GLcontext *ctx,
929 GLint x, GLint y,
930 GLsizei width, GLsizei height,
931 GLenum colorFormat, GLenum colorType,
932 const GLvoid *colors,
933 GLenum depthType, const GLvoid *depths,
934 const struct gl_pixelstore_attrib *unpack )
935 {
936 SWcontext *swrast = SWRAST_CONTEXT(ctx);
937
938 if (swrast->NewState)
939 _swrast_validate_derived( ctx );
940
941 RENDER_START(swrast,ctx);
942
943 switch (colorFormat) {
944 case GL_COLOR_INDEX:
945 if (ctx->Visual.rgbMode)
946 draw_rgba_pixels(ctx, x,y, width, height, colorFormat, colorType,
947 unpack, colors);
948 else
949 draw_index_pixels(ctx, x, y, width, height, colorType,
950 unpack, colors);
951 break;
952 case GL_RED:
953 case GL_GREEN:
954 case GL_BLUE:
955 case GL_ALPHA:
956 case GL_LUMINANCE:
957 case GL_LUMINANCE_ALPHA:
958 case GL_RGB:
959 case GL_BGR:
960 case GL_RGBA:
961 case GL_BGRA:
962 case GL_ABGR_EXT:
963 draw_rgba_pixels(ctx, x, y, width, height, colorFormat, colorType,
964 unpack, colors);
965 break;
966 default:
967 _mesa_problem(ctx, "unexpected format in glDrawDepthPixelsMESA");
968 }
969
970 RENDER_FINISH(swrast,ctx);
971 }
972 #endif