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_batchbuffer.h"
29 #include "intel_context.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_regions.h"
32 #include "intel_resolve_map.h"
33 #include "intel_span.h"
34 #include "intel_tex_layout.h"
35 #include "intel_tex.h"
36 #include "intel_blit.h"
38 #include "main/enums.h"
39 #include "main/formats.h"
40 #include "main/image.h"
41 #include "main/teximage.h"
43 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
46 target_to_target(GLenum target
)
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
51 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
52 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
55 return GL_TEXTURE_CUBE_MAP_ARB
;
61 static struct intel_mipmap_tree
*
62 intel_miptree_create_internal(struct intel_context
*intel
,
71 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
72 int compress_byte
= 0;
74 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
75 _mesa_lookup_enum_by_nr(target
),
76 _mesa_get_format_name(format
),
77 first_level
, last_level
, mt
);
79 if (_mesa_is_format_compressed(format
))
80 compress_byte
= intel_compressed_num_bytes(format
);
82 mt
->target
= target_to_target(target
);
84 mt
->first_level
= first_level
;
85 mt
->last_level
= last_level
;
87 mt
->height0
= height0
;
88 mt
->cpp
= compress_byte
? compress_byte
: _mesa_get_format_bytes(mt
->format
);
89 mt
->compressed
= compress_byte
? 1 : 0;
92 intel_get_texture_alignment_unit(intel
, format
,
93 &mt
->align_w
, &mt
->align_h
);
95 if (target
== GL_TEXTURE_CUBE_MAP
) {
102 if (format
== MESA_FORMAT_S8
) {
103 /* The stencil buffer has quirky pitch requirements. From Vol 2a,
104 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
105 * The pitch must be set to 2x the value computed based on width, as
106 * the stencil buffer is stored with two rows interleaved.
108 assert(intel
->has_separate_stencil
);
115 i945_miptree_layout(mt
);
117 i915_miptree_layout(mt
);
119 brw_miptree_layout(intel
, mt
);
122 if (_mesa_is_depthstencil_format(_mesa_get_format_base_format(format
)) &&
123 (intel
->must_use_separate_stencil
||
124 (intel
->has_separate_stencil
&&
125 intel
->vtbl
.is_hiz_depth_format(intel
, format
)))) {
126 mt
->stencil_mt
= intel_miptree_create(intel
,
135 if (!mt
->stencil_mt
) {
136 intel_miptree_release(&mt
);
145 struct intel_mipmap_tree
*
146 intel_miptree_create(struct intel_context
*intel
,
154 bool expect_accelerated_upload
)
156 struct intel_mipmap_tree
*mt
;
157 uint32_t tiling
= I915_TILING_NONE
;
158 GLenum base_format
= _mesa_get_format_base_format(format
);
160 if (intel
->use_texture_tiling
&& !_mesa_is_format_compressed(format
)) {
161 if (intel
->gen
>= 4 &&
162 (base_format
== GL_DEPTH_COMPONENT
||
163 base_format
== GL_DEPTH_STENCIL_EXT
))
164 tiling
= I915_TILING_Y
;
165 else if (format
== MESA_FORMAT_S8
)
166 tiling
= I915_TILING_NONE
;
167 else if (width0
>= 64)
168 tiling
= I915_TILING_X
;
171 mt
= intel_miptree_create_internal(intel
, target
, format
,
172 first_level
, last_level
, width0
,
175 * pitch == 0 || height == 0 indicates the null texture
177 if (!mt
|| !mt
->total_width
|| !mt
->total_height
) {
182 mt
->region
= intel_region_alloc(intel
->intelScreen
,
187 expect_accelerated_upload
);
198 struct intel_mipmap_tree
*
199 intel_miptree_create_for_region(struct intel_context
*intel
,
202 struct intel_region
*region
)
204 struct intel_mipmap_tree
*mt
;
206 mt
= intel_miptree_create_internal(intel
, target
, format
,
208 region
->width
, region
->height
, 1);
212 intel_region_reference(&mt
->region
, region
);
217 struct intel_mipmap_tree
*
218 intel_miptree_create_for_renderbuffer(struct intel_context
*intel
,
225 struct intel_region
*region
;
226 struct intel_mipmap_tree
*mt
;
228 region
= intel_region_alloc(intel
->intelScreen
,
229 tiling
, cpp
, width
, height
, true);
233 mt
= intel_miptree_create_for_region(intel
, GL_TEXTURE_2D
, format
, region
);
234 intel_region_release(®ion
);
239 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
240 struct intel_mipmap_tree
*src
)
245 intel_miptree_release(dst
);
249 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
257 intel_miptree_release(struct intel_mipmap_tree
**mt
)
262 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
263 if (--(*mt
)->refcount
<= 0) {
266 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
268 intel_region_release(&((*mt
)->region
));
269 intel_miptree_release(&(*mt
)->stencil_mt
);
270 intel_miptree_release(&(*mt
)->hiz_mt
);
271 intel_resolve_map_clear(&(*mt
)->hiz_map
);
273 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
274 free((*mt
)->level
[i
].slice
);
283 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
284 int *width
, int *height
, int *depth
)
286 switch (image
->TexObject
->Target
) {
287 case GL_TEXTURE_1D_ARRAY
:
288 *width
= image
->Width
;
290 *depth
= image
->Height
;
293 *width
= image
->Width
;
294 *height
= image
->Height
;
295 *depth
= image
->Depth
;
301 * Can the image be pulled into a unified mipmap tree? This mirrors
302 * the completeness test in a lot of ways.
304 * Not sure whether I want to pass gl_texture_image here.
307 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
308 struct gl_texture_image
*image
)
310 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
311 GLuint level
= intelImage
->base
.Base
.Level
;
312 int width
, height
, depth
;
314 if (image
->TexFormat
!= mt
->format
)
317 intel_miptree_get_dimensions_for_image(image
, &width
, &height
, &depth
);
319 /* Test image dimensions against the base level image adjusted for
320 * minification. This will also catch images not present in the
321 * tree, changed targets, etc.
323 if (width
!= mt
->level
[level
].width
||
324 height
!= mt
->level
[level
].height
||
325 depth
!= mt
->level
[level
].depth
)
333 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
336 GLuint w
, GLuint h
, GLuint d
)
338 mt
->level
[level
].width
= w
;
339 mt
->level
[level
].height
= h
;
340 mt
->level
[level
].depth
= d
;
341 mt
->level
[level
].level_x
= x
;
342 mt
->level
[level
].level_y
= y
;
344 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
345 level
, w
, h
, d
, x
, y
);
347 assert(mt
->level
[level
].slice
== NULL
);
349 mt
->level
[level
].slice
= calloc(d
, sizeof(*mt
->level
[0].slice
));
350 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
351 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
356 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
357 GLuint level
, GLuint img
,
360 if (img
== 0 && level
== 0)
361 assert(x
== 0 && y
== 0);
363 assert(img
< mt
->level
[level
].depth
);
365 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
366 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
368 DBG("%s level %d img %d pos %d,%d\n",
369 __FUNCTION__
, level
, img
,
370 mt
->level
[level
].slice
[img
].x_offset
,
371 mt
->level
[level
].slice
[img
].y_offset
);
376 * For cube map textures, either the \c face parameter can be used, of course,
377 * or the cube face can be interpreted as a depth layer and the \c layer
381 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
382 GLuint level
, GLuint face
, GLuint layer
,
383 GLuint
*x
, GLuint
*y
)
388 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
);
393 /* This branch may be taken even if the texture target is a cube map. In
394 * that case, the caller chose to interpret each cube face as a layer.
400 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
401 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
405 intel_miptree_copy_slice(struct intel_context
*intel
,
406 struct intel_mipmap_tree
*dst_mt
,
407 struct intel_mipmap_tree
*src_mt
,
413 gl_format format
= src_mt
->format
;
414 uint32_t width
= src_mt
->level
[level
].width
;
415 uint32_t height
= src_mt
->level
[level
].height
;
417 assert(depth
< src_mt
->level
[level
].depth
);
419 if (dst_mt
->compressed
) {
420 height
= ALIGN(height
, dst_mt
->align_h
) / dst_mt
->align_h
;
421 width
= ALIGN(width
, dst_mt
->align_w
);
424 uint32_t dst_x
, dst_y
, src_x
, src_y
;
425 intel_miptree_get_image_offset(dst_mt
, level
, face
, depth
,
427 intel_miptree_get_image_offset(src_mt
, level
, face
, depth
,
430 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
431 src_mt
, src_x
, src_y
, src_mt
->region
->pitch
* src_mt
->region
->cpp
,
432 dst_mt
, dst_x
, dst_y
, dst_mt
->region
->pitch
* dst_mt
->region
->cpp
,
435 if (!intelEmitCopyBlit(intel
,
437 src_mt
->region
->pitch
, src_mt
->region
->bo
,
438 0, src_mt
->region
->tiling
,
439 dst_mt
->region
->pitch
, dst_mt
->region
->bo
,
440 0, dst_mt
->region
->tiling
,
446 fallback_debug("miptree validate blit for %s failed\n",
447 _mesa_get_format_name(format
));
448 void *dst
= intel_region_map(intel
, dst_mt
->region
, GL_MAP_WRITE_BIT
);
449 void *src
= intel_region_map(intel
, src_mt
->region
, GL_MAP_READ_BIT
);
453 dst_mt
->region
->pitch
,
456 src
, src_mt
->region
->pitch
,
459 intel_region_unmap(intel
, dst_mt
->region
);
460 intel_region_unmap(intel
, src_mt
->region
);
463 if (src_mt
->stencil_mt
) {
464 intel_miptree_copy_slice(intel
,
465 dst_mt
->stencil_mt
, src_mt
->stencil_mt
,
471 * Copies the image's current data to the given miptree, and associates that
472 * miptree with the image.
475 intel_miptree_copy_teximage(struct intel_context
*intel
,
476 struct intel_texture_image
*intelImage
,
477 struct intel_mipmap_tree
*dst_mt
)
479 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
480 int level
= intelImage
->base
.Base
.Level
;
481 int face
= intelImage
->base
.Base
.Face
;
482 GLuint depth
= intelImage
->base
.Base
.Depth
;
484 for (int slice
= 0; slice
< depth
; slice
++) {
485 intel_miptree_copy_slice(intel
, dst_mt
, src_mt
, level
, face
, slice
);
488 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
492 * \param scatter Scatter if true. Gather if false.
494 * \see intel_miptree_s8z24_scatter()
495 * \see intel_miptree_s8z24_gather()
498 intel_miptree_s8z24_scattergather(struct intel_context
*intel
,
499 struct intel_mipmap_tree
*mt
,
504 /* Check function inputs. */
505 assert(level
>= mt
->first_level
);
506 assert(level
<= mt
->last_level
);
507 assert(layer
< mt
->level
[level
].depth
);
509 /* Label everything and its bit layout, just to make the code easier to
512 struct intel_mipmap_tree
*s8_mt
= mt
->stencil_mt
;
513 struct intel_mipmap_level
*s8_level
= &s8_mt
->level
[level
];
514 struct intel_mipmap_slice
*s8_slice
= &s8_mt
->level
[level
].slice
[layer
];
516 struct intel_mipmap_tree
*s8z24_mt
= mt
;
517 struct intel_mipmap_level
*s8z24_level
= &s8z24_mt
->level
[level
];
518 struct intel_mipmap_slice
*s8z24_slice
= &s8z24_mt
->level
[level
].slice
[layer
];
520 /* Check that both miptree levels have the same dimensions. */
521 assert(s8_level
->width
== s8z24_level
->width
);
522 assert(s8_level
->height
== s8z24_level
->height
);
523 assert(s8_level
->depth
== s8z24_level
->depth
);
525 /* Map the buffers. */
526 if (drm_intel_bo_references(intel
->batch
.bo
, s8_mt
->region
->bo
) ||
527 drm_intel_bo_references(intel
->batch
.bo
, s8z24_mt
->region
->bo
)) {
528 intel_batchbuffer_flush(intel
);
530 drm_intel_gem_bo_map_gtt(s8_mt
->region
->bo
);
531 drm_intel_gem_bo_map_gtt(s8z24_mt
->region
->bo
);
533 /* Define the invariant values outside the for loop, because I don't trust
534 * GCC to do it for us.
536 uint8_t *s8_map
= s8_mt
->region
->bo
->virtual
538 + s8_slice
->y_offset
;
540 uint8_t *s8z24_map
= s8z24_mt
->region
->bo
->virtual
541 + s8z24_slice
->x_offset
542 + s8z24_slice
->y_offset
;
544 ptrdiff_t s8z24_stride
= s8z24_mt
->region
->pitch
* s8z24_mt
->region
->cpp
;
546 uint32_t w
= s8_level
->width
;
547 uint32_t h
= s8_level
->height
;
549 for (uint32_t y
= 0; y
< h
; ++y
) {
550 for (uint32_t x
= 0; x
< w
; ++x
) {
551 ptrdiff_t s8_offset
= intel_offset_S8(s8_mt
->region
->pitch
, x
, y
);
552 ptrdiff_t s8z24_offset
= y
* s8z24_stride
556 s8_map
[s8_offset
] = s8z24_map
[s8z24_offset
];
558 s8z24_map
[s8z24_offset
] = s8_map
[s8_offset
];
563 drm_intel_gem_bo_unmap_gtt(s8_mt
->region
->bo
);
564 drm_intel_gem_bo_unmap_gtt(s8z24_mt
->region
->bo
);
568 intel_miptree_s8z24_scatter(struct intel_context
*intel
,
569 struct intel_mipmap_tree
*mt
,
573 intel_miptree_s8z24_scattergather(intel
, mt
, level
, layer
, true);
577 intel_miptree_s8z24_gather(struct intel_context
*intel
,
578 struct intel_mipmap_tree
*mt
,
582 intel_miptree_s8z24_scattergather(intel
, mt
, level
, layer
, false);
586 intel_miptree_alloc_hiz(struct intel_context
*intel
,
587 struct intel_mipmap_tree
*mt
)
589 assert(mt
->hiz_mt
== NULL
);
590 mt
->hiz_mt
= intel_miptree_create(intel
,
603 /* Mark that all slices need a HiZ resolve. */
604 struct intel_resolve_map
*head
= &mt
->hiz_map
;
605 for (int level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
606 for (int layer
= 0; layer
< mt
->level
[level
].depth
; ++layer
) {
607 head
->next
= malloc(sizeof(*head
->next
));
608 head
->next
->prev
= head
;
609 head
->next
->next
= NULL
;
614 head
->need
= INTEL_NEED_HIZ_RESOLVE
;
622 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree
*mt
,
626 intel_miptree_check_level_layer(mt
, level
, layer
);
631 intel_resolve_map_set(&mt
->hiz_map
,
632 level
, layer
, INTEL_NEED_HIZ_RESOLVE
);
637 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree
*mt
,
641 intel_miptree_check_level_layer(mt
, level
, layer
);
646 intel_resolve_map_set(&mt
->hiz_map
,
647 level
, layer
, INTEL_NEED_DEPTH_RESOLVE
);
650 typedef void (*resolve_func_t
)(struct intel_context
*intel
,
651 struct intel_mipmap_tree
*mt
,
656 intel_miptree_slice_resolve(struct intel_context
*intel
,
657 struct intel_mipmap_tree
*mt
,
660 enum intel_need_resolve need
,
663 intel_miptree_check_level_layer(mt
, level
, layer
);
665 struct intel_resolve_map
*item
=
666 intel_resolve_map_get(&mt
->hiz_map
, level
, layer
);
668 if (!item
|| item
->need
!= need
)
671 func(intel
, mt
, level
, layer
);
672 intel_resolve_map_remove(item
);
677 intel_miptree_slice_resolve_hiz(struct intel_context
*intel
,
678 struct intel_mipmap_tree
*mt
,
682 return intel_miptree_slice_resolve(intel
, mt
, level
, layer
,
683 INTEL_NEED_HIZ_RESOLVE
,
684 intel
->vtbl
.resolve_hiz_slice
);
688 intel_miptree_slice_resolve_depth(struct intel_context
*intel
,
689 struct intel_mipmap_tree
*mt
,
693 return intel_miptree_slice_resolve(intel
, mt
, level
, layer
,
694 INTEL_NEED_DEPTH_RESOLVE
,
695 intel
->vtbl
.resolve_depth_slice
);
699 intel_miptree_all_slices_resolve(struct intel_context
*intel
,
700 struct intel_mipmap_tree
*mt
,
701 enum intel_need_resolve need
,
704 bool did_resolve
= false;
705 struct intel_resolve_map
*i
;
707 for (i
= mt
->hiz_map
.next
; i
; i
= i
->next
) {
710 func(intel
, mt
, i
->level
, i
->layer
);
711 intel_resolve_map_remove(i
);
719 intel_miptree_all_slices_resolve_hiz(struct intel_context
*intel
,
720 struct intel_mipmap_tree
*mt
)
722 return intel_miptree_all_slices_resolve(intel
, mt
,
723 INTEL_NEED_HIZ_RESOLVE
,
724 intel
->vtbl
.resolve_hiz_slice
);
728 intel_miptree_all_slices_resolve_depth(struct intel_context
*intel
,
729 struct intel_mipmap_tree
*mt
)
731 return intel_miptree_all_slices_resolve(intel
, mt
,
732 INTEL_NEED_DEPTH_RESOLVE
,
733 intel
->vtbl
.resolve_depth_slice
);
737 intel_miptree_map(struct intel_context
*intel
,
738 struct intel_mipmap_tree
*mt
,
749 struct intel_miptree_map
*map
;
752 unsigned int image_x
, image_y
;
754 map
= calloc(1, sizeof(struct intel_miptree_map
));
761 assert(!mt
->level
[level
].slice
[slice
].map
);
762 mt
->level
[level
].slice
[slice
].map
= map
;
769 if (mt
->stencil_mt
) {
770 /* The miptree has depthstencil format, but uses separate stencil. The
771 * embedded stencil miptree contains the real stencil data, so gather
772 * that into the depthstencil miptree.
774 * FIXME: Avoid the gather if the texture is mapped as write-only.
776 intel_miptree_s8z24_gather(intel
, mt
, level
, slice
);
779 intel_miptree_slice_resolve_depth(intel
, mt
, level
, slice
);
780 if (mode
& GL_MAP_WRITE_BIT
) {
781 intel_miptree_slice_set_needs_hiz_resolve(mt
, level
, slice
);
784 /* For compressed formats, the stride is the number of bytes per
785 * row of blocks. intel_miptree_get_image_offset() already does
788 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
792 base
= intel_region_map(intel
, mt
->region
, mode
);
793 /* Note that in the case of cube maps, the caller must have passed the slice
794 * number referencing the face.
796 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
800 map
->stride
= mt
->region
->pitch
* mt
->cpp
;
801 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
804 *out_stride
= map
->stride
;
806 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
807 map
->x
, map
->y
, map
->w
, map
->h
,
808 mt
, _mesa_get_format_name(mt
->format
),
809 x
, y
, map
->ptr
, map
->stride
);
813 intel_miptree_unmap(struct intel_context
*intel
,
814 struct intel_mipmap_tree
*mt
,
818 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
823 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__
,
824 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
826 intel_region_unmap(intel
, mt
->region
);
828 if (mt
->stencil_mt
) {
829 /* The miptree has depthstencil format, but uses separate stencil. The
830 * embedded stencil miptree must contain the real stencil data after
831 * unmapping, so copy it from the depthstencil miptree into the stencil
834 * FIXME: Avoid the scatter if the texture was mapped as read-only.
836 intel_miptree_s8z24_scatter(intel
, mt
, level
, slice
);
839 mt
->level
[level
].slice
[slice
].map
= NULL
;