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 "intel_context.h"
35 #include "intel_blit.h"
36 #include "intel_buffer_objects.h"
37 #include "intel_batchbuffer.h"
38 #include "intel_regions.h"
41 intel_bufferobj_unmap(GLcontext
* ctx
,
42 GLenum target
, struct gl_buffer_object
*obj
);
44 /** Allocates a new dri_bo to store the data for the buffer object. */
46 intel_bufferobj_alloc_buffer(struct intel_context
*intel
,
47 struct intel_buffer_object
*intel_obj
)
49 intel_obj
->buffer
= dri_bo_alloc(intel
->bufmgr
, "bufferobj",
50 intel_obj
->Base
.Size
, 64);
54 * There is some duplication between mesa's bufferobjects and our
55 * bufmgr buffers. Both have an integer handle and a hashtable to
56 * lookup an opaque structure. It would be nice if the handles and
57 * internal structure where somehow shared.
59 static struct gl_buffer_object
*
60 intel_bufferobj_alloc(GLcontext
* ctx
, GLuint name
, GLenum target
)
62 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
64 _mesa_initialize_buffer_object(&obj
->Base
, name
, target
);
71 /* Break the COW tie to the region. The region gets to keep the data.
74 intel_bufferobj_release_region(struct intel_context
*intel
,
75 struct intel_buffer_object
*intel_obj
)
77 assert(intel_obj
->region
->buffer
== intel_obj
->buffer
);
78 intel_obj
->region
->pbo
= NULL
;
79 intel_obj
->region
= NULL
;
81 dri_bo_unreference(intel_obj
->buffer
);
82 intel_obj
->buffer
= NULL
;
85 /* Break the COW tie to the region. Both the pbo and the region end
86 * up with a copy of the data.
89 intel_bufferobj_cow(struct intel_context
*intel
,
90 struct intel_buffer_object
*intel_obj
)
92 assert(intel_obj
->region
);
93 intel_region_cow(intel
, intel_obj
->region
);
98 * Deallocate/free a vertex/pixel buffer object.
99 * Called via glDeleteBuffersARB().
102 intel_bufferobj_free(GLcontext
* ctx
, struct gl_buffer_object
*obj
)
104 struct intel_context
*intel
= intel_context(ctx
);
105 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
109 /* Buffer objects are automatically unmapped when deleting according
110 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
111 * (though it does if you call glDeleteBuffers)
114 intel_bufferobj_unmap(ctx
, 0, obj
);
116 _mesa_free(intel_obj
->sys_buffer
);
117 if (intel_obj
->region
) {
118 intel_bufferobj_release_region(intel
, intel_obj
);
120 else if (intel_obj
->buffer
) {
121 dri_bo_unreference(intel_obj
->buffer
);
124 _mesa_free(intel_obj
);
130 * Allocate space for and store data in a buffer object. Any data that was
131 * previously stored in the buffer object is lost. If data is NULL,
132 * memory will be allocated, but no copy will occur.
133 * Called via ctx->Driver.BufferData().
134 * \return GL_TRUE for success, GL_FALSE if out of memory
137 intel_bufferobj_data(GLcontext
* ctx
,
141 GLenum usage
, struct gl_buffer_object
*obj
)
143 struct intel_context
*intel
= intel_context(ctx
);
144 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
146 intel_obj
->Base
.Size
= size
;
147 intel_obj
->Base
.Usage
= usage
;
149 assert(!obj
->Pointer
); /* Mesa should have unmapped it */
151 if (intel_obj
->region
)
152 intel_bufferobj_release_region(intel
, intel_obj
);
154 if (intel_obj
->buffer
!= NULL
) {
155 dri_bo_unreference(intel_obj
->buffer
);
156 intel_obj
->buffer
= NULL
;
158 _mesa_free(intel_obj
->sys_buffer
);
159 intel_obj
->sys_buffer
= NULL
;
163 /* On pre-965, stick VBOs in system memory, as we're always doing swtnl
164 * with their contents anyway.
166 if (target
== GL_ARRAY_BUFFER
|| target
== GL_ELEMENT_ARRAY_BUFFER
) {
167 intel_obj
->sys_buffer
= _mesa_malloc(size
);
168 if (intel_obj
->sys_buffer
!= NULL
) {
170 memcpy(intel_obj
->sys_buffer
, data
, size
);
175 intel_bufferobj_alloc_buffer(intel
, intel_obj
);
176 if (!intel_obj
->buffer
)
180 dri_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
188 * Replace data in a subrange of buffer object. If the data range
189 * specified by size + offset extends beyond the end of the buffer or
190 * if data is NULL, no copy is performed.
191 * Called via glBufferSubDataARB().
194 intel_bufferobj_subdata(GLcontext
* ctx
,
198 const GLvoid
* data
, struct gl_buffer_object
*obj
)
200 struct intel_context
*intel
= intel_context(ctx
);
201 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
205 if (intel_obj
->region
)
206 intel_bufferobj_cow(intel
, intel_obj
);
208 if (intel_obj
->sys_buffer
)
209 memcpy((char *)intel_obj
->sys_buffer
+ offset
, data
, size
);
211 dri_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
216 * Called via glGetBufferSubDataARB().
219 intel_bufferobj_get_subdata(GLcontext
* ctx
,
223 GLvoid
* data
, struct gl_buffer_object
*obj
)
225 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
228 dri_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
234 * Called via glMapBufferARB().
237 intel_bufferobj_map(GLcontext
* ctx
,
239 GLenum access
, struct gl_buffer_object
*obj
)
241 struct intel_context
*intel
= intel_context(ctx
);
242 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
243 GLboolean read_only
= (access
== GL_READ_ONLY_ARB
);
244 GLboolean write_only
= (access
== GL_WRITE_ONLY_ARB
);
248 if (intel_obj
->sys_buffer
) {
249 obj
->Pointer
= intel_obj
->sys_buffer
;
253 /* Flush any existing batchbuffer that might have written to this
258 if (intel_obj
->region
)
259 intel_bufferobj_cow(intel
, intel_obj
);
261 if (intel_obj
->buffer
== NULL
) {
266 if (write_only
&& intel
->intelScreen
->kernel_exec_fencing
) {
267 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
268 intel_obj
->mapped_gtt
= GL_TRUE
;
270 drm_intel_bo_map(intel_obj
->buffer
, !read_only
);
271 intel_obj
->mapped_gtt
= GL_FALSE
;
274 obj
->Pointer
= intel_obj
->buffer
->virtual;
275 obj
->Length
= obj
->Size
;
282 * Called via glMapBufferRange().
284 * The goal of this extension is to allow apps to accumulate their rendering
285 * at the same time as they accumulate their buffer object. Without it,
286 * you'd end up blocking on execution of rendering every time you mapped
287 * the buffer to put new data in.
289 * We support it in 3 ways: If unsynchronized, then don't bother
290 * flushing the batchbuffer before mapping the buffer, which can save blocking
291 * in many cases. If we would still block, and they allow the whole buffer
292 * to be invalidated, then just allocate a new buffer to replace the old one.
293 * If not, and we'd block, and they allow the subrange of the buffer to be
294 * invalidated, then we can make a new little BO, let them write into that,
295 * and blit it into the real BO at unmap time.
298 intel_bufferobj_map_range(GLcontext
* ctx
,
299 GLenum target
, GLintptr offset
, GLsizeiptr length
,
300 GLbitfield access
, struct gl_buffer_object
*obj
)
302 struct intel_context
*intel
= intel_context(ctx
);
303 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
307 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
308 * internally uses our functions directly.
310 obj
->Offset
= offset
;
311 obj
->Length
= length
;
312 obj
->AccessFlags
= access
;
314 if (intel_obj
->sys_buffer
) {
315 obj
->Pointer
= intel_obj
->sys_buffer
+ offset
;
319 if (intel_obj
->region
)
320 intel_bufferobj_cow(intel
, intel_obj
);
322 /* If the mapping is synchronized with other GL operations, flush
323 * the batchbuffer so that GEM knows about the buffer access for later
326 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
))
329 if (intel_obj
->buffer
== NULL
) {
334 /* If the user doesn't care about existing buffer contents and mapping
335 * would cause us to block, then throw out the old buffer.
337 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
) &&
338 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) &&
339 drm_intel_bo_busy(intel_obj
->buffer
)) {
340 drm_intel_bo_unreference(intel_obj
->buffer
);
341 intel_obj
->buffer
= dri_bo_alloc(intel
->bufmgr
, "bufferobj",
342 intel_obj
->Base
.Size
, 64);
345 /* If the user is mapping a range of an active buffer object but
346 * doesn't require the current contents of that range, make a new
347 * BO, and we'll copy what they put in there out at unmap or
350 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
351 drm_intel_bo_busy(intel_obj
->buffer
)) {
352 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
) {
353 intel_obj
->range_map_buffer
= _mesa_malloc(length
);
354 obj
->Pointer
= intel_obj
->range_map_buffer
;
356 intel_obj
->range_map_bo
= drm_intel_bo_alloc(intel
->bufmgr
,
359 if (!(access
& GL_MAP_READ_BIT
) &&
360 intel
->intelScreen
->kernel_exec_fencing
) {
361 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
);
362 intel_obj
->mapped_gtt
= GL_TRUE
;
364 drm_intel_bo_map(intel_obj
->range_map_bo
,
365 (access
& GL_MAP_WRITE_BIT
) != 0);
366 intel_obj
->mapped_gtt
= GL_FALSE
;
368 obj
->Pointer
= intel_obj
->range_map_bo
->virtual;
373 if (!(access
& GL_MAP_READ_BIT
) &&
374 intel
->intelScreen
->kernel_exec_fencing
) {
375 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
376 intel_obj
->mapped_gtt
= GL_TRUE
;
378 drm_intel_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
379 intel_obj
->mapped_gtt
= GL_FALSE
;
382 obj
->Pointer
= intel_obj
->buffer
->virtual + offset
;
386 /* Ideally we'd use a BO to avoid taking up cache space for the temporary
387 * data, but FlushMappedBufferRange may be followed by further writes to
388 * the pointer, so we would have to re-map after emitting our blit, which
389 * would defeat the point.
392 intel_bufferobj_flush_mapped_range(GLcontext
*ctx
, GLenum target
,
393 GLintptr offset
, GLsizeiptr length
,
394 struct gl_buffer_object
*obj
)
396 struct intel_context
*intel
= intel_context(ctx
);
397 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
398 drm_intel_bo
*temp_bo
;
400 /* Unless we're in the range map using a temporary system buffer,
401 * there's no work to do.
403 if (intel_obj
->range_map_buffer
== NULL
)
406 temp_bo
= drm_intel_bo_alloc(intel
->bufmgr
, "range map flush", length
, 64);
408 drm_intel_bo_subdata(temp_bo
, 0, length
, intel_obj
->range_map_buffer
);
410 intel_emit_linear_blit(intel
,
411 intel_obj
->buffer
, obj
->Offset
+ offset
,
415 drm_intel_bo_unreference(temp_bo
);
420 * Called via glUnmapBuffer().
423 intel_bufferobj_unmap(GLcontext
* ctx
,
424 GLenum target
, struct gl_buffer_object
*obj
)
426 struct intel_context
*intel
= intel_context(ctx
);
427 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
430 assert(obj
->Pointer
);
431 if (intel_obj
->sys_buffer
!= NULL
) {
432 /* always keep the mapping around. */
433 } else if (intel_obj
->range_map_buffer
!= NULL
) {
434 /* Since we've emitted some blits to buffers that will (likely) be used
435 * in rendering operations in other cache domains in this batch, emit a
436 * flush. Once again, we wish for a domain tracker in libdrm to cover
437 * usage inside of a batchbuffer.
439 intel_batchbuffer_emit_mi_flush(intel
->batch
);
440 free(intel_obj
->range_map_buffer
);
441 intel_obj
->range_map_buffer
= NULL
;
442 } else if (intel_obj
->range_map_bo
!= NULL
) {
443 if (intel_obj
->mapped_gtt
) {
444 drm_intel_gem_bo_unmap_gtt(intel_obj
->range_map_bo
);
446 drm_intel_bo_unmap(intel_obj
->range_map_bo
);
449 intel_emit_linear_blit(intel
,
450 intel_obj
->buffer
, obj
->Offset
,
451 intel_obj
->range_map_bo
, 0,
454 /* Since we've emitted some blits to buffers that will (likely) be used
455 * in rendering operations in other cache domains in this batch, emit a
456 * flush. Once again, we wish for a domain tracker in libdrm to cover
457 * usage inside of a batchbuffer.
459 intel_batchbuffer_emit_mi_flush(intel
->batch
);
461 drm_intel_bo_unreference(intel_obj
->range_map_bo
);
462 intel_obj
->range_map_bo
= NULL
;
463 } else if (intel_obj
->buffer
!= NULL
) {
464 if (intel_obj
->mapped_gtt
) {
465 drm_intel_gem_bo_unmap_gtt(intel_obj
->buffer
);
467 drm_intel_bo_unmap(intel_obj
->buffer
);
478 intel_bufferobj_buffer(struct intel_context
*intel
,
479 struct intel_buffer_object
*intel_obj
, GLuint flag
)
481 if (intel_obj
->region
) {
482 if (flag
== INTEL_WRITE_PART
)
483 intel_bufferobj_cow(intel
, intel_obj
);
484 else if (flag
== INTEL_WRITE_FULL
) {
485 intel_bufferobj_release_region(intel
, intel_obj
);
486 intel_bufferobj_alloc_buffer(intel
, intel_obj
);
490 if (intel_obj
->buffer
== NULL
) {
491 void *sys_buffer
= intel_obj
->sys_buffer
;
493 /* only one of buffer and sys_buffer could be non-NULL */
494 intel_bufferobj_alloc_buffer(intel
, intel_obj
);
495 intel_obj
->sys_buffer
= NULL
;
497 intel_bufferobj_subdata(&intel
->ctx
,
500 intel_obj
->Base
.Size
,
503 _mesa_free(sys_buffer
);
504 intel_obj
->sys_buffer
= NULL
;
507 return intel_obj
->buffer
;
511 intel_bufferobj_copy_subdata(GLcontext
*ctx
,
512 struct gl_buffer_object
*src
,
513 struct gl_buffer_object
*dst
,
514 GLintptr read_offset
, GLintptr write_offset
,
517 struct intel_context
*intel
= intel_context(ctx
);
518 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
519 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
520 drm_intel_bo
*src_bo
, *dst_bo
;
525 /* If we're in system memory, just map and memcpy. */
526 if (intel_src
->sys_buffer
|| intel_dst
->sys_buffer
) {
527 /* The same buffer may be used, but note that regions copied may
531 char *ptr
= intel_bufferobj_map(ctx
, GL_COPY_WRITE_BUFFER
,
533 memcpy(ptr
+ write_offset
, ptr
+ read_offset
, size
);
534 intel_bufferobj_unmap(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
539 src_ptr
= intel_bufferobj_map(ctx
, GL_COPY_READ_BUFFER
,
541 dst_ptr
= intel_bufferobj_map(ctx
, GL_COPY_WRITE_BUFFER
,
544 memcpy(dst_ptr
+ write_offset
, src_ptr
+ read_offset
, size
);
546 intel_bufferobj_unmap(ctx
, GL_COPY_READ_BUFFER
, src
);
547 intel_bufferobj_unmap(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
551 /* Otherwise, we have real BOs, so blit them. */
553 dst_bo
= intel_bufferobj_buffer(intel
, intel_dst
, INTEL_WRITE_PART
);
554 src_bo
= intel_bufferobj_buffer(intel
, intel_src
, INTEL_READ
);
556 intel_emit_linear_blit(intel
,
557 dst_bo
, write_offset
,
558 src_bo
, read_offset
, size
);
560 /* Since we've emitted some blits to buffers that will (likely) be used
561 * in rendering operations in other cache domains in this batch, emit a
562 * flush. Once again, we wish for a domain tracker in libdrm to cover
563 * usage inside of a batchbuffer.
565 intel_batchbuffer_emit_mi_flush(intel
->batch
);
569 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
571 functions
->NewBufferObject
= intel_bufferobj_alloc
;
572 functions
->DeleteBuffer
= intel_bufferobj_free
;
573 functions
->BufferData
= intel_bufferobj_data
;
574 functions
->BufferSubData
= intel_bufferobj_subdata
;
575 functions
->GetBufferSubData
= intel_bufferobj_get_subdata
;
576 functions
->MapBuffer
= intel_bufferobj_map
;
577 functions
->MapBufferRange
= intel_bufferobj_map_range
;
578 functions
->FlushMappedBufferRange
= intel_bufferobj_flush_mapped_range
;
579 functions
->UnmapBuffer
= intel_bufferobj_unmap
;
580 functions
->CopyBufferSubData
= intel_bufferobj_copy_subdata
;