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