1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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_tex.h"
33 #include "main/enums.h"
34 #include "main/formats.h"
36 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
40 target_to_target(GLenum target)
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
49 return GL_TEXTURE_CUBE_MAP_ARB;
56 static struct intel_mipmap_tree *
57 intel_miptree_create_internal(struct intel_context *intel,
68 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
69 int compress_byte = 0;
71 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
72 _mesa_lookup_enum_by_nr(target),
73 _mesa_get_format_name(format),
74 first_level, last_level, mt);
76 if (_mesa_is_format_compressed(format))
77 compress_byte = intel_compressed_num_bytes(format);
79 mt->target = target_to_target(target);
81 mt->first_level = first_level;
82 mt->last_level = last_level;
84 mt->height0 = height0;
86 mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
87 mt->compressed = compress_byte ? 1 : 0;
92 ok = i945_miptree_layout(intel, mt, tiling);
94 ok = i915_miptree_layout(intel, mt, tiling);
96 ok = brw_miptree_layout(intel, mt, tiling);
101 DBG("%s not okay - returning NULL\n", __FUNCTION__);
109 struct intel_mipmap_tree *
110 intel_miptree_create(struct intel_context *intel,
118 GLboolean expect_accelerated_upload)
120 struct intel_mipmap_tree *mt;
121 uint32_t tiling = I915_TILING_NONE;
122 GLenum base_format = _mesa_get_format_base_format(format);
124 if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
125 if (intel->gen >= 4 &&
126 (base_format == GL_DEPTH_COMPONENT ||
127 base_format == GL_DEPTH_STENCIL_EXT))
128 tiling = I915_TILING_Y;
129 else if (width0 >= 64)
130 tiling = I915_TILING_X;
133 mt = intel_miptree_create_internal(intel, target, format,
134 first_level, last_level, width0,
138 * pitch == 0 || height == 0 indicates the null texture
140 if (!mt || !mt->total_width || !mt->total_height) {
145 mt->region = intel_region_alloc(intel->intelScreen,
150 expect_accelerated_upload);
161 struct intel_mipmap_tree *
162 intel_miptree_create_for_region(struct intel_context *intel,
165 struct intel_region *region,
168 struct intel_mipmap_tree *mt;
170 mt = intel_miptree_create_internal(intel, target, format,
172 region->width, region->height, 1,
177 intel_region_reference(&mt->region, region);
183 intel_miptree_reference(struct intel_mipmap_tree **dst,
184 struct intel_mipmap_tree *src)
188 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
193 intel_miptree_release(struct intel_context *intel,
194 struct intel_mipmap_tree **mt)
199 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
200 if (--(*mt)->refcount <= 0) {
203 DBG("%s deleting %p\n", __FUNCTION__, *mt);
205 intel_region_release(&((*mt)->region));
206 intel_region_release(&((*mt)->hiz_region));
208 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
209 free((*mt)->level[i].x_offset);
210 free((*mt)->level[i].y_offset);
220 * Can the image be pulled into a unified mipmap tree? This mirrors
221 * the completeness test in a lot of ways.
223 * Not sure whether I want to pass gl_texture_image here.
226 intel_miptree_match_image(struct intel_mipmap_tree *mt,
227 struct gl_texture_image *image)
229 struct intel_texture_image *intelImage = intel_texture_image(image);
230 GLuint level = intelImage->level;
232 /* Images with borders are never pulled into mipmap trees. */
236 if (image->TexFormat != mt->format)
239 /* Test image dimensions against the base level image adjusted for
240 * minification. This will also catch images not present in the
241 * tree, changed targets, etc.
243 if (image->Width != mt->level[level].width ||
244 image->Height != mt->level[level].height ||
245 image->Depth != mt->level[level].depth)
253 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
257 GLuint w, GLuint h, GLuint d)
259 mt->level[level].width = w;
260 mt->level[level].height = h;
261 mt->level[level].depth = d;
262 mt->level[level].level_x = x;
263 mt->level[level].level_y = y;
264 mt->level[level].nr_images = nr_images;
266 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
267 level, w, h, d, x, y);
270 assert(!mt->level[level].x_offset);
272 mt->level[level].x_offset = malloc(nr_images * sizeof(GLuint));
273 mt->level[level].x_offset[0] = mt->level[level].level_x;
274 mt->level[level].y_offset = malloc(nr_images * sizeof(GLuint));
275 mt->level[level].y_offset[0] = mt->level[level].level_y;
280 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
281 GLuint level, GLuint img,
284 if (img == 0 && level == 0)
285 assert(x == 0 && y == 0);
287 assert(img < mt->level[level].nr_images);
289 mt->level[level].x_offset[img] = mt->level[level].level_x + x;
290 mt->level[level].y_offset[img] = mt->level[level].level_y + y;
292 DBG("%s level %d img %d pos %d,%d\n",
293 __FUNCTION__, level, img,
294 mt->level[level].x_offset[img], mt->level[level].y_offset[img]);
299 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
300 GLuint level, GLuint face, GLuint depth,
301 GLuint *x, GLuint *y)
303 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) {
304 *x = mt->level[level].x_offset[face];
305 *y = mt->level[level].y_offset[face];
306 } else if (mt->target == GL_TEXTURE_3D) {
307 *x = mt->level[level].x_offset[depth];
308 *y = mt->level[level].y_offset[depth];
310 *x = mt->level[level].x_offset[0];
311 *y = mt->level[level].y_offset[0];
316 * Map a teximage in a mipmap tree.
317 * \param row_stride returns row stride in bytes
318 * \param image_stride returns image stride in bytes (for 3D textures).
319 * \param image_offsets pointer to array of pixel offsets from the returned
320 * pointer to each depth image
321 * \return address of mapping
324 intel_miptree_image_map(struct intel_context * intel,
325 struct intel_mipmap_tree * mt,
328 GLuint * row_stride, GLuint * image_offsets)
333 *row_stride = mt->region->pitch * mt->cpp;
335 if (mt->target == GL_TEXTURE_3D) {
338 for (i = 0; i < mt->level[level].depth; i++) {
340 intel_miptree_get_image_offset(mt, level, face, i,
342 image_offsets[i] = x + y * mt->region->pitch;
345 DBG("%s \n", __FUNCTION__);
347 return intel_region_map(intel, mt->region);
349 assert(mt->level[level].depth == 1);
350 intel_miptree_get_image_offset(mt, level, face, 0,
352 image_offsets[0] = 0;
354 DBG("%s: (%d,%d) -> (%d, %d)/%d\n",
355 __FUNCTION__, face, level, x, y, mt->region->pitch * mt->cpp);
357 return intel_region_map(intel, mt->region) +
358 (x + y * mt->region->pitch) * mt->cpp;
364 intel_miptree_image_unmap(struct intel_context *intel,
365 struct intel_mipmap_tree *mt)
367 DBG("%s\n", __FUNCTION__);
368 intel_region_unmap(intel, mt->region);
373 * Upload data for a particular image.
376 intel_miptree_image_data(struct intel_context *intel,
377 struct intel_mipmap_tree *dst,
381 GLuint src_row_pitch,
382 GLuint src_image_pitch)
384 const GLuint depth = dst->level[level].depth;
387 for (i = 0; i < depth; i++) {
388 GLuint dst_x, dst_y, height, width;
390 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
392 height = dst->level[level].height;
393 width = dst->level[level].width;
394 if (dst->compressed) {
395 unsigned int align_w, align_h;
397 intel_get_texture_alignment_unit(dst->format, &align_w, &align_h);
398 height = (height + align_h - 1) / align_h;
399 width = ALIGN(width, align_w);
402 DBG("%s: %d/%d %p/%d -> (%d, %d)/%d (%d, %d)\n",
403 __FUNCTION__, face, level,
404 src, src_row_pitch * dst->cpp,
405 dst_x, dst_y, dst->region->pitch * dst->cpp,
408 intel_region_data(intel,
409 dst->region, 0, dst_x, dst_y,
412 0, 0, /* source x, y */
415 src = (char *)src + src_image_pitch * dst->cpp;
421 * Copy mipmap image between trees
424 intel_miptree_image_copy(struct intel_context *intel,
425 struct intel_mipmap_tree *dst,
426 GLuint face, GLuint level,
427 struct intel_mipmap_tree *src)
429 GLuint width = src->level[level].width;
430 GLuint height = src->level[level].height;
431 GLuint depth = src->level[level].depth;
432 GLuint src_x, src_y, dst_x, dst_y;
436 if (dst->compressed) {
437 GLuint align_w, align_h;
439 intel_get_texture_alignment_unit(dst->format, &align_w, &align_h);
440 height = (height + 3) / 4;
441 width = ALIGN(width, align_w);
444 intel_prepare_render(intel);
446 for (i = 0; i < depth; i++) {
447 intel_miptree_get_image_offset(src, level, face, i, &src_x, &src_y);
448 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
449 success = intel_region_copy(intel,
450 dst->region, 0, dst_x, dst_y,
451 src->region, 0, src_x, src_y,
452 width, height, GL_FALSE,
455 GLubyte *src_ptr, *dst_ptr;
457 src_ptr = intel_region_map(intel, src->region);
458 dst_ptr = intel_region_map(intel, dst->region);
460 _mesa_copy_rect(dst_ptr,
463 dst_x, dst_y, width, height,
467 intel_region_unmap(intel, src->region);
468 intel_region_unmap(intel, dst->region);