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