3cf0ea0991d3b12de970577dd060c1324ac5604b
[mesa.git] / src / mesa / drivers / dri / intel / 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 #include "main/imports.h"
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/bufferobj.h"
33
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"
39
40 static GLboolean
41 intel_bufferobj_unmap(GLcontext * ctx,
42 GLenum target, struct gl_buffer_object *obj);
43
44 /** Allocates a new dri_bo to store the data for the buffer object. */
45 static void
46 intel_bufferobj_alloc_buffer(struct intel_context *intel,
47 struct intel_buffer_object *intel_obj)
48 {
49 intel_obj->buffer = dri_bo_alloc(intel->bufmgr, "bufferobj",
50 intel_obj->Base.Size, 64);
51 }
52
53 /**
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.
58 */
59 static struct gl_buffer_object *
60 intel_bufferobj_alloc(GLcontext * ctx, GLuint name, GLenum target)
61 {
62 struct intel_buffer_object *obj = CALLOC_STRUCT(intel_buffer_object);
63
64 _mesa_initialize_buffer_object(&obj->Base, name, target);
65
66 obj->buffer = NULL;
67
68 return &obj->Base;
69 }
70
71 /* Break the COW tie to the region. The region gets to keep the data.
72 */
73 void
74 intel_bufferobj_release_region(struct intel_context *intel,
75 struct intel_buffer_object *intel_obj)
76 {
77 assert(intel_obj->region->buffer == intel_obj->buffer);
78 intel_obj->region->pbo = NULL;
79 intel_obj->region = NULL;
80
81 dri_bo_unreference(intel_obj->buffer);
82 intel_obj->buffer = NULL;
83 }
84
85 /* Break the COW tie to the region. Both the pbo and the region end
86 * up with a copy of the data.
87 */
88 void
89 intel_bufferobj_cow(struct intel_context *intel,
90 struct intel_buffer_object *intel_obj)
91 {
92 assert(intel_obj->region);
93 intel_region_cow(intel, intel_obj->region);
94 }
95
96
97 /**
98 * Deallocate/free a vertex/pixel buffer object.
99 * Called via glDeleteBuffersARB().
100 */
101 static void
102 intel_bufferobj_free(GLcontext * ctx, struct gl_buffer_object *obj)
103 {
104 struct intel_context *intel = intel_context(ctx);
105 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
106
107 assert(intel_obj);
108
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)
112 */
113 if (obj->Pointer)
114 intel_bufferobj_unmap(ctx, 0, obj);
115
116 _mesa_free(intel_obj->sys_buffer);
117 if (intel_obj->region) {
118 intel_bufferobj_release_region(intel, intel_obj);
119 }
120 else if (intel_obj->buffer) {
121 dri_bo_unreference(intel_obj->buffer);
122 }
123
124 _mesa_free(intel_obj);
125 }
126
127
128
129 /**
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 glBufferDataARB().
134 */
135 static void
136 intel_bufferobj_data(GLcontext * ctx,
137 GLenum target,
138 GLsizeiptrARB size,
139 const GLvoid * data,
140 GLenum usage, struct gl_buffer_object *obj)
141 {
142 struct intel_context *intel = intel_context(ctx);
143 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
144
145 intel_obj->Base.Size = size;
146 intel_obj->Base.Usage = usage;
147
148 assert(!obj->Pointer); /* Mesa should have unmapped it */
149
150 if (intel_obj->region)
151 intel_bufferobj_release_region(intel, intel_obj);
152
153 if (intel_obj->buffer != NULL) {
154 dri_bo_unreference(intel_obj->buffer);
155 intel_obj->buffer = NULL;
156 }
157 _mesa_free(intel_obj->sys_buffer);
158 intel_obj->sys_buffer = NULL;
159
160 if (size != 0) {
161 #ifdef I915
162 /* On pre-965, stick VBOs in system memory, as we're always doing swtnl
163 * with their contents anyway.
164 */
165 if (target == GL_ARRAY_BUFFER || target == GL_ELEMENT_ARRAY_BUFFER) {
166 intel_obj->sys_buffer = _mesa_malloc(size);
167 if (intel_obj->sys_buffer != NULL) {
168 if (data != NULL)
169 memcpy(intel_obj->sys_buffer, data, size);
170 return;
171 }
172 }
173 #endif
174 intel_bufferobj_alloc_buffer(intel, intel_obj);
175
176 if (data != NULL)
177 dri_bo_subdata(intel_obj->buffer, 0, size, data);
178 }
179 }
180
181
182 /**
183 * Replace data in a subrange of buffer object. If the data range
184 * specified by size + offset extends beyond the end of the buffer or
185 * if data is NULL, no copy is performed.
186 * Called via glBufferSubDataARB().
187 */
188 static void
189 intel_bufferobj_subdata(GLcontext * ctx,
190 GLenum target,
191 GLintptrARB offset,
192 GLsizeiptrARB size,
193 const GLvoid * data, struct gl_buffer_object *obj)
194 {
195 struct intel_context *intel = intel_context(ctx);
196 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
197
198 assert(intel_obj);
199
200 if (intel_obj->region)
201 intel_bufferobj_cow(intel, intel_obj);
202
203 if (intel_obj->sys_buffer)
204 memcpy((char *)intel_obj->sys_buffer + offset, data, size);
205 else
206 dri_bo_subdata(intel_obj->buffer, offset, size, data);
207 }
208
209
210 /**
211 * Called via glGetBufferSubDataARB().
212 */
213 static void
214 intel_bufferobj_get_subdata(GLcontext * ctx,
215 GLenum target,
216 GLintptrARB offset,
217 GLsizeiptrARB size,
218 GLvoid * data, struct gl_buffer_object *obj)
219 {
220 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
221
222 assert(intel_obj);
223 dri_bo_get_subdata(intel_obj->buffer, offset, size, data);
224 }
225
226
227
228 /**
229 * Called via glMapBufferARB().
230 */
231 static void *
232 intel_bufferobj_map(GLcontext * ctx,
233 GLenum target,
234 GLenum access, struct gl_buffer_object *obj)
235 {
236 struct intel_context *intel = intel_context(ctx);
237 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
238 GLboolean read_only = (access == GL_READ_ONLY_ARB);
239 GLboolean write_only = (access == GL_WRITE_ONLY_ARB);
240
241 assert(intel_obj);
242
243 if (intel_obj->sys_buffer) {
244 obj->Pointer = intel_obj->sys_buffer;
245 return obj->Pointer;
246 }
247
248 /* Flush any existing batchbuffer that might have written to this
249 * buffer.
250 */
251 intelFlush(ctx);
252
253 if (intel_obj->region)
254 intel_bufferobj_cow(intel, intel_obj);
255
256 if (intel_obj->buffer == NULL) {
257 obj->Pointer = NULL;
258 return NULL;
259 }
260
261 if (write_only && intel->intelScreen->kernel_exec_fencing) {
262 drm_intel_gem_bo_map_gtt(intel_obj->buffer);
263 intel_obj->mapped_gtt = GL_TRUE;
264 } else {
265 drm_intel_bo_map(intel_obj->buffer, !read_only);
266 intel_obj->mapped_gtt = GL_FALSE;
267 }
268
269 obj->Pointer = intel_obj->buffer->virtual;
270 return obj->Pointer;
271 }
272
273 /**
274 * Called via glMapBufferRange().
275 *
276 * The goal of this extension is to allow apps to accumulate their rendering
277 * at the same time as they accumulate their buffer object. Without it,
278 * you'd end up blocking on execution of rendering every time you mapped
279 * the buffer to put new data in.
280 *
281 * We support it in 3 ways: If unsynchronized, then don't bother
282 * flushing the batchbuffer before mapping the buffer, which can save blocking
283 * in many cases. If we would still block, and they allow the whole buffer
284 * to be invalidated, then just allocate a new buffer to replace the old one.
285 * If not, and we'd block, and they allow the subrange of the buffer to be
286 * invalidated, then we can make a new little BO, let them write into that,
287 * and blit it into the real BO at unmap time.
288 */
289 static void *
290 intel_bufferobj_map_range(GLcontext * ctx,
291 GLenum target, GLsizei offset, GLsizeiptr length,
292 GLbitfield access, struct gl_buffer_object *obj)
293 {
294 struct intel_context *intel = intel_context(ctx);
295 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
296
297 assert(intel_obj);
298
299 if (intel_obj->sys_buffer) {
300 obj->Pointer = intel_obj->sys_buffer + offset;
301 return obj->Pointer;
302 }
303
304 if (intel_obj->region)
305 intel_bufferobj_cow(intel, intel_obj);
306
307 /* If the mapping is synchronized with other GL operations, flush
308 * the batchbuffer so that GEM knows about the buffer access for later
309 * syncing.
310 */
311 if ((access & GL_MAP_WRITE_BIT) && !(access & GL_MAP_UNSYNCHRONIZED_BIT))
312 intelFlush(ctx);
313
314 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
315 * internally uses our functions directly.
316 */
317 obj->Offset = offset;
318 obj->Length = length;
319 obj->AccessFlags = access;
320
321 if (intel_obj->buffer == NULL) {
322 obj->Pointer = NULL;
323 return NULL;
324 }
325
326 /* If the user doesn't care about existing buffer contents and mapping
327 * would cause us to block, then throw out the old buffer.
328 */
329 if (!(access & GL_MAP_UNSYNCHRONIZED_BIT) &&
330 (access & GL_MAP_INVALIDATE_BUFFER_BIT) &&
331 drm_intel_bo_busy(intel_obj->buffer)) {
332 drm_intel_bo_unreference(intel_obj->buffer);
333 intel_obj->buffer = dri_bo_alloc(intel->bufmgr, "bufferobj",
334 intel_obj->Base.Size, 64);
335 }
336
337 /* If the user is mapping a range of an active buffer object but
338 * doesn't require the current contents of that range, make a new
339 * BO, and we'll copy what they put in there out at unmap or
340 * FlushRange time.
341 */
342 if ((access & GL_MAP_INVALIDATE_RANGE_BIT) &&
343 drm_intel_bo_busy(intel_obj->buffer)) {
344 intel_obj->range_map_bo = drm_intel_bo_alloc(intel->bufmgr,
345 "range map",
346 length, 64);
347 if (!(access & GL_MAP_READ_BIT) &&
348 intel->intelScreen->kernel_exec_fencing) {
349 drm_intel_gem_bo_map_gtt(intel_obj->range_map_bo);
350 intel_obj->mapped_gtt = GL_TRUE;
351 } else {
352 drm_intel_bo_map(intel_obj->range_map_bo,
353 (access & GL_MAP_WRITE_BIT) != 0);
354 intel_obj->mapped_gtt = GL_FALSE;
355 }
356 obj->Pointer = intel_obj->range_map_bo->virtual;
357 return obj->Pointer;
358 }
359
360 if (!(access & GL_MAP_READ_BIT) &&
361 intel->intelScreen->kernel_exec_fencing) {
362 drm_intel_gem_bo_map_gtt(intel_obj->buffer);
363 intel_obj->mapped_gtt = GL_TRUE;
364 } else {
365 drm_intel_bo_map(intel_obj->buffer, (access & GL_MAP_WRITE_BIT) != 0);
366 intel_obj->mapped_gtt = GL_FALSE;
367 }
368
369 obj->Pointer = intel_obj->buffer->virtual + offset;
370 return obj->Pointer;
371 }
372
373
374 /**
375 * Called via glUnmapBuffer().
376 */
377 static GLboolean
378 intel_bufferobj_unmap(GLcontext * ctx,
379 GLenum target, struct gl_buffer_object *obj)
380 {
381 struct intel_context *intel = intel_context(ctx);
382 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
383
384 assert(intel_obj);
385 assert(obj->Pointer);
386 if (intel_obj->sys_buffer != NULL) {
387 /* always keep the mapping around. */
388 } else if (intel_obj->range_map_bo != NULL) {
389 if (intel_obj->mapped_gtt) {
390 drm_intel_gem_bo_unmap_gtt(intel_obj->range_map_bo);
391 } else {
392 drm_intel_bo_unmap(intel_obj->range_map_bo);
393 }
394
395 /* We ignore the FLUSH_EXPLICIT bit and the calls associated with it.
396 * It would be a small win to support that, but for now we just copy
397 * the whole mapped range into place.
398 */
399 intel_emit_linear_blit(intel,
400 intel_obj->buffer, obj->Offset,
401 intel_obj->range_map_bo, 0,
402 obj->Length);
403
404 /* Since we've emitted some blits to buffers that will (likely) be used
405 * in rendering operations in other cache domains in this batch, emit a
406 * flush. Once again, we wish for a domain tracker in libdrm to cover
407 * usage inside of a batchbuffer.
408 */
409 intel_batchbuffer_emit_mi_flush(intel->batch);
410
411 drm_intel_bo_unreference(intel_obj->range_map_bo);
412 intel_obj->range_map_bo = NULL;
413 } else if (intel_obj->buffer != NULL) {
414 if (intel_obj->mapped_gtt) {
415 drm_intel_gem_bo_unmap_gtt(intel_obj->buffer);
416 } else {
417 drm_intel_bo_unmap(intel_obj->buffer);
418 }
419 }
420 obj->Pointer = NULL;
421
422 return GL_TRUE;
423 }
424
425 dri_bo *
426 intel_bufferobj_buffer(struct intel_context *intel,
427 struct intel_buffer_object *intel_obj, GLuint flag)
428 {
429 if (intel_obj->region) {
430 if (flag == INTEL_WRITE_PART)
431 intel_bufferobj_cow(intel, intel_obj);
432 else if (flag == INTEL_WRITE_FULL) {
433 intel_bufferobj_release_region(intel, intel_obj);
434 intel_bufferobj_alloc_buffer(intel, intel_obj);
435 }
436 }
437
438 if (intel_obj->buffer == NULL) {
439 void *sys_buffer = intel_obj->sys_buffer;
440
441 /* only one of buffer and sys_buffer could be non-NULL */
442 intel_bufferobj_alloc_buffer(intel, intel_obj);
443 intel_obj->sys_buffer = NULL;
444
445 intel_bufferobj_subdata(&intel->ctx,
446 GL_ARRAY_BUFFER_ARB,
447 0,
448 intel_obj->Base.Size,
449 sys_buffer,
450 &intel_obj->Base);
451 _mesa_free(sys_buffer);
452 intel_obj->sys_buffer = NULL;
453 }
454
455 return intel_obj->buffer;
456 }
457
458 static void
459 intel_bufferobj_copy_subdata(GLcontext *ctx,
460 struct gl_buffer_object *src,
461 struct gl_buffer_object *dst,
462 GLintptr read_offset, GLintptr write_offset,
463 GLsizeiptr size)
464 {
465 struct intel_context *intel = intel_context(ctx);
466 struct intel_buffer_object *intel_src = intel_buffer_object(src);
467 struct intel_buffer_object *intel_dst = intel_buffer_object(dst);
468 drm_intel_bo *src_bo, *dst_bo;
469
470 if (size == 0)
471 return;
472
473 /* If we're in system memory, just map and memcpy. */
474 if (intel_src->sys_buffer || intel_dst->sys_buffer) {
475 /* The same buffer may be used, but note that regions copied may
476 * not overlap.
477 */
478 if (src == dst) {
479 char *ptr = intel_bufferobj_map(ctx, GL_COPY_WRITE_BUFFER,
480 GL_READ_WRITE, dst);
481 memcpy(ptr + write_offset, ptr + read_offset, size);
482 intel_bufferobj_unmap(ctx, GL_COPY_WRITE_BUFFER, dst);
483 } else {
484 const char *src_ptr;
485 char *dst_ptr;
486
487 src_ptr = intel_bufferobj_map(ctx, GL_COPY_READ_BUFFER,
488 GL_READ_ONLY, src);
489 dst_ptr = intel_bufferobj_map(ctx, GL_COPY_WRITE_BUFFER,
490 GL_WRITE_ONLY, dst);
491
492 memcpy(dst_ptr + write_offset, src_ptr + read_offset, size);
493
494 intel_bufferobj_unmap(ctx, GL_COPY_READ_BUFFER, src);
495 intel_bufferobj_unmap(ctx, GL_COPY_WRITE_BUFFER, dst);
496 }
497 }
498
499 /* Otherwise, we have real BOs, so blit them. */
500
501 dst_bo = intel_bufferobj_buffer(intel, intel_dst, INTEL_WRITE_PART);
502 src_bo = intel_bufferobj_buffer(intel, intel_src, INTEL_READ);
503
504 intel_emit_linear_blit(intel,
505 dst_bo, write_offset,
506 src_bo, read_offset, size);
507
508 /* Since we've emitted some blits to buffers that will (likely) be used
509 * in rendering operations in other cache domains in this batch, emit a
510 * flush. Once again, we wish for a domain tracker in libdrm to cover
511 * usage inside of a batchbuffer.
512 */
513 intel_batchbuffer_emit_mi_flush(intel->batch);
514 }
515
516 void
517 intelInitBufferObjectFuncs(struct dd_function_table *functions)
518 {
519 functions->NewBufferObject = intel_bufferobj_alloc;
520 functions->DeleteBuffer = intel_bufferobj_free;
521 functions->BufferData = intel_bufferobj_data;
522 functions->BufferSubData = intel_bufferobj_subdata;
523 functions->GetBufferSubData = intel_bufferobj_get_subdata;
524 functions->MapBuffer = intel_bufferobj_map;
525 functions->MapBufferRange = intel_bufferobj_map_range;
526 functions->UnmapBuffer = intel_bufferobj_unmap;
527 functions->CopyBufferSubData = intel_bufferobj_copy_subdata;
528 }