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.
27 * @file intel_buffer_objects.c
29 * This provides core GL buffer object functionality.
32 #include "main/imports.h"
33 #include "main/mtypes.h"
34 #include "main/macros.h"
35 #include "main/bufferobj.h"
37 #include "brw_context.h"
38 #include "intel_blit.h"
39 #include "intel_buffer_objects.h"
40 #include "intel_batchbuffer.h"
43 * Map a buffer object; issue performance warnings if mapping causes stalls.
45 * This matches the drm_intel_bo_map API, but takes an additional human-readable
46 * name for the buffer object to use in the performance debug message.
49 brw_bo_map(struct brw_context
*brw
,
50 drm_intel_bo
*bo
, int write_enable
,
53 if (likely(!brw
->perf_debug
) || !drm_intel_bo_busy(bo
))
54 return drm_intel_bo_map(bo
, write_enable
);
56 double start_time
= get_time();
58 int ret
= drm_intel_bo_map(bo
, write_enable
);
60 perf_debug("CPU mapping a busy %s BO stalled and took %.03f ms.\n",
61 bo_name
, (get_time() - start_time
) * 1000);
67 brw_bo_map_gtt(struct brw_context
*brw
, drm_intel_bo
*bo
, const char *bo_name
)
69 if (likely(!brw
->perf_debug
) || !drm_intel_bo_busy(bo
))
70 return drm_intel_gem_bo_map_gtt(bo
);
72 double start_time
= get_time();
74 int ret
= drm_intel_gem_bo_map_gtt(bo
);
76 perf_debug("GTT mapping a busy %s BO stalled and took %.03f ms.\n",
77 bo_name
, (get_time() - start_time
) * 1000);
83 mark_buffer_gpu_usage(struct intel_buffer_object
*intel_obj
,
84 uint32_t offset
, uint32_t size
)
86 intel_obj
->gpu_active_start
= MIN2(intel_obj
->gpu_active_start
, offset
);
87 intel_obj
->gpu_active_end
= MAX2(intel_obj
->gpu_active_end
, offset
+ size
);
91 mark_buffer_inactive(struct intel_buffer_object
*intel_obj
)
93 intel_obj
->gpu_active_start
= ~0;
94 intel_obj
->gpu_active_end
= 0;
97 /** Allocates a new drm_intel_bo to store the data for the buffer object. */
99 alloc_buffer_object(struct brw_context
*brw
,
100 struct intel_buffer_object
*intel_obj
)
102 intel_obj
->buffer
= drm_intel_bo_alloc(brw
->bufmgr
, "bufferobj",
103 intel_obj
->Base
.Size
, 64);
105 /* the buffer might be bound as a uniform buffer, need to update it
107 if (intel_obj
->Base
.UsageHistory
& USAGE_UNIFORM_BUFFER
)
108 brw
->ctx
.NewDriverState
|= BRW_NEW_UNIFORM_BUFFER
;
109 if (intel_obj
->Base
.UsageHistory
& USAGE_SHADER_STORAGE_BUFFER
)
110 brw
->ctx
.NewDriverState
|= BRW_NEW_UNIFORM_BUFFER
;
111 if (intel_obj
->Base
.UsageHistory
& USAGE_TEXTURE_BUFFER
)
112 brw
->ctx
.NewDriverState
|= BRW_NEW_TEXTURE_BUFFER
;
113 if (intel_obj
->Base
.UsageHistory
& USAGE_ATOMIC_COUNTER_BUFFER
)
114 brw
->ctx
.NewDriverState
|= BRW_NEW_ATOMIC_BUFFER
;
116 mark_buffer_inactive(intel_obj
);
120 release_buffer(struct intel_buffer_object
*intel_obj
)
122 drm_intel_bo_unreference(intel_obj
->buffer
);
123 intel_obj
->buffer
= NULL
;
127 * The NewBufferObject() driver hook.
129 * Allocates a new intel_buffer_object structure and initializes it.
131 * There is some duplication between mesa's bufferobjects and our
132 * bufmgr buffers. Both have an integer handle and a hashtable to
133 * lookup an opaque structure. It would be nice if the handles and
134 * internal structure where somehow shared.
136 static struct gl_buffer_object
*
137 brw_new_buffer_object(struct gl_context
* ctx
, GLuint name
)
139 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
141 _mesa_error_no_memory(__func__
);
144 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
);
152 * The DeleteBuffer() driver hook.
154 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
157 brw_delete_buffer(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
159 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
163 /* Buffer objects are automatically unmapped when deleting according
164 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
165 * (though it does if you call glDeleteBuffers)
167 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
169 drm_intel_bo_unreference(intel_obj
->buffer
);
170 _mesa_delete_buffer_object(ctx
, obj
);
175 * The BufferData() driver hook.
177 * Implements glBufferData(), which recreates a buffer object's data store
178 * and populates it with the given data, if present.
180 * Any data that was previously stored in the buffer object is lost.
182 * \return true for success, false if out of memory
185 brw_buffer_data(struct gl_context
*ctx
,
190 GLbitfield storageFlags
,
191 struct gl_buffer_object
*obj
)
193 struct brw_context
*brw
= brw_context(ctx
);
194 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
196 /* Part of the ABI, but this function doesn't use it.
200 intel_obj
->Base
.Size
= size
;
201 intel_obj
->Base
.Usage
= usage
;
202 intel_obj
->Base
.StorageFlags
= storageFlags
;
204 assert(!obj
->Mappings
[MAP_USER
].Pointer
); /* Mesa should have unmapped it */
205 assert(!obj
->Mappings
[MAP_INTERNAL
].Pointer
);
207 if (intel_obj
->buffer
!= NULL
)
208 release_buffer(intel_obj
);
211 alloc_buffer_object(brw
, intel_obj
);
212 if (!intel_obj
->buffer
)
216 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
224 * The BufferSubData() driver hook.
226 * Implements glBufferSubData(), which replaces a portion of the data in a
229 * If the data range specified by (size + offset) extends beyond the end of
230 * the buffer or if data is NULL, no copy is performed.
233 brw_buffer_subdata(struct gl_context
*ctx
,
237 struct gl_buffer_object
*obj
)
239 struct brw_context
*brw
= brw_context(ctx
);
240 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
248 /* See if we can unsynchronized write the data into the user's BO. This
249 * avoids GPU stalls in unfortunately common user patterns (uploading
250 * sequentially into a BO, with draw calls in between each upload).
252 * Once we've hit this path, we mark this GL BO as preferring stalling to
253 * blits, so that we can hopefully hit this path again in the future
254 * (otherwise, an app that might occasionally stall but mostly not will end
255 * up with blitting all the time, at the cost of bandwidth)
257 if (offset
+ size
<= intel_obj
->gpu_active_start
||
258 intel_obj
->gpu_active_end
<= offset
) {
260 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
261 memcpy(intel_obj
->buffer
->virtual + offset
, data
, size
);
262 drm_intel_bo_unmap(intel_obj
->buffer
);
264 if (intel_obj
->gpu_active_end
> intel_obj
->gpu_active_start
)
265 intel_obj
->prefer_stall_to_blit
= true;
268 perf_debug("BufferSubData could be unsynchronized, but !LLC doesn't support it yet\n");
273 drm_intel_bo_busy(intel_obj
->buffer
) ||
274 drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
);
277 if (size
== intel_obj
->Base
.Size
) {
278 /* Replace the current busy bo so the subdata doesn't stall. */
279 drm_intel_bo_unreference(intel_obj
->buffer
);
280 alloc_buffer_object(brw
, intel_obj
);
281 } else if (!intel_obj
->prefer_stall_to_blit
) {
282 perf_debug("Using a blit copy to avoid stalling on "
283 "glBufferSubData(%ld, %ld) (%ldkb) to a busy "
284 "(%d-%d) buffer object.\n",
285 (long)offset
, (long)offset
+ size
, (long)(size
/1024),
286 intel_obj
->gpu_active_start
,
287 intel_obj
->gpu_active_end
);
288 drm_intel_bo
*temp_bo
=
289 drm_intel_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
291 drm_intel_bo_subdata(temp_bo
, 0, size
, data
);
293 intel_emit_linear_blit(brw
,
294 intel_obj
->buffer
, offset
,
298 drm_intel_bo_unreference(temp_bo
);
301 perf_debug("Stalling on glBufferSubData(%ld, %ld) (%ldkb) to a busy "
302 "(%d-%d) buffer object. Use glMapBufferRange() to "
304 (long)offset
, (long)offset
+ size
, (long)(size
/1024),
305 intel_obj
->gpu_active_start
,
306 intel_obj
->gpu_active_end
);
307 intel_batchbuffer_flush(brw
);
311 drm_intel_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
312 mark_buffer_inactive(intel_obj
);
317 * The GetBufferSubData() driver hook.
319 * Implements glGetBufferSubData(), which copies a subrange of a buffer
320 * object into user memory.
323 brw_get_buffer_subdata(struct gl_context
*ctx
,
327 struct gl_buffer_object
*obj
)
329 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
330 struct brw_context
*brw
= brw_context(ctx
);
333 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
334 intel_batchbuffer_flush(brw
);
336 drm_intel_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
338 mark_buffer_inactive(intel_obj
);
343 * The MapBufferRange() driver hook.
345 * This implements both glMapBufferRange() and glMapBuffer().
347 * The goal of this extension is to allow apps to accumulate their rendering
348 * at the same time as they accumulate their buffer object. Without it,
349 * you'd end up blocking on execution of rendering every time you mapped
350 * the buffer to put new data in.
352 * We support it in 3 ways: If unsynchronized, then don't bother
353 * flushing the batchbuffer before mapping the buffer, which can save blocking
354 * in many cases. If we would still block, and they allow the whole buffer
355 * to be invalidated, then just allocate a new buffer to replace the old one.
356 * If not, and we'd block, and they allow the subrange of the buffer to be
357 * invalidated, then we can make a new little BO, let them write into that,
358 * and blit it into the real BO at unmap time.
361 brw_map_buffer_range(struct gl_context
*ctx
,
362 GLintptr offset
, GLsizeiptr length
,
363 GLbitfield access
, struct gl_buffer_object
*obj
,
364 gl_map_buffer_index index
)
366 struct brw_context
*brw
= brw_context(ctx
);
367 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
371 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
372 * internally uses our functions directly.
374 obj
->Mappings
[index
].Offset
= offset
;
375 obj
->Mappings
[index
].Length
= length
;
376 obj
->Mappings
[index
].AccessFlags
= access
;
378 if (intel_obj
->buffer
== NULL
) {
379 obj
->Mappings
[index
].Pointer
= NULL
;
383 /* If the access is synchronized (like a normal buffer mapping), then get
384 * things flushed out so the later mapping syncs appropriately through GEM.
385 * If the user doesn't care about existing buffer contents and mapping would
386 * cause us to block, then throw out the old buffer.
388 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
389 * achieve the required synchronization.
391 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
392 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
393 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
394 drm_intel_bo_unreference(intel_obj
->buffer
);
395 alloc_buffer_object(brw
, intel_obj
);
397 perf_debug("Stalling on the GPU for mapping a busy buffer "
399 intel_batchbuffer_flush(brw
);
401 } else if (drm_intel_bo_busy(intel_obj
->buffer
) &&
402 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
403 drm_intel_bo_unreference(intel_obj
->buffer
);
404 alloc_buffer_object(brw
, intel_obj
);
408 /* If the user is mapping a range of an active buffer object but
409 * doesn't require the current contents of that range, make a new
410 * BO, and we'll copy what they put in there out at unmap or
413 * That is, unless they're looking for a persistent mapping -- we would
414 * need to do blits in the MemoryBarrier call, and it's easier to just do a
415 * GPU stall and do a mapping.
417 if (!(access
& (GL_MAP_UNSYNCHRONIZED_BIT
| GL_MAP_PERSISTENT_BIT
)) &&
418 (access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
419 drm_intel_bo_busy(intel_obj
->buffer
)) {
420 /* Ensure that the base alignment of the allocation meets the alignment
421 * guarantees the driver has advertised to the application.
423 const unsigned alignment
= ctx
->Const
.MinMapBufferAlignment
;
425 intel_obj
->map_extra
[index
] = (uintptr_t) offset
% alignment
;
426 intel_obj
->range_map_bo
[index
] = drm_intel_bo_alloc(brw
->bufmgr
,
429 intel_obj
->map_extra
[index
],
432 brw_bo_map(brw
, intel_obj
->range_map_bo
[index
],
433 (access
& GL_MAP_WRITE_BIT
) != 0, "range-map");
435 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
[index
]);
437 obj
->Mappings
[index
].Pointer
=
438 intel_obj
->range_map_bo
[index
]->virtual + intel_obj
->map_extra
[index
];
439 return obj
->Mappings
[index
].Pointer
;
442 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
) {
443 if (!brw
->has_llc
&& brw
->perf_debug
&&
444 drm_intel_bo_busy(intel_obj
->buffer
)) {
445 perf_debug("MapBufferRange with GL_MAP_UNSYNCHRONIZED_BIT stalling (it's actually synchronized on non-LLC platforms)\n");
447 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
448 } else if (!brw
->has_llc
&& (!(access
& GL_MAP_READ_BIT
) ||
449 (access
& GL_MAP_PERSISTENT_BIT
))) {
450 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
451 mark_buffer_inactive(intel_obj
);
453 brw_bo_map(brw
, intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0,
455 mark_buffer_inactive(intel_obj
);
458 obj
->Mappings
[index
].Pointer
= intel_obj
->buffer
->virtual + offset
;
459 return obj
->Mappings
[index
].Pointer
;
463 * The FlushMappedBufferRange() driver hook.
465 * Implements glFlushMappedBufferRange(), which signifies that modifications
466 * have been made to a range of a mapped buffer, and it should be flushed.
468 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
470 * Ideally we'd use a BO to avoid taking up cache space for the temporary
471 * data, but FlushMappedBufferRange may be followed by further writes to
472 * the pointer, so we would have to re-map after emitting our blit, which
473 * would defeat the point.
476 brw_flush_mapped_buffer_range(struct gl_context
*ctx
,
477 GLintptr offset
, GLsizeiptr length
,
478 struct gl_buffer_object
*obj
,
479 gl_map_buffer_index index
)
481 struct brw_context
*brw
= brw_context(ctx
);
482 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
484 assert(obj
->Mappings
[index
].AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
);
486 /* If we gave a direct mapping of the buffer instead of using a temporary,
487 * then there's nothing to do.
489 if (intel_obj
->range_map_bo
[index
] == NULL
)
495 /* Note that we're not unmapping our buffer while executing the blit. We
496 * need to have a mapping still at the end of this call, since the user
497 * gets to make further modifications and glFlushMappedBufferRange() calls.
498 * This is safe, because:
500 * - On LLC platforms, we're using a CPU mapping that's coherent with the
501 * GPU (except for the render caches), so the kernel doesn't need to do
502 * any flushing work for us except for what happens at batch exec time
505 * - On non-LLC platforms, we're using a GTT mapping that writes directly
506 * to system memory (except for the chipset cache that gets flushed at
509 * In both cases we don't need to stall for the previous blit to complete
510 * so we can re-map (and we definitely don't want to, since that would be
511 * slow): If the user edits a part of their buffer that's previously been
512 * blitted, then our lack of synchoronization is fine, because either
513 * they'll get some too-new data in the first blit and not do another blit
514 * of that area (but in that case the results are undefined), or they'll do
515 * another blit of that area and the complete newer data will land the
518 intel_emit_linear_blit(brw
,
520 obj
->Mappings
[index
].Offset
+ offset
,
521 intel_obj
->range_map_bo
[index
],
522 intel_obj
->map_extra
[index
] + offset
,
524 mark_buffer_gpu_usage(intel_obj
,
525 obj
->Mappings
[index
].Offset
+ offset
,
531 * The UnmapBuffer() driver hook.
533 * Implements glUnmapBuffer().
536 brw_unmap_buffer(struct gl_context
*ctx
,
537 struct gl_buffer_object
*obj
,
538 gl_map_buffer_index index
)
540 struct brw_context
*brw
= brw_context(ctx
);
541 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
544 assert(obj
->Mappings
[index
].Pointer
);
545 if (intel_obj
->range_map_bo
[index
] != NULL
) {
546 drm_intel_bo_unmap(intel_obj
->range_map_bo
[index
]);
548 if (!(obj
->Mappings
[index
].AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
)) {
549 intel_emit_linear_blit(brw
,
550 intel_obj
->buffer
, obj
->Mappings
[index
].Offset
,
551 intel_obj
->range_map_bo
[index
],
552 intel_obj
->map_extra
[index
],
553 obj
->Mappings
[index
].Length
);
554 mark_buffer_gpu_usage(intel_obj
, obj
->Mappings
[index
].Offset
,
555 obj
->Mappings
[index
].Length
);
558 /* Since we've emitted some blits to buffers that will (likely) be used
559 * in rendering operations in other cache domains in this batch, emit a
560 * flush. Once again, we wish for a domain tracker in libdrm to cover
561 * usage inside of a batchbuffer.
563 brw_emit_mi_flush(brw
);
565 drm_intel_bo_unreference(intel_obj
->range_map_bo
[index
]);
566 intel_obj
->range_map_bo
[index
] = NULL
;
567 } else if (intel_obj
->buffer
!= NULL
) {
568 drm_intel_bo_unmap(intel_obj
->buffer
);
570 obj
->Mappings
[index
].Pointer
= NULL
;
571 obj
->Mappings
[index
].Offset
= 0;
572 obj
->Mappings
[index
].Length
= 0;
578 * Gets a pointer to the object's BO, and marks the given range as being used
581 * Anywhere that uses buffer objects in the pipeline should be using this to
582 * mark the range of the buffer that is being accessed by the pipeline.
585 intel_bufferobj_buffer(struct brw_context
*brw
,
586 struct intel_buffer_object
*intel_obj
,
587 uint32_t offset
, uint32_t size
)
589 /* This is needed so that things like transform feedback and texture buffer
590 * objects that need a BO but don't want to check that they exist for
591 * draw-time validation can just always get a BO from a GL buffer object.
593 if (intel_obj
->buffer
== NULL
)
594 alloc_buffer_object(brw
, intel_obj
);
596 mark_buffer_gpu_usage(intel_obj
, offset
, size
);
598 return intel_obj
->buffer
;
602 * The CopyBufferSubData() driver hook.
604 * Implements glCopyBufferSubData(), which copies a portion of one buffer
605 * object's data to another. Independent source and destination offsets
609 brw_copy_buffer_subdata(struct gl_context
*ctx
,
610 struct gl_buffer_object
*src
,
611 struct gl_buffer_object
*dst
,
612 GLintptr read_offset
, GLintptr write_offset
,
615 struct brw_context
*brw
= brw_context(ctx
);
616 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
617 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
618 drm_intel_bo
*src_bo
, *dst_bo
;
623 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, write_offset
, size
);
624 src_bo
= intel_bufferobj_buffer(brw
, intel_src
, read_offset
, size
);
626 intel_emit_linear_blit(brw
,
627 dst_bo
, write_offset
,
628 src_bo
, read_offset
, size
);
630 /* Since we've emitted some blits to buffers that will (likely) be used
631 * in rendering operations in other cache domains in this batch, emit a
632 * flush. Once again, we wish for a domain tracker in libdrm to cover
633 * usage inside of a batchbuffer.
635 brw_emit_mi_flush(brw
);
639 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
641 functions
->NewBufferObject
= brw_new_buffer_object
;
642 functions
->DeleteBuffer
= brw_delete_buffer
;
643 functions
->BufferData
= brw_buffer_data
;
644 functions
->BufferSubData
= brw_buffer_subdata
;
645 functions
->GetBufferSubData
= brw_get_buffer_subdata
;
646 functions
->MapBufferRange
= brw_map_buffer_range
;
647 functions
->FlushMappedBufferRange
= brw_flush_mapped_buffer_range
;
648 functions
->UnmapBuffer
= brw_unmap_buffer
;
649 functions
->CopyBufferSubData
= brw_copy_buffer_subdata
;