intel / DRI2: When available, use DRI2GetBuffersWithFormat
[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 "st_inlines.h"
34 #include "main/enums.h"
35 #include "main/teximage.h"
36 #include "main/texstore.h"
37
38 #undef Elements /* fix re-defined macro warning */
39
40 #include "pipe/p_state.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_inlines.h"
44 #include "util/u_rect.h"
45
46
47 #define DBG if(0) printf
48
49 #if 0
50 static GLenum
51 target_to_target(GLenum target)
52 {
53 switch (target) {
54 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
55 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
56 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
57 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
58 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
59 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
60 return GL_TEXTURE_CUBE_MAP_ARB;
61 default:
62 return target;
63 }
64 }
65 #endif
66
67
68 /**
69 * Allocate a new pipe_texture object
70 * width0, height0, depth0 are the dimensions of the level 0 image
71 * (the highest resolution). last_level indicates how many mipmap levels
72 * to allocate storage for. For non-mipmapped textures, this will be zero.
73 */
74 struct pipe_texture *
75 st_texture_create(struct st_context *st,
76 enum pipe_texture_target target,
77 enum pipe_format format,
78 GLuint last_level,
79 GLuint width0,
80 GLuint height0,
81 GLuint depth0,
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 pf_get_block(format, &pt.block);
105 pt.tex_usage = usage;
106
107 newtex = screen->texture_create(screen, &pt);
108
109 assert(!newtex || pipe_is_referenced(&newtex->reference));
110
111 return newtex;
112 }
113
114
115 /**
116 * Check if a texture image can be pulled into a unified mipmap texture.
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 /* Check if this image's format matches the established texture's format.
129 */
130 if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format)
131 return GL_FALSE;
132
133 /* Test if this image's size matches what's expected in the
134 * established texture.
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, enum pipe_transfer_usage usage,
190 GLuint x, GLuint y, GLuint w, GLuint h)
191 {
192 struct pipe_context *pipe = st->pipe;
193 struct pipe_screen *screen = pipe->screen;
194 struct pipe_texture *pt = stImage->pt;
195
196 DBG("%s \n", __FUNCTION__);
197
198 stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face,
199 stImage->level, zoffset,
200 usage, x, y, w, h);
201
202 if (stImage->transfer)
203 return screen->transfer_map(screen, stImage->transfer);
204 else
205 return NULL;
206 }
207
208
209 void
210 st_texture_image_unmap(struct st_context *st,
211 struct st_texture_image *stImage)
212 {
213 struct pipe_screen *screen = st->pipe->screen;
214
215 DBG("%s\n", __FUNCTION__);
216
217 screen->transfer_unmap(screen, stImage->transfer);
218
219 screen->tex_transfer_destroy(stImage->transfer);
220 }
221
222
223
224 /**
225 * Upload data to a rectangular sub-region. Lots of choices how to do this:
226 *
227 * - memcpy by span to current destination
228 * - upload data as new buffer and blit
229 *
230 * Currently always memcpy.
231 */
232 static void
233 st_surface_data(struct pipe_context *pipe,
234 struct pipe_transfer *dst,
235 unsigned dstx, unsigned dsty,
236 const void *src, unsigned src_stride,
237 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
238 {
239 struct pipe_screen *screen = pipe->screen;
240 void *map = screen->transfer_map(screen, dst);
241
242 pipe_copy_rect(map,
243 &dst->block,
244 dst->stride,
245 dstx, dsty,
246 width, height,
247 src, src_stride,
248 srcx, srcy);
249
250 screen->transfer_unmap(screen, dst);
251 }
252
253
254 /* Upload data for a particular image.
255 */
256 void
257 st_texture_image_data(struct st_context *st,
258 struct pipe_texture *dst,
259 GLuint face,
260 GLuint level,
261 void *src,
262 GLuint src_row_stride, GLuint src_image_stride)
263 {
264 struct pipe_context *pipe = st->pipe;
265 struct pipe_screen *screen = pipe->screen;
266 GLuint depth = dst->depth[level];
267 GLuint i;
268 const GLubyte *srcUB = src;
269 struct pipe_transfer *dst_transfer;
270
271 DBG("%s\n", __FUNCTION__);
272
273 for (i = 0; i < depth; i++) {
274 dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i,
275 PIPE_TRANSFER_WRITE, 0, 0,
276 dst->width[level],
277 dst->height[level]);
278
279 st_surface_data(pipe, dst_transfer,
280 0, 0, /* dstx, dsty */
281 srcUB,
282 src_row_stride,
283 0, 0, /* source x, y */
284 dst->width[level], dst->height[level]); /* width, height */
285
286 screen->tex_transfer_destroy(dst_transfer);
287
288 srcUB += src_image_stride;
289 }
290 }
291
292
293 /* Copy mipmap image between textures
294 */
295 void
296 st_texture_image_copy(struct pipe_context *pipe,
297 struct pipe_texture *dst, GLuint dstLevel,
298 struct pipe_texture *src,
299 GLuint face)
300 {
301 struct pipe_screen *screen = pipe->screen;
302 GLuint width = dst->width[dstLevel];
303 GLuint height = dst->height[dstLevel];
304 GLuint depth = dst->depth[dstLevel];
305 struct pipe_surface *src_surface;
306 struct pipe_surface *dst_surface;
307 GLuint i;
308
309 for (i = 0; i < depth; i++) {
310 GLuint srcLevel;
311
312 /* find src texture level of needed size */
313 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
314 if (src->width[srcLevel] == width &&
315 src->height[srcLevel] == height) {
316 break;
317 }
318 }
319 assert(src->width[srcLevel] == width);
320 assert(src->height[srcLevel] == height);
321
322 #if 0
323 {
324 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
325 PIPE_BUFFER_USAGE_CPU_READ);
326 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
327 map += src_surface->width * src_surface->height * 4 / 2;
328 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
329 __FUNCTION__,
330 map[0], map[1], map[2], map[3],
331 src, srcLevel, dst, dstLevel);
332
333 screen->surface_unmap(screen, src_surface);
334 pipe_surface_reference(&src_surface, NULL);
335 }
336 #endif
337
338 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
339 PIPE_BUFFER_USAGE_GPU_WRITE);
340
341 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
342 PIPE_BUFFER_USAGE_GPU_READ);
343
344 pipe->surface_copy(pipe,
345 dst_surface,
346 0, 0, /* destX, Y */
347 src_surface,
348 0, 0, /* srcX, Y */
349 width, height);
350
351 pipe_surface_reference(&src_surface, NULL);
352 pipe_surface_reference(&dst_surface, NULL);
353 }
354 }
355
356 /** Bind a pipe surface for use as a texture image */
357 int
358 st_set_teximage(struct pipe_texture *pt, int target)
359 {
360 GET_CURRENT_CONTEXT(ctx);
361 const GLuint unit = ctx->Texture.CurrentUnit;
362 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
363 struct gl_texture_object *texObj;
364 struct gl_texture_image *texImage;
365 struct st_texture_image *stImage;
366 int internalFormat;
367
368 switch (pt->format) {
369 case PIPE_FORMAT_A8R8G8B8_UNORM:
370 internalFormat = GL_RGBA8;
371 break;
372 default:
373 return 0;
374 };
375
376 switch (target) {
377 case ST_TEXTURE_2D:
378 target = GL_TEXTURE_2D;
379 break;
380 case ST_TEXTURE_RECT:
381 target = GL_TEXTURE_RECTANGLE_ARB;
382 break;
383 default:
384 return 0;
385 }
386
387 texObj = _mesa_select_tex_object(ctx, texUnit, target);
388 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
389 stImage = st_texture_image(texImage);
390
391 _mesa_init_teximage_fields(ctx, GL_TEXTURE_2D, texImage, pt->width[0],
392 pt->height[0], 1, 0, internalFormat);
393
394 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, GL_RGBA,
395 GL_UNSIGNED_BYTE);
396 _mesa_set_fetch_functions(texImage, 2);
397
398 pipe_texture_reference(&stImage->pt, pt);
399
400 return 1;
401 }
402
403 /** Redirect rendering into stfb's surface to a texture image */
404 int
405 st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
406 int target, int format, int level)
407 {
408 GET_CURRENT_CONTEXT(ctx);
409 struct st_context *st = ctx->st;
410 struct pipe_context *pipe = st->pipe;
411 struct pipe_screen *screen = pipe->screen;
412 const GLuint unit = ctx->Texture.CurrentUnit;
413 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
414 struct gl_texture_object *texObj;
415 struct gl_texture_image *texImage;
416 struct st_texture_image *stImage;
417 struct st_renderbuffer *strb;
418 GLint face = 0, slice = 0;
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 if (target == ST_TEXTURE_2D) {
430 texObj = texUnit->CurrentTex[TEXTURE_2D_INDEX];
431 texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level);
432 stImage = st_texture_image(texImage);
433 }
434 else {
435 /* unsupported target */
436 return 0;
437 }
438
439 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
440
441 /* save the renderbuffer's surface/texture info */
442 pipe_texture_reference(&strb->texture_save, strb->texture);
443 pipe_surface_reference(&strb->surface_save, strb->surface);
444
445 /* plug in new surface/texture info */
446 pipe_texture_reference(&strb->texture, stImage->pt);
447 strb->surface = screen->get_tex_surface(screen, strb->texture,
448 face, level, slice,
449 (PIPE_BUFFER_USAGE_GPU_READ |
450 PIPE_BUFFER_USAGE_GPU_WRITE));
451
452 st->dirty.st |= ST_NEW_FRAMEBUFFER;
453
454 return 1;
455 }
456
457
458 /** Undo surface-to-texture binding */
459 int
460 st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
461 int target, int format, int level)
462 {
463 GET_CURRENT_CONTEXT(ctx);
464 struct st_context *st = ctx->st;
465 struct st_renderbuffer *strb;
466
467 assert(surfIndex <= ST_SURFACE_DEPTH);
468
469 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
470
471 if (!strb->texture_save || !strb->surface_save) {
472 /* Error! */
473 return 0;
474 }
475
476 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
477
478 /* free tex surface, restore original */
479 pipe_surface_reference(&strb->surface, strb->surface_save);
480 pipe_texture_reference(&strb->texture, strb->texture_save);
481
482 pipe_surface_reference(&strb->surface_save, NULL);
483 pipe_texture_reference(&strb->texture_save, NULL);
484
485 st->dirty.st |= ST_NEW_FRAMEBUFFER;
486
487 return 1;
488 }
489
490 void
491 st_teximage_flush_before_map(struct st_context *st,
492 struct pipe_texture *pt,
493 unsigned int face,
494 unsigned int level,
495 enum pipe_transfer_usage usage)
496 {
497 struct pipe_context *pipe = st->pipe;
498 unsigned referenced =
499 pipe->is_texture_referenced(pipe, pt, face, level);
500
501 if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
502 usage == PIPE_TRANSFER_WRITE ||
503 usage == PIPE_TRANSFER_READ_WRITE))
504 st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL);
505 }