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_initialize_buffer_object(ctx
, &obj
->Base
, name
, target
);
148 * The DeleteBuffer() driver hook.
150 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
153 intel_bufferobj_free(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
155 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
159 /* Buffer objects are automatically unmapped when deleting according
160 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
161 * (though it does if you call glDeleteBuffers)
163 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
165 drm_intel_bo_unreference(intel_obj
->buffer
);
171 * The BufferData() driver hook.
173 * Implements glBufferData(), which recreates a buffer object's data store
174 * and populates it with the given data, if present.
176 * Any data that was previously stored in the buffer object is lost.
178 * \return true for success, false if out of memory
181 intel_bufferobj_data(struct gl_context
* ctx
,
186 GLbitfield storageFlags
,
187 struct gl_buffer_object
*obj
)
189 struct brw_context
*brw
= brw_context(ctx
);
190 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
192 /* Part of the ABI, but this function doesn't use it.
196 intel_obj
->Base
.Size
= size
;
197 intel_obj
->Base
.Usage
= usage
;
198 intel_obj
->Base
.StorageFlags
= storageFlags
;
200 assert(!obj
->Mappings
[MAP_USER
].Pointer
); /* Mesa should have unmapped it */
201 assert(!obj
->Mappings
[MAP_INTERNAL
].Pointer
);
203 if (intel_obj
->buffer
!= NULL
)
204 release_buffer(intel_obj
);
207 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
208 if (!intel_obj
->buffer
)
212 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
220 * The BufferSubData() driver hook.
222 * Implements glBufferSubData(), which replaces a portion of the data in a
225 * If the data range specified by (size + offset) extends beyond the end of
226 * the buffer or if data is NULL, no copy is performed.
229 intel_bufferobj_subdata(struct gl_context
* ctx
,
232 const GLvoid
* data
, struct gl_buffer_object
*obj
)
234 struct brw_context
*brw
= brw_context(ctx
);
235 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
243 /* See if we can unsynchronized write the data into the user's BO. This
244 * avoids GPU stalls in unfortunately common user patterns (uploading
245 * sequentially into a BO, with draw calls in between each upload).
247 * Once we've hit this path, we mark this GL BO as preferring stalling to
248 * blits, so that we can hopefully hit this path again in the future
249 * (otherwise, an app that might occasionally stall but mostly not will end
250 * up with blitting all the time, at the cost of bandwidth)
253 if (offset
+ size
<= intel_obj
->gpu_active_start
||
254 intel_obj
->gpu_active_end
<= offset
) {
255 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
256 memcpy(intel_obj
->buffer
->virtual + offset
, data
, size
);
257 drm_intel_bo_unmap(intel_obj
->buffer
);
259 if (intel_obj
->gpu_active_end
> intel_obj
->gpu_active_start
)
260 intel_obj
->prefer_stall_to_blit
= true;
266 drm_intel_bo_busy(intel_obj
->buffer
) ||
267 drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
);
270 if (size
== intel_obj
->Base
.Size
) {
271 /* Replace the current busy bo so the subdata doesn't stall. */
272 drm_intel_bo_unreference(intel_obj
->buffer
);
273 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
274 } else if (!intel_obj
->prefer_stall_to_blit
) {
275 perf_debug("Using a blit copy to avoid stalling on "
276 "glBufferSubData(%ld, %ld) (%ldkb) to a busy "
277 "(%d-%d) buffer object.\n",
278 (long)offset
, (long)offset
+ size
, (long)(size
/1024),
279 intel_obj
->gpu_active_start
,
280 intel_obj
->gpu_active_end
);
281 drm_intel_bo
*temp_bo
=
282 drm_intel_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
284 drm_intel_bo_subdata(temp_bo
, 0, size
, data
);
286 intel_emit_linear_blit(brw
,
287 intel_obj
->buffer
, offset
,
291 drm_intel_bo_unreference(temp_bo
);
294 perf_debug("Stalling on glBufferSubData(%ld, %ld) (%ldkb) to a busy "
295 "(%d-%d) buffer object. Use glMapBufferRange() to "
297 (long)offset
, (long)offset
+ size
, (long)(size
/1024),
298 intel_obj
->gpu_active_start
,
299 intel_obj
->gpu_active_end
);
300 intel_batchbuffer_flush(brw
);
304 drm_intel_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
305 intel_bufferobj_mark_inactive(intel_obj
);
310 * The GetBufferSubData() driver hook.
312 * Implements glGetBufferSubData(), which copies a subrange of a buffer
313 * object into user memory.
316 intel_bufferobj_get_subdata(struct gl_context
* ctx
,
319 GLvoid
* data
, struct gl_buffer_object
*obj
)
321 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
322 struct brw_context
*brw
= brw_context(ctx
);
325 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
326 intel_batchbuffer_flush(brw
);
328 drm_intel_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
330 intel_bufferobj_mark_inactive(intel_obj
);
335 * The MapBufferRange() driver hook.
337 * This implements both glMapBufferRange() and glMapBuffer().
339 * The goal of this extension is to allow apps to accumulate their rendering
340 * at the same time as they accumulate their buffer object. Without it,
341 * you'd end up blocking on execution of rendering every time you mapped
342 * the buffer to put new data in.
344 * We support it in 3 ways: If unsynchronized, then don't bother
345 * flushing the batchbuffer before mapping the buffer, which can save blocking
346 * in many cases. If we would still block, and they allow the whole buffer
347 * to be invalidated, then just allocate a new buffer to replace the old one.
348 * If not, and we'd block, and they allow the subrange of the buffer to be
349 * invalidated, then we can make a new little BO, let them write into that,
350 * and blit it into the real BO at unmap time.
353 intel_bufferobj_map_range(struct gl_context
* ctx
,
354 GLintptr offset
, GLsizeiptr length
,
355 GLbitfield access
, struct gl_buffer_object
*obj
,
356 gl_map_buffer_index index
)
358 struct brw_context
*brw
= brw_context(ctx
);
359 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
363 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
364 * internally uses our functions directly.
366 obj
->Mappings
[index
].Offset
= offset
;
367 obj
->Mappings
[index
].Length
= length
;
368 obj
->Mappings
[index
].AccessFlags
= access
;
370 if (intel_obj
->buffer
== NULL
) {
371 obj
->Mappings
[index
].Pointer
= NULL
;
375 /* If the access is synchronized (like a normal buffer mapping), then get
376 * things flushed out so the later mapping syncs appropriately through GEM.
377 * If the user doesn't care about existing buffer contents and mapping would
378 * cause us to block, then throw out the old buffer.
380 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
381 * achieve the required synchronization.
383 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
384 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
385 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
386 drm_intel_bo_unreference(intel_obj
->buffer
);
387 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
389 perf_debug("Stalling on the GPU for mapping a busy buffer "
391 intel_batchbuffer_flush(brw
);
393 } else if (drm_intel_bo_busy(intel_obj
->buffer
) &&
394 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
395 drm_intel_bo_unreference(intel_obj
->buffer
);
396 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
400 /* If the user is mapping a range of an active buffer object but
401 * doesn't require the current contents of that range, make a new
402 * BO, and we'll copy what they put in there out at unmap or
405 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
) &&
406 (access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
407 drm_intel_bo_busy(intel_obj
->buffer
)) {
408 /* Ensure that the base alignment of the allocation meets the alignment
409 * guarantees the driver has advertised to the application.
411 const unsigned alignment
= ctx
->Const
.MinMapBufferAlignment
;
412 const unsigned extra
= (uintptr_t) offset
% alignment
;
414 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
) {
415 intel_obj
->range_map_buffer
[index
] = _mesa_align_malloc(length
+ extra
,
417 obj
->Mappings
[index
].Pointer
=
418 intel_obj
->range_map_buffer
[index
] + extra
;
420 intel_obj
->range_map_bo
[index
] = drm_intel_bo_alloc(brw
->bufmgr
,
424 if (!(access
& GL_MAP_READ_BIT
)) {
425 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
[index
]);
427 drm_intel_bo_map(intel_obj
->range_map_bo
[index
],
428 (access
& GL_MAP_WRITE_BIT
) != 0);
430 obj
->Mappings
[index
].Pointer
=
431 intel_obj
->range_map_bo
[index
]->virtual + extra
;
433 return obj
->Mappings
[index
].Pointer
;
436 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
437 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
438 else if (!(access
& GL_MAP_READ_BIT
)) {
439 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
440 intel_bufferobj_mark_inactive(intel_obj
);
442 drm_intel_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
443 intel_bufferobj_mark_inactive(intel_obj
);
446 obj
->Mappings
[index
].Pointer
= intel_obj
->buffer
->virtual + offset
;
447 return obj
->Mappings
[index
].Pointer
;
451 * The FlushMappedBufferRange() driver hook.
453 * Implements glFlushMappedBufferRange(), which signifies that modifications
454 * have been made to a range of a mapped buffer, and it should be flushed.
456 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
458 * Ideally we'd use a BO to avoid taking up cache space for the temporary
459 * data, but FlushMappedBufferRange may be followed by further writes to
460 * the pointer, so we would have to re-map after emitting our blit, which
461 * would defeat the point.
464 intel_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
465 GLintptr offset
, GLsizeiptr length
,
466 struct gl_buffer_object
*obj
,
467 gl_map_buffer_index index
)
469 struct brw_context
*brw
= brw_context(ctx
);
470 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
471 drm_intel_bo
*temp_bo
;
473 /* Unless we're in the range map using a temporary system buffer,
474 * there's no work to do.
476 if (intel_obj
->range_map_buffer
[index
] == NULL
)
482 temp_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "range map flush", length
, 64);
484 /* Use obj->Pointer instead of intel_obj->range_map_buffer because the
485 * former points to the actual mapping while the latter may be offset to
486 * meet alignment guarantees.
488 drm_intel_bo_subdata(temp_bo
, 0, length
, obj
->Mappings
[index
].Pointer
);
490 intel_emit_linear_blit(brw
,
492 obj
->Mappings
[index
].Offset
+ offset
,
495 intel_bufferobj_mark_gpu_usage(intel_obj
,
496 obj
->Mappings
[index
].Offset
+ offset
,
499 drm_intel_bo_unreference(temp_bo
);
504 * The UnmapBuffer() driver hook.
506 * Implements glUnmapBuffer().
509 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
,
510 gl_map_buffer_index index
)
512 struct brw_context
*brw
= brw_context(ctx
);
513 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
516 assert(obj
->Mappings
[index
].Pointer
);
517 if (intel_obj
->range_map_buffer
[index
] != NULL
) {
518 /* Since we've emitted some blits to buffers that will (likely) be used
519 * in rendering operations in other cache domains in this batch, emit a
520 * flush. Once again, we wish for a domain tracker in libdrm to cover
521 * usage inside of a batchbuffer.
523 intel_batchbuffer_emit_mi_flush(brw
);
524 _mesa_align_free(intel_obj
->range_map_buffer
[index
]);
525 intel_obj
->range_map_buffer
[index
] = NULL
;
526 } else if (intel_obj
->range_map_bo
[index
] != NULL
) {
527 const unsigned extra
= obj
->Mappings
[index
].Pointer
-
528 intel_obj
->range_map_bo
[index
]->virtual;
530 drm_intel_bo_unmap(intel_obj
->range_map_bo
[index
]);
532 intel_emit_linear_blit(brw
,
533 intel_obj
->buffer
, obj
->Mappings
[index
].Offset
,
534 intel_obj
->range_map_bo
[index
], extra
,
535 obj
->Mappings
[index
].Length
);
536 intel_bufferobj_mark_gpu_usage(intel_obj
, obj
->Mappings
[index
].Offset
,
537 obj
->Mappings
[index
].Length
);
539 /* Since we've emitted some blits to buffers that will (likely) be used
540 * in rendering operations in other cache domains in this batch, emit a
541 * flush. Once again, we wish for a domain tracker in libdrm to cover
542 * usage inside of a batchbuffer.
544 intel_batchbuffer_emit_mi_flush(brw
);
546 drm_intel_bo_unreference(intel_obj
->range_map_bo
[index
]);
547 intel_obj
->range_map_bo
[index
] = NULL
;
548 } else if (intel_obj
->buffer
!= NULL
) {
549 drm_intel_bo_unmap(intel_obj
->buffer
);
551 obj
->Mappings
[index
].Pointer
= NULL
;
552 obj
->Mappings
[index
].Offset
= 0;
553 obj
->Mappings
[index
].Length
= 0;
559 * Gets a pointer to the object's BO, and marks the given range as being used
562 * Anywhere that uses buffer objects in the pipeline should be using this to
563 * mark the range of the buffer that is being accessed by the pipeline.
566 intel_bufferobj_buffer(struct brw_context
*brw
,
567 struct intel_buffer_object
*intel_obj
,
568 uint32_t offset
, uint32_t size
)
570 /* This is needed so that things like transform feedback and texture buffer
571 * objects that need a BO but don't want to check that they exist for
572 * draw-time validation can just always get a BO from a GL buffer object.
574 if (intel_obj
->buffer
== NULL
)
575 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
577 intel_bufferobj_mark_gpu_usage(intel_obj
, offset
, size
);
579 return intel_obj
->buffer
;
583 * The CopyBufferSubData() driver hook.
585 * Implements glCopyBufferSubData(), which copies a portion of one buffer
586 * object's data to another. Independent source and destination offsets
590 intel_bufferobj_copy_subdata(struct gl_context
*ctx
,
591 struct gl_buffer_object
*src
,
592 struct gl_buffer_object
*dst
,
593 GLintptr read_offset
, GLintptr write_offset
,
596 struct brw_context
*brw
= brw_context(ctx
);
597 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
598 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
599 drm_intel_bo
*src_bo
, *dst_bo
;
604 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, write_offset
, size
);
605 src_bo
= intel_bufferobj_buffer(brw
, intel_src
, read_offset
, size
);
607 intel_emit_linear_blit(brw
,
608 dst_bo
, write_offset
,
609 src_bo
, read_offset
, size
);
611 /* Since we've emitted some blits to buffers that will (likely) be used
612 * in rendering operations in other cache domains in this batch, emit a
613 * flush. Once again, we wish for a domain tracker in libdrm to cover
614 * usage inside of a batchbuffer.
616 intel_batchbuffer_emit_mi_flush(brw
);
620 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
622 functions
->NewBufferObject
= intel_bufferobj_alloc
;
623 functions
->DeleteBuffer
= intel_bufferobj_free
;
624 functions
->BufferData
= intel_bufferobj_data
;
625 functions
->BufferSubData
= intel_bufferobj_subdata
;
626 functions
->GetBufferSubData
= intel_bufferobj_get_subdata
;
627 functions
->MapBufferRange
= intel_bufferobj_map_range
;
628 functions
->FlushMappedBufferRange
= intel_bufferobj_flush_mapped_range
;
629 functions
->UnmapBuffer
= intel_bufferobj_unmap
;
630 functions
->CopyBufferSubData
= intel_bufferobj_copy_subdata
;