1 /**************************************************************************
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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 **************************************************************************/
29 #include <GL/internal/dri_interface.h>
31 #include "intel_batchbuffer.h"
32 #include "intel_mipmap_tree.h"
33 #include "intel_resolve_map.h"
34 #include "intel_tex.h"
35 #include "intel_blit.h"
36 #include "intel_fbo.h"
38 #include "brw_blorp.h"
39 #include "brw_context.h"
41 #include "main/enums.h"
42 #include "main/fbobject.h"
43 #include "main/formats.h"
44 #include "main/glformats.h"
45 #include "main/texcompress_etc.h"
46 #include "main/teximage.h"
47 #include "main/streaming-load-memcpy.h"
48 #include "x86/common_x86_asm.h"
50 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
53 intel_miptree_alloc_mcs(struct brw_context
*brw
,
54 struct intel_mipmap_tree
*mt
,
58 * Determine which MSAA layout should be used by the MSAA surface being
59 * created, based on the chip generation and the surface type.
61 static enum intel_msaa_layout
62 compute_msaa_layout(struct brw_context
*brw
, mesa_format format
, GLenum target
,
63 bool disable_aux_buffers
)
65 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
67 return INTEL_MSAA_LAYOUT_IMS
;
69 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
70 switch (_mesa_get_format_base_format(format
)) {
71 case GL_DEPTH_COMPONENT
:
72 case GL_STENCIL_INDEX
:
73 case GL_DEPTH_STENCIL
:
74 return INTEL_MSAA_LAYOUT_IMS
;
76 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
78 * This field must be set to 0 for all SINT MSRTs when all RT channels
81 * In practice this means that we have to disable MCS for all signed
82 * integer MSAA buffers. The alternative, to disable MCS only when one
83 * of the render target channels is disabled, is impractical because it
84 * would require converting between CMS and UMS MSAA layouts on the fly,
87 if (brw
->gen
== 7 && _mesa_get_format_datatype(format
) == GL_INT
) {
88 return INTEL_MSAA_LAYOUT_UMS
;
89 } else if (disable_aux_buffers
) {
90 /* We can't use the CMS layout because it uses an aux buffer, the MCS
91 * buffer. So fallback to UMS, which is identical to CMS without the
93 return INTEL_MSAA_LAYOUT_UMS
;
95 return INTEL_MSAA_LAYOUT_CMS
;
102 * For single-sampled render targets ("non-MSRT"), the MCS buffer is a
103 * scaled-down bitfield representation of the color buffer which is capable of
104 * recording when blocks of the color buffer are equal to the clear value.
105 * This function returns the block size that will be used by the MCS buffer
106 * corresponding to a certain color miptree.
108 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
109 * beneath the "Fast Color Clear" bullet (p327):
111 * The following table describes the RT alignment
125 * This alignment has the following uses:
127 * - For figuring out the size of the MCS buffer. Each 4k tile in the MCS
128 * buffer contains 128 blocks horizontally and 256 blocks vertically.
130 * - For figuring out alignment restrictions for a fast clear operation. Fast
131 * clear operations must always clear aligned multiples of 16 blocks
132 * horizontally and 32 blocks vertically.
134 * - For scaling down the coordinates sent through the render pipeline during
135 * a fast clear. X coordinates must be scaled down by 8 times the block
136 * width, and Y coordinates by 16 times the block height.
138 * - For scaling down the coordinates sent through the render pipeline during
139 * a "Render Target Resolve" operation. X coordinates must be scaled down
140 * by half the block width, and Y coordinates by half the block height.
143 intel_get_non_msrt_mcs_alignment(struct brw_context
*brw
,
144 struct intel_mipmap_tree
*mt
,
145 unsigned *width_px
, unsigned *height
)
147 switch (mt
->tiling
) {
149 unreachable("Non-MSRT MCS requires X or Y tiling");
150 /* In release builds, fall through */
152 *width_px
= 32 / mt
->cpp
;
156 *width_px
= 64 / mt
->cpp
;
163 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
166 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
167 * beneath the "Fast Color Clear" bullet (p326):
169 * - Support is limited to tiled render targets.
170 * - Support is for non-mip-mapped and non-array surface types only.
172 * And then later, on p327:
174 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
178 intel_is_non_msrt_mcs_buffer_supported(struct brw_context
*brw
,
179 struct intel_mipmap_tree
*mt
)
181 /* MCS support does not exist prior to Gen7 */
185 if (mt
->disable_aux_buffers
)
188 /* MCS is only supported for color buffers */
189 switch (_mesa_get_format_base_format(mt
->format
)) {
190 case GL_DEPTH_COMPONENT
:
191 case GL_DEPTH_STENCIL
:
192 case GL_STENCIL_INDEX
:
196 if (mt
->tiling
!= I915_TILING_X
&&
197 mt
->tiling
!= I915_TILING_Y
)
199 if (mt
->cpp
!= 4 && mt
->cpp
!= 8 && mt
->cpp
!= 16)
201 if (mt
->first_level
!= 0 || mt
->last_level
!= 0)
203 if (mt
->physical_depth0
!= 1)
206 /* There's no point in using an MCS buffer if the surface isn't in a
209 if (!brw
->format_supported_as_render_target
[mt
->format
])
217 * Determine depth format corresponding to a depth+stencil format,
218 * for separate stencil.
221 intel_depth_format_for_depthstencil_format(mesa_format format
) {
223 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
224 return MESA_FORMAT_Z24_UNORM_X8_UINT
;
225 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
226 return MESA_FORMAT_Z_FLOAT32
;
234 * @param for_bo Indicates that the caller is
235 * intel_miptree_create_for_bo(). If true, then do not create
238 static struct intel_mipmap_tree
*
239 intel_miptree_create_layout(struct brw_context
*brw
,
249 bool force_all_slices_at_each_lod
,
250 bool disable_aux_buffers
)
252 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
256 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __FUNCTION__
,
257 _mesa_lookup_enum_by_nr(target
),
258 _mesa_get_format_name(format
),
259 first_level
, last_level
, depth0
, mt
);
261 if (target
== GL_TEXTURE_1D_ARRAY
) {
262 /* For a 1D Array texture the OpenGL API will treat the height0
263 * parameter as the number of array slices. For Intel hardware, we treat
264 * the 1D array as a 2D Array with a height of 1.
266 * So, when we first come through this path to create a 1D Array
267 * texture, height0 stores the number of slices, and depth0 is 1. In
268 * this case, we want to swap height0 and depth0.
270 * Since some miptrees will be created based on the base miptree, we may
271 * come through this path and see height0 as 1 and depth0 being the
272 * number of slices. In this case we don't need to do the swap.
274 assert(height0
== 1 || depth0
== 1);
283 mt
->first_level
= first_level
;
284 mt
->last_level
= last_level
;
285 mt
->logical_width0
= width0
;
286 mt
->logical_height0
= height0
;
287 mt
->logical_depth0
= depth0
;
288 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_NO_MCS
;
289 mt
->disable_aux_buffers
= disable_aux_buffers
;
290 exec_list_make_empty(&mt
->hiz_map
);
292 /* The cpp is bytes per (1, blockheight)-sized block for compressed
293 * textures. This is why you'll see divides by blockheight all over
296 _mesa_get_format_block_size(format
, &bw
, &bh
);
297 assert(_mesa_get_format_bytes(mt
->format
) % bw
== 0);
298 mt
->cpp
= _mesa_get_format_bytes(mt
->format
) / bw
;
300 mt
->num_samples
= num_samples
;
301 mt
->compressed
= _mesa_is_format_compressed(format
);
302 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_NONE
;
305 if (num_samples
> 1) {
306 /* Adjust width/height/depth for MSAA */
307 mt
->msaa_layout
= compute_msaa_layout(brw
, format
,
308 mt
->target
, mt
->disable_aux_buffers
);
309 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
) {
310 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
311 * "If the surface is multisampled and it is a depth or stencil
312 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
313 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
316 * +----------------------------------------------------------------+
317 * | Num Multisamples | W_l = | H_l = |
318 * +----------------------------------------------------------------+
319 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
320 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
321 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
322 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
323 * +----------------------------------------------------------------+
326 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
327 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
328 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
330 * Another more complicated explanation for these adjustments comes
331 * from the Sandybridge PRM, volume 4, part 1, page 31:
333 * "Any of the other messages (sample*, LOD, load4) used with a
334 * (4x) multisampled surface will in-effect sample a surface with
335 * double the height and width as that indicated in the surface
336 * state. Each pixel position on the original-sized surface is
337 * replaced with a 2x2 of samples with the following arrangement:
342 * Thus, when sampling from a multisampled texture, it behaves as
343 * though the layout in memory for (x,y,sample) is:
345 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
346 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
348 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
349 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
351 * However, the actual layout of multisampled data in memory is:
353 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
354 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
356 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
357 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
359 * This pattern repeats for each 2x2 pixel block.
361 * As a result, when calculating the size of our 4-sample buffer for
362 * an odd width or height, we have to align before scaling up because
363 * sample 3 is in that bottom right 2x2 block.
365 switch (num_samples
) {
367 assert(brw
->gen
>= 8);
368 width0
= ALIGN(width0
, 2) * 2;
369 height0
= ALIGN(height0
, 2);
372 width0
= ALIGN(width0
, 2) * 2;
373 height0
= ALIGN(height0
, 2) * 2;
376 width0
= ALIGN(width0
, 2) * 4;
377 height0
= ALIGN(height0
, 2) * 2;
380 /* num_samples should already have been quantized to 0, 1, 2, 4, or
383 unreachable("not reached");
386 /* Non-interleaved */
387 depth0
*= num_samples
;
391 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when gen7+ array_spacing_lod0
392 * can be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces.
393 * TODO: can we use it elsewhere?
395 switch (mt
->msaa_layout
) {
396 case INTEL_MSAA_LAYOUT_NONE
:
397 case INTEL_MSAA_LAYOUT_IMS
:
398 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
400 case INTEL_MSAA_LAYOUT_UMS
:
401 case INTEL_MSAA_LAYOUT_CMS
:
402 mt
->array_layout
= ALL_SLICES_AT_EACH_LOD
;
406 if (target
== GL_TEXTURE_CUBE_MAP
) {
411 mt
->physical_width0
= width0
;
412 mt
->physical_height0
= height0
;
413 mt
->physical_depth0
= depth0
;
416 _mesa_get_format_base_format(format
) == GL_DEPTH_STENCIL
&&
417 (brw
->must_use_separate_stencil
||
418 (brw
->has_separate_stencil
&&
419 intel_miptree_wants_hiz_buffer(brw
, mt
)))) {
420 const bool force_all_slices_at_each_lod
= brw
->gen
== 6;
421 mt
->stencil_mt
= intel_miptree_create(brw
,
431 INTEL_MIPTREE_TILING_ANY
,
432 force_all_slices_at_each_lod
);
433 if (!mt
->stencil_mt
) {
434 intel_miptree_release(&mt
);
438 /* Fix up the Z miptree format for how we're splitting out separate
439 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
441 mt
->format
= intel_depth_format_for_depthstencil_format(mt
->format
);
444 if (format
== mt
->format
) {
445 _mesa_problem(NULL
, "Unknown format %s in separate stencil mt\n",
446 _mesa_get_format_name(mt
->format
));
450 if (force_all_slices_at_each_lod
)
451 mt
->array_layout
= ALL_SLICES_AT_EACH_LOD
;
453 brw_miptree_layout(brw
, mt
);
455 if (mt
->disable_aux_buffers
)
456 assert(mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_CMS
);
462 * \brief Helper function for intel_miptree_create().
465 intel_miptree_choose_tiling(struct brw_context
*brw
,
468 uint32_t num_samples
,
469 enum intel_miptree_tiling_mode requested
,
470 struct intel_mipmap_tree
*mt
)
472 if (format
== MESA_FORMAT_S_UINT8
) {
473 /* The stencil buffer is W tiled. However, we request from the kernel a
474 * non-tiled buffer because the GTT is incapable of W fencing.
476 return I915_TILING_NONE
;
479 /* Some usages may want only one type of tiling, like depth miptrees (Y
480 * tiled), or temporary BOs for uploading data once (linear).
483 case INTEL_MIPTREE_TILING_ANY
:
485 case INTEL_MIPTREE_TILING_Y
:
486 return I915_TILING_Y
;
487 case INTEL_MIPTREE_TILING_NONE
:
488 return I915_TILING_NONE
;
491 if (num_samples
> 1) {
492 /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
495 * [DevSNB+]: For multi-sample render targets, this field must be
496 * 1. MSRTs can only be tiled.
498 * Our usual reason for preferring X tiling (fast blits using the
499 * blitting engine) doesn't apply to MSAA, since we'll generally be
500 * downsampling or upsampling when blitting between the MSAA buffer
501 * and another buffer, and the blitting engine doesn't support that.
502 * So use Y tiling, since it makes better use of the cache.
504 return I915_TILING_Y
;
507 GLenum base_format
= _mesa_get_format_base_format(format
);
508 if (base_format
== GL_DEPTH_COMPONENT
||
509 base_format
== GL_DEPTH_STENCIL_EXT
)
510 return I915_TILING_Y
;
512 /* 1D textures (and 1D array textures) don't get any benefit from tiling,
513 * in fact it leads to a less efficient use of memory space and bandwidth
514 * due to tile alignment.
516 if (mt
->logical_height0
== 1)
517 return I915_TILING_NONE
;
519 int minimum_pitch
= mt
->total_width
* mt
->cpp
;
521 /* If the width is much smaller than a tile, don't bother tiling. */
522 if (minimum_pitch
< 64)
523 return I915_TILING_NONE
;
525 if (ALIGN(minimum_pitch
, 512) >= 32768 ||
526 mt
->total_width
>= 32768 || mt
->total_height
>= 32768) {
527 perf_debug("%dx%d miptree too large to blit, falling back to untiled",
528 mt
->total_width
, mt
->total_height
);
529 return I915_TILING_NONE
;
532 /* Pre-gen6 doesn't have BLORP to handle Y-tiling, so use X-tiling. */
534 return I915_TILING_X
;
536 /* From the Sandybridge PRM, Volume 1, Part 2, page 32:
537 * "NOTE: 128BPE Format Color Buffer ( render target ) MUST be either TileX
539 * 128 bits per pixel translates to 16 bytes per pixel. This is necessary
540 * all the way back to 965, but is permitted on Gen7+.
542 if (brw
->gen
< 7 && mt
->cpp
>= 16)
543 return I915_TILING_X
;
545 /* From the Ivy Bridge PRM, Vol4 Part1 2.12.2.1 (SURFACE_STATE for most
546 * messages), on p64, under the heading "Surface Vertical Alignment":
548 * This field must be set to VALIGN_4 for all tiled Y Render Target
551 * So if the surface is renderable and uses a vertical alignment of 2,
552 * force it to be X tiled. This is somewhat conservative (it's possible
553 * that the client won't ever render to this surface), but it's difficult
554 * to know that ahead of time. And besides, since we use a vertical
555 * alignment of 4 as often as we can, this shouldn't happen very often.
557 if (brw
->gen
== 7 && mt
->align_h
== 2 &&
558 brw
->format_supported_as_render_target
[format
]) {
559 return I915_TILING_X
;
562 return I915_TILING_Y
| I915_TILING_X
;
567 * Choose an appropriate uncompressed format for a requested
568 * compressed format, if unsupported.
571 intel_lower_compressed_format(struct brw_context
*brw
, mesa_format format
)
573 /* No need to lower ETC formats on these platforms,
574 * they are supported natively.
576 if (brw
->gen
>= 8 || brw
->is_baytrail
)
580 case MESA_FORMAT_ETC1_RGB8
:
581 return MESA_FORMAT_R8G8B8X8_UNORM
;
582 case MESA_FORMAT_ETC2_RGB8
:
583 return MESA_FORMAT_R8G8B8X8_UNORM
;
584 case MESA_FORMAT_ETC2_SRGB8
:
585 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
:
586 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
:
587 return MESA_FORMAT_B8G8R8A8_SRGB
;
588 case MESA_FORMAT_ETC2_RGBA8_EAC
:
589 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
:
590 return MESA_FORMAT_R8G8B8A8_UNORM
;
591 case MESA_FORMAT_ETC2_R11_EAC
:
592 return MESA_FORMAT_R_UNORM16
;
593 case MESA_FORMAT_ETC2_SIGNED_R11_EAC
:
594 return MESA_FORMAT_R_SNORM16
;
595 case MESA_FORMAT_ETC2_RG11_EAC
:
596 return MESA_FORMAT_R16G16_UNORM
;
597 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC
:
598 return MESA_FORMAT_R16G16_SNORM
;
600 /* Non ETC1 / ETC2 format */
606 struct intel_mipmap_tree
*
607 intel_miptree_create(struct brw_context
*brw
,
615 bool expect_accelerated_upload
,
617 enum intel_miptree_tiling_mode requested_tiling
,
618 bool force_all_slices_at_each_lod
)
620 struct intel_mipmap_tree
*mt
;
621 mesa_format tex_format
= format
;
622 mesa_format etc_format
= MESA_FORMAT_NONE
;
623 GLuint total_width
, total_height
;
625 format
= intel_lower_compressed_format(brw
, format
);
627 etc_format
= (format
!= tex_format
) ? tex_format
: MESA_FORMAT_NONE
;
629 mt
= intel_miptree_create_layout(brw
, target
, format
,
630 first_level
, last_level
, width0
,
633 force_all_slices_at_each_lod
,
634 false /*disable_aux_buffers*/);
636 * pitch == 0 || height == 0 indicates the null texture
638 if (!mt
|| !mt
->total_width
|| !mt
->total_height
) {
639 intel_miptree_release(&mt
);
643 total_width
= mt
->total_width
;
644 total_height
= mt
->total_height
;
646 if (format
== MESA_FORMAT_S_UINT8
) {
647 /* Align to size of W tile, 64x64. */
648 total_width
= ALIGN(total_width
, 64);
649 total_height
= ALIGN(total_height
, 64);
652 uint32_t tiling
= intel_miptree_choose_tiling(brw
, format
, width0
,
653 num_samples
, requested_tiling
,
657 if (tiling
== (I915_TILING_Y
| I915_TILING_X
)) {
659 mt
->tiling
= I915_TILING_Y
;
665 mt
->etc_format
= etc_format
;
666 mt
->bo
= drm_intel_bo_alloc_tiled(brw
->bufmgr
, "miptree",
667 total_width
, total_height
, mt
->cpp
,
669 (expect_accelerated_upload
?
670 BO_ALLOC_FOR_RENDER
: 0));
673 /* If the BO is too large to fit in the aperture, we need to use the
674 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
675 * handle Y-tiling, so we need to fall back to X.
677 if (brw
->gen
< 6 && y_or_x
&& mt
->bo
->size
>= brw
->max_gtt_map_object_size
) {
678 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
679 mt
->total_width
, mt
->total_height
);
681 mt
->tiling
= I915_TILING_X
;
682 drm_intel_bo_unreference(mt
->bo
);
683 mt
->bo
= drm_intel_bo_alloc_tiled(brw
->bufmgr
, "miptree",
684 total_width
, total_height
, mt
->cpp
,
686 (expect_accelerated_upload
?
687 BO_ALLOC_FOR_RENDER
: 0));
694 intel_miptree_release(&mt
);
699 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
700 if (!intel_miptree_alloc_mcs(brw
, mt
, num_samples
)) {
701 intel_miptree_release(&mt
);
706 /* If this miptree is capable of supporting fast color clears, set
707 * fast_clear_state appropriately to ensure that fast clears will occur.
708 * Allocation of the MCS miptree will be deferred until the first fast
709 * clear actually occurs.
711 if (intel_is_non_msrt_mcs_buffer_supported(brw
, mt
))
712 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;
717 struct intel_mipmap_tree
*
718 intel_miptree_create_for_bo(struct brw_context
*brw
,
726 bool disable_aux_buffers
)
728 struct intel_mipmap_tree
*mt
;
729 uint32_t tiling
, swizzle
;
732 drm_intel_bo_get_tiling(bo
, &tiling
, &swizzle
);
734 /* Nothing will be able to use this miptree with the BO if the offset isn't
737 if (tiling
!= I915_TILING_NONE
)
738 assert(offset
% 4096 == 0);
740 /* miptrees can't handle negative pitch. If you need flipping of images,
741 * that's outside of the scope of the mt.
745 target
= depth
> 1 ? GL_TEXTURE_2D_ARRAY
: GL_TEXTURE_2D
;
747 mt
= intel_miptree_create_layout(brw
, target
, format
,
749 width
, height
, depth
,
751 disable_aux_buffers
);
755 drm_intel_bo_reference(bo
);
765 * For a singlesample renderbuffer, this simply wraps the given BO with a
768 * For a multisample renderbuffer, this wraps the window system's
769 * (singlesample) BO with a singlesample miptree attached to the
770 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
771 * that will contain the actual rendering (which is lazily resolved to
772 * irb->singlesample_mt).
775 intel_update_winsys_renderbuffer_miptree(struct brw_context
*intel
,
776 struct intel_renderbuffer
*irb
,
778 uint32_t width
, uint32_t height
,
781 struct intel_mipmap_tree
*singlesample_mt
= NULL
;
782 struct intel_mipmap_tree
*multisample_mt
= NULL
;
783 struct gl_renderbuffer
*rb
= &irb
->Base
.Base
;
784 mesa_format format
= rb
->Format
;
785 int num_samples
= rb
->NumSamples
;
787 /* Only the front and back buffers, which are color buffers, are allocated
788 * through the image loader.
790 assert(_mesa_get_format_base_format(format
) == GL_RGB
||
791 _mesa_get_format_base_format(format
) == GL_RGBA
);
793 singlesample_mt
= intel_miptree_create_for_bo(intel
,
802 if (!singlesample_mt
)
805 /* If this miptree is capable of supporting fast color clears, set
806 * mcs_state appropriately to ensure that fast clears will occur.
807 * Allocation of the MCS miptree will be deferred until the first fast
808 * clear actually occurs.
810 if (intel_is_non_msrt_mcs_buffer_supported(intel
, singlesample_mt
))
811 singlesample_mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;
813 if (num_samples
== 0) {
814 intel_miptree_release(&irb
->mt
);
815 irb
->mt
= singlesample_mt
;
817 assert(!irb
->singlesample_mt
);
819 intel_miptree_release(&irb
->singlesample_mt
);
820 irb
->singlesample_mt
= singlesample_mt
;
823 irb
->mt
->logical_width0
!= width
||
824 irb
->mt
->logical_height0
!= height
) {
825 multisample_mt
= intel_miptree_create_for_renderbuffer(intel
,
833 irb
->need_downsample
= false;
834 intel_miptree_release(&irb
->mt
);
835 irb
->mt
= multisample_mt
;
841 intel_miptree_release(&irb
->singlesample_mt
);
842 intel_miptree_release(&irb
->mt
);
846 struct intel_mipmap_tree
*
847 intel_miptree_create_for_renderbuffer(struct brw_context
*brw
,
851 uint32_t num_samples
)
853 struct intel_mipmap_tree
*mt
;
856 GLenum target
= num_samples
> 1 ? GL_TEXTURE_2D_MULTISAMPLE
: GL_TEXTURE_2D
;
858 mt
= intel_miptree_create(brw
, target
, format
, 0, 0,
859 width
, height
, depth
, true, num_samples
,
860 INTEL_MIPTREE_TILING_ANY
, false);
864 if (intel_miptree_wants_hiz_buffer(brw
, mt
)) {
865 ok
= intel_miptree_alloc_hiz(brw
, mt
);
873 intel_miptree_release(&mt
);
878 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
879 struct intel_mipmap_tree
*src
)
884 intel_miptree_release(dst
);
888 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
896 intel_miptree_release(struct intel_mipmap_tree
**mt
)
901 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
902 if (--(*mt
)->refcount
<= 0) {
905 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
907 drm_intel_bo_unreference((*mt
)->bo
);
908 intel_miptree_release(&(*mt
)->stencil_mt
);
909 if ((*mt
)->hiz_buf
) {
910 if ((*mt
)->hiz_buf
->mt
)
911 intel_miptree_release(&(*mt
)->hiz_buf
->mt
);
913 drm_intel_bo_unreference((*mt
)->hiz_buf
->bo
);
914 free((*mt
)->hiz_buf
);
916 intel_miptree_release(&(*mt
)->mcs_mt
);
917 intel_resolve_map_clear(&(*mt
)->hiz_map
);
919 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
920 free((*mt
)->level
[i
].slice
);
929 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
930 int *width
, int *height
, int *depth
)
932 switch (image
->TexObject
->Target
) {
933 case GL_TEXTURE_1D_ARRAY
:
934 *width
= image
->Width
;
936 *depth
= image
->Height
;
939 *width
= image
->Width
;
940 *height
= image
->Height
;
941 *depth
= image
->Depth
;
947 * Can the image be pulled into a unified mipmap tree? This mirrors
948 * the completeness test in a lot of ways.
950 * Not sure whether I want to pass gl_texture_image here.
953 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
954 struct gl_texture_image
*image
)
956 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
957 GLuint level
= intelImage
->base
.Base
.Level
;
958 int width
, height
, depth
;
960 /* glTexImage* choose the texture object based on the target passed in, and
961 * objects can't change targets over their lifetimes, so this should be
964 assert(image
->TexObject
->Target
== mt
->target
);
966 mesa_format mt_format
= mt
->format
;
967 if (mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
&& mt
->stencil_mt
)
968 mt_format
= MESA_FORMAT_Z24_UNORM_S8_UINT
;
969 if (mt
->format
== MESA_FORMAT_Z_FLOAT32
&& mt
->stencil_mt
)
970 mt_format
= MESA_FORMAT_Z32_FLOAT_S8X24_UINT
;
971 if (mt
->etc_format
!= MESA_FORMAT_NONE
)
972 mt_format
= mt
->etc_format
;
974 if (image
->TexFormat
!= mt_format
)
977 intel_miptree_get_dimensions_for_image(image
, &width
, &height
, &depth
);
979 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
982 int level_depth
= mt
->level
[level
].depth
;
983 if (mt
->num_samples
> 1) {
984 switch (mt
->msaa_layout
) {
985 case INTEL_MSAA_LAYOUT_NONE
:
986 case INTEL_MSAA_LAYOUT_IMS
:
988 case INTEL_MSAA_LAYOUT_UMS
:
989 case INTEL_MSAA_LAYOUT_CMS
:
990 level_depth
/= mt
->num_samples
;
995 /* Test image dimensions against the base level image adjusted for
996 * minification. This will also catch images not present in the
997 * tree, changed targets, etc.
999 if (width
!= minify(mt
->logical_width0
, level
- mt
->first_level
) ||
1000 height
!= minify(mt
->logical_height0
, level
- mt
->first_level
) ||
1001 depth
!= level_depth
) {
1005 if (image
->NumSamples
!= mt
->num_samples
)
1013 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
1015 GLuint x
, GLuint y
, GLuint d
)
1017 mt
->level
[level
].depth
= d
;
1018 mt
->level
[level
].level_x
= x
;
1019 mt
->level
[level
].level_y
= y
;
1021 DBG("%s level %d, depth %d, offset %d,%d\n", __FUNCTION__
,
1024 assert(mt
->level
[level
].slice
== NULL
);
1026 mt
->level
[level
].slice
= calloc(d
, sizeof(*mt
->level
[0].slice
));
1027 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
1028 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
1033 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
1034 GLuint level
, GLuint img
,
1037 if (img
== 0 && level
== 0)
1038 assert(x
== 0 && y
== 0);
1040 assert(img
< mt
->level
[level
].depth
);
1042 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
1043 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
1045 DBG("%s level %d img %d pos %d,%d\n",
1046 __FUNCTION__
, level
, img
,
1047 mt
->level
[level
].slice
[img
].x_offset
,
1048 mt
->level
[level
].slice
[img
].y_offset
);
1052 intel_miptree_get_image_offset(const struct intel_mipmap_tree
*mt
,
1053 GLuint level
, GLuint slice
,
1054 GLuint
*x
, GLuint
*y
)
1056 assert(slice
< mt
->level
[level
].depth
);
1058 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
1059 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
1063 * This function computes masks that may be used to select the bits of the X
1064 * and Y coordinates that indicate the offset within a tile. If the BO is
1065 * untiled, the masks are set to 0.
1068 intel_miptree_get_tile_masks(const struct intel_mipmap_tree
*mt
,
1069 uint32_t *mask_x
, uint32_t *mask_y
,
1070 bool map_stencil_as_y_tiled
)
1073 uint32_t tiling
= mt
->tiling
;
1075 if (map_stencil_as_y_tiled
)
1076 tiling
= I915_TILING_Y
;
1080 unreachable("not reached");
1081 case I915_TILING_NONE
:
1082 *mask_x
= *mask_y
= 0;
1085 *mask_x
= 512 / cpp
- 1;
1089 *mask_x
= 128 / cpp
- 1;
1096 * Compute the offset (in bytes) from the start of the BO to the given x
1097 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1098 * multiples of the tile size.
1101 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree
*mt
,
1102 uint32_t x
, uint32_t y
,
1103 bool map_stencil_as_y_tiled
)
1106 uint32_t pitch
= mt
->pitch
;
1107 uint32_t tiling
= mt
->tiling
;
1109 if (map_stencil_as_y_tiled
) {
1110 tiling
= I915_TILING_Y
;
1112 /* When mapping a W-tiled stencil buffer as Y-tiled, each 64-high W-tile
1113 * gets transformed into a 32-high Y-tile. Accordingly, the pitch of
1114 * the resulting surface is twice the pitch of the original miptree,
1115 * since each row in the Y-tiled view corresponds to two rows in the
1116 * actual W-tiled surface. So we need to correct the pitch before
1117 * computing the offsets.
1124 unreachable("not reached");
1125 case I915_TILING_NONE
:
1126 return y
* pitch
+ x
* cpp
;
1128 assert((x
% (512 / cpp
)) == 0);
1129 assert((y
% 8) == 0);
1130 return y
* pitch
+ x
/ (512 / cpp
) * 4096;
1132 assert((x
% (128 / cpp
)) == 0);
1133 assert((y
% 32) == 0);
1134 return y
* pitch
+ x
/ (128 / cpp
) * 4096;
1139 * Rendering with tiled buffers requires that the base address of the buffer
1140 * be aligned to a page boundary. For renderbuffers, and sometimes with
1141 * textures, we may want the surface to point at a texture image level that
1142 * isn't at a page boundary.
1144 * This function returns an appropriately-aligned base offset
1145 * according to the tiling restrictions, plus any required x/y offset
1149 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree
*mt
,
1150 GLuint level
, GLuint slice
,
1155 uint32_t mask_x
, mask_y
;
1157 intel_miptree_get_tile_masks(mt
, &mask_x
, &mask_y
, false);
1158 intel_miptree_get_image_offset(mt
, level
, slice
, &x
, &y
);
1160 *tile_x
= x
& mask_x
;
1161 *tile_y
= y
& mask_y
;
1163 return intel_miptree_get_aligned_offset(mt
, x
& ~mask_x
, y
& ~mask_y
, false);
1167 intel_miptree_copy_slice_sw(struct brw_context
*brw
,
1168 struct intel_mipmap_tree
*dst_mt
,
1169 struct intel_mipmap_tree
*src_mt
,
1176 ptrdiff_t src_stride
, dst_stride
;
1177 int cpp
= dst_mt
->cpp
;
1179 intel_miptree_map(brw
, src_mt
,
1183 GL_MAP_READ_BIT
| BRW_MAP_DIRECT_BIT
,
1186 intel_miptree_map(brw
, dst_mt
,
1190 GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
|
1194 DBG("sw blit %s mt %p %p/%"PRIdPTR
" -> %s mt %p %p/%"PRIdPTR
" (%dx%d)\n",
1195 _mesa_get_format_name(src_mt
->format
),
1196 src_mt
, src
, src_stride
,
1197 _mesa_get_format_name(dst_mt
->format
),
1198 dst_mt
, dst
, dst_stride
,
1201 int row_size
= cpp
* width
;
1202 if (src_stride
== row_size
&&
1203 dst_stride
== row_size
) {
1204 memcpy(dst
, src
, row_size
* height
);
1206 for (int i
= 0; i
< height
; i
++) {
1207 memcpy(dst
, src
, row_size
);
1213 intel_miptree_unmap(brw
, dst_mt
, level
, slice
);
1214 intel_miptree_unmap(brw
, src_mt
, level
, slice
);
1216 /* Don't forget to copy the stencil data over, too. We could have skipped
1217 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1218 * shuffling the two data sources in/out of temporary storage instead of
1219 * the direct mapping we get this way.
1221 if (dst_mt
->stencil_mt
) {
1222 assert(src_mt
->stencil_mt
);
1223 intel_miptree_copy_slice_sw(brw
, dst_mt
->stencil_mt
, src_mt
->stencil_mt
,
1224 level
, slice
, width
, height
);
1229 intel_miptree_copy_slice(struct brw_context
*brw
,
1230 struct intel_mipmap_tree
*dst_mt
,
1231 struct intel_mipmap_tree
*src_mt
,
1237 mesa_format format
= src_mt
->format
;
1238 uint32_t width
= minify(src_mt
->physical_width0
, level
- src_mt
->first_level
);
1239 uint32_t height
= minify(src_mt
->physical_height0
, level
- src_mt
->first_level
);
1247 assert(depth
< src_mt
->level
[level
].depth
);
1248 assert(src_mt
->format
== dst_mt
->format
);
1250 if (dst_mt
->compressed
) {
1251 height
= ALIGN(height
, dst_mt
->align_h
) / dst_mt
->align_h
;
1252 width
= ALIGN(width
, dst_mt
->align_w
);
1255 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1256 * below won't apply since we can't do the depth's Y tiling or the
1257 * stencil's W tiling in the blitter.
1259 if (src_mt
->stencil_mt
) {
1260 intel_miptree_copy_slice_sw(brw
,
1267 uint32_t dst_x
, dst_y
, src_x
, src_y
;
1268 intel_miptree_get_image_offset(dst_mt
, level
, slice
, &dst_x
, &dst_y
);
1269 intel_miptree_get_image_offset(src_mt
, level
, slice
, &src_x
, &src_y
);
1271 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1272 _mesa_get_format_name(src_mt
->format
),
1273 src_mt
, src_x
, src_y
, src_mt
->pitch
,
1274 _mesa_get_format_name(dst_mt
->format
),
1275 dst_mt
, dst_x
, dst_y
, dst_mt
->pitch
,
1278 if (!intel_miptree_blit(brw
,
1279 src_mt
, level
, slice
, 0, 0, false,
1280 dst_mt
, level
, slice
, 0, 0, false,
1281 width
, height
, GL_COPY
)) {
1282 perf_debug("miptree validate blit for %s failed\n",
1283 _mesa_get_format_name(format
));
1285 intel_miptree_copy_slice_sw(brw
, dst_mt
, src_mt
, level
, slice
,
1291 * Copies the image's current data to the given miptree, and associates that
1292 * miptree with the image.
1294 * If \c invalidate is true, then the actual image data does not need to be
1295 * copied, but the image still needs to be associated to the new miptree (this
1296 * is set to true if we're about to clear the image).
1299 intel_miptree_copy_teximage(struct brw_context
*brw
,
1300 struct intel_texture_image
*intelImage
,
1301 struct intel_mipmap_tree
*dst_mt
,
1304 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
1305 struct intel_texture_object
*intel_obj
=
1306 intel_texture_object(intelImage
->base
.Base
.TexObject
);
1307 int level
= intelImage
->base
.Base
.Level
;
1308 int face
= intelImage
->base
.Base
.Face
;
1311 if (intel_obj
->base
.Target
== GL_TEXTURE_1D_ARRAY
)
1312 depth
= intelImage
->base
.Base
.Height
;
1314 depth
= intelImage
->base
.Base
.Depth
;
1317 for (int slice
= 0; slice
< depth
; slice
++) {
1318 intel_miptree_copy_slice(brw
, dst_mt
, src_mt
, level
, face
, slice
);
1322 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
1323 intel_obj
->needs_validate
= true;
1327 intel_miptree_alloc_mcs(struct brw_context
*brw
,
1328 struct intel_mipmap_tree
*mt
,
1331 assert(brw
->gen
>= 7); /* MCS only used on Gen7+ */
1332 assert(mt
->mcs_mt
== NULL
);
1333 assert(!mt
->disable_aux_buffers
);
1335 /* Choose the correct format for the MCS buffer. All that really matters
1336 * is that we allocate the right buffer size, since we'll always be
1337 * accessing this miptree using MCS-specific hardware mechanisms, which
1338 * infer the correct format based on num_samples.
1341 switch (num_samples
) {
1344 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1347 format
= MESA_FORMAT_R_UNORM8
;
1350 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1351 * for each sample, plus 8 padding bits).
1353 format
= MESA_FORMAT_R_UINT32
;
1356 unreachable("Unrecognized sample count in intel_miptree_alloc_mcs");
1359 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1361 * "The MCS surface must be stored as Tile Y."
1363 mt
->mcs_mt
= intel_miptree_create(brw
,
1369 mt
->logical_height0
,
1372 0 /* num_samples */,
1373 INTEL_MIPTREE_TILING_Y
,
1376 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1378 * When MCS buffer is enabled and bound to MSRT, it is required that it
1379 * is cleared prior to any rendering.
1381 * Since we don't use the MCS buffer for any purpose other than rendering,
1382 * it makes sense to just clear it immediately upon allocation.
1384 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1386 void *data
= intel_miptree_map_raw(brw
, mt
->mcs_mt
);
1387 memset(data
, 0xff, mt
->mcs_mt
->total_height
* mt
->mcs_mt
->pitch
);
1388 intel_miptree_unmap_raw(brw
, mt
->mcs_mt
);
1389 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_CLEAR
;
1396 intel_miptree_alloc_non_msrt_mcs(struct brw_context
*brw
,
1397 struct intel_mipmap_tree
*mt
)
1399 assert(mt
->mcs_mt
== NULL
);
1400 assert(!mt
->disable_aux_buffers
);
1402 /* The format of the MCS buffer is opaque to the driver; all that matters
1403 * is that we get its size and pitch right. We'll pretend that the format
1404 * is R32. Since an MCS tile covers 128 blocks horizontally, and a Y-tiled
1405 * R32 buffer is 32 pixels across, we'll need to scale the width down by
1406 * the block width and then a further factor of 4. Since an MCS tile
1407 * covers 256 blocks vertically, and a Y-tiled R32 buffer is 32 rows high,
1408 * we'll need to scale the height down by the block height and then a
1409 * further factor of 8.
1411 const mesa_format format
= MESA_FORMAT_R_UINT32
;
1412 unsigned block_width_px
;
1413 unsigned block_height
;
1414 intel_get_non_msrt_mcs_alignment(brw
, mt
, &block_width_px
, &block_height
);
1415 unsigned width_divisor
= block_width_px
* 4;
1416 unsigned height_divisor
= block_height
* 8;
1417 unsigned mcs_width
=
1418 ALIGN(mt
->logical_width0
, width_divisor
) / width_divisor
;
1419 unsigned mcs_height
=
1420 ALIGN(mt
->logical_height0
, height_divisor
) / height_divisor
;
1421 assert(mt
->logical_depth0
== 1);
1422 mt
->mcs_mt
= intel_miptree_create(brw
,
1431 0 /* num_samples */,
1432 INTEL_MIPTREE_TILING_Y
,
1440 * Helper for intel_miptree_alloc_hiz() that sets
1441 * \c mt->level[level].has_hiz. Return true if and only if
1442 * \c has_hiz was set.
1445 intel_miptree_level_enable_hiz(struct brw_context
*brw
,
1446 struct intel_mipmap_tree
*mt
,
1449 assert(mt
->hiz_buf
);
1451 if (brw
->gen
>= 8 || brw
->is_haswell
) {
1452 uint32_t width
= minify(mt
->physical_width0
, level
);
1453 uint32_t height
= minify(mt
->physical_height0
, level
);
1455 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1456 * and the height is 4 aligned. This allows our HiZ support
1457 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1458 * we can grow the width & height to allow the HiZ op to
1459 * force the proper size alignments.
1461 if (level
> 0 && ((width
& 7) || (height
& 3))) {
1462 DBG("mt %p level %d: HiZ DISABLED\n", mt
, level
);
1467 DBG("mt %p level %d: HiZ enabled\n", mt
, level
);
1468 mt
->level
[level
].has_hiz
= true;
1474 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1475 * buffer dimensions and allocates a bo for the hiz buffer.
1477 static struct intel_miptree_aux_buffer
*
1478 intel_gen7_hiz_buf_create(struct brw_context
*brw
,
1479 struct intel_mipmap_tree
*mt
)
1481 unsigned z_width
= mt
->logical_width0
;
1482 unsigned z_height
= mt
->logical_height0
;
1483 const unsigned z_depth
= MAX2(mt
->logical_depth0
, 1);
1484 unsigned hz_width
, hz_height
;
1485 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1490 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1491 * adjustments required for Z_Height and Z_Width based on multisampling.
1493 switch (mt
->num_samples
) {
1507 unreachable("unsupported sample count");
1510 const unsigned vertical_align
= 8; /* 'j' in the docs */
1511 const unsigned H0
= z_height
;
1512 const unsigned h0
= ALIGN(H0
, vertical_align
);
1513 const unsigned h1
= ALIGN(minify(H0
, 1), vertical_align
);
1514 const unsigned Z0
= z_depth
;
1516 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1517 hz_width
= ALIGN(z_width
, 16);
1519 if (mt
->target
== GL_TEXTURE_3D
) {
1523 for (int level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1524 unsigned h_i
= ALIGN(H_i
, vertical_align
);
1525 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1526 hz_height
+= h_i
* Z_i
;
1527 H_i
= minify(H_i
, 1);
1528 Z_i
= minify(Z_i
, 1);
1531 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1533 hz_height
= DIV_ROUND_UP(hz_height
, 2);
1535 const unsigned hz_qpitch
= h0
+ h1
+ (12 * vertical_align
);
1536 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1537 mt
->target
== GL_TEXTURE_CUBE_MAP
) {
1538 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth * 6/2) /8 ) * 8 */
1539 hz_height
= DIV_ROUND_UP(hz_qpitch
* Z0
* 6, 2 * 8) * 8;
1541 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1542 hz_height
= DIV_ROUND_UP(hz_qpitch
* Z0
, 2 * 8) * 8;
1546 unsigned long pitch
;
1547 uint32_t tiling
= I915_TILING_Y
;
1548 buf
->bo
= drm_intel_bo_alloc_tiled(brw
->bufmgr
, "hiz",
1549 hz_width
, hz_height
, 1,
1551 BO_ALLOC_FOR_RENDER
);
1555 } else if (tiling
!= I915_TILING_Y
) {
1556 drm_intel_bo_unreference(buf
->bo
);
1568 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1569 * buffer dimensions and allocates a bo for the hiz buffer.
1571 static struct intel_miptree_aux_buffer
*
1572 intel_gen8_hiz_buf_create(struct brw_context
*brw
,
1573 struct intel_mipmap_tree
*mt
)
1575 unsigned z_width
= mt
->logical_width0
;
1576 unsigned z_height
= mt
->logical_height0
;
1577 const unsigned z_depth
= MAX2(mt
->logical_depth0
, 1);
1578 unsigned hz_width
, hz_height
;
1579 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1584 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1585 * adjustments required for Z_Height and Z_Width based on multisampling.
1587 switch (mt
->num_samples
) {
1601 unreachable("unsupported sample count");
1604 const unsigned vertical_align
= 8; /* 'j' in the docs */
1605 const unsigned H0
= z_height
;
1606 const unsigned h0
= ALIGN(H0
, vertical_align
);
1607 const unsigned h1
= ALIGN(minify(H0
, 1), vertical_align
);
1608 const unsigned Z0
= z_depth
;
1610 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1611 hz_width
= ALIGN(z_width
, 16);
1615 unsigned sum_h_i
= 0;
1616 unsigned hz_height_3d_sum
= 0;
1617 for (int level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1618 unsigned i
= level
- mt
->first_level
;
1619 unsigned h_i
= ALIGN(H_i
, vertical_align
);
1620 /* sum(i=2 to m; h_i) */
1624 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1625 hz_height_3d_sum
+= h_i
* Z_i
;
1626 H_i
= minify(H_i
, 1);
1627 Z_i
= minify(Z_i
, 1);
1629 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1630 buf
->qpitch
= h0
+ MAX2(h1
, sum_h_i
);
1632 if (mt
->target
== GL_TEXTURE_3D
) {
1633 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1634 hz_height
= DIV_ROUND_UP(hz_height_3d_sum
, 2);
1636 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1637 hz_height
= DIV_ROUND_UP(buf
->qpitch
, 2 * 8) * 8 * Z0
;
1638 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1639 mt
->target
== GL_TEXTURE_CUBE_MAP
) {
1640 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * 6 * Z_Depth
1642 * We can can just take our hz_height calculation from above, and
1643 * multiply by 6 for the cube map and cube map array types.
1649 unsigned long pitch
;
1650 uint32_t tiling
= I915_TILING_Y
;
1651 buf
->bo
= drm_intel_bo_alloc_tiled(brw
->bufmgr
, "hiz",
1652 hz_width
, hz_height
, 1,
1654 BO_ALLOC_FOR_RENDER
);
1658 } else if (tiling
!= I915_TILING_Y
) {
1659 drm_intel_bo_unreference(buf
->bo
);
1670 static struct intel_miptree_aux_buffer
*
1671 intel_hiz_miptree_buf_create(struct brw_context
*brw
,
1672 struct intel_mipmap_tree
*mt
)
1674 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1675 const bool force_all_slices_at_each_lod
= brw
->gen
== 6;
1680 buf
->mt
= intel_miptree_create(brw
,
1686 mt
->logical_height0
,
1690 INTEL_MIPTREE_TILING_ANY
,
1691 force_all_slices_at_each_lod
);
1697 buf
->bo
= buf
->mt
->bo
;
1698 buf
->pitch
= buf
->mt
->pitch
;
1699 buf
->qpitch
= buf
->mt
->qpitch
;
1705 intel_miptree_wants_hiz_buffer(struct brw_context
*brw
,
1706 struct intel_mipmap_tree
*mt
)
1711 if (mt
->hiz_buf
!= NULL
)
1714 if (mt
->disable_aux_buffers
)
1717 switch (mt
->format
) {
1718 case MESA_FORMAT_Z_FLOAT32
:
1719 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
1720 case MESA_FORMAT_Z24_UNORM_X8_UINT
:
1721 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
1722 case MESA_FORMAT_Z_UNORM16
:
1730 intel_miptree_alloc_hiz(struct brw_context
*brw
,
1731 struct intel_mipmap_tree
*mt
)
1733 assert(mt
->hiz_buf
== NULL
);
1734 assert(!mt
->disable_aux_buffers
);
1736 if (brw
->gen
== 7) {
1737 mt
->hiz_buf
= intel_gen7_hiz_buf_create(brw
, mt
);
1738 } else if (brw
->gen
>= 8) {
1739 mt
->hiz_buf
= intel_gen8_hiz_buf_create(brw
, mt
);
1741 mt
->hiz_buf
= intel_hiz_miptree_buf_create(brw
, mt
);
1747 /* Mark that all slices need a HiZ resolve. */
1748 for (int level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1749 if (!intel_miptree_level_enable_hiz(brw
, mt
, level
))
1752 for (int layer
= 0; layer
< mt
->level
[level
].depth
; ++layer
) {
1753 struct intel_resolve_map
*m
= malloc(sizeof(struct intel_resolve_map
));
1754 exec_node_init(&m
->link
);
1757 m
->need
= GEN6_HIZ_OP_HIZ_RESOLVE
;
1759 exec_list_push_tail(&mt
->hiz_map
, &m
->link
);
1767 * Does the miptree slice have hiz enabled?
1770 intel_miptree_level_has_hiz(struct intel_mipmap_tree
*mt
, uint32_t level
)
1772 intel_miptree_check_level_layer(mt
, level
, 0);
1773 return mt
->level
[level
].has_hiz
;
1777 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree
*mt
,
1781 if (!intel_miptree_level_has_hiz(mt
, level
))
1784 intel_resolve_map_set(&mt
->hiz_map
,
1785 level
, layer
, GEN6_HIZ_OP_HIZ_RESOLVE
);
1790 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree
*mt
,
1794 if (!intel_miptree_level_has_hiz(mt
, level
))
1797 intel_resolve_map_set(&mt
->hiz_map
,
1798 level
, layer
, GEN6_HIZ_OP_DEPTH_RESOLVE
);
1802 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree
*mt
,
1806 uint32_t end_layer
= mt
->level
[level
].depth
;
1808 for (layer
= 0; layer
< end_layer
; layer
++) {
1809 intel_miptree_slice_set_needs_depth_resolve(mt
, level
, layer
);
1814 intel_miptree_slice_resolve(struct brw_context
*brw
,
1815 struct intel_mipmap_tree
*mt
,
1818 enum gen6_hiz_op need
)
1820 intel_miptree_check_level_layer(mt
, level
, layer
);
1822 struct intel_resolve_map
*item
=
1823 intel_resolve_map_get(&mt
->hiz_map
, level
, layer
);
1825 if (!item
|| item
->need
!= need
)
1828 intel_hiz_exec(brw
, mt
, level
, layer
, need
);
1829 intel_resolve_map_remove(item
);
1834 intel_miptree_slice_resolve_hiz(struct brw_context
*brw
,
1835 struct intel_mipmap_tree
*mt
,
1839 return intel_miptree_slice_resolve(brw
, mt
, level
, layer
,
1840 GEN6_HIZ_OP_HIZ_RESOLVE
);
1844 intel_miptree_slice_resolve_depth(struct brw_context
*brw
,
1845 struct intel_mipmap_tree
*mt
,
1849 return intel_miptree_slice_resolve(brw
, mt
, level
, layer
,
1850 GEN6_HIZ_OP_DEPTH_RESOLVE
);
1854 intel_miptree_all_slices_resolve(struct brw_context
*brw
,
1855 struct intel_mipmap_tree
*mt
,
1856 enum gen6_hiz_op need
)
1858 bool did_resolve
= false;
1860 foreach_list_typed_safe(struct intel_resolve_map
, map
, link
, &mt
->hiz_map
) {
1861 if (map
->need
!= need
)
1864 intel_hiz_exec(brw
, mt
, map
->level
, map
->layer
, need
);
1865 intel_resolve_map_remove(map
);
1873 intel_miptree_all_slices_resolve_hiz(struct brw_context
*brw
,
1874 struct intel_mipmap_tree
*mt
)
1876 return intel_miptree_all_slices_resolve(brw
, mt
,
1877 GEN6_HIZ_OP_HIZ_RESOLVE
);
1881 intel_miptree_all_slices_resolve_depth(struct brw_context
*brw
,
1882 struct intel_mipmap_tree
*mt
)
1884 return intel_miptree_all_slices_resolve(brw
, mt
,
1885 GEN6_HIZ_OP_DEPTH_RESOLVE
);
1890 intel_miptree_resolve_color(struct brw_context
*brw
,
1891 struct intel_mipmap_tree
*mt
)
1893 switch (mt
->fast_clear_state
) {
1894 case INTEL_FAST_CLEAR_STATE_NO_MCS
:
1895 case INTEL_FAST_CLEAR_STATE_RESOLVED
:
1896 /* No resolve needed */
1898 case INTEL_FAST_CLEAR_STATE_UNRESOLVED
:
1899 case INTEL_FAST_CLEAR_STATE_CLEAR
:
1900 /* Fast color clear resolves only make sense for non-MSAA buffers. */
1901 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
)
1902 brw_meta_resolve_color(brw
, mt
);
1909 * Make it possible to share the BO backing the given miptree with another
1910 * process or another miptree.
1912 * Fast color clears are unsafe with shared buffers, so we need to resolve and
1913 * then discard the MCS buffer, if present. We also set the fast_clear_state
1914 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
1915 * allocated in the future.
1918 intel_miptree_make_shareable(struct brw_context
*brw
,
1919 struct intel_mipmap_tree
*mt
)
1921 /* MCS buffers are also used for multisample buffers, but we can't resolve
1922 * away a multisample MCS buffer because it's an integral part of how the
1923 * pixel data is stored. Fortunately this code path should never be
1924 * reached for multisample buffers.
1926 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
);
1929 intel_miptree_resolve_color(brw
, mt
);
1930 intel_miptree_release(&mt
->mcs_mt
);
1931 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_NO_MCS
;
1937 * \brief Get pointer offset into stencil buffer.
1939 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
1940 * must decode the tile's layout in software.
1943 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
1945 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
1947 * Even though the returned offset is always positive, the return type is
1949 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
1950 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
1953 intel_offset_S8(uint32_t stride
, uint32_t x
, uint32_t y
, bool swizzled
)
1955 uint32_t tile_size
= 4096;
1956 uint32_t tile_width
= 64;
1957 uint32_t tile_height
= 64;
1958 uint32_t row_size
= 64 * stride
;
1960 uint32_t tile_x
= x
/ tile_width
;
1961 uint32_t tile_y
= y
/ tile_height
;
1963 /* The byte's address relative to the tile's base addres. */
1964 uint32_t byte_x
= x
% tile_width
;
1965 uint32_t byte_y
= y
% tile_height
;
1967 uintptr_t u
= tile_y
* row_size
1968 + tile_x
* tile_size
1969 + 512 * (byte_x
/ 8)
1971 + 32 * ((byte_y
/ 4) % 2)
1972 + 16 * ((byte_x
/ 4) % 2)
1973 + 8 * ((byte_y
/ 2) % 2)
1974 + 4 * ((byte_x
/ 2) % 2)
1979 /* adjust for bit6 swizzling */
1980 if (((byte_x
/ 8) % 2) == 1) {
1981 if (((byte_y
/ 8) % 2) == 0) {
1993 intel_miptree_updownsample(struct brw_context
*brw
,
1994 struct intel_mipmap_tree
*src
,
1995 struct intel_mipmap_tree
*dst
)
1998 brw_blorp_blit_miptrees(brw
,
1999 src
, 0 /* level */, 0 /* layer */, src
->format
,
2000 dst
, 0 /* level */, 0 /* layer */, dst
->format
,
2002 src
->logical_width0
, src
->logical_height0
,
2004 dst
->logical_width0
, dst
->logical_height0
,
2005 GL_NEAREST
, false, false /*mirror x, y*/);
2006 } else if (src
->format
== MESA_FORMAT_S_UINT8
) {
2007 brw_meta_stencil_updownsample(brw
, src
, dst
);
2009 brw_meta_updownsample(brw
, src
, dst
);
2012 if (src
->stencil_mt
) {
2013 if (brw
->gen
>= 8) {
2014 brw_meta_stencil_updownsample(brw
, src
->stencil_mt
, dst
);
2018 brw_blorp_blit_miptrees(brw
,
2019 src
->stencil_mt
, 0 /* level */, 0 /* layer */,
2020 src
->stencil_mt
->format
,
2021 dst
->stencil_mt
, 0 /* level */, 0 /* layer */,
2022 dst
->stencil_mt
->format
,
2024 src
->logical_width0
, src
->logical_height0
,
2026 dst
->logical_width0
, dst
->logical_height0
,
2027 GL_NEAREST
, false, false /*mirror x, y*/);
2032 intel_miptree_map_raw(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
)
2034 /* CPU accesses to color buffers don't understand fast color clears, so
2035 * resolve any pending fast color clears before we map.
2037 intel_miptree_resolve_color(brw
, mt
);
2039 drm_intel_bo
*bo
= mt
->bo
;
2041 if (drm_intel_bo_references(brw
->batch
.bo
, bo
))
2042 intel_batchbuffer_flush(brw
);
2044 if (mt
->tiling
!= I915_TILING_NONE
)
2045 brw_bo_map_gtt(brw
, bo
, "miptree");
2047 brw_bo_map(brw
, bo
, true, "miptree");
2053 intel_miptree_unmap_raw(struct brw_context
*brw
,
2054 struct intel_mipmap_tree
*mt
)
2056 drm_intel_bo_unmap(mt
->bo
);
2060 intel_miptree_map_gtt(struct brw_context
*brw
,
2061 struct intel_mipmap_tree
*mt
,
2062 struct intel_miptree_map
*map
,
2063 unsigned int level
, unsigned int slice
)
2065 unsigned int bw
, bh
;
2067 unsigned int image_x
, image_y
;
2068 intptr_t x
= map
->x
;
2069 intptr_t y
= map
->y
;
2071 /* For compressed formats, the stride is the number of bytes per
2072 * row of blocks. intel_miptree_get_image_offset() already does
2075 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
2076 assert(y
% bh
== 0);
2079 base
= intel_miptree_map_raw(brw
, mt
) + mt
->offset
;
2084 /* Note that in the case of cube maps, the caller must have passed the
2085 * slice number referencing the face.
2087 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2091 map
->stride
= mt
->pitch
;
2092 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
2095 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2096 "%"PRIiPTR
",%"PRIiPTR
" = %p/%d\n", __FUNCTION__
,
2097 map
->x
, map
->y
, map
->w
, map
->h
,
2098 mt
, _mesa_get_format_name(mt
->format
),
2099 x
, y
, map
->ptr
, map
->stride
);
2103 intel_miptree_unmap_gtt(struct brw_context
*brw
,
2104 struct intel_mipmap_tree
*mt
,
2105 struct intel_miptree_map
*map
,
2109 intel_miptree_unmap_raw(brw
, mt
);
2113 intel_miptree_map_blit(struct brw_context
*brw
,
2114 struct intel_mipmap_tree
*mt
,
2115 struct intel_miptree_map
*map
,
2116 unsigned int level
, unsigned int slice
)
2118 map
->mt
= intel_miptree_create(brw
, GL_TEXTURE_2D
, mt
->format
,
2122 INTEL_MIPTREE_TILING_NONE
,
2125 fprintf(stderr
, "Failed to allocate blit temporary\n");
2128 map
->stride
= map
->mt
->pitch
;
2130 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2131 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2132 * invalidate is set, since we'll be writing the whole rectangle from our
2133 * temporary buffer back out.
2135 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
2136 if (!intel_miptree_blit(brw
,
2138 map
->x
, map
->y
, false,
2141 map
->w
, map
->h
, GL_COPY
)) {
2142 fprintf(stderr
, "Failed to blit\n");
2147 map
->ptr
= intel_miptree_map_raw(brw
, map
->mt
);
2149 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
2150 map
->x
, map
->y
, map
->w
, map
->h
,
2151 mt
, _mesa_get_format_name(mt
->format
),
2152 level
, slice
, map
->ptr
, map
->stride
);
2157 intel_miptree_release(&map
->mt
);
2163 intel_miptree_unmap_blit(struct brw_context
*brw
,
2164 struct intel_mipmap_tree
*mt
,
2165 struct intel_miptree_map
*map
,
2169 struct gl_context
*ctx
= &brw
->ctx
;
2171 intel_miptree_unmap_raw(brw
, map
->mt
);
2173 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2174 bool ok
= intel_miptree_blit(brw
,
2178 map
->x
, map
->y
, false,
2179 map
->w
, map
->h
, GL_COPY
);
2180 WARN_ONCE(!ok
, "Failed to blit from linear temporary mapping");
2183 intel_miptree_release(&map
->mt
);
2187 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2189 #if defined(USE_SSE41)
2191 intel_miptree_map_movntdqa(struct brw_context
*brw
,
2192 struct intel_mipmap_tree
*mt
,
2193 struct intel_miptree_map
*map
,
2194 unsigned int level
, unsigned int slice
)
2196 assert(map
->mode
& GL_MAP_READ_BIT
);
2197 assert(!(map
->mode
& GL_MAP_WRITE_BIT
));
2199 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__
,
2200 map
->x
, map
->y
, map
->w
, map
->h
,
2201 mt
, _mesa_get_format_name(mt
->format
),
2202 level
, slice
, map
->ptr
, map
->stride
);
2204 /* Map the original image */
2207 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2211 void *src
= intel_miptree_map_raw(brw
, mt
);
2214 src
+= image_y
* mt
->pitch
;
2215 src
+= image_x
* mt
->cpp
;
2217 /* Due to the pixel offsets for the particular image being mapped, our
2218 * src pointer may not be 16-byte aligned. However, if the pitch is
2219 * divisible by 16, then the amount by which it's misaligned will remain
2220 * consistent from row to row.
2222 assert((mt
->pitch
% 16) == 0);
2223 const int misalignment
= ((uintptr_t) src
) & 15;
2225 /* Create an untiled temporary buffer for the mapping. */
2226 const unsigned width_bytes
= _mesa_format_row_stride(mt
->format
, map
->w
);
2228 map
->stride
= ALIGN(misalignment
+ width_bytes
, 16);
2230 map
->buffer
= _mesa_align_malloc(map
->stride
* map
->h
, 16);
2231 /* Offset the destination so it has the same misalignment as src. */
2232 map
->ptr
= map
->buffer
+ misalignment
;
2234 assert((((uintptr_t) map
->ptr
) & 15) == misalignment
);
2236 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2237 void *dst_ptr
= map
->ptr
+ y
* map
->stride
;
2238 void *src_ptr
= src
+ y
* mt
->pitch
;
2240 _mesa_streaming_load_memcpy(dst_ptr
, src_ptr
, width_bytes
);
2243 intel_miptree_unmap_raw(brw
, mt
);
2247 intel_miptree_unmap_movntdqa(struct brw_context
*brw
,
2248 struct intel_mipmap_tree
*mt
,
2249 struct intel_miptree_map
*map
,
2253 _mesa_align_free(map
->buffer
);
2260 intel_miptree_map_s8(struct brw_context
*brw
,
2261 struct intel_mipmap_tree
*mt
,
2262 struct intel_miptree_map
*map
,
2263 unsigned int level
, unsigned int slice
)
2265 map
->stride
= map
->w
;
2266 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
2270 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2271 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2272 * invalidate is set, since we'll be writing the whole rectangle from our
2273 * temporary buffer back out.
2275 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
2276 uint8_t *untiled_s8_map
= map
->ptr
;
2277 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
);
2278 unsigned int image_x
, image_y
;
2280 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2282 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2283 for (uint32_t x
= 0; x
< map
->w
; x
++) {
2284 ptrdiff_t offset
= intel_offset_S8(mt
->pitch
,
2285 x
+ image_x
+ map
->x
,
2286 y
+ image_y
+ map
->y
,
2287 brw
->has_swizzling
);
2288 untiled_s8_map
[y
* map
->w
+ x
] = tiled_s8_map
[offset
];
2292 intel_miptree_unmap_raw(brw
, mt
);
2294 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__
,
2295 map
->x
, map
->y
, map
->w
, map
->h
,
2296 mt
, map
->x
+ image_x
, map
->y
+ image_y
, map
->ptr
, map
->stride
);
2298 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__
,
2299 map
->x
, map
->y
, map
->w
, map
->h
,
2300 mt
, map
->ptr
, map
->stride
);
2305 intel_miptree_unmap_s8(struct brw_context
*brw
,
2306 struct intel_mipmap_tree
*mt
,
2307 struct intel_miptree_map
*map
,
2311 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2312 unsigned int image_x
, image_y
;
2313 uint8_t *untiled_s8_map
= map
->ptr
;
2314 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
);
2316 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2318 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2319 for (uint32_t x
= 0; x
< map
->w
; x
++) {
2320 ptrdiff_t offset
= intel_offset_S8(mt
->pitch
,
2323 brw
->has_swizzling
);
2324 tiled_s8_map
[offset
] = untiled_s8_map
[y
* map
->w
+ x
];
2328 intel_miptree_unmap_raw(brw
, mt
);
2335 intel_miptree_map_etc(struct brw_context
*brw
,
2336 struct intel_mipmap_tree
*mt
,
2337 struct intel_miptree_map
*map
,
2341 assert(mt
->etc_format
!= MESA_FORMAT_NONE
);
2342 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
) {
2343 assert(mt
->format
== MESA_FORMAT_R8G8B8X8_UNORM
);
2346 assert(map
->mode
& GL_MAP_WRITE_BIT
);
2347 assert(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
);
2349 map
->stride
= _mesa_format_row_stride(mt
->etc_format
, map
->w
);
2350 map
->buffer
= malloc(_mesa_format_image_size(mt
->etc_format
,
2351 map
->w
, map
->h
, 1));
2352 map
->ptr
= map
->buffer
;
2356 intel_miptree_unmap_etc(struct brw_context
*brw
,
2357 struct intel_mipmap_tree
*mt
,
2358 struct intel_miptree_map
*map
,
2364 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2369 uint8_t *dst
= intel_miptree_map_raw(brw
, mt
)
2370 + image_y
* mt
->pitch
2371 + image_x
* mt
->cpp
;
2373 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
)
2374 _mesa_etc1_unpack_rgba8888(dst
, mt
->pitch
,
2375 map
->ptr
, map
->stride
,
2378 _mesa_unpack_etc2_format(dst
, mt
->pitch
,
2379 map
->ptr
, map
->stride
,
2380 map
->w
, map
->h
, mt
->etc_format
);
2382 intel_miptree_unmap_raw(brw
, mt
);
2387 * Mapping function for packed depth/stencil miptrees backed by real separate
2388 * miptrees for depth and stencil.
2390 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2391 * separate from the depth buffer. Yet at the GL API level, we have to expose
2392 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2393 * be able to map that memory for texture storage and glReadPixels-type
2394 * operations. We give Mesa core that access by mallocing a temporary and
2395 * copying the data between the actual backing store and the temporary.
2398 intel_miptree_map_depthstencil(struct brw_context
*brw
,
2399 struct intel_mipmap_tree
*mt
,
2400 struct intel_miptree_map
*map
,
2401 unsigned int level
, unsigned int slice
)
2403 struct intel_mipmap_tree
*z_mt
= mt
;
2404 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
2405 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
2406 int packed_bpp
= map_z32f_x24s8
? 8 : 4;
2408 map
->stride
= map
->w
* packed_bpp
;
2409 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
2413 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2414 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2415 * invalidate is set, since we'll be writing the whole rectangle from our
2416 * temporary buffer back out.
2418 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
2419 uint32_t *packed_map
= map
->ptr
;
2420 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
);
2421 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
);
2422 unsigned int s_image_x
, s_image_y
;
2423 unsigned int z_image_x
, z_image_y
;
2425 intel_miptree_get_image_offset(s_mt
, level
, slice
,
2426 &s_image_x
, &s_image_y
);
2427 intel_miptree_get_image_offset(z_mt
, level
, slice
,
2428 &z_image_x
, &z_image_y
);
2430 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2431 for (uint32_t x
= 0; x
< map
->w
; x
++) {
2432 int map_x
= map
->x
+ x
, map_y
= map
->y
+ y
;
2433 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->pitch
,
2436 brw
->has_swizzling
);
2437 ptrdiff_t z_offset
= ((map_y
+ z_image_y
) *
2439 (map_x
+ z_image_x
));
2440 uint8_t s
= s_map
[s_offset
];
2441 uint32_t z
= z_map
[z_offset
];
2443 if (map_z32f_x24s8
) {
2444 packed_map
[(y
* map
->w
+ x
) * 2 + 0] = z
;
2445 packed_map
[(y
* map
->w
+ x
) * 2 + 1] = s
;
2447 packed_map
[y
* map
->w
+ x
] = (s
<< 24) | (z
& 0x00ffffff);
2452 intel_miptree_unmap_raw(brw
, s_mt
);
2453 intel_miptree_unmap_raw(brw
, z_mt
);
2455 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
2457 map
->x
, map
->y
, map
->w
, map
->h
,
2458 z_mt
, map
->x
+ z_image_x
, map
->y
+ z_image_y
,
2459 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
2460 map
->ptr
, map
->stride
);
2462 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__
,
2463 map
->x
, map
->y
, map
->w
, map
->h
,
2464 mt
, map
->ptr
, map
->stride
);
2469 intel_miptree_unmap_depthstencil(struct brw_context
*brw
,
2470 struct intel_mipmap_tree
*mt
,
2471 struct intel_miptree_map
*map
,
2475 struct intel_mipmap_tree
*z_mt
= mt
;
2476 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
2477 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
2479 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2480 uint32_t *packed_map
= map
->ptr
;
2481 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
);
2482 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
);
2483 unsigned int s_image_x
, s_image_y
;
2484 unsigned int z_image_x
, z_image_y
;
2486 intel_miptree_get_image_offset(s_mt
, level
, slice
,
2487 &s_image_x
, &s_image_y
);
2488 intel_miptree_get_image_offset(z_mt
, level
, slice
,
2489 &z_image_x
, &z_image_y
);
2491 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2492 for (uint32_t x
= 0; x
< map
->w
; x
++) {
2493 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->pitch
,
2494 x
+ s_image_x
+ map
->x
,
2495 y
+ s_image_y
+ map
->y
,
2496 brw
->has_swizzling
);
2497 ptrdiff_t z_offset
= ((y
+ z_image_y
+ map
->y
) *
2499 (x
+ z_image_x
+ map
->x
));
2501 if (map_z32f_x24s8
) {
2502 z_map
[z_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 0];
2503 s_map
[s_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 1];
2505 uint32_t packed
= packed_map
[y
* map
->w
+ x
];
2506 s_map
[s_offset
] = packed
>> 24;
2507 z_map
[z_offset
] = packed
;
2512 intel_miptree_unmap_raw(brw
, s_mt
);
2513 intel_miptree_unmap_raw(brw
, z_mt
);
2515 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2517 map
->x
, map
->y
, map
->w
, map
->h
,
2518 z_mt
, _mesa_get_format_name(z_mt
->format
),
2519 map
->x
+ z_image_x
, map
->y
+ z_image_y
,
2520 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
2521 map
->ptr
, map
->stride
);
2528 * Create and attach a map to the miptree at (level, slice). Return the
2531 static struct intel_miptree_map
*
2532 intel_miptree_attach_map(struct intel_mipmap_tree
*mt
,
2541 struct intel_miptree_map
*map
= calloc(1, sizeof(*map
));
2546 assert(mt
->level
[level
].slice
[slice
].map
== NULL
);
2547 mt
->level
[level
].slice
[slice
].map
= map
;
2559 * Release the map at (level, slice).
2562 intel_miptree_release_map(struct intel_mipmap_tree
*mt
,
2566 struct intel_miptree_map
**map
;
2568 map
= &mt
->level
[level
].slice
[slice
].map
;
2574 can_blit_slice(struct intel_mipmap_tree
*mt
,
2575 unsigned int level
, unsigned int slice
)
2579 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2580 if (image_x
>= 32768 || image_y
>= 32768)
2583 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2584 if (mt
->pitch
>= 32768)
2591 use_intel_mipree_map_blit(struct brw_context
*brw
,
2592 struct intel_mipmap_tree
*mt
,
2598 /* It's probably not worth swapping to the blit ring because of
2599 * all the overhead involved.
2601 !(mode
& GL_MAP_WRITE_BIT
) &&
2603 (mt
->tiling
== I915_TILING_X
||
2604 /* Prior to Sandybridge, the blitter can't handle Y tiling */
2605 (brw
->gen
>= 6 && mt
->tiling
== I915_TILING_Y
)) &&
2606 can_blit_slice(mt
, level
, slice
))
2609 if (mt
->tiling
!= I915_TILING_NONE
&&
2610 mt
->bo
->size
>= brw
->max_gtt_map_object_size
) {
2611 assert(can_blit_slice(mt
, level
, slice
));
2619 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
2620 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
2621 * arithmetic overflow.
2623 * If you call this function and use \a out_stride, then you're doing pointer
2624 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
2625 * bugs. The caller must still take care to avoid 32-bit overflow errors in
2626 * all arithmetic expressions that contain buffer offsets and pixel sizes,
2627 * which usually have type uint32_t or GLuint.
2630 intel_miptree_map(struct brw_context
*brw
,
2631 struct intel_mipmap_tree
*mt
,
2640 ptrdiff_t *out_stride
)
2642 struct intel_miptree_map
*map
;
2644 assert(mt
->num_samples
<= 1);
2646 map
= intel_miptree_attach_map(mt
, level
, slice
, x
, y
, w
, h
, mode
);
2653 intel_miptree_slice_resolve_depth(brw
, mt
, level
, slice
);
2654 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2655 intel_miptree_slice_set_needs_hiz_resolve(mt
, level
, slice
);
2658 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2659 intel_miptree_map_s8(brw
, mt
, map
, level
, slice
);
2660 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
2661 !(mode
& BRW_MAP_DIRECT_BIT
)) {
2662 intel_miptree_map_etc(brw
, mt
, map
, level
, slice
);
2663 } else if (mt
->stencil_mt
&& !(mode
& BRW_MAP_DIRECT_BIT
)) {
2664 intel_miptree_map_depthstencil(brw
, mt
, map
, level
, slice
);
2665 } else if (use_intel_mipree_map_blit(brw
, mt
, mode
, level
, slice
)) {
2666 intel_miptree_map_blit(brw
, mt
, map
, level
, slice
);
2667 #if defined(USE_SSE41)
2668 } else if (!(mode
& GL_MAP_WRITE_BIT
) && !mt
->compressed
&& cpu_has_sse4_1
) {
2669 intel_miptree_map_movntdqa(brw
, mt
, map
, level
, slice
);
2672 intel_miptree_map_gtt(brw
, mt
, map
, level
, slice
);
2675 *out_ptr
= map
->ptr
;
2676 *out_stride
= map
->stride
;
2678 if (map
->ptr
== NULL
)
2679 intel_miptree_release_map(mt
, level
, slice
);
2683 intel_miptree_unmap(struct brw_context
*brw
,
2684 struct intel_mipmap_tree
*mt
,
2688 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
2690 assert(mt
->num_samples
<= 1);
2695 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__
,
2696 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
2698 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2699 intel_miptree_unmap_s8(brw
, mt
, map
, level
, slice
);
2700 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
2701 !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
2702 intel_miptree_unmap_etc(brw
, mt
, map
, level
, slice
);
2703 } else if (mt
->stencil_mt
&& !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
2704 intel_miptree_unmap_depthstencil(brw
, mt
, map
, level
, slice
);
2705 } else if (map
->mt
) {
2706 intel_miptree_unmap_blit(brw
, mt
, map
, level
, slice
);
2707 #if defined(USE_SSE41)
2708 } else if (map
->buffer
&& cpu_has_sse4_1
) {
2709 intel_miptree_unmap_movntdqa(brw
, mt
, map
, level
, slice
);
2712 intel_miptree_unmap_gtt(brw
, mt
, map
, level
, slice
);
2715 intel_miptree_release_map(mt
, level
, slice
);