blob: 16a1e5e8b3f0344940874df4df7ca6150dc2bb06 [file] [log] [blame]
Brian Paul6061df02003-03-29 17:01:00 +00001/*
2 * Mesa 3-D graphics library
Brian Paul6061df02003-03-29 17:01:00 +00003 *
Briana429a252008-03-21 13:41:00 -06004 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Brian Pauldc0b71f2009-06-02 20:29:57 -06005 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
Brian Paul6061df02003-03-29 17:01:00 +00006 *
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
Kenneth Graunke3d8d5b22013-04-21 13:46:48 -070020 * 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.
Brian Paul6061df02003-03-29 17:01:00 +000024 */
25
26
27/**
28 * \file bufferobj.c
Brian Paul395bcad2009-02-27 12:41:11 -070029 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
Brian Paulaa00d122003-09-15 19:55:10 +000030 * \author Brian Paul, Ian Romanick
Brian Paul6061df02003-03-29 17:01:00 +000031 */
32
Ian Romanickbd4e5dd2012-07-25 16:08:12 -070033#include <stdbool.h>
Brian Paul80fa7fd2014-08-08 07:49:33 -060034#include <inttypes.h> /* for PRId64 macro */
Nicolai Hähnlebc8a6842016-01-11 15:56:22 -050035#include "util/debug.h"
Brian Paul6061df02003-03-29 17:01:00 +000036#include "glheader.h"
Brian Pauld6a9f5b2010-03-20 11:52:12 -060037#include "enums.h"
Ian Romanick0207b472003-09-09 00:10:12 +000038#include "hash.h"
Brian Paulaac73252003-04-09 02:31:35 +000039#include "context.h"
Brian Paul6061df02003-03-29 17:01:00 +000040#include "bufferobj.h"
Timothy Arceriba6eee22017-07-26 16:50:44 +100041#include "externalobjects.h"
Vinson Lee0117da42011-01-05 23:11:54 -080042#include "mtypes.h"
Pi Tabred5f7bc0c2013-12-14 10:32:00 -070043#include "teximage.h"
44#include "glformats.h"
45#include "texstore.h"
Kenneth Graunke3edd2ba2012-06-04 00:51:34 -070046#include "transformfeedback.h"
Mathias Fröhlichf7cb46a2016-06-17 08:09:04 +020047#include "varray.h"
Mathias Fröhlich2313c332018-02-03 15:06:16 +010048#include "util/u_atomic.h"
Dylan Bakerb8577592018-09-12 15:56:30 -070049#include "util/u_memory.h"
Ilia Mirkina1d664a2016-01-04 19:48:08 -050050
Brian Paulc7b872a2003-09-09 13:44:40 +000051
Brian Paul4a7fd632009-06-09 11:53:19 -060052/* Debug flags */
53/*#define VBO_DEBUG*/
54/*#define BOUNDS_CHECK*/
55
56
Ian Romanick0207b472003-09-09 00:10:12 +000057/**
Brian Paulab0651c2015-12-07 18:38:03 -070058 * We count the number of buffer modification calls to check for
59 * inefficient buffer use. This is the number of such calls before we
60 * issue a warning.
61 */
62#define BUFFER_WARNING_CALL_COUNT 4
63
64
65/**
66 * Helper to warn of possible performance issues, such as frequently
Brian Paule1815bc2015-12-09 16:00:55 -070067 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
68 * below.
Brian Paulab0651c2015-12-07 18:38:03 -070069 */
70static void
Brian Paule1815bc2015-12-09 16:00:55 -070071buffer_usage_warning(struct gl_context *ctx, GLuint *id, const char *fmt, ...)
Brian Paulab0651c2015-12-07 18:38:03 -070072{
73 va_list args;
Brian Paulab0651c2015-12-07 18:38:03 -070074
75 va_start(args, fmt);
Mark Janesb8a1a322018-12-06 16:35:43 -080076 _mesa_gl_vdebugf(ctx, id,
77 MESA_DEBUG_SOURCE_API,
78 MESA_DEBUG_TYPE_PERFORMANCE,
79 MESA_DEBUG_SEVERITY_MEDIUM,
80 fmt, args);
Brian Paulab0651c2015-12-07 18:38:03 -070081 va_end(args);
82}
83
Brian Paule1815bc2015-12-09 16:00:55 -070084#define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
85 do { \
86 static GLuint id = 0; \
87 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
88 } while (0)
89
Brian Paulab0651c2015-12-07 18:38:03 -070090
91/**
Brian Paulc552f272010-11-11 14:47:30 -070092 * Used as a placeholder for buffer objects between glGenBuffers() and
93 * glBindBuffer() so that glIsBuffer() can work correctly.
94 */
95static struct gl_buffer_object DummyBufferObject;
96
97
98/**
Brian Pauldd5691e2009-10-27 20:10:30 -060099 * Return pointer to address of a buffer object target.
100 * \param ctx the GL context
101 * \param target the buffer object target to be retrieved.
102 * \return pointer to pointer to the buffer object bound to \c target in the
103 * specified context or \c NULL if \c target is invalid.
104 */
Brian Paul9520f482011-09-30 21:03:42 -0600105static inline struct gl_buffer_object **
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400106get_buffer_target(struct gl_context *ctx, GLenum target)
Brian Pauldd5691e2009-10-27 20:10:30 -0600107{
Simon Ser0bc77bc2020-04-02 22:53:41 +0200108 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0. */
109 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)) {
110 switch (target) {
111 case GL_ARRAY_BUFFER:
112 case GL_ELEMENT_ARRAY_BUFFER:
113 break;
114 case GL_PIXEL_PACK_BUFFER:
115 case GL_PIXEL_UNPACK_BUFFER:
116 if (!ctx->Extensions.EXT_pixel_buffer_object)
117 return NULL;
118 break;
119 default:
120 return NULL;
121 }
122 }
Ian Romanickb0b6b762012-07-25 15:29:48 -0700123
Brian Pauldd5691e2009-10-27 20:10:30 -0600124 switch (target) {
125 case GL_ARRAY_BUFFER_ARB:
Mathias Fröhliche727f8c2018-12-21 18:41:27 +0100126 if (ctx->Array.ArrayBufferObj)
127 ctx->Array.ArrayBufferObj->UsageHistory |= USAGE_ARRAY_BUFFER;
Brian Pauldd5691e2009-10-27 20:10:30 -0600128 return &ctx->Array.ArrayBufferObj;
129 case GL_ELEMENT_ARRAY_BUFFER_ARB:
Mathias Fröhliche727f8c2018-12-21 18:41:27 +0100130 if (ctx->Array.VAO->IndexBufferObj)
131 ctx->Array.VAO->IndexBufferObj->UsageHistory
132 |= USAGE_ELEMENT_ARRAY_BUFFER;
Kenneth Graunkee1b1f2a2014-02-01 19:46:45 -0800133 return &ctx->Array.VAO->IndexBufferObj;
Brian Pauldd5691e2009-10-27 20:10:30 -0600134 case GL_PIXEL_PACK_BUFFER_EXT:
135 return &ctx->Pack.BufferObj;
136 case GL_PIXEL_UNPACK_BUFFER_EXT:
137 return &ctx->Unpack.BufferObj;
138 case GL_COPY_READ_BUFFER:
Ian Romanick4da5f1b2010-09-27 14:29:12 -0700139 return &ctx->CopyReadBuffer;
Brian Pauldd5691e2009-10-27 20:10:30 -0600140 case GL_COPY_WRITE_BUFFER:
Ian Romanick4da5f1b2010-09-27 14:29:12 -0700141 return &ctx->CopyWriteBuffer;
Rafal Mielniczuk2d0ec0c2014-03-27 21:59:04 +0100142 case GL_QUERY_BUFFER:
143 if (_mesa_has_ARB_query_buffer_object(ctx))
144 return &ctx->QueryBuffer;
145 break;
Chris Forbesa95236c2013-11-06 20:03:21 +1300146 case GL_DRAW_INDIRECT_BUFFER:
Timothy Arceri5f90fb42018-06-23 17:09:13 +1000147 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_draw_indirect) ||
Marta Lofstedt2e0179e2015-05-11 15:03:49 +0200148 _mesa_is_gles31(ctx)) {
Chris Forbesa95236c2013-11-06 20:03:21 +1300149 return &ctx->DrawIndirectBuffer;
150 }
151 break;
Ilia Mirkin9327e2d2015-12-31 15:47:17 -0500152 case GL_PARAMETER_BUFFER_ARB:
153 if (_mesa_has_ARB_indirect_parameters(ctx)) {
154 return &ctx->ParameterBuffer;
155 }
156 break;
Jordan Justen12cf91d2015-09-17 10:10:07 -0700157 case GL_DISPATCH_INDIRECT_BUFFER:
158 if (_mesa_has_compute_shaders(ctx)) {
159 return &ctx->DispatchIndirectBuffer;
160 }
161 break;
Brian Paul423860a2010-03-30 19:53:01 -0600162 case GL_TRANSFORM_FEEDBACK_BUFFER:
163 if (ctx->Extensions.EXT_transform_feedback) {
164 return &ctx->TransformFeedback.CurrentBuffer;
165 }
166 break;
Brian Paul874a2c02011-04-05 19:02:07 -0600167 case GL_TEXTURE_BUFFER:
Ilia Mirkinb4c0c512016-02-27 16:16:28 -0500168 if (_mesa_has_ARB_texture_buffer_object(ctx) ||
169 _mesa_has_OES_texture_buffer(ctx)) {
Brian Paul874a2c02011-04-05 19:02:07 -0600170 return &ctx->Texture.BufferObject;
171 }
172 break;
Eric Anholtc5c696e2012-06-18 11:17:04 -0700173 case GL_UNIFORM_BUFFER:
174 if (ctx->Extensions.ARB_uniform_buffer_object) {
175 return &ctx->UniformBuffer;
176 }
177 break;
Iago Toral Quirogacd509062015-03-19 10:15:30 +0100178 case GL_SHADER_STORAGE_BUFFER:
179 if (ctx->Extensions.ARB_shader_storage_buffer_object) {
180 return &ctx->ShaderStorageBuffer;
181 }
182 break;
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -0700183 case GL_ATOMIC_COUNTER_BUFFER:
184 if (ctx->Extensions.ARB_shader_atomic_counters) {
185 return &ctx->AtomicBuffer;
186 }
187 break;
Marek Olšák11ebb032015-02-10 01:39:41 +0100188 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD:
189 if (ctx->Extensions.AMD_pinned_memory) {
190 return &ctx->ExternalVirtualMemoryBuffer;
191 }
192 break;
Brian Pauldd5691e2009-10-27 20:10:30 -0600193 default:
194 return NULL;
195 }
196 return NULL;
197}
198
199
200/**
Ian Romanick0207b472003-09-09 00:10:12 +0000201 * Get the buffer object bound to the specified target in a GL context.
Brian Pauldd5691e2009-10-27 20:10:30 -0600202 * \param ctx the GL context
203 * \param target the buffer object target to be retrieved.
Pi Tabred7d946532013-12-14 10:32:00 -0700204 * \param error the GL error to record if target is illegal.
Brian Pauldd5691e2009-10-27 20:10:30 -0600205 * \return pointer to the buffer object bound to \c target in the
Brian Paul4277ea42006-08-25 22:06:02 +0000206 * specified context or \c NULL if \c target is invalid.
Ian Romanick0207b472003-09-09 00:10:12 +0000207 */
Brian Paul9520f482011-09-30 21:03:42 -0600208static inline struct gl_buffer_object *
Pi Tabred7d946532013-12-14 10:32:00 -0700209get_buffer(struct gl_context *ctx, const char *func, GLenum target,
210 GLenum error)
Ian Romanick0207b472003-09-09 00:10:12 +0000211{
Brian Pauldd5691e2009-10-27 20:10:30 -0600212 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
Eric Anholtf20fb802012-01-25 16:30:26 -0800213
214 if (!bufObj) {
215 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
216 return NULL;
217 }
218
Marek Olšák10beee82020-03-21 23:56:07 -0400219 if (!*bufObj) {
Pi Tabred7d946532013-12-14 10:32:00 -0700220 _mesa_error(ctx, error, "%s(no buffer bound)", func);
Eric Anholtf20fb802012-01-25 16:30:26 -0800221 return NULL;
222 }
223
224 return *bufObj;
Ian Romanick0207b472003-09-09 00:10:12 +0000225}
226
227
Kenneth Graunke8907b6a2012-11-17 21:23:28 -0800228/**
229 * Convert a GLbitfield describing the mapped buffer access flags
230 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
231 */
232static GLenum
233simplified_access_mode(struct gl_context *ctx, GLbitfield access)
Ian Romanickf0ea4672012-01-17 16:24:05 -0800234{
Kenneth Graunke8907b6a2012-11-17 21:23:28 -0800235 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
236 if ((access & rwFlags) == rwFlags)
237 return GL_READ_WRITE;
238 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
239 return GL_READ_ONLY;
240 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
241 return GL_WRITE_ONLY;
242
243 /* Otherwise, AccessFlags is zero (the default state).
244 *
245 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
Ian Romanickf0ea4672012-01-17 16:24:05 -0800246 *
247 * Name Type Initial Value Legal Values
248 * ... ... ... ...
249 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
250 * READ_WRITE
251 *
252 * However, table 6.8 in the GL_OES_mapbuffer extension says:
253 *
254 * Get Value Type Get Command Value Description
255 * --------- ---- ----------- ----- -----------
256 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
257 *
258 * The difference is because GL_OES_mapbuffer only supports mapping buffers
259 * write-only.
260 */
Kenneth Graunke8907b6a2012-11-17 21:23:28 -0800261 assert(access == 0);
Ian Romanickf0ea4672012-01-17 16:24:05 -0800262
Kenneth Graunke8907b6a2012-11-17 21:23:28 -0800263 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
Brian Paule75b2832009-06-08 17:02:36 -0600264}
265
266
267/**
Pi Tabred84c4ea52013-12-14 10:32:00 -0700268 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
269 * given range.
270 * The regions do not overlap if and only if the end of the given
271 * region is before the mapped region or the start of the given region
272 * is after the mapped region.
273 *
274 * \param obj Buffer object target on which to operate.
275 * \param offset Offset of the first byte of the subdata range.
276 * \param size Size, in bytes, of the subdata range.
277 * \return true if ranges overlap, false otherwise
278 *
279 */
280static bool
281bufferobj_range_mapped(const struct gl_buffer_object *obj,
282 GLintptr offset, GLsizeiptr size)
283{
Marek Olšákdca35022014-02-06 19:24:23 +0100284 if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
Pi Tabred84c4ea52013-12-14 10:32:00 -0700285 const GLintptr end = offset + size;
Marek Olšákdca35022014-02-06 19:24:23 +0100286 const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
287 obj->Mappings[MAP_USER].Length;
Pi Tabred84c4ea52013-12-14 10:32:00 -0700288
Marek Olšákdca35022014-02-06 19:24:23 +0100289 if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
Pi Tabred84c4ea52013-12-14 10:32:00 -0700290 return true;
291 }
292 }
293 return false;
294}
295
296
297/**
Ian Romanick0207b472003-09-09 00:10:12 +0000298 * Tests the subdata range parameters and sets the GL error code for
Pi Tabred84c4ea52013-12-14 10:32:00 -0700299 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
300 * \c glClearBufferSubData.
Ian Romanick0207b472003-09-09 00:10:12 +0000301 *
302 * \param ctx GL context.
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800303 * \param bufObj The buffer object.
Ian Romanick0207b472003-09-09 00:10:12 +0000304 * \param offset Offset of the first byte of the subdata range.
305 * \param size Size, in bytes, of the subdata range.
Pi Tabred84c4ea52013-12-14 10:32:00 -0700306 * \param mappedRange If true, checks if an overlapping range is mapped.
307 * If false, checks if buffer is mapped.
Brian Paul6f172f72006-06-02 22:51:45 +0000308 * \param caller Name of calling function for recording errors.
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800309 * \return false if error, true otherwise
Ian Romanick0207b472003-09-09 00:10:12 +0000310 *
Pi Tabred84c4ea52013-12-14 10:32:00 -0700311 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
Ian Romanick0207b472003-09-09 00:10:12 +0000312 */
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800313static bool
314buffer_object_subdata_range_good(struct gl_context *ctx,
Brian Paul4879b762015-09-17 09:45:42 -0600315 const struct gl_buffer_object *bufObj,
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800316 GLintptr offset, GLsizeiptr size,
317 bool mappedRange, const char *caller)
Ian Romanick0207b472003-09-09 00:10:12 +0000318{
Ian Romanick0207b472003-09-09 00:10:12 +0000319 if (size < 0) {
Brian Paul6f172f72006-06-02 22:51:45 +0000320 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800321 return false;
Ian Romanick0207b472003-09-09 00:10:12 +0000322 }
323
324 if (offset < 0) {
Brian Paul6f172f72006-06-02 22:51:45 +0000325 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800326 return false;
Ian Romanick0207b472003-09-09 00:10:12 +0000327 }
328
Brian7e85b0a2007-01-23 12:50:08 -0700329 if (offset + size > bufObj->Size) {
Ian Romanick0207b472003-09-09 00:10:12 +0000330 _mesa_error(ctx, GL_INVALID_VALUE,
Pi Tabred84c4ea52013-12-14 10:32:00 -0700331 "%s(offset %lu + size %lu > buffer size %lu)", caller,
Brian Paulb330f1f2012-01-04 14:15:53 -0700332 (unsigned long) offset,
333 (unsigned long) size,
334 (unsigned long) bufObj->Size);
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800335 return false;
Ian Romanick0207b472003-09-09 00:10:12 +0000336 }
Pi Tabred84c4ea52013-12-14 10:32:00 -0700337
Marek Olšákdca35022014-02-06 19:24:23 +0100338 if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800339 return true;
Marek Olšákd26a0652014-01-27 21:36:53 +0100340
Pi Tabred84c4ea52013-12-14 10:32:00 -0700341 if (mappedRange) {
342 if (bufferobj_range_mapped(bufObj, offset, size)) {
Laura Ekstrand9cb732b2015-02-11 11:06:42 -0800343 _mesa_error(ctx, GL_INVALID_OPERATION,
344 "%s(range is mapped without persistent bit)",
345 caller);
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800346 return false;
Pi Tabred84c4ea52013-12-14 10:32:00 -0700347 }
348 }
349 else {
Marek Olšákdca35022014-02-06 19:24:23 +0100350 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
Laura Ekstrand9cb732b2015-02-11 11:06:42 -0800351 _mesa_error(ctx, GL_INVALID_OPERATION,
352 "%s(buffer is mapped without persistent bit)",
353 caller);
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800354 return false;
Pi Tabred84c4ea52013-12-14 10:32:00 -0700355 }
Ian Romanick0207b472003-09-09 00:10:12 +0000356 }
357
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800358 return true;
Ian Romanick0207b472003-09-09 00:10:12 +0000359}
360
361
362/**
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700363 * Test the format and type parameters and set the GL error code for
Andres Gomeza43596d2019-02-12 11:19:49 +0200364 * \c glClearBufferData, \c glClearNamedBufferData, \c glClearBufferSubData
365 * and \c glClearNamedBufferSubData.
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700366 *
367 * \param ctx GL context.
368 * \param internalformat Format to which the data is to be converted.
369 * \param format Format of the supplied data.
370 * \param type Type of the supplied data.
371 * \param caller Name of calling function for recording errors.
Mark Mueller71fe9432014-01-04 14:11:43 -0800372 * \return If internalformat, format and type are legal the mesa_format
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700373 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
374 *
Andres Gomeza43596d2019-02-12 11:19:49 +0200375 * \sa glClearBufferData, glClearNamedBufferData, glClearBufferSubData and
376 * glClearNamedBufferSubData.
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700377 */
Mark Mueller71fe9432014-01-04 14:11:43 -0800378static mesa_format
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700379validate_clear_buffer_format(struct gl_context *ctx,
380 GLenum internalformat,
381 GLenum format, GLenum type,
382 const char *caller)
383{
Mark Mueller71fe9432014-01-04 14:11:43 -0800384 mesa_format mesaFormat;
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700385 GLenum errorFormatType;
386
387 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
388 if (mesaFormat == MESA_FORMAT_NONE) {
389 _mesa_error(ctx, GL_INVALID_ENUM,
390 "%s(invalid internalformat)", caller);
391 return MESA_FORMAT_NONE;
392 }
393
394 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
395 * EXT_texture_integer there is no conversion between integer and
396 * non-integer formats
397 */
398 if (_mesa_is_enum_format_signed_int(format) !=
399 _mesa_is_format_integer_color(mesaFormat)) {
400 _mesa_error(ctx, GL_INVALID_OPERATION,
401 "%s(integer vs non-integer)", caller);
402 return MESA_FORMAT_NONE;
403 }
404
405 if (!_mesa_is_color_format(format)) {
Andres Gomeza43596d2019-02-12 11:19:49 +0200406 _mesa_error(ctx, GL_INVALID_VALUE,
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700407 "%s(format is not a color format)", caller);
408 return MESA_FORMAT_NONE;
409 }
410
411 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
412 if (errorFormatType != GL_NO_ERROR) {
Andres Gomeza43596d2019-02-12 11:19:49 +0200413 _mesa_error(ctx, GL_INVALID_VALUE,
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700414 "%s(invalid format or type)", caller);
415 return MESA_FORMAT_NONE;
416 }
417
418 return mesaFormat;
419}
420
421
422/**
423 * Convert user-specified clear value to the specified internal format.
424 *
425 * \param ctx GL context.
426 * \param internalformat Format to which the data is converted.
427 * \param clearValue Points to the converted clear value.
428 * \param format Format of the supplied data.
429 * \param type Type of the supplied data.
430 * \param data Data which is to be converted to internalformat.
431 * \param caller Name of calling function for recording errors.
432 * \return true if data could be converted, false otherwise.
433 *
434 * \sa glClearBufferData, glClearBufferSubData
435 */
436static bool
437convert_clear_buffer_data(struct gl_context *ctx,
Mark Mueller71fe9432014-01-04 14:11:43 -0800438 mesa_format internalformat,
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700439 GLubyte *clearValue, GLenum format, GLenum type,
440 const GLvoid *data, const char *caller)
441{
442 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
443
444 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
445 0, &clearValue, 1, 1, 1,
446 format, type, data, &ctx->Unpack)) {
447 return true;
448 }
449 else {
450 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
451 return false;
452 }
453}
454
455
456/**
Ian Romanick0207b472003-09-09 00:10:12 +0000457 * Allocate and initialize a new buffer object.
Brian Paul8faed712015-09-15 14:04:58 -0600458 *
Brian Paul395bcad2009-02-27 12:41:11 -0700459 * Default callback for the \c dd_function_table::NewBufferObject() hook.
Ian Romanick0207b472003-09-09 00:10:12 +0000460 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600461static struct gl_buffer_object *
Kenneth Graunke63c65092014-10-15 13:00:39 -0700462_mesa_new_buffer_object(struct gl_context *ctx, GLuint name)
Ian Romanick0207b472003-09-09 00:10:12 +0000463{
Timothy Arceri2c34b492017-07-27 08:53:08 +1000464 struct gl_buffer_object *obj = MALLOC_STRUCT(gl_buffer_object);
465 if (!obj)
466 return NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000467
Kenneth Graunke63c65092014-10-15 13:00:39 -0700468 _mesa_initialize_buffer_object(ctx, obj, name);
Ian Romanick0207b472003-09-09 00:10:12 +0000469 return obj;
470}
471
472
473/**
Brian Paul148a2842003-09-17 03:40:11 +0000474 * Delete a buffer object.
Brian Paul8faed712015-09-15 14:04:58 -0600475 *
Brian Paul395bcad2009-02-27 12:41:11 -0700476 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
Brian Paul148a2842003-09-17 03:40:11 +0000477 */
Nicolai Hähnle6aed0832016-01-05 21:47:04 -0500478void
Brian Paul79f42812011-03-19 14:17:40 -0600479_mesa_delete_buffer_object(struct gl_context *ctx,
480 struct gl_buffer_object *bufObj)
Brian Paul148a2842003-09-17 03:40:11 +0000481{
Brian Paula6c423d2004-08-25 15:59:48 +0000482 (void) ctx;
483
Nicolai Hähnle6b057f82016-01-11 14:54:53 -0500484 vbo_delete_minmax_cache(bufObj);
Dylan Bakerc495c3a2018-09-12 16:31:13 -0700485 align_free(bufObj->Data);
Brian Paul37c74af2008-09-04 14:58:02 -0600486
487 /* assign strange values here to help w/ debugging */
488 bufObj->RefCount = -1000;
489 bufObj->Name = ~0;
490
Mathias Fröhlich2313c332018-02-03 15:06:16 +0100491 simple_mtx_destroy(&bufObj->MinMaxCacheMutex);
Timothy Arceri6d8dd592013-08-26 17:16:08 +1000492 free(bufObj->Label);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500493 free(bufObj);
Brian Paul148a2842003-09-17 03:40:11 +0000494}
495
496
Brian Paul37c74af2008-09-04 14:58:02 -0600497
498/**
499 * Set ptr to bufObj w/ reference counting.
Brian Paul62149632011-07-14 08:09:21 -0600500 * This is normally only called from the _mesa_reference_buffer_object() macro
501 * when there's a real pointer change.
Brian Paul37c74af2008-09-04 14:58:02 -0600502 */
Ian Romanickee34e6e2006-06-12 16:26:29 +0000503void
Brian Paul62149632011-07-14 08:09:21 -0600504_mesa_reference_buffer_object_(struct gl_context *ctx,
505 struct gl_buffer_object **ptr,
506 struct gl_buffer_object *bufObj)
Ian Romanickee34e6e2006-06-12 16:26:29 +0000507{
Brian Paul37c74af2008-09-04 14:58:02 -0600508 if (*ptr) {
Brian Paul105c8522009-05-07 13:10:48 -0600509 /* Unreference the old buffer */
Brian Paul37c74af2008-09-04 14:58:02 -0600510 struct gl_buffer_object *oldObj = *ptr;
511
Mathias Fröhlich2313c332018-02-03 15:06:16 +0100512 if (p_atomic_dec_zero(&oldObj->RefCount)) {
Matt Turnerbfcdb842015-02-20 20:18:47 -0800513 assert(ctx->Driver.DeleteBuffer);
Brian Paul37c74af2008-09-04 14:58:02 -0600514 ctx->Driver.DeleteBuffer(ctx, oldObj);
Ian Romanickee34e6e2006-06-12 16:26:29 +0000515 }
Brian Paul37c74af2008-09-04 14:58:02 -0600516
517 *ptr = NULL;
518 }
Matt Turnerbfcdb842015-02-20 20:18:47 -0800519 assert(!*ptr);
Brian Paul37c74af2008-09-04 14:58:02 -0600520
521 if (bufObj) {
Brian Paul105c8522009-05-07 13:10:48 -0600522 /* reference new buffer */
Mathias Fröhlich2313c332018-02-03 15:06:16 +0100523 p_atomic_inc(&bufObj->RefCount);
Timothy Arceri622a68e2017-04-21 13:29:46 +1000524 *ptr = bufObj;
Ian Romanickee34e6e2006-06-12 16:26:29 +0000525 }
526}
527
528
Brian Paul148a2842003-09-17 03:40:11 +0000529/**
Nicolai Hähnlebc8a6842016-01-11 15:56:22 -0500530 * Get the value of MESA_NO_MINMAX_CACHE.
531 */
532static bool
533get_no_minmax_cache()
534{
535 static bool read = false;
536 static bool disable = false;
537
538 if (!read) {
539 disable = env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
540 read = true;
541 }
542
543 return disable;
544}
545
546
547/**
Ian Romanick0207b472003-09-09 00:10:12 +0000548 * Initialize a buffer object to default values.
549 */
Ian Romanick0207b472003-09-09 00:10:12 +0000550void
Kenneth Graunke63c65092014-10-15 13:00:39 -0700551_mesa_initialize_buffer_object(struct gl_context *ctx,
552 struct gl_buffer_object *obj,
553 GLuint name)
Ian Romanick0207b472003-09-09 00:10:12 +0000554{
Brian Paul6bf1ea82010-02-19 08:32:36 -0700555 memset(obj, 0, sizeof(struct gl_buffer_object));
Ian Romanick0207b472003-09-09 00:10:12 +0000556 obj->RefCount = 1;
557 obj->Name = name;
Brian Paul85ad44b2004-02-13 14:04:26 +0000558 obj->Usage = GL_STATIC_DRAW_ARB;
Nicolai Hähnlebc8a6842016-01-11 15:56:22 -0500559
Mathias Fröhlich2313c332018-02-03 15:06:16 +0100560 simple_mtx_init(&obj->MinMaxCacheMutex, mtx_plain);
Nicolai Hähnlebc8a6842016-01-11 15:56:22 -0500561 if (get_no_minmax_cache())
562 obj->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
Ian Romanick0207b472003-09-09 00:10:12 +0000563}
564
565
Brian Paul223654b2012-04-06 15:44:56 -0600566
567/**
568 * Callback called from _mesa_HashWalk()
569 */
570static void
Ian Romanickcc6dcc62020-10-12 16:52:09 -0700571count_buffer_size(void *data, void *userData)
Brian Paul223654b2012-04-06 15:44:56 -0600572{
573 const struct gl_buffer_object *bufObj =
574 (const struct gl_buffer_object *) data;
575 GLuint *total = (GLuint *) userData;
576
577 *total = *total + bufObj->Size;
578}
579
580
581/**
582 * Compute total size (in bytes) of all buffer objects for the given context.
583 * For debugging purposes.
584 */
585GLuint
586_mesa_total_buffer_object_memory(struct gl_context *ctx)
587{
588 GLuint total = 0;
589
590 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
591
592 return total;
593}
594
595
Ian Romanick0207b472003-09-09 00:10:12 +0000596/**
Ian Romanick0207b472003-09-09 00:10:12 +0000597 * Allocate space for and store data in a buffer object. Any data that was
598 * previously stored in the buffer object is lost. If \c data is \c NULL,
599 * memory will be allocated, but no copy will occur.
600 *
Brian Paul395bcad2009-02-27 12:41:11 -0700601 * This is the default callback for \c dd_function_table::BufferData()
602 * Note that all GL error checking will have been done already.
Ian Romanick0207b472003-09-09 00:10:12 +0000603 *
604 * \param ctx GL context.
605 * \param target Buffer object target on which to operate.
606 * \param size Size, in bytes, of the new data store.
607 * \param data Pointer to the data to store in the buffer object. This
608 * pointer may be \c NULL.
609 * \param usage Hints about how the data will be used.
610 * \param bufObj Object to be used.
611 *
Brian Paul2f6d2a92009-09-03 09:41:41 -0600612 * \return GL_TRUE for success, GL_FALSE for failure
Ian Romanick0207b472003-09-09 00:10:12 +0000613 * \sa glBufferDataARB, dd_function_table::BufferData.
614 */
Brian Paul2f6d2a92009-09-03 09:41:41 -0600615static GLboolean
Brian Paul35ea66a2018-09-17 21:05:04 -0600616buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
Laura Ekstrandcb568352015-02-09 17:57:46 -0800617 const GLvoid *data, GLenum usage, GLenum storageFlags,
618 struct gl_buffer_object *bufObj)
Ian Romanick0207b472003-09-09 00:10:12 +0000619{
620 void * new_data;
621
Siavash Eliasif772d512013-11-28 12:26:37 +0330622 (void) target;
Ian Romanick0207b472003-09-09 00:10:12 +0000623
Dylan Bakerc495c3a2018-09-12 16:31:13 -0700624 align_free( bufObj->Data );
Siavash Eliasif772d512013-11-28 12:26:37 +0330625
Dylan Bakerc495c3a2018-09-12 16:31:13 -0700626 new_data = align_malloc( size, ctx->Const.MinMapBufferAlignment );
Brian Paula7892522004-11-22 20:01:25 +0000627 if (new_data) {
Brian Paule4fcea22003-09-19 15:38:15 +0000628 bufObj->Data = (GLubyte *) new_data;
Brian Paul148a2842003-09-17 03:40:11 +0000629 bufObj->Size = size;
630 bufObj->Usage = usage;
Marek Olšák7e548d02014-01-27 21:15:19 +0100631 bufObj->StorageFlags = storageFlags;
Ian Romanick0207b472003-09-09 00:10:12 +0000632
Brian Paula7892522004-11-22 20:01:25 +0000633 if (data) {
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800634 memcpy( bufObj->Data, data, size );
Ian Romanick0207b472003-09-09 00:10:12 +0000635 }
Brian Paul2f6d2a92009-09-03 09:41:41 -0600636
637 return GL_TRUE;
638 }
639 else {
640 return GL_FALSE;
Ian Romanick0207b472003-09-09 00:10:12 +0000641 }
642}
643
644
645/**
646 * Replace data in a subrange of buffer object. If the data range
647 * specified by \c size + \c offset extends beyond the end of the buffer or
648 * if \c data is \c NULL, no copy is performed.
649 *
Brian Paul395bcad2009-02-27 12:41:11 -0700650 * This is the default callback for \c dd_function_table::BufferSubData()
651 * Note that all GL error checking will have been done already.
Ian Romanick0207b472003-09-09 00:10:12 +0000652 *
653 * \param ctx GL context.
Ian Romanick0207b472003-09-09 00:10:12 +0000654 * \param offset Offset of the first byte to be modified.
655 * \param size Size, in bytes, of the data range.
656 * \param data Pointer to the data to store in the buffer object.
657 * \param bufObj Object to be used.
658 *
659 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
660 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600661static void
Brian Paul35ea66a2018-09-17 21:05:04 -0600662buffer_sub_data_fallback(struct gl_context *ctx, GLintptrARB offset,
663 GLsizeiptrARB size, const GLvoid *data,
Laura Ekstrand566ccdf2015-01-13 11:28:17 -0800664 struct gl_buffer_object *bufObj)
Ian Romanick0207b472003-09-09 00:10:12 +0000665{
Ian Romanick92f3fca2011-08-21 17:23:58 -0700666 (void) ctx;
Brian Paula6c423d2004-08-25 15:59:48 +0000667
Brian Paul76785cb2006-09-21 22:59:50 +0000668 /* this should have been caught in _mesa_BufferSubData() */
Matt Turnerbfcdb842015-02-20 20:18:47 -0800669 assert(size + offset <= bufObj->Size);
Brian Paul76785cb2006-09-21 22:59:50 +0000670
671 if (bufObj->Data) {
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800672 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
Ian Romanick0207b472003-09-09 00:10:12 +0000673 }
674}
675
676
677/**
678 * Retrieve data from a subrange of buffer object. If the data range
679 * specified by \c size + \c offset extends beyond the end of the buffer or
680 * if \c data is \c NULL, no copy is performed.
681 *
Brian Paul395bcad2009-02-27 12:41:11 -0700682 * This is the default callback for \c dd_function_table::GetBufferSubData()
683 * Note that all GL error checking will have been done already.
Ian Romanick0207b472003-09-09 00:10:12 +0000684 *
685 * \param ctx GL context.
686 * \param target Buffer object target on which to operate.
Brian Paul395bcad2009-02-27 12:41:11 -0700687 * \param offset Offset of the first byte to be fetched.
Ian Romanick0207b472003-09-09 00:10:12 +0000688 * \param size Size, in bytes, of the data range.
Brian Paul395bcad2009-02-27 12:41:11 -0700689 * \param data Destination for data
Ian Romanick0207b472003-09-09 00:10:12 +0000690 * \param bufObj Object to be used.
691 *
692 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
693 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600694static void
Timothy Arceri5e86bfa2017-05-04 14:32:28 +1000695buffer_get_subdata(struct gl_context *ctx, GLintptrARB offset,
696 GLsizeiptrARB size, GLvoid *data,
697 struct gl_buffer_object *bufObj )
Ian Romanick0207b472003-09-09 00:10:12 +0000698{
Ian Romanick6c8aa342011-08-21 17:30:35 -0700699 (void) ctx;
Brian Paula6c423d2004-08-25 15:59:48 +0000700
Brian Paulc1f2f902005-03-03 02:05:33 +0000701 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800702 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
Ian Romanick0207b472003-09-09 00:10:12 +0000703 }
704}
705
706
707/**
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700708 * Clear a subrange of the buffer object with copies of the supplied data.
709 * If data is NULL the buffer is filled with zeros.
710 *
711 * This is the default callback for \c dd_function_table::ClearBufferSubData()
712 * Note that all GL error checking will have been done already.
713 *
714 * \param ctx GL context.
715 * \param offset Offset of the first byte to be cleared.
716 * \param size Size, in bytes, of the to be cleared range.
717 * \param clearValue Source of the data.
718 * \param clearValueSize Size, in bytes, of the supplied data.
719 * \param bufObj Object to be cleared.
720 *
721 * \sa glClearBufferSubData, glClearBufferData and
722 * dd_function_table::ClearBufferSubData.
723 */
Ilia Mirkinf5ba1a12014-03-25 17:02:48 -0400724void
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -0800725_mesa_ClearBufferSubData_sw(struct gl_context *ctx,
726 GLintptr offset, GLsizeiptr size,
727 const GLvoid *clearValue,
728 GLsizeiptr clearValueSize,
729 struct gl_buffer_object *bufObj)
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700730{
731 GLsizeiptr i;
732 GLubyte *dest;
733
Matt Turnerbfcdb842015-02-20 20:18:47 -0800734 assert(ctx->Driver.MapBufferRange);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700735 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
736 GL_MAP_WRITE_BIT |
737 GL_MAP_INVALIDATE_RANGE_BIT,
Marek Olšákdca35022014-02-06 19:24:23 +0100738 bufObj, MAP_INTERNAL);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700739
740 if (!dest) {
741 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
742 return;
743 }
744
745 if (clearValue == NULL) {
746 /* Clear with zeros, per the spec */
747 memset(dest, 0, size);
Marek Olšákdca35022014-02-06 19:24:23 +0100748 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700749 return;
750 }
751
752 for (i = 0; i < size/clearValueSize; ++i) {
753 memcpy(dest, clearValue, clearValueSize);
754 dest += clearValueSize;
755 }
756
Marek Olšákdca35022014-02-06 19:24:23 +0100757 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -0700758}
759
760
761/**
Brian Paule75b2832009-06-08 17:02:36 -0600762 * Default fallback for \c dd_function_table::MapBufferRange().
763 * Called via glMapBufferRange().
764 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600765static void *
Laura Ekstrand4f513bc2015-01-14 12:44:39 -0800766map_buffer_range_fallback(struct gl_context *ctx, GLintptr offset,
767 GLsizeiptr length, GLbitfield access,
768 struct gl_buffer_object *bufObj,
769 gl_map_buffer_index index)
Brian Paule75b2832009-06-08 17:02:36 -0600770{
771 (void) ctx;
Marek Olšákdca35022014-02-06 19:24:23 +0100772 assert(!_mesa_bufferobj_mapped(bufObj, index));
Brian Paule75b2832009-06-08 17:02:36 -0600773 /* Just return a direct pointer to the data */
Marek Olšákdca35022014-02-06 19:24:23 +0100774 bufObj->Mappings[index].Pointer = bufObj->Data + offset;
775 bufObj->Mappings[index].Length = length;
776 bufObj->Mappings[index].Offset = offset;
777 bufObj->Mappings[index].AccessFlags = access;
778 return bufObj->Mappings[index].Pointer;
Brian Paule75b2832009-06-08 17:02:36 -0600779}
780
781
782/**
783 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
784 * Called via glFlushMappedBufferRange().
785 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600786static void
Laura Ekstrandee5fae62015-01-14 17:01:20 -0800787flush_mapped_buffer_range_fallback(struct gl_context *ctx,
788 GLintptr offset, GLsizeiptr length,
789 struct gl_buffer_object *obj,
790 gl_map_buffer_index index)
Brian Paule75b2832009-06-08 17:02:36 -0600791{
792 (void) ctx;
Brian Paule75b2832009-06-08 17:02:36 -0600793 (void) offset;
794 (void) length;
795 (void) obj;
Ian Romanick882aab02015-08-26 13:50:04 +0100796 (void) index;
Brian Paule75b2832009-06-08 17:02:36 -0600797 /* no-op */
798}
799
800
801/**
Laura Ekstrandf7f5df92015-01-14 14:52:01 -0800802 * Default callback for \c dd_function_table::UnmapBuffer().
Brian Paul2daa4132004-10-31 00:17:42 +0000803 *
804 * The input parameters will have been already tested for errors.
805 *
806 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
807 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600808static GLboolean
Laura Ekstrandf7f5df92015-01-14 14:52:01 -0800809unmap_buffer_fallback(struct gl_context *ctx, struct gl_buffer_object *bufObj,
810 gl_map_buffer_index index)
Brian Paul2daa4132004-10-31 00:17:42 +0000811{
812 (void) ctx;
Brian Paul2daa4132004-10-31 00:17:42 +0000813 /* XXX we might assert here that bufObj->Pointer is non-null */
Marek Olšákdca35022014-02-06 19:24:23 +0100814 bufObj->Mappings[index].Pointer = NULL;
815 bufObj->Mappings[index].Length = 0;
816 bufObj->Mappings[index].Offset = 0;
817 bufObj->Mappings[index].AccessFlags = 0x0;
Brian Paul2daa4132004-10-31 00:17:42 +0000818 return GL_TRUE;
Ian Romanick0207b472003-09-09 00:10:12 +0000819}
820
Brian Paul6061df02003-03-29 17:01:00 +0000821
Brian Paul148a2842003-09-17 03:40:11 +0000822/**
Brian Pauldc0b71f2009-06-02 20:29:57 -0600823 * Default fallback for \c dd_function_table::CopyBufferSubData().
Kenneth Graunke306c9f02012-06-08 22:03:50 -0700824 * Called via glCopyBufferSubData().
Brian Pauldc0b71f2009-06-02 20:29:57 -0600825 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600826static void
Laura Ekstrand4adaad52015-01-13 13:28:08 -0800827copy_buffer_sub_data_fallback(struct gl_context *ctx,
828 struct gl_buffer_object *src,
829 struct gl_buffer_object *dst,
830 GLintptr readOffset, GLintptr writeOffset,
831 GLsizeiptr size)
Brian Pauldc0b71f2009-06-02 20:29:57 -0600832{
José Fonseca34091182012-01-27 21:54:12 +0000833 GLubyte *srcPtr, *dstPtr;
Brian Pauldc0b71f2009-06-02 20:29:57 -0600834
Eric Anholt531e4442012-01-25 16:29:28 -0800835 if (src == dst) {
836 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
837 GL_MAP_READ_BIT |
Marek Olšákdca35022014-02-06 19:24:23 +0100838 GL_MAP_WRITE_BIT, src,
839 MAP_INTERNAL);
Eric Anholt531e4442012-01-25 16:29:28 -0800840
841 if (!srcPtr)
842 return;
843
844 srcPtr += readOffset;
845 dstPtr += writeOffset;
846 } else {
847 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
Marek Olšákdca35022014-02-06 19:24:23 +0100848 GL_MAP_READ_BIT, src,
849 MAP_INTERNAL);
Eric Anholt531e4442012-01-25 16:29:28 -0800850 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
851 (GL_MAP_WRITE_BIT |
Marek Olšákdca35022014-02-06 19:24:23 +0100852 GL_MAP_INVALIDATE_RANGE_BIT), dst,
853 MAP_INTERNAL);
Eric Anholt531e4442012-01-25 16:29:28 -0800854 }
Brian Pauldc0b71f2009-06-02 20:29:57 -0600855
Brian Paul4c502e02012-01-07 14:16:27 -0700856 /* Note: the src and dst regions will never overlap. Trying to do so
857 * would generate GL_INVALID_VALUE earlier.
858 */
Brian Pauldc0b71f2009-06-02 20:29:57 -0600859 if (srcPtr && dstPtr)
Brian Paul6aed6262012-01-04 14:53:55 -0700860 memcpy(dstPtr, srcPtr, size);
Brian Pauldc0b71f2009-06-02 20:29:57 -0600861
Marek Olšákdca35022014-02-06 19:24:23 +0100862 ctx->Driver.UnmapBuffer(ctx, src, MAP_INTERNAL);
Eric Anholt531e4442012-01-25 16:29:28 -0800863 if (dst != src)
Marek Olšákdca35022014-02-06 19:24:23 +0100864 ctx->Driver.UnmapBuffer(ctx, dst, MAP_INTERNAL);
Brian Pauldc0b71f2009-06-02 20:29:57 -0600865}
866
867
868
869/**
Brian Paul148a2842003-09-17 03:40:11 +0000870 * Initialize the state associated with buffer objects
871 */
872void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400873_mesa_init_buffer_objects( struct gl_context *ctx )
Brian Paul148a2842003-09-17 03:40:11 +0000874{
Eric Anholt5527c2d2012-06-14 16:37:22 -0700875 GLuint i;
876
Brian Paulc552f272010-11-11 14:47:30 -0700877 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
Mathias Fröhlich2313c332018-02-03 15:06:16 +0100878 simple_mtx_init(&DummyBufferObject.MinMaxCacheMutex, mtx_plain);
Brian Paulc552f272010-11-11 14:47:30 -0700879 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
880
Marek Olšák15a4b6d2013-05-14 17:58:32 +0200881 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
Eric Anholt5527c2d2012-06-14 16:37:22 -0700882 _mesa_reference_buffer_object(ctx,
883 &ctx->UniformBufferBindings[i].BufferObject,
Marek Olšák54525802020-03-21 23:47:08 -0400884 NULL);
Eric Anholt5527c2d2012-06-14 16:37:22 -0700885 ctx->UniformBufferBindings[i].Offset = -1;
886 ctx->UniformBufferBindings[i].Size = -1;
887 }
Aditya Atlurif455f342014-07-24 15:18:49 -0400888
Iago Toral Quiroga98a1a2c72015-03-19 11:50:51 +0100889 for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) {
890 _mesa_reference_buffer_object(ctx,
891 &ctx->ShaderStorageBufferBindings[i].BufferObject,
Marek Olšák54525802020-03-21 23:47:08 -0400892 NULL);
Iago Toral Quiroga98a1a2c72015-03-19 11:50:51 +0100893 ctx->ShaderStorageBufferBindings[i].Offset = -1;
894 ctx->ShaderStorageBufferBindings[i].Size = -1;
895 }
896
Aditya Atlurif455f342014-07-24 15:18:49 -0400897 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
898 _mesa_reference_buffer_object(ctx,
899 &ctx->AtomicBufferBindings[i].BufferObject,
Marek Olšák54525802020-03-21 23:47:08 -0400900 NULL);
Marta Lofstedtdd9d2962015-08-13 12:59:40 +0200901 ctx->AtomicBufferBindings[i].Offset = 0;
902 ctx->AtomicBufferBindings[i].Size = 0;
Aditya Atlurif455f342014-07-24 15:18:49 -0400903 }
Brian Paul148a2842003-09-17 03:40:11 +0000904}
905
Brian Paul395bcad2009-02-27 12:41:11 -0700906
Michal Krol01d7e3d2010-02-09 14:25:41 +0100907void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400908_mesa_free_buffer_objects( struct gl_context *ctx )
Michal Krol01d7e3d2010-02-09 14:25:41 +0100909{
Eric Anholt5527c2d2012-06-14 16:37:22 -0700910 GLuint i;
911
Michal Krol01d7e3d2010-02-09 14:25:41 +0100912 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
Michal Krol01d7e3d2010-02-09 14:25:41 +0100913
914 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
915 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
Eric Anholt5527c2d2012-06-14 16:37:22 -0700916
917 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
918
Iago Toral Quiroga98a1a2c72015-03-19 11:50:51 +0100919 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, NULL);
920
Aditya Atlurif455f342014-07-24 15:18:49 -0400921 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, NULL);
922
Aaron Watry18838352014-01-02 11:18:09 -0600923 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
924
Ilia Mirkin9327e2d2015-12-31 15:47:17 -0500925 _mesa_reference_buffer_object(ctx, &ctx->ParameterBuffer, NULL);
926
Jordan Justen12cf91d2015-09-17 10:10:07 -0700927 _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer, NULL);
928
Rafal Mielniczuk2d0ec0c2014-03-27 21:59:04 +0100929 _mesa_reference_buffer_object(ctx, &ctx->QueryBuffer, NULL);
930
Marek Olšák15a4b6d2013-05-14 17:58:32 +0200931 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
Eric Anholt5527c2d2012-06-14 16:37:22 -0700932 _mesa_reference_buffer_object(ctx,
933 &ctx->UniformBufferBindings[i].BufferObject,
934 NULL);
935 }
Aditya Atlurif455f342014-07-24 15:18:49 -0400936
Iago Toral Quiroga98a1a2c72015-03-19 11:50:51 +0100937 for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) {
938 _mesa_reference_buffer_object(ctx,
939 &ctx->ShaderStorageBufferBindings[i].BufferObject,
940 NULL);
941 }
942
Aditya Atlurif455f342014-07-24 15:18:49 -0400943 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
944 _mesa_reference_buffer_object(ctx,
945 &ctx->AtomicBufferBindings[i].BufferObject,
946 NULL);
947 }
948
Michal Krol01d7e3d2010-02-09 14:25:41 +0100949}
950
Fredrik Höglundccb62862013-11-01 19:09:58 +0100951bool
952_mesa_handle_bind_buffer_gen(struct gl_context *ctx,
Fredrik Höglundccb62862013-11-01 19:09:58 +0100953 GLuint buffer,
954 struct gl_buffer_object **buf_handle,
955 const char *caller)
Eric Anholt56e82e32012-06-26 13:17:08 -0700956{
957 struct gl_buffer_object *buf = *buf_handle;
958
Marta Lofstedt2bcacc62016-01-08 14:55:55 +0100959 if (!buf && (ctx->API == API_OPENGL_CORE)) {
Fredrik Höglundccb62862013-11-01 19:09:58 +0100960 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
Eric Anholt947d8ff2012-10-31 15:33:41 -0700961 return false;
962 }
963
Eric Anholt56e82e32012-06-26 13:17:08 -0700964 if (!buf || buf == &DummyBufferObject) {
965 /* If this is a new buffer object id, or one which was generated but
966 * never used before, allocate a buffer object now.
967 */
Pierre-Eric Pelloux-Prayer34852122020-09-04 14:07:04 +0200968 *buf_handle = ctx->Driver.NewBufferObject(ctx, buffer);
969 if (!*buf_handle) {
Fredrik Höglundccb62862013-11-01 19:09:58 +0100970 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
Eric Anholt947d8ff2012-10-31 15:33:41 -0700971 return false;
Eric Anholt56e82e32012-06-26 13:17:08 -0700972 }
Pierre-Eric Pelloux-Prayer34852122020-09-04 14:07:04 +0200973 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, *buf_handle, buf != NULL);
Eric Anholt56e82e32012-06-26 13:17:08 -0700974 }
Eric Anholt947d8ff2012-10-31 15:33:41 -0700975
976 return true;
Eric Anholt56e82e32012-06-26 13:17:08 -0700977}
Michal Krol01d7e3d2010-02-09 14:25:41 +0100978
Brian4b654d42007-08-23 08:53:43 +0100979/**
980 * Bind the specified target to buffer for the specified context.
Brian Pauldd5691e2009-10-27 20:10:30 -0600981 * Called by glBindBuffer() and other functions.
Brian4b654d42007-08-23 08:53:43 +0100982 */
983static void
Timothy Arceri8feb5bb2017-04-04 15:40:06 +1000984bind_buffer_object(struct gl_context *ctx,
985 struct gl_buffer_object **bindTarget, GLuint buffer)
Brian4b654d42007-08-23 08:53:43 +0100986{
987 struct gl_buffer_object *oldBufObj;
988 struct gl_buffer_object *newBufObj = NULL;
Brian4b654d42007-08-23 08:53:43 +0100989
Timothy Arceri8feb5bb2017-04-04 15:40:06 +1000990 assert(bindTarget);
Brian4b654d42007-08-23 08:53:43 +0100991
992 /* Get pointer to old buffer object (to be unbound) */
Brian Pauldd5691e2009-10-27 20:10:30 -0600993 oldBufObj = *bindTarget;
Marek Olšákf3cce702020-03-21 22:49:03 -0400994 if ((oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending) ||
995 (!oldBufObj && buffer == 0))
Brian4b654d42007-08-23 08:53:43 +0100996 return; /* rebinding the same buffer object- no change */
997
998 /*
999 * Get pointer to new buffer object (newBufObj)
1000 */
Marek Olšákf3cce702020-03-21 22:49:03 -04001001 if (buffer != 0) {
Brian4b654d42007-08-23 08:53:43 +01001002 /* non-default buffer object */
1003 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
Ian Romanick8ba3b762015-08-26 13:55:54 +01001004 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
Fredrik Höglundccb62862013-11-01 19:09:58 +01001005 &newBufObj, "glBindBuffer"))
Ian Romanick91107b42012-08-17 17:14:02 -07001006 return;
Brian Paul8faed712015-09-15 14:04:58 -06001007
Marek Olšákf3cce702020-03-21 22:49:03 -04001008 /* record usage history */
1009 if (bindTarget == &ctx->Pack.BufferObj)
1010 newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER;
Nicolai Hähnlefd7229b2016-01-07 14:48:27 -05001011 }
1012
Brian Paul37c74af2008-09-04 14:58:02 -06001013 /* bind new buffer */
1014 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
Brian4b654d42007-08-23 08:53:43 +01001015}
1016
1017
1018/**
1019 * Update the default buffer objects in the given context to reference those
Brian Paul8faed712015-09-15 14:04:58 -06001020 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +01001021 * shared state.
1022 */
1023void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001024_mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
Brian4b654d42007-08-23 08:53:43 +01001025{
Marek Olšákf3cce702020-03-21 22:49:03 -04001026 /* Bind 0 to remove references to those in the shared context hash table. */
Timothy Arceri8feb5bb2017-04-04 15:40:06 +10001027 bind_buffer_object(ctx, &ctx->Array.ArrayBufferObj, 0);
1028 bind_buffer_object(ctx, &ctx->Array.VAO->IndexBufferObj, 0);
1029 bind_buffer_object(ctx, &ctx->Pack.BufferObj, 0);
1030 bind_buffer_object(ctx, &ctx->Unpack.BufferObj, 0);
Brian4b654d42007-08-23 08:53:43 +01001031}
1032
Brian Paul148a2842003-09-17 03:40:11 +00001033
Briand933be62008-03-21 14:19:28 -06001034
Briana429a252008-03-21 13:41:00 -06001035/**
Brian Paul4cb9fff2006-06-03 15:32:27 +00001036 * Return the gl_buffer_object for the given ID.
1037 * Always return NULL for ID 0.
1038 */
Brian Paul4d12a052006-08-23 23:10:14 +00001039struct gl_buffer_object *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001040_mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
Brian Paul4cb9fff2006-06-03 15:32:27 +00001041{
1042 if (buffer == 0)
1043 return NULL;
1044 else
1045 return (struct gl_buffer_object *)
1046 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
1047}
1048
Brian Paul7a6b71e2004-03-13 18:21:40 +00001049
Fredrik Höglund28d73352013-11-15 19:47:20 +01001050struct gl_buffer_object *
1051_mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
1052{
Matt Turner015f2202015-07-30 14:31:04 -07001053 if (buffer == 0)
1054 return NULL;
1055 else
1056 return (struct gl_buffer_object *)
1057 _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
Fredrik Höglund28d73352013-11-15 19:47:20 +01001058}
1059
Laura Ekstrand768ca8b2015-01-09 16:16:48 -08001060/**
1061 * A convenience function for direct state access functions that throws
Fredrik Höglund2fd21d82015-03-19 19:44:57 +01001062 * GL_INVALID_OPERATION if buffer is not the name of an existing
1063 * buffer object.
Laura Ekstrand768ca8b2015-01-09 16:16:48 -08001064 */
1065struct gl_buffer_object *
1066_mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
1067 const char *caller)
1068{
1069 struct gl_buffer_object *bufObj;
1070
1071 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Fredrik Höglund2fd21d82015-03-19 19:44:57 +01001072 if (!bufObj || bufObj == &DummyBufferObject) {
Laura Ekstrand768ca8b2015-01-09 16:16:48 -08001073 _mesa_error(ctx, GL_INVALID_OPERATION,
Fredrik Höglund2fd21d82015-03-19 19:44:57 +01001074 "%s(non-existent buffer object %u)", caller, buffer);
1075 return NULL;
1076 }
Laura Ekstrand768ca8b2015-01-09 16:16:48 -08001077
1078 return bufObj;
1079}
1080
Fredrik Höglund28d73352013-11-15 19:47:20 +01001081
Fredrik Höglund28d73352013-11-15 19:47:20 +01001082/**
1083 * Look up a buffer object for a multi-bind function.
1084 *
1085 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1086 * of generating an error if the buffer ID is not zero or the name
1087 * of an existing buffer object.
1088 *
1089 * If the buffer ID refers to an existing buffer object, a pointer
Marek Olšák54525802020-03-21 23:47:08 -04001090 * to the buffer object is returned. If the ID is zero, NULL is returned.
1091 * If the ID is not zero and does not refer to a valid buffer object, this
1092 * function returns NULL.
Fredrik Höglund28d73352013-11-15 19:47:20 +01001093 *
1094 * This function assumes that the caller has already locked the
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10001095 * hash table mutex by calling
1096 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
Fredrik Höglund28d73352013-11-15 19:47:20 +01001097 */
1098struct gl_buffer_object *
1099_mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1100 const GLuint *buffers,
Marek Olšák54525802020-03-21 23:47:08 -04001101 GLuint index, const char *caller,
1102 bool *error)
Fredrik Höglund28d73352013-11-15 19:47:20 +01001103{
Marek Olšák54525802020-03-21 23:47:08 -04001104 struct gl_buffer_object *bufObj = NULL;
1105
1106 *error = false;
Fredrik Höglund28d73352013-11-15 19:47:20 +01001107
1108 if (buffers[index] != 0) {
1109 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1110
1111 /* The multi-bind functions don't create the buffer objects
1112 when they don't exist. */
1113 if (bufObj == &DummyBufferObject)
1114 bufObj = NULL;
Fredrik Höglund28d73352013-11-15 19:47:20 +01001115
Marek Olšák54525802020-03-21 23:47:08 -04001116 if (!bufObj) {
1117 /* The ARB_multi_bind spec says:
1118 *
1119 * "An INVALID_OPERATION error is generated if any value
1120 * in <buffers> is not zero or the name of an existing
1121 * buffer object (per binding)."
1122 */
1123 _mesa_error(ctx, GL_INVALID_OPERATION,
1124 "%s(buffers[%u]=%u is not zero or the name "
1125 "of an existing buffer object)",
1126 caller, index, buffers[index]);
1127 *error = true;
1128 }
Fredrik Höglund28d73352013-11-15 19:47:20 +01001129 }
1130
1131 return bufObj;
1132}
1133
1134
Brian Paul37c74af2008-09-04 14:58:02 -06001135/**
1136 * If *ptr points to obj, set ptr = the Null/default buffer object.
1137 * This is a helper for buffer object deletion.
1138 * The GL spec says that deleting a buffer object causes it to get
1139 * unbound from all arrays in the current context.
1140 */
1141static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001142unbind(struct gl_context *ctx,
Mathias Fröhlichf7cb46a2016-06-17 08:09:04 +02001143 struct gl_vertex_array_object *vao, unsigned index,
Brian Paul37c74af2008-09-04 14:58:02 -06001144 struct gl_buffer_object *obj)
1145{
Brian Paul910bc4d2016-10-19 17:58:44 -06001146 if (vao->BufferBinding[index].BufferObj == obj) {
Marek Olšáke6302712020-03-21 21:36:28 -04001147 _mesa_bind_vertex_buffer(ctx, vao, index, NULL,
Brian Paul910bc4d2016-10-19 17:58:44 -06001148 vao->BufferBinding[index].Offset,
Marek Olšák03ba57c2020-03-22 18:13:45 -04001149 vao->BufferBinding[index].Stride, true, false);
Brian Paul37c74af2008-09-04 14:58:02 -06001150 }
1151}
1152
1153
Brian Paul331eb58f2009-06-19 10:00:03 -06001154/**
1155 * Plug default/fallback buffer object functions into the device
1156 * driver hooks.
1157 */
1158void
1159_mesa_init_buffer_object_functions(struct dd_function_table *driver)
1160{
1161 /* GL_ARB_vertex/pixel_buffer_object */
1162 driver->NewBufferObject = _mesa_new_buffer_object;
1163 driver->DeleteBuffer = _mesa_delete_buffer_object;
Laura Ekstrandcb568352015-02-09 17:57:46 -08001164 driver->BufferData = buffer_data_fallback;
Laura Ekstrand566ccdf2015-01-13 11:28:17 -08001165 driver->BufferSubData = buffer_sub_data_fallback;
Timothy Arceri5e86bfa2017-05-04 14:32:28 +10001166 driver->GetBufferSubData = buffer_get_subdata;
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08001167 driver->UnmapBuffer = unmap_buffer_fallback;
Brian Paul331eb58f2009-06-19 10:00:03 -06001168
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07001169 /* GL_ARB_clear_buffer_object */
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08001170 driver->ClearBufferSubData = _mesa_ClearBufferSubData_sw;
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07001171
Brian Paul331eb58f2009-06-19 10:00:03 -06001172 /* GL_ARB_map_buffer_range */
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08001173 driver->MapBufferRange = map_buffer_range_fallback;
Laura Ekstrandee5fae62015-01-14 17:01:20 -08001174 driver->FlushMappedBufferRange = flush_mapped_buffer_range_fallback;
Brian Paul331eb58f2009-06-19 10:00:03 -06001175
1176 /* GL_ARB_copy_buffer */
Laura Ekstrand4adaad52015-01-13 13:28:08 -08001177 driver->CopyBufferSubData = copy_buffer_sub_data_fallback;
Brian Paul331eb58f2009-06-19 10:00:03 -06001178}
1179
1180
Marek Olšákdca35022014-02-06 19:24:23 +01001181void
1182_mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1183 struct gl_buffer_object *bufObj)
1184{
Timothy Arceri084fec02017-05-04 16:14:33 +10001185 for (int i = 0; i < MAP_COUNT; i++) {
Marek Olšákdca35022014-02-06 19:24:23 +01001186 if (_mesa_bufferobj_mapped(bufObj, i)) {
1187 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
Matt Turnerbfcdb842015-02-20 20:18:47 -08001188 assert(bufObj->Mappings[i].Pointer == NULL);
Marek Olšákdca35022014-02-06 19:24:23 +01001189 bufObj->Mappings[i].AccessFlags = 0;
1190 }
1191 }
1192}
1193
Brian Paul148a2842003-09-17 03:40:11 +00001194
1195/**********************************************************************/
1196/* API Functions */
1197/**********************************************************************/
1198
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001199void GLAPIENTRY
Samuel Pitoisetc2315902017-05-31 14:26:20 +02001200_mesa_BindBuffer_no_error(GLenum target, GLuint buffer)
1201{
1202 GET_CURRENT_CONTEXT(ctx);
1203
1204 struct gl_buffer_object **bindTarget = get_buffer_target(ctx, target);
1205 bind_buffer_object(ctx, bindTarget, buffer);
1206}
1207
1208
1209void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08001210_mesa_BindBuffer(GLenum target, GLuint buffer)
Brian Paul6061df02003-03-29 17:01:00 +00001211{
Brian Paulaac73252003-04-09 02:31:35 +00001212 GET_CURRENT_CONTEXT(ctx);
Brian Paulaac73252003-04-09 02:31:35 +00001213
Jordan Justen36db91c2015-12-29 14:24:09 -08001214 if (MESA_VERBOSE & VERBOSE_API) {
Brian Paul2634e922011-02-08 19:19:34 -07001215 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07001216 _mesa_enum_to_string(target), buffer);
Jordan Justen36db91c2015-12-29 14:24:09 -08001217 }
Brian Paul2634e922011-02-08 19:19:34 -07001218
Timothy Arceri8feb5bb2017-04-04 15:40:06 +10001219 struct gl_buffer_object **bindTarget = get_buffer_target(ctx, target);
1220 if (!bindTarget) {
1221 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target %s)",
1222 _mesa_enum_to_string(target));
1223 return;
1224 }
1225
1226 bind_buffer_object(ctx, bindTarget, buffer);
Brian Paul6061df02003-03-29 17:01:00 +00001227}
1228
Marek Olšák70847eb2020-03-06 20:37:57 -05001229void
1230_mesa_InternalBindElementBuffer(struct gl_context *ctx,
1231 struct gl_buffer_object *buf)
1232{
1233 struct gl_buffer_object **bindTarget =
1234 get_buffer_target(ctx, GL_ELEMENT_ARRAY_BUFFER);
1235
1236 /* Move the buffer reference from the parameter to the bind point. */
1237 _mesa_reference_buffer_object(ctx, bindTarget, NULL);
1238 if (buf)
1239 *bindTarget = buf;
1240}
1241
Timothy Arceri003c8b12017-07-25 23:34:06 +10001242/**
Dave Airlie35ac13e2017-09-15 12:55:50 +10001243 * Binds a buffer object to a binding point.
Timothy Arceri003c8b12017-07-25 23:34:06 +10001244 *
1245 * The caller is responsible for validating the offset,
1246 * flushing the vertices and updating NewDriverState.
1247 */
1248static void
Dave Airlie35ac13e2017-09-15 12:55:50 +10001249set_buffer_binding(struct gl_context *ctx,
1250 struct gl_buffer_binding *binding,
1251 struct gl_buffer_object *bufObj,
1252 GLintptr offset,
1253 GLsizeiptr size,
1254 bool autoSize, gl_buffer_usage usage)
Timothy Arceri003c8b12017-07-25 23:34:06 +10001255{
1256 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
1257
Dave Airlie65d3ef72017-09-15 12:38:18 +10001258 binding->Offset = offset;
1259 binding->Size = size;
1260 binding->AutomaticSize = autoSize;
1261
1262 /* If this is a real buffer object, mark it has having been used
1263 * at some point as an atomic counter buffer.
1264 */
1265 if (size >= 0)
Dave Airlie35ac13e2017-09-15 12:55:50 +10001266 bufObj->UsageHistory |= usage;
1267}
1268
1269static void
1270set_buffer_multi_binding(struct gl_context *ctx,
1271 const GLuint *buffers,
1272 int idx,
1273 const char *caller,
1274 struct gl_buffer_binding *binding,
1275 GLintptr offset,
1276 GLsizeiptr size,
1277 bool range,
1278 gl_buffer_usage usage)
1279{
1280 struct gl_buffer_object *bufObj;
Marek Olšák54525802020-03-21 23:47:08 -04001281
Dave Airlie35ac13e2017-09-15 12:55:50 +10001282 if (binding->BufferObject && binding->BufferObject->Name == buffers[idx])
1283 bufObj = binding->BufferObject;
Marek Olšák54525802020-03-21 23:47:08 -04001284 else {
1285 bool error;
1286 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, idx, caller,
1287 &error);
1288 if (error)
1289 return;
Dave Airlie35ac13e2017-09-15 12:55:50 +10001290 }
Marek Olšák54525802020-03-21 23:47:08 -04001291
1292 if (!bufObj)
1293 set_buffer_binding(ctx, binding, bufObj, -1, -1, !range, usage);
1294 else
1295 set_buffer_binding(ctx, binding, bufObj, offset, size, !range, usage);
Timothy Arceri003c8b12017-07-25 23:34:06 +10001296}
1297
Dave Airlie3e156b82017-09-15 13:13:20 +10001298static void
1299bind_buffer(struct gl_context *ctx,
1300 struct gl_buffer_binding *binding,
1301 struct gl_buffer_object *bufObj,
1302 GLintptr offset,
1303 GLsizeiptr size,
1304 GLboolean autoSize,
1305 uint64_t driver_state,
1306 gl_buffer_usage usage)
1307{
1308 if (binding->BufferObject == bufObj &&
1309 binding->Offset == offset &&
1310 binding->Size == size &&
1311 binding->AutomaticSize == autoSize) {
1312 return;
1313 }
1314
1315 FLUSH_VERTICES(ctx, 0);
1316 ctx->NewDriverState |= driver_state;
1317
1318 set_buffer_binding(ctx, binding, bufObj, offset, size, autoSize, usage);
1319}
1320
Timothy Arceri003c8b12017-07-25 23:34:06 +10001321/**
1322 * Binds a buffer object to a uniform buffer binding point.
1323 *
Dave Airlie35ac13e2017-09-15 12:55:50 +10001324 * Unlike set_buffer_binding(), this function also flushes vertices
Timothy Arceri003c8b12017-07-25 23:34:06 +10001325 * and updates NewDriverState. It also checks if the binding
1326 * has actually changed before updating it.
1327 */
1328static void
1329bind_uniform_buffer(struct gl_context *ctx,
1330 GLuint index,
1331 struct gl_buffer_object *bufObj,
1332 GLintptr offset,
1333 GLsizeiptr size,
1334 GLboolean autoSize)
1335{
Dave Airlie3e156b82017-09-15 13:13:20 +10001336 bind_buffer(ctx, &ctx->UniformBufferBindings[index],
1337 bufObj, offset, size, autoSize,
1338 ctx->DriverFlags.NewUniformBuffer,
1339 USAGE_UNIFORM_BUFFER);
Timothy Arceri003c8b12017-07-25 23:34:06 +10001340}
1341
1342/**
1343 * Binds a buffer object to a shader storage buffer binding point.
1344 *
1345 * Unlike set_ssbo_binding(), this function also flushes vertices
1346 * and updates NewDriverState. It also checks if the binding
1347 * has actually changed before updating it.
1348 */
1349static void
1350bind_shader_storage_buffer(struct gl_context *ctx,
1351 GLuint index,
1352 struct gl_buffer_object *bufObj,
1353 GLintptr offset,
1354 GLsizeiptr size,
1355 GLboolean autoSize)
1356{
Dave Airlie3e156b82017-09-15 13:13:20 +10001357 bind_buffer(ctx, &ctx->ShaderStorageBufferBindings[index],
1358 bufObj, offset, size, autoSize,
1359 ctx->DriverFlags.NewShaderStorageBuffer,
1360 USAGE_SHADER_STORAGE_BUFFER);
Timothy Arceri003c8b12017-07-25 23:34:06 +10001361}
1362
1363/**
Dave Airlie65d3ef72017-09-15 12:38:18 +10001364 * Binds a buffer object to an atomic buffer binding point.
1365 *
1366 * Unlike set_atomic_binding(), this function also flushes vertices
1367 * and updates NewDriverState. It also checks if the binding
1368 * has actually changed before updating it.
1369 */
1370static void
1371bind_atomic_buffer(struct gl_context *ctx, unsigned index,
1372 struct gl_buffer_object *bufObj, GLintptr offset,
1373 GLsizeiptr size, GLboolean autoSize)
1374{
Dave Airlie3e156b82017-09-15 13:13:20 +10001375 bind_buffer(ctx, &ctx->AtomicBufferBindings[index],
1376 bufObj, offset, size, autoSize,
1377 ctx->DriverFlags.NewAtomicBuffer,
1378 USAGE_ATOMIC_COUNTER_BUFFER);
Dave Airlie65d3ef72017-09-15 12:38:18 +10001379}
1380
1381/**
Timothy Arceri003c8b12017-07-25 23:34:06 +10001382 * Bind a buffer object to a uniform block binding point.
1383 * As above, but offset = 0.
1384 */
1385static void
1386bind_buffer_base_uniform_buffer(struct gl_context *ctx,
1387 GLuint index,
1388 struct gl_buffer_object *bufObj)
1389{
1390 if (index >= ctx->Const.MaxUniformBufferBindings) {
1391 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
1392 return;
1393 }
1394
1395 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
1396
Marek Olšák54525802020-03-21 23:47:08 -04001397 if (!bufObj)
Timothy Arceri003c8b12017-07-25 23:34:06 +10001398 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
1399 else
1400 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
1401}
1402
1403/**
1404 * Bind a buffer object to a shader storage block binding point.
1405 * As above, but offset = 0.
1406 */
1407static void
1408bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
1409 GLuint index,
1410 struct gl_buffer_object *bufObj)
1411{
1412 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
1413 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
1414 return;
1415 }
1416
1417 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
1418
Marek Olšák54525802020-03-21 23:47:08 -04001419 if (!bufObj)
Timothy Arceri003c8b12017-07-25 23:34:06 +10001420 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
1421 else
1422 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
1423}
1424
Dave Airlie65d3ef72017-09-15 12:38:18 +10001425/**
1426 * Bind a buffer object to a shader storage block binding point.
1427 * As above, but offset = 0.
1428 */
Timothy Arceri003c8b12017-07-25 23:34:06 +10001429static void
Dave Airlie65d3ef72017-09-15 12:38:18 +10001430bind_buffer_base_atomic_buffer(struct gl_context *ctx,
1431 GLuint index,
1432 struct gl_buffer_object *bufObj)
Timothy Arceri003c8b12017-07-25 23:34:06 +10001433{
Dave Airlie65d3ef72017-09-15 12:38:18 +10001434 if (index >= ctx->Const.MaxAtomicBufferBindings) {
1435 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
Timothy Arceri003c8b12017-07-25 23:34:06 +10001436 return;
1437 }
1438
Dave Airlie65d3ef72017-09-15 12:38:18 +10001439 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
Timothy Arceri003c8b12017-07-25 23:34:06 +10001440
Marek Olšák54525802020-03-21 23:47:08 -04001441 if (!bufObj)
Dave Airlie65d3ef72017-09-15 12:38:18 +10001442 bind_atomic_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
1443 else
1444 bind_atomic_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
Timothy Arceri003c8b12017-07-25 23:34:06 +10001445}
Ian Romanick0207b472003-09-09 00:10:12 +00001446
1447/**
1448 * Delete a set of buffer objects.
Brian Paul8faed712015-09-15 14:04:58 -06001449 *
Ian Romanick0207b472003-09-09 00:10:12 +00001450 * \param n Number of buffer objects to delete.
Jose Fonseca375457b2004-09-09 22:23:24 +00001451 * \param ids Array of \c n buffer object IDs.
Ian Romanick0207b472003-09-09 00:10:12 +00001452 */
Samuel Pitoisetfc039e92017-07-19 13:32:28 +02001453static void
1454delete_buffers(struct gl_context *ctx, GLsizei n, const GLuint *ids)
Brian Paul6061df02003-03-29 17:01:00 +00001455{
Brian Paul4293a122011-03-11 09:25:21 -07001456 FLUSH_VERTICES(ctx, 0);
Brian Paulaac73252003-04-09 02:31:35 +00001457
Matt Turner015f2202015-07-30 14:31:04 -07001458 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
Ian Romanick0207b472003-09-09 00:10:12 +00001459
Samuel Pitoisetfc039e92017-07-19 13:32:28 +02001460 for (GLsizei i = 0; i < n; i++) {
Matt Turner015f2202015-07-30 14:31:04 -07001461 struct gl_buffer_object *bufObj =
1462 _mesa_lookup_bufferobj_locked(ctx, ids[i]);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001463 if (bufObj) {
Kenneth Graunkeaac14152014-02-01 19:36:59 -08001464 struct gl_vertex_array_object *vao = ctx->Array.VAO;
Brian Paul4cb9fff2006-06-03 15:32:27 +00001465 GLuint j;
Brian Paul94ec5252004-03-04 14:46:00 +00001466
Matt Turnerbfcdb842015-02-20 20:18:47 -08001467 assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
Brian Paul94ec5252004-03-04 14:46:00 +00001468
Marek Olšákdca35022014-02-06 19:24:23 +01001469 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
Brian Paula7f434b2009-02-27 13:04:38 -07001470
Brian Paul395bcad2009-02-27 12:41:11 -07001471 /* unbind any vertex pointers bound to this buffer */
Brian Paul910bc4d2016-10-19 17:58:44 -06001472 for (j = 0; j < ARRAY_SIZE(vao->BufferBinding); j++) {
Mathias Fröhlichf7cb46a2016-06-17 08:09:04 +02001473 unbind(ctx, vao, j, bufObj);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001474 }
1475
1476 if (ctx->Array.ArrayBufferObj == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001477 bind_buffer_object(ctx, &ctx->Array.ArrayBufferObj, 0);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001478 }
Kenneth Graunkee1b1f2a2014-02-01 19:46:45 -08001479 if (vao->IndexBufferObj == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001480 bind_buffer_object(ctx, &vao->IndexBufferObj, 0);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001481 }
1482
Chris Forbesa95236c2013-11-06 20:03:21 +13001483 /* unbind ARB_draw_indirect binding point */
1484 if (ctx->DrawIndirectBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001485 bind_buffer_object(ctx, &ctx->DrawIndirectBuffer, 0);
Chris Forbesa95236c2013-11-06 20:03:21 +13001486 }
1487
Ilia Mirkin9327e2d2015-12-31 15:47:17 -05001488 /* unbind ARB_indirect_parameters binding point */
1489 if (ctx->ParameterBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001490 bind_buffer_object(ctx, &ctx->ParameterBuffer, 0);
Ilia Mirkin9327e2d2015-12-31 15:47:17 -05001491 }
1492
Jordan Justen12cf91d2015-09-17 10:10:07 -07001493 /* unbind ARB_compute_shader binding point */
1494 if (ctx->DispatchIndirectBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001495 bind_buffer_object(ctx, &ctx->DispatchIndirectBuffer, 0);
Jordan Justen12cf91d2015-09-17 10:10:07 -07001496 }
1497
Kenneth Graunkecb8ed932012-06-03 22:34:32 -07001498 /* unbind ARB_copy_buffer binding points */
1499 if (ctx->CopyReadBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001500 bind_buffer_object(ctx, &ctx->CopyReadBuffer, 0);
Kenneth Graunkecb8ed932012-06-03 22:34:32 -07001501 }
1502 if (ctx->CopyWriteBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001503 bind_buffer_object(ctx, &ctx->CopyWriteBuffer, 0);
Kenneth Graunkecb8ed932012-06-03 22:34:32 -07001504 }
1505
Kenneth Graunke3edd2ba2012-06-04 00:51:34 -07001506 /* unbind transform feedback binding points */
Kenneth Graunkecb8ed932012-06-03 22:34:32 -07001507 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001508 bind_buffer_object(ctx, &ctx->TransformFeedback.CurrentBuffer, 0);
Kenneth Graunkecb8ed932012-06-03 22:34:32 -07001509 }
Marek Olšák15ac66e2011-12-18 02:13:17 +01001510 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
Kenneth Graunke3edd2ba2012-06-04 00:51:34 -07001511 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
Timothy Arceri6be1c692017-07-25 23:16:14 +10001512 _mesa_bind_buffer_base_transform_feedback(ctx,
1513 ctx->TransformFeedback.CurrentObject,
Marek Olšák54525802020-03-21 23:47:08 -04001514 j, NULL, false);
Kenneth Graunke3edd2ba2012-06-04 00:51:34 -07001515 }
1516 }
Kenneth Graunkecb8ed932012-06-03 22:34:32 -07001517
Eric Anholtbfa046b2012-07-31 12:46:25 -07001518 /* unbind UBO binding points */
1519 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1520 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
Marek Olšák54525802020-03-21 23:47:08 -04001521 bind_buffer_base_uniform_buffer(ctx, j, NULL);
Eric Anholtbfa046b2012-07-31 12:46:25 -07001522 }
1523 }
1524
Eric Anholtc5c696e2012-06-18 11:17:04 -07001525 if (ctx->UniformBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001526 bind_buffer_object(ctx, &ctx->UniformBuffer, 0);
Eric Anholtc5c696e2012-06-18 11:17:04 -07001527 }
1528
Iago Toral Quirogae72f5ef2015-03-19 10:31:23 +01001529 /* unbind SSBO binding points */
1530 for (j = 0; j < ctx->Const.MaxShaderStorageBufferBindings; j++) {
1531 if (ctx->ShaderStorageBufferBindings[j].BufferObject == bufObj) {
Marek Olšák54525802020-03-21 23:47:08 -04001532 bind_buffer_base_shader_storage_buffer(ctx, j, NULL);
Iago Toral Quirogae72f5ef2015-03-19 10:31:23 +01001533 }
1534 }
1535
1536 if (ctx->ShaderStorageBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001537 bind_buffer_object(ctx, &ctx->ShaderStorageBuffer, 0);
Iago Toral Quirogae72f5ef2015-03-19 10:31:23 +01001538 }
1539
Aditya Atlurif455f342014-07-24 15:18:49 -04001540 /* unbind Atomci Buffer binding points */
1541 for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
1542 if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
Marek Olšák54525802020-03-21 23:47:08 -04001543 bind_buffer_base_atomic_buffer(ctx, j, NULL);
Aditya Atlurif455f342014-07-24 15:18:49 -04001544 }
1545 }
1546
Marek Olšáke8625a22015-02-10 01:35:23 +01001547 if (ctx->AtomicBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001548 bind_buffer_object(ctx, &ctx->AtomicBuffer, 0);
Aditya Atlurif455f342014-07-24 15:18:49 -04001549 }
1550
Brian Paul395bcad2009-02-27 12:41:11 -07001551 /* unbind any pixel pack/unpack pointers bound to this buffer */
Brian Paul4cb9fff2006-06-03 15:32:27 +00001552 if (ctx->Pack.BufferObj == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001553 bind_buffer_object(ctx, &ctx->Pack.BufferObj, 0);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001554 }
1555 if (ctx->Unpack.BufferObj == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001556 bind_buffer_object(ctx, &ctx->Unpack.BufferObj, 0);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001557 }
1558
Kenneth Graunke45c21f82012-06-09 21:44:58 -07001559 if (ctx->Texture.BufferObject == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001560 bind_buffer_object(ctx, &ctx->Texture.BufferObject, 0);
Kenneth Graunke45c21f82012-06-09 21:44:58 -07001561 }
1562
Marek Olšák11ebb032015-02-10 01:39:41 +01001563 if (ctx->ExternalVirtualMemoryBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001564 bind_buffer_object(ctx, &ctx->ExternalVirtualMemoryBuffer, 0);
Marek Olšák11ebb032015-02-10 01:39:41 +01001565 }
1566
Rafal Mielniczuk2d0ec0c2014-03-27 21:59:04 +01001567 /* unbind query buffer binding point */
1568 if (ctx->QueryBuffer == bufObj) {
Timothy Arceri1bfeb652017-04-04 16:38:35 +10001569 bind_buffer_object(ctx, &ctx->QueryBuffer, 0);
Rafal Mielniczuk2d0ec0c2014-03-27 21:59:04 +01001570 }
1571
Brian4b654d42007-08-23 08:53:43 +01001572 /* The ID is immediately freed for re-use */
Matt Turner015f2202015-07-30 14:31:04 -07001573 _mesa_HashRemoveLocked(ctx->Shared->BufferObjects, ids[i]);
Mathias Fröhlich9ed88982011-10-22 12:57:01 +02001574 /* Make sure we do not run into the classic ABA problem on bind.
1575 * We don't want to allow re-binding a buffer object that's been
1576 * "deleted" by glDeleteBuffers().
1577 *
1578 * The explicit rebinding to the default object in the current context
1579 * prevents the above in the current context, but another context
1580 * sharing the same objects might suffer from this problem.
1581 * The alternative would be to do the hash lookup in any case on bind
1582 * which would introduce more runtime overhead than this.
1583 */
1584 bufObj->DeletePending = GL_TRUE;
Brian Paul37c74af2008-09-04 14:58:02 -06001585 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
Ian Romanick0207b472003-09-09 00:10:12 +00001586 }
1587 }
1588
Matt Turner015f2202015-07-30 14:31:04 -07001589 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Brian Paul6061df02003-03-29 17:01:00 +00001590}
1591
Ian Romanick0207b472003-09-09 00:10:12 +00001592
Samuel Pitoisetfc039e92017-07-19 13:32:28 +02001593void GLAPIENTRY
Samuel Pitoiset1dd20032017-07-19 13:35:11 +02001594_mesa_DeleteBuffers_no_error(GLsizei n, const GLuint *ids)
1595{
1596 GET_CURRENT_CONTEXT(ctx);
1597 delete_buffers(ctx, n, ids);
1598}
1599
1600
1601void GLAPIENTRY
Samuel Pitoisetfc039e92017-07-19 13:32:28 +02001602_mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1603{
1604 GET_CURRENT_CONTEXT(ctx);
1605
1606 if (n < 0) {
1607 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1608 return;
1609 }
1610
1611 delete_buffers(ctx, n, ids);
1612}
1613
1614
Ian Romanick0207b472003-09-09 00:10:12 +00001615/**
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001616 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1617 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1618 * driver internals.
Ian Romanick0207b472003-09-09 00:10:12 +00001619 */
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001620static void
Samuel Pitoiset064bb742017-06-26 12:39:42 +02001621create_buffers(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa)
Brian Paul6061df02003-03-29 17:01:00 +00001622{
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001623 struct gl_buffer_object *buf;
1624
Samuel Pitoiset064bb742017-06-26 12:39:42 +02001625 if (!buffers)
Brian Paulaac73252003-04-09 02:31:35 +00001626 return;
Ian Romanick0207b472003-09-09 00:10:12 +00001627
1628 /*
1629 * This must be atomic (generation and allocation of buffer object IDs)
1630 */
Matt Turner015f2202015-07-30 14:31:04 -07001631 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
Ian Romanick0207b472003-09-09 00:10:12 +00001632
Pierre-Eric Pelloux-Prayera56849d2020-08-17 15:12:03 +02001633 _mesa_HashFindFreeKeys(ctx->Shared->BufferObjects, buffers, n);
Ian Romanick0207b472003-09-09 00:10:12 +00001634
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001635 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1636 * DummyBufferObject. Otherwise, create a new buffer object and insert
1637 * it.
1638 */
Timothy Arceri084fec02017-05-04 16:14:33 +10001639 for (int i = 0; i < n; i++) {
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001640 if (dsa) {
1641 assert(ctx->Driver.NewBufferObject);
1642 buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
1643 if (!buf) {
Samuel Pitoiset064bb742017-06-26 12:39:42 +02001644 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCreateBuffers");
Matt Turner015f2202015-07-30 14:31:04 -07001645 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001646 return;
1647 }
1648 }
1649 else
1650 buf = &DummyBufferObject;
1651
Pierre-Eric Pelloux-Prayer34852122020-09-04 14:07:04 +02001652 _mesa_HashInsertLocked(ctx->Shared->BufferObjects, buffers[i], buf, true);
Ian Romanick0207b472003-09-09 00:10:12 +00001653 }
1654
Matt Turner015f2202015-07-30 14:31:04 -07001655 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Brian Paul6061df02003-03-29 17:01:00 +00001656}
1657
Samuel Pitoiset064bb742017-06-26 12:39:42 +02001658
1659static void
1660create_buffers_err(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa)
1661{
1662 const char *func = dsa ? "glCreateBuffers" : "glGenBuffers";
1663
1664 if (MESA_VERBOSE & VERBOSE_API)
1665 _mesa_debug(ctx, "%s(%d)\n", func, n);
1666
1667 if (n < 0) {
1668 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n);
1669 return;
1670 }
1671
1672 create_buffers(ctx, n, buffers, dsa);
1673}
1674
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001675/**
1676 * Generate a set of unique buffer object IDs and store them in \c buffers.
1677 *
1678 * \param n Number of IDs to generate.
1679 * \param buffers Array of \c n locations to store the IDs.
1680 */
1681void GLAPIENTRY
Samuel Pitoiset7c022672017-06-26 12:42:25 +02001682_mesa_GenBuffers_no_error(GLsizei n, GLuint *buffers)
1683{
1684 GET_CURRENT_CONTEXT(ctx);
1685 create_buffers(ctx, n, buffers, false);
1686}
1687
1688
1689void GLAPIENTRY
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001690_mesa_GenBuffers(GLsizei n, GLuint *buffers)
1691{
Samuel Pitoiset064bb742017-06-26 12:39:42 +02001692 GET_CURRENT_CONTEXT(ctx);
1693 create_buffers_err(ctx, n, buffers, false);
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001694}
1695
1696/**
1697 * Create a set of buffer objects and store their unique IDs in \c buffers.
1698 *
1699 * \param n Number of IDs to generate.
1700 * \param buffers Array of \c n locations to store the IDs.
1701 */
1702void GLAPIENTRY
Samuel Pitoiset7c022672017-06-26 12:42:25 +02001703_mesa_CreateBuffers_no_error(GLsizei n, GLuint *buffers)
1704{
1705 GET_CURRENT_CONTEXT(ctx);
1706 create_buffers(ctx, n, buffers, true);
1707}
1708
1709
1710void GLAPIENTRY
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001711_mesa_CreateBuffers(GLsizei n, GLuint *buffers)
1712{
Samuel Pitoiset064bb742017-06-26 12:39:42 +02001713 GET_CURRENT_CONTEXT(ctx);
1714 create_buffers_err(ctx, n, buffers, true);
Laura Ekstrand2cf48c32014-12-18 17:10:06 -08001715}
1716
Ian Romanick0207b472003-09-09 00:10:12 +00001717
1718/**
1719 * Determine if ID is the name of a buffer object.
Brian Paul8faed712015-09-15 14:04:58 -06001720 *
Ian Romanick0207b472003-09-09 00:10:12 +00001721 * \param id ID of the potential buffer object.
Brian Paul8faed712015-09-15 14:04:58 -06001722 * \return \c GL_TRUE if \c id is the name of a buffer object,
Ian Romanick0207b472003-09-09 00:10:12 +00001723 * \c GL_FALSE otherwise.
1724 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001725GLboolean GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08001726_mesa_IsBuffer(GLuint id)
Brian Paul6061df02003-03-29 17:01:00 +00001727{
Brian Paul4cb9fff2006-06-03 15:32:27 +00001728 struct gl_buffer_object *bufObj;
Ian Romanick0207b472003-09-09 00:10:12 +00001729 GET_CURRENT_CONTEXT(ctx);
1730 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1731
Brian Paul4d12a052006-08-23 23:10:14 +00001732 bufObj = _mesa_lookup_bufferobj(ctx, id);
Ian Romanick0207b472003-09-09 00:10:12 +00001733
Brian Paulc552f272010-11-11 14:47:30 -07001734 return bufObj && bufObj != &DummyBufferObject;
Brian Paul6061df02003-03-29 17:01:00 +00001735}
1736
Ian Romanick0207b472003-09-09 00:10:12 +00001737
Timothy Arceri70d4d112017-05-12 15:41:22 +10001738static bool
1739validate_buffer_storage(struct gl_context *ctx,
1740 struct gl_buffer_object *bufObj, GLsizeiptr size,
1741 GLbitfield flags, const char *func)
Marek Olšáke592f112014-01-27 21:22:43 +01001742{
Marek Olšáke592f112014-01-27 21:22:43 +01001743 if (size <= 0) {
Laura Ekstranda76808d2015-01-09 16:17:10 -08001744 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
Timothy Arceri70d4d112017-05-12 15:41:22 +10001745 return false;
Marek Olšáke592f112014-01-27 21:22:43 +01001746 }
1747
Nicolai Hähnled6fcbe12017-02-02 21:50:32 +01001748 GLbitfield valid_flags = GL_MAP_READ_BIT |
1749 GL_MAP_WRITE_BIT |
1750 GL_MAP_PERSISTENT_BIT |
1751 GL_MAP_COHERENT_BIT |
1752 GL_DYNAMIC_STORAGE_BIT |
1753 GL_CLIENT_STORAGE_BIT;
1754
1755 if (ctx->Extensions.ARB_sparse_buffer)
1756 valid_flags |= GL_SPARSE_STORAGE_BIT_ARB;
1757
1758 if (flags & ~valid_flags) {
Laura Ekstranda76808d2015-01-09 16:17:10 -08001759 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func);
Timothy Arceri70d4d112017-05-12 15:41:22 +10001760 return false;
Marek Olšáke592f112014-01-27 21:22:43 +01001761 }
1762
Nicolai Hähnled6fcbe12017-02-02 21:50:32 +01001763 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1764 *
1765 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1766 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1767 * MAP_READ_BIT or MAP_WRITE_BIT."
1768 */
1769 if (flags & GL_SPARSE_STORAGE_BIT_ARB &&
1770 flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) {
1771 _mesa_error(ctx, GL_INVALID_VALUE, "%s(SPARSE_STORAGE and READ/WRITE)", func);
Timothy Arceri70d4d112017-05-12 15:41:22 +10001772 return false;
Nicolai Hähnled6fcbe12017-02-02 21:50:32 +01001773 }
1774
Marek Olšáke592f112014-01-27 21:22:43 +01001775 if (flags & GL_MAP_PERSISTENT_BIT &&
1776 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
Laura Ekstranda76808d2015-01-09 16:17:10 -08001777 _mesa_error(ctx, GL_INVALID_VALUE,
1778 "%s(PERSISTENT and flags!=READ/WRITE)", func);
Timothy Arceri70d4d112017-05-12 15:41:22 +10001779 return false;
Marek Olšáke592f112014-01-27 21:22:43 +01001780 }
1781
1782 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
Laura Ekstranda76808d2015-01-09 16:17:10 -08001783 _mesa_error(ctx, GL_INVALID_VALUE,
1784 "%s(COHERENT and flags!=PERSISTENT)", func);
Timothy Arceri70d4d112017-05-12 15:41:22 +10001785 return false;
Marek Olšáke592f112014-01-27 21:22:43 +01001786 }
1787
Samuel Pitoiset41257fd2017-03-29 16:29:24 +02001788 if (bufObj->Immutable || bufObj->HandleAllocated) {
Laura Ekstranda76808d2015-01-09 16:17:10 -08001789 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
Timothy Arceri70d4d112017-05-12 15:41:22 +10001790 return false;
Marek Olšáke592f112014-01-27 21:22:43 +01001791 }
1792
Timothy Arceri70d4d112017-05-12 15:41:22 +10001793 return true;
1794}
1795
1796
1797static void
1798buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
Timothy Arceriba6eee22017-07-26 16:50:44 +10001799 struct gl_memory_object *memObj, GLenum target,
1800 GLsizeiptr size, const GLvoid *data, GLbitfield flags,
1801 GLuint64 offset, const char *func)
Timothy Arceri70d4d112017-05-12 15:41:22 +10001802{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001803 GLboolean res;
1804
Marek Olšákdca35022014-02-06 19:24:23 +01001805 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1806 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
Marek Olšáke592f112014-01-27 21:22:43 +01001807
Marek Olšák95d9fdc2017-06-10 00:48:53 +02001808 FLUSH_VERTICES(ctx, 0);
Marek Olšáke592f112014-01-27 21:22:43 +01001809
1810 bufObj->Written = GL_TRUE;
1811 bufObj->Immutable = GL_TRUE;
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05001812 bufObj->MinMaxCacheDirty = true;
Marek Olšáke592f112014-01-27 21:22:43 +01001813
Timothy Arceriba6eee22017-07-26 16:50:44 +10001814 if (memObj) {
1815 assert(ctx->Driver.BufferDataMem);
1816 res = ctx->Driver.BufferDataMem(ctx, target, size, memObj, offset,
1817 GL_DYNAMIC_DRAW, bufObj);
1818 }
1819 else {
1820 assert(ctx->Driver.BufferData);
1821 res = ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1822 flags, bufObj);
1823 }
1824
1825 if (!res) {
Marek Olšák11ebb032015-02-10 01:39:41 +01001826 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1827 /* Even though the interaction between AMD_pinned_memory and
1828 * glBufferStorage is not described in the spec, Graham Sellers
1829 * said that it should behave the same as glBufferData.
1830 */
Laura Ekstranda76808d2015-01-09 16:17:10 -08001831 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
Marek Olšák11ebb032015-02-10 01:39:41 +01001832 }
1833 else {
Laura Ekstranda76808d2015-01-09 16:17:10 -08001834 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
Marek Olšák11ebb032015-02-10 01:39:41 +01001835 }
Marek Olšáke592f112014-01-27 21:22:43 +01001836 }
1837}
1838
Timothy Arceri09687c22017-05-12 16:31:01 +10001839
1840static ALWAYS_INLINE void
1841inlined_buffer_storage(GLenum target, GLuint buffer, GLsizeiptr size,
Timothy Arceriba6eee22017-07-26 16:50:44 +10001842 const GLvoid *data, GLbitfield flags,
1843 GLuint memory, GLuint64 offset,
1844 bool dsa, bool mem, bool no_error, const char *func)
Timothy Arceri09687c22017-05-12 16:31:01 +10001845{
1846 GET_CURRENT_CONTEXT(ctx);
1847 struct gl_buffer_object *bufObj;
Timothy Arceriba6eee22017-07-26 16:50:44 +10001848 struct gl_memory_object *memObj = NULL;
1849
1850 if (mem) {
Samuel Pitoiset46a8c4e2017-08-21 22:22:29 +02001851 if (!no_error) {
1852 if (!ctx->Extensions.EXT_memory_object) {
1853 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
1854 return;
1855 }
1856
1857 /* From the EXT_external_objects spec:
1858 *
1859 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1860 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1861 */
1862 if (memory == 0) {
1863 _mesa_error(ctx, GL_INVALID_VALUE, "%s(memory == 0)", func);
1864 }
Timothy Arceriba6eee22017-07-26 16:50:44 +10001865 }
1866
1867 memObj = _mesa_lookup_memory_object(ctx, memory);
1868 if (!memObj)
1869 return;
1870
1871 /* From the EXT_external_objects spec:
1872 *
1873 * "An INVALID_OPERATION error is generated if <memory> names a
1874 * valid memory object which has no associated memory."
1875 */
1876 if (!no_error && !memObj->Immutable) {
1877 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no associated memory)",
1878 func);
1879 return;
1880 }
1881 }
Timothy Arceri09687c22017-05-12 16:31:01 +10001882
1883 if (dsa) {
Timothy Arcericdbfb192017-05-12 16:43:30 +10001884 if (no_error) {
1885 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1886 } else {
1887 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
1888 if (!bufObj)
1889 return;
1890 }
Timothy Arceri09687c22017-05-12 16:31:01 +10001891 } else {
Timothy Arceri624dc282017-05-12 16:51:24 +10001892 if (no_error) {
1893 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
1894 bufObj = *bufObjPtr;
1895 } else {
1896 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
1897 if (!bufObj)
1898 return;
1899 }
Timothy Arceri09687c22017-05-12 16:31:01 +10001900 }
1901
Timothy Arcericdbfb192017-05-12 16:43:30 +10001902 if (no_error || validate_buffer_storage(ctx, bufObj, size, flags, func))
Timothy Arceriba6eee22017-07-26 16:50:44 +10001903 buffer_storage(ctx, bufObj, memObj, target, size, data, flags, offset, func);
Timothy Arceri09687c22017-05-12 16:31:01 +10001904}
1905
1906
Laura Ekstranda76808d2015-01-09 16:17:10 -08001907void GLAPIENTRY
Timothy Arceri624dc282017-05-12 16:51:24 +10001908_mesa_BufferStorage_no_error(GLenum target, GLsizeiptr size,
1909 const GLvoid *data, GLbitfield flags)
1910{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001911 inlined_buffer_storage(target, 0, size, data, flags, GL_NONE, 0,
1912 false, false, true, "glBufferStorage");
Timothy Arceri624dc282017-05-12 16:51:24 +10001913}
1914
1915
1916void GLAPIENTRY
Laura Ekstranda76808d2015-01-09 16:17:10 -08001917_mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1918 GLbitfield flags)
1919{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001920 inlined_buffer_storage(target, 0, size, data, flags, GL_NONE, 0,
1921 false, false, false, "glBufferStorage");
Laura Ekstranda76808d2015-01-09 16:17:10 -08001922}
1923
Timothy Arcerieec5c012018-05-18 13:23:15 +10001924void GLAPIENTRY
1925_mesa_NamedBufferStorageEXT(GLuint buffer, GLsizeiptr size,
1926 const GLvoid *data, GLbitfield flags)
1927{
1928 GET_CURRENT_CONTEXT(ctx);
1929
1930 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1931 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
1932 &bufObj, "glNamedBufferStorageEXT"))
1933 return;
1934
1935 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
1936 true, false, false, "glNamedBufferStorageEXT");
1937}
1938
Timothy Arcericdbfb192017-05-12 16:43:30 +10001939
1940void GLAPIENTRY
Andres Rodriguez322ee1b2017-07-12 18:45:06 -04001941_mesa_BufferStorageMemEXT(GLenum target, GLsizeiptr size,
1942 GLuint memory, GLuint64 offset)
1943{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001944 inlined_buffer_storage(target, 0, size, NULL, 0, memory, offset,
1945 false, true, false, "glBufferStorageMemEXT");
Andres Rodriguez322ee1b2017-07-12 18:45:06 -04001946}
1947
1948
1949void GLAPIENTRY
1950_mesa_BufferStorageMemEXT_no_error(GLenum target, GLsizeiptr size,
1951 GLuint memory, GLuint64 offset)
1952{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001953 inlined_buffer_storage(target, 0, size, NULL, 0, memory, offset,
1954 false, true, true, "glBufferStorageMemEXT");
Andres Rodriguez322ee1b2017-07-12 18:45:06 -04001955}
1956
1957
1958void GLAPIENTRY
Timothy Arcericdbfb192017-05-12 16:43:30 +10001959_mesa_NamedBufferStorage_no_error(GLuint buffer, GLsizeiptr size,
1960 const GLvoid *data, GLbitfield flags)
1961{
1962 /* In direct state access, buffer objects have an unspecified target
1963 * since they are not required to be bound.
1964 */
Timothy Arceriba6eee22017-07-26 16:50:44 +10001965 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
1966 true, false, true, "glNamedBufferStorage");
Timothy Arcericdbfb192017-05-12 16:43:30 +10001967}
1968
1969
Laura Ekstranda76808d2015-01-09 16:17:10 -08001970void GLAPIENTRY
1971_mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1972 GLbitfield flags)
1973{
Timothy Arceri09687c22017-05-12 16:31:01 +10001974 /* In direct state access, buffer objects have an unspecified target
1975 * since they are not required to be bound.
Laura Ekstranda76808d2015-01-09 16:17:10 -08001976 */
Timothy Arceriba6eee22017-07-26 16:50:44 +10001977 inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
1978 true, false, false, "glNamedBufferStorage");
Laura Ekstranda76808d2015-01-09 16:17:10 -08001979}
1980
Andres Rodriguez322ee1b2017-07-12 18:45:06 -04001981void GLAPIENTRY
1982_mesa_NamedBufferStorageMemEXT(GLuint buffer, GLsizeiptr size,
1983 GLuint memory, GLuint64 offset)
1984{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001985 inlined_buffer_storage(GL_NONE, buffer, size, GL_NONE, 0, memory, offset,
1986 true, true, false, "glNamedBufferStorageMemEXT");
Andres Rodriguez322ee1b2017-07-12 18:45:06 -04001987}
1988
1989
1990void GLAPIENTRY
1991_mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer, GLsizeiptr size,
1992 GLuint memory, GLuint64 offset)
1993{
Timothy Arceriba6eee22017-07-26 16:50:44 +10001994 inlined_buffer_storage(GL_NONE, buffer, size, GL_NONE, 0, memory, offset,
1995 true, true, true, "glNamedBufferStorageMemEXT");
Andres Rodriguez322ee1b2017-07-12 18:45:06 -04001996}
1997
Laura Ekstranda76808d2015-01-09 16:17:10 -08001998
Samuel Pitoiset7f190182017-07-20 10:43:30 +02001999static ALWAYS_INLINE void
2000buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2001 GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage,
2002 const char *func, bool no_error)
Brian Paul6061df02003-03-29 17:01:00 +00002003{
Ian Romanickbd4e5dd2012-07-25 16:08:12 -07002004 bool valid_usage;
Brian Paulaac73252003-04-09 02:31:35 +00002005
Jordan Justen36db91c2015-12-29 14:24:09 -08002006 if (MESA_VERBOSE & VERBOSE_API) {
Laura Ekstrandcb568352015-02-09 17:57:46 -08002007 _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
2008 func,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002009 _mesa_enum_to_string(target),
Brian Paul2634e922011-02-08 19:19:34 -07002010 (long int) size, data,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002011 _mesa_enum_to_string(usage));
Jordan Justen36db91c2015-12-29 14:24:09 -08002012 }
Brian Paul2634e922011-02-08 19:19:34 -07002013
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002014 if (!no_error) {
2015 if (size < 0) {
2016 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
2017 return;
2018 }
Brian Paulaac73252003-04-09 02:31:35 +00002019
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002020 switch (usage) {
2021 case GL_STREAM_DRAW_ARB:
2022 valid_usage = (ctx->API != API_OPENGLES);
2023 break;
2024 case GL_STATIC_DRAW_ARB:
2025 case GL_DYNAMIC_DRAW_ARB:
2026 valid_usage = true;
2027 break;
2028 case GL_STREAM_READ_ARB:
2029 case GL_STREAM_COPY_ARB:
2030 case GL_STATIC_READ_ARB:
2031 case GL_STATIC_COPY_ARB:
2032 case GL_DYNAMIC_READ_ARB:
2033 case GL_DYNAMIC_COPY_ARB:
2034 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
2035 break;
2036 default:
2037 valid_usage = false;
2038 break;
2039 }
Ian Romanickbd4e5dd2012-07-25 16:08:12 -07002040
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002041 if (!valid_usage) {
2042 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
2043 _mesa_enum_to_string(usage));
2044 return;
2045 }
Ian Romanickbd4e5dd2012-07-25 16:08:12 -07002046
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002047 if (bufObj->Immutable || bufObj->HandleAllocated) {
2048 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
2049 return;
2050 }
Marek Olšáke592f112014-01-27 21:22:43 +01002051 }
2052
Marek Olšákdca35022014-02-06 19:24:23 +01002053 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2054 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
Brian Paul66e6e3e2003-09-17 21:18:22 +00002055
Marek Olšák95d9fdc2017-06-10 00:48:53 +02002056 FLUSH_VERTICES(ctx, 0);
Brian Paulb9d0f942009-05-06 15:17:25 -06002057
Brian Pauldcca97a2009-05-06 15:23:09 -06002058 bufObj->Written = GL_TRUE;
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05002059 bufObj->MinMaxCacheDirty = true;
Brian Pauldcca97a2009-05-06 15:23:09 -06002060
Brian Paule7927622009-06-03 15:38:57 -06002061#ifdef VBO_DEBUG
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05002062 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
Brian Paule7927622009-06-03 15:38:57 -06002063 bufObj->Name, size, data, usage);
2064#endif
2065
Brian Paul4a7fd632009-06-09 11:53:19 -06002066#ifdef BOUNDS_CHECK
2067 size += 100;
2068#endif
Brian Paul2f6d2a92009-09-03 09:41:41 -06002069
Matt Turnerbfcdb842015-02-20 20:18:47 -08002070 assert(ctx->Driver.BufferData);
Marek Olšák7e548d02014-01-27 21:15:19 +01002071 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
2072 GL_MAP_READ_BIT |
2073 GL_MAP_WRITE_BIT |
2074 GL_DYNAMIC_STORAGE_BIT,
2075 bufObj)) {
Marek Olšák11ebb032015-02-10 01:39:41 +01002076 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002077 if (!no_error) {
2078 /* From GL_AMD_pinned_memory:
2079 *
2080 * INVALID_OPERATION is generated by BufferData if <target> is
2081 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2082 * mapped to the GPU address space.
2083 */
2084 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
2085 }
2086 } else {
Laura Ekstrandcb568352015-02-09 17:57:46 -08002087 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
Marek Olšák11ebb032015-02-10 01:39:41 +01002088 }
Brian Paul2f6d2a92009-09-03 09:41:41 -06002089 }
Brian Paul6061df02003-03-29 17:01:00 +00002090}
2091
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002092static void
2093buffer_data_error(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2094 GLenum target, GLsizeiptr size, const GLvoid *data,
2095 GLenum usage, const char *func)
2096{
2097 buffer_data(ctx, bufObj, target, size, data, usage, func, false);
2098}
2099
Samuel Pitoiset3f051932017-07-20 10:59:41 +02002100static void
2101buffer_data_no_error(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2102 GLenum target, GLsizeiptr size, const GLvoid *data,
2103 GLenum usage, const char *func)
2104{
2105 buffer_data(ctx, bufObj, target, size, data, usage, func, true);
2106}
2107
Samuel Pitoiset7f190182017-07-20 10:43:30 +02002108void
2109_mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2110 GLenum target, GLsizeiptr size, const GLvoid *data,
2111 GLenum usage, const char *func)
2112{
2113 buffer_data_error(ctx, bufObj, target, size, data, usage, func);
2114}
2115
Laura Ekstrandcb568352015-02-09 17:57:46 -08002116void GLAPIENTRY
Samuel Pitoiset3f051932017-07-20 10:59:41 +02002117_mesa_BufferData_no_error(GLenum target, GLsizeiptr size, const GLvoid *data,
2118 GLenum usage)
2119{
2120 GET_CURRENT_CONTEXT(ctx);
2121
2122 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
2123 buffer_data_no_error(ctx, *bufObj, target, size, data, usage,
2124 "glBufferData");
2125}
2126
2127void GLAPIENTRY
Laura Ekstrandcb568352015-02-09 17:57:46 -08002128_mesa_BufferData(GLenum target, GLsizeiptr size,
2129 const GLvoid *data, GLenum usage)
2130{
2131 GET_CURRENT_CONTEXT(ctx);
2132 struct gl_buffer_object *bufObj;
2133
2134 bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
2135 if (!bufObj)
2136 return;
2137
2138 _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
2139 "glBufferData");
2140}
2141
2142void GLAPIENTRY
Samuel Pitoiset3f051932017-07-20 10:59:41 +02002143_mesa_NamedBufferData_no_error(GLuint buffer, GLsizeiptr size,
2144 const GLvoid *data, GLenum usage)
2145{
2146 GET_CURRENT_CONTEXT(ctx);
2147
2148 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2149 buffer_data_no_error(ctx, bufObj, GL_NONE, size, data, usage,
2150 "glNamedBufferData");
2151}
2152
2153void GLAPIENTRY
Laura Ekstrandcb568352015-02-09 17:57:46 -08002154_mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
2155 GLenum usage)
2156{
2157 GET_CURRENT_CONTEXT(ctx);
2158 struct gl_buffer_object *bufObj;
2159
2160 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
2161 if (!bufObj)
2162 return;
2163
2164 /* In direct state access, buffer objects have an unspecified target since
2165 * they are not required to be bound.
2166 */
2167 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
2168 "glNamedBufferData");
2169}
2170
Timothy Arceri83ed9482018-05-18 15:20:35 +10002171void GLAPIENTRY
2172_mesa_NamedBufferDataEXT(GLuint buffer, GLsizeiptr size, const GLvoid *data,
2173 GLenum usage)
2174{
2175 GET_CURRENT_CONTEXT(ctx);
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002176 struct gl_buffer_object *bufObj;
Timothy Arceri83ed9482018-05-18 15:20:35 +10002177
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002178 if (!buffer) {
2179 _mesa_error(ctx, GL_INVALID_OPERATION,
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02002180 "glNamedBufferDataEXT(buffer=0)");
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002181 return;
2182 }
2183
2184 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Timothy Arceri83ed9482018-05-18 15:20:35 +10002185 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2186 &bufObj, "glNamedBufferDataEXT"))
2187 return;
2188
2189 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
2190 "glNamedBufferDataEXT");
2191}
Brian Paulc7b872a2003-09-09 13:44:40 +00002192
Timothy Arceri5d29e6a2017-05-12 17:10:10 +10002193static bool
2194validate_buffer_sub_data(struct gl_context *ctx,
2195 struct gl_buffer_object *bufObj,
2196 GLintptr offset, GLsizeiptr size,
2197 const char *func)
2198{
2199 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
2200 true, func)) {
2201 /* error already recorded */
2202 return false;
2203 }
2204
2205 if (bufObj->Immutable &&
2206 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
2207 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
2208 return false;
2209 }
2210
2211 if ((bufObj->Usage == GL_STATIC_DRAW ||
2212 bufObj->Usage == GL_STATIC_COPY) &&
2213 bufObj->NumSubDataCalls >= BUFFER_WARNING_CALL_COUNT - 1) {
2214 /* If the application declared the buffer as static draw/copy or stream
2215 * draw, it should not be frequently modified with glBufferSubData.
2216 */
2217 BUFFER_USAGE_WARNING(ctx,
2218 "using %s(buffer %u, offset %u, size %u) to "
2219 "update a %s buffer",
2220 func, bufObj->Name, offset, size,
2221 _mesa_enum_to_string(bufObj->Usage));
2222 }
2223
2224 return true;
2225}
2226
2227
Laura Ekstrand566ccdf2015-01-13 11:28:17 -08002228/**
2229 * Implementation for glBufferSubData and glNamedBufferSubData.
2230 *
2231 * \param ctx GL context.
2232 * \param bufObj The buffer object.
2233 * \param offset Offset of the first byte of the subdata range.
2234 * \param size Size, in bytes, of the subdata range.
2235 * \param data The data store.
2236 * \param func Name of calling function for recording errors.
2237 *
2238 */
2239void
2240_mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
Timothy Arceri5d29e6a2017-05-12 17:10:10 +10002241 GLintptr offset, GLsizeiptr size, const GLvoid *data)
Brian Paul6061df02003-03-29 17:01:00 +00002242{
Brian Paul6e2e1362010-11-09 12:24:49 -07002243 if (size == 0)
2244 return;
2245
Brian Paulab0651c2015-12-07 18:38:03 -07002246 bufObj->NumSubDataCalls++;
Brian Pauldcca97a2009-05-06 15:23:09 -06002247 bufObj->Written = GL_TRUE;
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05002248 bufObj->MinMaxCacheDirty = true;
Brian Pauldcca97a2009-05-06 15:23:09 -06002249
Matt Turnerbfcdb842015-02-20 20:18:47 -08002250 assert(ctx->Driver.BufferSubData);
Laura Ekstrand9cb732b2015-02-11 11:06:42 -08002251 ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
Brian Paul6061df02003-03-29 17:01:00 +00002252}
2253
Timothy Arcericab148c2017-05-12 21:21:38 +10002254
2255static ALWAYS_INLINE void
2256buffer_sub_data(GLenum target, GLuint buffer, GLintptr offset,
2257 GLsizeiptr size, const GLvoid *data,
Timothy Arceri7ec12292017-05-12 21:29:11 +10002258 bool dsa, bool no_error, const char *func)
Timothy Arcericab148c2017-05-12 21:21:38 +10002259{
2260 GET_CURRENT_CONTEXT(ctx);
2261 struct gl_buffer_object *bufObj;
2262
2263 if (dsa) {
Timothy Arceri7ec12292017-05-12 21:29:11 +10002264 if (no_error) {
2265 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2266 } else {
2267 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
2268 if (!bufObj)
2269 return;
2270 }
Timothy Arcericab148c2017-05-12 21:21:38 +10002271 } else {
Timothy Arcerib3888b72017-05-12 21:36:25 +10002272 if (no_error) {
2273 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2274 bufObj = *bufObjPtr;
2275 } else {
2276 bufObj = get_buffer(ctx, func, target, GL_INVALID_OPERATION);
2277 if (!bufObj)
2278 return;
2279 }
Timothy Arcericab148c2017-05-12 21:21:38 +10002280 }
2281
Timothy Arceri7ec12292017-05-12 21:29:11 +10002282 if (no_error || validate_buffer_sub_data(ctx, bufObj, offset, size, func))
Timothy Arcericab148c2017-05-12 21:21:38 +10002283 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data);
2284}
2285
2286
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002287void GLAPIENTRY
Timothy Arcerib3888b72017-05-12 21:36:25 +10002288_mesa_BufferSubData_no_error(GLenum target, GLintptr offset,
2289 GLsizeiptr size, const GLvoid *data)
2290{
2291 buffer_sub_data(target, 0, offset, size, data, false, true,
2292 "glBufferSubData");
2293}
2294
2295
2296void GLAPIENTRY
Laura Ekstrand566ccdf2015-01-13 11:28:17 -08002297_mesa_BufferSubData(GLenum target, GLintptr offset,
2298 GLsizeiptr size, const GLvoid *data)
Brian Paul6061df02003-03-29 17:01:00 +00002299{
Timothy Arceri7ec12292017-05-12 21:29:11 +10002300 buffer_sub_data(target, 0, offset, size, data, false, false,
2301 "glBufferSubData");
2302}
2303
2304void GLAPIENTRY
2305_mesa_NamedBufferSubData_no_error(GLuint buffer, GLintptr offset,
2306 GLsizeiptr size, const GLvoid *data)
2307{
2308 buffer_sub_data(0, buffer, offset, size, data, true, true,
2309 "glNamedBufferSubData");
Laura Ekstrand566ccdf2015-01-13 11:28:17 -08002310}
2311
2312void GLAPIENTRY
2313_mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
2314 GLsizeiptr size, const GLvoid *data)
2315{
Timothy Arceri7ec12292017-05-12 21:29:11 +10002316 buffer_sub_data(0, buffer, offset, size, data, true, false,
Timothy Arcericab148c2017-05-12 21:21:38 +10002317 "glNamedBufferSubData");
Laura Ekstrand566ccdf2015-01-13 11:28:17 -08002318}
2319
Timothy Arceri83ed9482018-05-18 15:20:35 +10002320void GLAPIENTRY
2321_mesa_NamedBufferSubDataEXT(GLuint buffer, GLintptr offset,
2322 GLsizeiptr size, const GLvoid *data)
2323{
2324 GET_CURRENT_CONTEXT(ctx);
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002325 struct gl_buffer_object *bufObj;
Timothy Arceri83ed9482018-05-18 15:20:35 +10002326
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002327 if (!buffer) {
2328 _mesa_error(ctx, GL_INVALID_OPERATION,
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02002329 "glNamedBufferSubDataEXT(buffer=0)");
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002330 return;
2331 }
2332
2333 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Timothy Arceri83ed9482018-05-18 15:20:35 +10002334 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2335 &bufObj, "glNamedBufferSubDataEXT"))
2336 return;
2337
2338 if (validate_buffer_sub_data(ctx, bufObj, offset, size,
2339 "glNamedBufferSubDataEXT")) {
2340 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data);
2341 }
2342}
2343
Laura Ekstrand566ccdf2015-01-13 11:28:17 -08002344
2345void GLAPIENTRY
2346_mesa_GetBufferSubData(GLenum target, GLintptr offset,
2347 GLsizeiptr size, GLvoid *data)
2348{
2349 GET_CURRENT_CONTEXT(ctx);
2350 struct gl_buffer_object *bufObj;
2351
2352 bufObj = get_buffer(ctx, "glGetBufferSubData", target,
2353 GL_INVALID_OPERATION);
2354 if (!bufObj)
2355 return;
2356
2357 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
2358 "glGetBufferSubData")) {
Brian Paulaa00d122003-09-15 19:55:10 +00002359 return;
Brian Paulaac73252003-04-09 02:31:35 +00002360 }
Brian Paul66e6e3e2003-09-17 21:18:22 +00002361
Matt Turnerbfcdb842015-02-20 20:18:47 -08002362 assert(ctx->Driver.GetBufferSubData);
Laura Ekstrand579297c2015-02-11 16:53:46 -08002363 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
Brian Paul6061df02003-03-29 17:01:00 +00002364}
2365
Laura Ekstrand23eab472015-01-20 15:24:53 -08002366void GLAPIENTRY
2367_mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
2368 GLsizeiptr size, GLvoid *data)
2369{
2370 GET_CURRENT_CONTEXT(ctx);
2371 struct gl_buffer_object *bufObj;
2372
2373 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2374 "glGetNamedBufferSubData");
2375 if (!bufObj)
2376 return;
2377
2378 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
2379 "glGetNamedBufferSubData")) {
2380 return;
2381 }
2382
2383 assert(ctx->Driver.GetBufferSubData);
2384 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
2385}
2386
2387
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02002388void GLAPIENTRY
2389_mesa_GetNamedBufferSubDataEXT(GLuint buffer, GLintptr offset,
2390 GLsizeiptr size, GLvoid *data)
2391{
2392 GET_CURRENT_CONTEXT(ctx);
2393 struct gl_buffer_object *bufObj;
2394
2395 if (!buffer) {
2396 _mesa_error(ctx, GL_INVALID_OPERATION,
2397 "glGetNamedBufferSubDataEXT(buffer=0)");
2398 return;
2399 }
2400
2401 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2402 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2403 &bufObj, "glGetNamedBufferSubDataEXT"))
2404 return;
2405
2406 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
2407 "glGetNamedBufferSubDataEXT")) {
2408 return;
2409 }
2410
2411 assert(ctx->Driver.GetBufferSubData);
2412 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
2413}
2414
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002415/**
2416 * \param subdata true if caller is *SubData, false if *Data
2417 */
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002418static ALWAYS_INLINE void
Timothy Arcerif9c28b92017-05-04 13:59:29 +10002419clear_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
2420 GLenum internalformat, GLintptr offset, GLsizeiptr size,
2421 GLenum format, GLenum type, const GLvoid *data,
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002422 const char *func, bool subdata, bool no_error)
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002423{
Mark Mueller71fe9432014-01-04 14:11:43 -08002424 mesa_format mesaFormat;
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002425 GLubyte clearValue[MAX_PIXEL_BYTES];
2426 GLsizeiptr clearValueSize;
2427
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002428 /* This checks for disallowed mappings. */
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002429 if (!no_error && !buffer_object_subdata_range_good(ctx, bufObj, offset, size,
2430 subdata, func)) {
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002431 return;
2432 }
2433
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002434 if (no_error) {
2435 mesaFormat = _mesa_get_texbuffer_format(ctx, internalformat);
2436 } else {
2437 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
2438 format, type, func);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002439 }
2440
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002441 if (mesaFormat == MESA_FORMAT_NONE)
2442 return;
2443
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002444 clearValueSize = _mesa_get_format_bytes(mesaFormat);
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002445 if (!no_error &&
2446 (offset % clearValueSize != 0 || size % clearValueSize != 0)) {
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002447 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002448 "%s(offset or size is not a multiple of "
2449 "internalformat size)", func);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002450 return;
2451 }
2452
Nicolai Hähnle46b7a522016-01-06 17:57:49 -05002453 /* Bail early. Negative size has already been checked. */
2454 if (size == 0)
2455 return;
2456
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05002457 bufObj->MinMaxCacheDirty = true;
2458
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002459 if (data == NULL) {
2460 /* clear to zeros, per the spec */
Nicolai Hähnle46b7a522016-01-06 17:57:49 -05002461 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2462 NULL, clearValueSize, bufObj);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002463 return;
2464 }
2465
2466 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002467 format, type, data, func)) {
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002468 return;
2469 }
2470
Nicolai Hähnle46b7a522016-01-06 17:57:49 -05002471 ctx->Driver.ClearBufferSubData(ctx, offset, size,
2472 clearValue, clearValueSize, bufObj);
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002473}
2474
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002475static void
2476clear_buffer_sub_data_error(struct gl_context *ctx,
2477 struct gl_buffer_object *bufObj,
2478 GLenum internalformat, GLintptr offset,
2479 GLsizeiptr size, GLenum format, GLenum type,
2480 const GLvoid *data, const char *func, bool subdata)
2481{
2482 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
2483 type, data, func, subdata, false);
2484}
2485
Samuel Pitoiset0127af12017-07-21 12:02:26 +02002486
2487static void
2488clear_buffer_sub_data_no_error(struct gl_context *ctx,
2489 struct gl_buffer_object *bufObj,
2490 GLenum internalformat, GLintptr offset,
2491 GLsizeiptr size, GLenum format, GLenum type,
2492 const GLvoid *data, const char *func,
2493 bool subdata)
2494{
2495 clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
2496 type, data, func, subdata, true);
2497}
2498
2499
2500void GLAPIENTRY
2501_mesa_ClearBufferData_no_error(GLenum target, GLenum internalformat,
2502 GLenum format, GLenum type, const GLvoid *data)
2503{
2504 GET_CURRENT_CONTEXT(ctx);
2505
2506 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
2507 clear_buffer_sub_data_no_error(ctx, *bufObj, internalformat, 0,
2508 (*bufObj)->Size, format, type, data,
2509 "glClearBufferData", false);
2510}
2511
2512
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002513void GLAPIENTRY
2514_mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
2515 GLenum type, const GLvoid *data)
2516{
2517 GET_CURRENT_CONTEXT(ctx);
2518 struct gl_buffer_object *bufObj;
2519
2520 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
2521 if (!bufObj)
2522 return;
2523
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002524 clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2525 format, type, data, "glClearBufferData", false);
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002526}
2527
Samuel Pitoiset0127af12017-07-21 12:02:26 +02002528
2529void GLAPIENTRY
2530_mesa_ClearNamedBufferData_no_error(GLuint buffer, GLenum internalformat,
2531 GLenum format, GLenum type,
2532 const GLvoid *data)
2533{
2534 GET_CURRENT_CONTEXT(ctx);
2535
2536 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2537 clear_buffer_sub_data_no_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2538 format, type, data, "glClearNamedBufferData",
2539 false);
2540}
2541
2542
Laura Ekstrand5030d0a2015-02-11 12:17:38 -08002543void GLAPIENTRY
2544_mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
2545 GLenum format, GLenum type, const GLvoid *data)
2546{
2547 GET_CURRENT_CONTEXT(ctx);
2548 struct gl_buffer_object *bufObj;
2549
2550 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
2551 if (!bufObj)
2552 return;
2553
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002554 clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2555 format, type, data, "glClearNamedBufferData",
2556 false);
Laura Ekstrand5030d0a2015-02-11 12:17:38 -08002557}
2558
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002559
2560void GLAPIENTRY
Pierre-Eric Pelloux-Prayere3385eb2019-11-05 15:37:12 +01002561_mesa_ClearNamedBufferDataEXT(GLuint buffer, GLenum internalformat,
2562 GLenum format, GLenum type, const GLvoid *data)
2563{
2564 GET_CURRENT_CONTEXT(ctx);
2565 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2566 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2567 &bufObj, "glClearNamedBufferDataEXT"))
2568 return;
2569
2570 clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
2571 format, type, data, "glClearNamedBufferDataEXT",
2572 false);
2573}
2574
2575
2576void GLAPIENTRY
Samuel Pitoiset0127af12017-07-21 12:02:26 +02002577_mesa_ClearBufferSubData_no_error(GLenum target, GLenum internalformat,
2578 GLintptr offset, GLsizeiptr size,
2579 GLenum format, GLenum type,
2580 const GLvoid *data)
2581{
2582 GET_CURRENT_CONTEXT(ctx);
2583
2584 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
2585 clear_buffer_sub_data_no_error(ctx, *bufObj, internalformat, offset, size,
2586 format, type, data, "glClearBufferSubData",
2587 true);
2588}
2589
2590
2591void GLAPIENTRY
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002592_mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
2593 GLintptr offset, GLsizeiptr size,
2594 GLenum format, GLenum type,
Laura Ekstrand16244522015-02-11 11:45:57 -08002595 const GLvoid *data)
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002596{
2597 GET_CURRENT_CONTEXT(ctx);
Laura Ekstrand16244522015-02-11 11:45:57 -08002598 struct gl_buffer_object *bufObj;
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002599
2600 bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
2601 if (!bufObj)
2602 return;
2603
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002604 clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
2605 format, type, data, "glClearBufferSubData",
2606 true);
Laura Ekstrand9fa6c3632015-01-13 15:20:19 -08002607}
2608
Samuel Pitoiset0127af12017-07-21 12:02:26 +02002609
2610void GLAPIENTRY
2611_mesa_ClearNamedBufferSubData_no_error(GLuint buffer, GLenum internalformat,
2612 GLintptr offset, GLsizeiptr size,
2613 GLenum format, GLenum type,
2614 const GLvoid *data)
2615{
2616 GET_CURRENT_CONTEXT(ctx);
2617
2618 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2619 clear_buffer_sub_data_no_error(ctx, bufObj, internalformat, offset, size,
2620 format, type, data,
2621 "glClearNamedBufferSubData", true);
2622}
2623
2624
Laura Ekstrand5030d0a2015-02-11 12:17:38 -08002625void GLAPIENTRY
2626_mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
2627 GLintptr offset, GLsizeiptr size,
2628 GLenum format, GLenum type,
2629 const GLvoid *data)
2630{
2631 GET_CURRENT_CONTEXT(ctx);
2632 struct gl_buffer_object *bufObj;
2633
2634 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2635 "glClearNamedBufferSubData");
2636 if (!bufObj)
2637 return;
2638
Samuel Pitoiset589450c2017-07-21 11:51:47 +02002639 clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
2640 format, type, data, "glClearNamedBufferSubData",
2641 true);
Laura Ekstrand5030d0a2015-02-11 12:17:38 -08002642}
2643
Pierre-Eric Pelloux-Prayere3385eb2019-11-05 15:37:12 +01002644void GLAPIENTRY
2645_mesa_ClearNamedBufferSubDataEXT(GLuint buffer, GLenum internalformat,
2646 GLintptr offset, GLsizeiptr size,
2647 GLenum format, GLenum type,
2648 const GLvoid *data)
2649{
2650 GET_CURRENT_CONTEXT(ctx);
2651 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2652 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2653 &bufObj, "glClearNamedBufferSubDataEXT"))
2654 return;
2655
2656 clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
2657 format, type, data, "glClearNamedBufferSubDataEXT",
2658 true);
2659}
2660
Timothy Arceri0b2e4da2017-05-04 13:26:45 +10002661static GLboolean
2662unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj)
2663{
2664 GLboolean status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
2665 bufObj->Mappings[MAP_USER].AccessFlags = 0;
2666 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
2667 assert(bufObj->Mappings[MAP_USER].Offset == 0);
2668 assert(bufObj->Mappings[MAP_USER].Length == 0);
2669
2670 return status;
2671}
Pi Tabred5f7bc0c2013-12-14 10:32:00 -07002672
Timothy Arceri6c376862017-05-04 13:14:27 +10002673static GLboolean
Timothy Arceri0b2e4da2017-05-04 13:26:45 +10002674validate_and_unmap_buffer(struct gl_context *ctx,
2675 struct gl_buffer_object *bufObj,
2676 const char *func)
Brian Paul6061df02003-03-29 17:01:00 +00002677{
Brian Paulaac73252003-04-09 02:31:35 +00002678 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2679
Marek Olšákdca35022014-02-06 19:24:23 +01002680 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002681 _mesa_error(ctx, GL_INVALID_OPERATION,
2682 "%s(buffer is not mapped)", func);
Ian Romanick0207b472003-09-09 00:10:12 +00002683 return GL_FALSE;
2684 }
2685
Brian Paul4a7fd632009-06-09 11:53:19 -06002686#ifdef BOUNDS_CHECK
2687 if (bufObj->Access != GL_READ_ONLY_ARB) {
2688 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2689 GLuint i;
2690 /* check that last 100 bytes are still = magic value */
2691 for (i = 0; i < 100; i++) {
2692 GLuint pos = bufObj->Size - i - 1;
2693 if (buf[pos] != 123) {
2694 _mesa_warning(ctx, "Out of bounds buffer object write detected"
2695 " at position %d (value = %u)\n",
2696 pos, buf[pos]);
2697 }
2698 }
2699 }
2700#endif
2701
Brian Paule7927622009-06-03 15:38:57 -06002702#ifdef VBO_DEBUG
Brian Paule75b2832009-06-08 17:02:36 -06002703 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
Brian Paule7927622009-06-03 15:38:57 -06002704 GLuint i, unchanged = 0;
2705 GLubyte *b = (GLubyte *) bufObj->Pointer;
2706 GLint pos = -1;
2707 /* check which bytes changed */
2708 for (i = 0; i < bufObj->Size - 1; i++) {
2709 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
2710 unchanged++;
2711 if (pos == -1)
2712 pos = i;
2713 }
2714 }
2715 if (unchanged) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05002716 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
Brian Paule7927622009-06-03 15:38:57 -06002717 bufObj->Name, unchanged, bufObj->Size, pos);
2718 }
2719 }
2720#endif
2721
Timothy Arceri0b2e4da2017-05-04 13:26:45 +10002722 return unmap_buffer(ctx, bufObj);
Brian Paul6061df02003-03-29 17:01:00 +00002723}
2724
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002725GLboolean GLAPIENTRY
Timothy Arceribbae62c2017-05-04 13:31:27 +10002726_mesa_UnmapBuffer_no_error(GLenum target)
2727{
2728 GET_CURRENT_CONTEXT(ctx);
2729 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
2730 struct gl_buffer_object *bufObj = *bufObjPtr;
2731
2732 return unmap_buffer(ctx, bufObj);
2733}
2734
2735GLboolean GLAPIENTRY
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002736_mesa_UnmapBuffer(GLenum target)
2737{
2738 GET_CURRENT_CONTEXT(ctx);
2739 struct gl_buffer_object *bufObj;
2740
2741 bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
2742 if (!bufObj)
2743 return GL_FALSE;
2744
Timothy Arceri0b2e4da2017-05-04 13:26:45 +10002745 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002746}
2747
2748GLboolean GLAPIENTRY
Timothy Arceri76e25ed2018-09-05 15:18:04 +10002749_mesa_UnmapNamedBufferEXT_no_error(GLuint buffer)
Timothy Arceribbae62c2017-05-04 13:31:27 +10002750{
2751 GET_CURRENT_CONTEXT(ctx);
2752 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2753
2754 return unmap_buffer(ctx, bufObj);
2755}
2756
2757GLboolean GLAPIENTRY
Timothy Arceri76e25ed2018-09-05 15:18:04 +10002758_mesa_UnmapNamedBufferEXT(GLuint buffer)
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002759{
2760 GET_CURRENT_CONTEXT(ctx);
2761 struct gl_buffer_object *bufObj;
2762
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002763 if (!buffer) {
2764 _mesa_error(ctx, GL_INVALID_OPERATION,
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02002765 "glUnmapNamedBufferEXT(buffer=0)");
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02002766 return GL_FALSE;
2767 }
2768
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002769 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
2770 if (!bufObj)
2771 return GL_FALSE;
2772
Timothy Arceri0b2e4da2017-05-04 13:26:45 +10002773 return validate_and_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
Laura Ekstrandf7f5df92015-01-14 14:52:01 -08002774}
Brian Paulc7b872a2003-09-09 13:44:40 +00002775
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002776
2777static bool
2778get_buffer_parameter(struct gl_context *ctx,
2779 struct gl_buffer_object *bufObj, GLenum pname,
2780 GLint64 *params, const char *func)
2781{
2782 switch (pname) {
2783 case GL_BUFFER_SIZE_ARB:
2784 *params = bufObj->Size;
2785 break;
2786 case GL_BUFFER_USAGE_ARB:
2787 *params = bufObj->Usage;
2788 break;
2789 case GL_BUFFER_ACCESS_ARB:
2790 *params = simplified_access_mode(ctx,
2791 bufObj->Mappings[MAP_USER].AccessFlags);
2792 break;
2793 case GL_BUFFER_MAPPED_ARB:
2794 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
2795 break;
2796 case GL_BUFFER_ACCESS_FLAGS:
2797 if (!ctx->Extensions.ARB_map_buffer_range)
2798 goto invalid_pname;
2799 *params = bufObj->Mappings[MAP_USER].AccessFlags;
2800 break;
2801 case GL_BUFFER_MAP_OFFSET:
2802 if (!ctx->Extensions.ARB_map_buffer_range)
2803 goto invalid_pname;
2804 *params = bufObj->Mappings[MAP_USER].Offset;
2805 break;
2806 case GL_BUFFER_MAP_LENGTH:
2807 if (!ctx->Extensions.ARB_map_buffer_range)
2808 goto invalid_pname;
2809 *params = bufObj->Mappings[MAP_USER].Length;
2810 break;
2811 case GL_BUFFER_IMMUTABLE_STORAGE:
2812 if (!ctx->Extensions.ARB_buffer_storage)
2813 goto invalid_pname;
2814 *params = bufObj->Immutable;
2815 break;
2816 case GL_BUFFER_STORAGE_FLAGS:
2817 if (!ctx->Extensions.ARB_buffer_storage)
2818 goto invalid_pname;
2819 *params = bufObj->StorageFlags;
2820 break;
2821 default:
2822 goto invalid_pname;
2823 }
2824
2825 return true;
2826
2827invalid_pname:
2828 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002829 _mesa_enum_to_string(pname));
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002830 return false;
2831}
2832
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002833void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08002834_mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
Brian Paul6061df02003-03-29 17:01:00 +00002835{
Brian Paulaac73252003-04-09 02:31:35 +00002836 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00002837 struct gl_buffer_object *bufObj;
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002838 GLint64 parameter;
Brian Paulaac73252003-04-09 02:31:35 +00002839
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002840 bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
Pi Tabred7d946532013-12-14 10:32:00 -07002841 GL_INVALID_OPERATION);
Eric Anholtf20fb802012-01-25 16:30:26 -08002842 if (!bufObj)
Brian Paul4277ea42006-08-25 22:06:02 +00002843 return;
Ian Romanick0207b472003-09-09 00:10:12 +00002844
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002845 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2846 "glGetBufferParameteriv"))
2847 return; /* Error already recorded. */
Brian Pauld6a9f5b2010-03-20 11:52:12 -06002848
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002849 *params = (GLint) parameter;
Brian Paul6061df02003-03-29 17:01:00 +00002850}
2851
Brian Paul1fbc7192010-01-01 17:50:02 -07002852void GLAPIENTRY
2853_mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
2854{
2855 GET_CURRENT_CONTEXT(ctx);
2856 struct gl_buffer_object *bufObj;
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002857 GLint64 parameter;
Brian Paul1fbc7192010-01-01 17:50:02 -07002858
Pi Tabred7d946532013-12-14 10:32:00 -07002859 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
2860 GL_INVALID_OPERATION);
Eric Anholtf20fb802012-01-25 16:30:26 -08002861 if (!bufObj)
Brian Paul1fbc7192010-01-01 17:50:02 -07002862 return;
Brian Paul1fbc7192010-01-01 17:50:02 -07002863
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002864 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2865 "glGetBufferParameteri64v"))
2866 return; /* Error already recorded. */
Brian Pauld6a9f5b2010-03-20 11:52:12 -06002867
Laura Ekstrandefcb8302015-02-11 16:07:45 -08002868 *params = parameter;
Brian Paul1fbc7192010-01-01 17:50:02 -07002869}
2870
Laura Ekstrand1e457522015-02-11 16:10:20 -08002871void GLAPIENTRY
2872_mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
2873{
2874 GET_CURRENT_CONTEXT(ctx);
2875 struct gl_buffer_object *bufObj;
2876 GLint64 parameter;
2877
2878 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2879 "glGetNamedBufferParameteriv");
2880 if (!bufObj)
2881 return;
2882
2883 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2884 "glGetNamedBufferParameteriv"))
2885 return; /* Error already recorded. */
2886
2887 *params = (GLint) parameter;
2888}
2889
2890void GLAPIENTRY
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02002891_mesa_GetNamedBufferParameterivEXT(GLuint buffer, GLenum pname, GLint *params)
2892{
2893 GET_CURRENT_CONTEXT(ctx);
2894 struct gl_buffer_object *bufObj;
2895 GLint64 parameter;
2896
2897 if (!buffer) {
2898 _mesa_error(ctx, GL_INVALID_OPERATION,
2899 "glGetNamedBufferParameterivEXT: buffer=0");
2900 return;
2901 }
2902
2903 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2904 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2905 &bufObj, "glGetNamedBufferParameterivEXT"))
2906 return;
2907
2908 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2909 "glGetNamedBufferParameterivEXT"))
2910 return; /* Error already recorded. */
2911
2912 *params = (GLint) parameter;
2913}
2914
2915void GLAPIENTRY
Laura Ekstrand1e457522015-02-11 16:10:20 -08002916_mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
2917 GLint64 *params)
2918{
2919 GET_CURRENT_CONTEXT(ctx);
2920 struct gl_buffer_object *bufObj;
2921 GLint64 parameter;
2922
2923 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2924 "glGetNamedBufferParameteri64v");
2925 if (!bufObj)
2926 return;
2927
2928 if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
2929 "glGetNamedBufferParameteri64v"))
2930 return; /* Error already recorded. */
2931
2932 *params = parameter;
2933}
2934
Brian Paul1fbc7192010-01-01 17:50:02 -07002935
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002936void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08002937_mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
Brian Paul6061df02003-03-29 17:01:00 +00002938{
Brian Paulaac73252003-04-09 02:31:35 +00002939 GET_CURRENT_CONTEXT(ctx);
Laura Ekstrand3706ace2015-03-16 16:08:36 -07002940 struct gl_buffer_object *bufObj;
Brian Paulaac73252003-04-09 02:31:35 +00002941
Laura Ekstrand3706ace2015-03-16 16:08:36 -07002942 if (pname != GL_BUFFER_MAP_POINTER) {
2943 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
2944 "GL_BUFFER_MAP_POINTER)");
Brian Paulaac73252003-04-09 02:31:35 +00002945 return;
2946 }
2947
Laura Ekstrand3706ace2015-03-16 16:08:36 -07002948 bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
Pi Tabred7d946532013-12-14 10:32:00 -07002949 GL_INVALID_OPERATION);
Eric Anholtf20fb802012-01-25 16:30:26 -08002950 if (!bufObj)
Brian Paul4277ea42006-08-25 22:06:02 +00002951 return;
Ian Romanick0207b472003-09-09 00:10:12 +00002952
Marek Olšákdca35022014-02-06 19:24:23 +01002953 *params = bufObj->Mappings[MAP_USER].Pointer;
Brian Paul6061df02003-03-29 17:01:00 +00002954}
Brian Pauldc0b71f2009-06-02 20:29:57 -06002955
Laura Ekstrand105ddc62015-01-20 14:32:35 -08002956void GLAPIENTRY
2957_mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
2958{
2959 GET_CURRENT_CONTEXT(ctx);
2960 struct gl_buffer_object *bufObj;
2961
2962 if (pname != GL_BUFFER_MAP_POINTER) {
2963 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
2964 "GL_BUFFER_MAP_POINTER)");
2965 return;
2966 }
2967
2968 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2969 "glGetNamedBufferPointerv");
2970 if (!bufObj)
2971 return;
2972
2973 *params = bufObj->Mappings[MAP_USER].Pointer;
2974}
2975
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02002976void GLAPIENTRY
2977_mesa_GetNamedBufferPointervEXT(GLuint buffer, GLenum pname, GLvoid **params)
2978{
2979 GET_CURRENT_CONTEXT(ctx);
2980 struct gl_buffer_object *bufObj;
2981
2982 if (!buffer) {
2983 _mesa_error(ctx, GL_INVALID_OPERATION,
2984 "glGetNamedBufferPointervEXT(buffer=0)");
2985 return;
2986 }
2987 if (pname != GL_BUFFER_MAP_POINTER) {
2988 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointervEXT(pname != "
2989 "GL_BUFFER_MAP_POINTER)");
2990 return;
2991 }
2992
2993 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2994 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
2995 &bufObj, "glGetNamedBufferPointervEXT"))
2996 return;
2997
2998 *params = bufObj->Mappings[MAP_USER].Pointer;
2999}
Brian Pauldc0b71f2009-06-02 20:29:57 -06003000
Timothy Arceri681647e2017-05-04 14:02:10 +10003001static void
3002copy_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *src,
3003 struct gl_buffer_object *dst, GLintptr readOffset,
3004 GLintptr writeOffset, GLsizeiptr size, const char *func)
Laura Ekstrand4adaad52015-01-13 13:28:08 -08003005{
3006 if (_mesa_check_disallowed_mapping(src)) {
3007 _mesa_error(ctx, GL_INVALID_OPERATION,
3008 "%s(readBuffer is mapped)", func);
3009 return;
3010 }
3011
3012 if (_mesa_check_disallowed_mapping(dst)) {
3013 _mesa_error(ctx, GL_INVALID_OPERATION,
3014 "%s(writeBuffer is mapped)", func);
3015 return;
3016 }
3017
3018 if (readOffset < 0) {
3019 _mesa_error(ctx, GL_INVALID_VALUE,
3020 "%s(readOffset %d < 0)", func, (int) readOffset);
3021 return;
3022 }
3023
3024 if (writeOffset < 0) {
3025 _mesa_error(ctx, GL_INVALID_VALUE,
3026 "%s(writeOffset %d < 0)", func, (int) writeOffset);
3027 return;
3028 }
3029
3030 if (size < 0) {
3031 _mesa_error(ctx, GL_INVALID_VALUE,
3032 "%s(size %d < 0)", func, (int) size);
3033 return;
3034 }
3035
3036 if (readOffset + size > src->Size) {
3037 _mesa_error(ctx, GL_INVALID_VALUE,
3038 "%s(readOffset %d + size %d > src_buffer_size %d)", func,
3039 (int) readOffset, (int) size, (int) src->Size);
3040 return;
3041 }
3042
3043 if (writeOffset + size > dst->Size) {
3044 _mesa_error(ctx, GL_INVALID_VALUE,
3045 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
3046 (int) writeOffset, (int) size, (int) dst->Size);
3047 return;
3048 }
3049
3050 if (src == dst) {
3051 if (readOffset + size <= writeOffset) {
3052 /* OK */
3053 }
3054 else if (writeOffset + size <= readOffset) {
3055 /* OK */
3056 }
3057 else {
3058 /* overlapping src/dst is illegal */
3059 _mesa_error(ctx, GL_INVALID_VALUE,
3060 "%s(overlapping src/dst)", func);
3061 return;
3062 }
3063 }
3064
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05003065 dst->MinMaxCacheDirty = true;
3066
Laura Ekstrand4adaad52015-01-13 13:28:08 -08003067 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
3068}
3069
Brian Pauldc0b71f2009-06-02 20:29:57 -06003070void GLAPIENTRY
Timothy Arcerif9e68202017-05-04 14:48:02 +10003071_mesa_CopyBufferSubData_no_error(GLenum readTarget, GLenum writeTarget,
3072 GLintptr readOffset, GLintptr writeOffset,
3073 GLsizeiptr size)
3074{
3075 GET_CURRENT_CONTEXT(ctx);
3076
3077 struct gl_buffer_object **src_ptr = get_buffer_target(ctx, readTarget);
3078 struct gl_buffer_object *src = *src_ptr;
3079
3080 struct gl_buffer_object **dst_ptr = get_buffer_target(ctx, writeTarget);
3081 struct gl_buffer_object *dst = *dst_ptr;
3082
3083 dst->MinMaxCacheDirty = true;
3084 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
3085 size);
3086}
3087
3088void GLAPIENTRY
Brian Pauldc0b71f2009-06-02 20:29:57 -06003089_mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
3090 GLintptr readOffset, GLintptr writeOffset,
3091 GLsizeiptr size)
3092{
3093 GET_CURRENT_CONTEXT(ctx);
3094 struct gl_buffer_object *src, *dst;
Brian Pauldc0b71f2009-06-02 20:29:57 -06003095
Pi Tabred7d946532013-12-14 10:32:00 -07003096 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
3097 GL_INVALID_OPERATION);
Eric Anholtf20fb802012-01-25 16:30:26 -08003098 if (!src)
Brian Pauldc0b71f2009-06-02 20:29:57 -06003099 return;
Brian Pauldc0b71f2009-06-02 20:29:57 -06003100
Pi Tabred7d946532013-12-14 10:32:00 -07003101 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
3102 GL_INVALID_OPERATION);
Eric Anholtf20fb802012-01-25 16:30:26 -08003103 if (!dst)
Brian Pauldc0b71f2009-06-02 20:29:57 -06003104 return;
Brian Pauldc0b71f2009-06-02 20:29:57 -06003105
Timothy Arceri681647e2017-05-04 14:02:10 +10003106 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
3107 "glCopyBufferSubData");
Laura Ekstrand4adaad52015-01-13 13:28:08 -08003108}
3109
3110void GLAPIENTRY
Pierre-Eric Pelloux-Prayer50533d42019-09-11 10:26:50 +02003111_mesa_NamedCopyBufferSubDataEXT(GLuint readBuffer, GLuint writeBuffer,
3112 GLintptr readOffset, GLintptr writeOffset,
3113 GLsizeiptr size)
3114{
3115 GET_CURRENT_CONTEXT(ctx);
3116 struct gl_buffer_object *src, *dst;
3117
3118 src = _mesa_lookup_bufferobj(ctx, readBuffer);
3119 if (!_mesa_handle_bind_buffer_gen(ctx, readBuffer,
3120 &src,
3121 "glNamedCopyBufferSubDataEXT"))
3122 return;
3123
3124 dst = _mesa_lookup_bufferobj(ctx, writeBuffer);
3125 if (!_mesa_handle_bind_buffer_gen(ctx, writeBuffer,
3126 &dst,
3127 "glNamedCopyBufferSubDataEXT"))
3128 return;
3129
3130 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
3131 "glNamedCopyBufferSubDataEXT");
3132}
3133
3134void GLAPIENTRY
Timothy Arcerif9e68202017-05-04 14:48:02 +10003135_mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer, GLuint writeBuffer,
3136 GLintptr readOffset,
3137 GLintptr writeOffset, GLsizeiptr size)
3138{
3139 GET_CURRENT_CONTEXT(ctx);
3140
3141 struct gl_buffer_object *src = _mesa_lookup_bufferobj(ctx, readBuffer);
3142 struct gl_buffer_object *dst = _mesa_lookup_bufferobj(ctx, writeBuffer);
3143
3144 dst->MinMaxCacheDirty = true;
3145 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset,
3146 size);
3147}
3148
3149void GLAPIENTRY
Laura Ekstrand4adaad52015-01-13 13:28:08 -08003150_mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
3151 GLintptr readOffset, GLintptr writeOffset,
3152 GLsizeiptr size)
3153{
3154 GET_CURRENT_CONTEXT(ctx);
3155 struct gl_buffer_object *src, *dst;
3156
3157 src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
3158 "glCopyNamedBufferSubData");
3159 if (!src)
Brian Pauldc0b71f2009-06-02 20:29:57 -06003160 return;
Brian Pauldc0b71f2009-06-02 20:29:57 -06003161
Laura Ekstrand4adaad52015-01-13 13:28:08 -08003162 dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
3163 "glCopyNamedBufferSubData");
3164 if (!dst)
Brian Pauldc0b71f2009-06-02 20:29:57 -06003165 return;
Brian Pauldc0b71f2009-06-02 20:29:57 -06003166
Timothy Arceri681647e2017-05-04 14:02:10 +10003167 copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
3168 "glCopyNamedBufferSubData");
Brian Pauldc0b71f2009-06-02 20:29:57 -06003169}
3170
Marek Olšáka82889e2020-03-06 20:19:11 -05003171void GLAPIENTRY
3172_mesa_InternalBufferSubDataCopyMESA(GLintptr srcBuffer, GLuint srcOffset,
3173 GLuint dstTargetOrName, GLintptr dstOffset,
3174 GLsizeiptr size, GLboolean named,
3175 GLboolean ext_dsa)
3176{
3177 GET_CURRENT_CONTEXT(ctx);
3178 struct gl_buffer_object *src = (struct gl_buffer_object *)srcBuffer;
3179 struct gl_buffer_object *dst;
3180 const char *func;
3181
3182 /* Handle behavior for all 3 variants. */
3183 if (named && ext_dsa) {
3184 func = "glNamedBufferSubDataEXT";
3185 dst = _mesa_lookup_bufferobj(ctx, dstTargetOrName);
3186 if (!_mesa_handle_bind_buffer_gen(ctx, dstTargetOrName, &dst, func))
3187 goto done;
3188 } else if (named) {
3189 func = "glNamedBufferSubData";
3190 dst = _mesa_lookup_bufferobj_err(ctx, dstTargetOrName, func);
3191 if (!dst)
3192 goto done;
3193 } else {
3194 assert(!ext_dsa);
3195 func = "glBufferSubData";
3196 dst = get_buffer(ctx, func, dstTargetOrName, GL_INVALID_OPERATION);
3197 if (!dst)
3198 goto done;
3199 }
3200
3201 if (!validate_buffer_sub_data(ctx, dst, dstOffset, size, func))
3202 goto done; /* the error is already set */
3203
3204 dst->MinMaxCacheDirty = true;
3205 ctx->Driver.CopyBufferSubData(ctx, src, dst, srcOffset, dstOffset, size);
3206
3207done:
3208 /* The caller passes the reference to this function, so unreference it. */
3209 _mesa_reference_buffer_object(ctx, &src, NULL);
3210}
3211
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003212static bool
3213validate_map_buffer_range(struct gl_context *ctx,
3214 struct gl_buffer_object *bufObj, GLintptr offset,
3215 GLsizeiptr length, GLbitfield access,
3216 const char *func)
Brian Paule75b2832009-06-08 17:02:36 -06003217{
Marek Olšák4f78e172014-01-27 21:31:58 +01003218 GLbitfield allowed_access;
Brian Paul92033a92009-08-31 08:50:15 -06003219
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003220 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, false);
Brian Paule75b2832009-06-08 17:02:36 -06003221
Brian Paule75b2832009-06-08 17:02:36 -06003222 if (offset < 0) {
3223 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003224 "%s(offset %ld < 0)", func, (long) offset);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003225 return false;
Brian Paule75b2832009-06-08 17:02:36 -06003226 }
3227
3228 if (length < 0) {
3229 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003230 "%s(length %ld < 0)", func, (long) length);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003231 return false;
Brian Paule75b2832009-06-08 17:02:36 -06003232 }
3233
Fredrik Höglund762d9ac2012-10-10 16:27:31 +02003234 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3235 *
3236 * "An INVALID_OPERATION error is generated for any of the following
3237 * conditions:
3238 *
3239 * * <length> is zero."
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003240 *
3241 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3242 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3243 * either.
Fredrik Höglund762d9ac2012-10-10 16:27:31 +02003244 */
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003245 if (length == 0) {
3246 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003247 return false;
Fredrik Höglund762d9ac2012-10-10 16:27:31 +02003248 }
3249
Marek Olšák4f78e172014-01-27 21:31:58 +01003250 allowed_access = GL_MAP_READ_BIT |
3251 GL_MAP_WRITE_BIT |
3252 GL_MAP_INVALIDATE_RANGE_BIT |
3253 GL_MAP_INVALIDATE_BUFFER_BIT |
3254 GL_MAP_FLUSH_EXPLICIT_BIT |
3255 GL_MAP_UNSYNCHRONIZED_BIT;
3256
3257 if (ctx->Extensions.ARB_buffer_storage) {
3258 allowed_access |= GL_MAP_PERSISTENT_BIT |
3259 GL_MAP_COHERENT_BIT;
3260 }
3261
3262 if (access & ~allowed_access) {
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003263 /* generate an error if any bits other than those allowed are set */
3264 _mesa_error(ctx, GL_INVALID_VALUE,
3265 "%s(access has undefined bits set)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003266 return false;
Yuanhan Liu099af9e2011-09-19 18:25:55 +08003267 }
3268
Brian Paule75b2832009-06-08 17:02:36 -06003269 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
3270 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003271 "%s(access indicates neither read or write)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003272 return false;
Brian Paule75b2832009-06-08 17:02:36 -06003273 }
3274
Brian Paul707eadc2011-05-18 08:14:32 -06003275 if ((access & GL_MAP_READ_BIT) &&
3276 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
3277 GL_MAP_INVALIDATE_BUFFER_BIT |
3278 GL_MAP_UNSYNCHRONIZED_BIT))) {
3279 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003280 "%s(read access with disallowed bits)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003281 return false;
Brian Paule75b2832009-06-08 17:02:36 -06003282 }
3283
3284 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
3285 ((access & GL_MAP_WRITE_BIT) == 0)) {
3286 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003287 "%s(access has flush explicit without write)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003288 return false;
Brian Paule75b2832009-06-08 17:02:36 -06003289 }
3290
Marek Olšák4f78e172014-01-27 21:31:58 +01003291 if (access & GL_MAP_READ_BIT &&
3292 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
3293 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003294 "%s(buffer does not allow read access)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003295 return false;
Marek Olšák4f78e172014-01-27 21:31:58 +01003296 }
3297
3298 if (access & GL_MAP_WRITE_BIT &&
3299 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
3300 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003301 "%s(buffer does not allow write access)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003302 return false;
Marek Olšák4f78e172014-01-27 21:31:58 +01003303 }
3304
3305 if (access & GL_MAP_COHERENT_BIT &&
3306 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
3307 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003308 "%s(buffer does not allow coherent access)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003309 return false;
Marek Olšák4f78e172014-01-27 21:31:58 +01003310 }
3311
3312 if (access & GL_MAP_PERSISTENT_BIT &&
3313 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
3314 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003315 "%s(buffer does not allow persistent access)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003316 return false;
Marek Olšák4f78e172014-01-27 21:31:58 +01003317 }
3318
Brian Paule75b2832009-06-08 17:02:36 -06003319 if (offset + length > bufObj->Size) {
3320 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paula62f0312016-05-02 19:10:06 -06003321 "%s(offset %lu + length %lu > buffer_size %lu)", func,
3322 (unsigned long) offset, (unsigned long) length,
3323 (unsigned long) bufObj->Size);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003324 return false;
Brian Paule75b2832009-06-08 17:02:36 -06003325 }
3326
Marek Olšákdca35022014-02-06 19:24:23 +01003327 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
Brian Paule75b2832009-06-08 17:02:36 -06003328 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003329 "%s(buffer already mapped)", func);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003330 return false;
Marek Olšák5572de82011-08-30 19:10:06 +02003331 }
3332
Brian Paulab0651c2015-12-07 18:38:03 -07003333 if (access & GL_MAP_WRITE_BIT) {
3334 bufObj->NumMapBufferWriteCalls++;
3335 if ((bufObj->Usage == GL_STATIC_DRAW ||
3336 bufObj->Usage == GL_STATIC_COPY) &&
3337 bufObj->NumMapBufferWriteCalls >= BUFFER_WARNING_CALL_COUNT) {
Brian Paule1815bc2015-12-09 16:00:55 -07003338 BUFFER_USAGE_WARNING(ctx,
Brian Paulab0651c2015-12-07 18:38:03 -07003339 "using %s(buffer %u, offset %u, length %u) to "
3340 "update a %s buffer",
3341 func, bufObj->Name, offset, length,
3342 _mesa_enum_to_string(bufObj->Usage));
3343 }
3344 }
Marek Olšák5572de82011-08-30 19:10:06 +02003345
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003346 return true;
3347}
3348
3349static void *
3350map_buffer_range(struct gl_context *ctx, struct gl_buffer_object *bufObj,
3351 GLintptr offset, GLsizeiptr length, GLbitfield access,
3352 const char *func)
3353{
3354 if (!bufObj->Size) {
3355 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
3356 return NULL;
3357 }
3358
Matt Turnerbfcdb842015-02-20 20:18:47 -08003359 assert(ctx->Driver.MapBufferRange);
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003360 void *map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
3361 MAP_USER);
Brian Paul2f6d2a92009-09-03 09:41:41 -06003362 if (!map) {
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003363 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
Brian Paul2f6d2a92009-09-03 09:41:41 -06003364 }
3365 else {
Brian Paul92033a92009-08-31 08:50:15 -06003366 /* The driver callback should have set all these fields.
3367 * This is important because other modules (like VBO) might call
3368 * the driver function directly.
3369 */
Matt Turnerbfcdb842015-02-20 20:18:47 -08003370 assert(bufObj->Mappings[MAP_USER].Pointer == map);
3371 assert(bufObj->Mappings[MAP_USER].Length == length);
3372 assert(bufObj->Mappings[MAP_USER].Offset == offset);
3373 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
Brian Paul92033a92009-08-31 08:50:15 -06003374 }
Brian Paule75b2832009-06-08 17:02:36 -06003375
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05003376 if (access & GL_MAP_WRITE_BIT) {
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003377 bufObj->Written = GL_TRUE;
Nicolai Hähnle6b057f82016-01-11 14:54:53 -05003378 bufObj->MinMaxCacheDirty = true;
3379 }
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003380
3381#ifdef VBO_DEBUG
3382 if (strstr(func, "Range") == NULL) { /* If not MapRange */
3383 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3384 bufObj->Name, bufObj->Size, access);
3385 /* Access must be write only */
3386 if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
3387 GLuint i;
3388 GLubyte *b = (GLubyte *) bufObj->Pointer;
3389 for (i = 0; i < bufObj->Size; i++)
3390 b[i] = i & 0xff;
3391 }
3392 }
3393#endif
3394
3395#ifdef BOUNDS_CHECK
3396 if (strstr(func, "Range") == NULL) { /* If not MapRange */
3397 GLubyte *buf = (GLubyte *) bufObj->Pointer;
3398 GLuint i;
3399 /* buffer is 100 bytes larger than requested, fill with magic value */
3400 for (i = 0; i < 100; i++) {
3401 buf[bufObj->Size - i - 1] = 123;
3402 }
3403 }
3404#endif
3405
Brian Paul92033a92009-08-31 08:50:15 -06003406 return map;
Brian Paule75b2832009-06-08 17:02:36 -06003407}
3408
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003409void * GLAPIENTRY
Timothy Arceri9d010f52017-05-04 13:08:57 +10003410_mesa_MapBufferRange_no_error(GLenum target, GLintptr offset,
3411 GLsizeiptr length, GLbitfield access)
3412{
3413 GET_CURRENT_CONTEXT(ctx);
3414
3415 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
3416 struct gl_buffer_object *bufObj = *bufObjPtr;
3417
3418 return map_buffer_range(ctx, bufObj, offset, length, access,
3419 "glMapBufferRange");
3420}
3421
3422void * GLAPIENTRY
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003423_mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
3424 GLbitfield access)
3425{
3426 GET_CURRENT_CONTEXT(ctx);
3427 struct gl_buffer_object *bufObj;
3428
3429 if (!ctx->Extensions.ARB_map_buffer_range) {
3430 _mesa_error(ctx, GL_INVALID_OPERATION,
3431 "glMapBufferRange(ARB_map_buffer_range not supported)");
3432 return NULL;
3433 }
3434
3435 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
3436 if (!bufObj)
3437 return NULL;
3438
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003439 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access,
3440 "glMapBufferRange"))
3441 return NULL;
3442
Timothy Arceri8a1c3602017-05-04 12:16:48 +10003443 return map_buffer_range(ctx, bufObj, offset, length, access,
3444 "glMapBufferRange");
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003445}
3446
Laura Ekstranda0cc0392015-02-11 14:09:52 -08003447void * GLAPIENTRY
Timothy Arceri9d010f52017-05-04 13:08:57 +10003448_mesa_MapNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
3449 GLsizeiptr length, GLbitfield access)
3450{
3451 GET_CURRENT_CONTEXT(ctx);
3452 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3453
3454 return map_buffer_range(ctx, bufObj, offset, length, access,
3455 "glMapNamedBufferRange");
3456}
3457
Timothy Arcericb0f25a2018-05-25 13:24:47 +10003458static void *
3459map_named_buffer_range(GLuint buffer, GLintptr offset, GLsizeiptr length,
Marek Olšák0de27542019-04-24 13:44:12 -04003460 GLbitfield access, bool dsa_ext, const char *func)
Timothy Arcericb0f25a2018-05-25 13:24:47 +10003461{
3462 GET_CURRENT_CONTEXT(ctx);
3463 struct gl_buffer_object *bufObj = NULL;
3464
3465 if (!ctx->Extensions.ARB_map_buffer_range) {
3466 _mesa_error(ctx, GL_INVALID_OPERATION,
3467 "%s(ARB_map_buffer_range not supported)", func);
3468 return NULL;
3469 }
3470
Marek Olšák0de27542019-04-24 13:44:12 -04003471 if (dsa_ext) {
Timothy Arcericb0f25a2018-05-25 13:24:47 +10003472 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3473 if (!_mesa_handle_bind_buffer_gen(ctx, buffer, &bufObj, func))
3474 return NULL;
3475 } else {
3476 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func);
3477 if (!bufObj)
3478 return NULL;
3479 }
3480
3481 if (!validate_map_buffer_range(ctx, bufObj, offset, length, access, func))
3482 return NULL;
3483
3484 return map_buffer_range(ctx, bufObj, offset, length, access, func);
3485}
3486
3487void * GLAPIENTRY
3488_mesa_MapNamedBufferRangeEXT(GLuint buffer, GLintptr offset, GLsizeiptr length,
3489 GLbitfield access)
3490{
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02003491 GET_CURRENT_CONTEXT(ctx);
3492 if (!buffer) {
3493 _mesa_error(ctx, GL_INVALID_OPERATION,
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02003494 "glMapNamedBufferRangeEXT(buffer=0)");
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02003495 return NULL;
3496 }
Timothy Arcericb0f25a2018-05-25 13:24:47 +10003497 return map_named_buffer_range(buffer, offset, length, access, true,
3498 "glMapNamedBufferRangeEXT");
3499}
3500
Timothy Arceri9d010f52017-05-04 13:08:57 +10003501void * GLAPIENTRY
Laura Ekstranda0cc0392015-02-11 14:09:52 -08003502_mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
3503 GLbitfield access)
3504{
Timothy Arcericb0f25a2018-05-25 13:24:47 +10003505 return map_named_buffer_range(buffer, offset, length, access, false,
3506 "glMapNamedBufferRange");
Laura Ekstranda0cc0392015-02-11 14:09:52 -08003507}
3508
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003509/**
3510 * Converts GLenum access from MapBuffer and MapNamedBuffer into
Timothy Arceri8a1c3602017-05-04 12:16:48 +10003511 * flags for input to map_buffer_range.
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003512 *
3513 * \return true if the type of requested access is permissible.
3514 */
3515static bool
3516get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
3517 GLbitfield *flags)
3518{
3519 switch (access) {
3520 case GL_READ_ONLY_ARB:
3521 *flags = GL_MAP_READ_BIT;
3522 return _mesa_is_desktop_gl(ctx);
3523 case GL_WRITE_ONLY_ARB:
3524 *flags = GL_MAP_WRITE_BIT;
3525 return true;
3526 case GL_READ_WRITE_ARB:
3527 *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
3528 return _mesa_is_desktop_gl(ctx);
3529 default:
Brian Paulfed85642017-10-03 13:54:31 -06003530 *flags = 0;
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003531 return false;
3532 }
3533}
3534
3535void * GLAPIENTRY
Timothy Arceri9d010f52017-05-04 13:08:57 +10003536_mesa_MapBuffer_no_error(GLenum target, GLenum access)
3537{
3538 GET_CURRENT_CONTEXT(ctx);
3539
3540 GLbitfield accessFlags;
3541 get_map_buffer_access_flags(ctx, access, &accessFlags);
3542
3543 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
3544 struct gl_buffer_object *bufObj = *bufObjPtr;
3545
3546 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3547 "glMapBuffer");
3548}
3549
3550void * GLAPIENTRY
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003551_mesa_MapBuffer(GLenum target, GLenum access)
3552{
3553 GET_CURRENT_CONTEXT(ctx);
3554 struct gl_buffer_object *bufObj;
3555 GLbitfield accessFlags;
3556
3557 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
3558 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
3559 return NULL;
3560 }
3561
3562 bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
3563 if (!bufObj)
3564 return NULL;
3565
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003566 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3567 "glMapBuffer"))
3568 return NULL;
3569
Timothy Arceri8a1c3602017-05-04 12:16:48 +10003570 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3571 "glMapBuffer");
Laura Ekstrand4f513bc2015-01-14 12:44:39 -08003572}
3573
Laura Ekstranda0cc0392015-02-11 14:09:52 -08003574void * GLAPIENTRY
Timothy Arceri9d010f52017-05-04 13:08:57 +10003575_mesa_MapNamedBuffer_no_error(GLuint buffer, GLenum access)
3576{
3577 GET_CURRENT_CONTEXT(ctx);
3578
3579 GLbitfield accessFlags;
3580 get_map_buffer_access_flags(ctx, access, &accessFlags);
3581
3582 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3583
3584 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3585 "glMapNamedBuffer");
3586}
3587
3588void * GLAPIENTRY
Laura Ekstranda0cc0392015-02-11 14:09:52 -08003589_mesa_MapNamedBuffer(GLuint buffer, GLenum access)
3590{
3591 GET_CURRENT_CONTEXT(ctx);
3592 struct gl_buffer_object *bufObj;
3593 GLbitfield accessFlags;
3594
3595 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
3596 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
3597 return NULL;
3598 }
3599
3600 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
3601 if (!bufObj)
3602 return NULL;
3603
Timothy Arcerie83b0a42017-05-04 12:28:09 +10003604 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3605 "glMapNamedBuffer"))
3606 return NULL;
3607
Timothy Arceri8a1c3602017-05-04 12:16:48 +10003608 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3609 "glMapNamedBuffer");
Laura Ekstranda0cc0392015-02-11 14:09:52 -08003610}
3611
Timothy Arceri9c53a2e2018-09-05 15:48:07 +10003612void * GLAPIENTRY
3613_mesa_MapNamedBufferEXT(GLuint buffer, GLenum access)
3614{
3615 GET_CURRENT_CONTEXT(ctx);
3616
3617 GLbitfield accessFlags;
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02003618 if (!buffer) {
3619 _mesa_error(ctx, GL_INVALID_OPERATION,
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02003620 "glMapNamedBufferEXT(buffer=0)");
Pierre-Eric Pelloux-Prayer32eefb72019-04-26 17:47:05 +02003621 return NULL;
3622 }
Timothy Arceri9c53a2e2018-09-05 15:48:07 +10003623 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
3624 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBufferEXT(invalid access)");
3625 return NULL;
3626 }
3627
3628 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3629 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
3630 &bufObj, "glMapNamedBufferEXT"))
3631 return NULL;
3632
3633 if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3634 "glMapNamedBufferEXT"))
3635 return NULL;
3636
3637 return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
3638 "glMapNamedBufferEXT");
3639}
Brian Paule75b2832009-06-08 17:02:36 -06003640
Timothy Arceri30d8dea2017-05-04 13:39:59 +10003641static void
3642flush_mapped_buffer_range(struct gl_context *ctx,
3643 struct gl_buffer_object *bufObj,
3644 GLintptr offset, GLsizeiptr length,
3645 const char *func)
Brian Paule75b2832009-06-08 17:02:36 -06003646{
Brian Paule75b2832009-06-08 17:02:36 -06003647 if (!ctx->Extensions.ARB_map_buffer_range) {
3648 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003649 "%s(ARB_map_buffer_range not supported)", func);
Brian Paule75b2832009-06-08 17:02:36 -06003650 return;
3651 }
3652
3653 if (offset < 0) {
3654 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003655 "%s(offset %ld < 0)", func, (long) offset);
Brian Paule75b2832009-06-08 17:02:36 -06003656 return;
3657 }
3658
3659 if (length < 0) {
3660 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003661 "%s(length %ld < 0)", func, (long) length);
Brian Paule75b2832009-06-08 17:02:36 -06003662 return;
3663 }
3664
Marek Olšákdca35022014-02-06 19:24:23 +01003665 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
Brian Paule75b2832009-06-08 17:02:36 -06003666 /* buffer is not mapped */
3667 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003668 "%s(buffer is not mapped)", func);
Brian Paule75b2832009-06-08 17:02:36 -06003669 return;
3670 }
3671
Marek Olšákdca35022014-02-06 19:24:23 +01003672 if ((bufObj->Mappings[MAP_USER].AccessFlags &
3673 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
Brian Paule75b2832009-06-08 17:02:36 -06003674 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003675 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
Brian Paule75b2832009-06-08 17:02:36 -06003676 return;
3677 }
3678
Marek Olšákdca35022014-02-06 19:24:23 +01003679 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
Brian Paule75b2832009-06-08 17:02:36 -06003680 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003681 "%s(offset %ld + length %ld > mapped length %ld)", func,
3682 (long) offset, (long) length,
3683 (long) bufObj->Mappings[MAP_USER].Length);
Brian Paule75b2832009-06-08 17:02:36 -06003684 return;
3685 }
3686
Matt Turnerbfcdb842015-02-20 20:18:47 -08003687 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
Brian Paule75b2832009-06-08 17:02:36 -06003688
3689 if (ctx->Driver.FlushMappedBufferRange)
Marek Olšákdca35022014-02-06 19:24:23 +01003690 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
3691 MAP_USER);
Brian Paule75b2832009-06-08 17:02:36 -06003692}
Chris Wilson99864d52009-11-13 12:19:35 +00003693
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003694void GLAPIENTRY
Timothy Arceri426e4762017-05-04 13:49:02 +10003695_mesa_FlushMappedBufferRange_no_error(GLenum target, GLintptr offset,
3696 GLsizeiptr length)
3697{
3698 GET_CURRENT_CONTEXT(ctx);
3699 struct gl_buffer_object **bufObjPtr = get_buffer_target(ctx, target);
3700 struct gl_buffer_object *bufObj = *bufObjPtr;
3701
3702 if (ctx->Driver.FlushMappedBufferRange)
3703 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
3704 MAP_USER);
3705}
3706
3707void GLAPIENTRY
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003708_mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
3709 GLsizeiptr length)
3710{
3711 GET_CURRENT_CONTEXT(ctx);
3712 struct gl_buffer_object *bufObj;
3713
3714 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
3715 GL_INVALID_OPERATION);
3716 if (!bufObj)
3717 return;
3718
Timothy Arceri30d8dea2017-05-04 13:39:59 +10003719 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3720 "glFlushMappedBufferRange");
Laura Ekstrandee5fae62015-01-14 17:01:20 -08003721}
3722
Laura Ekstrand1cfc18d2015-02-11 16:06:52 -08003723void GLAPIENTRY
Timothy Arceri426e4762017-05-04 13:49:02 +10003724_mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer, GLintptr offset,
3725 GLsizeiptr length)
3726{
3727 GET_CURRENT_CONTEXT(ctx);
3728 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3729
3730 if (ctx->Driver.FlushMappedBufferRange)
3731 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
3732 MAP_USER);
3733}
3734
3735void GLAPIENTRY
Laura Ekstrand1cfc18d2015-02-11 16:06:52 -08003736_mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
3737 GLsizeiptr length)
3738{
3739 GET_CURRENT_CONTEXT(ctx);
3740 struct gl_buffer_object *bufObj;
3741
3742 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
3743 "glFlushMappedNamedBufferRange");
3744 if (!bufObj)
3745 return;
3746
Timothy Arceri30d8dea2017-05-04 13:39:59 +10003747 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3748 "glFlushMappedNamedBufferRange");
Laura Ekstrand1cfc18d2015-02-11 16:06:52 -08003749}
3750
Pierre-Eric Pelloux-Prayere26c6762019-04-26 18:10:44 +02003751void GLAPIENTRY
3752_mesa_FlushMappedNamedBufferRangeEXT(GLuint buffer, GLintptr offset,
3753 GLsizeiptr length)
3754{
3755 GET_CURRENT_CONTEXT(ctx);
3756 struct gl_buffer_object *bufObj;
3757
3758 if (!buffer) {
3759 _mesa_error(ctx, GL_INVALID_OPERATION,
3760 "glFlushMappedNamedBufferRangeEXT(buffer=0)");
3761 return;
3762 }
3763
3764 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3765 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
3766 &bufObj, "glFlushMappedNamedBufferRangeEXT"))
3767 return;
3768
3769 flush_mapped_buffer_range(ctx, bufObj, offset, length,
3770 "glFlushMappedNamedBufferRangeEXT");
3771}
3772
Timothy Arcericea384f2017-05-22 15:46:56 +10003773static void
3774bind_buffer_range_uniform_buffer(struct gl_context *ctx, GLuint index,
3775 struct gl_buffer_object *bufObj,
3776 GLintptr offset, GLsizeiptr size)
3777{
Marek Olšák54525802020-03-21 23:47:08 -04003778 if (!bufObj) {
Timothy Arcericea384f2017-05-22 15:46:56 +10003779 offset = -1;
3780 size = -1;
3781 }
3782
3783 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
3784 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3785}
3786
Iago Toral Quiroga8a1d58b2015-03-19 11:37:43 +01003787/**
Brian Paul09af5782012-06-22 08:43:58 -06003788 * Bind a region of a buffer object to a uniform block binding point.
3789 * \param index the uniform buffer binding point index
3790 * \param bufObj the buffer object
3791 * \param offset offset to the start of buffer object region
3792 * \param size size of the buffer object region
Eric Anholtfb76ddc2012-06-14 19:48:02 -07003793 */
3794static void
Timothy Arcericea384f2017-05-22 15:46:56 +10003795bind_buffer_range_uniform_buffer_err(struct gl_context *ctx, GLuint index,
3796 struct gl_buffer_object *bufObj,
3797 GLintptr offset, GLsizeiptr size)
Eric Anholtfb76ddc2012-06-14 19:48:02 -07003798{
3799 if (index >= ctx->Const.MaxUniformBufferBindings) {
3800 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3801 return;
3802 }
3803
3804 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3805 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul4676c6c2014-10-20 11:49:17 -06003806 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
Eric Anholtfb76ddc2012-06-14 19:48:02 -07003807 ctx->Const.UniformBufferOffsetAlignment);
3808 return;
3809 }
3810
Timothy Arcericea384f2017-05-22 15:46:56 +10003811 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
Eric Anholtfb76ddc2012-06-14 19:48:02 -07003812}
3813
Timothy Arceri135e5652017-05-22 15:46:57 +10003814static void
3815bind_buffer_range_shader_storage_buffer(struct gl_context *ctx,
3816 GLuint index,
3817 struct gl_buffer_object *bufObj,
3818 GLintptr offset,
3819 GLsizeiptr size)
3820{
Marek Olšák54525802020-03-21 23:47:08 -04003821 if (!bufObj) {
Timothy Arceri135e5652017-05-22 15:46:57 +10003822 offset = -1;
3823 size = -1;
3824 }
3825
3826 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
3827 bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3828}
3829
Iago Toral Quiroga173ed052015-03-19 11:42:33 +01003830/**
3831 * Bind a region of a buffer object to a shader storage block binding point.
3832 * \param index the shader storage buffer binding point index
3833 * \param bufObj the buffer object
3834 * \param offset offset to the start of buffer object region
3835 * \param size size of the buffer object region
3836 */
3837static void
Timothy Arceri135e5652017-05-22 15:46:57 +10003838bind_buffer_range_shader_storage_buffer_err(struct gl_context *ctx,
3839 GLuint index,
3840 struct gl_buffer_object *bufObj,
3841 GLintptr offset, GLsizeiptr size)
Iago Toral Quiroga173ed052015-03-19 11:42:33 +01003842{
3843 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
3844 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
3845 return;
3846 }
3847
3848 if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3849 _mesa_error(ctx, GL_INVALID_VALUE,
3850 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3851 ctx->Const.ShaderStorageBufferOffsetAlignment);
3852 return;
3853 }
3854
Timothy Arceri135e5652017-05-22 15:46:57 +10003855 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size);
Iago Toral Quiroga173ed052015-03-19 11:42:33 +01003856}
Eric Anholtfb76ddc2012-06-14 19:48:02 -07003857
Dave Airlie65d3ef72017-09-15 12:38:18 +10003858static void
3859bind_buffer_range_atomic_buffer(struct gl_context *ctx, GLuint index,
3860 struct gl_buffer_object *bufObj,
3861 GLintptr offset, GLsizeiptr size)
3862{
Marek Olšák54525802020-03-21 23:47:08 -04003863 if (!bufObj) {
Dave Airlie65d3ef72017-09-15 12:38:18 +10003864 offset = -1;
3865 size = -1;
3866 }
3867
3868 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
3869 bind_atomic_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
3870}
3871
Eric Anholtfb76ddc2012-06-14 19:48:02 -07003872/**
Dave Airlie65d3ef72017-09-15 12:38:18 +10003873 * Bind a region of a buffer object to an atomic storage block binding point.
3874 * \param index the shader storage buffer binding point index
3875 * \param bufObj the buffer object
3876 * \param offset offset to the start of buffer object region
3877 * \param size size of the buffer object region
Fredrik Höglund19f7eeb2013-11-15 19:45:43 +01003878 */
3879static void
Dave Airlie65d3ef72017-09-15 12:38:18 +10003880bind_buffer_range_atomic_buffer_err(struct gl_context *ctx,
3881 GLuint index,
3882 struct gl_buffer_object *bufObj,
3883 GLintptr offset, GLsizeiptr size)
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07003884{
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07003885 if (index >= ctx->Const.MaxAtomicBufferBindings) {
Dave Airlie65d3ef72017-09-15 12:38:18 +10003886 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07003887 return;
3888 }
3889
3890 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
3891 _mesa_error(ctx, GL_INVALID_VALUE,
Dave Airlie65d3ef72017-09-15 12:38:18 +10003892 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
3893 ATOMIC_COUNTER_SIZE);
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07003894 return;
3895 }
3896
Dave Airlie65d3ef72017-09-15 12:38:18 +10003897 bind_buffer_range_atomic_buffer(ctx, index, bufObj, offset, size);
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07003898}
3899
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01003900static inline bool
3901bind_buffers_check_offset_and_size(struct gl_context *ctx,
3902 GLuint index,
3903 const GLintptr *offsets,
3904 const GLsizeiptr *sizes)
3905{
3906 if (offsets[index] < 0) {
Brian Paulf9052532016-11-11 08:11:40 -07003907 /* The ARB_multi_bind spec says:
3908 *
3909 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3910 * value in <offsets> is less than zero (per binding)."
3911 */
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01003912 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul80fa7fd2014-08-08 07:49:33 -06003913 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3914 index, (int64_t) offsets[index]);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01003915 return false;
3916 }
3917
3918 if (sizes[index] <= 0) {
Brian Paulf9052532016-11-11 08:11:40 -07003919 /* The ARB_multi_bind spec says:
3920 *
3921 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3922 * value in <sizes> is less than or equal to zero (per binding)."
3923 */
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01003924 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul80fa7fd2014-08-08 07:49:33 -06003925 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3926 index, (int64_t) sizes[index]);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01003927 return false;
3928 }
3929
3930 return true;
3931}
3932
Fredrik Höglund533cfa02013-11-15 19:56:07 +01003933static bool
3934error_check_bind_uniform_buffers(struct gl_context *ctx,
3935 GLuint first, GLsizei count,
3936 const char *caller)
3937{
3938 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3939 _mesa_error(ctx, GL_INVALID_ENUM,
3940 "%s(target=GL_UNIFORM_BUFFER)", caller);
3941 return false;
3942 }
3943
3944 /* The ARB_multi_bind_spec says:
3945 *
3946 * "An INVALID_OPERATION error is generated if <first> + <count> is
3947 * greater than the number of target-specific indexed binding points,
3948 * as described in section 6.7.1."
3949 */
3950 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3951 _mesa_error(ctx, GL_INVALID_OPERATION,
3952 "%s(first=%u + count=%d > the value of "
3953 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3954 caller, first, count,
3955 ctx->Const.MaxUniformBufferBindings);
3956 return false;
3957 }
3958
3959 return true;
3960}
3961
Iago Toral Quiroga0aa83f32015-03-19 10:47:17 +01003962static bool
3963error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3964 GLuint first, GLsizei count,
3965 const char *caller)
3966{
3967 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3968 _mesa_error(ctx, GL_INVALID_ENUM,
3969 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3970 return false;
3971 }
3972
3973 /* The ARB_multi_bind_spec says:
3974 *
3975 * "An INVALID_OPERATION error is generated if <first> + <count> is
3976 * greater than the number of target-specific indexed binding points,
3977 * as described in section 6.7.1."
3978 */
3979 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3980 _mesa_error(ctx, GL_INVALID_OPERATION,
3981 "%s(first=%u + count=%d > the value of "
3982 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3983 caller, first, count,
3984 ctx->Const.MaxShaderStorageBufferBindings);
3985 return false;
3986 }
3987
3988 return true;
3989}
3990
Fredrik Höglund533cfa02013-11-15 19:56:07 +01003991/**
3992 * Unbind all uniform buffers in the range
3993 * <first> through <first>+<count>-1
3994 */
3995static void
3996unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3997{
Timothy Arceri084fec02017-05-04 16:14:33 +10003998 for (int i = 0; i < count; i++)
Dave Airlie35ac13e2017-09-15 12:55:50 +10003999 set_buffer_binding(ctx, &ctx->UniformBufferBindings[first + i],
Marek Olšák54525802020-03-21 23:47:08 -04004000 NULL, -1, -1, GL_TRUE, 0);
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004001}
4002
Iago Toral Quiroga0aa83f32015-03-19 10:47:17 +01004003/**
4004 * Unbind all shader storage buffers in the range
4005 * <first> through <first>+<count>-1
4006 */
4007static void
4008unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
4009 GLsizei count)
4010{
Timothy Arceri084fec02017-05-04 16:14:33 +10004011 for (int i = 0; i < count; i++)
Dave Airlie35ac13e2017-09-15 12:55:50 +10004012 set_buffer_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
Marek Olšák54525802020-03-21 23:47:08 -04004013 NULL, -1, -1, GL_TRUE, 0);
Iago Toral Quiroga0aa83f32015-03-19 10:47:17 +01004014}
4015
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004016static void
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004017bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count,
4018 const GLuint *buffers,
4019 bool range,
4020 const GLintptr *offsets, const GLsizeiptr *sizes,
4021 const char *caller)
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004022{
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004023 if (!error_check_bind_uniform_buffers(ctx, first, count, caller))
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004024 return;
4025
4026 /* Assume that at least one binding will be changed */
4027 FLUSH_VERTICES(ctx, 0);
4028 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
4029
4030 if (!buffers) {
4031 /* The ARB_multi_bind spec says:
4032 *
4033 * "If <buffers> is NULL, all bindings from <first> through
4034 * <first>+<count>-1 are reset to their unbound (zero) state.
4035 * In this case, the offsets and sizes associated with the
4036 * binding points are set to default values, ignoring
4037 * <offsets> and <sizes>."
4038 */
4039 unbind_uniform_buffers(ctx, first, count);
4040 return;
4041 }
4042
4043 /* Note that the error semantics for multi-bind commands differ from
4044 * those of other GL commands.
4045 *
4046 * The Issues section in the ARB_multi_bind spec says:
4047 *
4048 * "(11) Typically, OpenGL specifies that if an error is generated by a
4049 * command, that command has no effect. This is somewhat
4050 * unfortunate for multi-bind commands, because it would require a
4051 * first pass to scan the entire list of bound objects for errors
4052 * and then a second pass to actually perform the bindings.
4053 * Should we have different error semantics?
4054 *
4055 * RESOLVED: Yes. In this specification, when the parameters for
4056 * one of the <count> binding points are invalid, that binding point
4057 * is not updated and an error will be generated. However, other
4058 * binding points in the same command will be updated if their
4059 * parameters are valid and no other error occurs."
4060 */
4061
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004062 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004063
Timothy Arceri084fec02017-05-04 16:14:33 +10004064 for (int i = 0; i < count; i++) {
Dave Airlied2bfa762017-09-15 12:43:55 +10004065 struct gl_buffer_binding *binding =
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004066 &ctx->UniformBufferBindings[first + i];
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004067 GLintptr offset = 0;
4068 GLsizeiptr size = 0;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004069
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004070 if (range) {
4071 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4072 continue;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004073
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004074 /* The ARB_multi_bind spec says:
4075 *
4076 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4077 * pair of values in <offsets> and <sizes> does not respectively
4078 * satisfy the constraints described for those parameters for the
4079 * specified target, as described in section 6.7.1 (per binding)."
4080 *
4081 * Section 6.7.1 refers to table 6.5, which says:
4082 *
4083 * "┌───────────────────────────────────────────────────────────────┐
4084 * │ Uniform buffer array bindings (see sec. 7.6) │
4085 * ├─────────────────────┬─────────────────────────────────────────┤
4086 * │ ... │ ... │
4087 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
4088 * │ │ OFFSET_ALIGNMENT │
4089 * │ ... │ ... │
4090 * │ size restriction │ none │
4091 * └─────────────────────┴─────────────────────────────────────────┘"
4092 */
4093 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
4094 _mesa_error(ctx, GL_INVALID_VALUE,
4095 "glBindBuffersRange(offsets[%u]=%" PRId64
4096 " is misaligned; it must be a multiple of the value of "
4097 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
4098 "target=GL_UNIFORM_BUFFER)",
4099 i, (int64_t) offsets[i],
4100 ctx->Const.UniformBufferOffsetAlignment);
4101 continue;
4102 }
4103
4104 offset = offsets[i];
4105 size = sizes[i];
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004106 }
4107
Dave Airlie35ac13e2017-09-15 12:55:50 +10004108 set_buffer_multi_binding(ctx, buffers, i, caller,
Dave Airlie11d688d2017-10-23 10:34:54 +10004109 binding, offset, size, range,
Dave Airlie35ac13e2017-09-15 12:55:50 +10004110 USAGE_UNIFORM_BUFFER);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004111 }
4112
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004113 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004114}
4115
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004116static void
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004117bind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
4118 GLsizei count, const GLuint *buffers,
4119 bool range,
4120 const GLintptr *offsets,
4121 const GLsizeiptr *sizes,
4122 const char *caller)
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004123{
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004124 if (!error_check_bind_shader_storage_buffers(ctx, first, count, caller))
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004125 return;
4126
4127 /* Assume that at least one binding will be changed */
4128 FLUSH_VERTICES(ctx, 0);
4129 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
4130
4131 if (!buffers) {
4132 /* The ARB_multi_bind spec says:
4133 *
4134 * "If <buffers> is NULL, all bindings from <first> through
4135 * <first>+<count>-1 are reset to their unbound (zero) state.
4136 * In this case, the offsets and sizes associated with the
4137 * binding points are set to default values, ignoring
4138 * <offsets> and <sizes>."
4139 */
4140 unbind_shader_storage_buffers(ctx, first, count);
4141 return;
4142 }
4143
4144 /* Note that the error semantics for multi-bind commands differ from
4145 * those of other GL commands.
4146 *
4147 * The Issues section in the ARB_multi_bind spec says:
4148 *
4149 * "(11) Typically, OpenGL specifies that if an error is generated by a
4150 * command, that command has no effect. This is somewhat
4151 * unfortunate for multi-bind commands, because it would require a
4152 * first pass to scan the entire list of bound objects for errors
4153 * and then a second pass to actually perform the bindings.
4154 * Should we have different error semantics?
4155 *
4156 * RESOLVED: Yes. In this specification, when the parameters for
4157 * one of the <count> binding points are invalid, that binding point
4158 * is not updated and an error will be generated. However, other
4159 * binding points in the same command will be updated if their
4160 * parameters are valid and no other error occurs."
4161 */
4162
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004163 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004164
Timothy Arceri084fec02017-05-04 16:14:33 +10004165 for (int i = 0; i < count; i++) {
Dave Airlied2bfa762017-09-15 12:43:55 +10004166 struct gl_buffer_binding *binding =
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004167 &ctx->ShaderStorageBufferBindings[first + i];
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004168 GLintptr offset = 0;
4169 GLsizeiptr size = 0;
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004170
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004171 if (range) {
4172 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4173 continue;
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004174
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004175 /* The ARB_multi_bind spec says:
4176 *
4177 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4178 * pair of values in <offsets> and <sizes> does not respectively
4179 * satisfy the constraints described for those parameters for the
4180 * specified target, as described in section 6.7.1 (per binding)."
4181 *
4182 * Section 6.7.1 refers to table 6.5, which says:
4183 *
4184 * "┌───────────────────────────────────────────────────────────────┐
4185 * │ Shader storage buffer array bindings (see sec. 7.8) │
4186 * ├─────────────────────┬─────────────────────────────────────────┤
4187 * │ ... │ ... │
4188 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
4189 * │ │ BUFFER_OFFSET_ALIGNMENT │
4190 * │ ... │ ... │
4191 * │ size restriction │ none │
4192 * └─────────────────────┴─────────────────────────────────────────┘"
4193 */
4194 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
4195 _mesa_error(ctx, GL_INVALID_VALUE,
4196 "glBindBuffersRange(offsets[%u]=%" PRId64
4197 " is misaligned; it must be a multiple of the value of "
4198 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
4199 "target=GL_SHADER_STORAGE_BUFFER)",
4200 i, (int64_t) offsets[i],
4201 ctx->Const.ShaderStorageBufferOffsetAlignment);
4202 continue;
4203 }
4204
4205 offset = offsets[i];
4206 size = sizes[i];
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004207 }
4208
Dave Airlie35ac13e2017-09-15 12:55:50 +10004209 set_buffer_multi_binding(ctx, buffers, i, caller,
Dave Airlie11d688d2017-10-23 10:34:54 +10004210 binding, offset, size, range,
Dave Airlie35ac13e2017-09-15 12:55:50 +10004211 USAGE_SHADER_STORAGE_BUFFER);
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004212 }
4213
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004214 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004215}
4216
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004217static bool
4218error_check_bind_xfb_buffers(struct gl_context *ctx,
4219 struct gl_transform_feedback_object *tfObj,
4220 GLuint first, GLsizei count, const char *caller)
4221{
4222 if (!ctx->Extensions.EXT_transform_feedback) {
4223 _mesa_error(ctx, GL_INVALID_ENUM,
4224 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
4225 return false;
4226 }
4227
4228 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
4229 *
4230 * "An INVALID_OPERATION error is generated :
4231 *
4232 * ...
4233 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
4234 * FEEDBACK_BUFFER and transform feedback is currently active."
4235 *
4236 * We assume that this is also meant to apply to BindBuffersRange
4237 * and BindBuffersBase.
4238 */
4239 if (tfObj->Active) {
4240 _mesa_error(ctx, GL_INVALID_OPERATION,
4241 "%s(Changing transform feedback buffers while "
4242 "transform feedback is active)", caller);
4243 return false;
4244 }
4245
4246 /* The ARB_multi_bind_spec says:
4247 *
4248 * "An INVALID_OPERATION error is generated if <first> + <count> is
4249 * greater than the number of target-specific indexed binding points,
4250 * as described in section 6.7.1."
4251 */
4252 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
4253 _mesa_error(ctx, GL_INVALID_OPERATION,
4254 "%s(first=%u + count=%d > the value of "
4255 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4256 caller, first, count,
4257 ctx->Const.MaxTransformFeedbackBuffers);
4258 return false;
4259 }
4260
4261 return true;
4262}
4263
4264/**
4265 * Unbind all transform feedback buffers in the range
4266 * <first> through <first>+<count>-1
4267 */
4268static void
4269unbind_xfb_buffers(struct gl_context *ctx,
4270 struct gl_transform_feedback_object *tfObj,
4271 GLuint first, GLsizei count)
4272{
Timothy Arceri084fec02017-05-04 16:14:33 +10004273 for (int i = 0; i < count; i++)
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004274 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
Marek Olšák54525802020-03-21 23:47:08 -04004275 NULL, 0, 0);
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004276}
4277
4278static void
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004279bind_xfb_buffers(struct gl_context *ctx,
4280 GLuint first, GLsizei count,
4281 const GLuint *buffers,
4282 bool range,
4283 const GLintptr *offsets,
4284 const GLsizeiptr *sizes,
4285 const char *caller)
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004286{
4287 struct gl_transform_feedback_object *tfObj =
4288 ctx->TransformFeedback.CurrentObject;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004289
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004290 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count, caller))
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004291 return;
4292
4293 /* Assume that at least one binding will be changed */
4294 FLUSH_VERTICES(ctx, 0);
4295 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
4296
4297 if (!buffers) {
4298 /* The ARB_multi_bind spec says:
4299 *
4300 * "If <buffers> is NULL, all bindings from <first> through
4301 * <first>+<count>-1 are reset to their unbound (zero) state.
4302 * In this case, the offsets and sizes associated with the
4303 * binding points are set to default values, ignoring
4304 * <offsets> and <sizes>."
4305 */
4306 unbind_xfb_buffers(ctx, tfObj, first, count);
4307 return;
4308 }
4309
4310 /* Note that the error semantics for multi-bind commands differ from
4311 * those of other GL commands.
4312 *
4313 * The Issues section in the ARB_multi_bind spec says:
4314 *
4315 * "(11) Typically, OpenGL specifies that if an error is generated by a
4316 * command, that command has no effect. This is somewhat
4317 * unfortunate for multi-bind commands, because it would require a
4318 * first pass to scan the entire list of bound objects for errors
4319 * and then a second pass to actually perform the bindings.
4320 * Should we have different error semantics?
4321 *
4322 * RESOLVED: Yes. In this specification, when the parameters for
4323 * one of the <count> binding points are invalid, that binding point
4324 * is not updated and an error will be generated. However, other
4325 * binding points in the same command will be updated if their
4326 * parameters are valid and no other error occurs."
4327 */
4328
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004329 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004330
Timothy Arceri084fec02017-05-04 16:14:33 +10004331 for (int i = 0; i < count; i++) {
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004332 const GLuint index = first + i;
4333 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
4334 struct gl_buffer_object *bufObj;
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004335 GLintptr offset = 0;
4336 GLsizeiptr size = 0;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004337
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004338 if (range) {
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004339 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4340 continue;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004341
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004342 /* The ARB_multi_bind spec says:
4343 *
4344 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4345 * pair of values in <offsets> and <sizes> does not respectively
4346 * satisfy the constraints described for those parameters for the
4347 * specified target, as described in section 6.7.1 (per binding)."
4348 *
4349 * Section 6.7.1 refers to table 6.5, which says:
4350 *
4351 * "┌───────────────────────────────────────────────────────────────┐
4352 * │ Transform feedback array bindings (see sec. 13.2.2) │
4353 * ├───────────────────────┬───────────────────────────────────────┤
4354 * │ ... │ ... │
4355 * │ offset restriction │ multiple of 4 │
4356 * │ ... │ ... │
4357 * │ size restriction │ multiple of 4 │
4358 * └───────────────────────┴───────────────────────────────────────┘"
4359 */
4360 if (offsets[i] & 0x3) {
4361 _mesa_error(ctx, GL_INVALID_VALUE,
4362 "glBindBuffersRange(offsets[%u]=%" PRId64
4363 " is misaligned; it must be a multiple of 4 when "
4364 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4365 i, (int64_t) offsets[i]);
4366 continue;
4367 }
4368
4369 if (sizes[i] & 0x3) {
4370 _mesa_error(ctx, GL_INVALID_VALUE,
4371 "glBindBuffersRange(sizes[%u]=%" PRId64
4372 " is misaligned; it must be a multiple of 4 when "
4373 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4374 i, (int64_t) sizes[i]);
4375 continue;
4376 }
4377
4378 offset = offsets[i];
4379 size = sizes[i];
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004380 }
4381
4382 if (boundBufObj && boundBufObj->Name == buffers[i])
4383 bufObj = boundBufObj;
Marek Olšák54525802020-03-21 23:47:08 -04004384 else {
4385 bool error;
4386 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller,
4387 &error);
4388 if (error)
4389 continue;
4390 }
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004391
Marek Olšák54525802020-03-21 23:47:08 -04004392 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
4393 offset, size);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004394 }
4395
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004396 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004397}
4398
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004399static bool
4400error_check_bind_atomic_buffers(struct gl_context *ctx,
4401 GLuint first, GLsizei count,
4402 const char *caller)
4403{
4404 if (!ctx->Extensions.ARB_shader_atomic_counters) {
4405 _mesa_error(ctx, GL_INVALID_ENUM,
4406 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
4407 return false;
4408 }
4409
4410 /* The ARB_multi_bind_spec says:
4411 *
4412 * "An INVALID_OPERATION error is generated if <first> + <count> is
4413 * greater than the number of target-specific indexed binding points,
4414 * as described in section 6.7.1."
4415 */
4416 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
4417 _mesa_error(ctx, GL_INVALID_OPERATION,
4418 "%s(first=%u + count=%d > the value of "
4419 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4420 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
4421 return false;
4422 }
4423
4424 return true;
4425}
4426
4427/**
4428 * Unbind all atomic counter buffers in the range
4429 * <first> through <first>+<count>-1
4430 */
4431static void
4432unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
4433{
Timothy Arceri084fec02017-05-04 16:14:33 +10004434 for (int i = 0; i < count; i++)
Dave Airlie35ac13e2017-09-15 12:55:50 +10004435 set_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
Marek Olšák54525802020-03-21 23:47:08 -04004436 NULL, -1, -1, GL_TRUE, 0);
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004437}
4438
4439static void
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004440bind_atomic_buffers(struct gl_context *ctx,
4441 GLuint first,
4442 GLsizei count,
4443 const GLuint *buffers,
4444 bool range,
4445 const GLintptr *offsets,
4446 const GLsizeiptr *sizes,
4447 const char *caller)
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004448{
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004449 if (!error_check_bind_atomic_buffers(ctx, first, count, caller))
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004450 return;
4451
4452 /* Assume that at least one binding will be changed */
4453 FLUSH_VERTICES(ctx, 0);
4454 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
4455
4456 if (!buffers) {
4457 /* The ARB_multi_bind spec says:
4458 *
4459 * "If <buffers> is NULL, all bindings from <first> through
4460 * <first>+<count>-1 are reset to their unbound (zero) state.
4461 * In this case, the offsets and sizes associated with the
4462 * binding points are set to default values, ignoring
4463 * <offsets> and <sizes>."
4464 */
4465 unbind_atomic_buffers(ctx, first, count);
4466 return;
4467 }
4468
4469 /* Note that the error semantics for multi-bind commands differ from
4470 * those of other GL commands.
4471 *
4472 * The Issues section in the ARB_multi_bind spec says:
4473 *
4474 * "(11) Typically, OpenGL specifies that if an error is generated by a
4475 * command, that command has no effect. This is somewhat
4476 * unfortunate for multi-bind commands, because it would require a
4477 * first pass to scan the entire list of bound objects for errors
4478 * and then a second pass to actually perform the bindings.
4479 * Should we have different error semantics?
4480 *
4481 * RESOLVED: Yes. In this specification, when the parameters for
4482 * one of the <count> binding points are invalid, that binding point
4483 * is not updated and an error will be generated. However, other
4484 * binding points in the same command will be updated if their
4485 * parameters are valid and no other error occurs."
4486 */
4487
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004488 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004489
Timothy Arceri084fec02017-05-04 16:14:33 +10004490 for (int i = 0; i < count; i++) {
Dave Airlied2bfa762017-09-15 12:43:55 +10004491 struct gl_buffer_binding *binding =
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004492 &ctx->AtomicBufferBindings[first + i];
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004493 GLintptr offset = 0;
4494 GLsizeiptr size = 0;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004495
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004496 if (range) {
4497 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
4498 continue;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004499
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004500 /* The ARB_multi_bind spec says:
4501 *
4502 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4503 * pair of values in <offsets> and <sizes> does not respectively
4504 * satisfy the constraints described for those parameters for the
4505 * specified target, as described in section 6.7.1 (per binding)."
4506 *
4507 * Section 6.7.1 refers to table 6.5, which says:
4508 *
4509 * "┌───────────────────────────────────────────────────────────────┐
4510 * │ Atomic counter array bindings (see sec. 7.7.2) │
4511 * ├───────────────────────┬───────────────────────────────────────┤
4512 * │ ... │ ... │
4513 * │ offset restriction │ multiple of 4 │
4514 * │ ... │ ... │
4515 * │ size restriction │ none │
4516 * └───────────────────────┴───────────────────────────────────────┘"
4517 */
4518 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
4519 _mesa_error(ctx, GL_INVALID_VALUE,
4520 "glBindBuffersRange(offsets[%u]=%" PRId64
4521 " is misaligned; it must be a multiple of %d when "
4522 "target=GL_ATOMIC_COUNTER_BUFFER)",
4523 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
4524 continue;
4525 }
4526
4527 offset = offsets[i];
4528 size = sizes[i];
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004529 }
4530
Dave Airlie35ac13e2017-09-15 12:55:50 +10004531 set_buffer_multi_binding(ctx, buffers, i, caller,
Dave Airlie11d688d2017-10-23 10:34:54 +10004532 binding, offset, size, range,
Dave Airlie35ac13e2017-09-15 12:55:50 +10004533 USAGE_ATOMIC_COUNTER_BUFFER);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004534 }
4535
Timothy Arceri31cb6fd2017-04-06 11:00:15 +10004536 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004537}
4538
Timothy Arcerib8174a82017-05-22 15:47:01 +10004539static ALWAYS_INLINE void
4540bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004541 GLsizeiptr size, bool no_error)
Eric Anholtb82c4722012-06-14 16:55:56 -07004542{
4543 GET_CURRENT_CONTEXT(ctx);
4544 struct gl_buffer_object *bufObj;
4545
Jordan Justen36db91c2015-12-29 14:24:09 -08004546 if (MESA_VERBOSE & VERBOSE_API) {
Brian Paula62f0312016-05-02 19:10:06 -06004547 _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4548 _mesa_enum_to_string(target), index, buffer,
4549 (unsigned long) offset, (unsigned long) size);
Jordan Justen36db91c2015-12-29 14:24:09 -08004550 }
4551
Eric Anholtb82c4722012-06-14 16:55:56 -07004552 if (buffer == 0) {
Marek Olšák54525802020-03-21 23:47:08 -04004553 bufObj = NULL;
Eric Anholtb82c4722012-06-14 16:55:56 -07004554 } else {
4555 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Timothy Arceri49433532017-07-25 23:06:03 +10004556 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4557 &bufObj, "glBindBufferRange"))
4558 return;
Timothy Arcerida154782017-07-26 11:11:02 +10004559
4560 if (!no_error && !bufObj) {
4561 _mesa_error(ctx, GL_INVALID_OPERATION,
4562 "glBindBufferRange(invalid buffer=%u)", buffer);
4563 return;
4564 }
Eric Anholtb82c4722012-06-14 16:55:56 -07004565 }
4566
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004567 if (no_error) {
4568 switch (target) {
4569 case GL_TRANSFORM_FEEDBACK_BUFFER:
4570 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4571 index, bufObj, offset, size);
4572 return;
4573 case GL_UNIFORM_BUFFER:
4574 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
4575 return;
4576 case GL_SHADER_STORAGE_BUFFER:
4577 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset,
4578 size);
4579 return;
4580 case GL_ATOMIC_COUNTER_BUFFER:
Dave Airlie65d3ef72017-09-15 12:38:18 +10004581 bind_buffer_range_atomic_buffer(ctx, index, bufObj, offset, size);
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004582 return;
4583 default:
4584 unreachable("invalid BindBufferRange target with KHR_no_error");
4585 }
4586 } else {
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004587 if (buffer != 0) {
4588 if (size <= 0) {
4589 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
4590 (int) size);
4591 return;
4592 }
4593 }
4594
4595 switch (target) {
4596 case GL_TRANSFORM_FEEDBACK_BUFFER:
4597 if (!_mesa_validate_buffer_range_xfb(ctx,
4598 ctx->TransformFeedback.CurrentObject,
4599 index, bufObj, offset, size,
4600 false))
4601 return;
4602
4603 _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject,
4604 index, bufObj, offset, size);
4605 return;
4606 case GL_UNIFORM_BUFFER:
4607 bind_buffer_range_uniform_buffer_err(ctx, index, bufObj, offset,
4608 size);
4609 return;
4610 case GL_SHADER_STORAGE_BUFFER:
4611 bind_buffer_range_shader_storage_buffer_err(ctx, index, bufObj,
4612 offset, size);
4613 return;
4614 case GL_ATOMIC_COUNTER_BUFFER:
Dave Airlie65d3ef72017-09-15 12:38:18 +10004615 bind_buffer_range_atomic_buffer_err(ctx, index, bufObj,
4616 offset, size);
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004617 return;
4618 default:
4619 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
Matt Turner970ec8d2012-12-04 17:52:00 -08004620 return;
4621 }
Eric Anholtb82c4722012-06-14 16:55:56 -07004622 }
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004623}
Eric Anholtb82c4722012-06-14 16:55:56 -07004624
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004625void GLAPIENTRY
4626_mesa_BindBufferRange_no_error(GLenum target, GLuint index, GLuint buffer,
4627 GLintptr offset, GLsizeiptr size)
4628{
4629 bind_buffer_range(target, index, buffer, offset, size, true);
Eric Anholtb82c4722012-06-14 16:55:56 -07004630}
4631
4632void GLAPIENTRY
Timothy Arcerib8174a82017-05-22 15:47:01 +10004633_mesa_BindBufferRange(GLenum target, GLuint index,
4634 GLuint buffer, GLintptr offset, GLsizeiptr size)
4635{
Timothy Arceri2c2ea572017-05-22 15:47:02 +10004636 bind_buffer_range(target, index, buffer, offset, size, false);
Timothy Arcerib8174a82017-05-22 15:47:01 +10004637}
4638
4639void GLAPIENTRY
Eric Anholtb82c4722012-06-14 16:55:56 -07004640_mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
4641{
4642 GET_CURRENT_CONTEXT(ctx);
4643 struct gl_buffer_object *bufObj;
4644
Jordan Justen36db91c2015-12-29 14:24:09 -08004645 if (MESA_VERBOSE & VERBOSE_API) {
4646 _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n",
4647 _mesa_enum_to_string(target), index, buffer);
4648 }
4649
Eric Anholtb82c4722012-06-14 16:55:56 -07004650 if (buffer == 0) {
Marek Olšák54525802020-03-21 23:47:08 -04004651 bufObj = NULL;
Eric Anholtb82c4722012-06-14 16:55:56 -07004652 } else {
4653 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Timothy Arceri49433532017-07-25 23:06:03 +10004654 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
4655 &bufObj, "glBindBufferBase"))
4656 return;
Eric Anholtb82c4722012-06-14 16:55:56 -07004657
Timothy Arcerida154782017-07-26 11:11:02 +10004658 if (!bufObj) {
4659 _mesa_error(ctx, GL_INVALID_OPERATION,
4660 "glBindBufferBase(invalid buffer=%u)", buffer);
4661 return;
4662 }
Eric Anholtb82c4722012-06-14 16:55:56 -07004663 }
4664
Eric Anholtcb9f35d2012-06-18 16:20:11 -07004665 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4666 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4667 *
4668 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4669 * zero and size equal to the size of buffer."
4670 *
4671 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4672 *
4673 * "If the parameter (starting offset or size) was not specified when the
4674 * buffer object was bound, zero is returned."
4675 *
4676 * What happens if the size of the buffer changes? Does the size of the
4677 * buffer at the moment glBindBufferBase was called still play a role, like
4678 * the first quote would imply, or is the size meaningless in the
4679 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4680 * core spec page 45 says:
4681 *
4682 * "It is equivalent to calling BindBufferRange with offset zero, while
4683 * size is determined by the size of the bound buffer at the time the
4684 * binding is used."
4685 *
4686 * My interpretation is that the GL 4.1 spec was a clarification of the
4687 * behavior, not a change. In particular, this choice will only make
4688 * rendering work in cases where it would have had undefined results.
4689 */
4690
Eric Anholtb82c4722012-06-14 16:55:56 -07004691 switch (target) {
4692 case GL_TRANSFORM_FEEDBACK_BUFFER:
Martin Peresa5d165a2015-01-20 16:30:32 +02004693 _mesa_bind_buffer_base_transform_feedback(ctx,
4694 ctx->TransformFeedback.CurrentObject,
4695 index, bufObj, false);
Eric Anholtb82c4722012-06-14 16:55:56 -07004696 return;
Eric Anholtfb76ddc2012-06-14 19:48:02 -07004697 case GL_UNIFORM_BUFFER:
4698 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4699 return;
Iago Toral Quiroga8a1d58b2015-03-19 11:37:43 +01004700 case GL_SHADER_STORAGE_BUFFER:
4701 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4702 return;
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07004703 case GL_ATOMIC_COUNTER_BUFFER:
Dave Airlie65d3ef72017-09-15 12:38:18 +10004704 bind_buffer_base_atomic_buffer(ctx, index, bufObj);
Francisco Jerez1c7dcfe2013-10-07 18:53:40 -07004705 return;
Eric Anholtb82c4722012-06-14 16:55:56 -07004706 default:
4707 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4708 return;
4709 }
4710}
4711
Paul Berryf7fa9462012-10-19 09:47:11 -07004712void GLAPIENTRY
Fredrik Höglund6655e702013-11-13 19:02:10 +01004713_mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4714 const GLuint *buffers,
4715 const GLintptr *offsets, const GLsizeiptr *sizes)
4716{
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004717 GET_CURRENT_CONTEXT(ctx);
4718
Jordan Justen36db91c2015-12-29 14:24:09 -08004719 if (MESA_VERBOSE & VERBOSE_API) {
4720 _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4721 _mesa_enum_to_string(target), first, count,
4722 buffers, offsets, sizes);
4723 }
4724
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004725 switch (target) {
4726 case GL_TRANSFORM_FEEDBACK_BUFFER:
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004727 bind_xfb_buffers(ctx, first, count, buffers, true, offsets, sizes,
4728 "glBindBuffersRange");
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004729 return;
4730 case GL_UNIFORM_BUFFER:
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004731 bind_uniform_buffers(ctx, first, count, buffers, true, offsets, sizes,
4732 "glBindBuffersRange");
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004733 return;
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004734 case GL_SHADER_STORAGE_BUFFER:
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004735 bind_shader_storage_buffers(ctx, first, count, buffers, true, offsets, sizes,
4736 "glBindBuffersRange");
Iago Toral Quiroga7b0d0a22015-03-19 11:21:52 +01004737 return;
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004738 case GL_ATOMIC_COUNTER_BUFFER:
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004739 bind_atomic_buffers(ctx, first, count, buffers, true, offsets, sizes,
4740 "glBindBuffersRange");
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004741 return;
4742 default:
4743 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004744 _mesa_enum_to_string(target));
Fredrik Höglundf0c36cf2013-11-15 19:57:41 +01004745 break;
4746 }
Fredrik Höglund6655e702013-11-13 19:02:10 +01004747}
4748
4749void GLAPIENTRY
4750_mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4751 const GLuint *buffers)
4752{
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004753 GET_CURRENT_CONTEXT(ctx);
4754
Jordan Justen36db91c2015-12-29 14:24:09 -08004755 if (MESA_VERBOSE & VERBOSE_API) {
4756 _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n",
4757 _mesa_enum_to_string(target), first, count, buffers);
4758 }
4759
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004760 switch (target) {
4761 case GL_TRANSFORM_FEEDBACK_BUFFER:
Nicolai Hähnleb3ca26c2016-01-06 15:47:01 -05004762 bind_xfb_buffers(ctx, first, count, buffers, false, NULL, NULL,
4763 "glBindBuffersBase");
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004764 return;
4765 case GL_UNIFORM_BUFFER:
Nicolai Hähnlee8dd7cc2016-01-06 17:20:57 -05004766 bind_uniform_buffers(ctx, first, count, buffers, false, NULL, NULL,
4767 "glBindBuffersBase");
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004768 return;
Iago Toral Quiroga0aa83f32015-03-19 10:47:17 +01004769 case GL_SHADER_STORAGE_BUFFER:
Nicolai Hähnle5eb104d2016-01-06 17:26:14 -05004770 bind_shader_storage_buffers(ctx, first, count, buffers, false, NULL, NULL,
4771 "glBindBuffersBase");
Iago Toral Quiroga0aa83f32015-03-19 10:47:17 +01004772 return;
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004773 case GL_ATOMIC_COUNTER_BUFFER:
Nicolai Hähnleda5d4582016-01-06 17:30:18 -05004774 bind_atomic_buffers(ctx, first, count, buffers, false, NULL, NULL,
4775 "glBindBuffersBase");
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004776 return;
4777 default:
4778 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004779 _mesa_enum_to_string(target));
Fredrik Höglund533cfa02013-11-15 19:56:07 +01004780 break;
4781 }
Fredrik Höglund6655e702013-11-13 19:02:10 +01004782}
4783
Samuel Pitoisetec0c2eb2017-05-31 14:14:55 +02004784static ALWAYS_INLINE void
4785invalidate_buffer_subdata(struct gl_context *ctx,
4786 struct gl_buffer_object *bufObj, GLintptr offset,
4787 GLsizeiptr length)
4788{
4789 if (ctx->Driver.InvalidateBufferSubData)
4790 ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length);
4791}
4792
Fredrik Höglund6655e702013-11-13 19:02:10 +01004793void GLAPIENTRY
Samuel Pitoiset9ab285e2017-05-31 14:15:37 +02004794_mesa_InvalidateBufferSubData_no_error(GLuint buffer, GLintptr offset,
4795 GLsizeiptr length)
4796{
4797 GET_CURRENT_CONTEXT(ctx);
4798
4799 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4800 invalidate_buffer_subdata(ctx, bufObj, offset, length);
4801}
4802
4803void GLAPIENTRY
Ian Romanickf241ffd2012-08-13 10:27:33 -07004804_mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4805 GLsizeiptr length)
4806{
4807 GET_CURRENT_CONTEXT(ctx);
4808 struct gl_buffer_object *bufObj;
4809 const GLintptr end = offset + length;
4810
Nicolai Hähnle53c77492016-01-09 17:51:39 -05004811 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4812 * Profile) spec says:
4813 *
4814 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4815 * name of an existing buffer object."
4816 */
Ian Romanickf241ffd2012-08-13 10:27:33 -07004817 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Nicolai Hähnle53c77492016-01-09 17:51:39 -05004818 if (!bufObj || bufObj == &DummyBufferObject) {
Ian Romanickf241ffd2012-08-13 10:27:33 -07004819 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul39b7b8b2016-05-23 14:58:02 -06004820 "glInvalidateBufferSubData(name = %u) invalid object",
Ian Romanickf241ffd2012-08-13 10:27:33 -07004821 buffer);
4822 return;
4823 }
4824
4825 /* The GL_ARB_invalidate_subdata spec says:
4826 *
4827 * "An INVALID_VALUE error is generated if <offset> or <length> is
4828 * negative, or if <offset> + <length> is greater than the value of
4829 * BUFFER_SIZE."
4830 */
Nicolai Hähnle53c77492016-01-09 17:51:39 -05004831 if (offset < 0 || length < 0 || end > bufObj->Size) {
Ian Romanickf241ffd2012-08-13 10:27:33 -07004832 _mesa_error(ctx, GL_INVALID_VALUE,
4833 "glInvalidateBufferSubData(invalid offset or length)");
4834 return;
4835 }
4836
Marek Olšákd26a0652014-01-27 21:36:53 +01004837 /* The OpenGL 4.4 (Core Profile) spec says:
Ian Romanickf241ffd2012-08-13 10:27:33 -07004838 *
Marek Olšákd26a0652014-01-27 21:36:53 +01004839 * "An INVALID_OPERATION error is generated if buffer is currently
4840 * mapped by MapBuffer or if the invalidate range intersects the range
4841 * currently mapped by MapBufferRange, unless it was mapped
4842 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
Ian Romanickf241ffd2012-08-13 10:27:33 -07004843 */
Marek Olšákdca35022014-02-06 19:24:23 +01004844 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
Marek Olšákd26a0652014-01-27 21:36:53 +01004845 bufferobj_range_mapped(bufObj, offset, length)) {
Pi Tabred84c4ea52013-12-14 10:32:00 -07004846 _mesa_error(ctx, GL_INVALID_OPERATION,
4847 "glInvalidateBufferSubData(intersection with mapped "
4848 "range)");
4849 return;
Ian Romanickf241ffd2012-08-13 10:27:33 -07004850 }
4851
Samuel Pitoisetec0c2eb2017-05-31 14:14:55 +02004852 invalidate_buffer_subdata(ctx, bufObj, offset, length);
Ian Romanickf241ffd2012-08-13 10:27:33 -07004853}
4854
Paul Berryf7fa9462012-10-19 09:47:11 -07004855void GLAPIENTRY
Samuel Pitoisetb019c4e2017-05-31 14:18:00 +02004856_mesa_InvalidateBufferData_no_error(GLuint buffer)
4857{
4858 GET_CURRENT_CONTEXT(ctx);
4859
4860 struct gl_buffer_object *bufObj =_mesa_lookup_bufferobj(ctx, buffer);
4861 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
4862}
4863
4864void GLAPIENTRY
Ian Romanickf241ffd2012-08-13 10:27:33 -07004865_mesa_InvalidateBufferData(GLuint buffer)
4866{
4867 GET_CURRENT_CONTEXT(ctx);
4868 struct gl_buffer_object *bufObj;
4869
Nicolai Hähnle53c77492016-01-09 17:51:39 -05004870 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4871 * Profile) spec says:
4872 *
4873 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4874 * name of an existing buffer object."
4875 */
Ian Romanickf241ffd2012-08-13 10:27:33 -07004876 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
Nicolai Hähnle53c77492016-01-09 17:51:39 -05004877 if (!bufObj || bufObj == &DummyBufferObject) {
Ian Romanickf241ffd2012-08-13 10:27:33 -07004878 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul39b7b8b2016-05-23 14:58:02 -06004879 "glInvalidateBufferData(name = %u) invalid object",
Ian Romanickf241ffd2012-08-13 10:27:33 -07004880 buffer);
4881 return;
4882 }
4883
Marek Olšákd26a0652014-01-27 21:36:53 +01004884 /* The OpenGL 4.4 (Core Profile) spec says:
Ian Romanickf241ffd2012-08-13 10:27:33 -07004885 *
Marek Olšákd26a0652014-01-27 21:36:53 +01004886 * "An INVALID_OPERATION error is generated if buffer is currently
4887 * mapped by MapBuffer or if the invalidate range intersects the range
4888 * currently mapped by MapBufferRange, unless it was mapped
4889 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
Ian Romanickf241ffd2012-08-13 10:27:33 -07004890 */
Marek Olšákd26a0652014-01-27 21:36:53 +01004891 if (_mesa_check_disallowed_mapping(bufObj)) {
Ian Romanickf241ffd2012-08-13 10:27:33 -07004892 _mesa_error(ctx, GL_INVALID_OPERATION,
4893 "glInvalidateBufferData(intersection with mapped "
4894 "range)");
4895 return;
4896 }
4897
Samuel Pitoisetec0c2eb2017-05-31 14:14:55 +02004898 invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size);
Ian Romanickf241ffd2012-08-13 10:27:33 -07004899}
Nicolai Hähnled085c7c2017-02-02 20:47:31 +01004900
Nicolai Hähnle4e6feac2017-02-02 23:24:35 +01004901static void
4902buffer_page_commitment(struct gl_context *ctx,
4903 struct gl_buffer_object *bufferObj,
4904 GLintptr offset, GLsizeiptr size,
4905 GLboolean commit, const char *func)
4906{
4907 if (!(bufferObj->StorageFlags & GL_SPARSE_STORAGE_BIT_ARB)) {
4908 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(not a sparse buffer object)",
4909 func);
4910 return;
4911 }
4912
4913 if (size < 0 || size > bufferObj->Size ||
4914 offset < 0 || offset > bufferObj->Size - size) {
4915 _mesa_error(ctx, GL_INVALID_VALUE, "%s(out of bounds)",
4916 func);
4917 return;
4918 }
4919
4920 /* The GL_ARB_sparse_buffer extension specification says:
4921 *
4922 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4923 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4924 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4925 * not extend to the end of the buffer's data store."
4926 */
4927 if (offset % ctx->Const.SparseBufferPageSize != 0) {
4928 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset not aligned to page size)",
4929 func);
4930 return;
4931 }
4932
4933 if (size % ctx->Const.SparseBufferPageSize != 0 &&
4934 offset + size != bufferObj->Size) {
4935 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size not aligned to page size)",
4936 func);
4937 return;
4938 }
4939
4940 ctx->Driver.BufferPageCommitment(ctx, bufferObj, offset, size, commit);
4941}
4942
Nicolai Hähnled085c7c2017-02-02 20:47:31 +01004943void GLAPIENTRY
4944_mesa_BufferPageCommitmentARB(GLenum target, GLintptr offset, GLsizeiptr size,
4945 GLboolean commit)
4946{
Nicolai Hähnle4e6feac2017-02-02 23:24:35 +01004947 GET_CURRENT_CONTEXT(ctx);
4948 struct gl_buffer_object *bufferObj;
4949
4950 bufferObj = get_buffer(ctx, "glBufferPageCommitmentARB", target,
4951 GL_INVALID_ENUM);
4952 if (!bufferObj)
4953 return;
4954
4955 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4956 "glBufferPageCommitmentARB");
Nicolai Hähnled085c7c2017-02-02 20:47:31 +01004957}
4958
4959void GLAPIENTRY
4960_mesa_NamedBufferPageCommitmentARB(GLuint buffer, GLintptr offset,
4961 GLsizeiptr size, GLboolean commit)
4962{
Nicolai Hähnle4e6feac2017-02-02 23:24:35 +01004963 GET_CURRENT_CONTEXT(ctx);
4964 struct gl_buffer_object *bufferObj;
4965
4966 bufferObj = _mesa_lookup_bufferobj(ctx, buffer);
4967 if (!bufferObj || bufferObj == &DummyBufferObject) {
4968 /* Note: the extension spec is not clear about the excpected error value. */
4969 _mesa_error(ctx, GL_INVALID_VALUE,
4970 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4971 buffer);
4972 return;
4973 }
4974
4975 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
4976 "glNamedBufferPageCommitmentARB");
Nicolai Hähnled085c7c2017-02-02 20:47:31 +01004977}
Pierre-Eric Pelloux-Prayer1ef29762019-11-07 14:25:19 +01004978
4979void GLAPIENTRY
4980_mesa_NamedBufferPageCommitmentEXT(GLuint buffer, GLintptr offset,
4981 GLsizeiptr size, GLboolean commit)
4982{
4983 GET_CURRENT_CONTEXT(ctx);
4984 struct gl_buffer_object *bufferObj;
4985
4986 /* Use NamedBuffer* functions logic from EXT_direct_state_access */
4987 if (buffer != 0) {
4988 bufferObj = _mesa_lookup_bufferobj(ctx, buffer);
4989 if (!_mesa_handle_bind_buffer_gen(ctx, buffer, &bufferObj,
4990 "glNamedBufferPageCommitmentEXT"))
4991 return;
4992 } else {
Marek Olšák58fab9a2020-03-21 23:52:31 -04004993 /* GL_EXT_direct_state_access says about NamedBuffer* functions:
4994 *
4995 * There is no buffer corresponding to the name zero, these commands
4996 * generate the INVALID_OPERATION error if the buffer parameter is
4997 * zero.
4998 */
4999 _mesa_error(ctx, GL_INVALID_OPERATION,
5000 "glNamedBufferPageCommitmentEXT(buffer = 0)");
5001 return;
Pierre-Eric Pelloux-Prayer1ef29762019-11-07 14:25:19 +01005002 }
5003 buffer_page_commitment(ctx, bufferObj, offset, size, commit,
5004 "glNamedBufferPageCommitmentEXT");
5005}