Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / mesa / state_tracker / st_texture.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_context.h"
29 #include "st_format.h"
30 #include "st_texture.h"
31 #include "enums.h"
32
33 #undef Elements /* fix re-defined macro warning */
34
35 #include "pipe/p_state.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "pipe/p_inlines.h"
39 #include "pipe/p_util.h"
40 #include "pipe/p_inlines.h"
41 #include "pipe/p_winsys.h"
42
43
44 #define DBG if(0) printf
45
46 #if 0
47 static GLenum
48 target_to_target(GLenum target)
49 {
50 switch (target) {
51 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
52 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
55 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
56 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
57 return GL_TEXTURE_CUBE_MAP_ARB;
58 default:
59 return target;
60 }
61 }
62 #endif
63
64
65 /**
66 * Allocate a new pipe_texture object
67 * width0, height0, depth0 are the dimensions of the level 0 image
68 * (the highest resolution). last_level indicates how many mipmap levels
69 * to allocate storage for. For non-mipmapped textures, this will be zero.
70 */
71 struct pipe_texture *
72 st_texture_create(struct st_context *st,
73 enum pipe_texture_target target,
74 enum pipe_format format,
75 GLuint last_level,
76 GLuint width0,
77 GLuint height0,
78 GLuint depth0,
79 GLuint compress_byte)
80 {
81 struct pipe_texture pt, *newtex;
82 struct pipe_screen *screen = st->pipe->screen;
83
84 assert(target <= PIPE_TEXTURE_CUBE);
85
86 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
87 _mesa_lookup_enum_by_nr(target),
88 _mesa_lookup_enum_by_nr(format), last_level);
89
90 assert(format);
91 assert(screen->is_format_supported(screen, format, PIPE_TEXTURE));
92
93 memset(&pt, 0, sizeof(pt));
94 pt.target = target;
95 pt.format = format;
96 pt.last_level = last_level;
97 pt.width[0] = width0;
98 pt.height[0] = height0;
99 pt.depth[0] = depth0;
100 pt.compressed = compress_byte ? 1 : 0;
101 pt.cpp = pt.compressed ? compress_byte : st_sizeof_format(format);
102
103 newtex = screen->texture_create(screen, &pt);
104
105 assert(!newtex || newtex->refcount == 1);
106
107 return newtex;
108 }
109
110
111 /**
112 * Check if a texture image be pulled into a unified mipmap texture.
113 * This mirrors the completeness test in a lot of ways.
114 *
115 * Not sure whether I want to pass gl_texture_image here.
116 */
117 GLboolean
118 st_texture_match_image(const struct pipe_texture *pt,
119 const struct gl_texture_image *image,
120 GLuint face, GLuint level)
121 {
122 /* Images with borders are never pulled into mipmap textures.
123 */
124 if (image->Border)
125 return GL_FALSE;
126
127 if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format ||
128 image->IsCompressed != pt->compressed)
129 return GL_FALSE;
130
131 /* Test image dimensions against the base level image adjusted for
132 * minification. This will also catch images not present in the
133 * texture, changed targets, etc.
134 */
135 if (image->Width != pt->width[level] ||
136 image->Height != pt->height[level] ||
137 image->Depth != pt->depth[level])
138 return GL_FALSE;
139
140 return GL_TRUE;
141 }
142
143
144 #if 000
145 /* Although we use the image_offset[] array to store relative offsets
146 * to cube faces, Mesa doesn't know anything about this and expects
147 * each cube face to be treated as a separate image.
148 *
149 * These functions present that view to mesa:
150 */
151 const GLuint *
152 st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
153 {
154 static const GLuint zero = 0;
155
156 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
157 return &zero;
158 else
159 return pt->level[level].image_offset;
160 }
161
162
163 /**
164 * Return the offset to the given mipmap texture image within the
165 * texture memory buffer, in bytes.
166 */
167 GLuint
168 st_texture_image_offset(const struct pipe_texture * pt,
169 GLuint face, GLuint level)
170 {
171 if (pt->target == PIPE_TEXTURE_CUBE)
172 return (pt->level[level].level_offset +
173 pt->level[level].image_offset[face] * pt->cpp);
174 else
175 return pt->level[level].level_offset;
176 }
177 #endif
178
179
180 /**
181 * Map a teximage in a mipmap texture.
182 * \param row_stride returns row stride in bytes
183 * \param image_stride returns image stride in bytes (for 3D textures).
184 * \return address of mapping
185 */
186 GLubyte *
187 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
188 GLuint zoffset)
189 {
190 struct pipe_screen *screen = st->pipe->screen;
191 struct pipe_texture *pt = stImage->pt;
192 DBG("%s \n", __FUNCTION__);
193
194 stImage->surface = screen->get_tex_surface(screen, pt, stImage->face,
195 stImage->level, zoffset);
196
197 return pipe_surface_map(stImage->surface);
198 }
199
200
201 void
202 st_texture_image_unmap(struct st_texture_image *stImage)
203 {
204 DBG("%s\n", __FUNCTION__);
205
206 pipe_surface_unmap(stImage->surface);
207
208 pipe_surface_reference(&stImage->surface, NULL);
209 }
210
211
212
213 /**
214 * Upload data to a rectangular sub-region. Lots of choices how to do this:
215 *
216 * - memcpy by span to current destination
217 * - upload data as new buffer and blit
218 *
219 * Currently always memcpy.
220 */
221 static void
222 st_surface_data(struct pipe_context *pipe,
223 struct pipe_surface *dst,
224 unsigned dstx, unsigned dsty,
225 const void *src, unsigned src_pitch,
226 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
227 {
228 pipe_copy_rect(pipe_surface_map(dst),
229 dst->cpp,
230 dst->pitch,
231 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
232
233 pipe_surface_unmap(dst);
234 }
235
236
237 /* Upload data for a particular image.
238 */
239 void
240 st_texture_image_data(struct pipe_context *pipe,
241 struct pipe_texture *dst,
242 GLuint face,
243 GLuint level,
244 void *src,
245 GLuint src_row_pitch, GLuint src_image_pitch)
246 {
247 struct pipe_screen *screen = pipe->screen;
248 GLuint depth = dst->depth[level];
249 GLuint i;
250 GLuint height = 0;
251 const GLubyte *srcUB = src;
252 struct pipe_surface *dst_surface;
253
254 DBG("%s\n", __FUNCTION__);
255 for (i = 0; i < depth; i++) {
256 height = dst->height[level];
257 if(dst->compressed)
258 height /= 4;
259
260 dst_surface = screen->get_tex_surface(screen, dst, face, level, i);
261
262 st_surface_data(pipe, dst_surface,
263 0, 0, /* dstx, dsty */
264 srcUB,
265 src_row_pitch,
266 0, 0, /* source x, y */
267 dst->width[level], height); /* width, height */
268
269 pipe_surface_reference(&dst_surface, NULL);
270
271 srcUB += src_image_pitch * dst->cpp;
272 }
273 }
274
275
276 /* Copy mipmap image between textures
277 */
278 void
279 st_texture_image_copy(struct pipe_context *pipe,
280 struct pipe_texture *dst, GLuint dstLevel,
281 struct pipe_texture *src,
282 GLuint face)
283 {
284 struct pipe_screen *screen = pipe->screen;
285 GLuint width = dst->width[dstLevel];
286 GLuint height = dst->height[dstLevel];
287 GLuint depth = dst->depth[dstLevel];
288 struct pipe_surface *src_surface;
289 struct pipe_surface *dst_surface;
290 GLuint i;
291
292 /* XXX this is a hack */
293 if (dst->compressed)
294 height /= 4;
295
296 for (i = 0; i < depth; i++) {
297 GLuint srcLevel;
298
299 /* find src texture level of needed size */
300 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
301 if (src->width[srcLevel] == width &&
302 src->height[srcLevel] == height) {
303 break;
304 }
305 }
306 assert(src->width[srcLevel] == width);
307 assert(src->height[srcLevel] == height);
308
309 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i);
310 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i);
311
312 pipe->surface_copy(pipe,
313 FALSE,
314 dst_surface,
315 0, 0, /* destX, Y */
316 src_surface,
317 0, 0, /* srcX, Y */
318 width, height);
319
320 pipe_surface_reference(&dst_surface, NULL);
321 pipe_surface_reference(&src_surface, NULL);
322 }
323 }