1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #include "main/imports.h"
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/bufferobj.h"
34 #include "brw_context.h"
35 #include "intel_blit.h"
36 #include "intel_buffer_objects.h"
37 #include "intel_batchbuffer.h"
38 #include "intel_fbo.h"
39 #include "intel_mipmap_tree.h"
40 #include "intel_regions.h"
42 #include "brw_context.h"
45 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
);
47 /** Allocates a new drm_intel_bo to store the data for the buffer object. */
49 intel_bufferobj_alloc_buffer(struct brw_context
*brw
,
50 struct intel_buffer_object
*intel_obj
)
52 intel_obj
->buffer
= drm_intel_bo_alloc(brw
->bufmgr
, "bufferobj",
53 intel_obj
->Base
.Size
, 64);
55 /* the buffer might be bound as a uniform buffer, need to update it
57 brw
->state
.dirty
.brw
|= BRW_NEW_UNIFORM_BUFFER
;
61 release_buffer(struct intel_buffer_object
*intel_obj
)
63 drm_intel_bo_unreference(intel_obj
->buffer
);
64 intel_obj
->buffer
= NULL
;
65 intel_obj
->offset
= 0;
69 * There is some duplication between mesa's bufferobjects and our
70 * bufmgr buffers. Both have an integer handle and a hashtable to
71 * lookup an opaque structure. It would be nice if the handles and
72 * internal structure where somehow shared.
74 static struct gl_buffer_object
*
75 intel_bufferobj_alloc(struct gl_context
* ctx
, GLuint name
, GLenum target
)
77 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
79 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
, target
);
87 * Deallocate/free a vertex/pixel buffer object.
88 * Called via glDeleteBuffersARB().
91 intel_bufferobj_free(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
93 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
97 /* Buffer objects are automatically unmapped when deleting according
98 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
99 * (though it does if you call glDeleteBuffers)
102 intel_bufferobj_unmap(ctx
, obj
);
104 drm_intel_bo_unreference(intel_obj
->buffer
);
111 * Allocate space for and store data in a buffer object. Any data that was
112 * previously stored in the buffer object is lost. If data is NULL,
113 * memory will be allocated, but no copy will occur.
114 * Called via ctx->Driver.BufferData().
115 * \return true for success, false if out of memory
118 intel_bufferobj_data(struct gl_context
* ctx
,
122 GLenum usage
, struct gl_buffer_object
*obj
)
124 struct brw_context
*brw
= brw_context(ctx
);
125 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
127 /* Part of the ABI, but this function doesn't use it.
131 intel_obj
->Base
.Size
= size
;
132 intel_obj
->Base
.Usage
= usage
;
134 assert(!obj
->Pointer
); /* Mesa should have unmapped it */
136 if (intel_obj
->buffer
!= NULL
)
137 release_buffer(intel_obj
);
140 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
141 if (!intel_obj
->buffer
)
145 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
153 * Replace data in a subrange of buffer object. If the data range
154 * specified by size + offset extends beyond the end of the buffer or
155 * if data is NULL, no copy is performed.
156 * Called via glBufferSubDataARB().
159 intel_bufferobj_subdata(struct gl_context
* ctx
,
162 const GLvoid
* data
, struct gl_buffer_object
*obj
)
164 struct brw_context
*brw
= brw_context(ctx
);
165 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
174 drm_intel_bo_busy(intel_obj
->buffer
) ||
175 drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
);
178 if (size
== intel_obj
->Base
.Size
) {
179 /* Replace the current busy bo with fresh data. */
180 drm_intel_bo_unreference(intel_obj
->buffer
);
181 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
182 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
184 perf_debug("Using a blit copy to avoid stalling on %ldb "
185 "glBufferSubData() to a busy buffer object.\n",
187 drm_intel_bo
*temp_bo
=
188 drm_intel_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
190 drm_intel_bo_subdata(temp_bo
, 0, size
, data
);
192 intel_emit_linear_blit(brw
,
193 intel_obj
->buffer
, offset
,
197 drm_intel_bo_unreference(temp_bo
);
200 drm_intel_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
206 * Called via glGetBufferSubDataARB().
209 intel_bufferobj_get_subdata(struct gl_context
* ctx
,
212 GLvoid
* data
, struct gl_buffer_object
*obj
)
214 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
215 struct brw_context
*brw
= brw_context(ctx
);
218 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
219 intel_batchbuffer_flush(brw
);
221 drm_intel_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
227 * Called via glMapBufferRange and glMapBuffer
229 * The goal of this extension is to allow apps to accumulate their rendering
230 * at the same time as they accumulate their buffer object. Without it,
231 * you'd end up blocking on execution of rendering every time you mapped
232 * the buffer to put new data in.
234 * We support it in 3 ways: If unsynchronized, then don't bother
235 * flushing the batchbuffer before mapping the buffer, which can save blocking
236 * in many cases. If we would still block, and they allow the whole buffer
237 * to be invalidated, then just allocate a new buffer to replace the old one.
238 * If not, and we'd block, and they allow the subrange of the buffer to be
239 * invalidated, then we can make a new little BO, let them write into that,
240 * and blit it into the real BO at unmap time.
243 intel_bufferobj_map_range(struct gl_context
* ctx
,
244 GLintptr offset
, GLsizeiptr length
,
245 GLbitfield access
, struct gl_buffer_object
*obj
)
247 struct brw_context
*brw
= brw_context(ctx
);
248 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
252 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
253 * internally uses our functions directly.
255 obj
->Offset
= offset
;
256 obj
->Length
= length
;
257 obj
->AccessFlags
= access
;
259 if (intel_obj
->buffer
== NULL
) {
264 /* If the access is synchronized (like a normal buffer mapping), then get
265 * things flushed out so the later mapping syncs appropriately through GEM.
266 * If the user doesn't care about existing buffer contents and mapping would
267 * cause us to block, then throw out the old buffer.
269 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
270 * achieve the required synchronization.
272 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
273 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
274 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
275 drm_intel_bo_unreference(intel_obj
->buffer
);
276 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
278 perf_debug("Stalling on the GPU for mapping a busy buffer "
282 } else if (drm_intel_bo_busy(intel_obj
->buffer
) &&
283 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
284 drm_intel_bo_unreference(intel_obj
->buffer
);
285 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
289 /* If the user is mapping a range of an active buffer object but
290 * doesn't require the current contents of that range, make a new
291 * BO, and we'll copy what they put in there out at unmap or
294 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
295 drm_intel_bo_busy(intel_obj
->buffer
)) {
296 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
) {
297 intel_obj
->range_map_buffer
= malloc(length
);
298 obj
->Pointer
= intel_obj
->range_map_buffer
;
300 intel_obj
->range_map_bo
= drm_intel_bo_alloc(brw
->bufmgr
,
303 if (!(access
& GL_MAP_READ_BIT
)) {
304 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
);
306 drm_intel_bo_map(intel_obj
->range_map_bo
,
307 (access
& GL_MAP_WRITE_BIT
) != 0);
309 obj
->Pointer
= intel_obj
->range_map_bo
->virtual;
314 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
315 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
316 else if (!(access
& GL_MAP_READ_BIT
)) {
317 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
319 drm_intel_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
322 obj
->Pointer
= intel_obj
->buffer
->virtual + offset
;
326 /* Ideally we'd use a BO to avoid taking up cache space for the temporary
327 * data, but FlushMappedBufferRange may be followed by further writes to
328 * the pointer, so we would have to re-map after emitting our blit, which
329 * would defeat the point.
332 intel_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
333 GLintptr offset
, GLsizeiptr length
,
334 struct gl_buffer_object
*obj
)
336 struct brw_context
*brw
= brw_context(ctx
);
337 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
338 drm_intel_bo
*temp_bo
;
340 /* Unless we're in the range map using a temporary system buffer,
341 * there's no work to do.
343 if (intel_obj
->range_map_buffer
== NULL
)
349 temp_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "range map flush", length
, 64);
351 drm_intel_bo_subdata(temp_bo
, 0, length
, intel_obj
->range_map_buffer
);
353 intel_emit_linear_blit(brw
,
354 intel_obj
->buffer
, obj
->Offset
+ offset
,
358 drm_intel_bo_unreference(temp_bo
);
363 * Called via glUnmapBuffer().
366 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
368 struct brw_context
*brw
= brw_context(ctx
);
369 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
372 assert(obj
->Pointer
);
373 if (intel_obj
->range_map_buffer
!= NULL
) {
374 /* Since we've emitted some blits to buffers that will (likely) be used
375 * in rendering operations in other cache domains in this batch, emit a
376 * flush. Once again, we wish for a domain tracker in libdrm to cover
377 * usage inside of a batchbuffer.
379 intel_batchbuffer_emit_mi_flush(brw
);
380 free(intel_obj
->range_map_buffer
);
381 intel_obj
->range_map_buffer
= NULL
;
382 } else if (intel_obj
->range_map_bo
!= NULL
) {
383 drm_intel_bo_unmap(intel_obj
->range_map_bo
);
385 intel_emit_linear_blit(brw
,
386 intel_obj
->buffer
, obj
->Offset
,
387 intel_obj
->range_map_bo
, 0,
390 /* Since we've emitted some blits to buffers that will (likely) be used
391 * in rendering operations in other cache domains in this batch, emit a
392 * flush. Once again, we wish for a domain tracker in libdrm to cover
393 * usage inside of a batchbuffer.
395 intel_batchbuffer_emit_mi_flush(brw
);
397 drm_intel_bo_unreference(intel_obj
->range_map_bo
);
398 intel_obj
->range_map_bo
= NULL
;
399 } else if (intel_obj
->buffer
!= NULL
) {
400 drm_intel_bo_unmap(intel_obj
->buffer
);
410 intel_bufferobj_buffer(struct brw_context
*brw
,
411 struct intel_buffer_object
*intel_obj
,
414 if (intel_obj
->buffer
== NULL
)
415 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
417 return intel_obj
->buffer
;
420 #define INTEL_UPLOAD_SIZE (64*1024)
423 intel_upload_finish(struct brw_context
*brw
)
428 if (brw
->upload
.buffer_len
) {
429 drm_intel_bo_subdata(brw
->upload
.bo
,
430 brw
->upload
.buffer_offset
,
431 brw
->upload
.buffer_len
,
433 brw
->upload
.buffer_len
= 0;
436 drm_intel_bo_unreference(brw
->upload
.bo
);
437 brw
->upload
.bo
= NULL
;
440 static void wrap_buffers(struct brw_context
*brw
, GLuint size
)
442 intel_upload_finish(brw
);
444 if (size
< INTEL_UPLOAD_SIZE
)
445 size
= INTEL_UPLOAD_SIZE
;
447 brw
->upload
.bo
= drm_intel_bo_alloc(brw
->bufmgr
, "upload", size
, 0);
448 brw
->upload
.offset
= 0;
451 void intel_upload_data(struct brw_context
*brw
,
452 const void *ptr
, GLuint size
, GLuint align
,
453 drm_intel_bo
**return_bo
,
454 GLuint
*return_offset
)
458 base
= (brw
->upload
.offset
+ align
- 1) / align
* align
;
459 if (brw
->upload
.bo
== NULL
|| base
+ size
> brw
->upload
.bo
->size
) {
460 wrap_buffers(brw
, size
);
464 drm_intel_bo_reference(brw
->upload
.bo
);
465 *return_bo
= brw
->upload
.bo
;
466 *return_offset
= base
;
468 delta
= base
- brw
->upload
.offset
;
469 if (brw
->upload
.buffer_len
&&
470 brw
->upload
.buffer_len
+ delta
+ size
> sizeof(brw
->upload
.buffer
))
472 drm_intel_bo_subdata(brw
->upload
.bo
,
473 brw
->upload
.buffer_offset
,
474 brw
->upload
.buffer_len
,
476 brw
->upload
.buffer_len
= 0;
479 if (size
< sizeof(brw
->upload
.buffer
))
481 if (brw
->upload
.buffer_len
== 0)
482 brw
->upload
.buffer_offset
= base
;
484 brw
->upload
.buffer_len
+= delta
;
486 memcpy(brw
->upload
.buffer
+ brw
->upload
.buffer_len
, ptr
, size
);
487 brw
->upload
.buffer_len
+= size
;
491 drm_intel_bo_subdata(brw
->upload
.bo
, base
, size
, ptr
);
494 brw
->upload
.offset
= base
+ size
;
497 void *intel_upload_map(struct brw_context
*brw
, GLuint size
, GLuint align
)
502 base
= (brw
->upload
.offset
+ align
- 1) / align
* align
;
503 if (brw
->upload
.bo
== NULL
|| base
+ size
> brw
->upload
.bo
->size
) {
504 wrap_buffers(brw
, size
);
508 delta
= base
- brw
->upload
.offset
;
509 if (brw
->upload
.buffer_len
&&
510 brw
->upload
.buffer_len
+ delta
+ size
> sizeof(brw
->upload
.buffer
))
512 drm_intel_bo_subdata(brw
->upload
.bo
,
513 brw
->upload
.buffer_offset
,
514 brw
->upload
.buffer_len
,
516 brw
->upload
.buffer_len
= 0;
519 if (size
<= sizeof(brw
->upload
.buffer
)) {
520 if (brw
->upload
.buffer_len
== 0)
521 brw
->upload
.buffer_offset
= base
;
523 brw
->upload
.buffer_len
+= delta
;
525 ptr
= brw
->upload
.buffer
+ brw
->upload
.buffer_len
;
526 brw
->upload
.buffer_len
+= size
;
533 void intel_upload_unmap(struct brw_context
*brw
,
534 const void *ptr
, GLuint size
, GLuint align
,
535 drm_intel_bo
**return_bo
,
536 GLuint
*return_offset
)
540 base
= (brw
->upload
.offset
+ align
- 1) / align
* align
;
541 if (size
> sizeof(brw
->upload
.buffer
)) {
542 drm_intel_bo_subdata(brw
->upload
.bo
, base
, size
, ptr
);
546 drm_intel_bo_reference(brw
->upload
.bo
);
547 *return_bo
= brw
->upload
.bo
;
548 *return_offset
= base
;
550 brw
->upload
.offset
= base
+ size
;
554 intel_bufferobj_source(struct brw_context
*brw
,
555 struct intel_buffer_object
*intel_obj
,
556 GLuint align
, GLuint
*offset
)
558 *offset
= intel_obj
->offset
;
559 return intel_obj
->buffer
;
563 intel_bufferobj_copy_subdata(struct gl_context
*ctx
,
564 struct gl_buffer_object
*src
,
565 struct gl_buffer_object
*dst
,
566 GLintptr read_offset
, GLintptr write_offset
,
569 struct brw_context
*brw
= brw_context(ctx
);
570 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
571 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
572 drm_intel_bo
*src_bo
, *dst_bo
;
578 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, INTEL_WRITE_PART
);
579 src_bo
= intel_bufferobj_source(brw
, intel_src
, 64, &src_offset
);
581 intel_emit_linear_blit(brw
,
582 dst_bo
, write_offset
,
583 src_bo
, read_offset
+ src_offset
, size
);
585 /* Since we've emitted some blits to buffers that will (likely) be used
586 * in rendering operations in other cache domains in this batch, emit a
587 * flush. Once again, we wish for a domain tracker in libdrm to cover
588 * usage inside of a batchbuffer.
590 intel_batchbuffer_emit_mi_flush(brw
);
594 intel_buffer_purgeable(drm_intel_bo
*buffer
)
599 retained
= drm_intel_bo_madvise (buffer
, I915_MADV_DONTNEED
);
601 return retained
? GL_VOLATILE_APPLE
: GL_RELEASED_APPLE
;
605 intel_buffer_object_purgeable(struct gl_context
* ctx
,
606 struct gl_buffer_object
*obj
,
609 struct intel_buffer_object
*intel_obj
= intel_buffer_object (obj
);
611 if (intel_obj
->buffer
!= NULL
)
612 return intel_buffer_purgeable(intel_obj
->buffer
);
614 if (option
== GL_RELEASED_APPLE
) {
615 return GL_RELEASED_APPLE
;
617 /* XXX Create the buffer and madvise(MADV_DONTNEED)? */
618 struct brw_context
*brw
= brw_context(ctx
);
619 drm_intel_bo
*bo
= intel_bufferobj_buffer(brw
, intel_obj
, INTEL_READ
);
621 return intel_buffer_purgeable(bo
);
626 intel_texture_object_purgeable(struct gl_context
* ctx
,
627 struct gl_texture_object
*obj
,
630 struct intel_texture_object
*intel
;
635 intel
= intel_texture_object(obj
);
636 if (intel
->mt
== NULL
|| intel
->mt
->region
== NULL
)
637 return GL_RELEASED_APPLE
;
639 return intel_buffer_purgeable(intel
->mt
->region
->bo
);
643 intel_render_object_purgeable(struct gl_context
* ctx
,
644 struct gl_renderbuffer
*obj
,
647 struct intel_renderbuffer
*intel
;
652 intel
= intel_renderbuffer(obj
);
653 if (intel
->mt
== NULL
)
654 return GL_RELEASED_APPLE
;
656 return intel_buffer_purgeable(intel
->mt
->region
->bo
);
660 intel_buffer_unpurgeable(drm_intel_bo
*buffer
)
666 retained
= drm_intel_bo_madvise (buffer
, I915_MADV_WILLNEED
);
668 return retained
? GL_RETAINED_APPLE
: GL_UNDEFINED_APPLE
;
672 intel_buffer_object_unpurgeable(struct gl_context
* ctx
,
673 struct gl_buffer_object
*obj
,
679 return intel_buffer_unpurgeable(intel_buffer_object (obj
)->buffer
);
683 intel_texture_object_unpurgeable(struct gl_context
* ctx
,
684 struct gl_texture_object
*obj
,
687 struct intel_texture_object
*intel
;
692 intel
= intel_texture_object(obj
);
693 if (intel
->mt
== NULL
|| intel
->mt
->region
== NULL
)
694 return GL_UNDEFINED_APPLE
;
696 return intel_buffer_unpurgeable(intel
->mt
->region
->bo
);
700 intel_render_object_unpurgeable(struct gl_context
* ctx
,
701 struct gl_renderbuffer
*obj
,
704 struct intel_renderbuffer
*intel
;
709 intel
= intel_renderbuffer(obj
);
710 if (intel
->mt
== NULL
)
711 return GL_UNDEFINED_APPLE
;
713 return intel_buffer_unpurgeable(intel
->mt
->region
->bo
);
717 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
719 functions
->NewBufferObject
= intel_bufferobj_alloc
;
720 functions
->DeleteBuffer
= intel_bufferobj_free
;
721 functions
->BufferData
= intel_bufferobj_data
;
722 functions
->BufferSubData
= intel_bufferobj_subdata
;
723 functions
->GetBufferSubData
= intel_bufferobj_get_subdata
;
724 functions
->MapBufferRange
= intel_bufferobj_map_range
;
725 functions
->FlushMappedBufferRange
= intel_bufferobj_flush_mapped_range
;
726 functions
->UnmapBuffer
= intel_bufferobj_unmap
;
727 functions
->CopyBufferSubData
= intel_bufferobj_copy_subdata
;
729 functions
->BufferObjectPurgeable
= intel_buffer_object_purgeable
;
730 functions
->TextureObjectPurgeable
= intel_texture_object_purgeable
;
731 functions
->RenderObjectPurgeable
= intel_render_object_purgeable
;
733 functions
->BufferObjectUnpurgeable
= intel_buffer_object_unpurgeable
;
734 functions
->TextureObjectUnpurgeable
= intel_texture_object_unpurgeable
;
735 functions
->RenderObjectUnpurgeable
= intel_render_object_unpurgeable
;