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 mark_buffer_gpu_usage(struct intel_buffer_object
*intel_obj
,
44 uint32_t offset
, uint32_t size
)
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
);
51 mark_buffer_inactive(struct intel_buffer_object
*intel_obj
)
53 intel_obj
->gpu_active_start
= ~0;
54 intel_obj
->gpu_active_end
= 0;
57 /** Allocates a new drm_bacon_bo to store the data for the buffer object. */
59 alloc_buffer_object(struct brw_context
*brw
,
60 struct intel_buffer_object
*intel_obj
)
62 intel_obj
->buffer
= drm_bacon_bo_alloc(brw
->bufmgr
, "bufferobj",
63 intel_obj
->Base
.Size
, 64);
65 /* the buffer might be bound as a uniform buffer, need to update it
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
;
76 mark_buffer_inactive(intel_obj
);
80 release_buffer(struct intel_buffer_object
*intel_obj
)
82 drm_bacon_bo_unreference(intel_obj
->buffer
);
83 intel_obj
->buffer
= NULL
;
87 * The NewBufferObject() driver hook.
89 * Allocates a new intel_buffer_object structure and initializes it.
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.
96 static struct gl_buffer_object
*
97 brw_new_buffer_object(struct gl_context
* ctx
, GLuint name
)
99 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
101 _mesa_error_no_memory(__func__
);
104 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
);
112 * The DeleteBuffer() driver hook.
114 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
117 brw_delete_buffer(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
119 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
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)
127 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
129 drm_bacon_bo_unreference(intel_obj
->buffer
);
130 _mesa_delete_buffer_object(ctx
, obj
);
135 * The BufferData() driver hook.
137 * Implements glBufferData(), which recreates a buffer object's data store
138 * and populates it with the given data, if present.
140 * Any data that was previously stored in the buffer object is lost.
142 * \return true for success, false if out of memory
145 brw_buffer_data(struct gl_context
*ctx
,
150 GLbitfield storageFlags
,
151 struct gl_buffer_object
*obj
)
153 struct brw_context
*brw
= brw_context(ctx
);
154 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
156 /* Part of the ABI, but this function doesn't use it.
160 intel_obj
->Base
.Size
= size
;
161 intel_obj
->Base
.Usage
= usage
;
162 intel_obj
->Base
.StorageFlags
= storageFlags
;
164 assert(!obj
->Mappings
[MAP_USER
].Pointer
); /* Mesa should have unmapped it */
165 assert(!obj
->Mappings
[MAP_INTERNAL
].Pointer
);
167 if (intel_obj
->buffer
!= NULL
)
168 release_buffer(intel_obj
);
171 alloc_buffer_object(brw
, intel_obj
);
172 if (!intel_obj
->buffer
)
176 drm_bacon_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
184 * The BufferSubData() driver hook.
186 * Implements glBufferSubData(), which replaces a portion of the data in a
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.
193 brw_buffer_subdata(struct gl_context
*ctx
,
197 struct gl_buffer_object
*obj
)
199 struct brw_context
*brw
= brw_context(ctx
);
200 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
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).
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)
217 if (offset
+ size
<= intel_obj
->gpu_active_start
||
218 intel_obj
->gpu_active_end
<= offset
) {
220 drm_bacon_gem_bo_map_unsynchronized(intel_obj
->buffer
);
221 memcpy(intel_obj
->buffer
->virtual + offset
, data
, size
);
222 drm_bacon_bo_unmap(intel_obj
->buffer
);
224 if (intel_obj
->gpu_active_end
> intel_obj
->gpu_active_start
)
225 intel_obj
->prefer_stall_to_blit
= true;
228 perf_debug("BufferSubData could be unsynchronized, but !LLC doesn't support it yet\n");
233 drm_bacon_bo_busy(intel_obj
->buffer
) ||
234 brw_batch_references(&brw
->batch
, intel_obj
->buffer
);
237 if (size
== intel_obj
->Base
.Size
) {
238 /* Replace the current busy bo so the subdata doesn't stall. */
239 drm_bacon_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 drm_bacon_bo
*temp_bo
=
249 drm_bacon_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
251 drm_bacon_bo_subdata(temp_bo
, 0, size
, data
);
253 intel_emit_linear_blit(brw
,
254 intel_obj
->buffer
, offset
,
258 drm_bacon_bo_unreference(temp_bo
);
261 perf_debug("Stalling on glBufferSubData(%ld, %ld) (%ldkb) to a busy "
262 "(%d-%d) buffer object. Use glMapBufferRange() to "
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
);
271 drm_bacon_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
272 mark_buffer_inactive(intel_obj
);
277 * The GetBufferSubData() driver hook.
279 * Implements glGetBufferSubData(), which copies a subrange of a buffer
280 * object into user memory.
283 brw_get_buffer_subdata(struct gl_context
*ctx
,
287 struct gl_buffer_object
*obj
)
289 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
290 struct brw_context
*brw
= brw_context(ctx
);
293 if (brw_batch_references(&brw
->batch
, intel_obj
->buffer
)) {
294 intel_batchbuffer_flush(brw
);
296 drm_bacon_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
298 mark_buffer_inactive(intel_obj
);
303 * The MapBufferRange() driver hook.
305 * This implements both glMapBufferRange() and glMapBuffer().
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.
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.
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
)
326 struct brw_context
*brw
= brw_context(ctx
);
327 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
331 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
332 * internally uses our functions directly.
334 obj
->Mappings
[index
].Offset
= offset
;
335 obj
->Mappings
[index
].Length
= length
;
336 obj
->Mappings
[index
].AccessFlags
= access
;
338 if (intel_obj
->buffer
== NULL
) {
339 obj
->Mappings
[index
].Pointer
= NULL
;
343 /* If the access is synchronized (like a normal buffer mapping), then get
344 * things flushed out so the later mapping syncs appropriately through GEM.
345 * If the user doesn't care about existing buffer contents and mapping would
346 * cause us to block, then throw out the old buffer.
348 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
349 * achieve the required synchronization.
351 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
352 if (brw_batch_references(&brw
->batch
, intel_obj
->buffer
)) {
353 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
354 drm_bacon_bo_unreference(intel_obj
->buffer
);
355 alloc_buffer_object(brw
, intel_obj
);
357 perf_debug("Stalling on the GPU for mapping a busy buffer "
359 intel_batchbuffer_flush(brw
);
361 } else if (drm_bacon_bo_busy(intel_obj
->buffer
) &&
362 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
363 drm_bacon_bo_unreference(intel_obj
->buffer
);
364 alloc_buffer_object(brw
, intel_obj
);
368 /* If the user is mapping a range of an active buffer object but
369 * doesn't require the current contents of that range, make a new
370 * BO, and we'll copy what they put in there out at unmap or
373 * That is, unless they're looking for a persistent mapping -- we would
374 * need to do blits in the MemoryBarrier call, and it's easier to just do a
375 * GPU stall and do a mapping.
377 if (!(access
& (GL_MAP_UNSYNCHRONIZED_BIT
| GL_MAP_PERSISTENT_BIT
)) &&
378 (access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
379 drm_bacon_bo_busy(intel_obj
->buffer
)) {
380 /* Ensure that the base alignment of the allocation meets the alignment
381 * guarantees the driver has advertised to the application.
383 const unsigned alignment
= ctx
->Const
.MinMapBufferAlignment
;
385 intel_obj
->map_extra
[index
] = (uintptr_t) offset
% alignment
;
386 intel_obj
->range_map_bo
[index
] = drm_bacon_bo_alloc(brw
->bufmgr
,
389 intel_obj
->map_extra
[index
],
392 drm_bacon_bo_map(intel_obj
->range_map_bo
[index
],
393 (access
& GL_MAP_WRITE_BIT
) != 0);
395 drm_bacon_gem_bo_map_gtt(intel_obj
->range_map_bo
[index
]);
397 obj
->Mappings
[index
].Pointer
=
398 intel_obj
->range_map_bo
[index
]->virtual + intel_obj
->map_extra
[index
];
399 return obj
->Mappings
[index
].Pointer
;
402 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
) {
403 if (!brw
->has_llc
&& brw
->perf_debug
&&
404 drm_bacon_bo_busy(intel_obj
->buffer
)) {
405 perf_debug("MapBufferRange with GL_MAP_UNSYNCHRONIZED_BIT stalling (it's actually synchronized on non-LLC platforms)\n");
407 drm_bacon_gem_bo_map_unsynchronized(intel_obj
->buffer
);
408 } else if (!brw
->has_llc
&& (!(access
& GL_MAP_READ_BIT
) ||
409 (access
& GL_MAP_PERSISTENT_BIT
))) {
410 drm_bacon_gem_bo_map_gtt(intel_obj
->buffer
);
411 mark_buffer_inactive(intel_obj
);
413 drm_bacon_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
414 mark_buffer_inactive(intel_obj
);
417 obj
->Mappings
[index
].Pointer
= intel_obj
->buffer
->virtual + offset
;
418 return obj
->Mappings
[index
].Pointer
;
422 * The FlushMappedBufferRange() driver hook.
424 * Implements glFlushMappedBufferRange(), which signifies that modifications
425 * have been made to a range of a mapped buffer, and it should be flushed.
427 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
429 * Ideally we'd use a BO to avoid taking up cache space for the temporary
430 * data, but FlushMappedBufferRange may be followed by further writes to
431 * the pointer, so we would have to re-map after emitting our blit, which
432 * would defeat the point.
435 brw_flush_mapped_buffer_range(struct gl_context
*ctx
,
436 GLintptr offset
, GLsizeiptr length
,
437 struct gl_buffer_object
*obj
,
438 gl_map_buffer_index index
)
440 struct brw_context
*brw
= brw_context(ctx
);
441 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
443 assert(obj
->Mappings
[index
].AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
);
445 /* If we gave a direct mapping of the buffer instead of using a temporary,
446 * then there's nothing to do.
448 if (intel_obj
->range_map_bo
[index
] == NULL
)
454 /* Note that we're not unmapping our buffer while executing the blit. We
455 * need to have a mapping still at the end of this call, since the user
456 * gets to make further modifications and glFlushMappedBufferRange() calls.
457 * This is safe, because:
459 * - On LLC platforms, we're using a CPU mapping that's coherent with the
460 * GPU (except for the render caches), so the kernel doesn't need to do
461 * any flushing work for us except for what happens at batch exec time
464 * - On non-LLC platforms, we're using a GTT mapping that writes directly
465 * to system memory (except for the chipset cache that gets flushed at
468 * In both cases we don't need to stall for the previous blit to complete
469 * so we can re-map (and we definitely don't want to, since that would be
470 * slow): If the user edits a part of their buffer that's previously been
471 * blitted, then our lack of synchoronization is fine, because either
472 * they'll get some too-new data in the first blit and not do another blit
473 * of that area (but in that case the results are undefined), or they'll do
474 * another blit of that area and the complete newer data will land the
477 intel_emit_linear_blit(brw
,
479 obj
->Mappings
[index
].Offset
+ offset
,
480 intel_obj
->range_map_bo
[index
],
481 intel_obj
->map_extra
[index
] + offset
,
483 mark_buffer_gpu_usage(intel_obj
,
484 obj
->Mappings
[index
].Offset
+ offset
,
490 * The UnmapBuffer() driver hook.
492 * Implements glUnmapBuffer().
495 brw_unmap_buffer(struct gl_context
*ctx
,
496 struct gl_buffer_object
*obj
,
497 gl_map_buffer_index index
)
499 struct brw_context
*brw
= brw_context(ctx
);
500 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
503 assert(obj
->Mappings
[index
].Pointer
);
504 if (intel_obj
->range_map_bo
[index
] != NULL
) {
505 drm_bacon_bo_unmap(intel_obj
->range_map_bo
[index
]);
507 if (!(obj
->Mappings
[index
].AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
)) {
508 intel_emit_linear_blit(brw
,
509 intel_obj
->buffer
, obj
->Mappings
[index
].Offset
,
510 intel_obj
->range_map_bo
[index
],
511 intel_obj
->map_extra
[index
],
512 obj
->Mappings
[index
].Length
);
513 mark_buffer_gpu_usage(intel_obj
, obj
->Mappings
[index
].Offset
,
514 obj
->Mappings
[index
].Length
);
517 /* Since we've emitted some blits to buffers that will (likely) be used
518 * in rendering operations in other cache domains in this batch, emit a
519 * flush. Once again, we wish for a domain tracker in libdrm to cover
520 * usage inside of a batchbuffer.
522 brw_emit_mi_flush(brw
);
524 drm_bacon_bo_unreference(intel_obj
->range_map_bo
[index
]);
525 intel_obj
->range_map_bo
[index
] = NULL
;
526 } else if (intel_obj
->buffer
!= NULL
) {
527 drm_bacon_bo_unmap(intel_obj
->buffer
);
529 obj
->Mappings
[index
].Pointer
= NULL
;
530 obj
->Mappings
[index
].Offset
= 0;
531 obj
->Mappings
[index
].Length
= 0;
537 * Gets a pointer to the object's BO, and marks the given range as being used
540 * Anywhere that uses buffer objects in the pipeline should be using this to
541 * mark the range of the buffer that is being accessed by the pipeline.
544 intel_bufferobj_buffer(struct brw_context
*brw
,
545 struct intel_buffer_object
*intel_obj
,
546 uint32_t offset
, uint32_t size
)
548 /* This is needed so that things like transform feedback and texture buffer
549 * objects that need a BO but don't want to check that they exist for
550 * draw-time validation can just always get a BO from a GL buffer object.
552 if (intel_obj
->buffer
== NULL
)
553 alloc_buffer_object(brw
, intel_obj
);
555 mark_buffer_gpu_usage(intel_obj
, offset
, size
);
557 return intel_obj
->buffer
;
561 * The CopyBufferSubData() driver hook.
563 * Implements glCopyBufferSubData(), which copies a portion of one buffer
564 * object's data to another. Independent source and destination offsets
568 brw_copy_buffer_subdata(struct gl_context
*ctx
,
569 struct gl_buffer_object
*src
,
570 struct gl_buffer_object
*dst
,
571 GLintptr read_offset
, GLintptr write_offset
,
574 struct brw_context
*brw
= brw_context(ctx
);
575 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
576 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
577 drm_bacon_bo
*src_bo
, *dst_bo
;
582 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, write_offset
, size
);
583 src_bo
= intel_bufferobj_buffer(brw
, intel_src
, read_offset
, size
);
585 intel_emit_linear_blit(brw
,
586 dst_bo
, write_offset
,
587 src_bo
, read_offset
, size
);
589 /* Since we've emitted some blits to buffers that will (likely) be used
590 * in rendering operations in other cache domains in this batch, emit a
591 * flush. Once again, we wish for a domain tracker in libdrm to cover
592 * usage inside of a batchbuffer.
594 brw_emit_mi_flush(brw
);
598 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
600 functions
->NewBufferObject
= brw_new_buffer_object
;
601 functions
->DeleteBuffer
= brw_delete_buffer
;
602 functions
->BufferData
= brw_buffer_data
;
603 functions
->BufferSubData
= brw_buffer_subdata
;
604 functions
->GetBufferSubData
= brw_get_buffer_subdata
;
605 functions
->MapBufferRange
= brw_map_buffer_range
;
606 functions
->FlushMappedBufferRange
= brw_flush_mapped_buffer_range
;
607 functions
->UnmapBuffer
= brw_unmap_buffer
;
608 functions
->CopyBufferSubData
= brw_copy_buffer_subdata
;