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