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