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 * @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 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 * The NewBufferObject() driver hook.
71 * Allocates a new intel_buffer_object structure and initializes it.
73 * There is some duplication between mesa's bufferobjects and our
74 * bufmgr buffers. Both have an integer handle and a hashtable to
75 * lookup an opaque structure. It would be nice if the handles and
76 * internal structure where somehow shared.
78 static struct gl_buffer_object
*
79 intel_bufferobj_alloc(struct gl_context
* ctx
, GLuint name
, GLenum target
)
81 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
83 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
, target
);
91 * The DeleteBuffer() driver hook.
93 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
96 intel_bufferobj_free(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
98 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
102 /* Buffer objects are automatically unmapped when deleting according
103 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
104 * (though it does if you call glDeleteBuffers)
107 intel_bufferobj_unmap(ctx
, obj
);
109 drm_intel_bo_unreference(intel_obj
->buffer
);
115 * The BufferData() driver hook.
117 * Implements glBufferData(), which recreates a buffer object's data store
118 * and populates it with the given data, if present.
120 * Any data that was previously stored in the buffer object is lost.
122 * \return true for success, false if out of memory
125 intel_bufferobj_data(struct gl_context
* ctx
,
129 GLenum usage
, struct gl_buffer_object
*obj
)
131 struct brw_context
*brw
= brw_context(ctx
);
132 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
134 /* Part of the ABI, but this function doesn't use it.
138 intel_obj
->Base
.Size
= size
;
139 intel_obj
->Base
.Usage
= usage
;
141 assert(!obj
->Pointer
); /* Mesa should have unmapped it */
143 if (intel_obj
->buffer
!= NULL
)
144 release_buffer(intel_obj
);
147 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
148 if (!intel_obj
->buffer
)
152 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
160 * The BufferSubData() driver hook.
162 * Implements glBufferSubData(), which replaces a portion of the data in a
165 * If the data range specified by (size + offset) extends beyond the end of
166 * the buffer or if data is NULL, no copy is performed.
169 intel_bufferobj_subdata(struct gl_context
* ctx
,
172 const GLvoid
* data
, struct gl_buffer_object
*obj
)
174 struct brw_context
*brw
= brw_context(ctx
);
175 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
184 drm_intel_bo_busy(intel_obj
->buffer
) ||
185 drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
);
188 if (size
== intel_obj
->Base
.Size
) {
189 /* Replace the current busy bo with fresh data. */
190 drm_intel_bo_unreference(intel_obj
->buffer
);
191 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
192 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
194 perf_debug("Using a blit copy to avoid stalling on %ldb "
195 "glBufferSubData() to a busy buffer object.\n",
197 drm_intel_bo
*temp_bo
=
198 drm_intel_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
200 drm_intel_bo_subdata(temp_bo
, 0, size
, data
);
202 intel_emit_linear_blit(brw
,
203 intel_obj
->buffer
, offset
,
207 drm_intel_bo_unreference(temp_bo
);
210 drm_intel_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
216 * The GetBufferSubData() driver hook.
218 * Implements glGetBufferSubData(), which copies a subrange of a buffer
219 * object into user memory.
222 intel_bufferobj_get_subdata(struct gl_context
* ctx
,
225 GLvoid
* data
, struct gl_buffer_object
*obj
)
227 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
228 struct brw_context
*brw
= brw_context(ctx
);
231 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
232 intel_batchbuffer_flush(brw
);
234 drm_intel_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
239 * The MapBufferRange() driver hook.
241 * This implements both glMapBufferRange() and glMapBuffer().
243 * The goal of this extension is to allow apps to accumulate their rendering
244 * at the same time as they accumulate their buffer object. Without it,
245 * you'd end up blocking on execution of rendering every time you mapped
246 * the buffer to put new data in.
248 * We support it in 3 ways: If unsynchronized, then don't bother
249 * flushing the batchbuffer before mapping the buffer, which can save blocking
250 * in many cases. If we would still block, and they allow the whole buffer
251 * to be invalidated, then just allocate a new buffer to replace the old one.
252 * If not, and we'd block, and they allow the subrange of the buffer to be
253 * invalidated, then we can make a new little BO, let them write into that,
254 * and blit it into the real BO at unmap time.
257 intel_bufferobj_map_range(struct gl_context
* ctx
,
258 GLintptr offset
, GLsizeiptr length
,
259 GLbitfield access
, struct gl_buffer_object
*obj
)
261 struct brw_context
*brw
= brw_context(ctx
);
262 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
266 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
267 * internally uses our functions directly.
269 obj
->Offset
= offset
;
270 obj
->Length
= length
;
271 obj
->AccessFlags
= access
;
273 if (intel_obj
->buffer
== NULL
) {
278 /* If the access is synchronized (like a normal buffer mapping), then get
279 * things flushed out so the later mapping syncs appropriately through GEM.
280 * If the user doesn't care about existing buffer contents and mapping would
281 * cause us to block, then throw out the old buffer.
283 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
284 * achieve the required synchronization.
286 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
287 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
288 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
289 drm_intel_bo_unreference(intel_obj
->buffer
);
290 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
292 perf_debug("Stalling on the GPU for mapping a busy buffer "
296 } else if (drm_intel_bo_busy(intel_obj
->buffer
) &&
297 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
298 drm_intel_bo_unreference(intel_obj
->buffer
);
299 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
303 /* If the user is mapping a range of an active buffer object but
304 * doesn't require the current contents of that range, make a new
305 * BO, and we'll copy what they put in there out at unmap or
308 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
309 drm_intel_bo_busy(intel_obj
->buffer
)) {
310 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
) {
311 intel_obj
->range_map_buffer
= malloc(length
);
312 obj
->Pointer
= intel_obj
->range_map_buffer
;
314 intel_obj
->range_map_bo
= drm_intel_bo_alloc(brw
->bufmgr
,
317 if (!(access
& GL_MAP_READ_BIT
)) {
318 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
);
320 drm_intel_bo_map(intel_obj
->range_map_bo
,
321 (access
& GL_MAP_WRITE_BIT
) != 0);
323 obj
->Pointer
= intel_obj
->range_map_bo
->virtual;
328 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
329 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
330 else if (!(access
& GL_MAP_READ_BIT
)) {
331 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
333 drm_intel_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
336 obj
->Pointer
= intel_obj
->buffer
->virtual + offset
;
341 * The FlushMappedBufferRange() driver hook.
343 * Implements glFlushMappedBufferRange(), which signifies that modifications
344 * have been made to a range of a mapped buffer, and it should be flushed.
346 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
348 * Ideally we'd use a BO to avoid taking up cache space for the temporary
349 * data, but FlushMappedBufferRange may be followed by further writes to
350 * the pointer, so we would have to re-map after emitting our blit, which
351 * would defeat the point.
354 intel_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
355 GLintptr offset
, GLsizeiptr length
,
356 struct gl_buffer_object
*obj
)
358 struct brw_context
*brw
= brw_context(ctx
);
359 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
360 drm_intel_bo
*temp_bo
;
362 /* Unless we're in the range map using a temporary system buffer,
363 * there's no work to do.
365 if (intel_obj
->range_map_buffer
== NULL
)
371 temp_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "range map flush", length
, 64);
373 drm_intel_bo_subdata(temp_bo
, 0, length
, intel_obj
->range_map_buffer
);
375 intel_emit_linear_blit(brw
,
376 intel_obj
->buffer
, obj
->Offset
+ offset
,
380 drm_intel_bo_unreference(temp_bo
);
385 * The UnmapBuffer() driver hook.
387 * Implements glUnmapBuffer().
390 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
392 struct brw_context
*brw
= brw_context(ctx
);
393 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
396 assert(obj
->Pointer
);
397 if (intel_obj
->range_map_buffer
!= NULL
) {
398 /* Since we've emitted some blits to buffers that will (likely) be used
399 * in rendering operations in other cache domains in this batch, emit a
400 * flush. Once again, we wish for a domain tracker in libdrm to cover
401 * usage inside of a batchbuffer.
403 intel_batchbuffer_emit_mi_flush(brw
);
404 free(intel_obj
->range_map_buffer
);
405 intel_obj
->range_map_buffer
= NULL
;
406 } else if (intel_obj
->range_map_bo
!= NULL
) {
407 drm_intel_bo_unmap(intel_obj
->range_map_bo
);
409 intel_emit_linear_blit(brw
,
410 intel_obj
->buffer
, obj
->Offset
,
411 intel_obj
->range_map_bo
, 0,
414 /* Since we've emitted some blits to buffers that will (likely) be used
415 * in rendering operations in other cache domains in this batch, emit a
416 * flush. Once again, we wish for a domain tracker in libdrm to cover
417 * usage inside of a batchbuffer.
419 intel_batchbuffer_emit_mi_flush(brw
);
421 drm_intel_bo_unreference(intel_obj
->range_map_bo
);
422 intel_obj
->range_map_bo
= NULL
;
423 } else if (intel_obj
->buffer
!= NULL
) {
424 drm_intel_bo_unmap(intel_obj
->buffer
);
434 intel_bufferobj_buffer(struct brw_context
*brw
,
435 struct intel_buffer_object
*intel_obj
,
438 if (intel_obj
->buffer
== NULL
)
439 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
441 return intel_obj
->buffer
;
445 intel_bufferobj_source(struct brw_context
*brw
,
446 struct intel_buffer_object
*intel_obj
,
447 GLuint align
, GLuint
*offset
)
449 *offset
= intel_obj
->offset
;
450 return intel_obj
->buffer
;
454 * The CopyBufferSubData() driver hook.
456 * Implements glCopyBufferSubData(), which copies a portion of one buffer
457 * object's data to another. Independent source and destination offsets
461 intel_bufferobj_copy_subdata(struct gl_context
*ctx
,
462 struct gl_buffer_object
*src
,
463 struct gl_buffer_object
*dst
,
464 GLintptr read_offset
, GLintptr write_offset
,
467 struct brw_context
*brw
= brw_context(ctx
);
468 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
469 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
470 drm_intel_bo
*src_bo
, *dst_bo
;
476 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, INTEL_WRITE_PART
);
477 src_bo
= intel_bufferobj_source(brw
, intel_src
, 64, &src_offset
);
479 intel_emit_linear_blit(brw
,
480 dst_bo
, write_offset
,
481 src_bo
, read_offset
+ src_offset
, size
);
483 /* Since we've emitted some blits to buffers that will (likely) be used
484 * in rendering operations in other cache domains in this batch, emit a
485 * flush. Once again, we wish for a domain tracker in libdrm to cover
486 * usage inside of a batchbuffer.
488 intel_batchbuffer_emit_mi_flush(brw
);
492 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
494 functions
->NewBufferObject
= intel_bufferobj_alloc
;
495 functions
->DeleteBuffer
= intel_bufferobj_free
;
496 functions
->BufferData
= intel_bufferobj_data
;
497 functions
->BufferSubData
= intel_bufferobj_subdata
;
498 functions
->GetBufferSubData
= intel_bufferobj_get_subdata
;
499 functions
->MapBufferRange
= intel_bufferobj_map_range
;
500 functions
->FlushMappedBufferRange
= intel_bufferobj_flush_mapped_range
;
501 functions
->UnmapBuffer
= intel_bufferobj_unmap
;
502 functions
->CopyBufferSubData
= intel_bufferobj_copy_subdata
;