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\n", __FUNCTION__
,
66 _mesa_lookup_enum_by_nr(target
),
67 _mesa_lookup_enum_by_nr(internal_format
), first_level
, last_level
);
69 mt
->target
= target_to_target(target
);
70 mt
->internal_format
= internal_format
;
71 mt
->first_level
= first_level
;
72 mt
->last_level
= last_level
;
74 mt
->height0
= height0
;
76 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
77 mt
->compressed
= compress_byte
? 1 : 0;
82 if (IS_945(intel
->intelScreen
->deviceID
))
83 ok
= i945_miptree_layout(intel
, mt
);
85 ok
= i915_miptree_layout(intel
, mt
);
87 ok
= brw_miptree_layout(intel
, mt
);
98 struct intel_mipmap_tree
*
99 intel_miptree_create(struct intel_context
*intel
,
101 GLenum internal_format
,
106 GLuint depth0
, GLuint cpp
, GLuint compress_byte
)
108 struct intel_mipmap_tree
*mt
;
110 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
111 first_level
, last_level
, width0
,
112 height0
, depth0
, cpp
, compress_byte
);
114 * pitch == 0 indicates the null texture
116 if (!mt
|| !mt
->pitch
)
119 mt
->region
= intel_region_alloc(intel
,
133 struct intel_mipmap_tree
*
134 intel_miptree_create_for_region(struct intel_context
*intel
,
136 GLenum internal_format
,
139 struct intel_region
*region
,
141 GLuint compress_byte
)
143 struct intel_mipmap_tree
*mt
;
145 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
146 first_level
, last_level
,
147 region
->width
, region
->height
, 1,
148 region
->cpp
, compress_byte
);
152 if (mt
->pitch
!= region
->pitch
) {
154 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
155 region
->pitch
, mt
->pitch
);
160 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
161 * to texture works, but we don't need that for texturing from a
162 * pixmap. Just override it here. */
163 mt
->pitch
= region
->pitch
;
172 * intel_miptree_pitch_align:
174 * @intel: intel context pointer
176 * @mt: the miptree to compute pitch alignment for
178 * @pitch: the natural pitch value
180 * Given @pitch, compute a larger value which accounts for
181 * any necessary alignment required by the device
184 int intel_miptree_pitch_align (struct intel_context
*intel
,
185 struct intel_mipmap_tree
*mt
,
189 GLcontext
*ctx
= &intel
->ctx
;
192 if (!mt
->compressed
) {
196 /* XXX: Align pitch to multiple of 64 bytes for now to allow
197 * render-to-texture to work in all cases. This should probably be
198 * replaced at some point by some scheme to only do this when really
206 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
209 /* XXX: At least the i915 seems very upset when the pitch is a multiple
210 * of 1024 and sometimes 512 bytes - performance can drop by several
211 * times. Go to the next multiple of the required alignment for now.
213 if (!(pitch
& 511) &&
214 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
215 pitch
+= pitch_align
;
224 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
225 struct intel_mipmap_tree
*src
)
229 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
233 intel_miptree_release(struct intel_context
*intel
,
234 struct intel_mipmap_tree
**mt
)
239 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
240 if (--(*mt
)->refcount
<= 0) {
243 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
245 intel_region_release(&((*mt
)->region
));
247 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
248 if ((*mt
)->level
[i
].image_offset
)
249 free((*mt
)->level
[i
].image_offset
);
259 /* Can the image be pulled into a unified mipmap tree. This mirrors
260 * the completeness test in a lot of ways.
262 * Not sure whether I want to pass gl_texture_image here.
265 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
266 struct gl_texture_image
*image
,
267 GLuint face
, GLuint level
)
269 /* Images with borders are never pulled into mipmap trees.
272 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
273 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
274 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
277 if (image
->InternalFormat
!= mt
->internal_format
||
278 image
->IsCompressed
!= mt
->compressed
)
281 if (!image
->IsCompressed
&&
283 image
->TexFormat
->TexelBytes
!= mt
->cpp
)
286 /* Test image dimensions against the base level image adjusted for
287 * minification. This will also catch images not present in the
288 * tree, changed targets, etc.
290 if (image
->Width
!= mt
->level
[level
].width
||
291 image
->Height
!= mt
->level
[level
].height
||
292 image
->Depth
!= mt
->level
[level
].depth
)
300 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
304 GLuint w
, GLuint h
, GLuint d
)
306 mt
->level
[level
].width
= w
;
307 mt
->level
[level
].height
= h
;
308 mt
->level
[level
].depth
= d
;
309 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
310 mt
->level
[level
].nr_images
= nr_images
;
312 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
313 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
315 /* Not sure when this would happen, but anyway:
317 if (mt
->level
[level
].image_offset
) {
318 free(mt
->level
[level
].image_offset
);
319 mt
->level
[level
].image_offset
= NULL
;
324 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
325 mt
->level
[level
].image_offset
[0] = 0;
331 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
332 GLuint level
, GLuint img
,
335 if (img
== 0 && level
== 0)
336 assert(x
== 0 && y
== 0);
338 assert(img
< mt
->level
[level
].nr_images
);
340 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
) * mt
->cpp
;
342 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
343 __FUNCTION__
, level
, img
, x
, y
, mt
->level
[level
].image_offset
[img
]);
347 /* Although we use the image_offset[] array to store relative offsets
348 * to cube faces, Mesa doesn't know anything about this and expects
349 * each cube face to be treated as a separate image.
351 * These functions present that view to mesa:
354 intel_miptree_depth_offsets(struct intel_mipmap_tree
*mt
, GLuint level
)
356 static const GLuint zero
= 0;
358 if (mt
->target
!= GL_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
361 return mt
->level
[level
].image_offset
;
366 intel_miptree_image_offset(struct intel_mipmap_tree
*mt
,
367 GLuint face
, GLuint level
)
369 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
370 return (mt
->level
[level
].level_offset
+
371 mt
->level
[level
].image_offset
[face
]);
373 return mt
->level
[level
].level_offset
;
379 * Map a teximage in a mipmap tree.
380 * \param row_stride returns row stride in bytes
381 * \param image_stride returns image stride in bytes (for 3D textures).
382 * \param image_offsets pointer to array of pixel offsets from the returned
383 * pointer to each depth image
384 * \return address of mapping
387 intel_miptree_image_map(struct intel_context
* intel
,
388 struct intel_mipmap_tree
* mt
,
391 GLuint
* row_stride
, GLuint
* image_offsets
)
393 DBG("%s \n", __FUNCTION__
);
396 *row_stride
= mt
->pitch
* mt
->cpp
;
398 if (mt
->target
== GL_TEXTURE_3D
) {
401 for (i
= 0; i
< mt
->level
[level
].depth
; i
++)
402 image_offsets
[i
] = mt
->level
[level
].image_offset
[i
] / mt
->cpp
;
404 assert(mt
->level
[level
].depth
== 1);
405 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
||
406 mt
->level
[level
].image_offset
[0] == 0);
407 image_offsets
[0] = 0;
410 return (intel_region_map(intel
, mt
->region
) +
411 intel_miptree_image_offset(mt
, face
, level
));
415 intel_miptree_image_unmap(struct intel_context
*intel
,
416 struct intel_mipmap_tree
*mt
)
418 DBG("%s\n", __FUNCTION__
);
419 intel_region_unmap(intel
, mt
->region
);
424 /* Upload data for a particular image.
427 intel_miptree_image_data(struct intel_context
*intel
,
428 struct intel_mipmap_tree
*dst
,
432 GLuint src_row_pitch
,
433 GLuint src_image_pitch
)
435 GLuint depth
= dst
->level
[level
].depth
;
436 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
437 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
441 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
442 for (i
= 0; i
< depth
; i
++) {
443 height
= dst
->level
[level
].height
;
445 height
= (height
+ 3) / 4;
446 intel_region_data(intel
,
448 dst_offset
+ dst_depth_offset
[i
], /* dst_offset */
449 0, 0, /* dstx, dsty */
452 0, 0, /* source x, y */
453 dst
->level
[level
].width
, height
); /* width, height */
455 src
+= src_image_pitch
* dst
->cpp
;
459 extern GLuint
intel_compressed_alignment(GLenum
);
460 /* Copy mipmap image between trees
463 intel_miptree_image_copy(struct intel_context
*intel
,
464 struct intel_mipmap_tree
*dst
,
465 GLuint face
, GLuint level
,
466 struct intel_mipmap_tree
*src
)
468 GLuint width
= src
->level
[level
].width
;
469 GLuint height
= src
->level
[level
].height
;
470 GLuint depth
= src
->level
[level
].depth
;
471 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
472 GLuint src_offset
= intel_miptree_image_offset(src
, face
, level
);
473 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
474 const GLuint
*src_depth_offset
= intel_miptree_depth_offsets(src
, level
);
477 if (dst
->compressed
) {
478 GLuint alignment
= intel_compressed_alignment(dst
->internal_format
);
479 height
= (height
+ 3) / 4;
480 width
= ((width
+ alignment
- 1) & ~(alignment
- 1));
483 for (i
= 0; i
< depth
; i
++) {
484 intel_region_copy(intel
,
485 dst
->region
, dst_offset
+ dst_depth_offset
[i
],
488 src
->region
, src_offset
+ src_depth_offset
[i
],
489 0, 0, width
, height
);