Drop GLcontext typedef and use struct gl_context instead
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex.c
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
12 static struct gl_texture_image *
13 intelNewTextureImage(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
21 static struct gl_texture_object *
22 intelNewTextureObject(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
32 static void
33 intelDeleteTextureObject(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
46 static void
47 intelFreeTextureImageData(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 */
77 static void *
78 do_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__
91 static unsigned
92 fastrdtsc(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
105 static unsigned
106 fastrdtsc(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
117 static unsigned
118 time_diff(unsigned t, unsigned t2)
119 {
120 return ((t < t2) ? t2 - t : 0xFFFFFFFFU - (t - t2 - 1));
121 }
122
123
124 static void *
125 timed_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 */
152 static void
153 intelGenerateMipmap(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 if (INTEL_DEBUG & DEBUG_FALLBACKS)
162 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
163
164 intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
165 _mesa_generate_mipmap(ctx, target, texObj);
166 intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
167
168 {
169 GLuint nr_faces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
170 GLuint face, i;
171 /* Update the level information in our private data in the new images,
172 * since it didn't get set as part of a normal TexImage path.
173 */
174 for (face = 0; face < nr_faces; face++) {
175 for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
176 struct intel_texture_image *intelImage =
177 intel_texture_image(texObj->Image[face][i]);
178 if (!intelImage)
179 break;
180 intelImage->level = i;
181 intelImage->face = face;
182 /* Unreference the miptree to signal that the new Data is a
183 * bare pointer from mesa.
184 */
185 intel_miptree_release(intel, &intelImage->mt);
186 }
187 }
188 }
189 }
190 else {
191 _mesa_meta_GenerateMipmap(ctx, target, texObj);
192 }
193 }
194
195
196 void
197 intelInitTextureFuncs(struct dd_function_table *functions)
198 {
199 functions->ChooseTextureFormat = intelChooseTextureFormat;
200 functions->GenerateMipmap = intelGenerateMipmap;
201
202 functions->NewTextureObject = intelNewTextureObject;
203 functions->NewTextureImage = intelNewTextureImage;
204 functions->DeleteTexture = intelDeleteTextureObject;
205 functions->FreeTexImageData = intelFreeTextureImageData;
206
207 #if DO_DEBUG && !defined(__ia64__)
208 if (INTEL_DEBUG & DEBUG_BUFMGR)
209 functions->TextureMemCpy = timed_memcpy;
210 else
211 #endif
212 functions->TextureMemCpy = do_memcpy;
213 }