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