Merge commit 'origin/master' into gallium-0.2
[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_public.h"
31 #include "st_texture.h"
32 #include "st_cb_fbo.h"
33 #include "main/enums.h"
34 #include "main/teximage.h"
35
36 #undef Elements /* fix re-defined macro warning */
37
38 #include "pipe/p_state.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_inlines.h"
42 #include "pipe/p_inlines.h"
43 #include "util/u_rect.h"
44
45
46 #define DBG if(0) printf
47
48 #if 0
49 static GLenum
50 target_to_target(GLenum target)
51 {
52 switch (target) {
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
55 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
56 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
57 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
58 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
59 return GL_TEXTURE_CUBE_MAP_ARB;
60 default:
61 return target;
62 }
63 }
64 #endif
65
66
67 /**
68 * Allocate a new pipe_texture object
69 * width0, height0, depth0 are the dimensions of the level 0 image
70 * (the highest resolution). last_level indicates how many mipmap levels
71 * to allocate storage for. For non-mipmapped textures, this will be zero.
72 */
73 struct pipe_texture *
74 st_texture_create(struct st_context *st,
75 enum pipe_texture_target target,
76 enum pipe_format format,
77 GLuint last_level,
78 GLuint width0,
79 GLuint height0,
80 GLuint depth0,
81 GLuint compress_byte,
82 GLuint usage )
83 {
84 struct pipe_texture pt, *newtex;
85 struct pipe_screen *screen = st->pipe->screen;
86
87 assert(target <= PIPE_TEXTURE_CUBE);
88
89 DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
90 _mesa_lookup_enum_by_nr(target),
91 _mesa_lookup_enum_by_nr(format), last_level);
92
93 assert(format);
94 assert(screen->is_format_supported(screen, format, target,
95 PIPE_TEXTURE_USAGE_SAMPLER, 0));
96
97 memset(&pt, 0, sizeof(pt));
98 pt.target = target;
99 pt.format = format;
100 pt.last_level = last_level;
101 pt.width[0] = width0;
102 pt.height[0] = height0;
103 pt.depth[0] = depth0;
104 pt.compressed = compress_byte ? 1 : 0;
105 pf_get_block(format, &pt.block);
106 pt.tex_usage = usage;
107
108 newtex = screen->texture_create(screen, &pt);
109
110 assert(!newtex || newtex->refcount == 1);
111
112 return newtex;
113 }
114
115
116 /**
117 * Check if a texture image be pulled into a unified mipmap texture.
118 * This mirrors the completeness test in a lot of ways.
119 *
120 * Not sure whether I want to pass gl_texture_image here.
121 */
122 GLboolean
123 st_texture_match_image(const struct pipe_texture *pt,
124 const struct gl_texture_image *image,
125 GLuint face, GLuint level)
126 {
127 /* Images with borders are never pulled into mipmap textures.
128 */
129 if (image->Border)
130 return GL_FALSE;
131
132 if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format ||
133 image->IsCompressed != pt->compressed)
134 return GL_FALSE;
135
136 /* Test image dimensions against the base level image adjusted for
137 * minification. This will also catch images not present in the
138 * texture, changed targets, etc.
139 */
140 if (image->Width != pt->width[level] ||
141 image->Height != pt->height[level] ||
142 image->Depth != pt->depth[level])
143 return GL_FALSE;
144
145 return GL_TRUE;
146 }
147
148
149 #if 000
150 /* Although we use the image_offset[] array to store relative offsets
151 * to cube faces, Mesa doesn't know anything about this and expects
152 * each cube face to be treated as a separate image.
153 *
154 * These functions present that view to mesa:
155 */
156 const GLuint *
157 st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
158 {
159 static const GLuint zero = 0;
160
161 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
162 return &zero;
163 else
164 return pt->level[level].image_offset;
165 }
166
167
168 /**
169 * Return the offset to the given mipmap texture image within the
170 * texture memory buffer, in bytes.
171 */
172 GLuint
173 st_texture_image_offset(const struct pipe_texture * pt,
174 GLuint face, GLuint level)
175 {
176 if (pt->target == PIPE_TEXTURE_CUBE)
177 return (pt->level[level].level_offset +
178 pt->level[level].image_offset[face] * pt->cpp);
179 else
180 return pt->level[level].level_offset;
181 }
182 #endif
183
184
185 /**
186 * Map a teximage in a mipmap texture.
187 * \param row_stride returns row stride in bytes
188 * \param image_stride returns image stride in bytes (for 3D textures).
189 * \return address of mapping
190 */
191 GLubyte *
192 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
193 GLuint zoffset,
194 GLuint flags )
195 {
196 struct pipe_screen *screen = st->pipe->screen;
197 struct pipe_texture *pt = stImage->pt;
198 DBG("%s \n", __FUNCTION__);
199
200 stImage->surface = screen->get_tex_surface(screen, pt, stImage->face,
201 stImage->level, zoffset,
202 flags);
203
204 if (stImage->surface)
205 return screen->surface_map(screen, stImage->surface, flags);
206 else
207 return NULL;
208 }
209
210
211 void
212 st_texture_image_unmap(struct st_context *st,
213 struct st_texture_image *stImage)
214 {
215 struct pipe_screen *screen = st->pipe->screen;
216
217 DBG("%s\n", __FUNCTION__);
218
219 screen->surface_unmap(screen, stImage->surface);
220
221 pipe_surface_reference(&stImage->surface, NULL);
222 }
223
224
225
226 /**
227 * Upload data to a rectangular sub-region. Lots of choices how to do this:
228 *
229 * - memcpy by span to current destination
230 * - upload data as new buffer and blit
231 *
232 * Currently always memcpy.
233 */
234 static void
235 st_surface_data(struct pipe_context *pipe,
236 struct pipe_surface *dst,
237 unsigned dstx, unsigned dsty,
238 const void *src, unsigned src_stride,
239 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
240 {
241 struct pipe_screen *screen = pipe->screen;
242 void *map = screen->surface_map(screen, dst, PIPE_BUFFER_USAGE_CPU_WRITE);
243
244 pipe_copy_rect(map,
245 &dst->block,
246 dst->stride,
247 dstx, dsty,
248 width, height,
249 src, src_stride,
250 srcx, srcy);
251
252 screen->surface_unmap(screen, dst);
253 }
254
255
256 /* Upload data for a particular image.
257 */
258 void
259 st_texture_image_data(struct pipe_context *pipe,
260 struct pipe_texture *dst,
261 GLuint face,
262 GLuint level,
263 void *src,
264 GLuint src_row_stride, GLuint src_image_stride)
265 {
266 struct pipe_screen *screen = pipe->screen;
267 GLuint depth = dst->depth[level];
268 GLuint i;
269 const GLubyte *srcUB = src;
270 struct pipe_surface *dst_surface;
271
272 DBG("%s\n", __FUNCTION__);
273 for (i = 0; i < depth; i++) {
274 dst_surface = screen->get_tex_surface(screen, dst, face, level, i,
275 PIPE_BUFFER_USAGE_CPU_WRITE);
276
277 st_surface_data(pipe, dst_surface,
278 0, 0, /* dstx, dsty */
279 srcUB,
280 src_row_stride,
281 0, 0, /* source x, y */
282 dst->width[level], dst->height[level]); /* width, height */
283
284 screen->tex_surface_release(screen, &dst_surface);
285
286 srcUB += src_image_stride;
287 }
288 }
289
290
291 /* Copy mipmap image between textures
292 */
293 void
294 st_texture_image_copy(struct pipe_context *pipe,
295 struct pipe_texture *dst, GLuint dstLevel,
296 struct pipe_texture *src,
297 GLuint face)
298 {
299 struct pipe_screen *screen = pipe->screen;
300 GLuint width = dst->width[dstLevel];
301 GLuint height = dst->height[dstLevel];
302 GLuint depth = dst->depth[dstLevel];
303 struct pipe_surface *src_surface;
304 struct pipe_surface *dst_surface;
305 GLuint i;
306
307 for (i = 0; i < depth; i++) {
308 GLuint srcLevel;
309
310 /* find src texture level of needed size */
311 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
312 if (src->width[srcLevel] == width &&
313 src->height[srcLevel] == height) {
314 break;
315 }
316 }
317 assert(src->width[srcLevel] == width);
318 assert(src->height[srcLevel] == height);
319
320 #if 0
321 {
322 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
323 PIPE_BUFFER_USAGE_CPU_READ);
324 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
325 map += src_surface->width * src_surface->height * 4 / 2;
326 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
327 __FUNCTION__,
328 map[0], map[1], map[2], map[3],
329 src, srcLevel, dst, dstLevel);
330
331 screen->surface_unmap(screen, src_surface);
332 pipe_surface_reference(&src_surface, NULL);
333 }
334 #endif
335
336 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
337 PIPE_BUFFER_USAGE_GPU_WRITE);
338
339 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
340 PIPE_BUFFER_USAGE_GPU_READ);
341
342 pipe->surface_copy(pipe,
343 FALSE,
344 dst_surface,
345 0, 0, /* destX, Y */
346 src_surface,
347 0, 0, /* srcX, Y */
348 width, height);
349
350 screen->tex_surface_release(screen, &src_surface);
351 screen->tex_surface_release(screen, &dst_surface);
352 }
353 }
354
355
356 /** Redirect rendering into stfb's surface to a texture image */
357 int
358 st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
359 int target, int format, int level)
360 {
361 GET_CURRENT_CONTEXT(ctx);
362 struct st_context *st = ctx->st;
363 struct pipe_context *pipe = st->pipe;
364 struct pipe_screen *screen = pipe->screen;
365 const GLuint unit = ctx->Texture.CurrentUnit;
366 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
367 struct gl_texture_object *texObj;
368 struct gl_texture_image *texImage;
369 struct st_texture_image *stImage;
370 struct st_renderbuffer *strb;
371 GLint face = 0, slice = 0;
372
373 assert(surfIndex <= ST_SURFACE_DEPTH);
374
375 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
376
377 if (strb->texture_save || strb->surface_save) {
378 /* Error! */
379 return 0;
380 }
381
382 if (target == ST_TEXTURE_2D) {
383 texObj = texUnit->Current2D;
384 texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level);
385 stImage = st_texture_image(texImage);
386 }
387 else {
388 /* unsupported target */
389 return 0;
390 }
391
392 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
393
394 /* save the renderbuffer's surface/texture info */
395 pipe_texture_reference(&strb->texture_save, strb->texture);
396 pipe_surface_reference(&strb->surface_save, strb->surface);
397
398 /* plug in new surface/texture info */
399 pipe_texture_reference(&strb->texture, stImage->pt);
400 strb->surface = screen->get_tex_surface(screen, strb->texture,
401 face, level, slice,
402 (PIPE_BUFFER_USAGE_GPU_READ |
403 PIPE_BUFFER_USAGE_GPU_WRITE));
404
405 st->dirty.st |= ST_NEW_FRAMEBUFFER;
406
407 return 1;
408 }
409
410
411 /** Undo surface-to-texture binding */
412 int
413 st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
414 int target, int format, int level)
415 {
416 GET_CURRENT_CONTEXT(ctx);
417 struct st_context *st = ctx->st;
418 struct st_renderbuffer *strb;
419
420 assert(surfIndex <= ST_SURFACE_DEPTH);
421
422 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
423
424 if (!strb->texture_save || !strb->surface_save) {
425 /* Error! */
426 return 0;
427 }
428
429 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
430
431 /* free tex surface, restore original */
432 pipe_surface_reference(&strb->surface, strb->surface_save);
433 pipe_texture_reference(&strb->texture, strb->texture_save);
434
435 pipe_surface_reference(&strb->surface_save, NULL);
436 pipe_texture_reference(&strb->texture_save, NULL);
437
438 st->dirty.st |= ST_NEW_FRAMEBUFFER;
439
440 return 1;
441 }