replace malloc macros in imports.h with u_memory.h versions
[mesa.git] / src / mesa / swrast / s_texture.c
index 4d3cd0f8ce1c92294de7043635922b0364072eca..88b4e87c62af3b2f88f524d00700cb897612cba9 100644 (file)
@@ -31,6 +31,7 @@
 #include "main/fbobject.h"
 #include "main/teximage.h"
 #include "main/texobj.h"
+#include "util/u_memory.h"
 #include "swrast/swrast.h"
 #include "swrast/s_context.h"
 
@@ -60,7 +61,7 @@ _swrast_delete_texture_image(struct gl_context *ctx,
 }
 
 static unsigned int
-texture_slices(struct gl_texture_image *texImage)
+texture_slices(const struct gl_texture_image *texImage)
 {
    if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY)
       return texImage->Height;
@@ -88,23 +89,27 @@ _swrast_alloc_texture_image_buffer(struct gl_context *ctx,
                                    struct gl_texture_image *texImage)
 {
    struct swrast_texture_image *swImg = swrast_texture_image(texImage);
-   GLuint bytes = _mesa_format_image_size(texImage->TexFormat, texImage->Width,
-                                          texImage->Height, texImage->Depth);
+   GLuint bytesPerSlice;
+   GLuint slices = texture_slices(texImage);
    GLuint i;
 
    if (!_swrast_init_texture_image(texImage))
       return GL_FALSE;
 
+   bytesPerSlice = _mesa_format_image_size(texImage->TexFormat, texImage->Width,
+                                           _swrast_teximage_slice_height(texImage), 1);
+
    assert(!swImg->Buffer);
-   swImg->Buffer = _mesa_align_malloc(bytes, 512);
+   swImg->Buffer = _mesa_align_malloc(bytesPerSlice * slices, 512);
    if (!swImg->Buffer)
       return GL_FALSE;
 
-   /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
-   swImg->RowStride = texImage->Width;
+   /* RowStride and ImageSlices[] describe how to address texels in 'Data' */
+   swImg->RowStride = _mesa_format_row_stride(texImage->TexFormat,
+                                              texImage->Width);
 
-   for (i = 0; i < texture_slices(texImage); i++) {
-      swImg->ImageOffsets[i] = i * texImage->Width * texImage->Height;
+   for (i = 0; i < slices; i++) {
+      swImg->ImageSlices[i] = swImg->Buffer + bytesPerSlice * i;
    }
 
    return GL_TRUE;
@@ -114,7 +119,7 @@ _swrast_alloc_texture_image_buffer(struct gl_context *ctx,
 /**
  * Code that overrides ctx->Driver.AllocTextureImageBuffer may use this to
  * initialize the fields of swrast_texture_image without allocating the image
- * buffer or initializing RowStride or the contents of ImageOffsets.
+ * buffer or initializing RowStride or the contents of ImageSlices.
  *
  * Returns GL_TRUE on success, GL_FALSE on memory allocation failure.
  */
@@ -123,9 +128,9 @@ _swrast_init_texture_image(struct gl_texture_image *texImage)
 {
    struct swrast_texture_image *swImg = swrast_texture_image(texImage);
 
-   if ((texImage->Width == 1 || _mesa_is_pow_two(texImage->Width2)) &&
-       (texImage->Height == 1 || _mesa_is_pow_two(texImage->Height2)) &&
-       (texImage->Depth == 1 || _mesa_is_pow_two(texImage->Depth2)))
+   if ((texImage->Width == 1 || util_is_power_of_two_or_zero(texImage->Width2)) &&
+       (texImage->Height == 1 || util_is_power_of_two_or_zero(texImage->Height2)) &&
+       (texImage->Depth == 1 || util_is_power_of_two_or_zero(texImage->Depth2)))
       swImg->_IsPowerOfTwo = GL_TRUE;
    else
       swImg->_IsPowerOfTwo = GL_FALSE;
@@ -143,9 +148,9 @@ _swrast_init_texture_image(struct gl_texture_image *texImage)
       swImg->DepthScale = (GLfloat) texImage->Depth;
    }
 
-   assert(!swImg->ImageOffsets);
-   swImg->ImageOffsets = malloc(texture_slices(texImage) * sizeof(GLuint));
-   if (!swImg->ImageOffsets)
+   assert(!swImg->ImageSlices);
+   swImg->ImageSlices = calloc(texture_slices(texImage), sizeof(void *));
+   if (!swImg->ImageSlices)
       return GL_FALSE;
 
    return GL_TRUE;
@@ -160,13 +165,12 @@ _swrast_free_texture_image_buffer(struct gl_context *ctx,
                                   struct gl_texture_image *texImage)
 {
    struct swrast_texture_image *swImage = swrast_texture_image(texImage);
-   if (swImage->Buffer) {
-      _mesa_align_free(swImage->Buffer);
-      swImage->Buffer = NULL;
-   }
 
-   free(swImage->ImageOffsets);
-   swImage->ImageOffsets = NULL;
+   _mesa_align_free(swImage->Buffer);
+   swImage->Buffer = NULL;
+
+   free(swImage->ImageSlices);
+   swImage->ImageSlices = NULL;
 }
 
 
@@ -174,8 +178,8 @@ _swrast_free_texture_image_buffer(struct gl_context *ctx,
  * Error checking for debugging only.
  */
 static void
-_mesa_check_map_teximage(struct gl_texture_image *texImage,
-                         GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h)
+check_map_teximage(const struct gl_texture_image *texImage,
+                   GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h)
 {
 
    if (texImage->TexObject->Target == GL_TEXTURE_1D)
@@ -185,6 +189,7 @@ _mesa_check_map_teximage(struct gl_texture_image *texImage,
    assert(y < texImage->Height || texImage->Height == 0);
    assert(x + w <= texImage->Width);
    assert(y + h <= texImage->Height);
+   assert(slice < texture_slices(texImage));
 }
 
 /**
@@ -213,7 +218,16 @@ _swrast_map_teximage(struct gl_context *ctx,
    GLint stride, texelSize;
    GLuint bw, bh;
 
-   _mesa_check_map_teximage(texImage, slice, x, y, w, h);
+   check_map_teximage(texImage, slice, x, y, w, h);
+
+   if (!swImage->Buffer) {
+      /* Either glTexImage was called with a NULL <pixels> argument or
+       * we ran out of memory when allocating texture memory,
+       */
+      *mapOut = NULL;
+      *rowStrideOut = 0;
+      return;
+   }
 
    texelSize = _mesa_get_format_bytes(texImage->TexFormat);
    stride = _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
@@ -222,22 +236,13 @@ _swrast_map_teximage(struct gl_context *ctx,
    assert(x % bw == 0);
    assert(y % bh == 0);
 
-   if (!swImage->Buffer) {
-      /* probably ran out of memory when allocating tex mem */
-      *mapOut = NULL;
-      return;
-   }
-      
-   map = swImage->Buffer;
+   /* This function can only be used with a swrast-allocated buffer, in which
+    * case ImageSlices is populated with pointers into Buffer.
+    */
+   assert(swImage->Buffer);
+   assert(swImage->Buffer == swImage->ImageSlices[0]);
 
-   assert(slice < texture_slices(texImage));
-   if (slice != 0) {
-      GLuint sliceSize = _mesa_format_image_size(texImage->TexFormat,
-                                                 texImage->Width,
-                                                 _swrast_teximage_slice_height(texImage),
-                                                 1);
-      map += slice * sliceSize;
-   }
+   map = swImage->ImageSlices[slice];
 
    /* apply x/y offset to map address */
    map += stride * (y / bh) + texelSize * (x / bw);
@@ -264,12 +269,51 @@ _swrast_map_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
    for (face = 0; face < faces; face++) {
       for (level = texObj->BaseLevel; level < MAX_TEXTURE_LEVELS; level++) {
          struct gl_texture_image *texImage = texObj->Image[face][level];
-         if (texImage) {
-            struct swrast_texture_image *swImage =
-               swrast_texture_image(texImage);
+         struct swrast_texture_image *swImage = swrast_texture_image(texImage);
+         unsigned int i, slices;
+
+         if (!texImage)
+            continue;
+
+         /* In the case of a swrast-allocated texture buffer, the ImageSlices
+          * and RowStride are always available.
+          */
+         if (swImage->Buffer) {
+            assert(swImage->ImageSlices[0] == swImage->Buffer);
+            continue;
+         }
+
+         if (!swImage->ImageSlices) {
+            swImage->ImageSlices =
+               calloc(texture_slices(texImage), sizeof(void *));
+            if (!swImage->ImageSlices)
+               continue;
+         }
 
-            /* XXX we'll eventually call _swrast_map_teximage() here */
-            swImage->Map = swImage->Buffer;
+         slices = texture_slices(texImage);
+
+         for (i = 0; i < slices; i++) {
+            GLubyte *map;
+            GLint rowStride;
+
+            if (swImage->ImageSlices[i])
+               continue;
+
+            ctx->Driver.MapTextureImage(ctx, texImage, i,
+                                        0, 0,
+                                        texImage->Width, texImage->Height,
+                                        GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
+                                        &map, &rowStride);
+
+            swImage->ImageSlices[i] = map;
+            /* A swrast-using driver has to return the same rowstride for
+             * every slice of the same texture, since we don't track them
+             * separately.
+             */
+            if (i == 0)
+               swImage->RowStride = rowStride;
+            else
+               assert(swImage->RowStride == rowStride);
          }
       }
    }
@@ -285,12 +329,25 @@ _swrast_unmap_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
    for (face = 0; face < faces; face++) {
       for (level = texObj->BaseLevel; level < MAX_TEXTURE_LEVELS; level++) {
          struct gl_texture_image *texImage = texObj->Image[face][level];
-         if (texImage) {
-            struct swrast_texture_image *swImage
-               = swrast_texture_image(texImage);
+         struct swrast_texture_image *swImage = swrast_texture_image(texImage);
+         unsigned int i, slices;
+
+         if (!texImage)
+            continue;
+
+         if (swImage->Buffer)
+            return;
 
-            /* XXX we'll eventually call _swrast_unmap_teximage() here */
-            swImage->Map = NULL;
+         if (!swImage->ImageSlices)
+            continue;
+
+         slices = texture_slices(texImage);
+
+         for (i = 0; i < slices; i++) {
+            if (swImage->ImageSlices[i]) {
+               ctx->Driver.UnmapTextureImage(ctx, texImage, i);
+               swImage->ImageSlices[i] = NULL;
+            }
          }
       }
    }
@@ -303,16 +360,13 @@ _swrast_unmap_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
 void
 _swrast_map_textures(struct gl_context *ctx)
 {
-   GLbitfield enabledUnits = ctx->Texture._EnabledUnits;
+   int unit;
 
-   /* loop over enabled texture units */
-   while (enabledUnits) {
-      GLuint unit = ffs(enabledUnits) - 1;
+   for (unit = 0; unit <= ctx->Texture._MaxEnabledTexImageUnit; unit++) {
       struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
-      
-      _swrast_map_texture(ctx, texObj);
 
-      enabledUnits &= ~(1 << unit);
+      if (texObj)
+         _swrast_map_texture(ctx, texObj);
    }
 }
 
@@ -323,15 +377,11 @@ _swrast_map_textures(struct gl_context *ctx)
 void
 _swrast_unmap_textures(struct gl_context *ctx)
 {
-   GLbitfield enabledUnits = ctx->Texture._EnabledUnits;
-
-   /* loop over enabled texture units */
-   while (enabledUnits) {
-      GLuint unit = ffs(enabledUnits) - 1;
+   int unit;
+   for (unit = 0; unit <= ctx->Texture._MaxEnabledTexImageUnit; unit++) {
       struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
-      
-      _swrast_unmap_texture(ctx, texObj);
 
-      enabledUnits &= ~(1 << unit);
+      if (texObj)
+         _swrast_unmap_texture(ctx, texObj);
    }
 }