Makeup checkin for radeon code change paired with r6/7 code.
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_mipmap_tree.c
1 /*
2 * Copyright (C) 2008 Nicolai Haehnle.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "radeon_mipmap_tree.h"
29
30 #include <errno.h>
31 #include <unistd.h>
32
33 #include "main/simple_list.h"
34 #include "main/texcompress.h"
35 #include "main/texformat.h"
36
37 static GLuint radeon_compressed_texture_size(GLcontext *ctx,
38 GLsizei width, GLsizei height, GLsizei depth,
39 GLuint mesaFormat)
40 {
41 GLuint size = _mesa_compressed_texture_size(ctx, width, height, depth, mesaFormat);
42
43 if (mesaFormat == MESA_FORMAT_RGB_DXT1 ||
44 mesaFormat == MESA_FORMAT_RGBA_DXT1) {
45 if (width + 3 < 8) /* width one block */
46 size = size * 4;
47 else if (width + 3 < 16)
48 size = size * 2;
49 } else {
50 /* DXT3/5, 16 bytes per block */
51 // WARN_ONCE("DXT 3/5 suffers from multitexturing problems!\n");
52 if (width + 3 < 8)
53 size = size * 2;
54 }
55
56 return size;
57 }
58
59
60 static int radeon_compressed_num_bytes(GLuint mesaFormat)
61 {
62 int bytes = 0;
63 switch(mesaFormat) {
64
65 case MESA_FORMAT_RGB_FXT1:
66 case MESA_FORMAT_RGBA_FXT1:
67 case MESA_FORMAT_RGB_DXT1:
68 case MESA_FORMAT_RGBA_DXT1:
69 bytes = 2;
70 break;
71
72 case MESA_FORMAT_RGBA_DXT3:
73 case MESA_FORMAT_RGBA_DXT5:
74 bytes = 4;
75 default:
76 break;
77 }
78
79 return bytes;
80 }
81
82 /**
83 * Compute sizes and fill in offset and blit information for the given
84 * image (determined by \p face and \p level).
85 *
86 * \param curOffset points to the offset at which the image is to be stored
87 * and is updated by this function according to the size of the image.
88 */
89 static void compute_tex_image_offset(radeon_mipmap_tree *mt,
90 GLuint face, GLuint level, GLuint* curOffset)
91 {
92 radeon_mipmap_level *lvl = &mt->levels[level];
93
94 /* Find image size in bytes */
95 if (mt->compressed) {
96 /* TODO: Is this correct? Need test cases for compressed textures! */
97 lvl->rowstride = (lvl->width * mt->bpp + 63) & ~63;
98 lvl->size = radeon_compressed_texture_size(mt->radeon->glCtx,
99 lvl->width, lvl->height, lvl->depth, mt->compressed);
100 } else if (mt->target == GL_TEXTURE_RECTANGLE_NV) {
101 lvl->rowstride = (lvl->width * mt->bpp + 63) & ~63;
102 lvl->size = lvl->rowstride * lvl->height;
103 } else if (mt->tilebits & RADEON_TXO_MICRO_TILE) {
104 /* tile pattern is 16 bytes x2. mipmaps stay 32 byte aligned,
105 * though the actual offset may be different (if texture is less than
106 * 32 bytes width) to the untiled case */
107 lvl->rowstride = (lvl->width * mt->bpp * 2 + 31) & ~31;
108 lvl->size = lvl->rowstride * ((lvl->height + 1) / 2) * lvl->depth;
109 } else {
110 lvl->rowstride = (lvl->width * mt->bpp + 31) & ~31;
111 lvl->size = lvl->rowstride * lvl->height * lvl->depth;
112 }
113 assert(lvl->size > 0);
114
115 /* All images are aligned to a 32-byte offset */
116 *curOffset = (*curOffset + 0x1f) & ~0x1f;
117 lvl->faces[face].offset = *curOffset;
118 *curOffset += lvl->size;
119
120 if (RADEON_DEBUG & DEBUG_TEXTURE)
121 fprintf(stderr,
122 "level %d, face %d: rs:%d %dx%d at %d\n",
123 level, face, lvl->rowstride, lvl->width, lvl->height, lvl->faces[face].offset);
124 }
125
126 static GLuint minify(GLuint size, GLuint levels)
127 {
128 size = size >> levels;
129 if (size < 1)
130 size = 1;
131 return size;
132 }
133
134 static void calculate_miptree_layout(radeon_mipmap_tree *mt)
135 {
136 GLuint curOffset;
137 GLuint numLevels;
138 GLuint i;
139
140 numLevels = mt->lastLevel - mt->firstLevel + 1;
141 assert(numLevels <= RADEON_MAX_TEXTURE_LEVELS);
142
143 curOffset = 0;
144 for(i = 0; i < numLevels; i++) {
145 GLuint face;
146
147 mt->levels[i].width = minify(mt->width0, i);
148 mt->levels[i].height = minify(mt->height0, i);
149 mt->levels[i].depth = minify(mt->depth0, i);
150
151 for(face = 0; face < mt->faces; face++)
152 compute_tex_image_offset(mt, face, i, &curOffset);
153 }
154
155 /* Note the required size in memory */
156 mt->totalsize = (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK;
157 }
158
159
160 /**
161 * Create a new mipmap tree, calculate its layout and allocate memory.
162 */
163 radeon_mipmap_tree* radeon_miptree_create(radeonContextPtr rmesa, radeonTexObj *t,
164 GLenum target, GLuint firstLevel, GLuint lastLevel,
165 GLuint width0, GLuint height0, GLuint depth0,
166 GLuint bpp, GLuint tilebits, GLuint compressed)
167 {
168 radeon_mipmap_tree *mt = CALLOC_STRUCT(_radeon_mipmap_tree);
169
170 mt->radeon = rmesa;
171 mt->refcount = 1;
172 mt->t = t;
173 mt->target = target;
174 mt->faces = (target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
175 mt->firstLevel = firstLevel;
176 mt->lastLevel = lastLevel;
177 mt->width0 = width0;
178 mt->height0 = height0;
179 mt->depth0 = depth0;
180 mt->bpp = compressed ? radeon_compressed_num_bytes(compressed) : bpp;
181 mt->tilebits = tilebits;
182 mt->compressed = compressed;
183
184 calculate_miptree_layout(mt);
185
186 #ifdef RADEON_DEBUG_BO
187 mt->bo = radeon_bo_open(rmesa->radeonScreen->bom,
188 0, mt->totalsize, 1024,
189 RADEON_GEM_DOMAIN_VRAM,
190 0,
191 "MIPMAP TREE");
192 #else
193 mt->bo = radeon_bo_open(rmesa->radeonScreen->bom,
194 0, mt->totalsize, 1024,
195 RADEON_GEM_DOMAIN_VRAM,
196 0);
197 #endif /* RADEON_DEBUG_BO */
198
199 return mt;
200 }
201
202 void radeon_miptree_reference(radeon_mipmap_tree *mt)
203 {
204 mt->refcount++;
205 assert(mt->refcount > 0);
206 }
207
208 void radeon_miptree_unreference(radeon_mipmap_tree *mt)
209 {
210 if (!mt)
211 return;
212
213 assert(mt->refcount > 0);
214 mt->refcount--;
215 if (!mt->refcount) {
216 radeon_bo_unref(mt->bo);
217 free(mt);
218 }
219 }
220
221
222 /**
223 * Calculate first and last mip levels for the given texture object,
224 * where the dimensions are taken from the given texture image at
225 * the given level.
226 *
227 * Note: level is the OpenGL level number, which is not necessarily the same
228 * as the first level that is actually present.
229 *
230 * The base level image of the given texture face must be non-null,
231 * or this will fail.
232 */
233 static void calculate_first_last_level(struct gl_texture_object *tObj,
234 GLuint *pfirstLevel, GLuint *plastLevel,
235 GLuint face, GLuint level)
236 {
237 const struct gl_texture_image * const baseImage =
238 tObj->Image[face][level];
239
240 assert(baseImage);
241
242 /* These must be signed values. MinLod and MaxLod can be negative numbers,
243 * and having firstLevel and lastLevel as signed prevents the need for
244 * extra sign checks.
245 */
246 int firstLevel;
247 int lastLevel;
248
249 /* Yes, this looks overly complicated, but it's all needed.
250 */
251 switch (tObj->Target) {
252 case GL_TEXTURE_1D:
253 case GL_TEXTURE_2D:
254 case GL_TEXTURE_3D:
255 case GL_TEXTURE_CUBE_MAP:
256 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
257 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
258 */
259 firstLevel = lastLevel = tObj->BaseLevel;
260 } else {
261 firstLevel = tObj->BaseLevel + (GLint)(tObj->MinLod + 0.5);
262 firstLevel = MAX2(firstLevel, tObj->BaseLevel);
263 firstLevel = MIN2(firstLevel, level + baseImage->MaxLog2);
264 lastLevel = tObj->BaseLevel + (GLint)(tObj->MaxLod + 0.5);
265 lastLevel = MAX2(lastLevel, tObj->BaseLevel);
266 lastLevel = MIN2(lastLevel, level + baseImage->MaxLog2);
267 lastLevel = MIN2(lastLevel, tObj->MaxLevel);
268 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */
269 }
270 break;
271 case GL_TEXTURE_RECTANGLE_NV:
272 case GL_TEXTURE_4D_SGIS:
273 firstLevel = lastLevel = 0;
274 break;
275 default:
276 return;
277 }
278
279 /* save these values */
280 *pfirstLevel = firstLevel;
281 *plastLevel = lastLevel;
282 }
283
284
285 /**
286 * Checks whether the given miptree can hold the given texture image at the
287 * given face and level.
288 */
289 GLboolean radeon_miptree_matches_image(radeon_mipmap_tree *mt,
290 struct gl_texture_image *texImage, GLuint face, GLuint level)
291 {
292 radeon_mipmap_level *lvl;
293
294 if (face >= mt->faces || level < mt->firstLevel || level > mt->lastLevel)
295 return GL_FALSE;
296
297 if (texImage->IsCompressed != mt->compressed)
298 return GL_FALSE;
299
300 if (!texImage->IsCompressed &&
301 !mt->compressed &&
302 texImage->TexFormat->TexelBytes != mt->bpp)
303 return GL_FALSE;
304
305 lvl = &mt->levels[level - mt->firstLevel];
306 if (lvl->width != texImage->Width ||
307 lvl->height != texImage->Height ||
308 lvl->depth != texImage->Depth)
309 return GL_FALSE;
310
311 return GL_TRUE;
312 }
313
314
315 /**
316 * Checks whether the given miptree has the right format to store the given texture object.
317 */
318 GLboolean radeon_miptree_matches_texture(radeon_mipmap_tree *mt, struct gl_texture_object *texObj)
319 {
320 struct gl_texture_image *firstImage;
321 GLuint compressed;
322 GLuint numfaces = 1;
323 GLuint firstLevel, lastLevel;
324
325 calculate_first_last_level(texObj, &firstLevel, &lastLevel, 0, texObj->BaseLevel);
326 if (texObj->Target == GL_TEXTURE_CUBE_MAP)
327 numfaces = 6;
328
329 firstImage = texObj->Image[0][firstLevel];
330 compressed = firstImage->IsCompressed ? firstImage->TexFormat->MesaFormat : 0;
331
332 return (mt->firstLevel == firstLevel &&
333 mt->lastLevel == lastLevel &&
334 mt->width0 == firstImage->Width &&
335 mt->height0 == firstImage->Height &&
336 mt->depth0 == firstImage->Depth &&
337 mt->bpp == firstImage->TexFormat->TexelBytes &&
338 mt->compressed == compressed);
339 }
340
341
342 /**
343 * Try to allocate a mipmap tree for the given texture that will fit the
344 * given image in the given position.
345 */
346 void radeon_try_alloc_miptree(radeonContextPtr rmesa, radeonTexObj *t,
347 struct gl_texture_image *texImage, GLuint face, GLuint level)
348 {
349 GLuint compressed = texImage->IsCompressed ? texImage->TexFormat->MesaFormat : 0;
350 GLuint numfaces = 1;
351 GLuint firstLevel, lastLevel;
352
353 assert(!t->mt);
354
355 calculate_first_last_level(&t->base, &firstLevel, &lastLevel, face, level);
356 if (t->base.Target == GL_TEXTURE_CUBE_MAP)
357 numfaces = 6;
358
359 if (level != firstLevel || face >= numfaces)
360 return;
361
362 t->mt = radeon_miptree_create(rmesa, t, t->base.Target,
363 firstLevel, lastLevel,
364 texImage->Width, texImage->Height, texImage->Depth,
365 texImage->TexFormat->TexelBytes, t->tile_bits, compressed);
366 }
367
368 /* Although we use the image_offset[] array to store relative offsets
369 * to cube faces, Mesa doesn't know anything about this and expects
370 * each cube face to be treated as a separate image.
371 *
372 * These functions present that view to mesa:
373 */
374 void
375 radeon_miptree_depth_offsets(radeon_mipmap_tree *mt, GLuint level, GLuint *offsets)
376 {
377 if (mt->target != GL_TEXTURE_3D || mt->faces == 1)
378 offsets[0] = 0;
379 else {
380 int i;
381 for (i = 0; i < 6; i++)
382 offsets[i] = mt->levels[level].faces[i].offset;
383 }
384 }
385
386 GLuint
387 radeon_miptree_image_offset(radeon_mipmap_tree *mt,
388 GLuint face, GLuint level)
389 {
390 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
391 return (mt->levels[level].faces[face].offset);
392 else
393 return mt->levels[level].faces[0].offset;
394 }