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