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 "intel_tex.h"
33 #include "main/enums.h"
34 #include "main/formats.h"
36 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
40 target_to_target(GLenum target
)
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
49 return GL_TEXTURE_CUBE_MAP_ARB
;
56 static struct intel_mipmap_tree
*
57 intel_miptree_create_internal(struct intel_context
*intel
,
66 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
67 int compress_byte
= 0;
69 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
70 _mesa_lookup_enum_by_nr(target
),
71 _mesa_get_format_name(format
),
72 first_level
, last_level
, mt
);
74 if (_mesa_is_format_compressed(format
))
75 compress_byte
= intel_compressed_num_bytes(format
);
77 mt
->target
= target_to_target(target
);
79 mt
->first_level
= first_level
;
80 mt
->last_level
= last_level
;
82 mt
->height0
= height0
;
84 mt
->cpp
= compress_byte
? compress_byte
: _mesa_get_format_bytes(mt
->format
);
85 mt
->compressed
= compress_byte
? 1 : 0;
91 i945_miptree_layout(mt
);
93 i915_miptree_layout(mt
);
95 brw_miptree_layout(intel
, mt
);
102 struct intel_mipmap_tree
*
103 intel_miptree_create(struct intel_context
*intel
,
111 GLboolean expect_accelerated_upload
)
113 struct intel_mipmap_tree
*mt
;
114 uint32_t tiling
= I915_TILING_NONE
;
115 GLenum base_format
= _mesa_get_format_base_format(format
);
117 if (intel
->use_texture_tiling
&& !_mesa_is_format_compressed(format
)) {
118 if (intel
->gen
>= 4 &&
119 (base_format
== GL_DEPTH_COMPONENT
||
120 base_format
== GL_DEPTH_STENCIL_EXT
))
121 tiling
= I915_TILING_Y
;
122 else if (width0
>= 64)
123 tiling
= I915_TILING_X
;
126 mt
= intel_miptree_create_internal(intel
, target
, format
,
127 first_level
, last_level
, width0
,
130 * pitch == 0 || height == 0 indicates the null texture
132 if (!mt
|| !mt
->total_width
|| !mt
->total_height
) {
137 mt
->region
= intel_region_alloc(intel
->intelScreen
,
142 expect_accelerated_upload
);
153 struct intel_mipmap_tree
*
154 intel_miptree_create_for_region(struct intel_context
*intel
,
157 struct intel_region
*region
)
159 struct intel_mipmap_tree
*mt
;
161 mt
= intel_miptree_create_internal(intel
, target
, format
,
163 region
->width
, region
->height
, 1);
167 intel_region_reference(&mt
->region
, region
);
173 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
174 struct intel_mipmap_tree
*src
)
179 intel_miptree_release(dst
);
183 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
191 intel_miptree_release(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
));
203 intel_region_release(&((*mt
)->hiz_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 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
227 GLuint level
= intelImage
->base
.Base
.Level
;
229 /* Images with borders are never pulled into mipmap trees. */
233 if (image
->TexFormat
!= mt
->format
)
236 /* Test image dimensions against the base level image adjusted for
237 * minification. This will also catch images not present in the
238 * tree, changed targets, etc.
240 if (image
->Width
!= mt
->level
[level
].width
||
241 image
->Height
!= mt
->level
[level
].height
||
242 image
->Depth
!= mt
->level
[level
].depth
)
250 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
254 GLuint w
, GLuint h
, GLuint d
)
256 mt
->level
[level
].width
= w
;
257 mt
->level
[level
].height
= h
;
258 mt
->level
[level
].depth
= d
;
259 mt
->level
[level
].level_x
= x
;
260 mt
->level
[level
].level_y
= y
;
261 mt
->level
[level
].nr_images
= nr_images
;
263 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
264 level
, w
, h
, d
, x
, y
);
267 assert(!mt
->level
[level
].x_offset
);
269 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
270 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
271 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
272 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
277 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
278 GLuint level
, GLuint img
,
281 if (img
== 0 && level
== 0)
282 assert(x
== 0 && y
== 0);
284 assert(img
< mt
->level
[level
].nr_images
);
286 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
287 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
289 DBG("%s level %d img %d pos %d,%d\n",
290 __FUNCTION__
, level
, img
,
291 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
296 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
297 GLuint level
, GLuint face
, GLuint depth
,
298 GLuint
*x
, GLuint
*y
)
300 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
) {
301 *x
= mt
->level
[level
].x_offset
[face
];
302 *y
= mt
->level
[level
].y_offset
[face
];
303 } else if (mt
->target
== GL_TEXTURE_3D
) {
304 *x
= mt
->level
[level
].x_offset
[depth
];
305 *y
= mt
->level
[level
].y_offset
[depth
];
307 *x
= mt
->level
[level
].x_offset
[0];
308 *y
= mt
->level
[level
].y_offset
[0];
313 * Upload data for a particular image.
316 intel_miptree_image_data(struct intel_context
*intel
,
317 struct intel_mipmap_tree
*dst
,
321 GLuint src_row_pitch
,
322 GLuint src_image_pitch
)
324 const GLuint depth
= dst
->level
[level
].depth
;
327 for (i
= 0; i
< depth
; i
++) {
328 GLuint dst_x
, dst_y
, height
, width
;
330 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
332 height
= dst
->level
[level
].height
;
333 width
= dst
->level
[level
].width
;
334 if (dst
->compressed
) {
335 unsigned int align_w
, align_h
;
337 intel_get_texture_alignment_unit(dst
->format
, &align_w
, &align_h
);
338 height
= (height
+ align_h
- 1) / align_h
;
339 width
= ALIGN(width
, align_w
);
342 DBG("%s: %d/%d %p/%d -> (%d, %d)/%d (%d, %d)\n",
343 __FUNCTION__
, face
, level
,
344 src
, src_row_pitch
* dst
->cpp
,
345 dst_x
, dst_y
, dst
->region
->pitch
* dst
->cpp
,
348 intel_region_data(intel
,
349 dst
->region
, 0, dst_x
, dst_y
,
352 0, 0, /* source x, y */
355 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
361 * Copy mipmap image between trees
364 intel_miptree_image_copy(struct intel_context
*intel
,
365 struct intel_mipmap_tree
*dst
,
366 GLuint face
, GLuint level
,
367 struct intel_mipmap_tree
*src
)
369 GLuint width
= src
->level
[level
].width
;
370 GLuint height
= src
->level
[level
].height
;
371 GLuint depth
= src
->level
[level
].depth
;
372 GLuint src_x
, src_y
, dst_x
, dst_y
;
376 if (dst
->compressed
) {
377 GLuint align_w
, align_h
;
379 intel_get_texture_alignment_unit(dst
->format
, &align_w
, &align_h
);
380 height
= (height
+ 3) / 4;
381 width
= ALIGN(width
, align_w
);
384 intel_prepare_render(intel
);
386 for (i
= 0; i
< depth
; i
++) {
387 intel_miptree_get_image_offset(src
, level
, face
, i
, &src_x
, &src_y
);
388 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
389 success
= intel_region_copy(intel
,
390 dst
->region
, 0, dst_x
, dst_y
,
391 src
->region
, 0, src_x
, src_y
,
392 width
, height
, GL_FALSE
,
395 GLubyte
*src_ptr
, *dst_ptr
;
397 src_ptr
= intel_region_map(intel
, src
->region
);
398 dst_ptr
= intel_region_map(intel
, dst
->region
);
400 _mesa_copy_rect(dst_ptr
,
403 dst_x
, dst_y
, width
, height
,
407 intel_region_unmap(intel
, src
->region
);
408 intel_region_unmap(intel
, dst
->region
);