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"
40 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
);
42 /** Allocates a new drm_intel_bo to store the data for the buffer object. */
44 intel_bufferobj_alloc_buffer(struct brw_context
*brw
,
45 struct intel_buffer_object
*intel_obj
)
47 intel_obj
->buffer
= drm_intel_bo_alloc(brw
->bufmgr
, "bufferobj",
48 intel_obj
->Base
.Size
, 64);
50 /* the buffer might be bound as a uniform buffer, need to update it
52 brw
->state
.dirty
.brw
|= BRW_NEW_UNIFORM_BUFFER
;
56 release_buffer(struct intel_buffer_object
*intel_obj
)
58 drm_intel_bo_unreference(intel_obj
->buffer
);
59 intel_obj
->buffer
= NULL
;
60 intel_obj
->offset
= 0;
64 * There is some duplication between mesa's bufferobjects and our
65 * bufmgr buffers. Both have an integer handle and a hashtable to
66 * lookup an opaque structure. It would be nice if the handles and
67 * internal structure where somehow shared.
69 static struct gl_buffer_object
*
70 intel_bufferobj_alloc(struct gl_context
* ctx
, GLuint name
, GLenum target
)
72 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
74 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
, target
);
82 * Deallocate/free a vertex/pixel buffer object.
83 * Called via glDeleteBuffersARB().
86 intel_bufferobj_free(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
88 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
92 /* Buffer objects are automatically unmapped when deleting according
93 * to the spec, but Mesa doesn't do UnmapBuffer for us at context destroy
94 * (though it does if you call glDeleteBuffers)
97 intel_bufferobj_unmap(ctx
, obj
);
99 drm_intel_bo_unreference(intel_obj
->buffer
);
106 * Allocate space for and store data in a buffer object. Any data that was
107 * previously stored in the buffer object is lost. If data is NULL,
108 * memory will be allocated, but no copy will occur.
109 * Called via ctx->Driver.BufferData().
110 * \return true for success, false if out of memory
113 intel_bufferobj_data(struct gl_context
* ctx
,
117 GLenum usage
, struct gl_buffer_object
*obj
)
119 struct brw_context
*brw
= brw_context(ctx
);
120 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
122 /* Part of the ABI, but this function doesn't use it.
126 intel_obj
->Base
.Size
= size
;
127 intel_obj
->Base
.Usage
= usage
;
129 assert(!obj
->Pointer
); /* Mesa should have unmapped it */
131 if (intel_obj
->buffer
!= NULL
)
132 release_buffer(intel_obj
);
135 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
136 if (!intel_obj
->buffer
)
140 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
148 * Replace data in a subrange of buffer object. If the data range
149 * specified by size + offset extends beyond the end of the buffer or
150 * if data is NULL, no copy is performed.
151 * Called via glBufferSubDataARB().
154 intel_bufferobj_subdata(struct gl_context
* ctx
,
157 const GLvoid
* data
, struct gl_buffer_object
*obj
)
159 struct brw_context
*brw
= brw_context(ctx
);
160 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
169 drm_intel_bo_busy(intel_obj
->buffer
) ||
170 drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
);
173 if (size
== intel_obj
->Base
.Size
) {
174 /* Replace the current busy bo with fresh data. */
175 drm_intel_bo_unreference(intel_obj
->buffer
);
176 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
177 drm_intel_bo_subdata(intel_obj
->buffer
, 0, size
, data
);
179 perf_debug("Using a blit copy to avoid stalling on %ldb "
180 "glBufferSubData() to a busy buffer object.\n",
182 drm_intel_bo
*temp_bo
=
183 drm_intel_bo_alloc(brw
->bufmgr
, "subdata temp", size
, 64);
185 drm_intel_bo_subdata(temp_bo
, 0, size
, data
);
187 intel_emit_linear_blit(brw
,
188 intel_obj
->buffer
, offset
,
192 drm_intel_bo_unreference(temp_bo
);
195 drm_intel_bo_subdata(intel_obj
->buffer
, offset
, size
, data
);
201 * Called via glGetBufferSubDataARB().
204 intel_bufferobj_get_subdata(struct gl_context
* ctx
,
207 GLvoid
* data
, struct gl_buffer_object
*obj
)
209 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
210 struct brw_context
*brw
= brw_context(ctx
);
213 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
214 intel_batchbuffer_flush(brw
);
216 drm_intel_bo_get_subdata(intel_obj
->buffer
, offset
, size
, data
);
222 * Called via glMapBufferRange and glMapBuffer
224 * The goal of this extension is to allow apps to accumulate their rendering
225 * at the same time as they accumulate their buffer object. Without it,
226 * you'd end up blocking on execution of rendering every time you mapped
227 * the buffer to put new data in.
229 * We support it in 3 ways: If unsynchronized, then don't bother
230 * flushing the batchbuffer before mapping the buffer, which can save blocking
231 * in many cases. If we would still block, and they allow the whole buffer
232 * to be invalidated, then just allocate a new buffer to replace the old one.
233 * If not, and we'd block, and they allow the subrange of the buffer to be
234 * invalidated, then we can make a new little BO, let them write into that,
235 * and blit it into the real BO at unmap time.
238 intel_bufferobj_map_range(struct gl_context
* ctx
,
239 GLintptr offset
, GLsizeiptr length
,
240 GLbitfield access
, struct gl_buffer_object
*obj
)
242 struct brw_context
*brw
= brw_context(ctx
);
243 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
247 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
248 * internally uses our functions directly.
250 obj
->Offset
= offset
;
251 obj
->Length
= length
;
252 obj
->AccessFlags
= access
;
254 if (intel_obj
->buffer
== NULL
) {
259 /* If the access is synchronized (like a normal buffer mapping), then get
260 * things flushed out so the later mapping syncs appropriately through GEM.
261 * If the user doesn't care about existing buffer contents and mapping would
262 * cause us to block, then throw out the old buffer.
264 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
265 * achieve the required synchronization.
267 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
268 if (drm_intel_bo_references(brw
->batch
.bo
, intel_obj
->buffer
)) {
269 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
270 drm_intel_bo_unreference(intel_obj
->buffer
);
271 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
273 perf_debug("Stalling on the GPU for mapping a busy buffer "
277 } else if (drm_intel_bo_busy(intel_obj
->buffer
) &&
278 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)) {
279 drm_intel_bo_unreference(intel_obj
->buffer
);
280 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
284 /* If the user is mapping a range of an active buffer object but
285 * doesn't require the current contents of that range, make a new
286 * BO, and we'll copy what they put in there out at unmap or
289 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) &&
290 drm_intel_bo_busy(intel_obj
->buffer
)) {
291 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
) {
292 intel_obj
->range_map_buffer
= malloc(length
);
293 obj
->Pointer
= intel_obj
->range_map_buffer
;
295 intel_obj
->range_map_bo
= drm_intel_bo_alloc(brw
->bufmgr
,
298 if (!(access
& GL_MAP_READ_BIT
)) {
299 drm_intel_gem_bo_map_gtt(intel_obj
->range_map_bo
);
301 drm_intel_bo_map(intel_obj
->range_map_bo
,
302 (access
& GL_MAP_WRITE_BIT
) != 0);
304 obj
->Pointer
= intel_obj
->range_map_bo
->virtual;
309 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
310 drm_intel_gem_bo_map_unsynchronized(intel_obj
->buffer
);
311 else if (!(access
& GL_MAP_READ_BIT
)) {
312 drm_intel_gem_bo_map_gtt(intel_obj
->buffer
);
314 drm_intel_bo_map(intel_obj
->buffer
, (access
& GL_MAP_WRITE_BIT
) != 0);
317 obj
->Pointer
= intel_obj
->buffer
->virtual + offset
;
321 /* Ideally we'd use a BO to avoid taking up cache space for the temporary
322 * data, but FlushMappedBufferRange may be followed by further writes to
323 * the pointer, so we would have to re-map after emitting our blit, which
324 * would defeat the point.
327 intel_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
328 GLintptr offset
, GLsizeiptr length
,
329 struct gl_buffer_object
*obj
)
331 struct brw_context
*brw
= brw_context(ctx
);
332 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
333 drm_intel_bo
*temp_bo
;
335 /* Unless we're in the range map using a temporary system buffer,
336 * there's no work to do.
338 if (intel_obj
->range_map_buffer
== NULL
)
344 temp_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "range map flush", length
, 64);
346 drm_intel_bo_subdata(temp_bo
, 0, length
, intel_obj
->range_map_buffer
);
348 intel_emit_linear_blit(brw
,
349 intel_obj
->buffer
, obj
->Offset
+ offset
,
353 drm_intel_bo_unreference(temp_bo
);
358 * Called via glUnmapBuffer().
361 intel_bufferobj_unmap(struct gl_context
* ctx
, struct gl_buffer_object
*obj
)
363 struct brw_context
*brw
= brw_context(ctx
);
364 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
367 assert(obj
->Pointer
);
368 if (intel_obj
->range_map_buffer
!= NULL
) {
369 /* Since we've emitted some blits to buffers that will (likely) be used
370 * in rendering operations in other cache domains in this batch, emit a
371 * flush. Once again, we wish for a domain tracker in libdrm to cover
372 * usage inside of a batchbuffer.
374 intel_batchbuffer_emit_mi_flush(brw
);
375 free(intel_obj
->range_map_buffer
);
376 intel_obj
->range_map_buffer
= NULL
;
377 } else if (intel_obj
->range_map_bo
!= NULL
) {
378 drm_intel_bo_unmap(intel_obj
->range_map_bo
);
380 intel_emit_linear_blit(brw
,
381 intel_obj
->buffer
, obj
->Offset
,
382 intel_obj
->range_map_bo
, 0,
385 /* Since we've emitted some blits to buffers that will (likely) be used
386 * in rendering operations in other cache domains in this batch, emit a
387 * flush. Once again, we wish for a domain tracker in libdrm to cover
388 * usage inside of a batchbuffer.
390 intel_batchbuffer_emit_mi_flush(brw
);
392 drm_intel_bo_unreference(intel_obj
->range_map_bo
);
393 intel_obj
->range_map_bo
= NULL
;
394 } else if (intel_obj
->buffer
!= NULL
) {
395 drm_intel_bo_unmap(intel_obj
->buffer
);
405 intel_bufferobj_buffer(struct brw_context
*brw
,
406 struct intel_buffer_object
*intel_obj
,
409 if (intel_obj
->buffer
== NULL
)
410 intel_bufferobj_alloc_buffer(brw
, intel_obj
);
412 return intel_obj
->buffer
;
416 intel_bufferobj_source(struct brw_context
*brw
,
417 struct intel_buffer_object
*intel_obj
,
418 GLuint align
, GLuint
*offset
)
420 *offset
= intel_obj
->offset
;
421 return intel_obj
->buffer
;
425 intel_bufferobj_copy_subdata(struct gl_context
*ctx
,
426 struct gl_buffer_object
*src
,
427 struct gl_buffer_object
*dst
,
428 GLintptr read_offset
, GLintptr write_offset
,
431 struct brw_context
*brw
= brw_context(ctx
);
432 struct intel_buffer_object
*intel_src
= intel_buffer_object(src
);
433 struct intel_buffer_object
*intel_dst
= intel_buffer_object(dst
);
434 drm_intel_bo
*src_bo
, *dst_bo
;
440 dst_bo
= intel_bufferobj_buffer(brw
, intel_dst
, INTEL_WRITE_PART
);
441 src_bo
= intel_bufferobj_source(brw
, intel_src
, 64, &src_offset
);
443 intel_emit_linear_blit(brw
,
444 dst_bo
, write_offset
,
445 src_bo
, read_offset
+ src_offset
, size
);
447 /* Since we've emitted some blits to buffers that will (likely) be used
448 * in rendering operations in other cache domains in this batch, emit a
449 * flush. Once again, we wish for a domain tracker in libdrm to cover
450 * usage inside of a batchbuffer.
452 intel_batchbuffer_emit_mi_flush(brw
);
456 intelInitBufferObjectFuncs(struct dd_function_table
*functions
)
458 functions
->NewBufferObject
= intel_bufferobj_alloc
;
459 functions
->DeleteBuffer
= intel_bufferobj_free
;
460 functions
->BufferData
= intel_bufferobj_data
;
461 functions
->BufferSubData
= intel_bufferobj_subdata
;
462 functions
->GetBufferSubData
= intel_bufferobj_get_subdata
;
463 functions
->MapBufferRange
= intel_bufferobj_map_range
;
464 functions
->FlushMappedBufferRange
= intel_bufferobj_flush_mapped_range
;
465 functions
->UnmapBuffer
= intel_bufferobj_unmap
;
466 functions
->CopyBufferSubData
= intel_bufferobj_copy_subdata
;