2 * Copyright 2003 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/enums.h"
27 #include "main/mtypes.h"
28 #include "main/macros.h"
29 #include "main/fbobject.h"
30 #include "main/image.h"
31 #include "main/bufferobj.h"
32 #include "main/readpix.h"
33 #include "main/state.h"
34 #include "main/glformats.h"
35 #include "program/prog_instruction.h"
36 #include "drivers/common/meta.h"
38 #include "brw_context.h"
39 #include "brw_blorp.h"
40 #include "intel_screen.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_buffers.h"
43 #include "intel_fbo.h"
44 #include "intel_mipmap_tree.h"
45 #include "intel_pixel.h"
46 #include "intel_buffer_objects.h"
48 #define FILE_DEBUG_FLAG DEBUG_PIXEL
51 * \brief A fast path for glReadPixels
53 * This fast path is taken when the source format is BGRA, RGBA,
54 * A or L and when the texture memory is X- or Y-tiled. It downloads
55 * the source data by directly mapping the memory without a GTT fence.
56 * This then needs to be de-tiled on the CPU before presenting the data to
57 * the user in the linear fasion.
59 * This is a performance win over the conventional texture download path.
60 * In the conventional texture download path, the texture is either mapped
61 * through the GTT or copied to a linear buffer with the blitter before
62 * handing off to a software path. This allows us to avoid round-tripping
63 * through the GPU (in the case where we would be blitting) and do only a
64 * single copy operation.
67 intel_readpixels_tiled_memcpy(struct gl_context
* ctx
,
68 GLint xoffset
, GLint yoffset
,
69 GLsizei width
, GLsizei height
,
70 GLenum format
, GLenum type
,
72 const struct gl_pixelstore_attrib
*pack
)
74 struct brw_context
*brw
= brw_context(ctx
);
75 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
76 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
78 /* This path supports reading from color buffers only */
82 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
85 /* The miptree's buffer. */
89 isl_memcpy_type copy_type
;
91 /* This fastpath is restricted to specific renderbuffer types:
92 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
95 if (!devinfo
->has_llc
||
96 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
99 pack
->Alignment
> 4 ||
100 pack
->SkipPixels
> 0 ||
101 pack
->SkipRows
> 0 ||
102 (pack
->RowLength
!= 0 && pack
->RowLength
!= width
) ||
108 /* Only a simple blit, no scale, bias or other mapping. */
109 if (ctx
->_ImageTransferState
)
112 /* It is possible that the renderbuffer (or underlying texture) is
113 * multisampled. Since ReadPixels from a multisampled buffer requires a
114 * multisample resolve, we can't handle this here
116 if (rb
->NumSamples
> 1)
119 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
120 * function doesn't set the last channel to 1. Note this checks BaseFormat
121 * rather than TexFormat in case the RGBX format is being simulated with an
124 if (rb
->_BaseFormat
== GL_RGB
)
127 copy_type
= intel_miptree_get_memcpy_type(rb
->Format
, format
, type
, &cpp
);
128 if (copy_type
== ISL_MEMCPY_INVALID
)
132 (irb
->mt
->surf
.tiling
!= ISL_TILING_X
&&
133 irb
->mt
->surf
.tiling
!= ISL_TILING_Y0
)) {
134 /* The algorithm is written only for X- or Y-tiled memory. */
138 /* tiled_to_linear() assumes that if the object is swizzled, it is using
139 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
140 * true on gen5 and above.
142 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
143 * parts of the memory aren't swizzled at all. Userspace just can't handle
146 if (devinfo
->gen
< 5 && brw
->has_swizzling
)
149 /* Since we are going to read raw data to the miptree, we need to resolve
150 * any pending fast color clears before we start.
152 intel_miptree_access_raw(brw
, irb
->mt
, irb
->mt_level
, irb
->mt_layer
, false);
156 if (brw_batch_references(&brw
->batch
, bo
)) {
157 perf_debug("Flushing before mapping a referenced bo.\n");
158 intel_batchbuffer_flush(brw
);
161 void *map
= brw_bo_map(brw
, bo
, MAP_READ
| MAP_RAW
);
163 DBG("%s: failed to map bo\n", __func__
);
167 unsigned slice_offset_x
, slice_offset_y
;
168 intel_miptree_get_image_offset(irb
->mt
, irb
->mt_level
, irb
->mt_layer
,
169 &slice_offset_x
, &slice_offset_y
);
170 xoffset
+= slice_offset_x
;
171 yoffset
+= slice_offset_y
;
173 dst_pitch
= _mesa_image_row_stride(pack
, width
, format
, type
);
175 /* For a window-system renderbuffer, the buffer is actually flipped
176 * vertically, so we need to handle that. Since the detiling function
177 * can only really work in the forwards direction, we have to be a
178 * little creative. First, we compute the Y-offset of the first row of
179 * the renderbuffer (in renderbuffer coordinates). We then match that
180 * with the last row of the client's data. Finally, we give
181 * tiled_to_linear a negative pitch so that it walks through the
182 * client's data backwards as it walks through the renderbufer forwards.
184 if (ctx
->ReadBuffer
->FlipY
) {
185 yoffset
= rb
->Height
- yoffset
- height
;
186 pixels
+= (ptrdiff_t) (height
- 1) * dst_pitch
;
187 dst_pitch
= -dst_pitch
;
190 /* We postponed printing this message until having committed to executing
193 DBG("%s: x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
194 "mesa_format=0x%x tiling=%d "
195 "pack=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
196 __func__
, xoffset
, yoffset
, width
, height
,
197 format
, type
, rb
->Format
, irb
->mt
->surf
.tiling
,
198 pack
->Alignment
, pack
->RowLength
, pack
->SkipPixels
,
201 isl_memcpy_tiled_to_linear(
202 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
203 yoffset
, yoffset
+ height
,
205 map
+ irb
->mt
->offset
,
206 dst_pitch
, irb
->mt
->surf
.row_pitch_B
,
208 irb
->mt
->surf
.tiling
,
217 intel_readpixels_blorp(struct gl_context
*ctx
,
218 unsigned x
, unsigned y
,
219 unsigned w
, unsigned h
,
220 GLenum format
, GLenum type
, const void *pixels
,
221 const struct gl_pixelstore_attrib
*packing
)
223 struct brw_context
*brw
= brw_context(ctx
);
224 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
228 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
230 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
231 * color clamping along with the ctx->_ImageTransferState.
233 if (_mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
237 GLenum dst_base_format
= _mesa_unpack_format_to_base_format(format
);
238 if (_mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
,
243 if (irb
->Base
.Base
._BaseFormat
== GL_RGB
) {
244 swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
246 swizzle
= SWIZZLE_XYZW
;
249 return brw_blorp_download_miptree(brw
, irb
->mt
, rb
->Format
, swizzle
,
250 irb
->mt_level
, x
, y
, irb
->mt_layer
,
251 w
, h
, 1, GL_TEXTURE_2D
, format
, type
,
252 ctx
->ReadBuffer
->FlipY
, pixels
, packing
);
256 intelReadPixels(struct gl_context
* ctx
,
257 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
258 GLenum format
, GLenum type
,
259 const struct gl_pixelstore_attrib
*pack
, GLvoid
* pixels
)
263 struct brw_context
*brw
= brw_context(ctx
);
266 DBG("%s\n", __func__
);
268 /* Reading pixels wont dirty the front buffer, so reset the dirty
269 * flag after calling intel_prepare_render().
271 dirty
= brw
->front_buffer_dirty
;
272 intel_prepare_render(brw
);
273 brw
->front_buffer_dirty
= dirty
;
275 if (pack
->BufferObj
) {
276 if (intel_readpixels_blorp(ctx
, x
, y
, width
, height
,
277 format
, type
, pixels
, pack
))
280 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__
);
283 ok
= intel_readpixels_tiled_memcpy(ctx
, x
, y
, width
, height
,
284 format
, type
, pixels
, pack
);
288 /* Update Mesa state before calling _mesa_readpixels().
289 * XXX this may not be needed since ReadPixels no longer uses the
294 _mesa_update_state(ctx
);
296 _mesa_readpixels(ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
);
298 /* There's an intel_prepare_render() call in intelSpanRenderStart(). */
299 brw
->front_buffer_dirty
= dirty
;