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/macros.h"
32 #include "main/imports.h"
33 #include "main/state.h"
35 #include "s_context.h"
37 #include "s_stencil.h"
43 * Try to do a fast and simple RGB(a) glDrawPixels.
44 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
47 fast_draw_rgba_pixels(struct gl_context *ctx, GLint x, GLint y,
48 GLsizei width, GLsizei height,
49 GLenum format, GLenum type,
50 const struct gl_pixelstore_attrib *userUnpack,
53 const GLint imgX = x, imgY = y;
54 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
56 SWcontext *swrast = SWRAST_CONTEXT(ctx);
59 GLint yStep; /* +1 or -1 */
60 struct gl_pixelstore_attrib unpack;
61 GLint destX, destY, drawWidth, drawHeight; /* post clipping */
64 return GL_TRUE; /* no-op */
66 rbType = rb->DataType;
68 if ((swrast->_RasterMask & ~CLIP_BIT) ||
69 ctx->Texture._EnabledCoordUnits ||
70 userUnpack->SwapBytes ||
71 ctx->_ImageTransferState) {
72 /* can't handle any of those conditions */
76 INIT_SPAN(span, GL_BITMAP);
77 span.arrayMask = SPAN_RGBA;
78 span.arrayAttribs = FRAG_BIT_COL0;
79 _swrast_span_default_attribs(ctx, &span);
81 /* copy input params since clipping may change them */
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 */
97 yStep = (GLint) ctx->Pixel.ZoomY;
98 ASSERT(yStep == 1 || yStep == -1);
101 /* non-simple zooming */
102 simpleZoom = GL_FALSE;
104 if (unpack.RowLength == 0)
105 unpack.RowLength = width;
112 if (format == GL_RGBA && type == rbType) {
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,
120 for (row = 0; row < drawHeight; row++) {
121 rb->PutRow(ctx, rb, drawWidth, destX, destY, src, NULL);
129 for (row = 0; row < drawHeight; row++) {
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);
137 span.array->ChanType = CHAN_TYPE;
142 if (format == GL_RGB && type == rbType) {
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,
150 for (row = 0; row < drawHeight; row++) {
151 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, src, NULL);
159 for (row = 0; row < drawHeight; row++) {
162 span.end = drawWidth;
163 span.array->ChanType = rbType;
164 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, src);
168 span.array->ChanType = CHAN_TYPE;
173 /* Remaining cases haven't been tested with alignment != 1 */
174 if (userUnpack->Alignment != 1)
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);
183 ASSERT(drawWidth <= MAX_WIDTH);
184 for (row = 0; row < drawHeight; row++) {
185 GLchan rgb[MAX_WIDTH][3];
187 for (i = 0;i<drawWidth;i++) {
192 rb->PutRowRGB(ctx, rb, drawWidth, destX, destY, rgb, NULL);
193 src += unpack.RowLength;
200 ASSERT(drawWidth <= MAX_WIDTH);
201 for (row = 0; row < drawHeight; row++) {
202 GLchan rgb[MAX_WIDTH][3];
204 for (i = 0;i<drawWidth;i++) {
211 span.end = drawWidth;
212 _swrast_write_zoomed_rgb_span(ctx, imgX, imgY, &span, rgb);
213 src += unpack.RowLength;
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;
225 ASSERT(drawWidth <= MAX_WIDTH);
226 for (row = 0; row < drawHeight; row++) {
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++;
235 rb->PutRow(ctx, rb, drawWidth, destX, destY,
236 span.array->rgba, NULL);
237 src += unpack.RowLength*2;
244 ASSERT(drawWidth <= MAX_WIDTH);
245 for (row = 0; row < drawHeight; row++) {
246 const GLchan *ptr = src;
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++;
256 span.end = drawWidth;
257 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
259 src += unpack.RowLength*2;
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 (rbType == GL_UNSIGNED_BYTE) {
270 /* convert ubyte/CI data to ubyte/RGBA */
273 for (row = 0; row < drawHeight; row++) {
274 ASSERT(drawWidth <= MAX_WIDTH);
275 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
277 rb->PutRow(ctx, rb, drawWidth, destX, destY,
278 span.array->rgba8, NULL);
279 src += unpack.RowLength;
284 /* ubyte/CI to ubyte/RGBA with zooming */
286 for (row = 0; row < drawHeight; row++) {
287 ASSERT(drawWidth <= MAX_WIDTH);
288 _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
292 span.end = drawWidth;
293 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
295 src += unpack.RowLength;
303 /* can't handle this pixel format and/or data type */
310 * Draw stencil image.
313 draw_stencil_pixels( struct gl_context *ctx, GLint x, GLint y,
314 GLsizei width, GLsizei height,
316 const struct gl_pixelstore_attrib *unpack,
317 const GLvoid *pixels )
319 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
322 /* if width > MAX_WIDTH, have to process image in chunks */
324 while (skipPixels < width) {
325 const GLint spanX = x + skipPixels;
326 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
328 for (row = 0; row < height; row++) {
329 const GLint spanY = y + row;
330 GLstencil values[MAX_WIDTH];
331 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
332 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
333 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
335 GL_COLOR_INDEX, type,
337 _mesa_unpack_stencil_span(ctx, spanWidth, destType, values,
338 type, source, unpack,
339 ctx->_ImageTransferState);
341 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
342 spanX, spanY, values);
345 _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
348 skipPixels += spanWidth;
357 draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
358 GLsizei width, GLsizei height,
360 const struct gl_pixelstore_attrib *unpack,
361 const GLvoid *pixels )
363 const GLboolean scaleOrBias
364 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
365 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
368 INIT_SPAN(span, GL_BITMAP);
369 span.arrayMask = SPAN_Z;
370 _swrast_span_default_attribs(ctx, &span);
372 if (type == GL_UNSIGNED_SHORT
373 && ctx->DrawBuffer->Visual.depthBits == 16
376 && width <= MAX_WIDTH
377 && !unpack->SwapBytes) {
378 /* Special case: directly write 16-bit depth values */
380 for (row = 0; row < height; row++) {
381 const GLushort *zSrc = (const GLushort *)
382 _mesa_image_address2d(unpack, pixels, width, height,
383 GL_DEPTH_COMPONENT, type, row, 0);
385 for (i = 0; i < width; i++)
386 span.array->z[i] = zSrc[i];
390 _swrast_write_rgba_span(ctx, &span);
393 else if (type == GL_UNSIGNED_INT
396 && width <= MAX_WIDTH
397 && !unpack->SwapBytes) {
398 /* Special case: shift 32-bit values down to Visual.depthBits */
399 const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
401 for (row = 0; row < height; row++) {
402 const GLuint *zSrc = (const GLuint *)
403 _mesa_image_address2d(unpack, pixels, width, height,
404 GL_DEPTH_COMPONENT, type, row, 0);
406 memcpy(span.array->z, zSrc, width * sizeof(GLuint));
410 for (col = 0; col < width; col++)
411 span.array->z[col] = zSrc[col] >> shift;
416 _swrast_write_rgba_span(ctx, &span);
421 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
422 GLint skipPixels = 0;
424 /* in case width > MAX_WIDTH do the copy in chunks */
425 while (skipPixels < width) {
426 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
428 ASSERT(span.end <= MAX_WIDTH);
429 for (row = 0; row < height; row++) {
430 const GLvoid *zSrc = _mesa_image_address2d(unpack,
431 pixels, width, height,
432 GL_DEPTH_COMPONENT, type,
435 /* Set these for each row since the _swrast_write_* function may
436 * change them while clipping.
438 span.x = x + skipPixels;
440 span.end = spanWidth;
442 _mesa_unpack_depth_span(ctx, spanWidth,
443 GL_UNSIGNED_INT, span.array->z, depthMax,
446 _swrast_write_zoomed_depth_span(ctx, x, y, &span);
449 _swrast_write_rgba_span(ctx, &span);
452 skipPixels += spanWidth;
463 draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
464 GLsizei width, GLsizei height,
465 GLenum format, GLenum type,
466 const struct gl_pixelstore_attrib *unpack,
467 const GLvoid *pixels )
469 const GLint imgX = x, imgY = y;
470 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
471 GLfloat *convImage = NULL;
472 GLbitfield transferOps = ctx->_ImageTransferState;
475 /* Try an optimized glDrawPixels first */
476 if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
481 INIT_SPAN(span, GL_BITMAP);
482 _swrast_span_default_attribs(ctx, &span);
483 span.arrayMask = SPAN_RGBA;
484 span.arrayAttribs = FRAG_BIT_COL0; /* we're fill in COL0 attrib values */
486 if (ctx->DrawBuffer->_NumColorDrawBuffers > 0 &&
487 ctx->DrawBuffer->_ColorDrawBuffers[0]->DataType != GL_FLOAT &&
488 ctx->Color.ClampFragmentColor != GL_FALSE) {
489 /* need to clamp colors before applying fragment ops */
490 transferOps |= IMAGE_CLAMP_BIT;
497 const GLbitfield interpMask = span.interpMask;
498 const GLbitfield arrayMask = span.arrayMask;
499 const GLint srcStride
500 = _mesa_image_row_stride(unpack, width, format, type);
501 GLint skipPixels = 0;
502 /* use span array for temp color storage */
503 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
505 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
506 while (skipPixels < width) {
507 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
508 const GLubyte *source
509 = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
510 width, height, format,
511 type, 0, skipPixels);
514 for (row = 0; row < height; row++) {
515 /* get image row as float/RGBA */
516 _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
517 format, type, source, unpack,
519 /* Set these for each row since the _swrast_write_* functions
520 * may change them while clipping/rendering.
522 span.array->ChanType = GL_FLOAT;
523 span.x = x + skipPixels;
525 span.end = spanWidth;
526 span.arrayMask = arrayMask;
527 span.interpMask = interpMask;
529 _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span, rgba);
532 _swrast_write_rgba_span(ctx, &span);
538 skipPixels += spanWidth;
539 } /* while skipPixels < width */
541 /* XXX this is ugly/temporary, to undo above change */
542 span.array->ChanType = CHAN_TYPE;
552 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
553 * The only per-pixel operations that apply are depth scale/bias,
554 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
556 * Also, only the depth buffer and stencil buffers are touched, not the
560 draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
561 GLsizei width, GLsizei height, GLenum type,
562 const struct gl_pixelstore_attrib *unpack,
563 const GLvoid *pixels)
565 const GLint imgX = x, imgY = y;
566 const GLboolean scaleOrBias
567 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
568 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
569 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
570 const GLuint stencilType = (STENCIL_BITS == 8) ?
571 GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
572 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
573 struct gl_renderbuffer *depthRb, *stencilRb;
574 struct gl_pixelstore_attrib clippedUnpack = *unpack;
577 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
579 /* totally clipped */
584 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
585 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
589 if (depthRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
590 stencilRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
591 depthRb == stencilRb &&
595 (stencilMask & 0xff) == 0xff) {
596 /* This is the ideal case.
597 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
598 * Plus, no pixel transfer ops, zooming, or masking needed.
601 for (i = 0; i < height; i++) {
602 const GLuint *src = (const GLuint *)
603 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
604 GL_DEPTH_STENCIL_EXT, type, i, 0);
605 depthRb->PutRow(ctx, depthRb, width, x, y + i, src, NULL);
609 /* sub-optimal cases:
610 * Separate depth/stencil buffers, or pixel transfer ops required.
612 /* XXX need to handle very wide images (skippixels) */
615 depthRb = ctx->DrawBuffer->_DepthBuffer;
616 stencilRb = ctx->DrawBuffer->_StencilBuffer;
618 for (i = 0; i < height; i++) {
619 const GLuint *depthStencilSrc = (const GLuint *)
620 _mesa_image_address2d(&clippedUnpack, pixels, width, height,
621 GL_DEPTH_STENCIL_EXT, type, i, 0);
623 if (ctx->Depth.Mask) {
624 if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 24) {
625 /* fast path 24-bit zbuffer */
626 GLuint zValues[MAX_WIDTH];
628 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
629 for (j = 0; j < width; j++) {
630 zValues[j] = depthStencilSrc[j] >> 8;
633 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
636 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
638 else if (!scaleOrBias && ctx->DrawBuffer->Visual.depthBits == 16) {
639 /* fast path 16-bit zbuffer */
640 GLushort zValues[MAX_WIDTH];
642 ASSERT(depthRb->DataType == GL_UNSIGNED_SHORT);
643 for (j = 0; j < width; j++) {
644 zValues[j] = depthStencilSrc[j] >> 16;
647 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width,
650 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
654 GLuint zValues[MAX_WIDTH]; /* 16 or 32-bit Z value storage */
655 _mesa_unpack_depth_span(ctx, width,
656 depthRb->DataType, zValues, depthMax,
657 type, depthStencilSrc, &clippedUnpack);
659 _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
663 depthRb->PutRow(ctx, depthRb, width, x, y + i, zValues,NULL);
668 if (stencilMask != 0x0) {
669 GLstencil stencilValues[MAX_WIDTH];
670 /* get stencil values, with shift/offset/mapping */
671 _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
672 type, depthStencilSrc, &clippedUnpack,
673 ctx->_ImageTransferState);
675 _swrast_write_zoomed_stencil_span(ctx, imgX, imgY, width,
676 x, y + i, stencilValues);
678 _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
686 * Execute software-based glDrawPixels.
687 * By time we get here, all error checking will have been done.
690 _swrast_DrawPixels( struct gl_context *ctx,
692 GLsizei width, GLsizei height,
693 GLenum format, GLenum type,
694 const struct gl_pixelstore_attrib *unpack,
695 const GLvoid *pixels )
697 SWcontext *swrast = SWRAST_CONTEXT(ctx);
698 GLboolean save_vp_override = ctx->VertexProgram._Overriden;
700 if (!_mesa_check_conditional_render(ctx))
701 return; /* don't draw */
703 /* We are creating fragments directly, without going through vertex
706 * This override flag tells the fragment processing code that its input
707 * comes from a non-standard source, and it may therefore not rely on
708 * optimizations that assume e.g. constant color if there is no color
711 _mesa_set_vp_override(ctx, GL_TRUE);
713 swrast_render_start(ctx);
716 _mesa_update_state(ctx);
718 if (swrast->NewState)
719 _swrast_validate_derived( ctx );
721 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
723 swrast_render_finish(ctx);
724 _mesa_set_vp_override(ctx, save_vp_override);
729 case GL_STENCIL_INDEX:
730 draw_stencil_pixels( ctx, x, y, width, height, type, unpack, pixels );
732 case GL_DEPTH_COMPONENT:
733 draw_depth_pixels( ctx, x, y, width, height, type, unpack, pixels );
741 case GL_LUMINANCE_ALPHA:
747 draw_rgba_pixels(ctx, x, y, width, height, format, type, unpack, pixels);
749 case GL_DEPTH_STENCIL_EXT:
750 draw_depth_stencil_pixels(ctx, x, y, width, height,
751 type, unpack, pixels);
754 _mesa_problem(ctx, "unexpected format in _swrast_DrawPixels");
755 /* don't return yet, clean-up */
758 swrast_render_finish(ctx);
759 _mesa_set_vp_override(ctx, save_vp_override);
761 _mesa_unmap_pbo_source(ctx, unpack);