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