2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #include "main/glheader.h"
27 #include "main/bufferobj.h"
28 #include "main/condrender.h"
29 #include "main/context.h"
30 #include "main/image.h"
31 #include "main/imports.h"
32 #include "main/macros.h"
33 #include "main/pack.h"
35 #include "main/pixeltransfer.h"
36 #include "main/state.h"
38 #include "s_context.h"
40 #include "s_stencil.h"
46 * Try to do a fast and simple RGB(a) glDrawPixels.
47 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
50 fast_draw_rgba_pixels(struct gl_context *ctx, GLint x, GLint y,
51 GLsizei width, GLsizei height,
52 GLenum format, GLenum type,
53 const struct gl_pixelstore_attrib *userUnpack,
56 const GLint imgX = x, imgY = y;
57 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
59 SWcontext *swrast = SWRAST_CONTEXT(ctx);
62 GLint yStep; /* +1 or -1 */
63 struct gl_pixelstore_attrib unpack;
64 GLint destX, destY, drawWidth, drawHeight; /* post clipping */
67 return GL_TRUE; /* no-op */
69 rbType = rb->DataType;
71 if ((swrast->_RasterMask & ~CLIP_BIT) ||
72 ctx->Texture._EnabledCoordUnits ||
73 userUnpack->SwapBytes ||
74 ctx->_ImageTransferState) {
75 /* can't handle any of those conditions */
79 INIT_SPAN(span, GL_BITMAP);
80 span.arrayMask = SPAN_RGBA;
81 span.arrayAttribs = FRAG_BIT_COL0;
82 _swrast_span_default_attribs(ctx, &span);
84 /* copy input params since clipping may change them */
91 /* check for simple zooming and clipping */
92 if (ctx->Pixel.ZoomX == 1.0F &&
93 (ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F)) {
94 if (!_mesa_clip_drawpixels(ctx, &destX, &destY,
95 &drawWidth, &drawHeight, &unpack)) {
96 /* image was completely clipped: no-op, all done */
100 yStep = (GLint) ctx->Pixel.ZoomY;
101 ASSERT(yStep == 1 || yStep == -1);
104 /* non-simple zooming */
105 simpleZoom = GL_FALSE;
107 if (unpack.RowLength == 0)
108 unpack.RowLength = width;
114 swrast_render_start(ctx);
116 if (format == GL_RGBA && type == rbType) {
118 = (const GLubyte *) _mesa_image_address2d(&unpack, pixels, width,
119 height, format, type, 0, 0);
120 const GLint srcStride = _mesa_image_row_stride(&unpack, width,
124 for (row = 0; row < drawHeight; row++) {
125 rb->PutRow(ctx, rb, drawWidth, destX, destY, src, NULL);
133 for (row = 0; row < drawHeight; row++) {
135 span.y = destY + row;
136 span.end = drawWidth;
137 span.array->ChanType = rbType;
138 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, src);
141 span.array->ChanType = CHAN_TYPE;
146 if (format == GL_RGB && type == rbType) {
148 = (const GLubyte *) _mesa_image_address2d(&unpack, pixels, width,
149 height, format, type, 0, 0);
150 const GLint srcStride = _mesa_image_row_stride(&unpack, width,
154 for (row = 0; row < drawHeight; row++) {
155 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, src, NULL);
163 for (row = 0; row < drawHeight; row++) {
166 span.end = drawWidth;
167 span.array->ChanType = rbType;
168 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, src);
172 span.array->ChanType = CHAN_TYPE;
177 /* Remaining cases haven't been tested with alignment != 1 */
178 if (userUnpack->Alignment != 1) {
179 swrast_render_finish(ctx);
183 if (format == GL_LUMINANCE && type == CHAN_TYPE && rbType == CHAN_TYPE) {
184 const GLchan *src = (const GLchan *) pixels
185 + (unpack.SkipRows * unpack.RowLength + unpack.SkipPixels);
189 ASSERT(drawWidth <= MAX_WIDTH);
190 for (row = 0; row < drawHeight; row++) {
191 GLchan rgb[MAX_WIDTH][3];
193 for (i = 0;i<drawWidth;i++) {
198 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, rgb, NULL);
199 src += unpack.RowLength;
206 ASSERT(drawWidth <= MAX_WIDTH);
207 for (row = 0; row < drawHeight; row++) {
208 GLchan rgb[MAX_WIDTH][3];
210 for (i = 0;i<drawWidth;i++) {
217 span.end = drawWidth;
218 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, rgb);
219 src += unpack.RowLength;
226 if (format == GL_LUMINANCE_ALPHA && type == CHAN_TYPE && rbType == CHAN_TYPE) {
227 const GLchan *src = (const GLchan *) pixels
228 + (unpack.SkipRows * unpack.RowLength + unpack.SkipPixels)*2;
231 ASSERT(drawWidth <= MAX_WIDTH);
232 for (row = 0; row < drawHeight; row++) {
234 const GLchan *ptr = src;
235 for (i = 0;i<drawWidth;i++) {
236 span.array->rgba[i][0] = *ptr;
237 span.array->rgba[i][1] = *ptr;
238 span.array->rgba[i][2] = *ptr++;
239 span.array->rgba[i][3] = *ptr++;
241 rb->PutRow(ctx, rb, drawWidth, destX, destY,
242 span.array->rgba, NULL);
243 src += unpack.RowLength*2;
250 ASSERT(drawWidth <= MAX_WIDTH);
251 for (row = 0; row < drawHeight; row++) {
252 const GLchan *ptr = src;
254 for (i = 0;i<drawWidth;i++) {
255 span.array->rgba[i][0] = *ptr;
256 span.array->rgba[i][1] = *ptr;
257 span.array->rgba[i][2] = *ptr++;
258 span.array->rgba[i][3] = *ptr++;
262 span.end = drawWidth;
263 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
265 src += unpack.RowLength*2;
272 if (format == GL_COLOR_INDEX && type == GL_UNSIGNED_BYTE) {
273 const GLubyte *src = (const GLubyte *) pixels
274 + unpack.SkipRows * unpack.RowLength + unpack.SkipPixels;
275 if (rbType == GL_UNSIGNED_BYTE) {
276 /* convert ubyte/CI data to ubyte/RGBA */
279 for (row = 0; row < drawHeight; row++) {
280 ASSERT(drawWidth <= MAX_WIDTH);
281 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
283 rb->PutRow(ctx, rb, drawWidth, destX, destY,
284 span.array->rgba8, NULL);
285 src += unpack.RowLength;
290 /* ubyte/CI to ubyte/RGBA with zooming */
292 for (row = 0; row < drawHeight; row++) {
293 ASSERT(drawWidth <= MAX_WIDTH);
294 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
298 span.end = drawWidth;
299 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
301 src += unpack.RowLength;
309 /* can't handle this pixel format and/or data type */
313 /* success, unmap render buffers */
314 swrast_render_finish(ctx);
321 * Draw stencil image.
324 draw_stencil_pixels( struct gl_context *ctx, GLint x, GLint y,
325 GLsizei width, GLsizei height,
327 const struct gl_pixelstore_attrib *unpack,
328 const GLvoid *pixels )
330 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
331 const GLenum destType = GL_UNSIGNED_BYTE;
334 /* if width > MAX_WIDTH, have to process image in chunks */
336 while (skipPixels < width) {
337 const GLint spanX = x + skipPixels;
338 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
340 for (row = 0; row < height; row++) {
341 const GLint spanY = y + row;
342 GLubyte values[MAX_WIDTH];
343 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
345 GL_STENCIL_INDEX, type,
347 _mesa_unpack_stencil_span(ctx, spanWidth, destType, values,
348 type, source, unpack,
349 ctx->_ImageTransferState);
351 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
352 spanX, spanY, values);
355 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
358 skipPixels += spanWidth;
367 draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
368 GLsizei width, GLsizei height,
370 const struct gl_pixelstore_attrib *unpack,
371 const GLvoid *pixels )
373 const GLboolean scaleOrBias
374 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
375 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
378 INIT_SPAN(span, GL_BITMAP);
379 span.arrayMask = SPAN_Z;
380 _swrast_span_default_attribs(ctx, &span);
382 if (type == GL_UNSIGNED_SHORT
383 && ctx->DrawBuffer->Visual.depthBits == 16
386 && width <= MAX_WIDTH
387 && !unpack->SwapBytes) {
388 /* Special case: directly write 16-bit depth values */
390 for (row = 0; row < height; row++) {
391 const GLushort *zSrc = (const GLushort *)
392 _mesa_image_address2d(unpack, pixels, width, height,
393 GL_DEPTH_COMPONENT, type, row, 0);
395 for (i = 0; i < width; i++)
396 span.array->z[i] = zSrc[i];
400 _swrast_write_rgba_span(ctx, &span);
403 else if (type == GL_UNSIGNED_INT
406 && width <= MAX_WIDTH
407 && !unpack->SwapBytes) {
408 /* Special case: shift 32-bit values down to Visual.depthBits */
409 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
411 for (row = 0; row < height; row++) {
412 const GLuint *zSrc = (const GLuint *)
413 _mesa_image_address2d(unpack, pixels, width, height,
414 GL_DEPTH_COMPONENT, type, row, 0);
416 memcpy(span.array->z, zSrc, width * sizeof(GLuint));
420 for (col = 0; col < width; col++)
421 span.array->z[col] = zSrc[col] >> shift;
426 _swrast_write_rgba_span(ctx, &span);
431 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
432 GLint skipPixels = 0;
434 /* in case width > MAX_WIDTH do the copy in chunks */
435 while (skipPixels < width) {
436 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
438 ASSERT(span.end <= MAX_WIDTH);
439 for (row = 0; row < height; row++) {
440 const GLvoid *zSrc = _mesa_image_address2d(unpack,
441 pixels, width, height,
442 GL_DEPTH_COMPONENT, type,
445 /* Set these for each row since the _swrast_write_* function may
446 * change them while clipping.
448 span.x = x + skipPixels;
450 span.end = spanWidth;
452 _mesa_unpack_depth_span(ctx, spanWidth,
453 GL_UNSIGNED_INT, span.array->z, depthMax,
456 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
459 _swrast_write_rgba_span(ctx, &span);
462 skipPixels += spanWidth;
473 draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
474 GLsizei width, GLsizei height,
475 GLenum format, GLenum type,
476 const struct gl_pixelstore_attrib *unpack,
477 const GLvoid *pixels )
479 const GLint imgX = x, imgY = y;
480 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
481 GLfloat *convImage = NULL;
482 GLbitfield transferOps = ctx->_ImageTransferState;
485 /* Try an optimized glDrawPixels first */
486 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
491 swrast_render_start(ctx);
493 INIT_SPAN(span, GL_BITMAP);
494 _swrast_span_default_attribs(ctx, &span);
495 span.arrayMask = SPAN_RGBA;
496 span.arrayAttribs = FRAG_BIT_COL0; /* we're fill in COL0 attrib values */
498 if (ctx->DrawBuffer->_NumColorDrawBuffers > 0 &&
499 ctx->DrawBuffer->_ColorDrawBuffers[0]->DataType != GL_FLOAT &&
500 ctx->Color.ClampFragmentColor != GL_FALSE) {
501 /* need to clamp colors before applying fragment ops */
502 transferOps |= IMAGE_CLAMP_BIT;
509 const GLbitfield interpMask = span.interpMask;
510 const GLbitfield arrayMask = span.arrayMask;
511 const GLint srcStride
512 = _mesa_image_row_stride(unpack, width, format, type);
513 GLint skipPixels = 0;
514 /* use span array for temp color storage */
515 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
517 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
518 while (skipPixels < width) {
519 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
520 const GLubyte *source
521 = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
522 width, height, format,
523 type, 0, skipPixels);
526 for (row = 0; row < height; row++) {
527 /* get image row as float/RGBA */
528 _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
529 format, type, source, unpack,
531 /* Set these for each row since the _swrast_write_* functions
532 * may change them while clipping/rendering.
534 span.array->ChanType = GL_FLOAT;
535 span.x = x + skipPixels;
537 span.end = spanWidth;
538 span.arrayMask = arrayMask;
539 span.interpMask = interpMask;
541 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, rgba);
544 _swrast_write_rgba_span(ctx, &span);
550 skipPixels += spanWidth;
551 } /* while skipPixels < width */
553 /* XXX this is ugly/temporary, to undo above change */
554 span.array->ChanType = CHAN_TYPE;
561 swrast_render_finish(ctx);
566 * Draw depth+stencil values into a MESA_FORAMT_Z24_S8 or MESA_FORMAT_S8_Z24
567 * renderbuffer. No masking, zooming, scaling, etc.
570 fast_draw_depth_stencil(struct gl_context *ctx, GLint x, GLint y,
571 GLsizei width, GLsizei height,
572 const struct gl_pixelstore_attrib *unpack,
573 const GLvoid *pixels)
575 const GLenum format = GL_DEPTH_STENCIL_EXT;
576 const GLenum type = GL_UNSIGNED_INT_24_8;
577 struct gl_renderbuffer *rb =
578 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
580 GLint srcRowStride, dstRowStride;
583 src = _mesa_image_address2d(unpack, pixels, width, height,
585 srcRowStride = _mesa_image_row_stride(unpack, width, format, type);
587 dst = _swrast_pixel_address(rb, x, y);
588 dstRowStride = rb->RowStride * 4;
590 for (i = 0; i < height; i++) {
591 _mesa_pack_uint_24_8_depth_stencil_row(rb->Format, width,
592 (const GLuint *) src, dst);
601 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
602 * The only per-pixel operations that apply are depth scale/bias,
603 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
605 * Also, only the depth buffer and stencil buffers are touched, not the
609 draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
610 GLsizei width, GLsizei height, GLenum type,
611 const struct gl_pixelstore_attrib *unpack,
612 const GLvoid *pixels)
614 const GLint imgX = x, imgY = y;
615 const GLboolean scaleOrBias
616 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
617 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
618 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
619 const GLenum stencilType = GL_UNSIGNED_BYTE;
620 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
621 struct gl_renderbuffer *depthRb, *stencilRb;
622 struct gl_pixelstore_attrib clippedUnpack = *unpack;
625 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
627 /* totally clipped */
632 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
633 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
637 if (depthRb == stencilRb &&
638 (depthRb->Format == MESA_FORMAT_Z24_S8 ||
639 depthRb->Format == MESA_FORMAT_S8_Z24) &&
640 type == GL_UNSIGNED_INT_24_8 &&
644 (stencilMask & 0xff) == 0xff) {
645 fast_draw_depth_stencil(ctx, x, y, width, height,
646 &clippedUnpack, pixels);
649 /* sub-optimal cases:
650 * Separate depth/stencil buffers, or pixel transfer ops required.
652 /* XXX need to handle very wide images (skippixels) */
655 depthRb = ctx->DrawBuffer->_DepthBuffer;
657 for (i = 0; i < height; i++) {
658 const GLuint *depthStencilSrc = (const GLuint *)
659 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
660 GL_DEPTH_STENCIL_EXT, type, i, 0);
662 if (ctx->Depth.Mask) {
663 if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 24 &&
664 type == GL_UNSIGNED_INT_24_8) {
665 /* fast path 24-bit zbuffer */
666 GLuint zValues[MAX_WIDTH];
668 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
669 for (j = 0; j < width; j++) {
670 zValues[j] = depthStencilSrc[j] >> 8;
673 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
676 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
678 else if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 16 &&
679 type == GL_UNSIGNED_INT_24_8) {
680 /* fast path 16-bit zbuffer */
681 GLushort zValues[MAX_WIDTH];
683 ASSERT(depthRb->DataType == GL_UNSIGNED_SHORT);
684 for (j = 0; j < width; j++) {
685 zValues[j] = depthStencilSrc[j] >> 16;
688 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
691 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
695 GLuint zValues[MAX_WIDTH]; /* 16 or 32-bit Z value storage */
696 _mesa_unpack_depth_span(ctx, width,
697 depthRb->DataType, zValues, depthMax,
698 type, depthStencilSrc, &clippedUnpack);
700 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
704 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
709 if (stencilMask != 0x0) {
710 GLubyte stencilValues[MAX_WIDTH];
711 /* get stencil values, with shift/offset/mapping */
712 _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
713 type, depthStencilSrc, &clippedUnpack,
714 ctx->_ImageTransferState);
716 _swrast_write_zoomed_stencil_span(ctx, imgX, imgY, width,
717 x, y + i, stencilValues);
719 _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
727 * Execute software-based glDrawPixels.
728 * By time we get here, all error checking will have been done.
731 _swrast_DrawPixels( struct gl_context *ctx,
733 GLsizei width, GLsizei height,
734 GLenum format, GLenum type,
735 const struct gl_pixelstore_attrib *unpack,
736 const GLvoid *pixels )
738 SWcontext *swrast = SWRAST_CONTEXT(ctx);
739 GLboolean save_vp_override = ctx->VertexProgram._Overriden;
741 if (!_mesa_check_conditional_render(ctx))
742 return; /* don't draw */
744 /* We are creating fragments directly, without going through vertex
747 * This override flag tells the fragment processing code that its input
748 * comes from a non-standard source, and it may therefore not rely on
749 * optimizations that assume e.g. constant color if there is no color
752 _mesa_set_vp_override(ctx, GL_TRUE);
755 _mesa_update_state(ctx);
757 if (swrast->NewState)
758 _swrast_validate_derived( ctx );
760 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
762 _mesa_set_vp_override(ctx, save_vp_override);
767 * By time we get here, all error checking should have been done.
770 case GL_STENCIL_INDEX:
771 swrast_render_start(ctx);
772 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
773 swrast_render_finish(ctx);
775 case GL_DEPTH_COMPONENT:
776 swrast_render_start(ctx);
777 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
778 swrast_render_finish(ctx);
780 case GL_DEPTH_STENCIL_EXT:
781 swrast_render_start(ctx);
782 draw_depth_stencil_pixels(ctx, x, y, width, height, type, unpack, pixels);
783 swrast_render_finish(ctx);
786 /* all other formats should be color formats */
787 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
790 _mesa_set_vp_override(ctx, save_vp_override);
792 _mesa_unmap_pbo_source(ctx, unpack);