2 * Copyright 2003 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/bufferobj.h"
27 #include "main/image.h"
28 #include "main/macros.h"
29 #include "main/mtypes.h"
31 #include "main/texobj.h"
32 #include "main/texstore.h"
33 #include "main/texcompress.h"
34 #include "main/enums.h"
35 #include "drivers/common/meta.h"
37 #include "brw_context.h"
38 #include "intel_batchbuffer.h"
39 #include "intel_tex.h"
40 #include "intel_mipmap_tree.h"
41 #include "intel_blit.h"
42 #include "intel_tiled_memcpy.h"
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
47 * \brief A fast path for glTexImage and glTexSubImage.
49 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
51 * This fast path is taken when the texture format is BGRA, RGBA,
52 * A or L and when the texture memory is X- or Y-tiled. It uploads
53 * the texture data by mapping the texture memory without a GTT fence, thus
54 * acquiring a tiled view of the memory, and then copying sucessive
55 * spans within each tile.
57 * This is a performance win over the conventional texture upload path because
58 * it avoids the performance penalty of writing through the write-combine
59 * buffer. In the conventional texture upload path,
60 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
61 * fence, thus acquiring a linear view of the memory, then each row in the
62 * image is memcpy'd. In this fast path, we replace each row's copy with
63 * a sequence of copies over each linear span in tile.
65 * One use case is Google Chrome's paint rectangles. Chrome (as
66 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
67 * Each page's content is initially uploaded with glTexImage2D and damaged
68 * regions are updated with glTexSubImage2D. On some workloads, the
69 * performance gain of this fastpath on Sandybridge is over 5x.
72 intel_texsubimage_tiled_memcpy(struct gl_context
* ctx
,
74 struct gl_texture_image
*texImage
,
75 GLint xoffset
, GLint yoffset
, GLint zoffset
,
76 GLsizei width
, GLsizei height
, GLsizei depth
,
77 GLenum format
, GLenum type
,
79 const struct gl_pixelstore_attrib
*packing
,
82 struct brw_context
*brw
= brw_context(ctx
);
83 struct intel_texture_image
*image
= intel_texture_image(texImage
);
86 /* The miptree's buffer. */
92 mem_copy_fn mem_copy
= NULL
;
94 /* This fastpath is restricted to specific texture types:
95 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
98 * FINISHME: The restrictions below on packing alignment and packing row
99 * length are likely unneeded now because we calculate the source stride
100 * with _mesa_image_row_stride. However, before removing the restrictions
104 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
105 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
106 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
108 _mesa_is_bufferobj(packing
->BufferObj
) ||
109 packing
->Alignment
> 4 ||
110 packing
->SkipPixels
> 0 ||
111 packing
->SkipRows
> 0 ||
112 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
113 packing
->SwapBytes
||
118 /* Only a simple blit, no scale, bias or other mapping. */
119 if (ctx
->_ImageTransferState
)
122 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
,
126 /* If this is a nontrivial texture view, let another path handle it instead. */
127 if (texImage
->TexObject
->MinLayer
)
131 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
134 (image
->mt
->tiling
!= I915_TILING_X
&&
135 image
->mt
->tiling
!= I915_TILING_Y
)) {
136 /* The algorithm is written only for X- or Y-tiled memory. */
140 /* Since we are going to write raw data to the miptree, we need to resolve
141 * any pending fast color clears before we start.
143 intel_miptree_resolve_color(brw
, image
->mt
);
147 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
148 perf_debug("Flushing before mapping a referenced bo.\n");
149 intel_batchbuffer_flush(brw
);
152 error
= brw_bo_map(brw
, bo
, true /* write enable */, "miptree");
153 if (error
|| bo
->virtual == NULL
) {
154 DBG("%s: failed to map bo\n", __func__
);
158 src_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
160 /* We postponed printing this message until having committed to executing
163 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
164 "mesa_format=0x%x tiling=%d "
165 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) "
166 "for_glTexImage=%d\n",
167 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
168 format
, type
, texImage
->TexFormat
, image
->mt
->tiling
,
169 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
170 packing
->SkipRows
, for_glTexImage
);
172 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
174 /* Adjust x and y offset based on miplevel */
175 xoffset
+= image
->mt
->level
[level
].level_x
;
176 yoffset
+= image
->mt
->level
[level
].level_y
;
179 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
180 yoffset
, yoffset
+ height
,
182 pixels
- (ptrdiff_t) yoffset
* src_pitch
- (ptrdiff_t) xoffset
* cpp
,
183 image
->mt
->pitch
, src_pitch
,
189 drm_intel_bo_unmap(bo
);
194 intelTexSubImage(struct gl_context
* ctx
,
196 struct gl_texture_image
*texImage
,
197 GLint xoffset
, GLint yoffset
, GLint zoffset
,
198 GLsizei width
, GLsizei height
, GLsizei depth
,
199 GLenum format
, GLenum type
,
200 const GLvoid
* pixels
,
201 const struct gl_pixelstore_attrib
*packing
)
203 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
206 bool tex_busy
= intelImage
->mt
&& drm_intel_bo_busy(intelImage
->mt
->bo
);
208 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
209 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
210 _mesa_enum_to_string(texImage
->TexObject
->Target
),
211 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
212 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
214 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
,
215 xoffset
, yoffset
, zoffset
,
216 width
, height
, depth
, format
, type
,
217 pixels
, false, tex_busy
, packing
);
221 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
222 xoffset
, yoffset
, zoffset
,
223 width
, height
, depth
,
224 format
, type
, pixels
, packing
,
225 false /*for_glTexImage*/);
229 _mesa_store_texsubimage(ctx
, dims
, texImage
,
230 xoffset
, yoffset
, zoffset
,
231 width
, height
, depth
,
232 format
, type
, pixels
, packing
);
236 intelInitTextureSubImageFuncs(struct dd_function_table
*functions
)
238 functions
->TexSubImage
= intelTexSubImage
;