ARB prog lexer: Fix lexer to eat both DOS and Unix line endings
[mesa.git] / src / mesa / main / drawpix.c
index 016ddd0a81a1b2f0da4fd054333711443e336bb2..67311f71a2671abda40bd67aea5021aaa4c21c91 100644 (file)
 #include "bufferobj.h"
 #include "context.h"
 #include "drawpix.h"
+#include "enums.h"
 #include "feedback.h"
 #include "framebuffer.h"
 #include "image.h"
+#include "readpix.h"
 #include "state.h"
 
 
+
 /**
- * Do error checking of the format/type parameters to glReadPixels and
- * glDrawPixels.
- * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
- *                for ReadPixels.
- * \return GL_TRUE if error detected, GL_FALSE if no errors
+ * If a fragment program is enabled, check that it's valid.
+ * \return GL_TRUE if valid, GL_FALSE otherwise
  */
 static GLboolean
-error_check_format_type(GLcontext *ctx, GLenum format, GLenum type,
-                        GLboolean drawing)
+valid_fragment_program(GLcontext *ctx)
 {
-   const char *readDraw = drawing ? "Draw" : "Read";
-
-   if (ctx->Extensions.EXT_packed_depth_stencil
-       && type == GL_UNSIGNED_INT_24_8_EXT
-       && format != GL_DEPTH_STENCIL_EXT) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
-      return GL_TRUE;
-   }
-
-   /* basic combinations test */
-   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
-      _mesa_error(ctx, GL_INVALID_ENUM,
-                  "gl%sPixels(format or type)", readDraw);
-      return GL_TRUE;
-   }
-
-   /* additional checks */
-   switch (format) {
-   case GL_RED:
-   case GL_GREEN:
-   case GL_BLUE:
-   case GL_ALPHA:
-   case GL_LUMINANCE:
-   case GL_LUMINANCE_ALPHA:
-   case GL_RGB:
-   case GL_BGR:
-   case GL_RGBA:
-   case GL_BGRA:
-   case GL_ABGR_EXT:
-      if (drawing && !ctx->Visual.rgbMode) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                   "glDrawPixels(drawing RGB pixels into color index buffer)");
-         return GL_TRUE;
-      }
-      if (!drawing && !_mesa_dest_buffer_exists(ctx, GL_COLOR)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glReadPixels(no color buffer)");
-         return GL_TRUE;
-      }
-      break;
-   case GL_COLOR_INDEX:
-      if (!drawing && ctx->Visual.rgbMode) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                    "glReadPixels(reading color index format from RGB buffer)");
-         return GL_TRUE;
-      }
-      if (!drawing && !_mesa_dest_buffer_exists(ctx, GL_COLOR)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glReadPixels(no color buffer)");
-         return GL_TRUE;
-      }
-      break;
-   case GL_STENCIL_INDEX:
-      if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
-          (!drawing && !_mesa_source_buffer_exists(ctx, format))) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "gl%sPixels(no stencil buffer)", readDraw);
-         return GL_TRUE;
-      }
-      break;
-   case GL_DEPTH_COMPONENT:
-      if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
-          (!drawing && !_mesa_source_buffer_exists(ctx, format))) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "gl%sPixels(no depth buffer)", readDraw);
-         return GL_TRUE;
-      }
-      break;
-   case GL_DEPTH_STENCIL_EXT:
-      if (!ctx->Extensions.EXT_packed_depth_stencil ||
-          type != GL_UNSIGNED_INT_24_8_EXT) {
-         _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
-         return GL_TRUE;
-      }
-      if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
-          (!drawing && !_mesa_source_buffer_exists(ctx, format))) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "gl%sPixels(no depth or stencil buffer)", readDraw);
-         return GL_TRUE;
-      }
-      break;
-   default:
-      /* this should have been caught in _mesa_is_legal_format_type() */
-      _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
-      return GL_TRUE;
-   }
-
-   /* no errors */
-   return GL_FALSE;
+   return !(ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled);
 }
-      
 
 
 #if _HAVE_FULL_GL
@@ -155,59 +64,66 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
       return;
    }
 
+   /* We're not using the current vertex program, and the driver may install
+    * it's own.
+    */
+   _mesa_set_vp_override(ctx, GL_TRUE);
+
    if (ctx->NewState) {
       _mesa_update_state(ctx);
    }
 
-   if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
+   if (!valid_fragment_program(ctx)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glDrawPixels (invalid fragment program)");
-      return;
+      goto end;
    }
 
-   if (error_check_format_type(ctx, format, type, GL_TRUE)) {
-      /* found an error */
-      return;
+   if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
+      /* the error was already recorded */
+      goto end;
    }
 
    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
                   "glDrawPixels(incomplete framebuffer)" );
-      return;
+      goto end;
    }
 
    if (!ctx->Current.RasterPosValid) {
-      return;
+      goto end; /* no-op, not an error */
    }
 
    if (ctx->RenderMode == GL_RENDER) {
-      /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
-      GLint x = IROUND(ctx->Current.RasterPos[0]);
-      GLint y = IROUND(ctx->Current.RasterPos[1]);
-
-      if (ctx->Unpack.BufferObj->Name) {
-         /* unpack from PBO */
-         if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
-                                        format, type, pixels)) {
-            _mesa_error(ctx, GL_INVALID_OPERATION,
-                        "glDrawPixels(invalid PBO access)");
-            return;
-         }
-         if (ctx->Unpack.BufferObj->Pointer) {
-            /* buffer is mapped - that's an error */
-            _mesa_error(ctx, GL_INVALID_OPERATION,
-                        "glDrawPixels(PBO is mapped)");
-            return;
+      if (width > 0 && height > 0) {
+         /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
+         GLint x = IROUND(ctx->Current.RasterPos[0]);
+         GLint y = IROUND(ctx->Current.RasterPos[1]);
+
+         if (ctx->Unpack.BufferObj->Name) {
+            /* unpack from PBO */
+            if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
+                                           format, type, pixels)) {
+               _mesa_error(ctx, GL_INVALID_OPERATION,
+                           "glDrawPixels(invalid PBO access)");
+               goto end;
+            }
+            if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
+               /* buffer is mapped - that's an error */
+               _mesa_error(ctx, GL_INVALID_OPERATION,
+                           "glDrawPixels(PBO is mapped)");
+               goto end;
+            }
          }
-      }
 
-      ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
-                            &ctx->Unpack, pixels);
+         ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
+                                &ctx->Unpack, pixels);
+      }
    }
    else if (ctx->RenderMode == GL_FEEDBACK) {
       /* Feedback the current raster pos info */
       FLUSH_CURRENT( ctx, 0 );
-      FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
+      _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
       _mesa_feedback_vertex( ctx,
                              ctx->Current.RasterPos,
                              ctx->Current.RasterColor,
@@ -218,6 +134,9 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
       ASSERT(ctx->RenderMode == GL_SELECT);
       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
    }
+
+end:
+   _mesa_set_vp_override(ctx, GL_FALSE);
 }
 
 
@@ -228,49 +147,69 @@ _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
+   if (width < 0 || height < 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
+      return;
+   }
+
+   /* Note: more detailed 'type' checking is done by the
+    * _mesa_source/dest_buffer_exists() calls below.  That's where we
+    * check if the stencil buffer exists, etc.
+    */
+   if (type != GL_COLOR &&
+       type != GL_DEPTH &&
+       type != GL_STENCIL &&
+       type != GL_DEPTH_STENCIL) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
+                  _mesa_lookup_enum_by_nr(type));
+      return;
+   }
+
+   /* We're not using the current vertex program, and the driver may install
+    * it's own.
+    */
+   _mesa_set_vp_override(ctx, GL_TRUE);
+
    if (ctx->NewState) {
       _mesa_update_state(ctx);
    }
 
-   if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
+   if (!valid_fragment_program(ctx)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glCopyPixels (invalid fragment program)");
-      return;
-   }
-
-   if (width < 0 || height < 0) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
-      return;
+      goto end;
    }
 
    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
        ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
                   "glCopyPixels(incomplete framebuffer)" );
-      return;
+      goto end;
    }
 
    if (!_mesa_source_buffer_exists(ctx, type) ||
        !_mesa_dest_buffer_exists(ctx, type)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glCopyPixels(missing source or dest buffer)");
-      return;
+      goto end;
    }
 
    if (!ctx->Current.RasterPosValid || width ==0 || height == 0) {
-      return;
+      goto end; /* no-op, not an error */
    }
 
    if (ctx->RenderMode == GL_RENDER) {
       /* Round to satisfy conformance tests (matches SGI's OpenGL) */
-      GLint destx = IROUND(ctx->Current.RasterPos[0]);
-      GLint desty = IROUND(ctx->Current.RasterPos[1]);
-      ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
-                             type );
+      if (width > 0 && height > 0) {
+         GLint destx = IROUND(ctx->Current.RasterPos[0]);
+         GLint desty = IROUND(ctx->Current.RasterPos[1]);
+         ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
+                                 type );
+      }
    }
    else if (ctx->RenderMode == GL_FEEDBACK) {
       FLUSH_CURRENT( ctx, 0 );
-      FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
+      _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
       _mesa_feedback_vertex( ctx, 
                              ctx->Current.RasterPos,
                              ctx->Current.RasterColor,
@@ -281,67 +220,15 @@ _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
       ASSERT(ctx->RenderMode == GL_SELECT);
       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
    }
+
+end:
+   _mesa_set_vp_override(ctx, GL_FALSE);
 }
 
 #endif /* _HAVE_FULL_GL */
 
 
 
-void GLAPIENTRY
-_mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
-                 GLenum format, GLenum type, GLvoid *pixels )
-{
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
-
-   FLUSH_CURRENT(ctx, 0);
-
-   if (width < 0 || height < 0) {
-      _mesa_error( ctx, GL_INVALID_VALUE,
-                   "glReadPixels(width=%d height=%d)", width, height );
-      return;
-   }
-
-   if (ctx->NewState)
-      _mesa_update_state(ctx);
-
-   if (error_check_format_type(ctx, format, type, GL_FALSE)) {
-      /* found an error */
-      return;
-   }
-
-   if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
-      _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
-                  "glReadPixels(incomplete framebuffer)" );
-      return;
-   }
-
-   if (!_mesa_source_buffer_exists(ctx, format)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
-      return;
-   }
-
-   if (ctx->Pack.BufferObj->Name) {
-      if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
-                                     format, type, pixels)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glReadPixels(invalid PBO access)");
-         return;
-      }
-
-      if (ctx->Pack.BufferObj->Pointer) {
-         /* buffer is mapped - that's an error */
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
-         return;
-      }
-   }
-
-   ctx->Driver.ReadPixels(ctx, x, y, width, height,
-                         format, type, &ctx->Pack, pixels);
-}
-
-
-
 void GLAPIENTRY
 _mesa_Bitmap( GLsizei width, GLsizei height,
               GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
@@ -363,7 +250,7 @@ _mesa_Bitmap( GLsizei width, GLsizei height,
       _mesa_update_state(ctx);
    }
 
-   if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
+   if (!valid_fragment_program(ctx)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glBitmap (invalid fragment program)");
       return;
@@ -377,7 +264,7 @@ _mesa_Bitmap( GLsizei width, GLsizei height,
 
    if (ctx->RenderMode == GL_RENDER) {
       /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
-      const GLfloat epsilon = (const GLfloat)0.0001;
+      const GLfloat epsilon = 0.0001F;
       GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
       GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
 
@@ -390,7 +277,7 @@ _mesa_Bitmap( GLsizei width, GLsizei height,
                         "glBitmap(invalid PBO access)");
             return;
          }
-         if (ctx->Unpack.BufferObj->Pointer) {
+         if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
             /* buffer is mapped - that's an error */
             _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
             return;
@@ -402,7 +289,7 @@ _mesa_Bitmap( GLsizei width, GLsizei height,
 #if _HAVE_FULL_GL
    else if (ctx->RenderMode == GL_FEEDBACK) {
       FLUSH_CURRENT(ctx, 0);
-      FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
+      _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
       _mesa_feedback_vertex( ctx,
                              ctx->Current.RasterPos,
                              ctx->Current.RasterColor,
@@ -419,68 +306,3 @@ _mesa_Bitmap( GLsizei width, GLsizei height,
    ctx->Current.RasterPos[0] += xmove;
    ctx->Current.RasterPos[1] += ymove;
 }
-
-
-
-#if 0  /* experimental */
-/*
- * Execute glDrawDepthPixelsMESA().  This function accepts both a color
- * image and depth (Z) image.  Rasterization produces fragments with
- * color and Z taken from these images.  This function is intended for
- * Z-compositing.  Normally, this operation requires two glDrawPixels
- * calls with stencil testing.
- */
-void GLAPIENTRY
-_mesa_DrawDepthPixelsMESA( GLsizei width, GLsizei height,
-                           GLenum colorFormat, GLenum colorType,
-                           const GLvoid *colors,
-                           GLenum depthType, const GLvoid *depths )
-{
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
-
-   if (width < 0 || height < 0) {
-      _mesa_error( ctx, GL_INVALID_VALUE,
-                   "glDrawDepthPixelsMESA(width or height < 0" );
-      return;
-   }
-
-   if (!ctx->Current.RasterPosValid) {
-      return;
-   }
-
-   if (ctx->NewState) {
-      _mesa_update_state(ctx);
-   }
-
-   if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
-      _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
-                  "glDrawDepthPixelsMESA(incomplete framebuffer)");
-      return;
-   }
-
-   if (ctx->RenderMode == GL_RENDER) {
-      /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
-      GLint x = IROUND(ctx->Current.RasterPos[0]);
-      GLint y = IROUND(ctx->Current.RasterPos[1]);
-      ctx->Driver.DrawDepthPixelsMESA(ctx, x, y, width, height,
-                                      colorFormat, colorType, colors,
-                                      depthType, depths, &ctx->Unpack);
-   }
-   else if (ctx->RenderMode == GL_FEEDBACK) {
-      /* Feedback the current raster pos info */
-      FLUSH_CURRENT( ctx, 0 );
-      FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
-      _mesa_feedback_vertex( ctx,
-                             ctx->Current.RasterPos,
-                             ctx->Current.RasterColor,
-                             ctx->Current.RasterIndex,
-                             ctx->Current.RasterTexCoords[0] );
-   }
-   else {
-      ASSERT(ctx->RenderMode == GL_SELECT);
-      /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
-   }
-}
-
-#endif