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