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