i965: relAddr local var (to make debug/test a little easier)
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_copy.c
index b85a25642a29fea269f4569c6e48ce581ac1ec75..08437aa0e2b5b520af74c2f7f3e902b4073114fb 100644 (file)
  * 
  **************************************************************************/
 
-#include "mtypes.h"
-#include "enums.h"
-#include "image.h"
-#include "teximage.h"
+#include "main/mtypes.h"
+#include "main/enums.h"
+#include "main/image.h"
+#include "main/teximage.h"
+#include "main/mipmap.h"
 #include "swrast/swrast.h"
 
 #include "intel_screen.h"
@@ -40,7 +41,6 @@
 #include "intel_fbo.h"
 #include "intel_tex.h"
 #include "intel_blit.h"
-#include "intel_pixel.h"
 
 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
 
@@ -60,7 +60,7 @@ get_teximage_source(struct intel_context *intel, GLenum internalFormat)
 
    switch (internalFormat) {
    case GL_DEPTH_COMPONENT:
-   case GL_DEPTH_COMPONENT16_ARB:
+   case GL_DEPTH_COMPONENT16:
       irb = intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
       if (irb && irb->region && irb->region->cpp == 2)
          return irb->region;
@@ -75,7 +75,7 @@ get_teximage_source(struct intel_context *intel, GLenum internalFormat)
    case GL_RGBA8:
       return intel_readbuf_region(intel);
    case GL_RGB:
-      if (intel->intelScreen->cpp == 2)
+      if (intel->ctx.Visual.rgbBits == 16)
          return intel_readbuf_region(intel);
       return NULL;
    default:
@@ -86,17 +86,21 @@ get_teximage_source(struct intel_context *intel, GLenum internalFormat)
 
 static GLboolean
 do_copy_texsubimage(struct intel_context *intel,
+                   GLenum target,
                     struct intel_texture_image *intelImage,
                     GLenum internalFormat,
                     GLint dstx, GLint dsty,
                     GLint x, GLint y, GLsizei width, GLsizei height)
 {
    GLcontext *ctx = &intel->ctx;
+   struct gl_texture_object *texObj = intelImage->base.TexObject;
    const struct intel_region *src =
       get_teximage_source(intel, internalFormat);
 
    if (!intelImage->mt || !src) {
-      DBG("%s fail %p %p\n", __FUNCTION__, intelImage->mt, src);
+      if (INTEL_DEBUG & DEBUG_FALLBACKS)
+        fprintf(stderr, "%s fail %p %p\n",
+                __FUNCTION__, intelImage->mt, src);
       return GL_FALSE;
    }
 
@@ -108,63 +112,54 @@ do_copy_texsubimage(struct intel_context *intel,
                                                        intelImage->level);
       const GLint orig_x = x;
       const GLint orig_y = y;
-      const struct gl_framebuffer *fb = ctx->DrawBuffer;
-
-      if (_mesa_clip_to_region(fb->_Xmin, fb->_Ymin, fb->_Xmax, fb->_Ymax,
-                               &x, &y, &width, &height)) {
-         /* Update dst for clipped src.  Need to also clip the source rect.
-          */
-         dstx += x - orig_x;
-         dsty += y - orig_y;
-
-         if (ctx->ReadBuffer->Name == 0) {
-            /* reading from a window, adjust x, y */
-            __DRIdrawablePrivate *dPriv = intel->driDrawable;
-            GLuint window_y;
-            /* window_y = position of window on screen if y=0=bottom */
-            window_y = intel->intelScreen->height - (dPriv->y + dPriv->h);
-            y = window_y + y;
-            x += dPriv->x;
-         }
-         else {
-            /* reading from a FBO */
-            /* invert Y */
-            y = ctx->ReadBuffer->Height - y - 1;
-         }
-
-
-         /* A bit of fiddling to get the blitter to work with -ve
-          * pitches.  But we get a nice inverted blit this way, so it's
-          * worth it:
-          */
-         intelEmitCopyBlit(intel,
-                           intelImage->mt->cpp,
-                           -src->pitch,
-                           src->buffer,
-                           src->height * src->pitch * src->cpp,
-                           intelImage->mt->pitch,
-                           intelImage->mt->region->buffer,
-                           image_offset,
-                           x, y + height, dstx, dsty, width, height,
-                          GL_COPY); /* ? */
-
-         intel_batchbuffer_flush(intel->batch);
+      GLshort src_pitch;
+
+      /* Update dst for clipped src.  Need to also clip the source rect. */
+      dstx += x - orig_x;
+      dsty += y - orig_y;
+
+      /* image_offset may be non-page-aligned, but that's illegal for tiling. */
+      assert(intelImage->mt->region->tiling == I915_TILING_NONE);
+
+      if (ctx->ReadBuffer->Name == 0) {
+        /* reading from a window, adjust x, y */
+        __DRIdrawablePrivate *dPriv = intel->driDrawable;
+        y = dPriv->y + (dPriv->h - (y + height));
+        x += dPriv->x;
+
+        /* Invert the data coming from the source rectangle due to GL
+         * and hardware disagreeing on where y=0 is.
+         *
+         * It appears that our offsets and pitches get mangled
+         * appropriately by the hardware, and we don't need to adjust them
+         * on our own.
+         */
+        src_pitch = -src->pitch;
+      } else {
+        /* reading from a FBO, y is already oriented the way we like */
+        src_pitch = src->pitch;
       }
-   }
 
+      intelEmitCopyBlit(intel,
+                       intelImage->mt->cpp,
+                       src_pitch,
+                       src->buffer,
+                       0,
+                       src->tiling,
+                       intelImage->mt->pitch,
+                       intelImage->mt->region->buffer,
+                       image_offset,
+                       intelImage->mt->region->tiling,
+                       x, y, dstx, dsty, width, height,
+                       GL_COPY);
+   }
 
    UNLOCK_HARDWARE(intel);
 
-#if 0
-   /* GL_SGIS_generate_mipmap -- this can be accelerated now.
-    * XXX Add a ctx->Driver.GenerateMipmaps() function?
-    */
-   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
-      intel_generate_mipmap(ctx, target,
-                            &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
-                            texObj);
+   /* GL_SGIS_generate_mipmap */
+   if (intelImage->level == texObj->BaseLevel && texObj->GenerateMipmap) {
+      ctx->Driver.GenerateMipmap(ctx, target, texObj);
    }
-#endif
 
    return GL_TRUE;
 }
@@ -184,6 +179,7 @@ intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
       _mesa_select_tex_object(ctx, texUnit, target);
    struct gl_texture_image *texImage =
       _mesa_select_tex_image(ctx, texObj, target, level);
+   int srcx, srcy, dstx, dsty, height;
 
    if (border)
       goto fail;
@@ -195,10 +191,20 @@ intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
                           width, border,
                           GL_RGBA, CHAN_TYPE, NULL,
                           &ctx->DefaultPacking, texObj, texImage);
-
-   if (!do_copy_texsubimage(intel_context(ctx),
+   srcx = x;
+   srcy = y;
+   dstx = 0;
+   dsty = 0;
+   height = 1;
+   if (!_mesa_clip_copytexsubimage(ctx,
+                                  &dstx, &dsty,
+                                  &srcx, &srcy,
+                                  &width, &height))
+      return;
+
+   if (!do_copy_texsubimage(intel_context(ctx), target,
                             intel_texture_image(texImage),
-                            internalFormat, 0, 0, x, y, width, 1))
+                            internalFormat, 0, 0, x, y, width, height))
       goto fail;
 
    return;
@@ -220,10 +226,21 @@ intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
       _mesa_select_tex_object(ctx, texUnit, target);
    struct gl_texture_image *texImage =
       _mesa_select_tex_image(ctx, texObj, target, level);
+   int srcx, srcy, dstx, dsty;
 
    if (border)
       goto fail;
 
+   srcx = x;
+   srcy = y;
+   dstx = 0;
+   dsty = 0;
+   if (!_mesa_clip_copytexsubimage(ctx,
+                                  &dstx, &dsty,
+                                  &srcx, &srcy,
+                                  &width, &height))
+      return;
+
    /* Setup or redefine the texture object, mipmap tree and texture
     * image.  Don't populate yet.  
     */
@@ -233,7 +250,7 @@ intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
                           &ctx->DefaultPacking, texObj, texImage);
 
 
-   if (!do_copy_texsubimage(intel_context(ctx),
+   if (!do_copy_texsubimage(intel_context(ctx), target,
                             intel_texture_image(texImage),
                             internalFormat, 0, 0, x, y, width, height))
       goto fail;
@@ -263,7 +280,7 @@ intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
    /* Need to check texture is compatible with source format. 
     */
 
-   if (!do_copy_texsubimage(intel_context(ctx),
+   if (!do_copy_texsubimage(intel_context(ctx), target,
                             intel_texture_image(texImage),
                             internalFormat, xoffset, 0, x, y, width, 1)) {
       _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
@@ -289,7 +306,7 @@ intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
    /* Need to check texture is compatible with source format. 
     */
 
-   if (!do_copy_texsubimage(intel_context(ctx),
+   if (!do_copy_texsubimage(intel_context(ctx), target,
                             intel_texture_image(texImage),
                             internalFormat,
                             xoffset, yoffset, x, y, width, height)) {