i965: Stop passing num_samples to intel_miptree_alloc_hiz().
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex.c
index 49b7d5ef6b1710d8c355151e80f330deb0349800..24f13dfee8993933345666fb0f0847223594978d 100644 (file)
@@ -1,4 +1,5 @@
 #include "swrast/swrast.h"
+#include "main/renderbuffer.h"
 #include "main/texobj.h"
 #include "main/teximage.h"
 #include "main/mipmap.h"
 #include "intel_context.h"
 #include "intel_mipmap_tree.h"
 #include "intel_tex.h"
+#include "intel_fbo.h"
 
 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
 
 static struct gl_texture_image *
-intelNewTextureImage(GLcontext * ctx)
+intelNewTextureImage(struct gl_context * ctx)
 {
    DBG("%s\n", __FUNCTION__);
    (void) ctx;
    return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
 }
 
+static void
+intelDeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
+{
+   /* nothing special (yet) for intel_texture_image */
+   _mesa_delete_texture_image(ctx, img);
+}
+
 
 static struct gl_texture_object *
-intelNewTextureObject(GLcontext * ctx, GLuint name, GLenum target)
+intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
 {
    struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
 
+   (void) ctx;
+
    DBG("%s\n", __FUNCTION__);
+
+   if (obj == NULL)
+      return NULL;
+
    _mesa_initialize_texture_object(&obj->base, name, target);
 
+   obj->needs_validate = true;
+
    return &obj->base;
 }
 
 static void 
-intelDeleteTextureObject(GLcontext *ctx,
+intelDeleteTextureObject(struct gl_context *ctx,
                         struct gl_texture_object *texObj)
 {
-   struct intel_context *intel = intel_context(ctx);
    struct intel_texture_object *intelObj = intel_texture_object(texObj);
 
-   if (intelObj->mt)
-      intel_miptree_release(intel, &intelObj->mt);
-
+   intel_miptree_release(&intelObj->mt);
    _mesa_delete_texture_object(ctx, texObj);
 }
 
-
-static void
-intelFreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage)
+static GLboolean
+intel_alloc_texture_image_buffer(struct gl_context *ctx,
+                                struct gl_texture_image *image)
 {
    struct intel_context *intel = intel_context(ctx);
-   struct intel_texture_image *intelImage = intel_texture_image(texImage);
+   struct intel_texture_image *intel_image = intel_texture_image(image);
+   struct gl_texture_object *texobj = image->TexObject;
+   struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
 
-   DBG("%s\n", __FUNCTION__);
+   assert(image->Border == 0);
 
-   if (intelImage->mt) {
-      intel_miptree_release(intel, &intelImage->mt);
+   /* Quantize sample count */
+   if (image->NumSamples) {
+      image->NumSamples = intel_quantize_num_samples(intel->intelScreen, image->NumSamples);
+      if (!image->NumSamples)
+         return false;
    }
 
-   if (texImage->Data) {
-      _mesa_free_texmemory(texImage->Data);
-      texImage->Data = NULL;
+   /* Because the driver uses AllocTextureImageBuffer() internally, it may end
+    * up mismatched with FreeTextureImageBuffer(), but that is safe to call
+    * multiple times.
+    */
+   ctx->Driver.FreeTextureImageBuffer(ctx, image);
+
+   if (!_swrast_init_texture_image(image))
+      return false;
+
+   if (intel_texobj->mt &&
+       intel_miptree_match_image(intel_texobj->mt, image)) {
+      intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
+      DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
+          __FUNCTION__, texobj, image->Level,
+          image->Width, image->Height, image->Depth, intel_texobj->mt);
+   } else {
+      intel_image->mt = intel_miptree_create_for_teximage(intel, intel_texobj,
+                                                          intel_image,
+                                                          false);
+
+      /* Even if the object currently has a mipmap tree associated
+       * with it, this one is a more likely candidate to represent the
+       * whole object since our level didn't fit what was there
+       * before, and any lower levels would fit into our miptree.
+       */
+      intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
+
+      DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
+          __FUNCTION__, texobj, image->Level,
+          image->Width, image->Height, image->Depth, intel_image->mt);
    }
-}
 
+   intel_texobj->needs_validate = true;
 
-/* The system memcpy (at least on ubuntu 5.10) has problems copying
- * to agp (writecombined) memory from a source which isn't 64-byte
- * aligned - there is a 4x performance falloff.
- *
- * The x86 __memcpy is immune to this but is slightly slower
- * (10%-ish) than the system memcpy.
- *
- * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
- * isn't much faster than x86_memcpy for agp copies.
- * 
- * TODO: switch dynamically.
- */
-static void *
-do_memcpy(void *dest, const void *src, size_t n)
-{
-   if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) {
-      return __memcpy(dest, src, n);
-   }
-   else
-      return memcpy(dest, src, n);
+   return true;
 }
 
+static void
+intel_free_texture_image_buffer(struct gl_context * ctx,
+                               struct gl_texture_image *texImage)
+{
+   struct intel_texture_image *intelImage = intel_texture_image(texImage);
 
-#if DO_DEBUG && !defined(__ia64__)
+   DBG("%s\n", __FUNCTION__);
 
-#ifndef __x86_64__
-static unsigned
-fastrdtsc(void)
-{
-   unsigned eax;
-   __asm__ volatile ("\t"
-                     "pushl  %%ebx\n\t"
-                     "cpuid\n\t" ".byte 0x0f, 0x31\n\t"
-                     "popl %%ebx\n":"=a" (eax)
-                     :"0"(0)
-                     :"ecx", "edx", "cc");
-
-   return eax;
-}
-#else
-static unsigned
-fastrdtsc(void)
-{
-   unsigned eax;
-   __asm__ volatile ("\t" "cpuid\n\t" ".byte 0x0f, 0x31\n\t":"=a" (eax)
-                     :"0"(0)
-                     :"ecx", "edx", "ebx", "cc");
+   intel_miptree_release(&intelImage->mt);
 
-   return eax;
+   _swrast_free_texture_image_buffer(ctx, texImage);
 }
-#endif
 
-static unsigned
-time_diff(unsigned t, unsigned t2)
+/**
+ * Map texture memory/buffer into user space.
+ * Note: the region of interest parameters are ignored here.
+ * \param mode  bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
+ * \param mapOut  returns start of mapping of region of interest
+ * \param rowStrideOut  returns row stride in bytes
+ */
+static void
+intel_map_texture_image(struct gl_context *ctx,
+                       struct gl_texture_image *tex_image,
+                       GLuint slice,
+                       GLuint x, GLuint y, GLuint w, GLuint h,
+                       GLbitfield mode,
+                       GLubyte **map,
+                       GLint *stride)
 {
-   return ((t < t2) ? t2 - t : 0xFFFFFFFFU - (t - t2 - 1));
-}
-
+   struct intel_context *intel = intel_context(ctx);
+   struct intel_texture_image *intel_image = intel_texture_image(tex_image);
+   struct intel_mipmap_tree *mt = intel_image->mt;
 
-static void *
-timed_memcpy(void *dest, const void *src, size_t n)
-{
-   void *ret;
-   unsigned t1, t2;
-   double rate;
+   /* Our texture data is always stored in a miptree. */
+   assert(mt);
 
-   if ((((unsigned) src) & 63) || (((unsigned) dest) & 63))
-      printf("Warning - non-aligned texture copy!\n");
+   /* Check that our caller wasn't confused about how to map a 1D texture. */
+   assert(tex_image->TexObject->Target != GL_TEXTURE_1D_ARRAY ||
+         h == 1);
 
-   t1 = fastrdtsc();
-   ret = do_memcpy(dest, src, n);
-   t2 = fastrdtsc();
+   /* intel_miptree_map operates on a unified "slice" number that references the
+    * cube face, since it's all just slices to the miptree code.
+    */
+   if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
+      slice = tex_image->Face;
 
-   rate = time_diff(t1, t2);
-   rate /= (double) n;
-   printf("timed_memcpy: %u %u --> %f clocks/byte\n", t1, t2, rate);
-   return ret;
+   intel_miptree_map(intel, mt, tex_image->Level, slice, x, y, w, h, mode,
+                    (void **)map, stride);
 }
-#endif /* DO_DEBUG */
 
-
-/**
- * Called via ctx->Driver.GenerateMipmap()
- * This is basically a wrapper for _mesa_meta_GenerateMipmap() which checks
- * if we'll be using software mipmap generation.  In that case, we need to
- * map/unmap the base level texture image.
- */
 static void
-intelGenerateMipmap(GLcontext *ctx, GLenum target,
-                    struct gl_texture_object *texObj)
+intel_unmap_texture_image(struct gl_context *ctx,
+                         struct gl_texture_image *tex_image, GLuint slice)
 {
-   if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
-      /* sw path: need to map texture images */
-      struct intel_context *intel = intel_context(ctx);
-      struct intel_texture_object *intelObj = intel_texture_object(texObj);
-
-      if (INTEL_DEBUG & DEBUG_FALLBACKS)
-        fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
-
-      intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
-      _mesa_generate_mipmap(ctx, target, texObj);
-      intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
-
-      {
-         GLuint nr_faces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
-         GLuint face, i;
-         /* Update the level information in our private data in the new images,
-          * since it didn't get set as part of a normal TexImage path.
-          */
-         for (face = 0; face < nr_faces; face++) {
-            for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
-               struct intel_texture_image *intelImage =
-                  intel_texture_image(texObj->Image[face][i]);
-               if (!intelImage)
-                  break;
-               intelImage->level = i;
-               intelImage->face = face;
-               /* Unreference the miptree to signal that the new Data is a
-                * bare pointer from mesa.
-                */
-               intel_miptree_release(intel, &intelImage->mt);
-            }
-         }
-      }
-   }
-   else {
-      _mesa_meta_GenerateMipmap(ctx, target, texObj);
-   }
-}
+   struct intel_context *intel = intel_context(ctx);
+   struct intel_texture_image *intel_image = intel_texture_image(tex_image);
+   struct intel_mipmap_tree *mt = intel_image->mt;
 
+   if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
+      slice = tex_image->Face;
+
+   intel_miptree_unmap(intel, mt, tex_image->Level, slice);
+}
 
 void
 intelInitTextureFuncs(struct dd_function_table *functions)
 {
-   functions->ChooseTextureFormat = intelChooseTextureFormat;
-   functions->GenerateMipmap = intelGenerateMipmap;
-
    functions->NewTextureObject = intelNewTextureObject;
    functions->NewTextureImage = intelNewTextureImage;
+   functions->DeleteTextureImage = intelDeleteTextureImage;
    functions->DeleteTexture = intelDeleteTextureObject;
-   functions->FreeTexImageData = intelFreeTextureImageData;
-   functions->UpdateTexturePalette = 0;
-
-#if DO_DEBUG && !defined(__ia64__)
-   if (INTEL_DEBUG & DEBUG_BUFMGR)
-      functions->TextureMemCpy = timed_memcpy;
-   else
-#endif
-      functions->TextureMemCpy = do_memcpy;
+   functions->AllocTextureImageBuffer = intel_alloc_texture_image_buffer;
+   functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
+   functions->MapTextureImage = intel_map_texture_image;
+   functions->UnmapTextureImage = intel_unmap_texture_image;
 }