gallium/radeon: assign the highest priority to scratch; make rings second
[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.first_element = addr->first_element;
1167 templ.u.buf.last_element = addr->last_element;
1168 templ.swizzle_r = PIPE_SWIZZLE_X;
1169 templ.swizzle_g = PIPE_SWIZZLE_Y;
1170 templ.swizzle_b = PIPE_SWIZZLE_Z;
1171 templ.swizzle_a = PIPE_SWIZZLE_W;
1172
1173 sampler_view = pipe->create_sampler_view(pipe, addr->buffer, &templ);
1174 if (sampler_view == NULL)
1175 goto fail;
1176
1177 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
1178
1179 pipe_sampler_view_reference(&sampler_view, NULL);
1180
1181 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
1182 }
1183
1184 /* Framebuffer_state */
1185 {
1186 struct pipe_framebuffer_state fb;
1187 memset(&fb, 0, sizeof(fb));
1188 fb.width = surface->width;
1189 fb.height = surface->height;
1190 fb.nr_cbufs = 1;
1191 pipe_surface_reference(&fb.cbufs[0], surface);
1192
1193 cso_set_framebuffer(cso, &fb);
1194
1195 pipe_surface_reference(&fb.cbufs[0], NULL);
1196 }
1197
1198 cso_set_viewport_dims(cso, surface->width, surface->height, FALSE);
1199
1200 /* Blend state */
1201 cso_set_blend(cso, &st->pbo.upload_blend);
1202
1203 /* Depth/stencil/alpha state */
1204 {
1205 struct pipe_depth_stencil_alpha_state dsa;
1206 memset(&dsa, 0, sizeof(dsa));
1207 cso_set_depth_stencil_alpha(cso, &dsa);
1208 }
1209
1210 /* Set up the fragment shader */
1211 cso_set_fragment_shader_handle(cso, st->pbo.upload_fs);
1212
1213 success = st_pbo_draw(st, addr, surface->width, surface->height);
1214
1215 fail:
1216 cso_restore_state(cso);
1217 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1218
1219 return success;
1220 }
1221
1222 static bool
1223 try_pbo_upload(struct gl_context *ctx, GLuint dims,
1224 struct gl_texture_image *texImage,
1225 GLenum format, GLenum type,
1226 enum pipe_format dst_format,
1227 GLint xoffset, GLint yoffset, GLint zoffset,
1228 GLint width, GLint height, GLint depth,
1229 const void *pixels,
1230 const struct gl_pixelstore_attrib *unpack)
1231 {
1232 struct st_context *st = st_context(ctx);
1233 struct st_texture_image *stImage = st_texture_image(texImage);
1234 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1235 struct pipe_resource *texture = stImage->pt;
1236 struct pipe_context *pipe = st->pipe;
1237 struct pipe_screen *screen = pipe->screen;
1238 struct pipe_surface *surface = NULL;
1239 struct st_pbo_addresses addr;
1240 enum pipe_format src_format;
1241 const struct util_format_description *desc;
1242 GLenum gl_target = texImage->TexObject->Target;
1243 bool success;
1244
1245 if (!st->pbo.upload_enabled)
1246 return false;
1247
1248 /* From now on, we need the gallium representation of dimensions. */
1249 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1250 depth = height;
1251 height = 1;
1252 zoffset = yoffset;
1253 yoffset = 0;
1254 }
1255
1256 if (depth != 1 && !st->pbo.layers)
1257 return false;
1258
1259 /* Choose the source format. Initially, we do so without checking driver
1260 * support at all because of the remapping we later perform and because
1261 * at least the Radeon driver actually supports some formats for texture
1262 * buffers which it doesn't support for regular textures. */
1263 src_format = st_choose_matching_format(st, 0, format, type, unpack->SwapBytes);
1264 if (!src_format) {
1265 return false;
1266 }
1267
1268 src_format = util_format_linear(src_format);
1269 desc = util_format_description(src_format);
1270
1271 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1272 return false;
1273
1274 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
1275 return false;
1276
1277 if (st->pbo.rgba_only) {
1278 enum pipe_format orig_dst_format = dst_format;
1279
1280 if (!reinterpret_formats(&src_format, &dst_format)) {
1281 return false;
1282 }
1283
1284 if (dst_format != orig_dst_format &&
1285 !screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D, 0,
1286 PIPE_BIND_RENDER_TARGET)) {
1287 return false;
1288 }
1289 }
1290
1291 if (!src_format ||
1292 !screen->is_format_supported(screen, src_format, PIPE_BUFFER, 0,
1293 PIPE_BIND_SAMPLER_VIEW)) {
1294 return false;
1295 }
1296
1297 /* Compute buffer addresses */
1298 addr.xoffset = xoffset;
1299 addr.yoffset = yoffset;
1300 addr.width = width;
1301 addr.height = height;
1302 addr.depth = depth;
1303 addr.bytes_per_pixel = desc->block.bits / 8;
1304
1305 if (!st_pbo_addresses_pixelstore(st, gl_target, dims == 3, unpack, pixels,
1306 &addr))
1307 return false;
1308
1309 /* Set up the surface */
1310 {
1311 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1312 unsigned max_layer = util_max_layer(texture, level);
1313
1314 zoffset += texImage->Face + texImage->TexObject->MinLayer;
1315
1316 struct pipe_surface templ;
1317 memset(&templ, 0, sizeof(templ));
1318 templ.format = dst_format;
1319 templ.u.tex.level = level;
1320 templ.u.tex.first_layer = MIN2(zoffset, max_layer);
1321 templ.u.tex.last_layer = MIN2(zoffset + depth - 1, max_layer);
1322
1323 surface = pipe->create_surface(pipe, texture, &templ);
1324 if (!surface)
1325 return false;
1326 }
1327
1328 success = try_pbo_upload_common(ctx, surface, &addr, src_format);
1329
1330 pipe_surface_reference(&surface, NULL);
1331
1332 return success;
1333 }
1334
1335 static void
1336 st_TexSubImage(struct gl_context *ctx, GLuint dims,
1337 struct gl_texture_image *texImage,
1338 GLint xoffset, GLint yoffset, GLint zoffset,
1339 GLint width, GLint height, GLint depth,
1340 GLenum format, GLenum type, const void *pixels,
1341 const struct gl_pixelstore_attrib *unpack)
1342 {
1343 struct st_context *st = st_context(ctx);
1344 struct st_texture_image *stImage = st_texture_image(texImage);
1345 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1346 struct pipe_context *pipe = st->pipe;
1347 struct pipe_screen *screen = pipe->screen;
1348 struct pipe_resource *dst = stImage->pt;
1349 struct pipe_resource *src = NULL;
1350 struct pipe_resource src_templ;
1351 struct pipe_transfer *transfer;
1352 struct pipe_blit_info blit;
1353 enum pipe_format src_format, dst_format;
1354 mesa_format mesa_src_format;
1355 GLenum gl_target = texImage->TexObject->Target;
1356 unsigned bind;
1357 GLubyte *map;
1358 unsigned dstz = texImage->Face + texImage->TexObject->MinLayer;
1359 unsigned dst_level = 0;
1360
1361 st_flush_bitmap_cache(st);
1362 st_invalidate_readpix_cache(st);
1363
1364 if (stObj->pt == stImage->pt)
1365 dst_level = texImage->TexObject->MinLevel + texImage->Level;
1366
1367 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1368 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1369
1370 if (!dst)
1371 goto fallback;
1372
1373 /* Try texture_subdata, which should be the fastest memcpy path. */
1374 if (pixels &&
1375 !_mesa_is_bufferobj(unpack->BufferObj) &&
1376 _mesa_texstore_can_use_memcpy(ctx, texImage->_BaseFormat,
1377 texImage->TexFormat, format, type,
1378 unpack)) {
1379 struct pipe_box box;
1380 unsigned stride, layer_stride;
1381 void *data;
1382
1383 stride = _mesa_image_row_stride(unpack, width, format, type);
1384 layer_stride = _mesa_image_image_stride(unpack, width, height, format,
1385 type);
1386 data = _mesa_image_address(dims, unpack, pixels, width, height, format,
1387 type, 0, 0, 0);
1388
1389 /* Convert to Gallium coordinates. */
1390 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1391 zoffset = yoffset;
1392 yoffset = 0;
1393 depth = height;
1394 height = 1;
1395 layer_stride = stride;
1396 }
1397
1398 u_box_3d(xoffset, yoffset, zoffset + dstz, width, height, depth, &box);
1399 pipe->texture_subdata(pipe, dst, dst_level, 0,
1400 &box, data, stride, layer_stride);
1401 return;
1402 }
1403
1404 if (!st->prefer_blit_based_texture_transfer) {
1405 goto fallback;
1406 }
1407
1408 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1409 * blit implementation in some drivers. */
1410 if (format == GL_DEPTH_STENCIL) {
1411 goto fallback;
1412 }
1413
1414 /* If the base internal format and the texture format don't match,
1415 * we can't use blit-based TexSubImage. */
1416 if (texImage->_BaseFormat !=
1417 _mesa_get_format_base_format(texImage->TexFormat)) {
1418 goto fallback;
1419 }
1420
1421
1422 /* See if the destination format is supported. */
1423 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1424 bind = PIPE_BIND_DEPTH_STENCIL;
1425 else
1426 bind = PIPE_BIND_RENDER_TARGET;
1427
1428 /* For luminance and intensity, only the red channel is stored
1429 * in the destination. */
1430 dst_format = util_format_linear(dst->format);
1431 dst_format = util_format_luminance_to_red(dst_format);
1432 dst_format = util_format_intensity_to_red(dst_format);
1433
1434 if (!dst_format ||
1435 !screen->is_format_supported(screen, dst_format, dst->target,
1436 dst->nr_samples, bind)) {
1437 goto fallback;
1438 }
1439
1440 if (_mesa_is_bufferobj(unpack->BufferObj)) {
1441 if (try_pbo_upload(ctx, dims, texImage, format, type, dst_format,
1442 xoffset, yoffset, zoffset,
1443 width, height, depth, pixels, unpack))
1444 return;
1445 }
1446
1447 /* See if the texture format already matches the format and type,
1448 * in which case the memcpy-based fast path will likely be used and
1449 * we don't have to blit. */
1450 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1451 type, unpack->SwapBytes, NULL)) {
1452 goto fallback;
1453 }
1454
1455 /* Choose the source format. */
1456 src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
1457 format, type, unpack->SwapBytes);
1458 if (!src_format) {
1459 goto fallback;
1460 }
1461
1462 mesa_src_format = st_pipe_format_to_mesa_format(src_format);
1463
1464 /* There is no reason to do this if we cannot use memcpy for the temporary
1465 * source texture at least. This also takes transfer ops into account,
1466 * etc. */
1467 if (!_mesa_texstore_can_use_memcpy(ctx,
1468 _mesa_get_format_base_format(mesa_src_format),
1469 mesa_src_format, format, type, unpack)) {
1470 goto fallback;
1471 }
1472
1473 /* TexSubImage only sets a single cubemap face. */
1474 if (gl_target == GL_TEXTURE_CUBE_MAP) {
1475 gl_target = GL_TEXTURE_2D;
1476 }
1477 /* TexSubImage can specify subsets of cube map array faces
1478 * so we need to upload via 2D array instead */
1479 if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
1480 gl_target = GL_TEXTURE_2D_ARRAY;
1481 }
1482
1483 /* Initialize the source texture description. */
1484 memset(&src_templ, 0, sizeof(src_templ));
1485 src_templ.target = gl_target_to_pipe(gl_target);
1486 src_templ.format = src_format;
1487 src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
1488 src_templ.usage = PIPE_USAGE_STAGING;
1489
1490 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1491 &src_templ.width0, &src_templ.height0,
1492 &src_templ.depth0, &src_templ.array_size);
1493
1494 /* Check for NPOT texture support. */
1495 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
1496 (!util_is_power_of_two(src_templ.width0) ||
1497 !util_is_power_of_two(src_templ.height0) ||
1498 !util_is_power_of_two(src_templ.depth0))) {
1499 goto fallback;
1500 }
1501
1502 /* Create the source texture. */
1503 src = screen->resource_create(screen, &src_templ);
1504 if (!src) {
1505 goto fallback;
1506 }
1507
1508 /* Map source pixels. */
1509 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1510 format, type, pixels, unpack,
1511 "glTexSubImage");
1512 if (!pixels) {
1513 /* This is a GL error. */
1514 pipe_resource_reference(&src, NULL);
1515 return;
1516 }
1517
1518 /* From now on, we need the gallium representation of dimensions. */
1519 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1520 zoffset = yoffset;
1521 yoffset = 0;
1522 depth = height;
1523 height = 1;
1524 }
1525
1526 map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
1527 width, height, depth, &transfer);
1528 if (!map) {
1529 _mesa_unmap_teximage_pbo(ctx, unpack);
1530 pipe_resource_reference(&src, NULL);
1531 goto fallback;
1532 }
1533
1534 /* Upload pixels (just memcpy). */
1535 {
1536 const uint bytesPerRow = width * util_format_get_blocksize(src_format);
1537 GLuint row, slice;
1538
1539 for (slice = 0; slice < (unsigned) depth; slice++) {
1540 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1541 /* 1D array textures.
1542 * We need to convert gallium coords to GL coords.
1543 */
1544 void *src = _mesa_image_address2d(unpack, pixels,
1545 width, depth, format,
1546 type, slice, 0);
1547 memcpy(map, src, bytesPerRow);
1548 }
1549 else {
1550 ubyte *slice_map = map;
1551
1552 for (row = 0; row < (unsigned) height; row++) {
1553 void *src = _mesa_image_address(dims, unpack, pixels,
1554 width, height, format,
1555 type, slice, row, 0);
1556 memcpy(slice_map, src, bytesPerRow);
1557 slice_map += transfer->stride;
1558 }
1559 }
1560 map += transfer->layer_stride;
1561 }
1562 }
1563
1564 pipe_transfer_unmap(pipe, transfer);
1565 _mesa_unmap_teximage_pbo(ctx, unpack);
1566
1567 /* Blit. */
1568 memset(&blit, 0, sizeof(blit));
1569 blit.src.resource = src;
1570 blit.src.level = 0;
1571 blit.src.format = src_format;
1572 blit.dst.resource = dst;
1573 blit.dst.level = dst_level;
1574 blit.dst.format = dst_format;
1575 blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
1576 blit.dst.box.x = xoffset;
1577 blit.dst.box.y = yoffset;
1578 blit.dst.box.z = zoffset + dstz;
1579 blit.src.box.width = blit.dst.box.width = width;
1580 blit.src.box.height = blit.dst.box.height = height;
1581 blit.src.box.depth = blit.dst.box.depth = depth;
1582 blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
1583 blit.filter = PIPE_TEX_FILTER_NEAREST;
1584 blit.scissor_enable = FALSE;
1585
1586 st->pipe->blit(st->pipe, &blit);
1587
1588 pipe_resource_reference(&src, NULL);
1589 return;
1590
1591 fallback:
1592 _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
1593 width, height, depth, format, type, pixels,
1594 unpack);
1595 }
1596
1597 static void
1598 st_TexImage(struct gl_context * ctx, GLuint dims,
1599 struct gl_texture_image *texImage,
1600 GLenum format, GLenum type, const void *pixels,
1601 const struct gl_pixelstore_attrib *unpack)
1602 {
1603 assert(dims == 1 || dims == 2 || dims == 3);
1604
1605 prep_teximage(ctx, texImage, format, type);
1606
1607 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
1608 return;
1609
1610 /* allocate storage for texture data */
1611 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
1612 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1613 return;
1614 }
1615
1616 st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
1617 texImage->Width, texImage->Height, texImage->Depth,
1618 format, type, pixels, unpack);
1619 }
1620
1621
1622 static void
1623 st_CompressedTexSubImage(struct gl_context *ctx, GLuint dims,
1624 struct gl_texture_image *texImage,
1625 GLint x, GLint y, GLint z,
1626 GLsizei w, GLsizei h, GLsizei d,
1627 GLenum format, GLsizei imageSize, const void *data)
1628 {
1629 struct st_context *st = st_context(ctx);
1630 struct st_texture_image *stImage = st_texture_image(texImage);
1631 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1632 struct pipe_resource *texture = stImage->pt;
1633 struct pipe_context *pipe = st->pipe;
1634 struct pipe_screen *screen = pipe->screen;
1635 struct pipe_resource *dst = stImage->pt;
1636 struct pipe_surface *surface = NULL;
1637 struct compressed_pixelstore store;
1638 struct st_pbo_addresses addr;
1639 enum pipe_format copy_format;
1640 unsigned bw, bh;
1641 intptr_t buf_offset;
1642 bool success = false;
1643
1644 /* Check basic pre-conditions for PBO upload */
1645 if (!st->prefer_blit_based_texture_transfer) {
1646 goto fallback;
1647 }
1648
1649 if (!_mesa_is_bufferobj(ctx->Unpack.BufferObj))
1650 goto fallback;
1651
1652 if (st_etc_fallback(st, texImage)) {
1653 /* ETC isn't supported and is represented by uncompressed formats. */
1654 goto fallback;
1655 }
1656
1657 if (!dst) {
1658 goto fallback;
1659 }
1660
1661 if (!st->pbo.upload_enabled ||
1662 !screen->get_param(screen, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS)) {
1663 goto fallback;
1664 }
1665
1666 /* Choose the pipe format for the upload. */
1667 addr.bytes_per_pixel = util_format_get_blocksize(dst->format);
1668 bw = util_format_get_blockwidth(dst->format);
1669 bh = util_format_get_blockheight(dst->format);
1670
1671 switch (addr.bytes_per_pixel) {
1672 case 8:
1673 copy_format = PIPE_FORMAT_R16G16B16A16_UINT;
1674 break;
1675 case 16:
1676 copy_format = PIPE_FORMAT_R32G32B32A32_UINT;
1677 break;
1678 default:
1679 goto fallback;
1680 }
1681
1682 if (!screen->is_format_supported(screen, copy_format, PIPE_BUFFER, 0,
1683 PIPE_BIND_SAMPLER_VIEW)) {
1684 goto fallback;
1685 }
1686
1687 if (!screen->is_format_supported(screen, copy_format, dst->target,
1688 dst->nr_samples, PIPE_BIND_RENDER_TARGET)) {
1689 goto fallback;
1690 }
1691
1692 /* Interpret the pixelstore settings. */
1693 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat, w, h, d,
1694 &ctx->Unpack, &store);
1695 assert(store.CopyBytesPerRow % addr.bytes_per_pixel == 0);
1696 assert(store.SkipBytes % addr.bytes_per_pixel == 0);
1697
1698 /* Compute the offset into the buffer */
1699 buf_offset = (intptr_t)data + store.SkipBytes;
1700
1701 if (buf_offset % addr.bytes_per_pixel) {
1702 goto fallback;
1703 }
1704
1705 buf_offset = buf_offset / addr.bytes_per_pixel;
1706
1707 addr.xoffset = x / bw;
1708 addr.yoffset = y / bh;
1709 addr.width = store.CopyBytesPerRow / addr.bytes_per_pixel;
1710 addr.height = store.CopyRowsPerSlice;
1711 addr.depth = d;
1712 addr.pixels_per_row = store.TotalBytesPerRow / addr.bytes_per_pixel;
1713 addr.image_height = store.TotalRowsPerSlice;
1714
1715 if (!st_pbo_addresses_setup(st, st_buffer_object(ctx->Unpack.BufferObj)->buffer,
1716 buf_offset, &addr))
1717 goto fallback;
1718
1719 /* Set up the surface. */
1720 {
1721 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1722 unsigned max_layer = util_max_layer(texture, level);
1723
1724 z += texImage->Face + texImage->TexObject->MinLayer;
1725
1726 struct pipe_surface templ;
1727 memset(&templ, 0, sizeof(templ));
1728 templ.format = copy_format;
1729 templ.u.tex.level = level;
1730 templ.u.tex.first_layer = MIN2(z, max_layer);
1731 templ.u.tex.last_layer = MIN2(z + d - 1, max_layer);
1732
1733 surface = pipe->create_surface(pipe, texture, &templ);
1734 if (!surface)
1735 goto fallback;
1736 }
1737
1738 success = try_pbo_upload_common(ctx, surface, &addr, copy_format);
1739
1740 pipe_surface_reference(&surface, NULL);
1741
1742 if (success)
1743 return;
1744
1745 fallback:
1746 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
1747 x, y, z, w, h, d,
1748 format, imageSize, data);
1749 }
1750
1751 static void
1752 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
1753 struct gl_texture_image *texImage,
1754 GLsizei imageSize, const void *data)
1755 {
1756 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
1757
1758 /* only 2D and 3D compressed images are supported at this time */
1759 if (dims == 1) {
1760 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
1761 return;
1762 }
1763
1764 /* This is pretty simple, because unlike the general texstore path we don't
1765 * have to worry about the usual image unpacking or image transfer
1766 * operations.
1767 */
1768 assert(texImage);
1769 assert(texImage->Width > 0);
1770 assert(texImage->Height > 0);
1771 assert(texImage->Depth > 0);
1772
1773 /* allocate storage for texture data */
1774 if (!st_AllocTextureImageBuffer(ctx, texImage)) {
1775 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
1776 return;
1777 }
1778
1779 st_CompressedTexSubImage(ctx, dims, texImage,
1780 0, 0, 0,
1781 texImage->Width, texImage->Height, texImage->Depth,
1782 texImage->TexFormat,
1783 imageSize, data);
1784 }
1785
1786
1787
1788
1789 /**
1790 * Called via ctx->Driver.GetTexSubImage()
1791 *
1792 * This uses a blit to copy the texture to a texture format which matches
1793 * the format and type combo and then a fast read-back is done using memcpy.
1794 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1795 * a format which matches the swizzling.
1796 *
1797 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1798 *
1799 * NOTE: Drivers usually do a blit to convert between tiled and linear
1800 * texture layouts during texture uploads/downloads, so the blit
1801 * we do here should be free in such cases.
1802 */
1803 static void
1804 st_GetTexSubImage(struct gl_context * ctx,
1805 GLint xoffset, GLint yoffset, GLint zoffset,
1806 GLsizei width, GLsizei height, GLint depth,
1807 GLenum format, GLenum type, void * pixels,
1808 struct gl_texture_image *texImage)
1809 {
1810 struct st_context *st = st_context(ctx);
1811 struct pipe_context *pipe = st->pipe;
1812 struct pipe_screen *screen = pipe->screen;
1813 struct st_texture_image *stImage = st_texture_image(texImage);
1814 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1815 struct pipe_resource *src = stObj->pt;
1816 struct pipe_resource *dst = NULL;
1817 struct pipe_resource dst_templ;
1818 enum pipe_format dst_format, src_format;
1819 mesa_format mesa_format;
1820 GLenum gl_target = texImage->TexObject->Target;
1821 enum pipe_texture_target pipe_target;
1822 struct pipe_blit_info blit;
1823 unsigned bind = PIPE_BIND_TRANSFER_READ;
1824 struct pipe_transfer *tex_xfer;
1825 ubyte *map = NULL;
1826 boolean done = FALSE;
1827
1828 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1829 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1830
1831 st_flush_bitmap_cache(st);
1832
1833 if (!st->prefer_blit_based_texture_transfer &&
1834 !_mesa_is_format_compressed(texImage->TexFormat)) {
1835 /* Try to avoid the fallback if we're doing texture decompression here */
1836 goto fallback;
1837 }
1838
1839 /* Handle non-finalized textures. */
1840 if (!stImage->pt || stImage->pt != stObj->pt || !src) {
1841 goto fallback;
1842 }
1843
1844 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1845 * due to an incomplete stencil blit implementation in some drivers. */
1846 if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
1847 goto fallback;
1848 }
1849
1850 /* If the base internal format and the texture format don't match, we have
1851 * to fall back to _mesa_GetTexImage_sw. */
1852 if (texImage->_BaseFormat !=
1853 _mesa_get_format_base_format(texImage->TexFormat)) {
1854 goto fallback;
1855 }
1856
1857 /* See if the texture format already matches the format and type,
1858 * in which case the memcpy-based fast path will be used. */
1859 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1860 type, ctx->Pack.SwapBytes, NULL)) {
1861 goto fallback;
1862 }
1863
1864 /* Convert the source format to what is expected by GetTexImage
1865 * and see if it's supported.
1866 *
1867 * This only applies to glGetTexImage:
1868 * - Luminance must be returned as (L,0,0,1).
1869 * - Luminance alpha must be returned as (L,0,0,A).
1870 * - Intensity must be returned as (I,0,0,1)
1871 */
1872 if (stObj->surface_based)
1873 src_format = util_format_linear(stObj->surface_format);
1874 else
1875 src_format = util_format_linear(src->format);
1876 src_format = util_format_luminance_to_red(src_format);
1877 src_format = util_format_intensity_to_red(src_format);
1878
1879 if (!src_format ||
1880 !screen->is_format_supported(screen, src_format, src->target,
1881 src->nr_samples,
1882 PIPE_BIND_SAMPLER_VIEW)) {
1883 goto fallback;
1884 }
1885
1886 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1887 bind |= PIPE_BIND_DEPTH_STENCIL;
1888 else
1889 bind |= PIPE_BIND_RENDER_TARGET;
1890
1891 /* GetTexImage only returns a single face for cubemaps. */
1892 if (gl_target == GL_TEXTURE_CUBE_MAP) {
1893 gl_target = GL_TEXTURE_2D;
1894 }
1895 pipe_target = gl_target_to_pipe(gl_target);
1896
1897 /* Choose the destination format by finding the best match
1898 * for the format+type combo. */
1899 dst_format = st_choose_matching_format(st, bind, format, type,
1900 ctx->Pack.SwapBytes);
1901
1902 if (dst_format == PIPE_FORMAT_NONE) {
1903 GLenum dst_glformat;
1904
1905 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1906 * where decompression with a blit is always preferred. */
1907 if (!util_format_is_compressed(src->format)) {
1908 goto fallback;
1909 }
1910
1911 /* Set the appropriate format for the decompressed texture.
1912 * Luminance and sRGB formats shouldn't appear here.*/
1913 switch (src_format) {
1914 case PIPE_FORMAT_DXT1_RGB:
1915 case PIPE_FORMAT_DXT1_RGBA:
1916 case PIPE_FORMAT_DXT3_RGBA:
1917 case PIPE_FORMAT_DXT5_RGBA:
1918 case PIPE_FORMAT_RGTC1_UNORM:
1919 case PIPE_FORMAT_RGTC2_UNORM:
1920 case PIPE_FORMAT_ETC1_RGB8:
1921 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1922 dst_glformat = GL_RGBA8;
1923 break;
1924 case PIPE_FORMAT_RGTC1_SNORM:
1925 case PIPE_FORMAT_RGTC2_SNORM:
1926 if (!ctx->Extensions.EXT_texture_snorm)
1927 goto fallback;
1928 dst_glformat = GL_RGBA8_SNORM;
1929 break;
1930 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1931 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1932 if (!ctx->Extensions.ARB_texture_float)
1933 goto fallback;
1934 dst_glformat = GL_RGBA32F;
1935 break;
1936 default:
1937 assert(0);
1938 goto fallback;
1939 }
1940
1941 dst_format = st_choose_format(st, dst_glformat, format, type,
1942 pipe_target, 0, bind, FALSE);
1943
1944 if (dst_format == PIPE_FORMAT_NONE) {
1945 /* unable to get an rgba format!?! */
1946 goto fallback;
1947 }
1948 }
1949
1950 /* create the destination texture of size (width X height X depth) */
1951 memset(&dst_templ, 0, sizeof(dst_templ));
1952 dst_templ.target = pipe_target;
1953 dst_templ.format = dst_format;
1954 dst_templ.bind = bind;
1955 dst_templ.usage = PIPE_USAGE_STAGING;
1956
1957 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1958 &dst_templ.width0, &dst_templ.height0,
1959 &dst_templ.depth0, &dst_templ.array_size);
1960
1961 dst = screen->resource_create(screen, &dst_templ);
1962 if (!dst) {
1963 goto fallback;
1964 }
1965
1966 /* From now on, we need the gallium representation of dimensions. */
1967 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1968 zoffset = yoffset;
1969 yoffset = 0;
1970 depth = height;
1971 height = 1;
1972 }
1973
1974 assert(texImage->Face == 0 ||
1975 texImage->TexObject->MinLayer == 0 ||
1976 zoffset == 0);
1977
1978 memset(&blit, 0, sizeof(blit));
1979 blit.src.resource = src;
1980 blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
1981 blit.src.format = src_format;
1982 blit.dst.resource = dst;
1983 blit.dst.level = 0;
1984 blit.dst.format = dst->format;
1985 blit.src.box.x = xoffset;
1986 blit.dst.box.x = 0;
1987 blit.src.box.y = yoffset;
1988 blit.dst.box.y = 0;
1989 blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
1990 blit.dst.box.z = 0;
1991 blit.src.box.width = blit.dst.box.width = width;
1992 blit.src.box.height = blit.dst.box.height = height;
1993 blit.src.box.depth = blit.dst.box.depth = depth;
1994 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
1995 blit.filter = PIPE_TEX_FILTER_NEAREST;
1996 blit.scissor_enable = FALSE;
1997
1998 /* blit/render/decompress */
1999 st->pipe->blit(st->pipe, &blit);
2000
2001 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
2002
2003 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
2004 0, 0, 0, width, height, depth, &tex_xfer);
2005 if (!map) {
2006 goto end;
2007 }
2008
2009 mesa_format = st_pipe_format_to_mesa_format(dst_format);
2010
2011 /* copy/pack data into user buffer */
2012 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2013 ctx->Pack.SwapBytes, NULL)) {
2014 /* memcpy */
2015 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
2016 GLuint row, slice;
2017
2018 for (slice = 0; slice < depth; slice++) {
2019 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2020 /* 1D array textures.
2021 * We need to convert gallium coords to GL coords.
2022 */
2023 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2024 width, depth, format,
2025 type, 0, slice, 0);
2026 memcpy(dest, map, bytesPerRow);
2027 }
2028 else {
2029 ubyte *slice_map = map;
2030
2031 for (row = 0; row < height; row++) {
2032 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2033 width, height, format,
2034 type, slice, row, 0);
2035 memcpy(dest, slice_map, bytesPerRow);
2036 slice_map += tex_xfer->stride;
2037 }
2038 }
2039 map += tex_xfer->layer_stride;
2040 }
2041 }
2042 else {
2043 /* format translation via floats */
2044 GLuint row, slice;
2045 GLfloat *rgba;
2046 uint32_t dstMesaFormat;
2047 int dstStride, srcStride;
2048
2049 assert(util_format_is_compressed(src->format));
2050
2051 rgba = malloc(width * 4 * sizeof(GLfloat));
2052 if (!rgba) {
2053 goto end;
2054 }
2055
2056 if (ST_DEBUG & DEBUG_FALLBACK)
2057 debug_printf("%s: fallback format translation\n", __func__);
2058
2059 dstMesaFormat = _mesa_format_from_format_and_type(format, type);
2060 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
2061 srcStride = 4 * width * sizeof(GLfloat);
2062 for (slice = 0; slice < depth; slice++) {
2063 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2064 /* 1D array textures.
2065 * We need to convert gallium coords to GL coords.
2066 */
2067 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2068 width, depth, format,
2069 type, 0, slice, 0);
2070
2071 /* get float[4] rgba row from surface */
2072 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
2073 dst_format, rgba);
2074
2075 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2076 rgba, RGBA32_FLOAT, srcStride,
2077 width, 1, NULL);
2078 }
2079 else {
2080 for (row = 0; row < height; row++) {
2081 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2082 width, height, format,
2083 type, slice, row, 0);
2084
2085 /* get float[4] rgba row from surface */
2086 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
2087 dst_format, rgba);
2088
2089 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2090 rgba, RGBA32_FLOAT, srcStride,
2091 width, 1, NULL);
2092 }
2093 }
2094 map += tex_xfer->layer_stride;
2095 }
2096
2097 free(rgba);
2098 }
2099 done = TRUE;
2100
2101 end:
2102 if (map)
2103 pipe_transfer_unmap(pipe, tex_xfer);
2104
2105 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
2106 pipe_resource_reference(&dst, NULL);
2107
2108 fallback:
2109 if (!done) {
2110 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
2111 width, height, depth,
2112 format, type, pixels, texImage);
2113 }
2114 }
2115
2116
2117 /**
2118 * Do a CopyTexSubImage operation using a read transfer from the source,
2119 * a write transfer to the destination and get_tile()/put_tile() to access
2120 * the pixels/texels.
2121 *
2122 * Note: srcY=0=TOP of renderbuffer
2123 */
2124 static void
2125 fallback_copy_texsubimage(struct gl_context *ctx,
2126 struct st_renderbuffer *strb,
2127 struct st_texture_image *stImage,
2128 GLenum baseFormat,
2129 GLint destX, GLint destY, GLint slice,
2130 GLint srcX, GLint srcY,
2131 GLsizei width, GLsizei height)
2132 {
2133 struct st_context *st = st_context(ctx);
2134 struct pipe_context *pipe = st->pipe;
2135 struct pipe_transfer *src_trans;
2136 GLubyte *texDest;
2137 enum pipe_transfer_usage transfer_usage;
2138 void *map;
2139 unsigned dst_width = width;
2140 unsigned dst_height = height;
2141 unsigned dst_depth = 1;
2142 struct pipe_transfer *transfer;
2143
2144 if (ST_DEBUG & DEBUG_FALLBACK)
2145 debug_printf("%s: fallback processing\n", __func__);
2146
2147 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2148 srcY = strb->Base.Height - srcY - height;
2149 }
2150
2151 map = pipe_transfer_map(pipe,
2152 strb->texture,
2153 strb->surface->u.tex.level,
2154 strb->surface->u.tex.first_layer,
2155 PIPE_TRANSFER_READ,
2156 srcX, srcY,
2157 width, height, &src_trans);
2158
2159 if ((baseFormat == GL_DEPTH_COMPONENT ||
2160 baseFormat == GL_DEPTH_STENCIL) &&
2161 util_format_is_depth_and_stencil(stImage->pt->format))
2162 transfer_usage = PIPE_TRANSFER_READ_WRITE;
2163 else
2164 transfer_usage = PIPE_TRANSFER_WRITE;
2165
2166 texDest = st_texture_image_map(st, stImage, transfer_usage,
2167 destX, destY, slice,
2168 dst_width, dst_height, dst_depth,
2169 &transfer);
2170
2171 if (baseFormat == GL_DEPTH_COMPONENT ||
2172 baseFormat == GL_DEPTH_STENCIL) {
2173 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
2174 ctx->Pixel.DepthBias != 0.0F);
2175 GLint row, yStep;
2176 uint *data;
2177
2178 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2179 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2180 srcY = height - 1;
2181 yStep = -1;
2182 }
2183 else {
2184 srcY = 0;
2185 yStep = 1;
2186 }
2187
2188 data = malloc(width * sizeof(uint));
2189
2190 if (data) {
2191 /* To avoid a large temp memory allocation, do copy row by row */
2192 for (row = 0; row < height; row++, srcY += yStep) {
2193 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
2194 if (scaleOrBias) {
2195 _mesa_scale_and_bias_depth_uint(ctx, width, data);
2196 }
2197
2198 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2199 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
2200 0, 0, width, 1, data);
2201 }
2202 else {
2203 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
2204 }
2205 }
2206 }
2207 else {
2208 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
2209 }
2210
2211 free(data);
2212 }
2213 else {
2214 /* RGBA format */
2215 GLfloat *tempSrc =
2216 malloc(width * height * 4 * sizeof(GLfloat));
2217
2218 if (tempSrc && texDest) {
2219 const GLint dims = 2;
2220 GLint dstRowStride;
2221 struct gl_texture_image *texImage = &stImage->base;
2222 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
2223
2224 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2225 unpack.Invert = GL_TRUE;
2226 }
2227
2228 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2229 dstRowStride = transfer->layer_stride;
2230 }
2231 else {
2232 dstRowStride = transfer->stride;
2233 }
2234
2235 /* get float/RGBA image from framebuffer */
2236 /* XXX this usually involves a lot of int/float conversion.
2237 * try to avoid that someday.
2238 */
2239 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
2240 util_format_linear(strb->texture->format),
2241 tempSrc);
2242
2243 /* Store into texture memory.
2244 * Note that this does some special things such as pixel transfer
2245 * ops and format conversion. In particular, if the dest tex format
2246 * is actually RGBA but the user created the texture as GL_RGB we
2247 * need to fill-in/override the alpha channel with 1.0.
2248 */
2249 _mesa_texstore(ctx, dims,
2250 texImage->_BaseFormat,
2251 texImage->TexFormat,
2252 dstRowStride,
2253 &texDest,
2254 width, height, 1,
2255 GL_RGBA, GL_FLOAT, tempSrc, /* src */
2256 &unpack);
2257 }
2258 else {
2259 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
2260 }
2261
2262 free(tempSrc);
2263 }
2264
2265 st_texture_image_unmap(st, stImage, slice);
2266 pipe->transfer_unmap(pipe, src_trans);
2267 }
2268
2269
2270 /**
2271 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2272 * Note that the region to copy has already been clipped so we know we
2273 * won't read from outside the source renderbuffer's bounds.
2274 *
2275 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2276 */
2277 static void
2278 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2279 struct gl_texture_image *texImage,
2280 GLint destX, GLint destY, GLint slice,
2281 struct gl_renderbuffer *rb,
2282 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
2283 {
2284 struct st_texture_image *stImage = st_texture_image(texImage);
2285 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2286 struct st_renderbuffer *strb = st_renderbuffer(rb);
2287 struct st_context *st = st_context(ctx);
2288 struct pipe_context *pipe = st->pipe;
2289 struct pipe_screen *screen = pipe->screen;
2290 struct pipe_blit_info blit;
2291 enum pipe_format dst_format;
2292 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
2293 unsigned bind;
2294 GLint srcY0, srcY1;
2295
2296 st_flush_bitmap_cache(st);
2297 st_invalidate_readpix_cache(st);
2298
2299 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2300 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2301
2302 if (!strb || !strb->surface || !stImage->pt) {
2303 debug_printf("%s: null strb or stImage\n", __func__);
2304 return;
2305 }
2306
2307 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
2308 texImage->TexFormat)) {
2309 goto fallback;
2310 }
2311
2312 /* The base internal format must match the mesa format, so make sure
2313 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2314 */
2315 if (texImage->_BaseFormat !=
2316 _mesa_get_format_base_format(texImage->TexFormat) ||
2317 rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
2318 goto fallback;
2319 }
2320
2321 /* Choose the destination format to match the TexImage behavior. */
2322 dst_format = util_format_linear(stImage->pt->format);
2323 dst_format = util_format_luminance_to_red(dst_format);
2324 dst_format = util_format_intensity_to_red(dst_format);
2325
2326 /* See if the destination format is supported. */
2327 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
2328 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
2329 bind = PIPE_BIND_DEPTH_STENCIL;
2330 }
2331 else {
2332 bind = PIPE_BIND_RENDER_TARGET;
2333 }
2334
2335 if (!dst_format ||
2336 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
2337 stImage->pt->nr_samples, bind)) {
2338 goto fallback;
2339 }
2340
2341 /* Y flipping for the main framebuffer. */
2342 if (do_flip) {
2343 srcY1 = strb->Base.Height - srcY - height;
2344 srcY0 = srcY1 + height;
2345 }
2346 else {
2347 srcY0 = srcY;
2348 srcY1 = srcY0 + height;
2349 }
2350
2351 /* Blit the texture.
2352 * This supports flipping, format conversions, and downsampling.
2353 */
2354 memset(&blit, 0, sizeof(blit));
2355 blit.src.resource = strb->texture;
2356 blit.src.format = util_format_linear(strb->surface->format);
2357 blit.src.level = strb->surface->u.tex.level;
2358 blit.src.box.x = srcX;
2359 blit.src.box.y = srcY0;
2360 blit.src.box.z = strb->surface->u.tex.first_layer;
2361 blit.src.box.width = width;
2362 blit.src.box.height = srcY1 - srcY0;
2363 blit.src.box.depth = 1;
2364 blit.dst.resource = stImage->pt;
2365 blit.dst.format = dst_format;
2366 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
2367 blit.dst.box.x = destX;
2368 blit.dst.box.y = destY;
2369 blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
2370 blit.dst.box.width = width;
2371 blit.dst.box.height = height;
2372 blit.dst.box.depth = 1;
2373 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
2374 blit.filter = PIPE_TEX_FILTER_NEAREST;
2375 pipe->blit(pipe, &blit);
2376 return;
2377
2378 fallback:
2379 /* software fallback */
2380 fallback_copy_texsubimage(ctx,
2381 strb, stImage, texImage->_BaseFormat,
2382 destX, destY, slice,
2383 srcX, srcY, width, height);
2384 }
2385
2386
2387 /**
2388 * Copy image data from stImage into the texture object 'stObj' at level
2389 * 'dstLevel'.
2390 */
2391 static void
2392 copy_image_data_to_texture(struct st_context *st,
2393 struct st_texture_object *stObj,
2394 GLuint dstLevel,
2395 struct st_texture_image *stImage)
2396 {
2397 /* debug checks */
2398 {
2399 const struct gl_texture_image *dstImage =
2400 stObj->base.Image[stImage->base.Face][dstLevel];
2401 assert(dstImage);
2402 assert(dstImage->Width == stImage->base.Width);
2403 assert(dstImage->Height == stImage->base.Height);
2404 assert(dstImage->Depth == stImage->base.Depth);
2405 }
2406
2407 if (stImage->pt) {
2408 /* Copy potentially with the blitter:
2409 */
2410 GLuint src_level;
2411 if (stImage->pt->last_level == 0)
2412 src_level = 0;
2413 else
2414 src_level = stImage->base.Level;
2415
2416 assert(src_level <= stImage->pt->last_level);
2417 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
2418 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
2419 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
2420 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
2421 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
2422 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
2423
2424 st_texture_image_copy(st->pipe,
2425 stObj->pt, dstLevel, /* dest texture, level */
2426 stImage->pt, src_level, /* src texture, level */
2427 stImage->base.Face);
2428
2429 pipe_resource_reference(&stImage->pt, NULL);
2430 }
2431 pipe_resource_reference(&stImage->pt, stObj->pt);
2432 }
2433
2434
2435 /**
2436 * Called during state validation. When this function is finished,
2437 * the texture object should be ready for rendering.
2438 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2439 */
2440 GLboolean
2441 st_finalize_texture(struct gl_context *ctx,
2442 struct pipe_context *pipe,
2443 struct gl_texture_object *tObj)
2444 {
2445 struct st_context *st = st_context(ctx);
2446 struct st_texture_object *stObj = st_texture_object(tObj);
2447 const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
2448 GLuint face;
2449 const struct st_texture_image *firstImage;
2450 enum pipe_format firstImageFormat;
2451 GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
2452
2453 if (tObj->Immutable)
2454 return GL_TRUE;
2455
2456 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
2457 /* The texture is complete and we know exactly how many mipmap levels
2458 * are present/needed. This is conditional because we may be called
2459 * from the st_generate_mipmap() function when the texture object is
2460 * incomplete. In that case, we'll have set stObj->lastLevel before
2461 * we get here.
2462 */
2463 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
2464 stObj->base.Sampler.MinFilter == GL_NEAREST)
2465 stObj->lastLevel = stObj->base.BaseLevel;
2466 else
2467 stObj->lastLevel = stObj->base._MaxLevel;
2468 }
2469
2470 if (tObj->Target == GL_TEXTURE_BUFFER) {
2471 struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
2472
2473 if (!st_obj) {
2474 pipe_resource_reference(&stObj->pt, NULL);
2475 st_texture_release_all_sampler_views(st, stObj);
2476 return GL_TRUE;
2477 }
2478
2479 if (st_obj->buffer != stObj->pt) {
2480 pipe_resource_reference(&stObj->pt, st_obj->buffer);
2481 st_texture_release_all_sampler_views(st, stObj);
2482 }
2483 return GL_TRUE;
2484
2485 }
2486
2487 firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
2488 assert(firstImage);
2489
2490 /* If both firstImage and stObj point to a texture which can contain
2491 * all active images, favour firstImage. Note that because of the
2492 * completeness requirement, we know that the image dimensions
2493 * will match.
2494 */
2495 if (firstImage->pt &&
2496 firstImage->pt != stObj->pt &&
2497 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
2498 pipe_resource_reference(&stObj->pt, firstImage->pt);
2499 st_texture_release_all_sampler_views(st, stObj);
2500 }
2501
2502 /* If this texture comes from a window system, there is nothing else to do. */
2503 if (stObj->surface_based) {
2504 return GL_TRUE;
2505 }
2506
2507 /* Find gallium format for the Mesa texture */
2508 firstImageFormat =
2509 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
2510
2511 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2512 {
2513 GLuint width, height, depth;
2514
2515 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
2516 firstImage->base.Width2,
2517 firstImage->base.Height2,
2518 firstImage->base.Depth2,
2519 &width, &height, &depth, &ptLayers);
2520
2521 /* If we previously allocated a pipe texture and its sizes are
2522 * compatible, use them.
2523 */
2524 if (stObj->pt &&
2525 u_minify(stObj->pt->width0, firstImage->base.Level) == width &&
2526 u_minify(stObj->pt->height0, firstImage->base.Level) == height &&
2527 u_minify(stObj->pt->depth0, firstImage->base.Level) == depth) {
2528 ptWidth = stObj->pt->width0;
2529 ptHeight = stObj->pt->height0;
2530 ptDepth = stObj->pt->depth0;
2531 } else {
2532 /* Otherwise, compute a new level=0 size that is compatible with the
2533 * base level image.
2534 */
2535 ptWidth = width > 1 ? width << firstImage->base.Level : 1;
2536 ptHeight = height > 1 ? height << firstImage->base.Level : 1;
2537 ptDepth = depth > 1 ? depth << firstImage->base.Level : 1;
2538
2539 /* If the base level image is 1x1x1, we still need to ensure that the
2540 * resulting pipe texture ends up with the required number of levels
2541 * in total.
2542 */
2543 if (ptWidth == 1 && ptHeight == 1 && ptDepth == 1) {
2544 ptWidth <<= firstImage->base.Level;
2545
2546 if (stObj->base.Target == GL_TEXTURE_CUBE_MAP ||
2547 stObj->base.Target == GL_TEXTURE_CUBE_MAP_ARRAY)
2548 ptHeight = ptWidth;
2549 }
2550 }
2551
2552 ptNumSamples = firstImage->base.NumSamples;
2553 }
2554
2555 /* If we already have a gallium texture, check that it matches the texture
2556 * object's format, target, size, num_levels, etc.
2557 */
2558 if (stObj->pt) {
2559 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
2560 stObj->pt->format != firstImageFormat ||
2561 stObj->pt->last_level < stObj->lastLevel ||
2562 stObj->pt->width0 != ptWidth ||
2563 stObj->pt->height0 != ptHeight ||
2564 stObj->pt->depth0 != ptDepth ||
2565 stObj->pt->nr_samples != ptNumSamples ||
2566 stObj->pt->array_size != ptLayers)
2567 {
2568 /* The gallium texture does not match the Mesa texture so delete the
2569 * gallium texture now. We'll make a new one below.
2570 */
2571 pipe_resource_reference(&stObj->pt, NULL);
2572 st_texture_release_all_sampler_views(st, stObj);
2573 st->dirty |= ST_NEW_FRAMEBUFFER;
2574 }
2575 }
2576
2577 /* May need to create a new gallium texture:
2578 */
2579 if (!stObj->pt) {
2580 GLuint bindings = default_bindings(st, firstImageFormat);
2581
2582 stObj->pt = st_texture_create(st,
2583 gl_target_to_pipe(stObj->base.Target),
2584 firstImageFormat,
2585 stObj->lastLevel,
2586 ptWidth,
2587 ptHeight,
2588 ptDepth,
2589 ptLayers, ptNumSamples,
2590 bindings);
2591
2592 if (!stObj->pt) {
2593 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
2594 return GL_FALSE;
2595 }
2596 }
2597
2598 /* Pull in any images not in the object's texture:
2599 */
2600 for (face = 0; face < nr_faces; face++) {
2601 GLuint level;
2602 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
2603 struct st_texture_image *stImage =
2604 st_texture_image(stObj->base.Image[face][level]);
2605
2606 /* Need to import images in main memory or held in other textures.
2607 */
2608 if (stImage && stObj->pt != stImage->pt) {
2609 GLuint height;
2610 GLuint depth;
2611
2612 if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
2613 height = u_minify(ptHeight, level);
2614 else
2615 height = ptLayers;
2616
2617 if (stObj->base.Target == GL_TEXTURE_3D)
2618 depth = u_minify(ptDepth, level);
2619 else if (stObj->base.Target == GL_TEXTURE_CUBE_MAP)
2620 depth = 1;
2621 else
2622 depth = ptLayers;
2623
2624 if (level == 0 ||
2625 (stImage->base.Width == u_minify(ptWidth, level) &&
2626 stImage->base.Height == height &&
2627 stImage->base.Depth == depth)) {
2628 /* src image fits expected dest mipmap level size */
2629 copy_image_data_to_texture(st, stObj, level, stImage);
2630 }
2631 }
2632 }
2633 }
2634
2635 return GL_TRUE;
2636 }
2637
2638
2639 /**
2640 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2641 * for a whole mipmap stack.
2642 */
2643 static GLboolean
2644 st_AllocTextureStorage(struct gl_context *ctx,
2645 struct gl_texture_object *texObj,
2646 GLsizei levels, GLsizei width,
2647 GLsizei height, GLsizei depth)
2648 {
2649 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
2650 struct gl_texture_image *texImage = texObj->Image[0][0];
2651 struct st_context *st = st_context(ctx);
2652 struct st_texture_object *stObj = st_texture_object(texObj);
2653 struct pipe_screen *screen = st->pipe->screen;
2654 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
2655 enum pipe_format fmt;
2656 GLint level;
2657 GLuint num_samples = texImage->NumSamples;
2658
2659 assert(levels > 0);
2660
2661 stObj->lastLevel = levels - 1;
2662
2663 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
2664
2665 bindings = default_bindings(st, fmt);
2666
2667 /* Raise the sample count if the requested one is unsupported. */
2668 if (num_samples > 1) {
2669 boolean found = FALSE;
2670
2671 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
2672 if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
2673 num_samples,
2674 PIPE_BIND_SAMPLER_VIEW)) {
2675 /* Update the sample count in gl_texture_image as well. */
2676 texImage->NumSamples = num_samples;
2677 found = TRUE;
2678 break;
2679 }
2680 }
2681
2682 if (!found) {
2683 return GL_FALSE;
2684 }
2685 }
2686
2687 st_gl_texture_dims_to_pipe_dims(texObj->Target,
2688 width, height, depth,
2689 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2690
2691 stObj->pt = st_texture_create(st,
2692 gl_target_to_pipe(texObj->Target),
2693 fmt,
2694 levels - 1,
2695 ptWidth,
2696 ptHeight,
2697 ptDepth,
2698 ptLayers, num_samples,
2699 bindings);
2700 if (!stObj->pt)
2701 return GL_FALSE;
2702
2703 /* Set image resource pointers */
2704 for (level = 0; level < levels; level++) {
2705 GLuint face;
2706 for (face = 0; face < numFaces; face++) {
2707 struct st_texture_image *stImage =
2708 st_texture_image(texObj->Image[face][level]);
2709 pipe_resource_reference(&stImage->pt, stObj->pt);
2710
2711 etc_fallback_allocate(st, stImage);
2712 }
2713 }
2714
2715 return GL_TRUE;
2716 }
2717
2718
2719 static GLboolean
2720 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
2721 GLuint numLevels, GLint level,
2722 mesa_format format, GLuint numSamples,
2723 GLint width, GLint height, GLint depth)
2724 {
2725 struct st_context *st = st_context(ctx);
2726 struct pipe_context *pipe = st->pipe;
2727
2728 if (width == 0 || height == 0 || depth == 0) {
2729 /* zero-sized images are legal, and always fit! */
2730 return GL_TRUE;
2731 }
2732
2733 if (pipe->screen->can_create_resource) {
2734 /* Ask the gallium driver if the texture is too large */
2735 struct gl_texture_object *texObj =
2736 _mesa_get_current_tex_object(ctx, target);
2737 struct pipe_resource pt;
2738
2739 /* Setup the pipe_resource object
2740 */
2741 memset(&pt, 0, sizeof(pt));
2742
2743 pt.target = gl_target_to_pipe(target);
2744 pt.format = st_mesa_format_to_pipe_format(st, format);
2745 pt.nr_samples = numSamples;
2746
2747 st_gl_texture_dims_to_pipe_dims(target,
2748 width, height, depth,
2749 &pt.width0, &pt.height0,
2750 &pt.depth0, &pt.array_size);
2751
2752 if (numLevels > 0) {
2753 /* For immutable textures we know the final number of mip levels */
2754 pt.last_level = numLevels - 1;
2755 }
2756 else if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
2757 texObj->Sampler.MinFilter == GL_NEAREST)) {
2758 /* assume just one mipmap level */
2759 pt.last_level = 0;
2760 }
2761 else {
2762 /* assume a full set of mipmaps */
2763 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
2764 }
2765
2766 return pipe->screen->can_create_resource(pipe->screen, &pt);
2767 }
2768 else {
2769 /* Use core Mesa fallback */
2770 return _mesa_test_proxy_teximage(ctx, target, numLevels, level, format,
2771 numSamples, width, height, depth);
2772 }
2773 }
2774
2775 static GLboolean
2776 st_TextureView(struct gl_context *ctx,
2777 struct gl_texture_object *texObj,
2778 struct gl_texture_object *origTexObj)
2779 {
2780 struct st_texture_object *orig = st_texture_object(origTexObj);
2781 struct st_texture_object *tex = st_texture_object(texObj);
2782 struct gl_texture_image *image = texObj->Image[0][0];
2783
2784 const int numFaces = _mesa_num_tex_faces(texObj->Target);
2785 const int numLevels = texObj->NumLevels;
2786
2787 int face;
2788 int level;
2789
2790 pipe_resource_reference(&tex->pt, orig->pt);
2791
2792 /* Set image resource pointers */
2793 for (level = 0; level < numLevels; level++) {
2794 for (face = 0; face < numFaces; face++) {
2795 struct st_texture_image *stImage =
2796 st_texture_image(texObj->Image[face][level]);
2797 pipe_resource_reference(&stImage->pt, tex->pt);
2798 }
2799 }
2800
2801 tex->surface_based = GL_TRUE;
2802 tex->surface_format =
2803 st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
2804
2805 tex->lastLevel = numLevels - 1;
2806
2807 return GL_TRUE;
2808 }
2809
2810 static void
2811 st_ClearTexSubImage(struct gl_context *ctx,
2812 struct gl_texture_image *texImage,
2813 GLint xoffset, GLint yoffset, GLint zoffset,
2814 GLsizei width, GLsizei height, GLsizei depth,
2815 const void *clearValue)
2816 {
2817 static const char zeros[16] = {0};
2818 struct st_texture_image *stImage = st_texture_image(texImage);
2819 struct pipe_resource *pt = stImage->pt;
2820 struct st_context *st = st_context(ctx);
2821 struct pipe_context *pipe = st->pipe;
2822 unsigned level = texImage->Level;
2823 struct pipe_box box;
2824
2825 if (!pt)
2826 return;
2827
2828 st_flush_bitmap_cache(st);
2829 st_invalidate_readpix_cache(st);
2830
2831 u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
2832 width, height, depth, &box);
2833 if (texImage->TexObject->Immutable) {
2834 level += texImage->TexObject->MinLevel;
2835 box.z += texImage->TexObject->MinLayer;
2836 }
2837
2838 pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
2839 }
2840
2841 void
2842 st_init_texture_functions(struct dd_function_table *functions)
2843 {
2844 functions->ChooseTextureFormat = st_ChooseTextureFormat;
2845 functions->QueryInternalFormat = st_QueryInternalFormat;
2846 functions->TexImage = st_TexImage;
2847 functions->TexSubImage = st_TexSubImage;
2848 functions->CompressedTexSubImage = st_CompressedTexSubImage;
2849 functions->CopyTexSubImage = st_CopyTexSubImage;
2850 functions->GenerateMipmap = st_generate_mipmap;
2851
2852 functions->GetTexSubImage = st_GetTexSubImage;
2853
2854 /* compressed texture functions */
2855 functions->CompressedTexImage = st_CompressedTexImage;
2856 functions->GetCompressedTexSubImage = _mesa_GetCompressedTexSubImage_sw;
2857
2858 functions->NewTextureObject = st_NewTextureObject;
2859 functions->NewTextureImage = st_NewTextureImage;
2860 functions->DeleteTextureImage = st_DeleteTextureImage;
2861 functions->DeleteTexture = st_DeleteTextureObject;
2862 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
2863 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
2864 functions->MapTextureImage = st_MapTextureImage;
2865 functions->UnmapTextureImage = st_UnmapTextureImage;
2866
2867 /* XXX Temporary until we can query pipe's texture sizes */
2868 functions->TestProxyTexImage = st_TestProxyTexImage;
2869
2870 functions->AllocTextureStorage = st_AllocTextureStorage;
2871 functions->TextureView = st_TextureView;
2872 functions->ClearTexSubImage = st_ClearTexSubImage;
2873 }