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