intel: Replace intel_mipmap_tree::hiz_region with a miptree [v2]
[mesa.git] / src / mesa / drivers / dri / intel / intel_mipmap_tree.h
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #ifndef INTEL_MIPMAP_TREE_H
29 #define INTEL_MIPMAP_TREE_H
30
31 #include <assert.h>
32
33 #include "intel_regions.h"
34
35 /* A layer on top of the intel_regions code which adds:
36 *
37 * - Code to size and layout a region to hold a set of mipmaps.
38 * - Query to determine if a new image fits in an existing tree.
39 * - More refcounting
40 * - maybe able to remove refcounting from intel_region?
41 * - ?
42 *
43 * The fixed mipmap layout of intel hardware where one offset
44 * specifies the position of all images in a mipmap hierachy
45 * complicates the implementation of GL texture image commands,
46 * compared to hardware where each image is specified with an
47 * independent offset.
48 *
49 * In an ideal world, each texture object would be associated with a
50 * single bufmgr buffer or 2d intel_region, and all the images within
51 * the texture object would slot into the tree as they arrive. The
52 * reality can be a little messier, as images can arrive from the user
53 * with sizes that don't fit in the existing tree, or in an order
54 * where the tree layout cannot be guessed immediately.
55 *
56 * This structure encodes an idealized mipmap tree. The GL image
57 * commands build these where possible, otherwise store the images in
58 * temporary system buffers.
59 */
60
61 struct intel_texture_image;
62
63 /**
64 * Describes the location of each texture image within a texture region.
65 */
66 struct intel_mipmap_level
67 {
68 /** Offset to this miptree level, used in computing x_offset. */
69 GLuint level_x;
70 /** Offset to this miptree level, used in computing y_offset. */
71 GLuint level_y;
72 GLuint width;
73 GLuint height;
74
75 /**
76 * \brief Number of 2D slices in this miplevel.
77 *
78 * The exact semantics of depth varies according to the texture target:
79 * - For GL_TEXTURE_CUBE_MAP, depth is 6.
80 * - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
81 * identical for all miplevels in the texture.
82 * - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
83 * value, like width and height, varies with miplevel.
84 * - For other texture types, depth is 1.
85 */
86 GLuint depth;
87
88 /**
89 * \brief List of 2D images in this mipmap level.
90 *
91 * This may be a list of cube faces, array slices in 2D array texture, or
92 * layers in a 3D texture. The list's length is \c depth.
93 */
94 struct intel_mipmap_slice {
95 /**
96 * \name Offset to slice
97 * \{
98 *
99 * Hardware formats are so diverse that that there is no unified way to
100 * compute the slice offsets, so we store them in this table.
101 *
102 * The (x, y) offset to slice \c s at level \c l relative the miptrees
103 * base address is
104 * \code
105 * x = mt->level[l].slice[s].x_offset
106 * y = mt->level[l].slice[s].y_offset
107 */
108 GLuint x_offset;
109 GLuint y_offset;
110 /** \} */
111 } *slice;
112 };
113
114 struct intel_mipmap_tree
115 {
116 /* Effectively the key:
117 */
118 GLenum target;
119 gl_format format;
120
121 GLuint first_level;
122 GLuint last_level;
123
124 GLuint width0, height0, depth0; /**< Level zero image dimensions */
125 GLuint cpp;
126 bool compressed;
127
128 /* Derived from the above:
129 */
130 GLuint total_width;
131 GLuint total_height;
132
133 /* Includes image offset tables:
134 */
135 struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
136
137 /* The data is held here:
138 */
139 struct intel_region *region;
140
141 /**
142 * \brief HiZ miptree
143 *
144 * This is non-null only if HiZ is enabled for this miptree.
145 *
146 * \see intel_miptree_alloc_hiz()
147 */
148 struct intel_mipmap_tree *hiz_mt;
149
150 /**
151 * \brief Stencil miptree for depthstencil textures.
152 *
153 * This miptree is used for depthstencil textures that require separate
154 * stencil. The stencil miptree's data is the golden copy of the
155 * parent miptree's stencil bits. When necessary, we scatter/gather the
156 * stencil bits between the parent miptree and the stencil miptree.
157 *
158 * \see intel_miptree_s8z24_scatter()
159 * \see intel_miptree_s8z24_gather()
160 */
161 struct intel_mipmap_tree *stencil_mt;
162
163 /* These are also refcounted:
164 */
165 GLuint refcount;
166 };
167
168
169
170 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
171 GLenum target,
172 gl_format format,
173 GLuint first_level,
174 GLuint last_level,
175 GLuint width0,
176 GLuint height0,
177 GLuint depth0,
178 bool expect_accelerated_upload);
179
180 struct intel_mipmap_tree *
181 intel_miptree_create_for_region(struct intel_context *intel,
182 GLenum target,
183 gl_format format,
184 struct intel_region *region);
185
186 /**
187 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
188 * The miptree has the following properties:
189 * - The target is GL_TEXTURE_2D.
190 * - There are no levels other than the base level 0.
191 * - Depth is 1.
192 */
193 struct intel_mipmap_tree*
194 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
195 gl_format format,
196 uint32_t tiling,
197 uint32_t cpp,
198 uint32_t width,
199 uint32_t height);
200
201 /** \brief Assert that the level and layer are valid for the miptree. */
202 static inline void
203 intel_miptree_check_level_layer(struct intel_mipmap_tree *mt,
204 uint32_t level,
205 uint32_t layer)
206 {
207 assert(level >= mt->first_level);
208 assert(level <= mt->last_level);
209 assert(layer < mt->level[level].depth);
210 }
211
212 int intel_miptree_pitch_align (struct intel_context *intel,
213 struct intel_mipmap_tree *mt,
214 uint32_t tiling,
215 int pitch);
216
217 void intel_miptree_reference(struct intel_mipmap_tree **dst,
218 struct intel_mipmap_tree *src);
219
220 void intel_miptree_release(struct intel_mipmap_tree **mt);
221
222 /* Check if an image fits an existing mipmap tree layout
223 */
224 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
225 struct gl_texture_image *image);
226
227 void
228 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
229 GLuint level, GLuint face, GLuint depth,
230 GLuint *x, GLuint *y);
231
232 void
233 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
234 int *width, int *height, int *depth);
235
236 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
237 GLuint level,
238 GLuint x, GLuint y,
239 GLuint w, GLuint h, GLuint d);
240
241 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
242 GLuint level,
243 GLuint img, GLuint x, GLuint y);
244
245 void
246 intel_miptree_copy_teximage(struct intel_context *intel,
247 struct intel_texture_image *intelImage,
248 struct intel_mipmap_tree *dst_mt);
249
250 /**
251 * Copy the stencil data from \c mt->stencil_mt->region to \c mt->region for
252 * the given miptree slice.
253 *
254 * \see intel_mipmap_tree::stencil_mt
255 */
256 void
257 intel_miptree_s8z24_scatter(struct intel_context *intel,
258 struct intel_mipmap_tree *mt,
259 uint32_t level,
260 uint32_t slice);
261
262 /**
263 * Copy the stencil data in \c mt->stencil_mt->region to \c mt->region for the
264 * given miptree slice.
265 *
266 * \see intel_mipmap_tree::stencil_mt
267 */
268 void
269 intel_miptree_s8z24_gather(struct intel_context *intel,
270 struct intel_mipmap_tree *mt,
271 uint32_t level,
272 uint32_t layer);
273
274 /**
275 * \brief Allocate the miptree's embedded HiZ miptree.
276 * \see intel_mipmap_tree:hiz_mt
277 * \return false if allocation failed
278 */
279 bool
280 intel_miptree_alloc_hiz(struct intel_context *intel,
281 struct intel_mipmap_tree *mt);
282
283 /* i915_mipmap_tree.c:
284 */
285 void i915_miptree_layout(struct intel_mipmap_tree *mt);
286 void i945_miptree_layout(struct intel_mipmap_tree *mt);
287 void brw_miptree_layout(struct intel_context *intel,
288 struct intel_mipmap_tree *mt);
289
290 #endif