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"
33 #include "main/formats.h"
35 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
39 target_to_target(GLenum target
)
42 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
46 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
47 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
48 return GL_TEXTURE_CUBE_MAP_ARB
;
55 static struct intel_mipmap_tree
*
56 intel_miptree_create_internal(struct intel_context
*intel
,
58 GLenum internal_format
,
63 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
67 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
69 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
70 _mesa_lookup_enum_by_nr(target
),
71 _mesa_lookup_enum_by_nr(internal_format
),
72 first_level
, last_level
, mt
);
74 mt
->target
= target_to_target(target
);
75 mt
->internal_format
= internal_format
;
76 mt
->first_level
= first_level
;
77 mt
->last_level
= last_level
;
79 mt
->height0
= height0
;
81 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
82 mt
->compressed
= compress_byte
? 1 : 0;
87 ok
= i945_miptree_layout(intel
, mt
, tiling
);
89 ok
= i915_miptree_layout(intel
, mt
, tiling
);
91 ok
= brw_miptree_layout(intel
, mt
, tiling
);
96 DBG("%s not okay - returning NULL\n", __FUNCTION__
);
104 struct intel_mipmap_tree
*
105 intel_miptree_create(struct intel_context
*intel
,
108 GLenum internal_format
,
113 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
114 GLboolean expect_accelerated_upload
)
116 struct intel_mipmap_tree
*mt
;
117 uint32_t tiling
= I915_TILING_NONE
;
119 if (intel
->use_texture_tiling
&& compress_byte
== 0) {
120 if (intel
->gen
>= 4 &&
121 (base_format
== GL_DEPTH_COMPONENT
||
122 base_format
== GL_DEPTH_STENCIL_EXT
))
123 tiling
= I915_TILING_Y
;
124 else if (width0
>= 64)
125 tiling
= I915_TILING_X
;
128 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
129 first_level
, last_level
, width0
,
130 height0
, depth0
, cpp
, compress_byte
,
133 * pitch == 0 || height == 0 indicates the null texture
135 if (!mt
|| !mt
->total_height
) {
140 mt
->region
= intel_region_alloc(intel
->intelScreen
,
145 expect_accelerated_upload
);
156 struct intel_mipmap_tree
*
157 intel_miptree_create_for_region(struct intel_context
*intel
,
159 GLenum internal_format
,
160 struct intel_region
*region
,
162 GLuint compress_byte
)
164 struct intel_mipmap_tree
*mt
;
166 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
168 region
->width
, region
->height
, 1,
169 region
->cpp
, compress_byte
,
174 intel_region_reference(&mt
->region
, region
);
180 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
181 struct intel_mipmap_tree
*src
)
185 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
190 intel_miptree_release(struct intel_context
*intel
,
191 struct intel_mipmap_tree
**mt
)
196 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
197 if (--(*mt
)->refcount
<= 0) {
200 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
202 intel_region_release(&((*mt
)->region
));
204 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
205 free((*mt
)->level
[i
].x_offset
);
206 free((*mt
)->level
[i
].y_offset
);
216 * Can the image be pulled into a unified mipmap tree? This mirrors
217 * the completeness test in a lot of ways.
219 * Not sure whether I want to pass gl_texture_image here.
222 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
223 struct gl_texture_image
*image
)
225 GLboolean isCompressed
= _mesa_is_format_compressed(image
->TexFormat
);
226 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
227 GLuint level
= intelImage
->level
;
229 /* Images with borders are never pulled into mipmap trees. */
233 if (image
->InternalFormat
!= mt
->internal_format
||
234 isCompressed
!= mt
->compressed
)
239 _mesa_get_format_bytes(image
->TexFormat
) != mt
->cpp
)
242 /* Test image dimensions against the base level image adjusted for
243 * minification. This will also catch images not present in the
244 * tree, changed targets, etc.
246 if (image
->Width
!= mt
->level
[level
].width
||
247 image
->Height
!= mt
->level
[level
].height
||
248 image
->Depth
!= mt
->level
[level
].depth
)
256 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
260 GLuint w
, GLuint h
, GLuint d
)
262 mt
->level
[level
].width
= w
;
263 mt
->level
[level
].height
= h
;
264 mt
->level
[level
].depth
= d
;
265 mt
->level
[level
].level_x
= x
;
266 mt
->level
[level
].level_y
= y
;
267 mt
->level
[level
].nr_images
= nr_images
;
269 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
270 level
, w
, h
, d
, x
, y
);
273 assert(!mt
->level
[level
].x_offset
);
275 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
276 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
277 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
278 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
283 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
284 GLuint level
, GLuint img
,
287 if (img
== 0 && level
== 0)
288 assert(x
== 0 && y
== 0);
290 assert(img
< mt
->level
[level
].nr_images
);
292 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
293 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
295 DBG("%s level %d img %d pos %d,%d\n",
296 __FUNCTION__
, level
, img
,
297 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
302 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
303 GLuint level
, GLuint face
, GLuint depth
,
304 GLuint
*x
, GLuint
*y
)
306 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
) {
307 *x
= mt
->level
[level
].x_offset
[face
];
308 *y
= mt
->level
[level
].y_offset
[face
];
309 } else if (mt
->target
== GL_TEXTURE_3D
) {
310 *x
= mt
->level
[level
].x_offset
[depth
];
311 *y
= mt
->level
[level
].y_offset
[depth
];
313 *x
= mt
->level
[level
].x_offset
[0];
314 *y
= mt
->level
[level
].y_offset
[0];
319 * Map a teximage in a mipmap tree.
320 * \param row_stride returns row stride in bytes
321 * \param image_stride returns image stride in bytes (for 3D textures).
322 * \param image_offsets pointer to array of pixel offsets from the returned
323 * pointer to each depth image
324 * \return address of mapping
327 intel_miptree_image_map(struct intel_context
* intel
,
328 struct intel_mipmap_tree
* mt
,
331 GLuint
* row_stride
, GLuint
* image_offsets
)
336 *row_stride
= mt
->region
->pitch
* mt
->cpp
;
338 if (mt
->target
== GL_TEXTURE_3D
) {
341 for (i
= 0; i
< mt
->level
[level
].depth
; i
++) {
343 intel_miptree_get_image_offset(mt
, level
, face
, i
,
345 image_offsets
[i
] = x
+ y
* mt
->region
->pitch
;
348 DBG("%s \n", __FUNCTION__
);
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 DBG("%s: (%d,%d) -> (%d, %d)/%d\n",
358 __FUNCTION__
, face
, level
, x
, y
, mt
->region
->pitch
* mt
->cpp
);
360 return intel_region_map(intel
, mt
->region
) +
361 (x
+ y
* mt
->region
->pitch
) * mt
->cpp
;
367 intel_miptree_image_unmap(struct intel_context
*intel
,
368 struct intel_mipmap_tree
*mt
)
370 DBG("%s\n", __FUNCTION__
);
371 intel_region_unmap(intel
, mt
->region
);
376 * Upload data for a particular image.
379 intel_miptree_image_data(struct intel_context
*intel
,
380 struct intel_mipmap_tree
*dst
,
384 GLuint src_row_pitch
,
385 GLuint src_image_pitch
)
387 const GLuint depth
= dst
->level
[level
].depth
;
390 for (i
= 0; i
< depth
; i
++) {
391 GLuint dst_x
, dst_y
, height
;
393 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
395 height
= dst
->level
[level
].height
;
397 height
= (height
+ 3) / 4;
399 DBG("%s: %d/%d %p/%d -> (%d, %d)/%d (%d, %d)\n",
400 __FUNCTION__
, face
, level
,
401 src
, src_row_pitch
* dst
->cpp
,
402 dst_x
, dst_y
, dst
->region
->pitch
* dst
->cpp
,
403 dst
->level
[level
].width
, height
);
405 intel_region_data(intel
,
406 dst
->region
, 0, dst_x
, dst_y
,
409 0, 0, /* source x, y */
410 dst
->level
[level
].width
, height
); /* width, height */
412 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
418 * Copy mipmap image between trees
421 intel_miptree_image_copy(struct intel_context
*intel
,
422 struct intel_mipmap_tree
*dst
,
423 GLuint face
, GLuint level
,
424 struct intel_mipmap_tree
*src
)
426 GLuint width
= src
->level
[level
].width
;
427 GLuint height
= src
->level
[level
].height
;
428 GLuint depth
= src
->level
[level
].depth
;
429 GLuint src_x
, src_y
, dst_x
, dst_y
;
433 if (dst
->compressed
) {
434 GLuint align_w
, align_h
;
436 intel_get_texture_alignment_unit(dst
->internal_format
,
438 height
= (height
+ 3) / 4;
439 width
= ALIGN(width
, align_w
);
442 intel_prepare_render(intel
);
444 for (i
= 0; i
< depth
; i
++) {
445 intel_miptree_get_image_offset(src
, level
, face
, i
, &src_x
, &src_y
);
446 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
447 success
= intel_region_copy(intel
,
448 dst
->region
, 0, dst_x
, dst_y
,
449 src
->region
, 0, src_x
, src_y
,
450 width
, height
, GL_FALSE
,
453 GLubyte
*src_ptr
, *dst_ptr
;
455 src_ptr
= intel_region_map(intel
, src
->region
);
456 dst_ptr
= intel_region_map(intel
, dst
->region
);
458 _mesa_copy_rect(dst_ptr
,
461 dst_x
, dst_y
, width
, height
,
465 intel_region_unmap(intel
, src
->region
);
466 intel_region_unmap(intel
, dst
->region
);