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