s/Tungsten Graphics/VMware/
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 /**
30 * Functions for pixel buffer objects and vertex/element buffer objects.
31 */
32
33
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/arrayobj.h"
37 #include "main/bufferobj.h"
38
39 #include "st_context.h"
40 #include "st_cb_bufferobjects.h"
41 #include "st_debug.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_inlines.h"
46
47
48 /**
49 * There is some duplication between mesa's bufferobjects and our
50 * bufmgr buffers. Both have an integer handle and a hashtable to
51 * lookup an opaque structure. It would be nice if the handles and
52 * internal structure where somehow shared.
53 */
54 static struct gl_buffer_object *
55 st_bufferobj_alloc(struct gl_context *ctx, GLuint name, GLenum target)
56 {
57 struct st_buffer_object *st_obj = ST_CALLOC_STRUCT(st_buffer_object);
58
59 if (!st_obj)
60 return NULL;
61
62 _mesa_initialize_buffer_object(ctx, &st_obj->Base, name, target);
63
64 return &st_obj->Base;
65 }
66
67
68
69 /**
70 * Deallocate/free a vertex/pixel buffer object.
71 * Called via glDeleteBuffersARB().
72 */
73 static void
74 st_bufferobj_free(struct gl_context *ctx, struct gl_buffer_object *obj)
75 {
76 struct st_buffer_object *st_obj = st_buffer_object(obj);
77
78 assert(obj->RefCount == 0);
79 assert(st_obj->transfer == NULL);
80
81 if (st_obj->buffer)
82 pipe_resource_reference(&st_obj->buffer, NULL);
83
84 free(st_obj->Base.Label);
85 free(st_obj);
86 }
87
88
89
90 /**
91 * Replace data in a subrange of buffer object. If the data range
92 * specified by size + offset extends beyond the end of the buffer or
93 * if data is NULL, no copy is performed.
94 * Called via glBufferSubDataARB().
95 */
96 static void
97 st_bufferobj_subdata(struct gl_context *ctx,
98 GLintptrARB offset,
99 GLsizeiptrARB size,
100 const GLvoid * data, struct gl_buffer_object *obj)
101 {
102 struct st_buffer_object *st_obj = st_buffer_object(obj);
103
104 /* we may be called from VBO code, so double-check params here */
105 ASSERT(offset >= 0);
106 ASSERT(size >= 0);
107 ASSERT(offset + size <= obj->Size);
108
109 if (!size)
110 return;
111
112 /*
113 * According to ARB_vertex_buffer_object specification, if data is null,
114 * then the contents of the buffer object's data store is undefined. We just
115 * ignore, and leave it unchanged.
116 */
117 if (!data)
118 return;
119
120 if (!st_obj->buffer) {
121 /* we probably ran out of memory during buffer allocation */
122 return;
123 }
124
125 /* Now that transfers are per-context, we don't have to figure out
126 * flushing here. Usually drivers won't need to flush in this case
127 * even if the buffer is currently referenced by hardware - they
128 * just queue the upload as dma rather than mapping the underlying
129 * buffer directly.
130 */
131 pipe_buffer_write(st_context(ctx)->pipe,
132 st_obj->buffer,
133 offset, size, data);
134 }
135
136
137 /**
138 * Called via glGetBufferSubDataARB().
139 */
140 static void
141 st_bufferobj_get_subdata(struct gl_context *ctx,
142 GLintptrARB offset,
143 GLsizeiptrARB size,
144 GLvoid * data, struct gl_buffer_object *obj)
145 {
146 struct st_buffer_object *st_obj = st_buffer_object(obj);
147
148 /* we may be called from VBO code, so double-check params here */
149 ASSERT(offset >= 0);
150 ASSERT(size >= 0);
151 ASSERT(offset + size <= obj->Size);
152
153 if (!size)
154 return;
155
156 if (!st_obj->buffer) {
157 /* we probably ran out of memory during buffer allocation */
158 return;
159 }
160
161 pipe_buffer_read(st_context(ctx)->pipe, st_obj->buffer,
162 offset, size, data);
163 }
164
165
166 /**
167 * Allocate space for and store data in a buffer object. Any data that was
168 * previously stored in the buffer object is lost. If data is NULL,
169 * memory will be allocated, but no copy will occur.
170 * Called via ctx->Driver.BufferData().
171 * \return GL_TRUE for success, GL_FALSE if out of memory
172 */
173 static GLboolean
174 st_bufferobj_data(struct gl_context *ctx,
175 GLenum target,
176 GLsizeiptrARB size,
177 const GLvoid * data,
178 GLenum usage,
179 struct gl_buffer_object *obj)
180 {
181 struct st_context *st = st_context(ctx);
182 struct pipe_context *pipe = st->pipe;
183 struct st_buffer_object *st_obj = st_buffer_object(obj);
184 unsigned bind, pipe_usage;
185
186 if (size && data && st_obj->buffer &&
187 st_obj->Base.Size == size && st_obj->Base.Usage == usage) {
188 /* Just discard the old contents and write new data.
189 * This should be the same as creating a new buffer, but we avoid
190 * a lot of validation in Mesa.
191 */
192 struct pipe_box box;
193
194 u_box_1d(0, size, &box);
195 pipe->transfer_inline_write(pipe, st_obj->buffer, 0,
196 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE,
197 &box, data, 0, 0);
198 return GL_TRUE;
199 }
200
201 st_obj->Base.Size = size;
202 st_obj->Base.Usage = usage;
203
204 switch (target) {
205 case GL_PIXEL_PACK_BUFFER_ARB:
206 case GL_PIXEL_UNPACK_BUFFER_ARB:
207 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
208 break;
209 case GL_ARRAY_BUFFER_ARB:
210 bind = PIPE_BIND_VERTEX_BUFFER;
211 break;
212 case GL_ELEMENT_ARRAY_BUFFER_ARB:
213 bind = PIPE_BIND_INDEX_BUFFER;
214 break;
215 case GL_TEXTURE_BUFFER:
216 bind = PIPE_BIND_SAMPLER_VIEW;
217 break;
218 case GL_TRANSFORM_FEEDBACK_BUFFER:
219 bind = PIPE_BIND_STREAM_OUTPUT;
220 break;
221 case GL_UNIFORM_BUFFER:
222 bind = PIPE_BIND_CONSTANT_BUFFER;
223 break;
224 default:
225 bind = 0;
226 }
227
228 switch (usage) {
229 case GL_STATIC_DRAW:
230 case GL_STATIC_READ:
231 case GL_STATIC_COPY:
232 pipe_usage = PIPE_USAGE_STATIC;
233 break;
234 case GL_DYNAMIC_DRAW:
235 case GL_DYNAMIC_READ:
236 case GL_DYNAMIC_COPY:
237 pipe_usage = PIPE_USAGE_DYNAMIC;
238 break;
239 case GL_STREAM_DRAW:
240 case GL_STREAM_READ:
241 case GL_STREAM_COPY:
242 pipe_usage = PIPE_USAGE_STREAM;
243 break;
244 default:
245 pipe_usage = PIPE_USAGE_DEFAULT;
246 }
247
248 pipe_resource_reference( &st_obj->buffer, NULL );
249
250 if (ST_DEBUG & DEBUG_BUFFER) {
251 debug_printf("Create buffer size %td bind 0x%x\n", size, bind);
252 }
253
254 if (size != 0) {
255 st_obj->buffer = pipe_buffer_create(pipe->screen, bind,
256 pipe_usage, size);
257
258 if (!st_obj->buffer) {
259 /* out of memory */
260 st_obj->Base.Size = 0;
261 return GL_FALSE;
262 }
263
264 if (data)
265 pipe_buffer_write(pipe, st_obj->buffer, 0, size, data);
266 }
267
268 /* BufferData may change an array or uniform buffer, need to update it */
269 st->dirty.st |= ST_NEW_VERTEX_ARRAYS | ST_NEW_UNIFORM_BUFFER;
270
271 return GL_TRUE;
272 }
273
274
275 /**
276 * Called via glMapBufferRange().
277 */
278 static void *
279 st_bufferobj_map_range(struct gl_context *ctx,
280 GLintptr offset, GLsizeiptr length, GLbitfield access,
281 struct gl_buffer_object *obj)
282 {
283 struct pipe_context *pipe = st_context(ctx)->pipe;
284 struct st_buffer_object *st_obj = st_buffer_object(obj);
285 enum pipe_transfer_usage flags = 0x0;
286
287 if (access & GL_MAP_WRITE_BIT)
288 flags |= PIPE_TRANSFER_WRITE;
289
290 if (access & GL_MAP_READ_BIT)
291 flags |= PIPE_TRANSFER_READ;
292
293 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
294 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
295
296 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
297 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
298 }
299 else if (access & GL_MAP_INVALIDATE_RANGE_BIT) {
300 if (offset == 0 && length == obj->Size)
301 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
302 else
303 flags |= PIPE_TRANSFER_DISCARD_RANGE;
304 }
305
306 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
307 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
308
309 /* ... other flags ...
310 */
311
312 if (access & MESA_MAP_NOWAIT_BIT)
313 flags |= PIPE_TRANSFER_DONTBLOCK;
314
315 assert(offset >= 0);
316 assert(length >= 0);
317 assert(offset < obj->Size);
318 assert(offset + length <= obj->Size);
319
320 obj->Pointer = pipe_buffer_map_range(pipe,
321 st_obj->buffer,
322 offset, length,
323 flags,
324 &st_obj->transfer);
325 if (obj->Pointer) {
326 obj->Offset = offset;
327 obj->Length = length;
328 obj->AccessFlags = access;
329 }
330 else {
331 st_obj->transfer = NULL;
332 }
333
334 return obj->Pointer;
335 }
336
337
338 static void
339 st_bufferobj_flush_mapped_range(struct gl_context *ctx,
340 GLintptr offset, GLsizeiptr length,
341 struct gl_buffer_object *obj)
342 {
343 struct pipe_context *pipe = st_context(ctx)->pipe;
344 struct st_buffer_object *st_obj = st_buffer_object(obj);
345
346 /* Subrange is relative to mapped range */
347 assert(offset >= 0);
348 assert(length >= 0);
349 assert(offset + length <= obj->Length);
350 assert(obj->Pointer);
351
352 if (!length)
353 return;
354
355 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer,
356 obj->Offset + offset, length);
357 }
358
359
360 /**
361 * Called via glUnmapBufferARB().
362 */
363 static GLboolean
364 st_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj)
365 {
366 struct pipe_context *pipe = st_context(ctx)->pipe;
367 struct st_buffer_object *st_obj = st_buffer_object(obj);
368
369 if (obj->Length)
370 pipe_buffer_unmap(pipe, st_obj->transfer);
371
372 st_obj->transfer = NULL;
373 obj->Pointer = NULL;
374 obj->Offset = 0;
375 obj->Length = 0;
376 return GL_TRUE;
377 }
378
379
380 /**
381 * Called via glCopyBufferSubData().
382 */
383 static void
384 st_copy_buffer_subdata(struct gl_context *ctx,
385 struct gl_buffer_object *src,
386 struct gl_buffer_object *dst,
387 GLintptr readOffset, GLintptr writeOffset,
388 GLsizeiptr size)
389 {
390 struct pipe_context *pipe = st_context(ctx)->pipe;
391 struct st_buffer_object *srcObj = st_buffer_object(src);
392 struct st_buffer_object *dstObj = st_buffer_object(dst);
393 struct pipe_box box;
394
395 if (!size)
396 return;
397
398 /* buffer should not already be mapped */
399 assert(!src->Pointer);
400 assert(!dst->Pointer);
401
402 u_box_1d(readOffset, size, &box);
403
404 pipe->resource_copy_region(pipe, dstObj->buffer, 0, writeOffset, 0, 0,
405 srcObj->buffer, 0, &box);
406 }
407
408
409 /* TODO: if buffer wasn't created with appropriate usage flags, need
410 * to recreate it now and copy contents -- or possibly create a
411 * gallium entrypoint to extend the usage flags and let the driver
412 * decide if a copy is necessary.
413 */
414 void
415 st_bufferobj_validate_usage(struct st_context *st,
416 struct st_buffer_object *obj,
417 unsigned usage)
418 {
419 }
420
421
422 void
423 st_init_bufferobject_functions(struct dd_function_table *functions)
424 {
425 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
426 _mesa_init_buffer_object_functions(functions);
427
428 functions->NewBufferObject = st_bufferobj_alloc;
429 functions->DeleteBuffer = st_bufferobj_free;
430 functions->BufferData = st_bufferobj_data;
431 functions->BufferSubData = st_bufferobj_subdata;
432 functions->GetBufferSubData = st_bufferobj_get_subdata;
433 functions->MapBufferRange = st_bufferobj_map_range;
434 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
435 functions->UnmapBuffer = st_bufferobj_unmap;
436 functions->CopyBufferSubData = st_copy_buffer_subdata;
437
438 /* For GL_APPLE_vertex_array_object */
439 functions->NewArrayObject = _mesa_new_array_object;
440 functions->DeleteArrayObject = _mesa_delete_array_object;
441 }