Replace GL_TEXTURE_* tokens with PIPE_TEXTURE_*
[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 != PIPE_TEXTURE_3D || mt->level[level].nr_images == 1)
184 return &zero;
185 else
186 return mt->level[level].image_offset;
187 }
188
189
190 /**
191 * Return the offset to the given mipmap texture image within the
192 * texture memory buffer, in bytes.
193 */
194 GLuint
195 st_miptree_image_offset(const struct pipe_mipmap_tree * mt,
196 GLuint face, GLuint level)
197 {
198 if (mt->target == PIPE_TEXTURE_CUBE)
199 return (mt->level[level].level_offset +
200 mt->level[level].image_offset[face] * mt->cpp);
201 else
202 return mt->level[level].level_offset;
203 }
204
205
206 GLuint
207 st_miptree_texel_offset(const struct pipe_mipmap_tree * mt,
208 GLuint face, GLuint level,
209 GLuint col, GLuint row, GLuint img)
210 {
211 GLuint imgOffset = st_miptree_image_offset(mt, face, level);
212
213 return imgOffset + row * (mt->pitch + col) * mt->cpp;
214 }
215
216
217
218 /**
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
223 */
224 GLubyte *
225 st_miptree_image_map(struct pipe_context *pipe,
226 struct pipe_mipmap_tree * mt,
227 GLuint face,
228 GLuint level,
229 GLuint * row_stride, GLuint * image_offsets)
230 {
231 GLubyte *ptr;
232 DBG("%s \n", __FUNCTION__);
233
234 if (row_stride)
235 *row_stride = mt->pitch * mt->cpp;
236
237 if (image_offsets)
238 memcpy(image_offsets, mt->level[level].image_offset,
239 mt->level[level].depth * sizeof(GLuint));
240
241 ptr = pipe->region_map(pipe, mt->region);
242
243 return ptr + st_miptree_image_offset(mt, face, level);
244 }
245
246 void
247 st_miptree_image_unmap(struct pipe_context *pipe,
248 struct pipe_mipmap_tree *mt)
249 {
250 DBG("%s\n", __FUNCTION__);
251 pipe->region_unmap(pipe, mt->region);
252 }
253
254
255
256 /* Upload data for a particular image.
257 */
258 void
259 st_miptree_image_data(struct pipe_context *pipe,
260 struct pipe_mipmap_tree *dst,
261 GLuint face,
262 GLuint level,
263 void *src,
264 GLuint src_row_pitch, GLuint src_image_pitch)
265 {
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);
269 GLuint i;
270 GLuint height = 0;
271 const GLubyte *srcUB = src;
272
273 DBG("%s\n", __FUNCTION__);
274 for (i = 0; i < depth; i++) {
275 height = dst->level[level].height;
276 if(dst->compressed)
277 height /= 4;
278 pipe->region_data(pipe, dst->region,
279 dst_offset + dst_depth_offset[i], /* dst_offset */
280 0, 0, /* dstx, dsty */
281 srcUB,
282 src_row_pitch,
283 0, 0, /* source x, y */
284 dst->level[level].width, height); /* width, height */
285
286 srcUB += src_image_pitch * dst->cpp;
287 }
288 }
289
290 /* Copy mipmap image between trees
291 */
292 void
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)
297 {
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);
305 GLuint i;
306
307 if (dst->compressed)
308 height /= 4;
309 for (i = 0; i < depth; i++) {
310 pipe->region_copy(pipe,
311 dst->region, dst_offset + dst_depth_offset[i],
312 0,
313 0,
314 src->region, src_offset + src_depth_offset[i],
315 0, 0, width, height);
316 }
317
318 }