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