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