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