i965/miptree: Set refcount before failing via _release()
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_subimage.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 */
25
26 #include "main/bufferobj.h"
27 #include "main/image.h"
28 #include "main/macros.h"
29 #include "main/mtypes.h"
30 #include "main/pbo.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"
36
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"
43
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
45
46 /**
47 * \brief A fast path for glTexImage and glTexSubImage.
48 *
49 * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
50 *
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.
56 *
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.
64 *
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.
70 */
71 bool
72 intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
73 GLuint dims,
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,
78 const GLvoid *pixels,
79 const struct gl_pixelstore_attrib *packing,
80 bool for_glTexImage)
81 {
82 struct brw_context *brw = brw_context(ctx);
83 struct intel_texture_image *image = intel_texture_image(texImage);
84 int src_pitch;
85
86 /* The miptree's buffer. */
87 struct brw_bo *bo;
88
89 uint32_t cpp;
90 mem_copy_fn mem_copy = NULL;
91
92 /* This fastpath is restricted to specific texture types:
93 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
94 * more types.
95 *
96 * FINISHME: The restrictions below on packing alignment and packing row
97 * length are likely unneeded now because we calculate the source stride
98 * with _mesa_image_row_stride. However, before removing the restrictions
99 * we need tests.
100 */
101 if (!brw->has_llc ||
102 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
103 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
104 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
105 pixels == NULL ||
106 _mesa_is_bufferobj(packing->BufferObj) ||
107 packing->Alignment > 4 ||
108 packing->SkipPixels > 0 ||
109 packing->SkipRows > 0 ||
110 (packing->RowLength != 0 && packing->RowLength != width) ||
111 packing->SwapBytes ||
112 packing->LsbFirst ||
113 packing->Invert)
114 return false;
115
116 /* Only a simple blit, no scale, bias or other mapping. */
117 if (ctx->_ImageTransferState)
118 return false;
119
120 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp))
121 return false;
122
123 /* If this is a nontrivial texture view, let another path handle it instead. */
124 if (texImage->TexObject->MinLayer)
125 return false;
126
127 if (for_glTexImage)
128 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
129
130 if (!image->mt ||
131 (image->mt->tiling != I915_TILING_X &&
132 image->mt->tiling != I915_TILING_Y)) {
133 /* The algorithm is written only for X- or Y-tiled memory. */
134 return false;
135 }
136
137 /* linear_to_tiled() assumes that if the object is swizzled, it is using
138 * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y. This is only
139 * true on gen5 and above.
140 *
141 * The killer on top is that some gen4 have an L-shaped swizzle mode, where
142 * parts of the memory aren't swizzled at all. Userspace just can't handle
143 * that.
144 */
145 if (brw->gen < 5 && brw->has_swizzling)
146 return false;
147
148 int level = texImage->Level + texImage->TexObject->MinLevel;
149
150 /* Since we are going to write raw data to the miptree, we need to resolve
151 * any pending fast color clears before we start.
152 */
153 assert(image->mt->logical_depth0 == 1);
154 intel_miptree_access_raw(brw, image->mt, level, 0, true);
155
156 bo = image->mt->bo;
157
158 if (brw_batch_references(&brw->batch, bo)) {
159 perf_debug("Flushing before mapping a referenced bo.\n");
160 intel_batchbuffer_flush(brw);
161 }
162
163 void *map = brw_bo_map(brw, bo, MAP_WRITE | MAP_RAW);
164 if (map == NULL) {
165 DBG("%s: failed to map bo\n", __func__);
166 return false;
167 }
168
169 src_pitch = _mesa_image_row_stride(packing, width, format, type);
170
171 /* We postponed printing this message until having committed to executing
172 * the function.
173 */
174 DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
175 "mesa_format=0x%x tiling=%d "
176 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d) "
177 "for_glTexImage=%d\n",
178 __func__, texImage->Level, xoffset, yoffset, width, height,
179 format, type, texImage->TexFormat, image->mt->tiling,
180 packing->Alignment, packing->RowLength, packing->SkipPixels,
181 packing->SkipRows, for_glTexImage);
182
183 /* Adjust x and y offset based on miplevel */
184 xoffset += image->mt->level[level].level_x;
185 yoffset += image->mt->level[level].level_y;
186
187 linear_to_tiled(
188 xoffset * cpp, (xoffset + width) * cpp,
189 yoffset, yoffset + height,
190 map,
191 pixels - (ptrdiff_t) yoffset * src_pitch - (ptrdiff_t) xoffset * cpp,
192 image->mt->pitch, src_pitch,
193 brw->has_swizzling,
194 image->mt->tiling,
195 mem_copy
196 );
197
198 brw_bo_unmap(bo);
199 return true;
200 }
201
202 static void
203 intelTexSubImage(struct gl_context * ctx,
204 GLuint dims,
205 struct gl_texture_image *texImage,
206 GLint xoffset, GLint yoffset, GLint zoffset,
207 GLsizei width, GLsizei height, GLsizei depth,
208 GLenum format, GLenum type,
209 const GLvoid * pixels,
210 const struct gl_pixelstore_attrib *packing)
211 {
212 struct intel_mipmap_tree *mt = intel_texture_image(texImage)->mt;
213 bool ok;
214
215 bool tex_busy = mt && brw_bo_busy(mt->bo);
216
217 if (mt && mt->format == MESA_FORMAT_S_UINT8)
218 mt->r8stencil_needs_update = true;
219
220 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
221 __func__, _mesa_get_format_name(texImage->TexFormat),
222 _mesa_enum_to_string(texImage->TexObject->Target),
223 _mesa_enum_to_string(format), _mesa_enum_to_string(type),
224 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
225
226 ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage,
227 xoffset, yoffset, zoffset,
228 width, height, depth, format, type,
229 pixels, tex_busy, packing);
230 if (ok)
231 return;
232
233 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
234 xoffset, yoffset, zoffset,
235 width, height, depth,
236 format, type, pixels, packing,
237 false /*for_glTexImage*/);
238 if (ok)
239 return;
240
241 _mesa_store_texsubimage(ctx, dims, texImage,
242 xoffset, yoffset, zoffset,
243 width, height, depth,
244 format, type, pixels, packing);
245 }
246
247 void
248 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
249 {
250 functions->TexSubImage = intelTexSubImage;
251 }