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