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