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