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