Merge branch 'mesa_7_5_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
357 void
358 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
359 GLuint level, GLuint img,
360 GLuint x, GLuint y)
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;
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
374 /* Although we use the image_offset[] array to store relative offsets
375 * to cube faces, Mesa doesn't know anything about this and expects
376 * each cube face to be treated as a separate image.
377 *
378 * These functions present that view to mesa:
379 */
380 const GLuint *
381 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
382 {
383 static const GLuint zero = 0;
384
385 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
386 return &zero;
387 else
388 return mt->level[level].image_offset;
389 }
390
391
392 GLuint
393 intel_miptree_image_offset(struct intel_mipmap_tree *mt,
394 GLuint face, GLuint level)
395 {
396 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
397 return (mt->level[level].level_offset +
398 mt->level[level].image_offset[face]);
399 else
400 return mt->level[level].level_offset;
401 }
402
403
404
405 /**
406 * Map a teximage in a mipmap tree.
407 * \param row_stride returns row stride in bytes
408 * \param image_stride returns image stride in bytes (for 3D textures).
409 * \param image_offsets pointer to array of pixel offsets from the returned
410 * pointer to each depth image
411 * \return address of mapping
412 */
413 GLubyte *
414 intel_miptree_image_map(struct intel_context * intel,
415 struct intel_mipmap_tree * mt,
416 GLuint face,
417 GLuint level,
418 GLuint * row_stride, GLuint * image_offsets)
419 {
420 DBG("%s \n", __FUNCTION__);
421
422 if (row_stride)
423 *row_stride = mt->pitch * mt->cpp;
424
425 if (mt->target == GL_TEXTURE_3D) {
426 int i;
427
428 for (i = 0; i < mt->level[level].depth; i++)
429 image_offsets[i] = mt->level[level].image_offset[i] / mt->cpp;
430 } else {
431 assert(mt->level[level].depth == 1);
432 assert(mt->target == GL_TEXTURE_CUBE_MAP ||
433 mt->level[level].image_offset[0] == 0);
434 image_offsets[0] = 0;
435 }
436
437 return (intel_region_map(intel, mt->region) +
438 intel_miptree_image_offset(mt, face, level));
439 }
440
441 void
442 intel_miptree_image_unmap(struct intel_context *intel,
443 struct intel_mipmap_tree *mt)
444 {
445 DBG("%s\n", __FUNCTION__);
446 intel_region_unmap(intel, mt->region);
447 }
448
449
450
451 /* Upload data for a particular image.
452 */
453 void
454 intel_miptree_image_data(struct intel_context *intel,
455 struct intel_mipmap_tree *dst,
456 GLuint face,
457 GLuint level,
458 void *src,
459 GLuint src_row_pitch,
460 GLuint src_image_pitch)
461 {
462 GLuint depth = dst->level[level].depth;
463 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
464 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
465 GLuint i;
466 GLuint height = 0;
467
468 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
469 for (i = 0; i < depth; i++) {
470 height = dst->level[level].height;
471 if(dst->compressed)
472 height = (height + 3) / 4;
473 intel_region_data(intel,
474 dst->region,
475 dst_offset + dst_depth_offset[i], /* dst_offset */
476 0, 0, /* dstx, dsty */
477 src,
478 src_row_pitch,
479 0, 0, /* source x, y */
480 dst->level[level].width, height); /* width, height */
481
482 src = (char *)src + src_image_pitch * dst->cpp;
483 }
484 }
485
486 extern GLuint intel_compressed_alignment(GLenum);
487 /* Copy mipmap image between trees
488 */
489 void
490 intel_miptree_image_copy(struct intel_context *intel,
491 struct intel_mipmap_tree *dst,
492 GLuint face, GLuint level,
493 struct intel_mipmap_tree *src)
494 {
495 GLuint width = src->level[level].width;
496 GLuint height = src->level[level].height;
497 GLuint depth = src->level[level].depth;
498 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
499 GLuint src_offset = intel_miptree_image_offset(src, face, level);
500 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
501 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
502 GLuint i;
503 GLboolean success;
504
505 if (dst->compressed) {
506 GLuint alignment = intel_compressed_alignment(dst->internal_format);
507 height = (height + 3) / 4;
508 width = ((width + alignment - 1) & ~(alignment - 1));
509 }
510
511 for (i = 0; i < depth; i++) {
512 success = intel_region_copy(intel,
513 dst->region, dst_offset + dst_depth_offset[i],
514 0, 0,
515 src->region, src_offset + src_depth_offset[i],
516 0, 0, width, height, GL_COPY);
517 if (!success) {
518 GLubyte *src_ptr, *dst_ptr;
519
520 src_ptr = intel_region_map(intel, src->region);
521 dst_ptr = intel_region_map(intel, dst->region);
522
523 _mesa_copy_rect(dst_ptr + dst_offset + dst_depth_offset[i],
524 dst->cpp,
525 dst->pitch,
526 0, 0, width, height,
527 src_ptr + src_offset + src_depth_offset[i],
528 src->pitch,
529 0, 0);
530 intel_region_unmap(intel, src->region);
531 intel_region_unmap(intel, dst->region);
532 }
533 }
534 }