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