1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * @file intel_buffer_objects.c
31 * This provides core GL buffer object functionality.
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/macros.h"
37 #include "main/bufferobj.h"
39 #include "brw_context.h"
40 #include "intel_blit.h"
41 #include "intel_buffer_objects.h"
42 #include "intel_batchbuffer.h"
45 * Map a buffer object; issue performance warnings if mapping causes stalls.
47 * This matches the drm_intel_bo_map API, but takes an additional human-readable
48 * name for the buffer object to use in the performance debug message.
51 brw_bo_map(struct brw_context
*brw
,
52 drm_intel_bo
*bo
, int write_enable
,
55 if (likely(!brw
->perf_debug
) || !drm_intel_bo_busy(bo
))
56 return drm_intel_bo_map(bo
, write_enable
);
58 double start_time
= get_time();
60 int ret
= drm_intel_bo_map(bo
, write_enable
);
62 perf_debug("CPU mapping a busy %s BO stalled and took %.03f ms.\n",
63 bo_name
, (get_time() - start_time
) * 1000);
69 brw_bo_map_gtt(struct brw_context
*brw
, drm_intel_bo
*bo
, const char *bo_name
)
71 if (likely(!brw
->perf_debug
) || !drm_intel_bo_busy(bo
))
72 return drm_intel_gem_bo_map_gtt(bo
);
74 double start_time
= get_time();
76 int ret
= drm_intel_gem_bo_map_gtt(bo
);
78 perf_debug("GTT mapping a busy %s BO stalled and took %.03f ms.\n",
79 bo_name
, (get_time() - start_time
) * 1000);
85 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
,
86 gl_map_buffer_index index
);
89 intel_bufferobj_mark_gpu_usage(struct intel_buffer_object
*intel_obj
,
90 uint32_t offset
, uint32_t size
)
92 intel_obj
->gpu_active_start
= MIN2(intel_obj
->gpu_active_start
, offset
);
93 intel_obj
->gpu_active_end
= MAX2(intel_obj
->gpu_active_end
, offset
+ size
);
97 intel_bufferobj_mark_inactive(struct intel_buffer_object
*intel_obj
)
99 intel_obj
->gpu_active_start
= ~0;
100 intel_obj
->gpu_active_end
= 0;
103 /** Allocates a new drm_intel_bo to store the data for the buffer object. */
105 intel_bufferobj_alloc_buffer(struct brw_context
*brw
,
106 struct intel_buffer_object
*intel_obj
)
108 intel_obj
->buffer
= drm_intel_bo_alloc(brw
->bufmgr
, "bufferobj",
109 intel_obj
->Base
.Size
, 64);
111 /* the buffer might be bound as a uniform buffer, need to update it
113 brw
->state
.dirty
.brw
|= BRW_NEW_UNIFORM_BUFFER
;
115 intel_bufferobj_mark_inactive(intel_obj
);
119 release_buffer(struct intel_buffer_object
*intel_obj
)
121 drm_intel_bo_unreference(intel_obj
->buffer
);
122 intel_obj
->buffer
= NULL
;
126 * The NewBufferObject() driver hook.
128 * Allocates a new intel_buffer_object structure and initializes it.
130 * There is some duplication between mesa's bufferobjects and our
131 * bufmgr buffers. Both have an integer handle and a hashtable to
132 * lookup an opaque structure. It would be nice if the handles and
133 * internal structure where somehow shared.
135 static struct gl_buffer_object
*
136 intel_bufferobj_alloc(struct gl_context
* ctx
, GLuint name
, GLenum target
)
138 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
140 _mesa_error_no_memory(__func__
);
143 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
, target
);
151 * The DeleteBuffer() driver hook.
153 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
156 intel_bufferobj_free(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
158 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
162 /* Buffer objects are automatically unmapped when deleting according
163 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
164 * (though it does if you call glDeleteBuffers)
166 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
168 drm_intel_bo_unreference(intel_obj
->buffer
);
174 * The BufferData() driver hook.
176 * Implements glBufferData(), which recreates a buffer object's data store
177 * and populates it with the given data, if present.
179 * Any data that was previously stored in the buffer object is lost.
181 * \return true for success, false if out of memory
184 intel_bufferobj_data(struct gl_context
* ctx
,
189 GLbitfield storageFlags
,
190 struct gl_buffer_object
*obj
)
192 struct brw_context
*brw
= brw_context(ctx
);
193 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
195 /* Part of the ABI, but this function doesn't use it.
199 intel_obj
->Base
.Size
= size
;
200 intel_obj
->Base
.Usage
= usage
;
201 intel_obj
->Base
.StorageFlags
= storageFlags
;
203 assert(!obj
->Mappings
[MAP_USER
].Pointer
); /* Mesa should have unmapped it */
204 assert(!obj
->Mappings
[MAP_INTERNAL
].Pointer
);
206 if (intel_obj
->buffer
!= NULL
)
207 release_buffer(intel_obj
);
210 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
211 if (!intel_obj
->buffer
)
215 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
223 * The BufferSubData() driver hook.
225 * Implements glBufferSubData(), which replaces a portion of the data in a
228 * If the data range specified by (size + offset) extends beyond the end of
229 * the buffer or if data is NULL, no copy is performed.
232 intel_bufferobj_subdata(struct gl_context
* ctx
,
235 const GLvoid
* data
, struct gl_buffer_object
*obj
)
237 struct brw_context
*brw
= brw_context(ctx
);
238 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
246 /* See if we can unsynchronized write the data into the user's BO. This
247 * avoids GPU stalls in unfortunately common user patterns (uploading
248 * sequentially into a BO, with draw calls in between each upload).
250 * Once we've hit this path, we mark this GL BO as preferring stalling to
251 * blits, so that we can hopefully hit this path again in the future
252 * (otherwise, an app that might occasionally stall but mostly not will end
253 * up with blitting all the time, at the cost of bandwidth)
256 if (offset
+ size
<= intel_obj
->gpu_active_start
||
257 intel_obj
->gpu_active_end
<= offset
) {
258 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
259 memcpy(intel_obj
->buffer
->virtual + offset
, data
, size
);
260 drm_intel_bo_unmap(intel_obj
->buffer
);
262 if (intel_obj
->gpu_active_end
> intel_obj
->gpu_active_start
)
263 intel_obj
->prefer_stall_to_blit
= true;
269 drm_intel_bo_busy(intel_obj
->buffer
) ||
270 drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
);
273 if (size
== intel_obj
->Base
.Size
) {
274 /* Replace the current busy bo so the subdata doesn't stall. */
275 drm_intel_bo_unreference(intel_obj
->buffer
);
276 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
277 } else if (!intel_obj
->prefer_stall_to_blit
) {
278 perf_debug("Using a blit copy to avoid stalling on "
279 "glBufferSubData(%ld, %ld) (%ldkb) to a busy "
280 "(%d-%d) buffer object.\n",
281 (long)offset
, (long)offset
+ size
, (long)(size
/1024),
282 intel_obj
->gpu_active_start
,
283 intel_obj
->gpu_active_end
);
284 drm_intel_bo
*temp_bo
=
285 drm_intel_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
287 drm_intel_bo_subdata(temp_bo
, 0, size
, data
);
289 intel_emit_linear_blit(brw
,
290 intel_obj
->buffer
, offset
,
294 drm_intel_bo_unreference(temp_bo
);
297 perf_debug("Stalling on glBufferSubData(%ld, %ld) (%ldkb) to a busy "
298 "(%d-%d) buffer object. Use glMapBufferRange() to "
300 (long)offset
, (long)offset
+ size
, (long)(size
/1024),
301 intel_obj
->gpu_active_start
,
302 intel_obj
->gpu_active_end
);
303 intel_batchbuffer_flush(brw
);
307 drm_intel_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
308 intel_bufferobj_mark_inactive(intel_obj
);
313 * The GetBufferSubData() driver hook.
315 * Implements glGetBufferSubData(), which copies a subrange of a buffer
316 * object into user memory.
319 intel_bufferobj_get_subdata(struct gl_context
* ctx
,
322 GLvoid
* data
, struct gl_buffer_object
*obj
)
324 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
325 struct brw_context
*brw
= brw_context(ctx
);
328 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
329 intel_batchbuffer_flush(brw
);
331 drm_intel_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
333 intel_bufferobj_mark_inactive(intel_obj
);
338 * The MapBufferRange() driver hook.
340 * This implements both glMapBufferRange() and glMapBuffer().
342 * The goal of this extension is to allow apps to accumulate their rendering
343 * at the same time as they accumulate their buffer object. Without it,
344 * you'd end up blocking on execution of rendering every time you mapped
345 * the buffer to put new data in.
347 * We support it in 3 ways: If unsynchronized, then don't bother
348 * flushing the batchbuffer before mapping the buffer, which can save blocking
349 * in many cases. If we would still block, and they allow the whole buffer
350 * to be invalidated, then just allocate a new buffer to replace the old one.
351 * If not, and we'd block, and they allow the subrange of the buffer to be
352 * invalidated, then we can make a new little BO, let them write into that,
353 * and blit it into the real BO at unmap time.
356 intel_bufferobj_map_range(struct gl_context
* ctx
,
357 GLintptr offset
, GLsizeiptr length
,
358 GLbitfield access
, struct gl_buffer_object
*obj
,
359 gl_map_buffer_index index
)
361 struct brw_context
*brw
= brw_context(ctx
);
362 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
366 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
367 * internally uses our functions directly.
369 obj
->Mappings
[index
].Offset
= offset
;
370 obj
->Mappings
[index
].Length
= length
;
371 obj
->Mappings
[index
].AccessFlags
= access
;
373 if (intel_obj
->buffer
== NULL
) {
374 obj
->Mappings
[index
].Pointer
= NULL
;
378 /* If the access is synchronized (like a normal buffer mapping), then get
379 * things flushed out so the later mapping syncs appropriately through GEM.
380 * If the user doesn't care about existing buffer contents and mapping would
381 * cause us to block, then throw out the old buffer.
383 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
384 * achieve the required synchronization.
386 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
387 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
388 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
389 drm_intel_bo_unreference(intel_obj
->buffer
);
390 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
392 perf_debug("Stalling on the GPU for mapping a busy buffer "
394 intel_batchbuffer_flush(brw
);
396 } else if (drm_intel_bo_busy(intel_obj
->buffer
) &&
397 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
398 drm_intel_bo_unreference(intel_obj
->buffer
);
399 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
403 /* If the user is mapping a range of an active buffer object but
404 * doesn't require the current contents of that range, make a new
405 * BO, and we'll copy what they put in there out at unmap or
408 * That is, unless they're looking for a persistent mapping -- we would
409 * need to do blits in the MemoryBarrier call, and it's easier to just do a
410 * GPU stall and do a mapping.
412 if (!(access
& (GL_MAP_UNSYNCHRONIZED_BIT
| GL_MAP_PERSISTENT_BIT
)) &&
413 (access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
414 drm_intel_bo_busy(intel_obj
->buffer
)) {
415 /* Ensure that the base alignment of the allocation meets the alignment
416 * guarantees the driver has advertised to the application.
418 const unsigned alignment
= ctx
->Const
.MinMapBufferAlignment
;
420 intel_obj
->map_extra
[index
] = (uintptr_t) offset
% alignment
;
421 intel_obj
->range_map_bo
[index
] = drm_intel_bo_alloc(brw
->bufmgr
,
424 intel_obj
->map_extra
[index
],
427 drm_intel_bo_map(intel_obj
->range_map_bo
[index
],
428 (access
& GL_MAP_WRITE_BIT
) != 0);
430 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
[index
]);
432 obj
->Mappings
[index
].Pointer
=
433 intel_obj
->range_map_bo
[index
]->virtual + intel_obj
->map_extra
[index
];
434 return obj
->Mappings
[index
].Pointer
;
437 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
438 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
439 else if (!brw
->has_llc
&& (!(access
& GL_MAP_READ_BIT
) ||
440 (access
& GL_MAP_PERSISTENT_BIT
))) {
441 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
442 intel_bufferobj_mark_inactive(intel_obj
);
444 drm_intel_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
445 intel_bufferobj_mark_inactive(intel_obj
);
448 obj
->Mappings
[index
].Pointer
= intel_obj
->buffer
->virtual + offset
;
449 return obj
->Mappings
[index
].Pointer
;
453 * The FlushMappedBufferRange() driver hook.
455 * Implements glFlushMappedBufferRange(), which signifies that modifications
456 * have been made to a range of a mapped buffer, and it should be flushed.
458 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
460 * Ideally we'd use a BO to avoid taking up cache space for the temporary
461 * data, but FlushMappedBufferRange may be followed by further writes to
462 * the pointer, so we would have to re-map after emitting our blit, which
463 * would defeat the point.
466 intel_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
467 GLintptr offset
, GLsizeiptr length
,
468 struct gl_buffer_object
*obj
,
469 gl_map_buffer_index index
)
471 struct brw_context
*brw
= brw_context(ctx
);
472 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
473 GLbitfield access
= obj
->Mappings
[index
].AccessFlags
;
475 assert(access
& GL_MAP_FLUSH_EXPLICIT_BIT
);
477 /* If we gave a direct mapping of the buffer instead of using a temporary,
478 * then there's nothing to do.
480 if (intel_obj
->range_map_bo
[index
] == NULL
)
486 /* Note that we're not unmapping our buffer while executing the blit. We
487 * need to have a mapping still at the end of this call, since the user
488 * gets to make further modifications and glFlushMappedBufferRange() calls.
489 * This is safe, because:
491 * - On LLC platforms, we're using a CPU mapping that's coherent with the
492 * GPU (except for the render caches), so the kernel doesn't need to do
493 * any flushing work for us except for what happens at batch exec time
496 * - On non-LLC platforms, we're using a GTT mapping that writes directly
497 * to system memory (except for the chipset cache that gets flushed at
500 * In both cases we don't need to stall for the previous blit to complete
501 * so we can re-map (and we definitely don't want to, since that would be
502 * slow): If the user edits a part of their buffer that's previously been
503 * blitted, then our lack of synchoronization is fine, because either
504 * they'll get some too-new data in the first blit and not do another blit
505 * of that area (but in that case the results are undefined), or they'll do
506 * another blit of that area and the complete newer data will land the
509 intel_emit_linear_blit(brw
,
511 obj
->Mappings
[index
].Offset
+ offset
,
512 intel_obj
->range_map_bo
[index
],
513 intel_obj
->map_extra
[index
] + offset
,
515 intel_bufferobj_mark_gpu_usage(intel_obj
,
516 obj
->Mappings
[index
].Offset
+ offset
,
522 * The UnmapBuffer() driver hook.
524 * Implements glUnmapBuffer().
527 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
,
528 gl_map_buffer_index index
)
530 struct brw_context
*brw
= brw_context(ctx
);
531 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
534 assert(obj
->Mappings
[index
].Pointer
);
535 if (intel_obj
->range_map_bo
[index
] != NULL
) {
536 drm_intel_bo_unmap(intel_obj
->range_map_bo
[index
]);
538 if (!(obj
->Mappings
[index
].AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
)) {
539 intel_emit_linear_blit(brw
,
540 intel_obj
->buffer
, obj
->Mappings
[index
].Offset
,
541 intel_obj
->range_map_bo
[index
],
542 intel_obj
->map_extra
[index
],
543 obj
->Mappings
[index
].Length
);
544 intel_bufferobj_mark_gpu_usage(intel_obj
, obj
->Mappings
[index
].Offset
,
545 obj
->Mappings
[index
].Length
);
548 /* Since we've emitted some blits to buffers that will (likely) be used
549 * in rendering operations in other cache domains in this batch, emit a
550 * flush. Once again, we wish for a domain tracker in libdrm to cover
551 * usage inside of a batchbuffer.
553 intel_batchbuffer_emit_mi_flush(brw
);
555 drm_intel_bo_unreference(intel_obj
->range_map_bo
[index
]);
556 intel_obj
->range_map_bo
[index
] = NULL
;
557 } else if (intel_obj
->buffer
!= NULL
) {
558 drm_intel_bo_unmap(intel_obj
->buffer
);
560 obj
->Mappings
[index
].Pointer
= NULL
;
561 obj
->Mappings
[index
].Offset
= 0;
562 obj
->Mappings
[index
].Length
= 0;
568 * Gets a pointer to the object's BO, and marks the given range as being used
571 * Anywhere that uses buffer objects in the pipeline should be using this to
572 * mark the range of the buffer that is being accessed by the pipeline.
575 intel_bufferobj_buffer(struct brw_context
*brw
,
576 struct intel_buffer_object
*intel_obj
,
577 uint32_t offset
, uint32_t size
)
579 /* This is needed so that things like transform feedback and texture buffer
580 * objects that need a BO but don't want to check that they exist for
581 * draw-time validation can just always get a BO from a GL buffer object.
583 if (intel_obj
->buffer
== NULL
)
584 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
586 intel_bufferobj_mark_gpu_usage(intel_obj
, offset
, size
);
588 return intel_obj
->buffer
;
592 * The CopyBufferSubData() driver hook.
594 * Implements glCopyBufferSubData(), which copies a portion of one buffer
595 * object's data to another. Independent source and destination offsets
599 intel_bufferobj_copy_subdata(struct gl_context
*ctx
,
600 struct gl_buffer_object
*src
,
601 struct gl_buffer_object
*dst
,
602 GLintptr read_offset
, GLintptr write_offset
,
605 struct brw_context
*brw
= brw_context(ctx
);
606 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
607 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
608 drm_intel_bo
*src_bo
, *dst_bo
;
613 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, write_offset
, size
);
614 src_bo
= intel_bufferobj_buffer(brw
, intel_src
, read_offset
, size
);
616 intel_emit_linear_blit(brw
,
617 dst_bo
, write_offset
,
618 src_bo
, read_offset
, size
);
620 /* Since we've emitted some blits to buffers that will (likely) be used
621 * in rendering operations in other cache domains in this batch, emit a
622 * flush. Once again, we wish for a domain tracker in libdrm to cover
623 * usage inside of a batchbuffer.
625 intel_batchbuffer_emit_mi_flush(brw
);
629 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
631 functions
->NewBufferObject
= intel_bufferobj_alloc
;
632 functions
->DeleteBuffer
= intel_bufferobj_free
;
633 functions
->BufferData
= intel_bufferobj_data
;
634 functions
->BufferSubData
= intel_bufferobj_subdata
;
635 functions
->GetBufferSubData
= intel_bufferobj_get_subdata
;
636 functions
->MapBufferRange
= intel_bufferobj_map_range
;
637 functions
->FlushMappedBufferRange
= intel_bufferobj_flush_mapped_range
;
638 functions
->UnmapBuffer
= intel_bufferobj_unmap
;
639 functions
->CopyBufferSubData
= intel_bufferobj_copy_subdata
;