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