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