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