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