i965: Move the remaining intel code to the i965 directory.
[mesa.git] / src / mesa / drivers / dri / i965 / 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
57 /* Try to do a blit upload of the subimage if the texture is
58 * currently busy.
59 */
60 if (!intelImage->mt)
61 return false;
62
63 /* The blitter can't handle Y tiling */
64 if (intelImage->mt->region->tiling == I915_TILING_Y)
65 return false;
66
67 if (texImage->TexObject->Target != GL_TEXTURE_2D)
68 return false;
69
70 /* On gen6, it's probably not worth swapping to the blit ring to do
71 * this because of all the overhead involved.
72 */
73 if (intel->gen >= 6)
74 return false;
75
76 if (!drm_intel_bo_busy(intelImage->mt->region->bo))
77 return false;
78
79 DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
80 __FUNCTION__,
81 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
82 texImage->Level, xoffset, yoffset, width, height);
83
84 pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1,
85 format, type, pixels, packing,
86 "glTexSubImage");
87 if (!pixels)
88 return false;
89
90 struct intel_mipmap_tree *temp_mt =
91 intel_miptree_create(intel, GL_TEXTURE_2D, texImage->TexFormat,
92 0, 0,
93 width, height, 1,
94 false, 0, INTEL_MIPTREE_TILING_NONE);
95 if (!temp_mt)
96 goto err;
97
98 GLubyte *dst = intel_miptree_map_raw(intel, temp_mt);
99 if (!dst)
100 goto err;
101
102 if (!_mesa_texstore(ctx, 2, texImage->_BaseFormat,
103 texImage->TexFormat,
104 temp_mt->region->pitch,
105 &dst,
106 width, height, 1,
107 format, type, pixels, packing)) {
108 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
109 }
110
111 intel_miptree_unmap_raw(intel, temp_mt);
112
113 bool ret;
114
115 ret = intel_miptree_blit(intel,
116 temp_mt, 0, 0,
117 0, 0, false,
118 intelImage->mt, texImage->Level, texImage->Face,
119 xoffset, yoffset, false,
120 width, height, GL_COPY);
121 assert(ret);
122
123 intel_miptree_release(&temp_mt);
124 _mesa_unmap_teximage_pbo(ctx, packing);
125
126 return ret;
127
128 err:
129 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
130 intel_miptree_release(&temp_mt);
131 _mesa_unmap_teximage_pbo(ctx, packing);
132 return false;
133 }
134
135 /**
136 * \brief A fast path for glTexImage and glTexSubImage.
137 *
138 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
139 *
140 * This fast path is taken when the hardware natively supports the texture
141 * format (such as GL_BGRA) and when the texture memory is X-tiled. It uploads
142 * the texture data by mapping the texture memory without a GTT fence, thus
143 * acquiring a tiled view of the memory, and then memcpy'ing sucessive
144 * subspans within each tile.
145 *
146 * This is a performance win over the conventional texture upload path because
147 * it avoids the performance penalty of writing through the write-combine
148 * buffer. In the conventional texture upload path,
149 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
150 * fence, thus acquiring a linear view of the memory, then each row in the
151 * image is memcpy'd. In this fast path, we replace each row's memcpy with
152 * a sequence of memcpy's over each bit6 swizzle span in the row.
153 *
154 * This fast path's use case is Google Chrome's paint rectangles. Chrome (as
155 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
156 * Each page's content is initially uploaded with glTexImage2D and damaged
157 * regions are updated with glTexSubImage2D. On some workloads, the
158 * performance gain of this fastpath on Sandybridge is over 5x.
159 */
160 bool
161 intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
162 GLuint dims,
163 struct gl_texture_image *texImage,
164 GLint xoffset, GLint yoffset, GLint zoffset,
165 GLsizei width, GLsizei height, GLsizei depth,
166 GLenum format, GLenum type,
167 const GLvoid *pixels,
168 const struct gl_pixelstore_attrib *packing,
169 bool for_glTexImage)
170 {
171 struct intel_context *intel = intel_context(ctx);
172 struct intel_texture_image *image = intel_texture_image(texImage);
173
174 /* The miptree's buffer. */
175 drm_intel_bo *bo;
176
177 int error = 0;
178
179 /* This fastpath is restricted to a specific texture type: level 0 of
180 * a 2D BGRA texture. It could be generalized to support more types by
181 * varying the arithmetic loop below.
182 */
183 if (!intel->has_llc ||
184 format != GL_BGRA ||
185 type != GL_UNSIGNED_BYTE ||
186 texImage->TexFormat != MESA_FORMAT_ARGB8888 ||
187 texImage->TexObject->Target != GL_TEXTURE_2D ||
188 texImage->Level != 0 ||
189 pixels == NULL ||
190 _mesa_is_bufferobj(packing->BufferObj) ||
191 packing->Alignment > 4 ||
192 packing->SkipPixels > 0 ||
193 packing->SkipRows > 0 ||
194 (packing->RowLength != 0 && packing->RowLength != width) ||
195 packing->SwapBytes ||
196 packing->LsbFirst ||
197 packing->Invert)
198 return false;
199
200 if (for_glTexImage)
201 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
202
203 if (!image->mt ||
204 image->mt->region->tiling != I915_TILING_X) {
205 /* The algorithm below is written only for X-tiled memory. */
206 return false;
207 }
208
209 /* Since we are going to write raw data to the miptree, we need to resolve
210 * any pending fast color clears before we start.
211 */
212 intel_miptree_resolve_color(intel, image->mt);
213
214 bo = image->mt->region->bo;
215
216 if (drm_intel_bo_references(intel->batch.bo, bo)) {
217 perf_debug("Flushing before mapping a referenced bo.\n");
218 intel_batchbuffer_flush(intel);
219 }
220
221 if (unlikely(intel->perf_debug)) {
222 if (drm_intel_bo_busy(bo)) {
223 perf_debug("Mapping a busy BO, causing a stall on the GPU.\n");
224 }
225 }
226
227 error = drm_intel_bo_map(bo, true /*write_enable*/);
228 if (error || bo->virtual == NULL) {
229 DBG("%s: failed to map bo\n", __FUNCTION__);
230 return false;
231 }
232
233 /* We postponed printing this message until having committed to executing
234 * the function.
235 */
236 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d)\n",
237 __FUNCTION__, texImage->Level, xoffset, yoffset, width, height);
238
239 /* In the tiling algorithm below, some variables are in units of pixels,
240 * others are in units of bytes, and others (such as height) are unitless.
241 * Each variable name is suffixed with its units.
242 */
243
244 const uint32_t x_max_pixels = xoffset + width;
245 const uint32_t y_max_pixels = yoffset + height;
246
247 const uint32_t tile_size_bytes = 4096;
248
249 const uint32_t tile_width_bytes = 512;
250 const uint32_t tile_width_pixels = 128;
251
252 const uint32_t tile_height = 8;
253
254 const uint32_t cpp = 4; /* chars per pixel of GL_BGRA */
255 const uint32_t swizzle_width_pixels = 16;
256
257 const uint32_t stride_bytes = image->mt->region->pitch;
258 const uint32_t width_tiles = stride_bytes / tile_width_bytes;
259
260 for (uint32_t y_pixels = yoffset; y_pixels < y_max_pixels; ++y_pixels) {
261 const uint32_t y_offset_bytes = (y_pixels / tile_height) * width_tiles * tile_size_bytes
262 + (y_pixels % tile_height) * tile_width_bytes;
263
264 for (uint32_t x_pixels = xoffset; x_pixels < x_max_pixels; x_pixels += swizzle_width_pixels) {
265 const uint32_t x_offset_bytes = (x_pixels / tile_width_pixels) * tile_size_bytes
266 + (x_pixels % tile_width_pixels) * cpp;
267
268 intptr_t offset_bytes = y_offset_bytes + x_offset_bytes;
269 if (intel->has_swizzling) {
270 #if 0
271 /* Clear, unoptimized version. */
272 bool bit6 = (offset_bytes >> 6) & 1;
273 bool bit9 = (offset_bytes >> 9) & 1;
274 bool bit10 = (offset_bytes >> 10) & 1;
275
276 if (bit9 ^ bit10)
277 offset_bytes ^= (1 << 6);
278 #else
279 /* Optimized, obfuscated version. */
280 offset_bytes ^= ((offset_bytes >> 3) ^ (offset_bytes >> 4))
281 & (1 << 6);
282 #endif
283 }
284
285 const uint32_t swizzle_bound_pixels = ALIGN(x_pixels + 1, swizzle_width_pixels);
286 const uint32_t memcpy_bound_pixels = MIN2(x_max_pixels, swizzle_bound_pixels);
287 const uint32_t copy_size = cpp * (memcpy_bound_pixels - x_pixels);
288
289 memcpy(bo->virtual + offset_bytes, pixels, copy_size);
290 pixels += copy_size;
291 x_pixels -= (x_pixels % swizzle_width_pixels);
292 }
293 }
294
295 drm_intel_bo_unmap(bo);
296 return true;
297 }
298
299 static void
300 intelTexSubImage(struct gl_context * ctx,
301 GLuint dims,
302 struct gl_texture_image *texImage,
303 GLint xoffset, GLint yoffset, GLint zoffset,
304 GLsizei width, GLsizei height, GLsizei depth,
305 GLenum format, GLenum type,
306 const GLvoid * pixels,
307 const struct gl_pixelstore_attrib *packing)
308 {
309 bool ok;
310
311 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
312 xoffset, yoffset, zoffset,
313 width, height, depth,
314 format, type, pixels, packing,
315 false /*for_glTexImage*/);
316 if (ok)
317 return;
318
319 /* The intel_blit_texsubimage() function only handles 2D images */
320 if (dims != 2 || !intel_blit_texsubimage(ctx, texImage,
321 xoffset, yoffset,
322 width, height,
323 format, type, pixels, packing)) {
324 _mesa_store_texsubimage(ctx, dims, texImage,
325 xoffset, yoffset, zoffset,
326 width, height, depth,
327 format, type, pixels, packing);
328 }
329 }
330
331 void
332 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
333 {
334 functions->TexSubImage = intelTexSubImage;
335 }