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 "intel_blit.h"
34 #include "main/enums.h"
35 #include "main/formats.h"
36 #include "main/teximage.h"
38 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
42 target_to_target(GLenum target
)
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
51 return GL_TEXTURE_CUBE_MAP_ARB
;
58 static struct intel_mipmap_tree
*
59 intel_miptree_create_internal(struct intel_context
*intel
,
68 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
69 int compress_byte
= 0;
71 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
72 _mesa_lookup_enum_by_nr(target
),
73 _mesa_get_format_name(format
),
74 first_level
, last_level
, mt
);
76 if (_mesa_is_format_compressed(format
))
77 compress_byte
= intel_compressed_num_bytes(format
);
79 mt
->target
= target_to_target(target
);
81 mt
->first_level
= first_level
;
82 mt
->last_level
= last_level
;
84 mt
->height0
= height0
;
86 mt
->cpp
= compress_byte
? compress_byte
: _mesa_get_format_bytes(mt
->format
);
87 mt
->compressed
= compress_byte
? 1 : 0;
93 i945_miptree_layout(mt
);
95 i915_miptree_layout(mt
);
97 brw_miptree_layout(intel
, mt
);
104 struct intel_mipmap_tree
*
105 intel_miptree_create(struct intel_context
*intel
,
113 bool expect_accelerated_upload
)
115 struct intel_mipmap_tree
*mt
;
116 uint32_t tiling
= I915_TILING_NONE
;
117 GLenum base_format
= _mesa_get_format_base_format(format
);
119 if (intel
->use_texture_tiling
&& !_mesa_is_format_compressed(format
)) {
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
, format
,
129 first_level
, last_level
, width0
,
132 * pitch == 0 || height == 0 indicates the null texture
134 if (!mt
|| !mt
->total_width
|| !mt
->total_height
) {
139 mt
->region
= intel_region_alloc(intel
->intelScreen
,
144 expect_accelerated_upload
);
155 struct intel_mipmap_tree
*
156 intel_miptree_create_for_region(struct intel_context
*intel
,
159 struct intel_region
*region
)
161 struct intel_mipmap_tree
*mt
;
163 mt
= intel_miptree_create_internal(intel
, target
, format
,
165 region
->width
, region
->height
, 1);
169 intel_region_reference(&mt
->region
, region
);
175 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
176 struct intel_mipmap_tree
*src
)
181 intel_miptree_release(dst
);
185 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
193 intel_miptree_release(struct intel_mipmap_tree
**mt
)
198 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
199 if (--(*mt
)->refcount
<= 0) {
202 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
204 intel_region_release(&((*mt
)->region
));
205 intel_region_release(&((*mt
)->hiz_region
));
207 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
208 free((*mt
)->level
[i
].x_offset
);
209 free((*mt
)->level
[i
].y_offset
);
218 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
219 int *width
, int *height
, int *depth
)
221 switch (image
->TexObject
->Target
) {
222 case GL_TEXTURE_1D_ARRAY
:
223 *width
= image
->Width
;
225 *depth
= image
->Height
;
228 *width
= image
->Width
;
229 *height
= image
->Height
;
230 *depth
= image
->Depth
;
236 * Can the image be pulled into a unified mipmap tree? This mirrors
237 * the completeness test in a lot of ways.
239 * Not sure whether I want to pass gl_texture_image here.
242 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
243 struct gl_texture_image
*image
)
245 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
246 GLuint level
= intelImage
->base
.Base
.Level
;
247 int width
, height
, depth
;
249 if (image
->TexFormat
!= mt
->format
)
252 intel_miptree_get_dimensions_for_image(image
, &width
, &height
, &depth
);
254 /* Test image dimensions against the base level image adjusted for
255 * minification. This will also catch images not present in the
256 * tree, changed targets, etc.
258 if (width
!= mt
->level
[level
].width
||
259 height
!= mt
->level
[level
].height
||
260 depth
!= mt
->level
[level
].depth
)
268 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
272 GLuint w
, GLuint h
, GLuint d
)
274 mt
->level
[level
].width
= w
;
275 mt
->level
[level
].height
= h
;
276 mt
->level
[level
].depth
= d
;
277 mt
->level
[level
].level_x
= x
;
278 mt
->level
[level
].level_y
= y
;
279 mt
->level
[level
].nr_images
= nr_images
;
281 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
282 level
, w
, h
, d
, x
, y
);
285 assert(!mt
->level
[level
].x_offset
);
287 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
288 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
289 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
290 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
295 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
296 GLuint level
, GLuint img
,
299 if (img
== 0 && level
== 0)
300 assert(x
== 0 && y
== 0);
302 assert(img
< mt
->level
[level
].nr_images
);
304 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
305 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
307 DBG("%s level %d img %d pos %d,%d\n",
308 __FUNCTION__
, level
, img
,
309 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
314 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
315 GLuint level
, GLuint face
, GLuint depth
,
316 GLuint
*x
, GLuint
*y
)
318 switch (mt
->target
) {
319 case GL_TEXTURE_CUBE_MAP_ARB
:
320 *x
= mt
->level
[level
].x_offset
[face
];
321 *y
= mt
->level
[level
].y_offset
[face
];
324 case GL_TEXTURE_2D_ARRAY_EXT
:
325 case GL_TEXTURE_1D_ARRAY_EXT
:
326 assert(depth
< mt
->level
[level
].nr_images
);
327 *x
= mt
->level
[level
].x_offset
[depth
];
328 *y
= mt
->level
[level
].y_offset
[depth
];
331 *x
= mt
->level
[level
].x_offset
[0];
332 *y
= mt
->level
[level
].y_offset
[0];
338 * Copies the image's current data to the given miptree, and associates that
339 * miptree with the image.
342 intel_miptree_copy_teximage(struct intel_context
*intel
,
343 struct intel_texture_image
*intelImage
,
344 struct intel_mipmap_tree
*dst_mt
)
346 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
347 int level
= intelImage
->base
.Base
.Level
;
348 int face
= intelImage
->base
.Base
.Face
;
349 GLuint width
= src_mt
->level
[level
].width
;
350 GLuint height
= src_mt
->level
[level
].height
;
351 GLuint depth
= src_mt
->level
[level
].depth
;
355 if (dst_mt
->compressed
) {
356 unsigned int align_w
, align_h
;
358 intel_get_texture_alignment_unit(intelImage
->base
.Base
.TexFormat
,
360 height
= ALIGN(height
, align_h
) / align_h
;
361 width
= ALIGN(width
, align_w
);
364 for (slice
= 0; slice
< depth
; slice
++) {
365 unsigned int dst_x
, dst_y
, src_x
, src_y
;
367 intel_miptree_get_image_offset(dst_mt
, level
, face
, slice
,
370 /* Copy potentially with the blitter:
372 intel_miptree_get_image_offset(src_mt
, level
, face
, slice
,
375 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
376 src_mt
, src_x
, src_y
, src_mt
->region
->pitch
* src_mt
->region
->cpp
,
377 dst_mt
, dst_x
, dst_y
, dst_mt
->region
->pitch
* dst_mt
->region
->cpp
,
380 if (!intelEmitCopyBlit(intel
,
382 src_mt
->region
->pitch
, src_mt
->region
->bo
,
383 0, src_mt
->region
->tiling
,
384 dst_mt
->region
->pitch
, dst_mt
->region
->bo
,
385 0, dst_mt
->region
->tiling
,
391 fallback_debug("miptree validate blit for %s failed\n",
392 _mesa_get_format_name(intelImage
->base
.Base
.TexFormat
));
393 dst
= intel_region_map(intel
, dst_mt
->region
, GL_MAP_WRITE_BIT
);
394 src
= intel_region_map(intel
, src_mt
->region
, GL_MAP_READ_BIT
);
398 dst_mt
->region
->pitch
,
401 src
, src_mt
->region
->pitch
,
404 intel_region_unmap(intel
, dst_mt
->region
);
405 intel_region_unmap(intel
, src_mt
->region
);
409 intel_miptree_reference(&intelImage
->mt
, dst_mt
);