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_chipset.h"
32 #include "main/enums.h"
34 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
37 target_to_target(GLenum 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
;
52 static struct intel_mipmap_tree
*
53 intel_miptree_create_internal(struct intel_context
*intel
,
55 GLenum internal_format
,
60 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
64 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
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
);
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
;
76 mt
->height0
= height0
;
78 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
79 mt
->compressed
= compress_byte
? 1 : 0;
84 if (IS_945(intel
->intelScreen
->deviceID
))
85 ok
= i945_miptree_layout(intel
, mt
, tiling
);
87 ok
= i915_miptree_layout(intel
, mt
, tiling
);
89 ok
= brw_miptree_layout(intel
, mt
, tiling
);
94 DBG("%s not okay - returning NULL\n", __FUNCTION__
);
101 struct intel_mipmap_tree
*
102 intel_miptree_create(struct intel_context
*intel
,
105 GLenum internal_format
,
110 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
111 GLboolean expect_accelerated_upload
)
113 struct intel_mipmap_tree
*mt
;
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
;
123 tiling
= I915_TILING_X
;
125 tiling
= I915_TILING_NONE
;
127 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
128 first_level
, last_level
, width0
,
129 height0
, depth0
, cpp
, compress_byte
,
132 * pitch == 0 || height == 0 indicates the null texture
134 if (!mt
|| !mt
->pitch
|| !mt
->total_height
)
137 mt
->region
= intel_region_alloc(intel
,
143 expect_accelerated_upload
);
153 struct intel_mipmap_tree
*
154 intel_miptree_create_for_region(struct intel_context
*intel
,
156 GLenum internal_format
,
159 struct intel_region
*region
,
161 GLuint compress_byte
)
163 struct intel_mipmap_tree
*mt
;
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
,
173 if (mt
->pitch
!= region
->pitch
) {
175 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
176 region
->pitch
, mt
->pitch
);
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
;
187 intel_region_reference(&mt
->region
, region
);
193 * intel_miptree_pitch_align:
195 * @intel: intel context pointer
197 * @mt: the miptree to compute pitch alignment for
199 * @pitch: the natural pitch value
201 * Given @pitch, compute a larger value which accounts for
202 * any necessary alignment required by the device
205 int intel_miptree_pitch_align (struct intel_context
*intel
,
206 struct intel_mipmap_tree
*mt
,
211 GLcontext
*ctx
= &intel
->ctx
;
214 if (!mt
->compressed
) {
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
228 if (tiling
== I915_TILING_X
)
230 else if (tiling
== I915_TILING_Y
)
233 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
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.
240 if (!(pitch
& 511) &&
241 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
242 pitch
+= pitch_align
;
251 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
252 struct intel_mipmap_tree
*src
)
256 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
260 intel_miptree_release(struct intel_context
*intel
,
261 struct intel_mipmap_tree
**mt
)
266 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
267 if (--(*mt
)->refcount
<= 0) {
270 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
272 intel_region_release(&((*mt
)->region
));
274 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
275 if ((*mt
)->level
[i
].image_offset
)
276 free((*mt
)->level
[i
].image_offset
);
286 /* Can the image be pulled into a unified mipmap tree. This mirrors
287 * the completeness test in a lot of ways.
289 * Not sure whether I want to pass gl_texture_image here.
292 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
293 struct gl_texture_image
*image
,
294 GLuint face
, GLuint level
)
296 /* Images with borders are never pulled into mipmap trees.
299 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
300 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
301 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
304 if (image
->InternalFormat
!= mt
->internal_format
||
305 image
->IsCompressed
!= mt
->compressed
)
308 if (!image
->IsCompressed
&&
310 image
->TexFormat
->TexelBytes
!= mt
->cpp
)
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.
317 if (image
->Width
!= mt
->level
[level
].width
||
318 image
->Height
!= mt
->level
[level
].height
||
319 image
->Depth
!= mt
->level
[level
].depth
)
327 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
331 GLuint w
, GLuint h
, GLuint d
)
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
;
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
);
342 /* Not sure when this would happen, but anyway:
344 if (mt
->level
[level
].image_offset
) {
345 free(mt
->level
[level
].image_offset
);
346 mt
->level
[level
].image_offset
= NULL
;
351 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
352 mt
->level
[level
].image_offset
[0] = 0;
357 intel_miptree_set_image_offset_ex(struct intel_mipmap_tree
*mt
,
358 GLuint level
, GLuint img
,
362 if (img
== 0 && level
== 0)
363 assert(x
== 0 && y
== 0);
365 assert(img
< mt
->level
[level
].nr_images
);
367 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
) * mt
->cpp
+ offset
;
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
]);
374 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
375 GLuint level
, GLuint img
,
378 intel_miptree_set_image_offset_ex(mt
, level
, img
, x
, y
, 0);
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.
386 * These functions present that view to mesa:
389 intel_miptree_depth_offsets(struct intel_mipmap_tree
*mt
, GLuint level
)
391 static const GLuint zero
= 0;
393 if (mt
->target
!= GL_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
396 return mt
->level
[level
].image_offset
;
401 intel_miptree_image_offset(struct intel_mipmap_tree
*mt
,
402 GLuint face
, GLuint level
)
404 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
405 return (mt
->level
[level
].level_offset
+
406 mt
->level
[level
].image_offset
[face
]);
408 return mt
->level
[level
].level_offset
;
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
422 intel_miptree_image_map(struct intel_context
* intel
,
423 struct intel_mipmap_tree
* mt
,
426 GLuint
* row_stride
, GLuint
* image_offsets
)
428 DBG("%s \n", __FUNCTION__
);
431 *row_stride
= mt
->pitch
* mt
->cpp
;
433 if (mt
->target
== GL_TEXTURE_3D
) {
436 for (i
= 0; i
< mt
->level
[level
].depth
; i
++)
437 image_offsets
[i
] = mt
->level
[level
].image_offset
[i
] / mt
->cpp
;
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;
445 return (intel_region_map(intel
, mt
->region
) +
446 intel_miptree_image_offset(mt
, face
, level
));
450 intel_miptree_image_unmap(struct intel_context
*intel
,
451 struct intel_mipmap_tree
*mt
)
453 DBG("%s\n", __FUNCTION__
);
454 intel_region_unmap(intel
, mt
->region
);
459 /* Upload data for a particular image.
462 intel_miptree_image_data(struct intel_context
*intel
,
463 struct intel_mipmap_tree
*dst
,
467 GLuint src_row_pitch
,
468 GLuint src_image_pitch
)
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
);
476 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
477 for (i
= 0; i
< depth
; i
++) {
478 height
= dst
->level
[level
].height
;
480 height
= (height
+ 3) / 4;
481 intel_region_data(intel
,
483 dst_offset
+ dst_depth_offset
[i
], /* dst_offset */
484 0, 0, /* dstx, dsty */
487 0, 0, /* source x, y */
488 dst
->level
[level
].width
, height
); /* width, height */
490 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
494 extern void intel_get_texture_alignment_unit(GLenum
, GLuint
*, GLuint
*);
495 /* Copy mipmap image between trees
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
)
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
);
513 if (dst
->compressed
) {
514 GLuint align_w
, align_h
;
516 intel_get_texture_alignment_unit(dst
->internal_format
, &align_w
, &align_h
);
517 height
= (height
+ 3) / 4;
518 width
= ALIGN(width
, align_w
);
521 for (i
= 0; i
< depth
; i
++) {
522 success
= intel_region_copy(intel
,
523 dst
->region
, dst_offset
+ dst_depth_offset
[i
],
525 src
->region
, src_offset
+ src_depth_offset
[i
],
526 0, 0, width
, height
, GL_COPY
);
528 GLubyte
*src_ptr
, *dst_ptr
;
530 src_ptr
= intel_region_map(intel
, src
->region
);
531 dst_ptr
= intel_region_map(intel
, dst
->region
);
533 _mesa_copy_rect(dst_ptr
+ dst_offset
+ dst_depth_offset
[i
],
537 src_ptr
+ src_offset
+ src_depth_offset
[i
],
540 intel_region_unmap(intel
, src
->region
);
541 intel_region_unmap(intel
, dst
->region
);