replace IROUND with util functions
[mesa.git] / src / mesa / main / pixelstore.c
index 05f6583a481da03978063366ea29bb98a2e079ee..f190c0b2bc1eaf44e95d6fd91215a49b8ebe44ba 100644 (file)
 #include "context.h"
 #include "pixelstore.h"
 #include "mtypes.h"
+#include "util/rounding.h"
 
 
-void GLAPIENTRY
-_mesa_PixelStorei( GLenum pname, GLint param )
+static ALWAYS_INLINE void
+pixel_storei(GLenum pname, GLint param, bool no_error)
 {
    /* NOTE: this call can't be compiled into the display list */
    GET_CURRENT_CONTEXT(ctx);
 
    switch (pname) {
       case GL_PACK_SWAP_BYTES:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
          ctx->Pack.SwapBytes = param ? GL_TRUE : GL_FALSE;
          break;
       case GL_PACK_LSB_FIRST:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
          ctx->Pack.LsbFirst = param ? GL_TRUE : GL_FALSE;
          break;
       case GL_PACK_ROW_LENGTH:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.RowLength = param;
          break;
       case GL_PACK_IMAGE_HEIGHT:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.ImageHeight = param;
          break;
       case GL_PACK_SKIP_PIXELS:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.SkipPixels = param;
          break;
       case GL_PACK_SKIP_ROWS:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.SkipRows = param;
          break;
       case GL_PACK_SKIP_IMAGES:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.SkipImages = param;
          break;
       case GL_PACK_ALIGNMENT:
-         if (param!=1 && param!=2 && param!=4 && param!=8)
+         if (!no_error && param!=1 && param!=2 && param!=4 && param!=8)
             goto invalid_value_error;
          ctx->Pack.Alignment = param;
          break;
       case GL_PACK_INVERT_MESA:
-         if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.MESA_pack_invert)
+         if (!no_error &&
+             (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.MESA_pack_invert))
             goto invalid_enum_error;
          ctx->Pack.Invert = param;
          break;
       case GL_PACK_COMPRESSED_BLOCK_WIDTH:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.CompressedBlockWidth = param;
          break;
       case GL_PACK_COMPRESSED_BLOCK_HEIGHT:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.CompressedBlockHeight = param;
          break;
       case GL_PACK_COMPRESSED_BLOCK_DEPTH:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.CompressedBlockDepth = param;
          break;
       case GL_PACK_COMPRESSED_BLOCK_SIZE:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Pack.CompressedBlockSize = param;
          break;
 
       case GL_UNPACK_SWAP_BYTES:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
          ctx->Unpack.SwapBytes = param ? GL_TRUE : GL_FALSE;
          break;
       case GL_UNPACK_LSB_FIRST:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
          ctx->Unpack.LsbFirst = param ? GL_TRUE : GL_FALSE;
          break;
       case GL_UNPACK_ROW_LENGTH:
-         if (ctx->API == API_OPENGLES)
+         if (!no_error && ctx->API == API_OPENGLES)
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.RowLength = param;
          break;
       case GL_UNPACK_IMAGE_HEIGHT:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.ImageHeight = param;
          break;
       case GL_UNPACK_SKIP_PIXELS:
-         if (ctx->API == API_OPENGLES)
+         if (!no_error && ctx->API == API_OPENGLES)
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.SkipPixels = param;
          break;
       case GL_UNPACK_SKIP_ROWS:
-         if (ctx->API == API_OPENGLES)
+         if (!no_error && ctx->API == API_OPENGLES)
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.SkipRows = param;
          break;
       case GL_UNPACK_SKIP_IMAGES:
-         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
             goto invalid_enum_error;
-         if (param < 0)
+         if (!no_error && param < 0)
             goto invalid_value_error;
          ctx->Unpack.SkipImages = param;
          break;
       case GL_UNPACK_ALIGNMENT:
-         if (param!=1 && param!=2 && param!=4 && param!=8)
+         if (!no_error && param!=1 && param!=2 && param!=4 && param!=8)
             goto invalid_value_error;
          ctx->Unpack.Alignment = param;
          break;
       case GL_UNPACK_COMPRESSED_BLOCK_WIDTH:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.CompressedBlockWidth = param;
          break;
       case GL_UNPACK_COMPRESSED_BLOCK_HEIGHT:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.CompressedBlockHeight = param;
          break;
       case GL_UNPACK_COMPRESSED_BLOCK_DEPTH:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.CompressedBlockDepth = param;
          break;
       case GL_UNPACK_COMPRESSED_BLOCK_SIZE:
-         if (!_mesa_is_desktop_gl(ctx))
+         if (!no_error && !_mesa_is_desktop_gl(ctx))
             goto invalid_enum_error;
-         if (param<0)
+         if (!no_error && param<0)
             goto invalid_value_error;
          ctx->Unpack.CompressedBlockSize = param;
          break;
       default:
-         goto invalid_enum_error;
+         if (!no_error)
+            goto invalid_enum_error;
+         else
+            unreachable("invalid pixel store enum");
    }
 
    return;
 
 invalid_enum_error:
-   _mesa_error( ctx, GL_INVALID_ENUM, "glPixelStore" );
+   _mesa_error(ctx, GL_INVALID_ENUM, "glPixelStore");
    return;
 
 invalid_value_error:
-   _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+   _mesa_error(ctx, GL_INVALID_VALUE, "glPixelStore(param)");
    return;
 }
 
 
 void GLAPIENTRY
-_mesa_PixelStoref( GLenum pname, GLfloat param )
+_mesa_PixelStorei(GLenum pname, GLint param)
+{
+   pixel_storei(pname, param, false);
+}
+
+
+void GLAPIENTRY
+_mesa_PixelStoref(GLenum pname, GLfloat param)
 {
-   _mesa_PixelStorei( pname, IROUND(param) );
+   _mesa_PixelStorei(pname, lroundf(param));
 }
 
 
+void GLAPIENTRY
+_mesa_PixelStorei_no_error(GLenum pname, GLint param)
+{
+   pixel_storei(pname, param, true);
+}
+
+
+void GLAPIENTRY
+_mesa_PixelStoref_no_error(GLenum pname, GLfloat param)
+{
+   _mesa_PixelStorei_no_error(pname, lroundf(param));
+}
+
 
 /**
  * Initialize the context's pixel store state.
  */
 void
-_mesa_init_pixelstore( struct gl_context *ctx )
+_mesa_init_pixelstore(struct gl_context *ctx)
 {
    /* Pixel transfer */
    ctx->Pack.Alignment = 4;
@@ -248,8 +273,7 @@ _mesa_init_pixelstore( struct gl_context *ctx )
    ctx->Pack.CompressedBlockHeight = 0;
    ctx->Pack.CompressedBlockDepth = 0;
    ctx->Pack.CompressedBlockSize = 0;
-   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
    ctx->Unpack.Alignment = 4;
    ctx->Unpack.RowLength = 0;
    ctx->Unpack.ImageHeight = 0;
@@ -263,8 +287,7 @@ _mesa_init_pixelstore( struct gl_context *ctx )
    ctx->Unpack.CompressedBlockHeight = 0;
    ctx->Unpack.CompressedBlockDepth = 0;
    ctx->Unpack.CompressedBlockSize = 0;
-   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
 
    /*
     * _mesa_unpack_image() returns image data in this format.  When we
@@ -281,6 +304,47 @@ _mesa_init_pixelstore( struct gl_context *ctx )
    ctx->DefaultPacking.SwapBytes = GL_FALSE;
    ctx->DefaultPacking.LsbFirst = GL_FALSE;
    ctx->DefaultPacking.Invert = GL_FALSE;
-   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj,
-                                 ctx->Shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
+}
+
+
+/**
+ * Check if the given compressed pixel storage parameters are legal.
+ * Record a GL error if illegal.
+ * \return  true if legal, false if illegal
+ */
+bool
+_mesa_compressed_pixel_storage_error_check(
+   struct gl_context *ctx,
+   GLint dimensions,
+   const struct gl_pixelstore_attrib *packing,
+   const char *caller)
+{
+   if (!_mesa_is_desktop_gl(ctx) || !packing->CompressedBlockSize)
+      return true;
+
+   if (packing->CompressedBlockWidth &&
+       packing->SkipPixels % packing->CompressedBlockWidth) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "%s(skip-pixels %% block-width)", caller);
+      return false;
+   }
+
+   if (dimensions > 1 &&
+       packing->CompressedBlockHeight &&
+       packing->SkipRows % packing->CompressedBlockHeight) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "%s(skip-rows %% block-height)", caller);
+      return false;
+   }
+
+   if (dimensions > 2 &&
+       packing->CompressedBlockDepth &&
+       packing->SkipImages % packing->CompressedBlockDepth) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "%s(skip-images %% block-depth)", caller);
+      return false;
+   }
+
+   return true;
 }