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