st/mesa: try to find exact format matching user format and type for DrawPixels
[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_cb_bufferobjects.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
56
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67 #include "util/u_box.h"
68
69 #define DBG if (0) printf
70
71
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target)
74 {
75 switch (target) {
76 case GL_TEXTURE_1D:
77 case GL_PROXY_TEXTURE_1D:
78 return PIPE_TEXTURE_1D;
79 case GL_TEXTURE_2D:
80 case GL_PROXY_TEXTURE_2D:
81 case GL_TEXTURE_EXTERNAL_OES:
82 return PIPE_TEXTURE_2D;
83 case GL_TEXTURE_RECTANGLE_NV:
84 case GL_PROXY_TEXTURE_RECTANGLE_NV:
85 return PIPE_TEXTURE_RECT;
86 case GL_TEXTURE_3D:
87 case GL_PROXY_TEXTURE_3D:
88 return PIPE_TEXTURE_3D;
89 case GL_TEXTURE_CUBE_MAP_ARB:
90 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
91 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
92 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
93 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
94 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
95 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
96 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
97 return PIPE_TEXTURE_CUBE;
98 case GL_TEXTURE_1D_ARRAY_EXT:
99 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
100 return PIPE_TEXTURE_1D_ARRAY;
101 case GL_TEXTURE_2D_ARRAY_EXT:
102 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
103 return PIPE_TEXTURE_2D_ARRAY;
104 case GL_TEXTURE_BUFFER:
105 return PIPE_BUFFER;
106 case GL_TEXTURE_CUBE_MAP_ARRAY:
107 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
108 return PIPE_TEXTURE_CUBE_ARRAY;
109 default:
110 assert(0);
111 return 0;
112 }
113 }
114
115
116 /** called via ctx->Driver.NewTextureImage() */
117 static struct gl_texture_image *
118 st_NewTextureImage(struct gl_context * ctx)
119 {
120 DBG("%s\n", __FUNCTION__);
121 (void) ctx;
122 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
123 }
124
125
126 /** called via ctx->Driver.DeleteTextureImage() */
127 static void
128 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
129 {
130 /* nothing special (yet) for st_texture_image */
131 _mesa_delete_texture_image(ctx, img);
132 }
133
134
135 /** called via ctx->Driver.NewTextureObject() */
136 static struct gl_texture_object *
137 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
138 {
139 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
140
141 DBG("%s\n", __FUNCTION__);
142 _mesa_initialize_texture_object(&obj->base, name, target);
143
144 return &obj->base;
145 }
146
147 /** called via ctx->Driver.DeleteTextureObject() */
148 static void
149 st_DeleteTextureObject(struct gl_context *ctx,
150 struct gl_texture_object *texObj)
151 {
152 struct st_context *st = st_context(ctx);
153 struct st_texture_object *stObj = st_texture_object(texObj);
154 if (stObj->pt)
155 pipe_resource_reference(&stObj->pt, NULL);
156 if (stObj->sampler_view) {
157 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
158 }
159 _mesa_delete_texture_object(ctx, texObj);
160 }
161
162
163 /** called via ctx->Driver.FreeTextureImageBuffer() */
164 static void
165 st_FreeTextureImageBuffer(struct gl_context *ctx,
166 struct gl_texture_image *texImage)
167 {
168 struct st_texture_image *stImage = st_texture_image(texImage);
169
170 DBG("%s\n", __FUNCTION__);
171
172 if (stImage->pt) {
173 pipe_resource_reference(&stImage->pt, NULL);
174 }
175
176 if (stImage->TexData) {
177 _mesa_align_free(stImage->TexData);
178 stImage->TexData = NULL;
179 }
180 }
181
182
183 /** called via ctx->Driver.MapTextureImage() */
184 static void
185 st_MapTextureImage(struct gl_context *ctx,
186 struct gl_texture_image *texImage,
187 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
188 GLbitfield mode,
189 GLubyte **mapOut, GLint *rowStrideOut)
190 {
191 struct st_context *st = st_context(ctx);
192 struct st_texture_image *stImage = st_texture_image(texImage);
193 unsigned pipeMode;
194 GLubyte *map;
195
196 pipeMode = 0x0;
197 if (mode & GL_MAP_READ_BIT)
198 pipeMode |= PIPE_TRANSFER_READ;
199 if (mode & GL_MAP_WRITE_BIT)
200 pipeMode |= PIPE_TRANSFER_WRITE;
201 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
202 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
203
204 map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1);
205 if (map) {
206 *mapOut = map;
207 *rowStrideOut = stImage->transfer->stride;
208 }
209 else {
210 *mapOut = NULL;
211 *rowStrideOut = 0;
212 }
213 }
214
215
216 /** called via ctx->Driver.UnmapTextureImage() */
217 static void
218 st_UnmapTextureImage(struct gl_context *ctx,
219 struct gl_texture_image *texImage,
220 GLuint slice)
221 {
222 struct st_context *st = st_context(ctx);
223 struct st_texture_image *stImage = st_texture_image(texImage);
224 st_texture_image_unmap(st, stImage);
225 }
226
227
228 /**
229 * Return default texture resource binding bitmask for the given format.
230 */
231 static GLuint
232 default_bindings(struct st_context *st, enum pipe_format format)
233 {
234 struct pipe_screen *screen = st->pipe->screen;
235 const unsigned target = PIPE_TEXTURE_2D;
236 unsigned bindings;
237
238 if (util_format_is_depth_or_stencil(format))
239 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
240 else
241 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
242
243 if (screen->is_format_supported(screen, format, target, 0, bindings))
244 return bindings;
245 else {
246 /* Try non-sRGB. */
247 format = util_format_linear(format);
248
249 if (screen->is_format_supported(screen, format, target, 0, bindings))
250 return bindings;
251 else
252 return PIPE_BIND_SAMPLER_VIEW;
253 }
254 }
255
256
257 /**
258 * Given the size of a mipmap image, try to compute the size of the level=0
259 * mipmap image.
260 *
261 * Note that this isn't always accurate for odd-sized, non-POW textures.
262 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
263 *
264 * \return GL_TRUE for success, GL_FALSE for failure
265 */
266 static GLboolean
267 guess_base_level_size(GLenum target,
268 GLuint width, GLuint height, GLuint depth, GLuint level,
269 GLuint *width0, GLuint *height0, GLuint *depth0)
270 {
271 assert(width >= 1);
272 assert(height >= 1);
273 assert(depth >= 1);
274
275 if (level > 0) {
276 /* Guess the size of the base level.
277 * Depending on the image's size, we can't always make a guess here.
278 */
279 switch (target) {
280 case GL_TEXTURE_1D:
281 case GL_TEXTURE_1D_ARRAY:
282 width <<= level;
283 break;
284
285 case GL_TEXTURE_2D:
286 case GL_TEXTURE_2D_ARRAY:
287 /* We can't make a good guess here, because the base level dimensions
288 * can be non-square.
289 */
290 if (width == 1 || height == 1) {
291 return GL_FALSE;
292 }
293 width <<= level;
294 height <<= level;
295 break;
296
297 case GL_TEXTURE_CUBE_MAP:
298 case GL_TEXTURE_CUBE_MAP_ARRAY:
299 width <<= level;
300 height <<= level;
301 break;
302
303 case GL_TEXTURE_3D:
304 /* We can't make a good guess here, because the base level dimensions
305 * can be non-cube.
306 */
307 if (width == 1 || height == 1 || depth == 1) {
308 return GL_FALSE;
309 }
310 width <<= level;
311 height <<= level;
312 depth <<= level;
313 break;
314
315 case GL_TEXTURE_RECTANGLE:
316 break;
317
318 default:
319 assert(0);
320 }
321 }
322
323 *width0 = width;
324 *height0 = height;
325 *depth0 = depth;
326
327 return GL_TRUE;
328 }
329
330
331 /**
332 * Try to allocate a pipe_resource object for the given st_texture_object.
333 *
334 * We use the given st_texture_image as a clue to determine the size of the
335 * mipmap image at level=0.
336 *
337 * \return GL_TRUE for success, GL_FALSE if out of memory.
338 */
339 static GLboolean
340 guess_and_alloc_texture(struct st_context *st,
341 struct st_texture_object *stObj,
342 const struct st_texture_image *stImage)
343 {
344 GLuint lastLevel, width, height, depth;
345 GLuint bindings;
346 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
347 enum pipe_format fmt;
348
349 DBG("%s\n", __FUNCTION__);
350
351 assert(!stObj->pt);
352
353 if (!guess_base_level_size(stObj->base.Target,
354 stImage->base.Width2,
355 stImage->base.Height2,
356 stImage->base.Depth2,
357 stImage->base.Level,
358 &width, &height, &depth)) {
359 /* we can't determine the image size at level=0 */
360 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
361 /* this is not an out of memory error */
362 return GL_TRUE;
363 }
364
365 /* At this point, (width x height x depth) is the expected size of
366 * the level=0 mipmap image.
367 */
368
369 /* Guess a reasonable value for lastLevel. With OpenGL we have no
370 * idea how many mipmap levels will be in a texture until we start
371 * to render with it. Make an educated guess here but be prepared
372 * to re-allocating a texture buffer with space for more (or fewer)
373 * mipmap levels later.
374 */
375 if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
376 stObj->base.Sampler.MinFilter == GL_LINEAR ||
377 (stObj->base.BaseLevel == 0 &&
378 stObj->base.MaxLevel == 0) ||
379 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
380 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
381 !stObj->base.GenerateMipmap &&
382 stImage->base.Level == 0) {
383 /* only alloc space for a single mipmap level */
384 lastLevel = 0;
385 }
386 else {
387 /* alloc space for a full mipmap */
388 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
389 width, height, depth) - 1;
390 }
391
392 /* Save the level=0 dimensions */
393 stObj->width0 = width;
394 stObj->height0 = height;
395 stObj->depth0 = depth;
396
397 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
398
399 bindings = default_bindings(st, fmt);
400
401 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
402 width, height, depth,
403 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
404
405 stObj->pt = st_texture_create(st,
406 gl_target_to_pipe(stObj->base.Target),
407 fmt,
408 lastLevel,
409 ptWidth,
410 ptHeight,
411 ptDepth,
412 ptLayers,
413 bindings);
414
415 stObj->lastLevel = lastLevel;
416
417 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
418
419 return stObj->pt != NULL;
420 }
421
422
423 /**
424 * Called via ctx->Driver.AllocTextureImageBuffer().
425 * If the texture object/buffer already has space for the indicated image,
426 * we're done. Otherwise, allocate memory for the new texture image.
427 */
428 static GLboolean
429 st_AllocTextureImageBuffer(struct gl_context *ctx,
430 struct gl_texture_image *texImage)
431 {
432 struct st_context *st = st_context(ctx);
433 struct st_texture_image *stImage = st_texture_image(texImage);
434 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
435 const GLuint level = texImage->Level;
436 GLuint width = texImage->Width;
437 GLuint height = texImage->Height;
438 GLuint depth = texImage->Depth;
439
440 DBG("%s\n", __FUNCTION__);
441
442 assert(!stImage->TexData);
443 assert(!stImage->pt); /* xxx this might be wrong */
444
445 /* Look if the parent texture object has space for this image */
446 if (stObj->pt &&
447 level <= stObj->pt->last_level &&
448 st_texture_match_image(stObj->pt, texImage)) {
449 /* this image will fit in the existing texture object's memory */
450 pipe_resource_reference(&stImage->pt, stObj->pt);
451 return GL_TRUE;
452 }
453
454 /* The parent texture object does not have space for this image */
455
456 pipe_resource_reference(&stObj->pt, NULL);
457 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
458
459 if (!guess_and_alloc_texture(st, stObj, stImage)) {
460 /* Probably out of memory.
461 * Try flushing any pending rendering, then retry.
462 */
463 st_finish(st);
464 if (!guess_and_alloc_texture(st, stObj, stImage)) {
465 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
466 return GL_FALSE;
467 }
468 }
469
470 if (stObj->pt &&
471 st_texture_match_image(stObj->pt, texImage)) {
472 /* The image will live in the object's mipmap memory */
473 pipe_resource_reference(&stImage->pt, stObj->pt);
474 assert(stImage->pt);
475 return GL_TRUE;
476 }
477 else {
478 /* Create a new, temporary texture/resource/buffer to hold this
479 * one texture image. Note that when we later access this image
480 * (either for mapping or copying) we'll want to always specify
481 * mipmap level=0, even if the image represents some other mipmap
482 * level.
483 */
484 enum pipe_format format =
485 st_mesa_format_to_pipe_format(texImage->TexFormat);
486 GLuint bindings = default_bindings(st, format);
487 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
488
489 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
490 width, height, depth,
491 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
492
493 stImage->pt = st_texture_create(st,
494 gl_target_to_pipe(stObj->base.Target),
495 format,
496 0, /* lastLevel */
497 ptWidth,
498 ptHeight,
499 ptDepth,
500 ptLayers,
501 bindings);
502 return stImage->pt != NULL;
503 }
504 }
505
506
507 /**
508 * Preparation prior to glTexImage. Basically check the 'surface_based'
509 * field and switch to a "normal" tex image if necessary.
510 */
511 static void
512 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
513 GLenum format, GLenum type)
514 {
515 struct gl_texture_object *texObj = texImage->TexObject;
516 struct st_texture_object *stObj = st_texture_object(texObj);
517
518 /* switch to "normal" */
519 if (stObj->surface_based) {
520 const GLenum target = texObj->Target;
521 const GLuint level = texImage->Level;
522 gl_format texFormat;
523
524 _mesa_clear_texture_object(ctx, texObj);
525 pipe_resource_reference(&stObj->pt, NULL);
526
527 /* oops, need to init this image again */
528 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
529 texImage->InternalFormat, format,
530 type);
531
532 _mesa_init_teximage_fields(ctx, texImage,
533 texImage->Width, texImage->Height,
534 texImage->Depth, texImage->Border,
535 texImage->InternalFormat, texFormat);
536
537 stObj->surface_based = GL_FALSE;
538 }
539 }
540
541
542 static void
543 st_TexImage(struct gl_context * ctx, GLuint dims,
544 struct gl_texture_image *texImage,
545 GLenum format, GLenum type, const void *pixels,
546 const struct gl_pixelstore_attrib *unpack)
547 {
548 prep_teximage(ctx, texImage, format, type);
549 _mesa_store_teximage(ctx, dims, texImage, format, type, pixels, unpack);
550 }
551
552
553 static void
554 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
555 struct gl_texture_image *texImage,
556 GLsizei imageSize, const GLvoid *data)
557 {
558 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
559 _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
560 }
561
562
563
564
565 /**
566 * Called via ctx->Driver.GetTexImage()
567 *
568 * This uses a blit to copy the texture to a texture format which matches
569 * the format and type combo and then a fast read-back is done using memcpy.
570 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
571 * a format which matches the swizzling.
572 *
573 * If such a format isn't available, it falls back to _mesa_get_teximage.
574 *
575 * NOTE: Drivers usually do a blit to convert between tiled and linear
576 * texture layouts during texture uploads/downloads, so the blit
577 * we do here should be free in such cases.
578 */
579 static void
580 st_GetTexImage(struct gl_context * ctx,
581 GLenum format, GLenum type, GLvoid * pixels,
582 struct gl_texture_image *texImage)
583 {
584 struct st_context *st = st_context(ctx);
585 struct pipe_context *pipe = st->pipe;
586 struct pipe_screen *screen = pipe->screen;
587 const GLuint width = texImage->Width;
588 const GLuint height = texImage->Height;
589 const GLuint depth = texImage->Depth;
590 struct st_texture_image *stImage = st_texture_image(texImage);
591 struct pipe_resource *src = st_texture_object(texImage->TexObject)->pt;
592 struct pipe_resource *dst = NULL;
593 struct pipe_resource dst_templ;
594 enum pipe_format dst_format, src_format;
595 gl_format mesa_format;
596 GLenum gl_target = texImage->TexObject->Target;
597 enum pipe_texture_target pipe_target;
598 struct pipe_blit_info blit;
599 unsigned bind = PIPE_BIND_TRANSFER_READ;
600 struct pipe_transfer *tex_xfer;
601 ubyte *map = NULL;
602 boolean done = FALSE;
603
604 if (!stImage->pt) {
605 goto fallback;
606 }
607
608 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
609 * due to an incomplete stencil blit implementation in some drivers. */
610 if (format == GL_DEPTH_STENCIL) {
611 goto fallback;
612 }
613
614 /* If the base internal format and the texture format don't match, we have
615 * to fall back to _mesa_get_teximage. */
616 if (texImage->_BaseFormat !=
617 _mesa_get_format_base_format(texImage->TexFormat)) {
618 goto fallback;
619 }
620
621 /* See if the texture format already matches the format and type,
622 * in which case the memcpy-based fast path will be used. */
623 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
624 type, ctx->Pack.SwapBytes)) {
625 goto fallback;
626 }
627
628 /* Convert the source format to what is expected by GetTexImage
629 * and see if it's supported.
630 *
631 * This only applies to glGetTexImage:
632 * - Luminance must be returned as (L,0,0,1).
633 * - Luminance alpha must be returned as (L,0,0,A).
634 * - Intensity must be returned as (I,0,0,1)
635 */
636 src_format = util_format_linear(src->format);
637 src_format = util_format_luminance_to_red(src_format);
638 src_format = util_format_intensity_to_red(src_format);
639
640 if (!src_format ||
641 !screen->is_format_supported(screen, src_format, src->target,
642 src->nr_samples,
643 PIPE_BIND_SAMPLER_VIEW)) {
644 goto fallback;
645 }
646
647 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
648 bind |= PIPE_BIND_DEPTH_STENCIL;
649 else
650 bind |= PIPE_BIND_RENDER_TARGET;
651
652 /* GetTexImage only returns a single face for cubemaps. */
653 if (gl_target == GL_TEXTURE_CUBE_MAP) {
654 gl_target = GL_TEXTURE_2D;
655 }
656 pipe_target = gl_target_to_pipe(gl_target);
657
658 /* Choose the destination format by finding the best match
659 * for the format+type combo. */
660 dst_format = st_choose_matching_format(screen, bind, format, type,
661 ctx->Pack.SwapBytes);
662
663 if (dst_format == PIPE_FORMAT_NONE) {
664 GLenum dst_glformat;
665
666 /* Fall back to _mesa_get_teximage except for compressed formats,
667 * where decompression with a blit is always preferred. */
668 if (!util_format_is_compressed(src->format)) {
669 goto fallback;
670 }
671
672 /* Set the appropriate format for the decompressed texture.
673 * Luminance and sRGB formats shouldn't appear here.*/
674 switch (src_format) {
675 case PIPE_FORMAT_DXT1_RGB:
676 case PIPE_FORMAT_DXT1_RGBA:
677 case PIPE_FORMAT_DXT3_RGBA:
678 case PIPE_FORMAT_DXT5_RGBA:
679 case PIPE_FORMAT_RGTC1_UNORM:
680 case PIPE_FORMAT_RGTC2_UNORM:
681 case PIPE_FORMAT_ETC1_RGB8:
682 dst_glformat = GL_RGBA8;
683 break;
684 case PIPE_FORMAT_RGTC1_SNORM:
685 case PIPE_FORMAT_RGTC2_SNORM:
686 if (!ctx->Extensions.EXT_texture_snorm)
687 goto fallback;
688 dst_glformat = GL_RGBA8_SNORM;
689 break;
690 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
691 default:
692 assert(0);
693 goto fallback;
694 }
695
696 dst_format = st_choose_format(st, dst_glformat, format, type,
697 pipe_target, 0, bind, FALSE);
698
699 if (dst_format == PIPE_FORMAT_NONE) {
700 /* unable to get an rgba format!?! */
701 goto fallback;
702 }
703 }
704
705 /* create the destination texture */
706 memset(&dst_templ, 0, sizeof(dst_templ));
707 dst_templ.target = pipe_target;
708 dst_templ.format = dst_format;
709 dst_templ.bind = bind;
710 dst_templ.usage = PIPE_USAGE_STAGING;
711
712 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
713 &dst_templ.width0, &dst_templ.height0,
714 &dst_templ.depth0, &dst_templ.array_size);
715
716 dst = screen->resource_create(screen, &dst_templ);
717 if (!dst) {
718 goto fallback;
719 }
720
721 blit.src.resource = src;
722 blit.src.level = texImage->Level;
723 blit.src.format = src_format;
724 blit.dst.resource = dst;
725 blit.dst.level = 0;
726 blit.dst.format = dst->format;
727 blit.src.box.x = blit.dst.box.x = 0;
728 blit.src.box.y = blit.dst.box.y = 0;
729 blit.src.box.z = texImage->Face;
730 blit.dst.box.z = 0;
731 blit.src.box.width = blit.dst.box.width = width;
732 blit.src.box.height = blit.dst.box.height = height;
733 blit.src.box.depth = blit.dst.box.depth = depth;
734 blit.mask = PIPE_MASK_RGBA;
735 blit.filter = PIPE_TEX_FILTER_NEAREST;
736 blit.scissor_enable = FALSE;
737
738 /* blit/render/decompress */
739 st->pipe->blit(st->pipe, &blit);
740
741 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
742
743 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
744 0, 0, 0, width, height, depth, &tex_xfer);
745 if (!map) {
746 goto end;
747 }
748
749 mesa_format = st_pipe_format_to_mesa_format(dst_format);
750
751 /* copy/pack data into user buffer */
752 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
753 ctx->Pack.SwapBytes)) {
754 /* memcpy */
755 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
756 GLuint row, slice;
757
758 for (slice = 0; slice < depth; slice++) {
759 ubyte *slice_map = map;
760
761 for (row = 0; row < height; row++) {
762 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
763 width, height, format,
764 type, slice, row, 0);
765 memcpy(dest, slice_map, bytesPerRow);
766 slice_map += tex_xfer->stride;
767 }
768 map += tex_xfer->layer_stride;
769 }
770 }
771 else {
772 /* format translation via floats */
773 GLuint row, slice;
774 GLfloat *rgba;
775
776 assert(util_format_is_compressed(src->format));
777
778 rgba = malloc(width * 4 * sizeof(GLfloat));
779 if (!rgba) {
780 goto end;
781 }
782
783 for (slice = 0; slice < depth; slice++) {
784 for (row = 0; row < height; row++) {
785 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
786 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
787 width, height, format,
788 type, slice, row, 0);
789
790 if (ST_DEBUG & DEBUG_FALLBACK)
791 debug_printf("%s: fallback format translation\n", __FUNCTION__);
792
793 /* get float[4] rgba row from surface */
794 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
795 dst_format, rgba);
796
797 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
798 type, dest, &ctx->Pack, transferOps);
799 }
800 map += tex_xfer->layer_stride;
801 }
802
803 free(rgba);
804 }
805 done = TRUE;
806
807 end:
808 if (map)
809 pipe_transfer_unmap(pipe, tex_xfer);
810
811 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
812 pipe_resource_reference(&dst, NULL);
813
814 fallback:
815 if (!done) {
816 _mesa_get_teximage(ctx, format, type, pixels, texImage);
817 }
818 }
819
820
821 /**
822 * Do a CopyTexSubImage operation using a read transfer from the source,
823 * a write transfer to the destination and get_tile()/put_tile() to access
824 * the pixels/texels.
825 *
826 * Note: srcY=0=TOP of renderbuffer
827 */
828 static void
829 fallback_copy_texsubimage(struct gl_context *ctx,
830 struct st_renderbuffer *strb,
831 struct st_texture_image *stImage,
832 GLenum baseFormat,
833 GLint destX, GLint destY, GLint destZ,
834 GLint srcX, GLint srcY,
835 GLsizei width, GLsizei height)
836 {
837 struct st_context *st = st_context(ctx);
838 struct pipe_context *pipe = st->pipe;
839 struct pipe_transfer *src_trans;
840 GLubyte *texDest;
841 enum pipe_transfer_usage transfer_usage;
842 void *map;
843 unsigned dst_width = width;
844 unsigned dst_height = height;
845 unsigned dst_depth = 1;
846
847 if (ST_DEBUG & DEBUG_FALLBACK)
848 debug_printf("%s: fallback processing\n", __FUNCTION__);
849
850 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
851 srcY = strb->Base.Height - srcY - height;
852 }
853
854 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
855 /* Move y/height to z/depth for 1D array textures. */
856 destZ = destY;
857 destY = 0;
858 dst_depth = dst_height;
859 dst_height = 1;
860 }
861
862 map = pipe_transfer_map(pipe,
863 strb->texture,
864 strb->rtt_level,
865 strb->rtt_face + strb->rtt_slice,
866 PIPE_TRANSFER_READ,
867 srcX, srcY,
868 width, height, &src_trans);
869
870 if ((baseFormat == GL_DEPTH_COMPONENT ||
871 baseFormat == GL_DEPTH_STENCIL) &&
872 util_format_is_depth_and_stencil(stImage->pt->format))
873 transfer_usage = PIPE_TRANSFER_READ_WRITE;
874 else
875 transfer_usage = PIPE_TRANSFER_WRITE;
876
877 texDest = st_texture_image_map(st, stImage, transfer_usage,
878 destX, destY, destZ,
879 dst_width, dst_height, dst_depth);
880
881 if (baseFormat == GL_DEPTH_COMPONENT ||
882 baseFormat == GL_DEPTH_STENCIL) {
883 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
884 ctx->Pixel.DepthBias != 0.0F);
885 GLint row, yStep;
886 uint *data;
887
888 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
889 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
890 srcY = height - 1;
891 yStep = -1;
892 }
893 else {
894 srcY = 0;
895 yStep = 1;
896 }
897
898 data = malloc(width * sizeof(uint));
899
900 if (data) {
901 /* To avoid a large temp memory allocation, do copy row by row */
902 for (row = 0; row < height; row++, srcY += yStep) {
903 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
904 if (scaleOrBias) {
905 _mesa_scale_and_bias_depth_uint(ctx, width, data);
906 }
907
908 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
909 pipe_put_tile_z(stImage->transfer,
910 texDest + row*stImage->transfer->layer_stride,
911 0, 0, width, 1, data);
912 }
913 else {
914 pipe_put_tile_z(stImage->transfer, texDest, 0, row, width, 1,
915 data);
916 }
917 }
918 }
919 else {
920 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
921 }
922
923 free(data);
924 }
925 else {
926 /* RGBA format */
927 GLfloat *tempSrc =
928 malloc(width * height * 4 * sizeof(GLfloat));
929
930 if (tempSrc && texDest) {
931 const GLint dims = 2;
932 GLint dstRowStride;
933 struct gl_texture_image *texImage = &stImage->base;
934 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
935
936 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
937 unpack.Invert = GL_TRUE;
938 }
939
940 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
941 dstRowStride = stImage->transfer->layer_stride;
942 }
943 else {
944 dstRowStride = stImage->transfer->stride;
945 }
946
947 /* get float/RGBA image from framebuffer */
948 /* XXX this usually involves a lot of int/float conversion.
949 * try to avoid that someday.
950 */
951 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
952 util_format_linear(strb->texture->format),
953 tempSrc);
954
955 /* Store into texture memory.
956 * Note that this does some special things such as pixel transfer
957 * ops and format conversion. In particular, if the dest tex format
958 * is actually RGBA but the user created the texture as GL_RGB we
959 * need to fill-in/override the alpha channel with 1.0.
960 */
961 _mesa_texstore(ctx, dims,
962 texImage->_BaseFormat,
963 texImage->TexFormat,
964 dstRowStride,
965 &texDest,
966 width, height, 1,
967 GL_RGBA, GL_FLOAT, tempSrc, /* src */
968 &unpack);
969 }
970 else {
971 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
972 }
973
974 free(tempSrc);
975 }
976
977 st_texture_image_unmap(st, stImage);
978 pipe->transfer_unmap(pipe, src_trans);
979 }
980
981
982
983 /**
984 * If the format of the src renderbuffer and the format of the dest
985 * texture are compatible (in terms of blitting), return a TGSI writemask
986 * to be used during the blit.
987 * If the src/dest are incompatible, return 0.
988 */
989 static unsigned
990 compatible_src_dst_formats(struct gl_context *ctx,
991 const struct gl_renderbuffer *src,
992 const struct gl_texture_image *dst)
993 {
994 /* Get logical base formats for the src and dest.
995 * That is, use the user-requested formats and not the actual, device-
996 * chosen formats.
997 * For example, the user may have requested an A8 texture but the
998 * driver may actually be using an RGBA texture format. When we
999 * copy/blit to that texture, we only want to copy the Alpha channel
1000 * and not the RGB channels.
1001 *
1002 * Similarly, when the src FBO was created an RGB format may have been
1003 * requested but the driver actually chose an RGBA format. In that case,
1004 * we don't want to copy the undefined Alpha channel to the dest texture
1005 * (it should be 1.0).
1006 */
1007 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1008 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1009
1010 /**
1011 * XXX when we have red-only and red/green renderbuffers we'll need
1012 * to add more cases here (or implement a general-purpose routine that
1013 * queries the existance of the R,G,B,A channels in the src and dest).
1014 */
1015 if (srcFormat == dstFormat) {
1016 /* This is the same as matching_base_formats, which should
1017 * always pass, as it did previously.
1018 */
1019 return TGSI_WRITEMASK_XYZW;
1020 }
1021 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1022 /* Make sure that A in the dest is 1. The actual src format
1023 * may be RGBA and have undefined A values.
1024 */
1025 return TGSI_WRITEMASK_XYZ;
1026 }
1027 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1028 /* Make sure that A in the dest is 1. The actual dst format
1029 * may be RGBA and will need A=1 to provide proper alpha values
1030 * when sampled later.
1031 */
1032 return TGSI_WRITEMASK_XYZ;
1033 }
1034 else {
1035 if (ST_DEBUG & DEBUG_FALLBACK)
1036 debug_printf("%s failed for src %s, dst %s\n",
1037 __FUNCTION__,
1038 _mesa_lookup_enum_by_nr(srcFormat),
1039 _mesa_lookup_enum_by_nr(dstFormat));
1040
1041 /* Otherwise fail.
1042 */
1043 return 0;
1044 }
1045 }
1046
1047
1048 /**
1049 * Do pipe->blit. Return FALSE if the blitting is unsupported
1050 * for the given formats.
1051 */
1052 static GLboolean
1053 st_pipe_blit(struct pipe_context *pipe, struct pipe_blit_info *blit)
1054 {
1055 struct pipe_screen *screen = pipe->screen;
1056 unsigned dst_usage;
1057
1058 if (util_format_is_depth_or_stencil(blit->dst.format)) {
1059 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1060 }
1061 else {
1062 dst_usage = PIPE_BIND_RENDER_TARGET;
1063 }
1064
1065 /* try resource_copy_region in case the format is not supported
1066 * for rendering */
1067 if (util_try_blit_via_copy_region(pipe, blit)) {
1068 return GL_TRUE; /* done */
1069 }
1070
1071 /* check the format support */
1072 if (!screen->is_format_supported(screen, blit->src.format,
1073 PIPE_TEXTURE_2D, 0,
1074 PIPE_BIND_SAMPLER_VIEW) ||
1075 !screen->is_format_supported(screen, blit->dst.format,
1076 PIPE_TEXTURE_2D, 0,
1077 dst_usage)) {
1078 return GL_FALSE;
1079 }
1080
1081 pipe->blit(pipe, blit);
1082 return GL_TRUE;
1083 }
1084
1085
1086 /**
1087 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1088 * Note that the region to copy has already been clipped so we know we
1089 * won't read from outside the source renderbuffer's bounds.
1090 *
1091 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1092 */
1093 static void
1094 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
1095 struct gl_texture_image *texImage,
1096 GLint destX, GLint destY, GLint destZ,
1097 struct gl_renderbuffer *rb,
1098 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
1099 {
1100 struct st_texture_image *stImage = st_texture_image(texImage);
1101 const GLenum texBaseFormat = texImage->_BaseFormat;
1102 struct st_renderbuffer *strb = st_renderbuffer(rb);
1103 struct st_context *st = st_context(ctx);
1104 struct pipe_context *pipe = st->pipe;
1105 struct pipe_screen *screen = pipe->screen;
1106 enum pipe_format dest_format, src_format;
1107 GLuint color_writemask;
1108 struct pipe_surface *dest_surface = NULL;
1109 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1110 struct pipe_surface surf_tmpl;
1111 unsigned dst_usage;
1112 unsigned blit_mask;
1113 GLint srcY0, srcY1, yStep;
1114
1115 /* make sure finalize_textures has been called?
1116 */
1117 if (0) st_validate_state(st);
1118
1119 if (!strb || !strb->surface || !stImage->pt) {
1120 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1121 return;
1122 }
1123
1124 assert(strb);
1125 assert(strb->surface);
1126 assert(stImage->pt);
1127
1128 src_format = strb->surface->format;
1129 dest_format = stImage->pt->format;
1130
1131 if (do_flip) {
1132 srcY1 = strb->Base.Height - srcY - height;
1133 srcY0 = srcY1 + height;
1134 yStep = -1;
1135 }
1136 else {
1137 srcY0 = srcY;
1138 srcY1 = srcY0 + height;
1139 yStep = 1;
1140 }
1141
1142 if (ctx->_ImageTransferState) {
1143 goto fallback;
1144 }
1145
1146 /* Compressed and subsampled textures aren't supported for blitting. */
1147 if (!util_format_is_plain(dest_format)) {
1148 goto fallback;
1149 }
1150
1151 /* Set the blit writemask. */
1152 switch (texBaseFormat) {
1153 case GL_DEPTH_STENCIL:
1154 switch (strb->Base._BaseFormat) {
1155 case GL_DEPTH_STENCIL:
1156 blit_mask = PIPE_MASK_ZS;
1157 break;
1158 case GL_DEPTH_COMPONENT:
1159 blit_mask = PIPE_MASK_Z;
1160 break;
1161 case GL_STENCIL_INDEX:
1162 blit_mask = PIPE_MASK_S;
1163 break;
1164 default:
1165 assert(0);
1166 return;
1167 }
1168 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1169 break;
1170
1171 case GL_DEPTH_COMPONENT:
1172 blit_mask = PIPE_MASK_Z;
1173 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1174 break;
1175
1176 default:
1177 /* Colorbuffers.
1178 *
1179 * Determine if the src framebuffer and dest texture have the same
1180 * base format. We need this to detect a case such as the framebuffer
1181 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1182 * texture format stores RGBA we need to set A=1 (overriding the
1183 * framebuffer's alpha values).
1184 *
1185 * XXX util_blit_pixels doesn't support MSAA resolve, so always use
1186 * pipe->blit
1187 */
1188 if (texBaseFormat == strb->Base._BaseFormat ||
1189 strb->texture->nr_samples > 1) {
1190 blit_mask = PIPE_MASK_RGBA;
1191 }
1192 else {
1193 blit_mask = 0;
1194 }
1195 dst_usage = PIPE_BIND_RENDER_TARGET;
1196 }
1197
1198 /* Blit the texture.
1199 * This supports flipping, format conversions, and downsampling.
1200 */
1201 if (blit_mask) {
1202 /* If stImage->pt is an independent image (not a pointer into a full
1203 * mipmap) stImage->pt.last_level will be zero and we need to use that
1204 * as the dest level.
1205 */
1206 unsigned dstLevel = MIN2(stImage->base.Level, stImage->pt->last_level);
1207 struct pipe_blit_info blit;
1208
1209 memset(&blit, 0, sizeof(blit));
1210 blit.src.resource = strb->texture;
1211 blit.src.format = src_format;
1212 blit.src.level = strb->surface->u.tex.level;
1213 blit.src.box.x = srcX;
1214 blit.src.box.y = srcY0;
1215 blit.src.box.z = strb->surface->u.tex.first_layer;
1216 blit.src.box.width = width;
1217 blit.src.box.height = srcY1 - srcY0;
1218 blit.src.box.depth = 1;
1219 blit.dst.resource = stImage->pt;
1220 blit.dst.format = dest_format;
1221 blit.dst.level = dstLevel;
1222 blit.dst.box.x = destX;
1223 blit.dst.box.y = destY;
1224 blit.dst.box.z = stImage->base.Face + destZ;
1225 blit.dst.box.width = width;
1226 blit.dst.box.height = height;
1227 blit.dst.box.depth = 1;
1228 blit.mask = blit_mask;
1229 blit.filter = PIPE_TEX_FILTER_NEAREST;
1230
1231 /* 1D array textures need special treatment.
1232 * Blit rows from the source to layers in the destination. */
1233 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
1234 int y, layer;
1235
1236 for (y = srcY0, layer = 0; layer < height; y += yStep, layer++) {
1237 blit.src.box.y = y;
1238 blit.src.box.height = 1;
1239 blit.dst.box.y = 0;
1240 blit.dst.box.height = 1;
1241 blit.dst.box.z = destY + layer;
1242
1243 if (!st_pipe_blit(pipe, &blit)) {
1244 goto fallback;
1245 }
1246 }
1247 }
1248 else {
1249 /* All the other texture targets. */
1250 if (!st_pipe_blit(pipe, &blit)) {
1251 goto fallback;
1252 }
1253 }
1254 return;
1255 }
1256
1257 /* try u_blit */
1258 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
1259 /* u_blit cannot copy 1D array textures as required by CopyTexSubImage */
1260 goto fallback;
1261 }
1262
1263 color_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1264
1265 if (!color_writemask ||
1266 !screen->is_format_supported(screen, src_format,
1267 PIPE_TEXTURE_2D, 0,
1268 PIPE_BIND_SAMPLER_VIEW) ||
1269 !screen->is_format_supported(screen, dest_format,
1270 PIPE_TEXTURE_2D, 0,
1271 dst_usage)) {
1272 goto fallback;
1273 }
1274
1275 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1276 surf_tmpl.format = util_format_linear(stImage->pt->format);
1277 surf_tmpl.u.tex.level = stImage->base.Level;
1278 surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1279 surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1280
1281 dest_surface = pipe->create_surface(pipe, stImage->pt,
1282 &surf_tmpl);
1283 util_blit_pixels(st->blit,
1284 strb->texture,
1285 strb->surface->u.tex.level,
1286 srcX, srcY0,
1287 srcX + width, srcY1,
1288 strb->surface->u.tex.first_layer,
1289 dest_surface,
1290 destX, destY,
1291 destX + width, destY + height,
1292 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1293 color_writemask, 0);
1294 pipe_surface_reference(&dest_surface, NULL);
1295 return;
1296
1297 fallback:
1298 /* software fallback */
1299 fallback_copy_texsubimage(ctx,
1300 strb, stImage, texBaseFormat,
1301 destX, destY, destZ,
1302 srcX, srcY, width, height);
1303 }
1304
1305
1306 /**
1307 * Copy image data from stImage into the texture object 'stObj' at level
1308 * 'dstLevel'.
1309 */
1310 static void
1311 copy_image_data_to_texture(struct st_context *st,
1312 struct st_texture_object *stObj,
1313 GLuint dstLevel,
1314 struct st_texture_image *stImage)
1315 {
1316 /* debug checks */
1317 {
1318 const struct gl_texture_image *dstImage =
1319 stObj->base.Image[stImage->base.Face][dstLevel];
1320 assert(dstImage);
1321 assert(dstImage->Width == stImage->base.Width);
1322 assert(dstImage->Height == stImage->base.Height);
1323 assert(dstImage->Depth == stImage->base.Depth);
1324 }
1325
1326 if (stImage->pt) {
1327 /* Copy potentially with the blitter:
1328 */
1329 GLuint src_level;
1330 if (stImage->pt->last_level == 0)
1331 src_level = 0;
1332 else
1333 src_level = stImage->base.Level;
1334
1335 assert(src_level <= stImage->pt->last_level);
1336 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1337 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1338 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1339 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1340 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
1341 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1342
1343 st_texture_image_copy(st->pipe,
1344 stObj->pt, dstLevel, /* dest texture, level */
1345 stImage->pt, src_level, /* src texture, level */
1346 stImage->base.Face);
1347
1348 pipe_resource_reference(&stImage->pt, NULL);
1349 }
1350 else if (stImage->TexData) {
1351 /* Copy from malloc'd memory */
1352 /* XXX this should be re-examined/tested with a compressed format */
1353 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1354 GLuint srcRowStride = stImage->base.Width * blockSize;
1355 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1356 st_texture_image_data(st,
1357 stObj->pt,
1358 stImage->base.Face,
1359 dstLevel,
1360 stImage->TexData,
1361 srcRowStride,
1362 srcSliceStride);
1363 _mesa_align_free(stImage->TexData);
1364 stImage->TexData = NULL;
1365 }
1366
1367 pipe_resource_reference(&stImage->pt, stObj->pt);
1368 }
1369
1370
1371 /**
1372 * Called during state validation. When this function is finished,
1373 * the texture object should be ready for rendering.
1374 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1375 */
1376 GLboolean
1377 st_finalize_texture(struct gl_context *ctx,
1378 struct pipe_context *pipe,
1379 struct gl_texture_object *tObj)
1380 {
1381 struct st_context *st = st_context(ctx);
1382 struct st_texture_object *stObj = st_texture_object(tObj);
1383 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1384 GLuint face;
1385 struct st_texture_image *firstImage;
1386 enum pipe_format firstImageFormat;
1387 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1388
1389 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1390 /* The texture is complete and we know exactly how many mipmap levels
1391 * are present/needed. This is conditional because we may be called
1392 * from the st_generate_mipmap() function when the texture object is
1393 * incomplete. In that case, we'll have set stObj->lastLevel before
1394 * we get here.
1395 */
1396 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1397 stObj->base.Sampler.MinFilter == GL_NEAREST)
1398 stObj->lastLevel = stObj->base.BaseLevel;
1399 else
1400 stObj->lastLevel = stObj->base._MaxLevel;
1401 }
1402
1403 if (tObj->Target == GL_TEXTURE_BUFFER) {
1404 struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
1405
1406 if (st_obj->buffer != stObj->pt) {
1407 pipe_resource_reference(&stObj->pt, st_obj->buffer);
1408 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1409 stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
1410 stObj->height0 = 1;
1411 stObj->depth0 = 1;
1412 }
1413 return GL_TRUE;
1414
1415 }
1416
1417 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1418 assert(firstImage);
1419
1420 /* If both firstImage and stObj point to a texture which can contain
1421 * all active images, favour firstImage. Note that because of the
1422 * completeness requirement, we know that the image dimensions
1423 * will match.
1424 */
1425 if (firstImage->pt &&
1426 firstImage->pt != stObj->pt &&
1427 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1428 pipe_resource_reference(&stObj->pt, firstImage->pt);
1429 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1430 }
1431
1432 /* Find gallium format for the Mesa texture */
1433 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1434
1435 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1436 {
1437 GLuint width, height, depth;
1438 if (!guess_base_level_size(stObj->base.Target,
1439 firstImage->base.Width2,
1440 firstImage->base.Height2,
1441 firstImage->base.Depth2,
1442 firstImage->base.Level,
1443 &width, &height, &depth)) {
1444 width = stObj->width0;
1445 height = stObj->height0;
1446 depth = stObj->depth0;
1447 }
1448 /* convert GL dims to Gallium dims */
1449 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1450 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1451 }
1452
1453 /* If we already have a gallium texture, check that it matches the texture
1454 * object's format, target, size, num_levels, etc.
1455 */
1456 if (stObj->pt) {
1457 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1458 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1459 stObj->pt->last_level < stObj->lastLevel ||
1460 stObj->pt->width0 != ptWidth ||
1461 stObj->pt->height0 != ptHeight ||
1462 stObj->pt->depth0 != ptDepth ||
1463 stObj->pt->array_size != ptLayers)
1464 {
1465 /* The gallium texture does not match the Mesa texture so delete the
1466 * gallium texture now. We'll make a new one below.
1467 */
1468 pipe_resource_reference(&stObj->pt, NULL);
1469 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1470 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1471 }
1472 }
1473
1474 /* May need to create a new gallium texture:
1475 */
1476 if (!stObj->pt) {
1477 GLuint bindings = default_bindings(st, firstImageFormat);
1478
1479 stObj->pt = st_texture_create(st,
1480 gl_target_to_pipe(stObj->base.Target),
1481 firstImageFormat,
1482 stObj->lastLevel,
1483 ptWidth,
1484 ptHeight,
1485 ptDepth,
1486 ptLayers,
1487 bindings);
1488
1489 if (!stObj->pt) {
1490 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1491 return GL_FALSE;
1492 }
1493 }
1494
1495 /* Pull in any images not in the object's texture:
1496 */
1497 for (face = 0; face < nr_faces; face++) {
1498 GLuint level;
1499 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1500 struct st_texture_image *stImage =
1501 st_texture_image(stObj->base.Image[face][level]);
1502
1503 /* Need to import images in main memory or held in other textures.
1504 */
1505 if (stImage && stObj->pt != stImage->pt) {
1506 if (level == 0 ||
1507 (stImage->base.Width == u_minify(stObj->width0, level) &&
1508 stImage->base.Height == u_minify(stObj->height0, level) &&
1509 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1510 /* src image fits expected dest mipmap level size */
1511 copy_image_data_to_texture(st, stObj, level, stImage);
1512 }
1513 }
1514 }
1515 }
1516
1517 return GL_TRUE;
1518 }
1519
1520
1521 /**
1522 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1523 * for a whole mipmap stack.
1524 */
1525 static GLboolean
1526 st_AllocTextureStorage(struct gl_context *ctx,
1527 struct gl_texture_object *texObj,
1528 GLsizei levels, GLsizei width,
1529 GLsizei height, GLsizei depth)
1530 {
1531 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1532 struct st_context *st = st_context(ctx);
1533 struct st_texture_object *stObj = st_texture_object(texObj);
1534 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1535 enum pipe_format fmt;
1536 GLint level;
1537
1538 assert(levels > 0);
1539
1540 /* Save the level=0 dimensions */
1541 stObj->width0 = width;
1542 stObj->height0 = height;
1543 stObj->depth0 = depth;
1544 stObj->lastLevel = levels - 1;
1545
1546 fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1547
1548 bindings = default_bindings(st, fmt);
1549
1550 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1551 width, height, depth,
1552 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1553
1554 stObj->pt = st_texture_create(st,
1555 gl_target_to_pipe(texObj->Target),
1556 fmt,
1557 levels,
1558 ptWidth,
1559 ptHeight,
1560 ptDepth,
1561 ptLayers,
1562 bindings);
1563 if (!stObj->pt)
1564 return GL_FALSE;
1565
1566 /* Set image resource pointers */
1567 for (level = 0; level < levels; level++) {
1568 GLuint face;
1569 for (face = 0; face < numFaces; face++) {
1570 struct st_texture_image *stImage =
1571 st_texture_image(texObj->Image[face][level]);
1572 pipe_resource_reference(&stImage->pt, stObj->pt);
1573 }
1574 }
1575
1576 return GL_TRUE;
1577 }
1578
1579
1580 static GLboolean
1581 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
1582 GLint level, gl_format format,
1583 GLint width, GLint height,
1584 GLint depth, GLint border)
1585 {
1586 struct st_context *st = st_context(ctx);
1587 struct pipe_context *pipe = st->pipe;
1588
1589 if (width == 0 || height == 0 || depth == 0) {
1590 /* zero-sized images are legal, and always fit! */
1591 return GL_TRUE;
1592 }
1593
1594 if (pipe->screen->can_create_resource) {
1595 /* Ask the gallium driver if the texture is too large */
1596 struct gl_texture_object *texObj =
1597 _mesa_get_current_tex_object(ctx, target);
1598 struct pipe_resource pt;
1599
1600 /* Setup the pipe_resource object
1601 */
1602 memset(&pt, 0, sizeof(pt));
1603
1604 pt.target = gl_target_to_pipe(target);
1605 pt.format = st_mesa_format_to_pipe_format(format);
1606
1607 st_gl_texture_dims_to_pipe_dims(target,
1608 width, height, depth,
1609 &pt.width0, &pt.height0,
1610 &pt.depth0, &pt.array_size);
1611
1612 if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
1613 texObj->Sampler.MinFilter == GL_NEAREST)) {
1614 /* assume just one mipmap level */
1615 pt.last_level = 0;
1616 }
1617 else {
1618 /* assume a full set of mipmaps */
1619 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
1620 }
1621
1622 return pipe->screen->can_create_resource(pipe->screen, &pt);
1623 }
1624 else {
1625 /* Use core Mesa fallback */
1626 return _mesa_test_proxy_teximage(ctx, target, level, format,
1627 width, height, depth, border);
1628 }
1629 }
1630
1631
1632 void
1633 st_init_texture_functions(struct dd_function_table *functions)
1634 {
1635 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1636 functions->QuerySamplesForFormat = st_QuerySamplesForFormat;
1637 functions->TexImage = st_TexImage;
1638 functions->TexSubImage = _mesa_store_texsubimage;
1639 functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1640 functions->CopyTexSubImage = st_CopyTexSubImage;
1641 functions->GenerateMipmap = st_generate_mipmap;
1642
1643 functions->GetTexImage = st_GetTexImage;
1644
1645 /* compressed texture functions */
1646 functions->CompressedTexImage = st_CompressedTexImage;
1647 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1648
1649 functions->NewTextureObject = st_NewTextureObject;
1650 functions->NewTextureImage = st_NewTextureImage;
1651 functions->DeleteTextureImage = st_DeleteTextureImage;
1652 functions->DeleteTexture = st_DeleteTextureObject;
1653 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1654 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1655 functions->MapTextureImage = st_MapTextureImage;
1656 functions->UnmapTextureImage = st_UnmapTextureImage;
1657
1658 /* XXX Temporary until we can query pipe's texture sizes */
1659 functions->TestProxyTexImage = st_TestProxyTexImage;
1660
1661 functions->AllocTextureStorage = st_AllocTextureStorage;
1662 }