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