i965: Use brw_wm_prog_data::uses_kill, not gl_fragment_program::UsesKill
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_subimage.c
1
2 /**************************************************************************
3 *
4 * Copyright 2003 VMware, Inc.
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 VMWARE 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/image.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
33 #include "main/pbo.h"
34 #include "main/texobj.h"
35 #include "main/texstore.h"
36 #include "main/texcompress.h"
37 #include "main/enums.h"
38
39 #include "brw_context.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_tex.h"
42 #include "intel_mipmap_tree.h"
43 #include "intel_blit.h"
44
45 #ifdef __SSSE3__
46 #include <tmmintrin.h>
47 #endif
48
49 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
50
51 #define ALIGN_DOWN(a, b) ROUND_DOWN_TO(a, b)
52 #define ALIGN_UP(a, b) ALIGN(a, b)
53
54 /* Tile dimensions.
55 * Width and span are in bytes, height is in pixels (i.e. unitless).
56 * A "span" is the most number of bytes we can copy from linear to tiled
57 * without needing to calculate a new destination address.
58 */
59 static const uint32_t xtile_width = 512;
60 static const uint32_t xtile_height = 8;
61 static const uint32_t xtile_span = 64;
62 static const uint32_t ytile_width = 128;
63 static const uint32_t ytile_height = 32;
64 static const uint32_t ytile_span = 16;
65
66 typedef void *(*mem_copy_fn)(void *dest, const void *src, size_t n);
67
68 /**
69 * Each row from y0 to y1 is copied in three parts: [x0,x1), [x1,x2), [x2,x3).
70 * These ranges are in bytes, i.e. pixels * bytes-per-pixel.
71 * The first and last ranges must be shorter than a "span" (the longest linear
72 * stretch within a tile) and the middle must equal a whole number of spans.
73 * Ranges may be empty. The region copied must land entirely within one tile.
74 * 'dst' is the start of the tile and 'src' is the corresponding
75 * address to copy from, though copying begins at (x0, y0).
76 * To enable swizzling 'swizzle_bit' must be 1<<6, otherwise zero.
77 * Swizzling flips bit 6 in the copy destination offset, when certain other
78 * bits are set in it.
79 */
80 typedef void (*tile_copy_fn)(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
81 uint32_t y0, uint32_t y1,
82 char *dst, const char *src,
83 uint32_t src_pitch,
84 uint32_t swizzle_bit,
85 mem_copy_fn mem_copy);
86
87
88 static bool
89 intel_blit_texsubimage(struct gl_context * ctx,
90 struct gl_texture_image *texImage,
91 GLint xoffset, GLint yoffset,
92 GLint width, GLint height,
93 GLenum format, GLenum type, const void *pixels,
94 const struct gl_pixelstore_attrib *packing)
95 {
96 struct brw_context *brw = brw_context(ctx);
97 struct intel_texture_image *intelImage = intel_texture_image(texImage);
98
99 /* Try to do a blit upload of the subimage if the texture is
100 * currently busy.
101 */
102 if (!intelImage->mt)
103 return false;
104
105 /* Prior to Sandybridge, the blitter can't handle Y tiling */
106 if (brw->gen < 6 && intelImage->mt->tiling == I915_TILING_Y)
107 return false;
108
109 if (texImage->TexObject->Target != GL_TEXTURE_2D)
110 return false;
111
112 /* On gen6, it's probably not worth swapping to the blit ring to do
113 * this because of all the overhead involved.
114 */
115 if (brw->gen >= 6)
116 return false;
117
118 if (!drm_intel_bo_busy(intelImage->mt->bo))
119 return false;
120
121 DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
122 __FUNCTION__,
123 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
124 texImage->Level, xoffset, yoffset, width, height);
125
126 pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1,
127 format, type, pixels, packing,
128 "glTexSubImage");
129 if (!pixels)
130 return false;
131
132 struct intel_mipmap_tree *temp_mt =
133 intel_miptree_create(brw, GL_TEXTURE_2D, texImage->TexFormat,
134 0, 0,
135 width, height, 1,
136 false, 0, INTEL_MIPTREE_TILING_NONE,
137 false);
138 if (!temp_mt)
139 goto err;
140
141 GLubyte *dst = intel_miptree_map_raw(brw, temp_mt);
142 if (!dst)
143 goto err;
144
145 if (!_mesa_texstore(ctx, 2, texImage->_BaseFormat,
146 texImage->TexFormat,
147 temp_mt->pitch,
148 &dst,
149 width, height, 1,
150 format, type, pixels, packing)) {
151 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
152 }
153
154 intel_miptree_unmap_raw(brw, temp_mt);
155
156 bool ret;
157
158 ret = intel_miptree_blit(brw,
159 temp_mt, 0, 0,
160 0, 0, false,
161 intelImage->mt, texImage->Level, texImage->Face,
162 xoffset, yoffset, false,
163 width, height, GL_COPY);
164 assert(ret);
165
166 intel_miptree_release(&temp_mt);
167 _mesa_unmap_teximage_pbo(ctx, packing);
168
169 return ret;
170
171 err:
172 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
173 intel_miptree_release(&temp_mt);
174 _mesa_unmap_teximage_pbo(ctx, packing);
175 return false;
176 }
177
178 #ifdef __SSSE3__
179 static const uint8_t rgba8_permutation[16] =
180 { 2,1,0,3, 6,5,4,7, 10,9,8,11, 14,13,12,15 };
181
182 /* NOTE: dst must be 16 byte aligned */
183 #define rgba8_copy_16(dst, src) \
184 *(__m128i *)(dst) = _mm_shuffle_epi8( \
185 (__m128i) _mm_loadu_ps((float *)(src)), \
186 *(__m128i *) rgba8_permutation \
187 )
188 #endif
189
190 /**
191 * Copy RGBA to BGRA - swap R and B.
192 */
193 static inline void *
194 rgba8_copy(void *dst, const void *src, size_t bytes)
195 {
196 uint8_t *d = dst;
197 uint8_t const *s = src;
198
199 #ifdef __SSSE3__
200 /* Fast copying for tile spans.
201 *
202 * As long as the destination texture is 16 aligned,
203 * any 16 or 64 spans we get here should also be 16 aligned.
204 */
205
206 if (bytes == 16) {
207 assert(!(((uintptr_t)dst) & 0xf));
208 rgba8_copy_16(d+ 0, s+ 0);
209 return dst;
210 }
211
212 if (bytes == 64) {
213 assert(!(((uintptr_t)dst) & 0xf));
214 rgba8_copy_16(d+ 0, s+ 0);
215 rgba8_copy_16(d+16, s+16);
216 rgba8_copy_16(d+32, s+32);
217 rgba8_copy_16(d+48, s+48);
218 return dst;
219 }
220 #endif
221
222 while (bytes >= 4) {
223 d[0] = s[2];
224 d[1] = s[1];
225 d[2] = s[0];
226 d[3] = s[3];
227 d += 4;
228 s += 4;
229 bytes -= 4;
230 }
231 return dst;
232 }
233
234 /**
235 * Copy texture data from linear to X tile layout.
236 *
237 * \copydoc tile_copy_fn
238 */
239 static inline void
240 xtile_copy(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
241 uint32_t y0, uint32_t y1,
242 char *dst, const char *src,
243 uint32_t src_pitch,
244 uint32_t swizzle_bit,
245 mem_copy_fn mem_copy)
246 {
247 /* The copy destination offset for each range copied is the sum of
248 * an X offset 'x0' or 'xo' and a Y offset 'yo.'
249 */
250 uint32_t xo, yo;
251
252 src += y0 * src_pitch;
253
254 for (yo = y0 * xtile_width; yo < y1 * xtile_width; yo += xtile_width) {
255 /* Bits 9 and 10 of the copy destination offset control swizzling.
256 * Only 'yo' contributes to those bits in the total offset,
257 * so calculate 'swizzle' just once per row.
258 * Move bits 9 and 10 three and four places respectively down
259 * to bit 6 and xor them.
260 */
261 uint32_t swizzle = ((yo >> 3) ^ (yo >> 4)) & swizzle_bit;
262
263 mem_copy(dst + ((x0 + yo) ^ swizzle), src + x0, x1 - x0);
264
265 for (xo = x1; xo < x2; xo += xtile_span) {
266 mem_copy(dst + ((xo + yo) ^ swizzle), src + xo, xtile_span);
267 }
268
269 mem_copy(dst + ((xo + yo) ^ swizzle), src + x2, x3 - x2);
270
271 src += src_pitch;
272 }
273 }
274
275 /**
276 * Copy texture data from linear to Y tile layout.
277 *
278 * \copydoc tile_copy_fn
279 */
280 static inline void
281 ytile_copy(
282 uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
283 uint32_t y0, uint32_t y1,
284 char *dst, const char *src,
285 uint32_t src_pitch,
286 uint32_t swizzle_bit,
287 mem_copy_fn mem_copy)
288 {
289 /* Y tiles consist of columns that are 'ytile_span' wide (and the same height
290 * as the tile). Thus the destination offset for (x,y) is the sum of:
291 * (x % column_width) // position within column
292 * (x / column_width) * bytes_per_column // column number * bytes per column
293 * y * column_width
294 *
295 * The copy destination offset for each range copied is the sum of
296 * an X offset 'xo0' or 'xo' and a Y offset 'yo.'
297 */
298 const uint32_t column_width = ytile_span;
299 const uint32_t bytes_per_column = column_width * ytile_height;
300
301 uint32_t xo0 = (x0 % ytile_span) + (x0 / ytile_span) * bytes_per_column;
302 uint32_t xo1 = (x1 % ytile_span) + (x1 / ytile_span) * bytes_per_column;
303
304 /* Bit 9 of the destination offset control swizzling.
305 * Only the X offset contributes to bit 9 of the total offset,
306 * so swizzle can be calculated in advance for these X positions.
307 * Move bit 9 three places down to bit 6.
308 */
309 uint32_t swizzle0 = (xo0 >> 3) & swizzle_bit;
310 uint32_t swizzle1 = (xo1 >> 3) & swizzle_bit;
311
312 uint32_t x, yo;
313
314 src += y0 * src_pitch;
315
316 for (yo = y0 * column_width; yo < y1 * column_width; yo += column_width) {
317 uint32_t xo = xo1;
318 uint32_t swizzle = swizzle1;
319
320 mem_copy(dst + ((xo0 + yo) ^ swizzle0), src + x0, x1 - x0);
321
322 /* Step by spans/columns. As it happens, the swizzle bit flips
323 * at each step so we don't need to calculate it explicitly.
324 */
325 for (x = x1; x < x2; x += ytile_span) {
326 mem_copy(dst + ((xo + yo) ^ swizzle), src + x, ytile_span);
327 xo += bytes_per_column;
328 swizzle ^= swizzle_bit;
329 }
330
331 mem_copy(dst + ((xo + yo) ^ swizzle), src + x2, x3 - x2);
332
333 src += src_pitch;
334 }
335 }
336
337 /**
338 * Copy texture data from linear to X tile layout, faster.
339 *
340 * Same as \ref xtile_copy but faster, because it passes constant parameters
341 * for common cases, allowing the compiler to inline code optimized for those
342 * cases.
343 *
344 * \copydoc tile_copy_fn
345 */
346 static FLATTEN void
347 xtile_copy_faster(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
348 uint32_t y0, uint32_t y1,
349 char *dst, const char *src,
350 uint32_t src_pitch,
351 uint32_t swizzle_bit,
352 mem_copy_fn mem_copy)
353 {
354 if (x0 == 0 && x3 == xtile_width && y0 == 0 && y1 == xtile_height) {
355 if (mem_copy == memcpy)
356 return xtile_copy(0, 0, xtile_width, xtile_width, 0, xtile_height,
357 dst, src, src_pitch, swizzle_bit, memcpy);
358 else if (mem_copy == rgba8_copy)
359 return xtile_copy(0, 0, xtile_width, xtile_width, 0, xtile_height,
360 dst, src, src_pitch, swizzle_bit, rgba8_copy);
361 } else {
362 if (mem_copy == memcpy)
363 return xtile_copy(x0, x1, x2, x3, y0, y1,
364 dst, src, src_pitch, swizzle_bit, memcpy);
365 else if (mem_copy == rgba8_copy)
366 return xtile_copy(x0, x1, x2, x3, y0, y1,
367 dst, src, src_pitch, swizzle_bit, rgba8_copy);
368 }
369 xtile_copy(x0, x1, x2, x3, y0, y1,
370 dst, src, src_pitch, swizzle_bit, mem_copy);
371 }
372
373 /**
374 * Copy texture data from linear to Y tile layout, faster.
375 *
376 * Same as \ref ytile_copy but faster, because it passes constant parameters
377 * for common cases, allowing the compiler to inline code optimized for those
378 * cases.
379 *
380 * \copydoc tile_copy_fn
381 */
382 static FLATTEN void
383 ytile_copy_faster(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
384 uint32_t y0, uint32_t y1,
385 char *dst, const char *src,
386 uint32_t src_pitch,
387 uint32_t swizzle_bit,
388 mem_copy_fn mem_copy)
389 {
390 if (x0 == 0 && x3 == ytile_width && y0 == 0 && y1 == ytile_height) {
391 if (mem_copy == memcpy)
392 return ytile_copy(0, 0, ytile_width, ytile_width, 0, ytile_height,
393 dst, src, src_pitch, swizzle_bit, memcpy);
394 else if (mem_copy == rgba8_copy)
395 return ytile_copy(0, 0, ytile_width, ytile_width, 0, ytile_height,
396 dst, src, src_pitch, swizzle_bit, rgba8_copy);
397 } else {
398 if (mem_copy == memcpy)
399 return ytile_copy(x0, x1, x2, x3, y0, y1,
400 dst, src, src_pitch, swizzle_bit, memcpy);
401 else if (mem_copy == rgba8_copy)
402 return ytile_copy(x0, x1, x2, x3, y0, y1,
403 dst, src, src_pitch, swizzle_bit, rgba8_copy);
404 }
405 ytile_copy(x0, x1, x2, x3, y0, y1,
406 dst, src, src_pitch, swizzle_bit, mem_copy);
407 }
408
409 /**
410 * Copy from linear to tiled texture.
411 *
412 * Divide the region given by X range [xt1, xt2) and Y range [yt1, yt2) into
413 * pieces that do not cross tile boundaries and copy each piece with a tile
414 * copy function (\ref tile_copy_fn).
415 * The X range is in bytes, i.e. pixels * bytes-per-pixel.
416 * The Y range is in pixels (i.e. unitless).
417 * 'dst' is the start of the texture and 'src' is the corresponding
418 * address to copy from, though copying begins at (xt1, yt1).
419 */
420 static void
421 linear_to_tiled(uint32_t xt1, uint32_t xt2,
422 uint32_t yt1, uint32_t yt2,
423 char *dst, const char *src,
424 uint32_t dst_pitch, uint32_t src_pitch,
425 bool has_swizzling,
426 uint32_t tiling,
427 mem_copy_fn mem_copy)
428 {
429 tile_copy_fn tile_copy;
430 uint32_t xt0, xt3;
431 uint32_t yt0, yt3;
432 uint32_t xt, yt;
433 uint32_t tw, th, span;
434 uint32_t swizzle_bit = has_swizzling ? 1<<6 : 0;
435
436 if (tiling == I915_TILING_X) {
437 tw = xtile_width;
438 th = xtile_height;
439 span = xtile_span;
440 tile_copy = xtile_copy_faster;
441 } else if (tiling == I915_TILING_Y) {
442 tw = ytile_width;
443 th = ytile_height;
444 span = ytile_span;
445 tile_copy = ytile_copy_faster;
446 } else {
447 unreachable("unsupported tiling");
448 }
449
450 /* Round out to tile boundaries. */
451 xt0 = ALIGN_DOWN(xt1, tw);
452 xt3 = ALIGN_UP (xt2, tw);
453 yt0 = ALIGN_DOWN(yt1, th);
454 yt3 = ALIGN_UP (yt2, th);
455
456 /* Loop over all tiles to which we have something to copy.
457 * 'xt' and 'yt' are the origin of the destination tile, whether copying
458 * copying a full or partial tile.
459 * tile_copy() copies one tile or partial tile.
460 * Looping x inside y is the faster memory access pattern.
461 */
462 for (yt = yt0; yt < yt3; yt += th) {
463 for (xt = xt0; xt < xt3; xt += tw) {
464 /* The area to update is [x0,x3) x [y0,y1).
465 * May not want the whole tile, hence the min and max.
466 */
467 uint32_t x0 = MAX2(xt1, xt);
468 uint32_t y0 = MAX2(yt1, yt);
469 uint32_t x3 = MIN2(xt2, xt + tw);
470 uint32_t y1 = MIN2(yt2, yt + th);
471
472 /* [x0,x3) is split into [x0,x1), [x1,x2), [x2,x3) such that
473 * the middle interval is the longest span-aligned part.
474 * The sub-ranges could be empty.
475 */
476 uint32_t x1, x2;
477 x1 = ALIGN_UP(x0, span);
478 if (x1 > x3)
479 x1 = x2 = x3;
480 else
481 x2 = ALIGN_DOWN(x3, span);
482
483 assert(x0 <= x1 && x1 <= x2 && x2 <= x3);
484 assert(x1 - x0 < span && x3 - x2 < span);
485 assert(x3 - x0 <= tw);
486 assert((x2 - x1) % span == 0);
487
488 /* Translate by (xt,yt) for single-tile copier. */
489 tile_copy(x0-xt, x1-xt, x2-xt, x3-xt,
490 y0-yt, y1-yt,
491 dst + xt * th + yt * dst_pitch,
492 src + xt + yt * src_pitch,
493 src_pitch,
494 swizzle_bit,
495 mem_copy);
496 }
497 }
498 }
499
500 /**
501 * \brief A fast path for glTexImage and glTexSubImage.
502 *
503 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
504 *
505 * This fast path is taken when the texture format is BGRA, RGBA,
506 * A or L and when the texture memory is X- or Y-tiled. It uploads
507 * the texture data by mapping the texture memory without a GTT fence, thus
508 * acquiring a tiled view of the memory, and then copying sucessive
509 * spans within each tile.
510 *
511 * This is a performance win over the conventional texture upload path because
512 * it avoids the performance penalty of writing through the write-combine
513 * buffer. In the conventional texture upload path,
514 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
515 * fence, thus acquiring a linear view of the memory, then each row in the
516 * image is memcpy'd. In this fast path, we replace each row's copy with
517 * a sequence of copies over each linear span in tile.
518 *
519 * One use case is Google Chrome's paint rectangles. Chrome (as
520 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
521 * Each page's content is initially uploaded with glTexImage2D and damaged
522 * regions are updated with glTexSubImage2D. On some workloads, the
523 * performance gain of this fastpath on Sandybridge is over 5x.
524 */
525 bool
526 intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
527 GLuint dims,
528 struct gl_texture_image *texImage,
529 GLint xoffset, GLint yoffset, GLint zoffset,
530 GLsizei width, GLsizei height, GLsizei depth,
531 GLenum format, GLenum type,
532 const GLvoid *pixels,
533 const struct gl_pixelstore_attrib *packing,
534 bool for_glTexImage)
535 {
536 struct brw_context *brw = brw_context(ctx);
537 struct intel_texture_image *image = intel_texture_image(texImage);
538 int src_pitch;
539
540 /* The miptree's buffer. */
541 drm_intel_bo *bo;
542
543 int error = 0;
544
545 uint32_t cpp;
546 mem_copy_fn mem_copy = NULL;
547
548 /* This fastpath is restricted to specific texture types:
549 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
550 * more types.
551 *
552 * FINISHME: The restrictions below on packing alignment and packing row
553 * length are likely unneeded now because we calculate the source stride
554 * with _mesa_image_row_stride. However, before removing the restrictions
555 * we need tests.
556 */
557 if (!brw->has_llc ||
558 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
559 texImage->TexObject->Target != GL_TEXTURE_2D ||
560 pixels == NULL ||
561 _mesa_is_bufferobj(packing->BufferObj) ||
562 packing->Alignment > 4 ||
563 packing->SkipPixels > 0 ||
564 packing->SkipRows > 0 ||
565 (packing->RowLength != 0 && packing->RowLength != width) ||
566 packing->SwapBytes ||
567 packing->LsbFirst ||
568 packing->Invert)
569 return false;
570
571 if (type == GL_UNSIGNED_INT_8_8_8_8_REV &&
572 !(format == GL_RGBA || format == GL_BGRA))
573 return false; /* Invalid type/format combination */
574
575 if ((texImage->TexFormat == MESA_FORMAT_L_UNORM8 && format == GL_LUMINANCE) ||
576 (texImage->TexFormat == MESA_FORMAT_A_UNORM8 && format == GL_ALPHA)) {
577 cpp = 1;
578 mem_copy = memcpy;
579 } else if ((texImage->TexFormat == MESA_FORMAT_B8G8R8A8_UNORM) ||
580 (texImage->TexFormat == MESA_FORMAT_B8G8R8X8_UNORM)) {
581 cpp = 4;
582 if (format == GL_BGRA) {
583 mem_copy = memcpy;
584 } else if (format == GL_RGBA) {
585 mem_copy = rgba8_copy;
586 }
587 } else if ((texImage->TexFormat == MESA_FORMAT_R8G8B8A8_UNORM) ||
588 (texImage->TexFormat == MESA_FORMAT_R8G8B8X8_UNORM)) {
589 cpp = 4;
590 if (format == GL_BGRA) {
591 /* Copying from RGBA to BGRA is the same as BGRA to RGBA so we can
592 * use the same function.
593 */
594 mem_copy = rgba8_copy;
595 } else if (format == GL_RGBA) {
596 mem_copy = memcpy;
597 }
598 }
599 if (!mem_copy)
600 return false;
601
602 /* If this is a nontrivial texture view, let another path handle it instead. */
603 if (texImage->TexObject->MinLayer)
604 return false;
605
606 if (for_glTexImage)
607 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
608
609 if (!image->mt ||
610 (image->mt->tiling != I915_TILING_X &&
611 image->mt->tiling != I915_TILING_Y)) {
612 /* The algorithm is written only for X- or Y-tiled memory. */
613 return false;
614 }
615
616 /* Since we are going to write raw data to the miptree, we need to resolve
617 * any pending fast color clears before we start.
618 */
619 intel_miptree_resolve_color(brw, image->mt);
620
621 bo = image->mt->bo;
622
623 if (drm_intel_bo_references(brw->batch.bo, bo)) {
624 perf_debug("Flushing before mapping a referenced bo.\n");
625 intel_batchbuffer_flush(brw);
626 }
627
628 error = brw_bo_map(brw, bo, true /* write enable */, "miptree");
629 if (error || bo->virtual == NULL) {
630 DBG("%s: failed to map bo\n", __FUNCTION__);
631 return false;
632 }
633
634 src_pitch = _mesa_image_row_stride(packing, width, format, type);
635
636 /* We postponed printing this message until having committed to executing
637 * the function.
638 */
639 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
640 "mesa_format=0x%x tiling=%d "
641 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) "
642 "for_glTexImage=%d\n",
643 __FUNCTION__, texImage->Level, xoffset, yoffset, width, height,
644 format, type, texImage->TexFormat, image->mt->tiling,
645 packing->Alignment, packing->RowLength, packing->SkipPixels,
646 packing->SkipRows, for_glTexImage);
647
648 int level = texImage->Level + texImage->TexObject->MinLevel;
649
650 /* Adjust x and y offset based on miplevel */
651 xoffset += image->mt->level[level].level_x;
652 yoffset += image->mt->level[level].level_y;
653
654 linear_to_tiled(
655 xoffset * cpp, (xoffset + width) * cpp,
656 yoffset, yoffset + height,
657 bo->virtual, pixels - yoffset * src_pitch - xoffset * cpp,
658 image->mt->pitch, src_pitch,
659 brw->has_swizzling,
660 image->mt->tiling,
661 mem_copy
662 );
663
664 drm_intel_bo_unmap(bo);
665 return true;
666 }
667
668 static void
669 intelTexSubImage(struct gl_context * ctx,
670 GLuint dims,
671 struct gl_texture_image *texImage,
672 GLint xoffset, GLint yoffset, GLint zoffset,
673 GLsizei width, GLsizei height, GLsizei depth,
674 GLenum format, GLenum type,
675 const GLvoid * pixels,
676 const struct gl_pixelstore_attrib *packing)
677 {
678 bool ok;
679
680 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
681 __FUNCTION__, _mesa_get_format_name(texImage->TexFormat),
682 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
683 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
684 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
685
686 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
687 xoffset, yoffset, zoffset,
688 width, height, depth,
689 format, type, pixels, packing,
690 false /*for_glTexImage*/);
691 if (ok)
692 return;
693
694 /* The intel_blit_texsubimage() function only handles 2D images */
695 if (dims != 2 || !intel_blit_texsubimage(ctx, texImage,
696 xoffset, yoffset,
697 width, height,
698 format, type, pixels, packing)) {
699 _mesa_store_texsubimage(ctx, dims, texImage,
700 xoffset, yoffset, zoffset,
701 width, height, depth,
702 format, type, pixels, packing);
703 }
704 }
705
706 void
707 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
708 {
709 functions->TexSubImage = intelTexSubImage;
710 }