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