Plug in selection/feedback code.
[mesa.git] / src / mesa / state_tracker / st_mipmap_tree.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "st_mipmap_tree.h"
29 #include "enums.h"
30
31 #include "pipe/p_state.h"
32 #include "pipe/p_context.h"
33 #include "pipe/p_defines.h"
34
35
36 #define DBG if(0) printf
37
38 #if 0
39 static GLenum
40 target_to_target(GLenum target)
41 {
42 switch (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;
50 default:
51 return target;
52 }
53 }
54 #endif
55
56 struct pipe_mipmap_tree *
57 st_miptree_create(struct pipe_context *pipe,
58 unsigned target,
59 GLenum internal_format,
60 GLuint first_level,
61 GLuint last_level,
62 GLuint width0,
63 GLuint height0,
64 GLuint depth0, GLuint cpp, GLuint compress_byte)
65 {
66 GLboolean ok;
67 struct pipe_mipmap_tree *mt = calloc(sizeof(*mt), 1);
68 GLbitfield flags = 0x0;
69
70 assert(target <= PIPE_TEXTURE_CUBE);
71
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);
75
76 mt->target = target;
77 mt->internal_format = internal_format;
78 mt->first_level = first_level;
79 mt->last_level = last_level;
80 mt->width0 = width0;
81 mt->height0 = height0;
82 mt->depth0 = depth0;
83 mt->cpp = compress_byte ? compress_byte : cpp;
84 mt->compressed = compress_byte ? 1 : 0;
85 mt->refcount = 1;
86
87 ok = pipe->mipmap_tree_layout(pipe, mt);
88 if (ok) {
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);
92 }
93
94 if (!mt->region) {
95 free(mt);
96 return NULL;
97 }
98
99 return mt;
100 }
101
102
103 void
104 st_miptree_reference(struct pipe_mipmap_tree **dst,
105 struct pipe_mipmap_tree *src)
106 {
107 src->refcount++;
108 *dst = src;
109 DBG("%s %p refcount now %d\n", __FUNCTION__, (void *) src, src->refcount);
110 }
111
112 void
113 st_miptree_release(struct pipe_context *pipe,
114 struct pipe_mipmap_tree **mt)
115 {
116 if (!*mt)
117 return;
118
119 DBG("%s %p refcount will be %d\n",
120 __FUNCTION__, (void *) *mt, (*mt)->refcount - 1);
121 if (--(*mt)->refcount <= 0) {
122 GLuint i;
123
124 DBG("%s deleting %p\n", __FUNCTION__, (void *) *mt);
125
126 pipe->region_release(pipe, &((*mt)->region));
127
128 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
129 if ((*mt)->level[i].image_offset)
130 free((*mt)->level[i].image_offset);
131
132 free(*mt);
133 }
134 *mt = NULL;
135 }
136
137
138
139
140 /* Can the image be pulled into a unified mipmap tree. This mirrors
141 * the completeness test in a lot of ways.
142 *
143 * Not sure whether I want to pass gl_texture_image here.
144 */
145 GLboolean
146 st_miptree_match_image(struct pipe_mipmap_tree *mt,
147 struct gl_texture_image *image,
148 GLuint face, GLuint level)
149 {
150 /* Images with borders are never pulled into mipmap trees.
151 */
152 if (image->Border)
153 return GL_FALSE;
154
155 if (image->InternalFormat != mt->internal_format ||
156 image->IsCompressed != mt->compressed)
157 return GL_FALSE;
158
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.
162 */
163 if (image->Width != mt->level[level].width ||
164 image->Height != mt->level[level].height ||
165 image->Depth != mt->level[level].depth)
166 return GL_FALSE;
167
168 return GL_TRUE;
169 }
170
171
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.
175 *
176 * These functions present that view to mesa:
177 */
178 const GLuint *
179 st_miptree_depth_offsets(struct pipe_mipmap_tree *mt, GLuint level)
180 {
181 static const GLuint zero = 0;
182
183 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
184 return &zero;
185 else
186 return mt->level[level].image_offset;
187 }
188
189
190 GLuint
191 st_miptree_image_offset(const struct pipe_mipmap_tree * mt,
192 GLuint face, GLuint level)
193 {
194 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
195 return (mt->level[level].level_offset +
196 mt->level[level].image_offset[face] * mt->cpp);
197 else
198 return mt->level[level].level_offset;
199 }
200
201
202 GLuint
203 st_miptree_texel_offset(const struct pipe_mipmap_tree * mt,
204 GLuint face, GLuint level,
205 GLuint col, GLuint row, GLuint img)
206 {
207 GLuint imgOffset = st_miptree_image_offset(mt, face, level);
208
209 return imgOffset + row * (mt->pitch + col) * mt->cpp;
210 }
211
212
213
214 /**
215 * Map a teximage in a mipmap tree.
216 * \param row_stride returns row stride in bytes
217 * \param image_stride returns image stride in bytes (for 3D textures).
218 * \return address of mapping
219 */
220 GLubyte *
221 st_miptree_image_map(struct pipe_context *pipe,
222 struct pipe_mipmap_tree * mt,
223 GLuint face,
224 GLuint level,
225 GLuint * row_stride, GLuint * image_offsets)
226 {
227 GLubyte *ptr;
228 DBG("%s \n", __FUNCTION__);
229
230 if (row_stride)
231 *row_stride = mt->pitch * mt->cpp;
232
233 if (image_offsets)
234 memcpy(image_offsets, mt->level[level].image_offset,
235 mt->level[level].depth * sizeof(GLuint));
236
237 ptr = pipe->region_map(pipe, mt->region);
238
239 return ptr + st_miptree_image_offset(mt, face, level);
240 }
241
242 void
243 st_miptree_image_unmap(struct pipe_context *pipe,
244 struct pipe_mipmap_tree *mt)
245 {
246 DBG("%s\n", __FUNCTION__);
247 pipe->region_unmap(pipe, mt->region);
248 }
249
250
251
252 /* Upload data for a particular image.
253 */
254 void
255 st_miptree_image_data(struct pipe_context *pipe,
256 struct pipe_mipmap_tree *dst,
257 GLuint face,
258 GLuint level,
259 void *src,
260 GLuint src_row_pitch, GLuint src_image_pitch)
261 {
262 GLuint depth = dst->level[level].depth;
263 GLuint dst_offset = st_miptree_image_offset(dst, face, level);
264 const GLuint *dst_depth_offset = st_miptree_depth_offsets(dst, level);
265 GLuint i;
266 GLuint height = 0;
267 const GLubyte *srcUB = src;
268
269 DBG("%s\n", __FUNCTION__);
270 for (i = 0; i < depth; i++) {
271 height = dst->level[level].height;
272 if(dst->compressed)
273 height /= 4;
274 pipe->region_data(pipe, dst->region,
275 dst_offset + dst_depth_offset[i], /* dst_offset */
276 0, 0, /* dstx, dsty */
277 srcUB,
278 src_row_pitch,
279 0, 0, /* source x, y */
280 dst->level[level].width, height); /* width, height */
281
282 srcUB += src_image_pitch * dst->cpp;
283 }
284 }
285
286 /* Copy mipmap image between trees
287 */
288 void
289 st_miptree_image_copy(struct pipe_context *pipe,
290 struct pipe_mipmap_tree *dst,
291 GLuint face, GLuint level,
292 struct pipe_mipmap_tree *src)
293 {
294 GLuint width = src->level[level].width;
295 GLuint height = src->level[level].height;
296 GLuint depth = src->level[level].depth;
297 GLuint dst_offset = st_miptree_image_offset(dst, face, level);
298 GLuint src_offset = st_miptree_image_offset(src, face, level);
299 const GLuint *dst_depth_offset = st_miptree_depth_offsets(dst, level);
300 const GLuint *src_depth_offset = st_miptree_depth_offsets(src, level);
301 GLuint i;
302
303 if (dst->compressed)
304 height /= 4;
305 for (i = 0; i < depth; i++) {
306 pipe->region_copy(pipe,
307 dst->region, dst_offset + dst_depth_offset[i],
308 0,
309 0,
310 src->region, src_offset + src_depth_offset[i],
311 0, 0, width, height);
312 }
313
314 }