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 #ifndef INTEL_MIPMAP_TREE_H
29 #define INTEL_MIPMAP_TREE_H
33 #include "intel_regions.h"
34 #include "intel_resolve_map.h"
40 /* A layer on top of the intel_regions code which adds:
42 * - Code to size and layout a region to hold a set of mipmaps.
43 * - Query to determine if a new image fits in an existing tree.
45 * - maybe able to remove refcounting from intel_region?
48 * The fixed mipmap layout of intel hardware where one offset
49 * specifies the position of all images in a mipmap hierachy
50 * complicates the implementation of GL texture image commands,
51 * compared to hardware where each image is specified with an
54 * In an ideal world, each texture object would be associated with a
55 * single bufmgr buffer or 2d intel_region, and all the images within
56 * the texture object would slot into the tree as they arrive. The
57 * reality can be a little messier, as images can arrive from the user
58 * with sizes that don't fit in the existing tree, or in an order
59 * where the tree layout cannot be guessed immediately.
61 * This structure encodes an idealized mipmap tree. The GL image
62 * commands build these where possible, otherwise store the images in
63 * temporary system buffers.
66 struct intel_resolve_map
;
67 struct intel_texture_image
;
69 struct intel_miptree_map
{
70 /** Bitfield of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_BIT */
72 /** Region of interest for the map. */
74 /** Possibly malloced temporary buffer for the mapping. */
76 /** Possible pointer to a BO temporary for the mapping. */
78 /** Pointer to the start of (map_x, map_y) returned by the mapping. */
80 /** Stride of the mapping. */
84 * intel_mipmap_tree::singlesample_mt is temporary storage that persists
85 * only for the duration of the map.
87 bool singlesample_mt_is_tmp
;
91 * Describes the location of each texture image within a texture region.
93 struct intel_mipmap_level
95 /** Offset to this miptree level, used in computing x_offset. */
97 /** 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.
116 * \brief List of 2D images in this mipmap level.
118 * This may be a list of cube faces, array slices in 2D array texture, or
119 * layers in a 3D texture. The list's length is \c depth.
121 struct intel_mipmap_slice
{
123 * \name Offset to slice
126 * Hardware formats are so diverse that that there is no unified way to
127 * compute the slice offsets, so we store them in this table.
129 * The (x, y) offset to slice \c s at level \c l relative the miptrees
132 * x = mt->level[l].slice[s].x_offset
133 * y = mt->level[l].slice[s].y_offset
140 * Mapping information. Persistent for the duration of
141 * intel_miptree_map/unmap on this slice.
143 struct intel_miptree_map
*map
;
148 * Enum for keeping track of the different MSAA layouts supported by Gen7.
150 enum intel_msaa_layout
153 * Ordinary surface with no MSAA.
155 INTEL_MSAA_LAYOUT_NONE
,
158 * Interleaved Multisample Surface. The additional samples are
159 * accommodated by scaling up the width and the height of the surface so
160 * that all the samples corresponding to a pixel are located at nearby
163 INTEL_MSAA_LAYOUT_IMS
,
166 * Uncompressed Multisample Surface. The surface is stored as a 2D array,
167 * with array slice n containing all pixel data for sample n.
169 INTEL_MSAA_LAYOUT_UMS
,
172 * Compressed Multisample Surface. The surface is stored as in
173 * INTEL_MSAA_LAYOUT_UMS, but there is an additional buffer called the MCS
174 * (Multisample Control Surface) buffer. Each pixel in the MCS buffer
175 * indicates the mapping from sample number to array slice. This allows
176 * the common case (where all samples constituting a pixel have the same
177 * color value) to be stored efficiently by just using a single array
180 INTEL_MSAA_LAYOUT_CMS
,
183 struct intel_mipmap_tree
185 /* Effectively the key:
190 * Generally, this is just the same as the gl_texture_image->TexFormat or
191 * gl_renderbuffer->Format.
193 * However, for textures and renderbuffers with packed depth/stencil formats
194 * on hardware where we want or need to use separate stencil, there will be
195 * two miptrees for storing the data. If the depthstencil texture or rb is
196 * MESA_FORMAT_Z32_FLOAT_X24S8, then mt->format will be
197 * MESA_FORMAT_Z32_FLOAT, otherwise for MESA_FORMAT_S8_Z24 objects it will be
198 * MESA_FORMAT_X8_Z24.
200 * For ETC1 textures, this is MESA_FORMAT_RGBX8888_REV if the hardware
201 * lacks support for ETC1. See @ref wraps_etc1.
206 * The X offset of each image in the miptree must be aligned to this. See
207 * the "Alignment Unit Size" section of the BSpec.
209 unsigned int align_w
;
210 unsigned int align_h
; /**< \see align_w */
215 GLuint width0
, height0
, depth0
; /**< Level zero image dimensions */
221 * If num_samples > 0, then singlesample_width0 is the value that width0
222 * would have if instead a singlesample miptree were created. Note that,
223 * for non-interleaved msaa layouts, the two values are the same.
225 * If num_samples == 0, then singlesample_width0 is undefined.
227 uint32_t singlesample_width0
;
229 /** \see singlesample_width0 */
230 uint32_t singlesample_height0
;
233 * For 1D array, 2D array, cube, and 2D multisampled surfaces on Gen7: true
234 * if the surface only contains LOD 0, and hence no space is for LOD's
235 * other than 0 in between array slices.
237 * Corresponds to the surface_array_spacing bit in gen7_surface_state.
239 bool array_spacing_lod0
;
242 * MSAA layout used by this buffer.
244 enum intel_msaa_layout msaa_layout
;
246 /* Derived from the above:
251 /* The 3DSTATE_CLEAR_PARAMS value associated with the last depth clear to
252 * this depth mipmap tree, if any.
254 uint32_t depth_clear_value
;
256 /* Includes image offset tables:
258 struct intel_mipmap_level level
[MAX_TEXTURE_LEVELS
];
260 /* The data is held here:
262 struct intel_region
*region
;
264 /* Offset into region bo where miptree starts:
269 * \brief Singlesample miptree.
271 * This is used under two cases.
273 * --- Case 1: As persistent singlesample storage for multisample window
274 * system front and back buffers ---
276 * Suppose that the window system FBO was created with a multisample
277 * config. Let `back_irb` be the `intel_renderbuffer` for the FBO's back
278 * buffer. Then `back_irb` contains two miptrees: a parent multisample
279 * miptree (back_irb->mt) and a child singlesample miptree
280 * (back_irb->mt->singlesample_mt). The DRM buffer shared with DRI2
281 * belongs to `back_irb->mt->singlesample_mt` and contains singlesample
282 * data. The singlesample miptree is created at the same time as and
283 * persists for the lifetime of its parent multisample miptree.
285 * When access to the singlesample data is needed, such as at
286 * eglSwapBuffers and glReadPixels, an automatic downsample occurs from
287 * `back_rb->mt` to `back_rb->mt->singlesample_mt` when necessary.
289 * This description of the back buffer applies analogously to the front
293 * --- Case 2: As temporary singlesample storage for mapping multisample
296 * Suppose the intel_miptree_map is called on a multisample miptree, `mt`,
297 * for which case 1 does not apply (that is, `mt` does not belong to
298 * a front or back buffer). Then `mt->singlesample_mt` is null at the
299 * start of the call. intel_miptree_map will create a temporary
300 * singlesample miptree, store it at `mt->singlesample_mt`, downsample from
301 * `mt` to `mt->singlesample_mt` if necessary, then map
302 * `mt->singlesample_mt`. The temporary miptree is later deleted during
303 * intel_miptree_unmap.
305 struct intel_mipmap_tree
*singlesample_mt
;
308 * \brief A downsample is needed from this miptree to singlesample_mt.
310 bool need_downsample
;
315 * This is non-null only if HiZ is enabled for this miptree.
317 * \see intel_miptree_alloc_hiz()
319 struct intel_mipmap_tree
*hiz_mt
;
322 * \brief Map of miptree slices to needed resolves.
324 * This is used only when the miptree has a child HiZ miptree.
326 * Let \c mt be a depth miptree with HiZ enabled. Then the resolve map is
327 * \c mt->hiz_map. The resolve map of the child HiZ miptree, \c
328 * mt->hiz_mt->hiz_map, is unused.
330 struct intel_resolve_map hiz_map
;
333 * \brief Stencil miptree for depthstencil textures.
335 * This miptree is used for depthstencil textures and renderbuffers that
336 * require separate stencil. It always has the true copy of the stencil
337 * bits, regardless of mt->format.
339 * \see intel_miptree_map_depthstencil()
340 * \see intel_miptree_unmap_depthstencil()
342 struct intel_mipmap_tree
*stencil_mt
;
345 * \brief MCS miptree for multisampled textures.
347 * This miptree contains the "multisample control surface", which stores
348 * the necessary information to implement compressed MSAA on Gen7+
349 * (INTEL_MSAA_FORMAT_CMS).
351 struct intel_mipmap_tree
*mcs_mt
;
354 * \brief The miptree contains RGBX data that was originally ETC1 data.
356 * On hardware that lacks support for ETC1 textures, we do the
357 * following on calls to glCompressedTexImage2D(GL_ETC1_RGB8_OES):
358 * 1. Create a miptree whose format is MESA_FORMAT_RGBX8888_REV with
359 * the wraps_etc1 flag set.
360 * 2. Translate the ETC1 data into RGBX.
361 * 3. Store the RGBX data into the miptree and discard the ETC1 data.
365 /* These are also refcounted:
372 struct intel_mipmap_tree
*intel_miptree_create(struct intel_context
*intel
,
380 bool expect_accelerated_upload
,
382 enum intel_msaa_layout msaa_layout
);
384 struct intel_mipmap_tree
*
385 intel_miptree_create_for_region(struct intel_context
*intel
,
388 struct intel_region
*region
);
390 struct intel_mipmap_tree
*
391 intel_miptree_create_for_dri2_buffer(struct intel_context
*intel
,
392 unsigned dri_attachment
,
394 uint32_t num_samples
,
395 struct intel_region
*region
);
398 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
399 * The miptree has the following properties:
400 * - The target is GL_TEXTURE_2D.
401 * - There are no levels other than the base level 0.
404 struct intel_mipmap_tree
*
405 intel_miptree_create_for_renderbuffer(struct intel_context
*intel
,
409 uint32_t num_samples
);
411 /** \brief Assert that the level and layer are valid for the miptree. */
413 intel_miptree_check_level_layer(struct intel_mipmap_tree
*mt
,
417 assert(level
>= mt
->first_level
);
418 assert(level
<= mt
->last_level
);
419 assert(layer
< mt
->level
[level
].depth
);
422 int intel_miptree_pitch_align (struct intel_context
*intel
,
423 struct intel_mipmap_tree
*mt
,
427 void intel_miptree_reference(struct intel_mipmap_tree
**dst
,
428 struct intel_mipmap_tree
*src
);
430 void intel_miptree_release(struct intel_mipmap_tree
**mt
);
432 /* Check if an image fits an existing mipmap tree layout
434 bool intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
435 struct gl_texture_image
*image
);
438 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
439 GLuint level
, GLuint slice
,
440 GLuint
*x
, GLuint
*y
);
443 intel_miptree_get_dimensions_for_image(struct gl_texture_image
*image
,
444 int *width
, int *height
, int *depth
);
446 void intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
449 GLuint w
, GLuint h
, GLuint d
);
451 void intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
453 GLuint img
, GLuint x
, GLuint y
);
456 intel_miptree_copy_teximage(struct intel_context
*intel
,
457 struct intel_texture_image
*intelImage
,
458 struct intel_mipmap_tree
*dst_mt
);
461 * Copy the stencil data from \c mt->stencil_mt->region to \c mt->region for
462 * the given miptree slice.
464 * \see intel_mipmap_tree::stencil_mt
467 intel_miptree_s8z24_scatter(struct intel_context
*intel
,
468 struct intel_mipmap_tree
*mt
,
473 * Copy the stencil data in \c mt->stencil_mt->region to \c mt->region for the
474 * given miptree slice.
476 * \see intel_mipmap_tree::stencil_mt
479 intel_miptree_s8z24_gather(struct intel_context
*intel
,
480 struct intel_mipmap_tree
*mt
,
485 intel_miptree_alloc_mcs(struct intel_context
*intel
,
486 struct intel_mipmap_tree
*mt
,
490 * \name Miptree HiZ functions
493 * It is safe to call the "slice_set_need_resolve" and "slice_resolve"
494 * functions on a miptree without HiZ. In that case, each function is a no-op.
498 * \brief Allocate the miptree's embedded HiZ miptree.
499 * \see intel_mipmap_tree:hiz_mt
500 * \return false if allocation failed
504 intel_miptree_alloc_hiz(struct intel_context
*intel
,
505 struct intel_mipmap_tree
*mt
,
509 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree
*mt
,
513 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree
*mt
,
517 intel_miptree_all_slices_set_need_hiz_resolve(struct intel_mipmap_tree
*mt
);
520 intel_miptree_all_slices_set_need_depth_resolve(struct intel_mipmap_tree
*mt
);
523 * \return false if no resolve was needed
526 intel_miptree_slice_resolve_hiz(struct intel_context
*intel
,
527 struct intel_mipmap_tree
*mt
,
532 * \return false if no resolve was needed
535 intel_miptree_slice_resolve_depth(struct intel_context
*intel
,
536 struct intel_mipmap_tree
*mt
,
541 * \return false if no resolve was needed
544 intel_miptree_all_slices_resolve_hiz(struct intel_context
*intel
,
545 struct intel_mipmap_tree
*mt
);
548 * \return false if no resolve was needed
551 intel_miptree_all_slices_resolve_depth(struct intel_context
*intel
,
552 struct intel_mipmap_tree
*mt
);
557 intel_miptree_downsample(struct intel_context
*intel
,
558 struct intel_mipmap_tree
*mt
);
561 intel_miptree_upsample(struct intel_context
*intel
,
562 struct intel_mipmap_tree
*mt
);
564 /* i915_mipmap_tree.c:
566 void i915_miptree_layout(struct intel_mipmap_tree
*mt
);
567 void i945_miptree_layout(struct intel_mipmap_tree
*mt
);
568 void brw_miptree_layout(struct intel_context
*intel
,
569 struct intel_mipmap_tree
*mt
);
572 intel_miptree_map(struct intel_context
*intel
,
573 struct intel_mipmap_tree
*mt
,
585 intel_miptree_unmap(struct intel_context
*intel
,
586 struct intel_mipmap_tree
*mt
,
592 intel_hiz_exec(struct intel_context
*intel
, struct intel_mipmap_tree
*mt
,
593 unsigned int level
, unsigned int layer
, enum gen6_hiz_op op
)
595 /* Stub on i915. It would be nice if we didn't execute resolve code at all
601 intel_hiz_exec(struct intel_context
*intel
, struct intel_mipmap_tree
*mt
,
602 unsigned int level
, unsigned int layer
, enum gen6_hiz_op op
);