Fix the comment for the 'bound' field: it's a bitmask, not an index.
[mesa.git] / src / mesa / drivers / dri / common / texmem.h
1 /*
2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * (c) Copyright IBM Corporation 2002
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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Ian Romanick <idr@us.ibm.com>
27 * Keith Whitwell <keithw@tungstengraphics.com>
28 * Kevin E. Martin <kem@users.sourceforge.net>
29 * Gareth Hughes <gareth@nvidia.com>
30 */
31 /* $XFree86:$ */
32
33 /** \file texmem.h
34 * Public interface to the DRI texture memory management routines.
35 *
36 * \sa texmem.c
37 */
38
39 #ifndef DRI_TEXMEM_H
40 #define DRI_TEXMEM_H
41
42 #include "mtypes.h"
43 #include "mm.h"
44 #include "xf86drm.h"
45
46 struct dri_tex_heap;
47 typedef struct dri_tex_heap driTexHeap;
48
49 struct dri_texture_object;
50 typedef struct dri_texture_object driTextureObject;
51
52
53 /**
54 * Base texture object type. Each driver will extend this type with its own
55 * private data members.
56 */
57
58 struct dri_texture_object {
59 struct dri_texture_object * next;
60 struct dri_texture_object * prev;
61
62 driTexHeap * heap; /**< Texture heap currently stored in */
63 struct gl_texture_object * tObj;/**< Pointer to Mesa texture object
64 * If NULL, this texture object is a
65 * "placeholder" object representing
66 * texture memory in use by another context.
67 * A placeholder should have a heap and a memBlock.
68 */
69 PMemBlock memBlock; /**< Memory block containing texture */
70 unsigned bound; /**< Bitmask indicating which tex units
71 * this texture object is bound to.
72 * Bit 0 = unit 0, Bit 1 = unit 1, etc
73 */
74
75 unsigned totalSize; /**< Total size of the texture,
76 * including all mipmap levels
77 */
78
79 unsigned dirty_images[6]; /**< Flags for whether or not images
80 * need to be uploaded to local or
81 * AGP texture space. One flag set
82 * for each cube face for cubic
83 * textures. Bit zero corresponds to
84 * the base-level, which may or may
85 * not be the level zero mipmap.
86 */
87
88 unsigned timestamp; /**< Timestamp used to
89 * synchronize with 3d engine
90 * in hardware where textures
91 * are uploaded directly to
92 * the framebuffer.
93 */
94
95 unsigned firstLevel; /**< Image in \c tObj->Image[0] that
96 * corresponds to the base-level of
97 * this texture object.
98 */
99
100 unsigned lastLevel; /**< Last image in \c tObj->Image[0]
101 * used by the
102 * current LOD settings of
103 * this texture object. This
104 * value must be greater than
105 * or equal to \c firstLevel.
106 */
107 };
108
109
110 typedef void (destroy_texture_object_t)( void * driverContext,
111 driTextureObject * t );
112
113 /**
114 * Client-private representation of texture memory state.
115 *
116 * Clients will place one or more of these structs in their driver
117 * context struct to manage one or more global texture heaps.
118 */
119
120 struct dri_tex_heap {
121
122 /** Client-supplied heap identifier
123 */
124 unsigned heapId;
125
126 /** Pointer to the client's private context
127 */
128 void *driverContext;
129
130 /** Total size of the heap, in bytes
131 */
132 unsigned size;
133
134 /** \brief \f$log_2\f$ of size of single heap region
135 *
136 * Each context takes memory from the global texture heap in
137 * \f$2^{logGranularity}\f$ byte blocks. The value of
138 * \a logGranularity is based on the amount of memory represented
139 * by the heap and the maximum number of regions in the SAREA. Given
140 * \a b bytes of texture memory an \a n regions in the SAREA,
141 * \a logGranularity will be \f$\lfloor\log_2( b / n )\rfloor\f$.
142 */
143 unsigned logGranularity;
144
145 /** \brief Required alignment of allocations in this heap
146 *
147 * The alignment shift is supplied to \a mmAllocMem when memory is
148 * allocated from this heap. The value of \a alignmentShift will
149 * typically reflect some require of the hardware. This value has
150 * \b no \b relation to \a logGranularity. \a alignmentShift is a
151 * per-context value.
152 *
153 * \sa mmAllocMem
154 */
155 unsigned alignmentShift;
156
157 /** Number of elements in global list (the SAREA).
158 */
159 unsigned nrRegions;
160
161 /** Pointer to SAREA \a driTexRegion array
162 */
163 drmTextureRegionPtr global_regions;
164
165 /** Pointer to the texture state age (generation number) in the SAREA
166 */
167 unsigned * global_age;
168
169 /** Local age (generation number) of texture state
170 */
171 unsigned local_age;
172
173 /** Memory heap used to manage texture memory represented by
174 * this texture heap.
175 */
176 memHeap_t * memory_heap;
177
178 /** List of objects that we currently believe to be in texture
179 * memory.
180 */
181 driTextureObject texture_objects;
182
183 /** Pointer to the list of texture objects that are not in
184 * texture memory.
185 */
186 driTextureObject * swapped_objects;
187
188 /** Size of the driver-speicific texture object.
189 */
190 unsigned texture_object_size;
191
192
193 /**
194 * \brief Function to destroy driver-specific texture object data.
195 *
196 * This function is supplied by the driver so that the texture manager
197 * can release all resources associated with a texture object. This
198 * function should only release driver-specific data. That is,
199 * \a driDestroyTextureObject will release the texture memory
200 * associated with the texture object, it will release the memory
201 * for the texture object itself, and it will unlink the texture
202 * object from the texture object lists.
203 *
204 * \param driverContext Pointer to the driver supplied context
205 * \param t Texture object that is to be destroyed
206 * \sa driDestroyTextureObject
207 */
208
209 destroy_texture_object_t * destroy_texture_object;
210
211
212 /**
213 */
214 unsigned * texture_swaps;
215
216 /**
217 * Timestamp used to synchronize with 3d engine in hardware
218 * where textures are uploaded directly to the
219 * framebuffer.
220 */
221 unsigned timestamp;
222
223 /** \brief Kick/upload weight
224 *
225 * When not enough free space is available this weight
226 * influences the choice of the heap from which textures are
227 * kicked. By default the weight is equal to the heap size.
228 */
229 double weight;
230
231 /** \brief Kick/upload duty
232 *
233 * The heap with the highest duty will be chosen for kicking
234 * textures if not enough free space is available. The duty is
235 * reduced by the amount of data kicked. Rebalancing of
236 * negative duties takes the weights into account.
237 */
238 int duty;
239 };
240
241
242
243
244 /**
245 * Called by the client on lock contention to determine whether textures have
246 * been stolen. If another client has modified a region in which we have
247 * textures, then we need to figure out which of our textures have been
248 * removed and update our global LRU.
249 *
250 * \param heap Texture heap to be updated
251 * \hideinitializer
252 */
253
254 #define DRI_AGE_TEXTURES( heap ) \
255 do { \
256 if ( ((heap) != NULL) \
257 && ((heap)->local_age != (heap)->global_age[0]) ) \
258 driAgeTextures( heap ); \
259 } while( 0 )
260
261
262
263
264 /* This should be called whenever there has been contention on the hardware
265 * lock. driAgeTextures should not be called directly. Instead, clients
266 * should use DRI_AGE_TEXTURES, above.
267 */
268
269 void driAgeTextures( driTexHeap * heap );
270
271 void driUpdateTextureLRU( driTextureObject * t );
272 void driSwapOutTextureObject( driTextureObject * t );
273 void driDestroyTextureObject( driTextureObject * t );
274 int driAllocateTexture( driTexHeap * const * heap_array, unsigned nr_heaps,
275 driTextureObject * t );
276
277 GLboolean driIsTextureResident( GLcontext * ctx,
278 struct gl_texture_object * texObj );
279
280 driTexHeap * driCreateTextureHeap( unsigned heap_id, void * context,
281 unsigned size, unsigned alignmentShift, unsigned nr_regions,
282 drmTextureRegionPtr global_regions, unsigned * global_age,
283 driTextureObject * swapped_objects, unsigned texture_object_size,
284 destroy_texture_object_t * destroy_tex_obj );
285 void driDestroyTextureHeap( driTexHeap * heap );
286
287 void
288 driCalculateMaxTextureLevels( driTexHeap * const * heaps,
289 unsigned nr_heaps,
290 struct gl_constants * limits,
291 unsigned max_bytes_per_texel,
292 unsigned max_2D_size,
293 unsigned max_3D_size,
294 unsigned max_cube_size,
295 unsigned max_rect_size,
296 unsigned mipmaps_at_once,
297 int all_textures_one_heap );
298
299 void
300 driSetTextureSwapCounterLocation( driTexHeap * heap, unsigned * counter );
301
302 #define DRI_TEXMGR_DO_TEXTURE_1D 0x0001
303 #define DRI_TEXMGR_DO_TEXTURE_2D 0x0002
304 #define DRI_TEXMGR_DO_TEXTURE_3D 0x0004
305 #define DRI_TEXMGR_DO_TEXTURE_CUBE 0x0008
306 #define DRI_TEXMGR_DO_TEXTURE_RECT 0x0010
307
308 void driInitTextureObjects( GLcontext *ctx, driTextureObject * swapped,
309 GLuint targets );
310
311 GLboolean driValidateTextureHeaps( driTexHeap * const * texture_heaps,
312 unsigned nr_heaps, const driTextureObject * swapped );
313
314 extern void driCalculateTextureFirstLastLevel( driTextureObject * t );
315
316
317 extern const struct gl_texture_format *_dri_texformat_rgba8888;
318 extern const struct gl_texture_format *_dri_texformat_argb8888;
319 extern const struct gl_texture_format *_dri_texformat_rgb565;
320 extern const struct gl_texture_format *_dri_texformat_argb4444;
321 extern const struct gl_texture_format *_dri_texformat_argb1555;
322 extern const struct gl_texture_format *_dri_texformat_al88;
323 extern const struct gl_texture_format *_dri_texformat_a8;
324 extern const struct gl_texture_format *_dri_texformat_ci8;
325 extern const struct gl_texture_format *_dri_texformat_i8;
326 extern const struct gl_texture_format *_dri_texformat_l8;
327
328 extern void driInitTextureFormats( void );
329
330 #endif /* DRI_TEXMEM_H */