r600: fix warning
[mesa.git] / src / mesa / state_tracker / st_cb_texture.c
index 0a72784ce0626f4510d8632005741c75676f61b5..90a059ca69a5769ca509fabc06180f3d4d9e1f05 100644 (file)
@@ -59,6 +59,7 @@
 #include "util/u_tile.h"
 #include "util/u_blit.h"
 #include "util/u_surface.h"
+#include "util/u_math.h"
 
 
 #define DBG if (0) printf
@@ -237,18 +238,6 @@ do_memcpy(void *dest, const void *src, size_t n)
 }
 
 
-static int
-logbase2(int n)
-{
-   GLint i = 1, log2 = 0;
-   while (n > i) {
-      i *= 2;
-      log2++;
-   }
-   return log2;
-}
-
-
 /**
  * Return default texture usage bitmask for the given texture format.
  */
@@ -342,9 +331,9 @@ guess_and_alloc_texture(struct st_context *st,
       lastLevel = firstLevel;
    }
    else {
-      GLuint l2width = logbase2(width);
-      GLuint l2height = logbase2(height);
-      GLuint l2depth = logbase2(depth);
+      GLuint l2width = util_logbase2(width);
+      GLuint l2height = util_logbase2(height);
+      GLuint l2depth = util_logbase2(depth);
       lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
    }
 
@@ -529,10 +518,17 @@ st_TexImage(GLcontext * ctx,
    GLint texelBytes, sizeInBytes;
    GLuint dstRowStride;
    struct gl_pixelstore_attrib unpackNB;
+   enum pipe_transfer_usage transfer_usage;
 
    DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
        _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
 
+   /* switch to "normal" */
+   if (stObj->surface_based) {
+      _mesa_clear_texture_object(ctx, texObj);
+      stObj->surface_based = GL_FALSE;
+   }
+
    /* gallium does not support texture borders, strip it off */
    if (border) {
       strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB);
@@ -680,8 +676,14 @@ st_TexImage(GLcontext * ctx,
    }
 
    if (stImage->pt) {
+      if (format == GL_DEPTH_COMPONENT &&
+          pf_is_depth_and_stencil(stImage->pt->format))
+         transfer_usage = PIPE_TRANSFER_READ_WRITE;
+      else
+         transfer_usage = PIPE_TRANSFER_WRITE;
+
       texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
-                                            PIPE_TRANSFER_WRITE, 0, 0,
+                                            transfer_usage, 0, 0,
                                             stImage->base.Width,
                                             stImage->base.Height);
       if(stImage->transfer)
@@ -742,7 +744,7 @@ st_TexImage(GLcontext * ctx,
            st_texture_image_unmap(ctx->st, stImage);
             /* map next slice of 3D texture */
            texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
-                                                  PIPE_TRANSFER_WRITE, 0, 0,
+                                                  transfer_usage, 0, 0,
                                                   stImage->base.Width,
                                                   stImage->base.Height);
            src += srcImageStride;
@@ -943,8 +945,9 @@ st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
       /* Image is stored in hardware format in a buffer managed by the
        * kernel.  Need to explicitly map and unmap it.
        */
+      unsigned face = _mesa_tex_target_to_face(target);
 
-      st_teximage_flush_before_map(ctx->st, stImage->pt, 0, level,
+      st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
                                   PIPE_TRANSFER_READ);
 
       texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
@@ -1041,6 +1044,8 @@ st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
       _mesa_image_image_stride(packing, width, height, format, type);
    GLint i;
    const GLubyte *src;
+   /* init to silence warning only: */
+   enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
 
    DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
        _mesa_lookup_enum_by_nr(target),
@@ -1072,10 +1077,18 @@ st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
     * from uploading the buffer under us.
     */
    if (stImage->pt) {
-      st_teximage_flush_before_map(ctx->st, stImage->pt, 0, level,
-                                  PIPE_TRANSFER_WRITE);
+      unsigned face = _mesa_tex_target_to_face(target);
+
+      if (format == GL_DEPTH_COMPONENT &&
+          pf_is_depth_and_stencil(stImage->pt->format))
+         transfer_usage = PIPE_TRANSFER_READ_WRITE;
+      else
+         transfer_usage = PIPE_TRANSFER_WRITE;
+
+      st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
+                                  transfer_usage);
       texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset, 
-                                            PIPE_TRANSFER_WRITE,
+                                            transfer_usage,
                                             xoffset, yoffset,
                                             width, height);
    }
@@ -1106,7 +1119,7 @@ st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
          /* map next slice of 3D texture */
         texImage->Data = st_texture_image_map(ctx->st, stImage,
                                                zoffset + i + 1,
-                                               PIPE_TRANSFER_WRITE,
+                                               transfer_usage,
                                                xoffset, yoffset,
                                                width, height);
         src += srcImageStride;
@@ -1199,7 +1212,9 @@ st_CompressedTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
    int y;
 
    if (stImage->pt) {
-      st_teximage_flush_before_map(ctx->st, stImage->pt, 0, level,
+      unsigned face = _mesa_tex_target_to_face(target);
+
+      st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
                                   PIPE_TRANSFER_WRITE);
       texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 
                                             PIPE_TRANSFER_WRITE,
@@ -1274,6 +1289,7 @@ fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
    struct pipe_screen *screen = pipe->screen;
    struct pipe_transfer *src_trans;
    GLvoid *texDest;
+   enum pipe_transfer_usage transfer_usage;
 
    assert(width <= MAX_WIDTH);
 
@@ -1288,10 +1304,16 @@ fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
                                               srcX, srcY,
                                               width, height);
 
+   if (baseFormat == GL_DEPTH_COMPONENT &&
+       pf_is_depth_and_stencil(stImage->pt->format))
+      transfer_usage = PIPE_TRANSFER_READ_WRITE;
+   else
+      transfer_usage = PIPE_TRANSFER_WRITE;
+
    st_teximage_flush_before_map(ctx->st, stImage->pt, 0, 0,
-                               PIPE_TRANSFER_WRITE);
+                               transfer_usage);
 
-   texDest = st_texture_image_map(ctx->st, stImage, 0, PIPE_TRANSFER_WRITE,
+   texDest = st_texture_image_map(ctx->st, stImage, 0, transfer_usage,
                                   destX, destY, width, height);
 
    if (baseFormat == GL_DEPTH_COMPONENT ||