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