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