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