st/mesa: add st_context parameter to st_mesa_format_to_pipe_format
[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 "main/bufferobj.h"
29 #include "main/enums.h"
30 #include "main/fbobject.h"
31 #include "main/formats.h"
32 #include "main/image.h"
33 #include "main/imports.h"
34 #include "main/macros.h"
35 #include "main/mipmap.h"
36 #include "main/pack.h"
37 #include "main/pbo.h"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texcompress_etc.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
45
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_cb_bufferobjects.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
56
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
67
68 #define DBG if (0) printf
69
70
71 enum pipe_texture_target
72 gl_target_to_pipe(GLenum target)
73 {
74 switch (target) {
75 case GL_TEXTURE_1D:
76 case GL_PROXY_TEXTURE_1D:
77 return PIPE_TEXTURE_1D;
78 case GL_TEXTURE_2D:
79 case GL_PROXY_TEXTURE_2D:
80 case GL_TEXTURE_EXTERNAL_OES:
81 case GL_TEXTURE_2D_MULTISAMPLE:
82 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
83 return PIPE_TEXTURE_2D;
84 case GL_TEXTURE_RECTANGLE_NV:
85 case GL_PROXY_TEXTURE_RECTANGLE_NV:
86 return PIPE_TEXTURE_RECT;
87 case GL_TEXTURE_3D:
88 case GL_PROXY_TEXTURE_3D:
89 return PIPE_TEXTURE_3D;
90 case GL_TEXTURE_CUBE_MAP_ARB:
91 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
92 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
93 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
94 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
95 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
96 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
97 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
98 return PIPE_TEXTURE_CUBE;
99 case GL_TEXTURE_1D_ARRAY_EXT:
100 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
101 return PIPE_TEXTURE_1D_ARRAY;
102 case GL_TEXTURE_2D_ARRAY_EXT:
103 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
104 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
105 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
106 return PIPE_TEXTURE_2D_ARRAY;
107 case GL_TEXTURE_BUFFER:
108 return PIPE_BUFFER;
109 case GL_TEXTURE_CUBE_MAP_ARRAY:
110 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
111 return PIPE_TEXTURE_CUBE_ARRAY;
112 default:
113 assert(0);
114 return 0;
115 }
116 }
117
118
119 /** called via ctx->Driver.NewTextureImage() */
120 static struct gl_texture_image *
121 st_NewTextureImage(struct gl_context * ctx)
122 {
123 DBG("%s\n", __FUNCTION__);
124 (void) ctx;
125 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
126 }
127
128
129 /** called via ctx->Driver.DeleteTextureImage() */
130 static void
131 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
132 {
133 /* nothing special (yet) for st_texture_image */
134 _mesa_delete_texture_image(ctx, img);
135 }
136
137
138 /** called via ctx->Driver.NewTextureObject() */
139 static struct gl_texture_object *
140 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
141 {
142 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
143
144 DBG("%s\n", __FUNCTION__);
145 _mesa_initialize_texture_object(ctx, &obj->base, name, target);
146
147 return &obj->base;
148 }
149
150 /** called via ctx->Driver.DeleteTextureObject() */
151 static void
152 st_DeleteTextureObject(struct gl_context *ctx,
153 struct gl_texture_object *texObj)
154 {
155 struct st_texture_object *stObj = st_texture_object(texObj);
156
157 pipe_resource_reference(&stObj->pt, NULL);
158 st_texture_release_all_sampler_views(stObj);
159 st_texture_free_sampler_views(stObj);
160 _mesa_delete_texture_object(ctx, texObj);
161 }
162
163
164 /** called via ctx->Driver.FreeTextureImageBuffer() */
165 static void
166 st_FreeTextureImageBuffer(struct gl_context *ctx,
167 struct gl_texture_image *texImage)
168 {
169 struct st_texture_image *stImage = st_texture_image(texImage);
170
171 DBG("%s\n", __FUNCTION__);
172
173 if (stImage->pt) {
174 pipe_resource_reference(&stImage->pt, NULL);
175 }
176
177 _mesa_align_free(stImage->TexData);
178 stImage->TexData = NULL;
179
180 free(stImage->transfer);
181 stImage->transfer = NULL;
182 stImage->num_transfers = 0;
183 }
184
185
186 /** called via ctx->Driver.MapTextureImage() */
187 static void
188 st_MapTextureImage(struct gl_context *ctx,
189 struct gl_texture_image *texImage,
190 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
191 GLbitfield mode,
192 GLubyte **mapOut, GLint *rowStrideOut)
193 {
194 struct st_context *st = st_context(ctx);
195 struct st_texture_image *stImage = st_texture_image(texImage);
196 unsigned pipeMode;
197 GLubyte *map;
198 struct pipe_transfer *transfer;
199
200 pipeMode = 0x0;
201 if (mode & GL_MAP_READ_BIT)
202 pipeMode |= PIPE_TRANSFER_READ;
203 if (mode & GL_MAP_WRITE_BIT)
204 pipeMode |= PIPE_TRANSFER_WRITE;
205 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
206 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
207
208 map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1,
209 &transfer);
210 if (map) {
211 if (_mesa_is_format_etc2(texImage->TexFormat)) {
212 /* ETC isn't supported by gallium and it's represented
213 * by uncompressed formats. Only write transfers with precompressed
214 * data are supported by ES3, which makes this really simple.
215 *
216 * Just create a temporary storage where the ETC texture will
217 * be stored. It will be decompressed in the Unmap function.
218 */
219 unsigned z = transfer->box.z;
220 struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
221
222 itransfer->temp_data =
223 malloc(_mesa_format_image_size(texImage->TexFormat, w, h, 1));
224 itransfer->temp_stride =
225 _mesa_format_row_stride(texImage->TexFormat, w);
226 itransfer->map = map;
227
228 *mapOut = itransfer->temp_data;
229 *rowStrideOut = itransfer->temp_stride;
230 }
231 else {
232 /* supported mapping */
233 *mapOut = map;
234 *rowStrideOut = transfer->stride;
235 }
236 }
237 else {
238 *mapOut = NULL;
239 *rowStrideOut = 0;
240 }
241 }
242
243
244 /** called via ctx->Driver.UnmapTextureImage() */
245 static void
246 st_UnmapTextureImage(struct gl_context *ctx,
247 struct gl_texture_image *texImage,
248 GLuint slice)
249 {
250 struct st_context *st = st_context(ctx);
251 struct st_texture_image *stImage = st_texture_image(texImage);
252
253 if (_mesa_is_format_etc2(texImage->TexFormat)) {
254 /* Decompress the ETC texture to the mapped one. */
255 unsigned z = slice + stImage->base.Face;
256 struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
257 struct pipe_transfer *transfer = itransfer->transfer;
258
259 assert(z == transfer->box.z);
260
261 _mesa_unpack_etc2_format(itransfer->map, transfer->stride,
262 itransfer->temp_data, itransfer->temp_stride,
263 transfer->box.width, transfer->box.height,
264 texImage->TexFormat);
265
266 free(itransfer->temp_data);
267 itransfer->temp_data = NULL;
268 itransfer->temp_stride = 0;
269 itransfer->map = 0;
270 }
271
272 st_texture_image_unmap(st, stImage, slice);
273 }
274
275
276 /**
277 * Return default texture resource binding bitmask for the given format.
278 */
279 static GLuint
280 default_bindings(struct st_context *st, enum pipe_format format)
281 {
282 struct pipe_screen *screen = st->pipe->screen;
283 const unsigned target = PIPE_TEXTURE_2D;
284 unsigned bindings;
285
286 if (util_format_is_depth_or_stencil(format))
287 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
288 else
289 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
290
291 if (screen->is_format_supported(screen, format, target, 0, bindings))
292 return bindings;
293 else {
294 /* Try non-sRGB. */
295 format = util_format_linear(format);
296
297 if (screen->is_format_supported(screen, format, target, 0, bindings))
298 return bindings;
299 else
300 return PIPE_BIND_SAMPLER_VIEW;
301 }
302 }
303
304
305 /**
306 * Given the size of a mipmap image, try to compute the size of the level=0
307 * mipmap image.
308 *
309 * Note that this isn't always accurate for odd-sized, non-POW textures.
310 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
311 *
312 * \return GL_TRUE for success, GL_FALSE for failure
313 */
314 static GLboolean
315 guess_base_level_size(GLenum target,
316 GLuint width, GLuint height, GLuint depth, GLuint level,
317 GLuint *width0, GLuint *height0, GLuint *depth0)
318 {
319 assert(width >= 1);
320 assert(height >= 1);
321 assert(depth >= 1);
322
323 if (level > 0) {
324 /* Guess the size of the base level.
325 * Depending on the image's size, we can't always make a guess here.
326 */
327 switch (target) {
328 case GL_TEXTURE_1D:
329 case GL_TEXTURE_1D_ARRAY:
330 width <<= level;
331 break;
332
333 case GL_TEXTURE_2D:
334 case GL_TEXTURE_2D_ARRAY:
335 /* We can't make a good guess here, because the base level dimensions
336 * can be non-square.
337 */
338 if (width == 1 || height == 1) {
339 return GL_FALSE;
340 }
341 width <<= level;
342 height <<= level;
343 break;
344
345 case GL_TEXTURE_CUBE_MAP:
346 case GL_TEXTURE_CUBE_MAP_ARRAY:
347 width <<= level;
348 height <<= level;
349 break;
350
351 case GL_TEXTURE_3D:
352 /* We can't make a good guess here, because the base level dimensions
353 * can be non-cube.
354 */
355 if (width == 1 || height == 1 || depth == 1) {
356 return GL_FALSE;
357 }
358 width <<= level;
359 height <<= level;
360 depth <<= level;
361 break;
362
363 case GL_TEXTURE_RECTANGLE:
364 break;
365
366 default:
367 assert(0);
368 }
369 }
370
371 *width0 = width;
372 *height0 = height;
373 *depth0 = depth;
374
375 return GL_TRUE;
376 }
377
378
379 /**
380 * Try to allocate a pipe_resource object for the given st_texture_object.
381 *
382 * We use the given st_texture_image as a clue to determine the size of the
383 * mipmap image at level=0.
384 *
385 * \return GL_TRUE for success, GL_FALSE if out of memory.
386 */
387 static GLboolean
388 guess_and_alloc_texture(struct st_context *st,
389 struct st_texture_object *stObj,
390 const struct st_texture_image *stImage)
391 {
392 GLuint lastLevel, width, height, depth;
393 GLuint bindings;
394 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
395 enum pipe_format fmt;
396
397 DBG("%s\n", __FUNCTION__);
398
399 assert(!stObj->pt);
400
401 if (!guess_base_level_size(stObj->base.Target,
402 stImage->base.Width2,
403 stImage->base.Height2,
404 stImage->base.Depth2,
405 stImage->base.Level,
406 &width, &height, &depth)) {
407 /* we can't determine the image size at level=0 */
408 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
409 /* this is not an out of memory error */
410 return GL_TRUE;
411 }
412
413 /* At this point, (width x height x depth) is the expected size of
414 * the level=0 mipmap image.
415 */
416
417 /* Guess a reasonable value for lastLevel. With OpenGL we have no
418 * idea how many mipmap levels will be in a texture until we start
419 * to render with it. Make an educated guess here but be prepared
420 * to re-allocating a texture buffer with space for more (or fewer)
421 * mipmap levels later.
422 */
423 if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
424 stObj->base.Sampler.MinFilter == GL_LINEAR ||
425 (stObj->base.BaseLevel == 0 &&
426 stObj->base.MaxLevel == 0) ||
427 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
428 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
429 !stObj->base.GenerateMipmap &&
430 stImage->base.Level == 0) {
431 /* only alloc space for a single mipmap level */
432 lastLevel = 0;
433 }
434 else {
435 /* alloc space for a full mipmap */
436 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
437 width, height, depth) - 1;
438 }
439
440 /* Save the level=0 dimensions */
441 stObj->width0 = width;
442 stObj->height0 = height;
443 stObj->depth0 = depth;
444
445 fmt = st_mesa_format_to_pipe_format(st, stImage->base.TexFormat);
446
447 bindings = default_bindings(st, fmt);
448
449 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
450 width, height, depth,
451 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
452
453 stObj->pt = st_texture_create(st,
454 gl_target_to_pipe(stObj->base.Target),
455 fmt,
456 lastLevel,
457 ptWidth,
458 ptHeight,
459 ptDepth,
460 ptLayers, 0,
461 bindings);
462
463 stObj->lastLevel = lastLevel;
464
465 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
466
467 return stObj->pt != NULL;
468 }
469
470
471 /**
472 * Called via ctx->Driver.AllocTextureImageBuffer().
473 * If the texture object/buffer already has space for the indicated image,
474 * we're done. Otherwise, allocate memory for the new texture image.
475 */
476 static GLboolean
477 st_AllocTextureImageBuffer(struct gl_context *ctx,
478 struct gl_texture_image *texImage)
479 {
480 struct st_context *st = st_context(ctx);
481 struct st_texture_image *stImage = st_texture_image(texImage);
482 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
483 const GLuint level = texImage->Level;
484 GLuint width = texImage->Width;
485 GLuint height = texImage->Height;
486 GLuint depth = texImage->Depth;
487
488 DBG("%s\n", __FUNCTION__);
489
490 assert(!stImage->TexData);
491 assert(!stImage->pt); /* xxx this might be wrong */
492
493 /* Look if the parent texture object has space for this image */
494 if (stObj->pt &&
495 level <= stObj->pt->last_level &&
496 st_texture_match_image(st, stObj->pt, texImage)) {
497 /* this image will fit in the existing texture object's memory */
498 pipe_resource_reference(&stImage->pt, stObj->pt);
499 return GL_TRUE;
500 }
501
502 /* The parent texture object does not have space for this image */
503
504 pipe_resource_reference(&stObj->pt, NULL);
505 st_texture_release_all_sampler_views(stObj);
506
507 if (!guess_and_alloc_texture(st, stObj, stImage)) {
508 /* Probably out of memory.
509 * Try flushing any pending rendering, then retry.
510 */
511 st_finish(st);
512 if (!guess_and_alloc_texture(st, stObj, stImage)) {
513 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
514 return GL_FALSE;
515 }
516 }
517
518 if (stObj->pt &&
519 st_texture_match_image(st, stObj->pt, texImage)) {
520 /* The image will live in the object's mipmap memory */
521 pipe_resource_reference(&stImage->pt, stObj->pt);
522 assert(stImage->pt);
523 return GL_TRUE;
524 }
525 else {
526 /* Create a new, temporary texture/resource/buffer to hold this
527 * one texture image. Note that when we later access this image
528 * (either for mapping or copying) we'll want to always specify
529 * mipmap level=0, even if the image represents some other mipmap
530 * level.
531 */
532 enum pipe_format format =
533 st_mesa_format_to_pipe_format(st, texImage->TexFormat);
534 GLuint bindings = default_bindings(st, format);
535 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
536
537 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
538 width, height, depth,
539 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
540
541 stImage->pt = st_texture_create(st,
542 gl_target_to_pipe(stObj->base.Target),
543 format,
544 0, /* lastLevel */
545 ptWidth,
546 ptHeight,
547 ptDepth,
548 ptLayers, 0,
549 bindings);
550 return stImage->pt != NULL;
551 }
552 }
553
554
555 /**
556 * Preparation prior to glTexImage. Basically check the 'surface_based'
557 * field and switch to a "normal" tex image if necessary.
558 */
559 static void
560 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
561 GLenum format, GLenum type)
562 {
563 struct gl_texture_object *texObj = texImage->TexObject;
564 struct st_texture_object *stObj = st_texture_object(texObj);
565
566 /* switch to "normal" */
567 if (stObj->surface_based) {
568 const GLenum target = texObj->Target;
569 const GLuint level = texImage->Level;
570 mesa_format texFormat;
571
572 _mesa_clear_texture_object(ctx, texObj);
573 pipe_resource_reference(&stObj->pt, NULL);
574
575 /* oops, need to init this image again */
576 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
577 texImage->InternalFormat, format,
578 type);
579
580 _mesa_init_teximage_fields(ctx, texImage,
581 texImage->Width, texImage->Height,
582 texImage->Depth, texImage->Border,
583 texImage->InternalFormat, texFormat);
584
585 stObj->surface_based = GL_FALSE;
586 }
587 }
588
589
590 /**
591 * Return a writemask for the gallium blit. The parameters can be base
592 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
593 */
594 unsigned
595 st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
596 {
597 switch (dstFormat) {
598 case GL_DEPTH_STENCIL:
599 switch (srcFormat) {
600 case GL_DEPTH_STENCIL:
601 return PIPE_MASK_ZS;
602 case GL_DEPTH_COMPONENT:
603 return PIPE_MASK_Z;
604 case GL_STENCIL_INDEX:
605 return PIPE_MASK_S;
606 default:
607 assert(0);
608 return 0;
609 }
610
611 case GL_DEPTH_COMPONENT:
612 switch (srcFormat) {
613 case GL_DEPTH_STENCIL:
614 case GL_DEPTH_COMPONENT:
615 return PIPE_MASK_Z;
616 default:
617 assert(0);
618 return 0;
619 }
620
621 case GL_STENCIL_INDEX:
622 switch (srcFormat) {
623 case GL_STENCIL_INDEX:
624 return PIPE_MASK_S;
625 default:
626 assert(0);
627 return 0;
628 }
629
630 default:
631 return PIPE_MASK_RGBA;
632 }
633 }
634
635
636 static void
637 st_TexSubImage(struct gl_context *ctx, GLuint dims,
638 struct gl_texture_image *texImage,
639 GLint xoffset, GLint yoffset, GLint zoffset,
640 GLint width, GLint height, GLint depth,
641 GLenum format, GLenum type, const void *pixels,
642 const struct gl_pixelstore_attrib *unpack)
643 {
644 struct st_context *st = st_context(ctx);
645 struct st_texture_image *stImage = st_texture_image(texImage);
646 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
647 struct pipe_context *pipe = st->pipe;
648 struct pipe_screen *screen = pipe->screen;
649 struct pipe_resource *dst = stImage->pt;
650 struct pipe_resource *src = NULL;
651 struct pipe_resource src_templ;
652 struct pipe_transfer *transfer;
653 struct pipe_blit_info blit;
654 enum pipe_format src_format, dst_format;
655 mesa_format mesa_src_format;
656 GLenum gl_target = texImage->TexObject->Target;
657 unsigned bind;
658 GLubyte *map;
659
660 assert(!_mesa_is_format_etc2(texImage->TexFormat));
661
662 if (!st->prefer_blit_based_texture_transfer) {
663 goto fallback;
664 }
665
666 if (!dst) {
667 goto fallback;
668 }
669
670 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
671 * blit implementation in some drivers. */
672 if (format == GL_DEPTH_STENCIL) {
673 goto fallback;
674 }
675
676 /* If the base internal format and the texture format don't match,
677 * we can't use blit-based TexSubImage. */
678 if (texImage->_BaseFormat !=
679 _mesa_get_format_base_format(texImage->TexFormat)) {
680 goto fallback;
681 }
682
683 /* See if the texture format already matches the format and type,
684 * in which case the memcpy-based fast path will likely be used and
685 * we don't have to blit. */
686 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
687 type, unpack->SwapBytes)) {
688 goto fallback;
689 }
690
691 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
692 bind = PIPE_BIND_DEPTH_STENCIL;
693 else
694 bind = PIPE_BIND_RENDER_TARGET;
695
696 /* See if the destination format is supported.
697 * For luminance and intensity, only the red channel is stored there. */
698 dst_format = util_format_linear(dst->format);
699 dst_format = util_format_luminance_to_red(dst_format);
700 dst_format = util_format_intensity_to_red(dst_format);
701
702 if (!dst_format ||
703 !screen->is_format_supported(screen, dst_format, dst->target,
704 dst->nr_samples, bind)) {
705 goto fallback;
706 }
707
708 /* Choose the source format. */
709 src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
710 format, type, unpack->SwapBytes);
711 if (!src_format) {
712 goto fallback;
713 }
714
715 mesa_src_format = st_pipe_format_to_mesa_format(src_format);
716
717 /* There is no reason to do this if we cannot use memcpy for the temporary
718 * source texture at least. This also takes transfer ops into account,
719 * etc. */
720 if (!_mesa_texstore_can_use_memcpy(ctx,
721 _mesa_get_format_base_format(mesa_src_format),
722 mesa_src_format, format, type, unpack)) {
723 goto fallback;
724 }
725
726 /* TexSubImage only sets a single cubemap face. */
727 if (gl_target == GL_TEXTURE_CUBE_MAP) {
728 gl_target = GL_TEXTURE_2D;
729 }
730
731 /* Initialize the source texture description. */
732 memset(&src_templ, 0, sizeof(src_templ));
733 src_templ.target = gl_target_to_pipe(gl_target);
734 src_templ.format = src_format;
735 src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
736 src_templ.usage = PIPE_USAGE_STAGING;
737
738 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
739 &src_templ.width0, &src_templ.height0,
740 &src_templ.depth0, &src_templ.array_size);
741
742 /* Check for NPOT texture support. */
743 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
744 (!util_is_power_of_two(src_templ.width0) ||
745 !util_is_power_of_two(src_templ.height0) ||
746 !util_is_power_of_two(src_templ.depth0))) {
747 goto fallback;
748 }
749
750 /* Create the source texture. */
751 src = screen->resource_create(screen, &src_templ);
752 if (!src) {
753 goto fallback;
754 }
755
756 /* Map source pixels. */
757 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
758 format, type, pixels, unpack,
759 "glTexSubImage");
760 if (!pixels) {
761 /* This is a GL error. */
762 pipe_resource_reference(&src, NULL);
763 return;
764 }
765
766 /* From now on, we need the gallium representation of dimensions. */
767 if (gl_target == GL_TEXTURE_1D_ARRAY) {
768 zoffset = yoffset;
769 yoffset = 0;
770 depth = height;
771 height = 1;
772 }
773
774 map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
775 width, height, depth, &transfer);
776 if (!map) {
777 _mesa_unmap_teximage_pbo(ctx, unpack);
778 pipe_resource_reference(&src, NULL);
779 goto fallback;
780 }
781
782 /* Upload pixels (just memcpy). */
783 {
784 const uint bytesPerRow = width * util_format_get_blocksize(src_format);
785 GLuint row, slice;
786
787 for (slice = 0; slice < (unsigned) depth; slice++) {
788 if (gl_target == GL_TEXTURE_1D_ARRAY) {
789 /* 1D array textures.
790 * We need to convert gallium coords to GL coords.
791 */
792 GLvoid *src = _mesa_image_address3d(unpack, pixels,
793 width, depth, format,
794 type, 0, slice, 0);
795 memcpy(map, src, bytesPerRow);
796 }
797 else {
798 ubyte *slice_map = map;
799
800 for (row = 0; row < (unsigned) height; row++) {
801 GLvoid *src = _mesa_image_address3d(unpack, pixels,
802 width, height, format,
803 type, slice, row, 0);
804 memcpy(slice_map, src, bytesPerRow);
805 slice_map += transfer->stride;
806 }
807 }
808 map += transfer->layer_stride;
809 }
810 }
811
812 pipe_transfer_unmap(pipe, transfer);
813 _mesa_unmap_teximage_pbo(ctx, unpack);
814
815 /* Blit. */
816 memset(&blit, 0, sizeof(blit));
817 blit.src.resource = src;
818 blit.src.level = 0;
819 blit.src.format = src_format;
820 blit.dst.resource = dst;
821 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level;
822 blit.dst.format = dst_format;
823 blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
824 blit.dst.box.x = xoffset;
825 blit.dst.box.y = yoffset;
826 blit.dst.box.z = zoffset + texImage->Face;
827 blit.src.box.width = blit.dst.box.width = width;
828 blit.src.box.height = blit.dst.box.height = height;
829 blit.src.box.depth = blit.dst.box.depth = depth;
830 blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
831 blit.filter = PIPE_TEX_FILTER_NEAREST;
832 blit.scissor_enable = FALSE;
833
834 st->pipe->blit(st->pipe, &blit);
835
836 pipe_resource_reference(&src, NULL);
837 return;
838
839 fallback:
840 _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
841 width, height, depth, format, type, pixels,
842 unpack);
843 }
844
845 static void
846 st_TexImage(struct gl_context * ctx, GLuint dims,
847 struct gl_texture_image *texImage,
848 GLenum format, GLenum type, const void *pixels,
849 const struct gl_pixelstore_attrib *unpack)
850 {
851 assert(dims == 1 || dims == 2 || dims == 3);
852
853 prep_teximage(ctx, texImage, format, type);
854
855 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
856 return;
857
858 /* allocate storage for texture data */
859 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
860 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
861 return;
862 }
863
864 st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
865 texImage->Width, texImage->Height, texImage->Depth,
866 format, type, pixels, unpack);
867 }
868
869
870 static void
871 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
872 struct gl_texture_image *texImage,
873 GLsizei imageSize, const GLvoid *data)
874 {
875 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
876 _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
877 }
878
879
880
881
882 /**
883 * Called via ctx->Driver.GetTexImage()
884 *
885 * This uses a blit to copy the texture to a texture format which matches
886 * the format and type combo and then a fast read-back is done using memcpy.
887 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
888 * a format which matches the swizzling.
889 *
890 * If such a format isn't available, it falls back to _mesa_get_teximage.
891 *
892 * NOTE: Drivers usually do a blit to convert between tiled and linear
893 * texture layouts during texture uploads/downloads, so the blit
894 * we do here should be free in such cases.
895 */
896 static void
897 st_GetTexImage(struct gl_context * ctx,
898 GLenum format, GLenum type, GLvoid * pixels,
899 struct gl_texture_image *texImage)
900 {
901 struct st_context *st = st_context(ctx);
902 struct pipe_context *pipe = st->pipe;
903 struct pipe_screen *screen = pipe->screen;
904 GLuint width = texImage->Width;
905 GLuint height = texImage->Height;
906 GLuint depth = texImage->Depth;
907 struct st_texture_image *stImage = st_texture_image(texImage);
908 struct pipe_resource *src = st_texture_object(texImage->TexObject)->pt;
909 struct pipe_resource *dst = NULL;
910 struct pipe_resource dst_templ;
911 enum pipe_format dst_format, src_format;
912 mesa_format mesa_format;
913 GLenum gl_target = texImage->TexObject->Target;
914 enum pipe_texture_target pipe_target;
915 struct pipe_blit_info blit;
916 unsigned bind = PIPE_BIND_TRANSFER_READ;
917 struct pipe_transfer *tex_xfer;
918 ubyte *map = NULL;
919 boolean done = FALSE;
920
921 assert(!_mesa_is_format_etc2(texImage->TexFormat));
922
923 if (!st->prefer_blit_based_texture_transfer &&
924 !_mesa_is_format_compressed(texImage->TexFormat)) {
925 /* Try to avoid the fallback if we're doing texture decompression here */
926 goto fallback;
927 }
928
929 if (!stImage->pt || !src) {
930 goto fallback;
931 }
932
933 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
934 * due to an incomplete stencil blit implementation in some drivers. */
935 if (format == GL_DEPTH_STENCIL) {
936 goto fallback;
937 }
938
939 /* If the base internal format and the texture format don't match, we have
940 * to fall back to _mesa_get_teximage. */
941 if (texImage->_BaseFormat !=
942 _mesa_get_format_base_format(texImage->TexFormat)) {
943 goto fallback;
944 }
945
946 /* See if the texture format already matches the format and type,
947 * in which case the memcpy-based fast path will be used. */
948 if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
949 type, ctx->Pack.SwapBytes)) {
950 goto fallback;
951 }
952
953 /* Convert the source format to what is expected by GetTexImage
954 * and see if it's supported.
955 *
956 * This only applies to glGetTexImage:
957 * - Luminance must be returned as (L,0,0,1).
958 * - Luminance alpha must be returned as (L,0,0,A).
959 * - Intensity must be returned as (I,0,0,1)
960 */
961 src_format = util_format_linear(src->format);
962 src_format = util_format_luminance_to_red(src_format);
963 src_format = util_format_intensity_to_red(src_format);
964
965 if (!src_format ||
966 !screen->is_format_supported(screen, src_format, src->target,
967 src->nr_samples,
968 PIPE_BIND_SAMPLER_VIEW)) {
969 goto fallback;
970 }
971
972 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
973 bind |= PIPE_BIND_DEPTH_STENCIL;
974 else
975 bind |= PIPE_BIND_RENDER_TARGET;
976
977 /* GetTexImage only returns a single face for cubemaps. */
978 if (gl_target == GL_TEXTURE_CUBE_MAP) {
979 gl_target = GL_TEXTURE_2D;
980 }
981 pipe_target = gl_target_to_pipe(gl_target);
982
983 /* Choose the destination format by finding the best match
984 * for the format+type combo. */
985 dst_format = st_choose_matching_format(st, bind, format, type,
986 ctx->Pack.SwapBytes);
987
988 if (dst_format == PIPE_FORMAT_NONE) {
989 GLenum dst_glformat;
990
991 /* Fall back to _mesa_get_teximage except for compressed formats,
992 * where decompression with a blit is always preferred. */
993 if (!util_format_is_compressed(src->format)) {
994 goto fallback;
995 }
996
997 /* Set the appropriate format for the decompressed texture.
998 * Luminance and sRGB formats shouldn't appear here.*/
999 switch (src_format) {
1000 case PIPE_FORMAT_DXT1_RGB:
1001 case PIPE_FORMAT_DXT1_RGBA:
1002 case PIPE_FORMAT_DXT3_RGBA:
1003 case PIPE_FORMAT_DXT5_RGBA:
1004 case PIPE_FORMAT_RGTC1_UNORM:
1005 case PIPE_FORMAT_RGTC2_UNORM:
1006 case PIPE_FORMAT_ETC1_RGB8:
1007 dst_glformat = GL_RGBA8;
1008 break;
1009 case PIPE_FORMAT_RGTC1_SNORM:
1010 case PIPE_FORMAT_RGTC2_SNORM:
1011 if (!ctx->Extensions.EXT_texture_snorm)
1012 goto fallback;
1013 dst_glformat = GL_RGBA8_SNORM;
1014 break;
1015 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
1016 default:
1017 assert(0);
1018 goto fallback;
1019 }
1020
1021 dst_format = st_choose_format(st, dst_glformat, format, type,
1022 pipe_target, 0, bind, FALSE);
1023
1024 if (dst_format == PIPE_FORMAT_NONE) {
1025 /* unable to get an rgba format!?! */
1026 goto fallback;
1027 }
1028 }
1029
1030 /* create the destination texture */
1031 memset(&dst_templ, 0, sizeof(dst_templ));
1032 dst_templ.target = pipe_target;
1033 dst_templ.format = dst_format;
1034 dst_templ.bind = bind;
1035 dst_templ.usage = PIPE_USAGE_STAGING;
1036
1037 st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1038 &dst_templ.width0, &dst_templ.height0,
1039 &dst_templ.depth0, &dst_templ.array_size);
1040
1041 dst = screen->resource_create(screen, &dst_templ);
1042 if (!dst) {
1043 goto fallback;
1044 }
1045
1046 /* From now on, we need the gallium representation of dimensions. */
1047 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1048 depth = height;
1049 height = 1;
1050 }
1051
1052 memset(&blit, 0, sizeof(blit));
1053 blit.src.resource = src;
1054 blit.src.level = texImage->Level;
1055 blit.src.format = src_format;
1056 blit.dst.resource = dst;
1057 blit.dst.level = 0;
1058 blit.dst.format = dst->format;
1059 blit.src.box.x = blit.dst.box.x = 0;
1060 blit.src.box.y = blit.dst.box.y = 0;
1061 blit.src.box.z = texImage->Face;
1062 blit.dst.box.z = 0;
1063 blit.src.box.width = blit.dst.box.width = width;
1064 blit.src.box.height = blit.dst.box.height = height;
1065 blit.src.box.depth = blit.dst.box.depth = depth;
1066 blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
1067 blit.filter = PIPE_TEX_FILTER_NEAREST;
1068 blit.scissor_enable = FALSE;
1069
1070 /* blit/render/decompress */
1071 st->pipe->blit(st->pipe, &blit);
1072
1073 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
1074
1075 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
1076 0, 0, 0, width, height, depth, &tex_xfer);
1077 if (!map) {
1078 goto end;
1079 }
1080
1081 mesa_format = st_pipe_format_to_mesa_format(dst_format);
1082
1083 /* copy/pack data into user buffer */
1084 if (_mesa_format_matches_format_and_type(mesa_format, format, type,
1085 ctx->Pack.SwapBytes)) {
1086 /* memcpy */
1087 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
1088 GLuint row, slice;
1089
1090 for (slice = 0; slice < depth; slice++) {
1091 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1092 /* 1D array textures.
1093 * We need to convert gallium coords to GL coords.
1094 */
1095 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1096 width, depth, format,
1097 type, 0, slice, 0);
1098 memcpy(dest, map, bytesPerRow);
1099 }
1100 else {
1101 ubyte *slice_map = map;
1102
1103 for (row = 0; row < height; row++) {
1104 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1105 width, height, format,
1106 type, slice, row, 0);
1107 memcpy(dest, slice_map, bytesPerRow);
1108 slice_map += tex_xfer->stride;
1109 }
1110 }
1111 map += tex_xfer->layer_stride;
1112 }
1113 }
1114 else {
1115 /* format translation via floats */
1116 GLuint row, slice;
1117 GLfloat *rgba;
1118
1119 assert(util_format_is_compressed(src->format));
1120
1121 rgba = malloc(width * 4 * sizeof(GLfloat));
1122 if (!rgba) {
1123 goto end;
1124 }
1125
1126 if (ST_DEBUG & DEBUG_FALLBACK)
1127 debug_printf("%s: fallback format translation\n", __FUNCTION__);
1128
1129 for (slice = 0; slice < depth; slice++) {
1130 if (gl_target == GL_TEXTURE_1D_ARRAY) {
1131 /* 1D array textures.
1132 * We need to convert gallium coords to GL coords.
1133 */
1134 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1135 width, depth, format,
1136 type, 0, slice, 0);
1137
1138 /* get float[4] rgba row from surface */
1139 pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
1140 dst_format, rgba);
1141
1142 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
1143 type, dest, &ctx->Pack, 0);
1144 }
1145 else {
1146 for (row = 0; row < height; row++) {
1147 GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1148 width, height, format,
1149 type, slice, row, 0);
1150
1151 /* get float[4] rgba row from surface */
1152 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
1153 dst_format, rgba);
1154
1155 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
1156 type, dest, &ctx->Pack, 0);
1157 }
1158 }
1159 map += tex_xfer->layer_stride;
1160 }
1161
1162 free(rgba);
1163 }
1164 done = TRUE;
1165
1166 end:
1167 if (map)
1168 pipe_transfer_unmap(pipe, tex_xfer);
1169
1170 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
1171 pipe_resource_reference(&dst, NULL);
1172
1173 fallback:
1174 if (!done) {
1175 _mesa_get_teximage(ctx, format, type, pixels, texImage);
1176 }
1177 }
1178
1179
1180 /**
1181 * Do a CopyTexSubImage operation using a read transfer from the source,
1182 * a write transfer to the destination and get_tile()/put_tile() to access
1183 * the pixels/texels.
1184 *
1185 * Note: srcY=0=TOP of renderbuffer
1186 */
1187 static void
1188 fallback_copy_texsubimage(struct gl_context *ctx,
1189 struct st_renderbuffer *strb,
1190 struct st_texture_image *stImage,
1191 GLenum baseFormat,
1192 GLint destX, GLint destY, GLint slice,
1193 GLint srcX, GLint srcY,
1194 GLsizei width, GLsizei height)
1195 {
1196 struct st_context *st = st_context(ctx);
1197 struct pipe_context *pipe = st->pipe;
1198 struct pipe_transfer *src_trans;
1199 GLubyte *texDest;
1200 enum pipe_transfer_usage transfer_usage;
1201 void *map;
1202 unsigned dst_width = width;
1203 unsigned dst_height = height;
1204 unsigned dst_depth = 1;
1205 struct pipe_transfer *transfer;
1206
1207 if (ST_DEBUG & DEBUG_FALLBACK)
1208 debug_printf("%s: fallback processing\n", __FUNCTION__);
1209
1210 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1211 srcY = strb->Base.Height - srcY - height;
1212 }
1213
1214 map = pipe_transfer_map(pipe,
1215 strb->texture,
1216 strb->surface->u.tex.level,
1217 strb->surface->u.tex.first_layer,
1218 PIPE_TRANSFER_READ,
1219 srcX, srcY,
1220 width, height, &src_trans);
1221
1222 if ((baseFormat == GL_DEPTH_COMPONENT ||
1223 baseFormat == GL_DEPTH_STENCIL) &&
1224 util_format_is_depth_and_stencil(stImage->pt->format))
1225 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1226 else
1227 transfer_usage = PIPE_TRANSFER_WRITE;
1228
1229 texDest = st_texture_image_map(st, stImage, transfer_usage,
1230 destX, destY, slice,
1231 dst_width, dst_height, dst_depth,
1232 &transfer);
1233
1234 if (baseFormat == GL_DEPTH_COMPONENT ||
1235 baseFormat == GL_DEPTH_STENCIL) {
1236 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1237 ctx->Pixel.DepthBias != 0.0F);
1238 GLint row, yStep;
1239 uint *data;
1240
1241 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1242 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1243 srcY = height - 1;
1244 yStep = -1;
1245 }
1246 else {
1247 srcY = 0;
1248 yStep = 1;
1249 }
1250
1251 data = malloc(width * sizeof(uint));
1252
1253 if (data) {
1254 /* To avoid a large temp memory allocation, do copy row by row */
1255 for (row = 0; row < height; row++, srcY += yStep) {
1256 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
1257 if (scaleOrBias) {
1258 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1259 }
1260
1261 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1262 pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
1263 0, 0, width, 1, data);
1264 }
1265 else {
1266 pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
1267 }
1268 }
1269 }
1270 else {
1271 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
1272 }
1273
1274 free(data);
1275 }
1276 else {
1277 /* RGBA format */
1278 GLfloat *tempSrc =
1279 malloc(width * height * 4 * sizeof(GLfloat));
1280
1281 if (tempSrc && texDest) {
1282 const GLint dims = 2;
1283 GLint dstRowStride;
1284 struct gl_texture_image *texImage = &stImage->base;
1285 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1286
1287 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1288 unpack.Invert = GL_TRUE;
1289 }
1290
1291 if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1292 dstRowStride = transfer->layer_stride;
1293 }
1294 else {
1295 dstRowStride = transfer->stride;
1296 }
1297
1298 /* get float/RGBA image from framebuffer */
1299 /* XXX this usually involves a lot of int/float conversion.
1300 * try to avoid that someday.
1301 */
1302 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
1303 util_format_linear(strb->texture->format),
1304 tempSrc);
1305
1306 /* Store into texture memory.
1307 * Note that this does some special things such as pixel transfer
1308 * ops and format conversion. In particular, if the dest tex format
1309 * is actually RGBA but the user created the texture as GL_RGB we
1310 * need to fill-in/override the alpha channel with 1.0.
1311 */
1312 _mesa_texstore(ctx, dims,
1313 texImage->_BaseFormat,
1314 texImage->TexFormat,
1315 dstRowStride,
1316 &texDest,
1317 width, height, 1,
1318 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1319 &unpack);
1320 }
1321 else {
1322 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1323 }
1324
1325 free(tempSrc);
1326 }
1327
1328 st_texture_image_unmap(st, stImage, slice);
1329 pipe->transfer_unmap(pipe, src_trans);
1330 }
1331
1332
1333 /**
1334 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1335 * Note that the region to copy has already been clipped so we know we
1336 * won't read from outside the source renderbuffer's bounds.
1337 *
1338 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1339 */
1340 static void
1341 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
1342 struct gl_texture_image *texImage,
1343 GLint destX, GLint destY, GLint slice,
1344 struct gl_renderbuffer *rb,
1345 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
1346 {
1347 struct st_texture_image *stImage = st_texture_image(texImage);
1348 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1349 struct st_renderbuffer *strb = st_renderbuffer(rb);
1350 struct st_context *st = st_context(ctx);
1351 struct pipe_context *pipe = st->pipe;
1352 struct pipe_screen *screen = pipe->screen;
1353 struct pipe_blit_info blit;
1354 enum pipe_format dst_format;
1355 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1356 unsigned bind;
1357 GLint srcY0, srcY1;
1358
1359 assert(!_mesa_is_format_etc2(texImage->TexFormat));
1360
1361 if (!strb || !strb->surface || !stImage->pt) {
1362 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1363 return;
1364 }
1365
1366 if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
1367 texImage->TexFormat)) {
1368 goto fallback;
1369 }
1370
1371 /* The base internal format must match the mesa format, so make sure
1372 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1373 */
1374 if (texImage->_BaseFormat !=
1375 _mesa_get_format_base_format(texImage->TexFormat) ||
1376 rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
1377 goto fallback;
1378 }
1379
1380 /* Choose the destination format to match the TexImage behavior. */
1381 dst_format = util_format_linear(stImage->pt->format);
1382 dst_format = util_format_luminance_to_red(dst_format);
1383 dst_format = util_format_intensity_to_red(dst_format);
1384
1385 /* See if the destination format is supported. */
1386 if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
1387 texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
1388 bind = PIPE_BIND_DEPTH_STENCIL;
1389 }
1390 else {
1391 bind = PIPE_BIND_RENDER_TARGET;
1392 }
1393
1394 if (!dst_format ||
1395 !screen->is_format_supported(screen, dst_format, stImage->pt->target,
1396 stImage->pt->nr_samples, bind)) {
1397 goto fallback;
1398 }
1399
1400 /* Y flipping for the main framebuffer. */
1401 if (do_flip) {
1402 srcY1 = strb->Base.Height - srcY - height;
1403 srcY0 = srcY1 + height;
1404 }
1405 else {
1406 srcY0 = srcY;
1407 srcY1 = srcY0 + height;
1408 }
1409
1410 /* Blit the texture.
1411 * This supports flipping, format conversions, and downsampling.
1412 */
1413 memset(&blit, 0, sizeof(blit));
1414 blit.src.resource = strb->texture;
1415 blit.src.format = util_format_linear(strb->surface->format);
1416 blit.src.level = strb->surface->u.tex.level;
1417 blit.src.box.x = srcX;
1418 blit.src.box.y = srcY0;
1419 blit.src.box.z = strb->surface->u.tex.first_layer;
1420 blit.src.box.width = width;
1421 blit.src.box.height = srcY1 - srcY0;
1422 blit.src.box.depth = 1;
1423 blit.dst.resource = stImage->pt;
1424 blit.dst.format = dst_format;
1425 blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level;
1426 blit.dst.box.x = destX;
1427 blit.dst.box.y = destY;
1428 blit.dst.box.z = stImage->base.Face + slice;
1429 blit.dst.box.width = width;
1430 blit.dst.box.height = height;
1431 blit.dst.box.depth = 1;
1432 blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
1433 blit.filter = PIPE_TEX_FILTER_NEAREST;
1434 pipe->blit(pipe, &blit);
1435 return;
1436
1437 fallback:
1438 /* software fallback */
1439 fallback_copy_texsubimage(ctx,
1440 strb, stImage, texImage->_BaseFormat,
1441 destX, destY, slice,
1442 srcX, srcY, width, height);
1443 }
1444
1445
1446 /**
1447 * Copy image data from stImage into the texture object 'stObj' at level
1448 * 'dstLevel'.
1449 */
1450 static void
1451 copy_image_data_to_texture(struct st_context *st,
1452 struct st_texture_object *stObj,
1453 GLuint dstLevel,
1454 struct st_texture_image *stImage)
1455 {
1456 /* debug checks */
1457 {
1458 const struct gl_texture_image *dstImage =
1459 stObj->base.Image[stImage->base.Face][dstLevel];
1460 assert(dstImage);
1461 assert(dstImage->Width == stImage->base.Width);
1462 assert(dstImage->Height == stImage->base.Height);
1463 assert(dstImage->Depth == stImage->base.Depth);
1464 }
1465
1466 if (stImage->pt) {
1467 /* Copy potentially with the blitter:
1468 */
1469 GLuint src_level;
1470 if (stImage->pt->last_level == 0)
1471 src_level = 0;
1472 else
1473 src_level = stImage->base.Level;
1474
1475 assert(src_level <= stImage->pt->last_level);
1476 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1477 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1478 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1479 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1480 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
1481 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1482
1483 st_texture_image_copy(st->pipe,
1484 stObj->pt, dstLevel, /* dest texture, level */
1485 stImage->pt, src_level, /* src texture, level */
1486 stImage->base.Face);
1487
1488 pipe_resource_reference(&stImage->pt, NULL);
1489 }
1490 else if (stImage->TexData) {
1491 /* Copy from malloc'd memory */
1492 /* XXX this should be re-examined/tested with a compressed format */
1493 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1494 GLuint srcRowStride = stImage->base.Width * blockSize;
1495 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1496 st_texture_image_data(st,
1497 stObj->pt,
1498 stImage->base.Face,
1499 dstLevel,
1500 stImage->TexData,
1501 srcRowStride,
1502 srcSliceStride);
1503 _mesa_align_free(stImage->TexData);
1504 stImage->TexData = NULL;
1505 }
1506
1507 pipe_resource_reference(&stImage->pt, stObj->pt);
1508 }
1509
1510
1511 /**
1512 * Called during state validation. When this function is finished,
1513 * the texture object should be ready for rendering.
1514 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1515 */
1516 GLboolean
1517 st_finalize_texture(struct gl_context *ctx,
1518 struct pipe_context *pipe,
1519 struct gl_texture_object *tObj)
1520 {
1521 struct st_context *st = st_context(ctx);
1522 struct st_texture_object *stObj = st_texture_object(tObj);
1523 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1524 GLuint face;
1525 struct st_texture_image *firstImage;
1526 enum pipe_format firstImageFormat;
1527 GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
1528
1529 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1530 /* The texture is complete and we know exactly how many mipmap levels
1531 * are present/needed. This is conditional because we may be called
1532 * from the st_generate_mipmap() function when the texture object is
1533 * incomplete. In that case, we'll have set stObj->lastLevel before
1534 * we get here.
1535 */
1536 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1537 stObj->base.Sampler.MinFilter == GL_NEAREST)
1538 stObj->lastLevel = stObj->base.BaseLevel;
1539 else
1540 stObj->lastLevel = stObj->base._MaxLevel;
1541 }
1542
1543 if (tObj->Target == GL_TEXTURE_BUFFER) {
1544 struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
1545
1546 if (!st_obj) {
1547 pipe_resource_reference(&stObj->pt, NULL);
1548 st_texture_release_all_sampler_views(stObj);
1549 return GL_TRUE;
1550 }
1551
1552 if (st_obj->buffer != stObj->pt) {
1553 pipe_resource_reference(&stObj->pt, st_obj->buffer);
1554 st_texture_release_all_sampler_views(stObj);
1555 stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
1556 stObj->height0 = 1;
1557 stObj->depth0 = 1;
1558 }
1559 return GL_TRUE;
1560
1561 }
1562
1563 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1564 assert(firstImage);
1565
1566 /* If both firstImage and stObj point to a texture which can contain
1567 * all active images, favour firstImage. Note that because of the
1568 * completeness requirement, we know that the image dimensions
1569 * will match.
1570 */
1571 if (firstImage->pt &&
1572 firstImage->pt != stObj->pt &&
1573 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1574 pipe_resource_reference(&stObj->pt, firstImage->pt);
1575 st_texture_release_all_sampler_views(stObj);
1576 }
1577
1578 /* If this texture comes from a window system, there is nothing else to do. */
1579 if (stObj->surface_based) {
1580 return GL_TRUE;
1581 }
1582
1583 /* Find gallium format for the Mesa texture */
1584 firstImageFormat =
1585 st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
1586
1587 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1588 {
1589 GLuint width, height, depth;
1590 if (!guess_base_level_size(stObj->base.Target,
1591 firstImage->base.Width2,
1592 firstImage->base.Height2,
1593 firstImage->base.Depth2,
1594 firstImage->base.Level,
1595 &width, &height, &depth)) {
1596 width = stObj->width0;
1597 height = stObj->height0;
1598 depth = stObj->depth0;
1599 }
1600 /* convert GL dims to Gallium dims */
1601 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1602 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1603 ptNumSamples = firstImage->base.NumSamples;
1604 }
1605
1606 /* If we already have a gallium texture, check that it matches the texture
1607 * object's format, target, size, num_levels, etc.
1608 */
1609 if (stObj->pt) {
1610 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1611 stObj->pt->format != firstImageFormat ||
1612 stObj->pt->last_level < stObj->lastLevel ||
1613 stObj->pt->width0 != ptWidth ||
1614 stObj->pt->height0 != ptHeight ||
1615 stObj->pt->depth0 != ptDepth ||
1616 stObj->pt->nr_samples != ptNumSamples ||
1617 stObj->pt->array_size != ptLayers)
1618 {
1619 /* The gallium texture does not match the Mesa texture so delete the
1620 * gallium texture now. We'll make a new one below.
1621 */
1622 pipe_resource_reference(&stObj->pt, NULL);
1623 st_texture_release_all_sampler_views(stObj);
1624 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1625 }
1626 }
1627
1628 /* May need to create a new gallium texture:
1629 */
1630 if (!stObj->pt) {
1631 GLuint bindings = default_bindings(st, firstImageFormat);
1632
1633 stObj->pt = st_texture_create(st,
1634 gl_target_to_pipe(stObj->base.Target),
1635 firstImageFormat,
1636 stObj->lastLevel,
1637 ptWidth,
1638 ptHeight,
1639 ptDepth,
1640 ptLayers, ptNumSamples,
1641 bindings);
1642
1643 if (!stObj->pt) {
1644 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1645 return GL_FALSE;
1646 }
1647 }
1648
1649 /* Pull in any images not in the object's texture:
1650 */
1651 for (face = 0; face < nr_faces; face++) {
1652 GLuint level;
1653 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1654 struct st_texture_image *stImage =
1655 st_texture_image(stObj->base.Image[face][level]);
1656
1657 /* Need to import images in main memory or held in other textures.
1658 */
1659 if (stImage && stObj->pt != stImage->pt) {
1660 if (level == 0 ||
1661 (stImage->base.Width == u_minify(stObj->width0, level) &&
1662 stImage->base.Height == u_minify(stObj->height0, level) &&
1663 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1664 /* src image fits expected dest mipmap level size */
1665 copy_image_data_to_texture(st, stObj, level, stImage);
1666 }
1667 }
1668 }
1669 }
1670
1671 return GL_TRUE;
1672 }
1673
1674
1675 /**
1676 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1677 * for a whole mipmap stack.
1678 */
1679 static GLboolean
1680 st_AllocTextureStorage(struct gl_context *ctx,
1681 struct gl_texture_object *texObj,
1682 GLsizei levels, GLsizei width,
1683 GLsizei height, GLsizei depth)
1684 {
1685 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1686 struct gl_texture_image *texImage = texObj->Image[0][0];
1687 struct st_context *st = st_context(ctx);
1688 struct st_texture_object *stObj = st_texture_object(texObj);
1689 struct pipe_screen *screen = st->pipe->screen;
1690 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1691 enum pipe_format fmt;
1692 GLint level;
1693 GLuint num_samples = texImage->NumSamples;
1694
1695 assert(levels > 0);
1696
1697 /* Save the level=0 dimensions */
1698 stObj->width0 = width;
1699 stObj->height0 = height;
1700 stObj->depth0 = depth;
1701 stObj->lastLevel = levels - 1;
1702
1703 fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
1704
1705 bindings = default_bindings(st, fmt);
1706
1707 /* Raise the sample count if the requested one is unsupported. */
1708 if (num_samples > 1) {
1709 boolean found = FALSE;
1710
1711 for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
1712 if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
1713 num_samples,
1714 PIPE_BIND_SAMPLER_VIEW)) {
1715 /* Update the sample count in gl_texture_image as well. */
1716 texImage->NumSamples = num_samples;
1717 found = TRUE;
1718 break;
1719 }
1720 }
1721
1722 if (!found) {
1723 return GL_FALSE;
1724 }
1725 }
1726
1727 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1728 width, height, depth,
1729 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1730
1731 stObj->pt = st_texture_create(st,
1732 gl_target_to_pipe(texObj->Target),
1733 fmt,
1734 levels - 1,
1735 ptWidth,
1736 ptHeight,
1737 ptDepth,
1738 ptLayers, num_samples,
1739 bindings);
1740 if (!stObj->pt)
1741 return GL_FALSE;
1742
1743 /* Set image resource pointers */
1744 for (level = 0; level < levels; level++) {
1745 GLuint face;
1746 for (face = 0; face < numFaces; face++) {
1747 struct st_texture_image *stImage =
1748 st_texture_image(texObj->Image[face][level]);
1749 pipe_resource_reference(&stImage->pt, stObj->pt);
1750 }
1751 }
1752
1753 return GL_TRUE;
1754 }
1755
1756
1757 static GLboolean
1758 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
1759 GLint level, mesa_format format,
1760 GLint width, GLint height,
1761 GLint depth, GLint border)
1762 {
1763 struct st_context *st = st_context(ctx);
1764 struct pipe_context *pipe = st->pipe;
1765
1766 if (width == 0 || height == 0 || depth == 0) {
1767 /* zero-sized images are legal, and always fit! */
1768 return GL_TRUE;
1769 }
1770
1771 if (pipe->screen->can_create_resource) {
1772 /* Ask the gallium driver if the texture is too large */
1773 struct gl_texture_object *texObj =
1774 _mesa_get_current_tex_object(ctx, target);
1775 struct pipe_resource pt;
1776
1777 /* Setup the pipe_resource object
1778 */
1779 memset(&pt, 0, sizeof(pt));
1780
1781 pt.target = gl_target_to_pipe(target);
1782 pt.format = st_mesa_format_to_pipe_format(st, format);
1783
1784 st_gl_texture_dims_to_pipe_dims(target,
1785 width, height, depth,
1786 &pt.width0, &pt.height0,
1787 &pt.depth0, &pt.array_size);
1788
1789 if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
1790 texObj->Sampler.MinFilter == GL_NEAREST)) {
1791 /* assume just one mipmap level */
1792 pt.last_level = 0;
1793 }
1794 else {
1795 /* assume a full set of mipmaps */
1796 pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
1797 }
1798
1799 return pipe->screen->can_create_resource(pipe->screen, &pt);
1800 }
1801 else {
1802 /* Use core Mesa fallback */
1803 return _mesa_test_proxy_teximage(ctx, target, level, format,
1804 width, height, depth, border);
1805 }
1806 }
1807
1808
1809 void
1810 st_init_texture_functions(struct dd_function_table *functions)
1811 {
1812 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1813 functions->QuerySamplesForFormat = st_QuerySamplesForFormat;
1814 functions->TexImage = st_TexImage;
1815 functions->TexSubImage = st_TexSubImage;
1816 functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1817 functions->CopyTexSubImage = st_CopyTexSubImage;
1818 functions->GenerateMipmap = st_generate_mipmap;
1819
1820 functions->GetTexImage = st_GetTexImage;
1821
1822 /* compressed texture functions */
1823 functions->CompressedTexImage = st_CompressedTexImage;
1824 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1825
1826 functions->NewTextureObject = st_NewTextureObject;
1827 functions->NewTextureImage = st_NewTextureImage;
1828 functions->DeleteTextureImage = st_DeleteTextureImage;
1829 functions->DeleteTexture = st_DeleteTextureObject;
1830 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1831 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1832 functions->MapTextureImage = st_MapTextureImage;
1833 functions->UnmapTextureImage = st_UnmapTextureImage;
1834
1835 /* XXX Temporary until we can query pipe's texture sizes */
1836 functions->TestProxyTexImage = st_TestProxyTexImage;
1837
1838 functions->AllocTextureStorage = st_AllocTextureStorage;
1839 }