intel: Avoid making tiled miptrees we won't be able to blit.
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_subimage.c
1
2 /**************************************************************************
3 *
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "main/bufferobj.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/pbo.h"
33 #include "main/texobj.h"
34 #include "main/texstore.h"
35 #include "main/texcompress.h"
36 #include "main/enums.h"
37
38 #include "intel_batchbuffer.h"
39 #include "intel_context.h"
40 #include "intel_tex.h"
41 #include "intel_mipmap_tree.h"
42 #include "intel_blit.h"
43
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
45
46 static bool
47 intel_blit_texsubimage(struct gl_context * ctx,
48 struct gl_texture_image *texImage,
49 GLint xoffset, GLint yoffset,
50 GLint width, GLint height,
51 GLenum format, GLenum type, const void *pixels,
52 const struct gl_pixelstore_attrib *packing)
53 {
54 struct intel_context *intel = intel_context(ctx);
55 struct intel_texture_image *intelImage = intel_texture_image(texImage);
56 GLuint dstRowStride = 0;
57 drm_intel_bo *temp_bo = NULL;
58 unsigned int blit_x = 0, blit_y = 0;
59 unsigned long pitch;
60 uint32_t tiling_mode = I915_TILING_NONE;
61 GLubyte *dstMap;
62
63 /* Try to do a blit upload of the subimage if the texture is
64 * currently busy.
65 */
66 if (!intelImage->mt)
67 return false;
68
69 /* The blitter can't handle Y tiling */
70 if (intelImage->mt->region->tiling == I915_TILING_Y)
71 return false;
72
73 if (texImage->TexObject->Target != GL_TEXTURE_2D)
74 return false;
75
76 /* On gen6, it's probably not worth swapping to the blit ring to do
77 * this because of all the overhead involved.
78 */
79 if (intel->gen >= 6)
80 return false;
81
82 if (!drm_intel_bo_busy(intelImage->mt->region->bo))
83 return false;
84
85 DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
86 __FUNCTION__,
87 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
88 texImage->Level, xoffset, yoffset, width, height);
89
90 pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1,
91 format, type, pixels, packing,
92 "glTexSubImage");
93 if (!pixels)
94 return false;
95
96 temp_bo = drm_intel_bo_alloc_tiled(intel->bufmgr,
97 "subimage blit bo",
98 width, height,
99 intelImage->mt->cpp,
100 &tiling_mode,
101 &pitch,
102 0);
103 if (temp_bo == NULL) {
104 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
105 return false;
106 }
107
108 if (drm_intel_gem_bo_map_gtt(temp_bo)) {
109 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
110 return false;
111 }
112
113 dstMap = temp_bo->virtual;
114 dstRowStride = pitch;
115
116 intel_miptree_get_image_offset(intelImage->mt, texImage->Level,
117 intelImage->base.Base.Face,
118 &blit_x, &blit_y);
119 blit_x += xoffset;
120 blit_y += yoffset;
121
122 if (!_mesa_texstore(ctx, 2, texImage->_BaseFormat,
123 texImage->TexFormat,
124 dstRowStride,
125 &dstMap,
126 width, height, 1,
127 format, type, pixels, packing)) {
128 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
129 }
130
131 bool ret;
132
133 drm_intel_gem_bo_unmap_gtt(temp_bo);
134
135 ret = intelEmitCopyBlit(intel,
136 intelImage->mt->cpp,
137 dstRowStride,
138 temp_bo, 0, false,
139 intelImage->mt->region->pitch,
140 intelImage->mt->region->bo, 0,
141 intelImage->mt->region->tiling,
142 0, 0, blit_x, blit_y, width, height,
143 GL_COPY);
144 assert(ret);
145
146 drm_intel_bo_unreference(temp_bo);
147 _mesa_unmap_teximage_pbo(ctx, packing);
148
149 return true;
150 }
151
152 /**
153 * \brief A fast path for glTexImage and glTexSubImage.
154 *
155 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
156 *
157 * This fast path is taken when the hardware natively supports the texture
158 * format (such as GL_BGRA) and when the texture memory is X-tiled. It uploads
159 * the texture data by mapping the texture memory without a GTT fence, thus
160 * acquiring a tiled view of the memory, and then memcpy'ing sucessive
161 * subspans within each tile.
162 *
163 * This is a performance win over the conventional texture upload path because
164 * it avoids the performance penalty of writing through the write-combine
165 * buffer. In the conventional texture upload path,
166 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
167 * fence, thus acquiring a linear view of the memory, then each row in the
168 * image is memcpy'd. In this fast path, we replace each row's memcpy with
169 * a sequence of memcpy's over each bit6 swizzle span in the row.
170 *
171 * This fast path's use case is Google Chrome's paint rectangles. Chrome (as
172 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
173 * Each page's content is initially uploaded with glTexImage2D and damaged
174 * regions are updated with glTexSubImage2D. On some workloads, the
175 * performance gain of this fastpath on Sandybridge is over 5x.
176 */
177 bool
178 intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
179 GLuint dims,
180 struct gl_texture_image *texImage,
181 GLint xoffset, GLint yoffset, GLint zoffset,
182 GLsizei width, GLsizei height, GLsizei depth,
183 GLenum format, GLenum type,
184 const GLvoid *pixels,
185 const struct gl_pixelstore_attrib *packing,
186 bool for_glTexImage)
187 {
188 struct intel_context *intel = intel_context(ctx);
189 struct intel_texture_image *image = intel_texture_image(texImage);
190
191 /* The miptree's buffer. */
192 drm_intel_bo *bo;
193
194 int error = 0;
195
196 /* This fastpath is restricted to a specific texture type: level 0 of
197 * a 2D BGRA texture. It could be generalized to support more types by
198 * varying the arithmetic loop below.
199 */
200 if (!intel->has_llc ||
201 format != GL_BGRA ||
202 type != GL_UNSIGNED_BYTE ||
203 texImage->TexFormat != MESA_FORMAT_ARGB8888 ||
204 texImage->TexObject->Target != GL_TEXTURE_2D ||
205 texImage->Level != 0 ||
206 pixels == NULL ||
207 _mesa_is_bufferobj(packing->BufferObj) ||
208 packing->Alignment > 4 ||
209 packing->SkipPixels > 0 ||
210 packing->SkipRows > 0 ||
211 (packing->RowLength != 0 && packing->RowLength != width) ||
212 packing->SwapBytes ||
213 packing->LsbFirst ||
214 packing->Invert)
215 return false;
216
217 if (for_glTexImage)
218 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
219
220 if (!image->mt ||
221 image->mt->region->tiling != I915_TILING_X) {
222 /* The algorithm below is written only for X-tiled memory. */
223 return false;
224 }
225
226 bo = image->mt->region->bo;
227
228 if (drm_intel_bo_references(intel->batch.bo, bo)) {
229 perf_debug("Flushing before mapping a referenced bo.\n");
230 intel_batchbuffer_flush(intel);
231 }
232
233 if (unlikely(intel->perf_debug)) {
234 if (drm_intel_bo_busy(bo)) {
235 perf_debug("Mapping a busy BO, causing a stall on the GPU.\n");
236 }
237 }
238
239 error = drm_intel_bo_map(bo, true /*write_enable*/);
240 if (error || bo->virtual == NULL) {
241 DBG("%s: failed to map bo\n", __FUNCTION__);
242 return false;
243 }
244
245 /* We postponed printing this message until having committed to executing
246 * the function.
247 */
248 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d)\n",
249 __FUNCTION__, texImage->Level, xoffset, yoffset, width, height);
250
251 /* In the tiling algorithm below, some variables are in units of pixels,
252 * others are in units of bytes, and others (such as height) are unitless.
253 * Each variable name is suffixed with its units.
254 */
255
256 const uint32_t x_max_pixels = xoffset + width;
257 const uint32_t y_max_pixels = yoffset + height;
258
259 const uint32_t tile_size_bytes = 4096;
260
261 const uint32_t tile_width_bytes = 512;
262 const uint32_t tile_width_pixels = 128;
263
264 const uint32_t tile_height = 8;
265
266 const uint32_t cpp = 4; /* chars per pixel of GL_BGRA */
267 const uint32_t swizzle_width_pixels = 16;
268
269 const uint32_t stride_bytes = image->mt->region->pitch;
270 const uint32_t width_tiles = stride_bytes / tile_width_bytes;
271
272 for (uint32_t y_pixels = yoffset; y_pixels < y_max_pixels; ++y_pixels) {
273 const uint32_t y_offset_bytes = (y_pixels / tile_height) * width_tiles * tile_size_bytes
274 + (y_pixels % tile_height) * tile_width_bytes;
275
276 for (uint32_t x_pixels = xoffset; x_pixels < x_max_pixels; x_pixels += swizzle_width_pixels) {
277 const uint32_t x_offset_bytes = (x_pixels / tile_width_pixels) * tile_size_bytes
278 + (x_pixels % tile_width_pixels) * cpp;
279
280 intptr_t offset_bytes = y_offset_bytes + x_offset_bytes;
281 if (intel->has_swizzling) {
282 #if 0
283 /* Clear, unoptimized version. */
284 bool bit6 = (offset_bytes >> 6) & 1;
285 bool bit9 = (offset_bytes >> 9) & 1;
286 bool bit10 = (offset_bytes >> 10) & 1;
287
288 if (bit9 ^ bit10)
289 offset_bytes ^= (1 << 6);
290 #else
291 /* Optimized, obfuscated version. */
292 offset_bytes ^= ((offset_bytes >> 3) ^ (offset_bytes >> 4))
293 & (1 << 6);
294 #endif
295 }
296
297 const uint32_t swizzle_bound_pixels = ALIGN(x_pixels + 1, swizzle_width_pixels);
298 const uint32_t memcpy_bound_pixels = MIN2(x_max_pixels, swizzle_bound_pixels);
299 const uint32_t copy_size = cpp * (memcpy_bound_pixels - x_pixels);
300
301 memcpy(bo->virtual + offset_bytes, pixels, copy_size);
302 pixels += copy_size;
303 x_pixels -= (x_pixels % swizzle_width_pixels);
304 }
305 }
306
307 drm_intel_bo_unmap(bo);
308 return true;
309 }
310
311 static void
312 intelTexSubImage(struct gl_context * ctx,
313 GLuint dims,
314 struct gl_texture_image *texImage,
315 GLint xoffset, GLint yoffset, GLint zoffset,
316 GLsizei width, GLsizei height, GLsizei depth,
317 GLenum format, GLenum type,
318 const GLvoid * pixels,
319 const struct gl_pixelstore_attrib *packing)
320 {
321 bool ok;
322
323 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
324 xoffset, yoffset, zoffset,
325 width, height, depth,
326 format, type, pixels, packing,
327 false /*for_glTexImage*/);
328 if (ok)
329 return;
330
331 /* The intel_blit_texsubimage() function only handles 2D images */
332 if (dims != 2 || !intel_blit_texsubimage(ctx, texImage,
333 xoffset, yoffset,
334 width, height,
335 format, type, pixels, packing)) {
336 _mesa_store_texsubimage(ctx, dims, texImage,
337 xoffset, yoffset, zoffset,
338 width, height, depth,
339 format, type, pixels, packing);
340 }
341 }
342
343 void
344 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
345 {
346 functions->TexSubImage = intelTexSubImage;
347 }