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