1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "st_mipmap_tree.h"
31 #include "pipe/p_state.h"
32 #include "pipe/p_context.h"
33 #include "pipe/p_defines.h"
36 #define DBG if(0) printf
40 target_to_target(GLenum target
)
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
49 return GL_TEXTURE_CUBE_MAP_ARB
;
56 struct pipe_mipmap_tree
*
57 st_miptree_create(struct pipe_context
*pipe
,
59 GLenum internal_format
,
64 GLuint depth0
, GLuint cpp
, GLuint compress_byte
)
67 struct pipe_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
68 GLbitfield flags
= 0x0;
70 assert(target
<= PIPE_TEXTURE_CUBE
);
72 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__
,
73 _mesa_lookup_enum_by_nr(target
),
74 _mesa_lookup_enum_by_nr(internal_format
), first_level
, last_level
);
77 mt
->internal_format
= internal_format
;
78 mt
->first_level
= first_level
;
79 mt
->last_level
= last_level
;
81 mt
->height0
= height0
;
83 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
84 mt
->compressed
= compress_byte
? 1 : 0;
87 ok
= pipe
->mipmap_tree_layout(pipe
, mt
);
89 /* note: it's OK to pass 'pitch' as 'width' here: */
90 mt
->region
= pipe
->region_alloc(pipe
, mt
->cpp
, mt
->pitch
,
91 mt
->total_height
, flags
);
104 st_miptree_reference(struct pipe_mipmap_tree
**dst
,
105 struct pipe_mipmap_tree
*src
)
109 DBG("%s %p refcount now %d\n", __FUNCTION__
, (void *) src
, src
->refcount
);
113 st_miptree_release(struct pipe_context
*pipe
,
114 struct pipe_mipmap_tree
**mt
)
119 DBG("%s %p refcount will be %d\n",
120 __FUNCTION__
, (void *) *mt
, (*mt
)->refcount
- 1);
121 if (--(*mt
)->refcount
<= 0) {
124 DBG("%s deleting %p\n", __FUNCTION__
, (void *) *mt
);
126 pipe
->region_release(pipe
, &((*mt
)->region
));
128 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
129 if ((*mt
)->level
[i
].image_offset
)
130 free((*mt
)->level
[i
].image_offset
);
140 /* Can the image be pulled into a unified mipmap tree. This mirrors
141 * the completeness test in a lot of ways.
143 * Not sure whether I want to pass gl_texture_image here.
146 st_miptree_match_image(struct pipe_mipmap_tree
*mt
,
147 struct gl_texture_image
*image
,
148 GLuint face
, GLuint level
)
150 /* Images with borders are never pulled into mipmap trees.
155 if (image
->InternalFormat
!= mt
->internal_format
||
156 image
->IsCompressed
!= mt
->compressed
)
159 /* Test image dimensions against the base level image adjusted for
160 * minification. This will also catch images not present in the
161 * tree, changed targets, etc.
163 if (image
->Width
!= mt
->level
[level
].width
||
164 image
->Height
!= mt
->level
[level
].height
||
165 image
->Depth
!= mt
->level
[level
].depth
)
172 /* Although we use the image_offset[] array to store relative offsets
173 * to cube faces, Mesa doesn't know anything about this and expects
174 * each cube face to be treated as a separate image.
176 * These functions present that view to mesa:
179 st_miptree_depth_offsets(struct pipe_mipmap_tree
*mt
, GLuint level
)
181 static const GLuint zero
= 0;
183 if (mt
->target
!= PIPE_TEXTURE_3D
|| mt
->level
[level
].nr_images
== 1)
186 return mt
->level
[level
].image_offset
;
191 * Return the offset to the given mipmap texture image within the
192 * texture memory buffer, in bytes.
195 st_miptree_image_offset(const struct pipe_mipmap_tree
* mt
,
196 GLuint face
, GLuint level
)
198 if (mt
->target
== PIPE_TEXTURE_CUBE
)
199 return (mt
->level
[level
].level_offset
+
200 mt
->level
[level
].image_offset
[face
] * mt
->cpp
);
202 return mt
->level
[level
].level_offset
;
207 st_miptree_texel_offset(const struct pipe_mipmap_tree
* mt
,
208 GLuint face
, GLuint level
,
209 GLuint col
, GLuint row
, GLuint img
)
211 GLuint imgOffset
= st_miptree_image_offset(mt
, face
, level
);
213 return imgOffset
+ row
* (mt
->pitch
+ col
) * mt
->cpp
;
219 * Map a teximage in a mipmap tree.
220 * \param row_stride returns row stride in bytes
221 * \param image_stride returns image stride in bytes (for 3D textures).
222 * \return address of mapping
225 st_miptree_image_map(struct pipe_context
*pipe
,
226 struct pipe_mipmap_tree
* mt
,
229 GLuint
* row_stride
, GLuint
* image_offsets
)
232 DBG("%s \n", __FUNCTION__
);
235 *row_stride
= mt
->pitch
* mt
->cpp
;
238 memcpy(image_offsets
, mt
->level
[level
].image_offset
,
239 mt
->level
[level
].depth
* sizeof(GLuint
));
241 ptr
= pipe
->region_map(pipe
, mt
->region
);
243 return ptr
+ st_miptree_image_offset(mt
, face
, level
);
247 st_miptree_image_unmap(struct pipe_context
*pipe
,
248 struct pipe_mipmap_tree
*mt
)
250 DBG("%s\n", __FUNCTION__
);
251 pipe
->region_unmap(pipe
, mt
->region
);
256 /* Upload data for a particular image.
259 st_miptree_image_data(struct pipe_context
*pipe
,
260 struct pipe_mipmap_tree
*dst
,
264 GLuint src_row_pitch
, GLuint src_image_pitch
)
266 GLuint depth
= dst
->level
[level
].depth
;
267 GLuint dst_offset
= st_miptree_image_offset(dst
, face
, level
);
268 const GLuint
*dst_depth_offset
= st_miptree_depth_offsets(dst
, level
);
271 const GLubyte
*srcUB
= src
;
273 DBG("%s\n", __FUNCTION__
);
274 for (i
= 0; i
< depth
; i
++) {
275 height
= dst
->level
[level
].height
;
278 pipe
->region_data(pipe
, dst
->region
,
279 dst_offset
+ dst_depth_offset
[i
], /* dst_offset */
280 0, 0, /* dstx, dsty */
283 0, 0, /* source x, y */
284 dst
->level
[level
].width
, height
); /* width, height */
286 srcUB
+= src_image_pitch
* dst
->cpp
;
290 /* Copy mipmap image between trees
293 st_miptree_image_copy(struct pipe_context
*pipe
,
294 struct pipe_mipmap_tree
*dst
,
295 GLuint face
, GLuint level
,
296 struct pipe_mipmap_tree
*src
)
298 GLuint width
= src
->level
[level
].width
;
299 GLuint height
= src
->level
[level
].height
;
300 GLuint depth
= src
->level
[level
].depth
;
301 GLuint dst_offset
= st_miptree_image_offset(dst
, face
, level
);
302 GLuint src_offset
= st_miptree_image_offset(src
, face
, level
);
303 const GLuint
*dst_depth_offset
= st_miptree_depth_offsets(dst
, level
);
304 const GLuint
*src_depth_offset
= st_miptree_depth_offsets(src
, level
);
309 for (i
= 0; i
< depth
; i
++) {
310 pipe
->region_copy(pipe
,
311 dst
->region
, dst_offset
+ dst_depth_offset
[i
],
314 src
->region
, src_offset
+ src_depth_offset
[i
],
315 0, 0, width
, height
);