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