intel: Add resolve functions for miptrees
[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 #include "intel_resolve_map.h"
35
36 /* A layer on top of the intel_regions code which adds:
37 *
38 * - Code to size and layout a region to hold a set of mipmaps.
39 * - Query to determine if a new image fits in an existing tree.
40 * - More refcounting
41 * - maybe able to remove refcounting from intel_region?
42 * - ?
43 *
44 * The fixed mipmap layout of intel hardware where one offset
45 * specifies the position of all images in a mipmap hierachy
46 * complicates the implementation of GL texture image commands,
47 * compared to hardware where each image is specified with an
48 * independent offset.
49 *
50 * In an ideal world, each texture object would be associated with a
51 * single bufmgr buffer or 2d intel_region, and all the images within
52 * the texture object would slot into the tree as they arrive. The
53 * reality can be a little messier, as images can arrive from the user
54 * with sizes that don't fit in the existing tree, or in an order
55 * where the tree layout cannot be guessed immediately.
56 *
57 * This structure encodes an idealized mipmap tree. The GL image
58 * commands build these where possible, otherwise store the images in
59 * temporary system buffers.
60 */
61
62 struct intel_resolve_map;
63 struct intel_texture_image;
64
65 /**
66 * Describes the location of each texture image within a texture region.
67 */
68 struct intel_mipmap_level
69 {
70 /** Offset to this miptree level, used in computing x_offset. */
71 GLuint level_x;
72 /** Offset to this miptree level, used in computing y_offset. */
73 GLuint level_y;
74 GLuint width;
75 GLuint height;
76
77 /**
78 * \brief Number of 2D slices in this miplevel.
79 *
80 * The exact semantics of depth varies according to the texture target:
81 * - For GL_TEXTURE_CUBE_MAP, depth is 6.
82 * - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
83 * identical for all miplevels in the texture.
84 * - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
85 * value, like width and height, varies with miplevel.
86 * - For other texture types, depth is 1.
87 */
88 GLuint depth;
89
90 /**
91 * \brief List of 2D images in this mipmap level.
92 *
93 * This may be a list of cube faces, array slices in 2D array texture, or
94 * layers in a 3D texture. The list's length is \c depth.
95 */
96 struct intel_mipmap_slice {
97 /**
98 * \name Offset to slice
99 * \{
100 *
101 * Hardware formats are so diverse that that there is no unified way to
102 * compute the slice offsets, so we store them in this table.
103 *
104 * The (x, y) offset to slice \c s at level \c l relative the miptrees
105 * base address is
106 * \code
107 * x = mt->level[l].slice[s].x_offset
108 * y = mt->level[l].slice[s].y_offset
109 */
110 GLuint x_offset;
111 GLuint y_offset;
112 /** \} */
113 } *slice;
114 };
115
116 struct intel_mipmap_tree
117 {
118 /* Effectively the key:
119 */
120 GLenum target;
121 gl_format format;
122
123 GLuint first_level;
124 GLuint last_level;
125
126 GLuint width0, height0, depth0; /**< Level zero image dimensions */
127 GLuint cpp;
128 bool compressed;
129
130 /* Derived from the above:
131 */
132 GLuint total_width;
133 GLuint total_height;
134
135 /* Includes image offset tables:
136 */
137 struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
138
139 /* The data is held here:
140 */
141 struct intel_region *region;
142
143 /**
144 * \brief HiZ miptree
145 *
146 * This is non-null only if HiZ is enabled for this miptree.
147 *
148 * \see intel_miptree_alloc_hiz()
149 */
150 struct intel_mipmap_tree *hiz_mt;
151
152 /**
153 * \brief Map of miptree slices to needed resolves.
154 *
155 * This is used only when the miptree has a child HiZ miptree.
156 *
157 * Let \c mt be a depth miptree with HiZ enabled. Then the resolve map is
158 * \c mt->hiz_map. The resolve map of the child HiZ miptree, \c
159 * mt->hiz_mt->hiz_map, is unused.
160 */
161 struct intel_resolve_map hiz_map;
162
163 /**
164 * \brief Stencil miptree for depthstencil textures.
165 *
166 * This miptree is used for depthstencil textures that require separate
167 * stencil. The stencil miptree's data is the golden copy of the
168 * parent miptree's stencil bits. When necessary, we scatter/gather the
169 * stencil bits between the parent miptree and the stencil miptree.
170 *
171 * \see intel_miptree_s8z24_scatter()
172 * \see intel_miptree_s8z24_gather()
173 */
174 struct intel_mipmap_tree *stencil_mt;
175
176 /* These are also refcounted:
177 */
178 GLuint refcount;
179 };
180
181
182
183 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
184 GLenum target,
185 gl_format format,
186 GLuint first_level,
187 GLuint last_level,
188 GLuint width0,
189 GLuint height0,
190 GLuint depth0,
191 bool expect_accelerated_upload);
192
193 struct intel_mipmap_tree *
194 intel_miptree_create_for_region(struct intel_context *intel,
195 GLenum target,
196 gl_format format,
197 struct intel_region *region);
198
199 /**
200 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
201 * The miptree has the following properties:
202 * - The target is GL_TEXTURE_2D.
203 * - There are no levels other than the base level 0.
204 * - Depth is 1.
205 */
206 struct intel_mipmap_tree*
207 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
208 gl_format format,
209 uint32_t tiling,
210 uint32_t cpp,
211 uint32_t width,
212 uint32_t height);
213
214 /** \brief Assert that the level and layer are valid for the miptree. */
215 static inline void
216 intel_miptree_check_level_layer(struct intel_mipmap_tree *mt,
217 uint32_t level,
218 uint32_t layer)
219 {
220 assert(level >= mt->first_level);
221 assert(level <= mt->last_level);
222 assert(layer < mt->level[level].depth);
223 }
224
225 int intel_miptree_pitch_align (struct intel_context *intel,
226 struct intel_mipmap_tree *mt,
227 uint32_t tiling,
228 int pitch);
229
230 void intel_miptree_reference(struct intel_mipmap_tree **dst,
231 struct intel_mipmap_tree *src);
232
233 void intel_miptree_release(struct intel_mipmap_tree **mt);
234
235 /* Check if an image fits an existing mipmap tree layout
236 */
237 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
238 struct gl_texture_image *image);
239
240 void
241 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
242 GLuint level, GLuint face, GLuint depth,
243 GLuint *x, GLuint *y);
244
245 void
246 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
247 int *width, int *height, int *depth);
248
249 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
250 GLuint level,
251 GLuint x, GLuint y,
252 GLuint w, GLuint h, GLuint d);
253
254 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
255 GLuint level,
256 GLuint img, GLuint x, GLuint y);
257
258 void
259 intel_miptree_copy_teximage(struct intel_context *intel,
260 struct intel_texture_image *intelImage,
261 struct intel_mipmap_tree *dst_mt);
262
263 /**
264 * Copy the stencil data from \c mt->stencil_mt->region to \c mt->region for
265 * the given miptree slice.
266 *
267 * \see intel_mipmap_tree::stencil_mt
268 */
269 void
270 intel_miptree_s8z24_scatter(struct intel_context *intel,
271 struct intel_mipmap_tree *mt,
272 uint32_t level,
273 uint32_t slice);
274
275 /**
276 * Copy the stencil data in \c mt->stencil_mt->region to \c mt->region for the
277 * given miptree slice.
278 *
279 * \see intel_mipmap_tree::stencil_mt
280 */
281 void
282 intel_miptree_s8z24_gather(struct intel_context *intel,
283 struct intel_mipmap_tree *mt,
284 uint32_t level,
285 uint32_t layer);
286
287 /**
288 * \name Miptree HiZ functions
289 * \{
290 *
291 * It is safe to call the "slice_set_need_resolve" and "slice_resolve"
292 * functions on a miptree without HiZ. In that case, each function is a no-op.
293 */
294
295 /**
296 * \brief Allocate the miptree's embedded HiZ miptree.
297 * \see intel_mipmap_tree:hiz_mt
298 * \return false if allocation failed
299 */
300
301 bool
302 intel_miptree_alloc_hiz(struct intel_context *intel,
303 struct intel_mipmap_tree *mt);
304
305 void
306 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
307 uint32_t level,
308 uint32_t depth);
309 void
310 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
311 uint32_t level,
312 uint32_t depth);
313 void
314 intel_miptree_all_slices_set_need_hiz_resolve(struct intel_mipmap_tree *mt);
315
316 void
317 intel_miptree_all_slices_set_need_depth_resolve(struct intel_mipmap_tree *mt);
318
319 /**
320 * \return false if no resolve was needed
321 */
322 bool
323 intel_miptree_slice_resolve_hiz(struct intel_context *intel,
324 struct intel_mipmap_tree *mt,
325 unsigned int level,
326 unsigned int depth);
327
328 /**
329 * \return false if no resolve was needed
330 */
331 bool
332 intel_miptree_slice_resolve_depth(struct intel_context *intel,
333 struct intel_mipmap_tree *mt,
334 unsigned int level,
335 unsigned int depth);
336
337 /**
338 * \return false if no resolve was needed
339 */
340 bool
341 intel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
342 struct intel_mipmap_tree *mt);
343
344 /**
345 * \return false if no resolve was needed
346 */
347 bool
348 intel_miptree_all_slices_resolve_depth(struct intel_context *intel,
349 struct intel_mipmap_tree *mt);
350
351 /**\}*/
352
353 /* i915_mipmap_tree.c:
354 */
355 void i915_miptree_layout(struct intel_mipmap_tree *mt);
356 void i945_miptree_layout(struct intel_mipmap_tree *mt);
357 void brw_miptree_layout(struct intel_context *intel,
358 struct intel_mipmap_tree *mt);
359
360 #endif