2 * Copyright (C) 2008 Nicolai Haehnle.
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:
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.
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.
28 #include "radeon_mipmap_tree.h"
33 #include "main/simple_list.h"
34 #include "main/texcompress.h"
35 #include "main/texformat.h"
37 static GLuint
radeon_compressed_texture_size(GLcontext
*ctx
,
38 GLsizei width
, GLsizei height
, GLsizei depth
,
41 GLuint size
= _mesa_compressed_texture_size(ctx
, width
, height
, depth
, mesaFormat
);
43 if (mesaFormat
== MESA_FORMAT_RGB_DXT1
||
44 mesaFormat
== MESA_FORMAT_RGBA_DXT1
) {
45 if (width
+ 3 < 8) /* width one block */
47 else if (width
+ 3 < 16)
50 /* DXT3/5, 16 bytes per block */
51 // WARN_ONCE("DXT 3/5 suffers from multitexturing problems!\n");
60 static int radeon_compressed_num_bytes(GLuint mesaFormat
)
65 case MESA_FORMAT_RGB_FXT1
:
66 case MESA_FORMAT_RGBA_FXT1
:
67 case MESA_FORMAT_RGB_DXT1
:
68 case MESA_FORMAT_RGBA_DXT1
:
72 case MESA_FORMAT_RGBA_DXT3
:
73 case MESA_FORMAT_RGBA_DXT5
:
83 * Compute sizes and fill in offset and blit information for the given
84 * image (determined by \p face and \p level).
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.
89 static void compute_tex_image_offset(radeon_mipmap_tree
*mt
,
90 GLuint face
, GLuint level
, GLuint
* curOffset
)
92 radeon_mipmap_level
*lvl
= &mt
->levels
[level
];
94 /* Find image size in bytes */
96 /* TODO: Is this correct? Need test cases for compressed textures! */
99 lvl
->rowstride
= (lvl
->width
* mt
->bpp
+ 63) & ~63;
100 lvl
->size
= radeon_compressed_texture_size(mt
->radeon
->glCtx
,
101 lvl
->width
, lvl
->height
, lvl
->depth
, mt
->compressed
);
102 } else if (mt
->target
== GL_TEXTURE_RECTANGLE_NV
) {
103 lvl
->rowstride
= (lvl
->width
* mt
->bpp
+ 63) & ~63;
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
;
112 lvl
->rowstride
= (lvl
->width
* mt
->bpp
+ 31) & ~31;
113 lvl
->size
= lvl
->rowstride
* lvl
->height
* lvl
->depth
;
115 assert(lvl
->size
> 0);
117 /* All images are aligned to a 32-byte offset */
118 *curOffset
= (*curOffset
+ 0x1f) & ~0x1f;
119 lvl
->faces
[face
].offset
= *curOffset
;
120 *curOffset
+= lvl
->size
;
122 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
124 "level %d, face %d: rs:%d %dx%d at %d\n",
125 level
, face
, lvl
->rowstride
, lvl
->width
, lvl
->height
, lvl
->faces
[face
].offset
);
128 static GLuint
minify(GLuint size
, GLuint levels
)
130 size
= size
>> levels
;
136 static void calculate_miptree_layout(radeon_mipmap_tree
*mt
)
142 numLevels
= mt
->lastLevel
- mt
->firstLevel
+ 1;
143 assert(numLevels
<= RADEON_MAX_TEXTURE_LEVELS
);
146 for(i
= 0; i
< numLevels
; i
++) {
149 mt
->levels
[i
].width
= minify(mt
->width0
, i
);
150 mt
->levels
[i
].height
= minify(mt
->height0
, i
);
151 mt
->levels
[i
].depth
= minify(mt
->depth0
, i
);
153 for(face
= 0; face
< mt
->faces
; face
++)
154 compute_tex_image_offset(mt
, face
, i
, &curOffset
);
157 /* Note the required size in memory */
158 mt
->totalsize
= (curOffset
+ RADEON_OFFSET_MASK
) & ~RADEON_OFFSET_MASK
;
163 * Create a new mipmap tree, calculate its layout and allocate memory.
165 radeon_mipmap_tree
* radeon_miptree_create(radeonContextPtr rmesa
, radeonTexObj
*t
,
166 GLenum target
, GLuint firstLevel
, GLuint lastLevel
,
167 GLuint width0
, GLuint height0
, GLuint depth0
,
168 GLuint bpp
, GLuint tilebits
, GLuint compressed
)
170 radeon_mipmap_tree
*mt
= CALLOC_STRUCT(_radeon_mipmap_tree
);
176 mt
->faces
= (target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
177 mt
->firstLevel
= firstLevel
;
178 mt
->lastLevel
= lastLevel
;
180 mt
->height0
= height0
;
182 mt
->bpp
= compressed
? radeon_compressed_num_bytes(compressed
) : bpp
;
183 mt
->tilebits
= tilebits
;
184 mt
->compressed
= compressed
;
186 calculate_miptree_layout(mt
);
188 mt
->bo
= radeon_bo_open(rmesa
->radeonScreen
->bom
,
189 0, mt
->totalsize
, 1024,
190 RADEON_GEM_DOMAIN_VRAM
,
196 void radeon_miptree_reference(radeon_mipmap_tree
*mt
)
199 assert(mt
->refcount
> 0);
202 void radeon_miptree_unreference(radeon_mipmap_tree
*mt
)
207 assert(mt
->refcount
> 0);
210 radeon_bo_unref(mt
->bo
);
217 * Calculate first and last mip levels for the given texture object,
218 * where the dimensions are taken from the given texture image at
221 * Note: level is the OpenGL level number, which is not necessarily the same
222 * as the first level that is actually present.
224 * The base level image of the given texture face must be non-null,
227 static void calculate_first_last_level(struct gl_texture_object
*tObj
,
228 GLuint
*pfirstLevel
, GLuint
*plastLevel
,
229 GLuint face
, GLuint level
)
231 const struct gl_texture_image
* const baseImage
=
232 tObj
->Image
[face
][level
];
236 /* These must be signed values. MinLod and MaxLod can be negative numbers,
237 * and having firstLevel and lastLevel as signed prevents the need for
243 /* Yes, this looks overly complicated, but it's all needed.
245 switch (tObj
->Target
) {
249 case GL_TEXTURE_CUBE_MAP
:
250 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
251 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
253 firstLevel
= lastLevel
= tObj
->BaseLevel
;
255 firstLevel
= tObj
->BaseLevel
+ (GLint
)(tObj
->MinLod
+ 0.5);
256 firstLevel
= MAX2(firstLevel
, tObj
->BaseLevel
);
257 firstLevel
= MIN2(firstLevel
, level
+ baseImage
->MaxLog2
);
258 lastLevel
= tObj
->BaseLevel
+ (GLint
)(tObj
->MaxLod
+ 0.5);
259 lastLevel
= MAX2(lastLevel
, tObj
->BaseLevel
);
260 lastLevel
= MIN2(lastLevel
, level
+ baseImage
->MaxLog2
);
261 lastLevel
= MIN2(lastLevel
, tObj
->MaxLevel
);
262 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
265 case GL_TEXTURE_RECTANGLE_NV
:
266 case GL_TEXTURE_4D_SGIS
:
267 firstLevel
= lastLevel
= 0;
273 /* save these values */
274 *pfirstLevel
= firstLevel
;
275 *plastLevel
= lastLevel
;
280 * Checks whether the given miptree can hold the given texture image at the
281 * given face and level.
283 GLboolean
radeon_miptree_matches_image(radeon_mipmap_tree
*mt
,
284 struct gl_texture_image
*texImage
, GLuint face
, GLuint level
)
286 radeon_mipmap_level
*lvl
;
288 if (face
>= mt
->faces
|| level
< mt
->firstLevel
|| level
> mt
->lastLevel
)
291 if (texImage
->IsCompressed
!= mt
->compressed
)
294 if (!texImage
->IsCompressed
&&
296 texImage
->TexFormat
->TexelBytes
!= mt
->bpp
)
299 lvl
= &mt
->levels
[level
- mt
->firstLevel
];
300 if (lvl
->width
!= texImage
->Width
||
301 lvl
->height
!= texImage
->Height
||
302 lvl
->depth
!= texImage
->Depth
)
310 * Checks whether the given miptree has the right format to store the given texture object.
312 GLboolean
radeon_miptree_matches_texture(radeon_mipmap_tree
*mt
, struct gl_texture_object
*texObj
)
314 struct gl_texture_image
*firstImage
;
317 GLuint firstLevel
, lastLevel
;
319 calculate_first_last_level(texObj
, &firstLevel
, &lastLevel
, 0, texObj
->BaseLevel
);
320 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
323 firstImage
= texObj
->Image
[0][firstLevel
];
324 compressed
= firstImage
->IsCompressed
? firstImage
->TexFormat
->MesaFormat
: 0;
326 return (mt
->firstLevel
== firstLevel
&&
327 mt
->lastLevel
== lastLevel
&&
328 mt
->width0
== firstImage
->Width
&&
329 mt
->height0
== firstImage
->Height
&&
330 mt
->depth0
== firstImage
->Depth
&&
331 mt
->bpp
== firstImage
->TexFormat
->TexelBytes
&&
332 mt
->compressed
== compressed
);
337 * Try to allocate a mipmap tree for the given texture that will fit the
338 * given image in the given position.
340 void radeon_try_alloc_miptree(radeonContextPtr rmesa
, radeonTexObj
*t
,
341 struct gl_texture_image
*texImage
, GLuint face
, GLuint level
)
343 GLuint compressed
= texImage
->IsCompressed
? texImage
->TexFormat
->MesaFormat
: 0;
345 GLuint firstLevel
, lastLevel
;
349 calculate_first_last_level(&t
->base
, &firstLevel
, &lastLevel
, face
, level
);
350 if (t
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
353 if (level
!= firstLevel
|| face
>= numfaces
)
356 t
->mt
= radeon_miptree_create(rmesa
, t
, t
->base
.Target
,
357 firstLevel
, lastLevel
,
358 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
359 texImage
->TexFormat
->TexelBytes
, t
->tile_bits
, compressed
);