Consolidate texObj->Pos/Neg/X/Y/Z and texObj->Image into a single
[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; /**< Texture unit currently bound to */
71
72 unsigned totalSize; /**< Total size of the texture,
73 * including all mipmap levels
74 */
75
76 unsigned dirty_images[6]; /**< Flags for whether or not images
77 * need to be uploaded to local or
78 * AGP texture space. One flag set
79 * for each cube face for cubic
80 * textures. Bit zero corresponds to
81 * the base-level, which may or may
82 * not be the level zero mipmap.
83 */
84
85 unsigned timestamp; /**< Timestamp used to
86 * synchronize with 3d engine
87 * in hardware where textures
88 * are uploaded directly to
89 * the framebuffer.
90 */
91
92 unsigned firstLevel; /**< Image in \c tObj->Image[0] that
93 * corresponds to the base-level of
94 * this texture object.
95 */
96
97 unsigned lastLevel; /**< Last image in \c tObj->Image[0]
98 * used by the
99 * current LOD settings of
100 * this texture object. This
101 * value must be greater than
102 * or equal to \c firstLevel.
103 */
104 };
105
106
107 typedef void (destroy_texture_object_t)( void * driverContext,
108 driTextureObject * t );
109
110 /**
111 * Client-private representation of texture memory state.
112 *
113 * Clients will place one or more of these structs in their driver
114 * context struct to manage one or more global texture heaps.
115 */
116
117 struct dri_tex_heap {
118
119 /** Client-supplied heap identifier
120 */
121 unsigned heapId;
122
123 /** Pointer to the client's private context
124 */
125 void *driverContext;
126
127 /** Total size of the heap, in bytes
128 */
129 unsigned size;
130
131 /** \brief \f$log_2\f$ of size of single heap region
132 *
133 * Each context takes memory from the global texture heap in
134 * \f$2^{logGranularity}\f$ byte blocks. The value of
135 * \a logGranularity is based on the amount of memory represented
136 * by the heap and the maximum number of regions in the SAREA. Given
137 * \a b bytes of texture memory an \a n regions in the SAREA,
138 * \a logGranularity will be \f$\lfloor\log_2( b / n )\rfloor\f$.
139 */
140 unsigned logGranularity;
141
142 /** \brief Required alignment of allocations in this heap
143 *
144 * The alignment shift is supplied to \a mmAllocMem when memory is
145 * allocated from this heap. The value of \a alignmentShift will
146 * typically reflect some require of the hardware. This value has
147 * \b no \b relation to \a logGranularity. \a alignmentShift is a
148 * per-context value.
149 *
150 * \sa mmAllocMem
151 */
152 unsigned alignmentShift;
153
154 /** Number of elements in global list (the SAREA).
155 */
156 unsigned nrRegions;
157
158 /** Pointer to SAREA \a driTexRegion array
159 */
160 drmTextureRegionPtr global_regions;
161
162 /** Pointer to the texture state age (generation number) in the SAREA
163 */
164 unsigned * global_age;
165
166 /** Local age (generation number) of texture state
167 */
168 unsigned local_age;
169
170 /** Memory heap used to manage texture memory represented by
171 * this texture heap.
172 */
173 memHeap_t * memory_heap;
174
175 /** List of objects that we currently believe to be in texture
176 * memory.
177 */
178 driTextureObject texture_objects;
179
180 /** Pointer to the list of texture objects that are not in
181 * texture memory.
182 */
183 driTextureObject * swapped_objects;
184
185 /** Size of the driver-speicific texture object.
186 */
187 unsigned texture_object_size;
188
189
190 /**
191 * \brief Function to destroy driver-specific texture object data.
192 *
193 * This function is supplied by the driver so that the texture manager
194 * can release all resources associated with a texture object. This
195 * function should only release driver-specific data. That is,
196 * \a driDestroyTextureObject will release the texture memory
197 * associated with the texture object, it will release the memory
198 * for the texture object itself, and it will unlink the texture
199 * object from the texture object lists.
200 *
201 * \param driverContext Pointer to the driver supplied context
202 * \param t Texture object that is to be destroyed
203 * \sa driDestroyTextureObject
204 */
205
206 destroy_texture_object_t * destroy_texture_object;
207
208
209 /**
210 */
211 unsigned * texture_swaps;
212
213 /**
214 * Timestamp used to synchronize with 3d engine in hardware
215 * where textures are uploaded directly to the
216 * framebuffer.
217 */
218 unsigned timestamp;
219 };
220
221
222
223
224 /**
225 * Called by the client on lock contention to determine whether textures have
226 * been stolen. If another client has modified a region in which we have
227 * textures, then we need to figure out which of our textures have been
228 * removed and update our global LRU.
229 *
230 * \param heap Texture heap to be updated
231 * \hideinitializer
232 */
233
234 #define DRI_AGE_TEXTURES( heap ) \
235 do { \
236 if ( ((heap) != NULL) \
237 && ((heap)->local_age != (heap)->global_age[0]) ) \
238 driAgeTextures( heap ); \
239 } while( 0 )
240
241
242
243
244 /* This should be called whenever there has been contention on the hardware
245 * lock. driAgeTextures should not be called directly. Instead, clients
246 * should use DRI_AGE_TEXTURES, above.
247 */
248
249 void driAgeTextures( driTexHeap * heap );
250
251 void driUpdateTextureLRU( driTextureObject * t );
252 void driSwapOutTextureObject( driTextureObject * t );
253 void driDestroyTextureObject( driTextureObject * t );
254 int driAllocateTexture( driTexHeap * const * heap_array, unsigned nr_heaps,
255 driTextureObject * t );
256
257 GLboolean driIsTextureResident( GLcontext * ctx,
258 struct gl_texture_object * texObj );
259
260 driTexHeap * driCreateTextureHeap( unsigned heap_id, void * context,
261 unsigned size, unsigned alignmentShift, unsigned nr_regions,
262 drmTextureRegionPtr global_regions, unsigned * global_age,
263 driTextureObject * swapped_objects, unsigned texture_object_size,
264 destroy_texture_object_t * destroy_tex_obj );
265 void driDestroyTextureHeap( driTexHeap * heap );
266
267 void
268 driCalculateMaxTextureLevels( driTexHeap * const * heaps,
269 unsigned nr_heaps,
270 struct gl_constants * limits,
271 unsigned max_bytes_per_texel,
272 unsigned max_2D_size,
273 unsigned max_3D_size,
274 unsigned max_cube_size,
275 unsigned max_rect_size,
276 unsigned mipmaps_at_once,
277 int all_textures_one_heap );
278
279 void
280 driSetTextureSwapCounterLocation( driTexHeap * heap, unsigned * counter );
281
282 #define DRI_TEXMGR_DO_TEXTURE_1D 0x0001
283 #define DRI_TEXMGR_DO_TEXTURE_2D 0x0002
284 #define DRI_TEXMGR_DO_TEXTURE_3D 0x0004
285 #define DRI_TEXMGR_DO_TEXTURE_CUBE 0x0008
286 #define DRI_TEXMGR_DO_TEXTURE_RECT 0x0010
287
288 void driInitTextureObjects( GLcontext *ctx, driTextureObject * swapped,
289 GLuint targets );
290
291 GLboolean driValidateTextureHeaps( driTexHeap * const * texture_heaps,
292 unsigned nr_heaps, const driTextureObject * swapped );
293
294 extern void driCalculateTextureFirstLastLevel( driTextureObject * t );
295
296 #endif /* DRI_TEXMEM_H */