i965/gen6+: Add support for fast depth clears.
[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 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /* A layer on top of the intel_regions code which adds:
41 *
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.
44 * - More refcounting
45 * - maybe able to remove refcounting from intel_region?
46 * - ?
47 *
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
52 * independent offset.
53 *
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.
60 *
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.
64 */
65
66 struct intel_resolve_map;
67 struct intel_texture_image;
68
69 struct intel_miptree_map {
70 /** Bitfield of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_BIT */
71 GLbitfield mode;
72 /** Region of interest for the map. */
73 int x, y, w, h;
74 /** Possibly malloced temporary buffer for the mapping. */
75 void *buffer;
76 /** Possible pointer to a BO temporary for the mapping. */
77 drm_intel_bo *bo;
78 /** Pointer to the start of (map_x, map_y) returned by the mapping. */
79 void *ptr;
80 /** Stride of the mapping. */
81 int stride;
82 };
83
84 /**
85 * Describes the location of each texture image within a texture region.
86 */
87 struct intel_mipmap_level
88 {
89 /** Offset to this miptree level, used in computing x_offset. */
90 GLuint level_x;
91 /** Offset to this miptree level, used in computing y_offset. */
92 GLuint level_y;
93 GLuint width;
94 GLuint height;
95
96 /**
97 * \brief Number of 2D slices in this miplevel.
98 *
99 * The exact semantics of depth varies according to the texture target:
100 * - For GL_TEXTURE_CUBE_MAP, depth is 6.
101 * - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
102 * identical for all miplevels in the texture.
103 * - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
104 * value, like width and height, varies with miplevel.
105 * - For other texture types, depth is 1.
106 */
107 GLuint depth;
108
109 /**
110 * \brief List of 2D images in this mipmap level.
111 *
112 * This may be a list of cube faces, array slices in 2D array texture, or
113 * layers in a 3D texture. The list's length is \c depth.
114 */
115 struct intel_mipmap_slice {
116 /**
117 * \name Offset to slice
118 * \{
119 *
120 * Hardware formats are so diverse that that there is no unified way to
121 * compute the slice offsets, so we store them in this table.
122 *
123 * The (x, y) offset to slice \c s at level \c l relative the miptrees
124 * base address is
125 * \code
126 * x = mt->level[l].slice[s].x_offset
127 * y = mt->level[l].slice[s].y_offset
128 */
129 GLuint x_offset;
130 GLuint y_offset;
131 /** \} */
132
133 /**
134 * Pointer to mapping information, present across
135 * intel_tex_image_map()/unmap of the slice.
136 */
137 struct intel_miptree_map *map;
138 } *slice;
139 };
140
141 struct intel_mipmap_tree
142 {
143 /* Effectively the key:
144 */
145 GLenum target;
146
147 /**
148 * Generally, this is just the same as the gl_texture_image->TexFormat or
149 * gl_renderbuffer->Format.
150 *
151 * However, for textures and renderbuffers with packed depth/stencil formats
152 * on hardware where we want or need to use separate stencil, there will be
153 * two miptrees for storing the data. If the depthstencil texture or rb is
154 * MESA_FORMAT_Z32_FLOAT_X24S8, then mt->format will be
155 * MESA_FORMAT_Z32_FLOAT, otherwise for MESA_FORMAT_S8_Z24 objects it will be
156 * MESA_FORMAT_X8_Z24.
157 */
158 gl_format format;
159
160 /**
161 * The X offset of each image in the miptree must be aligned to this. See
162 * the "Alignment Unit Size" section of the BSpec.
163 */
164 unsigned int align_w;
165 unsigned int align_h; /**< \see align_w */
166
167 GLuint first_level;
168 GLuint last_level;
169
170 GLuint width0, height0, depth0; /**< Level zero image dimensions */
171 GLuint cpp;
172 GLuint num_samples;
173 bool compressed;
174
175 /* Derived from the above:
176 */
177 GLuint total_width;
178 GLuint total_height;
179
180 /* The 3DSTATE_CLEAR_PARAMS value associated with the last depth clear to
181 * this depth mipmap tree, if any.
182 */
183 uint32_t depth_clear_value;
184
185 /* Includes image offset tables:
186 */
187 struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
188
189 /* The data is held here:
190 */
191 struct intel_region *region;
192
193 /**
194 * \brief HiZ miptree
195 *
196 * This is non-null only if HiZ is enabled for this miptree.
197 *
198 * \see intel_miptree_alloc_hiz()
199 */
200 struct intel_mipmap_tree *hiz_mt;
201
202 /**
203 * \brief Map of miptree slices to needed resolves.
204 *
205 * This is used only when the miptree has a child HiZ miptree.
206 *
207 * Let \c mt be a depth miptree with HiZ enabled. Then the resolve map is
208 * \c mt->hiz_map. The resolve map of the child HiZ miptree, \c
209 * mt->hiz_mt->hiz_map, is unused.
210 */
211 struct intel_resolve_map hiz_map;
212
213 /**
214 * \brief Stencil miptree for depthstencil textures.
215 *
216 * This miptree is used for depthstencil textures and renderbuffers that
217 * require separate stencil. It always has the true copy of the stencil
218 * bits, regardless of mt->format.
219 *
220 * \see intel_miptree_map_depthstencil()
221 * \see intel_miptree_unmap_depthstencil()
222 */
223 struct intel_mipmap_tree *stencil_mt;
224
225 /* These are also refcounted:
226 */
227 GLuint refcount;
228 };
229
230
231
232 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
233 GLenum target,
234 gl_format format,
235 GLuint first_level,
236 GLuint last_level,
237 GLuint width0,
238 GLuint height0,
239 GLuint depth0,
240 bool expect_accelerated_upload,
241 GLuint num_samples);
242
243 struct intel_mipmap_tree *
244 intel_miptree_create_for_region(struct intel_context *intel,
245 GLenum target,
246 gl_format format,
247 struct intel_region *region);
248
249 /**
250 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
251 * The miptree has the following properties:
252 * - The target is GL_TEXTURE_2D.
253 * - There are no levels other than the base level 0.
254 * - Depth is 1.
255 */
256 struct intel_mipmap_tree*
257 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
258 gl_format format,
259 uint32_t width,
260 uint32_t height,
261 uint32_t num_samples);
262
263 /** \brief Assert that the level and layer are valid for the miptree. */
264 static inline void
265 intel_miptree_check_level_layer(struct intel_mipmap_tree *mt,
266 uint32_t level,
267 uint32_t layer)
268 {
269 assert(level >= mt->first_level);
270 assert(level <= mt->last_level);
271 assert(layer < mt->level[level].depth);
272 }
273
274 int intel_miptree_pitch_align (struct intel_context *intel,
275 struct intel_mipmap_tree *mt,
276 uint32_t tiling,
277 int pitch);
278
279 void intel_miptree_reference(struct intel_mipmap_tree **dst,
280 struct intel_mipmap_tree *src);
281
282 void intel_miptree_release(struct intel_mipmap_tree **mt);
283
284 /* Check if an image fits an existing mipmap tree layout
285 */
286 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
287 struct gl_texture_image *image);
288
289 void
290 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
291 GLuint level, GLuint face, GLuint depth,
292 GLuint *x, GLuint *y);
293
294 void
295 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
296 int *width, int *height, int *depth);
297
298 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
299 GLuint level,
300 GLuint x, GLuint y,
301 GLuint w, GLuint h, GLuint d);
302
303 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
304 GLuint level,
305 GLuint img, GLuint x, GLuint y);
306
307 void
308 intel_miptree_copy_teximage(struct intel_context *intel,
309 struct intel_texture_image *intelImage,
310 struct intel_mipmap_tree *dst_mt);
311
312 /**
313 * Copy the stencil data from \c mt->stencil_mt->region to \c mt->region for
314 * the given miptree slice.
315 *
316 * \see intel_mipmap_tree::stencil_mt
317 */
318 void
319 intel_miptree_s8z24_scatter(struct intel_context *intel,
320 struct intel_mipmap_tree *mt,
321 uint32_t level,
322 uint32_t slice);
323
324 /**
325 * Copy the stencil data in \c mt->stencil_mt->region to \c mt->region for the
326 * given miptree slice.
327 *
328 * \see intel_mipmap_tree::stencil_mt
329 */
330 void
331 intel_miptree_s8z24_gather(struct intel_context *intel,
332 struct intel_mipmap_tree *mt,
333 uint32_t level,
334 uint32_t layer);
335
336 /**
337 * \name Miptree HiZ functions
338 * \{
339 *
340 * It is safe to call the "slice_set_need_resolve" and "slice_resolve"
341 * functions on a miptree without HiZ. In that case, each function is a no-op.
342 */
343
344 /**
345 * \brief Allocate the miptree's embedded HiZ miptree.
346 * \see intel_mipmap_tree:hiz_mt
347 * \return false if allocation failed
348 */
349
350 bool
351 intel_miptree_alloc_hiz(struct intel_context *intel,
352 struct intel_mipmap_tree *mt,
353 GLuint num_samples);
354
355 void
356 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
357 uint32_t level,
358 uint32_t depth);
359 void
360 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
361 uint32_t level,
362 uint32_t depth);
363 void
364 intel_miptree_all_slices_set_need_hiz_resolve(struct intel_mipmap_tree *mt);
365
366 void
367 intel_miptree_all_slices_set_need_depth_resolve(struct intel_mipmap_tree *mt);
368
369 /**
370 * \return false if no resolve was needed
371 */
372 bool
373 intel_miptree_slice_resolve_hiz(struct intel_context *intel,
374 struct intel_mipmap_tree *mt,
375 unsigned int level,
376 unsigned int depth);
377
378 /**
379 * \return false if no resolve was needed
380 */
381 bool
382 intel_miptree_slice_resolve_depth(struct intel_context *intel,
383 struct intel_mipmap_tree *mt,
384 unsigned int level,
385 unsigned int depth);
386
387 /**
388 * \return false if no resolve was needed
389 */
390 bool
391 intel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
392 struct intel_mipmap_tree *mt);
393
394 /**
395 * \return false if no resolve was needed
396 */
397 bool
398 intel_miptree_all_slices_resolve_depth(struct intel_context *intel,
399 struct intel_mipmap_tree *mt);
400
401 /**\}*/
402
403 /* i915_mipmap_tree.c:
404 */
405 void i915_miptree_layout(struct intel_mipmap_tree *mt);
406 void i945_miptree_layout(struct intel_mipmap_tree *mt);
407 void brw_miptree_layout(struct intel_context *intel,
408 struct intel_mipmap_tree *mt);
409
410 void
411 intel_miptree_map(struct intel_context *intel,
412 struct intel_mipmap_tree *mt,
413 unsigned int level,
414 unsigned int slice,
415 unsigned int x,
416 unsigned int y,
417 unsigned int w,
418 unsigned int h,
419 GLbitfield mode,
420 void **out_ptr,
421 int *out_stride);
422
423 void
424 intel_miptree_unmap(struct intel_context *intel,
425 struct intel_mipmap_tree *mt,
426 unsigned int level,
427 unsigned int slice);
428
429 #ifdef I915
430 static inline void
431 intel_hiz_exec(struct intel_context *intel, struct intel_mipmap_tree *mt,
432 unsigned int level, unsigned int layer, enum gen6_hiz_op op)
433 {
434 /* Stub on i915. It would be nice if we didn't execute resolve code at all
435 * there.
436 */
437 }
438 #else
439 void
440 intel_hiz_exec(struct intel_context *intel, struct intel_mipmap_tree *mt,
441 unsigned int level, unsigned int layer, enum gen6_hiz_op op);
442 #endif
443
444 #ifdef __cplusplus
445 }
446 #endif
447
448 #endif