st/mesa: generalize code for the compressed texture map/unmap fallback
[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_BPTC_RGBA_UNORM:
1971 dst_glformat = GL_RGBA8;
1972 break;
1973 case PIPE_FORMAT_RGTC1_SNORM:
1974 case PIPE_FORMAT_RGTC2_SNORM:
1975 if (!ctx->Extensions.EXT_texture_snorm)
1976 goto fallback;
1977 dst_glformat = GL_RGBA8_SNORM;
1978 break;
1979 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1980 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1981 if (!ctx->Extensions.ARB_texture_float)
1982 goto fallback;
1983 dst_glformat = GL_RGBA32F;
1984 break;
1985 default:
1986 assert(0);
1987 goto fallback;
1988 }
1989
1990 dst_format = st_choose_format(st, dst_glformat, format, type,
1991 pipe_target, 0, bind, FALSE);
1992
1993 if (dst_format == PIPE_FORMAT_NONE) {
1994 /* unable to get an rgba format!?! */
1995 goto fallback;
1996 }
1997 }
1998
1999 /* create the destination texture of size (width X height X depth) */
2000 memset(&dst_templ, 0, sizeof(dst_templ));
2001 dst_templ.target = pipe_target;
2002 dst_templ.format = dst_format;
2003 dst_templ.bind = bind;
2004 dst_templ.usage = PIPE_USAGE_STAGING;
2005
2006 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
2007 &dst_templ.width0, &dst_templ.height0,
2008 &dst_templ.depth0, &dst_templ.array_size);
2009
2010 dst = screen->resource_create(screen, &dst_templ);
2011 if (!dst) {
2012 goto fallback;
2013 }
2014
2015 /* From now on, we need the gallium representation of dimensions. */
2016 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2017 zoffset = yoffset;
2018 yoffset = 0;
2019 depth = height;
2020 height = 1;
2021 }
2022
2023 assert(texImage->Face == 0 ||
2024 texImage->TexObject->MinLayer == 0 ||
2025 zoffset == 0);
2026
2027 memset(&blit, 0, sizeof(blit));
2028 blit.src.resource = src;
2029 blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
2030 blit.src.format = src_format;
2031 blit.dst.resource = dst;
2032 blit.dst.level = 0;
2033 blit.dst.format = dst->format;
2034 blit.src.box.x = xoffset;
2035 blit.dst.box.x = 0;
2036 blit.src.box.y = yoffset;
2037 blit.dst.box.y = 0;
2038 blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
2039 blit.dst.box.z = 0;
2040 blit.src.box.width = blit.dst.box.width = width;
2041 blit.src.box.height = blit.dst.box.height = height;
2042 blit.src.box.depth = blit.dst.box.depth = depth;
2043 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
2044 blit.filter = PIPE_TEX_FILTER_NEAREST;
2045 blit.scissor_enable = FALSE;
2046
2047 /* blit/render/decompress */
2048 st->pipe->blit(st->pipe, &blit);
2049
2050 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
2051
2052 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
2053 0, 0, 0, width, height, depth, &tex_xfer);
2054 if (!map) {
2055 goto end;
2056 }
2057
2058 mesa_format = st_pipe_format_to_mesa_format(dst_format);
2059 dims = _mesa_get_texture_dimensions(gl_target);
2060
2061 /* copy/pack data into user buffer */
2062 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2063 ctx->Pack.SwapBytes, NULL)) {
2064 /* memcpy */
2065 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
2066 GLuint row, slice;
2067
2068 for (slice = 0; slice < depth; slice++) {
2069 ubyte *slice_map = map;
2070
2071 for (row = 0; row < height; row++) {
2072 void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
2073 width, height, format, type,
2074 slice, row, 0);
2075
2076 memcpy(dest, slice_map, bytesPerRow);
2077
2078 slice_map += tex_xfer->stride;
2079 }
2080
2081 map += tex_xfer->layer_stride;
2082 }
2083 }
2084 else {
2085 /* format translation via floats */
2086 GLuint slice;
2087 GLfloat *rgba;
2088 uint32_t dstMesaFormat;
2089 int dstStride, srcStride;
2090
2091 assert(util_format_is_compressed(src->format));
2092
2093 rgba = malloc(width * height * 4 * sizeof(GLfloat));
2094 if (!rgba) {
2095 goto end;
2096 }
2097
2098 if (ST_DEBUG & DEBUG_FALLBACK)
2099 debug_printf("%s: fallback format translation\n", __func__);
2100
2101 dstMesaFormat = _mesa_format_from_format_and_type(format, type);
2102 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
2103 srcStride = 4 * width * sizeof(GLfloat);
2104 for (slice = 0; slice < depth; slice++) {
2105 void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
2106 width, height, format, type,
2107 slice, 0, 0);
2108
2109 /* get float[4] rgba row from surface */
2110 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, height,
2111 dst_format, rgba);
2112
2113 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2114 rgba, RGBA32_FLOAT, srcStride,
2115 width, height, NULL);
2116
2117 /* Handle byte swapping if required */
2118 if (ctx->Pack.SwapBytes) {
2119 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
2120 width, height, dest, dest);
2121 }
2122
2123 map += tex_xfer->layer_stride;
2124 }
2125
2126 free(rgba);
2127 }
2128 done = TRUE;
2129
2130 end:
2131 if (map)
2132 pipe_transfer_unmap(pipe, tex_xfer);
2133
2134 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
2135 pipe_resource_reference(&dst, NULL);
2136
2137 fallback:
2138 if (!done) {
2139 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
2140 width, height, depth,
2141 format, type, pixels, texImage);
2142 }
2143 }
2144
2145
2146 /**
2147 * Do a CopyTexSubImage operation using a read transfer from the source,
2148 * a write transfer to the destination and get_tile()/put_tile() to access
2149 * the pixels/texels.
2150 *
2151 * Note: srcY=0=TOP of renderbuffer
2152 */
2153 static void
2154 fallback_copy_texsubimage(struct gl_context *ctx,
2155 struct st_renderbuffer *strb,
2156 struct st_texture_image *stImage,
2157 GLenum baseFormat,
2158 GLint destX, GLint destY, GLint slice,
2159 GLint srcX, GLint srcY,
2160 GLsizei width, GLsizei height)
2161 {
2162 struct st_context *st = st_context(ctx);
2163 struct pipe_context *pipe = st->pipe;
2164 struct pipe_transfer *src_trans;
2165 GLubyte *texDest;
2166 enum pipe_transfer_usage transfer_usage;
2167 void *map;
2168 unsigned dst_width = width;
2169 unsigned dst_height = height;
2170 unsigned dst_depth = 1;
2171 struct pipe_transfer *transfer;
2172
2173 if (ST_DEBUG & DEBUG_FALLBACK)
2174 debug_printf("%s: fallback processing\n", __func__);
2175
2176 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2177 srcY = strb->Base.Height - srcY - height;
2178 }
2179
2180 map = pipe_transfer_map(pipe,
2181 strb->texture,
2182 strb->surface->u.tex.level,
2183 strb->surface->u.tex.first_layer,
2184 PIPE_TRANSFER_READ,
2185 srcX, srcY,
2186 width, height, &src_trans);
2187
2188 if ((baseFormat == GL_DEPTH_COMPONENT ||
2189 baseFormat == GL_DEPTH_STENCIL) &&
2190 util_format_is_depth_and_stencil(stImage->pt->format))
2191 transfer_usage = PIPE_TRANSFER_READ_WRITE;
2192 else
2193 transfer_usage = PIPE_TRANSFER_WRITE;
2194
2195 texDest = st_texture_image_map(st, stImage, transfer_usage,
2196 destX, destY, slice,
2197 dst_width, dst_height, dst_depth,
2198 &transfer);
2199
2200 if (baseFormat == GL_DEPTH_COMPONENT ||
2201 baseFormat == GL_DEPTH_STENCIL) {
2202 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
2203 ctx->Pixel.DepthBias != 0.0F);
2204 GLint row, yStep;
2205 uint *data;
2206
2207 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2208 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2209 srcY = height - 1;
2210 yStep = -1;
2211 }
2212 else {
2213 srcY = 0;
2214 yStep = 1;
2215 }
2216
2217 data = malloc(width * sizeof(uint));
2218
2219 if (data) {
2220 /* To avoid a large temp memory allocation, do copy row by row */
2221 for (row = 0; row < height; row++, srcY += yStep) {
2222 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
2223 if (scaleOrBias) {
2224 _mesa_scale_and_bias_depth_uint(ctx, width, data);
2225 }
2226
2227 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2228 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
2229 0, 0, width, 1, data);
2230 }
2231 else {
2232 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
2233 }
2234 }
2235 }
2236 else {
2237 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
2238 }
2239
2240 free(data);
2241 }
2242 else {
2243 /* RGBA format */
2244 GLfloat *tempSrc =
2245 malloc(width * height * 4 * sizeof(GLfloat));
2246
2247 if (tempSrc && texDest) {
2248 const GLint dims = 2;
2249 GLint dstRowStride;
2250 struct gl_texture_image *texImage = &stImage->base;
2251 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
2252
2253 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2254 unpack.Invert = GL_TRUE;
2255 }
2256
2257 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2258 dstRowStride = transfer->layer_stride;
2259 }
2260 else {
2261 dstRowStride = transfer->stride;
2262 }
2263
2264 /* get float/RGBA image from framebuffer */
2265 /* XXX this usually involves a lot of int/float conversion.
2266 * try to avoid that someday.
2267 */
2268 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
2269 util_format_linear(strb->texture->format),
2270 tempSrc);
2271
2272 /* Store into texture memory.
2273 * Note that this does some special things such as pixel transfer
2274 * ops and format conversion. In particular, if the dest tex format
2275 * is actually RGBA but the user created the texture as GL_RGB we
2276 * need to fill-in/override the alpha channel with 1.0.
2277 */
2278 _mesa_texstore(ctx, dims,
2279 texImage->_BaseFormat,
2280 texImage->TexFormat,
2281 dstRowStride,
2282 &texDest,
2283 width, height, 1,
2284 GL_RGBA, GL_FLOAT, tempSrc, /* src */
2285 &unpack);
2286 }
2287 else {
2288 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
2289 }
2290
2291 free(tempSrc);
2292 }
2293
2294 st_texture_image_unmap(st, stImage, slice);
2295 pipe->transfer_unmap(pipe, src_trans);
2296 }
2297
2298 static bool
2299 st_can_copyteximage_using_blit(const struct gl_texture_image *texImage,
2300 const struct gl_renderbuffer *rb)
2301 {
2302 GLenum tex_baseformat = _mesa_get_format_base_format(texImage->TexFormat);
2303
2304 /* We don't blit to a teximage where the GL base format doesn't match the
2305 * texture's chosen format, except in the case of a GL_RGB texture
2306 * represented with GL_RGBA (where the alpha channel is just being
2307 * dropped).
2308 */
2309 if (texImage->_BaseFormat != tex_baseformat &&
2310 ((texImage->_BaseFormat != GL_RGB || tex_baseformat != GL_RGBA))) {
2311 return false;
2312 }
2313
2314 /* We can't blit from a RB where the GL base format doesn't match the RB's
2315 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2316 * type, because the other channels will be undefined).
2317 */
2318 if (rb->_BaseFormat != _mesa_get_format_base_format(rb->Format))
2319 return false;
2320
2321 return true;
2322 }
2323
2324 /**
2325 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2326 * Note that the region to copy has already been clipped so we know we
2327 * won't read from outside the source renderbuffer's bounds.
2328 *
2329 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2330 */
2331 static void
2332 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2333 struct gl_texture_image *texImage,
2334 GLint destX, GLint destY, GLint slice,
2335 struct gl_renderbuffer *rb,
2336 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
2337 {
2338 struct st_texture_image *stImage = st_texture_image(texImage);
2339 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2340 struct st_renderbuffer *strb = st_renderbuffer(rb);
2341 struct st_context *st = st_context(ctx);
2342 struct pipe_context *pipe = st->pipe;
2343 struct pipe_screen *screen = pipe->screen;
2344 struct pipe_blit_info blit;
2345 enum pipe_format dst_format;
2346 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
2347 unsigned bind;
2348 GLint srcY0, srcY1;
2349
2350 st_flush_bitmap_cache(st);
2351 st_invalidate_readpix_cache(st);
2352
2353 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2354 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2355
2356 if (!strb || !strb->surface || !stImage->pt) {
2357 debug_printf("%s: null strb or stImage\n", __func__);
2358 return;
2359 }
2360
2361 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
2362 texImage->TexFormat)) {
2363 goto fallback;
2364 }
2365
2366 if (!st_can_copyteximage_using_blit(texImage, rb)) {
2367 goto fallback;
2368 }
2369
2370 /* Choose the destination format to match the TexImage behavior. */
2371 dst_format = util_format_linear(stImage->pt->format);
2372 dst_format = util_format_luminance_to_red(dst_format);
2373 dst_format = util_format_intensity_to_red(dst_format);
2374
2375 /* See if the destination format is supported. */
2376 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
2377 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
2378 bind = PIPE_BIND_DEPTH_STENCIL;
2379 }
2380 else {
2381 bind = PIPE_BIND_RENDER_TARGET;
2382 }
2383
2384 if (!dst_format ||
2385 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
2386 stImage->pt->nr_samples, bind)) {
2387 goto fallback;
2388 }
2389
2390 /* Y flipping for the main framebuffer. */
2391 if (do_flip) {
2392 srcY1 = strb->Base.Height - srcY - height;
2393 srcY0 = srcY1 + height;
2394 }
2395 else {
2396 srcY0 = srcY;
2397 srcY1 = srcY0 + height;
2398 }
2399
2400 /* Blit the texture.
2401 * This supports flipping, format conversions, and downsampling.
2402 */
2403 memset(&blit, 0, sizeof(blit));
2404 blit.src.resource = strb->texture;
2405 blit.src.format = util_format_linear(strb->surface->format);
2406 blit.src.level = strb->surface->u.tex.level;
2407 blit.src.box.x = srcX;
2408 blit.src.box.y = srcY0;
2409 blit.src.box.z = strb->surface->u.tex.first_layer;
2410 blit.src.box.width = width;
2411 blit.src.box.height = srcY1 - srcY0;
2412 blit.src.box.depth = 1;
2413 blit.dst.resource = stImage->pt;
2414 blit.dst.format = dst_format;
2415 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
2416 blit.dst.box.x = destX;
2417 blit.dst.box.y = destY;
2418 blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
2419 blit.dst.box.width = width;
2420 blit.dst.box.height = height;
2421 blit.dst.box.depth = 1;
2422 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
2423 blit.filter = PIPE_TEX_FILTER_NEAREST;
2424 pipe->blit(pipe, &blit);
2425 return;
2426
2427 fallback:
2428 /* software fallback */
2429 fallback_copy_texsubimage(ctx,
2430 strb, stImage, texImage->_BaseFormat,
2431 destX, destY, slice,
2432 srcX, srcY, width, height);
2433 }
2434
2435
2436 /**
2437 * Copy image data from stImage into the texture object 'stObj' at level
2438 * 'dstLevel'.
2439 */
2440 static void
2441 copy_image_data_to_texture(struct st_context *st,
2442 struct st_texture_object *stObj,
2443 GLuint dstLevel,
2444 struct st_texture_image *stImage)
2445 {
2446 /* debug checks */
2447 {
2448 const struct gl_texture_image MAYBE_UNUSED *dstImage =
2449 stObj->base.Image[stImage->base.Face][dstLevel];
2450 assert(dstImage);
2451 assert(dstImage->Width == stImage->base.Width);
2452 assert(dstImage->Height == stImage->base.Height);
2453 assert(dstImage->Depth == stImage->base.Depth);
2454 }
2455
2456 if (stImage->pt) {
2457 /* Copy potentially with the blitter:
2458 */
2459 GLuint src_level;
2460 if (stImage->pt->last_level == 0)
2461 src_level = 0;
2462 else
2463 src_level = stImage->base.Level;
2464
2465 assert(src_level <= stImage->pt->last_level);
2466 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
2467 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
2468 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
2469 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
2470 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
2471 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
2472
2473 st_texture_image_copy(st->pipe,
2474 stObj->pt, dstLevel, /* dest texture, level */
2475 stImage->pt, src_level, /* src texture, level */
2476 stImage->base.Face);
2477
2478 pipe_resource_reference(&stImage->pt, NULL);
2479 }
2480 pipe_resource_reference(&stImage->pt, stObj->pt);
2481 }
2482
2483
2484 /**
2485 * Called during state validation. When this function is finished,
2486 * the texture object should be ready for rendering.
2487 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2488 */
2489 GLboolean
2490 st_finalize_texture(struct gl_context *ctx,
2491 struct pipe_context *pipe,
2492 struct gl_texture_object *tObj,
2493 GLuint cubeMapFace)
2494 {
2495 struct st_context *st = st_context(ctx);
2496 struct st_texture_object *stObj = st_texture_object(tObj);
2497 const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
2498 GLuint face;
2499 const struct st_texture_image *firstImage;
2500 enum pipe_format firstImageFormat;
2501 unsigned ptWidth;
2502 uint16_t ptHeight, ptDepth, ptLayers, ptNumSamples;
2503
2504 if (tObj->Immutable)
2505 return GL_TRUE;
2506
2507 if (tObj->_MipmapComplete)
2508 stObj->lastLevel = stObj->base._MaxLevel;
2509 else if (tObj->_BaseComplete)
2510 stObj->lastLevel = stObj->base.BaseLevel;
2511
2512 /* Skip the loop over images in the common case of no images having
2513 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2514 * haven't looked at, then we do need to look at those new images.
2515 */
2516 if (!stObj->needs_validation &&
2517 stObj->base.BaseLevel >= stObj->validated_first_level &&
2518 stObj->lastLevel <= stObj->validated_last_level) {
2519 return GL_TRUE;
2520 }
2521
2522 /* If this texture comes from a window system, there is nothing else to do. */
2523 if (stObj->surface_based) {
2524 return GL_TRUE;
2525 }
2526
2527 firstImage = st_texture_image_const(stObj->base.Image[cubeMapFace][stObj->base.BaseLevel]);
2528 assert(firstImage);
2529
2530 /* If both firstImage and stObj point to a texture which can contain
2531 * all active images, favour firstImage. Note that because of the
2532 * completeness requirement, we know that the image dimensions
2533 * will match.
2534 */
2535 if (firstImage->pt &&
2536 firstImage->pt != stObj->pt &&
2537 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
2538 pipe_resource_reference(&stObj->pt, firstImage->pt);
2539 st_texture_release_all_sampler_views(st, stObj);
2540 }
2541
2542 /* Find gallium format for the Mesa texture */
2543 firstImageFormat =
2544 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
2545
2546 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2547 {
2548 unsigned width;
2549 uint16_t height, depth;
2550
2551 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
2552 firstImage->base.Width2,
2553 firstImage->base.Height2,
2554 firstImage->base.Depth2,
2555 &width, &height, &depth, &ptLayers);
2556
2557 /* If we previously allocated a pipe texture and its sizes are
2558 * compatible, use them.
2559 */
2560 if (stObj->pt &&
2561 u_minify(stObj->pt->width0, firstImage->base.Level) == width &&
2562 u_minify(stObj->pt->height0, firstImage->base.Level) == height &&
2563 u_minify(stObj->pt->depth0, firstImage->base.Level) == depth) {
2564 ptWidth = stObj->pt->width0;
2565 ptHeight = stObj->pt->height0;
2566 ptDepth = stObj->pt->depth0;
2567 } else {
2568 /* Otherwise, compute a new level=0 size that is compatible with the
2569 * base level image.
2570 */
2571 ptWidth = width > 1 ? width << firstImage->base.Level : 1;
2572 ptHeight = height > 1 ? height << firstImage->base.Level : 1;
2573 ptDepth = depth > 1 ? depth << firstImage->base.Level : 1;
2574
2575 /* If the base level image is 1x1x1, we still need to ensure that the
2576 * resulting pipe texture ends up with the required number of levels
2577 * in total.
2578 */
2579 if (ptWidth == 1 && ptHeight == 1 && ptDepth == 1) {
2580 ptWidth <<= firstImage->base.Level;
2581
2582 if (stObj->base.Target == GL_TEXTURE_CUBE_MAP ||
2583 stObj->base.Target == GL_TEXTURE_CUBE_MAP_ARRAY)
2584 ptHeight = ptWidth;
2585 }
2586
2587 /* At this point, the texture may be incomplete (mismatched cube
2588 * face sizes, for example). If that's the case, give up, but
2589 * don't return GL_FALSE as that would raise an incorrect
2590 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2591 */
2592 if (!stObj->base._BaseComplete) {
2593 _mesa_test_texobj_completeness(ctx, &stObj->base);
2594 if (!stObj->base._BaseComplete) {
2595 return TRUE;
2596 }
2597 }
2598 }
2599
2600 ptNumSamples = firstImage->base.NumSamples;
2601 }
2602
2603 /* If we already have a gallium texture, check that it matches the texture
2604 * object's format, target, size, num_levels, etc.
2605 */
2606 if (stObj->pt) {
2607 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
2608 stObj->pt->format != firstImageFormat ||
2609 stObj->pt->last_level < stObj->lastLevel ||
2610 stObj->pt->width0 != ptWidth ||
2611 stObj->pt->height0 != ptHeight ||
2612 stObj->pt->depth0 != ptDepth ||
2613 stObj->pt->nr_samples != ptNumSamples ||
2614 stObj->pt->array_size != ptLayers)
2615 {
2616 /* The gallium texture does not match the Mesa texture so delete the
2617 * gallium texture now. We'll make a new one below.
2618 */
2619 pipe_resource_reference(&stObj->pt, NULL);
2620 st_texture_release_all_sampler_views(st, stObj);
2621 st->dirty |= ST_NEW_FRAMEBUFFER;
2622 }
2623 }
2624
2625 /* May need to create a new gallium texture:
2626 */
2627 if (!stObj->pt) {
2628 GLuint bindings = default_bindings(st, firstImageFormat);
2629
2630 stObj->pt = st_texture_create(st,
2631 gl_target_to_pipe(stObj->base.Target),
2632 firstImageFormat,
2633 stObj->lastLevel,
2634 ptWidth,
2635 ptHeight,
2636 ptDepth,
2637 ptLayers, ptNumSamples,
2638 bindings);
2639
2640 if (!stObj->pt) {
2641 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
2642 return GL_FALSE;
2643 }
2644 }
2645
2646 /* Pull in any images not in the object's texture:
2647 */
2648 for (face = 0; face < nr_faces; face++) {
2649 GLuint level;
2650 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
2651 struct st_texture_image *stImage =
2652 st_texture_image(stObj->base.Image[face][level]);
2653
2654 /* Need to import images in main memory or held in other textures.
2655 */
2656 if (stImage && stObj->pt != stImage->pt) {
2657 GLuint height;
2658 GLuint depth;
2659
2660 if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
2661 height = u_minify(ptHeight, level);
2662 else
2663 height = ptLayers;
2664
2665 if (stObj->base.Target == GL_TEXTURE_3D)
2666 depth = u_minify(ptDepth, level);
2667 else if (stObj->base.Target == GL_TEXTURE_CUBE_MAP)
2668 depth = 1;
2669 else
2670 depth = ptLayers;
2671
2672 if (level == 0 ||
2673 (stImage->base.Width == u_minify(ptWidth, level) &&
2674 stImage->base.Height == height &&
2675 stImage->base.Depth == depth)) {
2676 /* src image fits expected dest mipmap level size */
2677 copy_image_data_to_texture(st, stObj, level, stImage);
2678 }
2679 }
2680 }
2681 }
2682
2683 stObj->validated_first_level = stObj->base.BaseLevel;
2684 stObj->validated_last_level = stObj->lastLevel;
2685 stObj->needs_validation = false;
2686
2687 return GL_TRUE;
2688 }
2689
2690 /**
2691 * Allocate a new pipe_resource object
2692 * width0, height0, depth0 are the dimensions of the level 0 image
2693 * (the highest resolution). last_level indicates how many mipmap levels
2694 * to allocate storage for. For non-mipmapped textures, this will be zero.
2695 */
2696 static struct pipe_resource *
2697 st_texture_create_from_memory(struct st_context *st,
2698 struct st_memory_object *memObj,
2699 GLuint64 offset,
2700 enum pipe_texture_target target,
2701 enum pipe_format format,
2702 GLuint last_level,
2703 GLuint width0,
2704 GLuint height0,
2705 GLuint depth0,
2706 GLuint layers,
2707 GLuint nr_samples,
2708 GLuint bind )
2709 {
2710 struct pipe_resource pt, *newtex;
2711 struct pipe_screen *screen = st->pipe->screen;
2712
2713 assert(target < PIPE_MAX_TEXTURE_TYPES);
2714 assert(width0 > 0);
2715 assert(height0 > 0);
2716 assert(depth0 > 0);
2717 if (target == PIPE_TEXTURE_CUBE)
2718 assert(layers == 6);
2719
2720 DBG("%s target %d format %s last_level %d\n", __func__,
2721 (int) target, util_format_name(format), last_level);
2722
2723 assert(format);
2724 assert(screen->is_format_supported(screen, format, target, 0,
2725 PIPE_BIND_SAMPLER_VIEW));
2726
2727 memset(&pt, 0, sizeof(pt));
2728 pt.target = target;
2729 pt.format = format;
2730 pt.last_level = last_level;
2731 pt.width0 = width0;
2732 pt.height0 = height0;
2733 pt.depth0 = depth0;
2734 pt.array_size = layers;
2735 pt.usage = PIPE_USAGE_DEFAULT;
2736 pt.bind = bind;
2737 /* only set this for OpenGL textures, not renderbuffers */
2738 pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
2739 pt.nr_samples = nr_samples;
2740
2741 newtex = screen->resource_from_memobj(screen, &pt, memObj->memory, offset);
2742
2743 assert(!newtex || pipe_is_referenced(&newtex->reference));
2744
2745 return newtex;
2746 }
2747
2748 /**
2749 * Allocate texture memory for a whole mipmap stack.
2750 * Note: for multisample textures if the requested sample count is not
2751 * supported, we search for the next higher supported sample count.
2752 */
2753 static GLboolean
2754 st_texture_storage(struct gl_context *ctx,
2755 struct gl_texture_object *texObj,
2756 GLsizei levels, GLsizei width,
2757 GLsizei height, GLsizei depth,
2758 struct gl_memory_object *memObj,
2759 GLuint64 offset)
2760 {
2761 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
2762 struct gl_texture_image *texImage = texObj->Image[0][0];
2763 struct st_context *st = st_context(ctx);
2764 struct st_texture_object *stObj = st_texture_object(texObj);
2765 struct st_memory_object *smObj = st_memory_object(memObj);
2766 struct pipe_screen *screen = st->pipe->screen;
2767 unsigned ptWidth, bindings;
2768 uint16_t ptHeight, ptDepth, ptLayers;
2769 enum pipe_format fmt;
2770 GLint level;
2771 GLuint num_samples = texImage->NumSamples;
2772
2773 assert(levels > 0);
2774
2775 stObj->lastLevel = levels - 1;
2776
2777 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
2778
2779 bindings = default_bindings(st, fmt);
2780
2781 if (num_samples > 0) {
2782 /* Find msaa sample count which is actually supported. For example,
2783 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2784 * choose 4x here.
2785 */
2786 enum pipe_texture_target ptarget = gl_target_to_pipe(texObj->Target);
2787 boolean found = FALSE;
2788
2789 if (ctx->Const.MaxSamples > 1 && num_samples == 1) {
2790 /* don't try num_samples = 1 with drivers that support real msaa */
2791 num_samples = 2;
2792 }
2793
2794 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
2795 if (screen->is_format_supported(screen, fmt, ptarget,
2796 num_samples,
2797 PIPE_BIND_SAMPLER_VIEW)) {
2798 /* Update the sample count in gl_texture_image as well. */
2799 texImage->NumSamples = num_samples;
2800 found = TRUE;
2801 break;
2802 }
2803 }
2804
2805 if (!found) {
2806 return GL_FALSE;
2807 }
2808 }
2809
2810 st_gl_texture_dims_to_pipe_dims(texObj->Target,
2811 width, height, depth,
2812 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2813
2814 if (smObj) {
2815 stObj->pt = st_texture_create_from_memory(st,
2816 smObj,
2817 offset,
2818 gl_target_to_pipe(texObj->Target),
2819 fmt,
2820 levels - 1,
2821 ptWidth,
2822 ptHeight,
2823 ptDepth,
2824 ptLayers, num_samples,
2825 bindings);
2826 }
2827 else {
2828 stObj->pt = st_texture_create(st,
2829 gl_target_to_pipe(texObj->Target),
2830 fmt,
2831 levels - 1,
2832 ptWidth,
2833 ptHeight,
2834 ptDepth,
2835 ptLayers, num_samples,
2836 bindings);
2837 }
2838
2839 if (!stObj->pt)
2840 return GL_FALSE;
2841
2842 /* Set image resource pointers */
2843 for (level = 0; level < levels; level++) {
2844 GLuint face;
2845 for (face = 0; face < numFaces; face++) {
2846 struct st_texture_image *stImage =
2847 st_texture_image(texObj->Image[face][level]);
2848 pipe_resource_reference(&stImage->pt, stObj->pt);
2849
2850 compressed_tex_fallback_allocate(st, stImage);
2851 }
2852 }
2853
2854 /* The texture is in a validated state, so no need to check later. */
2855 stObj->needs_validation = false;
2856 stObj->validated_first_level = 0;
2857 stObj->validated_last_level = levels - 1;
2858
2859 return GL_TRUE;
2860 }
2861
2862 /**
2863 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2864 * for a whole mipmap stack.
2865 */
2866 static GLboolean
2867 st_AllocTextureStorage(struct gl_context *ctx,
2868 struct gl_texture_object *texObj,
2869 GLsizei levels, GLsizei width,
2870 GLsizei height, GLsizei depth)
2871 {
2872 return st_texture_storage(ctx, texObj, levels,
2873 width, height, depth,
2874 NULL, 0);
2875 }
2876
2877
2878 static GLboolean
2879 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
2880 GLuint numLevels, GLint level,
2881 mesa_format format, GLuint numSamples,
2882 GLint width, GLint height, GLint depth)
2883 {
2884 struct st_context *st = st_context(ctx);
2885 struct pipe_context *pipe = st->pipe;
2886
2887 if (width == 0 || height == 0 || depth == 0) {
2888 /* zero-sized images are legal, and always fit! */
2889 return GL_TRUE;
2890 }
2891
2892 if (pipe->screen->can_create_resource) {
2893 /* Ask the gallium driver if the texture is too large */
2894 struct gl_texture_object *texObj =
2895 _mesa_get_current_tex_object(ctx, target);
2896 struct pipe_resource pt;
2897
2898 /* Setup the pipe_resource object
2899 */
2900 memset(&pt, 0, sizeof(pt));
2901
2902 pt.target = gl_target_to_pipe(target);
2903 pt.format = st_mesa_format_to_pipe_format(st, format);
2904 pt.nr_samples = numSamples;
2905
2906 st_gl_texture_dims_to_pipe_dims(target,
2907 width, height, depth,
2908 &pt.width0, &pt.height0,
2909 &pt.depth0, &pt.array_size);
2910
2911 if (numLevels > 0) {
2912 /* For immutable textures we know the final number of mip levels */
2913 pt.last_level = numLevels - 1;
2914 }
2915 else if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
2916 texObj->Sampler.MinFilter == GL_NEAREST)) {
2917 /* assume just one mipmap level */
2918 pt.last_level = 0;
2919 }
2920 else {
2921 /* assume a full set of mipmaps */
2922 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
2923 }
2924
2925 return pipe->screen->can_create_resource(pipe->screen, &pt);
2926 }
2927 else {
2928 /* Use core Mesa fallback */
2929 return _mesa_test_proxy_teximage(ctx, target, numLevels, level, format,
2930 numSamples, width, height, depth);
2931 }
2932 }
2933
2934 static GLboolean
2935 st_TextureView(struct gl_context *ctx,
2936 struct gl_texture_object *texObj,
2937 struct gl_texture_object *origTexObj)
2938 {
2939 struct st_context *st = st_context(ctx);
2940 struct st_texture_object *orig = st_texture_object(origTexObj);
2941 struct st_texture_object *tex = st_texture_object(texObj);
2942 struct gl_texture_image *image = texObj->Image[0][0];
2943
2944 const int numFaces = _mesa_num_tex_faces(texObj->Target);
2945 const int numLevels = texObj->NumLevels;
2946
2947 int face;
2948 int level;
2949
2950 pipe_resource_reference(&tex->pt, orig->pt);
2951
2952 /* Set image resource pointers */
2953 for (level = 0; level < numLevels; level++) {
2954 for (face = 0; face < numFaces; face++) {
2955 struct st_texture_image *stImage =
2956 st_texture_image(texObj->Image[face][level]);
2957 pipe_resource_reference(&stImage->pt, tex->pt);
2958 }
2959 }
2960
2961 tex->surface_based = GL_TRUE;
2962 tex->surface_format =
2963 st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
2964
2965 tex->lastLevel = numLevels - 1;
2966
2967 /* free texture sampler views. They need to be recreated when we
2968 * change the texture view parameters.
2969 */
2970 st_texture_release_all_sampler_views(st, tex);
2971
2972 /* The texture is in a validated state, so no need to check later. */
2973 tex->needs_validation = false;
2974 tex->validated_first_level = 0;
2975 tex->validated_last_level = numLevels - 1;
2976
2977 return GL_TRUE;
2978 }
2979
2980
2981 /**
2982 * Find the mipmap level in 'pt' which matches the level described by
2983 * 'texImage'.
2984 */
2985 static unsigned
2986 find_mipmap_level(const struct gl_texture_image *texImage,
2987 const struct pipe_resource *pt)
2988 {
2989 const GLenum target = texImage->TexObject->Target;
2990 GLint texWidth = texImage->Width;
2991 GLint texHeight = texImage->Height;
2992 GLint texDepth = texImage->Depth;
2993 unsigned level, w;
2994 uint16_t h, d, layers;
2995
2996 st_gl_texture_dims_to_pipe_dims(target, texWidth, texHeight, texDepth,
2997 &w, &h, &d, &layers);
2998
2999 for (level = 0; level <= pt->last_level; level++) {
3000 if (u_minify(pt->width0, level) == w &&
3001 u_minify(pt->height0, level) == h &&
3002 u_minify(pt->depth0, level) == d) {
3003 return level;
3004 }
3005 }
3006
3007 /* If we get here, there must be some sort of inconsistency between
3008 * the Mesa texture object/images and the gallium resource.
3009 */
3010 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3011
3012 return texImage->Level;
3013 }
3014
3015
3016 static void
3017 st_ClearTexSubImage(struct gl_context *ctx,
3018 struct gl_texture_image *texImage,
3019 GLint xoffset, GLint yoffset, GLint zoffset,
3020 GLsizei width, GLsizei height, GLsizei depth,
3021 const void *clearValue)
3022 {
3023 static const char zeros[16] = {0};
3024 struct gl_texture_object *texObj = texImage->TexObject;
3025 struct st_texture_image *stImage = st_texture_image(texImage);
3026 struct pipe_resource *pt = stImage->pt;
3027 struct st_context *st = st_context(ctx);
3028 struct pipe_context *pipe = st->pipe;
3029 unsigned level;
3030 struct pipe_box box;
3031
3032 if (!pt)
3033 return;
3034
3035 st_flush_bitmap_cache(st);
3036 st_invalidate_readpix_cache(st);
3037
3038 u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
3039 width, height, depth, &box);
3040 if (texObj->Immutable) {
3041 /* The texture object has to be consistent (no "loose", per-image
3042 * gallium resources). If this texture is a view into another
3043 * texture, we have to apply the MinLevel/Layer offsets. If this is
3044 * not a texture view, the offsets will be zero.
3045 */
3046 assert(stImage->pt == st_texture_object(texObj)->pt);
3047 level = texImage->Level + texObj->MinLevel;
3048 box.z += texObj->MinLayer;
3049 }
3050 else {
3051 /* Texture level sizes may be inconsistent. We my have "loose",
3052 * per-image gallium resources. The texImage->Level may not match
3053 * the gallium resource texture level.
3054 */
3055 level = find_mipmap_level(texImage, pt);
3056 }
3057
3058 assert(level <= pt->last_level);
3059
3060 pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
3061 }
3062
3063
3064 /**
3065 * Called via the glTexParam*() function, but only when some texture object
3066 * state has actually changed.
3067 */
3068 static void
3069 st_TexParameter(struct gl_context *ctx,
3070 struct gl_texture_object *texObj, GLenum pname)
3071 {
3072 struct st_context *st = st_context(ctx);
3073 struct st_texture_object *stObj = st_texture_object(texObj);
3074
3075 switch (pname) {
3076 case GL_TEXTURE_BASE_LEVEL:
3077 case GL_TEXTURE_MAX_LEVEL:
3078 case GL_DEPTH_TEXTURE_MODE:
3079 case GL_DEPTH_STENCIL_TEXTURE_MODE:
3080 case GL_TEXTURE_SRGB_DECODE_EXT:
3081 case GL_TEXTURE_SWIZZLE_R:
3082 case GL_TEXTURE_SWIZZLE_G:
3083 case GL_TEXTURE_SWIZZLE_B:
3084 case GL_TEXTURE_SWIZZLE_A:
3085 case GL_TEXTURE_SWIZZLE_RGBA:
3086 case GL_TEXTURE_BUFFER_SIZE:
3087 case GL_TEXTURE_BUFFER_OFFSET:
3088 /* changing any of these texture parameters means we must create
3089 * new sampler views.
3090 */
3091 st_texture_release_all_sampler_views(st, stObj);
3092 break;
3093 default:
3094 ; /* nothing */
3095 }
3096 }
3097
3098 static GLboolean
3099 st_SetTextureStorageForMemoryObject(struct gl_context *ctx,
3100 struct gl_texture_object *texObj,
3101 struct gl_memory_object *memObj,
3102 GLsizei levels, GLsizei width,
3103 GLsizei height, GLsizei depth,
3104 GLuint64 offset)
3105 {
3106 return st_texture_storage(ctx, texObj, levels,
3107 width, height, depth,
3108 memObj, offset);
3109 }
3110
3111 static GLuint64
3112 st_NewTextureHandle(struct gl_context *ctx, struct gl_texture_object *texObj,
3113 struct gl_sampler_object *sampObj)
3114 {
3115 struct st_context *st = st_context(ctx);
3116 struct st_texture_object *stObj = st_texture_object(texObj);
3117 struct pipe_context *pipe = st->pipe;
3118 struct pipe_sampler_view *view;
3119 struct pipe_sampler_state sampler = {0};
3120
3121 if (texObj->Target != GL_TEXTURE_BUFFER) {
3122 if (!st_finalize_texture(ctx, pipe, texObj, 0))
3123 return 0;
3124
3125 st_convert_sampler(st, texObj, sampObj, 0, &sampler);
3126
3127 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3128 view = st_get_texture_sampler_view_from_stobj(st, stObj, sampObj, 0, true);
3129 } else {
3130 view = st_get_buffer_sampler_view_from_stobj(st, stObj);
3131 }
3132
3133 return pipe->create_texture_handle(pipe, view, &sampler);
3134 }
3135
3136
3137 static void
3138 st_DeleteTextureHandle(struct gl_context *ctx, GLuint64 handle)
3139 {
3140 struct st_context *st = st_context(ctx);
3141 struct pipe_context *pipe = st->pipe;
3142
3143 pipe->delete_texture_handle(pipe, handle);
3144 }
3145
3146
3147 static void
3148 st_MakeTextureHandleResident(struct gl_context *ctx, GLuint64 handle,
3149 bool resident)
3150 {
3151 struct st_context *st = st_context(ctx);
3152 struct pipe_context *pipe = st->pipe;
3153
3154 pipe->make_texture_handle_resident(pipe, handle, resident);
3155 }
3156
3157
3158 static GLuint64
3159 st_NewImageHandle(struct gl_context *ctx, struct gl_image_unit *imgObj)
3160 {
3161 struct st_context *st = st_context(ctx);
3162 struct pipe_context *pipe = st->pipe;
3163 struct pipe_image_view image;
3164
3165 st_convert_image(st, imgObj, &image);
3166
3167 return pipe->create_image_handle(pipe, &image);
3168 }
3169
3170
3171 static void
3172 st_DeleteImageHandle(struct gl_context *ctx, GLuint64 handle)
3173 {
3174 struct st_context *st = st_context(ctx);
3175 struct pipe_context *pipe = st->pipe;
3176
3177 pipe->delete_image_handle(pipe, handle);
3178 }
3179
3180
3181 static void
3182 st_MakeImageHandleResident(struct gl_context *ctx, GLuint64 handle,
3183 GLenum access, bool resident)
3184 {
3185 struct st_context *st = st_context(ctx);
3186 struct pipe_context *pipe = st->pipe;
3187
3188 pipe->make_image_handle_resident(pipe, handle, access, resident);
3189 }
3190
3191
3192 void
3193 st_init_texture_functions(struct dd_function_table *functions)
3194 {
3195 functions->ChooseTextureFormat = st_ChooseTextureFormat;
3196 functions->QueryInternalFormat = st_QueryInternalFormat;
3197 functions->TexImage = st_TexImage;
3198 functions->TexSubImage = st_TexSubImage;
3199 functions->CompressedTexSubImage = st_CompressedTexSubImage;
3200 functions->CopyTexSubImage = st_CopyTexSubImage;
3201 functions->GenerateMipmap = st_generate_mipmap;
3202
3203 functions->GetTexSubImage = st_GetTexSubImage;
3204
3205 /* compressed texture functions */
3206 functions->CompressedTexImage = st_CompressedTexImage;
3207
3208 functions->NewTextureObject = st_NewTextureObject;
3209 functions->NewTextureImage = st_NewTextureImage;
3210 functions->DeleteTextureImage = st_DeleteTextureImage;
3211 functions->DeleteTexture = st_DeleteTextureObject;
3212 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
3213 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
3214 functions->MapTextureImage = st_MapTextureImage;
3215 functions->UnmapTextureImage = st_UnmapTextureImage;
3216
3217 /* XXX Temporary until we can query pipe's texture sizes */
3218 functions->TestProxyTexImage = st_TestProxyTexImage;
3219
3220 functions->AllocTextureStorage = st_AllocTextureStorage;
3221 functions->TextureView = st_TextureView;
3222 functions->ClearTexSubImage = st_ClearTexSubImage;
3223
3224 functions->TexParameter = st_TexParameter;
3225
3226 /* bindless functions */
3227 functions->NewTextureHandle = st_NewTextureHandle;
3228 functions->DeleteTextureHandle = st_DeleteTextureHandle;
3229 functions->MakeTextureHandleResident = st_MakeTextureHandleResident;
3230 functions->NewImageHandle = st_NewImageHandle;
3231 functions->DeleteImageHandle = st_DeleteImageHandle;
3232 functions->MakeImageHandleResident = st_MakeImageHandleResident;
3233
3234 /* external object functions */
3235 functions->SetTextureStorageForMemoryObject = st_SetTextureStorageForMemoryObject;
3236 }