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
,
104 GLenum internal_format
,
109 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
110 GLboolean expect_accelerated_upload
)
112 struct intel_mipmap_tree
*mt
;
115 if (intel
->use_texture_tiling
&& compress_byte
== 0 &&
116 intel
->intelScreen
->kernel_exec_fencing
) {
117 if (IS_965(intel
->intelScreen
->deviceID
) &&
118 internal_format
== GL_DEPTH_COMPONENT
)
119 tiling
= I915_TILING_Y
;
121 tiling
= I915_TILING_X
;
123 tiling
= I915_TILING_NONE
;
125 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
126 first_level
, last_level
, width0
,
127 height0
, depth0
, cpp
, compress_byte
,
130 * pitch == 0 || height == 0 indicates the null texture
132 if (!mt
|| !mt
->pitch
|| !mt
->total_height
)
135 mt
->region
= intel_region_alloc(intel
,
141 expect_accelerated_upload
);
151 struct intel_mipmap_tree
*
152 intel_miptree_create_for_region(struct intel_context
*intel
,
154 GLenum internal_format
,
157 struct intel_region
*region
,
159 GLuint compress_byte
)
161 struct intel_mipmap_tree
*mt
;
163 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
164 first_level
, last_level
,
165 region
->width
, region
->height
, 1,
166 region
->cpp
, compress_byte
,
171 if (mt
->pitch
!= region
->pitch
) {
173 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
174 region
->pitch
, mt
->pitch
);
179 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
180 * to texture works, but we don't need that for texturing from a
181 * pixmap. Just override it here. */
182 mt
->pitch
= region
->pitch
;
185 intel_region_reference(&mt
->region
, region
);
191 * intel_miptree_pitch_align:
193 * @intel: intel context pointer
195 * @mt: the miptree to compute pitch alignment for
197 * @pitch: the natural pitch value
199 * Given @pitch, compute a larger value which accounts for
200 * any necessary alignment required by the device
203 int intel_miptree_pitch_align (struct intel_context
*intel
,
204 struct intel_mipmap_tree
*mt
,
209 GLcontext
*ctx
= &intel
->ctx
;
212 if (!mt
->compressed
) {
216 /* XXX: Align pitch to multiple of 64 bytes for now to allow
217 * render-to-texture to work in all cases. This should probably be
218 * replaced at some point by some scheme to only do this when really
226 if (tiling
== I915_TILING_X
)
228 else if (tiling
== I915_TILING_Y
)
231 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
234 /* XXX: At least the i915 seems very upset when the pitch is a multiple
235 * of 1024 and sometimes 512 bytes - performance can drop by several
236 * times. Go to the next multiple of the required alignment for now.
238 if (!(pitch
& 511) &&
239 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
240 pitch
+= pitch_align
;
249 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
250 struct intel_mipmap_tree
*src
)
254 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
258 intel_miptree_release(struct intel_context
*intel
,
259 struct intel_mipmap_tree
**mt
)
264 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
265 if (--(*mt
)->refcount
<= 0) {
268 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
270 intel_region_release(&((*mt
)->region
));
272 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
273 if ((*mt
)->level
[i
].image_offset
)
274 free((*mt
)->level
[i
].image_offset
);
284 /* Can the image be pulled into a unified mipmap tree. This mirrors
285 * the completeness test in a lot of ways.
287 * Not sure whether I want to pass gl_texture_image here.
290 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
291 struct gl_texture_image
*image
,
292 GLuint face
, GLuint level
)
294 /* Images with borders are never pulled into mipmap trees.
297 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
298 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
299 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
302 if (image
->InternalFormat
!= mt
->internal_format
||
303 image
->IsCompressed
!= mt
->compressed
)
306 if (!image
->IsCompressed
&&
308 image
->TexFormat
->TexelBytes
!= mt
->cpp
)
311 /* Test image dimensions against the base level image adjusted for
312 * minification. This will also catch images not present in the
313 * tree, changed targets, etc.
315 if (image
->Width
!= mt
->level
[level
].width
||
316 image
->Height
!= mt
->level
[level
].height
||
317 image
->Depth
!= mt
->level
[level
].depth
)
325 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
329 GLuint w
, GLuint h
, GLuint d
)
331 mt
->level
[level
].width
= w
;
332 mt
->level
[level
].height
= h
;
333 mt
->level
[level
].depth
= d
;
334 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
335 mt
->level
[level
].nr_images
= nr_images
;
337 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
338 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
340 /* Not sure when this would happen, but anyway:
342 if (mt
->level
[level
].image_offset
) {
343 free(mt
->level
[level
].image_offset
);
344 mt
->level
[level
].image_offset
= NULL
;
349 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
350 mt
->level
[level
].image_offset
[0] = 0;
356 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
357 GLuint level
, GLuint img
,
360 if (img
== 0 && level
== 0)
361 assert(x
== 0 && y
== 0);
363 assert(img
< mt
->level
[level
].nr_images
);
365 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
) * mt
->cpp
;
367 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
368 __FUNCTION__
, level
, img
, x
, y
, mt
->level
[level
].image_offset
[img
]);
372 /* Although we use the image_offset[] array to store relative offsets
373 * to cube faces, Mesa doesn't know anything about this and expects
374 * each cube face to be treated as a separate image.
376 * These functions present that view to mesa:
379 intel_miptree_depth_offsets(struct intel_mipmap_tree
*mt
, GLuint level
)
381 static const GLuint zero
= 0;
383 if (mt
->target
!= GL_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
386 return mt
->level
[level
].image_offset
;
391 intel_miptree_image_offset(struct intel_mipmap_tree
*mt
,
392 GLuint face
, GLuint level
)
394 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
395 return (mt
->level
[level
].level_offset
+
396 mt
->level
[level
].image_offset
[face
]);
398 return mt
->level
[level
].level_offset
;
404 * Map a teximage in a mipmap tree.
405 * \param row_stride returns row stride in bytes
406 * \param image_stride returns image stride in bytes (for 3D textures).
407 * \param image_offsets pointer to array of pixel offsets from the returned
408 * pointer to each depth image
409 * \return address of mapping
412 intel_miptree_image_map(struct intel_context
* intel
,
413 struct intel_mipmap_tree
* mt
,
416 GLuint
* row_stride
, GLuint
* image_offsets
)
418 DBG("%s \n", __FUNCTION__
);
421 *row_stride
= mt
->pitch
* mt
->cpp
;
423 if (mt
->target
== GL_TEXTURE_3D
) {
426 for (i
= 0; i
< mt
->level
[level
].depth
; i
++)
427 image_offsets
[i
] = mt
->level
[level
].image_offset
[i
] / mt
->cpp
;
429 assert(mt
->level
[level
].depth
== 1);
430 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
||
431 mt
->level
[level
].image_offset
[0] == 0);
432 image_offsets
[0] = 0;
435 return (intel_region_map(intel
, mt
->region
) +
436 intel_miptree_image_offset(mt
, face
, level
));
440 intel_miptree_image_unmap(struct intel_context
*intel
,
441 struct intel_mipmap_tree
*mt
)
443 DBG("%s\n", __FUNCTION__
);
444 intel_region_unmap(intel
, mt
->region
);
449 /* Upload data for a particular image.
452 intel_miptree_image_data(struct intel_context
*intel
,
453 struct intel_mipmap_tree
*dst
,
457 GLuint src_row_pitch
,
458 GLuint src_image_pitch
)
460 GLuint depth
= dst
->level
[level
].depth
;
461 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
462 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
466 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
467 for (i
= 0; i
< depth
; i
++) {
468 height
= dst
->level
[level
].height
;
470 height
= (height
+ 3) / 4;
471 intel_region_data(intel
,
473 dst_offset
+ dst_depth_offset
[i
], /* dst_offset */
474 0, 0, /* dstx, dsty */
477 0, 0, /* source x, y */
478 dst
->level
[level
].width
, height
); /* width, height */
480 src
+= src_image_pitch
* dst
->cpp
;
484 extern GLuint
intel_compressed_alignment(GLenum
);
485 /* Copy mipmap image between trees
488 intel_miptree_image_copy(struct intel_context
*intel
,
489 struct intel_mipmap_tree
*dst
,
490 GLuint face
, GLuint level
,
491 struct intel_mipmap_tree
*src
)
493 GLuint width
= src
->level
[level
].width
;
494 GLuint height
= src
->level
[level
].height
;
495 GLuint depth
= src
->level
[level
].depth
;
496 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
497 GLuint src_offset
= intel_miptree_image_offset(src
, face
, level
);
498 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
499 const GLuint
*src_depth_offset
= intel_miptree_depth_offsets(src
, level
);
502 if (dst
->compressed
) {
503 GLuint alignment
= intel_compressed_alignment(dst
->internal_format
);
504 height
= (height
+ 3) / 4;
505 width
= ((width
+ alignment
- 1) & ~(alignment
- 1));
508 for (i
= 0; i
< depth
; i
++) {
509 intel_region_copy(intel
,
510 dst
->region
, dst_offset
+ dst_depth_offset
[i
],
513 src
->region
, src_offset
+ src_depth_offset
[i
],
514 0, 0, width
, height
);