st/mesa: move PBO vertex and geometry shader 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 void *
1074 create_pbo_upload_fs(struct st_context *st)
1075 {
1076 struct pipe_context *pipe = st->pipe;
1077 struct pipe_screen *screen = pipe->screen;
1078 struct ureg_program *ureg;
1079 struct ureg_dst out;
1080 struct ureg_src sampler;
1081 struct ureg_src pos;
1082 struct ureg_src layer;
1083 struct ureg_src const0;
1084 struct ureg_dst temp0;
1085
1086 ureg = ureg_create(PIPE_SHADER_FRAGMENT);
1087 if (!ureg)
1088 return NULL;
1089
1090 out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
1091 sampler = ureg_DECL_sampler(ureg, 0);
1092 if (screen->get_param(screen, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL)) {
1093 pos = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_POSITION, 0);
1094 } else {
1095 pos = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_POSITION, 0,
1096 TGSI_INTERPOLATE_LINEAR);
1097 }
1098 if (st->pbo_upload.upload_layers) {
1099 layer = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_LAYER, 0,
1100 TGSI_INTERPOLATE_CONSTANT);
1101 }
1102 const0 = ureg_DECL_constant(ureg, 0);
1103 temp0 = ureg_DECL_temporary(ureg);
1104
1105 /* Note: const0 = [ -xoffset + skip_pixels, -yoffset, stride, image_height ] */
1106
1107 /* temp0.xy = f2i(temp0.xy) */
1108 ureg_F2I(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_XY),
1109 ureg_swizzle(pos,
1110 TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y,
1111 TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y));
1112
1113 /* temp0.xy = temp0.xy + const0.xy */
1114 ureg_UADD(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_XY),
1115 ureg_swizzle(ureg_src(temp0),
1116 TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y,
1117 TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y),
1118 ureg_swizzle(const0,
1119 TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y,
1120 TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y));
1121
1122 /* temp0.x = const0.z * temp0.y + temp0.x */
1123 ureg_UMAD(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_X),
1124 ureg_scalar(const0, TGSI_SWIZZLE_Z),
1125 ureg_scalar(ureg_src(temp0), TGSI_SWIZZLE_Y),
1126 ureg_scalar(ureg_src(temp0), TGSI_SWIZZLE_X));
1127
1128 if (st->pbo_upload.upload_layers) {
1129 /* temp0.x = const0.w * layer + temp0.x */
1130 ureg_UMAD(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_X),
1131 ureg_scalar(const0, TGSI_SWIZZLE_W),
1132 ureg_scalar(layer, TGSI_SWIZZLE_X),
1133 ureg_scalar(ureg_src(temp0), TGSI_SWIZZLE_X));
1134 }
1135
1136 /* temp0.w = 0 */
1137 ureg_MOV(ureg, ureg_writemask(temp0, TGSI_WRITEMASK_W), ureg_imm1u(ureg, 0));
1138
1139 /* out = txf(sampler, temp0.x) */
1140 ureg_TXF(ureg, out, TGSI_TEXTURE_BUFFER, ureg_src(temp0), sampler);
1141
1142 ureg_release_temporary(ureg, temp0);
1143
1144 ureg_END(ureg);
1145
1146 return ureg_create_shader_and_destroy(ureg, pipe);
1147 }
1148
1149 static bool
1150 try_pbo_upload_common(struct gl_context *ctx,
1151 struct pipe_surface *surface,
1152 int xoffset, int yoffset,
1153 unsigned upload_width, unsigned upload_height,
1154 struct pipe_resource *buffer,
1155 enum pipe_format src_format,
1156 intptr_t buf_offset,
1157 unsigned bytes_per_pixel,
1158 unsigned stride,
1159 unsigned image_height)
1160 {
1161 struct st_context *st = st_context(ctx);
1162 struct cso_context *cso = st->cso_context;
1163 struct pipe_context *pipe = st->pipe;
1164 unsigned depth = surface->u.tex.last_layer - surface->u.tex.first_layer + 1;
1165 unsigned skip_pixels = 0;
1166 bool success = false;
1167
1168 /* Check alignment. */
1169 {
1170 unsigned ofs = (buf_offset * bytes_per_pixel) % ctx->Const.TextureBufferOffsetAlignment;
1171 if (ofs != 0) {
1172 if (ofs % bytes_per_pixel != 0)
1173 return false;
1174
1175 skip_pixels = ofs / bytes_per_pixel;
1176 buf_offset -= skip_pixels;
1177 }
1178 }
1179
1180 /* Create the shaders */
1181 if (!st->pbo_upload.vs) {
1182 st->pbo_upload.vs = st_pbo_create_vs(st);
1183 if (!st->pbo_upload.vs)
1184 return false;
1185 }
1186
1187 if (depth != 1 && st->pbo_upload.use_gs && !st->pbo_upload.gs) {
1188 st->pbo_upload.gs = st_pbo_create_gs(st);
1189 if (!st->pbo_upload.gs)
1190 return false;
1191 }
1192
1193 if (!st->pbo_upload.fs) {
1194 st->pbo_upload.fs = create_pbo_upload_fs(st);
1195 if (!st->pbo_upload.fs)
1196 return false;
1197 }
1198
1199 cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
1200 CSO_BIT_FRAGMENT_SAMPLERS |
1201 CSO_BIT_VERTEX_ELEMENTS |
1202 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
1203 CSO_BIT_FRAMEBUFFER |
1204 CSO_BIT_VIEWPORT |
1205 CSO_BIT_BLEND |
1206 CSO_BIT_DEPTH_STENCIL_ALPHA |
1207 CSO_BIT_RASTERIZER |
1208 CSO_BIT_STREAM_OUTPUTS |
1209 CSO_BIT_PAUSE_QUERIES |
1210 CSO_BITS_ALL_SHADERS));
1211 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1212
1213
1214 /* Set up the sampler_view */
1215 {
1216 unsigned first_element = buf_offset;
1217 unsigned last_element = buf_offset + skip_pixels + upload_width - 1
1218 + (upload_height - 1 + (depth - 1) * image_height) * stride;
1219 struct pipe_sampler_view templ;
1220 struct pipe_sampler_view *sampler_view;
1221 struct pipe_sampler_state sampler = {0};
1222 const struct pipe_sampler_state *samplers[1] = {&sampler};
1223
1224 /* This should be ensured by Mesa before calling our callbacks */
1225 assert((last_element + 1) * bytes_per_pixel <= buffer->width0);
1226
1227 if (last_element - first_element > ctx->Const.MaxTextureBufferSize - 1)
1228 goto fail;
1229
1230 memset(&templ, 0, sizeof(templ));
1231 templ.target = PIPE_BUFFER;
1232 templ.format = src_format;
1233 templ.u.buf.first_element = first_element;
1234 templ.u.buf.last_element = last_element;
1235 templ.swizzle_r = PIPE_SWIZZLE_X;
1236 templ.swizzle_g = PIPE_SWIZZLE_Y;
1237 templ.swizzle_b = PIPE_SWIZZLE_Z;
1238 templ.swizzle_a = PIPE_SWIZZLE_W;
1239
1240 sampler_view = pipe->create_sampler_view(pipe, buffer, &templ);
1241 if (sampler_view == NULL)
1242 goto fail;
1243
1244 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
1245
1246 pipe_sampler_view_reference(&sampler_view, NULL);
1247
1248 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
1249 }
1250
1251 /* Upload vertices */
1252 {
1253 struct pipe_vertex_buffer vbo;
1254 struct pipe_vertex_element velem;
1255
1256 float x0 = (float) xoffset / surface->width * 2.0f - 1.0f;
1257 float y0 = (float) yoffset / surface->height * 2.0f - 1.0f;
1258 float x1 = (float) (xoffset + upload_width) / surface->width * 2.0f - 1.0f;
1259 float y1 = (float) (yoffset + upload_height) / surface->height * 2.0f - 1.0f;
1260
1261 float *verts = NULL;
1262
1263 vbo.user_buffer = NULL;
1264 vbo.buffer = NULL;
1265 vbo.stride = 2 * sizeof(float);
1266
1267 u_upload_alloc(st->uploader, 0, 8 * sizeof(float), 4,
1268 &vbo.buffer_offset, &vbo.buffer, (void **) &verts);
1269 if (!verts)
1270 goto fail;
1271
1272 verts[0] = x0;
1273 verts[1] = y0;
1274 verts[2] = x0;
1275 verts[3] = y1;
1276 verts[4] = x1;
1277 verts[5] = y0;
1278 verts[6] = x1;
1279 verts[7] = y1;
1280
1281 u_upload_unmap(st->uploader);
1282
1283 velem.src_offset = 0;
1284 velem.instance_divisor = 0;
1285 velem.vertex_buffer_index = cso_get_aux_vertex_buffer_slot(cso);
1286 velem.src_format = PIPE_FORMAT_R32G32_FLOAT;
1287
1288 cso_set_vertex_elements(cso, 1, &velem);
1289
1290 cso_set_vertex_buffers(cso, velem.vertex_buffer_index, 1, &vbo);
1291
1292 pipe_resource_reference(&vbo.buffer, NULL);
1293 }
1294
1295 /* Upload constants */
1296 /* Note: the user buffer must be valid until draw time */
1297 struct {
1298 int32_t xoffset;
1299 int32_t yoffset;
1300 int32_t stride;
1301 int32_t image_size;
1302 } constants;
1303
1304 {
1305 struct pipe_constant_buffer cb;
1306
1307 constants.xoffset = -xoffset + skip_pixels;
1308 constants.yoffset = -yoffset;
1309 constants.stride = stride;
1310 constants.image_size = stride * image_height;
1311
1312 if (st->constbuf_uploader) {
1313 cb.buffer = NULL;
1314 cb.user_buffer = NULL;
1315 u_upload_data(st->constbuf_uploader, 0, sizeof(constants),
1316 ctx->Const.UniformBufferOffsetAlignment,
1317 &constants, &cb.buffer_offset, &cb.buffer);
1318 if (!cb.buffer)
1319 goto fail;
1320
1321 u_upload_unmap(st->constbuf_uploader);
1322 } else {
1323 cb.buffer = NULL;
1324 cb.user_buffer = &constants;
1325 cb.buffer_offset = 0;
1326 }
1327 cb.buffer_size = sizeof(constants);
1328
1329 cso_set_constant_buffer(cso, PIPE_SHADER_FRAGMENT, 0, &cb);
1330
1331 pipe_resource_reference(&cb.buffer, NULL);
1332 }
1333
1334 /* Framebuffer_state */
1335 {
1336 struct pipe_framebuffer_state fb;
1337 memset(&fb, 0, sizeof(fb));
1338 fb.width = surface->width;
1339 fb.height = surface->height;
1340 fb.nr_cbufs = 1;
1341 pipe_surface_reference(&fb.cbufs[0], surface);
1342
1343 cso_set_framebuffer(cso, &fb);
1344
1345 pipe_surface_reference(&fb.cbufs[0], NULL);
1346 }
1347
1348 cso_set_viewport_dims(cso, surface->width, surface->height, FALSE);
1349
1350 /* Blend state */
1351 cso_set_blend(cso, &st->pbo_upload.blend);
1352
1353 /* Depth/stencil/alpha state */
1354 {
1355 struct pipe_depth_stencil_alpha_state dsa;
1356 memset(&dsa, 0, sizeof(dsa));
1357 cso_set_depth_stencil_alpha(cso, &dsa);
1358 }
1359
1360 /* Rasterizer state */
1361 cso_set_rasterizer(cso, &st->pbo_upload.raster);
1362
1363 /* Set up the shaders */
1364 cso_set_vertex_shader_handle(cso, st->pbo_upload.vs);
1365
1366 cso_set_geometry_shader_handle(cso, depth != 1 ? st->pbo_upload.gs : NULL);
1367
1368 cso_set_tessctrl_shader_handle(cso, NULL);
1369
1370 cso_set_tesseval_shader_handle(cso, NULL);
1371
1372 cso_set_fragment_shader_handle(cso, st->pbo_upload.fs);
1373
1374 /* Disable stream output */
1375 cso_set_stream_outputs(cso, 0, NULL, 0);
1376
1377 if (depth == 1) {
1378 cso_draw_arrays(cso, PIPE_PRIM_TRIANGLE_STRIP, 0, 4);
1379 } else {
1380 cso_draw_arrays_instanced(cso, PIPE_PRIM_TRIANGLE_STRIP,
1381 0, 4, 0, depth);
1382 }
1383
1384 success = true;
1385
1386 fail:
1387 cso_restore_state(cso);
1388 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
1389
1390 return success;
1391 }
1392
1393 static bool
1394 try_pbo_upload(struct gl_context *ctx, GLuint dims,
1395 struct gl_texture_image *texImage,
1396 GLenum format, GLenum type,
1397 enum pipe_format dst_format,
1398 GLint xoffset, GLint yoffset, GLint zoffset,
1399 GLint width, GLint height, GLint depth,
1400 const void *pixels,
1401 const struct gl_pixelstore_attrib *unpack)
1402 {
1403 struct st_context *st = st_context(ctx);
1404 struct st_texture_image *stImage = st_texture_image(texImage);
1405 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1406 struct pipe_resource *texture = stImage->pt;
1407 struct pipe_context *pipe = st->pipe;
1408 struct pipe_screen *screen = pipe->screen;
1409 struct pipe_surface *surface = NULL;
1410 enum pipe_format src_format;
1411 const struct util_format_description *desc;
1412 GLenum gl_target = texImage->TexObject->Target;
1413 intptr_t buf_offset;
1414 unsigned bytes_per_pixel;
1415 unsigned stride, image_height;
1416 bool success;
1417
1418 if (!st->pbo_upload.enabled)
1419 return false;
1420
1421 /* From now on, we need the gallium representation of dimensions. */
1422 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1423 depth = height;
1424 height = 1;
1425 zoffset = yoffset;
1426 yoffset = 0;
1427 image_height = 1;
1428 } else {
1429 image_height = unpack->ImageHeight > 0 ? unpack->ImageHeight : height;
1430 }
1431
1432 if (depth != 1 && !st->pbo_upload.upload_layers)
1433 return false;
1434
1435 /* Choose the source format. Initially, we do so without checking driver
1436 * support at all because of the remapping we later perform and because
1437 * at least the Radeon driver actually supports some formats for texture
1438 * buffers which it doesn't support for regular textures. */
1439 src_format = st_choose_matching_format(st, 0, format, type, unpack->SwapBytes);
1440 if (!src_format) {
1441 return false;
1442 }
1443
1444 src_format = util_format_linear(src_format);
1445 desc = util_format_description(src_format);
1446
1447 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1448 return false;
1449
1450 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
1451 return false;
1452
1453 if (st->pbo_upload.rgba_only) {
1454 enum pipe_format orig_dst_format = dst_format;
1455
1456 if (!reinterpret_formats(&src_format, &dst_format)) {
1457 return false;
1458 }
1459
1460 if (dst_format != orig_dst_format &&
1461 !screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D, 0,
1462 PIPE_BIND_RENDER_TARGET)) {
1463 return false;
1464 }
1465 }
1466
1467 if (!src_format ||
1468 !screen->is_format_supported(screen, src_format, PIPE_BUFFER, 0,
1469 PIPE_BIND_SAMPLER_VIEW)) {
1470 return false;
1471 }
1472
1473 /* Check if the offset satisfies the alignment requirements */
1474 buf_offset = (intptr_t) pixels;
1475 bytes_per_pixel = desc->block.bits / 8;
1476
1477 if (buf_offset % bytes_per_pixel) {
1478 return false;
1479 }
1480
1481 /* Convert to texels */
1482 buf_offset = buf_offset / bytes_per_pixel;
1483
1484 /* Compute the stride, taking unpack->Alignment into account */
1485 {
1486 unsigned pixels_per_row = unpack->RowLength > 0 ?
1487 unpack->RowLength : width;
1488 unsigned bytes_per_row = pixels_per_row * bytes_per_pixel;
1489 unsigned remainder = bytes_per_row % unpack->Alignment;
1490 unsigned offset_rows;
1491
1492 if (remainder > 0)
1493 bytes_per_row += (unpack->Alignment - remainder);
1494
1495 if (bytes_per_row % bytes_per_pixel) {
1496 return false;
1497 }
1498
1499 stride = bytes_per_row / bytes_per_pixel;
1500
1501 offset_rows = unpack->SkipRows;
1502 if (dims == 3)
1503 offset_rows += image_height * unpack->SkipImages;
1504
1505 buf_offset += unpack->SkipPixels + stride * offset_rows;
1506 }
1507
1508 /* Set up the surface */
1509 {
1510 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1511 unsigned max_layer = util_max_layer(texture, level);
1512
1513 zoffset += texImage->Face + texImage->TexObject->MinLayer;
1514
1515 struct pipe_surface templ;
1516 memset(&templ, 0, sizeof(templ));
1517 templ.format = dst_format;
1518 templ.u.tex.level = level;
1519 templ.u.tex.first_layer = MIN2(zoffset, max_layer);
1520 templ.u.tex.last_layer = MIN2(zoffset + depth - 1, max_layer);
1521
1522 surface = pipe->create_surface(pipe, texture, &templ);
1523 if (!surface)
1524 return false;
1525 }
1526
1527 success = try_pbo_upload_common(ctx, surface,
1528 xoffset, yoffset, width, height,
1529 st_buffer_object(unpack->BufferObj)->buffer,
1530 src_format,
1531 buf_offset,
1532 bytes_per_pixel, stride, image_height);
1533
1534 pipe_surface_reference(&surface, NULL);
1535
1536 return success;
1537 }
1538
1539 static void
1540 st_TexSubImage(struct gl_context *ctx, GLuint dims,
1541 struct gl_texture_image *texImage,
1542 GLint xoffset, GLint yoffset, GLint zoffset,
1543 GLint width, GLint height, GLint depth,
1544 GLenum format, GLenum type, const void *pixels,
1545 const struct gl_pixelstore_attrib *unpack)
1546 {
1547 struct st_context *st = st_context(ctx);
1548 struct st_texture_image *stImage = st_texture_image(texImage);
1549 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1550 struct pipe_context *pipe = st->pipe;
1551 struct pipe_screen *screen = pipe->screen;
1552 struct pipe_resource *dst = stImage->pt;
1553 struct pipe_resource *src = NULL;
1554 struct pipe_resource src_templ;
1555 struct pipe_transfer *transfer;
1556 struct pipe_blit_info blit;
1557 enum pipe_format src_format, dst_format;
1558 mesa_format mesa_src_format;
1559 GLenum gl_target = texImage->TexObject->Target;
1560 unsigned bind;
1561 GLubyte *map;
1562 unsigned dstz = texImage->Face + texImage->TexObject->MinLayer;
1563 unsigned dst_level = 0;
1564
1565 if (stObj->pt == stImage->pt)
1566 dst_level = texImage->TexObject->MinLevel + texImage->Level;
1567
1568 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1569 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1570
1571 if (!dst)
1572 goto fallback;
1573
1574 /* Try transfer_inline_write, which should be the fastest memcpy path. */
1575 if (pixels &&
1576 !_mesa_is_bufferobj(unpack->BufferObj) &&
1577 _mesa_texstore_can_use_memcpy(ctx, texImage->_BaseFormat,
1578 texImage->TexFormat, format, type,
1579 unpack)) {
1580 struct pipe_box box;
1581 unsigned stride, layer_stride;
1582 void *data;
1583
1584 stride = _mesa_image_row_stride(unpack, width, format, type);
1585 layer_stride = _mesa_image_image_stride(unpack, width, height, format,
1586 type);
1587 data = _mesa_image_address(dims, unpack, pixels, width, height, format,
1588 type, 0, 0, 0);
1589
1590 /* Convert to Gallium coordinates. */
1591 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1592 zoffset = yoffset;
1593 yoffset = 0;
1594 depth = height;
1595 height = 1;
1596 layer_stride = stride;
1597 }
1598
1599 u_box_3d(xoffset, yoffset, zoffset + dstz, width, height, depth, &box);
1600 pipe->transfer_inline_write(pipe, dst, dst_level, 0,
1601 &box, data, stride, layer_stride);
1602 return;
1603 }
1604
1605 if (!st->prefer_blit_based_texture_transfer) {
1606 goto fallback;
1607 }
1608
1609 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1610 * blit implementation in some drivers. */
1611 if (format == GL_DEPTH_STENCIL) {
1612 goto fallback;
1613 }
1614
1615 /* If the base internal format and the texture format don't match,
1616 * we can't use blit-based TexSubImage. */
1617 if (texImage->_BaseFormat !=
1618 _mesa_get_format_base_format(texImage->TexFormat)) {
1619 goto fallback;
1620 }
1621
1622
1623 /* See if the destination format is supported. */
1624 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1625 bind = PIPE_BIND_DEPTH_STENCIL;
1626 else
1627 bind = PIPE_BIND_RENDER_TARGET;
1628
1629 /* For luminance and intensity, only the red channel is stored
1630 * in the destination. */
1631 dst_format = util_format_linear(dst->format);
1632 dst_format = util_format_luminance_to_red(dst_format);
1633 dst_format = util_format_intensity_to_red(dst_format);
1634
1635 if (!dst_format ||
1636 !screen->is_format_supported(screen, dst_format, dst->target,
1637 dst->nr_samples, bind)) {
1638 goto fallback;
1639 }
1640
1641 if (_mesa_is_bufferobj(unpack->BufferObj)) {
1642 if (try_pbo_upload(ctx, dims, texImage, format, type, dst_format,
1643 xoffset, yoffset, zoffset,
1644 width, height, depth, pixels, unpack))
1645 return;
1646 }
1647
1648 /* See if the texture format already matches the format and type,
1649 * in which case the memcpy-based fast path will likely be used and
1650 * we don't have to blit. */
1651 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1652 type, unpack->SwapBytes, NULL)) {
1653 goto fallback;
1654 }
1655
1656 /* Choose the source format. */
1657 src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
1658 format, type, unpack->SwapBytes);
1659 if (!src_format) {
1660 goto fallback;
1661 }
1662
1663 mesa_src_format = st_pipe_format_to_mesa_format(src_format);
1664
1665 /* There is no reason to do this if we cannot use memcpy for the temporary
1666 * source texture at least. This also takes transfer ops into account,
1667 * etc. */
1668 if (!_mesa_texstore_can_use_memcpy(ctx,
1669 _mesa_get_format_base_format(mesa_src_format),
1670 mesa_src_format, format, type, unpack)) {
1671 goto fallback;
1672 }
1673
1674 /* TexSubImage only sets a single cubemap face. */
1675 if (gl_target == GL_TEXTURE_CUBE_MAP) {
1676 gl_target = GL_TEXTURE_2D;
1677 }
1678 /* TexSubImage can specify subsets of cube map array faces
1679 * so we need to upload via 2D array instead */
1680 if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
1681 gl_target = GL_TEXTURE_2D_ARRAY;
1682 }
1683
1684 /* Initialize the source texture description. */
1685 memset(&src_templ, 0, sizeof(src_templ));
1686 src_templ.target = gl_target_to_pipe(gl_target);
1687 src_templ.format = src_format;
1688 src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
1689 src_templ.usage = PIPE_USAGE_STAGING;
1690
1691 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1692 &src_templ.width0, &src_templ.height0,
1693 &src_templ.depth0, &src_templ.array_size);
1694
1695 /* Check for NPOT texture support. */
1696 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
1697 (!util_is_power_of_two(src_templ.width0) ||
1698 !util_is_power_of_two(src_templ.height0) ||
1699 !util_is_power_of_two(src_templ.depth0))) {
1700 goto fallback;
1701 }
1702
1703 /* Create the source texture. */
1704 src = screen->resource_create(screen, &src_templ);
1705 if (!src) {
1706 goto fallback;
1707 }
1708
1709 /* Map source pixels. */
1710 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1711 format, type, pixels, unpack,
1712 "glTexSubImage");
1713 if (!pixels) {
1714 /* This is a GL error. */
1715 pipe_resource_reference(&src, NULL);
1716 return;
1717 }
1718
1719 /* From now on, we need the gallium representation of dimensions. */
1720 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1721 zoffset = yoffset;
1722 yoffset = 0;
1723 depth = height;
1724 height = 1;
1725 }
1726
1727 map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
1728 width, height, depth, &transfer);
1729 if (!map) {
1730 _mesa_unmap_teximage_pbo(ctx, unpack);
1731 pipe_resource_reference(&src, NULL);
1732 goto fallback;
1733 }
1734
1735 /* Upload pixels (just memcpy). */
1736 {
1737 const uint bytesPerRow = width * util_format_get_blocksize(src_format);
1738 GLuint row, slice;
1739
1740 for (slice = 0; slice < (unsigned) depth; slice++) {
1741 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1742 /* 1D array textures.
1743 * We need to convert gallium coords to GL coords.
1744 */
1745 void *src = _mesa_image_address2d(unpack, pixels,
1746 width, depth, format,
1747 type, slice, 0);
1748 memcpy(map, src, bytesPerRow);
1749 }
1750 else {
1751 ubyte *slice_map = map;
1752
1753 for (row = 0; row < (unsigned) height; row++) {
1754 void *src = _mesa_image_address(dims, unpack, pixels,
1755 width, height, format,
1756 type, slice, row, 0);
1757 memcpy(slice_map, src, bytesPerRow);
1758 slice_map += transfer->stride;
1759 }
1760 }
1761 map += transfer->layer_stride;
1762 }
1763 }
1764
1765 pipe_transfer_unmap(pipe, transfer);
1766 _mesa_unmap_teximage_pbo(ctx, unpack);
1767
1768 /* Blit. */
1769 memset(&blit, 0, sizeof(blit));
1770 blit.src.resource = src;
1771 blit.src.level = 0;
1772 blit.src.format = src_format;
1773 blit.dst.resource = dst;
1774 blit.dst.level = dst_level;
1775 blit.dst.format = dst_format;
1776 blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
1777 blit.dst.box.x = xoffset;
1778 blit.dst.box.y = yoffset;
1779 blit.dst.box.z = zoffset + dstz;
1780 blit.src.box.width = blit.dst.box.width = width;
1781 blit.src.box.height = blit.dst.box.height = height;
1782 blit.src.box.depth = blit.dst.box.depth = depth;
1783 blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
1784 blit.filter = PIPE_TEX_FILTER_NEAREST;
1785 blit.scissor_enable = FALSE;
1786
1787 st->pipe->blit(st->pipe, &blit);
1788
1789 pipe_resource_reference(&src, NULL);
1790 return;
1791
1792 fallback:
1793 _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
1794 width, height, depth, format, type, pixels,
1795 unpack);
1796 }
1797
1798 static void
1799 st_TexImage(struct gl_context * ctx, GLuint dims,
1800 struct gl_texture_image *texImage,
1801 GLenum format, GLenum type, const void *pixels,
1802 const struct gl_pixelstore_attrib *unpack)
1803 {
1804 assert(dims == 1 || dims == 2 || dims == 3);
1805
1806 prep_teximage(ctx, texImage, format, type);
1807
1808 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
1809 return;
1810
1811 /* allocate storage for texture data */
1812 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
1813 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1814 return;
1815 }
1816
1817 st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
1818 texImage->Width, texImage->Height, texImage->Depth,
1819 format, type, pixels, unpack);
1820 }
1821
1822
1823 static void
1824 st_CompressedTexSubImage(struct gl_context *ctx, GLuint dims,
1825 struct gl_texture_image *texImage,
1826 GLint x, GLint y, GLint z,
1827 GLsizei w, GLsizei h, GLsizei d,
1828 GLenum format, GLsizei imageSize, const void *data)
1829 {
1830 struct st_context *st = st_context(ctx);
1831 struct st_texture_image *stImage = st_texture_image(texImage);
1832 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1833 struct pipe_resource *texture = stImage->pt;
1834 struct pipe_context *pipe = st->pipe;
1835 struct pipe_screen *screen = pipe->screen;
1836 struct pipe_resource *dst = stImage->pt;
1837 struct pipe_surface *surface = NULL;
1838 struct compressed_pixelstore store;
1839 enum pipe_format copy_format;
1840 unsigned bytes_per_block;
1841 unsigned bw, bh;
1842 intptr_t buf_offset;
1843 bool success = false;
1844
1845 /* Check basic pre-conditions for PBO upload */
1846 if (!st->prefer_blit_based_texture_transfer) {
1847 goto fallback;
1848 }
1849
1850 if (!_mesa_is_bufferobj(ctx->Unpack.BufferObj))
1851 goto fallback;
1852
1853 if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
1854 (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
1855 /* ETC isn't supported and is represented by uncompressed formats. */
1856 goto fallback;
1857 }
1858
1859 if (!dst) {
1860 goto fallback;
1861 }
1862
1863 if (!st->pbo_upload.enabled ||
1864 !screen->get_param(screen, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS)) {
1865 goto fallback;
1866 }
1867
1868 /* Choose the pipe format for the upload. */
1869 bytes_per_block = util_format_get_blocksize(dst->format);
1870 bw = util_format_get_blockwidth(dst->format);
1871 bh = util_format_get_blockheight(dst->format);
1872
1873 switch (bytes_per_block) {
1874 case 8:
1875 copy_format = PIPE_FORMAT_R16G16B16A16_UINT;
1876 break;
1877 case 16:
1878 copy_format = PIPE_FORMAT_R32G32B32A32_UINT;
1879 break;
1880 default:
1881 goto fallback;
1882 }
1883
1884 if (!screen->is_format_supported(screen, copy_format, PIPE_BUFFER, 0,
1885 PIPE_BIND_SAMPLER_VIEW)) {
1886 goto fallback;
1887 }
1888
1889 if (!screen->is_format_supported(screen, copy_format, dst->target,
1890 dst->nr_samples, PIPE_BIND_RENDER_TARGET)) {
1891 goto fallback;
1892 }
1893
1894 /* Interpret the pixelstore settings. */
1895 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat, w, h, d,
1896 &ctx->Unpack, &store);
1897 assert(store.CopyBytesPerRow % bytes_per_block == 0);
1898 assert(store.SkipBytes % bytes_per_block == 0);
1899
1900 /* Compute the offset into the buffer */
1901 buf_offset = (intptr_t)data + store.SkipBytes;
1902
1903 if (buf_offset % bytes_per_block) {
1904 goto fallback;
1905 }
1906
1907 buf_offset = buf_offset / bytes_per_block;
1908
1909 /* Set up the surface. */
1910 {
1911 unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
1912 unsigned max_layer = util_max_layer(texture, level);
1913
1914 z += texImage->Face + texImage->TexObject->MinLayer;
1915
1916 struct pipe_surface templ;
1917 memset(&templ, 0, sizeof(templ));
1918 templ.format = copy_format;
1919 templ.u.tex.level = level;
1920 templ.u.tex.first_layer = MIN2(z, max_layer);
1921 templ.u.tex.last_layer = MIN2(z + d - 1, max_layer);
1922
1923 surface = pipe->create_surface(pipe, texture, &templ);
1924 if (!surface)
1925 goto fallback;
1926 }
1927
1928 success = try_pbo_upload_common(ctx, surface,
1929 x / bw, y / bh,
1930 store.CopyBytesPerRow / bytes_per_block,
1931 store.CopyRowsPerSlice,
1932 st_buffer_object(ctx->Unpack.BufferObj)->buffer,
1933 copy_format,
1934 buf_offset,
1935 bytes_per_block,
1936 store.TotalBytesPerRow / bytes_per_block,
1937 store.TotalRowsPerSlice);
1938
1939 pipe_surface_reference(&surface, NULL);
1940
1941 if (success)
1942 return;
1943
1944 fallback:
1945 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
1946 x, y, z, w, h, d,
1947 format, imageSize, data);
1948 }
1949
1950 static void
1951 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
1952 struct gl_texture_image *texImage,
1953 GLsizei imageSize, const void *data)
1954 {
1955 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
1956
1957 /* only 2D and 3D compressed images are supported at this time */
1958 if (dims == 1) {
1959 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
1960 return;
1961 }
1962
1963 /* This is pretty simple, because unlike the general texstore path we don't
1964 * have to worry about the usual image unpacking or image transfer
1965 * operations.
1966 */
1967 assert(texImage);
1968 assert(texImage->Width > 0);
1969 assert(texImage->Height > 0);
1970 assert(texImage->Depth > 0);
1971
1972 /* allocate storage for texture data */
1973 if (!st_AllocTextureImageBuffer(ctx, texImage)) {
1974 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
1975 return;
1976 }
1977
1978 st_CompressedTexSubImage(ctx, dims, texImage,
1979 0, 0, 0,
1980 texImage->Width, texImage->Height, texImage->Depth,
1981 texImage->TexFormat,
1982 imageSize, data);
1983 }
1984
1985
1986
1987
1988 /**
1989 * Called via ctx->Driver.GetTexSubImage()
1990 *
1991 * This uses a blit to copy the texture to a texture format which matches
1992 * the format and type combo and then a fast read-back is done using memcpy.
1993 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1994 * a format which matches the swizzling.
1995 *
1996 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1997 *
1998 * NOTE: Drivers usually do a blit to convert between tiled and linear
1999 * texture layouts during texture uploads/downloads, so the blit
2000 * we do here should be free in such cases.
2001 */
2002 static void
2003 st_GetTexSubImage(struct gl_context * ctx,
2004 GLint xoffset, GLint yoffset, GLint zoffset,
2005 GLsizei width, GLsizei height, GLint depth,
2006 GLenum format, GLenum type, void * pixels,
2007 struct gl_texture_image *texImage)
2008 {
2009 struct st_context *st = st_context(ctx);
2010 struct pipe_context *pipe = st->pipe;
2011 struct pipe_screen *screen = pipe->screen;
2012 struct st_texture_image *stImage = st_texture_image(texImage);
2013 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2014 struct pipe_resource *src = stObj->pt;
2015 struct pipe_resource *dst = NULL;
2016 struct pipe_resource dst_templ;
2017 enum pipe_format dst_format, src_format;
2018 mesa_format mesa_format;
2019 GLenum gl_target = texImage->TexObject->Target;
2020 enum pipe_texture_target pipe_target;
2021 struct pipe_blit_info blit;
2022 unsigned bind = PIPE_BIND_TRANSFER_READ;
2023 struct pipe_transfer *tex_xfer;
2024 ubyte *map = NULL;
2025 boolean done = FALSE;
2026
2027 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2028 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2029
2030 if (!st->prefer_blit_based_texture_transfer &&
2031 !_mesa_is_format_compressed(texImage->TexFormat)) {
2032 /* Try to avoid the fallback if we're doing texture decompression here */
2033 goto fallback;
2034 }
2035
2036 /* Handle non-finalized textures. */
2037 if (!stImage->pt || stImage->pt != stObj->pt || !src) {
2038 goto fallback;
2039 }
2040
2041 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
2042 * due to an incomplete stencil blit implementation in some drivers. */
2043 if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
2044 goto fallback;
2045 }
2046
2047 /* If the base internal format and the texture format don't match, we have
2048 * to fall back to _mesa_GetTexImage_sw. */
2049 if (texImage->_BaseFormat !=
2050 _mesa_get_format_base_format(texImage->TexFormat)) {
2051 goto fallback;
2052 }
2053
2054 /* See if the texture format already matches the format and type,
2055 * in which case the memcpy-based fast path will be used. */
2056 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
2057 type, ctx->Pack.SwapBytes, NULL)) {
2058 goto fallback;
2059 }
2060
2061 /* Convert the source format to what is expected by GetTexImage
2062 * and see if it's supported.
2063 *
2064 * This only applies to glGetTexImage:
2065 * - Luminance must be returned as (L,0,0,1).
2066 * - Luminance alpha must be returned as (L,0,0,A).
2067 * - Intensity must be returned as (I,0,0,1)
2068 */
2069 if (stObj->surface_based)
2070 src_format = util_format_linear(stObj->surface_format);
2071 else
2072 src_format = util_format_linear(src->format);
2073 src_format = util_format_luminance_to_red(src_format);
2074 src_format = util_format_intensity_to_red(src_format);
2075
2076 if (!src_format ||
2077 !screen->is_format_supported(screen, src_format, src->target,
2078 src->nr_samples,
2079 PIPE_BIND_SAMPLER_VIEW)) {
2080 goto fallback;
2081 }
2082
2083 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
2084 bind |= PIPE_BIND_DEPTH_STENCIL;
2085 else
2086 bind |= PIPE_BIND_RENDER_TARGET;
2087
2088 /* GetTexImage only returns a single face for cubemaps. */
2089 if (gl_target == GL_TEXTURE_CUBE_MAP) {
2090 gl_target = GL_TEXTURE_2D;
2091 }
2092 pipe_target = gl_target_to_pipe(gl_target);
2093
2094 /* Choose the destination format by finding the best match
2095 * for the format+type combo. */
2096 dst_format = st_choose_matching_format(st, bind, format, type,
2097 ctx->Pack.SwapBytes);
2098
2099 if (dst_format == PIPE_FORMAT_NONE) {
2100 GLenum dst_glformat;
2101
2102 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2103 * where decompression with a blit is always preferred. */
2104 if (!util_format_is_compressed(src->format)) {
2105 goto fallback;
2106 }
2107
2108 /* Set the appropriate format for the decompressed texture.
2109 * Luminance and sRGB formats shouldn't appear here.*/
2110 switch (src_format) {
2111 case PIPE_FORMAT_DXT1_RGB:
2112 case PIPE_FORMAT_DXT1_RGBA:
2113 case PIPE_FORMAT_DXT3_RGBA:
2114 case PIPE_FORMAT_DXT5_RGBA:
2115 case PIPE_FORMAT_RGTC1_UNORM:
2116 case PIPE_FORMAT_RGTC2_UNORM:
2117 case PIPE_FORMAT_ETC1_RGB8:
2118 case PIPE_FORMAT_BPTC_RGBA_UNORM:
2119 dst_glformat = GL_RGBA8;
2120 break;
2121 case PIPE_FORMAT_RGTC1_SNORM:
2122 case PIPE_FORMAT_RGTC2_SNORM:
2123 if (!ctx->Extensions.EXT_texture_snorm)
2124 goto fallback;
2125 dst_glformat = GL_RGBA8_SNORM;
2126 break;
2127 case PIPE_FORMAT_BPTC_RGB_FLOAT:
2128 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
2129 if (!ctx->Extensions.ARB_texture_float)
2130 goto fallback;
2131 dst_glformat = GL_RGBA32F;
2132 break;
2133 default:
2134 assert(0);
2135 goto fallback;
2136 }
2137
2138 dst_format = st_choose_format(st, dst_glformat, format, type,
2139 pipe_target, 0, bind, FALSE);
2140
2141 if (dst_format == PIPE_FORMAT_NONE) {
2142 /* unable to get an rgba format!?! */
2143 goto fallback;
2144 }
2145 }
2146
2147 /* create the destination texture of size (width X height X depth) */
2148 memset(&dst_templ, 0, sizeof(dst_templ));
2149 dst_templ.target = pipe_target;
2150 dst_templ.format = dst_format;
2151 dst_templ.bind = bind;
2152 dst_templ.usage = PIPE_USAGE_STAGING;
2153
2154 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
2155 &dst_templ.width0, &dst_templ.height0,
2156 &dst_templ.depth0, &dst_templ.array_size);
2157
2158 dst = screen->resource_create(screen, &dst_templ);
2159 if (!dst) {
2160 goto fallback;
2161 }
2162
2163 /* From now on, we need the gallium representation of dimensions. */
2164 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2165 zoffset = yoffset;
2166 yoffset = 0;
2167 depth = height;
2168 height = 1;
2169 }
2170
2171 assert(texImage->Face == 0 ||
2172 texImage->TexObject->MinLayer == 0 ||
2173 zoffset == 0);
2174
2175 memset(&blit, 0, sizeof(blit));
2176 blit.src.resource = src;
2177 blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
2178 blit.src.format = src_format;
2179 blit.dst.resource = dst;
2180 blit.dst.level = 0;
2181 blit.dst.format = dst->format;
2182 blit.src.box.x = xoffset;
2183 blit.dst.box.x = 0;
2184 blit.src.box.y = yoffset;
2185 blit.dst.box.y = 0;
2186 blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
2187 blit.dst.box.z = 0;
2188 blit.src.box.width = blit.dst.box.width = width;
2189 blit.src.box.height = blit.dst.box.height = height;
2190 blit.src.box.depth = blit.dst.box.depth = depth;
2191 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
2192 blit.filter = PIPE_TEX_FILTER_NEAREST;
2193 blit.scissor_enable = FALSE;
2194
2195 /* blit/render/decompress */
2196 st->pipe->blit(st->pipe, &blit);
2197
2198 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
2199
2200 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
2201 0, 0, 0, width, height, depth, &tex_xfer);
2202 if (!map) {
2203 goto end;
2204 }
2205
2206 mesa_format = st_pipe_format_to_mesa_format(dst_format);
2207
2208 /* copy/pack data into user buffer */
2209 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
2210 ctx->Pack.SwapBytes, NULL)) {
2211 /* memcpy */
2212 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
2213 GLuint row, slice;
2214
2215 for (slice = 0; slice < depth; slice++) {
2216 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2217 /* 1D array textures.
2218 * We need to convert gallium coords to GL coords.
2219 */
2220 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2221 width, depth, format,
2222 type, 0, slice, 0);
2223 memcpy(dest, map, bytesPerRow);
2224 }
2225 else {
2226 ubyte *slice_map = map;
2227
2228 for (row = 0; row < height; row++) {
2229 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2230 width, height, format,
2231 type, slice, row, 0);
2232 memcpy(dest, slice_map, bytesPerRow);
2233 slice_map += tex_xfer->stride;
2234 }
2235 }
2236 map += tex_xfer->layer_stride;
2237 }
2238 }
2239 else {
2240 /* format translation via floats */
2241 GLuint row, slice;
2242 GLfloat *rgba;
2243 uint32_t dstMesaFormat;
2244 int dstStride, srcStride;
2245
2246 assert(util_format_is_compressed(src->format));
2247
2248 rgba = malloc(width * 4 * sizeof(GLfloat));
2249 if (!rgba) {
2250 goto end;
2251 }
2252
2253 if (ST_DEBUG & DEBUG_FALLBACK)
2254 debug_printf("%s: fallback format translation\n", __func__);
2255
2256 dstMesaFormat = _mesa_format_from_format_and_type(format, type);
2257 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
2258 srcStride = 4 * width * sizeof(GLfloat);
2259 for (slice = 0; slice < depth; slice++) {
2260 if (gl_target == GL_TEXTURE_1D_ARRAY) {
2261 /* 1D array textures.
2262 * We need to convert gallium coords to GL coords.
2263 */
2264 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2265 width, depth, format,
2266 type, 0, slice, 0);
2267
2268 /* get float[4] rgba row from surface */
2269 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
2270 dst_format, rgba);
2271
2272 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2273 rgba, RGBA32_FLOAT, srcStride,
2274 width, 1, NULL);
2275 }
2276 else {
2277 for (row = 0; row < height; row++) {
2278 void *dest = _mesa_image_address3d(&ctx->Pack, pixels,
2279 width, height, format,
2280 type, slice, row, 0);
2281
2282 /* get float[4] rgba row from surface */
2283 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
2284 dst_format, rgba);
2285
2286 _mesa_format_convert(dest, dstMesaFormat, dstStride,
2287 rgba, RGBA32_FLOAT, srcStride,
2288 width, 1, NULL);
2289 }
2290 }
2291 map += tex_xfer->layer_stride;
2292 }
2293
2294 free(rgba);
2295 }
2296 done = TRUE;
2297
2298 end:
2299 if (map)
2300 pipe_transfer_unmap(pipe, tex_xfer);
2301
2302 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
2303 pipe_resource_reference(&dst, NULL);
2304
2305 fallback:
2306 if (!done) {
2307 _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
2308 width, height, depth,
2309 format, type, pixels, texImage);
2310 }
2311 }
2312
2313
2314 /**
2315 * Do a CopyTexSubImage operation using a read transfer from the source,
2316 * a write transfer to the destination and get_tile()/put_tile() to access
2317 * the pixels/texels.
2318 *
2319 * Note: srcY=0=TOP of renderbuffer
2320 */
2321 static void
2322 fallback_copy_texsubimage(struct gl_context *ctx,
2323 struct st_renderbuffer *strb,
2324 struct st_texture_image *stImage,
2325 GLenum baseFormat,
2326 GLint destX, GLint destY, GLint slice,
2327 GLint srcX, GLint srcY,
2328 GLsizei width, GLsizei height)
2329 {
2330 struct st_context *st = st_context(ctx);
2331 struct pipe_context *pipe = st->pipe;
2332 struct pipe_transfer *src_trans;
2333 GLubyte *texDest;
2334 enum pipe_transfer_usage transfer_usage;
2335 void *map;
2336 unsigned dst_width = width;
2337 unsigned dst_height = height;
2338 unsigned dst_depth = 1;
2339 struct pipe_transfer *transfer;
2340
2341 if (ST_DEBUG & DEBUG_FALLBACK)
2342 debug_printf("%s: fallback processing\n", __func__);
2343
2344 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2345 srcY = strb->Base.Height - srcY - height;
2346 }
2347
2348 map = pipe_transfer_map(pipe,
2349 strb->texture,
2350 strb->surface->u.tex.level,
2351 strb->surface->u.tex.first_layer,
2352 PIPE_TRANSFER_READ,
2353 srcX, srcY,
2354 width, height, &src_trans);
2355
2356 if ((baseFormat == GL_DEPTH_COMPONENT ||
2357 baseFormat == GL_DEPTH_STENCIL) &&
2358 util_format_is_depth_and_stencil(stImage->pt->format))
2359 transfer_usage = PIPE_TRANSFER_READ_WRITE;
2360 else
2361 transfer_usage = PIPE_TRANSFER_WRITE;
2362
2363 texDest = st_texture_image_map(st, stImage, transfer_usage,
2364 destX, destY, slice,
2365 dst_width, dst_height, dst_depth,
2366 &transfer);
2367
2368 if (baseFormat == GL_DEPTH_COMPONENT ||
2369 baseFormat == GL_DEPTH_STENCIL) {
2370 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
2371 ctx->Pixel.DepthBias != 0.0F);
2372 GLint row, yStep;
2373 uint *data;
2374
2375 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2376 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2377 srcY = height - 1;
2378 yStep = -1;
2379 }
2380 else {
2381 srcY = 0;
2382 yStep = 1;
2383 }
2384
2385 data = malloc(width * sizeof(uint));
2386
2387 if (data) {
2388 /* To avoid a large temp memory allocation, do copy row by row */
2389 for (row = 0; row < height; row++, srcY += yStep) {
2390 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
2391 if (scaleOrBias) {
2392 _mesa_scale_and_bias_depth_uint(ctx, width, data);
2393 }
2394
2395 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2396 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
2397 0, 0, width, 1, data);
2398 }
2399 else {
2400 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
2401 }
2402 }
2403 }
2404 else {
2405 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
2406 }
2407
2408 free(data);
2409 }
2410 else {
2411 /* RGBA format */
2412 GLfloat *tempSrc =
2413 malloc(width * height * 4 * sizeof(GLfloat));
2414
2415 if (tempSrc && texDest) {
2416 const GLint dims = 2;
2417 GLint dstRowStride;
2418 struct gl_texture_image *texImage = &stImage->base;
2419 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
2420
2421 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
2422 unpack.Invert = GL_TRUE;
2423 }
2424
2425 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
2426 dstRowStride = transfer->layer_stride;
2427 }
2428 else {
2429 dstRowStride = transfer->stride;
2430 }
2431
2432 /* get float/RGBA image from framebuffer */
2433 /* XXX this usually involves a lot of int/float conversion.
2434 * try to avoid that someday.
2435 */
2436 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
2437 util_format_linear(strb->texture->format),
2438 tempSrc);
2439
2440 /* Store into texture memory.
2441 * Note that this does some special things such as pixel transfer
2442 * ops and format conversion. In particular, if the dest tex format
2443 * is actually RGBA but the user created the texture as GL_RGB we
2444 * need to fill-in/override the alpha channel with 1.0.
2445 */
2446 _mesa_texstore(ctx, dims,
2447 texImage->_BaseFormat,
2448 texImage->TexFormat,
2449 dstRowStride,
2450 &texDest,
2451 width, height, 1,
2452 GL_RGBA, GL_FLOAT, tempSrc, /* src */
2453 &unpack);
2454 }
2455 else {
2456 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
2457 }
2458
2459 free(tempSrc);
2460 }
2461
2462 st_texture_image_unmap(st, stImage, slice);
2463 pipe->transfer_unmap(pipe, src_trans);
2464 }
2465
2466
2467 /**
2468 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2469 * Note that the region to copy has already been clipped so we know we
2470 * won't read from outside the source renderbuffer's bounds.
2471 *
2472 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2473 */
2474 static void
2475 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
2476 struct gl_texture_image *texImage,
2477 GLint destX, GLint destY, GLint slice,
2478 struct gl_renderbuffer *rb,
2479 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
2480 {
2481 struct st_texture_image *stImage = st_texture_image(texImage);
2482 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
2483 struct st_renderbuffer *strb = st_renderbuffer(rb);
2484 struct st_context *st = st_context(ctx);
2485 struct pipe_context *pipe = st->pipe;
2486 struct pipe_screen *screen = pipe->screen;
2487 struct pipe_blit_info blit;
2488 enum pipe_format dst_format;
2489 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
2490 unsigned bind;
2491 GLint srcY0, srcY1;
2492
2493 assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
2494 texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
2495
2496 if (!strb || !strb->surface || !stImage->pt) {
2497 debug_printf("%s: null strb or stImage\n", __func__);
2498 return;
2499 }
2500
2501 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
2502 texImage->TexFormat)) {
2503 goto fallback;
2504 }
2505
2506 /* The base internal format must match the mesa format, so make sure
2507 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2508 */
2509 if (texImage->_BaseFormat !=
2510 _mesa_get_format_base_format(texImage->TexFormat) ||
2511 rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
2512 goto fallback;
2513 }
2514
2515 /* Choose the destination format to match the TexImage behavior. */
2516 dst_format = util_format_linear(stImage->pt->format);
2517 dst_format = util_format_luminance_to_red(dst_format);
2518 dst_format = util_format_intensity_to_red(dst_format);
2519
2520 /* See if the destination format is supported. */
2521 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
2522 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
2523 bind = PIPE_BIND_DEPTH_STENCIL;
2524 }
2525 else {
2526 bind = PIPE_BIND_RENDER_TARGET;
2527 }
2528
2529 if (!dst_format ||
2530 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
2531 stImage->pt->nr_samples, bind)) {
2532 goto fallback;
2533 }
2534
2535 /* Y flipping for the main framebuffer. */
2536 if (do_flip) {
2537 srcY1 = strb->Base.Height - srcY - height;
2538 srcY0 = srcY1 + height;
2539 }
2540 else {
2541 srcY0 = srcY;
2542 srcY1 = srcY0 + height;
2543 }
2544
2545 /* Blit the texture.
2546 * This supports flipping, format conversions, and downsampling.
2547 */
2548 memset(&blit, 0, sizeof(blit));
2549 blit.src.resource = strb->texture;
2550 blit.src.format = util_format_linear(strb->surface->format);
2551 blit.src.level = strb->surface->u.tex.level;
2552 blit.src.box.x = srcX;
2553 blit.src.box.y = srcY0;
2554 blit.src.box.z = strb->surface->u.tex.first_layer;
2555 blit.src.box.width = width;
2556 blit.src.box.height = srcY1 - srcY0;
2557 blit.src.box.depth = 1;
2558 blit.dst.resource = stImage->pt;
2559 blit.dst.format = dst_format;
2560 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
2561 blit.dst.box.x = destX;
2562 blit.dst.box.y = destY;
2563 blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
2564 blit.dst.box.width = width;
2565 blit.dst.box.height = height;
2566 blit.dst.box.depth = 1;
2567 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
2568 blit.filter = PIPE_TEX_FILTER_NEAREST;
2569 pipe->blit(pipe, &blit);
2570 return;
2571
2572 fallback:
2573 /* software fallback */
2574 fallback_copy_texsubimage(ctx,
2575 strb, stImage, texImage->_BaseFormat,
2576 destX, destY, slice,
2577 srcX, srcY, width, height);
2578 }
2579
2580
2581 /**
2582 * Copy image data from stImage into the texture object 'stObj' at level
2583 * 'dstLevel'.
2584 */
2585 static void
2586 copy_image_data_to_texture(struct st_context *st,
2587 struct st_texture_object *stObj,
2588 GLuint dstLevel,
2589 struct st_texture_image *stImage)
2590 {
2591 /* debug checks */
2592 {
2593 const struct gl_texture_image *dstImage =
2594 stObj->base.Image[stImage->base.Face][dstLevel];
2595 assert(dstImage);
2596 assert(dstImage->Width == stImage->base.Width);
2597 assert(dstImage->Height == stImage->base.Height);
2598 assert(dstImage->Depth == stImage->base.Depth);
2599 }
2600
2601 if (stImage->pt) {
2602 /* Copy potentially with the blitter:
2603 */
2604 GLuint src_level;
2605 if (stImage->pt->last_level == 0)
2606 src_level = 0;
2607 else
2608 src_level = stImage->base.Level;
2609
2610 assert(src_level <= stImage->pt->last_level);
2611 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
2612 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
2613 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
2614 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
2615 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
2616 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
2617
2618 st_texture_image_copy(st->pipe,
2619 stObj->pt, dstLevel, /* dest texture, level */
2620 stImage->pt, src_level, /* src texture, level */
2621 stImage->base.Face);
2622
2623 pipe_resource_reference(&stImage->pt, NULL);
2624 }
2625 pipe_resource_reference(&stImage->pt, stObj->pt);
2626 }
2627
2628
2629 /**
2630 * Called during state validation. When this function is finished,
2631 * the texture object should be ready for rendering.
2632 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2633 */
2634 GLboolean
2635 st_finalize_texture(struct gl_context *ctx,
2636 struct pipe_context *pipe,
2637 struct gl_texture_object *tObj)
2638 {
2639 struct st_context *st = st_context(ctx);
2640 struct st_texture_object *stObj = st_texture_object(tObj);
2641 const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
2642 GLuint face;
2643 const struct st_texture_image *firstImage;
2644 enum pipe_format firstImageFormat;
2645 GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
2646
2647 if (tObj->Immutable)
2648 return GL_TRUE;
2649
2650 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
2651 /* The texture is complete and we know exactly how many mipmap levels
2652 * are present/needed. This is conditional because we may be called
2653 * from the st_generate_mipmap() function when the texture object is
2654 * incomplete. In that case, we'll have set stObj->lastLevel before
2655 * we get here.
2656 */
2657 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
2658 stObj->base.Sampler.MinFilter == GL_NEAREST)
2659 stObj->lastLevel = stObj->base.BaseLevel;
2660 else
2661 stObj->lastLevel = stObj->base._MaxLevel;
2662 }
2663
2664 if (tObj->Target == GL_TEXTURE_BUFFER) {
2665 struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
2666
2667 if (!st_obj) {
2668 pipe_resource_reference(&stObj->pt, NULL);
2669 st_texture_release_all_sampler_views(st, stObj);
2670 return GL_TRUE;
2671 }
2672
2673 if (st_obj->buffer != stObj->pt) {
2674 pipe_resource_reference(&stObj->pt, st_obj->buffer);
2675 st_texture_release_all_sampler_views(st, stObj);
2676 stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
2677 stObj->height0 = 1;
2678 stObj->depth0 = 1;
2679 }
2680 return GL_TRUE;
2681
2682 }
2683
2684 firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
2685 assert(firstImage);
2686
2687 /* If both firstImage and stObj point to a texture which can contain
2688 * all active images, favour firstImage. Note that because of the
2689 * completeness requirement, we know that the image dimensions
2690 * will match.
2691 */
2692 if (firstImage->pt &&
2693 firstImage->pt != stObj->pt &&
2694 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
2695 pipe_resource_reference(&stObj->pt, firstImage->pt);
2696 st_texture_release_all_sampler_views(st, stObj);
2697 }
2698
2699 /* If this texture comes from a window system, there is nothing else to do. */
2700 if (stObj->surface_based) {
2701 return GL_TRUE;
2702 }
2703
2704 /* Find gallium format for the Mesa texture */
2705 firstImageFormat =
2706 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
2707
2708 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2709 {
2710 GLuint width, height, depth;
2711 if (!guess_base_level_size(stObj->base.Target,
2712 firstImage->base.Width2,
2713 firstImage->base.Height2,
2714 firstImage->base.Depth2,
2715 firstImage->base.Level,
2716 &width, &height, &depth)) {
2717 width = stObj->width0;
2718 height = stObj->height0;
2719 depth = stObj->depth0;
2720 } else {
2721 /* The width/height/depth may have been previously reset in
2722 * guess_and_alloc_texture. */
2723 stObj->width0 = width;
2724 stObj->height0 = height;
2725 stObj->depth0 = depth;
2726 }
2727 /* convert GL dims to Gallium dims */
2728 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
2729 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2730 ptNumSamples = firstImage->base.NumSamples;
2731 }
2732
2733 /* If we already have a gallium texture, check that it matches the texture
2734 * object's format, target, size, num_levels, etc.
2735 */
2736 if (stObj->pt) {
2737 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
2738 stObj->pt->format != firstImageFormat ||
2739 stObj->pt->last_level < stObj->lastLevel ||
2740 stObj->pt->width0 != ptWidth ||
2741 stObj->pt->height0 != ptHeight ||
2742 stObj->pt->depth0 != ptDepth ||
2743 stObj->pt->nr_samples != ptNumSamples ||
2744 stObj->pt->array_size != ptLayers)
2745 {
2746 /* The gallium texture does not match the Mesa texture so delete the
2747 * gallium texture now. We'll make a new one below.
2748 */
2749 pipe_resource_reference(&stObj->pt, NULL);
2750 st_texture_release_all_sampler_views(st, stObj);
2751 st->dirty.st |= ST_NEW_FRAMEBUFFER;
2752 }
2753 }
2754
2755 /* May need to create a new gallium texture:
2756 */
2757 if (!stObj->pt) {
2758 GLuint bindings = default_bindings(st, firstImageFormat);
2759
2760 stObj->pt = st_texture_create(st,
2761 gl_target_to_pipe(stObj->base.Target),
2762 firstImageFormat,
2763 stObj->lastLevel,
2764 ptWidth,
2765 ptHeight,
2766 ptDepth,
2767 ptLayers, ptNumSamples,
2768 bindings);
2769
2770 if (!stObj->pt) {
2771 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
2772 return GL_FALSE;
2773 }
2774 }
2775
2776 /* Pull in any images not in the object's texture:
2777 */
2778 for (face = 0; face < nr_faces; face++) {
2779 GLuint level;
2780 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
2781 struct st_texture_image *stImage =
2782 st_texture_image(stObj->base.Image[face][level]);
2783
2784 /* Need to import images in main memory or held in other textures.
2785 */
2786 if (stImage && stObj->pt != stImage->pt) {
2787 GLuint height = stObj->height0;
2788 GLuint depth = stObj->depth0;
2789
2790 if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
2791 height = u_minify(height, level);
2792 if (stObj->base.Target == GL_TEXTURE_3D)
2793 depth = u_minify(depth, level);
2794
2795 if (level == 0 ||
2796 (stImage->base.Width == u_minify(stObj->width0, level) &&
2797 stImage->base.Height == height &&
2798 stImage->base.Depth == depth)) {
2799 /* src image fits expected dest mipmap level size */
2800 copy_image_data_to_texture(st, stObj, level, stImage);
2801 }
2802 }
2803 }
2804 }
2805
2806 return GL_TRUE;
2807 }
2808
2809
2810 /**
2811 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2812 * for a whole mipmap stack.
2813 */
2814 static GLboolean
2815 st_AllocTextureStorage(struct gl_context *ctx,
2816 struct gl_texture_object *texObj,
2817 GLsizei levels, GLsizei width,
2818 GLsizei height, GLsizei depth)
2819 {
2820 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
2821 struct gl_texture_image *texImage = texObj->Image[0][0];
2822 struct st_context *st = st_context(ctx);
2823 struct st_texture_object *stObj = st_texture_object(texObj);
2824 struct pipe_screen *screen = st->pipe->screen;
2825 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
2826 enum pipe_format fmt;
2827 GLint level;
2828 GLuint num_samples = texImage->NumSamples;
2829
2830 assert(levels > 0);
2831
2832 /* Save the level=0 dimensions */
2833 stObj->width0 = width;
2834 stObj->height0 = height;
2835 stObj->depth0 = depth;
2836 stObj->lastLevel = levels - 1;
2837
2838 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
2839
2840 bindings = default_bindings(st, fmt);
2841
2842 /* Raise the sample count if the requested one is unsupported. */
2843 if (num_samples > 1) {
2844 boolean found = FALSE;
2845
2846 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
2847 if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
2848 num_samples,
2849 PIPE_BIND_SAMPLER_VIEW)) {
2850 /* Update the sample count in gl_texture_image as well. */
2851 texImage->NumSamples = num_samples;
2852 found = TRUE;
2853 break;
2854 }
2855 }
2856
2857 if (!found) {
2858 return GL_FALSE;
2859 }
2860 }
2861
2862 st_gl_texture_dims_to_pipe_dims(texObj->Target,
2863 width, height, depth,
2864 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
2865
2866 stObj->pt = st_texture_create(st,
2867 gl_target_to_pipe(texObj->Target),
2868 fmt,
2869 levels - 1,
2870 ptWidth,
2871 ptHeight,
2872 ptDepth,
2873 ptLayers, num_samples,
2874 bindings);
2875 if (!stObj->pt)
2876 return GL_FALSE;
2877
2878 /* Set image resource pointers */
2879 for (level = 0; level < levels; level++) {
2880 GLuint face;
2881 for (face = 0; face < numFaces; face++) {
2882 struct st_texture_image *stImage =
2883 st_texture_image(texObj->Image[face][level]);
2884 pipe_resource_reference(&stImage->pt, stObj->pt);
2885 }
2886 }
2887
2888 return GL_TRUE;
2889 }
2890
2891
2892 static GLboolean
2893 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
2894 GLint level, mesa_format format,
2895 GLint width, GLint height,
2896 GLint depth, GLint border)
2897 {
2898 struct st_context *st = st_context(ctx);
2899 struct pipe_context *pipe = st->pipe;
2900
2901 if (width == 0 || height == 0 || depth == 0) {
2902 /* zero-sized images are legal, and always fit! */
2903 return GL_TRUE;
2904 }
2905
2906 if (pipe->screen->can_create_resource) {
2907 /* Ask the gallium driver if the texture is too large */
2908 struct gl_texture_object *texObj =
2909 _mesa_get_current_tex_object(ctx, target);
2910 struct pipe_resource pt;
2911
2912 /* Setup the pipe_resource object
2913 */
2914 memset(&pt, 0, sizeof(pt));
2915
2916 pt.target = gl_target_to_pipe(target);
2917 pt.format = st_mesa_format_to_pipe_format(st, format);
2918
2919 st_gl_texture_dims_to_pipe_dims(target,
2920 width, height, depth,
2921 &pt.width0, &pt.height0,
2922 &pt.depth0, &pt.array_size);
2923
2924 if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
2925 texObj->Sampler.MinFilter == GL_NEAREST)) {
2926 /* assume just one mipmap level */
2927 pt.last_level = 0;
2928 }
2929 else {
2930 /* assume a full set of mipmaps */
2931 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
2932 }
2933
2934 return pipe->screen->can_create_resource(pipe->screen, &pt);
2935 }
2936 else {
2937 /* Use core Mesa fallback */
2938 return _mesa_test_proxy_teximage(ctx, target, level, format,
2939 width, height, depth, border);
2940 }
2941 }
2942
2943 static GLboolean
2944 st_TextureView(struct gl_context *ctx,
2945 struct gl_texture_object *texObj,
2946 struct gl_texture_object *origTexObj)
2947 {
2948 struct st_texture_object *orig = st_texture_object(origTexObj);
2949 struct st_texture_object *tex = st_texture_object(texObj);
2950 struct gl_texture_image *image = texObj->Image[0][0];
2951
2952 const int numFaces = _mesa_num_tex_faces(texObj->Target);
2953 const int numLevels = texObj->NumLevels;
2954
2955 int face;
2956 int level;
2957
2958 pipe_resource_reference(&tex->pt, orig->pt);
2959
2960 /* Set image resource pointers */
2961 for (level = 0; level < numLevels; level++) {
2962 for (face = 0; face < numFaces; face++) {
2963 struct st_texture_image *stImage =
2964 st_texture_image(texObj->Image[face][level]);
2965 pipe_resource_reference(&stImage->pt, tex->pt);
2966 }
2967 }
2968
2969 tex->surface_based = GL_TRUE;
2970 tex->surface_format =
2971 st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
2972
2973 tex->width0 = image->Width;
2974 tex->height0 = image->Height;
2975 tex->depth0 = image->Depth;
2976 tex->lastLevel = numLevels - 1;
2977
2978 return GL_TRUE;
2979 }
2980
2981 static void
2982 st_ClearTexSubImage(struct gl_context *ctx,
2983 struct gl_texture_image *texImage,
2984 GLint xoffset, GLint yoffset, GLint zoffset,
2985 GLsizei width, GLsizei height, GLsizei depth,
2986 const void *clearValue)
2987 {
2988 static const char zeros[16] = {0};
2989 struct st_texture_image *stImage = st_texture_image(texImage);
2990 struct pipe_resource *pt = stImage->pt;
2991 struct st_context *st = st_context(ctx);
2992 struct pipe_context *pipe = st->pipe;
2993 unsigned level = texImage->Level;
2994 struct pipe_box box;
2995
2996 if (!pt)
2997 return;
2998
2999 u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
3000 width, height, depth, &box);
3001 if (texImage->TexObject->Immutable) {
3002 level += texImage->TexObject->MinLevel;
3003 box.z += texImage->TexObject->MinLayer;
3004 }
3005
3006 pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
3007 }
3008
3009 void
3010 st_init_texture_functions(struct dd_function_table *functions)
3011 {
3012 functions->ChooseTextureFormat = st_ChooseTextureFormat;
3013 functions->QueryInternalFormat = st_QueryInternalFormat;
3014 functions->TexImage = st_TexImage;
3015 functions->TexSubImage = st_TexSubImage;
3016 functions->CompressedTexSubImage = st_CompressedTexSubImage;
3017 functions->CopyTexSubImage = st_CopyTexSubImage;
3018 functions->GenerateMipmap = st_generate_mipmap;
3019
3020 functions->GetTexSubImage = st_GetTexSubImage;
3021
3022 /* compressed texture functions */
3023 functions->CompressedTexImage = st_CompressedTexImage;
3024 functions->GetCompressedTexSubImage = _mesa_GetCompressedTexSubImage_sw;
3025
3026 functions->NewTextureObject = st_NewTextureObject;
3027 functions->NewTextureImage = st_NewTextureImage;
3028 functions->DeleteTextureImage = st_DeleteTextureImage;
3029 functions->DeleteTexture = st_DeleteTextureObject;
3030 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
3031 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
3032 functions->MapTextureImage = st_MapTextureImage;
3033 functions->UnmapTextureImage = st_UnmapTextureImage;
3034
3035 /* XXX Temporary until we can query pipe's texture sizes */
3036 functions->TestProxyTexImage = st_TestProxyTexImage;
3037
3038 functions->AllocTextureStorage = st_AllocTextureStorage;
3039 functions->TextureView = st_TextureView;
3040 functions->ClearTexSubImage = st_ClearTexSubImage;
3041 }