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