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