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 #include "radeon_buffer.h"
39 static GLuint
radeon_compressed_texture_size(GLcontext
*ctx
,
40 GLsizei width
, GLsizei height
, GLsizei depth
,
43 GLuint size
= _mesa_compressed_texture_size(ctx
, width
, height
, depth
, mesaFormat
);
45 if (mesaFormat
== MESA_FORMAT_RGB_DXT1
||
46 mesaFormat
== MESA_FORMAT_RGBA_DXT1
) {
47 if (width
+ 3 < 8) /* width one block */
49 else if (width
+ 3 < 16)
52 /* DXT3/5, 16 bytes per block */
53 // WARN_ONCE("DXT 3/5 suffers from multitexturing problems!\n");
62 * Compute sizes and fill in offset and blit information for the given
63 * image (determined by \p face and \p level).
65 * \param curOffset points to the offset at which the image is to be stored
66 * and is updated by this function according to the size of the image.
68 static void compute_tex_image_offset(radeon_mipmap_tree
*mt
,
69 GLuint face
, GLuint level
, GLuint
* curOffset
)
71 radeon_mipmap_level
*lvl
= &mt
->levels
[level
];
73 /* Find image size in bytes */
75 /* TODO: Is this correct? Need test cases for compressed textures! */
78 if (mt
->target
== GL_TEXTURE_RECTANGLE_NV
)
82 lvl
->rowstride
= (lvl
->width
+ align
- 1) & ~(align
- 1);
83 lvl
->size
= radeon_compressed_texture_size(mt
->radeon
->glCtx
,
84 lvl
->width
, lvl
->height
, lvl
->depth
, mt
->compressed
);
85 } else if (mt
->target
== GL_TEXTURE_RECTANGLE_NV
) {
86 lvl
->rowstride
= (lvl
->width
* mt
->bpp
+ 63) & ~63;
87 lvl
->size
= lvl
->rowstride
* lvl
->height
;
88 } else if (mt
->tilebits
& RADEON_TXO_MICRO_TILE
) {
89 /* tile pattern is 16 bytes x2. mipmaps stay 32 byte aligned,
90 * though the actual offset may be different (if texture is less than
91 * 32 bytes width) to the untiled case */
92 lvl
->rowstride
= (lvl
->width
* mt
->bpp
* 2 + 31) & ~31;
93 lvl
->size
= lvl
->rowstride
* ((lvl
->height
+ 1) / 2) * lvl
->depth
;
95 lvl
->rowstride
= (lvl
->width
* mt
->bpp
+ 31) & ~31;
96 lvl
->size
= lvl
->rowstride
* lvl
->height
* lvl
->depth
;
98 assert(lvl
->size
> 0);
100 /* All images are aligned to a 32-byte offset */
101 *curOffset
= (*curOffset
+ 0x1f) & ~0x1f;
102 lvl
->faces
[face
].offset
= *curOffset
;
103 *curOffset
+= lvl
->size
;
105 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
107 "level %d, face %d: rs:%d %dx%d at %d\n",
108 level
, face
, lvl
->rowstride
, lvl
->width
, lvl
->height
, lvl
->faces
[face
].offset
);
111 static GLuint
minify(GLuint size
, GLuint levels
)
113 size
= size
>> levels
;
119 static void calculate_miptree_layout(radeon_mipmap_tree
*mt
)
125 numLevels
= mt
->lastLevel
- mt
->firstLevel
+ 1;
126 assert(numLevels
<= RADEON_MAX_TEXTURE_LEVELS
);
129 for(i
= 0; i
< numLevels
; i
++) {
132 mt
->levels
[i
].width
= minify(mt
->width0
, i
);
133 mt
->levels
[i
].height
= minify(mt
->height0
, i
);
134 mt
->levels
[i
].depth
= minify(mt
->depth0
, i
);
136 for(face
= 0; face
< mt
->faces
; face
++)
137 compute_tex_image_offset(mt
, face
, i
, &curOffset
);
140 /* Note the required size in memory */
141 mt
->totalsize
= (curOffset
+ RADEON_OFFSET_MASK
) & ~RADEON_OFFSET_MASK
;
146 * Create a new mipmap tree, calculate its layout and allocate memory.
148 radeon_mipmap_tree
* radeon_miptree_create(radeonContextPtr rmesa
, radeonTexObj
*t
,
149 GLenum target
, GLuint firstLevel
, GLuint lastLevel
,
150 GLuint width0
, GLuint height0
, GLuint depth0
,
151 GLuint bpp
, GLuint tilebits
, GLuint compressed
)
153 radeon_mipmap_tree
*mt
= CALLOC_STRUCT(_radeon_mipmap_tree
);
159 mt
->faces
= (target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
160 mt
->firstLevel
= firstLevel
;
161 mt
->lastLevel
= lastLevel
;
163 mt
->height0
= height0
;
166 mt
->tilebits
= tilebits
;
167 mt
->compressed
= compressed
;
169 calculate_miptree_layout(mt
);
171 mt
->bo
= radeon_bo_open(rmesa
->radeonScreen
->bom
,
172 0, mt
->totalsize
, 1024,
173 RADEON_GEM_DOMAIN_VRAM
,
179 void radeon_miptree_reference(radeon_mipmap_tree
*mt
)
182 assert(mt
->refcount
> 0);
185 void radeon_miptree_unreference(radeon_mipmap_tree
*mt
)
190 assert(mt
->refcount
> 0);
193 radeon_bo_unref(mt
->bo
);
199 static void calculate_first_last_level(struct gl_texture_object
*tObj
,
200 GLuint
*pfirstLevel
, GLuint
*plastLevel
)
202 const struct gl_texture_image
* const baseImage
=
203 tObj
->Image
[0][tObj
->BaseLevel
];
205 /* These must be signed values. MinLod and MaxLod can be negative numbers,
206 * and having firstLevel and lastLevel as signed prevents the need for
212 /* Yes, this looks overly complicated, but it's all needed.
214 switch (tObj
->Target
) {
218 case GL_TEXTURE_CUBE_MAP
:
219 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
220 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
222 firstLevel
= lastLevel
= tObj
->BaseLevel
;
224 firstLevel
= tObj
->BaseLevel
+ (GLint
)(tObj
->MinLod
+ 0.5);
225 firstLevel
= MAX2(firstLevel
, tObj
->BaseLevel
);
226 firstLevel
= MIN2(firstLevel
, tObj
->BaseLevel
+ baseImage
->MaxLog2
);
227 lastLevel
= tObj
->BaseLevel
+ (GLint
)(tObj
->MaxLod
+ 0.5);
228 lastLevel
= MAX2(lastLevel
, tObj
->BaseLevel
);
229 lastLevel
= MIN2(lastLevel
, tObj
->BaseLevel
+ baseImage
->MaxLog2
);
230 lastLevel
= MIN2(lastLevel
, tObj
->MaxLevel
);
231 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
234 case GL_TEXTURE_RECTANGLE_NV
:
235 case GL_TEXTURE_4D_SGIS
:
236 firstLevel
= lastLevel
= 0;
242 /* save these values */
243 *pfirstLevel
= firstLevel
;
244 *plastLevel
= lastLevel
;
249 * Checks whether the given miptree can hold the given texture image at the
250 * given face and level.
252 GLboolean
radeon_miptree_matches_image(radeon_mipmap_tree
*mt
,
253 struct gl_texture_image
*texImage
, GLuint face
, GLuint level
)
255 radeon_mipmap_level
*lvl
;
257 if (face
>= mt
->faces
|| level
< mt
->firstLevel
|| level
> mt
->lastLevel
)
260 if (texImage
->TexFormat
->TexelBytes
!= mt
->bpp
)
263 lvl
= &mt
->levels
[level
- mt
->firstLevel
];
264 if (lvl
->width
!= texImage
->Width
||
265 lvl
->height
!= texImage
->Height
||
266 lvl
->depth
!= texImage
->Depth
)
274 * Checks whether the given miptree has the right format to store the given texture object.
276 GLboolean
radeon_miptree_matches_texture(radeon_mipmap_tree
*mt
, struct gl_texture_object
*texObj
)
278 struct gl_texture_image
*firstImage
;
281 GLuint firstLevel
, lastLevel
;
283 calculate_first_last_level(texObj
, &firstLevel
, &lastLevel
);
284 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
)
287 firstImage
= texObj
->Image
[0][firstLevel
];
288 compressed
= firstImage
->IsCompressed
? firstImage
->TexFormat
->MesaFormat
: 0;
290 return (mt
->firstLevel
== firstLevel
&&
291 mt
->lastLevel
== lastLevel
&&
292 mt
->width0
== firstImage
->Width
&&
293 mt
->height0
== firstImage
->Height
&&
294 mt
->depth0
== firstImage
->Depth
&&
295 mt
->bpp
== firstImage
->TexFormat
->TexelBytes
&&
296 mt
->compressed
== compressed
);
301 * Try to allocate a mipmap tree for the given texture that will fit the
302 * given image in the given position.
304 void radeon_try_alloc_miptree(radeonContextPtr rmesa
, radeonTexObj
*t
,
305 struct gl_texture_image
*texImage
, GLuint face
, GLuint level
)
307 GLuint compressed
= texImage
->IsCompressed
? texImage
->TexFormat
->MesaFormat
: 0;
309 GLuint firstLevel
, lastLevel
;
313 calculate_first_last_level(&t
->base
, &firstLevel
, &lastLevel
);
314 if (t
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
317 if (level
!= firstLevel
|| face
>= numfaces
)
320 t
->mt
= radeon_miptree_create(rmesa
, t
, t
->base
.Target
,
321 firstLevel
, lastLevel
,
322 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
323 texImage
->TexFormat
->TexelBytes
, t
->tile_bits
, compressed
);