xref: /third_party/mesa3d/src/mesa/main/bufferobj.h (revision bf215546)
1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5 * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27
28#ifndef BUFFEROBJ_H
29#define BUFFEROBJ_H
30
31#include <stdbool.h>
32#include "mtypes.h"
33
34
35/*
36 * Internal functions
37 */
38
39static inline struct pipe_resource *
40_mesa_get_bufferobj_reference(struct gl_context *ctx, struct gl_buffer_object *obj)
41{
42   if (unlikely(!obj))
43      return NULL;
44
45   struct pipe_resource *buffer = obj->buffer;
46
47   if (unlikely(!buffer))
48      return NULL;
49
50   /* Only one context is using the fast path. All other contexts must use
51    * the slow path.
52    */
53   if (unlikely(obj->private_refcount_ctx != ctx)) {
54      p_atomic_inc(&buffer->reference.count);
55      return buffer;
56   }
57
58   if (unlikely(obj->private_refcount <= 0)) {
59      assert(obj->private_refcount == 0);
60
61      /* This is the number of atomic increments we will skip. */
62      obj->private_refcount = 100000000;
63      p_atomic_add(&buffer->reference.count, obj->private_refcount);
64   }
65
66   /* Return a buffer reference while decrementing the private refcount. */
67   obj->private_refcount--;
68   return buffer;
69}
70
71void _mesa_bufferobj_subdata(struct gl_context *ctx,
72                          GLintptrARB offset,
73                          GLsizeiptrARB size,
74                          const void * data, struct gl_buffer_object *obj);
75GLboolean _mesa_bufferobj_data(struct gl_context *ctx,
76                            GLenum target,
77                            GLsizeiptrARB size,
78                            const void *data,
79                            GLenum usage,
80                            GLbitfield storageFlags,
81                            struct gl_buffer_object *obj);
82void
83_mesa_bufferobj_get_subdata(struct gl_context *ctx,
84                            GLintptrARB offset,
85                            GLsizeiptrARB size,
86                            void *data, struct gl_buffer_object *obj);
87
88void *_mesa_bufferobj_map_range(struct gl_context *ctx,
89                                GLintptr offset, GLsizeiptr length,
90                                GLbitfield access,
91                                struct gl_buffer_object *obj,
92                                gl_map_buffer_index index);
93
94void _mesa_bufferobj_flush_mapped_range(struct gl_context *ctx,
95                                        GLintptr offset, GLsizeiptr length,
96                                        struct gl_buffer_object *obj,
97                                        gl_map_buffer_index index);
98GLboolean _mesa_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj,
99                                 gl_map_buffer_index index);
100
101struct gl_buffer_object *
102_mesa_bufferobj_alloc(struct gl_context *ctx, GLuint id);
103void
104_mesa_bufferobj_release_buffer(struct gl_buffer_object *obj);
105
106enum pipe_map_flags
107_mesa_access_flags_to_transfer_flags(GLbitfield access, bool wholeBuffer);
108
109/** Is the given buffer object currently mapped by the GL user? */
110static inline GLboolean
111_mesa_bufferobj_mapped(const struct gl_buffer_object *obj,
112                       gl_map_buffer_index index)
113{
114   return obj->Mappings[index].Pointer != NULL;
115}
116
117/**
118 * Check whether the given buffer object is illegally mapped prior to
119 * drawing from (or reading back to) the buffer.
120 * Note that it's legal for a buffer to be mapped at draw/readback time
121 * if it was mapped persistently (See GL_ARB_buffer_storage spec).
122 * \return true if the buffer is illegally mapped, false otherwise
123 */
124static inline bool
125_mesa_check_disallowed_mapping(const struct gl_buffer_object *obj)
126{
127   return _mesa_bufferobj_mapped(obj, MAP_USER) &&
128          !(obj->Mappings[MAP_USER].AccessFlags &
129            GL_MAP_PERSISTENT_BIT);
130}
131
132
133extern void
134_mesa_init_buffer_objects(struct gl_context *ctx);
135
136extern void
137_mesa_free_buffer_objects(struct gl_context *ctx);
138
139extern bool
140_mesa_handle_bind_buffer_gen(struct gl_context *ctx,
141                             GLuint buffer,
142                             struct gl_buffer_object **buf_handle,
143                             const char *caller, bool no_error);
144
145extern void
146_mesa_update_default_objects_buffer_objects(struct gl_context *ctx);
147
148
149extern struct gl_buffer_object *
150_mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer);
151
152extern struct gl_buffer_object *
153_mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer);
154
155extern struct gl_buffer_object *
156_mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
157                           const char *caller);
158
159extern struct gl_buffer_object *
160_mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
161                                  const GLuint *buffers,
162                                  GLuint index, const char *caller,
163                                  bool *error);
164
165extern void
166_mesa_delete_buffer_object(struct gl_context *ctx,
167                           struct gl_buffer_object *bufObj);
168
169extern void
170_mesa_reference_buffer_object_(struct gl_context *ctx,
171                               struct gl_buffer_object **ptr,
172                               struct gl_buffer_object *bufObj,
173                               bool shared_binding);
174
175/**
176 * Assign a buffer into a pointer with reference counting. The destination
177 * must be private within a context.
178 */
179static inline void
180_mesa_reference_buffer_object(struct gl_context *ctx,
181                              struct gl_buffer_object **ptr,
182                              struct gl_buffer_object *bufObj)
183{
184   if (*ptr != bufObj)
185      _mesa_reference_buffer_object_(ctx, ptr, bufObj, false);
186}
187
188/**
189 * Assign a buffer into a pointer with reference counting. The destination
190 * must be shareable among multiple contexts.
191 */
192static inline void
193_mesa_reference_buffer_object_shared(struct gl_context *ctx,
194                                     struct gl_buffer_object **ptr,
195                                     struct gl_buffer_object *bufObj)
196{
197   if (*ptr != bufObj)
198      _mesa_reference_buffer_object_(ctx, ptr, bufObj, true);
199}
200
201extern GLuint
202_mesa_total_buffer_object_memory(struct gl_context *ctx);
203
204extern void
205_mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
206                  GLenum target, GLsizeiptr size, const GLvoid *data,
207                  GLenum usage, const char *func);
208
209extern void
210_mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
211                      GLintptr offset, GLsizeiptr size, const GLvoid *data);
212
213extern void
214_mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
215                                struct gl_buffer_object *bufObj);
216
217extern void
218_mesa_ClearBufferSubData_sw(struct gl_context *ctx,
219                            GLintptr offset, GLsizeiptr size,
220                            const GLvoid *clearValue,
221                            GLsizeiptr clearValueSize,
222                            struct gl_buffer_object *bufObj);
223
224void
225_mesa_InternalBindElementBuffer(struct gl_context *ctx,
226                                struct gl_buffer_object *buf);
227
228#endif
229