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