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