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