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