• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
39 static inline struct pipe_resource *
_mesa_get_bufferobj_reference(struct gl_context * ctx,struct gl_buffer_object * obj)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 
71 void _mesa_bufferobj_subdata(struct gl_context *ctx,
72                           GLintptrARB offset,
73                           GLsizeiptrARB size,
74                           const void * data, struct gl_buffer_object *obj);
75 GLboolean _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);
82 void
83 _mesa_bufferobj_get_subdata(struct gl_context *ctx,
84                             GLintptrARB offset,
85                             GLsizeiptrARB size,
86                             void *data, struct gl_buffer_object *obj);
87 
88 void *_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 
94 void _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);
98 GLboolean _mesa_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj,
99                                  gl_map_buffer_index index);
100 
101 struct gl_buffer_object *
102 _mesa_bufferobj_alloc(struct gl_context *ctx, GLuint id);
103 void
104 _mesa_bufferobj_release_buffer(struct gl_buffer_object *obj);
105 
106 enum 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? */
110 static inline GLboolean
_mesa_bufferobj_mapped(const struct gl_buffer_object * obj,gl_map_buffer_index index)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  */
124 static inline bool
_mesa_check_disallowed_mapping(const struct gl_buffer_object * obj)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 
133 extern void
134 _mesa_init_buffer_objects(struct gl_context *ctx);
135 
136 extern void
137 _mesa_free_buffer_objects(struct gl_context *ctx);
138 
139 extern 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 
145 extern void
146 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx);
147 
148 
149 extern struct gl_buffer_object *
150 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer);
151 
152 extern struct gl_buffer_object *
153 _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer);
154 
155 extern struct gl_buffer_object *
156 _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
157                            const char *caller);
158 
159 extern 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 
165 extern void
166 _mesa_delete_buffer_object(struct gl_context *ctx,
167                            struct gl_buffer_object *bufObj);
168 
169 extern 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  */
179 static inline void
_mesa_reference_buffer_object(struct gl_context * ctx,struct gl_buffer_object ** ptr,struct gl_buffer_object * bufObj)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  */
192 static inline void
_mesa_reference_buffer_object_shared(struct gl_context * ctx,struct gl_buffer_object ** ptr,struct gl_buffer_object * bufObj)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 
201 extern GLuint
202 _mesa_total_buffer_object_memory(struct gl_context *ctx);
203 
204 extern 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 
209 extern void
210 _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
211                       GLintptr offset, GLsizeiptr size, const GLvoid *data);
212 
213 extern void
214 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
215                                 struct gl_buffer_object *bufObj);
216 
217 extern 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 
224 void
225 _mesa_InternalBindElementBuffer(struct gl_context *ctx,
226                                 struct gl_buffer_object *buf);
227 
228 #endif
229