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 {
80 struct pipe_texture pt, *newtex;
81 struct pipe_screen *screen = st->pipe->screen;
82
83 assert(target <= PIPE_TEXTURE_CUBE);
84
85 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
86 _mesa_lookup_enum_by_nr(target),
87 _mesa_lookup_enum_by_nr(format), last_level);
88
89 assert(format);
90 assert(screen->is_format_supported(screen, format, PIPE_TEXTURE));
91
92 memset(&pt, 0, sizeof(pt));
93 pt.target = target;
94 pt.format = format;
95 pt.last_level = last_level;
96 pt.width[0] = width0;
97 pt.height[0] = height0;
98 pt.depth[0] = depth0;
99 pt.compressed = compress_byte ? 1 : 0;
100 pt.cpp = pt.compressed ? compress_byte : st_sizeof_format(format);
101
102 newtex = screen->texture_create(screen, &pt);
103
104 assert(!newtex || newtex->refcount == 1);
105
106 return newtex;
107 }
108
109
110 /**
111 * Check if a texture image be pulled into a unified mipmap texture.
112 * This mirrors the completeness test in a lot of ways.
113 *
114 * Not sure whether I want to pass gl_texture_image here.
115 */
116 GLboolean
117 st_texture_match_image(const struct pipe_texture *pt,
118 const struct gl_texture_image *image,
119 GLuint face, GLuint level)
120 {
121 /* Images with borders are never pulled into mipmap textures.
122 */
123 if (image->Border)
124 return GL_FALSE;
125
126 if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format ||
127 image->IsCompressed != pt->compressed)
128 return GL_FALSE;
129
130 /* Test image dimensions against the base level image adjusted for
131 * minification. This will also catch images not present in the
132 * texture, changed targets, etc.
133 */
134 if (image->Width != pt->width[level] ||
135 image->Height != pt->height[level] ||
136 image->Depth != pt->depth[level])
137 return GL_FALSE;
138
139 return GL_TRUE;
140 }
141
142
143 #if 000
144 /* Although we use the image_offset[] array to store relative offsets
145 * to cube faces, Mesa doesn't know anything about this and expects
146 * each cube face to be treated as a separate image.
147 *
148 * These functions present that view to mesa:
149 */
150 const GLuint *
151 st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
152 {
153 static const GLuint zero = 0;
154
155 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
156 return &zero;
157 else
158 return pt->level[level].image_offset;
159 }
160
161
162 /**
163 * Return the offset to the given mipmap texture image within the
164 * texture memory buffer, in bytes.
165 */
166 GLuint
167 st_texture_image_offset(const struct pipe_texture * pt,
168 GLuint face, GLuint level)
169 {
170 if (pt->target == PIPE_TEXTURE_CUBE)
171 return (pt->level[level].level_offset +
172 pt->level[level].image_offset[face] * pt->cpp);
173 else
174 return pt->level[level].level_offset;
175 }
176 #endif
177
178
179 /**
180 * Map a teximage in a mipmap texture.
181 * \param row_stride returns row stride in bytes
182 * \param image_stride returns image stride in bytes (for 3D textures).
183 * \return address of mapping
184 */
185 GLubyte *
186 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
187 GLuint zoffset,
188 GLuint flags )
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 flags);
197
198 return screen->surface_map(screen, stImage->surface, flags);
199 }
200
201
202 void
203 st_texture_image_unmap(struct st_context *st,
204 struct st_texture_image *stImage)
205 {
206 struct pipe_screen *screen = st->pipe->screen;
207
208 DBG("%s\n", __FUNCTION__);
209
210 screen->surface_unmap(screen, stImage->surface);
211
212 pipe_surface_reference(&stImage->surface, NULL);
213 }
214
215
216
217 /**
218 * Upload data to a rectangular sub-region. Lots of choices how to do this:
219 *
220 * - memcpy by span to current destination
221 * - upload data as new buffer and blit
222 *
223 * Currently always memcpy.
224 */
225 static void
226 st_surface_data(struct pipe_context *pipe,
227 struct pipe_surface *dst,
228 unsigned dstx, unsigned dsty,
229 const void *src, unsigned src_pitch,
230 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
231 {
232 struct pipe_screen *screen = pipe->screen;
233 void *map = screen->surface_map(screen, dst, PIPE_BUFFER_USAGE_CPU_WRITE);
234
235 pipe_copy_rect(map,
236 dst->cpp,
237 dst->pitch,
238 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
239
240 screen->surface_unmap(screen, dst);
241 }
242
243
244 /* Upload data for a particular image.
245 */
246 void
247 st_texture_image_data(struct pipe_context *pipe,
248 struct pipe_texture *dst,
249 GLuint face,
250 GLuint level,
251 void *src,
252 GLuint src_row_pitch, GLuint src_image_pitch)
253 {
254 struct pipe_screen *screen = pipe->screen;
255 GLuint depth = dst->depth[level];
256 GLuint i;
257 GLuint height = 0;
258 const GLubyte *srcUB = src;
259 struct pipe_surface *dst_surface;
260
261 DBG("%s\n", __FUNCTION__);
262 for (i = 0; i < depth; i++) {
263 height = dst->height[level];
264 if(dst->compressed)
265 height /= 4;
266
267 dst_surface = screen->get_tex_surface(screen, dst, face, level, i,
268 PIPE_BUFFER_USAGE_CPU_WRITE);
269
270 st_surface_data(pipe, dst_surface,
271 0, 0, /* dstx, dsty */
272 srcUB,
273 src_row_pitch,
274 0, 0, /* source x, y */
275 dst->width[level], height); /* width, height */
276
277 screen->tex_surface_release(screen, &dst_surface);
278
279 srcUB += src_image_pitch * dst->cpp;
280 }
281 }
282
283
284 /* Copy mipmap image between textures
285 */
286 void
287 st_texture_image_copy(struct pipe_context *pipe,
288 struct pipe_texture *dst, GLuint dstLevel,
289 struct pipe_texture *src,
290 GLuint face)
291 {
292 struct pipe_screen *screen = pipe->screen;
293 GLuint width = dst->width[dstLevel];
294 GLuint height = dst->height[dstLevel];
295 GLuint depth = dst->depth[dstLevel];
296 struct pipe_surface *src_surface;
297 struct pipe_surface *dst_surface;
298 GLuint i;
299
300 /* XXX this is a hack */
301 const GLuint copyHeight = dst->compressed ? height / 4 : height;
302
303 for (i = 0; i < depth; i++) {
304 GLuint srcLevel;
305
306 /* find src texture level of needed size */
307 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
308 if (src->width[srcLevel] == width &&
309 src->height[srcLevel] == height) {
310 break;
311 }
312 }
313 assert(src->width[srcLevel] == width);
314 assert(src->height[srcLevel] == height);
315
316 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
317 PIPE_BUFFER_USAGE_GPU_WRITE);
318
319 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
320 PIPE_BUFFER_USAGE_GPU_READ);
321
322 pipe->surface_copy(pipe,
323 FALSE,
324 dst_surface,
325 0, 0, /* destX, Y */
326 src_surface,
327 0, 0, /* srcX, Y */
328 width, copyHeight);
329
330 screen->tex_surface_release(screen, &src_surface);
331 screen->tex_surface_release(screen, &dst_surface);
332 }
333 }