Merge branch 'mesa_7_6_branch'
[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
36 static GLuint radeon_compressed_texture_size(GLcontext *ctx,
37 GLsizei width, GLsizei height, GLsizei depth,
38 GLuint mesaFormat)
39 {
40 GLuint size = _mesa_format_image_size(mesaFormat, width, height, depth);
41
42 if (mesaFormat == MESA_FORMAT_RGB_DXT1 ||
43 mesaFormat == MESA_FORMAT_RGBA_DXT1) {
44 if (width + 3 < 8) /* width one block */
45 size = size * 4;
46 else if (width + 3 < 16)
47 size = size * 2;
48 } else {
49 /* DXT3/5, 16 bytes per block */
50 // WARN_ONCE("DXT 3/5 suffers from multitexturing problems!\n");
51 if (width + 3 < 8)
52 size = size * 2;
53 }
54
55 return size;
56 }
57
58
59 static int radeon_compressed_num_bytes(GLuint mesaFormat)
60 {
61 int bytes = 0;
62 switch(mesaFormat) {
63
64 case MESA_FORMAT_RGB_FXT1:
65 case MESA_FORMAT_RGBA_FXT1:
66 case MESA_FORMAT_RGB_DXT1:
67 case MESA_FORMAT_RGBA_DXT1:
68 bytes = 2;
69 break;
70
71 case MESA_FORMAT_RGBA_DXT3:
72 case MESA_FORMAT_RGBA_DXT5:
73 bytes = 4;
74 default:
75 break;
76 }
77
78 return bytes;
79 }
80
81 /**
82 * Compute sizes and fill in offset and blit information for the given
83 * image (determined by \p face and \p level).
84 *
85 * \param curOffset points to the offset at which the image is to be stored
86 * and is updated by this function according to the size of the image.
87 */
88 static void compute_tex_image_offset(radeonContextPtr rmesa, radeon_mipmap_tree *mt,
89 GLuint face, GLuint level, GLuint* curOffset)
90 {
91 radeon_mipmap_level *lvl = &mt->levels[level];
92 uint32_t row_align;
93
94 /* Find image size in bytes */
95 if (mt->compressed) {
96 /* TODO: Is this correct? Need test cases for compressed textures! */
97 row_align = rmesa->texture_compressed_row_align - 1;
98 lvl->rowstride = (lvl->width * mt->bpp + row_align) & ~row_align;
99 lvl->size = radeon_compressed_texture_size(mt->radeon->glCtx,
100 lvl->width, lvl->height, lvl->depth, mt->compressed);
101 } else if (mt->target == GL_TEXTURE_RECTANGLE_NV) {
102 row_align = rmesa->texture_rect_row_align - 1;
103 lvl->rowstride = (lvl->width * mt->bpp + row_align) & ~row_align;
104 lvl->size = lvl->rowstride * lvl->height;
105 } else if (mt->tilebits & RADEON_TXO_MICRO_TILE) {
106 /* tile pattern is 16 bytes x2. mipmaps stay 32 byte aligned,
107 * though the actual offset may be different (if texture is less than
108 * 32 bytes width) to the untiled case */
109 lvl->rowstride = (lvl->width * mt->bpp * 2 + 31) & ~31;
110 lvl->size = lvl->rowstride * ((lvl->height + 1) / 2) * lvl->depth;
111 } else {
112 row_align = rmesa->texture_row_align - 1;
113 lvl->rowstride = (lvl->width * mt->bpp + row_align) & ~row_align;
114 lvl->size = lvl->rowstride * lvl->height * lvl->depth;
115 }
116 assert(lvl->size > 0);
117
118 /* All images are aligned to a 32-byte offset */
119 *curOffset = (*curOffset + 0x1f) & ~0x1f;
120 lvl->faces[face].offset = *curOffset;
121 *curOffset += lvl->size;
122
123 if (RADEON_DEBUG & RADEON_TEXTURE)
124 fprintf(stderr,
125 "level %d, face %d: rs:%d %dx%d at %d\n",
126 level, face, lvl->rowstride, lvl->width, lvl->height, lvl->faces[face].offset);
127 }
128
129 static GLuint minify(GLuint size, GLuint levels)
130 {
131 size = size >> levels;
132 if (size < 1)
133 size = 1;
134 return size;
135 }
136
137
138 static void calculate_miptree_layout_r100(radeonContextPtr rmesa, radeon_mipmap_tree *mt)
139 {
140 GLuint curOffset;
141 GLuint numLevels;
142 GLuint i;
143 GLuint face;
144
145 numLevels = mt->lastLevel - mt->firstLevel + 1;
146 assert(numLevels <= rmesa->glCtx->Const.MaxTextureLevels);
147
148 curOffset = 0;
149 for(face = 0; face < mt->faces; face++) {
150
151 for(i = 0; i < numLevels; i++) {
152 mt->levels[i].width = minify(mt->width0, i);
153 mt->levels[i].height = minify(mt->height0, i);
154 mt->levels[i].depth = minify(mt->depth0, i);
155 compute_tex_image_offset(rmesa, mt, face, i, &curOffset);
156 }
157 }
158
159 /* Note the required size in memory */
160 mt->totalsize = (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK;
161 }
162
163 static void calculate_miptree_layout_r300(radeonContextPtr rmesa, radeon_mipmap_tree *mt)
164 {
165 GLuint curOffset;
166 GLuint numLevels;
167 GLuint i;
168
169 numLevels = mt->lastLevel - mt->firstLevel + 1;
170 assert(numLevels <= rmesa->glCtx->Const.MaxTextureLevels);
171
172 curOffset = 0;
173 for(i = 0; i < numLevels; i++) {
174 GLuint face;
175
176 mt->levels[i].width = minify(mt->width0, i);
177 mt->levels[i].height = minify(mt->height0, i);
178 mt->levels[i].depth = minify(mt->depth0, i);
179
180 for(face = 0; face < mt->faces; face++)
181 compute_tex_image_offset(rmesa, mt, face, i, &curOffset);
182 }
183
184 /* Note the required size in memory */
185 mt->totalsize = (curOffset + RADEON_OFFSET_MASK) & ~RADEON_OFFSET_MASK;
186 }
187
188 /**
189 * Create a new mipmap tree, calculate its layout and allocate memory.
190 */
191 radeon_mipmap_tree* radeon_miptree_create(radeonContextPtr rmesa, radeonTexObj *t,
192 GLenum target, GLenum internal_format, GLuint firstLevel, GLuint lastLevel,
193 GLuint width0, GLuint height0, GLuint depth0,
194 GLuint bpp, GLuint tilebits, GLuint compressed)
195 {
196 radeon_mipmap_tree *mt = CALLOC_STRUCT(_radeon_mipmap_tree);
197
198 mt->radeon = rmesa;
199 mt->internal_format = internal_format;
200 mt->refcount = 1;
201 mt->t = t;
202 mt->target = target;
203 mt->faces = (target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
204 mt->firstLevel = firstLevel;
205 mt->lastLevel = lastLevel;
206 mt->width0 = width0;
207 mt->height0 = height0;
208 mt->depth0 = depth0;
209 mt->bpp = compressed ? radeon_compressed_num_bytes(compressed) : bpp;
210 mt->tilebits = tilebits;
211 mt->compressed = compressed;
212
213 if (rmesa->radeonScreen->chip_family >= CHIP_FAMILY_R300)
214 calculate_miptree_layout_r300(rmesa, mt);
215 else
216 calculate_miptree_layout_r100(rmesa, mt);
217
218 mt->bo = radeon_bo_open(rmesa->radeonScreen->bom,
219 0, mt->totalsize, 1024,
220 RADEON_GEM_DOMAIN_VRAM,
221 0);
222
223 return mt;
224 }
225
226 void radeon_miptree_reference(radeon_mipmap_tree *mt)
227 {
228 mt->refcount++;
229 assert(mt->refcount > 0);
230 }
231
232 void radeon_miptree_unreference(radeon_mipmap_tree *mt)
233 {
234 if (!mt)
235 return;
236
237 assert(mt->refcount > 0);
238 mt->refcount--;
239 if (!mt->refcount) {
240 radeon_bo_unref(mt->bo);
241 free(mt);
242 }
243 }
244
245
246 /**
247 * Calculate first and last mip levels for the given texture object,
248 * where the dimensions are taken from the given texture image at
249 * the given level.
250 *
251 * Note: level is the OpenGL level number, which is not necessarily the same
252 * as the first level that is actually present.
253 *
254 * The base level image of the given texture face must be non-null,
255 * or this will fail.
256 */
257 static void calculate_first_last_level(struct gl_texture_object *tObj,
258 GLuint *pfirstLevel, GLuint *plastLevel,
259 GLuint face, GLuint level)
260 {
261 const struct gl_texture_image * const baseImage =
262 tObj->Image[face][level];
263
264 assert(baseImage);
265
266 /* These must be signed values. MinLod and MaxLod can be negative numbers,
267 * and having firstLevel and lastLevel as signed prevents the need for
268 * extra sign checks.
269 */
270 int firstLevel;
271 int lastLevel;
272
273 /* Yes, this looks overly complicated, but it's all needed.
274 */
275 switch (tObj->Target) {
276 case GL_TEXTURE_1D:
277 case GL_TEXTURE_2D:
278 case GL_TEXTURE_3D:
279 case GL_TEXTURE_CUBE_MAP:
280 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
281 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
282 */
283 firstLevel = lastLevel = tObj->BaseLevel;
284 } else {
285 firstLevel = tObj->BaseLevel + (GLint)(tObj->MinLod + 0.5);
286 firstLevel = MAX2(firstLevel, tObj->BaseLevel);
287 firstLevel = MIN2(firstLevel, level + baseImage->MaxLog2);
288 lastLevel = tObj->BaseLevel + (GLint)(tObj->MaxLod + 0.5);
289 lastLevel = MAX2(lastLevel, tObj->BaseLevel);
290 lastLevel = MIN2(lastLevel, level + baseImage->MaxLog2);
291 lastLevel = MIN2(lastLevel, tObj->MaxLevel);
292 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */
293 }
294 break;
295 case GL_TEXTURE_RECTANGLE_NV:
296 case GL_TEXTURE_4D_SGIS:
297 firstLevel = lastLevel = 0;
298 break;
299 default:
300 return;
301 }
302
303 /* save these values */
304 *pfirstLevel = firstLevel;
305 *plastLevel = lastLevel;
306 }
307
308
309 /**
310 * Checks whether the given miptree can hold the given texture image at the
311 * given face and level.
312 */
313 GLboolean radeon_miptree_matches_image(radeon_mipmap_tree *mt,
314 struct gl_texture_image *texImage, GLuint face, GLuint level)
315 {
316 GLboolean isCompressed = _mesa_is_format_compressed(texImage->TexFormat);
317 radeon_mipmap_level *lvl;
318
319 if (face >= mt->faces || level < mt->firstLevel || level > mt->lastLevel)
320 return GL_FALSE;
321
322 if (texImage->InternalFormat != mt->internal_format ||
323 isCompressed != mt->compressed)
324 return GL_FALSE;
325
326 if (!isCompressed &&
327 !mt->compressed &&
328 _mesa_get_format_bytes(texImage->TexFormat) != mt->bpp)
329 return GL_FALSE;
330
331 lvl = &mt->levels[level - mt->firstLevel];
332 if (lvl->width != texImage->Width ||
333 lvl->height != texImage->Height ||
334 lvl->depth != texImage->Depth)
335 return GL_FALSE;
336
337 return GL_TRUE;
338 }
339
340
341 /**
342 * Checks whether the given miptree has the right format to store the given texture object.
343 */
344 GLboolean radeon_miptree_matches_texture(radeon_mipmap_tree *mt, struct gl_texture_object *texObj)
345 {
346 struct gl_texture_image *firstImage;
347 GLuint compressed;
348 GLuint numfaces = 1;
349 GLuint firstLevel, lastLevel;
350 GLuint texelBytes;
351
352 calculate_first_last_level(texObj, &firstLevel, &lastLevel, 0, texObj->BaseLevel);
353 if (texObj->Target == GL_TEXTURE_CUBE_MAP)
354 numfaces = 6;
355
356 firstImage = texObj->Image[0][firstLevel];
357 compressed = _mesa_is_format_compressed(firstImage->TexFormat) ? firstImage->TexFormat : 0;
358 texelBytes = _mesa_get_format_bytes(firstImage->TexFormat);
359
360 return (mt->firstLevel == firstLevel &&
361 mt->lastLevel == lastLevel &&
362 mt->width0 == firstImage->Width &&
363 mt->height0 == firstImage->Height &&
364 mt->depth0 == firstImage->Depth &&
365 mt->compressed == compressed &&
366 (!mt->compressed ? (mt->bpp == texelBytes) : 1));
367 }
368
369
370 /**
371 * Try to allocate a mipmap tree for the given texture that will fit the
372 * given image in the given position.
373 */
374 void radeon_try_alloc_miptree(radeonContextPtr rmesa, radeonTexObj *t,
375 radeon_texture_image *image, GLuint face, GLuint level)
376 {
377 GLuint compressed = _mesa_is_format_compressed(image->base.TexFormat) ? image->base.TexFormat : 0;
378 GLuint numfaces = 1;
379 GLuint firstLevel, lastLevel;
380 GLuint texelBytes;
381
382 assert(!t->mt);
383
384 calculate_first_last_level(&t->base, &firstLevel, &lastLevel, face, level);
385 if (t->base.Target == GL_TEXTURE_CUBE_MAP)
386 numfaces = 6;
387
388 if (level != firstLevel || face >= numfaces)
389 return;
390
391 texelBytes = _mesa_get_format_bytes(image->base.TexFormat);
392
393 t->mt = radeon_miptree_create(rmesa, t, t->base.Target,
394 image->base.InternalFormat,
395 firstLevel, lastLevel,
396 image->base.Width, image->base.Height, image->base.Depth,
397 texelBytes, t->tile_bits, compressed);
398 }
399
400 /* Although we use the image_offset[] array to store relative offsets
401 * to cube faces, Mesa doesn't know anything about this and expects
402 * each cube face to be treated as a separate image.
403 *
404 * These functions present that view to mesa:
405 */
406 void
407 radeon_miptree_depth_offsets(radeon_mipmap_tree *mt, GLuint level, GLuint *offsets)
408 {
409 if (mt->target != GL_TEXTURE_3D || mt->faces == 1)
410 offsets[0] = 0;
411 else {
412 int i;
413 for (i = 0; i < 6; i++)
414 offsets[i] = mt->levels[level].faces[i].offset;
415 }
416 }
417
418 GLuint
419 radeon_miptree_image_offset(radeon_mipmap_tree *mt,
420 GLuint face, GLuint level)
421 {
422 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
423 return (mt->levels[level].faces[face].offset);
424 else
425 return mt->levels[level].faces[0].offset;
426 }