intel_tex.c revision bb1540835056cdea5db6f55b19c0c87358f14cd1
1#include "swrast/swrast.h"
2#include "main/texobj.h"
3#include "main/teximage.h"
4#include "main/mipmap.h"
5#include "drivers/common/meta.h"
6#include "intel_context.h"
7#include "intel_mipmap_tree.h"
8#include "intel_tex.h"
9
10#define FILE_DEBUG_FLAG DEBUG_TEXTURE
11
12static struct gl_texture_image *
13intelNewTextureImage(struct gl_context * ctx)
14{
15   DBG("%s\n", __FUNCTION__);
16   (void) ctx;
17   return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
18}
19
20
21static struct gl_texture_object *
22intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
23{
24   struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
25
26   DBG("%s\n", __FUNCTION__);
27   _mesa_initialize_texture_object(&obj->base, name, target);
28
29   return &obj->base;
30}
31
32static void
33intelDeleteTextureObject(struct gl_context *ctx,
34			 struct gl_texture_object *texObj)
35{
36   struct intel_context *intel = intel_context(ctx);
37   struct intel_texture_object *intelObj = intel_texture_object(texObj);
38
39   if (intelObj->mt)
40      intel_miptree_release(intel, &intelObj->mt);
41
42   _mesa_delete_texture_object(ctx, texObj);
43}
44
45
46static void
47intelFreeTextureImageData(struct gl_context * ctx, struct gl_texture_image *texImage)
48{
49   struct intel_context *intel = intel_context(ctx);
50   struct intel_texture_image *intelImage = intel_texture_image(texImage);
51
52   DBG("%s\n", __FUNCTION__);
53
54   if (intelImage->mt) {
55      intel_miptree_release(intel, &intelImage->mt);
56   }
57
58   if (texImage->Data) {
59      _mesa_free_texmemory(texImage->Data);
60      texImage->Data = NULL;
61   }
62}
63
64
65/* The system memcpy (at least on ubuntu 5.10) has problems copying
66 * to agp (writecombined) memory from a source which isn't 64-byte
67 * aligned - there is a 4x performance falloff.
68 *
69 * The x86 __memcpy is immune to this but is slightly slower
70 * (10%-ish) than the system memcpy.
71 *
72 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
73 * isn't much faster than x86_memcpy for agp copies.
74 *
75 * TODO: switch dynamically.
76 */
77static void *
78do_memcpy(void *dest, const void *src, size_t n)
79{
80   if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) {
81      return __memcpy(dest, src, n);
82   }
83   else
84      return memcpy(dest, src, n);
85}
86
87
88#if DO_DEBUG && !defined(__ia64__)
89
90#ifndef __x86_64__
91static unsigned
92fastrdtsc(void)
93{
94   unsigned eax;
95   __asm__ volatile ("\t"
96                     "pushl  %%ebx\n\t"
97                     "cpuid\n\t" ".byte 0x0f, 0x31\n\t"
98                     "popl %%ebx\n":"=a" (eax)
99                     :"0"(0)
100                     :"ecx", "edx", "cc");
101
102   return eax;
103}
104#else
105static unsigned
106fastrdtsc(void)
107{
108   unsigned eax;
109   __asm__ volatile ("\t" "cpuid\n\t" ".byte 0x0f, 0x31\n\t":"=a" (eax)
110                     :"0"(0)
111                     :"ecx", "edx", "ebx", "cc");
112
113   return eax;
114}
115#endif
116
117static unsigned
118time_diff(unsigned t, unsigned t2)
119{
120   return ((t < t2) ? t2 - t : 0xFFFFFFFFU - (t - t2 - 1));
121}
122
123
124static void *
125timed_memcpy(void *dest, const void *src, size_t n)
126{
127   void *ret;
128   unsigned t1, t2;
129   double rate;
130
131   if ((((unsigned) src) & 63) || (((unsigned) dest) & 63))
132      printf("Warning - non-aligned texture copy!\n");
133
134   t1 = fastrdtsc();
135   ret = do_memcpy(dest, src, n);
136   t2 = fastrdtsc();
137
138   rate = time_diff(t1, t2);
139   rate /= (double) n;
140   printf("timed_memcpy: %u %u --> %f clocks/byte\n", t1, t2, rate);
141   return ret;
142}
143#endif /* DO_DEBUG */
144
145
146/**
147 * Called via ctx->Driver.GenerateMipmap()
148 * This is basically a wrapper for _mesa_meta_GenerateMipmap() which checks
149 * if we'll be using software mipmap generation.  In that case, we need to
150 * map/unmap the base level texture image.
151 */
152static void
153intelGenerateMipmap(struct gl_context *ctx, GLenum target,
154                    struct gl_texture_object *texObj)
155{
156   if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
157      /* sw path: need to map texture images */
158      struct intel_context *intel = intel_context(ctx);
159      struct intel_texture_object *intelObj = intel_texture_object(texObj);
160
161      fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
162
163      intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
164      _mesa_generate_mipmap(ctx, target, texObj);
165      intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
166
167      {
168         GLuint nr_faces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
169         GLuint face, i;
170         /* Update the level information in our private data in the new images,
171          * since it didn't get set as part of a normal TexImage path.
172          */
173         for (face = 0; face < nr_faces; face++) {
174            for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
175               struct intel_texture_image *intelImage =
176                  intel_texture_image(texObj->Image[face][i]);
177               if (!intelImage)
178                  break;
179               intelImage->level = i;
180               intelImage->face = face;
181               /* Unreference the miptree to signal that the new Data is a
182                * bare pointer from mesa.
183                */
184               intel_miptree_release(intel, &intelImage->mt);
185            }
186         }
187      }
188   }
189   else {
190      _mesa_meta_GenerateMipmap(ctx, target, texObj);
191   }
192}
193
194
195void
196intelInitTextureFuncs(struct dd_function_table *functions)
197{
198   functions->ChooseTextureFormat = intelChooseTextureFormat;
199   functions->GenerateMipmap = intelGenerateMipmap;
200
201   functions->NewTextureObject = intelNewTextureObject;
202   functions->NewTextureImage = intelNewTextureImage;
203   functions->DeleteTexture = intelDeleteTextureObject;
204   functions->FreeTexImageData = intelFreeTextureImageData;
205
206#if DO_DEBUG && !defined(__ia64__)
207   if (INTEL_DEBUG & DEBUG_BUFMGR)
208      functions->TextureMemCpy = timed_memcpy;
209   else
210#endif
211      functions->TextureMemCpy = do_memcpy;
212}
213