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