st/mesa: add ETC2 & ASTC fast path for GetTex(Sub)Image
[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 <stdio.h>
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/errors.h"
32 #include "main/fbobject.h"
33 #include "main/formats.h"
34 #include "main/format_utils.h"
35 #include "main/glformats.h"
36 #include "main/image.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/pack.h"
41 #include "main/pbo.h"
42 #include "main/pixeltransfer.h"
43 #include "main/texcompress.h"
44 #include "main/texcompress_etc.h"
45 #include "main/texgetimage.h"
46 #include "main/teximage.h"
47 #include "main/texobj.h"
48 #include "main/texstore.h"
49
50 #include "state_tracker/st_debug.h"
51 #include "state_tracker/st_context.h"
52 #include "state_tracker/st_cb_bitmap.h"
53 #include "state_tracker/st_cb_fbo.h"
54 #include "state_tracker/st_cb_flush.h"
55 #include "state_tracker/st_cb_texture.h"
56 #include "state_tracker/st_cb_bufferobjects.h"
57 #include "state_tracker/st_cb_memoryobjects.h"
58 #include "state_tracker/st_format.h"
59 #include "state_tracker/st_pbo.h"
60 #include "state_tracker/st_texture.h"
61 #include "state_tracker/st_gen_mipmap.h"
62 #include "state_tracker/st_atom.h"
63 #include "state_tracker/st_sampler_view.h"
64
65 #include "pipe/p_context.h"
66 #include "pipe/p_defines.h"
67 #include "util/u_inlines.h"
68 #include "util/u_upload_mgr.h"
69 #include "pipe/p_shader_tokens.h"
70 #include "util/u_tile.h"
71 #include "util/u_format.h"
72 #include "util/u_surface.h"
73 #include "util/u_sampler.h"
74 #include "util/u_math.h"
75 #include "util/u_box.h"
76 #include "util/u_simple_shaders.h"
77 #include "cso_cache/cso_context.h"
78 #include "tgsi/tgsi_ureg.h"
79
80 #define DBG if (0) printf
81
82
83 enum pipe_texture_target
84 gl_target_to_pipe(GLenum target)
85 {
86 switch (target) {
87 case GL_TEXTURE_1D:
88 case GL_PROXY_TEXTURE_1D:
89 return PIPE_TEXTURE_1D;
90 case GL_TEXTURE_2D:
91 case GL_PROXY_TEXTURE_2D:
92 case GL_TEXTURE_EXTERNAL_OES:
93 case GL_TEXTURE_2D_MULTISAMPLE:
94 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
95 return PIPE_TEXTURE_2D;
96 case GL_TEXTURE_RECTANGLE_NV:
97 case GL_PROXY_TEXTURE_RECTANGLE_NV:
98 return PIPE_TEXTURE_RECT;
99 case GL_TEXTURE_3D:
100 case GL_PROXY_TEXTURE_3D:
101 return PIPE_TEXTURE_3D;
102 case GL_TEXTURE_CUBE_MAP_ARB:
103 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
104 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
105 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
106 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
107 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
108 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
109 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
110 return PIPE_TEXTURE_CUBE;
111 case GL_TEXTURE_1D_ARRAY_EXT:
112 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
113 return PIPE_TEXTURE_1D_ARRAY;
114 case GL_TEXTURE_2D_ARRAY_EXT:
115 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
116 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
117 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
118 return PIPE_TEXTURE_2D_ARRAY;
119 case GL_TEXTURE_BUFFER:
120 return PIPE_BUFFER;
121 case GL_TEXTURE_CUBE_MAP_ARRAY:
122 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
123 return PIPE_TEXTURE_CUBE_ARRAY;
124 default:
125 assert(0);
126 return 0;
127 }
128 }
129
130
131 /** called via ctx->Driver.NewTextureImage() */
132 static struct gl_texture_image *
133 st_NewTextureImage(struct gl_context * ctx)
134 {
135 DBG("%s\n", __func__);
136 (void) ctx;
137 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
138 }
139
140
141 /** called via ctx->Driver.DeleteTextureImage() */
142 static void
143 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
144 {
145 /* nothing special (yet) for st_texture_image */
146 _mesa_delete_texture_image(ctx, img);
147 }
148
149
150 /** called via ctx->Driver.NewTextureObject() */
151 static struct gl_texture_object *
152 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
153 {
154 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
155 if (!obj)
156 return NULL;
157
158 /* Pre-allocate a sampler views container to save a branch in the fast path. */
159 obj->sampler_views = calloc(1, sizeof(struct st_sampler_views) + sizeof(struct st_sampler_view));
160 if (!obj->sampler_views) {
161 free(obj);
162 return NULL;
163 }
164 obj->sampler_views->max = 1;
165
166 DBG("%s\n", __func__);
167 _mesa_initialize_texture_object(ctx, &obj->base, name, target);
168
169 simple_mtx_init(&obj->validate_mutex, mtx_plain);
170 obj->needs_validation = true;
171
172 return &obj->base;
173 }
174
175 /** called via ctx->Driver.DeleteTextureObject() */
176 static void
177 st_DeleteTextureObject(struct gl_context *ctx,
178 struct gl_texture_object *texObj)
179 {
180 struct st_context *st = st_context(ctx);
181 struct st_texture_object *stObj = st_texture_object(texObj);
182
183 pipe_resource_reference(&stObj->pt, NULL);
184 st_texture_release_all_sampler_views(st, stObj);
185 st_texture_free_sampler_views(stObj);
186 simple_mtx_destroy(&stObj->validate_mutex);
187 _mesa_delete_texture_object(ctx, texObj);
188 }
189
190
191 /** called via ctx->Driver.FreeTextureImageBuffer() */
192 static void
193 st_FreeTextureImageBuffer(struct gl_context *ctx,
194 struct gl_texture_image *texImage)
195 {
196 struct st_context *st = st_context(ctx);
197 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
198 struct st_texture_image *stImage = st_texture_image(texImage);
199
200 DBG("%s\n", __func__);
201
202 if (stImage->pt) {
203 pipe_resource_reference(&stImage->pt, NULL);
204 }
205
206 free(stImage->transfer);
207 stImage->transfer = NULL;
208 stImage->num_transfers = 0;
209
210 if (stImage->compressed_data) {
211 free(stImage->compressed_data);
212 stImage->compressed_data = NULL;
213 }
214
215 /* if the texture image is being deallocated, the structure of the
216 * texture is changing so we'll likely need a new sampler view.
217 */
218 st_texture_release_all_sampler_views(st, stObj);
219 }
220
221 bool
222 st_compressed_format_fallback(struct st_context *st, mesa_format format)
223 {
224 if (format == MESA_FORMAT_ETC1_RGB8)
225 return !st->has_etc1;
226
227 if (_mesa_is_format_etc2(format))
228 return !st->has_etc2;
229
230 return false;
231 }
232
233 static void
234 compressed_tex_fallback_allocate(struct st_context *st,
235 struct st_texture_image *stImage)
236 {
237 struct gl_texture_image *texImage = &stImage->base;
238
239 if (!st_compressed_format_fallback(st, texImage->TexFormat))
240 return;
241
242 if (stImage->compressed_data)
243 free(stImage->compressed_data);
244
245 unsigned data_size = _mesa_format_image_size(texImage->TexFormat,
246 texImage->Width2,
247 texImage->Height2,
248 texImage->Depth2);
249
250 stImage->compressed_data =
251 malloc(data_size * _mesa_num_tex_faces(texImage->TexObject->Target));
252 }
253
254 /** called via ctx->Driver.MapTextureImage() */
255 static void
256 st_MapTextureImage(struct gl_context *ctx,
257 struct gl_texture_image *texImage,
258 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
259 GLbitfield mode,
260 GLubyte **mapOut, GLint *rowStrideOut)
261 {
262 struct st_context *st = st_context(ctx);
263 struct st_texture_image *stImage = st_texture_image(texImage);
264 GLubyte *map;
265 struct pipe_transfer *transfer;
266
267 /* Check for unexpected flags */
268 assert((mode & ~(GL_MAP_READ_BIT |
269 GL_MAP_WRITE_BIT |
270 GL_MAP_INVALIDATE_RANGE_BIT)) == 0);
271
272 const enum pipe_transfer_usage transfer_flags =
273 st_access_flags_to_transfer_flags(mode, false);
274
275 map = st_texture_image_map(st, stImage, transfer_flags, x, y, slice, w, h, 1,
276 &transfer);
277 if (map) {
278 if (st_compressed_format_fallback(st, texImage->TexFormat)) {
279 /* Some compressed formats don't have to be supported by drivers,
280 * and st/mesa transparently handles decompression on upload (Unmap),
281 * so that drivers don't see the compressed formats.
282 *
283 * We store the compressed data (it's needed for glGetCompressedTex-
284 * Image and image copies in OES_copy_image).
285 */
286 unsigned z = transfer->box.z;
287 struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
288
289 unsigned blk_w, blk_h;
290 _mesa_get_format_block_size(texImage->TexFormat, &blk_w, &blk_h);
291
292 unsigned y_blocks = DIV_ROUND_UP(texImage->Height2, blk_h);
293 unsigned stride = *rowStrideOut = itransfer->temp_stride =
294 _mesa_format_row_stride(texImage->TexFormat, texImage->Width2);
295 unsigned block_size = _mesa_get_format_bytes(texImage->TexFormat);
296
297 *mapOut = itransfer->temp_data =
298 stImage->compressed_data +
299 (z * y_blocks + (y / blk_h)) * stride +
300 (x / blk_w) * block_size;
301 itransfer->map = map;
302 }
303 else {
304 /* supported mapping */
305 *mapOut = map;
306 *rowStrideOut = transfer->stride;
307 }
308 }
309 else {
310 *mapOut = NULL;
311 *rowStrideOut = 0;
312 }
313 }
314
315
316 /** called via ctx->Driver.UnmapTextureImage() */
317 static void
318 st_UnmapTextureImage(struct gl_context *ctx,
319 struct gl_texture_image *texImage,
320 GLuint slice)
321 {
322 struct st_context *st = st_context(ctx);
323 struct st_texture_image *stImage = st_texture_image(texImage);
324
325 if (st_compressed_format_fallback(st, texImage->TexFormat)) {
326 /* Decompress the compressed image on upload if the driver doesn't
327 * support the compressed format. */
328 unsigned z = slice + stImage->base.Face;
329 struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
330 struct pipe_transfer *transfer = itransfer->transfer;
331
332 assert(z == transfer->box.z);
333
334 if (transfer->usage & PIPE_TRANSFER_WRITE) {
335 if (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8) {
336 _mesa_etc1_unpack_rgba8888(itransfer->map, transfer->stride,
337 itransfer->temp_data,
338 itransfer->temp_stride,
339 transfer->box.width, transfer->box.height);
340 }
341 else {
342 bool bgra = stImage->pt->format == PIPE_FORMAT_B8G8R8A8_SRGB;
343 _mesa_unpack_etc2_format(itransfer->map, transfer->stride,
344 itransfer->temp_data, itransfer->temp_stride,
345 transfer->box.width, transfer->box.height,
346 texImage->TexFormat,
347 bgra);
348 }
349 }
350
351 itransfer->temp_data = NULL;
352 itransfer->temp_stride = 0;
353 itransfer->map = 0;
354 }
355
356 st_texture_image_unmap(st, stImage, slice);
357 }
358
359
360 /**
361 * Return default texture resource binding bitmask for the given format.
362 */
363 static GLuint
364 default_bindings(struct st_context *st, enum pipe_format format)
365 {
366 struct pipe_screen *screen = st->pipe->screen;
367 const unsigned target = PIPE_TEXTURE_2D;
368 unsigned bindings;
369
370 if (util_format_is_depth_or_stencil(format))
371 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
372 else
373 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
374
375 if (screen->is_format_supported(screen, format, target, 0, bindings))
376 return bindings;
377 else {
378 /* Try non-sRGB. */
379 format = util_format_linear(format);
380
381 if (screen->is_format_supported(screen, format, target, 0, bindings))
382 return bindings;
383 else
384 return PIPE_BIND_SAMPLER_VIEW;
385 }
386 }
387
388
389 /**
390 * Given the size of a mipmap image, try to compute the size of the level=0
391 * mipmap image.
392 *
393 * Note that this isn't always accurate for odd-sized, non-POW textures.
394 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
395 *
396 * \return GL_TRUE for success, GL_FALSE for failure
397 */
398 static GLboolean
399 guess_base_level_size(GLenum target,
400 GLuint width, GLuint height, GLuint depth, GLuint level,
401 GLuint *width0, GLuint *height0, GLuint *depth0)
402 {
403 assert(width >= 1);
404 assert(height >= 1);
405 assert(depth >= 1);
406
407 if (level > 0) {
408 /* Guess the size of the base level.
409 * Depending on the image's size, we can't always make a guess here.
410 */
411 switch (target) {
412 case GL_TEXTURE_1D:
413 case GL_TEXTURE_1D_ARRAY:
414 width <<= level;
415 break;
416
417 case GL_TEXTURE_2D:
418 case GL_TEXTURE_2D_ARRAY:
419 /* We can't make a good guess here, because the base level dimensions
420 * can be non-square.
421 */
422 if (width == 1 || height == 1) {
423 return GL_FALSE;
424 }
425 width <<= level;
426 height <<= level;
427 break;
428
429 case GL_TEXTURE_CUBE_MAP:
430 case GL_TEXTURE_CUBE_MAP_ARRAY:
431 width <<= level;
432 height <<= level;
433 break;
434
435 case GL_TEXTURE_3D:
436 /* We can't make a good guess here, because the base level dimensions
437 * can be non-cube.
438 */
439 if (width == 1 || height == 1 || depth == 1) {
440 return GL_FALSE;
441 }
442 width <<= level;
443 height <<= level;
444 depth <<= level;
445 break;
446
447 case GL_TEXTURE_RECTANGLE:
448 break;
449
450 default:
451 assert(0);
452 }
453 }
454
455 *width0 = width;
456 *height0 = height;
457 *depth0 = depth;
458
459 return GL_TRUE;
460 }
461
462
463 /**
464 * Try to determine whether we should allocate memory for a full texture
465 * mipmap. The problem is when we get a glTexImage(level=0) call, we
466 * can't immediately know if other mipmap levels are coming next. Here
467 * we try to guess whether to allocate memory for a mipmap or just the
468 * 0th level.
469 *
470 * If we guess incorrectly here we'll later reallocate the right amount of
471 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
472 *
473 * \param stObj the texture object we're going to allocate memory for.
474 * \param stImage describes the incoming image which we need to store.
475 */
476 static boolean
477 allocate_full_mipmap(const struct st_texture_object *stObj,
478 const struct st_texture_image *stImage)
479 {
480 switch (stObj->base.Target) {
481 case GL_TEXTURE_RECTANGLE_NV:
482 case GL_TEXTURE_BUFFER:
483 case GL_TEXTURE_EXTERNAL_OES:
484 case GL_TEXTURE_2D_MULTISAMPLE:
485 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
486 /* these texture types cannot be mipmapped */
487 return FALSE;
488 }
489
490 if (stImage->base.Level > 0 || stObj->base.GenerateMipmap)
491 return TRUE;
492
493 if (stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
494 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT)
495 /* depth/stencil textures are seldom mipmapped */
496 return FALSE;
497
498 if (stObj->base.BaseLevel == 0 && stObj->base.MaxLevel == 0)
499 return FALSE;
500
501 if (stObj->base.Sampler.MinFilter == GL_NEAREST ||
502 stObj->base.Sampler.MinFilter == GL_LINEAR)
503 /* not a mipmap minification filter */
504 return FALSE;
505
506 if (stObj->base.Target == GL_TEXTURE_3D)
507 /* 3D textures are seldom mipmapped */
508 return FALSE;
509
510 return TRUE;
511 }
512
513
514 /**
515 * Try to allocate a pipe_resource object for the given st_texture_object.
516 *
517 * We use the given st_texture_image as a clue to determine the size of the
518 * mipmap image at level=0.
519 *
520 * \return GL_TRUE for success, GL_FALSE if out of memory.
521 */
522 static GLboolean
523 guess_and_alloc_texture(struct st_context *st,
524 struct st_texture_object *stObj,
525 const struct st_texture_image *stImage)
526 {
527 const struct gl_texture_image *firstImage;
528 GLuint lastLevel, width, height, depth;
529 GLuint bindings;
530 unsigned ptWidth;
531 uint16_t ptHeight, ptDepth, ptLayers;
532 enum pipe_format fmt;
533 bool guessed_box = false;
534
535 DBG("%s\n", __func__);
536
537 assert(!stObj->pt);
538
539 /* If a base level image with compatible size exists, use that as our guess.
540 */
541 firstImage = _mesa_base_tex_image(&stObj->base);
542 if (firstImage &&
543 firstImage->Width2 > 0 &&
544 firstImage->Height2 > 0 &&
545 firstImage->Depth2 > 0 &&
546 guess_base_level_size(stObj->base.Target,
547 firstImage->Width2,
548 firstImage->Height2,
549 firstImage->Depth2,
550 firstImage->Level,
551 &width, &height, &depth)) {
552 if (stImage->base.Width2 == u_minify(width, stImage->base.Level) &&
553 stImage->base.Height2 == u_minify(height, stImage->base.Level) &&
554 stImage->base.Depth2 == u_minify(depth, stImage->base.Level))
555 guessed_box = true;
556 }
557
558 if (!guessed_box)
559 guessed_box = guess_base_level_size(stObj->base.Target,
560 stImage->base.Width2,
561 stImage->base.Height2,
562 stImage->base.Depth2,
563 stImage->base.Level,
564 &width, &height, &depth);
565
566 if (!guessed_box) {
567 /* we can't determine the image size at level=0 */
568 /* this is not an out of memory error */
569 return GL_TRUE;
570 }
571
572 /* At this point, (width x height x depth) is the expected size of
573 * the level=0 mipmap image.
574 */
575
576 /* Guess a reasonable value for lastLevel. With OpenGL we have no
577 * idea how many mipmap levels will be in a texture until we start
578 * to render with it. Make an educated guess here but be prepared
579 * to re-allocating a texture buffer with space for more (or fewer)
580 * mipmap levels later.
581 */
582 if (allocate_full_mipmap(stObj, stImage)) {
583 /* alloc space for a full mipmap */
584 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
585 width, height, depth) - 1;
586 }
587 else {
588 /* only alloc space for a single mipmap level */
589 lastLevel = 0;
590 }
591
592 fmt = st_mesa_format_to_pipe_format(st, stImage->base.TexFormat);
593
594 bindings = default_bindings(st, fmt);
595
596 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
597 width, height, depth,
598 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
599
600 stObj->pt = st_texture_create(st,
601 gl_target_to_pipe(stObj->base.Target),
602 fmt,
603 lastLevel,
604 ptWidth,
605 ptHeight,
606 ptDepth,
607 ptLayers, 0,
608 bindings);
609
610 stObj->lastLevel = lastLevel;
611
612 DBG("%s returning %d\n", __func__, (stObj->pt != NULL));
613
614 return stObj->pt != NULL;
615 }
616
617
618 /**
619 * Called via ctx->Driver.AllocTextureImageBuffer().
620 * If the texture object/buffer already has space for the indicated image,
621 * we're done. Otherwise, allocate memory for the new texture image.
622 */
623 static GLboolean
624 st_AllocTextureImageBuffer(struct gl_context *ctx,
625 struct gl_texture_image *texImage)
626 {
627 struct st_context *st = st_context(ctx);
628 struct st_texture_image *stImage = st_texture_image(texImage);
629 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
630 const GLuint level = texImage->Level;
631 GLuint width = texImage->Width;
632 GLuint height = texImage->Height;
633 GLuint depth = texImage->Depth;
634
635 DBG("%s\n", __func__);
636
637 assert(!stImage->pt); /* xxx this might be wrong */
638
639 stObj->needs_validation = true;
640
641 compressed_tex_fallback_allocate(st, stImage);
642
643 /* Look if the parent texture object has space for this image */
644 if (stObj->pt &&
645 level <= stObj->pt->last_level &&
646 st_texture_match_image(st, stObj->pt, texImage)) {
647 /* this image will fit in the existing texture object's memory */
648 pipe_resource_reference(&stImage->pt, stObj->pt);
649 return GL_TRUE;
650 }
651
652 /* The parent texture object does not have space for this image */
653
654 pipe_resource_reference(&stObj->pt, NULL);
655 st_texture_release_all_sampler_views(st, stObj);
656
657 if (!guess_and_alloc_texture(st, stObj, stImage)) {
658 /* Probably out of memory.
659 * Try flushing any pending rendering, then retry.
660 */
661 st_finish(st);
662 if (!guess_and_alloc_texture(st, stObj, stImage)) {
663 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
664 return GL_FALSE;
665 }
666 }
667
668 if (stObj->pt &&
669 st_texture_match_image(st, stObj->pt, texImage)) {
670 /* The image will live in the object's mipmap memory */
671 pipe_resource_reference(&stImage->pt, stObj->pt);
672 assert(stImage->pt);
673 return GL_TRUE;
674 }
675 else {
676 /* Create a new, temporary texture/resource/buffer to hold this
677 * one texture image. Note that when we later access this image
678 * (either for mapping or copying) we'll want to always specify
679 * mipmap level=0, even if the image represents some other mipmap
680 * level.
681 */
682 enum pipe_format format =
683 st_mesa_format_to_pipe_format(st, texImage->TexFormat);
684 GLuint bindings = default_bindings(st, format);
685 unsigned ptWidth;
686 uint16_t ptHeight, ptDepth, ptLayers;
687
688 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
689 width, height, depth,
690 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
691
692 stImage->pt = st_texture_create(st,
693 gl_target_to_pipe(stObj->base.Target),
694 format,
695 0, /* lastLevel */
696 ptWidth,
697 ptHeight,
698 ptDepth,
699 ptLayers, 0,
700 bindings);
701 return stImage->pt != NULL;
702 }
703 }
704
705
706 /**
707 * Preparation prior to glTexImage. Basically check the 'surface_based'
708 * field and switch to a "normal" tex image if necessary.
709 */
710 static void
711 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
712 GLenum format, GLenum type)
713 {
714 struct gl_texture_object *texObj = texImage->TexObject;
715 struct st_texture_object *stObj = st_texture_object(texObj);
716
717 /* switch to "normal" */
718 if (stObj->surface_based) {
719 const GLenum target = texObj->Target;
720 const GLuint level = texImage->Level;
721 mesa_format texFormat;
722
723 assert(!st_texture_image(texImage)->pt);
724 _mesa_clear_texture_object(ctx, texObj, texImage);
725 stObj->layer_override = 0;
726 stObj->level_override = 0;
727 pipe_resource_reference(&stObj->pt, NULL);
728
729 /* oops, need to init this image again */
730 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
731 texImage->InternalFormat, format,
732 type);
733
734 _mesa_init_teximage_fields(ctx, texImage,
735 texImage->Width, texImage->Height,
736 texImage->Depth, texImage->Border,
737 texImage->InternalFormat, texFormat);
738
739 stObj->surface_based = GL_FALSE;
740 }
741 }
742
743
744 /**
745 * Return a writemask for the gallium blit. The parameters can be base
746 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
747 */
748 unsigned
749 st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
750 {
751 switch (dstFormat) {
752 case GL_DEPTH_STENCIL:
753 switch (srcFormat) {
754 case GL_DEPTH_STENCIL:
755 return PIPE_MASK_ZS;
756 case GL_DEPTH_COMPONENT:
757 return PIPE_MASK_Z;
758 case GL_STENCIL_INDEX:
759 return PIPE_MASK_S;
760 default:
761 assert(0);
762 return 0;
763 }
764
765 case GL_DEPTH_COMPONENT:
766 switch (srcFormat) {
767 case GL_DEPTH_STENCIL:
768 case GL_DEPTH_COMPONENT:
769 return PIPE_MASK_Z;
770 default:
771 assert(0);
772 return 0;
773 }
774
775 case GL_STENCIL_INDEX:
776 switch (srcFormat) {
777 case GL_STENCIL_INDEX:
778 return PIPE_MASK_S;
779 default:
780 assert(0);
781 return 0;
782 }
783
784 default:
785 return PIPE_MASK_RGBA;
786 }
787 }
788
789 /**
790 * Converts format to a format with the same components, types
791 * and sizes, but with the components in RGBA order.
792 */
793 static enum pipe_format
794 unswizzle_format(enum pipe_format format)
795 {
796 switch (format)
797 {
798 case PIPE_FORMAT_B8G8R8A8_UNORM:
799 case PIPE_FORMAT_A8R8G8B8_UNORM:
800 case PIPE_FORMAT_A8B8G8R8_UNORM:
801 return PIPE_FORMAT_R8G8B8A8_UNORM;
802
803 case PIPE_FORMAT_B10G10R10A2_UNORM:
804 return PIPE_FORMAT_R10G10B10A2_UNORM;
805
806 case PIPE_FORMAT_B10G10R10A2_SNORM:
807 return PIPE_FORMAT_R10G10B10A2_SNORM;
808
809 case PIPE_FORMAT_B10G10R10A2_UINT:
810 return PIPE_FORMAT_R10G10B10A2_UINT;
811
812 default:
813 return format;
814 }
815 }
816
817 /**
818 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
819 */
820 static enum pipe_format
821 alpha_to_red(enum pipe_format format)
822 {
823 switch (format)
824 {
825 case PIPE_FORMAT_A8_UNORM:
826 return PIPE_FORMAT_R8_UNORM;
827 case PIPE_FORMAT_A8_SNORM:
828 return PIPE_FORMAT_R8_SNORM;
829 case PIPE_FORMAT_A8_UINT:
830 return PIPE_FORMAT_R8_UINT;
831 case PIPE_FORMAT_A8_SINT:
832 return PIPE_FORMAT_R8_SINT;
833
834 case PIPE_FORMAT_A16_UNORM:
835 return PIPE_FORMAT_R16_UNORM;
836 case PIPE_FORMAT_A16_SNORM:
837 return PIPE_FORMAT_R16_SNORM;
838 case PIPE_FORMAT_A16_UINT:
839 return PIPE_FORMAT_R16_UINT;
840 case PIPE_FORMAT_A16_SINT:
841 return PIPE_FORMAT_R16_SINT;
842 case PIPE_FORMAT_A16_FLOAT:
843 return PIPE_FORMAT_R16_FLOAT;
844
845 case PIPE_FORMAT_A32_UINT:
846 return PIPE_FORMAT_R32_UINT;
847 case PIPE_FORMAT_A32_SINT:
848 return PIPE_FORMAT_R32_SINT;
849 case PIPE_FORMAT_A32_FLOAT:
850 return PIPE_FORMAT_R32_FLOAT;
851
852 default:
853 return format;
854 }
855 }
856
857 /**
858 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
859 */
860 static enum pipe_format
861 red_alpha_to_red_green(enum pipe_format format)
862 {
863 switch (format)
864 {
865 case PIPE_FORMAT_R8A8_UNORM:
866 return PIPE_FORMAT_R8G8_UNORM;
867 case PIPE_FORMAT_R8A8_SNORM:
868 return PIPE_FORMAT_R8G8_SNORM;
869 case PIPE_FORMAT_R8A8_UINT:
870 return PIPE_FORMAT_R8G8_UINT;
871 case PIPE_FORMAT_R8A8_SINT:
872 return PIPE_FORMAT_R8G8_SINT;
873
874 case PIPE_FORMAT_R16A16_UNORM:
875 return PIPE_FORMAT_R16G16_UNORM;
876 case PIPE_FORMAT_R16A16_SNORM:
877 return PIPE_FORMAT_R16G16_SNORM;
878 case PIPE_FORMAT_R16A16_UINT:
879 return PIPE_FORMAT_R16G16_UINT;
880 case PIPE_FORMAT_R16A16_SINT:
881 return PIPE_FORMAT_R16G16_SINT;
882 case PIPE_FORMAT_R16A16_FLOAT:
883 return PIPE_FORMAT_R16G16_FLOAT;
884
885 case PIPE_FORMAT_R32A32_UINT:
886 return PIPE_FORMAT_R32G32_UINT;
887 case PIPE_FORMAT_R32A32_SINT:
888 return PIPE_FORMAT_R32G32_SINT;
889 case PIPE_FORMAT_R32A32_FLOAT:
890 return PIPE_FORMAT_R32G32_FLOAT;
891
892 default:
893 return format;
894 }
895 }
896
897 /**
898 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
899 */
900 static enum pipe_format
901 luminance_alpha_to_red_green(enum pipe_format format)
902 {
903 switch (format)
904 {
905 case PIPE_FORMAT_L8A8_UNORM:
906 return PIPE_FORMAT_R8G8_UNORM;
907 case PIPE_FORMAT_L8A8_SNORM:
908 return PIPE_FORMAT_R8G8_SNORM;
909 case PIPE_FORMAT_L8A8_UINT:
910 return PIPE_FORMAT_R8G8_UINT;
911 case PIPE_FORMAT_L8A8_SINT:
912 return PIPE_FORMAT_R8G8_SINT;
913
914 case PIPE_FORMAT_L16A16_UNORM:
915 return PIPE_FORMAT_R16G16_UNORM;
916 case PIPE_FORMAT_L16A16_SNORM:
917 return PIPE_FORMAT_R16G16_SNORM;
918 case PIPE_FORMAT_L16A16_UINT:
919 return PIPE_FORMAT_R16G16_UINT;
920 case PIPE_FORMAT_L16A16_SINT:
921 return PIPE_FORMAT_R16G16_SINT;
922 case PIPE_FORMAT_L16A16_FLOAT:
923 return PIPE_FORMAT_R16G16_FLOAT;
924
925 case PIPE_FORMAT_L32A32_UINT:
926 return PIPE_FORMAT_R32G32_UINT;
927 case PIPE_FORMAT_L32A32_SINT:
928 return PIPE_FORMAT_R32G32_SINT;
929 case PIPE_FORMAT_L32A32_FLOAT:
930 return PIPE_FORMAT_R32G32_FLOAT;
931
932 default:
933 return format;
934 }
935 }
936
937 /**
938 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
939 */
940 static bool
941 format_is_alpha(enum pipe_format format)
942 {
943 const struct util_format_description *desc = util_format_description(format);
944
945 if (desc->nr_channels == 1 &&
946 desc->swizzle[0] == PIPE_SWIZZLE_0 &&
947 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
948 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
949 desc->swizzle[3] == PIPE_SWIZZLE_X)
950 return true;
951
952 return false;
953 }
954
955 /**
956 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
957 */
958 static bool
959 format_is_red(enum pipe_format format)
960 {
961 const struct util_format_description *desc = util_format_description(format);
962
963 if (desc->nr_channels == 1 &&
964 desc->swizzle[0] == PIPE_SWIZZLE_X &&
965 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
966 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
967 desc->swizzle[3] == PIPE_SWIZZLE_1)
968 return true;
969
970 return false;
971 }
972
973
974 /**
975 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
976 */
977 static bool
978 format_is_luminance(enum pipe_format format)
979 {
980 const struct util_format_description *desc = util_format_description(format);
981
982 if (desc->nr_channels == 1 &&
983 desc->swizzle[0] == PIPE_SWIZZLE_X &&
984 desc->swizzle[1] == PIPE_SWIZZLE_X &&
985 desc->swizzle[2] == PIPE_SWIZZLE_X &&
986 desc->swizzle[3] == PIPE_SWIZZLE_1)
987 return true;
988
989 return false;
990 }
991
992 /**
993 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
994 */
995 static bool
996 format_is_red_alpha(enum pipe_format format)
997 {
998 const struct util_format_description *desc = util_format_description(format);
999
1000 if (desc->nr_channels == 2 &&
1001 desc->swizzle[0] == PIPE_SWIZZLE_X &&
1002 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
1003 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
1004 desc->swizzle[3] == PIPE_SWIZZLE_Y)
1005 return true;
1006
1007 return false;
1008 }
1009
1010 static bool
1011 format_is_swizzled_rgba(enum pipe_format format)
1012 {
1013 const struct util_format_description *desc = util_format_description(format);
1014
1015 if ((desc->swizzle[0] == TGSI_SWIZZLE_X || desc->swizzle[0] == PIPE_SWIZZLE_0) &&
1016 (desc->swizzle[1] == TGSI_SWIZZLE_Y || desc->swizzle[1] == PIPE_SWIZZLE_0) &&
1017 (desc->swizzle[2] == TGSI_SWIZZLE_Z || desc->swizzle[2] == PIPE_SWIZZLE_0) &&
1018 (desc->swizzle[3] == TGSI_SWIZZLE_W || desc->swizzle[3] == PIPE_SWIZZLE_1))
1019 return false;
1020
1021 return true;
1022 }
1023
1024 struct format_table
1025 {
1026 unsigned char swizzle[4];
1027 enum pipe_format format;
1028 };
1029
1030 static const struct format_table table_8888_unorm[] = {
1031 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM },
1032 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM },
1033 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM },
1034 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM }
1035 };
1036
1037 static const struct format_table table_1010102_unorm[] = {
1038 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM },
1039 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM }
1040 };
1041
1042 static const struct format_table table_1010102_snorm[] = {
1043 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM },
1044 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM }
1045 };
1046
1047 static const struct format_table table_1010102_uint[] = {
1048 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT },
1049 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT }
1050 };
1051
1052 static enum pipe_format
1053 swizzle_format(enum pipe_format format, const int * const swizzle)
1054 {
1055 unsigned i;
1056
1057 switch (format) {
1058 case PIPE_FORMAT_R8G8B8A8_UNORM:
1059 case PIPE_FORMAT_B8G8R8A8_UNORM:
1060 case PIPE_FORMAT_A8R8G8B8_UNORM:
1061 case PIPE_FORMAT_A8B8G8R8_UNORM:
1062 for (i = 0; i < ARRAY_SIZE(table_8888_unorm); i++) {
1063 if (swizzle[0] == table_8888_unorm[i].swizzle[0] &&
1064 swizzle[1] == table_8888_unorm[i].swizzle[1] &&
1065 swizzle[2] == table_8888_unorm[i].swizzle[2] &&
1066 swizzle[3] == table_8888_unorm[i].swizzle[3])
1067 return table_8888_unorm[i].format;
1068 }
1069 break;
1070
1071 case PIPE_FORMAT_R10G10B10A2_UNORM:
1072 case PIPE_FORMAT_B10G10R10A2_UNORM:
1073 for (i = 0; i < ARRAY_SIZE(table_1010102_unorm); i++) {
1074 if (swizzle[0] == table_1010102_unorm[i].swizzle[0] &&
1075 swizzle[1] == table_1010102_unorm[i].swizzle[1] &&
1076 swizzle[2] == table_1010102_unorm[i].swizzle[2] &&
1077 swizzle[3] == table_1010102_unorm[i].swizzle[3])
1078 return table_1010102_unorm[i].format;
1079 }
1080 break;
1081
1082 case PIPE_FORMAT_R10G10B10A2_SNORM:
1083 case PIPE_FORMAT_B10G10R10A2_SNORM:
1084 for (i = 0; i < ARRAY_SIZE(table_1010102_snorm); i++) {
1085 if (swizzle[0] == table_1010102_snorm[i].swizzle[0] &&
1086 swizzle[1] == table_1010102_snorm[i].swizzle[1] &&
1087 swizzle[2] == table_1010102_snorm[i].swizzle[2] &&
1088 swizzle[3] == table_1010102_snorm[i].swizzle[3])
1089 return table_1010102_snorm[i].format;
1090 }
1091 break;
1092
1093 case PIPE_FORMAT_R10G10B10A2_UINT:
1094 case PIPE_FORMAT_B10G10R10A2_UINT:
1095 for (i = 0; i < ARRAY_SIZE(table_1010102_uint); i++) {
1096 if (swizzle[0] == table_1010102_uint[i].swizzle[0] &&
1097 swizzle[1] == table_1010102_uint[i].swizzle[1] &&
1098 swizzle[2] == table_1010102_uint[i].swizzle[2] &&
1099 swizzle[3] == table_1010102_uint[i].swizzle[3])
1100 return table_1010102_uint[i].format;
1101 }
1102 break;
1103
1104 default:
1105 break;
1106 }
1107
1108 return PIPE_FORMAT_NONE;
1109 }
1110
1111 static bool
1112 reinterpret_formats(enum pipe_format *src_format, enum pipe_format *dst_format)
1113 {
1114 enum pipe_format src = *src_format;
1115 enum pipe_format dst = *dst_format;
1116
1117 /* Note: dst_format has already been transformed from luminance/intensity
1118 * to red when this function is called. The source format will never
1119 * be an intensity format, because GL_INTENSITY is not a legal value
1120 * for the format parameter in glTex(Sub)Image(). */
1121
1122 if (format_is_alpha(src)) {
1123 if (!format_is_alpha(dst))
1124 return false;
1125
1126 src = alpha_to_red(src);
1127 dst = alpha_to_red(dst);
1128 } else if (format_is_luminance(src)) {
1129 if (!format_is_red(dst) && !format_is_red_alpha(dst))
1130 return false;
1131
1132 src = util_format_luminance_to_red(src);
1133 } else if (util_format_is_luminance_alpha(src)) {
1134 src = luminance_alpha_to_red_green(src);
1135
1136 if (format_is_red_alpha(dst)) {
1137 dst = red_alpha_to_red_green(dst);
1138 } else if (!format_is_red(dst))
1139 return false;
1140 } else if (format_is_swizzled_rgba(src)) {
1141 const struct util_format_description *src_desc = util_format_description(src);
1142 const struct util_format_description *dst_desc = util_format_description(dst);
1143 int swizzle[4];
1144 unsigned i;
1145
1146 /* Make sure the format is an RGBA and not an RGBX format */
1147 if (src_desc->nr_channels != 4 || src_desc->swizzle[3] == PIPE_SWIZZLE_1)
1148 return false;
1149
1150 if (dst_desc->nr_channels != 4 || dst_desc->swizzle[3] == PIPE_SWIZZLE_1)
1151 return false;
1152
1153 for (i = 0; i < 4; i++)
1154 swizzle[i] = dst_desc->swizzle[src_desc->swizzle[i]];
1155
1156 dst = swizzle_format(dst, swizzle);
1157 if (dst == PIPE_FORMAT_NONE)
1158 return false;
1159
1160 src = unswizzle_format(src);
1161 }
1162
1163 *src_format = src;
1164 *dst_format = dst;
1165 return true;
1166 }
1167
1168 static bool
1169 try_pbo_upload_common(struct gl_context *ctx,
1170 struct pipe_surface *surface,
1171 const struct st_pbo_addresses *addr,
1172 enum pipe_format src_format)
1173 {
1174 struct st_context *st = st_context(ctx);
1175 struct cso_context *cso = st->cso_context;
1176 struct pipe_context *pipe = st->pipe;
1177 bool success = false;
1178 void *fs;
1179
1180 fs = st_pbo_get_upload_fs(st, src_format, surface->format);
1181 if (!fs)
1182 return false;
1183
1184 cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
1185 CSO_BIT_FRAGMENT_SAMPLERS |
1186 CSO_BIT_VERTEX_ELEMENTS |
1187 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
1188 CSO_BIT_FRAMEBUFFER |
1189 CSO_BIT_VIEWPORT |
1190 CSO_BIT_BLEND |
1191 CSO_BIT_DEPTH_STENCIL_ALPHA |
1192 CSO_BIT_RASTERIZER |
1193 CSO_BIT_STREAM_OUTPUTS |
1194 CSO_BIT_PAUSE_QUERIES |
1195 CSO_BIT_SAMPLE_MASK |
1196 CSO_BIT_MIN_SAMPLES |
1197 CSO_BIT_RENDER_CONDITION |
1198 CSO_BITS_ALL_SHADERS));
1199 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1200
1201 cso_set_sample_mask(cso, ~0);
1202 cso_set_min_samples(cso, 1);
1203 cso_set_render_condition(cso, NULL, FALSE, 0);
1204
1205 /* Set up the sampler_view */
1206 {
1207 struct pipe_sampler_view templ;
1208 struct pipe_sampler_view *sampler_view;
1209 struct pipe_sampler_state sampler = {0};
1210 const struct pipe_sampler_state *samplers[1] = {&sampler};
1211
1212 memset(&templ, 0, sizeof(templ));
1213 templ.target = PIPE_BUFFER;
1214 templ.format = src_format;
1215 templ.u.buf.offset = addr->first_element * addr->bytes_per_pixel;
1216 templ.u.buf.size = (addr->last_element - addr->first_element + 1) *
1217 addr->bytes_per_pixel;
1218 templ.swizzle_r = PIPE_SWIZZLE_X;
1219 templ.swizzle_g = PIPE_SWIZZLE_Y;
1220 templ.swizzle_b = PIPE_SWIZZLE_Z;
1221 templ.swizzle_a = PIPE_SWIZZLE_W;
1222
1223 sampler_view = pipe->create_sampler_view(pipe, addr->buffer, &templ);
1224 if (sampler_view == NULL)
1225 goto fail;
1226
1227 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
1228
1229 pipe_sampler_view_reference(&sampler_view, NULL);
1230
1231 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
1232 }
1233
1234 /* Framebuffer_state */
1235 {
1236 struct pipe_framebuffer_state fb;
1237 memset(&fb, 0, sizeof(fb));
1238 fb.width = surface->width;
1239 fb.height = surface->height;
1240 fb.nr_cbufs = 1;
1241 pipe_surface_reference(&fb.cbufs[0], surface);
1242
1243 cso_set_framebuffer(cso, &fb);
1244
1245 pipe_surface_reference(&fb.cbufs[0], NULL);
1246 }
1247
1248 cso_set_viewport_dims(cso, surface->width, surface->height, FALSE);
1249
1250 /* Blend state */
1251 cso_set_blend(cso, &st->pbo.upload_blend);
1252
1253 /* Depth/stencil/alpha state */
1254 {
1255 struct pipe_depth_stencil_alpha_state dsa;
1256 memset(&dsa, 0, sizeof(dsa));
1257 cso_set_depth_stencil_alpha(cso, &dsa);
1258 }
1259
1260 /* Set up the fragment shader */
1261 cso_set_fragment_shader_handle(cso, fs);
1262
1263 success = st_pbo_draw(st, addr, surface->width, surface->height);
1264
1265 fail:
1266 cso_restore_state(cso);
1267 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1268
1269 return success;
1270 }
1271
1272 static bool
1273 try_pbo_upload(struct gl_context *ctx, GLuint dims,
1274 struct gl_texture_image *texImage,
1275 GLenum format, GLenum type,
1276 enum pipe_format dst_format,
1277 GLint xoffset, GLint yoffset, GLint zoffset,
1278 GLint width, GLint height, GLint depth,
1279 const void *pixels,
1280 const struct gl_pixelstore_attrib *unpack)
1281 {
1282 struct st_context *st = st_context(ctx);
1283 struct st_texture_image *stImage = st_texture_image(texImage);
1284 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1285 struct pipe_resource *texture = stImage->pt;
1286 struct pipe_context *pipe = st->pipe;
1287 struct pipe_screen *screen = pipe->screen;
1288 struct pipe_surface *surface = NULL;
1289 struct st_pbo_addresses addr;
1290 enum pipe_format src_format;
1291 const struct util_format_description *desc;
1292 GLenum gl_target = texImage->TexObject->Target;
1293 bool success;
1294
1295 if (!st->pbo.upload_enabled)
1296 return false;
1297
1298 /* From now on, we need the gallium representation of dimensions. */
1299 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1300 depth = height;
1301 height = 1;
1302 zoffset = yoffset;
1303 yoffset = 0;
1304 }
1305
1306 if (depth != 1 && !st->pbo.layers)
1307 return false;
1308
1309 /* Choose the source format. Initially, we do so without checking driver
1310 * support at all because of the remapping we later perform and because
1311 * at least the Radeon driver actually supports some formats for texture
1312 * buffers which it doesn't support for regular textures. */
1313 src_format = st_choose_matching_format(st, 0, format, type, unpack->SwapBytes);
1314 if (!src_format) {
1315 return false;
1316 }
1317
1318 src_format = util_format_linear(src_format);
1319 desc = util_format_description(src_format);
1320
1321 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1322 return false;
1323
1324 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
1325 return false;
1326
1327 if (st->pbo.rgba_only) {
1328 enum pipe_format orig_dst_format = dst_format;
1329
1330 if (!reinterpret_formats(&src_format, &dst_format)) {
1331 return false;
1332 }
1333
1334 if (dst_format != orig_dst_format &&
1335 !screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D, 0,
1336 PIPE_BIND_RENDER_TARGET)) {
1337 return false;
1338 }
1339 }
1340
1341 if (!src_format ||
1342 !screen->is_format_supported(screen, src_format, PIPE_BUFFER, 0,
1343 PIPE_BIND_SAMPLER_VIEW)) {
1344 return false;
1345 }
1346
1347 /* Compute buffer addresses */
1348 addr.xoffset = xoffset;
1349 addr.yoffset = yoffset;
1350 addr.width = width;
1351 addr.height = height;
1352 addr.depth = depth;
1353 addr.bytes_per_pixel = desc->block.bits / 8;
1354
1355 if (!st_pbo_addresses_pixelstore(st, gl_target, dims == 3, unpack, pixels,
1356 &addr))
1357 return false;
1358
1359 /* Set up the surface */
1360 {
1361 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1362 unsigned max_layer = util_max_layer(texture, level);
1363
1364 zoffset += texImage->Face + texImage->TexObject->MinLayer;
1365
1366 struct pipe_surface templ;
1367 memset(&templ, 0, sizeof(templ));
1368 templ.format = dst_format;
1369 templ.u.tex.level = level;
1370 templ.u.tex.first_layer = MIN2(zoffset, max_layer);
1371 templ.u.tex.last_layer = MIN2(zoffset + depth - 1, max_layer);
1372
1373 surface = pipe->create_surface(pipe, texture, &templ);
1374 if (!surface)
1375 return false;
1376 }
1377
1378 success = try_pbo_upload_common(ctx, surface, &addr, src_format);
1379
1380 pipe_surface_reference(&surface, NULL);
1381
1382 return success;
1383 }
1384
1385 static void
1386 st_TexSubImage(struct gl_context *ctx, GLuint dims,
1387 struct gl_texture_image *texImage,
1388 GLint xoffset, GLint yoffset, GLint zoffset,
1389 GLint width, GLint height, GLint depth,
1390 GLenum format, GLenum type, const void *pixels,
1391 const struct gl_pixelstore_attrib *unpack)
1392 {
1393 struct st_context *st = st_context(ctx);
1394 struct st_texture_image *stImage = st_texture_image(texImage);
1395 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1396 struct pipe_context *pipe = st->pipe;
1397 struct pipe_screen *screen = pipe->screen;
1398 struct pipe_resource *dst = stImage->pt;
1399 struct pipe_resource *src = NULL;
1400 struct pipe_resource src_templ;
1401 struct pipe_transfer *transfer;
1402 struct pipe_blit_info blit;
1403 enum pipe_format src_format, dst_format;
1404 mesa_format mesa_src_format;
1405 GLenum gl_target = texImage->TexObject->Target;
1406 unsigned bind;
1407 GLubyte *map;
1408 unsigned dstz = texImage->Face + texImage->TexObject->MinLayer;
1409 unsigned dst_level = 0;
1410
1411 st_flush_bitmap_cache(st);
1412 st_invalidate_readpix_cache(st);
1413
1414 if (stObj->pt == stImage->pt)
1415 dst_level = texImage->TexObject->MinLevel + texImage->Level;
1416
1417 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1418 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1419
1420 if (!dst)
1421 goto fallback;
1422
1423 /* Try texture_subdata, which should be the fastest memcpy path. */
1424 if (pixels &&
1425 !_mesa_is_bufferobj(unpack->BufferObj) &&
1426 _mesa_texstore_can_use_memcpy(ctx, texImage->_BaseFormat,
1427 texImage->TexFormat, format, type,
1428 unpack)) {
1429 struct pipe_box box;
1430 unsigned stride, layer_stride;
1431 void *data;
1432
1433 stride = _mesa_image_row_stride(unpack, width, format, type);
1434 layer_stride = _mesa_image_image_stride(unpack, width, height, format,
1435 type);
1436 data = _mesa_image_address(dims, unpack, pixels, width, height, format,
1437 type, 0, 0, 0);
1438
1439 /* Convert to Gallium coordinates. */
1440 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1441 zoffset = yoffset;
1442 yoffset = 0;
1443 depth = height;
1444 height = 1;
1445 layer_stride = stride;
1446 }
1447
1448 u_box_3d(xoffset, yoffset, zoffset + dstz, width, height, depth, &box);
1449 pipe->texture_subdata(pipe, dst, dst_level, 0,
1450 &box, data, stride, layer_stride);
1451 return;
1452 }
1453
1454 if (!st->prefer_blit_based_texture_transfer) {
1455 goto fallback;
1456 }
1457
1458 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1459 * blit implementation in some drivers. */
1460 if (format == GL_DEPTH_STENCIL) {
1461 goto fallback;
1462 }
1463
1464 /* If the base internal format and the texture format don't match,
1465 * we can't use blit-based TexSubImage. */
1466 if (texImage->_BaseFormat !=
1467 _mesa_get_format_base_format(texImage->TexFormat)) {
1468 goto fallback;
1469 }
1470
1471
1472 /* See if the destination format is supported. */
1473 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1474 bind = PIPE_BIND_DEPTH_STENCIL;
1475 else
1476 bind = PIPE_BIND_RENDER_TARGET;
1477
1478 /* For luminance and intensity, only the red channel is stored
1479 * in the destination. */
1480 dst_format = util_format_linear(dst->format);
1481 dst_format = util_format_luminance_to_red(dst_format);
1482 dst_format = util_format_intensity_to_red(dst_format);
1483
1484 if (!dst_format ||
1485 !screen->is_format_supported(screen, dst_format, dst->target,
1486 dst->nr_samples, bind)) {
1487 goto fallback;
1488 }
1489
1490 if (_mesa_is_bufferobj(unpack->BufferObj)) {
1491 if (try_pbo_upload(ctx, dims, texImage, format, type, dst_format,
1492 xoffset, yoffset, zoffset,
1493 width, height, depth, pixels, unpack))
1494 return;
1495 }
1496
1497 /* See if the texture format already matches the format and type,
1498 * in which case the memcpy-based fast path will likely be used and
1499 * we don't have to blit. */
1500 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1501 type, unpack->SwapBytes, NULL)) {
1502 goto fallback;
1503 }
1504
1505 /* Choose the source format. */
1506 src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
1507 format, type, unpack->SwapBytes);
1508 if (!src_format) {
1509 goto fallback;
1510 }
1511
1512 mesa_src_format = st_pipe_format_to_mesa_format(src_format);
1513
1514 /* There is no reason to do this if we cannot use memcpy for the temporary
1515 * source texture at least. This also takes transfer ops into account,
1516 * etc. */
1517 if (!_mesa_texstore_can_use_memcpy(ctx,
1518 _mesa_get_format_base_format(mesa_src_format),
1519 mesa_src_format, format, type, unpack)) {
1520 goto fallback;
1521 }
1522
1523 /* TexSubImage only sets a single cubemap face. */
1524 if (gl_target == GL_TEXTURE_CUBE_MAP) {
1525 gl_target = GL_TEXTURE_2D;
1526 }
1527 /* TexSubImage can specify subsets of cube map array faces
1528 * so we need to upload via 2D array instead */
1529 if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
1530 gl_target = GL_TEXTURE_2D_ARRAY;
1531 }
1532
1533 /* Initialize the source texture description. */
1534 memset(&src_templ, 0, sizeof(src_templ));
1535 src_templ.target = gl_target_to_pipe(gl_target);
1536 src_templ.format = src_format;
1537 src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
1538 src_templ.usage = PIPE_USAGE_STAGING;
1539
1540 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1541 &src_templ.width0, &src_templ.height0,
1542 &src_templ.depth0, &src_templ.array_size);
1543
1544 /* Check for NPOT texture support. */
1545 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
1546 (!util_is_power_of_two_or_zero(src_templ.width0) ||
1547 !util_is_power_of_two_or_zero(src_templ.height0) ||
1548 !util_is_power_of_two_or_zero(src_templ.depth0))) {
1549 goto fallback;
1550 }
1551
1552 /* Create the source texture. */
1553 src = screen->resource_create(screen, &src_templ);
1554 if (!src) {
1555 goto fallback;
1556 }
1557
1558 /* Map source pixels. */
1559 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1560 format, type, pixels, unpack,
1561 "glTexSubImage");
1562 if (!pixels) {
1563 /* This is a GL error. */
1564 pipe_resource_reference(&src, NULL);
1565 return;
1566 }
1567
1568 /* From now on, we need the gallium representation of dimensions. */
1569 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1570 zoffset = yoffset;
1571 yoffset = 0;
1572 depth = height;
1573 height = 1;
1574 }
1575
1576 map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
1577 width, height, depth, &transfer);
1578 if (!map) {
1579 _mesa_unmap_teximage_pbo(ctx, unpack);
1580 pipe_resource_reference(&src, NULL);
1581 goto fallback;
1582 }
1583
1584 /* Upload pixels (just memcpy). */
1585 {
1586 const uint bytesPerRow = width * util_format_get_blocksize(src_format);
1587 GLuint row, slice;
1588
1589 for (slice = 0; slice < (unsigned) depth; slice++) {
1590 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1591 /* 1D array textures.
1592 * We need to convert gallium coords to GL coords.
1593 */
1594 void *src = _mesa_image_address2d(unpack, pixels,
1595 width, depth, format,
1596 type, slice, 0);
1597 memcpy(map, src, bytesPerRow);
1598 }
1599 else {
1600 ubyte *slice_map = map;
1601
1602 for (row = 0; row < (unsigned) height; row++) {
1603 void *src = _mesa_image_address(dims, unpack, pixels,
1604 width, height, format,
1605 type, slice, row, 0);
1606 memcpy(slice_map, src, bytesPerRow);
1607 slice_map += transfer->stride;
1608 }
1609 }
1610 map += transfer->layer_stride;
1611 }
1612 }
1613
1614 pipe_transfer_unmap(pipe, transfer);
1615 _mesa_unmap_teximage_pbo(ctx, unpack);
1616
1617 /* Blit. */
1618 memset(&blit, 0, sizeof(blit));
1619 blit.src.resource = src;
1620 blit.src.level = 0;
1621 blit.src.format = src_format;
1622 blit.dst.resource = dst;
1623 blit.dst.level = dst_level;
1624 blit.dst.format = dst_format;
1625 blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
1626 blit.dst.box.x = xoffset;
1627 blit.dst.box.y = yoffset;
1628 blit.dst.box.z = zoffset + dstz;
1629 blit.src.box.width = blit.dst.box.width = width;
1630 blit.src.box.height = blit.dst.box.height = height;
1631 blit.src.box.depth = blit.dst.box.depth = depth;
1632 blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
1633 blit.filter = PIPE_TEX_FILTER_NEAREST;
1634 blit.scissor_enable = FALSE;
1635
1636 st->pipe->blit(st->pipe, &blit);
1637
1638 pipe_resource_reference(&src, NULL);
1639 return;
1640
1641 fallback:
1642 _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
1643 width, height, depth, format, type, pixels,
1644 unpack);
1645 }
1646
1647 static void
1648 st_TexImage(struct gl_context * ctx, GLuint dims,
1649 struct gl_texture_image *texImage,
1650 GLenum format, GLenum type, const void *pixels,
1651 const struct gl_pixelstore_attrib *unpack)
1652 {
1653 assert(dims == 1 || dims == 2 || dims == 3);
1654
1655 prep_teximage(ctx, texImage, format, type);
1656
1657 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
1658 return;
1659
1660 /* allocate storage for texture data */
1661 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
1662 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1663 return;
1664 }
1665
1666 st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
1667 texImage->Width, texImage->Height, texImage->Depth,
1668 format, type, pixels, unpack);
1669 }
1670
1671
1672 static void
1673 st_CompressedTexSubImage(struct gl_context *ctx, GLuint dims,
1674 struct gl_texture_image *texImage,
1675 GLint x, GLint y, GLint z,
1676 GLsizei w, GLsizei h, GLsizei d,
1677 GLenum format, GLsizei imageSize, const void *data)
1678 {
1679 struct st_context *st = st_context(ctx);
1680 struct st_texture_image *stImage = st_texture_image(texImage);
1681 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1682 struct pipe_resource *texture = stImage->pt;
1683 struct pipe_context *pipe = st->pipe;
1684 struct pipe_screen *screen = pipe->screen;
1685 struct pipe_resource *dst = stImage->pt;
1686 struct pipe_surface *surface = NULL;
1687 struct compressed_pixelstore store;
1688 struct st_pbo_addresses addr;
1689 enum pipe_format copy_format;
1690 unsigned bw, bh;
1691 intptr_t buf_offset;
1692 bool success = false;
1693
1694 /* Check basic pre-conditions for PBO upload */
1695 if (!st->prefer_blit_based_texture_transfer) {
1696 goto fallback;
1697 }
1698
1699 if (!_mesa_is_bufferobj(ctx->Unpack.BufferObj))
1700 goto fallback;
1701
1702 if (st_compressed_format_fallback(st, texImage->TexFormat))
1703 goto fallback;
1704
1705 if (!dst) {
1706 goto fallback;
1707 }
1708
1709 if (!st->pbo.upload_enabled ||
1710 !screen->get_param(screen, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS)) {
1711 goto fallback;
1712 }
1713
1714 /* Choose the pipe format for the upload. */
1715 addr.bytes_per_pixel = util_format_get_blocksize(dst->format);
1716 bw = util_format_get_blockwidth(dst->format);
1717 bh = util_format_get_blockheight(dst->format);
1718
1719 switch (addr.bytes_per_pixel) {
1720 case 8:
1721 copy_format = PIPE_FORMAT_R16G16B16A16_UINT;
1722 break;
1723 case 16:
1724 copy_format = PIPE_FORMAT_R32G32B32A32_UINT;
1725 break;
1726 default:
1727 goto fallback;
1728 }
1729
1730 if (!screen->is_format_supported(screen, copy_format, PIPE_BUFFER, 0,
1731 PIPE_BIND_SAMPLER_VIEW)) {
1732 goto fallback;
1733 }
1734
1735 if (!screen->is_format_supported(screen, copy_format, dst->target,
1736 dst->nr_samples, PIPE_BIND_RENDER_TARGET)) {
1737 goto fallback;
1738 }
1739
1740 /* Interpret the pixelstore settings. */
1741 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat, w, h, d,
1742 &ctx->Unpack, &store);
1743 assert(store.CopyBytesPerRow % addr.bytes_per_pixel == 0);
1744 assert(store.SkipBytes % addr.bytes_per_pixel == 0);
1745
1746 /* Compute the offset into the buffer */
1747 buf_offset = (intptr_t)data + store.SkipBytes;
1748
1749 if (buf_offset % addr.bytes_per_pixel) {
1750 goto fallback;
1751 }
1752
1753 buf_offset = buf_offset / addr.bytes_per_pixel;
1754
1755 addr.xoffset = x / bw;
1756 addr.yoffset = y / bh;
1757 addr.width = store.CopyBytesPerRow / addr.bytes_per_pixel;
1758 addr.height = store.CopyRowsPerSlice;
1759 addr.depth = d;
1760 addr.pixels_per_row = store.TotalBytesPerRow / addr.bytes_per_pixel;
1761 addr.image_height = store.TotalRowsPerSlice;
1762
1763 if (!st_pbo_addresses_setup(st, st_buffer_object(ctx->Unpack.BufferObj)->buffer,
1764 buf_offset, &addr))
1765 goto fallback;
1766
1767 /* Set up the surface. */
1768 {
1769 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1770 unsigned max_layer = util_max_layer(texture, level);
1771
1772 z += texImage->Face + texImage->TexObject->MinLayer;
1773
1774 struct pipe_surface templ;
1775 memset(&templ, 0, sizeof(templ));
1776 templ.format = copy_format;
1777 templ.u.tex.level = level;
1778 templ.u.tex.first_layer = MIN2(z, max_layer);
1779 templ.u.tex.last_layer = MIN2(z + d - 1, max_layer);
1780
1781 surface = pipe->create_surface(pipe, texture, &templ);
1782 if (!surface)
1783 goto fallback;
1784 }
1785
1786 success = try_pbo_upload_common(ctx, surface, &addr, copy_format);
1787
1788 pipe_surface_reference(&surface, NULL);
1789
1790 if (success)
1791 return;
1792
1793 fallback:
1794 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
1795 x, y, z, w, h, d,
1796 format, imageSize, data);
1797 }
1798
1799 static void
1800 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
1801 struct gl_texture_image *texImage,
1802 GLsizei imageSize, const void *data)
1803 {
1804 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
1805
1806 /* only 2D and 3D compressed images are supported at this time */
1807 if (dims == 1) {
1808 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
1809 return;
1810 }
1811
1812 /* This is pretty simple, because unlike the general texstore path we don't
1813 * have to worry about the usual image unpacking or image transfer
1814 * operations.
1815 */
1816 assert(texImage);
1817 assert(texImage->Width > 0);
1818 assert(texImage->Height > 0);
1819 assert(texImage->Depth > 0);
1820
1821 /* allocate storage for texture data */
1822 if (!st_AllocTextureImageBuffer(ctx, texImage)) {
1823 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
1824 return;
1825 }
1826
1827 st_CompressedTexSubImage(ctx, dims, texImage,
1828 0, 0, 0,
1829 texImage->Width, texImage->Height, texImage->Depth,
1830 texImage->TexFormat,
1831 imageSize, data);
1832 }
1833
1834
1835
1836
1837 /**
1838 * Called via ctx->Driver.GetTexSubImage()
1839 *
1840 * This uses a blit to copy the texture to a texture format which matches
1841 * the format and type combo and then a fast read-back is done using memcpy.
1842 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1843 * a format which matches the swizzling.
1844 *
1845 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1846 *
1847 * NOTE: Drivers usually do a blit to convert between tiled and linear
1848 * texture layouts during texture uploads/downloads, so the blit
1849 * we do here should be free in such cases.
1850 */
1851 static void
1852 st_GetTexSubImage(struct gl_context * ctx,
1853 GLint xoffset, GLint yoffset, GLint zoffset,
1854 GLsizei width, GLsizei height, GLint depth,
1855 GLenum format, GLenum type, void * pixels,
1856 struct gl_texture_image *texImage)
1857 {
1858 struct st_context *st = st_context(ctx);
1859 struct pipe_context *pipe = st->pipe;
1860 struct pipe_screen *screen = pipe->screen;
1861 struct st_texture_image *stImage = st_texture_image(texImage);
1862 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1863 struct pipe_resource *src = stObj->pt;
1864 struct pipe_resource *dst = NULL;
1865 struct pipe_resource dst_templ;
1866 enum pipe_format dst_format, src_format;
1867 mesa_format mesa_format;
1868 GLenum gl_target = texImage->TexObject->Target;
1869 enum pipe_texture_target pipe_target;
1870 unsigned dims;
1871 struct pipe_blit_info blit;
1872 unsigned bind;
1873 struct pipe_transfer *tex_xfer;
1874 ubyte *map = NULL;
1875 boolean done = FALSE;
1876
1877 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1878 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1879
1880 st_flush_bitmap_cache(st);
1881
1882 if (!st->prefer_blit_based_texture_transfer &&
1883 !_mesa_is_format_compressed(texImage->TexFormat)) {
1884 /* Try to avoid the fallback if we're doing texture decompression here */
1885 goto fallback;
1886 }
1887
1888 /* Handle non-finalized textures. */
1889 if (!stImage->pt || stImage->pt != stObj->pt || !src) {
1890 goto fallback;
1891 }
1892
1893 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1894 * due to an incomplete stencil blit implementation in some drivers. */
1895 if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
1896 goto fallback;
1897 }
1898
1899 /* If the base internal format and the texture format don't match, we have
1900 * to fall back to _mesa_GetTexImage_sw. */
1901 if (texImage->_BaseFormat !=
1902 _mesa_get_format_base_format(texImage->TexFormat)) {
1903 goto fallback;
1904 }
1905
1906 /* See if the texture format already matches the format and type,
1907 * in which case the memcpy-based fast path will be used. */
1908 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1909 type, ctx->Pack.SwapBytes, NULL)) {
1910 goto fallback;
1911 }
1912
1913 /* Convert the source format to what is expected by GetTexImage
1914 * and see if it's supported.
1915 *
1916 * This only applies to glGetTexImage:
1917 * - Luminance must be returned as (L,0,0,1).
1918 * - Luminance alpha must be returned as (L,0,0,A).
1919 * - Intensity must be returned as (I,0,0,1)
1920 */
1921 if (stObj->surface_based)
1922 src_format = util_format_linear(stObj->surface_format);
1923 else
1924 src_format = util_format_linear(src->format);
1925 src_format = util_format_luminance_to_red(src_format);
1926 src_format = util_format_intensity_to_red(src_format);
1927
1928 if (!src_format ||
1929 !screen->is_format_supported(screen, src_format, src->target,
1930 src->nr_samples,
1931 PIPE_BIND_SAMPLER_VIEW)) {
1932 goto fallback;
1933 }
1934
1935 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1936 bind = PIPE_BIND_DEPTH_STENCIL;
1937 else
1938 bind = PIPE_BIND_RENDER_TARGET;
1939
1940 /* GetTexImage only returns a single face for cubemaps. */
1941 if (gl_target == GL_TEXTURE_CUBE_MAP) {
1942 gl_target = GL_TEXTURE_2D;
1943 }
1944 pipe_target = gl_target_to_pipe(gl_target);
1945
1946 /* Choose the destination format by finding the best match
1947 * for the format+type combo. */
1948 dst_format = st_choose_matching_format(st, bind, format, type,
1949 ctx->Pack.SwapBytes);
1950
1951 if (dst_format == PIPE_FORMAT_NONE) {
1952 GLenum dst_glformat;
1953
1954 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1955 * where decompression with a blit is always preferred. */
1956 if (!util_format_is_compressed(src->format)) {
1957 goto fallback;
1958 }
1959
1960 /* Set the appropriate format for the decompressed texture.
1961 * Luminance and sRGB formats shouldn't appear here.*/
1962 switch (src_format) {
1963 case PIPE_FORMAT_DXT1_RGB:
1964 case PIPE_FORMAT_DXT1_RGBA:
1965 case PIPE_FORMAT_DXT3_RGBA:
1966 case PIPE_FORMAT_DXT5_RGBA:
1967 case PIPE_FORMAT_RGTC1_UNORM:
1968 case PIPE_FORMAT_RGTC2_UNORM:
1969 case PIPE_FORMAT_ETC1_RGB8:
1970 case PIPE_FORMAT_ETC2_RGB8:
1971 case PIPE_FORMAT_ETC2_RGB8A1:
1972 case PIPE_FORMAT_ETC2_RGBA8:
1973 case PIPE_FORMAT_ASTC_4x4:
1974 case PIPE_FORMAT_ASTC_5x4:
1975 case PIPE_FORMAT_ASTC_5x5:
1976 case PIPE_FORMAT_ASTC_6x5:
1977 case PIPE_FORMAT_ASTC_6x6:
1978 case PIPE_FORMAT_ASTC_8x5:
1979 case PIPE_FORMAT_ASTC_8x6:
1980 case PIPE_FORMAT_ASTC_8x8:
1981 case PIPE_FORMAT_ASTC_10x5:
1982 case PIPE_FORMAT_ASTC_10x6:
1983 case PIPE_FORMAT_ASTC_10x8:
1984 case PIPE_FORMAT_ASTC_10x10:
1985 case PIPE_FORMAT_ASTC_12x10:
1986 case PIPE_FORMAT_ASTC_12x12:
1987 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1988 dst_glformat = GL_RGBA8;
1989 break;
1990 case PIPE_FORMAT_RGTC1_SNORM:
1991 case PIPE_FORMAT_RGTC2_SNORM:
1992 if (!ctx->Extensions.EXT_texture_snorm)
1993 goto fallback;
1994 dst_glformat = GL_RGBA8_SNORM;
1995 break;
1996 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1997 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1998 if (!ctx->Extensions.ARB_texture_float)
1999 goto fallback;
2000 dst_glformat = GL_RGBA32F;
2001 break;
2002 case PIPE_FORMAT_ETC2_R11_UNORM:
2003 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16_UNORM,
2004 pipe_target, 0, bind))
2005 goto fallback;
2006 dst_glformat = GL_R16;
2007 break;
2008 case PIPE_FORMAT_ETC2_R11_SNORM:
2009 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16_SNORM,
2010 pipe_target, 0, bind))
2011 goto fallback;
2012 dst_glformat = GL_R16_SNORM;
2013 break;
2014 case PIPE_FORMAT_ETC2_RG11_UNORM:
2015 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16G16_UNORM,
2016 pipe_target, 0, bind))
2017 goto fallback;
2018 dst_glformat = GL_RG16;
2019 break;
2020 case PIPE_FORMAT_ETC2_RG11_SNORM:
2021 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16G16_SNORM,
2022 pipe_target, 0, bind))
2023 goto fallback;
2024 dst_glformat = GL_RG16_SNORM;
2025 break;
2026 default:
2027 assert(0);
2028 goto fallback;
2029 }
2030
2031 dst_format = st_choose_format(st, dst_glformat, format, type,
2032 pipe_target, 0, bind, FALSE);
2033
2034 if (dst_format == PIPE_FORMAT_NONE) {
2035 /* unable to get an rgba format!?! */
2036 goto fallback;
2037 }
2038 }
2039
2040 /* create the destination texture of size (width X height X depth) */
2041 memset(&dst_templ, 0, sizeof(dst_templ));
2042 dst_templ.target = pipe_target;
2043 dst_templ.format = dst_format;
2044 dst_templ.bind = bind;
2045 dst_templ.usage = PIPE_USAGE_STAGING;
2046
2047 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
2048 &dst_templ.width0, &dst_templ.height0,
2049 &dst_templ.depth0, &dst_templ.array_size);
2050
2051 dst = screen->resource_create(screen, &dst_templ);
2052 if (!dst) {
2053 goto fallback;
2054 }
2055
2056 /* From now on, we need the gallium representation of dimensions. */
2057 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2058 zoffset = yoffset;
2059 yoffset = 0;
2060 depth = height;
2061 height = 1;
2062 }
2063
2064 assert(texImage->Face == 0 ||
2065 texImage->TexObject->MinLayer == 0 ||
2066 zoffset == 0);
2067
2068 memset(&blit, 0, sizeof(blit));
2069 blit.src.resource = src;
2070 blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
2071 blit.src.format = src_format;
2072 blit.dst.resource = dst;
2073 blit.dst.level = 0;
2074 blit.dst.format = dst->format;
2075 blit.src.box.x = xoffset;
2076 blit.dst.box.x = 0;
2077 blit.src.box.y = yoffset;
2078 blit.dst.box.y = 0;
2079 blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
2080 blit.dst.box.z = 0;
2081 blit.src.box.width = blit.dst.box.width = width;
2082 blit.src.box.height = blit.dst.box.height = height;
2083 blit.src.box.depth = blit.dst.box.depth = depth;
2084 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
2085 blit.filter = PIPE_TEX_FILTER_NEAREST;
2086 blit.scissor_enable = FALSE;
2087
2088 /* blit/render/decompress */
2089 st->pipe->blit(st->pipe, &blit);
2090
2091 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
2092
2093 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
2094 0, 0, 0, width, height, depth, &tex_xfer);
2095 if (!map) {
2096 goto end;
2097 }
2098
2099 mesa_format = st_pipe_format_to_mesa_format(dst_format);
2100 dims = _mesa_get_texture_dimensions(gl_target);
2101
2102 /* copy/pack data into user buffer */
2103 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2104 ctx->Pack.SwapBytes, NULL)) {
2105 /* memcpy */
2106 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
2107 GLuint row, slice;
2108
2109 for (slice = 0; slice < depth; slice++) {
2110 ubyte *slice_map = map;
2111
2112 for (row = 0; row < height; row++) {
2113 void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
2114 width, height, format, type,
2115 slice, row, 0);
2116
2117 memcpy(dest, slice_map, bytesPerRow);
2118
2119 slice_map += tex_xfer->stride;
2120 }
2121
2122 map += tex_xfer->layer_stride;
2123 }
2124 }
2125 else {
2126 /* format translation via floats */
2127 GLuint slice;
2128 GLfloat *rgba;
2129 uint32_t dstMesaFormat;
2130 int dstStride, srcStride;
2131
2132 assert(util_format_is_compressed(src->format));
2133
2134 rgba = malloc(width * height * 4 * sizeof(GLfloat));
2135 if (!rgba) {
2136 goto end;
2137 }
2138
2139 if (ST_DEBUG & DEBUG_FALLBACK)
2140 debug_printf("%s: fallback format translation\n", __func__);
2141
2142 dstMesaFormat = _mesa_format_from_format_and_type(format, type);
2143 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
2144 srcStride = 4 * width * sizeof(GLfloat);
2145 for (slice = 0; slice < depth; slice++) {
2146 void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
2147 width, height, format, type,
2148 slice, 0, 0);
2149
2150 /* get float[4] rgba row from surface */
2151 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, height,
2152 dst_format, rgba);
2153
2154 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2155 rgba, RGBA32_FLOAT, srcStride,
2156 width, height, NULL);
2157
2158 /* Handle byte swapping if required */
2159 if (ctx->Pack.SwapBytes) {
2160 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
2161 width, height, dest, dest);
2162 }
2163
2164 map += tex_xfer->layer_stride;
2165 }
2166
2167 free(rgba);
2168 }
2169 done = TRUE;
2170
2171 end:
2172 if (map)
2173 pipe_transfer_unmap(pipe, tex_xfer);
2174
2175 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
2176 pipe_resource_reference(&dst, NULL);
2177
2178 fallback:
2179 if (!done) {
2180 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
2181 width, height, depth,
2182 format, type, pixels, texImage);
2183 }
2184 }
2185
2186
2187 /**
2188 * Do a CopyTexSubImage operation using a read transfer from the source,
2189 * a write transfer to the destination and get_tile()/put_tile() to access
2190 * the pixels/texels.
2191 *
2192 * Note: srcY=0=TOP of renderbuffer
2193 */
2194 static void
2195 fallback_copy_texsubimage(struct gl_context *ctx,
2196 struct st_renderbuffer *strb,
2197 struct st_texture_image *stImage,
2198 GLenum baseFormat,
2199 GLint destX, GLint destY, GLint slice,
2200 GLint srcX, GLint srcY,
2201 GLsizei width, GLsizei height)
2202 {
2203 struct st_context *st = st_context(ctx);
2204 struct pipe_context *pipe = st->pipe;
2205 struct pipe_transfer *src_trans;
2206 GLubyte *texDest;
2207 enum pipe_transfer_usage transfer_usage;
2208 void *map;
2209 unsigned dst_width = width;
2210 unsigned dst_height = height;
2211 unsigned dst_depth = 1;
2212 struct pipe_transfer *transfer;
2213
2214 if (ST_DEBUG & DEBUG_FALLBACK)
2215 debug_printf("%s: fallback processing\n", __func__);
2216
2217 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2218 srcY = strb->Base.Height - srcY - height;
2219 }
2220
2221 map = pipe_transfer_map(pipe,
2222 strb->texture,
2223 strb->surface->u.tex.level,
2224 strb->surface->u.tex.first_layer,
2225 PIPE_TRANSFER_READ,
2226 srcX, srcY,
2227 width, height, &src_trans);
2228
2229 if ((baseFormat == GL_DEPTH_COMPONENT ||
2230 baseFormat == GL_DEPTH_STENCIL) &&
2231 util_format_is_depth_and_stencil(stImage->pt->format))
2232 transfer_usage = PIPE_TRANSFER_READ_WRITE;
2233 else
2234 transfer_usage = PIPE_TRANSFER_WRITE;
2235
2236 texDest = st_texture_image_map(st, stImage, transfer_usage,
2237 destX, destY, slice,
2238 dst_width, dst_height, dst_depth,
2239 &transfer);
2240
2241 if (baseFormat == GL_DEPTH_COMPONENT ||
2242 baseFormat == GL_DEPTH_STENCIL) {
2243 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
2244 ctx->Pixel.DepthBias != 0.0F);
2245 GLint row, yStep;
2246 uint *data;
2247
2248 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2249 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2250 srcY = height - 1;
2251 yStep = -1;
2252 }
2253 else {
2254 srcY = 0;
2255 yStep = 1;
2256 }
2257
2258 data = malloc(width * sizeof(uint));
2259
2260 if (data) {
2261 /* To avoid a large temp memory allocation, do copy row by row */
2262 for (row = 0; row < height; row++, srcY += yStep) {
2263 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
2264 if (scaleOrBias) {
2265 _mesa_scale_and_bias_depth_uint(ctx, width, data);
2266 }
2267
2268 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2269 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
2270 0, 0, width, 1, data);
2271 }
2272 else {
2273 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
2274 }
2275 }
2276 }
2277 else {
2278 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
2279 }
2280
2281 free(data);
2282 }
2283 else {
2284 /* RGBA format */
2285 GLfloat *tempSrc =
2286 malloc(width * height * 4 * sizeof(GLfloat));
2287
2288 if (tempSrc && texDest) {
2289 const GLint dims = 2;
2290 GLint dstRowStride;
2291 struct gl_texture_image *texImage = &stImage->base;
2292 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
2293
2294 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2295 unpack.Invert = GL_TRUE;
2296 }
2297
2298 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2299 dstRowStride = transfer->layer_stride;
2300 }
2301 else {
2302 dstRowStride = transfer->stride;
2303 }
2304
2305 /* get float/RGBA image from framebuffer */
2306 /* XXX this usually involves a lot of int/float conversion.
2307 * try to avoid that someday.
2308 */
2309 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
2310 util_format_linear(strb->texture->format),
2311 tempSrc);
2312
2313 /* Store into texture memory.
2314 * Note that this does some special things such as pixel transfer
2315 * ops and format conversion. In particular, if the dest tex format
2316 * is actually RGBA but the user created the texture as GL_RGB we
2317 * need to fill-in/override the alpha channel with 1.0.
2318 */
2319 _mesa_texstore(ctx, dims,
2320 texImage->_BaseFormat,
2321 texImage->TexFormat,
2322 dstRowStride,
2323 &texDest,
2324 width, height, 1,
2325 GL_RGBA, GL_FLOAT, tempSrc, /* src */
2326 &unpack);
2327 }
2328 else {
2329 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
2330 }
2331
2332 free(tempSrc);
2333 }
2334
2335 st_texture_image_unmap(st, stImage, slice);
2336 pipe->transfer_unmap(pipe, src_trans);
2337 }
2338
2339 static bool
2340 st_can_copyteximage_using_blit(const struct gl_texture_image *texImage,
2341 const struct gl_renderbuffer *rb)
2342 {
2343 GLenum tex_baseformat = _mesa_get_format_base_format(texImage->TexFormat);
2344
2345 /* We don't blit to a teximage where the GL base format doesn't match the
2346 * texture's chosen format, except in the case of a GL_RGB texture
2347 * represented with GL_RGBA (where the alpha channel is just being
2348 * dropped).
2349 */
2350 if (texImage->_BaseFormat != tex_baseformat &&
2351 ((texImage->_BaseFormat != GL_RGB || tex_baseformat != GL_RGBA))) {
2352 return false;
2353 }
2354
2355 /* We can't blit from a RB where the GL base format doesn't match the RB's
2356 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2357 * type, because the other channels will be undefined).
2358 */
2359 if (rb->_BaseFormat != _mesa_get_format_base_format(rb->Format))
2360 return false;
2361
2362 return true;
2363 }
2364
2365 /**
2366 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2367 * Note that the region to copy has already been clipped so we know we
2368 * won't read from outside the source renderbuffer's bounds.
2369 *
2370 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2371 */
2372 static void
2373 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2374 struct gl_texture_image *texImage,
2375 GLint destX, GLint destY, GLint slice,
2376 struct gl_renderbuffer *rb,
2377 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
2378 {
2379 struct st_texture_image *stImage = st_texture_image(texImage);
2380 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2381 struct st_renderbuffer *strb = st_renderbuffer(rb);
2382 struct st_context *st = st_context(ctx);
2383 struct pipe_context *pipe = st->pipe;
2384 struct pipe_screen *screen = pipe->screen;
2385 struct pipe_blit_info blit;
2386 enum pipe_format dst_format;
2387 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
2388 unsigned bind;
2389 GLint srcY0, srcY1;
2390
2391 st_flush_bitmap_cache(st);
2392 st_invalidate_readpix_cache(st);
2393
2394 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2395 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2396
2397 if (!strb || !strb->surface || !stImage->pt) {
2398 debug_printf("%s: null strb or stImage\n", __func__);
2399 return;
2400 }
2401
2402 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
2403 texImage->TexFormat)) {
2404 goto fallback;
2405 }
2406
2407 if (!st_can_copyteximage_using_blit(texImage, rb)) {
2408 goto fallback;
2409 }
2410
2411 /* Choose the destination format to match the TexImage behavior. */
2412 dst_format = util_format_linear(stImage->pt->format);
2413 dst_format = util_format_luminance_to_red(dst_format);
2414 dst_format = util_format_intensity_to_red(dst_format);
2415
2416 /* See if the destination format is supported. */
2417 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
2418 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
2419 bind = PIPE_BIND_DEPTH_STENCIL;
2420 }
2421 else {
2422 bind = PIPE_BIND_RENDER_TARGET;
2423 }
2424
2425 if (!dst_format ||
2426 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
2427 stImage->pt->nr_samples, bind)) {
2428 goto fallback;
2429 }
2430
2431 /* Y flipping for the main framebuffer. */
2432 if (do_flip) {
2433 srcY1 = strb->Base.Height - srcY - height;
2434 srcY0 = srcY1 + height;
2435 }
2436 else {
2437 srcY0 = srcY;
2438 srcY1 = srcY0 + height;
2439 }
2440
2441 /* Blit the texture.
2442 * This supports flipping, format conversions, and downsampling.
2443 */
2444 memset(&blit, 0, sizeof(blit));
2445 blit.src.resource = strb->texture;
2446 blit.src.format = util_format_linear(strb->surface->format);
2447 blit.src.level = strb->surface->u.tex.level;
2448 blit.src.box.x = srcX;
2449 blit.src.box.y = srcY0;
2450 blit.src.box.z = strb->surface->u.tex.first_layer;
2451 blit.src.box.width = width;
2452 blit.src.box.height = srcY1 - srcY0;
2453 blit.src.box.depth = 1;
2454 blit.dst.resource = stImage->pt;
2455 blit.dst.format = dst_format;
2456 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
2457 blit.dst.box.x = destX;
2458 blit.dst.box.y = destY;
2459 blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
2460 blit.dst.box.width = width;
2461 blit.dst.box.height = height;
2462 blit.dst.box.depth = 1;
2463 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
2464 blit.filter = PIPE_TEX_FILTER_NEAREST;
2465 pipe->blit(pipe, &blit);
2466 return;
2467
2468 fallback:
2469 /* software fallback */
2470 fallback_copy_texsubimage(ctx,
2471 strb, stImage, texImage->_BaseFormat,
2472 destX, destY, slice,
2473 srcX, srcY, width, height);
2474 }
2475
2476
2477 /**
2478 * Copy image data from stImage into the texture object 'stObj' at level
2479 * 'dstLevel'.
2480 */
2481 static void
2482 copy_image_data_to_texture(struct st_context *st,
2483 struct st_texture_object *stObj,
2484 GLuint dstLevel,
2485 struct st_texture_image *stImage)
2486 {
2487 /* debug checks */
2488 {
2489 const struct gl_texture_image MAYBE_UNUSED *dstImage =
2490 stObj->base.Image[stImage->base.Face][dstLevel];
2491 assert(dstImage);
2492 assert(dstImage->Width == stImage->base.Width);
2493 assert(dstImage->Height == stImage->base.Height);
2494 assert(dstImage->Depth == stImage->base.Depth);
2495 }
2496
2497 if (stImage->pt) {
2498 /* Copy potentially with the blitter:
2499 */
2500 GLuint src_level;
2501 if (stImage->pt->last_level == 0)
2502 src_level = 0;
2503 else
2504 src_level = stImage->base.Level;
2505
2506 assert(src_level <= stImage->pt->last_level);
2507 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
2508 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
2509 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
2510 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
2511 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
2512 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
2513
2514 st_texture_image_copy(st->pipe,
2515 stObj->pt, dstLevel, /* dest texture, level */
2516 stImage->pt, src_level, /* src texture, level */
2517 stImage->base.Face);
2518
2519 pipe_resource_reference(&stImage->pt, NULL);
2520 }
2521 pipe_resource_reference(&stImage->pt, stObj->pt);
2522 }
2523
2524
2525 /**
2526 * Called during state validation. When this function is finished,
2527 * the texture object should be ready for rendering.
2528 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2529 */
2530 GLboolean
2531 st_finalize_texture(struct gl_context *ctx,
2532 struct pipe_context *pipe,
2533 struct gl_texture_object *tObj,
2534 GLuint cubeMapFace)
2535 {
2536 struct st_context *st = st_context(ctx);
2537 struct st_texture_object *stObj = st_texture_object(tObj);
2538 const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
2539 GLuint face;
2540 const struct st_texture_image *firstImage;
2541 enum pipe_format firstImageFormat;
2542 unsigned ptWidth;
2543 uint16_t ptHeight, ptDepth, ptLayers, ptNumSamples;
2544
2545 if (tObj->Immutable)
2546 return GL_TRUE;
2547
2548 if (tObj->_MipmapComplete)
2549 stObj->lastLevel = stObj->base._MaxLevel;
2550 else if (tObj->_BaseComplete)
2551 stObj->lastLevel = stObj->base.BaseLevel;
2552
2553 /* Skip the loop over images in the common case of no images having
2554 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2555 * haven't looked at, then we do need to look at those new images.
2556 */
2557 if (!stObj->needs_validation &&
2558 stObj->base.BaseLevel >= stObj->validated_first_level &&
2559 stObj->lastLevel <= stObj->validated_last_level) {
2560 return GL_TRUE;
2561 }
2562
2563 /* If this texture comes from a window system, there is nothing else to do. */
2564 if (stObj->surface_based) {
2565 return GL_TRUE;
2566 }
2567
2568 firstImage = st_texture_image_const(stObj->base.Image[cubeMapFace][stObj->base.BaseLevel]);
2569 assert(firstImage);
2570
2571 /* If both firstImage and stObj point to a texture which can contain
2572 * all active images, favour firstImage. Note that because of the
2573 * completeness requirement, we know that the image dimensions
2574 * will match.
2575 */
2576 if (firstImage->pt &&
2577 firstImage->pt != stObj->pt &&
2578 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
2579 pipe_resource_reference(&stObj->pt, firstImage->pt);
2580 st_texture_release_all_sampler_views(st, stObj);
2581 }
2582
2583 /* Find gallium format for the Mesa texture */
2584 firstImageFormat =
2585 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
2586
2587 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2588 {
2589 unsigned width;
2590 uint16_t height, depth;
2591
2592 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
2593 firstImage->base.Width2,
2594 firstImage->base.Height2,
2595 firstImage->base.Depth2,
2596 &width, &height, &depth, &ptLayers);
2597
2598 /* If we previously allocated a pipe texture and its sizes are
2599 * compatible, use them.
2600 */
2601 if (stObj->pt &&
2602 u_minify(stObj->pt->width0, firstImage->base.Level) == width &&
2603 u_minify(stObj->pt->height0, firstImage->base.Level) == height &&
2604 u_minify(stObj->pt->depth0, firstImage->base.Level) == depth) {
2605 ptWidth = stObj->pt->width0;
2606 ptHeight = stObj->pt->height0;
2607 ptDepth = stObj->pt->depth0;
2608 } else {
2609 /* Otherwise, compute a new level=0 size that is compatible with the
2610 * base level image.
2611 */
2612 ptWidth = width > 1 ? width << firstImage->base.Level : 1;
2613 ptHeight = height > 1 ? height << firstImage->base.Level : 1;
2614 ptDepth = depth > 1 ? depth << firstImage->base.Level : 1;
2615
2616 /* If the base level image is 1x1x1, we still need to ensure that the
2617 * resulting pipe texture ends up with the required number of levels
2618 * in total.
2619 */
2620 if (ptWidth == 1 && ptHeight == 1 && ptDepth == 1) {
2621 ptWidth <<= firstImage->base.Level;
2622
2623 if (stObj->base.Target == GL_TEXTURE_CUBE_MAP ||
2624 stObj->base.Target == GL_TEXTURE_CUBE_MAP_ARRAY)
2625 ptHeight = ptWidth;
2626 }
2627
2628 /* At this point, the texture may be incomplete (mismatched cube
2629 * face sizes, for example). If that's the case, give up, but
2630 * don't return GL_FALSE as that would raise an incorrect
2631 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2632 */
2633 if (!stObj->base._BaseComplete) {
2634 _mesa_test_texobj_completeness(ctx, &stObj->base);
2635 if (!stObj->base._BaseComplete) {
2636 return TRUE;
2637 }
2638 }
2639 }
2640
2641 ptNumSamples = firstImage->base.NumSamples;
2642 }
2643
2644 /* If we already have a gallium texture, check that it matches the texture
2645 * object's format, target, size, num_levels, etc.
2646 */
2647 if (stObj->pt) {
2648 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
2649 stObj->pt->format != firstImageFormat ||
2650 stObj->pt->last_level < stObj->lastLevel ||
2651 stObj->pt->width0 != ptWidth ||
2652 stObj->pt->height0 != ptHeight ||
2653 stObj->pt->depth0 != ptDepth ||
2654 stObj->pt->nr_samples != ptNumSamples ||
2655 stObj->pt->array_size != ptLayers)
2656 {
2657 /* The gallium texture does not match the Mesa texture so delete the
2658 * gallium texture now. We'll make a new one below.
2659 */
2660 pipe_resource_reference(&stObj->pt, NULL);
2661 st_texture_release_all_sampler_views(st, stObj);
2662 st->dirty |= ST_NEW_FRAMEBUFFER;
2663 }
2664 }
2665
2666 /* May need to create a new gallium texture:
2667 */
2668 if (!stObj->pt) {
2669 GLuint bindings = default_bindings(st, firstImageFormat);
2670
2671 stObj->pt = st_texture_create(st,
2672 gl_target_to_pipe(stObj->base.Target),
2673 firstImageFormat,
2674 stObj->lastLevel,
2675 ptWidth,
2676 ptHeight,
2677 ptDepth,
2678 ptLayers, ptNumSamples,
2679 bindings);
2680
2681 if (!stObj->pt) {
2682 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
2683 return GL_FALSE;
2684 }
2685 }
2686
2687 /* Pull in any images not in the object's texture:
2688 */
2689 for (face = 0; face < nr_faces; face++) {
2690 GLuint level;
2691 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
2692 struct st_texture_image *stImage =
2693 st_texture_image(stObj->base.Image[face][level]);
2694
2695 /* Need to import images in main memory or held in other textures.
2696 */
2697 if (stImage && stObj->pt != stImage->pt) {
2698 GLuint height;
2699 GLuint depth;
2700
2701 if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
2702 height = u_minify(ptHeight, level);
2703 else
2704 height = ptLayers;
2705
2706 if (stObj->base.Target == GL_TEXTURE_3D)
2707 depth = u_minify(ptDepth, level);
2708 else if (stObj->base.Target == GL_TEXTURE_CUBE_MAP)
2709 depth = 1;
2710 else
2711 depth = ptLayers;
2712
2713 if (level == 0 ||
2714 (stImage->base.Width == u_minify(ptWidth, level) &&
2715 stImage->base.Height == height &&
2716 stImage->base.Depth == depth)) {
2717 /* src image fits expected dest mipmap level size */
2718 copy_image_data_to_texture(st, stObj, level, stImage);
2719 }
2720 }
2721 }
2722 }
2723
2724 stObj->validated_first_level = stObj->base.BaseLevel;
2725 stObj->validated_last_level = stObj->lastLevel;
2726 stObj->needs_validation = false;
2727
2728 return GL_TRUE;
2729 }
2730
2731 /**
2732 * Allocate a new pipe_resource object
2733 * width0, height0, depth0 are the dimensions of the level 0 image
2734 * (the highest resolution). last_level indicates how many mipmap levels
2735 * to allocate storage for. For non-mipmapped textures, this will be zero.
2736 */
2737 static struct pipe_resource *
2738 st_texture_create_from_memory(struct st_context *st,
2739 struct st_memory_object *memObj,
2740 GLuint64 offset,
2741 enum pipe_texture_target target,
2742 enum pipe_format format,
2743 GLuint last_level,
2744 GLuint width0,
2745 GLuint height0,
2746 GLuint depth0,
2747 GLuint layers,
2748 GLuint nr_samples,
2749 GLuint bind )
2750 {
2751 struct pipe_resource pt, *newtex;
2752 struct pipe_screen *screen = st->pipe->screen;
2753
2754 assert(target < PIPE_MAX_TEXTURE_TYPES);
2755 assert(width0 > 0);
2756 assert(height0 > 0);
2757 assert(depth0 > 0);
2758 if (target == PIPE_TEXTURE_CUBE)
2759 assert(layers == 6);
2760
2761 DBG("%s target %d format %s last_level %d\n", __func__,
2762 (int) target, util_format_name(format), last_level);
2763
2764 assert(format);
2765 assert(screen->is_format_supported(screen, format, target, 0,
2766 PIPE_BIND_SAMPLER_VIEW));
2767
2768 memset(&pt, 0, sizeof(pt));
2769 pt.target = target;
2770 pt.format = format;
2771 pt.last_level = last_level;
2772 pt.width0 = width0;
2773 pt.height0 = height0;
2774 pt.depth0 = depth0;
2775 pt.array_size = layers;
2776 pt.usage = PIPE_USAGE_DEFAULT;
2777 pt.bind = bind;
2778 /* only set this for OpenGL textures, not renderbuffers */
2779 pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
2780 pt.nr_samples = nr_samples;
2781
2782 newtex = screen->resource_from_memobj(screen, &pt, memObj->memory, offset);
2783
2784 assert(!newtex || pipe_is_referenced(&newtex->reference));
2785
2786 return newtex;
2787 }
2788
2789 /**
2790 * Allocate texture memory for a whole mipmap stack.
2791 * Note: for multisample textures if the requested sample count is not
2792 * supported, we search for the next higher supported sample count.
2793 */
2794 static GLboolean
2795 st_texture_storage(struct gl_context *ctx,
2796 struct gl_texture_object *texObj,
2797 GLsizei levels, GLsizei width,
2798 GLsizei height, GLsizei depth,
2799 struct gl_memory_object *memObj,
2800 GLuint64 offset)
2801 {
2802 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
2803 struct gl_texture_image *texImage = texObj->Image[0][0];
2804 struct st_context *st = st_context(ctx);
2805 struct st_texture_object *stObj = st_texture_object(texObj);
2806 struct st_memory_object *smObj = st_memory_object(memObj);
2807 struct pipe_screen *screen = st->pipe->screen;
2808 unsigned ptWidth, bindings;
2809 uint16_t ptHeight, ptDepth, ptLayers;
2810 enum pipe_format fmt;
2811 GLint level;
2812 GLuint num_samples = texImage->NumSamples;
2813
2814 assert(levels > 0);
2815
2816 stObj->lastLevel = levels - 1;
2817
2818 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
2819
2820 bindings = default_bindings(st, fmt);
2821
2822 if (num_samples > 0) {
2823 /* Find msaa sample count which is actually supported. For example,
2824 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2825 * choose 4x here.
2826 */
2827 enum pipe_texture_target ptarget = gl_target_to_pipe(texObj->Target);
2828 boolean found = FALSE;
2829
2830 if (ctx->Const.MaxSamples > 1 && num_samples == 1) {
2831 /* don't try num_samples = 1 with drivers that support real msaa */
2832 num_samples = 2;
2833 }
2834
2835 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
2836 if (screen->is_format_supported(screen, fmt, ptarget,
2837 num_samples,
2838 PIPE_BIND_SAMPLER_VIEW)) {
2839 /* Update the sample count in gl_texture_image as well. */
2840 texImage->NumSamples = num_samples;
2841 found = TRUE;
2842 break;
2843 }
2844 }
2845
2846 if (!found) {
2847 return GL_FALSE;
2848 }
2849 }
2850
2851 st_gl_texture_dims_to_pipe_dims(texObj->Target,
2852 width, height, depth,
2853 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2854
2855 if (smObj) {
2856 stObj->pt = st_texture_create_from_memory(st,
2857 smObj,
2858 offset,
2859 gl_target_to_pipe(texObj->Target),
2860 fmt,
2861 levels - 1,
2862 ptWidth,
2863 ptHeight,
2864 ptDepth,
2865 ptLayers, num_samples,
2866 bindings);
2867 }
2868 else {
2869 stObj->pt = st_texture_create(st,
2870 gl_target_to_pipe(texObj->Target),
2871 fmt,
2872 levels - 1,
2873 ptWidth,
2874 ptHeight,
2875 ptDepth,
2876 ptLayers, num_samples,
2877 bindings);
2878 }
2879
2880 if (!stObj->pt)
2881 return GL_FALSE;
2882
2883 /* Set image resource pointers */
2884 for (level = 0; level < levels; level++) {
2885 GLuint face;
2886 for (face = 0; face < numFaces; face++) {
2887 struct st_texture_image *stImage =
2888 st_texture_image(texObj->Image[face][level]);
2889 pipe_resource_reference(&stImage->pt, stObj->pt);
2890
2891 compressed_tex_fallback_allocate(st, stImage);
2892 }
2893 }
2894
2895 /* The texture is in a validated state, so no need to check later. */
2896 stObj->needs_validation = false;
2897 stObj->validated_first_level = 0;
2898 stObj->validated_last_level = levels - 1;
2899
2900 return GL_TRUE;
2901 }
2902
2903 /**
2904 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2905 * for a whole mipmap stack.
2906 */
2907 static GLboolean
2908 st_AllocTextureStorage(struct gl_context *ctx,
2909 struct gl_texture_object *texObj,
2910 GLsizei levels, GLsizei width,
2911 GLsizei height, GLsizei depth)
2912 {
2913 return st_texture_storage(ctx, texObj, levels,
2914 width, height, depth,
2915 NULL, 0);
2916 }
2917
2918
2919 static GLboolean
2920 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
2921 GLuint numLevels, GLint level,
2922 mesa_format format, GLuint numSamples,
2923 GLint width, GLint height, GLint depth)
2924 {
2925 struct st_context *st = st_context(ctx);
2926 struct pipe_context *pipe = st->pipe;
2927
2928 if (width == 0 || height == 0 || depth == 0) {
2929 /* zero-sized images are legal, and always fit! */
2930 return GL_TRUE;
2931 }
2932
2933 if (pipe->screen->can_create_resource) {
2934 /* Ask the gallium driver if the texture is too large */
2935 struct gl_texture_object *texObj =
2936 _mesa_get_current_tex_object(ctx, target);
2937 struct pipe_resource pt;
2938
2939 /* Setup the pipe_resource object
2940 */
2941 memset(&pt, 0, sizeof(pt));
2942
2943 pt.target = gl_target_to_pipe(target);
2944 pt.format = st_mesa_format_to_pipe_format(st, format);
2945 pt.nr_samples = numSamples;
2946
2947 st_gl_texture_dims_to_pipe_dims(target,
2948 width, height, depth,
2949 &pt.width0, &pt.height0,
2950 &pt.depth0, &pt.array_size);
2951
2952 if (numLevels > 0) {
2953 /* For immutable textures we know the final number of mip levels */
2954 pt.last_level = numLevels - 1;
2955 }
2956 else if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
2957 texObj->Sampler.MinFilter == GL_NEAREST)) {
2958 /* assume just one mipmap level */
2959 pt.last_level = 0;
2960 }
2961 else {
2962 /* assume a full set of mipmaps */
2963 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
2964 }
2965
2966 return pipe->screen->can_create_resource(pipe->screen, &pt);
2967 }
2968 else {
2969 /* Use core Mesa fallback */
2970 return _mesa_test_proxy_teximage(ctx, target, numLevels, level, format,
2971 numSamples, width, height, depth);
2972 }
2973 }
2974
2975 static GLboolean
2976 st_TextureView(struct gl_context *ctx,
2977 struct gl_texture_object *texObj,
2978 struct gl_texture_object *origTexObj)
2979 {
2980 struct st_context *st = st_context(ctx);
2981 struct st_texture_object *orig = st_texture_object(origTexObj);
2982 struct st_texture_object *tex = st_texture_object(texObj);
2983 struct gl_texture_image *image = texObj->Image[0][0];
2984
2985 const int numFaces = _mesa_num_tex_faces(texObj->Target);
2986 const int numLevels = texObj->NumLevels;
2987
2988 int face;
2989 int level;
2990
2991 pipe_resource_reference(&tex->pt, orig->pt);
2992
2993 /* Set image resource pointers */
2994 for (level = 0; level < numLevels; level++) {
2995 for (face = 0; face < numFaces; face++) {
2996 struct st_texture_image *stImage =
2997 st_texture_image(texObj->Image[face][level]);
2998 pipe_resource_reference(&stImage->pt, tex->pt);
2999 }
3000 }
3001
3002 tex->surface_based = GL_TRUE;
3003 tex->surface_format =
3004 st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
3005
3006 tex->lastLevel = numLevels - 1;
3007
3008 /* free texture sampler views. They need to be recreated when we
3009 * change the texture view parameters.
3010 */
3011 st_texture_release_all_sampler_views(st, tex);
3012
3013 /* The texture is in a validated state, so no need to check later. */
3014 tex->needs_validation = false;
3015 tex->validated_first_level = 0;
3016 tex->validated_last_level = numLevels - 1;
3017
3018 return GL_TRUE;
3019 }
3020
3021
3022 /**
3023 * Find the mipmap level in 'pt' which matches the level described by
3024 * 'texImage'.
3025 */
3026 static unsigned
3027 find_mipmap_level(const struct gl_texture_image *texImage,
3028 const struct pipe_resource *pt)
3029 {
3030 const GLenum target = texImage->TexObject->Target;
3031 GLint texWidth = texImage->Width;
3032 GLint texHeight = texImage->Height;
3033 GLint texDepth = texImage->Depth;
3034 unsigned level, w;
3035 uint16_t h, d, layers;
3036
3037 st_gl_texture_dims_to_pipe_dims(target, texWidth, texHeight, texDepth,
3038 &w, &h, &d, &layers);
3039
3040 for (level = 0; level <= pt->last_level; level++) {
3041 if (u_minify(pt->width0, level) == w &&
3042 u_minify(pt->height0, level) == h &&
3043 u_minify(pt->depth0, level) == d) {
3044 return level;
3045 }
3046 }
3047
3048 /* If we get here, there must be some sort of inconsistency between
3049 * the Mesa texture object/images and the gallium resource.
3050 */
3051 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3052
3053 return texImage->Level;
3054 }
3055
3056
3057 static void
3058 st_ClearTexSubImage(struct gl_context *ctx,
3059 struct gl_texture_image *texImage,
3060 GLint xoffset, GLint yoffset, GLint zoffset,
3061 GLsizei width, GLsizei height, GLsizei depth,
3062 const void *clearValue)
3063 {
3064 static const char zeros[16] = {0};
3065 struct gl_texture_object *texObj = texImage->TexObject;
3066 struct st_texture_image *stImage = st_texture_image(texImage);
3067 struct pipe_resource *pt = stImage->pt;
3068 struct st_context *st = st_context(ctx);
3069 struct pipe_context *pipe = st->pipe;
3070 unsigned level;
3071 struct pipe_box box;
3072
3073 if (!pt)
3074 return;
3075
3076 st_flush_bitmap_cache(st);
3077 st_invalidate_readpix_cache(st);
3078
3079 u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
3080 width, height, depth, &box);
3081 if (texObj->Immutable) {
3082 /* The texture object has to be consistent (no "loose", per-image
3083 * gallium resources). If this texture is a view into another
3084 * texture, we have to apply the MinLevel/Layer offsets. If this is
3085 * not a texture view, the offsets will be zero.
3086 */
3087 assert(stImage->pt == st_texture_object(texObj)->pt);
3088 level = texImage->Level + texObj->MinLevel;
3089 box.z += texObj->MinLayer;
3090 }
3091 else {
3092 /* Texture level sizes may be inconsistent. We my have "loose",
3093 * per-image gallium resources. The texImage->Level may not match
3094 * the gallium resource texture level.
3095 */
3096 level = find_mipmap_level(texImage, pt);
3097 }
3098
3099 assert(level <= pt->last_level);
3100
3101 pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
3102 }
3103
3104
3105 /**
3106 * Called via the glTexParam*() function, but only when some texture object
3107 * state has actually changed.
3108 */
3109 static void
3110 st_TexParameter(struct gl_context *ctx,
3111 struct gl_texture_object *texObj, GLenum pname)
3112 {
3113 struct st_context *st = st_context(ctx);
3114 struct st_texture_object *stObj = st_texture_object(texObj);
3115
3116 switch (pname) {
3117 case GL_TEXTURE_BASE_LEVEL:
3118 case GL_TEXTURE_MAX_LEVEL:
3119 case GL_DEPTH_TEXTURE_MODE:
3120 case GL_DEPTH_STENCIL_TEXTURE_MODE:
3121 case GL_TEXTURE_SRGB_DECODE_EXT:
3122 case GL_TEXTURE_SWIZZLE_R:
3123 case GL_TEXTURE_SWIZZLE_G:
3124 case GL_TEXTURE_SWIZZLE_B:
3125 case GL_TEXTURE_SWIZZLE_A:
3126 case GL_TEXTURE_SWIZZLE_RGBA:
3127 case GL_TEXTURE_BUFFER_SIZE:
3128 case GL_TEXTURE_BUFFER_OFFSET:
3129 /* changing any of these texture parameters means we must create
3130 * new sampler views.
3131 */
3132 st_texture_release_all_sampler_views(st, stObj);
3133 break;
3134 default:
3135 ; /* nothing */
3136 }
3137 }
3138
3139 static GLboolean
3140 st_SetTextureStorageForMemoryObject(struct gl_context *ctx,
3141 struct gl_texture_object *texObj,
3142 struct gl_memory_object *memObj,
3143 GLsizei levels, GLsizei width,
3144 GLsizei height, GLsizei depth,
3145 GLuint64 offset)
3146 {
3147 return st_texture_storage(ctx, texObj, levels,
3148 width, height, depth,
3149 memObj, offset);
3150 }
3151
3152 static GLuint64
3153 st_NewTextureHandle(struct gl_context *ctx, struct gl_texture_object *texObj,
3154 struct gl_sampler_object *sampObj)
3155 {
3156 struct st_context *st = st_context(ctx);
3157 struct st_texture_object *stObj = st_texture_object(texObj);
3158 struct pipe_context *pipe = st->pipe;
3159 struct pipe_sampler_view *view;
3160 struct pipe_sampler_state sampler = {0};
3161
3162 if (texObj->Target != GL_TEXTURE_BUFFER) {
3163 if (!st_finalize_texture(ctx, pipe, texObj, 0))
3164 return 0;
3165
3166 st_convert_sampler(st, texObj, sampObj, 0, &sampler);
3167
3168 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3169 view = st_get_texture_sampler_view_from_stobj(st, stObj, sampObj, 0, true);
3170 } else {
3171 view = st_get_buffer_sampler_view_from_stobj(st, stObj);
3172 }
3173
3174 return pipe->create_texture_handle(pipe, view, &sampler);
3175 }
3176
3177
3178 static void
3179 st_DeleteTextureHandle(struct gl_context *ctx, GLuint64 handle)
3180 {
3181 struct st_context *st = st_context(ctx);
3182 struct pipe_context *pipe = st->pipe;
3183
3184 pipe->delete_texture_handle(pipe, handle);
3185 }
3186
3187
3188 static void
3189 st_MakeTextureHandleResident(struct gl_context *ctx, GLuint64 handle,
3190 bool resident)
3191 {
3192 struct st_context *st = st_context(ctx);
3193 struct pipe_context *pipe = st->pipe;
3194
3195 pipe->make_texture_handle_resident(pipe, handle, resident);
3196 }
3197
3198
3199 static GLuint64
3200 st_NewImageHandle(struct gl_context *ctx, struct gl_image_unit *imgObj)
3201 {
3202 struct st_context *st = st_context(ctx);
3203 struct pipe_context *pipe = st->pipe;
3204 struct pipe_image_view image;
3205
3206 st_convert_image(st, imgObj, &image);
3207
3208 return pipe->create_image_handle(pipe, &image);
3209 }
3210
3211
3212 static void
3213 st_DeleteImageHandle(struct gl_context *ctx, GLuint64 handle)
3214 {
3215 struct st_context *st = st_context(ctx);
3216 struct pipe_context *pipe = st->pipe;
3217
3218 pipe->delete_image_handle(pipe, handle);
3219 }
3220
3221
3222 static void
3223 st_MakeImageHandleResident(struct gl_context *ctx, GLuint64 handle,
3224 GLenum access, bool resident)
3225 {
3226 struct st_context *st = st_context(ctx);
3227 struct pipe_context *pipe = st->pipe;
3228
3229 pipe->make_image_handle_resident(pipe, handle, access, resident);
3230 }
3231
3232
3233 void
3234 st_init_texture_functions(struct dd_function_table *functions)
3235 {
3236 functions->ChooseTextureFormat = st_ChooseTextureFormat;
3237 functions->QueryInternalFormat = st_QueryInternalFormat;
3238 functions->TexImage = st_TexImage;
3239 functions->TexSubImage = st_TexSubImage;
3240 functions->CompressedTexSubImage = st_CompressedTexSubImage;
3241 functions->CopyTexSubImage = st_CopyTexSubImage;
3242 functions->GenerateMipmap = st_generate_mipmap;
3243
3244 functions->GetTexSubImage = st_GetTexSubImage;
3245
3246 /* compressed texture functions */
3247 functions->CompressedTexImage = st_CompressedTexImage;
3248
3249 functions->NewTextureObject = st_NewTextureObject;
3250 functions->NewTextureImage = st_NewTextureImage;
3251 functions->DeleteTextureImage = st_DeleteTextureImage;
3252 functions->DeleteTexture = st_DeleteTextureObject;
3253 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
3254 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
3255 functions->MapTextureImage = st_MapTextureImage;
3256 functions->UnmapTextureImage = st_UnmapTextureImage;
3257
3258 /* XXX Temporary until we can query pipe's texture sizes */
3259 functions->TestProxyTexImage = st_TestProxyTexImage;
3260
3261 functions->AllocTextureStorage = st_AllocTextureStorage;
3262 functions->TextureView = st_TextureView;
3263 functions->ClearTexSubImage = st_ClearTexSubImage;
3264
3265 functions->TexParameter = st_TexParameter;
3266
3267 /* bindless functions */
3268 functions->NewTextureHandle = st_NewTextureHandle;
3269 functions->DeleteTextureHandle = st_DeleteTextureHandle;
3270 functions->MakeTextureHandleResident = st_MakeTextureHandleResident;
3271 functions->NewImageHandle = st_NewImageHandle;
3272 functions->DeleteImageHandle = st_DeleteImageHandle;
3273 functions->MakeImageHandleResident = st_MakeImageHandleResident;
3274
3275 /* external object functions */
3276 functions->SetTextureStorageForMemoryObject = st_SetTextureStorageForMemoryObject;
3277 }