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
,
107 GLboolean expect_accelerated_upload
)
109 struct intel_mipmap_tree
*mt
;
111 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
112 first_level
, last_level
, width0
,
113 height0
, depth0
, cpp
, compress_byte
);
115 * pitch == 0 || height == 0 indicates the null texture
117 if (!mt
|| !mt
->pitch
|| !mt
->total_height
)
120 mt
->region
= intel_region_alloc(intel
,
125 expect_accelerated_upload
);
135 struct intel_mipmap_tree
*
136 intel_miptree_create_for_region(struct intel_context
*intel
,
138 GLenum internal_format
,
141 struct intel_region
*region
,
143 GLuint compress_byte
)
145 struct intel_mipmap_tree
*mt
;
147 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
148 first_level
, last_level
,
149 region
->width
, region
->height
, 1,
150 region
->cpp
, compress_byte
);
154 if (mt
->pitch
!= region
->pitch
) {
156 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
157 region
->pitch
, mt
->pitch
);
162 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
163 * to texture works, but we don't need that for texturing from a
164 * pixmap. Just override it here. */
165 mt
->pitch
= region
->pitch
;
168 intel_region_reference(&mt
->region
, region
);
174 * intel_miptree_pitch_align:
176 * @intel: intel context pointer
178 * @mt: the miptree to compute pitch alignment for
180 * @pitch: the natural pitch value
182 * Given @pitch, compute a larger value which accounts for
183 * any necessary alignment required by the device
186 int intel_miptree_pitch_align (struct intel_context
*intel
,
187 struct intel_mipmap_tree
*mt
,
191 GLcontext
*ctx
= &intel
->ctx
;
194 if (!mt
->compressed
) {
198 /* XXX: Align pitch to multiple of 64 bytes for now to allow
199 * render-to-texture to work in all cases. This should probably be
200 * replaced at some point by some scheme to only do this when really
208 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
211 /* XXX: At least the i915 seems very upset when the pitch is a multiple
212 * of 1024 and sometimes 512 bytes - performance can drop by several
213 * times. Go to the next multiple of the required alignment for now.
215 if (!(pitch
& 511) &&
216 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
217 pitch
+= pitch_align
;
226 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
227 struct intel_mipmap_tree
*src
)
231 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
235 intel_miptree_release(struct intel_context
*intel
,
236 struct intel_mipmap_tree
**mt
)
241 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
242 if (--(*mt
)->refcount
<= 0) {
245 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
247 intel_region_release(&((*mt
)->region
));
249 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
250 if ((*mt
)->level
[i
].image_offset
)
251 free((*mt
)->level
[i
].image_offset
);
261 /* Can the image be pulled into a unified mipmap tree. This mirrors
262 * the completeness test in a lot of ways.
264 * Not sure whether I want to pass gl_texture_image here.
267 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
268 struct gl_texture_image
*image
,
269 GLuint face
, GLuint level
)
271 /* Images with borders are never pulled into mipmap trees.
274 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
275 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
276 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
279 if (image
->InternalFormat
!= mt
->internal_format
||
280 image
->IsCompressed
!= mt
->compressed
)
283 if (!image
->IsCompressed
&&
285 image
->TexFormat
->TexelBytes
!= mt
->cpp
)
288 /* Test image dimensions against the base level image adjusted for
289 * minification. This will also catch images not present in the
290 * tree, changed targets, etc.
292 if (image
->Width
!= mt
->level
[level
].width
||
293 image
->Height
!= mt
->level
[level
].height
||
294 image
->Depth
!= mt
->level
[level
].depth
)
302 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
306 GLuint w
, GLuint h
, GLuint d
)
308 mt
->level
[level
].width
= w
;
309 mt
->level
[level
].height
= h
;
310 mt
->level
[level
].depth
= d
;
311 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
312 mt
->level
[level
].nr_images
= nr_images
;
314 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
315 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
317 /* Not sure when this would happen, but anyway:
319 if (mt
->level
[level
].image_offset
) {
320 free(mt
->level
[level
].image_offset
);
321 mt
->level
[level
].image_offset
= NULL
;
326 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
327 mt
->level
[level
].image_offset
[0] = 0;
333 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
334 GLuint level
, GLuint img
,
337 if (img
== 0 && level
== 0)
338 assert(x
== 0 && y
== 0);
340 assert(img
< mt
->level
[level
].nr_images
);
342 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
) * mt
->cpp
;
344 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
345 __FUNCTION__
, level
, img
, x
, y
, mt
->level
[level
].image_offset
[img
]);
349 /* Although we use the image_offset[] array to store relative offsets
350 * to cube faces, Mesa doesn't know anything about this and expects
351 * each cube face to be treated as a separate image.
353 * These functions present that view to mesa:
356 intel_miptree_depth_offsets(struct intel_mipmap_tree
*mt
, GLuint level
)
358 static const GLuint zero
= 0;
360 if (mt
->target
!= GL_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
363 return mt
->level
[level
].image_offset
;
368 intel_miptree_image_offset(struct intel_mipmap_tree
*mt
,
369 GLuint face
, GLuint level
)
371 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
372 return (mt
->level
[level
].level_offset
+
373 mt
->level
[level
].image_offset
[face
]);
375 return mt
->level
[level
].level_offset
;
381 * Map a teximage in a mipmap tree.
382 * \param row_stride returns row stride in bytes
383 * \param image_stride returns image stride in bytes (for 3D textures).
384 * \param image_offsets pointer to array of pixel offsets from the returned
385 * pointer to each depth image
386 * \return address of mapping
389 intel_miptree_image_map(struct intel_context
* intel
,
390 struct intel_mipmap_tree
* mt
,
393 GLuint
* row_stride
, GLuint
* image_offsets
)
395 DBG("%s \n", __FUNCTION__
);
398 *row_stride
= mt
->pitch
* mt
->cpp
;
400 if (mt
->target
== GL_TEXTURE_3D
) {
403 for (i
= 0; i
< mt
->level
[level
].depth
; i
++)
404 image_offsets
[i
] = mt
->level
[level
].image_offset
[i
] / mt
->cpp
;
406 assert(mt
->level
[level
].depth
== 1);
407 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
||
408 mt
->level
[level
].image_offset
[0] == 0);
409 image_offsets
[0] = 0;
412 return (intel_region_map(intel
, mt
->region
) +
413 intel_miptree_image_offset(mt
, face
, level
));
417 intel_miptree_image_unmap(struct intel_context
*intel
,
418 struct intel_mipmap_tree
*mt
)
420 DBG("%s\n", __FUNCTION__
);
421 intel_region_unmap(intel
, mt
->region
);
426 /* Upload data for a particular image.
429 intel_miptree_image_data(struct intel_context
*intel
,
430 struct intel_mipmap_tree
*dst
,
434 GLuint src_row_pitch
,
435 GLuint src_image_pitch
)
437 GLuint depth
= dst
->level
[level
].depth
;
438 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
439 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
443 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
444 for (i
= 0; i
< depth
; i
++) {
445 height
= dst
->level
[level
].height
;
447 height
= (height
+ 3) / 4;
448 intel_region_data(intel
,
450 dst_offset
+ dst_depth_offset
[i
], /* dst_offset */
451 0, 0, /* dstx, dsty */
454 0, 0, /* source x, y */
455 dst
->level
[level
].width
, height
); /* width, height */
457 src
+= src_image_pitch
* dst
->cpp
;
461 extern GLuint
intel_compressed_alignment(GLenum
);
462 /* Copy mipmap image between trees
465 intel_miptree_image_copy(struct intel_context
*intel
,
466 struct intel_mipmap_tree
*dst
,
467 GLuint face
, GLuint level
,
468 struct intel_mipmap_tree
*src
)
470 GLuint width
= src
->level
[level
].width
;
471 GLuint height
= src
->level
[level
].height
;
472 GLuint depth
= src
->level
[level
].depth
;
473 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
474 GLuint src_offset
= intel_miptree_image_offset(src
, face
, level
);
475 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
476 const GLuint
*src_depth_offset
= intel_miptree_depth_offsets(src
, level
);
479 if (dst
->compressed
) {
480 GLuint alignment
= intel_compressed_alignment(dst
->internal_format
);
481 height
= (height
+ 3) / 4;
482 width
= ((width
+ alignment
- 1) & ~(alignment
- 1));
485 for (i
= 0; i
< depth
; i
++) {
486 intel_region_copy(intel
,
487 dst
->region
, dst_offset
+ dst_depth_offset
[i
],
490 src
->region
, src_offset
+ src_depth_offset
[i
],
491 0, 0, width
, height
);