i965: intel_texture_barrier reimplemented
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex.c
1 #include "swrast/swrast.h"
2 #include "main/renderbuffer.h"
3 #include "main/texobj.h"
4 #include "main/teximage.h"
5 #include "main/mipmap.h"
6 #include "drivers/common/meta.h"
7 #include "brw_context.h"
8 #include "intel_buffer_objects.h"
9 #include "intel_mipmap_tree.h"
10 #include "intel_tex.h"
11 #include "intel_fbo.h"
12 #include "intel_reg.h"
13
14 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
15
16 static struct gl_texture_image *
17 intelNewTextureImage(struct gl_context * ctx)
18 {
19 DBG("%s\n", __func__);
20 (void) ctx;
21 return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
22 }
23
24 static void
25 intelDeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
26 {
27 /* nothing special (yet) for intel_texture_image */
28 _mesa_delete_texture_image(ctx, img);
29 }
30
31
32 static struct gl_texture_object *
33 intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
34 {
35 struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
36
37 (void) ctx;
38
39 DBG("%s\n", __func__);
40
41 if (obj == NULL)
42 return NULL;
43
44 _mesa_initialize_texture_object(ctx, &obj->base, name, target);
45
46 obj->needs_validate = true;
47
48 return &obj->base;
49 }
50
51 static void
52 intelDeleteTextureObject(struct gl_context *ctx,
53 struct gl_texture_object *texObj)
54 {
55 struct intel_texture_object *intelObj = intel_texture_object(texObj);
56
57 intel_miptree_release(&intelObj->mt);
58 _mesa_delete_texture_object(ctx, texObj);
59 }
60
61 static GLboolean
62 intel_alloc_texture_image_buffer(struct gl_context *ctx,
63 struct gl_texture_image *image)
64 {
65 struct brw_context *brw = brw_context(ctx);
66 struct intel_texture_image *intel_image = intel_texture_image(image);
67 struct gl_texture_object *texobj = image->TexObject;
68 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
69
70 assert(image->Border == 0);
71
72 /* Quantize sample count */
73 if (image->NumSamples) {
74 image->NumSamples = intel_quantize_num_samples(brw->intelScreen, image->NumSamples);
75 if (!image->NumSamples)
76 return false;
77 }
78
79 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
80 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
81 * multiple times.
82 */
83 ctx->Driver.FreeTextureImageBuffer(ctx, image);
84
85 if (!_swrast_init_texture_image(image))
86 return false;
87
88 if (intel_texobj->mt &&
89 intel_miptree_match_image(intel_texobj->mt, image)) {
90 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
91 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
92 __func__, texobj, image->Level,
93 image->Width, image->Height, image->Depth, intel_texobj->mt);
94 } else {
95 intel_image->mt = intel_miptree_create_for_teximage(brw, intel_texobj,
96 intel_image,
97 0);
98
99 /* Even if the object currently has a mipmap tree associated
100 * with it, this one is a more likely candidate to represent the
101 * whole object since our level didn't fit what was there
102 * before, and any lower levels would fit into our miptree.
103 */
104 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
105
106 DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
107 __func__, texobj, image->Level,
108 image->Width, image->Height, image->Depth, intel_image->mt);
109 }
110
111 intel_texobj->needs_validate = true;
112
113 return true;
114 }
115
116 /**
117 * ctx->Driver.AllocTextureStorage() handler.
118 *
119 * Compare this to _mesa_AllocTextureStorage_sw, which would call into
120 * intel_alloc_texture_image_buffer() above.
121 */
122 static GLboolean
123 intel_alloc_texture_storage(struct gl_context *ctx,
124 struct gl_texture_object *texobj,
125 GLsizei levels, GLsizei width,
126 GLsizei height, GLsizei depth)
127 {
128 struct brw_context *brw = brw_context(ctx);
129 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
130 struct gl_texture_image *first_image = texobj->Image[0][0];
131 int num_samples = intel_quantize_num_samples(brw->intelScreen,
132 first_image->NumSamples);
133 const int numFaces = _mesa_num_tex_faces(texobj->Target);
134 int face;
135 int level;
136
137 /* If the object's current miptree doesn't match what we need, make a new
138 * one.
139 */
140 if (!intel_texobj->mt ||
141 !intel_miptree_match_image(intel_texobj->mt, first_image) ||
142 intel_texobj->mt->last_level != levels - 1) {
143 intel_miptree_release(&intel_texobj->mt);
144 intel_texobj->mt = intel_miptree_create(brw, texobj->Target,
145 first_image->TexFormat,
146 0, levels - 1,
147 width, height, depth,
148 num_samples,
149 MIPTREE_LAYOUT_TILING_ANY);
150
151 if (intel_texobj->mt == NULL) {
152 return false;
153 }
154 }
155
156 for (face = 0; face < numFaces; face++) {
157 for (level = 0; level < levels; level++) {
158 struct gl_texture_image *image = texobj->Image[face][level];
159 struct intel_texture_image *intel_image = intel_texture_image(image);
160
161 image->NumSamples = num_samples;
162
163 _swrast_free_texture_image_buffer(ctx, image);
164 if (!_swrast_init_texture_image(image))
165 return false;
166
167 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
168 }
169 }
170
171 /* The miptree is in a validated state, so no need to check later. */
172 intel_texobj->needs_validate = false;
173 intel_texobj->validated_first_level = 0;
174 intel_texobj->validated_last_level = levels - 1;
175 intel_texobj->_Format = intel_texobj->mt->format;
176
177 return true;
178 }
179
180
181 static void
182 intel_free_texture_image_buffer(struct gl_context * ctx,
183 struct gl_texture_image *texImage)
184 {
185 struct intel_texture_image *intelImage = intel_texture_image(texImage);
186
187 DBG("%s\n", __func__);
188
189 intel_miptree_release(&intelImage->mt);
190
191 _swrast_free_texture_image_buffer(ctx, texImage);
192 }
193
194 /**
195 * Map texture memory/buffer into user space.
196 * Note: the region of interest parameters are ignored here.
197 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
198 * \param mapOut returns start of mapping of region of interest
199 * \param rowStrideOut returns row stride in bytes
200 */
201 static void
202 intel_map_texture_image(struct gl_context *ctx,
203 struct gl_texture_image *tex_image,
204 GLuint slice,
205 GLuint x, GLuint y, GLuint w, GLuint h,
206 GLbitfield mode,
207 GLubyte **map,
208 GLint *out_stride)
209 {
210 struct brw_context *brw = brw_context(ctx);
211 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
212 struct intel_mipmap_tree *mt = intel_image->mt;
213 ptrdiff_t stride;
214
215 /* Our texture data is always stored in a miptree. */
216 assert(mt);
217
218 /* Check that our caller wasn't confused about how to map a 1D texture. */
219 assert(tex_image->TexObject->Target != GL_TEXTURE_1D_ARRAY ||
220 h == 1);
221
222 /* intel_miptree_map operates on a unified "slice" number that references the
223 * cube face, since it's all just slices to the miptree code.
224 */
225 if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
226 slice = tex_image->Face;
227
228 intel_miptree_map(brw, mt,
229 tex_image->Level + tex_image->TexObject->MinLevel,
230 slice + tex_image->TexObject->MinLayer,
231 x, y, w, h, mode,
232 (void **)map, &stride);
233
234 *out_stride = stride;
235 }
236
237 static void
238 intel_unmap_texture_image(struct gl_context *ctx,
239 struct gl_texture_image *tex_image, GLuint slice)
240 {
241 struct brw_context *brw = brw_context(ctx);
242 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
243 struct intel_mipmap_tree *mt = intel_image->mt;
244
245 if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
246 slice = tex_image->Face;
247
248 intel_miptree_unmap(brw, mt,
249 tex_image->Level + tex_image->TexObject->MinLevel,
250 slice + tex_image->TexObject->MinLayer);
251 }
252
253 static GLboolean
254 intel_texture_view(struct gl_context *ctx,
255 struct gl_texture_object *texObj,
256 struct gl_texture_object *origTexObj)
257 {
258 struct brw_context *brw = brw_context(ctx);
259 struct intel_texture_object *intel_tex = intel_texture_object(texObj);
260 struct intel_texture_object *intel_orig_tex = intel_texture_object(origTexObj);
261
262 assert(intel_orig_tex->mt);
263 intel_miptree_reference(&intel_tex->mt, intel_orig_tex->mt);
264
265 /* Since we can only make views of immutable-format textures,
266 * we can assume that everything is in origTexObj's miptree.
267 *
268 * Mesa core has already made us a copy of all the teximage objects,
269 * except it hasn't copied our mt pointers, etc.
270 */
271 const int numFaces = _mesa_num_tex_faces(texObj->Target);
272 const int numLevels = texObj->NumLevels;
273
274 int face;
275 int level;
276
277 for (face = 0; face < numFaces; face++) {
278 for (level = 0; level < numLevels; level++) {
279 struct gl_texture_image *image = texObj->Image[face][level];
280 struct intel_texture_image *intel_image = intel_texture_image(image);
281
282 intel_miptree_reference(&intel_image->mt, intel_orig_tex->mt);
283 }
284 }
285
286 /* The miptree is in a validated state, so no need to check later. */
287 intel_tex->needs_validate = false;
288 intel_tex->validated_first_level = 0;
289 intel_tex->validated_last_level = numLevels - 1;
290
291 /* Set the validated texture format, with the same adjustments that
292 * would have been applied to determine the underlying texture's
293 * mt->format.
294 */
295 intel_tex->_Format = intel_depth_format_for_depthstencil_format(
296 intel_lower_compressed_format(brw, texObj->Image[0][0]->TexFormat));
297
298 return GL_TRUE;
299 }
300
301 static bool
302 intel_set_texture_storage_for_buffer_object(struct gl_context *ctx,
303 struct gl_texture_object *tex_obj,
304 struct gl_buffer_object *buffer_obj,
305 uint32_t buffer_offset,
306 uint32_t row_stride,
307 bool read_only)
308 {
309 struct brw_context *brw = brw_context(ctx);
310 struct intel_texture_object *intel_texobj = intel_texture_object(tex_obj);
311 struct gl_texture_image *image = tex_obj->Image[0][0];
312 struct intel_texture_image *intel_image = intel_texture_image(image);
313 struct intel_buffer_object *intel_buffer_obj = intel_buffer_object(buffer_obj);
314
315 if (!read_only) {
316 /* Renderbuffers have the restriction that the buffer offset and
317 * surface pitch must be a multiple of the element size. If it's
318 * not, we have to fail and fall back to software.
319 */
320 int cpp = _mesa_get_format_bytes(image->TexFormat);
321 if (buffer_offset % cpp || row_stride % cpp) {
322 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
323 return false;
324 }
325
326 if (!brw->format_supported_as_render_target[image->TexFormat]) {
327 perf_debug("Non-renderable PBO format; fallback to CPU mapping\n");
328 return false;
329 }
330 }
331
332 assert(intel_texobj->mt == NULL);
333
334 drm_intel_bo *bo = intel_bufferobj_buffer(brw, intel_buffer_obj,
335 buffer_offset,
336 row_stride * image->Height);
337 intel_texobj->mt =
338 intel_miptree_create_for_bo(brw, bo,
339 image->TexFormat,
340 buffer_offset,
341 image->Width, image->Height, image->Depth,
342 row_stride,
343 0);
344 if (!intel_texobj->mt)
345 return false;
346
347 if (!_swrast_init_texture_image(image))
348 return false;
349
350 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
351
352 /* The miptree is in a validated state, so no need to check later. */
353 intel_texobj->needs_validate = false;
354 intel_texobj->validated_first_level = 0;
355 intel_texobj->validated_last_level = 0;
356 intel_texobj->_Format = intel_texobj->mt->format;
357
358 return true;
359 }
360
361 static void
362 intel_texture_barrier(struct gl_context *ctx)
363 {
364 struct brw_context *brw = brw_context(ctx);
365
366 if (brw->gen >= 6) {
367 if (brw->gen == 6) {
368 /* [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache
369 * Flush Enable = 1, a PIPE_CONTROL with any non-zero
370 * post-sync-op is required.
371 */
372 brw_emit_post_sync_nonzero_flush(brw);
373 }
374
375 brw_emit_pipe_control_flush(brw,
376 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
377 PIPE_CONTROL_RENDER_TARGET_FLUSH |
378 PIPE_CONTROL_CS_STALL);
379
380 brw_emit_pipe_control_flush(brw,
381 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
382 } else {
383 brw_emit_mi_flush(brw);
384 }
385 }
386
387 void
388 intelInitTextureFuncs(struct dd_function_table *functions)
389 {
390 functions->NewTextureObject = intelNewTextureObject;
391 functions->NewTextureImage = intelNewTextureImage;
392 functions->DeleteTextureImage = intelDeleteTextureImage;
393 functions->DeleteTexture = intelDeleteTextureObject;
394 functions->AllocTextureImageBuffer = intel_alloc_texture_image_buffer;
395 functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
396 functions->AllocTextureStorage = intel_alloc_texture_storage;
397 functions->MapTextureImage = intel_map_texture_image;
398 functions->UnmapTextureImage = intel_unmap_texture_image;
399 functions->TextureView = intel_texture_view;
400 functions->SetTextureStorageForBufferObject =
401 intel_set_texture_storage_for_buffer_object;
402 functions->TextureBarrier = intel_texture_barrier;
403 }