util: rename PIPE_ARCH_*_ENDIAN to UTIL_ARCH_*_ENDIAN
[mesa.git] / src / mesa / swrast / s_drawpix.c
index 5828a789ab9e597609c00f4f7a85c1d73fba1138..7ee401b2d4ce0f9e3065b910a612502da77d07ca 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
  *
  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
@@ -29,6 +29,8 @@
 #include "main/condrender.h"
 #include "main/context.h"
 #include "main/format_pack.h"
+#include "main/format_utils.h"
+#include "main/glformats.h"
 #include "main/image.h"
 #include "main/imports.h"
 #include "main/macros.h"
@@ -53,7 +55,8 @@ fast_draw_rgb_ubyte_pixels(struct gl_context *ctx,
                            GLint x, GLint y,
                            GLsizei width, GLsizei height,
                            const struct gl_pixelstore_attrib *unpack,
-                           const GLvoid *pixels)
+                           const GLvoid *pixels,
+                           bool flip_y)
 {
    const GLubyte *src = (const GLubyte *)
       _mesa_image_address2d(unpack, pixels, width,
@@ -65,7 +68,8 @@ fast_draw_rgb_ubyte_pixels(struct gl_context *ctx,
    GLint dstRowStride;
 
    ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
-                               GL_MAP_WRITE_BIT, &dst, &dstRowStride);
+                               GL_MAP_WRITE_BIT, &dst, &dstRowStride,
+                               flip_y);
 
    if (!dst) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
@@ -100,7 +104,8 @@ fast_draw_rgba_ubyte_pixels(struct gl_context *ctx,
                            GLint x, GLint y,
                            GLsizei width, GLsizei height,
                            const struct gl_pixelstore_attrib *unpack,
-                           const GLvoid *pixels)
+                           const GLvoid *pixels,
+                           bool flip_y)
 {
    const GLubyte *src = (const GLubyte *)
       _mesa_image_address2d(unpack, pixels, width,
@@ -112,7 +117,8 @@ fast_draw_rgba_ubyte_pixels(struct gl_context *ctx,
    GLint dstRowStride;
 
    ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
-                               GL_MAP_WRITE_BIT, &dst, &dstRowStride);
+                               GL_MAP_WRITE_BIT, &dst, &dstRowStride,
+                               flip_y);
 
    if (!dst) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
@@ -149,7 +155,8 @@ fast_draw_generic_pixels(struct gl_context *ctx,
                          GLsizei width, GLsizei height,
                          GLenum format, GLenum type,
                          const struct gl_pixelstore_attrib *unpack,
-                         const GLvoid *pixels)
+                         const GLvoid *pixels,
+                         bool flip_y)
 {
    const GLubyte *src = (const GLubyte *)
       _mesa_image_address2d(unpack, pixels, width,
@@ -162,7 +169,8 @@ fast_draw_generic_pixels(struct gl_context *ctx,
    GLint dstRowStride;
 
    ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
-                               GL_MAP_WRITE_BIT, &dst, &dstRowStride);
+                               GL_MAP_WRITE_BIT, &dst, &dstRowStride,
+                               flip_y);
 
    if (!dst) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
@@ -195,6 +203,7 @@ fast_draw_rgba_pixels(struct gl_context *ctx, GLint x, GLint y,
                       const struct gl_pixelstore_attrib *userUnpack,
                       const GLvoid *pixels)
 {
+   struct gl_framebuffer *fb = ctx->DrawBuffer;
    struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    struct gl_pixelstore_attrib unpack;
@@ -223,25 +232,27 @@ fast_draw_rgba_pixels(struct gl_context *ctx, GLint x, GLint y,
 
    if (format == GL_RGB &&
        type == GL_UNSIGNED_BYTE &&
-       (rb->Format == MESA_FORMAT_XRGB8888 ||
-        rb->Format == MESA_FORMAT_ARGB8888)) {
+       (rb->Format == MESA_FORMAT_B8G8R8X8_UNORM ||
+        rb->Format == MESA_FORMAT_B8G8R8A8_UNORM)) {
       fast_draw_rgb_ubyte_pixels(ctx, rb, x, y, width, height,
-                                 &unpack, pixels);
+                                 &unpack, pixels, fb->FlipY);
       return GL_TRUE;
    }
 
    if (format == GL_RGBA &&
        type == GL_UNSIGNED_BYTE &&
-       (rb->Format == MESA_FORMAT_XRGB8888 ||
-        rb->Format == MESA_FORMAT_ARGB8888)) {
+       (rb->Format == MESA_FORMAT_B8G8R8X8_UNORM ||
+        rb->Format == MESA_FORMAT_B8G8R8A8_UNORM)) {
       fast_draw_rgba_ubyte_pixels(ctx, rb, x, y, width, height,
-                                  &unpack, pixels);
+                                  &unpack, pixels, fb->FlipY);
       return GL_TRUE;
    }
 
-   if (_mesa_format_matches_format_and_type(rb->Format, format, type)) {
+   if (_mesa_format_matches_format_and_type(rb->Format, format, type,
+                                            ctx->Unpack.SwapBytes, NULL)) {
       fast_draw_generic_pixels(ctx, rb, x, y, width, height,
-                               format, type, &unpack, pixels);
+                               format, type, &unpack, pixels,
+                               fb->FlipY);
       return GL_TRUE;
    }
 
@@ -261,36 +272,37 @@ draw_stencil_pixels( struct gl_context *ctx, GLint x, GLint y,
                      const struct gl_pixelstore_attrib *unpack,
                      const GLvoid *pixels )
 {
-   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
+   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    const GLenum destType = GL_UNSIGNED_BYTE;
-   GLint skipPixels;
+   GLint row;
+   GLubyte *values;
 
-   /* if width > MAX_WIDTH, have to process image in chunks */
-   skipPixels = 0;
-   while (skipPixels < width) {
-      const GLint spanX = x + skipPixels;
-      const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
-      GLint row;
-      for (row = 0; row < height; row++) {
-         const GLint spanY = y + row;
-         GLubyte values[MAX_WIDTH];
-         const GLvoid *source = _mesa_image_address2d(unpack, pixels,
-                                                      width, height,
-                                                      GL_STENCIL_INDEX, type,
-                                                      row, skipPixels);
-         _mesa_unpack_stencil_span(ctx, spanWidth, destType, values,
-                                   type, source, unpack,
-                                   ctx->_ImageTransferState);
-         if (zoom) {
-            _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
-                                              spanX, spanY, values);
-         }
-         else {
-            _swrast_write_stencil_span(ctx, spanWidth, spanX, spanY, values);
-         }
+   values = malloc(width * sizeof(GLubyte));
+   if (!values) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
+      return;
+   }
+
+   for (row = 0; row < height; row++) {
+      const GLvoid *source = _mesa_image_address2d(unpack, pixels,
+                                                   width, height,
+                                                   GL_STENCIL_INDEX, type,
+                                                   row, 0);
+      _mesa_unpack_stencil_span(ctx, width, destType, values,
+                                type, source, unpack,
+                                ctx->_ImageTransferState);
+      if (zoom) {
+         _swrast_write_zoomed_stencil_span(ctx, x, y, width,
+                                           x, y, values);
       }
-      skipPixels += spanWidth;
+      else {
+         _swrast_write_stencil_span(ctx, width, x, y, values);
+      }
+
+      y++;
    }
+
+   free(values);
 }
 
 
@@ -305,8 +317,8 @@ draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
                    const GLvoid *pixels )
 {
    const GLboolean scaleOrBias
-      = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
-   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
+      = ctx->Pixel.DepthScale != 1.0f || ctx->Pixel.DepthBias != 0.0f;
+   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0f || ctx->Pixel.ZoomY != 1.0f;
    SWspan span;
 
    INIT_SPAN(span, GL_BITMAP);
@@ -317,7 +329,7 @@ draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
        && ctx->DrawBuffer->Visual.depthBits == 16
        && !scaleOrBias
        && !zoom
-       && width <= MAX_WIDTH
+       && width <= SWRAST_MAX_WIDTH
        && !unpack->SwapBytes) {
       /* Special case: directly write 16-bit depth values */
       GLint row;
@@ -337,7 +349,7 @@ draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
    else if (type == GL_UNSIGNED_INT
             && !scaleOrBias
             && !zoom
-            && width <= MAX_WIDTH
+            && width <= SWRAST_MAX_WIDTH
             && !unpack->SwapBytes) {
       /* Special case: shift 32-bit values down to Visual.depthBits */
       const GLint shift = 32 - ctx->DrawBuffer->Visual.depthBits;
@@ -365,11 +377,11 @@ draw_depth_pixels( struct gl_context *ctx, GLint x, GLint y,
       const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
       GLint skipPixels = 0;
 
-      /* in case width > MAX_WIDTH do the copy in chunks */
+      /* in case width > SWRAST_MAX_WIDTH do the copy in chunks */
       while (skipPixels < width) {
-         const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
+         const GLint spanWidth = MIN2(width - skipPixels, SWRAST_MAX_WIDTH);
          GLint row;
-         ASSERT(span.end <= MAX_WIDTH);
+         assert(span.end <= SWRAST_MAX_WIDTH);
          for (row = 0; row < height; row++) {
             const GLvoid *zSrc = _mesa_image_address2d(unpack,
                                                       pixels, width, height,
@@ -411,8 +423,7 @@ draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
                   const GLvoid *pixels )
 {
    const GLint imgX = x, imgY = y;
-   const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
-   GLfloat *convImage = NULL;
+   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLbitfield transferOps = ctx->_ImageTransferState;
    SWspan span;
 
@@ -427,7 +438,7 @@ draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
    INIT_SPAN(span, GL_BITMAP);
    _swrast_span_default_attribs(ctx, &span);
    span.arrayMask = SPAN_RGBA;
-   span.arrayAttribs = FRAG_BIT_COL0; /* we're fill in COL0 attrib values */
+   span.arrayAttribs = VARYING_BIT_COL0; /* we're fill in COL0 attrib values */
 
    if (ctx->DrawBuffer->_NumColorDrawBuffers > 0) {
       GLenum datatype = _mesa_get_format_datatype(
@@ -445,26 +456,75 @@ draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
    {
       const GLbitfield interpMask = span.interpMask;
       const GLbitfield arrayMask = span.arrayMask;
-      const GLint srcStride
-         = _mesa_image_row_stride(unpack, width, format, type);
       GLint skipPixels = 0;
       /* use span array for temp color storage */
-      GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
+      GLfloat *rgba = (GLfloat *) span.array->attribs[VARYING_SLOT_COL0];
+      void *tempImage = NULL;
+
+      /* We have to deal with GL_COLOR_INDEX manually because
+       * _mesa_format_convert does not handle this format. So what we do here is
+       * convert it to RGBA ubyte first and then convert from that to dst as
+       * usual.
+       */
+      if (format == GL_COLOR_INDEX) {
+         /* This will handle byte swapping and transferops if needed */
+         tempImage =
+            _mesa_unpack_color_index_to_rgba_ubyte(ctx, 2,
+                                                   pixels, format, type,
+                                                   width, height, 1,
+                                                   unpack,
+                                                   transferOps);
+         if (!tempImage) {
+            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
+            return;
+         }
+
+         transferOps = 0;
+         pixels = tempImage;
+         format = GL_RGBA;
+         type = GL_UNSIGNED_BYTE;
+      } else if (unpack->SwapBytes) {
+         /* We have to handle byte-swapping scenarios before calling
+          * _mesa_format_convert
+          */
+         GLint swapSize = _mesa_sizeof_packed_type(type);
+         if (swapSize == 2 || swapSize == 4) {
+            int imageStride = _mesa_image_image_stride(unpack, width, height, format, type);
+
+            tempImage = malloc(imageStride);
+            if (!tempImage) {
+               _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
+               return;
+            }
+
+            _mesa_swap_bytes_2d_image(format, type, unpack,
+                                      width, height, tempImage, pixels);
+
+            pixels = tempImage;
+         }
+      }
 
-      /* if the span is wider than MAX_WIDTH we have to do it in chunks */
+      const GLint srcStride
+         = _mesa_image_row_stride(unpack, width, format, type);
+
+      /* if the span is wider than SWRAST_MAX_WIDTH we have to do it in chunks */
       while (skipPixels < width) {
-         const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
+         const GLint spanWidth = MIN2(width - skipPixels, SWRAST_MAX_WIDTH);
          const GLubyte *source
             = (const GLubyte *) _mesa_image_address2d(unpack, pixels,
                                                       width, height, format,
                                                       type, 0, skipPixels);
          GLint row;
 
+         /* get image row as float/RGBA */
+         uint32_t srcMesaFormat = _mesa_format_from_format_and_type(format, type);
          for (row = 0; row < height; row++) {
-            /* get image row as float/RGBA */
-            _mesa_unpack_color_span_float(ctx, spanWidth, GL_RGBA, rgba,
-                                     format, type, source, unpack,
-                                     transferOps);
+            int dstRowStride = 4 * width * sizeof(float);
+            _mesa_format_convert(rgba, RGBA32_FLOAT, dstRowStride,
+                                 (void*)source, srcMesaFormat, srcStride,
+                                 spanWidth, 1, NULL);
+            if (transferOps)
+               _mesa_apply_rgba_transfer_ops(ctx, transferOps, spanWidth, (GLfloat (*)[4])rgba);
            /* Set these for each row since the _swrast_write_* functions
             * may change them while clipping/rendering.
             */
@@ -489,10 +549,8 @@ draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
 
       /* XXX this is ugly/temporary, to undo above change */
       span.array->ChanType = CHAN_TYPE;
-   }
 
-   if (convImage) {
-      free(convImage);
+      free(tempImage);
    }
 
    swrast_render_finish(ctx);
@@ -500,7 +558,7 @@ draw_rgba_pixels( struct gl_context *ctx, GLint x, GLint y,
 
 
 /**
- * Draw depth+stencil values into a MESA_FORAMT_Z24_S8 or MESA_FORMAT_S8_Z24
+ * Draw depth+stencil values into a MESA_FORAMT_Z24_S8 or MESA_FORMAT_Z24_UNORM_S8_UINT
  * renderbuffer.  No masking, zooming, scaling, etc.
  */
 static void
@@ -513,6 +571,7 @@ fast_draw_depth_stencil(struct gl_context *ctx, GLint x, GLint y,
    const GLenum type = GL_UNSIGNED_INT_24_8;
    struct gl_renderbuffer *rb =
       ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
    GLubyte *src, *dst;
    GLint srcRowStride, dstRowStride;
    GLint i;
@@ -522,7 +581,7 @@ fast_draw_depth_stencil(struct gl_context *ctx, GLint x, GLint y,
    srcRowStride = _mesa_image_row_stride(unpack, width, format, type);
 
    dst = _swrast_pixel_address(rb, x, y);
-   dstRowStride = rb->RowStride * 4;
+   dstRowStride = srb->RowStride;
 
    for (i = 0; i < height; i++) {
       _mesa_pack_uint_24_8_depth_stencil_row(rb->Format, width,
@@ -550,10 +609,10 @@ draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
 {
    const GLint imgX = x, imgY = y;
    const GLboolean scaleOrBias
-      = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
+      = ctx->Pixel.DepthScale != 1.0F || ctx->Pixel.DepthBias != 0.0F;
    const GLuint stencilMask = ctx->Stencil.WriteMask[0];
    const GLenum stencilType = GL_UNSIGNED_BYTE;
-   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
+   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    struct gl_renderbuffer *depthRb, *stencilRb;
    struct gl_pixelstore_attrib clippedUnpack = *unpack;
 
@@ -567,12 +626,12 @@ draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
    
    depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
    stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
-   ASSERT(depthRb);
-   ASSERT(stencilRb);
+   assert(depthRb);
+   assert(stencilRb);
 
    if (depthRb == stencilRb &&
-       (depthRb->Format == MESA_FORMAT_Z24_S8 ||
-        depthRb->Format == MESA_FORMAT_S8_Z24) &&
+       (depthRb->Format == MESA_FORMAT_S8_UINT_Z24_UNORM ||
+        depthRb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT) &&
        type == GL_UNSIGNED_INT_24_8 &&
        !scaleOrBias &&
        !zoom &&
@@ -586,15 +645,21 @@ draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
        * Separate depth/stencil buffers, or pixel transfer ops required.
        */
       /* XXX need to handle very wide images (skippixels) */
+      GLuint *zValues;  /* 32-bit Z values */
       GLint i;
 
+      zValues = malloc(width * sizeof(GLuint));
+      if (!zValues) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels");
+         return;
+      }
+
       for (i = 0; i < height; i++) {
          const GLuint *depthStencilSrc = (const GLuint *)
             _mesa_image_address2d(&clippedUnpack, pixels, width, height,
                                   GL_DEPTH_STENCIL_EXT, type, i, 0);
 
          if (ctx->Depth.Mask) {
-            GLuint zValues[MAX_WIDTH];  /* 32-bit Z values */
             _mesa_unpack_depth_span(ctx, width,
                                     GL_UNSIGNED_INT, /* dest type */
                                     zValues,         /* dest addr */
@@ -613,7 +678,7 @@ draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
          }
 
          if (stencilMask != 0x0) {
-            GLubyte stencilValues[MAX_WIDTH];
+            GLubyte *stencilValues = (GLubyte *) zValues; /* re-use buffer */
             /* get stencil values, with shift/offset/mapping */
             _mesa_unpack_stencil_span(ctx, width, stencilType, stencilValues,
                                       type, depthStencilSrc, &clippedUnpack,
@@ -625,6 +690,8 @@ draw_depth_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
                _swrast_write_stencil_span(ctx, width, x, y + i, stencilValues);
          }
       }
+
+      free(zValues);
    }
 }