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