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