+ return PIPE_FORMAT_NONE;
+}
+
+static bool
+reinterpret_formats(enum pipe_format *src_format, enum pipe_format *dst_format)
+{
+ enum pipe_format src = *src_format;
+ enum pipe_format dst = *dst_format;
+
+ /* Note: dst_format has already been transformed from luminance/intensity
+ * to red when this function is called. The source format will never
+ * be an intensity format, because GL_INTENSITY is not a legal value
+ * for the format parameter in glTex(Sub)Image(). */
+
+ if (format_is_alpha(src)) {
+ if (!format_is_alpha(dst))
+ return false;
+
+ src = alpha_to_red(src);
+ dst = alpha_to_red(dst);
+ } else if (format_is_luminance(src)) {
+ if (!format_is_red(dst) && !format_is_red_alpha(dst))
+ return false;
+
+ src = util_format_luminance_to_red(src);
+ } else if (util_format_is_luminance_alpha(src)) {
+ src = luminance_alpha_to_red_green(src);
+
+ if (format_is_red_alpha(dst)) {
+ dst = red_alpha_to_red_green(dst);
+ } else if (!format_is_red(dst))
+ return false;
+ } else if (format_is_swizzled_rgba(src)) {
+ const struct util_format_description *src_desc = util_format_description(src);
+ const struct util_format_description *dst_desc = util_format_description(dst);
+ int swizzle[4];
+ unsigned i;
+
+ /* Make sure the format is an RGBA and not an RGBX format */
+ if (src_desc->nr_channels != 4 || src_desc->swizzle[3] == PIPE_SWIZZLE_1)
+ return false;
+
+ if (dst_desc->nr_channels != 4 || dst_desc->swizzle[3] == PIPE_SWIZZLE_1)
+ return false;
+
+ for (i = 0; i < 4; i++)
+ swizzle[i] = dst_desc->swizzle[src_desc->swizzle[i]];
+
+ dst = swizzle_format(dst, swizzle);
+ if (dst == PIPE_FORMAT_NONE)
+ return false;
+
+ src = unswizzle_format(src);
+ }
+
+ *src_format = src;
+ *dst_format = dst;
+ return true;
+}
+
+static bool
+try_pbo_upload_common(struct gl_context *ctx,
+ struct pipe_surface *surface,
+ const struct st_pbo_addresses *addr,
+ enum pipe_format src_format)
+{
+ struct st_context *st = st_context(ctx);
+ struct cso_context *cso = st->cso_context;
+ struct pipe_context *pipe = st->pipe;
+ bool success = false;
+ void *fs;
+
+ fs = st_pbo_get_upload_fs(st, src_format, surface->format);
+ if (!fs)
+ return false;
+
+ cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
+ CSO_BIT_VERTEX_ELEMENTS |
+ CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
+ CSO_BIT_FRAMEBUFFER |
+ CSO_BIT_VIEWPORT |
+ CSO_BIT_BLEND |
+ CSO_BIT_DEPTH_STENCIL_ALPHA |
+ CSO_BIT_RASTERIZER |
+ CSO_BIT_STREAM_OUTPUTS |
+ CSO_BIT_PAUSE_QUERIES |
+ CSO_BIT_SAMPLE_MASK |
+ CSO_BIT_MIN_SAMPLES |
+ CSO_BIT_RENDER_CONDITION |
+ CSO_BITS_ALL_SHADERS));
+ cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
+
+ cso_set_sample_mask(cso, ~0);
+ cso_set_min_samples(cso, 1);
+ cso_set_render_condition(cso, NULL, FALSE, 0);
+
+ /* Set up the sampler_view */
+ {
+ struct pipe_sampler_view templ;
+ struct pipe_sampler_view *sampler_view;
+
+ memset(&templ, 0, sizeof(templ));
+ templ.target = PIPE_BUFFER;
+ templ.format = src_format;
+ templ.u.buf.offset = addr->first_element * addr->bytes_per_pixel;
+ templ.u.buf.size = (addr->last_element - addr->first_element + 1) *
+ addr->bytes_per_pixel;
+ templ.swizzle_r = PIPE_SWIZZLE_X;
+ templ.swizzle_g = PIPE_SWIZZLE_Y;
+ templ.swizzle_b = PIPE_SWIZZLE_Z;
+ templ.swizzle_a = PIPE_SWIZZLE_W;
+
+ sampler_view = pipe->create_sampler_view(pipe, addr->buffer, &templ);
+ if (sampler_view == NULL)
+ goto fail;
+
+ cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
+
+ pipe_sampler_view_reference(&sampler_view, NULL);
+ }
+
+ /* Framebuffer_state */
+ {
+ struct pipe_framebuffer_state fb;
+ memset(&fb, 0, sizeof(fb));
+ fb.width = surface->width;
+ fb.height = surface->height;
+ fb.nr_cbufs = 1;
+ fb.cbufs[0] = surface;
+
+ cso_set_framebuffer(cso, &fb);
+ }
+
+ cso_set_viewport_dims(cso, surface->width, surface->height, FALSE);
+
+ /* Blend state */
+ cso_set_blend(cso, &st->pbo.upload_blend);
+
+ /* Depth/stencil/alpha state */
+ {
+ struct pipe_depth_stencil_alpha_state dsa;
+ memset(&dsa, 0, sizeof(dsa));
+ cso_set_depth_stencil_alpha(cso, &dsa);
+ }
+
+ /* Set up the fragment shader */
+ cso_set_fragment_shader_handle(cso, fs);
+
+ success = st_pbo_draw(st, addr, surface->width, surface->height);
+
+fail:
+ cso_restore_state(cso);
+ cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
+
+ return success;
+}
+
+
+static bool
+try_pbo_upload(struct gl_context *ctx, GLuint dims,
+ struct gl_texture_image *texImage,
+ GLenum format, GLenum type,
+ enum pipe_format dst_format,
+ GLint xoffset, GLint yoffset, GLint zoffset,
+ GLint width, GLint height, GLint depth,
+ const void *pixels,
+ const struct gl_pixelstore_attrib *unpack)
+{
+ struct st_context *st = st_context(ctx);
+ struct st_texture_image *stImage = st_texture_image(texImage);
+ struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
+ struct pipe_resource *texture = stImage->pt;
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_screen *screen = pipe->screen;
+ struct pipe_surface *surface = NULL;
+ struct st_pbo_addresses addr;
+ enum pipe_format src_format;
+ const struct util_format_description *desc;
+ GLenum gl_target = texImage->TexObject->Target;
+ bool success;
+
+ if (!st->pbo.upload_enabled)
+ return false;
+
+ /* From now on, we need the gallium representation of dimensions. */
+ if (gl_target == GL_TEXTURE_1D_ARRAY) {
+ depth = height;
+ height = 1;
+ zoffset = yoffset;
+ yoffset = 0;
+ }
+
+ if (depth != 1 && !st->pbo.layers)
+ return false;
+
+ /* Choose the source format. Initially, we do so without checking driver
+ * support at all because of the remapping we later perform and because
+ * at least the Radeon driver actually supports some formats for texture
+ * buffers which it doesn't support for regular textures. */
+ src_format = st_choose_matching_format(st, 0, format, type,
+ unpack->SwapBytes);
+ if (!src_format) {
+ return false;
+ }
+
+ src_format = util_format_linear(src_format);
+ desc = util_format_description(src_format);
+
+ if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
+ return false;
+
+ if (desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
+ return false;
+
+ if (st->pbo.rgba_only) {
+ enum pipe_format orig_dst_format = dst_format;
+
+ if (!reinterpret_formats(&src_format, &dst_format)) {
+ return false;
+ }
+
+ if (dst_format != orig_dst_format &&
+ !screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D, 0,
+ 0, PIPE_BIND_RENDER_TARGET)) {
+ return false;
+ }
+ }
+
+ if (!src_format ||
+ !screen->is_format_supported(screen, src_format, PIPE_BUFFER, 0, 0,
+ PIPE_BIND_SAMPLER_VIEW)) {
+ return false;
+ }
+
+ /* Compute buffer addresses */
+ addr.xoffset = xoffset;
+ addr.yoffset = yoffset;
+ addr.width = width;
+ addr.height = height;
+ addr.depth = depth;
+ addr.bytes_per_pixel = desc->block.bits / 8;
+
+ if (!st_pbo_addresses_pixelstore(st, gl_target, dims == 3, unpack, pixels,
+ &addr))
+ return false;
+
+ /* Set up the surface */
+ {
+ unsigned level = stObj->pt != stImage->pt
+ ? 0 : texImage->TexObject->MinLevel + texImage->Level;
+ unsigned max_layer = util_max_layer(texture, level);
+
+ zoffset += texImage->Face + texImage->TexObject->MinLayer;
+
+ struct pipe_surface templ;
+ memset(&templ, 0, sizeof(templ));
+ templ.format = dst_format;
+ templ.u.tex.level = level;
+ templ.u.tex.first_layer = MIN2(zoffset, max_layer);
+ templ.u.tex.last_layer = MIN2(zoffset + depth - 1, max_layer);
+
+ surface = pipe->create_surface(pipe, texture, &templ);
+ if (!surface)
+ return false;
+ }
+
+ success = try_pbo_upload_common(ctx, surface, &addr, src_format);
+
+ pipe_surface_reference(&surface, NULL);
+
+ return success;
+}
+
+
+static void
+st_TexSubImage(struct gl_context *ctx, GLuint dims,
+ struct gl_texture_image *texImage,
+ GLint xoffset, GLint yoffset, GLint zoffset,
+ GLint width, GLint height, GLint depth,
+ GLenum format, GLenum type, const void *pixels,
+ const struct gl_pixelstore_attrib *unpack)
+{
+ struct st_context *st = st_context(ctx);
+ struct st_texture_image *stImage = st_texture_image(texImage);
+ struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_screen *screen = pipe->screen;
+ struct pipe_resource *dst = stImage->pt;
+ struct pipe_resource *src = NULL;
+ struct pipe_resource src_templ;
+ struct pipe_transfer *transfer;
+ struct pipe_blit_info blit;
+ enum pipe_format src_format, dst_format;
+ mesa_format mesa_src_format;
+ GLenum gl_target = texImage->TexObject->Target;
+ unsigned bind;
+ GLubyte *map;
+ unsigned dstz = texImage->Face + texImage->TexObject->MinLayer;
+ unsigned dst_level = 0;
+ bool throttled = false;
+
+ st_flush_bitmap_cache(st);
+ st_invalidate_readpix_cache(st);
+
+ if (stObj->pt == stImage->pt)
+ dst_level = texImage->TexObject->MinLevel + texImage->Level;
+
+ assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
+ !_mesa_is_format_astc_2d(texImage->TexFormat) &&
+ texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
+
+ if (!dst)
+ goto fallback;
+
+ /* Try texture_subdata, which should be the fastest memcpy path. */
+ if (pixels &&
+ !_mesa_is_bufferobj(unpack->BufferObj) &&
+ _mesa_texstore_can_use_memcpy(ctx, texImage->_BaseFormat,
+ texImage->TexFormat, format, type,
+ unpack)) {
+ struct pipe_box box;
+ unsigned stride, layer_stride;
+ void *data;
+
+ stride = _mesa_image_row_stride(unpack, width, format, type);
+ layer_stride = _mesa_image_image_stride(unpack, width, height, format,
+ type);
+ data = _mesa_image_address(dims, unpack, pixels, width, height, format,
+ type, 0, 0, 0);
+
+ /* Convert to Gallium coordinates. */
+ if (gl_target == GL_TEXTURE_1D_ARRAY) {
+ zoffset = yoffset;
+ yoffset = 0;
+ depth = height;
+ height = 1;
+ layer_stride = stride;
+ }
+
+ util_throttle_memory_usage(pipe, &st->throttle,
+ width * height * depth *
+ util_format_get_blocksize(dst->format));
+
+ u_box_3d(xoffset, yoffset, zoffset + dstz, width, height, depth, &box);
+ pipe->texture_subdata(pipe, dst, dst_level, 0,
+ &box, data, stride, layer_stride);
+ return;
+ }
+
+ if (!st->prefer_blit_based_texture_transfer) {
+ goto fallback;
+ }
+
+ /* XXX Fallback for depth-stencil formats due to an incomplete stencil
+ * blit implementation in some drivers. */
+ if (format == GL_DEPTH_STENCIL) {
+ goto fallback;
+ }
+
+ /* If the base internal format and the texture format don't match,
+ * we can't use blit-based TexSubImage. */
+ if (texImage->_BaseFormat !=
+ _mesa_get_format_base_format(texImage->TexFormat)) {
+ goto fallback;
+ }
+
+
+ /* See if the destination format is supported. */
+ if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
+ bind = PIPE_BIND_DEPTH_STENCIL;
+ else
+ bind = PIPE_BIND_RENDER_TARGET;
+
+ /* For luminance and intensity, only the red channel is stored
+ * in the destination. */
+ dst_format = util_format_linear(dst->format);
+ dst_format = util_format_luminance_to_red(dst_format);
+ dst_format = util_format_intensity_to_red(dst_format);
+
+ if (!dst_format ||
+ !screen->is_format_supported(screen, dst_format, dst->target,
+ dst->nr_samples, dst->nr_storage_samples,
+ bind)) {
+ goto fallback;
+ }
+
+ if (_mesa_is_bufferobj(unpack->BufferObj)) {
+ if (try_pbo_upload(ctx, dims, texImage, format, type, dst_format,
+ xoffset, yoffset, zoffset,
+ width, height, depth, pixels, unpack))
+ return;
+ }
+
+ /* See if the texture format already matches the format and type,
+ * in which case the memcpy-based fast path will likely be used and
+ * we don't have to blit. */
+ if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
+ type, unpack->SwapBytes, NULL)) {
+ goto fallback;
+ }
+
+ /* Choose the source format. */
+ src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
+ format, type, unpack->SwapBytes);
+ if (!src_format) {
+ goto fallback;
+ }
+
+ mesa_src_format = st_pipe_format_to_mesa_format(src_format);
+
+ /* There is no reason to do this if we cannot use memcpy for the temporary
+ * source texture at least. This also takes transfer ops into account,
+ * etc. */
+ if (!_mesa_texstore_can_use_memcpy(ctx,
+ _mesa_get_format_base_format(mesa_src_format),
+ mesa_src_format, format, type, unpack)) {
+ goto fallback;
+ }
+
+ /* TexSubImage only sets a single cubemap face. */
+ if (gl_target == GL_TEXTURE_CUBE_MAP) {
+ gl_target = GL_TEXTURE_2D;
+ }
+ /* TexSubImage can specify subsets of cube map array faces
+ * so we need to upload via 2D array instead */
+ if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
+ gl_target = GL_TEXTURE_2D_ARRAY;
+ }
+
+ /* Initialize the source texture description. */
+ memset(&src_templ, 0, sizeof(src_templ));
+ src_templ.target = gl_target_to_pipe(gl_target);
+ src_templ.format = src_format;
+ src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
+ src_templ.usage = PIPE_USAGE_STAGING;
+
+ st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
+ &src_templ.width0, &src_templ.height0,
+ &src_templ.depth0, &src_templ.array_size);
+
+ /* Check for NPOT texture support. */
+ if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
+ (!util_is_power_of_two_or_zero(src_templ.width0) ||
+ !util_is_power_of_two_or_zero(src_templ.height0) ||
+ !util_is_power_of_two_or_zero(src_templ.depth0))) {
+ goto fallback;
+ }
+
+ util_throttle_memory_usage(pipe, &st->throttle,
+ width * height * depth *
+ util_format_get_blocksize(src_templ.format));
+ throttled = true;
+
+ /* Create the source texture. */
+ src = screen->resource_create(screen, &src_templ);
+ if (!src) {
+ goto fallback;
+ }
+
+ /* Map source pixels. */
+ pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
+ format, type, pixels, unpack,
+ "glTexSubImage");
+ if (!pixels) {
+ /* This is a GL error. */
+ pipe_resource_reference(&src, NULL);
+ return;
+ }
+
+ /* From now on, we need the gallium representation of dimensions. */
+ if (gl_target == GL_TEXTURE_1D_ARRAY) {
+ zoffset = yoffset;
+ yoffset = 0;
+ depth = height;
+ height = 1;
+ }
+
+ map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
+ width, height, depth, &transfer);
+ if (!map) {
+ _mesa_unmap_teximage_pbo(ctx, unpack);
+ pipe_resource_reference(&src, NULL);
+ goto fallback;
+ }
+
+ /* Upload pixels (just memcpy). */
+ {
+ const uint bytesPerRow = width * util_format_get_blocksize(src_format);
+ GLuint row, slice;
+
+ for (slice = 0; slice < (unsigned) depth; slice++) {
+ if (gl_target == GL_TEXTURE_1D_ARRAY) {
+ /* 1D array textures.
+ * We need to convert gallium coords to GL coords.
+ */
+ void *src = _mesa_image_address2d(unpack, pixels,
+ width, depth, format,
+ type, slice, 0);
+ memcpy(map, src, bytesPerRow);
+ }
+ else {
+ ubyte *slice_map = map;
+
+ for (row = 0; row < (unsigned) height; row++) {
+ void *src = _mesa_image_address(dims, unpack, pixels,
+ width, height, format,
+ type, slice, row, 0);
+ memcpy(slice_map, src, bytesPerRow);
+ slice_map += transfer->stride;
+ }
+ }
+ map += transfer->layer_stride;
+ }
+ }
+
+ pipe_transfer_unmap(pipe, transfer);
+ _mesa_unmap_teximage_pbo(ctx, unpack);
+
+ /* Blit. */
+ memset(&blit, 0, sizeof(blit));
+ blit.src.resource = src;
+ blit.src.level = 0;
+ blit.src.format = src_format;
+ blit.dst.resource = dst;
+ blit.dst.level = dst_level;
+ blit.dst.format = dst_format;
+ blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
+ blit.dst.box.x = xoffset;
+ blit.dst.box.y = yoffset;
+ blit.dst.box.z = zoffset + dstz;
+ blit.src.box.width = blit.dst.box.width = width;
+ blit.src.box.height = blit.dst.box.height = height;
+ blit.src.box.depth = blit.dst.box.depth = depth;
+ blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+ blit.scissor_enable = FALSE;
+
+ st->pipe->blit(st->pipe, &blit);
+
+ pipe_resource_reference(&src, NULL);
+ return;
+
+fallback:
+ if (!throttled) {
+ util_throttle_memory_usage(pipe, &st->throttle,
+ width * height * depth *
+ _mesa_get_format_bytes(texImage->TexFormat));
+ }
+ _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
+ width, height, depth, format, type, pixels,
+ unpack);
+}
+
+
+static void
+st_TexImage(struct gl_context * ctx, GLuint dims,
+ struct gl_texture_image *texImage,
+ GLenum format, GLenum type, const void *pixels,
+ const struct gl_pixelstore_attrib *unpack)
+{
+ assert(dims == 1 || dims == 2 || dims == 3);
+
+ prep_teximage(ctx, texImage, format, type);
+
+ if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
+ return;
+
+ /* allocate storage for texture data */
+ if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
+ return;
+ }
+
+ st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
+ texImage->Width, texImage->Height, texImage->Depth,
+ format, type, pixels, unpack);
+}
+
+
+static void
+st_CompressedTexSubImage(struct gl_context *ctx, GLuint dims,
+ struct gl_texture_image *texImage,
+ GLint x, GLint y, GLint z,
+ GLsizei w, GLsizei h, GLsizei d,
+ GLenum format, GLsizei imageSize, const void *data)
+{
+ struct st_context *st = st_context(ctx);
+ struct st_texture_image *stImage = st_texture_image(texImage);
+ struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
+ struct pipe_resource *texture = stImage->pt;
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_screen *screen = pipe->screen;
+ struct pipe_resource *dst = stImage->pt;
+ struct pipe_surface *surface = NULL;
+ struct compressed_pixelstore store;
+ struct st_pbo_addresses addr;
+ enum pipe_format copy_format;
+ unsigned bw, bh;
+ intptr_t buf_offset;
+ bool success = false;
+
+ /* Check basic pre-conditions for PBO upload */
+ if (!st->prefer_blit_based_texture_transfer) {
+ goto fallback;
+ }
+
+ if (!_mesa_is_bufferobj(ctx->Unpack.BufferObj))
+ goto fallback;
+
+ if (st_compressed_format_fallback(st, texImage->TexFormat))
+ goto fallback;
+
+ if (!dst) {
+ goto fallback;
+ }
+
+ if (!st->pbo.upload_enabled ||
+ !screen->get_param(screen, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS)) {
+ goto fallback;
+ }
+
+ /* Choose the pipe format for the upload. */
+ addr.bytes_per_pixel = util_format_get_blocksize(dst->format);
+ bw = util_format_get_blockwidth(dst->format);
+ bh = util_format_get_blockheight(dst->format);
+
+ switch (addr.bytes_per_pixel) {
+ case 8:
+ copy_format = PIPE_FORMAT_R16G16B16A16_UINT;
+ break;
+ case 16:
+ copy_format = PIPE_FORMAT_R32G32B32A32_UINT;
+ break;
+ default:
+ goto fallback;
+ }
+
+ if (!screen->is_format_supported(screen, copy_format, PIPE_BUFFER, 0, 0,
+ PIPE_BIND_SAMPLER_VIEW)) {
+ goto fallback;
+ }
+
+ if (!screen->is_format_supported(screen, copy_format, dst->target,
+ dst->nr_samples, dst->nr_storage_samples,
+ PIPE_BIND_RENDER_TARGET)) {
+ goto fallback;
+ }
+
+ /* Interpret the pixelstore settings. */
+ _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat, w, h, d,
+ &ctx->Unpack, &store);
+ assert(store.CopyBytesPerRow % addr.bytes_per_pixel == 0);
+ assert(store.SkipBytes % addr.bytes_per_pixel == 0);
+
+ /* Compute the offset into the buffer */
+ buf_offset = (intptr_t)data + store.SkipBytes;
+
+ if (buf_offset % addr.bytes_per_pixel) {
+ goto fallback;
+ }
+
+ buf_offset = buf_offset / addr.bytes_per_pixel;
+
+ addr.xoffset = x / bw;
+ addr.yoffset = y / bh;
+ addr.width = store.CopyBytesPerRow / addr.bytes_per_pixel;
+ addr.height = store.CopyRowsPerSlice;
+ addr.depth = d;
+ addr.pixels_per_row = store.TotalBytesPerRow / addr.bytes_per_pixel;
+ addr.image_height = store.TotalRowsPerSlice;
+
+ if (!st_pbo_addresses_setup(st,
+ st_buffer_object(ctx->Unpack.BufferObj)->buffer,
+ buf_offset, &addr))
+ goto fallback;
+
+ /* Set up the surface. */
+ {
+ unsigned level = stObj->pt != stImage->pt
+ ? 0 : texImage->TexObject->MinLevel + texImage->Level;
+ unsigned max_layer = util_max_layer(texture, level);
+
+ z += texImage->Face + texImage->TexObject->MinLayer;
+
+ struct pipe_surface templ;
+ memset(&templ, 0, sizeof(templ));
+ templ.format = copy_format;
+ templ.u.tex.level = level;
+ templ.u.tex.first_layer = MIN2(z, max_layer);
+ templ.u.tex.last_layer = MIN2(z + d - 1, max_layer);
+
+ surface = pipe->create_surface(pipe, texture, &templ);
+ if (!surface)
+ goto fallback;
+ }
+
+ success = try_pbo_upload_common(ctx, surface, &addr, copy_format);
+
+ pipe_surface_reference(&surface, NULL);
+
+ if (success)
+ return;
+
+fallback:
+ _mesa_store_compressed_texsubimage(ctx, dims, texImage,
+ x, y, z, w, h, d,
+ format, imageSize, data);
+}
+
+
+static void
+st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
+ struct gl_texture_image *texImage,
+ GLsizei imageSize, const void *data)
+{
+ prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
+
+ /* only 2D and 3D compressed images are supported at this time */
+ if (dims == 1) {
+ _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
+ return;
+ }
+
+ /* This is pretty simple, because unlike the general texstore path we don't
+ * have to worry about the usual image unpacking or image transfer
+ * operations.
+ */
+ assert(texImage);
+ assert(texImage->Width > 0);
+ assert(texImage->Height > 0);
+ assert(texImage->Depth > 0);
+
+ /* allocate storage for texture data */
+ if (!st_AllocTextureImageBuffer(ctx, texImage)) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
+ return;
+ }
+
+ st_CompressedTexSubImage(ctx, dims, texImage,
+ 0, 0, 0,
+ texImage->Width, texImage->Height, texImage->Depth,
+ texImage->TexFormat,
+ imageSize, data);
+}
+
+
+/**
+ * Called via ctx->Driver.GetTexSubImage()
+ *
+ * This uses a blit to copy the texture to a texture format which matches
+ * the format and type combo and then a fast read-back is done using memcpy.
+ * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
+ * a format which matches the swizzling.
+ *
+ * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
+ *
+ * NOTE: Drivers usually do a blit to convert between tiled and linear
+ * texture layouts during texture uploads/downloads, so the blit
+ * we do here should be free in such cases.
+ */
+static void
+st_GetTexSubImage(struct gl_context * ctx,
+ GLint xoffset, GLint yoffset, GLint zoffset,
+ GLsizei width, GLsizei height, GLint depth,
+ GLenum format, GLenum type, void * pixels,
+ struct gl_texture_image *texImage)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_screen *screen = pipe->screen;
+ struct st_texture_image *stImage = st_texture_image(texImage);
+ struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
+ struct pipe_resource *src = stObj->pt;
+ struct pipe_resource *dst = NULL;
+ struct pipe_resource dst_templ;
+ enum pipe_format dst_format, src_format;
+ mesa_format mesa_format;
+ GLenum gl_target = texImage->TexObject->Target;
+ enum pipe_texture_target pipe_target;
+ unsigned dims;
+ struct pipe_blit_info blit;
+ unsigned bind;
+ struct pipe_transfer *tex_xfer;
+ ubyte *map = NULL;
+ boolean done = FALSE;
+
+ assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
+ !_mesa_is_format_astc_2d(texImage->TexFormat) &&
+ texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
+
+ st_flush_bitmap_cache(st);
+
+ if (!st->prefer_blit_based_texture_transfer &&
+ !_mesa_is_format_compressed(texImage->TexFormat)) {
+ /* Try to avoid the fallback if we're doing texture decompression here */
+ goto fallback;
+ }
+
+ /* Handle non-finalized textures. */
+ if (!stImage->pt || stImage->pt != stObj->pt || !src) {
+ goto fallback;
+ }
+
+ /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
+ * due to an incomplete stencil blit implementation in some drivers. */
+ if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
+ goto fallback;
+ }
+
+ /* If the base internal format and the texture format don't match, we have
+ * to fall back to _mesa_GetTexImage_sw. */
+ if (texImage->_BaseFormat !=
+ _mesa_get_format_base_format(texImage->TexFormat)) {
+ goto fallback;
+ }
+
+ /* See if the texture format already matches the format and type,
+ * in which case the memcpy-based fast path will be used. */
+ if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
+ type, ctx->Pack.SwapBytes, NULL)) {
+ goto fallback;
+ }
+
+ /* Convert the source format to what is expected by GetTexImage
+ * and see if it's supported.
+ *
+ * This only applies to glGetTexImage:
+ * - Luminance must be returned as (L,0,0,1).
+ * - Luminance alpha must be returned as (L,0,0,A).
+ * - Intensity must be returned as (I,0,0,1)
+ */
+ if (stObj->surface_based)
+ src_format = util_format_linear(stObj->surface_format);
+ else
+ src_format = util_format_linear(src->format);
+ src_format = util_format_luminance_to_red(src_format);
+ src_format = util_format_intensity_to_red(src_format);
+
+ if (!src_format ||
+ !screen->is_format_supported(screen, src_format, src->target,
+ src->nr_samples, src->nr_storage_samples,
+ PIPE_BIND_SAMPLER_VIEW)) {
+ goto fallback;
+ }
+
+ if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
+ bind = PIPE_BIND_DEPTH_STENCIL;
+ else
+ bind = PIPE_BIND_RENDER_TARGET;
+
+ /* GetTexImage only returns a single face for cubemaps. */
+ if (gl_target == GL_TEXTURE_CUBE_MAP) {
+ gl_target = GL_TEXTURE_2D;
+ }
+ pipe_target = gl_target_to_pipe(gl_target);
+
+ /* Choose the destination format by finding the best match
+ * for the format+type combo. */
+ dst_format = st_choose_matching_format(st, bind, format, type,
+ ctx->Pack.SwapBytes);
+
+ if (dst_format == PIPE_FORMAT_NONE) {
+ GLenum dst_glformat;
+
+ /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
+ * where decompression with a blit is always preferred. */
+ if (!util_format_is_compressed(src->format)) {
+ goto fallback;
+ }
+
+ /* Set the appropriate format for the decompressed texture.
+ * Luminance and sRGB formats shouldn't appear here.*/
+ switch (src_format) {
+ case PIPE_FORMAT_DXT1_RGB:
+ case PIPE_FORMAT_DXT1_RGBA:
+ case PIPE_FORMAT_DXT3_RGBA:
+ case PIPE_FORMAT_DXT5_RGBA:
+ case PIPE_FORMAT_RGTC1_UNORM:
+ case PIPE_FORMAT_RGTC2_UNORM:
+ case PIPE_FORMAT_ETC1_RGB8:
+ case PIPE_FORMAT_ETC2_RGB8:
+ case PIPE_FORMAT_ETC2_RGB8A1:
+ case PIPE_FORMAT_ETC2_RGBA8:
+ case PIPE_FORMAT_ASTC_4x4:
+ case PIPE_FORMAT_ASTC_5x4:
+ case PIPE_FORMAT_ASTC_5x5:
+ case PIPE_FORMAT_ASTC_6x5:
+ case PIPE_FORMAT_ASTC_6x6:
+ case PIPE_FORMAT_ASTC_8x5:
+ case PIPE_FORMAT_ASTC_8x6:
+ case PIPE_FORMAT_ASTC_8x8:
+ case PIPE_FORMAT_ASTC_10x5:
+ case PIPE_FORMAT_ASTC_10x6:
+ case PIPE_FORMAT_ASTC_10x8:
+ case PIPE_FORMAT_ASTC_10x10:
+ case PIPE_FORMAT_ASTC_12x10:
+ case PIPE_FORMAT_ASTC_12x12:
+ case PIPE_FORMAT_BPTC_RGBA_UNORM:
+ dst_glformat = GL_RGBA8;
+ break;
+ case PIPE_FORMAT_RGTC1_SNORM:
+ case PIPE_FORMAT_RGTC2_SNORM:
+ if (!ctx->Extensions.EXT_texture_snorm)
+ goto fallback;
+ dst_glformat = GL_RGBA8_SNORM;
+ break;
+ case PIPE_FORMAT_BPTC_RGB_FLOAT:
+ case PIPE_FORMAT_BPTC_RGB_UFLOAT:
+ if (!ctx->Extensions.ARB_texture_float)
+ goto fallback;
+ dst_glformat = GL_RGBA32F;
+ break;
+ case PIPE_FORMAT_ETC2_R11_UNORM:
+ if (!screen->is_format_supported(screen, PIPE_FORMAT_R16_UNORM,
+ pipe_target, 0, 0, bind))
+ goto fallback;
+ dst_glformat = GL_R16;
+ break;
+ case PIPE_FORMAT_ETC2_R11_SNORM:
+ if (!screen->is_format_supported(screen, PIPE_FORMAT_R16_SNORM,
+ pipe_target, 0, 0, bind))
+ goto fallback;
+ dst_glformat = GL_R16_SNORM;
+ break;
+ case PIPE_FORMAT_ETC2_RG11_UNORM:
+ if (!screen->is_format_supported(screen, PIPE_FORMAT_R16G16_UNORM,
+ pipe_target, 0, 0, bind))
+ goto fallback;
+ dst_glformat = GL_RG16;
+ break;
+ case PIPE_FORMAT_ETC2_RG11_SNORM:
+ if (!screen->is_format_supported(screen, PIPE_FORMAT_R16G16_SNORM,
+ pipe_target, 0, 0, bind))
+ goto fallback;
+ dst_glformat = GL_RG16_SNORM;
+ break;
+ default:
+ assert(0);
+ goto fallback;
+ }
+
+ dst_format = st_choose_format(st, dst_glformat, format, type,
+ pipe_target, 0, 0, bind, FALSE);
+
+ if (dst_format == PIPE_FORMAT_NONE) {
+ /* unable to get an rgba format!?! */
+ goto fallback;
+ }
+ }
+
+ /* create the destination texture of size (width X height X depth) */
+ memset(&dst_templ, 0, sizeof(dst_templ));
+ dst_templ.target = pipe_target;
+ dst_templ.format = dst_format;
+ dst_templ.bind = bind;
+ dst_templ.usage = PIPE_USAGE_STAGING;
+
+ st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
+ &dst_templ.width0, &dst_templ.height0,
+ &dst_templ.depth0, &dst_templ.array_size);
+
+ dst = screen->resource_create(screen, &dst_templ);
+ if (!dst) {
+ goto fallback;
+ }
+
+ /* From now on, we need the gallium representation of dimensions. */
+ if (gl_target == GL_TEXTURE_1D_ARRAY) {
+ zoffset = yoffset;
+ yoffset = 0;
+ depth = height;
+ height = 1;
+ }
+
+ assert(texImage->Face == 0 ||
+ texImage->TexObject->MinLayer == 0 ||
+ zoffset == 0);
+
+ memset(&blit, 0, sizeof(blit));
+ blit.src.resource = src;
+ blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
+ blit.src.format = src_format;
+ blit.dst.resource = dst;
+ blit.dst.level = 0;
+ blit.dst.format = dst->format;
+ blit.src.box.x = xoffset;
+ blit.dst.box.x = 0;
+ blit.src.box.y = yoffset;
+ blit.dst.box.y = 0;
+ blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
+ blit.dst.box.z = 0;
+ blit.src.box.width = blit.dst.box.width = width;
+ blit.src.box.height = blit.dst.box.height = height;
+ blit.src.box.depth = blit.dst.box.depth = depth;
+ blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+ blit.scissor_enable = FALSE;
+
+ /* blit/render/decompress */
+ st->pipe->blit(st->pipe, &blit);
+
+ pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
+
+ map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
+ 0, 0, 0, width, height, depth, &tex_xfer);
+ if (!map) {
+ goto end;
+ }
+
+ mesa_format = st_pipe_format_to_mesa_format(dst_format);
+ dims = _mesa_get_texture_dimensions(gl_target);
+
+ /* copy/pack data into user buffer */
+ if (_mesa_format_matches_format_and_type(mesa_format, format, type,
+ ctx->Pack.SwapBytes, NULL)) {
+ /* memcpy */
+ const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
+ GLuint row, slice;
+
+ for (slice = 0; slice < depth; slice++) {
+ ubyte *slice_map = map;
+
+ for (row = 0; row < height; row++) {
+ void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
+ width, height, format, type,
+ slice, row, 0);
+
+ memcpy(dest, slice_map, bytesPerRow);
+
+ slice_map += tex_xfer->stride;
+ }
+
+ map += tex_xfer->layer_stride;
+ }
+ }
+ else {
+ /* format translation via floats */
+ GLuint slice;
+ GLfloat *rgba;
+ uint32_t dstMesaFormat;
+ int dstStride, srcStride;
+
+ assert(util_format_is_compressed(src->format));
+
+ rgba = malloc(width * height * 4 * sizeof(GLfloat));
+ if (!rgba) {
+ goto end;
+ }
+
+ if (ST_DEBUG & DEBUG_FALLBACK)
+ debug_printf("%s: fallback format translation\n", __func__);
+
+ dstMesaFormat = _mesa_format_from_format_and_type(format, type);
+ dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
+ srcStride = 4 * width * sizeof(GLfloat);
+ for (slice = 0; slice < depth; slice++) {
+ void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
+ width, height, format, type,
+ slice, 0, 0);
+
+ /* get float[4] rgba row from surface */
+ pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, height,
+ dst_format, rgba);
+
+ _mesa_format_convert(dest, dstMesaFormat, dstStride,
+ rgba, RGBA32_FLOAT, srcStride,
+ width, height, NULL);
+
+ /* Handle byte swapping if required */
+ if (ctx->Pack.SwapBytes) {
+ _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
+ width, height, dest, dest);
+ }
+
+ map += tex_xfer->layer_stride;
+ }
+
+ free(rgba);
+ }
+ done = TRUE;
+
+end:
+ if (map)
+ pipe_transfer_unmap(pipe, tex_xfer);
+
+ _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
+ pipe_resource_reference(&dst, NULL);
+
+fallback:
+ if (!done) {
+ _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
+ width, height, depth,
+ format, type, pixels, texImage);
+ }
+}
+
+
+/**
+ * Do a CopyTexSubImage operation using a read transfer from the source,
+ * a write transfer to the destination and get_tile()/put_tile() to access
+ * the pixels/texels.
+ *
+ * Note: srcY=0=TOP of renderbuffer
+ */
+static void
+fallback_copy_texsubimage(struct gl_context *ctx,
+ struct st_renderbuffer *strb,
+ struct st_texture_image *stImage,
+ GLenum baseFormat,
+ GLint destX, GLint destY, GLint slice,
+ GLint srcX, GLint srcY,
+ GLsizei width, GLsizei height)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_transfer *src_trans;
+ GLubyte *texDest;
+ enum pipe_transfer_usage transfer_usage;
+ void *map;
+ unsigned dst_width = width;
+ unsigned dst_height = height;
+ unsigned dst_depth = 1;
+ struct pipe_transfer *transfer;
+
+ if (ST_DEBUG & DEBUG_FALLBACK)
+ debug_printf("%s: fallback processing\n", __func__);
+
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ srcY = strb->Base.Height - srcY - height;
+ }
+
+ map = pipe_transfer_map(pipe,
+ strb->texture,
+ strb->surface->u.tex.level,
+ strb->surface->u.tex.first_layer,
+ PIPE_TRANSFER_READ,
+ srcX, srcY,
+ width, height, &src_trans);
+
+ if ((baseFormat == GL_DEPTH_COMPONENT ||
+ baseFormat == GL_DEPTH_STENCIL) &&
+ util_format_is_depth_and_stencil(stImage->pt->format))
+ transfer_usage = PIPE_TRANSFER_READ_WRITE;
+ else
+ transfer_usage = PIPE_TRANSFER_WRITE;
+
+ texDest = st_texture_image_map(st, stImage, transfer_usage,
+ destX, destY, slice,
+ dst_width, dst_height, dst_depth,
+ &transfer);
+
+ if (baseFormat == GL_DEPTH_COMPONENT ||
+ baseFormat == GL_DEPTH_STENCIL) {
+ const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
+ ctx->Pixel.DepthBias != 0.0F);
+ GLint row, yStep;
+ uint *data;
+
+ /* determine bottom-to-top vs. top-to-bottom order for src buffer */
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ srcY = height - 1;
+ yStep = -1;
+ }
+ else {
+ srcY = 0;
+ yStep = 1;
+ }
+
+ data = malloc(width * sizeof(uint));
+
+ if (data) {