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