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