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