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