i965: Split intel_upload code out into a separate file.
[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 #include "main/imports.h"
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/bufferobj.h"
33
34 #include "brw_context.h"
35 #include "intel_blit.h"
36 #include "intel_buffer_objects.h"
37 #include "intel_batchbuffer.h"
38
39 static GLboolean
40 intel_bufferobj_unmap(struct gl_context * ctx, struct gl_buffer_object *obj);
41
42 /** Allocates a new drm_intel_bo to store the data for the buffer object. */
43 static void
44 intel_bufferobj_alloc_buffer(struct brw_context *brw,
45 struct intel_buffer_object *intel_obj)
46 {
47 intel_obj->buffer = drm_intel_bo_alloc(brw->bufmgr, "bufferobj",
48 intel_obj->Base.Size, 64);
49
50 /* the buffer might be bound as a uniform buffer, need to update it
51 */
52 brw->state.dirty.brw |= BRW_NEW_UNIFORM_BUFFER;
53 }
54
55 static void
56 release_buffer(struct intel_buffer_object *intel_obj)
57 {
58 drm_intel_bo_unreference(intel_obj->buffer);
59 intel_obj->buffer = NULL;
60 intel_obj->offset = 0;
61 }
62
63 /**
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.
68 */
69 static struct gl_buffer_object *
70 intel_bufferobj_alloc(struct gl_context * ctx, GLuint name, GLenum target)
71 {
72 struct intel_buffer_object *obj = CALLOC_STRUCT(intel_buffer_object);
73
74 _mesa_initialize_buffer_object(ctx, &obj->Base, name, target);
75
76 obj->buffer = NULL;
77
78 return &obj->Base;
79 }
80
81 /**
82 * Deallocate/free a vertex/pixel buffer object.
83 * Called via glDeleteBuffersARB().
84 */
85 static void
86 intel_bufferobj_free(struct gl_context * ctx, struct gl_buffer_object *obj)
87 {
88 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
89
90 assert(intel_obj);
91
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)
95 */
96 if (obj->Pointer)
97 intel_bufferobj_unmap(ctx, obj);
98
99 drm_intel_bo_unreference(intel_obj->buffer);
100 free(intel_obj);
101 }
102
103
104
105 /**
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
111 */
112 static GLboolean
113 intel_bufferobj_data(struct gl_context * ctx,
114 GLenum target,
115 GLsizeiptrARB size,
116 const GLvoid * data,
117 GLenum usage, struct gl_buffer_object *obj)
118 {
119 struct brw_context *brw = brw_context(ctx);
120 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
121
122 /* Part of the ABI, but this function doesn't use it.
123 */
124 (void) target;
125
126 intel_obj->Base.Size = size;
127 intel_obj->Base.Usage = usage;
128
129 assert(!obj->Pointer); /* Mesa should have unmapped it */
130
131 if (intel_obj->buffer != NULL)
132 release_buffer(intel_obj);
133
134 if (size != 0) {
135 intel_bufferobj_alloc_buffer(brw, intel_obj);
136 if (!intel_obj->buffer)
137 return false;
138
139 if (data != NULL)
140 drm_intel_bo_subdata(intel_obj->buffer, 0, size, data);
141 }
142
143 return true;
144 }
145
146
147 /**
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().
152 */
153 static void
154 intel_bufferobj_subdata(struct gl_context * ctx,
155 GLintptrARB offset,
156 GLsizeiptrARB size,
157 const GLvoid * data, struct gl_buffer_object *obj)
158 {
159 struct brw_context *brw = brw_context(ctx);
160 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
161 bool busy;
162
163 if (size == 0)
164 return;
165
166 assert(intel_obj);
167
168 busy =
169 drm_intel_bo_busy(intel_obj->buffer) ||
170 drm_intel_bo_references(brw->batch.bo, intel_obj->buffer);
171
172 if (busy) {
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);
178 } else {
179 perf_debug("Using a blit copy to avoid stalling on %ldb "
180 "glBufferSubData() to a busy buffer object.\n",
181 (long)size);
182 drm_intel_bo *temp_bo =
183 drm_intel_bo_alloc(brw->bufmgr, "subdata temp", size, 64);
184
185 drm_intel_bo_subdata(temp_bo, 0, size, data);
186
187 intel_emit_linear_blit(brw,
188 intel_obj->buffer, offset,
189 temp_bo, 0,
190 size);
191
192 drm_intel_bo_unreference(temp_bo);
193 }
194 } else {
195 drm_intel_bo_subdata(intel_obj->buffer, offset, size, data);
196 }
197 }
198
199
200 /**
201 * Called via glGetBufferSubDataARB().
202 */
203 static void
204 intel_bufferobj_get_subdata(struct gl_context * ctx,
205 GLintptrARB offset,
206 GLsizeiptrARB size,
207 GLvoid * data, struct gl_buffer_object *obj)
208 {
209 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
210 struct brw_context *brw = brw_context(ctx);
211
212 assert(intel_obj);
213 if (drm_intel_bo_references(brw->batch.bo, intel_obj->buffer)) {
214 intel_batchbuffer_flush(brw);
215 }
216 drm_intel_bo_get_subdata(intel_obj->buffer, offset, size, data);
217 }
218
219
220
221 /**
222 * Called via glMapBufferRange and glMapBuffer
223 *
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.
228 *
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.
236 */
237 static void *
238 intel_bufferobj_map_range(struct gl_context * ctx,
239 GLintptr offset, GLsizeiptr length,
240 GLbitfield access, struct gl_buffer_object *obj)
241 {
242 struct brw_context *brw = brw_context(ctx);
243 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
244
245 assert(intel_obj);
246
247 /* _mesa_MapBufferRange (GL entrypoint) sets these, but the vbo module also
248 * internally uses our functions directly.
249 */
250 obj->Offset = offset;
251 obj->Length = length;
252 obj->AccessFlags = access;
253
254 if (intel_obj->buffer == NULL) {
255 obj->Pointer = NULL;
256 return NULL;
257 }
258
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.
263 *
264 * If they set INVALIDATE_BUFFER, we can pitch the current contents to
265 * achieve the required synchronization.
266 */
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);
272 } else {
273 perf_debug("Stalling on the GPU for mapping a busy buffer "
274 "object\n");
275 intel_flush(ctx);
276 }
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);
281 }
282 }
283
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
287 * FlushRange time.
288 */
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;
294 } else {
295 intel_obj->range_map_bo = drm_intel_bo_alloc(brw->bufmgr,
296 "range map",
297 length, 64);
298 if (!(access & GL_MAP_READ_BIT)) {
299 drm_intel_gem_bo_map_gtt(intel_obj->range_map_bo);
300 } else {
301 drm_intel_bo_map(intel_obj->range_map_bo,
302 (access & GL_MAP_WRITE_BIT) != 0);
303 }
304 obj->Pointer = intel_obj->range_map_bo->virtual;
305 }
306 return obj->Pointer;
307 }
308
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);
313 } else {
314 drm_intel_bo_map(intel_obj->buffer, (access & GL_MAP_WRITE_BIT) != 0);
315 }
316
317 obj->Pointer = intel_obj->buffer->virtual + offset;
318 return obj->Pointer;
319 }
320
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.
325 */
326 static void
327 intel_bufferobj_flush_mapped_range(struct gl_context *ctx,
328 GLintptr offset, GLsizeiptr length,
329 struct gl_buffer_object *obj)
330 {
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;
334
335 /* Unless we're in the range map using a temporary system buffer,
336 * there's no work to do.
337 */
338 if (intel_obj->range_map_buffer == NULL)
339 return;
340
341 if (length == 0)
342 return;
343
344 temp_bo = drm_intel_bo_alloc(brw->bufmgr, "range map flush", length, 64);
345
346 drm_intel_bo_subdata(temp_bo, 0, length, intel_obj->range_map_buffer);
347
348 intel_emit_linear_blit(brw,
349 intel_obj->buffer, obj->Offset + offset,
350 temp_bo, 0,
351 length);
352
353 drm_intel_bo_unreference(temp_bo);
354 }
355
356
357 /**
358 * Called via glUnmapBuffer().
359 */
360 static GLboolean
361 intel_bufferobj_unmap(struct gl_context * ctx, struct gl_buffer_object *obj)
362 {
363 struct brw_context *brw = brw_context(ctx);
364 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
365
366 assert(intel_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.
373 */
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);
379
380 intel_emit_linear_blit(brw,
381 intel_obj->buffer, obj->Offset,
382 intel_obj->range_map_bo, 0,
383 obj->Length);
384
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.
389 */
390 intel_batchbuffer_emit_mi_flush(brw);
391
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);
396 }
397 obj->Pointer = NULL;
398 obj->Offset = 0;
399 obj->Length = 0;
400
401 return true;
402 }
403
404 drm_intel_bo *
405 intel_bufferobj_buffer(struct brw_context *brw,
406 struct intel_buffer_object *intel_obj,
407 GLuint flag)
408 {
409 if (intel_obj->buffer == NULL)
410 intel_bufferobj_alloc_buffer(brw, intel_obj);
411
412 return intel_obj->buffer;
413 }
414
415 drm_intel_bo *
416 intel_bufferobj_source(struct brw_context *brw,
417 struct intel_buffer_object *intel_obj,
418 GLuint align, GLuint *offset)
419 {
420 *offset = intel_obj->offset;
421 return intel_obj->buffer;
422 }
423
424 static void
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,
429 GLsizeiptr size)
430 {
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;
435 GLuint src_offset;
436
437 if (size == 0)
438 return;
439
440 dst_bo = intel_bufferobj_buffer(brw, intel_dst, INTEL_WRITE_PART);
441 src_bo = intel_bufferobj_source(brw, intel_src, 64, &src_offset);
442
443 intel_emit_linear_blit(brw,
444 dst_bo, write_offset,
445 src_bo, read_offset + src_offset, size);
446
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.
451 */
452 intel_batchbuffer_emit_mi_flush(brw);
453 }
454
455 void
456 intelInitBufferObjectFuncs(struct dd_function_table *functions)
457 {
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;
467 }