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/glformats.h"
41 #include "main/texcompress_etc.h"
42 #include "main/teximage.h"
44 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
47 target_to_target(GLenum target
)
50 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
51 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
52 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
53 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
54 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
55 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
56 return GL_TEXTURE_CUBE_MAP_ARB
;
63 * @param for_region Indicates that the caller is
64 * intel_miptree_create_for_region(). If true, then do not create
67 static struct intel_mipmap_tree
*
68 intel_miptree_create_internal(struct intel_context
*intel
,
78 enum intel_msaa_layout msaa_layout
)
80 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
81 int compress_byte
= 0;
83 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
84 _mesa_lookup_enum_by_nr(target
),
85 _mesa_get_format_name(format
),
86 first_level
, last_level
, mt
);
88 if (_mesa_is_format_compressed(format
))
89 compress_byte
= intel_compressed_num_bytes(format
);
91 mt
->target
= target_to_target(target
);
93 mt
->first_level
= first_level
;
94 mt
->last_level
= last_level
;
96 mt
->height0
= height0
;
97 mt
->cpp
= compress_byte
? compress_byte
: _mesa_get_format_bytes(mt
->format
);
98 mt
->num_samples
= num_samples
;
99 mt
->compressed
= compress_byte
? 1 : 0;
100 mt
->msaa_layout
= msaa_layout
;
103 /* array_spacing_lod0 is only used for non-IMS MSAA surfaces. TODO: can we
106 switch (msaa_layout
) {
107 case INTEL_MSAA_LAYOUT_NONE
:
108 case INTEL_MSAA_LAYOUT_IMS
:
109 mt
->array_spacing_lod0
= false;
111 case INTEL_MSAA_LAYOUT_UMS
:
112 case INTEL_MSAA_LAYOUT_CMS
:
113 mt
->array_spacing_lod0
= true;
117 if (target
== GL_TEXTURE_CUBE_MAP
) {
125 _mesa_is_depthstencil_format(_mesa_get_format_base_format(format
)) &&
126 (intel
->must_use_separate_stencil
||
127 (intel
->has_separate_stencil
&&
128 intel
->vtbl
.is_hiz_depth_format(intel
, format
)))) {
129 /* MSAA stencil surfaces always use IMS layout. */
130 enum intel_msaa_layout msaa_layout
=
131 num_samples
> 1 ? INTEL_MSAA_LAYOUT_IMS
: INTEL_MSAA_LAYOUT_NONE
;
132 mt
->stencil_mt
= intel_miptree_create(intel
,
143 if (!mt
->stencil_mt
) {
144 intel_miptree_release(&mt
);
148 /* Fix up the Z miptree format for how we're splitting out separate
149 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
151 if (mt
->format
== MESA_FORMAT_S8_Z24
) {
152 mt
->format
= MESA_FORMAT_X8_Z24
;
153 } else if (mt
->format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
154 mt
->format
= MESA_FORMAT_Z32_FLOAT
;
157 _mesa_problem(NULL
, "Unknown format %s in separate stencil mt\n",
158 _mesa_get_format_name(mt
->format
));
162 intel_get_texture_alignment_unit(intel
, mt
->format
,
163 &mt
->align_w
, &mt
->align_h
);
168 i945_miptree_layout(mt
);
170 i915_miptree_layout(mt
);
172 brw_miptree_layout(intel
, mt
);
179 struct intel_mipmap_tree
*
180 intel_miptree_create(struct intel_context
*intel
,
188 bool expect_accelerated_upload
,
190 enum intel_msaa_layout msaa_layout
)
192 struct intel_mipmap_tree
*mt
;
193 uint32_t tiling
= I915_TILING_NONE
;
195 bool wraps_etc1
= false;
197 if (format
== MESA_FORMAT_ETC1_RGB8
) {
198 format
= MESA_FORMAT_RGBX8888_REV
;
202 base_format
= _mesa_get_format_base_format(format
);
204 if (intel
->use_texture_tiling
&& !_mesa_is_format_compressed(format
)) {
205 if (intel
->gen
>= 4 &&
206 (base_format
== GL_DEPTH_COMPONENT
||
207 base_format
== GL_DEPTH_STENCIL_EXT
))
208 tiling
= I915_TILING_Y
;
209 else if (msaa_layout
!= INTEL_MSAA_LAYOUT_NONE
) {
210 /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
213 * [DevSNB+]: For multi-sample render targets, this field must be
214 * 1. MSRTs can only be tiled.
216 * Our usual reason for preferring X tiling (fast blits using the
217 * blitting engine) doesn't apply to MSAA, since we'll generally be
218 * downsampling or upsampling when blitting between the MSAA buffer
219 * and another buffer, and the blitting engine doesn't support that.
220 * So use Y tiling, since it makes better use of the cache.
222 tiling
= I915_TILING_Y
;
223 } else if (width0
>= 64)
224 tiling
= I915_TILING_X
;
227 if (format
== MESA_FORMAT_S8
) {
228 /* The stencil buffer is W tiled. However, we request from the kernel a
229 * non-tiled buffer because the GTT is incapable of W fencing. So round
230 * up the width and height to match the size of W tiles (64x64).
232 tiling
= I915_TILING_NONE
;
233 width0
= ALIGN(width0
, 64);
234 height0
= ALIGN(height0
, 64);
237 mt
= intel_miptree_create_internal(intel
, target
, format
,
238 first_level
, last_level
, width0
,
240 false, num_samples
, msaa_layout
);
242 * pitch == 0 || height == 0 indicates the null texture
244 if (!mt
|| !mt
->total_width
|| !mt
->total_height
) {
245 intel_miptree_release(&mt
);
249 mt
->wraps_etc1
= wraps_etc1
;
250 mt
->region
= intel_region_alloc(intel
->intelScreen
,
255 expect_accelerated_upload
);
259 intel_miptree_release(&mt
);
267 struct intel_mipmap_tree
*
268 intel_miptree_create_for_region(struct intel_context
*intel
,
271 struct intel_region
*region
)
273 struct intel_mipmap_tree
*mt
;
275 mt
= intel_miptree_create_internal(intel
, target
, format
,
277 region
->width
, region
->height
, 1,
278 true, 0 /* num_samples */,
279 INTEL_MSAA_LAYOUT_NONE
);
283 intel_region_reference(&mt
->region
, region
);
289 * Determine which MSAA layout should be used by the MSAA surface being
290 * created, based on the chip generation and the surface type.
292 static enum intel_msaa_layout
293 compute_msaa_layout(struct intel_context
*intel
, gl_format format
)
295 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
297 return INTEL_MSAA_LAYOUT_IMS
;
299 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
300 switch (_mesa_get_format_base_format(format
)) {
301 case GL_DEPTH_COMPONENT
:
302 case GL_STENCIL_INDEX
:
303 case GL_DEPTH_STENCIL
:
304 return INTEL_MSAA_LAYOUT_IMS
;
306 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
308 * This field must be set to 0 for all SINT MSRTs when all RT channels
311 * In practice this means that we have to disable MCS for all signed
312 * integer MSAA buffers. The alternative, to disable MCS only when one
313 * of the render target channels is disabled, is impractical because it
314 * would require converting between CMS and UMS MSAA layouts on the fly,
315 * which is expensive.
317 if (_mesa_get_format_datatype(format
) == GL_INT
) {
318 /* TODO: is this workaround needed for future chipsets? */
319 assert(intel
->gen
== 7);
320 return INTEL_MSAA_LAYOUT_UMS
;
322 return INTEL_MSAA_LAYOUT_CMS
;
327 struct intel_mipmap_tree
*
328 intel_miptree_create_for_renderbuffer(struct intel_context
*intel
,
332 uint32_t num_samples
)
334 struct intel_mipmap_tree
*mt
;
336 enum intel_msaa_layout msaa_layout
= INTEL_MSAA_LAYOUT_NONE
;
339 if (num_samples
> 1) {
340 /* Adjust width/height/depth for MSAA */
341 msaa_layout
= compute_msaa_layout(intel
, format
);
342 if (msaa_layout
== INTEL_MSAA_LAYOUT_IMS
) {
343 /* In the Sandy Bridge PRM, volume 4, part 1, page 31, it says:
345 * "Any of the other messages (sample*, LOD, load4) used with a
346 * (4x) multisampled surface will in-effect sample a surface with
347 * double the height and width as that indicated in the surface
348 * state. Each pixel position on the original-sized surface is
349 * replaced with a 2x2 of samples with the following arrangement:
354 * Thus, when sampling from a multisampled texture, it behaves as
355 * though the layout in memory for (x,y,sample) is:
357 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
358 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
360 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
361 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
363 * However, the actual layout of multisampled data in memory is:
365 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
366 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
368 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
369 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
371 * This pattern repeats for each 2x2 pixel block.
373 * As a result, when calculating the size of our 4-sample buffer for
374 * an odd width or height, we have to align before scaling up because
375 * sample 3 is in that bottom right 2x2 block.
377 switch (num_samples
) {
379 width
= ALIGN(width
, 2) * 2;
380 height
= ALIGN(height
, 2) * 2;
383 width
= ALIGN(width
, 2) * 4;
384 height
= ALIGN(height
, 2) * 2;
387 /* num_samples should already have been quantized to 0, 1, 4, or
393 /* Non-interleaved */
398 mt
= intel_miptree_create(intel
, GL_TEXTURE_2D
, format
, 0, 0,
399 width
, height
, depth
, true, num_samples
,
404 if (intel
->vtbl
.is_hiz_depth_format(intel
, format
)) {
405 ok
= intel_miptree_alloc_hiz(intel
, mt
, num_samples
);
410 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
411 ok
= intel_miptree_alloc_mcs(intel
, mt
, num_samples
);
419 intel_miptree_release(&mt
);
424 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
425 struct intel_mipmap_tree
*src
)
430 intel_miptree_release(dst
);
434 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
442 intel_miptree_release(struct intel_mipmap_tree
**mt
)
447 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
448 if (--(*mt
)->refcount
<= 0) {
451 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
453 intel_region_release(&((*mt
)->region
));
454 intel_miptree_release(&(*mt
)->stencil_mt
);
455 intel_miptree_release(&(*mt
)->hiz_mt
);
456 intel_miptree_release(&(*mt
)->mcs_mt
);
457 intel_resolve_map_clear(&(*mt
)->hiz_map
);
459 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
460 free((*mt
)->level
[i
].slice
);
469 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
470 int *width
, int *height
, int *depth
)
472 switch (image
->TexObject
->Target
) {
473 case GL_TEXTURE_1D_ARRAY
:
474 *width
= image
->Width
;
476 *depth
= image
->Height
;
479 *width
= image
->Width
;
480 *height
= image
->Height
;
481 *depth
= image
->Depth
;
487 * Can the image be pulled into a unified mipmap tree? This mirrors
488 * the completeness test in a lot of ways.
490 * Not sure whether I want to pass gl_texture_image here.
493 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
494 struct gl_texture_image
*image
)
496 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
497 GLuint level
= intelImage
->base
.Base
.Level
;
498 int width
, height
, depth
;
500 if (target_to_target(image
->TexObject
->Target
) != mt
->target
)
503 if (image
->TexFormat
!= mt
->format
&&
504 !(image
->TexFormat
== MESA_FORMAT_S8_Z24
&&
505 mt
->format
== MESA_FORMAT_X8_Z24
&&
510 intel_miptree_get_dimensions_for_image(image
, &width
, &height
, &depth
);
512 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
515 /* Test image dimensions against the base level image adjusted for
516 * minification. This will also catch images not present in the
517 * tree, changed targets, etc.
519 if (width
!= mt
->level
[level
].width
||
520 height
!= mt
->level
[level
].height
||
521 depth
!= mt
->level
[level
].depth
)
529 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
532 GLuint w
, GLuint h
, GLuint d
)
534 mt
->level
[level
].width
= w
;
535 mt
->level
[level
].height
= h
;
536 mt
->level
[level
].depth
= d
;
537 mt
->level
[level
].level_x
= x
;
538 mt
->level
[level
].level_y
= y
;
540 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__
,
541 level
, w
, h
, d
, x
, y
);
543 assert(mt
->level
[level
].slice
== NULL
);
545 mt
->level
[level
].slice
= calloc(d
, sizeof(*mt
->level
[0].slice
));
546 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
547 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
552 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
553 GLuint level
, GLuint img
,
556 if (img
== 0 && level
== 0)
557 assert(x
== 0 && y
== 0);
559 assert(img
< mt
->level
[level
].depth
);
561 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
562 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
564 DBG("%s level %d img %d pos %d,%d\n",
565 __FUNCTION__
, level
, img
,
566 mt
->level
[level
].slice
[img
].x_offset
,
567 mt
->level
[level
].slice
[img
].y_offset
);
572 * For cube map textures, either the \c face parameter can be used, of course,
573 * or the cube face can be interpreted as a depth layer and the \c layer
577 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
578 GLuint level
, GLuint face
, GLuint layer
,
579 GLuint
*x
, GLuint
*y
)
584 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
);
589 /* This branch may be taken even if the texture target is a cube map. In
590 * that case, the caller chose to interpret each cube face as a layer.
596 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
597 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
601 intel_miptree_copy_slice(struct intel_context
*intel
,
602 struct intel_mipmap_tree
*dst_mt
,
603 struct intel_mipmap_tree
*src_mt
,
609 gl_format format
= src_mt
->format
;
610 uint32_t width
= src_mt
->level
[level
].width
;
611 uint32_t height
= src_mt
->level
[level
].height
;
613 assert(depth
< src_mt
->level
[level
].depth
);
615 if (dst_mt
->compressed
) {
616 height
= ALIGN(height
, dst_mt
->align_h
) / dst_mt
->align_h
;
617 width
= ALIGN(width
, dst_mt
->align_w
);
620 uint32_t dst_x
, dst_y
, src_x
, src_y
;
621 intel_miptree_get_image_offset(dst_mt
, level
, face
, depth
,
623 intel_miptree_get_image_offset(src_mt
, level
, face
, depth
,
626 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
627 src_mt
, src_x
, src_y
, src_mt
->region
->pitch
* src_mt
->region
->cpp
,
628 dst_mt
, dst_x
, dst_y
, dst_mt
->region
->pitch
* dst_mt
->region
->cpp
,
631 if (!intelEmitCopyBlit(intel
,
633 src_mt
->region
->pitch
, src_mt
->region
->bo
,
634 0, src_mt
->region
->tiling
,
635 dst_mt
->region
->pitch
, dst_mt
->region
->bo
,
636 0, dst_mt
->region
->tiling
,
642 fallback_debug("miptree validate blit for %s failed\n",
643 _mesa_get_format_name(format
));
644 void *dst
= intel_region_map(intel
, dst_mt
->region
, GL_MAP_WRITE_BIT
);
645 void *src
= intel_region_map(intel
, src_mt
->region
, GL_MAP_READ_BIT
);
649 dst_mt
->region
->pitch
,
652 src
, src_mt
->region
->pitch
,
655 intel_region_unmap(intel
, dst_mt
->region
);
656 intel_region_unmap(intel
, src_mt
->region
);
659 if (src_mt
->stencil_mt
) {
660 intel_miptree_copy_slice(intel
,
661 dst_mt
->stencil_mt
, src_mt
->stencil_mt
,
667 * Copies the image's current data to the given miptree, and associates that
668 * miptree with the image.
671 intel_miptree_copy_teximage(struct intel_context
*intel
,
672 struct intel_texture_image
*intelImage
,
673 struct intel_mipmap_tree
*dst_mt
)
675 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
676 int level
= intelImage
->base
.Base
.Level
;
677 int face
= intelImage
->base
.Base
.Face
;
678 GLuint depth
= intelImage
->base
.Base
.Depth
;
680 for (int slice
= 0; slice
< depth
; slice
++) {
681 intel_miptree_copy_slice(intel
, dst_mt
, src_mt
, level
, face
, slice
);
684 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
688 intel_miptree_alloc_mcs(struct intel_context
*intel
,
689 struct intel_mipmap_tree
*mt
,
692 assert(mt
->mcs_mt
== NULL
);
693 assert(intel
->gen
>= 7); /* MCS only used on Gen7+ */
695 /* Choose the correct format for the MCS buffer. All that really matters
696 * is that we allocate the right buffer size, since we'll always be
697 * accessing this miptree using MCS-specific hardware mechanisms, which
698 * infer the correct format based on num_samples.
701 switch (num_samples
) {
703 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
706 format
= MESA_FORMAT_R8
;
709 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
710 * for each sample, plus 8 padding bits).
712 format
= MESA_FORMAT_R_UINT32
;
715 assert(!"Unrecognized sample count in intel_miptree_alloc_mcs");
719 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
721 * "The MCS surface must be stored as Tile Y."
723 * We set msaa_format to INTEL_MSAA_LAYOUT_CMS to force
724 * intel_miptree_create() to use Y tiling. msaa_format is otherwise
725 * ignored for the MCS miptree.
727 mt
->mcs_mt
= intel_miptree_create(intel
,
737 INTEL_MSAA_LAYOUT_CMS
);
739 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
741 * When MCS buffer is enabled and bound to MSRT, it is required that it
742 * is cleared prior to any rendering.
744 * Since we don't use the MCS buffer for any purpose other than rendering,
745 * it makes sense to just clear it immediately upon allocation.
747 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
749 void *data
= intel_region_map(intel
, mt
->mcs_mt
->region
, 0);
750 memset(data
, 0xff, mt
->mcs_mt
->region
->bo
->size
);
751 intel_region_unmap(intel
, mt
->mcs_mt
->region
);
757 intel_miptree_alloc_hiz(struct intel_context
*intel
,
758 struct intel_mipmap_tree
*mt
,
761 assert(mt
->hiz_mt
== NULL
);
762 /* MSAA HiZ surfaces always use IMS layout. */
763 mt
->hiz_mt
= intel_miptree_create(intel
,
773 INTEL_MSAA_LAYOUT_IMS
);
778 /* Mark that all slices need a HiZ resolve. */
779 struct intel_resolve_map
*head
= &mt
->hiz_map
;
780 for (int level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
781 for (int layer
= 0; layer
< mt
->level
[level
].depth
; ++layer
) {
782 head
->next
= malloc(sizeof(*head
->next
));
783 head
->next
->prev
= head
;
784 head
->next
->next
= NULL
;
789 head
->need
= GEN6_HIZ_OP_HIZ_RESOLVE
;
797 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree
*mt
,
801 intel_miptree_check_level_layer(mt
, level
, layer
);
806 intel_resolve_map_set(&mt
->hiz_map
,
807 level
, layer
, GEN6_HIZ_OP_HIZ_RESOLVE
);
812 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree
*mt
,
816 intel_miptree_check_level_layer(mt
, level
, layer
);
821 intel_resolve_map_set(&mt
->hiz_map
,
822 level
, layer
, GEN6_HIZ_OP_DEPTH_RESOLVE
);
826 intel_miptree_slice_resolve(struct intel_context
*intel
,
827 struct intel_mipmap_tree
*mt
,
830 enum gen6_hiz_op need
)
832 intel_miptree_check_level_layer(mt
, level
, layer
);
834 struct intel_resolve_map
*item
=
835 intel_resolve_map_get(&mt
->hiz_map
, level
, layer
);
837 if (!item
|| item
->need
!= need
)
840 intel_hiz_exec(intel
, mt
, level
, layer
, need
);
841 intel_resolve_map_remove(item
);
846 intel_miptree_slice_resolve_hiz(struct intel_context
*intel
,
847 struct intel_mipmap_tree
*mt
,
851 return intel_miptree_slice_resolve(intel
, mt
, level
, layer
,
852 GEN6_HIZ_OP_HIZ_RESOLVE
);
856 intel_miptree_slice_resolve_depth(struct intel_context
*intel
,
857 struct intel_mipmap_tree
*mt
,
861 return intel_miptree_slice_resolve(intel
, mt
, level
, layer
,
862 GEN6_HIZ_OP_DEPTH_RESOLVE
);
866 intel_miptree_all_slices_resolve(struct intel_context
*intel
,
867 struct intel_mipmap_tree
*mt
,
868 enum gen6_hiz_op need
)
870 bool did_resolve
= false;
871 struct intel_resolve_map
*i
, *next
;
873 for (i
= mt
->hiz_map
.next
; i
; i
= next
) {
878 intel_hiz_exec(intel
, mt
, i
->level
, i
->layer
, need
);
879 intel_resolve_map_remove(i
);
887 intel_miptree_all_slices_resolve_hiz(struct intel_context
*intel
,
888 struct intel_mipmap_tree
*mt
)
890 return intel_miptree_all_slices_resolve(intel
, mt
,
891 GEN6_HIZ_OP_HIZ_RESOLVE
);
895 intel_miptree_all_slices_resolve_depth(struct intel_context
*intel
,
896 struct intel_mipmap_tree
*mt
)
898 return intel_miptree_all_slices_resolve(intel
, mt
,
899 GEN6_HIZ_OP_DEPTH_RESOLVE
);
903 intel_miptree_map_gtt(struct intel_context
*intel
,
904 struct intel_mipmap_tree
*mt
,
905 struct intel_miptree_map
*map
,
906 unsigned int level
, unsigned int slice
)
910 unsigned int image_x
, image_y
;
914 /* For compressed formats, the stride is the number of bytes per
915 * row of blocks. intel_miptree_get_image_offset() already does
918 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
922 base
= intel_region_map(intel
, mt
->region
, map
->mode
);
927 /* Note that in the case of cube maps, the caller must have passed the
928 * slice number referencing the face.
930 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
934 map
->stride
= mt
->region
->pitch
* mt
->cpp
;
935 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
938 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
939 map
->x
, map
->y
, map
->w
, map
->h
,
940 mt
, _mesa_get_format_name(mt
->format
),
941 x
, y
, map
->ptr
, map
->stride
);
945 intel_miptree_unmap_gtt(struct intel_context
*intel
,
946 struct intel_mipmap_tree
*mt
,
947 struct intel_miptree_map
*map
,
951 intel_region_unmap(intel
, mt
->region
);
955 intel_miptree_map_blit(struct intel_context
*intel
,
956 struct intel_mipmap_tree
*mt
,
957 struct intel_miptree_map
*map
,
958 unsigned int level
, unsigned int slice
)
960 unsigned int image_x
, image_y
;
965 /* The blitter requires the pitch to be aligned to 4. */
966 map
->stride
= ALIGN(map
->w
* mt
->region
->cpp
, 4);
968 map
->bo
= drm_intel_bo_alloc(intel
->bufmgr
, "intel_miptree_map_blit() temp",
969 map
->stride
* map
->h
, 4096);
971 fprintf(stderr
, "Failed to allocate blit temporary\n");
975 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
979 if (!intelEmitCopyBlit(intel
,
981 mt
->region
->pitch
, mt
->region
->bo
,
982 0, mt
->region
->tiling
,
983 map
->stride
/ mt
->region
->cpp
, map
->bo
,
989 fprintf(stderr
, "Failed to blit\n");
993 intel_batchbuffer_flush(intel
);
994 ret
= drm_intel_bo_map(map
->bo
, (map
->mode
& GL_MAP_WRITE_BIT
) != 0);
996 fprintf(stderr
, "Failed to map blit temporary\n");
1000 map
->ptr
= map
->bo
->virtual;
1002 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
1003 map
->x
, map
->y
, map
->w
, map
->h
,
1004 mt
, _mesa_get_format_name(mt
->format
),
1005 x
, y
, map
->ptr
, map
->stride
);
1010 drm_intel_bo_unreference(map
->bo
);
1016 intel_miptree_unmap_blit(struct intel_context
*intel
,
1017 struct intel_mipmap_tree
*mt
,
1018 struct intel_miptree_map
*map
,
1022 assert(!(map
->mode
& GL_MAP_WRITE_BIT
));
1024 drm_intel_bo_unmap(map
->bo
);
1025 drm_intel_bo_unreference(map
->bo
);
1029 intel_miptree_map_s8(struct intel_context
*intel
,
1030 struct intel_mipmap_tree
*mt
,
1031 struct intel_miptree_map
*map
,
1032 unsigned int level
, unsigned int slice
)
1034 map
->stride
= map
->w
;
1035 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
1039 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1040 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1041 * invalidate is set, since we'll be writing the whole rectangle from our
1042 * temporary buffer back out.
1044 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
1045 uint8_t *untiled_s8_map
= map
->ptr
;
1046 uint8_t *tiled_s8_map
= intel_region_map(intel
, mt
->region
,
1048 unsigned int image_x
, image_y
;
1050 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
1052 for (uint32_t y
= 0; y
< map
->h
; y
++) {
1053 for (uint32_t x
= 0; x
< map
->w
; x
++) {
1054 ptrdiff_t offset
= intel_offset_S8(mt
->region
->pitch
,
1055 x
+ image_x
+ map
->x
,
1056 y
+ image_y
+ map
->y
,
1057 intel
->has_swizzling
);
1058 untiled_s8_map
[y
* map
->w
+ x
] = tiled_s8_map
[offset
];
1062 intel_region_unmap(intel
, mt
->region
);
1064 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__
,
1065 map
->x
, map
->y
, map
->w
, map
->h
,
1066 mt
, map
->x
+ image_x
, map
->y
+ image_y
, map
->ptr
, map
->stride
);
1068 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__
,
1069 map
->x
, map
->y
, map
->w
, map
->h
,
1070 mt
, map
->ptr
, map
->stride
);
1075 intel_miptree_unmap_s8(struct intel_context
*intel
,
1076 struct intel_mipmap_tree
*mt
,
1077 struct intel_miptree_map
*map
,
1081 if (map
->mode
& GL_MAP_WRITE_BIT
) {
1082 unsigned int image_x
, image_y
;
1083 uint8_t *untiled_s8_map
= map
->ptr
;
1084 uint8_t *tiled_s8_map
= intel_region_map(intel
, mt
->region
, map
->mode
);
1086 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
1088 for (uint32_t y
= 0; y
< map
->h
; y
++) {
1089 for (uint32_t x
= 0; x
< map
->w
; x
++) {
1090 ptrdiff_t offset
= intel_offset_S8(mt
->region
->pitch
,
1093 intel
->has_swizzling
);
1094 tiled_s8_map
[offset
] = untiled_s8_map
[y
* map
->w
+ x
];
1098 intel_region_unmap(intel
, mt
->region
);
1105 intel_miptree_map_etc1(struct intel_context
*intel
,
1106 struct intel_mipmap_tree
*mt
,
1107 struct intel_miptree_map
*map
,
1111 /* For justification of these invariants,
1112 * see intel_mipmap_tree:wraps_etc1.
1114 assert(mt
->wraps_etc1
);
1115 assert(mt
->format
== MESA_FORMAT_RGBX8888_REV
);
1117 /* From the GL_OES_compressed_ETC1_RGB8_texture spec:
1118 * INVALID_OPERATION is generated by CompressedTexSubImage2D,
1119 * TexSubImage2D, or CopyTexSubImage2D if the texture image <level>
1120 * bound to <target> has internal format ETC1_RGB8_OES.
1122 * This implies that intel_miptree_map_etc1() can only be called from
1123 * glCompressedTexImage2D, and hence the assertions below hold.
1125 assert(map
->mode
& GL_MAP_WRITE_BIT
);
1126 assert(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
);
1127 assert(map
->x
== 0);
1128 assert(map
->y
== 0);
1130 /* Each ETC1 block contains 4x4 pixels in 8 bytes. */
1131 map
->stride
= 2 * map
->w
;
1132 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
1136 intel_miptree_unmap_etc1(struct intel_context
*intel
,
1137 struct intel_mipmap_tree
*mt
,
1138 struct intel_miptree_map
*map
,
1144 intel_miptree_get_image_offset(mt
, level
, 0, slice
, &image_x
, &image_y
);
1146 uint8_t *xbgr
= intel_region_map(intel
, mt
->region
, map
->mode
)
1147 + image_y
* mt
->region
->pitch
* mt
->region
->cpp
1148 + image_x
* mt
->region
->cpp
;
1150 _mesa_etc1_unpack_rgba8888(xbgr
, mt
->region
->pitch
* mt
->region
->cpp
,
1151 map
->ptr
, map
->stride
,
1154 intel_region_unmap(intel
, mt
->region
);
1159 * Mapping function for packed depth/stencil miptrees backed by real separate
1160 * miptrees for depth and stencil.
1162 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
1163 * separate from the depth buffer. Yet at the GL API level, we have to expose
1164 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
1165 * be able to map that memory for texture storage and glReadPixels-type
1166 * operations. We give Mesa core that access by mallocing a temporary and
1167 * copying the data between the actual backing store and the temporary.
1170 intel_miptree_map_depthstencil(struct intel_context
*intel
,
1171 struct intel_mipmap_tree
*mt
,
1172 struct intel_miptree_map
*map
,
1173 unsigned int level
, unsigned int slice
)
1175 struct intel_mipmap_tree
*z_mt
= mt
;
1176 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
1177 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z32_FLOAT
;
1178 int packed_bpp
= map_z32f_x24s8
? 8 : 4;
1180 map
->stride
= map
->w
* packed_bpp
;
1181 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
1185 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1186 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1187 * invalidate is set, since we'll be writing the whole rectangle from our
1188 * temporary buffer back out.
1190 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
1191 uint32_t *packed_map
= map
->ptr
;
1192 uint8_t *s_map
= intel_region_map(intel
, s_mt
->region
, GL_MAP_READ_BIT
);
1193 uint32_t *z_map
= intel_region_map(intel
, z_mt
->region
, GL_MAP_READ_BIT
);
1194 unsigned int s_image_x
, s_image_y
;
1195 unsigned int z_image_x
, z_image_y
;
1197 intel_miptree_get_image_offset(s_mt
, level
, 0, slice
,
1198 &s_image_x
, &s_image_y
);
1199 intel_miptree_get_image_offset(z_mt
, level
, 0, slice
,
1200 &z_image_x
, &z_image_y
);
1202 for (uint32_t y
= 0; y
< map
->h
; y
++) {
1203 for (uint32_t x
= 0; x
< map
->w
; x
++) {
1204 int map_x
= map
->x
+ x
, map_y
= map
->y
+ y
;
1205 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->region
->pitch
,
1208 intel
->has_swizzling
);
1209 ptrdiff_t z_offset
= ((map_y
+ z_image_y
) * z_mt
->region
->pitch
+
1210 (map_x
+ z_image_x
));
1211 uint8_t s
= s_map
[s_offset
];
1212 uint32_t z
= z_map
[z_offset
];
1214 if (map_z32f_x24s8
) {
1215 packed_map
[(y
* map
->w
+ x
) * 2 + 0] = z
;
1216 packed_map
[(y
* map
->w
+ x
) * 2 + 1] = s
;
1218 packed_map
[y
* map
->w
+ x
] = (s
<< 24) | (z
& 0x00ffffff);
1223 intel_region_unmap(intel
, s_mt
->region
);
1224 intel_region_unmap(intel
, z_mt
->region
);
1226 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
1228 map
->x
, map
->y
, map
->w
, map
->h
,
1229 z_mt
, map
->x
+ z_image_x
, map
->y
+ z_image_y
,
1230 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
1231 map
->ptr
, map
->stride
);
1233 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__
,
1234 map
->x
, map
->y
, map
->w
, map
->h
,
1235 mt
, map
->ptr
, map
->stride
);
1240 intel_miptree_unmap_depthstencil(struct intel_context
*intel
,
1241 struct intel_mipmap_tree
*mt
,
1242 struct intel_miptree_map
*map
,
1246 struct intel_mipmap_tree
*z_mt
= mt
;
1247 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
1248 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z32_FLOAT
;
1250 if (map
->mode
& GL_MAP_WRITE_BIT
) {
1251 uint32_t *packed_map
= map
->ptr
;
1252 uint8_t *s_map
= intel_region_map(intel
, s_mt
->region
, map
->mode
);
1253 uint32_t *z_map
= intel_region_map(intel
, z_mt
->region
, map
->mode
);
1254 unsigned int s_image_x
, s_image_y
;
1255 unsigned int z_image_x
, z_image_y
;
1257 intel_miptree_get_image_offset(s_mt
, level
, 0, slice
,
1258 &s_image_x
, &s_image_y
);
1259 intel_miptree_get_image_offset(z_mt
, level
, 0, slice
,
1260 &z_image_x
, &z_image_y
);
1262 for (uint32_t y
= 0; y
< map
->h
; y
++) {
1263 for (uint32_t x
= 0; x
< map
->w
; x
++) {
1264 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->region
->pitch
,
1265 x
+ s_image_x
+ map
->x
,
1266 y
+ s_image_y
+ map
->y
,
1267 intel
->has_swizzling
);
1268 ptrdiff_t z_offset
= ((y
+ z_image_y
) * z_mt
->region
->pitch
+
1271 if (map_z32f_x24s8
) {
1272 z_map
[z_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 0];
1273 s_map
[s_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 1];
1275 uint32_t packed
= packed_map
[y
* map
->w
+ x
];
1276 s_map
[s_offset
] = packed
>> 24;
1277 z_map
[z_offset
] = packed
;
1282 intel_region_unmap(intel
, s_mt
->region
);
1283 intel_region_unmap(intel
, z_mt
->region
);
1285 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1287 map
->x
, map
->y
, map
->w
, map
->h
,
1288 z_mt
, _mesa_get_format_name(z_mt
->format
),
1289 map
->x
+ z_image_x
, map
->y
+ z_image_y
,
1290 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
1291 map
->ptr
, map
->stride
);
1298 intel_miptree_map(struct intel_context
*intel
,
1299 struct intel_mipmap_tree
*mt
,
1310 struct intel_miptree_map
*map
;
1312 map
= calloc(1, sizeof(struct intel_miptree_map
));
1319 assert(!mt
->level
[level
].slice
[slice
].map
);
1320 mt
->level
[level
].slice
[slice
].map
= map
;
1327 intel_miptree_slice_resolve_depth(intel
, mt
, level
, slice
);
1328 if (map
->mode
& GL_MAP_WRITE_BIT
) {
1329 intel_miptree_slice_set_needs_hiz_resolve(mt
, level
, slice
);
1332 if (mt
->format
== MESA_FORMAT_S8
) {
1333 intel_miptree_map_s8(intel
, mt
, map
, level
, slice
);
1334 } else if (mt
->wraps_etc1
) {
1335 intel_miptree_map_etc1(intel
, mt
, map
, level
, slice
);
1336 } else if (mt
->stencil_mt
) {
1337 intel_miptree_map_depthstencil(intel
, mt
, map
, level
, slice
);
1338 } else if (intel
->has_llc
&&
1339 !(mode
& GL_MAP_WRITE_BIT
) &&
1341 mt
->region
->tiling
== I915_TILING_X
) {
1342 intel_miptree_map_blit(intel
, mt
, map
, level
, slice
);
1344 intel_miptree_map_gtt(intel
, mt
, map
, level
, slice
);
1347 *out_ptr
= map
->ptr
;
1348 *out_stride
= map
->stride
;
1350 if (map
->ptr
== NULL
) {
1351 mt
->level
[level
].slice
[slice
].map
= NULL
;
1357 intel_miptree_unmap(struct intel_context
*intel
,
1358 struct intel_mipmap_tree
*mt
,
1362 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
1367 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__
,
1368 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
1370 if (mt
->format
== MESA_FORMAT_S8
) {
1371 intel_miptree_unmap_s8(intel
, mt
, map
, level
, slice
);
1372 } else if (mt
->wraps_etc1
) {
1373 intel_miptree_unmap_etc1(intel
, mt
, map
, level
, slice
);
1374 } else if (mt
->stencil_mt
) {
1375 intel_miptree_unmap_depthstencil(intel
, mt
, map
, level
, slice
);
1376 } else if (map
->bo
) {
1377 intel_miptree_unmap_blit(intel
, mt
, map
, level
, slice
);
1379 intel_miptree_unmap_gtt(intel
, mt
, map
, level
, slice
);
1382 mt
->level
[level
].slice
[slice
].map
= NULL
;