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 **************************************************************************/
28 /** @file intel_mipmap_tree.h
30 * This file defines the structure that wraps a BO and describes how the
31 * mipmap levels and slices of a texture are laid out.
33 * The hardware has a fixed layout of a texture depending on parameters such
34 * as the target/type (2D, 3D, CUBE), width, height, pitch, and number of
35 * mipmap levels. The individual level/layer slices are each 2D rectangles of
36 * pixels at some x/y offset from the start of the drm_intel_bo.
38 * Original OpenGL allowed texture miplevels to be specified in arbitrary
39 * order, and a texture may change size over time. Thus, each
40 * intel_texture_image has a reference to a miptree that contains the pixel
41 * data sized appropriately for it, which will later be referenced by/copied
42 * to the intel_texture_object at draw time (intel_finalize_mipmap_tree()) so
43 * that there's a single miptree for the complete texture.
46 #ifndef INTEL_MIPMAP_TREE_H
47 #define INTEL_MIPMAP_TREE_H
51 #include "main/mtypes.h"
52 #include "intel_bufmgr.h"
53 #include "intel_resolve_map.h"
54 #include <GL/internal/dri_interface.h>
61 struct intel_renderbuffer
;
63 struct intel_resolve_map
;
64 struct intel_texture_image
;
67 * When calling intel_miptree_map() on an ETC-transcoded-to-RGB miptree or a
68 * depthstencil-split-to-separate-stencil miptree, we'll normally make a
69 * tmeporary and recreate the kind of data requested by Mesa core, since we're
70 * satisfying some glGetTexImage() request or something.
72 * However, occasionally you want to actually map the miptree's current data
73 * without transcoding back. This flag to intel_miptree_map() gets you that.
75 #define BRW_MAP_DIRECT_BIT 0x80000000
77 struct intel_miptree_map
{
78 /** Bitfield of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_BIT */
80 /** Region of interest for the map. */
82 /** Possibly malloced temporary buffer for the mapping. */
84 /** Possible pointer to a temporary linear miptree for the mapping. */
85 struct intel_mipmap_tree
*mt
;
86 /** Pointer to the start of (map_x, map_y) returned by the mapping. */
88 /** Stride of the mapping. */
93 * Describes the location of each texture image within a miptree.
95 struct intel_mipmap_level
97 /** Offset to this miptree level, used in computing x_offset. */
99 /** Offset to this miptree level, used in computing y_offset. */
103 * \brief Number of 2D slices in this miplevel.
105 * The exact semantics of depth varies according to the texture target:
106 * - For GL_TEXTURE_CUBE_MAP, depth is 6.
107 * - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
108 * identical for all miplevels in the texture.
109 * - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
110 * value, like width and height, varies with miplevel.
111 * - For other texture types, depth is 1.
112 * - Additionally, for UMS and CMS miptrees, depth is multiplied by
118 * \brief Is HiZ enabled for this level?
120 * If \c mt->level[l].has_hiz is set, then (1) \c mt->hiz_mt has been
121 * allocated and (2) the HiZ memory for the slices in this level reside at
122 * \c mt->hiz_mt->level[l].
127 * \brief List of 2D images in this mipmap level.
129 * This may be a list of cube faces, array slices in 2D array texture, or
130 * layers in a 3D texture. The list's length is \c depth.
132 struct intel_mipmap_slice
{
134 * \name Offset to slice
137 * Hardware formats are so diverse that that there is no unified way to
138 * compute the slice offsets, so we store them in this table.
140 * The (x, y) offset to slice \c s at level \c l relative the miptrees
143 * x = mt->level[l].slice[s].x_offset
144 * y = mt->level[l].slice[s].y_offset
151 * Mapping information. Persistent for the duration of
152 * intel_miptree_map/unmap on this slice.
154 struct intel_miptree_map
*map
;
159 * Enum for keeping track of the different MSAA layouts supported by Gen7.
161 enum intel_msaa_layout
164 * Ordinary surface with no MSAA.
166 INTEL_MSAA_LAYOUT_NONE
,
169 * Interleaved Multisample Surface. The additional samples are
170 * accommodated by scaling up the width and the height of the surface so
171 * that all the samples corresponding to a pixel are located at nearby
174 INTEL_MSAA_LAYOUT_IMS
,
177 * Uncompressed Multisample Surface. The surface is stored as a 2D array,
178 * with array slice n containing all pixel data for sample n.
180 INTEL_MSAA_LAYOUT_UMS
,
183 * Compressed Multisample Surface. The surface is stored as in
184 * INTEL_MSAA_LAYOUT_UMS, but there is an additional buffer called the MCS
185 * (Multisample Control Surface) buffer. Each pixel in the MCS buffer
186 * indicates the mapping from sample number to array slice. This allows
187 * the common case (where all samples constituting a pixel have the same
188 * color value) to be stored efficiently by just using a single array
191 INTEL_MSAA_LAYOUT_CMS
,
196 * Enum for keeping track of the fast clear state of a buffer associated with
199 * Fast clear works by deferring the memory writes that would be used to clear
200 * the buffer, so that instead of performing them at the time of the clear
201 * operation, the hardware automatically performs them at the time that the
202 * buffer is later accessed for rendering. The MCS buffer keeps track of
203 * which regions of the buffer still have pending clear writes.
205 * This enum keeps track of the driver's knowledge of pending fast clears in
208 * MCS buffers only exist on Gen7+.
210 enum intel_fast_clear_state
213 * There is no MCS buffer for this miptree, and one should never be
216 INTEL_FAST_CLEAR_STATE_NO_MCS
,
219 * No deferred clears are pending for this miptree, and the contents of the
220 * color buffer are entirely correct. An MCS buffer may or may not exist
221 * for this miptree. If it does exist, it is entirely in the "no deferred
222 * clears pending" state. If it does not exist, it will be created the
223 * first time a fast color clear is executed.
225 * In this state, the color buffer can be used for purposes other than
226 * rendering without needing a render target resolve.
228 * Since there is no such thing as a "fast color clear resolve" for MSAA
229 * buffers, an MSAA buffer will never be in this state.
231 INTEL_FAST_CLEAR_STATE_RESOLVED
,
234 * An MCS buffer exists for this miptree, and deferred clears are pending
235 * for some regions of the color buffer, as indicated by the MCS buffer.
236 * The contents of the color buffer are only correct for the regions where
237 * the MCS buffer doesn't indicate a deferred clear.
239 * If a single-sample buffer is in this state, a render target resolve must
240 * be performed before it can be used for purposes other than rendering.
242 INTEL_FAST_CLEAR_STATE_UNRESOLVED
,
245 * An MCS buffer exists for this miptree, and deferred clears are pending
246 * for the entire color buffer, and the contents of the MCS buffer reflect
247 * this. The contents of the color buffer are undefined.
249 * If a single-sample buffer is in this state, a render target resolve must
250 * be performed before it can be used for purposes other than rendering.
252 * If the client attempts to clear a buffer which is already in this state,
253 * the clear can be safely skipped, since the buffer is already clear.
255 INTEL_FAST_CLEAR_STATE_CLEAR
,
258 enum miptree_array_layout
{
259 /* Each array slice contains all miplevels packed together.
261 * Gen hardware usually wants multilevel miptrees configured this way.
263 * A 2D Array texture with 2 slices and multiple LODs using
264 * ALL_LOD_IN_EACH_SLICE would look somewhat like this:
281 ALL_LOD_IN_EACH_SLICE
,
283 /* Each LOD contains all slices of that LOD packed together.
285 * In some situations, Gen7+ hardware can use the array_spacing_lod0
286 * feature to save space when the surface only contains LOD 0.
288 * Gen6 uses this for separate stencil and hiz since gen6 does not support
289 * multiple LODs for separate stencil and hiz.
291 * A 2D Array texture with 2 slices and multiple LODs using
292 * ALL_SLICES_AT_EACH_LOD would look somewhat like this:
307 ALL_SLICES_AT_EACH_LOD
,
310 struct intel_mipmap_tree
312 /** Buffer object containing the pixel data. */
315 uint32_t pitch
; /**< pitch in bytes. */
317 uint32_t tiling
; /**< One of the I915_TILING_* flags */
319 /* Effectively the key:
324 * Generally, this is just the same as the gl_texture_image->TexFormat or
325 * gl_renderbuffer->Format.
327 * However, for textures and renderbuffers with packed depth/stencil formats
328 * on hardware where we want or need to use separate stencil, there will be
329 * two miptrees for storing the data. If the depthstencil texture or rb is
330 * MESA_FORMAT_Z32_FLOAT_S8X24_UINT, then mt->format will be
331 * MESA_FORMAT_Z_FLOAT32, otherwise for MESA_FORMAT_Z24_UNORM_S8_UINT objects it will be
332 * MESA_FORMAT_Z24_UNORM_X8_UINT.
334 * For ETC1/ETC2 textures, this is one of the uncompressed mesa texture
335 * formats if the hardware lacks support for ETC1/ETC2. See @ref etc_format.
339 /** This variable stores the value of ETC compressed texture format */
340 mesa_format etc_format
;
343 * The X offset of each image in the miptree must be aligned to this.
344 * See the comments in brw_tex_layout.c.
346 unsigned int align_w
;
347 unsigned int align_h
; /**< \see align_w */
353 * Level zero image dimensions. These dimensions correspond to the
354 * physical layout of data in memory. Accordingly, they account for the
355 * extra width, height, and or depth that must be allocated in order to
356 * accommodate multisample formats, and they account for the extra factor
357 * of 6 in depth that must be allocated in order to accommodate cubemap
360 GLuint physical_width0
, physical_height0
, physical_depth0
;
362 GLuint cpp
; /**< bytes per pixel */
367 * Level zero image dimensions. These dimensions correspond to the
368 * logical width, height, and depth of the texture as seen by client code.
369 * Accordingly, they do not account for the extra width, height, and/or
370 * depth that must be allocated in order to accommodate multisample
371 * formats, nor do they account for the extra factor of 6 in depth that
372 * must be allocated in order to accommodate cubemap textures.
374 uint32_t logical_width0
, logical_height0
, logical_depth0
;
377 * Indicates if we use the standard miptree layout (ALL_LOD_IN_EACH_SLICE),
378 * or if we tightly pack array slices at each LOD (ALL_SLICES_AT_EACH_LOD).
380 enum miptree_array_layout array_layout
;
383 * The distance in rows between array slices in an uncompressed surface.
385 * For compressed surfaces, slices are stored closer together physically;
386 * the real distance is (qpitch / block height).
391 * MSAA layout used by this buffer.
393 enum intel_msaa_layout msaa_layout
;
395 /* Derived from the above:
400 /* The 3DSTATE_CLEAR_PARAMS value associated with the last depth clear to
401 * this depth mipmap tree, if any.
403 uint32_t depth_clear_value
;
405 /* Includes image offset tables:
407 struct intel_mipmap_level level
[MAX_TEXTURE_LEVELS
];
409 /* Offset into bo where miptree starts:
416 * The hiz miptree contains the miptree's hiz buffer. To allocate the hiz
417 * miptree, use intel_miptree_alloc_hiz().
419 * To determine if hiz is enabled, do not check this pointer. Instead, use
420 * intel_miptree_slice_has_hiz().
422 struct intel_mipmap_tree
*hiz_mt
;
425 * \brief Map of miptree slices to needed resolves.
427 * This is used only when the miptree has a child HiZ miptree.
429 * Let \c mt be a depth miptree with HiZ enabled. Then the resolve map is
430 * \c mt->hiz_map. The resolve map of the child HiZ miptree, \c
431 * mt->hiz_mt->hiz_map, is unused.
433 struct exec_list hiz_map
; /* List of intel_resolve_map. */
436 * \brief Stencil miptree for depthstencil textures.
438 * This miptree is used for depthstencil textures and renderbuffers that
439 * require separate stencil. It always has the true copy of the stencil
440 * bits, regardless of mt->format.
442 * \see intel_miptree_map_depthstencil()
443 * \see intel_miptree_unmap_depthstencil()
445 struct intel_mipmap_tree
*stencil_mt
;
448 * \brief MCS miptree.
450 * This miptree contains the "multisample control surface", which stores
451 * the necessary information to implement compressed MSAA
452 * (INTEL_MSAA_FORMAT_CMS) and "fast color clear" behaviour on Gen7+.
454 * NULL if no MCS miptree is in use for this surface.
456 struct intel_mipmap_tree
*mcs_mt
;
459 * Fast clear state for this buffer.
461 enum intel_fast_clear_state fast_clear_state
;
464 * The SURFACE_STATE bits associated with the last fast color clear to this
465 * color mipmap tree, if any.
467 * This value will only ever contain ones in bits 28-31, so it is safe to
468 * OR into dword 7 of SURFACE_STATE.
470 uint32_t fast_clear_color_value
;
472 /* These are also refcounted:
477 enum intel_miptree_tiling_mode
{
478 INTEL_MIPTREE_TILING_ANY
,
479 INTEL_MIPTREE_TILING_Y
,
480 INTEL_MIPTREE_TILING_NONE
,
484 intel_is_non_msrt_mcs_buffer_supported(struct brw_context
*brw
,
485 struct intel_mipmap_tree
*mt
);
488 intel_get_non_msrt_mcs_alignment(struct brw_context
*brw
,
489 struct intel_mipmap_tree
*mt
,
490 unsigned *width_px
, unsigned *height
);
493 intel_miptree_alloc_non_msrt_mcs(struct brw_context
*brw
,
494 struct intel_mipmap_tree
*mt
);
496 struct intel_mipmap_tree
*intel_miptree_create(struct brw_context
*brw
,
504 bool expect_accelerated_upload
,
506 enum intel_miptree_tiling_mode
,
507 bool force_all_slices_at_each_lod
);
509 struct intel_mipmap_tree
*
510 intel_miptree_create_layout(struct brw_context
*brw
,
520 bool force_all_slices_at_each_lod
);
522 struct intel_mipmap_tree
*
523 intel_miptree_create_for_bo(struct brw_context
*brw
,
532 intel_update_winsys_renderbuffer_miptree(struct brw_context
*intel
,
533 struct intel_renderbuffer
*irb
,
535 uint32_t width
, uint32_t height
,
539 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
540 * The miptree has the following properties:
541 * - The target is GL_TEXTURE_2D.
542 * - There are no levels other than the base level 0.
545 struct intel_mipmap_tree
*
546 intel_miptree_create_for_renderbuffer(struct brw_context
*brw
,
550 uint32_t num_samples
);
553 intel_depth_format_for_depthstencil_format(mesa_format format
);
556 intel_lower_compressed_format(struct brw_context
*brw
, mesa_format format
);
558 /** \brief Assert that the level and layer are valid for the miptree. */
560 intel_miptree_check_level_layer(struct intel_mipmap_tree
*mt
,
564 assert(level
>= mt
->first_level
);
565 assert(level
<= mt
->last_level
);
566 assert(layer
< mt
->level
[level
].depth
);
569 void intel_miptree_reference(struct intel_mipmap_tree
**dst
,
570 struct intel_mipmap_tree
*src
);
572 void intel_miptree_release(struct intel_mipmap_tree
**mt
);
574 /* Check if an image fits an existing mipmap tree layout
576 bool intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
577 struct gl_texture_image
*image
);
580 intel_miptree_get_image_offset(const struct intel_mipmap_tree
*mt
,
581 GLuint level
, GLuint slice
,
582 GLuint
*x
, GLuint
*y
);
585 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
586 int *width
, int *height
, int *depth
);
589 intel_miptree_get_tile_masks(const struct intel_mipmap_tree
*mt
,
590 uint32_t *mask_x
, uint32_t *mask_y
,
591 bool map_stencil_as_y_tiled
);
594 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree
*mt
,
595 GLuint level
, GLuint slice
,
599 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree
*mt
,
600 uint32_t x
, uint32_t y
,
601 bool map_stencil_as_y_tiled
);
603 void intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
605 GLuint x
, GLuint y
, GLuint d
);
607 void intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
609 GLuint img
, GLuint x
, GLuint y
);
612 intel_miptree_copy_teximage(struct brw_context
*brw
,
613 struct intel_texture_image
*intelImage
,
614 struct intel_mipmap_tree
*dst_mt
, bool invalidate
);
617 intel_miptree_alloc_mcs(struct brw_context
*brw
,
618 struct intel_mipmap_tree
*mt
,
622 * \name Miptree HiZ functions
625 * It is safe to call the "slice_set_need_resolve" and "slice_resolve"
626 * functions on a miptree without HiZ. In that case, each function is a no-op.
630 * \brief Allocate the miptree's embedded HiZ miptree.
631 * \see intel_mipmap_tree:hiz_mt
632 * \return false if allocation failed
636 intel_miptree_alloc_hiz(struct brw_context
*brw
,
637 struct intel_mipmap_tree
*mt
);
640 intel_miptree_level_has_hiz(struct intel_mipmap_tree
*mt
, uint32_t level
);
643 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree
*mt
,
647 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree
*mt
,
652 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree
*mt
,
656 * \return false if no resolve was needed
659 intel_miptree_slice_resolve_hiz(struct brw_context
*brw
,
660 struct intel_mipmap_tree
*mt
,
665 * \return false if no resolve was needed
668 intel_miptree_slice_resolve_depth(struct brw_context
*brw
,
669 struct intel_mipmap_tree
*mt
,
674 * \return false if no resolve was needed
677 intel_miptree_all_slices_resolve_hiz(struct brw_context
*brw
,
678 struct intel_mipmap_tree
*mt
);
681 * \return false if no resolve was needed
684 intel_miptree_all_slices_resolve_depth(struct brw_context
*brw
,
685 struct intel_mipmap_tree
*mt
);
690 * Update the fast clear state for a miptree to indicate that it has been used
694 intel_miptree_used_for_rendering(struct intel_mipmap_tree
*mt
)
696 /* If the buffer was previously in fast clear state, change it to
697 * unresolved state, since it won't be guaranteed to be clear after
700 if (mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
)
701 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_UNRESOLVED
;
705 intel_miptree_resolve_color(struct brw_context
*brw
,
706 struct intel_mipmap_tree
*mt
);
709 intel_miptree_make_shareable(struct brw_context
*brw
,
710 struct intel_mipmap_tree
*mt
);
713 intel_miptree_updownsample(struct brw_context
*brw
,
714 struct intel_mipmap_tree
*src
,
715 struct intel_mipmap_tree
*dst
);
717 void brw_miptree_layout(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
);
719 void *intel_miptree_map_raw(struct brw_context
*brw
,
720 struct intel_mipmap_tree
*mt
);
722 void intel_miptree_unmap_raw(struct brw_context
*brw
,
723 struct intel_mipmap_tree
*mt
);
726 intel_miptree_map(struct brw_context
*brw
,
727 struct intel_mipmap_tree
*mt
,
739 intel_miptree_unmap(struct brw_context
*brw
,
740 struct intel_mipmap_tree
*mt
,
745 intel_hiz_exec(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
,
746 unsigned int level
, unsigned int layer
, enum gen6_hiz_op op
);