intel: Refactor intel_mipmap_level offsets
[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 "intel_regions.h"
32
33 /* A layer on top of the intel_regions code which adds:
34 *
35 * - Code to size and layout a region to hold a set of mipmaps.
36 * - Query to determine if a new image fits in an existing tree.
37 * - More refcounting
38 * - maybe able to remove refcounting from intel_region?
39 * - ?
40 *
41 * The fixed mipmap layout of intel hardware where one offset
42 * specifies the position of all images in a mipmap hierachy
43 * complicates the implementation of GL texture image commands,
44 * compared to hardware where each image is specified with an
45 * independent offset.
46 *
47 * In an ideal world, each texture object would be associated with a
48 * single bufmgr buffer or 2d intel_region, and all the images within
49 * the texture object would slot into the tree as they arrive. The
50 * reality can be a little messier, as images can arrive from the user
51 * with sizes that don't fit in the existing tree, or in an order
52 * where the tree layout cannot be guessed immediately.
53 *
54 * This structure encodes an idealized mipmap tree. The GL image
55 * commands build these where possible, otherwise store the images in
56 * temporary system buffers.
57 */
58
59 struct intel_texture_image;
60
61 /**
62 * Describes the location of each texture image within a texture region.
63 */
64 struct intel_mipmap_level
65 {
66 /** Offset to this miptree level, used in computing x_offset. */
67 GLuint level_x;
68 /** Offset to this miptree level, used in computing y_offset. */
69 GLuint level_y;
70 GLuint width;
71 GLuint height;
72 /** Depth of the mipmap at this level: 1 for 1D/2D/CUBE, n for 3D. */
73 GLuint depth;
74 /** Number of images at this level: 1 for 1D/2D, 6 for CUBE, depth for 3D */
75 GLuint nr_images;
76
77 /**
78 * \brief List of 2D images in this mipmap level.
79 *
80 * This may be a list of cube faces, array slices in 2D array texture, or
81 * layers in a 3D texture. The list's length is \c nr_images.
82 */
83 struct intel_mipmap_slice {
84 /**
85 * \name Offset to slice
86 * \{
87 *
88 * Hardware formats are so diverse that that there is no unified way to
89 * compute the slice offsets, so we store them in this table.
90 *
91 * The (x, y) offset to slice \c s at level \c l relative the miptrees
92 * base address is
93 * \code
94 * x = mt->level[l].slice[s].x_offset
95 * y = mt->level[l].slice[s].y_offset
96 */
97 GLuint x_offset;
98 GLuint y_offset;
99 /** \} */
100 } *slice;
101 };
102
103 struct intel_mipmap_tree
104 {
105 /* Effectively the key:
106 */
107 GLenum target;
108 gl_format format;
109
110 GLuint first_level;
111 GLuint last_level;
112
113 GLuint width0, height0, depth0; /**< Level zero image dimensions */
114 GLuint cpp;
115 bool compressed;
116
117 /* Derived from the above:
118 */
119 GLuint total_width;
120 GLuint total_height;
121
122 /* Includes image offset tables:
123 */
124 struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
125
126 /* The data is held here:
127 */
128 struct intel_region *region;
129
130 /**
131 * This points to an auxillary hiz region if all of the following hold:
132 * 1. The texture has been attached to an FBO as a depthbuffer.
133 * 2. The texture format is hiz compatible.
134 * 3. The intel context supports hiz.
135 *
136 * When a texture is attached to multiple FBO's, a separate renderbuffer
137 * wrapper is created for each attachment. This necessitates storing the
138 * hiz region in the texture itself instead of the renderbuffer wrapper.
139 *
140 * \see intel_fbo.c:intel_wrap_texture()
141 */
142 struct intel_region *hiz_region;
143
144 /* These are also refcounted:
145 */
146 GLuint refcount;
147 };
148
149
150
151 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
152 GLenum target,
153 gl_format format,
154 GLuint first_level,
155 GLuint last_level,
156 GLuint width0,
157 GLuint height0,
158 GLuint depth0,
159 bool expect_accelerated_upload);
160
161 struct intel_mipmap_tree *
162 intel_miptree_create_for_region(struct intel_context *intel,
163 GLenum target,
164 gl_format format,
165 struct intel_region *region);
166
167 /**
168 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
169 * The miptree has the following properties:
170 * - The target is GL_TEXTURE_2D.
171 * - There are no levels other than the base level 0.
172 * - Depth is 1.
173 */
174 struct intel_mipmap_tree*
175 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
176 gl_format format,
177 uint32_t tiling,
178 uint32_t cpp,
179 uint32_t width,
180 uint32_t height);
181
182 int intel_miptree_pitch_align (struct intel_context *intel,
183 struct intel_mipmap_tree *mt,
184 uint32_t tiling,
185 int pitch);
186
187 void intel_miptree_reference(struct intel_mipmap_tree **dst,
188 struct intel_mipmap_tree *src);
189
190 void intel_miptree_release(struct intel_mipmap_tree **mt);
191
192 /* Check if an image fits an existing mipmap tree layout
193 */
194 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
195 struct gl_texture_image *image);
196
197 void
198 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
199 GLuint level, GLuint face, GLuint depth,
200 GLuint *x, GLuint *y);
201
202 void
203 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
204 int *width, int *height, int *depth);
205
206 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
207 GLuint level,
208 GLuint nr_images,
209 GLuint x, GLuint y,
210 GLuint w, GLuint h, GLuint d);
211
212 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
213 GLuint level,
214 GLuint img, GLuint x, GLuint y);
215
216 void
217 intel_miptree_copy_teximage(struct intel_context *intel,
218 struct intel_texture_image *intelImage,
219 struct intel_mipmap_tree *dst_mt);
220
221 /* i915_mipmap_tree.c:
222 */
223 void i915_miptree_layout(struct intel_mipmap_tree *mt);
224 void i945_miptree_layout(struct intel_mipmap_tree *mt);
225 void brw_miptree_layout(struct intel_context *intel,
226 struct intel_mipmap_tree *mt);
227
228 #endif