util: Move gallium's PIPE_FORMAT utils to /util/format/
[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 dst_glformat = GL_RGBA8;
2050 break;
2051 case PIPE_FORMAT_RGTC1_SNORM:
2052 case PIPE_FORMAT_RGTC2_SNORM:
2053 if (!ctx->Extensions.EXT_texture_snorm)
2054 goto fallback;
2055 dst_glformat = GL_RGBA8_SNORM;
2056 break;
2057 case PIPE_FORMAT_BPTC_RGB_FLOAT:
2058 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
2059 if (!ctx->Extensions.ARB_texture_float)
2060 goto fallback;
2061 dst_glformat = GL_RGBA32F;
2062 break;
2063 case PIPE_FORMAT_ETC2_R11_UNORM:
2064 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16_UNORM,
2065 pipe_target, 0, 0, bind))
2066 goto fallback;
2067 dst_glformat = GL_R16;
2068 break;
2069 case PIPE_FORMAT_ETC2_R11_SNORM:
2070 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16_SNORM,
2071 pipe_target, 0, 0, bind))
2072 goto fallback;
2073 dst_glformat = GL_R16_SNORM;
2074 break;
2075 case PIPE_FORMAT_ETC2_RG11_UNORM:
2076 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16G16_UNORM,
2077 pipe_target, 0, 0, bind))
2078 goto fallback;
2079 dst_glformat = GL_RG16;
2080 break;
2081 case PIPE_FORMAT_ETC2_RG11_SNORM:
2082 if (!screen->is_format_supported(screen, PIPE_FORMAT_R16G16_SNORM,
2083 pipe_target, 0, 0, bind))
2084 goto fallback;
2085 dst_glformat = GL_RG16_SNORM;
2086 break;
2087 default:
2088 assert(0);
2089 goto fallback;
2090 }
2091
2092 dst_format = st_choose_format(st, dst_glformat, format, type,
2093 pipe_target, 0, 0, bind, FALSE);
2094
2095 if (dst_format == PIPE_FORMAT_NONE) {
2096 /* unable to get an rgba format!?! */
2097 goto fallback;
2098 }
2099 }
2100
2101 /* create the destination texture of size (width X height X depth) */
2102 memset(&dst_templ, 0, sizeof(dst_templ));
2103 dst_templ.target = pipe_target;
2104 dst_templ.format = dst_format;
2105 dst_templ.bind = bind;
2106 dst_templ.usage = PIPE_USAGE_STAGING;
2107
2108 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
2109 &dst_templ.width0, &dst_templ.height0,
2110 &dst_templ.depth0, &dst_templ.array_size);
2111
2112 dst = screen->resource_create(screen, &dst_templ);
2113 if (!dst) {
2114 goto fallback;
2115 }
2116
2117 /* From now on, we need the gallium representation of dimensions. */
2118 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2119 zoffset = yoffset;
2120 yoffset = 0;
2121 depth = height;
2122 height = 1;
2123 }
2124
2125 assert(texImage->Face == 0 ||
2126 texImage->TexObject->MinLayer == 0 ||
2127 zoffset == 0);
2128
2129 memset(&blit, 0, sizeof(blit));
2130 blit.src.resource = src;
2131 blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
2132 blit.src.format = src_format;
2133 blit.dst.resource = dst;
2134 blit.dst.level = 0;
2135 blit.dst.format = dst->format;
2136 blit.src.box.x = xoffset;
2137 blit.dst.box.x = 0;
2138 blit.src.box.y = yoffset;
2139 blit.dst.box.y = 0;
2140 blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
2141 blit.dst.box.z = 0;
2142 blit.src.box.width = blit.dst.box.width = width;
2143 blit.src.box.height = blit.dst.box.height = height;
2144 blit.src.box.depth = blit.dst.box.depth = depth;
2145 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
2146 blit.filter = PIPE_TEX_FILTER_NEAREST;
2147 blit.scissor_enable = FALSE;
2148
2149 /* blit/render/decompress */
2150 st->pipe->blit(st->pipe, &blit);
2151
2152 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
2153
2154 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
2155 0, 0, 0, width, height, depth, &tex_xfer);
2156 if (!map) {
2157 goto end;
2158 }
2159
2160 mesa_format = st_pipe_format_to_mesa_format(dst_format);
2161 dims = _mesa_get_texture_dimensions(gl_target);
2162
2163 /* copy/pack data into user buffer */
2164 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2165 ctx->Pack.SwapBytes, NULL)) {
2166 /* memcpy */
2167 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
2168 GLuint row, slice;
2169
2170 for (slice = 0; slice < depth; slice++) {
2171 ubyte *slice_map = map;
2172
2173 for (row = 0; row < height; row++) {
2174 void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
2175 width, height, format, type,
2176 slice, row, 0);
2177
2178 memcpy(dest, slice_map, bytesPerRow);
2179
2180 slice_map += tex_xfer->stride;
2181 }
2182
2183 map += tex_xfer->layer_stride;
2184 }
2185 }
2186 else {
2187 /* format translation via floats */
2188 GLuint slice;
2189 GLfloat *rgba;
2190 uint32_t dstMesaFormat;
2191 int dstStride, srcStride;
2192
2193 assert(util_format_is_compressed(src->format));
2194
2195 rgba = malloc(width * height * 4 * sizeof(GLfloat));
2196 if (!rgba) {
2197 goto end;
2198 }
2199
2200 if (ST_DEBUG & DEBUG_FALLBACK)
2201 debug_printf("%s: fallback format translation\n", __func__);
2202
2203 dstMesaFormat = _mesa_format_from_format_and_type(format, type);
2204 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
2205 srcStride = 4 * width * sizeof(GLfloat);
2206 for (slice = 0; slice < depth; slice++) {
2207 void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
2208 width, height, format, type,
2209 slice, 0, 0);
2210
2211 /* get float[4] rgba row from surface */
2212 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, height,
2213 dst_format, rgba);
2214
2215 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2216 rgba, RGBA32_FLOAT, srcStride,
2217 width, height, NULL);
2218
2219 /* Handle byte swapping if required */
2220 if (ctx->Pack.SwapBytes) {
2221 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
2222 width, height, dest, dest);
2223 }
2224
2225 map += tex_xfer->layer_stride;
2226 }
2227
2228 free(rgba);
2229 }
2230 done = TRUE;
2231
2232 end:
2233 if (map)
2234 pipe_transfer_unmap(pipe, tex_xfer);
2235
2236 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
2237 pipe_resource_reference(&dst, NULL);
2238
2239 fallback:
2240 if (!done) {
2241 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
2242 width, height, depth,
2243 format, type, pixels, texImage);
2244 }
2245 }
2246
2247
2248 /**
2249 * Do a CopyTexSubImage operation using a read transfer from the source,
2250 * a write transfer to the destination and get_tile()/put_tile() to access
2251 * the pixels/texels.
2252 *
2253 * Note: srcY=0=TOP of renderbuffer
2254 */
2255 static void
2256 fallback_copy_texsubimage(struct gl_context *ctx,
2257 struct st_renderbuffer *strb,
2258 struct st_texture_image *stImage,
2259 GLenum baseFormat,
2260 GLint destX, GLint destY, GLint slice,
2261 GLint srcX, GLint srcY,
2262 GLsizei width, GLsizei height)
2263 {
2264 struct st_context *st = st_context(ctx);
2265 struct pipe_context *pipe = st->pipe;
2266 struct pipe_transfer *src_trans;
2267 GLubyte *texDest;
2268 enum pipe_transfer_usage transfer_usage;
2269 void *map;
2270 unsigned dst_width = width;
2271 unsigned dst_height = height;
2272 unsigned dst_depth = 1;
2273 struct pipe_transfer *transfer;
2274
2275 if (ST_DEBUG & DEBUG_FALLBACK)
2276 debug_printf("%s: fallback processing\n", __func__);
2277
2278 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2279 srcY = strb->Base.Height - srcY - height;
2280 }
2281
2282 map = pipe_transfer_map(pipe,
2283 strb->texture,
2284 strb->surface->u.tex.level,
2285 strb->surface->u.tex.first_layer,
2286 PIPE_TRANSFER_READ,
2287 srcX, srcY,
2288 width, height, &src_trans);
2289
2290 if ((baseFormat == GL_DEPTH_COMPONENT ||
2291 baseFormat == GL_DEPTH_STENCIL) &&
2292 util_format_is_depth_and_stencil(stImage->pt->format))
2293 transfer_usage = PIPE_TRANSFER_READ_WRITE;
2294 else
2295 transfer_usage = PIPE_TRANSFER_WRITE;
2296
2297 texDest = st_texture_image_map(st, stImage, transfer_usage,
2298 destX, destY, slice,
2299 dst_width, dst_height, dst_depth,
2300 &transfer);
2301
2302 if (baseFormat == GL_DEPTH_COMPONENT ||
2303 baseFormat == GL_DEPTH_STENCIL) {
2304 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
2305 ctx->Pixel.DepthBias != 0.0F);
2306 GLint row, yStep;
2307 uint *data;
2308
2309 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2310 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2311 srcY = height - 1;
2312 yStep = -1;
2313 }
2314 else {
2315 srcY = 0;
2316 yStep = 1;
2317 }
2318
2319 data = malloc(width * sizeof(uint));
2320
2321 if (data) {
2322 /* To avoid a large temp memory allocation, do copy row by row */
2323 for (row = 0; row < height; row++, srcY += yStep) {
2324 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
2325 if (scaleOrBias) {
2326 _mesa_scale_and_bias_depth_uint(ctx, width, data);
2327 }
2328
2329 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2330 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
2331 0, 0, width, 1, data);
2332 }
2333 else {
2334 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
2335 }
2336 }
2337 }
2338 else {
2339 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
2340 }
2341
2342 free(data);
2343 }
2344 else {
2345 /* RGBA format */
2346 GLfloat *tempSrc =
2347 malloc(width * height * 4 * sizeof(GLfloat));
2348
2349 if (tempSrc && texDest) {
2350 const GLint dims = 2;
2351 GLint dstRowStride;
2352 struct gl_texture_image *texImage = &stImage->base;
2353 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
2354
2355 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2356 unpack.Invert = GL_TRUE;
2357 }
2358
2359 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2360 dstRowStride = transfer->layer_stride;
2361 }
2362 else {
2363 dstRowStride = transfer->stride;
2364 }
2365
2366 /* get float/RGBA image from framebuffer */
2367 /* XXX this usually involves a lot of int/float conversion.
2368 * try to avoid that someday.
2369 */
2370 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
2371 util_format_linear(strb->texture->format),
2372 tempSrc);
2373
2374 /* Store into texture memory.
2375 * Note that this does some special things such as pixel transfer
2376 * ops and format conversion. In particular, if the dest tex format
2377 * is actually RGBA but the user created the texture as GL_RGB we
2378 * need to fill-in/override the alpha channel with 1.0.
2379 */
2380 _mesa_texstore(ctx, dims,
2381 texImage->_BaseFormat,
2382 texImage->TexFormat,
2383 dstRowStride,
2384 &texDest,
2385 width, height, 1,
2386 GL_RGBA, GL_FLOAT, tempSrc, /* src */
2387 &unpack);
2388 }
2389 else {
2390 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
2391 }
2392
2393 free(tempSrc);
2394 }
2395
2396 st_texture_image_unmap(st, stImage, slice);
2397 pipe->transfer_unmap(pipe, src_trans);
2398 }
2399
2400
2401 static bool
2402 st_can_copyteximage_using_blit(const struct gl_texture_image *texImage,
2403 const struct gl_renderbuffer *rb)
2404 {
2405 GLenum tex_baseformat = _mesa_get_format_base_format(texImage->TexFormat);
2406
2407 /* We don't blit to a teximage where the GL base format doesn't match the
2408 * texture's chosen format, except in the case of a GL_RGB texture
2409 * represented with GL_RGBA (where the alpha channel is just being
2410 * dropped).
2411 */
2412 if (texImage->_BaseFormat != tex_baseformat &&
2413 ((texImage->_BaseFormat != GL_RGB || tex_baseformat != GL_RGBA))) {
2414 return false;
2415 }
2416
2417 /* We can't blit from a RB where the GL base format doesn't match the RB's
2418 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2419 * type, because the other channels will be undefined).
2420 */
2421 if (rb->_BaseFormat != _mesa_get_format_base_format(rb->Format))
2422 return false;
2423
2424 return true;
2425 }
2426
2427
2428 /**
2429 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2430 * Note that the region to copy has already been clipped so we know we
2431 * won't read from outside the source renderbuffer's bounds.
2432 *
2433 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2434 */
2435 static void
2436 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2437 struct gl_texture_image *texImage,
2438 GLint destX, GLint destY, GLint slice,
2439 struct gl_renderbuffer *rb,
2440 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
2441 {
2442 struct st_texture_image *stImage = st_texture_image(texImage);
2443 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2444 struct st_renderbuffer *strb = st_renderbuffer(rb);
2445 struct st_context *st = st_context(ctx);
2446 struct pipe_context *pipe = st->pipe;
2447 struct pipe_screen *screen = pipe->screen;
2448 struct pipe_blit_info blit;
2449 enum pipe_format dst_format;
2450 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
2451 unsigned bind;
2452 GLint srcY0, srcY1;
2453
2454 st_flush_bitmap_cache(st);
2455 st_invalidate_readpix_cache(st);
2456
2457 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2458 !_mesa_is_format_astc_2d(texImage->TexFormat) &&
2459 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2460
2461 if (!strb || !strb->surface || !stImage->pt) {
2462 debug_printf("%s: null strb or stImage\n", __func__);
2463 return;
2464 }
2465
2466 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
2467 texImage->TexFormat)) {
2468 goto fallback;
2469 }
2470
2471 if (!st_can_copyteximage_using_blit(texImage, rb)) {
2472 goto fallback;
2473 }
2474
2475 /* Choose the destination format to match the TexImage behavior. */
2476 dst_format = util_format_linear(stImage->pt->format);
2477 dst_format = util_format_luminance_to_red(dst_format);
2478 dst_format = util_format_intensity_to_red(dst_format);
2479
2480 /* See if the destination format is supported. */
2481 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
2482 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
2483 bind = PIPE_BIND_DEPTH_STENCIL;
2484 }
2485 else {
2486 bind = PIPE_BIND_RENDER_TARGET;
2487 }
2488
2489 if (!dst_format ||
2490 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
2491 stImage->pt->nr_samples,
2492 stImage->pt->nr_storage_samples, bind)) {
2493 goto fallback;
2494 }
2495
2496 /* Y flipping for the main framebuffer. */
2497 if (do_flip) {
2498 srcY1 = strb->Base.Height - srcY - height;
2499 srcY0 = srcY1 + height;
2500 }
2501 else {
2502 srcY0 = srcY;
2503 srcY1 = srcY0 + height;
2504 }
2505
2506 /* Blit the texture.
2507 * This supports flipping, format conversions, and downsampling.
2508 */
2509 memset(&blit, 0, sizeof(blit));
2510 blit.src.resource = strb->texture;
2511 blit.src.format = util_format_linear(strb->surface->format);
2512 blit.src.level = strb->surface->u.tex.level;
2513 blit.src.box.x = srcX;
2514 blit.src.box.y = srcY0;
2515 blit.src.box.z = strb->surface->u.tex.first_layer;
2516 blit.src.box.width = width;
2517 blit.src.box.height = srcY1 - srcY0;
2518 blit.src.box.depth = 1;
2519 blit.dst.resource = stImage->pt;
2520 blit.dst.format = dst_format;
2521 blit.dst.level = stObj->pt != stImage->pt
2522 ? 0 : texImage->Level + texImage->TexObject->MinLevel;
2523 blit.dst.box.x = destX;
2524 blit.dst.box.y = destY;
2525 blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
2526 blit.dst.box.width = width;
2527 blit.dst.box.height = height;
2528 blit.dst.box.depth = 1;
2529 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
2530 blit.filter = PIPE_TEX_FILTER_NEAREST;
2531 pipe->blit(pipe, &blit);
2532 return;
2533
2534 fallback:
2535 /* software fallback */
2536 fallback_copy_texsubimage(ctx,
2537 strb, stImage, texImage->_BaseFormat,
2538 destX, destY, slice,
2539 srcX, srcY, width, height);
2540 }
2541
2542
2543 /**
2544 * Copy image data from stImage into the texture object 'stObj' at level
2545 * 'dstLevel'.
2546 */
2547 static void
2548 copy_image_data_to_texture(struct st_context *st,
2549 struct st_texture_object *stObj,
2550 GLuint dstLevel,
2551 struct st_texture_image *stImage)
2552 {
2553 /* debug checks */
2554 {
2555 ASSERTED const struct gl_texture_image *dstImage =
2556 stObj->base.Image[stImage->base.Face][dstLevel];
2557 assert(dstImage);
2558 assert(dstImage->Width == stImage->base.Width);
2559 assert(dstImage->Height == stImage->base.Height);
2560 assert(dstImage->Depth == stImage->base.Depth);
2561 }
2562
2563 if (stImage->pt) {
2564 /* Copy potentially with the blitter:
2565 */
2566 GLuint src_level;
2567 if (stImage->pt->last_level == 0)
2568 src_level = 0;
2569 else
2570 src_level = stImage->base.Level;
2571
2572 assert(src_level <= stImage->pt->last_level);
2573 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
2574 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
2575 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
2576 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
2577 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
2578 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
2579
2580 st_texture_image_copy(st->pipe,
2581 stObj->pt, dstLevel, /* dest texture, level */
2582 stImage->pt, src_level, /* src texture, level */
2583 stImage->base.Face);
2584
2585 pipe_resource_reference(&stImage->pt, NULL);
2586 }
2587 pipe_resource_reference(&stImage->pt, stObj->pt);
2588 }
2589
2590
2591 /**
2592 * Called during state validation. When this function is finished,
2593 * the texture object should be ready for rendering.
2594 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2595 */
2596 GLboolean
2597 st_finalize_texture(struct gl_context *ctx,
2598 struct pipe_context *pipe,
2599 struct gl_texture_object *tObj,
2600 GLuint cubeMapFace)
2601 {
2602 struct st_context *st = st_context(ctx);
2603 struct st_texture_object *stObj = st_texture_object(tObj);
2604 const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
2605 GLuint face;
2606 const struct st_texture_image *firstImage;
2607 enum pipe_format firstImageFormat;
2608 unsigned ptWidth;
2609 uint16_t ptHeight, ptDepth, ptLayers, ptNumSamples;
2610
2611 if (tObj->Immutable)
2612 return GL_TRUE;
2613
2614 if (tObj->_MipmapComplete)
2615 stObj->lastLevel = stObj->base._MaxLevel;
2616 else if (tObj->_BaseComplete)
2617 stObj->lastLevel = stObj->base.BaseLevel;
2618
2619 /* Skip the loop over images in the common case of no images having
2620 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2621 * haven't looked at, then we do need to look at those new images.
2622 */
2623 if (!stObj->needs_validation &&
2624 stObj->base.BaseLevel >= stObj->validated_first_level &&
2625 stObj->lastLevel <= stObj->validated_last_level) {
2626 return GL_TRUE;
2627 }
2628
2629 /* If this texture comes from a window system, there is nothing else to do. */
2630 if (stObj->surface_based) {
2631 return GL_TRUE;
2632 }
2633
2634 firstImage = st_texture_image_const(stObj->base.Image[cubeMapFace]
2635 [stObj->base.BaseLevel]);
2636 assert(firstImage);
2637
2638 /* If both firstImage and stObj point to a texture which can contain
2639 * all active images, favour firstImage. Note that because of the
2640 * completeness requirement, we know that the image dimensions
2641 * will match.
2642 */
2643 if (firstImage->pt &&
2644 firstImage->pt != stObj->pt &&
2645 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
2646 pipe_resource_reference(&stObj->pt, firstImage->pt);
2647 st_texture_release_all_sampler_views(st, stObj);
2648 }
2649
2650 /* Find gallium format for the Mesa texture */
2651 firstImageFormat =
2652 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
2653
2654 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2655 {
2656 unsigned width;
2657 uint16_t height, depth;
2658
2659 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
2660 firstImage->base.Width2,
2661 firstImage->base.Height2,
2662 firstImage->base.Depth2,
2663 &width, &height, &depth, &ptLayers);
2664
2665 /* If we previously allocated a pipe texture and its sizes are
2666 * compatible, use them.
2667 */
2668 if (stObj->pt &&
2669 u_minify(stObj->pt->width0, firstImage->base.Level) == width &&
2670 u_minify(stObj->pt->height0, firstImage->base.Level) == height &&
2671 u_minify(stObj->pt->depth0, firstImage->base.Level) == depth) {
2672 ptWidth = stObj->pt->width0;
2673 ptHeight = stObj->pt->height0;
2674 ptDepth = stObj->pt->depth0;
2675 } else {
2676 /* Otherwise, compute a new level=0 size that is compatible with the
2677 * base level image.
2678 */
2679 ptWidth = width > 1 ? width << firstImage->base.Level : 1;
2680 ptHeight = height > 1 ? height << firstImage->base.Level : 1;
2681 ptDepth = depth > 1 ? depth << firstImage->base.Level : 1;
2682
2683 /* If the base level image is 1x1x1, we still need to ensure that the
2684 * resulting pipe texture ends up with the required number of levels
2685 * in total.
2686 */
2687 if (ptWidth == 1 && ptHeight == 1 && ptDepth == 1) {
2688 ptWidth <<= firstImage->base.Level;
2689
2690 if (stObj->base.Target == GL_TEXTURE_CUBE_MAP ||
2691 stObj->base.Target == GL_TEXTURE_CUBE_MAP_ARRAY)
2692 ptHeight = ptWidth;
2693 }
2694
2695 /* At this point, the texture may be incomplete (mismatched cube
2696 * face sizes, for example). If that's the case, give up, but
2697 * don't return GL_FALSE as that would raise an incorrect
2698 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2699 */
2700 if (!stObj->base._BaseComplete) {
2701 _mesa_test_texobj_completeness(ctx, &stObj->base);
2702 if (!stObj->base._BaseComplete) {
2703 return TRUE;
2704 }
2705 }
2706 }
2707
2708 ptNumSamples = firstImage->base.NumSamples;
2709 }
2710
2711 /* If we already have a gallium texture, check that it matches the texture
2712 * object's format, target, size, num_levels, etc.
2713 */
2714 if (stObj->pt) {
2715 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
2716 stObj->pt->format != firstImageFormat ||
2717 stObj->pt->last_level < stObj->lastLevel ||
2718 stObj->pt->width0 != ptWidth ||
2719 stObj->pt->height0 != ptHeight ||
2720 stObj->pt->depth0 != ptDepth ||
2721 stObj->pt->nr_samples != ptNumSamples ||
2722 stObj->pt->array_size != ptLayers)
2723 {
2724 /* The gallium texture does not match the Mesa texture so delete the
2725 * gallium texture now. We'll make a new one below.
2726 */
2727 pipe_resource_reference(&stObj->pt, NULL);
2728 st_texture_release_all_sampler_views(st, stObj);
2729 st->dirty |= ST_NEW_FRAMEBUFFER;
2730 }
2731 }
2732
2733 /* May need to create a new gallium texture:
2734 */
2735 if (!stObj->pt) {
2736 GLuint bindings = default_bindings(st, firstImageFormat);
2737
2738 stObj->pt = st_texture_create(st,
2739 gl_target_to_pipe(stObj->base.Target),
2740 firstImageFormat,
2741 stObj->lastLevel,
2742 ptWidth,
2743 ptHeight,
2744 ptDepth,
2745 ptLayers, ptNumSamples,
2746 bindings);
2747
2748 if (!stObj->pt) {
2749 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
2750 return GL_FALSE;
2751 }
2752 }
2753
2754 /* Pull in any images not in the object's texture:
2755 */
2756 for (face = 0; face < nr_faces; face++) {
2757 GLuint level;
2758 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
2759 struct st_texture_image *stImage =
2760 st_texture_image(stObj->base.Image[face][level]);
2761
2762 /* Need to import images in main memory or held in other textures.
2763 */
2764 if (stImage && stObj->pt != stImage->pt) {
2765 GLuint height;
2766 GLuint depth;
2767
2768 if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
2769 height = u_minify(ptHeight, level);
2770 else
2771 height = ptLayers;
2772
2773 if (stObj->base.Target == GL_TEXTURE_3D)
2774 depth = u_minify(ptDepth, level);
2775 else if (stObj->base.Target == GL_TEXTURE_CUBE_MAP)
2776 depth = 1;
2777 else
2778 depth = ptLayers;
2779
2780 if (level == 0 ||
2781 (stImage->base.Width == u_minify(ptWidth, level) &&
2782 stImage->base.Height == height &&
2783 stImage->base.Depth == depth)) {
2784 /* src image fits expected dest mipmap level size */
2785 copy_image_data_to_texture(st, stObj, level, stImage);
2786 }
2787 }
2788 }
2789 }
2790
2791 stObj->validated_first_level = stObj->base.BaseLevel;
2792 stObj->validated_last_level = stObj->lastLevel;
2793 stObj->needs_validation = false;
2794
2795 return GL_TRUE;
2796 }
2797
2798
2799 /**
2800 * Allocate a new pipe_resource object
2801 * width0, height0, depth0 are the dimensions of the level 0 image
2802 * (the highest resolution). last_level indicates how many mipmap levels
2803 * to allocate storage for. For non-mipmapped textures, this will be zero.
2804 */
2805 static struct pipe_resource *
2806 st_texture_create_from_memory(struct st_context *st,
2807 struct st_memory_object *memObj,
2808 GLuint64 offset,
2809 enum pipe_texture_target target,
2810 enum pipe_format format,
2811 GLuint last_level,
2812 GLuint width0,
2813 GLuint height0,
2814 GLuint depth0,
2815 GLuint layers,
2816 GLuint nr_samples,
2817 GLuint bind)
2818 {
2819 struct pipe_resource pt, *newtex;
2820 struct pipe_screen *screen = st->pipe->screen;
2821
2822 assert(target < PIPE_MAX_TEXTURE_TYPES);
2823 assert(width0 > 0);
2824 assert(height0 > 0);
2825 assert(depth0 > 0);
2826 if (target == PIPE_TEXTURE_CUBE)
2827 assert(layers == 6);
2828
2829 DBG("%s target %d format %s last_level %d\n", __func__,
2830 (int) target, util_format_name(format), last_level);
2831
2832 assert(format);
2833 assert(screen->is_format_supported(screen, format, target, 0, 0,
2834 PIPE_BIND_SAMPLER_VIEW));
2835
2836 memset(&pt, 0, sizeof(pt));
2837 pt.target = target;
2838 pt.format = format;
2839 pt.last_level = last_level;
2840 pt.width0 = width0;
2841 pt.height0 = height0;
2842 pt.depth0 = depth0;
2843 pt.array_size = layers;
2844 pt.usage = PIPE_USAGE_DEFAULT;
2845 pt.bind = bind;
2846 /* only set this for OpenGL textures, not renderbuffers */
2847 pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
2848 pt.nr_samples = nr_samples;
2849 pt.nr_storage_samples = nr_samples;
2850
2851 newtex = screen->resource_from_memobj(screen, &pt, memObj->memory, offset);
2852
2853 assert(!newtex || pipe_is_referenced(&newtex->reference));
2854
2855 return newtex;
2856 }
2857
2858
2859 /**
2860 * Allocate texture memory for a whole mipmap stack.
2861 * Note: for multisample textures if the requested sample count is not
2862 * supported, we search for the next higher supported sample count.
2863 */
2864 static GLboolean
2865 st_texture_storage(struct gl_context *ctx,
2866 struct gl_texture_object *texObj,
2867 GLsizei levels, GLsizei width,
2868 GLsizei height, GLsizei depth,
2869 struct gl_memory_object *memObj,
2870 GLuint64 offset)
2871 {
2872 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
2873 struct gl_texture_image *texImage = texObj->Image[0][0];
2874 struct st_context *st = st_context(ctx);
2875 struct st_texture_object *stObj = st_texture_object(texObj);
2876 struct st_memory_object *smObj = st_memory_object(memObj);
2877 struct pipe_screen *screen = st->pipe->screen;
2878 unsigned ptWidth, bindings;
2879 uint16_t ptHeight, ptDepth, ptLayers;
2880 enum pipe_format fmt;
2881 GLint level;
2882 GLuint num_samples = texImage->NumSamples;
2883
2884 assert(levels > 0);
2885
2886 stObj->lastLevel = levels - 1;
2887
2888 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
2889
2890 bindings = default_bindings(st, fmt);
2891
2892 if (num_samples > 0) {
2893 /* Find msaa sample count which is actually supported. For example,
2894 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2895 * choose 4x here.
2896 */
2897 enum pipe_texture_target ptarget = gl_target_to_pipe(texObj->Target);
2898 boolean found = FALSE;
2899
2900 if (ctx->Const.MaxSamples > 1 && num_samples == 1) {
2901 /* don't try num_samples = 1 with drivers that support real msaa */
2902 num_samples = 2;
2903 }
2904
2905 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
2906 if (screen->is_format_supported(screen, fmt, ptarget,
2907 num_samples, num_samples,
2908 PIPE_BIND_SAMPLER_VIEW)) {
2909 /* Update the sample count in gl_texture_image as well. */
2910 texImage->NumSamples = num_samples;
2911 found = TRUE;
2912 break;
2913 }
2914 }
2915
2916 if (!found) {
2917 return GL_FALSE;
2918 }
2919 }
2920
2921 st_gl_texture_dims_to_pipe_dims(texObj->Target,
2922 width, height, depth,
2923 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2924
2925 if (smObj) {
2926 stObj->pt = st_texture_create_from_memory(st,
2927 smObj,
2928 offset,
2929 gl_target_to_pipe(texObj->Target),
2930 fmt,
2931 levels - 1,
2932 ptWidth,
2933 ptHeight,
2934 ptDepth,
2935 ptLayers, num_samples,
2936 bindings);
2937 }
2938 else {
2939 stObj->pt = st_texture_create(st,
2940 gl_target_to_pipe(texObj->Target),
2941 fmt,
2942 levels - 1,
2943 ptWidth,
2944 ptHeight,
2945 ptDepth,
2946 ptLayers, num_samples,
2947 bindings);
2948 }
2949
2950 if (!stObj->pt)
2951 return GL_FALSE;
2952
2953 /* Set image resource pointers */
2954 for (level = 0; level < levels; level++) {
2955 GLuint face;
2956 for (face = 0; face < numFaces; face++) {
2957 struct st_texture_image *stImage =
2958 st_texture_image(texObj->Image[face][level]);
2959 pipe_resource_reference(&stImage->pt, stObj->pt);
2960
2961 compressed_tex_fallback_allocate(st, stImage);
2962 }
2963 }
2964
2965 /* The texture is in a validated state, so no need to check later. */
2966 stObj->needs_validation = false;
2967 stObj->validated_first_level = 0;
2968 stObj->validated_last_level = levels - 1;
2969
2970 return GL_TRUE;
2971 }
2972
2973 /**
2974 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2975 * for a whole mipmap stack.
2976 */
2977 static GLboolean
2978 st_AllocTextureStorage(struct gl_context *ctx,
2979 struct gl_texture_object *texObj,
2980 GLsizei levels, GLsizei width,
2981 GLsizei height, GLsizei depth)
2982 {
2983 return st_texture_storage(ctx, texObj, levels,
2984 width, height, depth,
2985 NULL, 0);
2986 }
2987
2988
2989 static GLboolean
2990 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
2991 GLuint numLevels, GLint level,
2992 mesa_format format, GLuint numSamples,
2993 GLint width, GLint height, GLint depth)
2994 {
2995 struct st_context *st = st_context(ctx);
2996 struct pipe_context *pipe = st->pipe;
2997
2998 if (width == 0 || height == 0 || depth == 0) {
2999 /* zero-sized images are legal, and always fit! */
3000 return GL_TRUE;
3001 }
3002
3003 if (pipe->screen->can_create_resource) {
3004 /* Ask the gallium driver if the texture is too large */
3005 struct gl_texture_object *texObj =
3006 _mesa_get_current_tex_object(ctx, target);
3007 struct pipe_resource pt;
3008
3009 /* Setup the pipe_resource object
3010 */
3011 memset(&pt, 0, sizeof(pt));
3012
3013 pt.target = gl_target_to_pipe(target);
3014 pt.format = st_mesa_format_to_pipe_format(st, format);
3015 pt.nr_samples = numSamples;
3016 pt.nr_storage_samples = numSamples;
3017
3018 st_gl_texture_dims_to_pipe_dims(target,
3019 width, height, depth,
3020 &pt.width0, &pt.height0,
3021 &pt.depth0, &pt.array_size);
3022
3023 if (numLevels > 0) {
3024 /* For immutable textures we know the final number of mip levels */
3025 pt.last_level = numLevels - 1;
3026 }
3027 else if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
3028 texObj->Sampler.MinFilter == GL_NEAREST)) {
3029 /* assume just one mipmap level */
3030 pt.last_level = 0;
3031 }
3032 else {
3033 /* assume a full set of mipmaps */
3034 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
3035 }
3036
3037 return pipe->screen->can_create_resource(pipe->screen, &pt);
3038 }
3039 else {
3040 /* Use core Mesa fallback */
3041 return _mesa_test_proxy_teximage(ctx, target, numLevels, level, format,
3042 numSamples, width, height, depth);
3043 }
3044 }
3045
3046 static GLboolean
3047 st_TextureView(struct gl_context *ctx,
3048 struct gl_texture_object *texObj,
3049 struct gl_texture_object *origTexObj)
3050 {
3051 struct st_context *st = st_context(ctx);
3052 struct st_texture_object *orig = st_texture_object(origTexObj);
3053 struct st_texture_object *tex = st_texture_object(texObj);
3054 struct gl_texture_image *image = texObj->Image[0][0];
3055
3056 const int numFaces = _mesa_num_tex_faces(texObj->Target);
3057 const int numLevels = texObj->NumLevels;
3058
3059 int face;
3060 int level;
3061
3062 pipe_resource_reference(&tex->pt, orig->pt);
3063
3064 /* Set image resource pointers */
3065 for (level = 0; level < numLevels; level++) {
3066 for (face = 0; face < numFaces; face++) {
3067 struct st_texture_image *stImage =
3068 st_texture_image(texObj->Image[face][level]);
3069 pipe_resource_reference(&stImage->pt, tex->pt);
3070 }
3071 }
3072
3073 tex->surface_based = GL_TRUE;
3074 tex->surface_format =
3075 st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
3076
3077 tex->lastLevel = numLevels - 1;
3078
3079 /* free texture sampler views. They need to be recreated when we
3080 * change the texture view parameters.
3081 */
3082 st_texture_release_all_sampler_views(st, tex);
3083
3084 /* The texture is in a validated state, so no need to check later. */
3085 tex->needs_validation = false;
3086 tex->validated_first_level = 0;
3087 tex->validated_last_level = numLevels - 1;
3088
3089 return GL_TRUE;
3090 }
3091
3092
3093 /**
3094 * Find the mipmap level in 'pt' which matches the level described by
3095 * 'texImage'.
3096 */
3097 static unsigned
3098 find_mipmap_level(const struct gl_texture_image *texImage,
3099 const struct pipe_resource *pt)
3100 {
3101 const GLenum target = texImage->TexObject->Target;
3102 GLint texWidth = texImage->Width;
3103 GLint texHeight = texImage->Height;
3104 GLint texDepth = texImage->Depth;
3105 unsigned level, w;
3106 uint16_t h, d, layers;
3107
3108 st_gl_texture_dims_to_pipe_dims(target, texWidth, texHeight, texDepth,
3109 &w, &h, &d, &layers);
3110
3111 for (level = 0; level <= pt->last_level; level++) {
3112 if (u_minify(pt->width0, level) == w &&
3113 u_minify(pt->height0, level) == h &&
3114 u_minify(pt->depth0, level) == d) {
3115 return level;
3116 }
3117 }
3118
3119 /* If we get here, there must be some sort of inconsistency between
3120 * the Mesa texture object/images and the gallium resource.
3121 */
3122 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3123
3124 return texImage->Level;
3125 }
3126
3127
3128 static void
3129 st_ClearTexSubImage(struct gl_context *ctx,
3130 struct gl_texture_image *texImage,
3131 GLint xoffset, GLint yoffset, GLint zoffset,
3132 GLsizei width, GLsizei height, GLsizei depth,
3133 const void *clearValue)
3134 {
3135 static const char zeros[16] = {0};
3136 struct gl_texture_object *texObj = texImage->TexObject;
3137 struct st_texture_image *stImage = st_texture_image(texImage);
3138 struct pipe_resource *pt = stImage->pt;
3139 struct st_context *st = st_context(ctx);
3140 struct pipe_context *pipe = st->pipe;
3141 unsigned level;
3142 struct pipe_box box;
3143
3144 if (!pt)
3145 return;
3146
3147 st_flush_bitmap_cache(st);
3148 st_invalidate_readpix_cache(st);
3149
3150 u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
3151 width, height, depth, &box);
3152 if (texObj->Immutable) {
3153 /* The texture object has to be consistent (no "loose", per-image
3154 * gallium resources). If this texture is a view into another
3155 * texture, we have to apply the MinLevel/Layer offsets. If this is
3156 * not a texture view, the offsets will be zero.
3157 */
3158 assert(stImage->pt == st_texture_object(texObj)->pt);
3159 level = texImage->Level + texObj->MinLevel;
3160 box.z += texObj->MinLayer;
3161 }
3162 else {
3163 /* Texture level sizes may be inconsistent. We my have "loose",
3164 * per-image gallium resources. The texImage->Level may not match
3165 * the gallium resource texture level.
3166 */
3167 level = find_mipmap_level(texImage, pt);
3168 }
3169
3170 assert(level <= pt->last_level);
3171
3172 pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
3173 }
3174
3175
3176 /**
3177 * Called via the glTexParam*() function, but only when some texture object
3178 * state has actually changed.
3179 */
3180 static void
3181 st_TexParameter(struct gl_context *ctx,
3182 struct gl_texture_object *texObj, GLenum pname)
3183 {
3184 struct st_context *st = st_context(ctx);
3185 struct st_texture_object *stObj = st_texture_object(texObj);
3186
3187 switch (pname) {
3188 case GL_TEXTURE_BASE_LEVEL:
3189 case GL_TEXTURE_MAX_LEVEL:
3190 case GL_DEPTH_TEXTURE_MODE:
3191 case GL_DEPTH_STENCIL_TEXTURE_MODE:
3192 case GL_TEXTURE_SRGB_DECODE_EXT:
3193 case GL_TEXTURE_SWIZZLE_R:
3194 case GL_TEXTURE_SWIZZLE_G:
3195 case GL_TEXTURE_SWIZZLE_B:
3196 case GL_TEXTURE_SWIZZLE_A:
3197 case GL_TEXTURE_SWIZZLE_RGBA:
3198 case GL_TEXTURE_BUFFER_SIZE:
3199 case GL_TEXTURE_BUFFER_OFFSET:
3200 /* changing any of these texture parameters means we must create
3201 * new sampler views.
3202 */
3203 st_texture_release_all_sampler_views(st, stObj);
3204 break;
3205 default:
3206 ; /* nothing */
3207 }
3208 }
3209
3210 static GLboolean
3211 st_SetTextureStorageForMemoryObject(struct gl_context *ctx,
3212 struct gl_texture_object *texObj,
3213 struct gl_memory_object *memObj,
3214 GLsizei levels, GLsizei width,
3215 GLsizei height, GLsizei depth,
3216 GLuint64 offset)
3217 {
3218 return st_texture_storage(ctx, texObj, levels,
3219 width, height, depth,
3220 memObj, offset);
3221 }
3222
3223 static GLuint64
3224 st_NewTextureHandle(struct gl_context *ctx, struct gl_texture_object *texObj,
3225 struct gl_sampler_object *sampObj)
3226 {
3227 struct st_context *st = st_context(ctx);
3228 struct st_texture_object *stObj = st_texture_object(texObj);
3229 struct pipe_context *pipe = st->pipe;
3230 struct pipe_sampler_view *view;
3231 struct pipe_sampler_state sampler = {0};
3232
3233 if (texObj->Target != GL_TEXTURE_BUFFER) {
3234 if (!st_finalize_texture(ctx, pipe, texObj, 0))
3235 return 0;
3236
3237 st_convert_sampler(st, texObj, sampObj, 0, &sampler);
3238
3239 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3240 view = st_get_texture_sampler_view_from_stobj(st, stObj, sampObj, 0, true);
3241 } else {
3242 view = st_get_buffer_sampler_view_from_stobj(st, stObj);
3243 }
3244
3245 return pipe->create_texture_handle(pipe, view, &sampler);
3246 }
3247
3248
3249 static void
3250 st_DeleteTextureHandle(struct gl_context *ctx, GLuint64 handle)
3251 {
3252 struct st_context *st = st_context(ctx);
3253 struct pipe_context *pipe = st->pipe;
3254
3255 pipe->delete_texture_handle(pipe, handle);
3256 }
3257
3258
3259 static void
3260 st_MakeTextureHandleResident(struct gl_context *ctx, GLuint64 handle,
3261 bool resident)
3262 {
3263 struct st_context *st = st_context(ctx);
3264 struct pipe_context *pipe = st->pipe;
3265
3266 pipe->make_texture_handle_resident(pipe, handle, resident);
3267 }
3268
3269
3270 static GLuint64
3271 st_NewImageHandle(struct gl_context *ctx, struct gl_image_unit *imgObj)
3272 {
3273 struct st_context *st = st_context(ctx);
3274 struct pipe_context *pipe = st->pipe;
3275 struct pipe_image_view image;
3276
3277 st_convert_image(st, imgObj, &image, GL_READ_WRITE);
3278
3279 return pipe->create_image_handle(pipe, &image);
3280 }
3281
3282
3283 static void
3284 st_DeleteImageHandle(struct gl_context *ctx, GLuint64 handle)
3285 {
3286 struct st_context *st = st_context(ctx);
3287 struct pipe_context *pipe = st->pipe;
3288
3289 pipe->delete_image_handle(pipe, handle);
3290 }
3291
3292
3293 static void
3294 st_MakeImageHandleResident(struct gl_context *ctx, GLuint64 handle,
3295 GLenum access, bool resident)
3296 {
3297 struct st_context *st = st_context(ctx);
3298 struct pipe_context *pipe = st->pipe;
3299
3300 pipe->make_image_handle_resident(pipe, handle, access, resident);
3301 }
3302
3303
3304 void
3305 st_init_texture_functions(struct dd_function_table *functions)
3306 {
3307 functions->ChooseTextureFormat = st_ChooseTextureFormat;
3308 functions->QueryInternalFormat = st_QueryInternalFormat;
3309 functions->TexImage = st_TexImage;
3310 functions->TexSubImage = st_TexSubImage;
3311 functions->CompressedTexSubImage = st_CompressedTexSubImage;
3312 functions->CopyTexSubImage = st_CopyTexSubImage;
3313 functions->GenerateMipmap = st_generate_mipmap;
3314
3315 functions->GetTexSubImage = st_GetTexSubImage;
3316
3317 /* compressed texture functions */
3318 functions->CompressedTexImage = st_CompressedTexImage;
3319
3320 functions->NewTextureObject = st_NewTextureObject;
3321 functions->NewTextureImage = st_NewTextureImage;
3322 functions->DeleteTextureImage = st_DeleteTextureImage;
3323 functions->DeleteTexture = st_DeleteTextureObject;
3324 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
3325 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
3326 functions->MapTextureImage = st_MapTextureImage;
3327 functions->UnmapTextureImage = st_UnmapTextureImage;
3328
3329 /* XXX Temporary until we can query pipe's texture sizes */
3330 functions->TestProxyTexImage = st_TestProxyTexImage;
3331
3332 functions->AllocTextureStorage = st_AllocTextureStorage;
3333 functions->TextureView = st_TextureView;
3334 functions->ClearTexSubImage = st_ClearTexSubImage;
3335
3336 functions->TexParameter = st_TexParameter;
3337
3338 /* bindless functions */
3339 functions->NewTextureHandle = st_NewTextureHandle;
3340 functions->DeleteTextureHandle = st_DeleteTextureHandle;
3341 functions->MakeTextureHandleResident = st_MakeTextureHandleResident;
3342 functions->NewImageHandle = st_NewImageHandle;
3343 functions->DeleteImageHandle = st_DeleteImageHandle;
3344 functions->MakeImageHandleResident = st_MakeImageHandleResident;
3345
3346 /* external object functions */
3347 functions->SetTextureStorageForMemoryObject = st_SetTextureStorageForMemoryObject;
3348 }