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