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_tex_layout.h"
32 #include "main/enums.h"
34 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
38 target_to_target(GLenum target
)
41 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
42 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
47 return GL_TEXTURE_CUBE_MAP_ARB
;
54 static struct intel_mipmap_tree
*
55 intel_miptree_create_internal(struct intel_context
*intel
,
57 GLenum internal_format
,
62 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
66 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
68 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
69 _mesa_lookup_enum_by_nr(target
),
70 _mesa_lookup_enum_by_nr(internal_format
),
71 first_level
, last_level
, mt
);
73 mt
->target
= target_to_target(target
);
74 mt
->internal_format
= internal_format
;
75 mt
->first_level
= first_level
;
76 mt
->last_level
= last_level
;
78 mt
->height0
= height0
;
80 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
81 mt
->compressed
= compress_byte
? 1 : 0;
86 ok
= i945_miptree_layout(intel
, mt
, tiling
);
88 ok
= i915_miptree_layout(intel
, mt
, tiling
);
90 ok
= brw_miptree_layout(intel
, mt
, tiling
);
95 DBG("%s not okay - returning NULL\n", __FUNCTION__
);
103 struct intel_mipmap_tree
*
104 intel_miptree_create(struct intel_context
*intel
,
107 GLenum internal_format
,
112 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
113 GLboolean expect_accelerated_upload
)
115 struct intel_mipmap_tree
*mt
;
116 uint32_t tiling
= I915_TILING_NONE
;
118 if (intel
->use_texture_tiling
&& compress_byte
== 0) {
119 if (intel
->gen
>= 4 &&
120 (base_format
== GL_DEPTH_COMPONENT
||
121 base_format
== GL_DEPTH_STENCIL_EXT
))
122 tiling
= I915_TILING_Y
;
123 else if (width0
>= 64)
124 tiling
= I915_TILING_X
;
127 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
128 first_level
, last_level
, width0
,
129 height0
, depth0
, cpp
, compress_byte
,
132 * pitch == 0 || height == 0 indicates the null texture
134 if (!mt
|| !mt
->total_height
) {
139 mt
->region
= intel_region_alloc(intel
,
144 expect_accelerated_upload
);
155 struct intel_mipmap_tree
*
156 intel_miptree_create_for_region(struct intel_context
*intel
,
158 GLenum internal_format
,
161 struct intel_region
*region
,
163 GLuint compress_byte
)
165 struct intel_mipmap_tree
*mt
;
167 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
168 first_level
, last_level
,
169 region
->width
, region
->height
, 1,
170 region
->cpp
, compress_byte
,
175 intel_region_reference(&mt
->region
, region
);
181 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
182 struct intel_mipmap_tree
*src
)
186 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
191 intel_miptree_release(struct intel_context
*intel
,
192 struct intel_mipmap_tree
**mt
)
197 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
198 if (--(*mt
)->refcount
<= 0) {
201 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
203 intel_region_release(&((*mt
)->region
));
205 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
206 free((*mt
)->level
[i
].x_offset
);
207 free((*mt
)->level
[i
].y_offset
);
217 * Can the image be pulled into a unified mipmap tree? This mirrors
218 * the completeness test in a lot of ways.
220 * Not sure whether I want to pass gl_texture_image here.
223 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
224 struct gl_texture_image
*image
)
226 GLboolean isCompressed
= _mesa_is_format_compressed(image
->TexFormat
);
227 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
228 GLuint level
= intelImage
->level
;
230 /* Images with borders are never pulled into mipmap trees. */
234 if (image
->InternalFormat
!= mt
->internal_format
||
235 isCompressed
!= mt
->compressed
)
240 _mesa_get_format_bytes(image
->TexFormat
) != mt
->cpp
)
243 /* Test image dimensions against the base level image adjusted for
244 * minification. This will also catch images not present in the
245 * tree, changed targets, etc.
247 if (image
->Width
!= mt
->level
[level
].width
||
248 image
->Height
!= mt
->level
[level
].height
||
249 image
->Depth
!= mt
->level
[level
].depth
)
257 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
261 GLuint w
, GLuint h
, GLuint d
)
263 mt
->level
[level
].width
= w
;
264 mt
->level
[level
].height
= h
;
265 mt
->level
[level
].depth
= d
;
266 mt
->level
[level
].level_x
= x
;
267 mt
->level
[level
].level_y
= y
;
268 mt
->level
[level
].nr_images
= nr_images
;
270 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
271 level
, w
, h
, d
, x
, y
);
274 assert(!mt
->level
[level
].x_offset
);
276 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
277 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
278 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
279 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
284 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
285 GLuint level
, GLuint img
,
288 if (img
== 0 && level
== 0)
289 assert(x
== 0 && y
== 0);
291 assert(img
< mt
->level
[level
].nr_images
);
293 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
294 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
296 DBG("%s level %d img %d pos %d,%d\n",
297 __FUNCTION__
, level
, img
,
298 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
303 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
304 GLuint level
, GLuint face
, GLuint depth
,
305 GLuint
*x
, GLuint
*y
)
307 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
) {
308 *x
= mt
->level
[level
].x_offset
[face
];
309 *y
= mt
->level
[level
].y_offset
[face
];
310 } else if (mt
->target
== GL_TEXTURE_3D
) {
311 *x
= mt
->level
[level
].x_offset
[depth
];
312 *y
= mt
->level
[level
].y_offset
[depth
];
314 *x
= mt
->level
[level
].x_offset
[0];
315 *y
= mt
->level
[level
].y_offset
[0];
320 * Map a teximage in a mipmap tree.
321 * \param row_stride returns row stride in bytes
322 * \param image_stride returns image stride in bytes (for 3D textures).
323 * \param image_offsets pointer to array of pixel offsets from the returned
324 * pointer to each depth image
325 * \return address of mapping
328 intel_miptree_image_map(struct intel_context
* intel
,
329 struct intel_mipmap_tree
* mt
,
332 GLuint
* row_stride
, GLuint
* image_offsets
)
335 DBG("%s \n", __FUNCTION__
);
338 *row_stride
= mt
->region
->pitch
* mt
->cpp
;
340 if (mt
->target
== GL_TEXTURE_3D
) {
343 for (i
= 0; i
< mt
->level
[level
].depth
; i
++) {
345 intel_miptree_get_image_offset(mt
, level
, face
, i
,
347 image_offsets
[i
] = x
+ y
* mt
->region
->pitch
;
350 return intel_region_map(intel
, mt
->region
);
352 assert(mt
->level
[level
].depth
== 1);
353 intel_miptree_get_image_offset(mt
, level
, face
, 0,
355 image_offsets
[0] = 0;
357 return intel_region_map(intel
, mt
->region
) +
358 (x
+ y
* mt
->region
->pitch
) * mt
->cpp
;
364 intel_miptree_image_unmap(struct intel_context
*intel
,
365 struct intel_mipmap_tree
*mt
)
367 DBG("%s\n", __FUNCTION__
);
368 intel_region_unmap(intel
, mt
->region
);
373 * Upload data for a particular image.
376 intel_miptree_image_data(struct intel_context
*intel
,
377 struct intel_mipmap_tree
*dst
,
381 GLuint src_row_pitch
,
382 GLuint src_image_pitch
)
384 const GLuint depth
= dst
->level
[level
].depth
;
387 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
388 for (i
= 0; i
< depth
; i
++) {
389 GLuint dst_x
, dst_y
, height
;
391 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
393 height
= dst
->level
[level
].height
;
395 height
= (height
+ 3) / 4;
397 intel_region_data(intel
,
398 dst
->region
, 0, dst_x
, dst_y
,
401 0, 0, /* source x, y */
402 dst
->level
[level
].width
, height
); /* width, height */
404 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
410 * Copy mipmap image between trees
413 intel_miptree_image_copy(struct intel_context
*intel
,
414 struct intel_mipmap_tree
*dst
,
415 GLuint face
, GLuint level
,
416 struct intel_mipmap_tree
*src
)
418 GLuint width
= src
->level
[level
].width
;
419 GLuint height
= src
->level
[level
].height
;
420 GLuint depth
= src
->level
[level
].depth
;
421 GLuint src_x
, src_y
, dst_x
, dst_y
;
425 if (dst
->compressed
) {
426 GLuint align_w
, align_h
;
428 intel_get_texture_alignment_unit(dst
->internal_format
,
430 height
= (height
+ 3) / 4;
431 width
= ALIGN(width
, align_w
);
434 intel_prepare_render(intel
);
436 for (i
= 0; i
< depth
; i
++) {
437 intel_miptree_get_image_offset(src
, level
, face
, i
, &src_x
, &src_y
);
438 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
439 success
= intel_region_copy(intel
,
440 dst
->region
, 0, dst_x
, dst_y
,
441 src
->region
, 0, src_x
, src_y
,
442 width
, height
, GL_FALSE
,
445 GLubyte
*src_ptr
, *dst_ptr
;
447 src_ptr
= intel_region_map(intel
, src
->region
);
448 dst_ptr
= intel_region_map(intel
, dst
->region
);
450 _mesa_copy_rect(dst_ptr
,
453 dst_x
, dst_y
, width
, height
,
457 intel_region_unmap(intel
, src
->region
);
458 intel_region_unmap(intel
, dst
->region
);