i965: Directly call intel_batchbuffer_flush() after i915 split.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_buffer_objects.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file intel_buffer_objects.c
30 *
31 * This provides core GL buffer object functionality.
32 */
33
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/macros.h"
37 #include "main/bufferobj.h"
38
39 #include "brw_context.h"
40 #include "intel_blit.h"
41 #include "intel_buffer_objects.h"
42 #include "intel_batchbuffer.h"
43
44 static GLboolean
45 intel_bufferobj_unmap(struct gl_context * ctx, struct gl_buffer_object *obj);
46
47 /** Allocates a new drm_intel_bo to store the data for the buffer object. */
48 static void
49 intel_bufferobj_alloc_buffer(struct brw_context *brw,
50 struct intel_buffer_object *intel_obj)
51 {
52 intel_obj->buffer = drm_intel_bo_alloc(brw->bufmgr, "bufferobj",
53 intel_obj->Base.Size, 64);
54
55 /* the buffer might be bound as a uniform buffer, need to update it
56 */
57 brw->state.dirty.brw |= BRW_NEW_UNIFORM_BUFFER;
58 }
59
60 static void
61 release_buffer(struct intel_buffer_object *intel_obj)
62 {
63 drm_intel_bo_unreference(intel_obj->buffer);
64 intel_obj->buffer = NULL;
65 intel_obj->offset = 0;
66 }
67
68 /**
69 * The NewBufferObject() driver hook.
70 *
71 * Allocates a new intel_buffer_object structure and initializes it.
72 *
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.
77 */
78 static struct gl_buffer_object *
79 intel_bufferobj_alloc(struct gl_context * ctx, GLuint name, GLenum target)
80 {
81 struct intel_buffer_object *obj = CALLOC_STRUCT(intel_buffer_object);
82
83 _mesa_initialize_buffer_object(ctx, &obj->Base, name, target);
84
85 obj->buffer = NULL;
86
87 return &obj->Base;
88 }
89
90 /**
91 * The DeleteBuffer() driver hook.
92 *
93 * Deletes a single OpenGL buffer object. Used by glDeleteBuffers().
94 */
95 static void
96 intel_bufferobj_free(struct gl_context * ctx, struct gl_buffer_object *obj)
97 {
98 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
99
100 assert(intel_obj);
101
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)
105 */
106 if (obj->Pointer)
107 intel_bufferobj_unmap(ctx, obj);
108
109 drm_intel_bo_unreference(intel_obj->buffer);
110 free(intel_obj);
111 }
112
113
114 /**
115 * The BufferData() driver hook.
116 *
117 * Implements glBufferData(), which recreates a buffer object's data store
118 * and populates it with the given data, if present.
119 *
120 * Any data that was previously stored in the buffer object is lost.
121 *
122 * \return true for success, false if out of memory
123 */
124 static GLboolean
125 intel_bufferobj_data(struct gl_context * ctx,
126 GLenum target,
127 GLsizeiptrARB size,
128 const GLvoid * data,
129 GLenum usage, struct gl_buffer_object *obj)
130 {
131 struct brw_context *brw = brw_context(ctx);
132 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
133
134 /* Part of the ABI, but this function doesn't use it.
135 */
136 (void) target;
137
138 intel_obj->Base.Size = size;
139 intel_obj->Base.Usage = usage;
140
141 assert(!obj->Pointer); /* Mesa should have unmapped it */
142
143 if (intel_obj->buffer != NULL)
144 release_buffer(intel_obj);
145
146 if (size != 0) {
147 intel_bufferobj_alloc_buffer(brw, intel_obj);
148 if (!intel_obj->buffer)
149 return false;
150
151 if (data != NULL)
152 drm_intel_bo_subdata(intel_obj->buffer, 0, size, data);
153 }
154
155 return true;
156 }
157
158
159 /**
160 * The BufferSubData() driver hook.
161 *
162 * Implements glBufferSubData(), which replaces a portion of the data in a
163 * buffer object.
164 *
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.
167 */
168 static void
169 intel_bufferobj_subdata(struct gl_context * ctx,
170 GLintptrARB offset,
171 GLsizeiptrARB size,
172 const GLvoid * data, struct gl_buffer_object *obj)
173 {
174 struct brw_context *brw = brw_context(ctx);
175 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
176 bool busy;
177
178 if (size == 0)
179 return;
180
181 assert(intel_obj);
182
183 busy =
184 drm_intel_bo_busy(intel_obj->buffer) ||
185 drm_intel_bo_references(brw->batch.bo, intel_obj->buffer);
186
187 if (busy) {
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);
193 } else {
194 perf_debug("Using a blit copy to avoid stalling on %ldb "
195 "glBufferSubData() to a busy buffer object.\n",
196 (long)size);
197 drm_intel_bo *temp_bo =
198 drm_intel_bo_alloc(brw->bufmgr, "subdata temp", size, 64);
199
200 drm_intel_bo_subdata(temp_bo, 0, size, data);
201
202 intel_emit_linear_blit(brw,
203 intel_obj->buffer, offset,
204 temp_bo, 0,
205 size);
206
207 drm_intel_bo_unreference(temp_bo);
208 }
209 } else {
210 drm_intel_bo_subdata(intel_obj->buffer, offset, size, data);
211 }
212 }
213
214
215 /**
216 * The GetBufferSubData() driver hook.
217 *
218 * Implements glGetBufferSubData(), which copies a subrange of a buffer
219 * object into user memory.
220 */
221 static void
222 intel_bufferobj_get_subdata(struct gl_context * ctx,
223 GLintptrARB offset,
224 GLsizeiptrARB size,
225 GLvoid * data, struct gl_buffer_object *obj)
226 {
227 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
228 struct brw_context *brw = brw_context(ctx);
229
230 assert(intel_obj);
231 if (drm_intel_bo_references(brw->batch.bo, intel_obj->buffer)) {
232 intel_batchbuffer_flush(brw);
233 }
234 drm_intel_bo_get_subdata(intel_obj->buffer, offset, size, data);
235 }
236
237
238 /**
239 * The MapBufferRange() driver hook.
240 *
241 * This implements both glMapBufferRange() and glMapBuffer().
242 *
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.
247 *
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.
255 */
256 static void *
257 intel_bufferobj_map_range(struct gl_context * ctx,
258 GLintptr offset, GLsizeiptr length,
259 GLbitfield access, struct gl_buffer_object *obj)
260 {
261 struct brw_context *brw = brw_context(ctx);
262 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
263
264 assert(intel_obj);
265
266 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
267 * internally uses our functions directly.
268 */
269 obj->Offset = offset;
270 obj->Length = length;
271 obj->AccessFlags = access;
272
273 if (intel_obj->buffer == NULL) {
274 obj->Pointer = NULL;
275 return NULL;
276 }
277
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.
282 *
283 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
284 * achieve the required synchronization.
285 */
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);
291 } else {
292 perf_debug("Stalling on the GPU for mapping a busy buffer "
293 "object\n");
294 intel_batchbuffer_flush(brw);
295 }
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);
300 }
301 }
302
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
306 * FlushRange time.
307 */
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;
313 } else {
314 intel_obj->range_map_bo = drm_intel_bo_alloc(brw->bufmgr,
315 "range map",
316 length, 64);
317 if (!(access & GL_MAP_READ_BIT)) {
318 drm_intel_gem_bo_map_gtt(intel_obj->range_map_bo);
319 } else {
320 drm_intel_bo_map(intel_obj->range_map_bo,
321 (access & GL_MAP_WRITE_BIT) != 0);
322 }
323 obj->Pointer = intel_obj->range_map_bo->virtual;
324 }
325 return obj->Pointer;
326 }
327
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);
332 } else {
333 drm_intel_bo_map(intel_obj->buffer, (access & GL_MAP_WRITE_BIT) != 0);
334 }
335
336 obj->Pointer = intel_obj->buffer->virtual + offset;
337 return obj->Pointer;
338 }
339
340 /**
341 * The FlushMappedBufferRange() driver hook.
342 *
343 * Implements glFlushMappedBufferRange(), which signifies that modifications
344 * have been made to a range of a mapped buffer, and it should be flushed.
345 *
346 * This is only used for buffers mapped with GL_MAP_FLUSH_EXPLICIT_BIT.
347 *
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.
352 */
353 static void
354 intel_bufferobj_flush_mapped_range(struct gl_context *ctx,
355 GLintptr offset, GLsizeiptr length,
356 struct gl_buffer_object *obj)
357 {
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;
361
362 /* Unless we're in the range map using a temporary system buffer,
363 * there's no work to do.
364 */
365 if (intel_obj->range_map_buffer == NULL)
366 return;
367
368 if (length == 0)
369 return;
370
371 temp_bo = drm_intel_bo_alloc(brw->bufmgr, "range map flush", length, 64);
372
373 drm_intel_bo_subdata(temp_bo, 0, length, intel_obj->range_map_buffer);
374
375 intel_emit_linear_blit(brw,
376 intel_obj->buffer, obj->Offset + offset,
377 temp_bo, 0,
378 length);
379
380 drm_intel_bo_unreference(temp_bo);
381 }
382
383
384 /**
385 * The UnmapBuffer() driver hook.
386 *
387 * Implements glUnmapBuffer().
388 */
389 static GLboolean
390 intel_bufferobj_unmap(struct gl_context * ctx, struct gl_buffer_object *obj)
391 {
392 struct brw_context *brw = brw_context(ctx);
393 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
394
395 assert(intel_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.
402 */
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);
408
409 intel_emit_linear_blit(brw,
410 intel_obj->buffer, obj->Offset,
411 intel_obj->range_map_bo, 0,
412 obj->Length);
413
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.
418 */
419 intel_batchbuffer_emit_mi_flush(brw);
420
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);
425 }
426 obj->Pointer = NULL;
427 obj->Offset = 0;
428 obj->Length = 0;
429
430 return true;
431 }
432
433 drm_intel_bo *
434 intel_bufferobj_buffer(struct brw_context *brw,
435 struct intel_buffer_object *intel_obj,
436 GLuint flag)
437 {
438 if (intel_obj->buffer == NULL)
439 intel_bufferobj_alloc_buffer(brw, intel_obj);
440
441 return intel_obj->buffer;
442 }
443
444 drm_intel_bo *
445 intel_bufferobj_source(struct brw_context *brw,
446 struct intel_buffer_object *intel_obj,
447 GLuint align, GLuint *offset)
448 {
449 *offset = intel_obj->offset;
450 return intel_obj->buffer;
451 }
452
453 /**
454 * The CopyBufferSubData() driver hook.
455 *
456 * Implements glCopyBufferSubData(), which copies a portion of one buffer
457 * object's data to another. Independent source and destination offsets
458 * are allowed.
459 */
460 static void
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,
465 GLsizeiptr size)
466 {
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;
471 GLuint src_offset;
472
473 if (size == 0)
474 return;
475
476 dst_bo = intel_bufferobj_buffer(brw, intel_dst, INTEL_WRITE_PART);
477 src_bo = intel_bufferobj_source(brw, intel_src, 64, &src_offset);
478
479 intel_emit_linear_blit(brw,
480 dst_bo, write_offset,
481 src_bo, read_offset + src_offset, size);
482
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.
487 */
488 intel_batchbuffer_emit_mi_flush(brw);
489 }
490
491 void
492 intelInitBufferObjectFuncs(struct dd_function_table *functions)
493 {
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;
503 }