i965: Make unsynchronized maps unsynchronized on non-LLC
[mesa.git] / src / mesa / drivers / dri / i965 / intel_buffer_objects.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 /**
27 * @file intel_buffer_objects.c
28 *
29 * This provides core GL buffer object functionality.
30 */
31
32 #include "main/imports.h"
33 #include "main/mtypes.h"
34 #include "main/macros.h"
35 #include "main/bufferobj.h"
36
37 #include "brw_context.h"
38 #include "intel_blit.h"
39 #include "intel_buffer_objects.h"
40 #include "intel_batchbuffer.h"
41
42 static void
43 mark_buffer_gpu_usage(struct intel_buffer_object *intel_obj,
44 uint32_t offset, uint32_t size)
45 {
46 intel_obj->gpu_active_start = MIN2(intel_obj->gpu_active_start, offset);
47 intel_obj->gpu_active_end = MAX2(intel_obj->gpu_active_end, offset + size);
48 }
49
50 static void
51 mark_buffer_inactive(struct intel_buffer_object *intel_obj)
52 {
53 intel_obj->gpu_active_start = ~0;
54 intel_obj->gpu_active_end = 0;
55 }
56
57 /** Allocates a new brw_bo to store the data for the buffer object. */
58 static void
59 alloc_buffer_object(struct brw_context *brw,
60 struct intel_buffer_object *intel_obj)
61 {
62 intel_obj->buffer = brw_bo_alloc(brw->bufmgr, "bufferobj",
63 intel_obj->Base.Size, 64);
64
65 /* the buffer might be bound as a uniform buffer, need to update it
66 */
67 if (intel_obj->Base.UsageHistory & USAGE_UNIFORM_BUFFER)
68 brw->ctx.NewDriverState |= BRW_NEW_UNIFORM_BUFFER;
69 if (intel_obj->Base.UsageHistory & USAGE_SHADER_STORAGE_BUFFER)
70 brw->ctx.NewDriverState |= BRW_NEW_UNIFORM_BUFFER;
71 if (intel_obj->Base.UsageHistory & USAGE_TEXTURE_BUFFER)
72 brw->ctx.NewDriverState |= BRW_NEW_TEXTURE_BUFFER;
73 if (intel_obj->Base.UsageHistory & USAGE_ATOMIC_COUNTER_BUFFER)
74 brw->ctx.NewDriverState |= BRW_NEW_ATOMIC_BUFFER;
75
76 mark_buffer_inactive(intel_obj);
77 }
78
79 static void
80 release_buffer(struct intel_buffer_object *intel_obj)
81 {
82 brw_bo_unreference(intel_obj->buffer);
83 intel_obj->buffer = NULL;
84 }
85
86 /**
87 * The NewBufferObject() driver hook.
88 *
89 * Allocates a new intel_buffer_object structure and initializes it.
90 *
91 * There is some duplication between mesa's bufferobjects and our
92 * bufmgr buffers. Both have an integer handle and a hashtable to
93 * lookup an opaque structure. It would be nice if the handles and
94 * internal structure where somehow shared.
95 */
96 static struct gl_buffer_object *
97 brw_new_buffer_object(struct gl_context * ctx, GLuint name)
98 {
99 struct intel_buffer_object *obj = CALLOC_STRUCT(intel_buffer_object);
100 if (!obj) {
101 _mesa_error_no_memory(__func__);
102 }
103
104 _mesa_initialize_buffer_object(ctx, &obj->Base, name);
105
106 obj->buffer = NULL;
107
108 return &obj->Base;
109 }
110
111 /**
112 * The DeleteBuffer() driver hook.
113 *
114 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
115 */
116 static void
117 brw_delete_buffer(struct gl_context * ctx, struct gl_buffer_object *obj)
118 {
119 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
120
121 assert(intel_obj);
122
123 /* Buffer objects are automatically unmapped when deleting according
124 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
125 * (though it does if you call glDeleteBuffers)
126 */
127 _mesa_buffer_unmap_all_mappings(ctx, obj);
128
129 brw_bo_unreference(intel_obj->buffer);
130 _mesa_delete_buffer_object(ctx, obj);
131 }
132
133
134 /**
135 * The BufferData() driver hook.
136 *
137 * Implements glBufferData(), which recreates a buffer object's data store
138 * and populates it with the given data, if present.
139 *
140 * Any data that was previously stored in the buffer object is lost.
141 *
142 * \return true for success, false if out of memory
143 */
144 static GLboolean
145 brw_buffer_data(struct gl_context *ctx,
146 GLenum target,
147 GLsizeiptrARB size,
148 const GLvoid *data,
149 GLenum usage,
150 GLbitfield storageFlags,
151 struct gl_buffer_object *obj)
152 {
153 struct brw_context *brw = brw_context(ctx);
154 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
155
156 /* Part of the ABI, but this function doesn't use it.
157 */
158 (void) target;
159
160 intel_obj->Base.Size = size;
161 intel_obj->Base.Usage = usage;
162 intel_obj->Base.StorageFlags = storageFlags;
163
164 assert(!obj->Mappings[MAP_USER].Pointer); /* Mesa should have unmapped it */
165 assert(!obj->Mappings[MAP_INTERNAL].Pointer);
166
167 if (intel_obj->buffer != NULL)
168 release_buffer(intel_obj);
169
170 if (size != 0) {
171 alloc_buffer_object(brw, intel_obj);
172 if (!intel_obj->buffer)
173 return false;
174
175 if (data != NULL)
176 brw_bo_subdata(intel_obj->buffer, 0, size, data);
177 }
178
179 return true;
180 }
181
182
183 /**
184 * The BufferSubData() driver hook.
185 *
186 * Implements glBufferSubData(), which replaces a portion of the data in a
187 * buffer object.
188 *
189 * If the data range specified by (size + offset) extends beyond the end of
190 * the buffer or if data is NULL, no copy is performed.
191 */
192 static void
193 brw_buffer_subdata(struct gl_context *ctx,
194 GLintptrARB offset,
195 GLsizeiptrARB size,
196 const GLvoid *data,
197 struct gl_buffer_object *obj)
198 {
199 struct brw_context *brw = brw_context(ctx);
200 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
201 bool busy;
202
203 if (size == 0)
204 return;
205
206 assert(intel_obj);
207
208 /* See if we can unsynchronized write the data into the user's BO. This
209 * avoids GPU stalls in unfortunately common user patterns (uploading
210 * sequentially into a BO, with draw calls in between each upload).
211 *
212 * Once we've hit this path, we mark this GL BO as preferring stalling to
213 * blits, so that we can hopefully hit this path again in the future
214 * (otherwise, an app that might occasionally stall but mostly not will end
215 * up with blitting all the time, at the cost of bandwidth)
216 */
217 if (offset + size <= intel_obj->gpu_active_start ||
218 intel_obj->gpu_active_end <= offset) {
219 if (brw->has_llc) {
220 void *map = brw_bo_map_unsynchronized(brw, intel_obj->buffer);
221 memcpy(map + offset, data, size);
222 brw_bo_unmap(intel_obj->buffer);
223
224 if (intel_obj->gpu_active_end > intel_obj->gpu_active_start)
225 intel_obj->prefer_stall_to_blit = true;
226 return;
227 } else {
228 perf_debug("BufferSubData could be unsynchronized, but !LLC doesn't support it yet\n");
229 }
230 }
231
232 busy =
233 brw_bo_busy(intel_obj->buffer) ||
234 brw_batch_references(&brw->batch, intel_obj->buffer);
235
236 if (busy) {
237 if (size == intel_obj->Base.Size) {
238 /* Replace the current busy bo so the subdata doesn't stall. */
239 brw_bo_unreference(intel_obj->buffer);
240 alloc_buffer_object(brw, intel_obj);
241 } else if (!intel_obj->prefer_stall_to_blit) {
242 perf_debug("Using a blit copy to avoid stalling on "
243 "glBufferSubData(%ld, %ld) (%ldkb) to a busy "
244 "(%d-%d) buffer object.\n",
245 (long)offset, (long)offset + size, (long)(size/1024),
246 intel_obj->gpu_active_start,
247 intel_obj->gpu_active_end);
248 struct brw_bo *temp_bo =
249 brw_bo_alloc(brw->bufmgr, "subdata temp", size, 64);
250
251 brw_bo_subdata(temp_bo, 0, size, data);
252
253 intel_emit_linear_blit(brw,
254 intel_obj->buffer, offset,
255 temp_bo, 0,
256 size);
257
258 brw_bo_unreference(temp_bo);
259 return;
260 } else {
261 perf_debug("Stalling on glBufferSubData(%ld, %ld) (%ldkb) to a busy "
262 "(%d-%d) buffer object. Use glMapBufferRange() to "
263 "avoid this.\n",
264 (long)offset, (long)offset + size, (long)(size/1024),
265 intel_obj->gpu_active_start,
266 intel_obj->gpu_active_end);
267 intel_batchbuffer_flush(brw);
268 }
269 }
270
271 brw_bo_subdata(intel_obj->buffer, offset, size, data);
272 mark_buffer_inactive(intel_obj);
273 }
274
275
276 /**
277 * The GetBufferSubData() driver hook.
278 *
279 * Implements glGetBufferSubData(), which copies a subrange of a buffer
280 * object into user memory.
281 */
282 static void
283 brw_get_buffer_subdata(struct gl_context *ctx,
284 GLintptrARB offset,
285 GLsizeiptrARB size,
286 GLvoid *data,
287 struct gl_buffer_object *obj)
288 {
289 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
290 struct brw_context *brw = brw_context(ctx);
291
292 assert(intel_obj);
293 if (brw_batch_references(&brw->batch, intel_obj->buffer)) {
294 intel_batchbuffer_flush(brw);
295 }
296 brw_bo_get_subdata(intel_obj->buffer, offset, size, data);
297
298 mark_buffer_inactive(intel_obj);
299 }
300
301
302 /**
303 * The MapBufferRange() driver hook.
304 *
305 * This implements both glMapBufferRange() and glMapBuffer().
306 *
307 * The goal of this extension is to allow apps to accumulate their rendering
308 * at the same time as they accumulate their buffer object. Without it,
309 * you'd end up blocking on execution of rendering every time you mapped
310 * the buffer to put new data in.
311 *
312 * We support it in 3 ways: If unsynchronized, then don't bother
313 * flushing the batchbuffer before mapping the buffer, which can save blocking
314 * in many cases. If we would still block, and they allow the whole buffer
315 * to be invalidated, then just allocate a new buffer to replace the old one.
316 * If not, and we'd block, and they allow the subrange of the buffer to be
317 * invalidated, then we can make a new little BO, let them write into that,
318 * and blit it into the real BO at unmap time.
319 */
320 static void *
321 brw_map_buffer_range(struct gl_context *ctx,
322 GLintptr offset, GLsizeiptr length,
323 GLbitfield access, struct gl_buffer_object *obj,
324 gl_map_buffer_index index)
325 {
326 struct brw_context *brw = brw_context(ctx);
327 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
328
329 assert(intel_obj);
330
331 STATIC_ASSERT(GL_MAP_UNSYNCHRONIZED_BIT == MAP_ASYNC);
332 STATIC_ASSERT(GL_MAP_WRITE_BIT == MAP_WRITE);
333 STATIC_ASSERT(GL_MAP_READ_BIT == MAP_READ);
334 STATIC_ASSERT(GL_MAP_PERSISTENT_BIT == MAP_PERSISTENT);
335 STATIC_ASSERT(GL_MAP_COHERENT_BIT == MAP_COHERENT);
336 assert((access & MAP_INTERNAL_MASK) == 0);
337
338 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
339 * internally uses our functions directly.
340 */
341 obj->Mappings[index].Offset = offset;
342 obj->Mappings[index].Length = length;
343 obj->Mappings[index].AccessFlags = access;
344
345 if (intel_obj->buffer == NULL) {
346 obj->Mappings[index].Pointer = NULL;
347 return NULL;
348 }
349
350 /* If the access is synchronized (like a normal buffer mapping), then get
351 * things flushed out so the later mapping syncs appropriately through GEM.
352 * If the user doesn't care about existing buffer contents and mapping would
353 * cause us to block, then throw out the old buffer.
354 *
355 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
356 * achieve the required synchronization.
357 */
358 if (!(access & GL_MAP_UNSYNCHRONIZED_BIT)) {
359 if (brw_batch_references(&brw->batch, intel_obj->buffer)) {
360 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
361 brw_bo_unreference(intel_obj->buffer);
362 alloc_buffer_object(brw, intel_obj);
363 } else {
364 perf_debug("Stalling on the GPU for mapping a busy buffer "
365 "object\n");
366 intel_batchbuffer_flush(brw);
367 }
368 } else if (brw_bo_busy(intel_obj->buffer) &&
369 (access & GL_MAP_INVALIDATE_BUFFER_BIT)) {
370 brw_bo_unreference(intel_obj->buffer);
371 alloc_buffer_object(brw, intel_obj);
372 }
373 }
374
375 /* If the user is mapping a range of an active buffer object but
376 * doesn't require the current contents of that range, make a new
377 * BO, and we'll copy what they put in there out at unmap or
378 * FlushRange time.
379 *
380 * That is, unless they're looking for a persistent mapping -- we would
381 * need to do blits in the MemoryBarrier call, and it's easier to just do a
382 * GPU stall and do a mapping.
383 */
384 if (!(access & (GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_PERSISTENT_BIT)) &&
385 (access & GL_MAP_INVALIDATE_RANGE_BIT) &&
386 brw_bo_busy(intel_obj->buffer)) {
387 /* Ensure that the base alignment of the allocation meets the alignment
388 * guarantees the driver has advertised to the application.
389 */
390 const unsigned alignment = ctx->Const.MinMapBufferAlignment;
391
392 intel_obj->map_extra[index] = (uintptr_t) offset % alignment;
393 intel_obj->range_map_bo[index] = brw_bo_alloc(brw->bufmgr,
394 "BO blit temp",
395 length +
396 intel_obj->map_extra[index],
397 alignment);
398 void *map = brw_bo_map(brw, intel_obj->range_map_bo[index], access);
399 obj->Mappings[index].Pointer = map + intel_obj->map_extra[index];
400 return obj->Mappings[index].Pointer;
401 }
402
403 void *map = brw_bo_map(brw, intel_obj->buffer, access);
404 if (!(access & GL_MAP_UNSYNCHRONIZED_BIT)) {
405 mark_buffer_inactive(intel_obj);
406 }
407
408 obj->Mappings[index].Pointer = map + offset;
409 return obj->Mappings[index].Pointer;
410 }
411
412 /**
413 * The FlushMappedBufferRange() driver hook.
414 *
415 * Implements glFlushMappedBufferRange(), which signifies that modifications
416 * have been made to a range of a mapped buffer, and it should be flushed.
417 *
418 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
419 *
420 * Ideally we'd use a BO to avoid taking up cache space for the temporary
421 * data, but FlushMappedBufferRange may be followed by further writes to
422 * the pointer, so we would have to re-map after emitting our blit, which
423 * would defeat the point.
424 */
425 static void
426 brw_flush_mapped_buffer_range(struct gl_context *ctx,
427 GLintptr offset, GLsizeiptr length,
428 struct gl_buffer_object *obj,
429 gl_map_buffer_index index)
430 {
431 struct brw_context *brw = brw_context(ctx);
432 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
433
434 assert(obj->Mappings[index].AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT);
435
436 /* If we gave a direct mapping of the buffer instead of using a temporary,
437 * then there's nothing to do.
438 */
439 if (intel_obj->range_map_bo[index] == NULL)
440 return;
441
442 if (length == 0)
443 return;
444
445 /* Note that we're not unmapping our buffer while executing the blit. We
446 * need to have a mapping still at the end of this call, since the user
447 * gets to make further modifications and glFlushMappedBufferRange() calls.
448 * This is safe, because:
449 *
450 * - On LLC platforms, we're using a CPU mapping that's coherent with the
451 * GPU (except for the render caches), so the kernel doesn't need to do
452 * any flushing work for us except for what happens at batch exec time
453 * anyway.
454 *
455 * - On non-LLC platforms, we're using a GTT mapping that writes directly
456 * to system memory (except for the chipset cache that gets flushed at
457 * batch exec time).
458 *
459 * In both cases we don't need to stall for the previous blit to complete
460 * so we can re-map (and we definitely don't want to, since that would be
461 * slow): If the user edits a part of their buffer that's previously been
462 * blitted, then our lack of synchoronization is fine, because either
463 * they'll get some too-new data in the first blit and not do another blit
464 * of that area (but in that case the results are undefined), or they'll do
465 * another blit of that area and the complete newer data will land the
466 * second time.
467 */
468 intel_emit_linear_blit(brw,
469 intel_obj->buffer,
470 obj->Mappings[index].Offset + offset,
471 intel_obj->range_map_bo[index],
472 intel_obj->map_extra[index] + offset,
473 length);
474 mark_buffer_gpu_usage(intel_obj,
475 obj->Mappings[index].Offset + offset,
476 length);
477 }
478
479
480 /**
481 * The UnmapBuffer() driver hook.
482 *
483 * Implements glUnmapBuffer().
484 */
485 static GLboolean
486 brw_unmap_buffer(struct gl_context *ctx,
487 struct gl_buffer_object *obj,
488 gl_map_buffer_index index)
489 {
490 struct brw_context *brw = brw_context(ctx);
491 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
492
493 assert(intel_obj);
494 assert(obj->Mappings[index].Pointer);
495 if (intel_obj->range_map_bo[index] != NULL) {
496 brw_bo_unmap(intel_obj->range_map_bo[index]);
497
498 if (!(obj->Mappings[index].AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT)) {
499 intel_emit_linear_blit(brw,
500 intel_obj->buffer, obj->Mappings[index].Offset,
501 intel_obj->range_map_bo[index],
502 intel_obj->map_extra[index],
503 obj->Mappings[index].Length);
504 mark_buffer_gpu_usage(intel_obj, obj->Mappings[index].Offset,
505 obj->Mappings[index].Length);
506 }
507
508 /* Since we've emitted some blits to buffers that will (likely) be used
509 * in rendering operations in other cache domains in this batch, emit a
510 * flush. Once again, we wish for a domain tracker in libdrm to cover
511 * usage inside of a batchbuffer.
512 */
513 brw_emit_mi_flush(brw);
514
515 brw_bo_unreference(intel_obj->range_map_bo[index]);
516 intel_obj->range_map_bo[index] = NULL;
517 } else if (intel_obj->buffer != NULL) {
518 brw_bo_unmap(intel_obj->buffer);
519 }
520 obj->Mappings[index].Pointer = NULL;
521 obj->Mappings[index].Offset = 0;
522 obj->Mappings[index].Length = 0;
523
524 return true;
525 }
526
527 /**
528 * Gets a pointer to the object's BO, and marks the given range as being used
529 * on the GPU.
530 *
531 * Anywhere that uses buffer objects in the pipeline should be using this to
532 * mark the range of the buffer that is being accessed by the pipeline.
533 */
534 struct brw_bo *
535 intel_bufferobj_buffer(struct brw_context *brw,
536 struct intel_buffer_object *intel_obj,
537 uint32_t offset, uint32_t size)
538 {
539 /* This is needed so that things like transform feedback and texture buffer
540 * objects that need a BO but don't want to check that they exist for
541 * draw-time validation can just always get a BO from a GL buffer object.
542 */
543 if (intel_obj->buffer == NULL)
544 alloc_buffer_object(brw, intel_obj);
545
546 mark_buffer_gpu_usage(intel_obj, offset, size);
547
548 return intel_obj->buffer;
549 }
550
551 /**
552 * The CopyBufferSubData() driver hook.
553 *
554 * Implements glCopyBufferSubData(), which copies a portion of one buffer
555 * object's data to another. Independent source and destination offsets
556 * are allowed.
557 */
558 static void
559 brw_copy_buffer_subdata(struct gl_context *ctx,
560 struct gl_buffer_object *src,
561 struct gl_buffer_object *dst,
562 GLintptr read_offset, GLintptr write_offset,
563 GLsizeiptr size)
564 {
565 struct brw_context *brw = brw_context(ctx);
566 struct intel_buffer_object *intel_src = intel_buffer_object(src);
567 struct intel_buffer_object *intel_dst = intel_buffer_object(dst);
568 struct brw_bo *src_bo, *dst_bo;
569
570 if (size == 0)
571 return;
572
573 dst_bo = intel_bufferobj_buffer(brw, intel_dst, write_offset, size);
574 src_bo = intel_bufferobj_buffer(brw, intel_src, read_offset, size);
575
576 intel_emit_linear_blit(brw,
577 dst_bo, write_offset,
578 src_bo, read_offset, size);
579
580 /* Since we've emitted some blits to buffers that will (likely) be used
581 * in rendering operations in other cache domains in this batch, emit a
582 * flush. Once again, we wish for a domain tracker in libdrm to cover
583 * usage inside of a batchbuffer.
584 */
585 brw_emit_mi_flush(brw);
586 }
587
588 void
589 intelInitBufferObjectFuncs(struct dd_function_table *functions)
590 {
591 functions->NewBufferObject = brw_new_buffer_object;
592 functions->DeleteBuffer = brw_delete_buffer;
593 functions->BufferData = brw_buffer_data;
594 functions->BufferSubData = brw_buffer_subdata;
595 functions->GetBufferSubData = brw_get_buffer_subdata;
596 functions->MapBufferRange = brw_map_buffer_range;
597 functions->FlushMappedBufferRange = brw_flush_mapped_buffer_range;
598 functions->UnmapBuffer = brw_unmap_buffer;
599 functions->CopyBufferSubData = brw_copy_buffer_subdata;
600 }