Merge branch 'master' of git+ssh://brianp@git.freedesktop.org/git/mesa/mesa
[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 _swrast_span_default_attribs(ctx, &span);
75
76 /* copy input params since clipping may change them */
77 unpack = *userUnpack;
78 destX = x;
79 destY = y;
80 drawWidth = width;
81 drawHeight = height;
82
83 /* check for simple zooming and clipping */
84 if (ctx->Pixel.ZoomX == 1.0F &&
85 (ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F)) {
86 if (!_mesa_clip_drawpixels(ctx, &destX, &destY,
87 &drawWidth, &drawHeight, &unpack)) {
88 /* image was completely clipped: no-op, all done */
89 return GL_TRUE;
90 }
91 simpleZoom = GL_TRUE;
92 yStep = (GLint) ctx->Pixel.ZoomY;
93 ASSERT(yStep == 1 || yStep == -1);
94 }
95 else {
96 /* non-simple zooming */
97 simpleZoom = GL_FALSE;
98 yStep = 1;
99 if (unpack.RowLength == 0)
100 unpack.RowLength = width;
101 }
102
103 /*
104 * Ready to draw!
105 */
106
107 if (format == GL_RGBA && type == rbType) {
108 const GLubyte *src
109 = (const GLubyte *) _mesa_image_address2d(&unpack, pixels, width,
110 height, format, type, 0, 0);
111 const GLint srcStride = _mesa_image_row_stride(&unpack, width,
112 format, type);
113 if (simpleZoom) {
114 GLint row;
115 for (row = 0; row < drawHeight; row++) {
116 rb->PutRow(ctx, rb, drawWidth, destX, destY, src, NULL);
117 src += srcStride;
118 destY += yStep;
119 }
120 }
121 else {
122 /* with zooming */
123 GLint row;
124 for (row = 0; row < drawHeight; row++) {
125 span.x = destX;
126 span.y = destY + row;
127 span.end = drawWidth;
128 span.array->ChanType = rbType;
129 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, src);
130 src += srcStride;
131 }
132 span.array->ChanType = CHAN_TYPE;
133 }
134 return GL_TRUE;
135 }
136
137 if (format == GL_RGB && type == rbType) {
138 const GLubyte *src
139 = (const GLubyte *) _mesa_image_address2d(&unpack, pixels, width,
140 height, format, type, 0, 0);
141 const GLint srcStride = _mesa_image_row_stride(&unpack, width,
142 format, type);
143 if (simpleZoom) {
144 GLint row;
145 for (row = 0; row < drawHeight; row++) {
146 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, src, NULL);
147 src += srcStride;
148 destY += yStep;
149 }
150 }
151 else {
152 /* with zooming */
153 GLint row;
154 for (row = 0; row < drawHeight; row++) {
155 span.x = destX;
156 span.y = destY;
157 span.end = drawWidth;
158 span.array->ChanType = rbType;
159 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, src);
160 src += srcStride;
161 destY++;
162 }
163 span.array->ChanType = CHAN_TYPE;
164 }
165 return GL_TRUE;
166 }
167
168 /* Remaining cases haven't been tested with alignment != 1 */
169 if (userUnpack->Alignment != 1)
170 return GL_FALSE;
171
172 if (format == GL_LUMINANCE && type == CHAN_TYPE && rbType == CHAN_TYPE) {
173 const GLchan *src = (const GLchan *) pixels
174 + (unpack.SkipRows * unpack.RowLength + unpack.SkipPixels);
175 if (simpleZoom) {
176 /* no zooming */
177 GLint row;
178 ASSERT(drawWidth <= MAX_WIDTH);
179 for (row = 0; row < drawHeight; row++) {
180 GLchan rgb[MAX_WIDTH][3];
181 GLint i;
182 for (i = 0;i<drawWidth;i++) {
183 rgb[i][0] = src[i];
184 rgb[i][1] = src[i];
185 rgb[i][2] = src[i];
186 }
187 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, rgb, NULL);
188 src += unpack.RowLength;
189 destY += yStep;
190 }
191 }
192 else {
193 /* with zooming */
194 GLint row;
195 ASSERT(drawWidth <= MAX_WIDTH);
196 for (row = 0; row < drawHeight; row++) {
197 GLchan rgb[MAX_WIDTH][3];
198 GLint i;
199 for (i = 0;i<drawWidth;i++) {
200 rgb[i][0] = src[i];
201 rgb[i][1] = src[i];
202 rgb[i][2] = src[i];
203 }
204 span.x = destX;
205 span.y = destY;
206 span.end = drawWidth;
207 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, rgb);
208 src += unpack.RowLength;
209 destY++;
210 }
211 }
212 return GL_TRUE;
213 }
214
215 if (format == GL_LUMINANCE_ALPHA && type == CHAN_TYPE && rbType == CHAN_TYPE) {
216 const GLchan *src = (const GLchan *) pixels
217 + (unpack.SkipRows * unpack.RowLength + unpack.SkipPixels)*2;
218 if (simpleZoom) {
219 GLint row;
220 ASSERT(drawWidth <= MAX_WIDTH);
221 for (row = 0; row < drawHeight; row++) {
222 GLint i;
223 const GLchan *ptr = src;
224 for (i = 0;i<drawWidth;i++) {
225 span.array->rgba[i][0] = *ptr;
226 span.array->rgba[i][1] = *ptr;
227 span.array->rgba[i][2] = *ptr++;
228 span.array->rgba[i][3] = *ptr++;
229 }
230 rb->PutRow(ctx, rb, drawWidth, destX, destY,
231 span.array->rgba, NULL);
232 src += unpack.RowLength*2;
233 destY += yStep;
234 }
235 }
236 else {
237 /* with zooming */
238 GLint row;
239 ASSERT(drawWidth <= MAX_WIDTH);
240 for (row = 0; row < drawHeight; row++) {
241 const GLchan *ptr = src;
242 GLint i;
243 for (i = 0;i<drawWidth;i++) {
244 span.array->rgba[i][0] = *ptr;
245 span.array->rgba[i][1] = *ptr;
246 span.array->rgba[i][2] = *ptr++;
247 span.array->rgba[i][3] = *ptr++;
248 }
249 span.x = destX;
250 span.y = destY;
251 span.end = drawWidth;
252 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
253 span.array->rgba);
254 src += unpack.RowLength*2;
255 destY++;
256 }
257 }
258 return GL_TRUE;
259 }
260
261 if (format == GL_COLOR_INDEX && type == GL_UNSIGNED_BYTE) {
262 const GLubyte *src = (const GLubyte *) pixels
263 + unpack.SkipRows * unpack.RowLength + unpack.SkipPixels;
264 if (ctx->Visual.rgbMode && rbType == GL_UNSIGNED_BYTE) {
265 /* convert ubyte/CI data to ubyte/RGBA */
266 if (simpleZoom) {
267 GLint row;
268 for (row = 0; row < drawHeight; row++) {
269 ASSERT(drawWidth <= MAX_WIDTH);
270 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
271 span.array->rgba8);
272 rb->PutRow(ctx, rb, drawWidth, destX, destY,
273 span.array->rgba8, NULL);
274 src += unpack.RowLength;
275 destY += yStep;
276 }
277 }
278 else {
279 /* ubyte/CI to ubyte/RGBA with zooming */
280 GLint row;
281 for (row = 0; row < drawHeight; row++) {
282 ASSERT(drawWidth <= MAX_WIDTH);
283 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
284 span.array->rgba8);
285 span.x = destX;
286 span.y = destY;
287 span.end = drawWidth;
288 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
289 span.array->rgba8);
290 src += unpack.RowLength;
291 destY++;
292 }
293 }
294 return GL_TRUE;
295 }
296 else if (!ctx->Visual.rgbMode && rbType == GL_UNSIGNED_INT) {
297 /* write CI data to CI frame buffer */
298 GLint row;
299 if (simpleZoom) {
300 for (row = 0; row < drawHeight; row++) {
301 GLuint index32[MAX_WIDTH];
302 GLint col;
303 for (col = 0; col < drawWidth; col++)
304 index32[col] = src[col];
305 rb->PutRow(ctx, rb, drawWidth, destX, destY, index32, NULL);
306 src += unpack.RowLength;
307 destY += yStep;
308 }
309 return GL_TRUE;
310 }
311 }
312 }
313
314 /* can't handle this pixel format and/or data type */
315 return GL_FALSE;
316 }
317
318
319
320 /*
321 * Draw color index image.
322 */
323 static void
324 draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
325 GLsizei width, GLsizei height,
326 GLenum type,
327 const struct gl_pixelstore_attrib *unpack,
328 const GLvoid *pixels )
329 {
330 const GLint imgX = x, imgY = y;
331 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
332 GLint row, skipPixels;
333 SWspan span;
334
335 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
336 _swrast_span_default_attribs(ctx, &span);
337
338 /*
339 * General solution
340 */
341 skipPixels = 0;
342 while (skipPixels < width) {
343 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
344 ASSERT(spanWidth <= MAX_WIDTH);
345 for (row = 0; row < height; row++) {
346 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
347 width, height,
348 GL_COLOR_INDEX, type,
349 row, skipPixels);
350 _mesa_unpack_index_span(ctx, spanWidth, GL_UNSIGNED_INT,
351 span.array->index, type, source, unpack,
352 ctx->_ImageTransferState);
353
354 /* These may get changed during writing/clipping */
355 span.x = x + skipPixels;
356 span.y = y + row;
357 span.end = spanWidth;
358
359 if (zoom)
360 _swrast_write_zoomed_index_span(ctx, imgX, imgY, &span);
361 else
362 _swrast_write_index_span(ctx, &span);
363 }
364 skipPixels += spanWidth;
365 }
366 }
367
368
369
370 /*
371 * Draw stencil image.
372 */
373 static void
374 draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
375 GLsizei width, GLsizei height,
376 GLenum type,
377 const struct gl_pixelstore_attrib *unpack,
378 const GLvoid *pixels )
379 {
380 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
381 GLint skipPixels;
382
383 /* if width > MAX_WIDTH, have to process image in chunks */
384 skipPixels = 0;
385 while (skipPixels < width) {
386 const GLint spanX = x + skipPixels;
387 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
388 GLint row;
389 for (row = 0; row < height; row++) {
390 const GLint spanY = y + row;
391 GLstencil values[MAX_WIDTH];
392 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
393 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
394 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
395 width, height,
396 GL_COLOR_INDEX, type,
397 row, skipPixels);
398 _mesa_unpack_index_span(ctx, spanWidth, destType, values,
399 type, source, unpack,
400 ctx->_ImageTransferState);
401 _mesa_apply_stencil_transfer_ops(ctx, spanWidth, values);
402 if (zoom) {
403 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
404 spanX, spanY, values);
405 }
406 else {
407 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
408 }
409 }
410 skipPixels += spanWidth;
411 }
412 }
413
414
415 /*
416 * Draw depth image.
417 */
418 static void
419 draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
420 GLsizei width, GLsizei height,
421 GLenum type,
422 const struct gl_pixelstore_attrib *unpack,
423 const GLvoid *pixels )
424 {
425 const GLboolean scaleOrBias
426 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
427 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
428 SWspan span;
429
430 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
431 _swrast_span_default_attribs(ctx, &span);
432
433 if (type == GL_UNSIGNED_SHORT
434 && ctx->DrawBuffer->Visual.depthBits == 16
435 && !scaleOrBias
436 && !zoom
437 && ctx->Visual.rgbMode
438 && width <= MAX_WIDTH
439 && !unpack->SwapBytes) {
440 /* Special case: directly write 16-bit depth values */
441 GLint row;
442 for (row = 0; row < height; row++) {
443 const GLushort *zSrc = (const GLushort *)
444 _mesa_image_address2d(unpack, pixels, width, height,
445 GL_DEPTH_COMPONENT, type, row, 0);
446 GLint i;
447 for (i = 0; i < width; i++)
448 span.array->z[i] = zSrc[i];
449 span.x = x;
450 span.y = y + row;
451 span.end = width;
452 _swrast_write_rgba_span(ctx, &span);
453 }
454 }
455 else if (type == GL_UNSIGNED_INT
456 && !scaleOrBias
457 && !zoom
458 && ctx->Visual.rgbMode
459 && width <= MAX_WIDTH
460 && !unpack->SwapBytes) {
461 /* Special case: shift 32-bit values down to Visual.depthBits */
462 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
463 GLint row;
464 for (row = 0; row < height; row++) {
465 const GLuint *zSrc = (const GLuint *)
466 _mesa_image_address2d(unpack, pixels, width, height,
467 GL_DEPTH_COMPONENT, type, row, 0);
468 if (shift == 0) {
469 _mesa_memcpy(span.array->z, zSrc, width * sizeof(GLuint));
470 }
471 else {
472 GLint col;
473 for (col = 0; col < width; col++)
474 span.array->z[col] = zSrc[col] >> shift;
475 }
476 span.x = x;
477 span.y = y + row;
478 span.end = width;
479 _swrast_write_rgba_span(ctx, &span);
480 }
481 }
482 else {
483 /* General case */
484 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
485 GLint skipPixels = 0;
486
487 /* in case width > MAX_WIDTH do the copy in chunks */
488 while (skipPixels < width) {
489 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
490 GLint row;
491 ASSERT(span.end <= MAX_WIDTH);
492 for (row = 0; row < height; row++) {
493 const GLvoid *zSrc = _mesa_image_address2d(unpack,
494 pixels, width, height,
495 GL_DEPTH_COMPONENT, type,
496 row, skipPixels);
497
498 /* Set these for each row since the _swrast_write_* function may
499 * change them while clipping.
500 */
501 span.x = x + skipPixels;
502 span.y = y + row;
503 span.end = spanWidth;
504
505 _mesa_unpack_depth_span(ctx, spanWidth,
506 GL_UNSIGNED_INT, span.array->z, depthMax,
507 type, zSrc, unpack);
508 if (zoom) {
509 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
510 }
511 else if (ctx->Visual.rgbMode) {
512 _swrast_write_rgba_span(ctx, &span);
513 }
514 else {
515 _swrast_write_index_span(ctx, &span);
516 }
517 }
518 skipPixels += spanWidth;
519 }
520 }
521 }
522
523
524
525 /**
526 * Draw RGBA image.
527 */
528 static void
529 draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
530 GLsizei width, GLsizei height,
531 GLenum format, GLenum type,
532 const struct gl_pixelstore_attrib *unpack,
533 const GLvoid *pixels )
534 {
535 SWcontext *swrast = SWRAST_CONTEXT(ctx);
536 const GLbitfield prevActiveAttribs = swrast->_ActiveAttribMask;
537 const GLint imgX = x, imgY = y;
538 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
539 GLfloat *convImage = NULL;
540 GLbitfield transferOps = ctx->_ImageTransferState;
541 SWspan span;
542
543 /* don't interpolate COL0 and overwrite the glDrawPixel colors! */
544 swrast->_ActiveAttribMask &= ~FRAG_BIT_COL0;
545
546 /* Try an optimized glDrawPixels first */
547 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
548 unpack, pixels)) {
549 goto end;
550 }
551
552 INIT_SPAN(span, GL_BITMAP, 0, 0x0, SPAN_RGBA);
553 _swrast_span_default_attribs(ctx, &span);
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 goto end;
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 goto end;
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 end:
680 swrast->_ActiveAttribMask = prevActiveAttribs;
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 GLfloat depthScale = ctx->DrawBuffer->_DepthMaxF;
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, depthScale,
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 /**
820 * Execute software-based glDrawPixels.
821 * By time we get here, all error checking will have been done.
822 */
823 void
824 _swrast_DrawPixels( GLcontext *ctx,
825 GLint x, GLint y,
826 GLsizei width, GLsizei height,
827 GLenum format, GLenum type,
828 const struct gl_pixelstore_attrib *unpack,
829 const GLvoid *pixels )
830 {
831 SWcontext *swrast = SWRAST_CONTEXT(ctx);
832
833 RENDER_START(swrast,ctx);
834
835 if (ctx->NewState)
836 _mesa_update_state(ctx);
837
838 if (swrast->NewState)
839 _swrast_validate_derived( ctx );
840
841 if (unpack->BufferObj->Name) {
842 /* unpack from PBO */
843 GLubyte *buf;
844 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
845 format, type, pixels)) {
846 _mesa_error(ctx, GL_INVALID_OPERATION,
847 "glDrawPixels(invalid PBO access)");
848 goto end;
849 }
850 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
851 GL_READ_ONLY_ARB,
852 unpack->BufferObj);
853 if (!buf) {
854 /* buffer is already mapped - that's an error */
855 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(PBO is mapped)");
856 goto end;
857 }
858 pixels = ADD_POINTERS(buf, pixels);
859 }
860
861 switch (format) {
862 case GL_STENCIL_INDEX:
863 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
864 break;
865 case GL_DEPTH_COMPONENT:
866 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
867 break;
868 case GL_COLOR_INDEX:
869 if (ctx->Visual.rgbMode)
870 draw_rgba_pixels(ctx, x,y, width, height, format, type, unpack, pixels);
871 else
872 draw_index_pixels(ctx, x, y, width, height, type, unpack, pixels);
873 break;
874 case GL_RED:
875 case GL_GREEN:
876 case GL_BLUE:
877 case GL_ALPHA:
878 case GL_LUMINANCE:
879 case GL_LUMINANCE_ALPHA:
880 case GL_RGB:
881 case GL_BGR:
882 case GL_RGBA:
883 case GL_BGRA:
884 case GL_ABGR_EXT:
885 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
886 break;
887 case GL_DEPTH_STENCIL_EXT:
888 draw_depth_stencil_pixels(ctx, x, y, width, height,
889 type, unpack, pixels);
890 break;
891 default:
892 _mesa_problem(ctx, "unexpected format in _swrast_DrawPixels");
893 /* don't return yet, clean-up */
894 }
895
896 end:
897
898 RENDER_FINISH(swrast,ctx);
899
900 if (unpack->BufferObj->Name) {
901 /* done with PBO so unmap it now */
902 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
903 unpack->BufferObj);
904 }
905 }
906
907
908
909 #if 0 /* experimental */
910 /*
911 * Execute glDrawDepthPixelsMESA().
912 */
913 void
914 _swrast_DrawDepthPixelsMESA( GLcontext *ctx,
915 GLint x, GLint y,
916 GLsizei width, GLsizei height,
917 GLenum colorFormat, GLenum colorType,
918 const GLvoid *colors,
919 GLenum depthType, const GLvoid *depths,
920 const struct gl_pixelstore_attrib *unpack )
921 {
922 SWcontext *swrast = SWRAST_CONTEXT(ctx);
923
924 if (swrast->NewState)
925 _swrast_validate_derived( ctx );
926
927 RENDER_START(swrast,ctx);
928
929 switch (colorFormat) {
930 case GL_COLOR_INDEX:
931 if (ctx->Visual.rgbMode)
932 draw_rgba_pixels(ctx, x,y, width, height, colorFormat, colorType,
933 unpack, colors);
934 else
935 draw_index_pixels(ctx, x, y, width, height, colorType,
936 unpack, colors);
937 break;
938 case GL_RED:
939 case GL_GREEN:
940 case GL_BLUE:
941 case GL_ALPHA:
942 case GL_LUMINANCE:
943 case GL_LUMINANCE_ALPHA:
944 case GL_RGB:
945 case GL_BGR:
946 case GL_RGBA:
947 case GL_BGRA:
948 case GL_ABGR_EXT:
949 draw_rgba_pixels(ctx, x, y, width, height, colorFormat, colorType,
950 unpack, colors);
951 break;
952 default:
953 _mesa_problem(ctx, "unexpected format in glDrawDepthPixelsMESA");
954 }
955
956 RENDER_FINISH(swrast,ctx);
957 }
958 #endif