853a4a7986de38404de8c50a0db47e8a1ba86d7e
[mesa.git] / src / mesa / drivers / dri / i915 / intel_mipmap_tree.h
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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_screen.h"
34 #include "intel_regions.h"
35 #include "GL/internal/dri_interface.h"
36
37 /* A layer on top of the intel_regions code which adds:
38 *
39 * - Code to size and layout a region to hold a set of mipmaps.
40 * - Query to determine if a new image fits in an existing tree.
41 * - More refcounting
42 * - maybe able to remove refcounting from intel_region?
43 * - ?
44 *
45 * The fixed mipmap layout of intel hardware where one offset
46 * specifies the position of all images in a mipmap hierachy
47 * complicates the implementation of GL texture image commands,
48 * compared to hardware where each image is specified with an
49 * independent offset.
50 *
51 * In an ideal world, each texture object would be associated with a
52 * single bufmgr buffer or 2d intel_region, and all the images within
53 * the texture object would slot into the tree as they arrive. The
54 * reality can be a little messier, as images can arrive from the user
55 * with sizes that don't fit in the existing tree, or in an order
56 * where the tree layout cannot be guessed immediately.
57 *
58 * This structure encodes an idealized mipmap tree. The GL image
59 * commands build these where possible, otherwise store the images in
60 * temporary system buffers.
61 */
62
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 /** Possible pointer to a temporary linear miptree for the mapping. */
73 struct intel_mipmap_tree *mt;
74 /** Pointer to the start of (map_x, map_y) returned by the mapping. */
75 void *ptr;
76 /** Stride of the mapping. */
77 int stride;
78 };
79
80 /**
81 * Describes the location of each texture image within a texture region.
82 */
83 struct intel_mipmap_level
84 {
85 /** Offset to this miptree level, used in computing x_offset. */
86 GLuint level_x;
87 /** Offset to this miptree level, used in computing y_offset. */
88 GLuint level_y;
89 GLuint width;
90 GLuint height;
91
92 /**
93 * \brief Number of 2D slices in this miplevel.
94 *
95 * The exact semantics of depth varies according to the texture target:
96 * - For GL_TEXTURE_CUBE_MAP, depth is 6.
97 * - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
98 * identical for all miplevels in the texture.
99 * - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
100 * value, like width and height, varies with miplevel.
101 * - For other texture types, depth is 1.
102 */
103 GLuint depth;
104
105 /**
106 * \brief List of 2D images in this mipmap level.
107 *
108 * This may be a list of cube faces, array slices in 2D array texture, or
109 * layers in a 3D texture. The list's length is \c depth.
110 */
111 struct intel_mipmap_slice {
112 /**
113 * \name Offset to slice
114 * \{
115 *
116 * Hardware formats are so diverse that that there is no unified way to
117 * compute the slice offsets, so we store them in this table.
118 *
119 * The (x, y) offset to slice \c s at level \c l relative the miptrees
120 * base address is
121 * \code
122 * x = mt->level[l].slice[s].x_offset
123 * y = mt->level[l].slice[s].y_offset
124 */
125 GLuint x_offset;
126 GLuint y_offset;
127 /** \} */
128
129 /**
130 * Mapping information. Persistent for the duration of
131 * intel_miptree_map/unmap on this slice.
132 */
133 struct intel_miptree_map *map;
134 } *slice;
135 };
136
137
138 struct intel_mipmap_tree
139 {
140 /* Effectively the key:
141 */
142 GLenum target;
143
144 /**
145 * This is just the same as the gl_texture_image->TexFormat or
146 * gl_renderbuffer->Format.
147 */
148 mesa_format format;
149
150 /**
151 * The X offset of each image in the miptree must be aligned to this. See
152 * the "Alignment Unit Size" section of the BSpec.
153 */
154 unsigned int align_w;
155 unsigned int align_h; /**< \see align_w */
156
157 GLuint first_level;
158 GLuint last_level;
159
160 /**
161 * Level zero image dimensions. These dimensions correspond to the
162 * physical layout of data in memory. Accordingly, they account for the
163 * extra width, height, and or depth that must be allocated in order to
164 * accommodate multisample formats, and they account for the extra factor
165 * of 6 in depth that must be allocated in order to accommodate cubemap
166 * textures.
167 */
168 GLuint physical_width0, physical_height0, physical_depth0;
169
170 GLuint cpp;
171 bool compressed;
172
173 /**
174 * Level zero image dimensions. These dimensions correspond to the
175 * logical width, height, and depth of the region as seen by client code.
176 * Accordingly, they do not account for the extra width, height, and/or
177 * depth that must be allocated in order to accommodate multisample
178 * formats, nor do they account for the extra factor of 6 in depth that
179 * must be allocated in order to accommodate cubemap textures.
180 */
181 uint32_t logical_width0, logical_height0, logical_depth0;
182
183 /**
184 * For 1D array, 2D array, cube, and 2D multisampled surfaces on Gen7: true
185 * if the surface only contains LOD 0, and hence no space is for LOD's
186 * other than 0 in between array slices.
187 *
188 * Corresponds to the surface_array_spacing bit in gen7_surface_state.
189 */
190 bool array_spacing_lod0;
191
192 /* Derived from the above:
193 */
194 GLuint total_width;
195 GLuint total_height;
196
197 /* Includes image offset tables:
198 */
199 struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
200
201 /* The data is held here:
202 */
203 struct intel_region *region;
204
205 /* Offset into region bo where miptree starts:
206 */
207 uint32_t offset;
208
209 /* These are also refcounted:
210 */
211 GLuint refcount;
212 };
213
214 enum intel_miptree_tiling_mode {
215 INTEL_MIPTREE_TILING_ANY,
216 INTEL_MIPTREE_TILING_Y,
217 INTEL_MIPTREE_TILING_NONE,
218 };
219
220 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
221 GLenum target,
222 mesa_format format,
223 GLuint first_level,
224 GLuint last_level,
225 GLuint width0,
226 GLuint height0,
227 GLuint depth0,
228 bool expect_accelerated_upload,
229 enum intel_miptree_tiling_mode);
230
231 struct intel_mipmap_tree *
232 intel_miptree_create_layout(struct intel_context *intel,
233 GLenum target,
234 mesa_format format,
235 GLuint first_level,
236 GLuint last_level,
237 GLuint width0,
238 GLuint height0,
239 GLuint depth0);
240
241 struct intel_mipmap_tree *
242 intel_miptree_create_for_bo(struct intel_context *intel,
243 drm_intel_bo *bo,
244 mesa_format format,
245 uint32_t offset,
246 uint32_t width,
247 uint32_t height,
248 int pitch,
249 uint32_t tiling);
250
251 struct intel_mipmap_tree*
252 intel_miptree_create_for_dri2_buffer(struct intel_context *intel,
253 unsigned dri_attachment,
254 mesa_format format,
255 struct intel_region *region);
256
257 struct intel_mipmap_tree*
258 intel_miptree_create_for_image_buffer(struct intel_context *intel,
259 enum __DRIimageBufferMask buffer_type,
260 mesa_format format,
261 uint32_t num_samples,
262 struct intel_region *region);
263
264 /**
265 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
266 * The miptree has the following properties:
267 * - The target is GL_TEXTURE_2D.
268 * - There are no levels other than the base level 0.
269 * - Depth is 1.
270 */
271 struct intel_mipmap_tree*
272 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
273 mesa_format format,
274 uint32_t width,
275 uint32_t height);
276
277 /** \brief Assert that the level and layer are valid for the miptree. */
278 static inline void
279 intel_miptree_check_level_layer(struct intel_mipmap_tree *mt,
280 uint32_t level,
281 uint32_t layer)
282 {
283 (void) mt;
284 (void) level;
285 (void) layer;
286
287 assert(level >= mt->first_level);
288 assert(level <= mt->last_level);
289 assert(layer < mt->level[level].depth);
290 }
291
292 int intel_miptree_pitch_align (struct intel_context *intel,
293 struct intel_mipmap_tree *mt,
294 uint32_t tiling,
295 int pitch);
296
297 void intel_miptree_reference(struct intel_mipmap_tree **dst,
298 struct intel_mipmap_tree *src);
299
300 void intel_miptree_release(struct intel_mipmap_tree **mt);
301
302 /* Check if an image fits an existing mipmap tree layout
303 */
304 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
305 struct gl_texture_image *image);
306
307 void
308 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
309 GLuint level, GLuint slice,
310 GLuint *x, GLuint *y);
311
312 void
313 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
314 int *width, int *height, int *depth);
315
316 uint32_t
317 intel_miptree_get_tile_offsets(struct intel_mipmap_tree *mt,
318 GLuint level, GLuint slice,
319 uint32_t *tile_x,
320 uint32_t *tile_y);
321
322 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
323 GLuint level,
324 GLuint x, GLuint y,
325 GLuint w, GLuint h, GLuint d);
326
327 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
328 GLuint level,
329 GLuint img, GLuint x, GLuint y);
330
331 void
332 intel_miptree_copy_teximage(struct intel_context *intel,
333 struct intel_texture_image *intelImage,
334 struct intel_mipmap_tree *dst_mt, bool invalidate);
335
336 /**\}*/
337
338 /* i915_mipmap_tree.c:
339 */
340 void i915_miptree_layout(struct intel_mipmap_tree *mt);
341 void i945_miptree_layout(struct intel_mipmap_tree *mt);
342
343 void *intel_miptree_map_raw(struct intel_context *intel,
344 struct intel_mipmap_tree *mt);
345
346 void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
347
348 void
349 intel_miptree_map(struct intel_context *intel,
350 struct intel_mipmap_tree *mt,
351 unsigned int level,
352 unsigned int slice,
353 unsigned int x,
354 unsigned int y,
355 unsigned int w,
356 unsigned int h,
357 GLbitfield mode,
358 void **out_ptr,
359 int *out_stride);
360
361 void
362 intel_miptree_unmap(struct intel_context *intel,
363 struct intel_mipmap_tree *mt,
364 unsigned int level,
365 unsigned int slice);
366
367
368 #endif