[intel] fix random ut2004 crash on some machine, for cubemap textures,
[mesa.git] / src / mesa / drivers / dri / intel / 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 "intel_chipset.h"
32 #include "enums.h"
33
34 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
35
36 static GLenum
37 target_to_target(GLenum target)
38 {
39 switch (target) {
40 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
41 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
42 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
46 return GL_TEXTURE_CUBE_MAP_ARB;
47 default:
48 return target;
49 }
50 }
51
52 static struct intel_mipmap_tree *
53 intel_miptree_create_internal(struct intel_context *intel,
54 GLenum target,
55 GLenum internal_format,
56 GLuint first_level,
57 GLuint last_level,
58 GLuint width0,
59 GLuint height0,
60 GLuint depth0, GLuint cpp, GLuint compress_byte)
61 {
62 GLboolean ok;
63 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
64
65 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
66 _mesa_lookup_enum_by_nr(target),
67 _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
68
69 mt->target = target_to_target(target);
70 mt->internal_format = internal_format;
71 mt->first_level = first_level;
72 mt->last_level = last_level;
73 mt->width0 = width0;
74 mt->height0 = height0;
75 mt->depth0 = depth0;
76 mt->cpp = compress_byte ? compress_byte : cpp;
77 mt->compressed = compress_byte ? 1 : 0;
78 mt->refcount = 1;
79 mt->pitch = 0;
80
81 #ifdef I915
82 if (IS_945(intel->intelScreen->deviceID))
83 ok = i945_miptree_layout(intel, mt);
84 else
85 ok = i915_miptree_layout(intel, mt);
86 #else
87 ok = brw_miptree_layout(intel, mt);
88 #endif
89
90 if (!ok) {
91 free(mt);
92 return NULL;
93 }
94
95 return mt;
96 }
97
98 struct intel_mipmap_tree *
99 intel_miptree_create(struct intel_context *intel,
100 GLenum target,
101 GLenum internal_format,
102 GLuint first_level,
103 GLuint last_level,
104 GLuint width0,
105 GLuint height0,
106 GLuint depth0, GLuint cpp, GLuint compress_byte)
107 {
108 struct intel_mipmap_tree *mt;
109
110 mt = intel_miptree_create_internal(intel, target, internal_format,
111 first_level, last_level, width0,
112 height0, depth0, cpp, compress_byte);
113 if (!mt)
114 return NULL;
115
116 assert (mt->pitch);
117 mt->region = intel_region_alloc(intel,
118 mt->cpp, mt->pitch, mt->total_height);
119
120 if (!mt->region) {
121 free(mt);
122 return NULL;
123 }
124
125 return mt;
126 }
127
128 struct intel_mipmap_tree *
129 intel_miptree_create_for_region(struct intel_context *intel,
130 GLenum target,
131 GLenum internal_format,
132 GLuint first_level,
133 GLuint last_level,
134 struct intel_region *region,
135 GLuint depth0,
136 GLuint compress_byte)
137 {
138 struct intel_mipmap_tree *mt;
139
140 mt = intel_miptree_create_internal(intel, target, internal_format,
141 first_level, last_level,
142 region->pitch, region->height, depth0,
143 region->cpp, compress_byte);
144 if (!mt)
145 return mt;
146 #if 0
147 if (mt->pitch != region->pitch) {
148 fprintf(stderr,
149 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
150 region->pitch, mt->pitch);
151 free(mt);
152 return NULL;
153 }
154 #else
155 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
156 * to texture works, but we don't need that for texturing from a
157 * pixmap. Just override it here. */
158 mt->pitch = region->pitch;
159 #endif
160
161 mt->region = region;
162
163 return mt;
164 }
165
166 /**
167 * intel_miptree_pitch_align:
168 *
169 * @intel: intel context pointer
170 *
171 * @mt: the miptree to compute pitch alignment for
172 *
173 * @pitch: the natural pitch value
174 *
175 * Given @pitch, compute a larger value which accounts for
176 * any necessary alignment required by the device
177 */
178
179 int intel_miptree_pitch_align (struct intel_context *intel,
180 struct intel_mipmap_tree *mt,
181 int pitch)
182 {
183 if (!mt->compressed) {
184 int pitch_align;
185
186 if (intel->ttm) {
187 /* XXX: Align pitch to multiple of 64 bytes for now to allow
188 * render-to-texture to work in all cases. This should probably be
189 * replaced at some point by some scheme to only do this when really
190 * necessary.
191 */
192 pitch_align = 64;
193 } else {
194 pitch_align = 4;
195 }
196
197 pitch = ALIGN(pitch * mt->cpp, pitch_align);
198
199 #ifdef I915
200 /* XXX: At least the i915 seems very upset when the pitch is a multiple
201 * of 1024 and sometimes 512 bytes - performance can drop by several
202 * times. Go to the next multiple of the required alignment for now.
203 */
204 if (!(pitch & 511))
205 pitch += pitch_align;
206 #endif
207
208 pitch /= mt->cpp;
209 }
210 return pitch;
211 }
212
213 void
214 intel_miptree_reference(struct intel_mipmap_tree **dst,
215 struct intel_mipmap_tree *src)
216 {
217 src->refcount++;
218 *dst = src;
219 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
220 }
221
222 void
223 intel_miptree_release(struct intel_context *intel,
224 struct intel_mipmap_tree **mt)
225 {
226 if (!*mt)
227 return;
228
229 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
230 if (--(*mt)->refcount <= 0) {
231 GLuint i;
232
233 DBG("%s deleting %p\n", __FUNCTION__, *mt);
234
235 intel_region_release(&((*mt)->region));
236
237 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
238 if ((*mt)->level[i].image_offset)
239 free((*mt)->level[i].image_offset);
240
241 free(*mt);
242 }
243 *mt = NULL;
244 }
245
246
247
248
249 /* Can the image be pulled into a unified mipmap tree. This mirrors
250 * the completeness test in a lot of ways.
251 *
252 * Not sure whether I want to pass gl_texture_image here.
253 */
254 GLboolean
255 intel_miptree_match_image(struct intel_mipmap_tree *mt,
256 struct gl_texture_image *image,
257 GLuint face, GLuint level)
258 {
259 /* Images with borders are never pulled into mipmap trees.
260 */
261 if (image->Border)
262 return GL_FALSE;
263
264 if (image->InternalFormat != mt->internal_format ||
265 image->IsCompressed != mt->compressed)
266 return GL_FALSE;
267
268 /* Test image dimensions against the base level image adjusted for
269 * minification. This will also catch images not present in the
270 * tree, changed targets, etc.
271 */
272 if (image->Width != mt->level[level].width ||
273 image->Height != mt->level[level].height ||
274 image->Depth != mt->level[level].depth)
275 return GL_FALSE;
276
277 return GL_TRUE;
278 }
279
280
281 void
282 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
283 GLuint level,
284 GLuint nr_images,
285 GLuint x, GLuint y,
286 GLuint w, GLuint h, GLuint d)
287 {
288 mt->level[level].width = w;
289 mt->level[level].height = h;
290 mt->level[level].depth = d;
291 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
292 mt->level[level].nr_images = nr_images;
293
294 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
295 level, w, h, d, x, y, mt->level[level].level_offset);
296
297 /* Not sure when this would happen, but anyway:
298 */
299 if (mt->level[level].image_offset) {
300 free(mt->level[level].image_offset);
301 mt->level[level].image_offset = NULL;
302 }
303
304 assert(nr_images);
305
306 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
307 mt->level[level].image_offset[0] = 0;
308 }
309
310
311
312 void
313 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
314 GLuint level, GLuint img,
315 GLuint x, GLuint y)
316 {
317 if (img == 0 && level == 0)
318 assert(x == 0 && y == 0);
319
320 assert(img < mt->level[level].nr_images);
321
322 mt->level[level].image_offset[img] = (x + y * mt->pitch);
323
324 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
325 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
326 }
327
328
329 /* Although we use the image_offset[] array to store relative offsets
330 * to cube faces, Mesa doesn't know anything about this and expects
331 * each cube face to be treated as a separate image.
332 *
333 * These functions present that view to mesa:
334 */
335 const GLuint *
336 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
337 {
338 static const GLuint zero = 0;
339
340 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
341 return &zero;
342 else
343 return mt->level[level].image_offset;
344 }
345
346
347 GLuint
348 intel_miptree_image_offset(struct intel_mipmap_tree *mt,
349 GLuint face, GLuint level)
350 {
351 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
352 return (mt->level[level].level_offset +
353 mt->level[level].image_offset[face] * mt->cpp);
354 else
355 return mt->level[level].level_offset;
356 }
357
358
359
360 /**
361 * Map a teximage in a mipmap tree.
362 * \param row_stride returns row stride in bytes
363 * \param image_stride returns image stride in bytes (for 3D textures).
364 * \return address of mapping
365 */
366 GLubyte *
367 intel_miptree_image_map(struct intel_context * intel,
368 struct intel_mipmap_tree * mt,
369 GLuint face,
370 GLuint level,
371 GLuint * row_stride, GLuint * image_offsets)
372 {
373 DBG("%s \n", __FUNCTION__);
374
375 if (row_stride)
376 *row_stride = mt->pitch * mt->cpp;
377
378 if (image_offsets) {
379 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
380 memset(image_offsets, 0, mt->level[level].depth * sizeof(GLuint));
381 else
382 memcpy(image_offsets, mt->level[level].image_offset,
383 mt->level[level].depth * sizeof(GLuint));
384 }
385
386 return (intel_region_map(intel, mt->region) +
387 intel_miptree_image_offset(mt, face, level));
388 }
389
390 void
391 intel_miptree_image_unmap(struct intel_context *intel,
392 struct intel_mipmap_tree *mt)
393 {
394 DBG("%s\n", __FUNCTION__);
395 intel_region_unmap(intel, mt->region);
396 }
397
398
399
400 /* Upload data for a particular image.
401 */
402 void
403 intel_miptree_image_data(struct intel_context *intel,
404 struct intel_mipmap_tree *dst,
405 GLuint face,
406 GLuint level,
407 void *src,
408 GLuint src_row_pitch,
409 GLuint src_image_pitch)
410 {
411 GLuint depth = dst->level[level].depth;
412 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
413 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
414 GLuint i;
415 GLuint height = 0;
416
417 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
418 for (i = 0; i < depth; i++) {
419 height = dst->level[level].height;
420 if(dst->compressed)
421 height /= 4;
422 intel_region_data(intel,
423 dst->region,
424 dst_offset + dst_depth_offset[i] * dst->cpp, /* dst_offset */
425 0, 0, /* dstx, dsty */
426 src,
427 src_row_pitch,
428 0, 0, /* source x, y */
429 dst->level[level].width, height); /* width, height */
430
431 src += src_image_pitch * dst->cpp;
432 }
433 }
434
435 extern GLuint intel_compressed_alignment(GLenum);
436 /* Copy mipmap image between trees
437 */
438 void
439 intel_miptree_image_copy(struct intel_context *intel,
440 struct intel_mipmap_tree *dst,
441 GLuint face, GLuint level,
442 struct intel_mipmap_tree *src)
443 {
444 GLuint width = src->level[level].width;
445 GLuint height = src->level[level].height;
446 GLuint depth = src->level[level].depth;
447 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
448 GLuint src_offset = intel_miptree_image_offset(src, face, level);
449 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
450 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
451 GLuint i;
452
453 if (dst->compressed) {
454 GLuint alignment = intel_compressed_alignment(dst->internal_format);
455 height = (height + 3) / 4;
456 width = ((width + alignment - 1) & ~(alignment - 1));
457 }
458
459 for (i = 0; i < depth; i++) {
460 intel_region_copy(intel,
461 dst->region, dst_offset + dst_depth_offset[i] * dst->cpp,
462 0,
463 0,
464 src->region, src_offset + src_depth_offset[i] * src->cpp,
465 0, 0, width, height);
466 }
467
468 }