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 /* Images with borders are never pulled into mipmap trees. */
253 if (image
->TexFormat
!= mt
->format
)
256 intel_miptree_get_dimensions_for_image(image
, &width
, &height
, &depth
);
258 /* Test image dimensions against the base level image adjusted for
259 * minification. This will also catch images not present in the
260 * tree, changed targets, etc.
262 if (width
!= mt
->level
[level
].width
||
263 height
!= mt
->level
[level
].height
||
264 depth
!= mt
->level
[level
].depth
)
272 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
276 GLuint w
, GLuint h
, GLuint d
)
278 mt
->level
[level
].width
= w
;
279 mt
->level
[level
].height
= h
;
280 mt
->level
[level
].depth
= d
;
281 mt
->level
[level
].level_x
= x
;
282 mt
->level
[level
].level_y
= y
;
283 mt
->level
[level
].nr_images
= nr_images
;
285 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
286 level
, w
, h
, d
, x
, y
);
289 assert(!mt
->level
[level
].x_offset
);
291 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
292 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
293 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
294 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
299 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
300 GLuint level
, GLuint img
,
303 if (img
== 0 && level
== 0)
304 assert(x
== 0 && y
== 0);
306 assert(img
< mt
->level
[level
].nr_images
);
308 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
309 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
311 DBG("%s level %d img %d pos %d,%d\n",
312 __FUNCTION__
, level
, img
,
313 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
318 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
319 GLuint level
, GLuint face
, GLuint depth
,
320 GLuint
*x
, GLuint
*y
)
322 switch (mt
->target
) {
323 case GL_TEXTURE_CUBE_MAP_ARB
:
324 *x
= mt
->level
[level
].x_offset
[face
];
325 *y
= mt
->level
[level
].y_offset
[face
];
328 case GL_TEXTURE_2D_ARRAY_EXT
:
329 case GL_TEXTURE_1D_ARRAY_EXT
:
330 assert(depth
< mt
->level
[level
].nr_images
);
331 *x
= mt
->level
[level
].x_offset
[depth
];
332 *y
= mt
->level
[level
].y_offset
[depth
];
335 *x
= mt
->level
[level
].x_offset
[0];
336 *y
= mt
->level
[level
].y_offset
[0];
342 * Copies the image's current data to the given miptree, and associates that
343 * miptree with the image.
346 intel_miptree_copy_teximage(struct intel_context
*intel
,
347 struct intel_texture_image
*intelImage
,
348 struct intel_mipmap_tree
*dst_mt
)
350 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
351 int level
= intelImage
->base
.Base
.Level
;
352 int face
= intelImage
->base
.Base
.Face
;
353 GLuint width
= src_mt
->level
[level
].width
;
354 GLuint height
= src_mt
->level
[level
].height
;
355 GLuint depth
= src_mt
->level
[level
].depth
;
359 if (dst_mt
->compressed
) {
360 unsigned int align_w
, align_h
;
362 intel_get_texture_alignment_unit(intelImage
->base
.Base
.TexFormat
,
364 height
= ALIGN(height
, align_h
) / align_h
;
365 width
= ALIGN(width
, align_w
);
368 for (slice
= 0; slice
< depth
; slice
++) {
369 unsigned int dst_x
, dst_y
, src_x
, src_y
;
371 intel_miptree_get_image_offset(dst_mt
, level
, face
, slice
,
375 /* Copy potentially with the blitter:
377 intel_miptree_get_image_offset(src_mt
, level
, face
, slice
,
380 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
381 src_mt
, src_x
, src_y
, src_mt
->region
->pitch
* src_mt
->region
->cpp
,
382 dst_mt
, dst_x
, dst_y
, dst_mt
->region
->pitch
* dst_mt
->region
->cpp
,
385 if (!intelEmitCopyBlit(intel
,
387 src_mt
->region
->pitch
, src_mt
->region
->bo
,
388 0, src_mt
->region
->tiling
,
389 dst_mt
->region
->pitch
, dst_mt
->region
->bo
,
390 0, dst_mt
->region
->tiling
,
396 fallback_debug("miptree validate blit for %s failed\n",
397 _mesa_get_format_name(intelImage
->base
.Base
.TexFormat
));
398 dst
= intel_region_map(intel
, dst_mt
->region
, GL_MAP_WRITE_BIT
);
399 src
= intel_region_map(intel
, src_mt
->region
, GL_MAP_READ_BIT
);
403 dst_mt
->region
->pitch
,
406 src
, src_mt
->region
->pitch
,
409 intel_region_unmap(intel
, dst_mt
->region
);
410 intel_region_unmap(intel
, src_mt
->region
);
413 dst
= intel_region_map(intel
, dst_mt
->region
, GL_MAP_WRITE_BIT
);
415 DBG("validate upload mt %p -> mt %p %d,%d/%d (%dx%d)\n",
417 dst_mt
, dst_x
, dst_y
, dst_mt
->region
->pitch
* dst_mt
->region
->cpp
,
420 src
= intelImage
->base
.Data
;
421 src
+= (intelImage
->base
.RowStride
*
422 intelImage
->base
.Base
.Height
*
423 dst_mt
->region
->cpp
*
428 dst_mt
->region
->pitch
,
432 intelImage
->base
.RowStride
,
435 intel_region_unmap(intel
, dst_mt
->region
);
439 if (!src_mt
&& intelImage
->base
.Data
) {
440 _mesa_align_free(intelImage
->base
.Data
);
441 intelImage
->base
.Data
= NULL
;
444 intel_miptree_reference(&intelImage
->mt
, dst_mt
);