don't use driDrawable information directly, don't resize at makecurrent
[mesa.git] / src / mesa / drivers / dri / i915tex / intel_mipmap_tree.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_context.h"
29 #include "intel_mipmap_tree.h"
30 #include "intel_regions.h"
31 #include "enums.h"
32
33 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
34
35 static GLenum
36 target_to_target(GLenum target)
37 {
38 switch (target) {
39 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
40 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
41 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
42 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
45 return GL_TEXTURE_CUBE_MAP_ARB;
46 default:
47 return target;
48 }
49 }
50
51 struct intel_mipmap_tree *
52 intel_miptree_create(struct intel_context *intel,
53 GLenum target,
54 GLenum internal_format,
55 GLuint first_level,
56 GLuint last_level,
57 GLuint width0,
58 GLuint height0,
59 GLuint depth0, GLuint cpp, GLuint compress_byte)
60 {
61 GLboolean ok;
62 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
63
64 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
65 _mesa_lookup_enum_by_nr(target),
66 _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
67
68 mt->target = target_to_target(target);
69 mt->internal_format = internal_format;
70 mt->first_level = first_level;
71 mt->last_level = last_level;
72 mt->width0 = width0;
73 mt->height0 = height0;
74 mt->depth0 = depth0;
75 mt->cpp = compress_byte ? compress_byte : cpp;
76 mt->compressed = compress_byte ? 1 : 0;
77 mt->refcount = 1;
78
79 switch (intel->intelScreen->deviceID) {
80 case PCI_CHIP_I945_G:
81 case PCI_CHIP_I945_GM:
82 case PCI_CHIP_I945_GME:
83 case PCI_CHIP_G33_G:
84 case PCI_CHIP_Q33_G:
85 case PCI_CHIP_Q35_G:
86 ok = i945_miptree_layout(mt);
87 break;
88 case PCI_CHIP_I915_G:
89 case PCI_CHIP_I915_GM:
90 case PCI_CHIP_I830_M:
91 case PCI_CHIP_I855_GM:
92 case PCI_CHIP_I865_G:
93 default:
94 /* All the i830 chips and the i915 use this layout:
95 */
96 ok = i915_miptree_layout(mt);
97 break;
98 }
99
100 if (ok)
101 mt->region = intel_region_alloc(intel->intelScreen,
102 mt->cpp, mt->pitch, mt->total_height);
103
104 if (!mt->region) {
105 free(mt);
106 return NULL;
107 }
108
109 return mt;
110 }
111
112
113 void
114 intel_miptree_reference(struct intel_mipmap_tree **dst,
115 struct intel_mipmap_tree *src)
116 {
117 src->refcount++;
118 *dst = src;
119 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
120 }
121
122 void
123 intel_miptree_release(struct intel_context *intel,
124 struct intel_mipmap_tree **mt)
125 {
126 if (!*mt)
127 return;
128
129 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
130 if (--(*mt)->refcount <= 0) {
131 GLuint i;
132
133 DBG("%s deleting %p\n", __FUNCTION__, *mt);
134
135 intel_region_release(&((*mt)->region));
136
137 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
138 if ((*mt)->level[i].image_offset)
139 free((*mt)->level[i].image_offset);
140
141 free(*mt);
142 }
143 *mt = NULL;
144 }
145
146
147
148
149 /* Can the image be pulled into a unified mipmap tree. This mirrors
150 * the completeness test in a lot of ways.
151 *
152 * Not sure whether I want to pass gl_texture_image here.
153 */
154 GLboolean
155 intel_miptree_match_image(struct intel_mipmap_tree *mt,
156 struct gl_texture_image *image,
157 GLuint face, GLuint level)
158 {
159 /* Images with borders are never pulled into mipmap trees.
160 */
161 if (image->Border)
162 return GL_FALSE;
163
164 if (image->InternalFormat != mt->internal_format ||
165 image->IsCompressed != mt->compressed)
166 return GL_FALSE;
167
168 /* Test image dimensions against the base level image adjusted for
169 * minification. This will also catch images not present in the
170 * tree, changed targets, etc.
171 */
172 if (image->Width != mt->level[level].width ||
173 image->Height != mt->level[level].height ||
174 image->Depth != mt->level[level].depth)
175 return GL_FALSE;
176
177 return GL_TRUE;
178 }
179
180
181 void
182 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
183 GLuint level,
184 GLuint nr_images,
185 GLuint x, GLuint y, GLuint w, GLuint h, GLuint d)
186 {
187
188 mt->level[level].width = w;
189 mt->level[level].height = h;
190 mt->level[level].depth = d;
191 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
192 mt->level[level].nr_images = nr_images;
193
194 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
195 level, w, h, d, x, y, mt->level[level].level_offset);
196
197 /* Not sure when this would happen, but anyway:
198 */
199 if (mt->level[level].image_offset) {
200 free(mt->level[level].image_offset);
201 mt->level[level].image_offset = NULL;
202 }
203
204 assert(nr_images);
205
206 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
207 mt->level[level].image_offset[0] = 0;
208 }
209
210
211
212 void
213 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
214 GLuint level, GLuint img, GLuint x, GLuint y)
215 {
216 if (img == 0 && level == 0)
217 assert(x == 0 && y == 0);
218
219 assert(img < mt->level[level].nr_images);
220
221 mt->level[level].image_offset[img] = (x + y * mt->pitch);
222
223 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
224 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
225 }
226
227
228 /* Although we use the image_offset[] array to store relative offsets
229 * to cube faces, Mesa doesn't know anything about this and expects
230 * each cube face to be treated as a separate image.
231 *
232 * These functions present that view to mesa:
233 */
234 const GLuint *
235 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
236 {
237 static const GLuint zero = 0;
238
239 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
240 return &zero;
241 else
242 return mt->level[level].image_offset;
243 }
244
245
246 GLuint
247 intel_miptree_image_offset(struct intel_mipmap_tree * mt,
248 GLuint face, GLuint level)
249 {
250 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
251 return (mt->level[level].level_offset +
252 mt->level[level].image_offset[face] * mt->cpp);
253 else
254 return mt->level[level].level_offset;
255 }
256
257
258
259 /**
260 * Map a teximage in a mipmap tree.
261 * \param row_stride returns row stride in bytes
262 * \param image_stride returns image stride in bytes (for 3D textures).
263 * \return address of mapping
264 */
265 GLubyte *
266 intel_miptree_image_map(struct intel_context * intel,
267 struct intel_mipmap_tree * mt,
268 GLuint face,
269 GLuint level,
270 GLuint * row_stride, GLuint * image_offsets)
271 {
272 DBG("%s \n", __FUNCTION__);
273
274 if (row_stride)
275 *row_stride = mt->pitch * mt->cpp;
276
277 if (image_offsets)
278 memcpy(image_offsets, mt->level[level].image_offset,
279 mt->level[level].depth * sizeof(GLuint));
280
281 return (intel_region_map(intel->intelScreen, mt->region) +
282 intel_miptree_image_offset(mt, face, level));
283 }
284
285 void
286 intel_miptree_image_unmap(struct intel_context *intel,
287 struct intel_mipmap_tree *mt)
288 {
289 DBG("%s\n", __FUNCTION__);
290 intel_region_unmap(intel->intelScreen, mt->region);
291 }
292
293
294
295 /* Upload data for a particular image.
296 */
297 void
298 intel_miptree_image_data(struct intel_context *intel,
299 struct intel_mipmap_tree *dst,
300 GLuint face,
301 GLuint level,
302 void *src,
303 GLuint src_row_pitch, GLuint src_image_pitch)
304 {
305 GLuint depth = dst->level[level].depth;
306 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
307 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
308 GLuint i;
309 GLuint height = 0;
310
311 DBG("%s\n", __FUNCTION__);
312 for (i = 0; i < depth; i++) {
313 height = dst->level[level].height;
314 if(dst->compressed)
315 height /= 4;
316 intel_region_data(intel->intelScreen, dst->region,
317 dst_offset + dst_depth_offset[i], /* dst_offset */
318 0, 0, /* dstx, dsty */
319 src,
320 src_row_pitch,
321 0, 0, /* source x, y */
322 dst->level[level].width, height); /* width, height */
323
324 src += src_image_pitch * dst->cpp;
325 }
326 }
327
328 /* Copy mipmap image between trees
329 */
330 void
331 intel_miptree_image_copy(struct intel_context *intel,
332 struct intel_mipmap_tree *dst,
333 GLuint face, GLuint level,
334 struct intel_mipmap_tree *src)
335 {
336 GLuint width = src->level[level].width;
337 GLuint height = src->level[level].height;
338 GLuint depth = src->level[level].depth;
339 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
340 GLuint src_offset = intel_miptree_image_offset(src, face, level);
341 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
342 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
343 GLuint i;
344
345 if (dst->compressed)
346 height /= 4;
347 for (i = 0; i < depth; i++) {
348 intel_region_copy(intel->intelScreen,
349 dst->region, dst_offset + dst_depth_offset[i],
350 0,
351 0,
352 src->region, src_offset + src_depth_offset[i],
353 0, 0, width, height);
354 }
355
356 }