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