fd801a354b15ba055bec666f8416040e26d54053
[mesa.git] / src / mesa / state_tracker / st_cb_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 "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #if FEATURE_convolve
31 #include "main/convolve.h"
32 #endif
33 #include "main/enums.h"
34 #include "main/fbobject.h"
35 #include "main/formats.h"
36 #include "main/image.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
46
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_inlines.h"
55 #include "state_tracker/st_atom.h"
56
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67
68
69 #define DBG if (0) printf
70
71
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target)
74 {
75 switch (target) {
76 case GL_TEXTURE_1D:
77 return PIPE_TEXTURE_1D;
78
79 case GL_TEXTURE_2D:
80 case GL_TEXTURE_RECTANGLE_NV:
81 return PIPE_TEXTURE_2D;
82
83 case GL_TEXTURE_3D:
84 return PIPE_TEXTURE_3D;
85
86 case GL_TEXTURE_CUBE_MAP_ARB:
87 return PIPE_TEXTURE_CUBE;
88
89 default:
90 assert(0);
91 return 0;
92 }
93 }
94
95
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image *
98 st_NewTextureImage(GLcontext * ctx)
99 {
100 DBG("%s\n", __FUNCTION__);
101 (void) ctx;
102 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
103 }
104
105
106 /** called via ctx->Driver.NewTextureObject() */
107 static struct gl_texture_object *
108 st_NewTextureObject(GLcontext * ctx, GLuint name, GLenum target)
109 {
110 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
111
112 DBG("%s\n", __FUNCTION__);
113 _mesa_initialize_texture_object(&obj->base, name, target);
114
115 return &obj->base;
116 }
117
118 /** called via ctx->Driver.DeleteTextureImage() */
119 static void
120 st_DeleteTextureObject(GLcontext *ctx,
121 struct gl_texture_object *texObj)
122 {
123 struct st_context *st = st_context(ctx);
124 struct st_texture_object *stObj = st_texture_object(texObj);
125 if (stObj->pt)
126 pipe_resource_reference(&stObj->pt, NULL);
127 if (stObj->sampler_view) {
128 if (stObj->sampler_view->context != st->pipe) {
129 /* Take "ownership" of this texture sampler view by setting
130 * its context pointer to this context. This avoids potential
131 * crashes when the texture object is shared among contexts
132 * and the original/owner context has already been destroyed.
133 */
134 stObj->sampler_view->context = st->pipe;
135 }
136 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
137 }
138 _mesa_delete_texture_object(ctx, texObj);
139 }
140
141
142 /** called via ctx->Driver.FreeTexImageData() */
143 static void
144 st_FreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage)
145 {
146 struct st_texture_image *stImage = st_texture_image(texImage);
147
148 DBG("%s\n", __FUNCTION__);
149
150 if (stImage->pt) {
151 pipe_resource_reference(&stImage->pt, NULL);
152 }
153
154 if (texImage->Data) {
155 _mesa_align_free(texImage->Data);
156 texImage->Data = NULL;
157 }
158 }
159
160
161 /**
162 * From linux kernel i386 header files, copes with odd sizes better
163 * than COPY_DWORDS would:
164 * XXX Put this in src/mesa/main/imports.h ???
165 */
166 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
167 static INLINE void *
168 __memcpy(void *to, const void *from, size_t n)
169 {
170 int d0, d1, d2;
171 __asm__ __volatile__("rep ; movsl\n\t"
172 "testb $2,%b4\n\t"
173 "je 1f\n\t"
174 "movsw\n"
175 "1:\ttestb $1,%b4\n\t"
176 "je 2f\n\t"
177 "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2)
178 :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from)
179 :"memory");
180 return (to);
181 }
182 #else
183 #define __memcpy(a,b,c) memcpy(a,b,c)
184 #endif
185
186
187 /**
188 * The system memcpy (at least on ubuntu 5.10) has problems copying
189 * to agp (writecombined) memory from a source which isn't 64-byte
190 * aligned - there is a 4x performance falloff.
191 *
192 * The x86 __memcpy is immune to this but is slightly slower
193 * (10%-ish) than the system memcpy.
194 *
195 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
196 * isn't much faster than x86_memcpy for agp copies.
197 *
198 * TODO: switch dynamically.
199 */
200 static void *
201 do_memcpy(void *dest, const void *src, size_t n)
202 {
203 if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) {
204 return __memcpy(dest, src, n);
205 }
206 else
207 return memcpy(dest, src, n);
208 }
209
210
211 /**
212 * Return default texture resource binding bitmask for the given format.
213 */
214 static GLuint
215 default_bindings(struct st_context *st, enum pipe_format format)
216 {
217 struct pipe_screen *screen = st->pipe->screen;
218 const unsigned target = PIPE_TEXTURE_2D;
219 const unsigned geom = 0x0;
220 unsigned bindings;
221
222 if (util_format_is_depth_or_stencil(format))
223 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
224 else
225 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
226
227 if (screen->is_format_supported(screen, format, target, bindings, geom))
228 return bindings;
229 else
230 return PIPE_BIND_SAMPLER_VIEW;
231 }
232
233
234 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
235 static GLuint
236 get_texture_dims(GLenum target)
237 {
238 switch (target) {
239 case GL_TEXTURE_1D:
240 case GL_TEXTURE_1D_ARRAY_EXT:
241 return 1;
242 case GL_TEXTURE_2D:
243 case GL_TEXTURE_CUBE_MAP_ARB:
244 case GL_TEXTURE_RECTANGLE_NV:
245 case GL_TEXTURE_2D_ARRAY_EXT:
246 return 2;
247 case GL_TEXTURE_3D:
248 return 3;
249 default:
250 assert(0 && "invalid texture target in get_texture_dims()");
251 return 1;
252 }
253 }
254
255
256 /**
257 * Try to allocate a pipe_resource object for the given st_texture_object.
258 *
259 * We use the given st_texture_image as a clue to determine the size of the
260 * mipmap image at level=0.
261 *
262 */
263 static void
264 guess_and_alloc_texture(struct st_context *st,
265 struct st_texture_object *stObj,
266 const struct st_texture_image *stImage)
267 {
268 const GLuint dims = get_texture_dims(stObj->base.Target);
269 GLuint level, lastLevel, width, height, depth;
270 GLuint bindings;
271 enum pipe_format fmt;
272
273 DBG("%s\n", __FUNCTION__);
274
275 assert(!stObj->pt);
276
277 level = stImage->level;
278 width = stImage->base.Width2; /* size w/out border */
279 height = stImage->base.Height2;
280 depth = stImage->base.Depth2;
281
282 assert(width > 0);
283 assert(height > 0);
284 assert(depth > 0);
285
286 /* Depending on the image's size, we can't always make a guess here.
287 */
288 if (level > 0) {
289 if ( (dims >= 1 && width == 1) ||
290 (dims >= 2 && height == 1) ||
291 (dims >= 3 && depth == 1) ) {
292 /* we can't determine the image size at level=0 */
293 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
294 return;
295 }
296 }
297
298 /* grow the image size until we hit level = 0 */
299 while (level > 0) {
300 if (width != 1)
301 width <<= 1;
302 if (height != 1)
303 height <<= 1;
304 if (depth != 1)
305 depth <<= 1;
306 level--;
307 }
308
309 assert(level == 0);
310
311 /* At this point, (width x height x depth) is the expected size of
312 * the level=0 mipmap image.
313 */
314
315 /* Guess a reasonable value for lastLevel. This is probably going
316 * to be wrong fairly often and might mean that we have to look at
317 * resizable buffers, or require that buffers implement lazy
318 * pagetable arrangements.
319 */
320 if ((stObj->base.MinFilter == GL_NEAREST ||
321 stObj->base.MinFilter == GL_LINEAR ||
322 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
323 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
324 !stObj->base.GenerateMipmap &&
325 stImage->level == 0) {
326 /* only alloc space for a single mipmap level */
327 lastLevel = 0;
328 }
329 else {
330 /* alloc space for a full mipmap */
331 GLuint l2width = util_logbase2(width);
332 GLuint l2height = util_logbase2(height);
333 GLuint l2depth = util_logbase2(depth);
334 lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
335 }
336
337 /* Save the level=0 dimensions */
338 stObj->width0 = width;
339 stObj->height0 = height;
340 stObj->depth0 = depth;
341
342 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
343
344 bindings = default_bindings(st, fmt);
345
346 stObj->pt = st_texture_create(st,
347 gl_target_to_pipe(stObj->base.Target),
348 fmt,
349 lastLevel,
350 width,
351 height,
352 depth,
353 bindings);
354
355 DBG("%s - success\n", __FUNCTION__);
356 }
357
358
359 /**
360 * Adjust pixel unpack params and image dimensions to strip off the
361 * texture border.
362 * Gallium doesn't support texture borders. They've seldem been used
363 * and seldom been implemented correctly anyway.
364 * \param unpackNew returns the new pixel unpack parameters
365 */
366 static void
367 strip_texture_border(GLint border,
368 GLint *width, GLint *height, GLint *depth,
369 const struct gl_pixelstore_attrib *unpack,
370 struct gl_pixelstore_attrib *unpackNew)
371 {
372 assert(border > 0); /* sanity check */
373
374 *unpackNew = *unpack;
375
376 if (unpackNew->RowLength == 0)
377 unpackNew->RowLength = *width;
378
379 if (depth && unpackNew->ImageHeight == 0)
380 unpackNew->ImageHeight = *height;
381
382 unpackNew->SkipPixels += border;
383 if (height)
384 unpackNew->SkipRows += border;
385 if (depth)
386 unpackNew->SkipImages += border;
387
388 assert(*width >= 3);
389 *width = *width - 2 * border;
390 if (height && *height >= 3)
391 *height = *height - 2 * border;
392 if (depth && *depth >= 3)
393 *depth = *depth - 2 * border;
394 }
395
396
397 /**
398 * Try to do texture compression via rendering. If the Gallium driver
399 * can render into a compressed surface this will allow us to do texture
400 * compression.
401 * \return GL_TRUE for success, GL_FALSE for failure
402 */
403 static GLboolean
404 compress_with_blit(GLcontext * ctx,
405 GLenum target, GLint level,
406 GLint xoffset, GLint yoffset, GLint zoffset,
407 GLint width, GLint height, GLint depth,
408 GLenum format, GLenum type, const void *pixels,
409 const struct gl_pixelstore_attrib *unpack,
410 struct gl_texture_image *texImage)
411 {
412 const GLuint dstImageOffsets[1] = {0};
413 struct st_texture_image *stImage = st_texture_image(texImage);
414 struct st_context *st = st_context(ctx);
415 struct pipe_context *pipe = st->pipe;
416 struct pipe_screen *screen = pipe->screen;
417 gl_format mesa_format;
418 struct pipe_resource templ;
419 struct pipe_resource *src_tex;
420 struct pipe_sampler_view view_templ;
421 struct pipe_sampler_view *src_view;
422 struct pipe_surface *dst_surface;
423 struct pipe_transfer *tex_xfer;
424 void *map;
425
426 if (!stImage->pt) {
427 /* XXX: Can this happen? Should we assert? */
428 return GL_FALSE;
429 }
430
431 /* get destination surface (in the compressed texture) */
432 dst_surface = screen->get_tex_surface(screen, stImage->pt,
433 stImage->face, stImage->level, 0,
434 PIPE_BIND_BLIT_DESTINATION);
435 if (!dst_surface) {
436 /* can't render into this format (or other problem) */
437 return GL_FALSE;
438 }
439
440 /* Choose format for the temporary RGBA texture image.
441 */
442 mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type);
443 assert(mesa_format);
444 if (!mesa_format)
445 return GL_FALSE;
446
447 /* Create the temporary source texture
448 */
449 memset(&templ, 0, sizeof(templ));
450 templ.target = PIPE_TEXTURE_2D;
451 templ.format = st_mesa_format_to_pipe_format(mesa_format);
452 templ.width0 = width;
453 templ.height0 = height;
454 templ.depth0 = 1;
455 templ.last_level = 0;
456 templ.usage = PIPE_USAGE_DEFAULT;
457 templ.bind = PIPE_BIND_SAMPLER_VIEW;
458 src_tex = screen->resource_create(screen, &templ);
459
460 if (!src_tex)
461 return GL_FALSE;
462
463 /* Put user's tex data into the temporary texture
464 */
465 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx), src_tex,
466 0, 0, 0, /* face, level are zero */
467 PIPE_TRANSFER_WRITE,
468 0, 0, width, height); /* x, y, w, h */
469 map = pipe_transfer_map(pipe, tex_xfer);
470
471 _mesa_texstore(ctx, 2, GL_RGBA, mesa_format,
472 map, /* dest ptr */
473 0, 0, 0, /* dest x/y/z offset */
474 tex_xfer->stride, /* dest row stride (bytes) */
475 dstImageOffsets, /* image offsets (for 3D only) */
476 width, height, 1, /* size */
477 format, type, /* source format/type */
478 pixels, /* source data */
479 unpack); /* source data packing */
480
481 pipe_transfer_unmap(pipe, tex_xfer);
482 pipe->transfer_destroy(pipe, tex_xfer);
483
484 /* Create temporary sampler view */
485 u_sampler_view_default_template(&view_templ,
486 src_tex,
487 src_tex->format);
488 src_view = pipe->create_sampler_view(pipe, src_tex, &view_templ);
489
490
491 /* copy / compress image */
492 util_blit_pixels_tex(st->blit,
493 src_view, /* sampler view (src) */
494 0, 0, /* src x0, y0 */
495 width, height, /* src x1, y1 */
496 dst_surface, /* pipe_surface (dst) */
497 xoffset, yoffset, /* dst x0, y0 */
498 xoffset + width, /* dst x1 */
499 yoffset + height, /* dst y1 */
500 0.0, /* z */
501 PIPE_TEX_MIPFILTER_NEAREST);
502
503 pipe_surface_reference(&dst_surface, NULL);
504 pipe_resource_reference(&src_tex, NULL);
505 pipe_sampler_view_reference(&src_view, NULL);
506
507 return GL_TRUE;
508 }
509
510
511 /**
512 * Do glTexImage1/2/3D().
513 */
514 static void
515 st_TexImage(GLcontext * ctx,
516 GLint dims,
517 GLenum target, GLint level,
518 GLint internalFormat,
519 GLint width, GLint height, GLint depth,
520 GLint border,
521 GLenum format, GLenum type, const void *pixels,
522 const struct gl_pixelstore_attrib *unpack,
523 struct gl_texture_object *texObj,
524 struct gl_texture_image *texImage,
525 GLsizei imageSize, GLboolean compressed_src)
526 {
527 struct st_context *st = st_context(ctx);
528 struct pipe_screen *screen = st->pipe->screen;
529 struct st_texture_object *stObj = st_texture_object(texObj);
530 struct st_texture_image *stImage = st_texture_image(texImage);
531 GLint postConvWidth, postConvHeight;
532 GLint texelBytes, sizeInBytes;
533 GLuint dstRowStride = 0;
534 struct gl_pixelstore_attrib unpackNB;
535 enum pipe_transfer_usage transfer_usage = 0;
536
537 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
538 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
539
540 /* switch to "normal" */
541 if (stObj->surface_based) {
542 _mesa_clear_texture_object(ctx, texObj);
543 stObj->surface_based = GL_FALSE;
544 }
545
546 /* gallium does not support texture borders, strip it off */
547 if (border) {
548 strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB);
549 unpack = &unpackNB;
550 texImage->Width = width;
551 texImage->Height = height;
552 texImage->Depth = depth;
553 texImage->Border = 0;
554 border = 0;
555 }
556
557 postConvWidth = width;
558 postConvHeight = height;
559
560 stImage->face = _mesa_tex_target_to_face(target);
561 stImage->level = level;
562
563 #if FEATURE_convolve
564 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
565 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
566 &postConvHeight);
567 }
568 #endif
569
570 _mesa_set_fetch_functions(texImage, dims);
571
572 if (_mesa_is_format_compressed(texImage->TexFormat)) {
573 /* must be a compressed format */
574 texelBytes = 0;
575 }
576 else {
577 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
578
579 /* Minimum pitch of 32 bytes */
580 if (postConvWidth * texelBytes < 32) {
581 postConvWidth = 32 / texelBytes;
582 texImage->RowStride = postConvWidth;
583 }
584
585 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
586 /*assert(texImage->RowStride == postConvWidth);*/
587 }
588
589 /* Release the reference to a potentially orphaned buffer.
590 * Release any old malloced memory.
591 */
592 if (stImage->pt) {
593 pipe_resource_reference(&stImage->pt, NULL);
594 assert(!texImage->Data);
595 }
596 else if (texImage->Data) {
597 _mesa_align_free(texImage->Data);
598 }
599
600 /*
601 * See if the new image is somehow incompatible with the existing
602 * mipmap. If so, free the old mipmap.
603 */
604 if (stObj->pt) {
605 if (level > (GLint) stObj->pt->last_level ||
606 !st_texture_match_image(stObj->pt, &stImage->base,
607 stImage->face, stImage->level)) {
608 DBG("release it\n");
609 pipe_resource_reference(&stObj->pt, NULL);
610 assert(!stObj->pt);
611 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
612 }
613 }
614
615 if (width == 0 || height == 0 || depth == 0) {
616 /* stop after freeing old image */
617 return;
618 }
619
620 if (!stObj->pt) {
621 guess_and_alloc_texture(st, stObj, stImage);
622 if (!stObj->pt) {
623 /* Probably out of memory.
624 * Try flushing any pending rendering, then retry.
625 */
626 st_finish(st);
627 guess_and_alloc_texture(st, stObj, stImage);
628 if (!stObj->pt) {
629 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
630 return;
631 }
632 }
633 }
634
635 assert(!stImage->pt);
636
637 if (stObj->pt &&
638 st_texture_match_image(stObj->pt, &stImage->base,
639 stImage->face, stImage->level)) {
640
641 pipe_resource_reference(&stImage->pt, stObj->pt);
642 assert(stImage->pt);
643 }
644
645 if (!stImage->pt)
646 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
647
648 /* st_CopyTexImage calls this function with pixels == NULL, with
649 * the expectation that the texture will be set up but nothing
650 * more will be done. This is where those calls return:
651 */
652 if (compressed_src) {
653 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
654 unpack,
655 "glCompressedTexImage");
656 }
657 else {
658 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
659 format, type,
660 pixels, unpack, "glTexImage");
661 }
662
663 /* Note: we can't check for pixels==NULL until after we've allocated
664 * memory for the texture.
665 */
666
667 /* See if we can do texture compression with a blit/render.
668 */
669 if (!compressed_src &&
670 !ctx->Mesa_DXTn &&
671 _mesa_is_format_compressed(texImage->TexFormat) &&
672 screen->is_format_supported(screen,
673 stImage->pt->format,
674 stImage->pt->target,
675 PIPE_BIND_RENDER_TARGET, 0)) {
676 if (!pixels)
677 goto done;
678
679 if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth,
680 format, type, pixels, unpack, texImage)) {
681 goto done;
682 }
683 }
684
685 if (stImage->pt) {
686 if (format == GL_DEPTH_COMPONENT &&
687 util_format_is_depth_and_stencil(stImage->pt->format))
688 transfer_usage = PIPE_TRANSFER_READ_WRITE;
689 else
690 transfer_usage = PIPE_TRANSFER_WRITE;
691
692 texImage->Data = st_texture_image_map(st, stImage, 0,
693 transfer_usage, 0, 0,
694 stImage->base.Width,
695 stImage->base.Height);
696 if(stImage->transfer)
697 dstRowStride = stImage->transfer->stride;
698 }
699 else {
700 /* Allocate regular memory and store the image there temporarily. */
701 if (_mesa_is_format_compressed(texImage->TexFormat)) {
702 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
703 texImage->Width,
704 texImage->Height,
705 texImage->Depth);
706 dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
707 assert(dims != 3);
708 }
709 else {
710 dstRowStride = postConvWidth * texelBytes;
711 sizeInBytes = depth * dstRowStride * postConvHeight;
712 }
713
714 texImage->Data = _mesa_align_malloc(sizeInBytes, 16);
715 }
716
717 if (!texImage->Data) {
718 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
719 return;
720 }
721
722 if (!pixels)
723 goto done;
724
725 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
726 width, height, depth, width * texelBytes, dstRowStride);
727
728 /* Copy data. Would like to know when it's ok for us to eg. use
729 * the blitter to copy. Or, use the hardware to do the format
730 * conversion and copy:
731 */
732 if (compressed_src) {
733 const GLuint srcImageStride = _mesa_format_row_stride(texImage->TexFormat, width);
734 if(dstRowStride == srcImageStride)
735 memcpy(texImage->Data, pixels, imageSize);
736 else
737 {
738 char *dst = texImage->Data;
739 const char *src = pixels;
740 GLuint i, bw, bh, lines;
741 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
742 lines = (height + bh - 1) / bh;
743
744 for(i = 0; i < lines; ++i)
745 {
746 memcpy(dst, src, srcImageStride);
747 dst += dstRowStride;
748 src += srcImageStride;
749 }
750 }
751 }
752 else {
753 const GLuint srcImageStride =
754 _mesa_image_image_stride(unpack, width, height, format, type);
755 GLint i;
756 const GLubyte *src = (const GLubyte *) pixels;
757
758 for (i = 0; i < depth; i++) {
759 if (!_mesa_texstore(ctx, dims,
760 texImage->_BaseFormat,
761 texImage->TexFormat,
762 texImage->Data,
763 0, 0, 0, /* dstX/Y/Zoffset */
764 dstRowStride,
765 texImage->ImageOffsets,
766 width, height, 1,
767 format, type, src, unpack)) {
768 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
769 }
770
771 if (stImage->pt && i + 1 < depth) {
772 /* unmap this slice */
773 st_texture_image_unmap(st, stImage);
774 /* map next slice of 3D texture */
775 texImage->Data = st_texture_image_map(st, stImage, i + 1,
776 transfer_usage, 0, 0,
777 stImage->base.Width,
778 stImage->base.Height);
779 src += srcImageStride;
780 }
781 }
782 }
783
784 done:
785 _mesa_unmap_teximage_pbo(ctx, unpack);
786
787 if (stImage->pt && texImage->Data) {
788 st_texture_image_unmap(st, stImage);
789 texImage->Data = NULL;
790 }
791 }
792
793
794 static void
795 st_TexImage3D(GLcontext * ctx,
796 GLenum target, GLint level,
797 GLint internalFormat,
798 GLint width, GLint height, GLint depth,
799 GLint border,
800 GLenum format, GLenum type, const void *pixels,
801 const struct gl_pixelstore_attrib *unpack,
802 struct gl_texture_object *texObj,
803 struct gl_texture_image *texImage)
804 {
805 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
806 border, format, type, pixels, unpack, texObj, texImage,
807 0, GL_FALSE);
808 }
809
810
811 static void
812 st_TexImage2D(GLcontext * ctx,
813 GLenum target, GLint level,
814 GLint internalFormat,
815 GLint width, GLint height, GLint border,
816 GLenum format, GLenum type, const void *pixels,
817 const struct gl_pixelstore_attrib *unpack,
818 struct gl_texture_object *texObj,
819 struct gl_texture_image *texImage)
820 {
821 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
822 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
823 }
824
825
826 static void
827 st_TexImage1D(GLcontext * ctx,
828 GLenum target, GLint level,
829 GLint internalFormat,
830 GLint width, GLint border,
831 GLenum format, GLenum type, const void *pixels,
832 const struct gl_pixelstore_attrib *unpack,
833 struct gl_texture_object *texObj,
834 struct gl_texture_image *texImage)
835 {
836 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
837 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
838 }
839
840
841 static void
842 st_CompressedTexImage2D(GLcontext *ctx, GLenum target, GLint level,
843 GLint internalFormat,
844 GLint width, GLint height, GLint border,
845 GLsizei imageSize, const GLvoid *data,
846 struct gl_texture_object *texObj,
847 struct gl_texture_image *texImage)
848 {
849 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
850 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
851 }
852
853
854
855 /**
856 * glGetTexImage() helper: decompress a compressed texture by rendering
857 * a textured quad. Store the results in the user's buffer.
858 */
859 static void
860 decompress_with_blit(GLcontext * ctx, GLenum target, GLint level,
861 GLenum format, GLenum type, GLvoid *pixels,
862 struct gl_texture_object *texObj,
863 struct gl_texture_image *texImage)
864 {
865 struct st_context *st = st_context(ctx);
866 struct pipe_context *pipe = st->pipe;
867 struct pipe_screen *screen = pipe->screen;
868 struct st_texture_image *stImage = st_texture_image(texImage);
869 struct st_texture_object *stObj = st_texture_object(texObj);
870 struct pipe_sampler_view *src_view =
871 st_get_texture_sampler_view(stObj, pipe);
872 const GLuint width = texImage->Width;
873 const GLuint height = texImage->Height;
874 struct pipe_surface *dst_surface;
875 struct pipe_resource *dst_texture;
876 struct pipe_transfer *tex_xfer;
877 unsigned bind = (PIPE_BIND_BLIT_DESTINATION |
878 PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
879 PIPE_BIND_TRANSFER_READ);
880
881 /* create temp / dest surface */
882 if (!util_create_rgba_surface(screen, width, height, bind,
883 &dst_texture, &dst_surface)) {
884 _mesa_problem(ctx, "util_create_rgba_surface() failed "
885 "in decompress_with_blit()");
886 return;
887 }
888
889 /* blit/render/decompress */
890 util_blit_pixels_tex(st->blit,
891 src_view, /* pipe_resource (src) */
892 0, 0, /* src x0, y0 */
893 width, height, /* src x1, y1 */
894 dst_surface, /* pipe_surface (dst) */
895 0, 0, /* dst x0, y0 */
896 width, height, /* dst x1, y1 */
897 0.0, /* z */
898 PIPE_TEX_MIPFILTER_NEAREST);
899
900 /* map the dst_surface so we can read from it */
901 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx),
902 dst_texture, 0, 0, 0,
903 PIPE_TRANSFER_READ,
904 0, 0, width, height);
905
906 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
907
908 /* copy/pack data into user buffer */
909 if (st_equal_formats(stImage->pt->format, format, type)) {
910 /* memcpy */
911 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
912 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
913 GLuint row;
914 for (row = 0; row < height; row++) {
915 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
916 height, format, type, row, 0);
917 memcpy(dest, map, bytesPerRow);
918 map += tex_xfer->stride;
919 }
920 pipe_transfer_unmap(pipe, tex_xfer);
921 }
922 else {
923 /* format translation via floats */
924 GLuint row;
925 for (row = 0; row < height; row++) {
926 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
927 GLfloat rgba[4 * MAX_WIDTH];
928 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
929 height, format, type, row, 0);
930
931 if (ST_DEBUG & DEBUG_FALLBACK)
932 debug_printf("%s: fallback format translation\n", __FUNCTION__);
933
934 /* get float[4] rgba row from surface */
935 pipe_get_tile_rgba(pipe, tex_xfer, 0, row, width, 1, rgba);
936
937 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
938 type, dest, &ctx->Pack, transferOps);
939 }
940 }
941
942 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
943
944 pipe->transfer_destroy(pipe, tex_xfer);
945
946 /* destroy the temp / dest surface */
947 util_destroy_rgba_surface(dst_texture, dst_surface);
948 }
949
950
951
952 /**
953 * Need to map texture image into memory before copying image data,
954 * then unmap it.
955 */
956 static void
957 st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
958 GLenum format, GLenum type, GLvoid * pixels,
959 struct gl_texture_object *texObj,
960 struct gl_texture_image *texImage, GLboolean compressed_dst)
961 {
962 struct st_context *st = st_context(ctx);
963 struct st_texture_image *stImage = st_texture_image(texImage);
964 const GLuint dstImageStride =
965 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
966 format, type);
967 GLuint depth, i;
968 GLubyte *dest;
969
970 if (stImage->pt &&
971 util_format_is_s3tc(stImage->pt->format) &&
972 !compressed_dst) {
973 /* Need to decompress the texture.
974 * We'll do this by rendering a textured quad.
975 * Note that we only expect RGBA formats (no Z/depth formats).
976 */
977 decompress_with_blit(ctx, target, level, format, type, pixels,
978 texObj, texImage);
979 return;
980 }
981
982 /* Map */
983 if (stImage->pt) {
984 /* Image is stored in hardware format in a buffer managed by the
985 * kernel. Need to explicitly map and unmap it.
986 */
987 unsigned face = _mesa_tex_target_to_face(target);
988
989 st_teximage_flush_before_map(st, stImage->pt, face, level,
990 PIPE_TRANSFER_READ);
991
992 texImage->Data = st_texture_image_map(st, stImage, 0,
993 PIPE_TRANSFER_READ, 0, 0,
994 stImage->base.Width,
995 stImage->base.Height);
996 /* compute stride in texels from stride in bytes */
997 texImage->RowStride = stImage->transfer->stride
998 * util_format_get_blockwidth(stImage->pt->format)
999 / util_format_get_blocksize(stImage->pt->format);
1000 }
1001 else {
1002 /* Otherwise, the image should actually be stored in
1003 * texImage->Data. This is pretty confusing for
1004 * everybody, I'd much prefer to separate the two functions of
1005 * texImage->Data - storage for texture images in main memory
1006 * and access (ie mappings) of images. In other words, we'd
1007 * create a new texImage->Map field and leave Data simply for
1008 * storage.
1009 */
1010 assert(texImage->Data);
1011 }
1012
1013 depth = texImage->Depth;
1014 texImage->Depth = 1;
1015
1016 dest = (GLubyte *) pixels;
1017
1018 for (i = 0; i < depth; i++) {
1019 if (compressed_dst) {
1020 _mesa_get_compressed_teximage(ctx, target, level, dest,
1021 texObj, texImage);
1022 }
1023 else {
1024 _mesa_get_teximage(ctx, target, level, format, type, dest,
1025 texObj, texImage);
1026 }
1027
1028 if (stImage->pt && i + 1 < depth) {
1029 /* unmap this slice */
1030 st_texture_image_unmap(st, stImage);
1031 /* map next slice of 3D texture */
1032 texImage->Data = st_texture_image_map(st, stImage, i + 1,
1033 PIPE_TRANSFER_READ, 0, 0,
1034 stImage->base.Width,
1035 stImage->base.Height);
1036 dest += dstImageStride;
1037 }
1038 }
1039
1040 texImage->Depth = depth;
1041
1042 /* Unmap */
1043 if (stImage->pt) {
1044 st_texture_image_unmap(st, stImage);
1045 texImage->Data = NULL;
1046 }
1047 }
1048
1049
1050 static void
1051 st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
1052 GLenum format, GLenum type, GLvoid * pixels,
1053 struct gl_texture_object *texObj,
1054 struct gl_texture_image *texImage)
1055 {
1056 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
1057 GL_FALSE);
1058 }
1059
1060
1061 static void
1062 st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
1063 GLvoid *pixels,
1064 struct gl_texture_object *texObj,
1065 struct gl_texture_image *texImage)
1066 {
1067 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
1068 GL_TRUE);
1069 }
1070
1071
1072
1073 static void
1074 st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
1075 GLint xoffset, GLint yoffset, GLint zoffset,
1076 GLint width, GLint height, GLint depth,
1077 GLenum format, GLenum type, const void *pixels,
1078 const struct gl_pixelstore_attrib *packing,
1079 struct gl_texture_object *texObj,
1080 struct gl_texture_image *texImage)
1081 {
1082 struct st_context *st = st_context(ctx);
1083 struct pipe_screen *screen = st->pipe->screen;
1084 struct st_texture_image *stImage = st_texture_image(texImage);
1085 GLuint dstRowStride;
1086 const GLuint srcImageStride =
1087 _mesa_image_image_stride(packing, width, height, format, type);
1088 GLint i;
1089 const GLubyte *src;
1090 /* init to silence warning only: */
1091 enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
1092
1093 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
1094 _mesa_lookup_enum_by_nr(target),
1095 level, xoffset, yoffset, width, height);
1096
1097 pixels =
1098 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
1099 type, pixels, packing, "glTexSubImage2D");
1100 if (!pixels)
1101 return;
1102
1103 /* See if we can do texture compression with a blit/render.
1104 */
1105 if (!ctx->Mesa_DXTn &&
1106 _mesa_is_format_compressed(texImage->TexFormat) &&
1107 screen->is_format_supported(screen,
1108 stImage->pt->format,
1109 stImage->pt->target,
1110 PIPE_BIND_RENDER_TARGET, 0)) {
1111 if (compress_with_blit(ctx, target, level,
1112 xoffset, yoffset, zoffset,
1113 width, height, depth,
1114 format, type, pixels, packing, texImage)) {
1115 goto done;
1116 }
1117 }
1118
1119 /* Map buffer if necessary. Need to lock to prevent other contexts
1120 * from uploading the buffer under us.
1121 */
1122 if (stImage->pt) {
1123 unsigned face = _mesa_tex_target_to_face(target);
1124
1125 if (format == GL_DEPTH_COMPONENT &&
1126 util_format_is_depth_and_stencil(stImage->pt->format))
1127 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1128 else
1129 transfer_usage = PIPE_TRANSFER_WRITE;
1130
1131 st_teximage_flush_before_map(st, stImage->pt, face, level,
1132 transfer_usage);
1133 texImage->Data = st_texture_image_map(st, stImage, zoffset,
1134 transfer_usage,
1135 xoffset, yoffset,
1136 width, height);
1137 }
1138
1139 if (!texImage->Data) {
1140 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1141 goto done;
1142 }
1143
1144 src = (const GLubyte *) pixels;
1145 dstRowStride = stImage->transfer->stride;
1146
1147 for (i = 0; i < depth; i++) {
1148 if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
1149 texImage->TexFormat,
1150 texImage->Data,
1151 0, 0, 0,
1152 dstRowStride,
1153 texImage->ImageOffsets,
1154 width, height, 1,
1155 format, type, src, packing)) {
1156 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1157 }
1158
1159 if (stImage->pt && i + 1 < depth) {
1160 /* unmap this slice */
1161 st_texture_image_unmap(st, stImage);
1162 /* map next slice of 3D texture */
1163 texImage->Data = st_texture_image_map(st, stImage,
1164 zoffset + i + 1,
1165 transfer_usage,
1166 xoffset, yoffset,
1167 width, height);
1168 src += srcImageStride;
1169 }
1170 }
1171
1172 done:
1173 _mesa_unmap_teximage_pbo(ctx, packing);
1174
1175 if (stImage->pt && texImage->Data) {
1176 st_texture_image_unmap(st, stImage);
1177 texImage->Data = NULL;
1178 }
1179 }
1180
1181
1182
1183 static void
1184 st_TexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1185 GLint xoffset, GLint yoffset, GLint zoffset,
1186 GLsizei width, GLsizei height, GLsizei depth,
1187 GLenum format, GLenum type, const GLvoid *pixels,
1188 const struct gl_pixelstore_attrib *packing,
1189 struct gl_texture_object *texObj,
1190 struct gl_texture_image *texImage)
1191 {
1192 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1193 width, height, depth, format, type,
1194 pixels, packing, texObj, texImage);
1195 }
1196
1197
1198 static void
1199 st_TexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1200 GLint xoffset, GLint yoffset,
1201 GLsizei width, GLsizei height,
1202 GLenum format, GLenum type, const GLvoid * pixels,
1203 const struct gl_pixelstore_attrib *packing,
1204 struct gl_texture_object *texObj,
1205 struct gl_texture_image *texImage)
1206 {
1207 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1208 width, height, 1, format, type,
1209 pixels, packing, texObj, texImage);
1210 }
1211
1212
1213 static void
1214 st_TexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1215 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1216 const GLvoid * pixels,
1217 const struct gl_pixelstore_attrib *packing,
1218 struct gl_texture_object *texObj,
1219 struct gl_texture_image *texImage)
1220 {
1221 st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
1222 format, type, pixels, packing, texObj, texImage);
1223 }
1224
1225
1226 static void
1227 st_CompressedTexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1228 GLint xoffset, GLsizei width,
1229 GLenum format,
1230 GLsizei imageSize, const GLvoid *data,
1231 struct gl_texture_object *texObj,
1232 struct gl_texture_image *texImage)
1233 {
1234 assert(0);
1235 }
1236
1237
1238 static void
1239 st_CompressedTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1240 GLint xoffset, GLint yoffset,
1241 GLsizei width, GLint height,
1242 GLenum format,
1243 GLsizei imageSize, const GLvoid *data,
1244 struct gl_texture_object *texObj,
1245 struct gl_texture_image *texImage)
1246 {
1247 struct st_context *st = st_context(ctx);
1248 struct st_texture_image *stImage = st_texture_image(texImage);
1249 int srcBlockStride;
1250 int dstBlockStride;
1251 int y;
1252 enum pipe_format pformat;
1253
1254 if (stImage->pt) {
1255 unsigned face = _mesa_tex_target_to_face(target);
1256 pformat = stImage->pt->format;
1257
1258 st_teximage_flush_before_map(st, stImage->pt, face, level,
1259 PIPE_TRANSFER_WRITE);
1260 texImage->Data = st_texture_image_map(st, stImage, 0,
1261 PIPE_TRANSFER_WRITE,
1262 xoffset, yoffset,
1263 width, height);
1264
1265 srcBlockStride = util_format_get_stride(pformat, width);
1266 dstBlockStride = stImage->transfer->stride;
1267 } else {
1268 assert(stImage->pt);
1269 /* TODO find good values for block and strides */
1270 /* TODO also adjust texImage->data for yoffset/xoffset */
1271 return;
1272 }
1273
1274 if (!texImage->Data) {
1275 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
1276 return;
1277 }
1278
1279 assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1280 assert(yoffset % util_format_get_blockheight(pformat) == 0);
1281 assert(width % util_format_get_blockwidth(pformat) == 0);
1282 assert(height % util_format_get_blockheight(pformat) == 0);
1283
1284 for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1285 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1286 const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1287 char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1288 memcpy(dst, src, util_format_get_stride(pformat, width));
1289 }
1290
1291 if (stImage->pt) {
1292 st_texture_image_unmap(st, stImage);
1293 texImage->Data = NULL;
1294 }
1295 }
1296
1297
1298 static void
1299 st_CompressedTexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1300 GLint xoffset, GLint yoffset, GLint zoffset,
1301 GLsizei width, GLint height, GLint depth,
1302 GLenum format,
1303 GLsizei imageSize, const GLvoid *data,
1304 struct gl_texture_object *texObj,
1305 struct gl_texture_image *texImage)
1306 {
1307 assert(0);
1308 }
1309
1310
1311
1312 /**
1313 * Do a CopyTexSubImage operation using a read transfer from the source,
1314 * a write transfer to the destination and get_tile()/put_tile() to access
1315 * the pixels/texels.
1316 *
1317 * Note: srcY=0=TOP of renderbuffer
1318 */
1319 static void
1320 fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
1321 struct st_renderbuffer *strb,
1322 struct st_texture_image *stImage,
1323 GLenum baseFormat,
1324 GLint destX, GLint destY, GLint destZ,
1325 GLint srcX, GLint srcY,
1326 GLsizei width, GLsizei height)
1327 {
1328 struct st_context *st = st_context(ctx);
1329 struct pipe_context *pipe = st->pipe;
1330 struct pipe_transfer *src_trans;
1331 GLvoid *texDest;
1332 enum pipe_transfer_usage transfer_usage;
1333
1334 if (ST_DEBUG & DEBUG_FALLBACK)
1335 debug_printf("%s: fallback processing\n", __FUNCTION__);
1336
1337 assert(width <= MAX_WIDTH);
1338
1339 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1340 srcY = strb->Base.Height - srcY - height;
1341 }
1342
1343 src_trans = st_cond_flush_get_tex_transfer( st_context(ctx),
1344 strb->texture,
1345 0, 0, 0,
1346 PIPE_TRANSFER_READ,
1347 srcX, srcY,
1348 width, height);
1349
1350 if ((baseFormat == GL_DEPTH_COMPONENT ||
1351 baseFormat == GL_DEPTH_STENCIL) &&
1352 util_format_is_depth_and_stencil(stImage->pt->format))
1353 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1354 else
1355 transfer_usage = PIPE_TRANSFER_WRITE;
1356
1357 st_teximage_flush_before_map(st, stImage->pt, 0, 0,
1358 transfer_usage);
1359
1360 texDest = st_texture_image_map(st, stImage, 0, transfer_usage,
1361 destX, destY, width, height);
1362
1363 if (baseFormat == GL_DEPTH_COMPONENT ||
1364 baseFormat == GL_DEPTH_STENCIL) {
1365 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1366 ctx->Pixel.DepthBias != 0.0F);
1367 GLint row, yStep;
1368
1369 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1370 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1371 srcY = height - 1;
1372 yStep = -1;
1373 }
1374 else {
1375 srcY = 0;
1376 yStep = 1;
1377 }
1378
1379 /* To avoid a large temp memory allocation, do copy row by row */
1380 for (row = 0; row < height; row++, srcY += yStep) {
1381 uint data[MAX_WIDTH];
1382 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1383 if (scaleOrBias) {
1384 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1385 }
1386 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1387 }
1388 }
1389 else {
1390 /* RGBA format */
1391 GLfloat *tempSrc =
1392 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1393
1394 if (tempSrc && texDest) {
1395 const GLint dims = 2;
1396 const GLint dstRowStride = stImage->transfer->stride;
1397 struct gl_texture_image *texImage = &stImage->base;
1398 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1399
1400 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1401 unpack.Invert = GL_TRUE;
1402 }
1403
1404 /* get float/RGBA image from framebuffer */
1405 /* XXX this usually involves a lot of int/float conversion.
1406 * try to avoid that someday.
1407 */
1408 pipe_get_tile_rgba(pipe, src_trans, 0, 0, width, height, tempSrc);
1409
1410 /* Store into texture memory.
1411 * Note that this does some special things such as pixel transfer
1412 * ops and format conversion. In particular, if the dest tex format
1413 * is actually RGBA but the user created the texture as GL_RGB we
1414 * need to fill-in/override the alpha channel with 1.0.
1415 */
1416 _mesa_texstore(ctx, dims,
1417 texImage->_BaseFormat,
1418 texImage->TexFormat,
1419 texDest,
1420 0, 0, 0,
1421 dstRowStride,
1422 texImage->ImageOffsets,
1423 width, height, 1,
1424 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1425 &unpack);
1426 }
1427 else {
1428 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1429 }
1430
1431 if (tempSrc)
1432 free(tempSrc);
1433 }
1434
1435 st_texture_image_unmap(st, stImage);
1436 pipe->transfer_destroy(pipe, src_trans);
1437 }
1438
1439
1440
1441 /**
1442 * If the format of the src renderbuffer and the format of the dest
1443 * texture are compatible (in terms of blitting), return a TGSI writemask
1444 * to be used during the blit.
1445 * If the src/dest are incompatible, return 0.
1446 */
1447 static unsigned
1448 compatible_src_dst_formats(GLcontext *ctx,
1449 const struct gl_renderbuffer *src,
1450 const struct gl_texture_image *dst)
1451 {
1452 /* Get logical base formats for the src and dest.
1453 * That is, use the user-requested formats and not the actual, device-
1454 * chosen formats.
1455 * For example, the user may have requested an A8 texture but the
1456 * driver may actually be using an RGBA texture format. When we
1457 * copy/blit to that texture, we only want to copy the Alpha channel
1458 * and not the RGB channels.
1459 *
1460 * Similarly, when the src FBO was created an RGB format may have been
1461 * requested but the driver actually chose an RGBA format. In that case,
1462 * we don't want to copy the undefined Alpha channel to the dest texture
1463 * (it should be 1.0).
1464 */
1465 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1466 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1467
1468 /**
1469 * XXX when we have red-only and red/green renderbuffers we'll need
1470 * to add more cases here (or implement a general-purpose routine that
1471 * queries the existance of the R,G,B,A channels in the src and dest).
1472 */
1473 if (srcFormat == dstFormat) {
1474 /* This is the same as matching_base_formats, which should
1475 * always pass, as it did previously.
1476 */
1477 return TGSI_WRITEMASK_XYZW;
1478 }
1479 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1480 /* Make sure that A in the dest is 1. The actual src format
1481 * may be RGBA and have undefined A values.
1482 */
1483 return TGSI_WRITEMASK_XYZ;
1484 }
1485 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1486 /* Make sure that A in the dest is 1. The actual dst format
1487 * may be RGBA and will need A=1 to provide proper alpha values
1488 * when sampled later.
1489 */
1490 return TGSI_WRITEMASK_XYZ;
1491 }
1492 else {
1493 if (ST_DEBUG & DEBUG_FALLBACK)
1494 debug_printf("%s failed for src %s, dst %s\n",
1495 __FUNCTION__,
1496 _mesa_lookup_enum_by_nr(srcFormat),
1497 _mesa_lookup_enum_by_nr(dstFormat));
1498
1499 /* Otherwise fail.
1500 */
1501 return 0;
1502 }
1503 }
1504
1505
1506
1507 /**
1508 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1509 * Note that the region to copy has already been clipped so we know we
1510 * won't read from outside the source renderbuffer's bounds.
1511 *
1512 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1513 */
1514 static void
1515 st_copy_texsubimage(GLcontext *ctx,
1516 GLenum target, GLint level,
1517 GLint destX, GLint destY, GLint destZ,
1518 GLint srcX, GLint srcY,
1519 GLsizei width, GLsizei height)
1520 {
1521 struct gl_texture_unit *texUnit =
1522 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1523 struct gl_texture_object *texObj =
1524 _mesa_select_tex_object(ctx, texUnit, target);
1525 struct gl_texture_image *texImage =
1526 _mesa_select_tex_image(ctx, texObj, target, level);
1527 struct st_texture_image *stImage = st_texture_image(texImage);
1528 const GLenum texBaseFormat = texImage->_BaseFormat;
1529 struct gl_framebuffer *fb = ctx->ReadBuffer;
1530 struct st_renderbuffer *strb;
1531 struct st_context *st = st_context(ctx);
1532 struct pipe_context *pipe = st->pipe;
1533 struct pipe_screen *screen = pipe->screen;
1534 enum pipe_format dest_format, src_format;
1535 GLboolean use_fallback = GL_TRUE;
1536 GLboolean matching_base_formats;
1537 GLuint format_writemask;
1538 struct pipe_surface *dest_surface = NULL;
1539 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1540
1541 /* any rendering in progress must flushed before we grab the fb image */
1542 st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL);
1543
1544 /* make sure finalize_textures has been called?
1545 */
1546 if (0) st_validate_state(st);
1547
1548 /* determine if copying depth or color data */
1549 if (texBaseFormat == GL_DEPTH_COMPONENT ||
1550 texBaseFormat == GL_DEPTH_STENCIL) {
1551 strb = st_renderbuffer(fb->_DepthBuffer);
1552 }
1553 else {
1554 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1555 strb = st_renderbuffer(fb->_ColorReadBuffer);
1556 }
1557
1558 if (!strb || !strb->surface || !stImage->pt) {
1559 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1560 return;
1561 }
1562
1563 if (srcX < 0) {
1564 width -= -srcX;
1565 destX += -srcX;
1566 srcX = 0;
1567 }
1568
1569 if (srcY < 0) {
1570 height -= -srcY;
1571 destY += -srcY;
1572 srcY = 0;
1573 }
1574
1575 if (destX < 0) {
1576 width -= -destX;
1577 srcX += -destX;
1578 destX = 0;
1579 }
1580
1581 if (destY < 0) {
1582 height -= -destY;
1583 srcY += -destY;
1584 destY = 0;
1585 }
1586
1587 if (width < 0 || height < 0)
1588 return;
1589
1590
1591 assert(strb);
1592 assert(strb->surface);
1593 assert(stImage->pt);
1594
1595 src_format = strb->surface->format;
1596 dest_format = stImage->pt->format;
1597
1598 /*
1599 * Determine if the src framebuffer and dest texture have the same
1600 * base format. We need this to detect a case such as the framebuffer
1601 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1602 * texture format stores RGBA we need to set A=1 (overriding the
1603 * framebuffer's alpha values). We can't do that with the blit or
1604 * textured-quad paths.
1605 */
1606 matching_base_formats =
1607 (_mesa_get_format_base_format(strb->Base.Format) ==
1608 _mesa_get_format_base_format(texImage->TexFormat));
1609 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1610
1611 if (ctx->_ImageTransferState == 0x0) {
1612
1613 if (matching_base_formats &&
1614 src_format == dest_format &&
1615 !do_flip)
1616 {
1617 /* use surface_copy() / blit */
1618
1619 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1620 stImage->face, stImage->level,
1621 destZ,
1622 PIPE_BIND_BLIT_DESTINATION);
1623
1624 /* for surface_copy(), y=0=top, always */
1625 pipe->surface_copy(pipe,
1626 /* dest */
1627 dest_surface,
1628 destX, destY,
1629 /* src */
1630 strb->surface,
1631 srcX, srcY,
1632 /* size */
1633 width, height);
1634 use_fallback = GL_FALSE;
1635 }
1636 else if (format_writemask &&
1637 texBaseFormat != GL_DEPTH_COMPONENT &&
1638 texBaseFormat != GL_DEPTH_STENCIL &&
1639 screen->is_format_supported(screen, src_format,
1640 PIPE_TEXTURE_2D,
1641 PIPE_BIND_SAMPLER_VIEW,
1642 0) &&
1643 screen->is_format_supported(screen, dest_format,
1644 PIPE_TEXTURE_2D,
1645 PIPE_BIND_RENDER_TARGET,
1646 0)) {
1647 /* draw textured quad to do the copy */
1648 GLint srcY0, srcY1;
1649
1650 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1651 stImage->face, stImage->level,
1652 destZ,
1653 PIPE_BIND_BLIT_DESTINATION);
1654
1655 if (do_flip) {
1656 srcY1 = strb->Base.Height - srcY - height;
1657 srcY0 = srcY1 + height;
1658 }
1659 else {
1660 srcY0 = srcY;
1661 srcY1 = srcY0 + height;
1662 }
1663 util_blit_pixels_writemask(st->blit,
1664 strb->surface,
1665 st_get_renderbuffer_sampler_view(strb, pipe),
1666 srcX, srcY0,
1667 srcX + width, srcY1,
1668 dest_surface,
1669 destX, destY,
1670 destX + width, destY + height,
1671 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1672 format_writemask);
1673 use_fallback = GL_FALSE;
1674 }
1675
1676 if (dest_surface)
1677 pipe_surface_reference(&dest_surface, NULL);
1678 }
1679
1680 if (use_fallback) {
1681 /* software fallback */
1682 fallback_copy_texsubimage(ctx, target, level,
1683 strb, stImage, texBaseFormat,
1684 destX, destY, destZ,
1685 srcX, srcY, width, height);
1686 }
1687 }
1688
1689
1690
1691 static void
1692 st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1693 GLenum internalFormat,
1694 GLint x, GLint y, GLsizei width, GLint border)
1695 {
1696 struct gl_texture_unit *texUnit =
1697 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1698 struct gl_texture_object *texObj =
1699 _mesa_select_tex_object(ctx, texUnit, target);
1700 struct gl_texture_image *texImage =
1701 _mesa_select_tex_image(ctx, texObj, target, level);
1702
1703 /* Setup or redefine the texture object, texture and texture
1704 * image. Don't populate yet.
1705 */
1706 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1707 width, border,
1708 GL_RGBA, CHAN_TYPE, NULL,
1709 &ctx->DefaultPacking, texObj, texImage);
1710
1711 st_copy_texsubimage(ctx, target, level,
1712 0, 0, 0, /* destX,Y,Z */
1713 x, y, width, 1); /* src X, Y, size */
1714 }
1715
1716
1717 static void
1718 st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1719 GLenum internalFormat,
1720 GLint x, GLint y, GLsizei width, GLsizei height,
1721 GLint border)
1722 {
1723 struct gl_texture_unit *texUnit =
1724 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1725 struct gl_texture_object *texObj =
1726 _mesa_select_tex_object(ctx, texUnit, target);
1727 struct gl_texture_image *texImage =
1728 _mesa_select_tex_image(ctx, texObj, target, level);
1729
1730 /* Setup or redefine the texture object, texture and texture
1731 * image. Don't populate yet.
1732 */
1733 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1734 width, height, border,
1735 GL_RGBA, CHAN_TYPE, NULL,
1736 &ctx->DefaultPacking, texObj, texImage);
1737
1738 st_copy_texsubimage(ctx, target, level,
1739 0, 0, 0, /* destX,Y,Z */
1740 x, y, width, height); /* src X, Y, size */
1741 }
1742
1743
1744 static void
1745 st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1746 GLint xoffset, GLint x, GLint y, GLsizei width)
1747 {
1748 const GLint yoffset = 0, zoffset = 0;
1749 const GLsizei height = 1;
1750 st_copy_texsubimage(ctx, target, level,
1751 xoffset, yoffset, zoffset, /* destX,Y,Z */
1752 x, y, width, height); /* src X, Y, size */
1753 }
1754
1755
1756 static void
1757 st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1758 GLint xoffset, GLint yoffset,
1759 GLint x, GLint y, GLsizei width, GLsizei height)
1760 {
1761 const GLint zoffset = 0;
1762 st_copy_texsubimage(ctx, target, level,
1763 xoffset, yoffset, zoffset, /* destX,Y,Z */
1764 x, y, width, height); /* src X, Y, size */
1765 }
1766
1767
1768 static void
1769 st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1770 GLint xoffset, GLint yoffset, GLint zoffset,
1771 GLint x, GLint y, GLsizei width, GLsizei height)
1772 {
1773 st_copy_texsubimage(ctx, target, level,
1774 xoffset, yoffset, zoffset, /* destX,Y,Z */
1775 x, y, width, height); /* src X, Y, size */
1776 }
1777
1778
1779 /**
1780 * Copy image data from stImage into the texture object 'stObj' at level
1781 * 'dstLevel'.
1782 */
1783 static void
1784 copy_image_data_to_texture(struct st_context *st,
1785 struct st_texture_object *stObj,
1786 GLuint dstLevel,
1787 struct st_texture_image *stImage)
1788 {
1789 /* debug checks */
1790 {
1791 const struct gl_texture_image *dstImage =
1792 stObj->base.Image[stImage->face][stImage->level];
1793 assert(dstImage);
1794 assert(dstImage->Width == stImage->base.Width);
1795 assert(dstImage->Height == stImage->base.Height);
1796 assert(dstImage->Depth == stImage->base.Depth);
1797 }
1798
1799 if (stImage->pt) {
1800 /* Copy potentially with the blitter:
1801 */
1802 st_texture_image_copy(st->pipe,
1803 stObj->pt, dstLevel, /* dest texture, level */
1804 stImage->pt, /* src texture */
1805 stImage->face);
1806
1807 pipe_resource_reference(&stImage->pt, NULL);
1808 }
1809 else if (stImage->base.Data) {
1810 /* More straightforward upload.
1811 */
1812 st_teximage_flush_before_map(st, stObj->pt, stImage->face, dstLevel,
1813 PIPE_TRANSFER_WRITE);
1814
1815 st_texture_image_data(st,
1816 stObj->pt,
1817 stImage->face,
1818 dstLevel,
1819 stImage->base.Data,
1820 stImage->base.RowStride *
1821 util_format_get_blocksize(stObj->pt->format),
1822 stImage->base.RowStride *
1823 stImage->base.Height *
1824 util_format_get_blocksize(stObj->pt->format));
1825 _mesa_align_free(stImage->base.Data);
1826 stImage->base.Data = NULL;
1827 }
1828
1829 pipe_resource_reference(&stImage->pt, stObj->pt);
1830 }
1831
1832
1833 /**
1834 * Called during state validation. When this function is finished,
1835 * the texture object should be ready for rendering.
1836 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1837 */
1838 GLboolean
1839 st_finalize_texture(GLcontext *ctx,
1840 struct pipe_context *pipe,
1841 struct gl_texture_object *tObj,
1842 GLboolean *needFlush)
1843 {
1844 struct st_context *st = st_context(ctx);
1845 struct st_texture_object *stObj = st_texture_object(tObj);
1846 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1847 GLuint face;
1848 struct st_texture_image *firstImage;
1849 enum pipe_format firstImageFormat;
1850
1851 *needFlush = GL_FALSE;
1852
1853 if (stObj->base._Complete) {
1854 /* The texture is complete and we know exactly how many mipmap levels
1855 * are present/needed. This is conditional because we may be called
1856 * from the st_generate_mipmap() function when the texture object is
1857 * incomplete. In that case, we'll have set stObj->lastLevel before
1858 * we get here.
1859 */
1860 if (stObj->base.MinFilter == GL_LINEAR ||
1861 stObj->base.MinFilter == GL_NEAREST)
1862 stObj->lastLevel = stObj->base.BaseLevel;
1863 else
1864 stObj->lastLevel = stObj->base._MaxLevel;
1865 }
1866
1867 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1868 assert(firstImage);
1869
1870 /* If both firstImage and stObj point to a texture which can contain
1871 * all active images, favour firstImage. Note that because of the
1872 * completeness requirement, we know that the image dimensions
1873 * will match.
1874 */
1875 if (firstImage->pt &&
1876 firstImage->pt != stObj->pt &&
1877 firstImage->pt->last_level >= stObj->lastLevel) {
1878 pipe_resource_reference(&stObj->pt, firstImage->pt);
1879 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1880 }
1881
1882 /* Find gallium format for the Mesa texture */
1883 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1884
1885 /* If we already have a gallium texture, check that it matches the texture
1886 * object's format, target, size, num_levels, etc.
1887 */
1888 if (stObj->pt) {
1889 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1890 stObj->pt->format != firstImageFormat ||
1891 stObj->pt->last_level < stObj->lastLevel ||
1892 stObj->pt->width0 != stObj->width0 ||
1893 stObj->pt->height0 != stObj->height0 ||
1894 stObj->pt->depth0 != stObj->depth0)
1895 {
1896 /* The gallium texture does not match the Mesa texture so delete the
1897 * gallium texture now. We'll make a new one below.
1898 */
1899 pipe_resource_reference(&stObj->pt, NULL);
1900 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1901 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1902 }
1903 }
1904
1905 /* May need to create a new gallium texture:
1906 */
1907 if (!stObj->pt) {
1908 GLuint bindings = default_bindings(st, firstImageFormat);
1909
1910 stObj->pt = st_texture_create(st,
1911 gl_target_to_pipe(stObj->base.Target),
1912 firstImageFormat,
1913 stObj->lastLevel,
1914 stObj->width0,
1915 stObj->height0,
1916 stObj->depth0,
1917 bindings);
1918
1919 if (!stObj->pt) {
1920 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1921 return GL_FALSE;
1922 }
1923 }
1924
1925 /* Pull in any images not in the object's texture:
1926 */
1927 for (face = 0; face < nr_faces; face++) {
1928 GLuint level;
1929 for (level = 0; level <= stObj->lastLevel; level++) {
1930 struct st_texture_image *stImage =
1931 st_texture_image(stObj->base.Image[face][level]);
1932
1933 /* Need to import images in main memory or held in other textures.
1934 */
1935 if (stImage && stObj->pt != stImage->pt) {
1936 copy_image_data_to_texture(st, stObj, level, stImage);
1937 *needFlush = GL_TRUE;
1938 }
1939 }
1940 }
1941
1942 return GL_TRUE;
1943 }
1944
1945
1946 /**
1947 * Returns pointer to a default/dummy texture.
1948 * This is typically used when the current shader has tex/sample instructions
1949 * but the user has not provided a (any) texture(s).
1950 */
1951 struct gl_texture_object *
1952 st_get_default_texture(struct st_context *st)
1953 {
1954 if (!st->default_texture) {
1955 static const GLenum target = GL_TEXTURE_2D;
1956 GLubyte pixels[16][16][4];
1957 struct gl_texture_object *texObj;
1958 struct gl_texture_image *texImg;
1959 GLuint i, j;
1960
1961 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1962 * when attempting to sample incomplete textures.
1963 */
1964 for (i = 0; i < 16; i++) {
1965 for (j = 0; j < 16; j++) {
1966 pixels[i][j][0] = 0;
1967 pixels[i][j][1] = 0;
1968 pixels[i][j][2] = 0;
1969 pixels[i][j][3] = 255;
1970 }
1971 }
1972
1973 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1974
1975 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1976
1977 _mesa_init_teximage_fields(st->ctx, target, texImg,
1978 16, 16, 1, 0, /* w, h, d, border */
1979 GL_RGBA);
1980
1981 st_TexImage(st->ctx, 2, target,
1982 0, GL_RGBA, /* level, intformat */
1983 16, 16, 1, 0, /* w, h, d, border */
1984 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1985 &st->ctx->DefaultPacking,
1986 texObj, texImg,
1987 0, 0);
1988
1989 texObj->MinFilter = GL_NEAREST;
1990 texObj->MagFilter = GL_NEAREST;
1991 texObj->_Complete = GL_TRUE;
1992
1993 st->default_texture = texObj;
1994 }
1995 return st->default_texture;
1996 }
1997
1998
1999 void
2000 st_init_texture_functions(struct dd_function_table *functions)
2001 {
2002 functions->ChooseTextureFormat = st_ChooseTextureFormat;
2003 functions->TexImage1D = st_TexImage1D;
2004 functions->TexImage2D = st_TexImage2D;
2005 functions->TexImage3D = st_TexImage3D;
2006 functions->TexSubImage1D = st_TexSubImage1D;
2007 functions->TexSubImage2D = st_TexSubImage2D;
2008 functions->TexSubImage3D = st_TexSubImage3D;
2009 functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
2010 functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
2011 functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
2012 functions->CopyTexImage1D = st_CopyTexImage1D;
2013 functions->CopyTexImage2D = st_CopyTexImage2D;
2014 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
2015 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
2016 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
2017 functions->GenerateMipmap = st_generate_mipmap;
2018
2019 functions->GetTexImage = st_GetTexImage;
2020
2021 /* compressed texture functions */
2022 functions->CompressedTexImage2D = st_CompressedTexImage2D;
2023 functions->GetCompressedTexImage = st_GetCompressedTexImage;
2024
2025 functions->NewTextureObject = st_NewTextureObject;
2026 functions->NewTextureImage = st_NewTextureImage;
2027 functions->DeleteTexture = st_DeleteTextureObject;
2028 functions->FreeTexImageData = st_FreeTextureImageData;
2029 functions->UpdateTexturePalette = 0;
2030
2031 functions->TextureMemCpy = do_memcpy;
2032
2033 /* XXX Temporary until we can query pipe's texture sizes */
2034 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
2035 }