Merge remote branch 'origin/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 return NULL;
143
144 mt->region = intel_region_alloc(intel,
145 tiling,
146 mt->cpp,
147 mt->pitch,
148 mt->total_height,
149 mt->pitch,
150 expect_accelerated_upload);
151
152 if (!mt->region) {
153 free(mt);
154 return NULL;
155 }
156
157 return mt;
158 }
159
160
161 struct intel_mipmap_tree *
162 intel_miptree_create_for_region(struct intel_context *intel,
163 GLenum target,
164 GLenum internal_format,
165 GLuint first_level,
166 GLuint last_level,
167 struct intel_region *region,
168 GLuint depth0,
169 GLuint compress_byte)
170 {
171 struct intel_mipmap_tree *mt;
172
173 mt = intel_miptree_create_internal(intel, target, internal_format,
174 first_level, last_level,
175 region->width, region->height, 1,
176 region->cpp, compress_byte,
177 I915_TILING_NONE);
178 if (!mt)
179 return mt;
180 #if 0
181 if (mt->pitch != region->pitch) {
182 fprintf(stderr,
183 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
184 region->pitch, mt->pitch);
185 free(mt);
186 return NULL;
187 }
188 #else
189 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
190 * to texture works, but we don't need that for texturing from a
191 * pixmap. Just override it here. */
192 mt->pitch = region->pitch;
193 #endif
194
195 intel_region_reference(&mt->region, region);
196
197 return mt;
198 }
199
200
201 /**
202 * intel_miptree_pitch_align:
203 *
204 * @intel: intel context pointer
205 *
206 * @mt: the miptree to compute pitch alignment for
207 *
208 * @pitch: the natural pitch value
209 *
210 * Given @pitch, compute a larger value which accounts for
211 * any necessary alignment required by the device
212 */
213 int intel_miptree_pitch_align (struct intel_context *intel,
214 struct intel_mipmap_tree *mt,
215 uint32_t tiling,
216 int pitch)
217 {
218 #ifdef I915
219 GLcontext *ctx = &intel->ctx;
220 #endif
221
222 if (!mt->compressed) {
223 int pitch_align;
224
225 if (intel->ttm) {
226 /* XXX: Align pitch to multiple of 64 bytes for now to allow
227 * render-to-texture to work in all cases. This should probably be
228 * replaced at some point by some scheme to only do this when really
229 * necessary.
230 */
231 pitch_align = 64;
232 } else {
233 pitch_align = 4;
234 }
235
236 if (tiling == I915_TILING_X)
237 pitch_align = 512;
238 else if (tiling == I915_TILING_Y)
239 pitch_align = 128;
240
241 pitch = ALIGN(pitch * mt->cpp, pitch_align);
242
243 #ifdef I915
244 /* XXX: At least the i915 seems very upset when the pitch is a multiple
245 * of 1024 and sometimes 512 bytes - performance can drop by several
246 * times. Go to the next multiple of the required alignment for now.
247 */
248 if (!(pitch & 511) &&
249 (pitch + pitch_align) < (1 << ctx->Const.MaxTextureLevels))
250 pitch += pitch_align;
251 #endif
252
253 pitch /= mt->cpp;
254 }
255 return pitch;
256 }
257
258
259 void
260 intel_miptree_reference(struct intel_mipmap_tree **dst,
261 struct intel_mipmap_tree *src)
262 {
263 src->refcount++;
264 *dst = src;
265 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
266 }
267
268
269 void
270 intel_miptree_release(struct intel_context *intel,
271 struct intel_mipmap_tree **mt)
272 {
273 if (!*mt)
274 return;
275
276 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
277 if (--(*mt)->refcount <= 0) {
278 GLuint i;
279
280 DBG("%s deleting %p\n", __FUNCTION__, *mt);
281
282 #ifndef I915
283 /* Free up cached binding tables holding a reference on our buffer, to
284 * avoid excessive memory consumption.
285 *
286 * This isn't as aggressive as we could be, as we'd like to do
287 * it from any time we free the last ref on a region. But intel_region.c
288 * is context-agnostic. Perhaps our constant state cache should be, as
289 * well.
290 */
291 brw_state_cache_bo_delete(&brw_context(&intel->ctx)->surface_cache,
292 (*mt)->region->buffer);
293 #endif
294
295 intel_region_release(&((*mt)->region));
296
297 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
298 free((*mt)->level[i].x_offset);
299 free((*mt)->level[i].y_offset);
300 }
301
302 free(*mt);
303 }
304 *mt = NULL;
305 }
306
307
308 /**
309 * Can the image be pulled into a unified mipmap tree? This mirrors
310 * the completeness test in a lot of ways.
311 *
312 * Not sure whether I want to pass gl_texture_image here.
313 */
314 GLboolean
315 intel_miptree_match_image(struct intel_mipmap_tree *mt,
316 struct gl_texture_image *image,
317 GLuint face, GLuint level)
318 {
319 /* Images with borders are never pulled into mipmap trees.
320 */
321 if (image->Border ||
322 ((image->_BaseFormat == GL_DEPTH_COMPONENT) &&
323 ((image->TexObject->WrapS == GL_CLAMP_TO_BORDER) ||
324 (image->TexObject->WrapT == GL_CLAMP_TO_BORDER))))
325 return GL_FALSE;
326
327 if (image->InternalFormat != mt->internal_format ||
328 image->IsCompressed != mt->compressed)
329 return GL_FALSE;
330
331 if (!image->IsCompressed &&
332 !mt->compressed &&
333 image->TexFormat->TexelBytes != mt->cpp)
334 return GL_FALSE;
335
336 /* Test image dimensions against the base level image adjusted for
337 * minification. This will also catch images not present in the
338 * tree, changed targets, etc.
339 */
340 if (image->Width != mt->level[level].width ||
341 image->Height != mt->level[level].height ||
342 image->Depth != mt->level[level].depth)
343 return GL_FALSE;
344
345 return GL_TRUE;
346 }
347
348
349 void
350 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
351 GLuint level,
352 GLuint nr_images,
353 GLuint x, GLuint y,
354 GLuint w, GLuint h, GLuint d)
355 {
356 mt->level[level].width = w;
357 mt->level[level].height = h;
358 mt->level[level].depth = d;
359 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
360 mt->level[level].level_x = x;
361 mt->level[level].level_y = y;
362 mt->level[level].nr_images = nr_images;
363
364 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
365 level, w, h, d, x, y, mt->level[level].level_offset);
366
367 assert(nr_images);
368 assert(!mt->level[level].x_offset);
369
370 mt->level[level].x_offset = malloc(nr_images * sizeof(GLuint));
371 mt->level[level].x_offset[0] = mt->level[level].level_x;
372 mt->level[level].y_offset = malloc(nr_images * sizeof(GLuint));
373 mt->level[level].y_offset[0] = mt->level[level].level_y;
374 }
375
376
377 void
378 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
379 GLuint level, GLuint img,
380 GLuint x, GLuint y)
381 {
382 if (img == 0 && level == 0)
383 assert(x == 0 && y == 0);
384
385 assert(img < mt->level[level].nr_images);
386
387 mt->level[level].x_offset[img] = mt->level[level].level_x + x;
388 mt->level[level].y_offset[img] = mt->level[level].level_y + y;
389
390 DBG("%s level %d img %d pos %d,%d\n",
391 __FUNCTION__, level, img,
392 mt->level[level].x_offset[img], mt->level[level].y_offset[img]);
393 }
394
395
396 void
397 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
398 GLuint level, GLuint face, GLuint depth,
399 GLuint *x, GLuint *y)
400 {
401 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) {
402 *x = mt->level[level].x_offset[face];
403 *y = mt->level[level].y_offset[face];
404 } else if (mt->target == GL_TEXTURE_3D) {
405 *x = mt->level[level].x_offset[depth];
406 *y = mt->level[level].y_offset[depth];
407 } else {
408 *x = mt->level[level].x_offset[0];
409 *y = mt->level[level].y_offset[0];
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 GLuint x, y;
429 DBG("%s \n", __FUNCTION__);
430
431 if (row_stride)
432 *row_stride = mt->pitch * mt->cpp;
433
434 if (mt->target == GL_TEXTURE_3D) {
435 int i;
436
437 for (i = 0; i < mt->level[level].depth; i++) {
438
439 intel_miptree_get_image_offset(mt, level, face, i,
440 &x, &y);
441 image_offsets[i] = x + y * mt->pitch;
442 }
443
444 return intel_region_map(intel, mt->region);
445 } else {
446 assert(mt->level[level].depth == 1);
447 intel_miptree_get_image_offset(mt, level, face, 0,
448 &x, &y);
449 image_offsets[0] = 0;
450
451 return intel_region_map(intel, mt->region) +
452 (x + y * mt->pitch) * mt->cpp;
453 }
454 }
455
456
457 void
458 intel_miptree_image_unmap(struct intel_context *intel,
459 struct intel_mipmap_tree *mt)
460 {
461 DBG("%s\n", __FUNCTION__);
462 intel_region_unmap(intel, mt->region);
463 }
464
465
466 /**
467 * Upload data for a particular image.
468 */
469 void
470 intel_miptree_image_data(struct intel_context *intel,
471 struct intel_mipmap_tree *dst,
472 GLuint face,
473 GLuint level,
474 void *src,
475 GLuint src_row_pitch,
476 GLuint src_image_pitch)
477 {
478 const GLuint depth = dst->level[level].depth;
479 GLuint i;
480
481 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
482 for (i = 0; i < depth; i++) {
483 GLuint dst_x, dst_y, height;
484
485 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
486
487 height = dst->level[level].height;
488 if(dst->compressed)
489 height = (height + 3) / 4;
490
491 intel_region_data(intel,
492 dst->region, 0, dst_x, dst_y,
493 src,
494 src_row_pitch,
495 0, 0, /* source x, y */
496 dst->level[level].width, height); /* width, height */
497
498 src = (char *)src + src_image_pitch * dst->cpp;
499 }
500 }
501
502
503 /**
504 * Copy mipmap image between trees
505 */
506 void
507 intel_miptree_image_copy(struct intel_context *intel,
508 struct intel_mipmap_tree *dst,
509 GLuint face, GLuint level,
510 struct intel_mipmap_tree *src)
511 {
512 GLuint width = src->level[level].width;
513 GLuint height = src->level[level].height;
514 GLuint depth = src->level[level].depth;
515 GLuint src_x, src_y, dst_x, dst_y;
516 GLuint i;
517 GLboolean success;
518
519 if (dst->compressed) {
520 GLuint align_w, align_h;
521
522 intel_get_texture_alignment_unit(dst->internal_format,
523 &align_w, &align_h);
524 height = (height + 3) / 4;
525 width = ALIGN(width, align_w);
526 }
527
528 for (i = 0; i < depth; i++) {
529 intel_miptree_get_image_offset(src, level, face, i, &src_x, &src_y);
530 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
531 success = intel_region_copy(intel,
532 dst->region, 0, dst_x, dst_y,
533 src->region, 0, src_x, src_y, width, height,
534 GL_COPY);
535 if (!success) {
536 GLubyte *src_ptr, *dst_ptr;
537
538 src_ptr = intel_region_map(intel, src->region);
539 dst_ptr = intel_region_map(intel, dst->region);
540
541 _mesa_copy_rect(dst_ptr + dst->cpp * (dst_x + dst_y * dst->pitch),
542 dst->cpp,
543 dst->pitch,
544 0, 0, width, height,
545 src_ptr + src->cpp * (src_x + src_y * src->pitch),
546 src->pitch,
547 0, 0);
548 intel_region_unmap(intel, src->region);
549 intel_region_unmap(intel, dst->region);
550 }
551 }
552 }