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