added GL_SGIX/SGIS_pixel_texture
[mesa.git] / src / mesa / main / drawpix.c
index 103855f7fa82bcc450d0729372593762070c27ee..23479bf321f2b4d7ecd5a2fc186801212dfe26ac 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: drawpix.c,v 1.6 1999/11/11 01:22:26 brianp Exp $ */
+/* $Id: drawpix.c,v 1.17 2000/04/07 16:27:26 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.3
  * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2000  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 "feedback.h"
 #include "image.h"
 #include "macros.h"
+#include "mem.h"
 #include "mmath.h"
 #include "pixel.h"
+#include "pixeltex.h"
 #include "span.h"
+#include "state.h"
 #include "stencil.h"
+#include "texture.h"
 #include "types.h"
 #include "zoom.h"
 #endif
 
 
 
+/*
+ * Given the dest position, size and skipPixels and skipRows values
+ * for a glDrawPixels command, perform clipping of the image bounds
+ * so the result lies withing the context's buffer bounds.
+ * Return:  GL_TRUE if image is ready for drawing
+ *          GL_FALSE if image was completely clipped away (draw nothing)
+ */
+GLboolean
+_mesa_clip_pixelrect(const GLcontext *ctx,
+                     GLint *destX, GLint *destY,
+                     GLsizei *width, GLsizei *height,
+                     GLint *skipPixels, GLint *skipRows)
+{
+   const GLframebuffer *buffer = ctx->DrawBuffer;
+
+   /* left clipping */
+   if (*destX < buffer->Xmin) {
+      *skipPixels += (buffer->Xmin - *destX);
+      *width -= (buffer->Xmin - *destX);
+      *destX = buffer->Xmin;
+   }
+   /* right clipping */
+   if (*destX + *width > buffer->Xmax)
+      *width -= (*destX + *width - buffer->Xmax - 1);
+
+   if (*width <= 0)
+      return GL_FALSE;
+
+   /* bottom clipping */
+   if (*destY < buffer->Ymin) {
+      *skipRows += (buffer->Ymin - *destY);
+      *height -= (buffer->Ymin - *destY);
+      *destY = buffer->Ymin;
+   }
+   /* top clipping */
+   if (*destY + *height > buffer->Ymax)
+      *height -= (*destY + *height - buffer->Ymax - 1);
+
+   if (*height <= 0)
+      return GL_TRUE;
+
+   return GL_TRUE;
+}
+
+
+
 /*
  * Try to do a fast and simple RGB(a) glDrawPixels.
  * Return:  GL_TRUE if success, GL_FALSE if slow path must be used instead
@@ -70,13 +120,6 @@ simple_DrawPixels( GLcontext *ctx, GLint x, GLint y,
       gl_update_state(ctx);
    }
 
-   /* see if device driver can do the drawpix */
-   if (ctx->Driver.DrawPixels
-       && (*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type,
-                                    unpack, pixels)) {
-      return GL_TRUE;
-   }
-
    if ((ctx->RasterMask&(~(SCISSOR_BIT|WINCLIP_BIT)))==0
        && ctx->Pixel.RedBias==0.0 && ctx->Pixel.RedScale==1.0
        && ctx->Pixel.GreenBias==0.0 && ctx->Pixel.GreenScale==1.0
@@ -84,6 +127,7 @@ simple_DrawPixels( GLcontext *ctx, GLint x, GLint y,
        && ctx->Pixel.AlphaBias==0.0 && ctx->Pixel.AlphaScale==1.0
        && ctx->Pixel.IndexShift==0 && ctx->Pixel.IndexOffset==0
        && ctx->Pixel.MapColorFlag==0
+       && ctx->Texture.ReallyEnabled == 0
        && unpack->Alignment==1
        && !unpack->SwapBytes
        && !unpack->LsbFirst) {
@@ -109,30 +153,32 @@ simple_DrawPixels( GLcontext *ctx, GLint x, GLint y,
        */
       if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
          /* horizontal clipping */
-         if (destX < ctx->Buffer->Xmin) {
-            skipPixels += (ctx->Buffer->Xmin - destX);
-            drawWidth  -= (ctx->Buffer->Xmin - destX);
-            destX = ctx->Buffer->Xmin;
+         if (destX < ctx->DrawBuffer->Xmin) {
+            skipPixels += (ctx->DrawBuffer->Xmin - destX);
+            drawWidth  -= (ctx->DrawBuffer->Xmin - destX);
+            destX = ctx->DrawBuffer->Xmin;
          }
-         if (destX + drawWidth > ctx->Buffer->Xmax)
-            drawWidth -= (destX + drawWidth - ctx->Buffer->Xmax - 1);
+         if (destX + drawWidth > ctx->DrawBuffer->Xmax)
+            drawWidth -= (destX + drawWidth - ctx->DrawBuffer->Xmax - 1);
          if (drawWidth <= 0)
             return GL_TRUE;
 
          /* vertical clipping */
-         if (destY < ctx->Buffer->Ymin) {
-            skipRows   += (ctx->Buffer->Ymin - destY);
-            drawHeight -= (ctx->Buffer->Ymin - destY);
-            destY = ctx->Buffer->Ymin;
+         if (destY < ctx->DrawBuffer->Ymin) {
+            skipRows   += (ctx->DrawBuffer->Ymin - destY);
+            drawHeight -= (ctx->DrawBuffer->Ymin - destY);
+            destY = ctx->DrawBuffer->Ymin;
          }
-         if (destY + drawHeight > ctx->Buffer->Ymax)
-            drawHeight -= (destY + drawHeight - ctx->Buffer->Ymax - 1);
+         if (destY + drawHeight > ctx->DrawBuffer->Ymax)
+            drawHeight -= (destY + drawHeight - ctx->DrawBuffer->Ymax - 1);
          if (drawHeight <= 0)
             return GL_TRUE;
+
+         zoomY0 = 0;  /* not used - silence compiler warning */
       }
       else {
          /* setup array of fragment Z value to pass to zoom function */
-         GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
+         GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF);
          GLint i;
          assert(drawWidth < MAX_WIDTH);
          for (i=0; i<drawWidth; i++)
@@ -369,8 +415,8 @@ draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
    drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
 
    /* Fragment depth values */
-   if (ctx->Depth.Test) {
-      GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
+   if (ctx->Depth.Test || ctx->Fog.Enabled) {
+      GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF);
       GLint i;
       for (i = 0; i < drawWidth; i++) {
         zspan[i] = zval;
@@ -382,7 +428,7 @@ draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
     */
    for (row = 0; row < height; row++, y++) {
       GLuint indexes[MAX_WIDTH];
-      const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack,
+      const GLvoid *source = _mesa_image_address(&ctx->Unpack,
                     pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
       _mesa_unpack_index_span(ctx, drawWidth, GL_UNSIGNED_INT, indexes,
                               type, source, &ctx->Unpack, GL_TRUE);
@@ -428,7 +474,7 @@ draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
       GLstencil values[MAX_WIDTH];
       GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
                       ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
-      const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack,
+      const GLvoid *source = _mesa_image_address(&ctx->Unpack,
                     pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
       _mesa_unpack_index_span(ctx, drawWidth, destType, values,
                               type, source, &ctx->Unpack, GL_TRUE);
@@ -497,30 +543,23 @@ draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
       /* Special case: directly write 16-bit depth values */
       GLint row;
       for (row = 0; row < height; row++, y++) {
-         const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack,
+         GLdepth zspan[MAX_WIDTH];
+         const GLushort *zptr = _mesa_image_address(&ctx->Unpack,
                 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
-         gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP );
+         GLint i;
+         for (i = 0; i < width; i++)
+            zspan[i] = zptr[i];
+         gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
       }
    }
-   else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint)
+   else if (type==GL_UNSIGNED_INT && ctx->Visual->DepthBits == 32
        && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
       /* Special case: directly write 32-bit depth values */
-      GLint i, row;
-      /* Compute shift value to scale 32-bit uints down to depth values. */
-      GLuint shift = 0;
-      GLuint max = MAX_DEPTH;
-      while ((max & 0x80000000) == 0) {
-         max = max << 1;
-         shift++;
-      }
+      GLint row;
       for (row = 0; row < height; row++, y++) {
-         GLdepth zspan[MAX_WIDTH];
-         const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack,
+         const GLuint *zptr = _mesa_image_address(&ctx->Unpack,
                 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
-         for (i=0;i<width;i++) {
-            zspan[i] = zptr[i] >> shift;
-         }
-         gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
+         gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP );
       }
    }
    else {
@@ -528,7 +567,7 @@ draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
       GLint row;
       for (row = 0; row < height; row++, y++) {
          GLdepth zspan[MAX_WIDTH];
-         const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack,
+         const GLvoid *src = _mesa_image_address(&ctx->Unpack,
                 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
          _mesa_unpack_depth_span( ctx, drawWidth, zspan, type, src,
                                   &ctx->Unpack, GL_TRUE );
@@ -575,9 +614,9 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
       return;
 
    /* Fragment depth values */
-   if (ctx->Depth.Test) {
+   if (ctx->Depth.Test || ctx->Fog.Enabled) {
       /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
+      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF);
       GLint i;
       for (i=0;i<width;i++) {
         zspan[i] = z;
@@ -587,8 +626,8 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
 
    if (ctx->RasterMask == 0 && !zoom
        && x >= 0 && y >= 0
-       && x + width <= ctx->Buffer->Width
-       && y + height <= ctx->Buffer->Height) {
+       && x + width <= ctx->DrawBuffer->Width
+       && y + height <= ctx->DrawBuffer->Height) {
       quickDraw = GL_TRUE;
    }
    else {
@@ -604,11 +643,22 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
       if (width > MAX_WIDTH)
          width = MAX_WIDTH;
       for (row = 0; row < height; row++, y++) {
-         const GLvoid *source = gl_pixel_addr_in_image(unpack,
+         const GLvoid *source = _mesa_image_address(unpack,
                   pixels, width, height, format, type, 0, row, 0);
          _mesa_unpack_ubyte_color_span(ctx, width, GL_RGBA, (void*) rgba,
                    format, type, source, unpack, GL_TRUE);
 
+         if (ctx->Texture.ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
+            GLfloat s[MAX_WIDTH], t[MAX_WIDTH], r[MAX_WIDTH], q[MAX_WIDTH];
+            GLuint unit;
+            /* XXX not sure how multitexture is supposed to work here */
+            for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++) {
+               _mesa_pixeltexgen(ctx, width, (const GLubyte (*)[4]) rgba,
+                                 s, t, r, q);
+               gl_texture_pixels(ctx, unit, width, s, t, r, NULL, rgba);
+            }
+         }
+
          if (quickDraw) {
             (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y,
                                           (CONST GLubyte (*)[]) rgba, NULL);
@@ -645,6 +695,15 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
       x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
       y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
 
+      ctx->OcclusionResult = GL_TRUE;
+
+      /* see if device driver can do the drawpix */
+      if (ctx->Driver.DrawPixels
+          && (*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type,
+                                       &ctx->Unpack, pixels)) {
+         return;
+      }
+
       switch (format) {
         case GL_STENCIL_INDEX:
            draw_stencil_pixels( ctx, x, y, width, height, type, pixels );
@@ -654,9 +713,9 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
            break;
         case GL_COLOR_INDEX:
             if (ctx->Visual->RGBAflag)
-               draw_index_pixels(ctx, x, y, width, height, type, pixels);
-            else
                draw_rgba_pixels(ctx, x,y, width, height, format, type, pixels);
+            else
+               draw_index_pixels(ctx, x, y, width, height, type, pixels);
            break;
         case GL_RED:
         case GL_GREEN: