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