mesa/formats: remove compressed formats from matching function
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.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/image.h"
29 #include "main/pbo.h"
30 #include "main/imports.h"
31 #include "main/readpix.h"
32 #include "main/enums.h"
33 #include "main/framebuffer.h"
34 #include "util/u_inlines.h"
35 #include "util/u_format.h"
36
37 #include "st_cb_fbo.h"
38 #include "st_atom.h"
39 #include "st_context.h"
40 #include "st_cb_bitmap.h"
41 #include "st_cb_readpixels.h"
42 #include "state_tracker/st_cb_texture.h"
43 #include "state_tracker/st_format.h"
44 #include "state_tracker/st_texture.h"
45
46 static boolean
47 needs_integer_signed_unsigned_conversion(const struct gl_context *ctx,
48 GLenum format, GLenum type)
49 {
50 struct gl_renderbuffer *rb =
51 _mesa_get_read_renderbuffer_for_format(ctx, format);
52
53 assert(rb);
54
55 GLenum srcType = _mesa_get_format_datatype(rb->Format);
56
57 if ((srcType == GL_INT &&
58 (type == GL_UNSIGNED_INT ||
59 type == GL_UNSIGNED_SHORT ||
60 type == GL_UNSIGNED_BYTE)) ||
61 (srcType == GL_UNSIGNED_INT &&
62 (type == GL_INT ||
63 type == GL_SHORT ||
64 type == GL_BYTE))) {
65 return TRUE;
66 }
67
68 return FALSE;
69 }
70
71 /**
72 * This uses a blit to copy the read buffer to a texture format which matches
73 * the format and type combo and then a fast read-back is done using memcpy.
74 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
75 * a format which matches the swizzling.
76 *
77 * If such a format isn't available, we fall back to _mesa_readpixels.
78 *
79 * NOTE: Some drivers use a blit to convert between tiled and linear
80 * texture layouts during texture uploads/downloads, so the blit
81 * we do here should be free in such cases.
82 */
83 static void
84 st_readpixels(struct gl_context *ctx, GLint x, GLint y,
85 GLsizei width, GLsizei height,
86 GLenum format, GLenum type,
87 const struct gl_pixelstore_attrib *pack,
88 GLvoid *pixels)
89 {
90 struct st_context *st = st_context(ctx);
91 struct gl_renderbuffer *rb =
92 _mesa_get_read_renderbuffer_for_format(ctx, format);
93 struct st_renderbuffer *strb = st_renderbuffer(rb);
94 struct pipe_context *pipe = st->pipe;
95 struct pipe_screen *screen = pipe->screen;
96 struct pipe_resource *src;
97 struct pipe_resource *dst = NULL;
98 struct pipe_resource dst_templ;
99 enum pipe_format dst_format, src_format;
100 struct pipe_blit_info blit;
101 unsigned bind = PIPE_BIND_TRANSFER_READ;
102 struct pipe_transfer *tex_xfer;
103 ubyte *map = NULL;
104
105 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
106 * and flush the bitmap cache prior to reading. */
107 st_validate_state(st);
108 st_flush_bitmap_cache(st);
109
110 if (!st->prefer_blit_based_texture_transfer) {
111 goto fallback;
112 }
113
114 /* This must be done after state validation. */
115 src = strb->texture;
116
117 /* XXX Fallback for depth-stencil formats due to an incomplete
118 * stencil blit implementation in some drivers. */
119 if (format == GL_DEPTH_STENCIL) {
120 goto fallback;
121 }
122
123 /* We are creating a texture of the size of the region being read back.
124 * Need to check for NPOT texture support. */
125 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
126 (!util_is_power_of_two(width) ||
127 !util_is_power_of_two(height))) {
128 goto fallback;
129 }
130
131 /* If the base internal format and the texture format don't match, we have
132 * to use the slow path. */
133 if (rb->_BaseFormat !=
134 _mesa_get_format_base_format(rb->Format)) {
135 goto fallback;
136 }
137
138 /* See if the texture format already matches the format and type,
139 * in which case the memcpy-based fast path will likely be used and
140 * we don't have to blit. */
141 if (_mesa_format_matches_format_and_type(rb->Format, format,
142 type, pack->SwapBytes, NULL)) {
143 goto fallback;
144 }
145
146 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_TRUE)) {
147 goto fallback;
148 }
149
150 if (needs_integer_signed_unsigned_conversion(ctx, format, type)) {
151 goto fallback;
152 }
153
154 /* Convert the source format to what is expected by ReadPixels
155 * and see if it's supported. */
156 src_format = util_format_linear(src->format);
157 src_format = util_format_luminance_to_red(src_format);
158 src_format = util_format_intensity_to_red(src_format);
159
160 if (!src_format ||
161 !screen->is_format_supported(screen, src_format, src->target,
162 src->nr_samples,
163 PIPE_BIND_SAMPLER_VIEW)) {
164 goto fallback;
165 }
166
167 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
168 bind |= PIPE_BIND_DEPTH_STENCIL;
169 else
170 bind |= PIPE_BIND_RENDER_TARGET;
171
172 /* Choose the destination format by finding the best match
173 * for the format+type combo. */
174 dst_format = st_choose_matching_format(st, bind, format, type,
175 pack->SwapBytes);
176 if (dst_format == PIPE_FORMAT_NONE) {
177 goto fallback;
178 }
179
180 /* create the destination texture */
181 memset(&dst_templ, 0, sizeof(dst_templ));
182 dst_templ.target = PIPE_TEXTURE_2D;
183 dst_templ.format = dst_format;
184 dst_templ.bind = bind;
185 dst_templ.usage = PIPE_USAGE_STAGING;
186
187 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D, width, height, 1,
188 &dst_templ.width0, &dst_templ.height0,
189 &dst_templ.depth0, &dst_templ.array_size);
190
191 dst = screen->resource_create(screen, &dst_templ);
192 if (!dst) {
193 goto fallback;
194 }
195
196 memset(&blit, 0, sizeof(blit));
197 blit.src.resource = src;
198 blit.src.level = strb->surface->u.tex.level;
199 blit.src.format = src_format;
200 blit.dst.resource = dst;
201 blit.dst.level = 0;
202 blit.dst.format = dst->format;
203 blit.src.box.x = x;
204 blit.dst.box.x = 0;
205 blit.src.box.y = y;
206 blit.dst.box.y = 0;
207 blit.src.box.z = strb->surface->u.tex.first_layer;
208 blit.dst.box.z = 0;
209 blit.src.box.width = blit.dst.box.width = width;
210 blit.src.box.height = blit.dst.box.height = height;
211 blit.src.box.depth = blit.dst.box.depth = 1;
212 blit.mask = st_get_blit_mask(rb->_BaseFormat, format);
213 blit.filter = PIPE_TEX_FILTER_NEAREST;
214 blit.scissor_enable = FALSE;
215
216 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
217 blit.src.box.y = rb->Height - blit.src.box.y;
218 blit.src.box.height = -blit.src.box.height;
219 }
220
221 /* blit */
222 st->pipe->blit(st->pipe, &blit);
223
224 /* map resources */
225 pixels = _mesa_map_pbo_dest(ctx, pack, pixels);
226
227 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
228 0, 0, 0, width, height, 1, &tex_xfer);
229 if (!map) {
230 _mesa_unmap_pbo_dest(ctx, pack);
231 pipe_resource_reference(&dst, NULL);
232 goto fallback;
233 }
234
235 /* memcpy data into a user buffer */
236 {
237 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
238 GLuint row;
239
240 for (row = 0; row < (unsigned) height; row++) {
241 GLvoid *dest = _mesa_image_address3d(pack, pixels,
242 width, height, format,
243 type, 0, row, 0);
244 memcpy(dest, map, bytesPerRow);
245 map += tex_xfer->stride;
246 }
247 }
248
249 pipe_transfer_unmap(pipe, tex_xfer);
250 _mesa_unmap_pbo_dest(ctx, pack);
251 pipe_resource_reference(&dst, NULL);
252 return;
253
254 fallback:
255 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
256 }
257
258 void st_init_readpixels_functions(struct dd_function_table *functions)
259 {
260 functions->ReadPixels = st_readpixels;
261 }