mesa: use _mesa_get_current_tex_unit() helper
[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 ctx->Driver.BufferData().
134 * \return GL_TRUE for success, GL_FALSE if out of memory
135 */
136 static GLboolean
137 intel_bufferobj_data(GLcontext * ctx,
138 GLenum target,
139 GLsizeiptrARB size,
140 const GLvoid * data,
141 GLenum usage, struct gl_buffer_object *obj)
142 {
143 struct intel_context *intel = intel_context(ctx);
144 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
145
146 intel_obj->Base.Size = size;
147 intel_obj->Base.Usage = usage;
148
149 assert(!obj->Pointer); /* Mesa should have unmapped it */
150
151 if (intel_obj->region)
152 intel_bufferobj_release_region(intel, intel_obj);
153
154 if (intel_obj->buffer != NULL) {
155 dri_bo_unreference(intel_obj->buffer);
156 intel_obj->buffer = NULL;
157 }
158 _mesa_free(intel_obj->sys_buffer);
159 intel_obj->sys_buffer = NULL;
160
161 if (size != 0) {
162 #ifdef I915
163 /* On pre-965, stick VBOs in system memory, as we're always doing swtnl
164 * with their contents anyway.
165 */
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) {
169 if (data != NULL)
170 memcpy(intel_obj->sys_buffer, data, size);
171 return GL_TRUE;
172 }
173 }
174 #endif
175 intel_bufferobj_alloc_buffer(intel, intel_obj);
176 if (!intel_obj->buffer)
177 return GL_FALSE;
178
179 if (data != NULL)
180 dri_bo_subdata(intel_obj->buffer, 0, size, data);
181 }
182
183 return GL_TRUE;
184 }
185
186
187 /**
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().
192 */
193 static void
194 intel_bufferobj_subdata(GLcontext * ctx,
195 GLenum target,
196 GLintptrARB offset,
197 GLsizeiptrARB size,
198 const GLvoid * data, struct gl_buffer_object *obj)
199 {
200 struct intel_context *intel = intel_context(ctx);
201 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
202
203 assert(intel_obj);
204
205 if (intel_obj->region)
206 intel_bufferobj_cow(intel, intel_obj);
207
208 if (intel_obj->sys_buffer)
209 memcpy((char *)intel_obj->sys_buffer + offset, data, size);
210 else {
211 /* Flush any existing batchbuffer that might reference this data. */
212 intelFlush(ctx);
213
214 dri_bo_subdata(intel_obj->buffer, offset, size, data);
215 }
216 }
217
218
219 /**
220 * Called via glGetBufferSubDataARB().
221 */
222 static void
223 intel_bufferobj_get_subdata(GLcontext * ctx,
224 GLenum target,
225 GLintptrARB offset,
226 GLsizeiptrARB size,
227 GLvoid * data, struct gl_buffer_object *obj)
228 {
229 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
230
231 assert(intel_obj);
232 dri_bo_get_subdata(intel_obj->buffer, offset, size, data);
233 }
234
235
236
237 /**
238 * Called via glMapBufferARB().
239 */
240 static void *
241 intel_bufferobj_map(GLcontext * ctx,
242 GLenum target,
243 GLenum access, struct gl_buffer_object *obj)
244 {
245 struct intel_context *intel = intel_context(ctx);
246 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
247 GLboolean read_only = (access == GL_READ_ONLY_ARB);
248 GLboolean write_only = (access == GL_WRITE_ONLY_ARB);
249
250 assert(intel_obj);
251
252 if (intel_obj->sys_buffer) {
253 obj->Pointer = intel_obj->sys_buffer;
254 return obj->Pointer;
255 }
256
257 /* Flush any existing batchbuffer that might have written to this
258 * buffer.
259 */
260 intelFlush(ctx);
261
262 if (intel_obj->region)
263 intel_bufferobj_cow(intel, intel_obj);
264
265 if (intel_obj->buffer == NULL) {
266 obj->Pointer = NULL;
267 return NULL;
268 }
269
270 if (write_only && intel->intelScreen->kernel_exec_fencing) {
271 drm_intel_gem_bo_map_gtt(intel_obj->buffer);
272 intel_obj->mapped_gtt = GL_TRUE;
273 } else {
274 drm_intel_bo_map(intel_obj->buffer, !read_only);
275 intel_obj->mapped_gtt = GL_FALSE;
276 }
277
278 obj->Pointer = intel_obj->buffer->virtual;
279 obj->Length = obj->Size;
280 obj->Offset = 0;
281
282 return obj->Pointer;
283 }
284
285 /**
286 * Called via glMapBufferRange().
287 *
288 * The goal of this extension is to allow apps to accumulate their rendering
289 * at the same time as they accumulate their buffer object. Without it,
290 * you'd end up blocking on execution of rendering every time you mapped
291 * the buffer to put new data in.
292 *
293 * We support it in 3 ways: If unsynchronized, then don't bother
294 * flushing the batchbuffer before mapping the buffer, which can save blocking
295 * in many cases. If we would still block, and they allow the whole buffer
296 * to be invalidated, then just allocate a new buffer to replace the old one.
297 * If not, and we'd block, and they allow the subrange of the buffer to be
298 * invalidated, then we can make a new little BO, let them write into that,
299 * and blit it into the real BO at unmap time.
300 */
301 static void *
302 intel_bufferobj_map_range(GLcontext * ctx,
303 GLenum target, GLintptr offset, GLsizeiptr length,
304 GLbitfield access, struct gl_buffer_object *obj)
305 {
306 struct intel_context *intel = intel_context(ctx);
307 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
308
309 assert(intel_obj);
310
311 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
312 * internally uses our functions directly.
313 */
314 obj->Offset = offset;
315 obj->Length = length;
316 obj->AccessFlags = access;
317
318 if (intel_obj->sys_buffer) {
319 obj->Pointer = intel_obj->sys_buffer + offset;
320 return obj->Pointer;
321 }
322
323 if (intel_obj->region)
324 intel_bufferobj_cow(intel, intel_obj);
325
326 /* If the mapping is synchronized with other GL operations, flush
327 * the batchbuffer so that GEM knows about the buffer access for later
328 * syncing.
329 */
330 if (!(access & GL_MAP_UNSYNCHRONIZED_BIT))
331 intelFlush(ctx);
332
333 if (intel_obj->buffer == NULL) {
334 obj->Pointer = NULL;
335 return NULL;
336 }
337
338 /* If the user doesn't care about existing buffer contents and mapping
339 * would cause us to block, then throw out the old buffer.
340 */
341 if (!(access & GL_MAP_UNSYNCHRONIZED_BIT) &&
342 (access & GL_MAP_INVALIDATE_BUFFER_BIT) &&
343 drm_intel_bo_busy(intel_obj->buffer)) {
344 drm_intel_bo_unreference(intel_obj->buffer);
345 intel_obj->buffer = dri_bo_alloc(intel->bufmgr, "bufferobj",
346 intel_obj->Base.Size, 64);
347 }
348
349 /* If the user is mapping a range of an active buffer object but
350 * doesn't require the current contents of that range, make a new
351 * BO, and we'll copy what they put in there out at unmap or
352 * FlushRange time.
353 */
354 if ((access & GL_MAP_INVALIDATE_RANGE_BIT) &&
355 drm_intel_bo_busy(intel_obj->buffer)) {
356 if (access & GL_MAP_FLUSH_EXPLICIT_BIT) {
357 intel_obj->range_map_buffer = _mesa_malloc(length);
358 obj->Pointer = intel_obj->range_map_buffer;
359 } else {
360 intel_obj->range_map_bo = drm_intel_bo_alloc(intel->bufmgr,
361 "range map",
362 length, 64);
363 if (!(access & GL_MAP_READ_BIT) &&
364 intel->intelScreen->kernel_exec_fencing) {
365 drm_intel_gem_bo_map_gtt(intel_obj->range_map_bo);
366 intel_obj->mapped_gtt = GL_TRUE;
367 } else {
368 drm_intel_bo_map(intel_obj->range_map_bo,
369 (access & GL_MAP_WRITE_BIT) != 0);
370 intel_obj->mapped_gtt = GL_FALSE;
371 }
372 obj->Pointer = intel_obj->range_map_bo->virtual;
373 }
374 return obj->Pointer;
375 }
376
377 if (!(access & GL_MAP_READ_BIT) &&
378 intel->intelScreen->kernel_exec_fencing) {
379 drm_intel_gem_bo_map_gtt(intel_obj->buffer);
380 intel_obj->mapped_gtt = GL_TRUE;
381 } else {
382 drm_intel_bo_map(intel_obj->buffer, (access & GL_MAP_WRITE_BIT) != 0);
383 intel_obj->mapped_gtt = GL_FALSE;
384 }
385
386 obj->Pointer = intel_obj->buffer->virtual + offset;
387 return obj->Pointer;
388 }
389
390 /* Ideally we'd use a BO to avoid taking up cache space for the temporary
391 * data, but FlushMappedBufferRange may be followed by further writes to
392 * the pointer, so we would have to re-map after emitting our blit, which
393 * would defeat the point.
394 */
395 static void
396 intel_bufferobj_flush_mapped_range(GLcontext *ctx, GLenum target,
397 GLintptr offset, GLsizeiptr length,
398 struct gl_buffer_object *obj)
399 {
400 struct intel_context *intel = intel_context(ctx);
401 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
402 drm_intel_bo *temp_bo;
403
404 /* Unless we're in the range map using a temporary system buffer,
405 * there's no work to do.
406 */
407 if (intel_obj->range_map_buffer == NULL)
408 return;
409
410 temp_bo = drm_intel_bo_alloc(intel->bufmgr, "range map flush", length, 64);
411
412 drm_intel_bo_subdata(temp_bo, 0, length, intel_obj->range_map_buffer);
413
414 intel_emit_linear_blit(intel,
415 intel_obj->buffer, obj->Offset + offset,
416 temp_bo, 0,
417 length);
418
419 drm_intel_bo_unreference(temp_bo);
420 }
421
422
423 /**
424 * Called via glUnmapBuffer().
425 */
426 static GLboolean
427 intel_bufferobj_unmap(GLcontext * ctx,
428 GLenum target, struct gl_buffer_object *obj)
429 {
430 struct intel_context *intel = intel_context(ctx);
431 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
432
433 assert(intel_obj);
434 assert(obj->Pointer);
435 if (intel_obj->sys_buffer != NULL) {
436 /* always keep the mapping around. */
437 } else if (intel_obj->range_map_buffer != NULL) {
438 /* Since we've emitted some blits to buffers that will (likely) be used
439 * in rendering operations in other cache domains in this batch, emit a
440 * flush. Once again, we wish for a domain tracker in libdrm to cover
441 * usage inside of a batchbuffer.
442 */
443 intel_batchbuffer_emit_mi_flush(intel->batch);
444 free(intel_obj->range_map_buffer);
445 intel_obj->range_map_buffer = NULL;
446 } else if (intel_obj->range_map_bo != NULL) {
447 if (intel_obj->mapped_gtt) {
448 drm_intel_gem_bo_unmap_gtt(intel_obj->range_map_bo);
449 } else {
450 drm_intel_bo_unmap(intel_obj->range_map_bo);
451 }
452
453 intel_emit_linear_blit(intel,
454 intel_obj->buffer, obj->Offset,
455 intel_obj->range_map_bo, 0,
456 obj->Length);
457
458 /* Since we've emitted some blits to buffers that will (likely) be used
459 * in rendering operations in other cache domains in this batch, emit a
460 * flush. Once again, we wish for a domain tracker in libdrm to cover
461 * usage inside of a batchbuffer.
462 */
463 intel_batchbuffer_emit_mi_flush(intel->batch);
464
465 drm_intel_bo_unreference(intel_obj->range_map_bo);
466 intel_obj->range_map_bo = NULL;
467 } else if (intel_obj->buffer != NULL) {
468 if (intel_obj->mapped_gtt) {
469 drm_intel_gem_bo_unmap_gtt(intel_obj->buffer);
470 } else {
471 drm_intel_bo_unmap(intel_obj->buffer);
472 }
473 }
474 obj->Pointer = NULL;
475 obj->Offset = 0;
476 obj->Length = 0;
477
478 return GL_TRUE;
479 }
480
481 dri_bo *
482 intel_bufferobj_buffer(struct intel_context *intel,
483 struct intel_buffer_object *intel_obj, GLuint flag)
484 {
485 if (intel_obj->region) {
486 if (flag == INTEL_WRITE_PART)
487 intel_bufferobj_cow(intel, intel_obj);
488 else if (flag == INTEL_WRITE_FULL) {
489 intel_bufferobj_release_region(intel, intel_obj);
490 intel_bufferobj_alloc_buffer(intel, intel_obj);
491 }
492 }
493
494 if (intel_obj->buffer == NULL) {
495 void *sys_buffer = intel_obj->sys_buffer;
496
497 /* only one of buffer and sys_buffer could be non-NULL */
498 intel_bufferobj_alloc_buffer(intel, intel_obj);
499 intel_obj->sys_buffer = NULL;
500
501 intel_bufferobj_subdata(&intel->ctx,
502 GL_ARRAY_BUFFER_ARB,
503 0,
504 intel_obj->Base.Size,
505 sys_buffer,
506 &intel_obj->Base);
507 _mesa_free(sys_buffer);
508 intel_obj->sys_buffer = NULL;
509 }
510
511 return intel_obj->buffer;
512 }
513
514 static void
515 intel_bufferobj_copy_subdata(GLcontext *ctx,
516 struct gl_buffer_object *src,
517 struct gl_buffer_object *dst,
518 GLintptr read_offset, GLintptr write_offset,
519 GLsizeiptr size)
520 {
521 struct intel_context *intel = intel_context(ctx);
522 struct intel_buffer_object *intel_src = intel_buffer_object(src);
523 struct intel_buffer_object *intel_dst = intel_buffer_object(dst);
524 drm_intel_bo *src_bo, *dst_bo;
525
526 if (size == 0)
527 return;
528
529 /* If we're in system memory, just map and memcpy. */
530 if (intel_src->sys_buffer || intel_dst->sys_buffer) {
531 /* The same buffer may be used, but note that regions copied may
532 * not overlap.
533 */
534 if (src == dst) {
535 char *ptr = intel_bufferobj_map(ctx, GL_COPY_WRITE_BUFFER,
536 GL_READ_WRITE, dst);
537 memcpy(ptr + write_offset, ptr + read_offset, size);
538 intel_bufferobj_unmap(ctx, GL_COPY_WRITE_BUFFER, dst);
539 } else {
540 const char *src_ptr;
541 char *dst_ptr;
542
543 src_ptr = intel_bufferobj_map(ctx, GL_COPY_READ_BUFFER,
544 GL_READ_ONLY, src);
545 dst_ptr = intel_bufferobj_map(ctx, GL_COPY_WRITE_BUFFER,
546 GL_WRITE_ONLY, dst);
547
548 memcpy(dst_ptr + write_offset, src_ptr + read_offset, size);
549
550 intel_bufferobj_unmap(ctx, GL_COPY_READ_BUFFER, src);
551 intel_bufferobj_unmap(ctx, GL_COPY_WRITE_BUFFER, dst);
552 }
553 }
554
555 /* Otherwise, we have real BOs, so blit them. */
556
557 dst_bo = intel_bufferobj_buffer(intel, intel_dst, INTEL_WRITE_PART);
558 src_bo = intel_bufferobj_buffer(intel, intel_src, INTEL_READ);
559
560 intel_emit_linear_blit(intel,
561 dst_bo, write_offset,
562 src_bo, read_offset, size);
563
564 /* Since we've emitted some blits to buffers that will (likely) be used
565 * in rendering operations in other cache domains in this batch, emit a
566 * flush. Once again, we wish for a domain tracker in libdrm to cover
567 * usage inside of a batchbuffer.
568 */
569 intel_batchbuffer_emit_mi_flush(intel->batch);
570 }
571
572 void
573 intelInitBufferObjectFuncs(struct dd_function_table *functions)
574 {
575 functions->NewBufferObject = intel_bufferobj_alloc;
576 functions->DeleteBuffer = intel_bufferobj_free;
577 functions->BufferData = intel_bufferobj_data;
578 functions->BufferSubData = intel_bufferobj_subdata;
579 functions->GetBufferSubData = intel_bufferobj_get_subdata;
580 functions->MapBuffer = intel_bufferobj_map;
581 functions->MapBufferRange = intel_bufferobj_map_range;
582 functions->FlushMappedBufferRange = intel_bufferobj_flush_mapped_range;
583 functions->UnmapBuffer = intel_bufferobj_unmap;
584 functions->CopyBufferSubData = intel_bufferobj_copy_subdata;
585 }