Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / mesa / swrast / s_drawpix.c
index 50147f329f6c47543bb81a2fc1655f108f5d808a..700f76d4bc461152046a37024af0dfdc624f3251 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5.2
+ * Version:  7.1
  *
- * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  */
 
 
-#include "glheader.h"
-#include "bufferobj.h"
-#include "context.h"
-#include "convolve.h"
-#include "image.h"
-#include "macros.h"
-#include "imports.h"
-#include "pixel.h"
-#include "state.h"
+#include "main/glheader.h"
+#include "main/bufferobj.h"
+#include "main/context.h"
+#include "main/convolve.h"
+#include "main/image.h"
+#include "main/macros.h"
+#include "main/imports.h"
+#include "main/pixel.h"
+#include "main/state.h"
 
 #include "s_context.h"
-#include "s_drawpix.h"
 #include "s_span.h"
 #include "s_stencil.h"
 #include "s_zoom.h"
@@ -53,8 +52,8 @@ fast_draw_rgba_pixels(GLcontext *ctx, GLint x, GLint y,
                       const GLvoid *pixels)
 {
    const GLint imgX = x, imgY = y;
-   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
-   const GLenum rbType = rb->DataType;
+   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
+   GLenum rbType;
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    SWspan span;
    GLboolean simpleZoom;
@@ -62,6 +61,11 @@ fast_draw_rgba_pixels(GLcontext *ctx, GLint x, GLint y,
    struct gl_pixelstore_attrib unpack;
    GLint destX, destY, drawWidth, drawHeight; /* post clipping */
 
+   if (!rb)
+      return GL_TRUE; /* no-op */
+
+   rbType = rb->DataType;
+
    if ((swrast->_RasterMask & ~CLIP_BIT) ||
        ctx->Texture._EnabledCoordUnits ||
        userUnpack->SwapBytes ||
@@ -70,13 +74,10 @@ fast_draw_rgba_pixels(GLcontext *ctx, GLint x, GLint y,
       return GL_FALSE;
    }
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
-   if (ctx->Depth.Test)
-      _swrast_span_default_z(ctx, &span);
-   if (swrast->_FogEnabled)
-      _swrast_span_default_fog(ctx, &span);
-   if (ctx->Texture._EnabledCoordUnits)
-      _swrast_span_default_texcoords(ctx, &span);
+   INIT_SPAN(span, GL_BITMAP);
+   span.arrayMask = SPAN_RGBA;
+   span.arrayAttribs = FRAG_BIT_COL0;
+   _swrast_span_default_attribs(ctx, &span);
 
    /* copy input params since clipping may change them */
    unpack = *userUnpack;
@@ -273,9 +274,9 @@ fast_draw_rgba_pixels(GLcontext *ctx, GLint x, GLint y,
             for (row = 0; row < drawHeight; row++) {
                ASSERT(drawWidth <= MAX_WIDTH);
                _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
-                                      span.array->color.sz1.rgba);
+                                      span.array->rgba8);
                rb->PutRow(ctx, rb, drawWidth, destX, destY,
-                          span.array->color.sz1.rgba, NULL);
+                          span.array->rgba8, NULL);
                src += unpack.RowLength;
                destY += yStep;
             }
@@ -286,12 +287,12 @@ fast_draw_rgba_pixels(GLcontext *ctx, GLint x, GLint y,
             for (row = 0; row < drawHeight; row++) {
                ASSERT(drawWidth <= MAX_WIDTH);
                _mesa_map_ci8_to_rgba8(ctx, drawWidth, src,
-                                      span.array->color.sz1.rgba);
+                                      span.array->rgba8);
                span.x = destX;
                span.y = destY;
                span.end = drawWidth;
                _swrast_write_zoomed_rgba_span(ctx, imgX, imgY, &span,
-                                              span.array->color.sz1.rgba);
+                                              span.array->rgba8);
                src += unpack.RowLength;
                destY++;
             }
@@ -332,18 +333,14 @@ draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
                    const struct gl_pixelstore_attrib *unpack,
                    const GLvoid *pixels )
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLint imgX = x, imgY = y;
    const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
    GLint row, skipPixels;
    SWspan span;
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
-
-   if (ctx->Depth.Test)
-      _swrast_span_default_z(ctx, &span);
-   if (swrast->_FogEnabled)
-      _swrast_span_default_fog(ctx, &span);
+   INIT_SPAN(span, GL_BITMAP);
+   span.arrayMask = SPAN_INDEX;
+   _swrast_span_default_attribs(ctx, &span);
 
    /*
     * General solution
@@ -405,10 +402,9 @@ draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
                                                       width, height,
                                                       GL_COLOR_INDEX, type,
                                                       row, skipPixels);
-         _mesa_unpack_index_span(ctx, spanWidth, destType, values,
-                                 type, source, unpack,
-                                 ctx->_ImageTransferState);
-         _mesa_apply_stencil_transfer_ops(ctx, spanWidth, values);
+         _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);
@@ -432,20 +428,14 @@ draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
                    const struct gl_pixelstore_attrib *unpack,
                    const GLvoid *pixels )
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    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;
    SWspan span;
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
-
-   _swrast_span_default_color(ctx, &span);
-
-   if (swrast->_FogEnabled)
-      _swrast_span_default_fog(ctx, &span);
-   if (ctx->Texture._EnabledCoordUnits)
-      _swrast_span_default_texcoords(ctx, &span);
+   INIT_SPAN(span, GL_BITMAP);
+   span.arrayMask = SPAN_Z;
+   _swrast_span_default_attribs(ctx, &span);
 
    if (type == GL_UNSIGNED_SHORT
        && ctx->DrawBuffer->Visual.depthBits == 16
@@ -498,7 +488,7 @@ draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
    }
    else {
       /* General case */
-      const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
+      const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
       GLint skipPixels = 0;
 
       /* in case width > MAX_WIDTH do the copy in chunks */
@@ -549,7 +539,6 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
                   const struct gl_pixelstore_attrib *unpack,
                   const GLvoid *pixels )
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLint imgX = x, imgY = y;
    const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
    GLfloat *convImage = NULL;
@@ -558,16 +547,14 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
 
    /* Try an optimized glDrawPixels first */
    if (fast_draw_rgba_pixels(ctx, x, y, width, height, format, type,
-                             unpack, pixels))
+                             unpack, pixels)) {
       return;
+   }
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
-   if (ctx->Depth.Test)
-      _swrast_span_default_z(ctx, &span);
-   if (swrast->_FogEnabled)
-      _swrast_span_default_fog(ctx, &span);
-   if (ctx->Texture._EnabledCoordUnits)
-      _swrast_span_default_texcoords(ctx, &span);
+   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 */
 
    if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
       /* Convolution has to be handled specially.  We'll create an
@@ -625,8 +612,8 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
                        IMAGE_POST_CONVOLUTION_SCALE_BIAS);
    }
 
-   if (ctx->DrawBuffer->_NumColorDrawBuffers[0] > 0 &&
-       ctx->DrawBuffer->_ColorDrawBuffers[0][0]->DataType != GL_FLOAT &&
+   if (ctx->DrawBuffer->_NumColorDrawBuffers > 0 &&
+       ctx->DrawBuffer->_ColorDrawBuffers[0]->DataType != GL_FLOAT &&
        ctx->Color.ClampFragmentColor != GL_FALSE) {
       /* need to clamp colors before applying fragment ops */
       transferOps |= IMAGE_CLAMP_BIT;
@@ -712,7 +699,7 @@ draw_depth_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
    const GLint imgX = x, imgY = y;
    const GLboolean scaleOrBias
       = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
-   const GLfloat depthScale = ctx->DrawBuffer->_DepthMaxF;
+   const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
    const GLuint stencilMask = ctx->Stencil.WriteMask[0];
    const GLuint stencilType = (STENCIL_BITS == 8) ? 
       GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
@@ -800,7 +787,7 @@ draw_depth_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
                /* general case */
                GLuint zValues[MAX_WIDTH];  /* 16 or 32-bit Z value storage */
                _mesa_unpack_depth_span(ctx, width,
-                                       depthRb->DataType, zValues, depthScale,
+                                       depthRb->DataType, zValues, depthMax,
                                        type, depthStencilSrc, &clippedUnpack);
                if (zoom) {
                   _swrast_write_zoomed_z_span(ctx, imgX, imgY, width, x,
@@ -829,7 +816,6 @@ draw_depth_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
 }
 
 
-
 /**
  * Execute software-based glDrawPixels.
  * By time we get here, all error checking will have been done.
@@ -844,7 +830,7 @@ _swrast_DrawPixels( GLcontext *ctx,
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
 
-   RENDER_START(swrast,ctx);
+   swrast_render_start(ctx);
 
    if (ctx->NewState)
       _mesa_update_state(ctx);
@@ -852,25 +838,11 @@ _swrast_DrawPixels( GLcontext *ctx,
    if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
-   if (unpack->BufferObj->Name) {
-      /* unpack from PBO */
-      GLubyte *buf;
-      if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
-                                     format, type, pixels)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glDrawPixels(invalid PBO access)");
-         goto end;
-      }
-      buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                                              GL_READ_ONLY_ARB,
-                                              unpack->BufferObj);
-      if (!buf) {
-         /* buffer is already mapped - that's an error */
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(PBO is mapped)");
-         goto end;
-      }
-      pixels = ADD_POINTERS(buf, pixels);
-   }
+    pixels = _mesa_map_drawpix_pbo(ctx, unpack, pixels);
+    if (!pixels) {
+       swrast_render_finish(ctx);
+       return;
+    }
 
    switch (format) {
    case GL_STENCIL_INDEX:
@@ -907,15 +879,9 @@ _swrast_DrawPixels( GLcontext *ctx,
       /* don't return yet, clean-up */
    }
 
-end:
+   swrast_render_finish(ctx);
 
-   RENDER_FINISH(swrast,ctx);
-
-   if (unpack->BufferObj->Name) {
-      /* done with PBO so unmap it now */
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
-                              unpack->BufferObj);
-   }
+   _mesa_unmap_drawpix_pbo(ctx, unpack);
 }
 
 
@@ -938,7 +904,7 @@ _swrast_DrawDepthPixelsMESA( GLcontext *ctx,
    if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
-   RENDER_START(swrast,ctx);
+   swrast_render_start(ctx);
 
    switch (colorFormat) {
    case GL_COLOR_INDEX:
@@ -967,6 +933,6 @@ _swrast_DrawDepthPixelsMESA( GLcontext *ctx,
       _mesa_problem(ctx, "unexpected format in glDrawDepthPixelsMESA");
    }
 
-   RENDER_FINISH(swrast,ctx);
+   swrast_render_finish(ctx);
 }
 #endif