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