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
)
63 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
65 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
66 _mesa_lookup_enum_by_nr(target
),
67 _mesa_lookup_enum_by_nr(internal_format
),
68 first_level
, last_level
, mt
);
70 mt
->target
= target_to_target(target
);
71 mt
->internal_format
= internal_format
;
72 mt
->first_level
= first_level
;
73 mt
->last_level
= last_level
;
75 mt
->height0
= height0
;
77 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
78 mt
->compressed
= compress_byte
? 1 : 0;
83 if (IS_945(intel
->intelScreen
->deviceID
))
84 ok
= i945_miptree_layout(intel
, mt
);
86 ok
= i915_miptree_layout(intel
, mt
);
88 ok
= brw_miptree_layout(intel
, mt
);
93 DBG("%s not okay - returning NULL\n", __FUNCTION__
);
100 struct intel_mipmap_tree
*
101 intel_miptree_create(struct intel_context
*intel
,
103 GLenum internal_format
,
108 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
109 GLboolean expect_accelerated_upload
)
111 struct intel_mipmap_tree
*mt
;
113 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
114 first_level
, last_level
, width0
,
115 height0
, depth0
, cpp
, compress_byte
);
117 * pitch == 0 || height == 0 indicates the null texture
119 if (!mt
|| !mt
->pitch
|| !mt
->total_height
)
122 mt
->region
= intel_region_alloc(intel
,
127 expect_accelerated_upload
);
137 struct intel_mipmap_tree
*
138 intel_miptree_create_for_region(struct intel_context
*intel
,
140 GLenum internal_format
,
143 struct intel_region
*region
,
145 GLuint compress_byte
)
147 struct intel_mipmap_tree
*mt
;
149 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
150 first_level
, last_level
,
151 region
->width
, region
->height
, 1,
152 region
->cpp
, compress_byte
);
156 if (mt
->pitch
!= region
->pitch
) {
158 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
159 region
->pitch
, mt
->pitch
);
164 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
165 * to texture works, but we don't need that for texturing from a
166 * pixmap. Just override it here. */
167 mt
->pitch
= region
->pitch
;
170 intel_region_reference(&mt
->region
, region
);
176 * intel_miptree_pitch_align:
178 * @intel: intel context pointer
180 * @mt: the miptree to compute pitch alignment for
182 * @pitch: the natural pitch value
184 * Given @pitch, compute a larger value which accounts for
185 * any necessary alignment required by the device
188 int intel_miptree_pitch_align (struct intel_context
*intel
,
189 struct intel_mipmap_tree
*mt
,
193 GLcontext
*ctx
= &intel
->ctx
;
196 if (!mt
->compressed
) {
200 /* XXX: Align pitch to multiple of 64 bytes for now to allow
201 * render-to-texture to work in all cases. This should probably be
202 * replaced at some point by some scheme to only do this when really
210 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
213 /* XXX: At least the i915 seems very upset when the pitch is a multiple
214 * of 1024 and sometimes 512 bytes - performance can drop by several
215 * times. Go to the next multiple of the required alignment for now.
217 if (!(pitch
& 511) &&
218 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
219 pitch
+= pitch_align
;
228 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
229 struct intel_mipmap_tree
*src
)
233 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
237 intel_miptree_release(struct intel_context
*intel
,
238 struct intel_mipmap_tree
**mt
)
243 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
244 if (--(*mt
)->refcount
<= 0) {
247 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
249 intel_region_release(&((*mt
)->region
));
251 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
252 if ((*mt
)->level
[i
].image_offset
)
253 free((*mt
)->level
[i
].image_offset
);
263 /* Can the image be pulled into a unified mipmap tree. This mirrors
264 * the completeness test in a lot of ways.
266 * Not sure whether I want to pass gl_texture_image here.
269 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
270 struct gl_texture_image
*image
,
271 GLuint face
, GLuint level
)
273 /* Images with borders are never pulled into mipmap trees.
276 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
277 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
278 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
281 if (image
->InternalFormat
!= mt
->internal_format
||
282 image
->IsCompressed
!= mt
->compressed
)
285 if (!image
->IsCompressed
&&
287 image
->TexFormat
->TexelBytes
!= mt
->cpp
)
290 /* Test image dimensions against the base level image adjusted for
291 * minification. This will also catch images not present in the
292 * tree, changed targets, etc.
294 if (image
->Width
!= mt
->level
[level
].width
||
295 image
->Height
!= mt
->level
[level
].height
||
296 image
->Depth
!= mt
->level
[level
].depth
)
304 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
308 GLuint w
, GLuint h
, GLuint d
)
310 mt
->level
[level
].width
= w
;
311 mt
->level
[level
].height
= h
;
312 mt
->level
[level
].depth
= d
;
313 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
314 mt
->level
[level
].nr_images
= nr_images
;
316 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
317 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
319 /* Not sure when this would happen, but anyway:
321 if (mt
->level
[level
].image_offset
) {
322 free(mt
->level
[level
].image_offset
);
323 mt
->level
[level
].image_offset
= NULL
;
328 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
329 mt
->level
[level
].image_offset
[0] = 0;
335 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
336 GLuint level
, GLuint img
,
339 if (img
== 0 && level
== 0)
340 assert(x
== 0 && y
== 0);
342 assert(img
< mt
->level
[level
].nr_images
);
344 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
) * mt
->cpp
;
346 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
347 __FUNCTION__
, level
, img
, x
, y
, mt
->level
[level
].image_offset
[img
]);
351 /* Although we use the image_offset[] array to store relative offsets
352 * to cube faces, Mesa doesn't know anything about this and expects
353 * each cube face to be treated as a separate image.
355 * These functions present that view to mesa:
358 intel_miptree_depth_offsets(struct intel_mipmap_tree
*mt
, GLuint level
)
360 static const GLuint zero
= 0;
362 if (mt
->target
!= GL_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
365 return mt
->level
[level
].image_offset
;
370 intel_miptree_image_offset(struct intel_mipmap_tree
*mt
,
371 GLuint face
, GLuint level
)
373 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
374 return (mt
->level
[level
].level_offset
+
375 mt
->level
[level
].image_offset
[face
]);
377 return mt
->level
[level
].level_offset
;
383 * Map a teximage in a mipmap tree.
384 * \param row_stride returns row stride in bytes
385 * \param image_stride returns image stride in bytes (for 3D textures).
386 * \param image_offsets pointer to array of pixel offsets from the returned
387 * pointer to each depth image
388 * \return address of mapping
391 intel_miptree_image_map(struct intel_context
* intel
,
392 struct intel_mipmap_tree
* mt
,
395 GLuint
* row_stride
, GLuint
* image_offsets
)
397 DBG("%s \n", __FUNCTION__
);
400 *row_stride
= mt
->pitch
* mt
->cpp
;
402 if (mt
->target
== GL_TEXTURE_3D
) {
405 for (i
= 0; i
< mt
->level
[level
].depth
; i
++)
406 image_offsets
[i
] = mt
->level
[level
].image_offset
[i
] / mt
->cpp
;
408 assert(mt
->level
[level
].depth
== 1);
409 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
||
410 mt
->level
[level
].image_offset
[0] == 0);
411 image_offsets
[0] = 0;
414 return (intel_region_map(intel
, mt
->region
) +
415 intel_miptree_image_offset(mt
, face
, level
));
419 intel_miptree_image_unmap(struct intel_context
*intel
,
420 struct intel_mipmap_tree
*mt
)
422 DBG("%s\n", __FUNCTION__
);
423 intel_region_unmap(intel
, mt
->region
);
428 /* Upload data for a particular image.
431 intel_miptree_image_data(struct intel_context
*intel
,
432 struct intel_mipmap_tree
*dst
,
436 GLuint src_row_pitch
,
437 GLuint src_image_pitch
)
439 GLuint depth
= dst
->level
[level
].depth
;
440 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
441 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
445 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
446 for (i
= 0; i
< depth
; i
++) {
447 height
= dst
->level
[level
].height
;
449 height
= (height
+ 3) / 4;
450 intel_region_data(intel
,
452 dst_offset
+ dst_depth_offset
[i
], /* dst_offset */
453 0, 0, /* dstx, dsty */
456 0, 0, /* source x, y */
457 dst
->level
[level
].width
, height
); /* width, height */
459 src
+= src_image_pitch
* dst
->cpp
;
463 extern GLuint
intel_compressed_alignment(GLenum
);
464 /* Copy mipmap image between trees
467 intel_miptree_image_copy(struct intel_context
*intel
,
468 struct intel_mipmap_tree
*dst
,
469 GLuint face
, GLuint level
,
470 struct intel_mipmap_tree
*src
)
472 GLuint width
= src
->level
[level
].width
;
473 GLuint height
= src
->level
[level
].height
;
474 GLuint depth
= src
->level
[level
].depth
;
475 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
476 GLuint src_offset
= intel_miptree_image_offset(src
, face
, level
);
477 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
478 const GLuint
*src_depth_offset
= intel_miptree_depth_offsets(src
, level
);
481 if (dst
->compressed
) {
482 GLuint alignment
= intel_compressed_alignment(dst
->internal_format
);
483 height
= (height
+ 3) / 4;
484 width
= ((width
+ alignment
- 1) & ~(alignment
- 1));
487 for (i
= 0; i
< depth
; i
++) {
488 intel_region_copy(intel
,
489 dst
->region
, dst_offset
+ dst_depth_offset
[i
],
492 src
->region
, src_offset
+ src_depth_offset
[i
],
493 0, 0, width
, height
);