2 /**************************************************************************
4 * Copyright 2003 VMware, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #include "main/bufferobj.h"
30 #include "main/image.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
34 #include "main/texobj.h"
35 #include "main/texstore.h"
36 #include "main/texcompress.h"
37 #include "main/enums.h"
38 #include "drivers/common/meta.h"
40 #include "brw_context.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_tex.h"
43 #include "intel_mipmap_tree.h"
44 #include "intel_blit.h"
45 #include "intel_tiled_memcpy.h"
47 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
50 * \brief A fast path for glTexImage and glTexSubImage.
52 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
54 * This fast path is taken when the texture format is BGRA, RGBA,
55 * A or L and when the texture memory is X- or Y-tiled. It uploads
56 * the texture data by mapping the texture memory without a GTT fence, thus
57 * acquiring a tiled view of the memory, and then copying sucessive
58 * spans within each tile.
60 * This is a performance win over the conventional texture upload path because
61 * it avoids the performance penalty of writing through the write-combine
62 * buffer. In the conventional texture upload path,
63 * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
64 * fence, thus acquiring a linear view of the memory, then each row in the
65 * image is memcpy'd. In this fast path, we replace each row's copy with
66 * a sequence of copies over each linear span in tile.
68 * One use case is Google Chrome's paint rectangles. Chrome (as
69 * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
70 * Each page's content is initially uploaded with glTexImage2D and damaged
71 * regions are updated with glTexSubImage2D. On some workloads, the
72 * performance gain of this fastpath on Sandybridge is over 5x.
75 intel_texsubimage_tiled_memcpy(struct gl_context
* ctx
,
77 struct gl_texture_image
*texImage
,
78 GLint xoffset
, GLint yoffset
, GLint zoffset
,
79 GLsizei width
, GLsizei height
, GLsizei depth
,
80 GLenum format
, GLenum type
,
82 const struct gl_pixelstore_attrib
*packing
,
85 struct brw_context
*brw
= brw_context(ctx
);
86 struct intel_texture_image
*image
= intel_texture_image(texImage
);
89 /* The miptree's buffer. */
95 mem_copy_fn mem_copy
= NULL
;
97 /* This fastpath is restricted to specific texture types:
98 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
101 * FINISHME: The restrictions below on packing alignment and packing row
102 * length are likely unneeded now because we calculate the source stride
103 * with _mesa_image_row_stride. However, before removing the restrictions
107 !(type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
108 !(texImage
->TexObject
->Target
== GL_TEXTURE_2D
||
109 texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE
) ||
111 _mesa_is_bufferobj(packing
->BufferObj
) ||
112 packing
->Alignment
> 4 ||
113 packing
->SkipPixels
> 0 ||
114 packing
->SkipRows
> 0 ||
115 (packing
->RowLength
!= 0 && packing
->RowLength
!= width
) ||
116 packing
->SwapBytes
||
121 if (!intel_get_memcpy(texImage
->TexFormat
, format
, type
, &mem_copy
, &cpp
,
125 /* If this is a nontrivial texture view, let another path handle it instead. */
126 if (texImage
->TexObject
->MinLayer
)
130 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
133 (image
->mt
->tiling
!= I915_TILING_X
&&
134 image
->mt
->tiling
!= I915_TILING_Y
)) {
135 /* The algorithm is written only for X- or Y-tiled memory. */
139 /* Since we are going to write raw data to the miptree, we need to resolve
140 * any pending fast color clears before we start.
142 intel_miptree_resolve_color(brw
, image
->mt
);
146 if (drm_intel_bo_references(brw
->batch
.bo
, bo
)) {
147 perf_debug("Flushing before mapping a referenced bo.\n");
148 intel_batchbuffer_flush(brw
);
151 error
= brw_bo_map(brw
, bo
, true /* write enable */, "miptree");
152 if (error
|| bo
->virtual == NULL
) {
153 DBG("%s: failed to map bo\n", __func__
);
157 src_pitch
= _mesa_image_row_stride(packing
, width
, format
, type
);
159 /* We postponed printing this message until having committed to executing
162 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
163 "mesa_format=0x%x tiling=%d "
164 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) "
165 "for_glTexImage=%d\n",
166 __func__
, texImage
->Level
, xoffset
, yoffset
, width
, height
,
167 format
, type
, texImage
->TexFormat
, image
->mt
->tiling
,
168 packing
->Alignment
, packing
->RowLength
, packing
->SkipPixels
,
169 packing
->SkipRows
, for_glTexImage
);
171 int level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
173 /* Adjust x and y offset based on miplevel */
174 xoffset
+= image
->mt
->level
[level
].level_x
;
175 yoffset
+= image
->mt
->level
[level
].level_y
;
178 xoffset
* cpp
, (xoffset
+ width
) * cpp
,
179 yoffset
, yoffset
+ height
,
181 pixels
- (ptrdiff_t) yoffset
* src_pitch
- (ptrdiff_t) xoffset
* cpp
,
182 image
->mt
->pitch
, src_pitch
,
188 drm_intel_bo_unmap(bo
);
193 intelTexSubImage(struct gl_context
* ctx
,
195 struct gl_texture_image
*texImage
,
196 GLint xoffset
, GLint yoffset
, GLint zoffset
,
197 GLsizei width
, GLsizei height
, GLsizei depth
,
198 GLenum format
, GLenum type
,
199 const GLvoid
* pixels
,
200 const struct gl_pixelstore_attrib
*packing
)
202 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
205 bool tex_busy
= intelImage
->mt
&& drm_intel_bo_busy(intelImage
->mt
->bo
);
207 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
208 __func__
, _mesa_get_format_name(texImage
->TexFormat
),
209 _mesa_enum_to_string(texImage
->TexObject
->Target
),
210 _mesa_enum_to_string(format
), _mesa_enum_to_string(type
),
211 texImage
->Level
, texImage
->Width
, texImage
->Height
, texImage
->Depth
);
213 ok
= _mesa_meta_pbo_TexSubImage(ctx
, dims
, texImage
,
214 xoffset
, yoffset
, zoffset
,
215 width
, height
, depth
, format
, type
,
216 pixels
, false, tex_busy
, packing
);
220 ok
= intel_texsubimage_tiled_memcpy(ctx
, dims
, texImage
,
221 xoffset
, yoffset
, zoffset
,
222 width
, height
, depth
,
223 format
, type
, pixels
, packing
,
224 false /*for_glTexImage*/);
228 _mesa_store_texsubimage(ctx
, dims
, texImage
,
229 xoffset
, yoffset
, zoffset
,
230 width
, height
, depth
,
231 format
, type
, pixels
, packing
);
235 intelInitTextureSubImageFuncs(struct dd_function_table
*functions
)
237 functions
->TexSubImage
= intelTexSubImage
;