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"
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
);
117 mt
->region
= intel_region_alloc(intel
,
118 mt
->cpp
, mt
->pitch
, mt
->total_height
);
128 struct intel_mipmap_tree
*
129 intel_miptree_create_for_region(struct intel_context
*intel
,
131 GLenum internal_format
,
134 struct intel_region
*region
,
136 GLuint compress_byte
)
138 struct intel_mipmap_tree
*mt
;
140 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
141 first_level
, last_level
,
142 region
->pitch
, region
->height
, depth0
,
143 region
->cpp
, compress_byte
);
147 if (mt
->pitch
!= region
->pitch
) {
149 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
150 region
->pitch
, mt
->pitch
);
155 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
156 * to texture works, but we don't need that for texturing from a
157 * pixmap. Just override it here. */
158 mt
->pitch
= region
->pitch
;
167 * intel_miptree_pitch_align:
169 * @intel: intel context pointer
171 * @mt: the miptree to compute pitch alignment for
173 * @pitch: the natural pitch value
175 * Given @pitch, compute a larger value which accounts for
176 * any necessary alignment required by the device
179 int intel_miptree_pitch_align (struct intel_context
*intel
,
180 struct intel_mipmap_tree
*mt
,
183 if (!mt
->compressed
) {
187 /* XXX: Align pitch to multiple of 64 bytes for now to allow
188 * render-to-texture to work in all cases. This should probably be
189 * replaced at some point by some scheme to only do this when really
197 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
200 /* XXX: At least the i915 seems very upset when the pitch is a multiple
201 * of 1024 and sometimes 512 bytes - performance can drop by several
202 * times. Go to the next multiple of the required alignment for now.
205 pitch
+= pitch_align
;
214 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
215 struct intel_mipmap_tree
*src
)
219 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
223 intel_miptree_release(struct intel_context
*intel
,
224 struct intel_mipmap_tree
**mt
)
229 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
230 if (--(*mt
)->refcount
<= 0) {
233 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
235 intel_region_release(&((*mt
)->region
));
237 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
238 if ((*mt
)->level
[i
].image_offset
)
239 free((*mt
)->level
[i
].image_offset
);
249 /* Can the image be pulled into a unified mipmap tree. This mirrors
250 * the completeness test in a lot of ways.
252 * Not sure whether I want to pass gl_texture_image here.
255 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
256 struct gl_texture_image
*image
,
257 GLuint face
, GLuint level
)
259 /* Images with borders are never pulled into mipmap trees.
264 if (image
->InternalFormat
!= mt
->internal_format
||
265 image
->IsCompressed
!= mt
->compressed
)
268 /* Test image dimensions against the base level image adjusted for
269 * minification. This will also catch images not present in the
270 * tree, changed targets, etc.
272 if (image
->Width
!= mt
->level
[level
].width
||
273 image
->Height
!= mt
->level
[level
].height
||
274 image
->Depth
!= mt
->level
[level
].depth
)
282 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
286 GLuint w
, GLuint h
, GLuint d
)
288 mt
->level
[level
].width
= w
;
289 mt
->level
[level
].height
= h
;
290 mt
->level
[level
].depth
= d
;
291 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
292 mt
->level
[level
].nr_images
= nr_images
;
294 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
295 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
297 /* Not sure when this would happen, but anyway:
299 if (mt
->level
[level
].image_offset
) {
300 free(mt
->level
[level
].image_offset
);
301 mt
->level
[level
].image_offset
= NULL
;
306 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
307 mt
->level
[level
].image_offset
[0] = 0;
313 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
314 GLuint level
, GLuint img
,
317 if (img
== 0 && level
== 0)
318 assert(x
== 0 && y
== 0);
320 assert(img
< mt
->level
[level
].nr_images
);
322 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
);
324 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
325 __FUNCTION__
, level
, img
, x
, y
, mt
->level
[level
].image_offset
[img
]);
329 /* Although we use the image_offset[] array to store relative offsets
330 * to cube faces, Mesa doesn't know anything about this and expects
331 * each cube face to be treated as a separate image.
333 * These functions present that view to mesa:
336 intel_miptree_depth_offsets(struct intel_mipmap_tree
*mt
, GLuint level
)
338 static const GLuint zero
= 0;
340 if (mt
->target
!= GL_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
343 return mt
->level
[level
].image_offset
;
348 intel_miptree_image_offset(struct intel_mipmap_tree
*mt
,
349 GLuint face
, GLuint level
)
351 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
352 return (mt
->level
[level
].level_offset
+
353 mt
->level
[level
].image_offset
[face
] * mt
->cpp
);
355 return mt
->level
[level
].level_offset
;
361 * Map a teximage in a mipmap tree.
362 * \param row_stride returns row stride in bytes
363 * \param image_stride returns image stride in bytes (for 3D textures).
364 * \return address of mapping
367 intel_miptree_image_map(struct intel_context
* intel
,
368 struct intel_mipmap_tree
* mt
,
371 GLuint
* row_stride
, GLuint
* image_offsets
)
373 DBG("%s \n", __FUNCTION__
);
376 *row_stride
= mt
->pitch
* mt
->cpp
;
379 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
380 memset(image_offsets
, 0, mt
->level
[level
].depth
* sizeof(GLuint
));
382 memcpy(image_offsets
, mt
->level
[level
].image_offset
,
383 mt
->level
[level
].depth
* sizeof(GLuint
));
386 return (intel_region_map(intel
, mt
->region
) +
387 intel_miptree_image_offset(mt
, face
, level
));
391 intel_miptree_image_unmap(struct intel_context
*intel
,
392 struct intel_mipmap_tree
*mt
)
394 DBG("%s\n", __FUNCTION__
);
395 intel_region_unmap(intel
, mt
->region
);
400 /* Upload data for a particular image.
403 intel_miptree_image_data(struct intel_context
*intel
,
404 struct intel_mipmap_tree
*dst
,
408 GLuint src_row_pitch
,
409 GLuint src_image_pitch
)
411 GLuint depth
= dst
->level
[level
].depth
;
412 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
413 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
417 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
418 for (i
= 0; i
< depth
; i
++) {
419 height
= dst
->level
[level
].height
;
422 intel_region_data(intel
,
424 dst_offset
+ dst_depth_offset
[i
] * dst
->cpp
, /* dst_offset */
425 0, 0, /* dstx, dsty */
428 0, 0, /* source x, y */
429 dst
->level
[level
].width
, height
); /* width, height */
431 src
+= src_image_pitch
* dst
->cpp
;
435 extern GLuint
intel_compressed_alignment(GLenum
);
436 /* Copy mipmap image between trees
439 intel_miptree_image_copy(struct intel_context
*intel
,
440 struct intel_mipmap_tree
*dst
,
441 GLuint face
, GLuint level
,
442 struct intel_mipmap_tree
*src
)
444 GLuint width
= src
->level
[level
].width
;
445 GLuint height
= src
->level
[level
].height
;
446 GLuint depth
= src
->level
[level
].depth
;
447 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
);
448 GLuint src_offset
= intel_miptree_image_offset(src
, face
, level
);
449 const GLuint
*dst_depth_offset
= intel_miptree_depth_offsets(dst
, level
);
450 const GLuint
*src_depth_offset
= intel_miptree_depth_offsets(src
, level
);
453 if (dst
->compressed
) {
454 GLuint alignment
= intel_compressed_alignment(dst
->internal_format
);
455 height
= (height
+ 3) / 4;
456 width
= ((width
+ alignment
- 1) & ~(alignment
- 1));
459 for (i
= 0; i
< depth
; i
++) {
460 intel_region_copy(intel
,
461 dst
->region
, dst_offset
+ dst_depth_offset
[i
] * dst
->cpp
,
464 src
->region
, src_offset
+ src_depth_offset
[i
] * src
->cpp
,
465 0, 0, width
, height
);