fix GL_DOT3_RGBA texture combiner mode in generated fragment programs (bug #11030)
[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);
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_index_span(ctx, spanWidth, destType, values,
402 type, source, unpack,
403 ctx->_ImageTransferState);
404 _mesa_apply_stencil_transfer_ops(ctx, spanWidth, values);
405 if (zoom) {
406 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
407 spanX, spanY, values);
408 }
409 else {
410 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
411 }
412 }
413 skipPixels += spanWidth;
414 }
415 }
416
417
418 /*
419 * Draw depth image.
420 */
421 static void
422 draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
423 GLsizei width, GLsizei height,
424 GLenum type,
425 const struct gl_pixelstore_attrib *unpack,
426 const GLvoid *pixels )
427 {
428 const GLboolean scaleOrBias
429 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
430 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
431 SWspan span;
432
433 INIT_SPAN(span, GL_BITMAP);
434 span.arrayMask = SPAN_Z;
435 _swrast_span_default_attribs(ctx, &span);
436
437 if (type == GL_UNSIGNED_SHORT
438 && ctx->DrawBuffer->Visual.depthBits == 16
439 && !scaleOrBias
440 && !zoom
441 && ctx->Visual.rgbMode
442 && width <= MAX_WIDTH
443 && !unpack->SwapBytes) {
444 /* Special case: directly write 16-bit depth values */
445 GLint row;
446 for (row = 0; row < height; row++) {
447 const GLushort *zSrc = (const GLushort *)
448 _mesa_image_address2d(unpack, pixels, width, height,
449 GL_DEPTH_COMPONENT, type, row, 0);
450 GLint i;
451 for (i = 0; i < width; i++)
452 span.array->z[i] = zSrc[i];
453 span.x = x;
454 span.y = y + row;
455 span.end = width;
456 _swrast_write_rgba_span(ctx, &span);
457 }
458 }
459 else if (type == GL_UNSIGNED_INT
460 && !scaleOrBias
461 && !zoom
462 && ctx->Visual.rgbMode
463 && width <= MAX_WIDTH
464 && !unpack->SwapBytes) {
465 /* Special case: shift 32-bit values down to Visual.depthBits */
466 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
467 GLint row;
468 for (row = 0; row < height; row++) {
469 const GLuint *zSrc = (const GLuint *)
470 _mesa_image_address2d(unpack, pixels, width, height,
471 GL_DEPTH_COMPONENT, type, row, 0);
472 if (shift == 0) {
473 _mesa_memcpy(span.array->z, zSrc, width * sizeof(GLuint));
474 }
475 else {
476 GLint col;
477 for (col = 0; col < width; col++)
478 span.array->z[col] = zSrc[col] >> shift;
479 }
480 span.x = x;
481 span.y = y + row;
482 span.end = width;
483 _swrast_write_rgba_span(ctx, &span);
484 }
485 }
486 else {
487 /* General case */
488 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
489 GLint skipPixels = 0;
490
491 /* in case width > MAX_WIDTH do the copy in chunks */
492 while (skipPixels < width) {
493 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
494 GLint row;
495 ASSERT(span.end <= MAX_WIDTH);
496 for (row = 0; row < height; row++) {
497 const GLvoid *zSrc = _mesa_image_address2d(unpack,
498 pixels, width, height,
499 GL_DEPTH_COMPONENT, type,
500 row, skipPixels);
501
502 /* Set these for each row since the _swrast_write_* function may
503 * change them while clipping.
504 */
505 span.x = x + skipPixels;
506 span.y = y + row;
507 span.end = spanWidth;
508
509 _mesa_unpack_depth_span(ctx, spanWidth,
510 GL_UNSIGNED_INT, span.array->z, depthMax,
511 type, zSrc, unpack);
512 if (zoom) {
513 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
514 }
515 else if (ctx->Visual.rgbMode) {
516 _swrast_write_rgba_span(ctx, &span);
517 }
518 else {
519 _swrast_write_index_span(ctx, &span);
520 }
521 }
522 skipPixels += spanWidth;
523 }
524 }
525 }
526
527
528
529 /**
530 * Draw RGBA image.
531 */
532 static void
533 draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
534 GLsizei width, GLsizei height,
535 GLenum format, GLenum type,
536 const struct gl_pixelstore_attrib *unpack,
537 const GLvoid *pixels )
538 {
539 const GLint imgX = x, imgY = y;
540 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
541 GLfloat *convImage = NULL;
542 GLbitfield transferOps = ctx->_ImageTransferState;
543 SWspan span;
544
545 /* Try an optimized glDrawPixels first */
546 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
547 unpack, pixels)) {
548 return;
549 }
550
551 INIT_SPAN(span, GL_BITMAP);
552 _swrast_span_default_attribs(ctx, &span);
553 span.arrayMask = SPAN_RGBA;
554 span.arrayAttribs = FRAG_BIT_COL0; /* we're fill in COL0 attrib values */
555
556 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
557 /* Convolution has to be handled specially. We'll create an
558 * intermediate image, applying all pixel transfer operations
559 * up to convolution. Then we'll convolve the image. Then
560 * we'll proceed with the rest of the transfer operations and
561 * rasterize the image.
562 */
563 GLint row;
564 GLfloat *dest, *tmpImage;
565
566 tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
567 if (!tmpImage) {
568 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
569 return;
570 }
571 convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
572 if (!convImage) {
573 _mesa_free(tmpImage);
574 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
575 return;
576 }
577
578 /* Unpack the image and apply transfer ops up to convolution */
579 dest = tmpImage;
580 for (row = 0; row < height; row++) {
581 const GLvoid *source = _mesa_image_address2d(unpack,
582 pixels, width, height, format, type, row, 0);
583 _mesa_unpack_color_span_float(ctx, width, GL_RGBA, (GLfloat *) dest,
584 format, type, source, unpack,
585 transferOps & IMAGE_PRE_CONVOLUTION_BITS);
586 dest += width * 4;
587 }
588
589 /* do convolution */
590 if (ctx->Pixel.Convolution2DEnabled) {
591 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
592 }
593 else {
594 ASSERT(ctx->Pixel.Separable2DEnabled);
595 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
596 }
597 _mesa_free(tmpImage);
598
599 /* continue transfer ops and draw the convolved image */
600 unpack = &ctx->DefaultPacking;
601 pixels = convImage;
602 format = GL_RGBA;
603 type = GL_FLOAT;
604 transferOps &= IMAGE_POST_CONVOLUTION_BITS;
605 }
606 else if (ctx->Pixel.Convolution1DEnabled) {
607 /* we only want to apply 1D convolution to glTexImage1D */
608 transferOps &= ~(IMAGE_CONVOLUTION_BIT |
609 IMAGE_POST_CONVOLUTION_SCALE_BIAS);
610 }
611
612 if (ctx->DrawBuffer->_NumColorDrawBuffers[0] > 0 &&
613 ctx->DrawBuffer->_ColorDrawBuffers[0][0]->DataType != GL_FLOAT &&
614 ctx->Color.ClampFragmentColor != GL_FALSE) {
615 /* need to clamp colors before applying fragment ops */
616 transferOps |= IMAGE_CLAMP_BIT;
617 }
618
619 /*
620 * General solution
621 */
622 {
623 const GLboolean sink = (ctx->Pixel.MinMaxEnabled && ctx->MinMax.Sink)
624 || (ctx->Pixel.HistogramEnabled && ctx->Histogram.Sink);
625 const GLbitfield interpMask = span.interpMask;
626 const GLbitfield arrayMask = span.arrayMask;
627 const GLint srcStride
628 = _mesa_image_row_stride(unpack, width, format, type);
629 GLint skipPixels = 0;
630 /* use span array for temp color storage */
631 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
632
633 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
634 while (skipPixels < width) {
635 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
636 const GLubyte *source
637 = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
638 width, height, format,
639 type, 0, skipPixels);
640 GLint row;
641
642 for (row = 0; row < height; row++) {
643 /* get image row as float/RGBA */
644 _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
645 format, type, source, unpack,
646 transferOps);
647 /* draw the span */
648 if (!sink) {
649 /* Set these for each row since the _swrast_write_* functions
650 * may change them while clipping/rendering.
651 */
652 span.array->ChanType = GL_FLOAT;
653 span.x = x + skipPixels;
654 span.y = y + row;
655 span.end = spanWidth;
656 span.arrayMask = arrayMask;
657 span.interpMask = interpMask;
658 if (zoom) {
659 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, rgba);
660 }
661 else {
662 _swrast_write_rgba_span(ctx, &span);
663 }
664 }
665
666 source += srcStride;
667 } /* for row */
668
669 skipPixels += spanWidth;
670 } /* while skipPixels < width */
671
672 /* XXX this is ugly/temporary, to undo above change */
673 span.array->ChanType = CHAN_TYPE;
674 }
675
676 if (convImage) {
677 _mesa_free(convImage);
678 }
679 }
680
681
682 /**
683 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
684 * The only per-pixel operations that apply are depth scale/bias,
685 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
686 * and pixel zoom.
687 * Also, only the depth buffer and stencil buffers are touched, not the
688 * color buffer(s).
689 */
690 static void
691 draw_depth_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
692 GLsizei width, GLsizei height, GLenum type,
693 const struct gl_pixelstore_attrib *unpack,
694 const GLvoid *pixels)
695 {
696 const GLint imgX = x, imgY = y;
697 const GLboolean scaleOrBias
698 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
699 const GLfloat depthScale = ctx->DrawBuffer->_DepthMaxF;
700 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
701 const GLuint stencilType = (STENCIL_BITS == 8) ?
702 GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
703 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
704 struct gl_renderbuffer *depthRb, *stencilRb;
705 struct gl_pixelstore_attrib clippedUnpack = *unpack;
706
707 if (!zoom) {
708 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
709 &clippedUnpack)) {
710 /* totally clipped */
711 return;
712 }
713 }
714
715 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
716 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
717 ASSERT(depthRb);
718 ASSERT(stencilRb);
719
720 if (depthRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
721 stencilRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
722 depthRb == stencilRb &&
723 !scaleOrBias &&
724 !zoom &&
725 ctx->Depth.Mask &&
726 (stencilMask & 0xff) == 0xff) {
727 /* This is the ideal case.
728 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
729 * Plus, no pixel transfer ops, zooming, or masking needed.
730 */
731 GLint i;
732 for (i = 0; i < height; i++) {
733 const GLuint *src = (const GLuint *)
734 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
735 GL_DEPTH_STENCIL_EXT, type, i, 0);
736 depthRb->PutRow(ctx, depthRb, width, x, y + i, src, NULL);
737 }
738 }
739 else {
740 /* sub-optimal cases:
741 * Separate depth/stencil buffers, or pixel transfer ops required.
742 */
743 /* XXX need to handle very wide images (skippixels) */
744 GLint i;
745
746 depthRb = ctx->DrawBuffer->_DepthBuffer;
747 stencilRb = ctx->DrawBuffer->_StencilBuffer;
748
749 for (i = 0; i < height; i++) {
750 const GLuint *depthStencilSrc = (const GLuint *)
751 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
752 GL_DEPTH_STENCIL_EXT, type, i, 0);
753
754 if (ctx->Depth.Mask) {
755 if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 24) {
756 /* fast path 24-bit zbuffer */
757 GLuint zValues[MAX_WIDTH];
758 GLint j;
759 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
760 for (j = 0; j < width; j++) {
761 zValues[j] = depthStencilSrc[j] >> 8;
762 }
763 if (zoom)
764 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
765 x, y + i, zValues);
766 else
767 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
768 }
769 else if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 16) {
770 /* fast path 16-bit zbuffer */
771 GLushort zValues[MAX_WIDTH];
772 GLint j;
773 ASSERT(depthRb->DataType == GL_UNSIGNED_SHORT);
774 for (j = 0; j < width; j++) {
775 zValues[j] = depthStencilSrc[j] >> 16;
776 }
777 if (zoom)
778 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
779 x, y + i, zValues);
780 else
781 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
782 }
783 else {
784 /* general case */
785 GLuint zValues[MAX_WIDTH]; /* 16 or 32-bit Z value storage */
786 _mesa_unpack_depth_span(ctx, width,
787 depthRb->DataType, zValues, depthScale,
788 type, depthStencilSrc, &clippedUnpack);
789 if (zoom) {
790 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
791 y + i, zValues);
792 }
793 else {
794 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
795 }
796 }
797 }
798
799 if (stencilMask != 0x0) {
800 GLstencil stencilValues[MAX_WIDTH];
801 /* get stencil values, with shift/offset/mapping */
802 _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
803 type, depthStencilSrc, &clippedUnpack,
804 ctx->_ImageTransferState);
805 if (zoom)
806 _swrast_write_zoomed_stencil_span(ctx, imgX, imgY, width,
807 x, y + i, stencilValues);
808 else
809 _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
810 }
811 }
812 }
813 }
814
815
816
817 /**
818 * Execute software-based glDrawPixels.
819 * By time we get here, all error checking will have been done.
820 */
821 void
822 _swrast_DrawPixels( GLcontext *ctx,
823 GLint x, GLint y,
824 GLsizei width, GLsizei height,
825 GLenum format, GLenum type,
826 const struct gl_pixelstore_attrib *unpack,
827 const GLvoid *pixels )
828 {
829 SWcontext *swrast = SWRAST_CONTEXT(ctx);
830
831 RENDER_START(swrast,ctx);
832
833 if (ctx->NewState)
834 _mesa_update_state(ctx);
835
836 if (swrast->NewState)
837 _swrast_validate_derived( ctx );
838
839 if (unpack->BufferObj->Name) {
840 /* unpack from PBO */
841 GLubyte *buf;
842 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
843 format, type, pixels)) {
844 _mesa_error(ctx, GL_INVALID_OPERATION,
845 "glDrawPixels(invalid PBO access)");
846 goto end;
847 }
848 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
849 GL_READ_ONLY_ARB,
850 unpack->BufferObj);
851 if (!buf) {
852 /* buffer is already mapped - that's an error */
853 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(PBO is mapped)");
854 goto end;
855 }
856 pixels = ADD_POINTERS(buf, pixels);
857 }
858
859 switch (format) {
860 case GL_STENCIL_INDEX:
861 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
862 break;
863 case GL_DEPTH_COMPONENT:
864 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
865 break;
866 case GL_COLOR_INDEX:
867 if (ctx->Visual.rgbMode)
868 draw_rgba_pixels(ctx, x,y, width, height, format, type, unpack, pixels);
869 else
870 draw_index_pixels(ctx, x, y, width, height, type, unpack, pixels);
871 break;
872 case GL_RED:
873 case GL_GREEN:
874 case GL_BLUE:
875 case GL_ALPHA:
876 case GL_LUMINANCE:
877 case GL_LUMINANCE_ALPHA:
878 case GL_RGB:
879 case GL_BGR:
880 case GL_RGBA:
881 case GL_BGRA:
882 case GL_ABGR_EXT:
883 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
884 break;
885 case GL_DEPTH_STENCIL_EXT:
886 draw_depth_stencil_pixels(ctx, x, y, width, height,
887 type, unpack, pixels);
888 break;
889 default:
890 _mesa_problem(ctx, "unexpected format in _swrast_DrawPixels");
891 /* don't return yet, clean-up */
892 }
893
894 end:
895
896 RENDER_FINISH(swrast,ctx);
897
898 if (unpack->BufferObj->Name) {
899 /* done with PBO so unmap it now */
900 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
901 unpack->BufferObj);
902 }
903 }
904
905
906
907 #if 0 /* experimental */
908 /*
909 * Execute glDrawDepthPixelsMESA().
910 */
911 void
912 _swrast_DrawDepthPixelsMESA( GLcontext *ctx,
913 GLint x, GLint y,
914 GLsizei width, GLsizei height,
915 GLenum colorFormat, GLenum colorType,
916 const GLvoid *colors,
917 GLenum depthType, const GLvoid *depths,
918 const struct gl_pixelstore_attrib *unpack )
919 {
920 SWcontext *swrast = SWRAST_CONTEXT(ctx);
921
922 if (swrast->NewState)
923 _swrast_validate_derived( ctx );
924
925 RENDER_START(swrast,ctx);
926
927 switch (colorFormat) {
928 case GL_COLOR_INDEX:
929 if (ctx->Visual.rgbMode)
930 draw_rgba_pixels(ctx, x,y, width, height, colorFormat, colorType,
931 unpack, colors);
932 else
933 draw_index_pixels(ctx, x, y, width, height, colorType,
934 unpack, colors);
935 break;
936 case GL_RED:
937 case GL_GREEN:
938 case GL_BLUE:
939 case GL_ALPHA:
940 case GL_LUMINANCE:
941 case GL_LUMINANCE_ALPHA:
942 case GL_RGB:
943 case GL_BGR:
944 case GL_RGBA:
945 case GL_BGRA:
946 case GL_ABGR_EXT:
947 draw_rgba_pixels(ctx, x, y, width, height, colorFormat, colorType,
948 unpack, colors);
949 break;
950 default:
951 _mesa_problem(ctx, "unexpected format in glDrawDepthPixelsMESA");
952 }
953
954 RENDER_FINISH(swrast,ctx);
955 }
956 #endif