59dfd65488922a9636394c5beb0d6f2868e38f7f
[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/context.h"
29 #include "main/convolve.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(GLcontext *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 (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->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 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 const GLint imgX = x, imgY = y;
336 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
337 GLint row, skipPixels;
338 SWspan span;
339
340 INIT_SPAN(span, GL_BITMAP);
341 span.arrayMask = SPAN_INDEX;
342 _swrast_span_default_attribs(ctx, &span);
343
344 /*
345 * General solution
346 */
347 skipPixels = 0;
348 while (skipPixels < width) {
349 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
350 ASSERT(spanWidth <= MAX_WIDTH);
351 for (row = 0; row < height; row++) {
352 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
353 width, height,
354 GL_COLOR_INDEX, type,
355 row, skipPixels);
356 _mesa_unpack_index_span(ctx, spanWidth, GL_UNSIGNED_INT,
357 span.array->index, type, source, unpack,
358 ctx->_ImageTransferState);
359
360 /* These may get changed during writing/clipping */
361 span.x = x + skipPixels;
362 span.y = y + row;
363 span.end = spanWidth;
364
365 if (zoom)
366 _swrast_write_zoomed_index_span(ctx, imgX, imgY, &span);
367 else
368 _swrast_write_index_span(ctx, &span);
369 }
370 skipPixels += spanWidth;
371 }
372 }
373
374
375
376 /*
377 * Draw stencil image.
378 */
379 static void
380 draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
381 GLsizei width, GLsizei height,
382 GLenum type,
383 const struct gl_pixelstore_attrib *unpack,
384 const GLvoid *pixels )
385 {
386 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
387 GLint skipPixels;
388
389 /* if width > MAX_WIDTH, have to process image in chunks */
390 skipPixels = 0;
391 while (skipPixels < width) {
392 const GLint spanX = x + skipPixels;
393 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
394 GLint row;
395 for (row = 0; row < height; row++) {
396 const GLint spanY = y + row;
397 GLstencil values[MAX_WIDTH];
398 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
399 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
400 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
401 width, height,
402 GL_COLOR_INDEX, type,
403 row, skipPixels);
404 _mesa_unpack_stencil_span(ctx, spanWidth, destType, values,
405 type, source, unpack,
406 ctx->_ImageTransferState);
407 if (zoom) {
408 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
409 spanX, spanY, values);
410 }
411 else {
412 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
413 }
414 }
415 skipPixels += spanWidth;
416 }
417 }
418
419
420 /*
421 * Draw depth image.
422 */
423 static void
424 draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
425 GLsizei width, GLsizei height,
426 GLenum type,
427 const struct gl_pixelstore_attrib *unpack,
428 const GLvoid *pixels )
429 {
430 const GLboolean scaleOrBias
431 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
432 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
433 SWspan span;
434
435 INIT_SPAN(span, GL_BITMAP);
436 span.arrayMask = SPAN_Z;
437 _swrast_span_default_attribs(ctx, &span);
438
439 if (type == GL_UNSIGNED_SHORT
440 && ctx->DrawBuffer->Visual.depthBits == 16
441 && !scaleOrBias
442 && !zoom
443 && ctx->Visual.rgbMode
444 && width <= MAX_WIDTH
445 && !unpack->SwapBytes) {
446 /* Special case: directly write 16-bit depth values */
447 GLint row;
448 for (row = 0; row < height; row++) {
449 const GLushort *zSrc = (const GLushort *)
450 _mesa_image_address2d(unpack, pixels, width, height,
451 GL_DEPTH_COMPONENT, type, row, 0);
452 GLint i;
453 for (i = 0; i < width; i++)
454 span.array->z[i] = zSrc[i];
455 span.x = x;
456 span.y = y + row;
457 span.end = width;
458 _swrast_write_rgba_span(ctx, &span);
459 }
460 }
461 else if (type == GL_UNSIGNED_INT
462 && !scaleOrBias
463 && !zoom
464 && ctx->Visual.rgbMode
465 && width <= MAX_WIDTH
466 && !unpack->SwapBytes) {
467 /* Special case: shift 32-bit values down to Visual.depthBits */
468 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
469 GLint row;
470 for (row = 0; row < height; row++) {
471 const GLuint *zSrc = (const GLuint *)
472 _mesa_image_address2d(unpack, pixels, width, height,
473 GL_DEPTH_COMPONENT, type, row, 0);
474 if (shift == 0) {
475 _mesa_memcpy(span.array->z, zSrc, width * sizeof(GLuint));
476 }
477 else {
478 GLint col;
479 for (col = 0; col < width; col++)
480 span.array->z[col] = zSrc[col] >> shift;
481 }
482 span.x = x;
483 span.y = y + row;
484 span.end = width;
485 _swrast_write_rgba_span(ctx, &span);
486 }
487 }
488 else {
489 /* General case */
490 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
491 GLint skipPixels = 0;
492
493 /* in case width > MAX_WIDTH do the copy in chunks */
494 while (skipPixels < width) {
495 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
496 GLint row;
497 ASSERT(span.end <= MAX_WIDTH);
498 for (row = 0; row < height; row++) {
499 const GLvoid *zSrc = _mesa_image_address2d(unpack,
500 pixels, width, height,
501 GL_DEPTH_COMPONENT, type,
502 row, skipPixels);
503
504 /* Set these for each row since the _swrast_write_* function may
505 * change them while clipping.
506 */
507 span.x = x + skipPixels;
508 span.y = y + row;
509 span.end = spanWidth;
510
511 _mesa_unpack_depth_span(ctx, spanWidth,
512 GL_UNSIGNED_INT, span.array->z, depthMax,
513 type, zSrc, unpack);
514 if (zoom) {
515 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
516 }
517 else if (ctx->Visual.rgbMode) {
518 _swrast_write_rgba_span(ctx, &span);
519 }
520 else {
521 _swrast_write_index_span(ctx, &span);
522 }
523 }
524 skipPixels += spanWidth;
525 }
526 }
527 }
528
529
530
531 /**
532 * Draw RGBA image.
533 */
534 static void
535 draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
536 GLsizei width, GLsizei height,
537 GLenum format, GLenum type,
538 const struct gl_pixelstore_attrib *unpack,
539 const GLvoid *pixels )
540 {
541 const GLint imgX = x, imgY = y;
542 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
543 GLfloat *convImage = NULL;
544 GLbitfield transferOps = ctx->_ImageTransferState;
545 SWspan span;
546
547 /* Try an optimized glDrawPixels first */
548 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
549 unpack, pixels)) {
550 return;
551 }
552
553 INIT_SPAN(span, GL_BITMAP);
554 _swrast_span_default_attribs(ctx, &span);
555 span.arrayMask = SPAN_RGBA;
556 span.arrayAttribs = FRAG_BIT_COL0; /* we're fill in COL0 attrib values */
557
558 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
559 /* Convolution has to be handled specially. We'll create an
560 * intermediate image, applying all pixel transfer operations
561 * up to convolution. Then we'll convolve the image. Then
562 * we'll proceed with the rest of the transfer operations and
563 * rasterize the image.
564 */
565 GLint row;
566 GLfloat *dest, *tmpImage;
567
568 tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
569 if (!tmpImage) {
570 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
571 return;
572 }
573 convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
574 if (!convImage) {
575 _mesa_free(tmpImage);
576 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
577 return;
578 }
579
580 /* Unpack the image and apply transfer ops up to convolution */
581 dest = tmpImage;
582 for (row = 0; row < height; row++) {
583 const GLvoid *source = _mesa_image_address2d(unpack,
584 pixels, width, height, format, type, row, 0);
585 _mesa_unpack_color_span_float(ctx, width, GL_RGBA, (GLfloat *) dest,
586 format, type, source, unpack,
587 transferOps & IMAGE_PRE_CONVOLUTION_BITS);
588 dest += width * 4;
589 }
590
591 /* do convolution */
592 if (ctx->Pixel.Convolution2DEnabled) {
593 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
594 }
595 else {
596 ASSERT(ctx->Pixel.Separable2DEnabled);
597 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
598 }
599 _mesa_free(tmpImage);
600
601 /* continue transfer ops and draw the convolved image */
602 unpack = &ctx->DefaultPacking;
603 pixels = convImage;
604 format = GL_RGBA;
605 type = GL_FLOAT;
606 transferOps &= IMAGE_POST_CONVOLUTION_BITS;
607 }
608 else if (ctx->Pixel.Convolution1DEnabled) {
609 /* we only want to apply 1D convolution to glTexImage1D */
610 transferOps &= ~(IMAGE_CONVOLUTION_BIT |
611 IMAGE_POST_CONVOLUTION_SCALE_BIAS);
612 }
613
614 if (ctx->DrawBuffer->_NumColorDrawBuffers > 0 &&
615 ctx->DrawBuffer->_ColorDrawBuffers[0]->DataType != GL_FLOAT &&
616 ctx->Color.ClampFragmentColor != GL_FALSE) {
617 /* need to clamp colors before applying fragment ops */
618 transferOps |= IMAGE_CLAMP_BIT;
619 }
620
621 /*
622 * General solution
623 */
624 {
625 const GLboolean sink = (ctx->Pixel.MinMaxEnabled && ctx->MinMax.Sink)
626 || (ctx->Pixel.HistogramEnabled && ctx->Histogram.Sink);
627 const GLbitfield interpMask = span.interpMask;
628 const GLbitfield arrayMask = span.arrayMask;
629 const GLint srcStride
630 = _mesa_image_row_stride(unpack, width, format, type);
631 GLint skipPixels = 0;
632 /* use span array for temp color storage */
633 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
634
635 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
636 while (skipPixels < width) {
637 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
638 const GLubyte *source
639 = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
640 width, height, format,
641 type, 0, skipPixels);
642 GLint row;
643
644 for (row = 0; row < height; row++) {
645 /* get image row as float/RGBA */
646 _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
647 format, type, source, unpack,
648 transferOps);
649 /* draw the span */
650 if (!sink) {
651 /* Set these for each row since the _swrast_write_* functions
652 * may change them while clipping/rendering.
653 */
654 span.array->ChanType = GL_FLOAT;
655 span.x = x + skipPixels;
656 span.y = y + row;
657 span.end = spanWidth;
658 span.arrayMask = arrayMask;
659 span.interpMask = interpMask;
660 if (zoom) {
661 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, rgba);
662 }
663 else {
664 _swrast_write_rgba_span(ctx, &span);
665 }
666 }
667
668 source += srcStride;
669 } /* for row */
670
671 skipPixels += spanWidth;
672 } /* while skipPixels < width */
673
674 /* XXX this is ugly/temporary, to undo above change */
675 span.array->ChanType = CHAN_TYPE;
676 }
677
678 if (convImage) {
679 _mesa_free(convImage);
680 }
681 }
682
683
684 /**
685 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
686 * The only per-pixel operations that apply are depth scale/bias,
687 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
688 * and pixel zoom.
689 * Also, only the depth buffer and stencil buffers are touched, not the
690 * color buffer(s).
691 */
692 static void
693 draw_depth_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
694 GLsizei width, GLsizei height, GLenum type,
695 const struct gl_pixelstore_attrib *unpack,
696 const GLvoid *pixels)
697 {
698 const GLint imgX = x, imgY = y;
699 const GLboolean scaleOrBias
700 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
701 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
702 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
703 const GLuint stencilType = (STENCIL_BITS == 8) ?
704 GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
705 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
706 struct gl_renderbuffer *depthRb, *stencilRb;
707 struct gl_pixelstore_attrib clippedUnpack = *unpack;
708
709 if (!zoom) {
710 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
711 &clippedUnpack)) {
712 /* totally clipped */
713 return;
714 }
715 }
716
717 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
718 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
719 ASSERT(depthRb);
720 ASSERT(stencilRb);
721
722 if (depthRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
723 stencilRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
724 depthRb == stencilRb &&
725 !scaleOrBias &&
726 !zoom &&
727 ctx->Depth.Mask &&
728 (stencilMask & 0xff) == 0xff) {
729 /* This is the ideal case.
730 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
731 * Plus, no pixel transfer ops, zooming, or masking needed.
732 */
733 GLint i;
734 for (i = 0; i < height; i++) {
735 const GLuint *src = (const GLuint *)
736 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
737 GL_DEPTH_STENCIL_EXT, type, i, 0);
738 depthRb->PutRow(ctx, depthRb, width, x, y + i, src, NULL);
739 }
740 }
741 else {
742 /* sub-optimal cases:
743 * Separate depth/stencil buffers, or pixel transfer ops required.
744 */
745 /* XXX need to handle very wide images (skippixels) */
746 GLint i;
747
748 depthRb = ctx->DrawBuffer->_DepthBuffer;
749 stencilRb = ctx->DrawBuffer->_StencilBuffer;
750
751 for (i = 0; i < height; i++) {
752 const GLuint *depthStencilSrc = (const GLuint *)
753 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
754 GL_DEPTH_STENCIL_EXT, type, i, 0);
755
756 if (ctx->Depth.Mask) {
757 if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 24) {
758 /* fast path 24-bit zbuffer */
759 GLuint zValues[MAX_WIDTH];
760 GLint j;
761 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
762 for (j = 0; j < width; j++) {
763 zValues[j] = depthStencilSrc[j] >> 8;
764 }
765 if (zoom)
766 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
767 x, y + i, zValues);
768 else
769 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
770 }
771 else if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 16) {
772 /* fast path 16-bit zbuffer */
773 GLushort zValues[MAX_WIDTH];
774 GLint j;
775 ASSERT(depthRb->DataType == GL_UNSIGNED_SHORT);
776 for (j = 0; j < width; j++) {
777 zValues[j] = depthStencilSrc[j] >> 16;
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 {
786 /* general case */
787 GLuint zValues[MAX_WIDTH]; /* 16 or 32-bit Z value storage */
788 _mesa_unpack_depth_span(ctx, width,
789 depthRb->DataType, zValues, depthMax,
790 type, depthStencilSrc, &clippedUnpack);
791 if (zoom) {
792 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
793 y + i, zValues);
794 }
795 else {
796 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
797 }
798 }
799 }
800
801 if (stencilMask != 0x0) {
802 GLstencil stencilValues[MAX_WIDTH];
803 /* get stencil values, with shift/offset/mapping */
804 _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
805 type, depthStencilSrc, &clippedUnpack,
806 ctx->_ImageTransferState);
807 if (zoom)
808 _swrast_write_zoomed_stencil_span(ctx, imgX, imgY, width,
809 x, y + i, stencilValues);
810 else
811 _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
812 }
813 }
814 }
815 }
816
817
818 /**
819 * Execute software-based glDrawPixels.
820 * By time we get here, all error checking will have been done.
821 */
822 void
823 _swrast_DrawPixels( GLcontext *ctx,
824 GLint x, GLint y,
825 GLsizei width, GLsizei height,
826 GLenum format, GLenum type,
827 const struct gl_pixelstore_attrib *unpack,
828 const GLvoid *pixels )
829 {
830 SWcontext *swrast = SWRAST_CONTEXT(ctx);
831 GLboolean save_vp_override = ctx->VertexProgram._Overriden;
832
833 /* We are creating fragments directly, without going through vertex
834 * programs.
835 *
836 * This override flag tells the fragment processing code that its input
837 * comes from a non-standard source, and it may therefore not rely on
838 * optimizations that assume e.g. constant color if there is no color
839 * vertex array.
840 */
841 _mesa_set_vp_override(ctx, GL_TRUE);
842
843 swrast_render_start(ctx);
844
845 if (ctx->NewState)
846 _mesa_update_state(ctx);
847
848 if (swrast->NewState)
849 _swrast_validate_derived( ctx );
850
851 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
852 if (!pixels) {
853 swrast_render_finish(ctx);
854 _mesa_set_vp_override(ctx, save_vp_override);
855 return;
856 }
857
858 switch (format) {
859 case GL_STENCIL_INDEX:
860 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
861 break;
862 case GL_DEPTH_COMPONENT:
863 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
864 break;
865 case GL_COLOR_INDEX:
866 if (ctx->Visual.rgbMode)
867 draw_rgba_pixels(ctx, x,y, width, height, format, type, unpack, pixels);
868 else
869 draw_index_pixels(ctx, x, y, width, height, type, unpack, pixels);
870 break;
871 case GL_RED:
872 case GL_GREEN:
873 case GL_BLUE:
874 case GL_ALPHA:
875 case GL_LUMINANCE:
876 case GL_LUMINANCE_ALPHA:
877 case GL_RGB:
878 case GL_BGR:
879 case GL_RGBA:
880 case GL_BGRA:
881 case GL_ABGR_EXT:
882 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
883 break;
884 case GL_DEPTH_STENCIL_EXT:
885 draw_depth_stencil_pixels(ctx, x, y, width, height,
886 type, unpack, pixels);
887 break;
888 default:
889 _mesa_problem(ctx, "unexpected format in _swrast_DrawPixels");
890 /* don't return yet, clean-up */
891 }
892
893 swrast_render_finish(ctx);
894 _mesa_set_vp_override(ctx, save_vp_override);
895
896 _mesa_unmap_pbo_source(ctx, unpack);
897 }