i915tex: Implement SetTexOffset hook.
[mesa.git] / src / mesa / drivers / dri / i915tex / intel_tex.c
1 #include "texobj.h"
2 #include "intel_context.h"
3 #include "intel_mipmap_tree.h"
4 #include "intel_tex.h"
5
6 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
7
8 static GLboolean
9 intelIsTextureResident(GLcontext * ctx, struct gl_texture_object *texObj)
10 {
11 #if 0
12 struct intel_context *intel = intel_context(ctx);
13 struct intel_texture_object *intelObj = intel_texture_object(texObj);
14
15 return
16 intelObj->mt &&
17 intelObj->mt->region &&
18 intel_is_region_resident(intel, intelObj->mt->region);
19 #endif
20 return 1;
21 }
22
23
24
25 static struct gl_texture_image *
26 intelNewTextureImage(GLcontext * ctx)
27 {
28 DBG("%s\n", __FUNCTION__);
29 (void) ctx;
30 return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
31 }
32
33
34 static struct gl_texture_object *
35 intelNewTextureObject(GLcontext * ctx, GLuint name, GLenum target)
36 {
37 struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
38
39 DBG("%s\n", __FUNCTION__);
40 _mesa_initialize_texture_object(&obj->base, name, target);
41
42 return &obj->base;
43 }
44
45 static void
46 intelDeleteTextureObject(GLcontext *ctx,
47 struct gl_texture_object *texObj)
48 {
49 struct intel_context *intel = intel_context(ctx);
50 struct intel_texture_object *intelObj = intel_texture_object(texObj);
51
52 if (intelObj->mt)
53 intel_miptree_release(intel, &intelObj->mt);
54
55 _mesa_delete_texture_object(ctx, texObj);
56 }
57
58
59 static void
60 intelFreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage)
61 {
62 struct intel_context *intel = intel_context(ctx);
63 struct intel_texture_image *intelImage = intel_texture_image(texImage);
64
65 DBG("%s\n", __FUNCTION__);
66
67 if (intelImage->mt) {
68 intel_miptree_release(intel, &intelImage->mt);
69 }
70
71 if (texImage->Data) {
72 free(texImage->Data);
73 texImage->Data = NULL;
74 }
75 }
76
77
78 /* The system memcpy (at least on ubuntu 5.10) has problems copying
79 * to agp (writecombined) memory from a source which isn't 64-byte
80 * aligned - there is a 4x performance falloff.
81 *
82 * The x86 __memcpy is immune to this but is slightly slower
83 * (10%-ish) than the system memcpy.
84 *
85 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
86 * isn't much faster than x86_memcpy for agp copies.
87 *
88 * TODO: switch dynamically.
89 */
90 static void *
91 do_memcpy(void *dest, const void *src, size_t n)
92 {
93 if ((((unsigned) src) & 63) || (((unsigned) dest) & 63)) {
94 return __memcpy(dest, src, n);
95 }
96 else
97 return memcpy(dest, src, n);
98 }
99
100
101 #if DO_DEBUG
102
103 #ifndef __x86_64__
104 static unsigned
105 fastrdtsc(void)
106 {
107 unsigned eax;
108 __asm__ volatile ("\t"
109 "pushl %%ebx\n\t"
110 "cpuid\n\t" ".byte 0x0f, 0x31\n\t"
111 "popl %%ebx\n":"=a" (eax)
112 :"0"(0)
113 :"ecx", "edx", "cc");
114
115 return eax;
116 }
117 #else
118 static unsigned
119 fastrdtsc(void)
120 {
121 unsigned eax;
122 __asm__ volatile ("\t" "cpuid\n\t" ".byte 0x0f, 0x31\n\t":"=a" (eax)
123 :"0"(0)
124 :"ecx", "edx", "ebx", "cc");
125
126 return eax;
127 }
128 #endif
129
130 static unsigned
131 time_diff(unsigned t, unsigned t2)
132 {
133 return ((t < t2) ? t2 - t : 0xFFFFFFFFU - (t - t2 - 1));
134 }
135
136
137 static void *
138 timed_memcpy(void *dest, const void *src, size_t n)
139 {
140 void *ret;
141 unsigned t1, t2;
142 double rate;
143
144 if ((((unsigned) src) & 63) || (((unsigned) dest) & 63))
145 _mesa_printf("Warning - non-aligned texture copy!\n");
146
147 t1 = fastrdtsc();
148 ret = do_memcpy(dest, src, n);
149 t2 = fastrdtsc();
150
151 rate = time_diff(t1, t2);
152 rate /= (double) n;
153 _mesa_printf("timed_memcpy: %u %u --> %f clocks/byte\n", t1, t2, rate);
154 return ret;
155 }
156 #endif /* DO_DEBUG */
157
158
159 void
160 intelInitTextureFuncs(struct dd_function_table *functions)
161 {
162 functions->ChooseTextureFormat = intelChooseTextureFormat;
163 functions->TexImage1D = intelTexImage1D;
164 functions->TexImage2D = intelTexImage2D;
165 functions->TexImage3D = intelTexImage3D;
166 functions->TexSubImage1D = intelTexSubImage1D;
167 functions->TexSubImage2D = intelTexSubImage2D;
168 functions->TexSubImage3D = intelTexSubImage3D;
169 functions->CopyTexImage1D = intelCopyTexImage1D;
170 functions->CopyTexImage2D = intelCopyTexImage2D;
171 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
172 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
173 functions->GetTexImage = intelGetTexImage;
174
175 /* compressed texture functions */
176 functions->CompressedTexImage2D = intelCompressedTexImage2D;
177 functions->GetCompressedTexImage = intelGetCompressedTexImage;
178
179 functions->NewTextureObject = intelNewTextureObject;
180 functions->NewTextureImage = intelNewTextureImage;
181 functions->DeleteTexture = intelDeleteTextureObject;
182 functions->FreeTexImageData = intelFreeTextureImageData;
183 functions->UpdateTexturePalette = 0;
184 functions->IsTextureResident = intelIsTextureResident;
185
186 #if DO_DEBUG
187 if (INTEL_DEBUG & DEBUG_BUFMGR)
188 functions->TextureMemCpy = timed_memcpy;
189 else
190 #endif
191 functions->TextureMemCpy = do_memcpy;
192 }