Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 3 | * |
Brian | a429a25 | 2008-03-21 13:41:00 -0600 | [diff] [blame] | 4 | * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 5 | * Copyright (C) 2009 VMware, Inc. All Rights Reserved. |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Kenneth Graunke | 3d8d5b2 | 2013-04-21 13:46:48 -0700 | [diff] [blame] | 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | * OTHER DEALINGS IN THE SOFTWARE. |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * \file bufferobj.c |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 29 | * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions. |
Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 30 | * \author Brian Paul, Ian Romanick |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Ian Romanick | bd4e5dd | 2012-07-25 16:08:12 -0700 | [diff] [blame] | 33 | #include <stdbool.h> |
Brian Paul | 80fa7fd | 2014-08-08 07:49:33 -0600 | [diff] [blame] | 34 | #include <inttypes.h> /* for PRId64 macro */ |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 35 | #include "glheader.h" |
Brian Paul | d6a9f5b | 2010-03-20 11:52:12 -0600 | [diff] [blame] | 36 | #include "enums.h" |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 37 | #include "hash.h" |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 38 | #include "imports.h" |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 39 | #include "context.h" |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 40 | #include "bufferobj.h" |
Vinson Lee | 0117da4 | 2011-01-05 23:11:54 -0800 | [diff] [blame] | 41 | #include "mtypes.h" |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 42 | #include "teximage.h" |
| 43 | #include "glformats.h" |
| 44 | #include "texstore.h" |
Kenneth Graunke | 3edd2ba | 2012-06-04 00:51:34 -0700 | [diff] [blame] | 45 | #include "transformfeedback.h" |
Ilia Mirkin | a1d664a | 2016-01-04 19:48:08 -0500 | [diff] [blame] | 46 | |
Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 47 | |
Brian Paul | 4a7fd63 | 2009-06-09 11:53:19 -0600 | [diff] [blame] | 48 | /* Debug flags */ |
| 49 | /*#define VBO_DEBUG*/ |
| 50 | /*#define BOUNDS_CHECK*/ |
| 51 | |
| 52 | |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 53 | /** |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 54 | * We count the number of buffer modification calls to check for |
| 55 | * inefficient buffer use. This is the number of such calls before we |
| 56 | * issue a warning. |
| 57 | */ |
| 58 | #define BUFFER_WARNING_CALL_COUNT 4 |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Helper to warn of possible performance issues, such as frequently |
Brian Paul | e1815bc | 2015-12-09 16:00:55 -0700 | [diff] [blame] | 63 | * updating a buffer created with GL_STATIC_DRAW. Called via the macro |
| 64 | * below. |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 65 | */ |
| 66 | static void |
Brian Paul | e1815bc | 2015-12-09 16:00:55 -0700 | [diff] [blame] | 67 | buffer_usage_warning(struct gl_context *ctx, GLuint *id, const char *fmt, ...) |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 68 | { |
| 69 | va_list args; |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 70 | |
| 71 | va_start(args, fmt); |
Brian Paul | e1815bc | 2015-12-09 16:00:55 -0700 | [diff] [blame] | 72 | _mesa_gl_vdebug(ctx, id, |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 73 | MESA_DEBUG_SOURCE_API, |
| 74 | MESA_DEBUG_TYPE_PERFORMANCE, |
| 75 | MESA_DEBUG_SEVERITY_MEDIUM, |
| 76 | fmt, args); |
| 77 | va_end(args); |
| 78 | } |
| 79 | |
Brian Paul | e1815bc | 2015-12-09 16:00:55 -0700 | [diff] [blame] | 80 | #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \ |
| 81 | do { \ |
| 82 | static GLuint id = 0; \ |
| 83 | buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \ |
| 84 | } while (0) |
| 85 | |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
Brian Paul | c552f27 | 2010-11-11 14:47:30 -0700 | [diff] [blame] | 88 | * Used as a placeholder for buffer objects between glGenBuffers() and |
| 89 | * glBindBuffer() so that glIsBuffer() can work correctly. |
| 90 | */ |
| 91 | static struct gl_buffer_object DummyBufferObject; |
| 92 | |
| 93 | |
| 94 | /** |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 95 | * Return pointer to address of a buffer object target. |
| 96 | * \param ctx the GL context |
| 97 | * \param target the buffer object target to be retrieved. |
| 98 | * \return pointer to pointer to the buffer object bound to \c target in the |
| 99 | * specified context or \c NULL if \c target is invalid. |
| 100 | */ |
Brian Paul | 9520f48 | 2011-09-30 21:03:42 -0600 | [diff] [blame] | 101 | static inline struct gl_buffer_object ** |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 102 | get_buffer_target(struct gl_context *ctx, GLenum target) |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 103 | { |
Ian Romanick | b0b6b76 | 2012-07-25 15:29:48 -0700 | [diff] [blame] | 104 | /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0. |
| 105 | */ |
| 106 | if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx) |
| 107 | && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER) |
| 108 | return NULL; |
| 109 | |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 110 | switch (target) { |
| 111 | case GL_ARRAY_BUFFER_ARB: |
| 112 | return &ctx->Array.ArrayBufferObj; |
| 113 | case GL_ELEMENT_ARRAY_BUFFER_ARB: |
Kenneth Graunke | e1b1f2a | 2014-02-01 19:46:45 -0800 | [diff] [blame] | 114 | return &ctx->Array.VAO->IndexBufferObj; |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 115 | case GL_PIXEL_PACK_BUFFER_EXT: |
| 116 | return &ctx->Pack.BufferObj; |
| 117 | case GL_PIXEL_UNPACK_BUFFER_EXT: |
| 118 | return &ctx->Unpack.BufferObj; |
| 119 | case GL_COPY_READ_BUFFER: |
Ian Romanick | 4da5f1b | 2010-09-27 14:29:12 -0700 | [diff] [blame] | 120 | return &ctx->CopyReadBuffer; |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 121 | case GL_COPY_WRITE_BUFFER: |
Ian Romanick | 4da5f1b | 2010-09-27 14:29:12 -0700 | [diff] [blame] | 122 | return &ctx->CopyWriteBuffer; |
Chris Forbes | a95236c | 2013-11-06 20:03:21 +1300 | [diff] [blame] | 123 | case GL_DRAW_INDIRECT_BUFFER: |
Marta Lofstedt | 2e0179e | 2015-05-11 15:03:49 +0200 | [diff] [blame] | 124 | if ((ctx->API == API_OPENGL_CORE && |
| 125 | ctx->Extensions.ARB_draw_indirect) || |
| 126 | _mesa_is_gles31(ctx)) { |
Chris Forbes | a95236c | 2013-11-06 20:03:21 +1300 | [diff] [blame] | 127 | return &ctx->DrawIndirectBuffer; |
| 128 | } |
| 129 | break; |
Ilia Mirkin | 9327e2d | 2015-12-31 15:47:17 -0500 | [diff] [blame] | 130 | case GL_PARAMETER_BUFFER_ARB: |
| 131 | if (_mesa_has_ARB_indirect_parameters(ctx)) { |
| 132 | return &ctx->ParameterBuffer; |
| 133 | } |
| 134 | break; |
Jordan Justen | 12cf91d | 2015-09-17 10:10:07 -0700 | [diff] [blame] | 135 | case GL_DISPATCH_INDIRECT_BUFFER: |
| 136 | if (_mesa_has_compute_shaders(ctx)) { |
| 137 | return &ctx->DispatchIndirectBuffer; |
| 138 | } |
| 139 | break; |
Brian Paul | 423860a | 2010-03-30 19:53:01 -0600 | [diff] [blame] | 140 | case GL_TRANSFORM_FEEDBACK_BUFFER: |
| 141 | if (ctx->Extensions.EXT_transform_feedback) { |
| 142 | return &ctx->TransformFeedback.CurrentBuffer; |
| 143 | } |
| 144 | break; |
Brian Paul | 874a2c0 | 2011-04-05 19:02:07 -0600 | [diff] [blame] | 145 | case GL_TEXTURE_BUFFER: |
Marek Olšák | afa902a | 2012-12-08 22:53:23 +0100 | [diff] [blame] | 146 | if (ctx->API == API_OPENGL_CORE && |
| 147 | ctx->Extensions.ARB_texture_buffer_object) { |
Brian Paul | 874a2c0 | 2011-04-05 19:02:07 -0600 | [diff] [blame] | 148 | return &ctx->Texture.BufferObject; |
| 149 | } |
| 150 | break; |
Eric Anholt | c5c696e | 2012-06-18 11:17:04 -0700 | [diff] [blame] | 151 | case GL_UNIFORM_BUFFER: |
| 152 | if (ctx->Extensions.ARB_uniform_buffer_object) { |
| 153 | return &ctx->UniformBuffer; |
| 154 | } |
| 155 | break; |
Iago Toral Quiroga | cd50906 | 2015-03-19 10:15:30 +0100 | [diff] [blame] | 156 | case GL_SHADER_STORAGE_BUFFER: |
| 157 | if (ctx->Extensions.ARB_shader_storage_buffer_object) { |
| 158 | return &ctx->ShaderStorageBuffer; |
| 159 | } |
| 160 | break; |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 161 | case GL_ATOMIC_COUNTER_BUFFER: |
| 162 | if (ctx->Extensions.ARB_shader_atomic_counters) { |
| 163 | return &ctx->AtomicBuffer; |
| 164 | } |
| 165 | break; |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 166 | case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD: |
| 167 | if (ctx->Extensions.AMD_pinned_memory) { |
| 168 | return &ctx->ExternalVirtualMemoryBuffer; |
| 169 | } |
| 170 | break; |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 171 | default: |
| 172 | return NULL; |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 179 | * Get the buffer object bound to the specified target in a GL context. |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 180 | * \param ctx the GL context |
| 181 | * \param target the buffer object target to be retrieved. |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 182 | * \param error the GL error to record if target is illegal. |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 183 | * \return pointer to the buffer object bound to \c target in the |
Brian Paul | 4277ea4 | 2006-08-25 22:06:02 +0000 | [diff] [blame] | 184 | * specified context or \c NULL if \c target is invalid. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 185 | */ |
Brian Paul | 9520f48 | 2011-09-30 21:03:42 -0600 | [diff] [blame] | 186 | static inline struct gl_buffer_object * |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 187 | get_buffer(struct gl_context *ctx, const char *func, GLenum target, |
| 188 | GLenum error) |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 189 | { |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 190 | struct gl_buffer_object **bufObj = get_buffer_target(ctx, target); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 191 | |
| 192 | if (!bufObj) { |
| 193 | _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | if (!_mesa_is_bufferobj(*bufObj)) { |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 198 | _mesa_error(ctx, error, "%s(no buffer bound)", func); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | return *bufObj; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
Kenneth Graunke | 8907b6a | 2012-11-17 21:23:28 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Convert a GLbitfield describing the mapped buffer access flags |
| 208 | * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY. |
| 209 | */ |
| 210 | static GLenum |
| 211 | simplified_access_mode(struct gl_context *ctx, GLbitfield access) |
Ian Romanick | f0ea467 | 2012-01-17 16:24:05 -0800 | [diff] [blame] | 212 | { |
Kenneth Graunke | 8907b6a | 2012-11-17 21:23:28 -0800 | [diff] [blame] | 213 | const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT; |
| 214 | if ((access & rwFlags) == rwFlags) |
| 215 | return GL_READ_WRITE; |
| 216 | if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT) |
| 217 | return GL_READ_ONLY; |
| 218 | if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT) |
| 219 | return GL_WRITE_ONLY; |
| 220 | |
| 221 | /* Otherwise, AccessFlags is zero (the default state). |
| 222 | * |
| 223 | * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says: |
Ian Romanick | f0ea467 | 2012-01-17 16:24:05 -0800 | [diff] [blame] | 224 | * |
| 225 | * Name Type Initial Value Legal Values |
| 226 | * ... ... ... ... |
| 227 | * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY |
| 228 | * READ_WRITE |
| 229 | * |
| 230 | * However, table 6.8 in the GL_OES_mapbuffer extension says: |
| 231 | * |
| 232 | * Get Value Type Get Command Value Description |
| 233 | * --------- ---- ----------- ----- ----------- |
| 234 | * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag |
| 235 | * |
| 236 | * The difference is because GL_OES_mapbuffer only supports mapping buffers |
| 237 | * write-only. |
| 238 | */ |
Kenneth Graunke | 8907b6a | 2012-11-17 21:23:28 -0800 | [diff] [blame] | 239 | assert(access == 0); |
Ian Romanick | f0ea467 | 2012-01-17 16:24:05 -0800 | [diff] [blame] | 240 | |
Kenneth Graunke | 8907b6a | 2012-11-17 21:23:28 -0800 | [diff] [blame] | 241 | return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE; |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 246 | * Test if the buffer is mapped, and if so, if the mapped range overlaps the |
| 247 | * given range. |
| 248 | * The regions do not overlap if and only if the end of the given |
| 249 | * region is before the mapped region or the start of the given region |
| 250 | * is after the mapped region. |
| 251 | * |
| 252 | * \param obj Buffer object target on which to operate. |
| 253 | * \param offset Offset of the first byte of the subdata range. |
| 254 | * \param size Size, in bytes, of the subdata range. |
| 255 | * \return true if ranges overlap, false otherwise |
| 256 | * |
| 257 | */ |
| 258 | static bool |
| 259 | bufferobj_range_mapped(const struct gl_buffer_object *obj, |
| 260 | GLintptr offset, GLsizeiptr size) |
| 261 | { |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 262 | if (_mesa_bufferobj_mapped(obj, MAP_USER)) { |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 263 | const GLintptr end = offset + size; |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 264 | const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset + |
| 265 | obj->Mappings[MAP_USER].Length; |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 266 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 267 | if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) { |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 276 | * Tests the subdata range parameters and sets the GL error code for |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 277 | * \c glBufferSubDataARB, \c glGetBufferSubDataARB and |
| 278 | * \c glClearBufferSubData. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 279 | * |
| 280 | * \param ctx GL context. |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 281 | * \param bufObj The buffer object. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 282 | * \param offset Offset of the first byte of the subdata range. |
| 283 | * \param size Size, in bytes, of the subdata range. |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 284 | * \param mappedRange If true, checks if an overlapping range is mapped. |
| 285 | * If false, checks if buffer is mapped. |
Brian Paul | 6f172f7 | 2006-06-02 22:51:45 +0000 | [diff] [blame] | 286 | * \param caller Name of calling function for recording errors. |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 287 | * \return false if error, true otherwise |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 288 | * |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 289 | * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 290 | */ |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 291 | static bool |
| 292 | buffer_object_subdata_range_good(struct gl_context *ctx, |
Brian Paul | 4879b76 | 2015-09-17 09:45:42 -0600 | [diff] [blame] | 293 | const struct gl_buffer_object *bufObj, |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 294 | GLintptr offset, GLsizeiptr size, |
| 295 | bool mappedRange, const char *caller) |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 296 | { |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 297 | if (size < 0) { |
Brian Paul | 6f172f7 | 2006-06-02 22:51:45 +0000 | [diff] [blame] | 298 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller); |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 299 | return false; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (offset < 0) { |
Brian Paul | 6f172f7 | 2006-06-02 22:51:45 +0000 | [diff] [blame] | 303 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller); |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 304 | return false; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Brian | 7e85b0a | 2007-01-23 12:50:08 -0700 | [diff] [blame] | 307 | if (offset + size > bufObj->Size) { |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 308 | _mesa_error(ctx, GL_INVALID_VALUE, |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 309 | "%s(offset %lu + size %lu > buffer size %lu)", caller, |
Brian Paul | b330f1f | 2012-01-04 14:15:53 -0700 | [diff] [blame] | 310 | (unsigned long) offset, |
| 311 | (unsigned long) size, |
| 312 | (unsigned long) bufObj->Size); |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 313 | return false; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 314 | } |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 315 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 316 | if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 317 | return true; |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 318 | |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 319 | if (mappedRange) { |
| 320 | if (bufferobj_range_mapped(bufObj, offset, size)) { |
Laura Ekstrand | 9cb732b | 2015-02-11 11:06:42 -0800 | [diff] [blame] | 321 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 322 | "%s(range is mapped without persistent bit)", |
| 323 | caller); |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 324 | return false; |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | else { |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 328 | if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) { |
Laura Ekstrand | 9cb732b | 2015-02-11 11:06:42 -0800 | [diff] [blame] | 329 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 330 | "%s(buffer is mapped without persistent bit)", |
| 331 | caller); |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 332 | return false; |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 333 | } |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 336 | return true; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 341 | * Test the format and type parameters and set the GL error code for |
| 342 | * \c glClearBufferData and \c glClearBufferSubData. |
| 343 | * |
| 344 | * \param ctx GL context. |
| 345 | * \param internalformat Format to which the data is to be converted. |
| 346 | * \param format Format of the supplied data. |
| 347 | * \param type Type of the supplied data. |
| 348 | * \param caller Name of calling function for recording errors. |
Mark Mueller | 71fe943 | 2014-01-04 14:11:43 -0800 | [diff] [blame] | 349 | * \return If internalformat, format and type are legal the mesa_format |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 350 | * corresponding to internalformat, otherwise MESA_FORMAT_NONE. |
| 351 | * |
| 352 | * \sa glClearBufferData and glClearBufferSubData |
| 353 | */ |
Mark Mueller | 71fe943 | 2014-01-04 14:11:43 -0800 | [diff] [blame] | 354 | static mesa_format |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 355 | validate_clear_buffer_format(struct gl_context *ctx, |
| 356 | GLenum internalformat, |
| 357 | GLenum format, GLenum type, |
| 358 | const char *caller) |
| 359 | { |
Mark Mueller | 71fe943 | 2014-01-04 14:11:43 -0800 | [diff] [blame] | 360 | mesa_format mesaFormat; |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 361 | GLenum errorFormatType; |
| 362 | |
| 363 | mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat); |
| 364 | if (mesaFormat == MESA_FORMAT_NONE) { |
| 365 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 366 | "%s(invalid internalformat)", caller); |
| 367 | return MESA_FORMAT_NONE; |
| 368 | } |
| 369 | |
| 370 | /* NOTE: not mentioned in ARB_clear_buffer_object but according to |
| 371 | * EXT_texture_integer there is no conversion between integer and |
| 372 | * non-integer formats |
| 373 | */ |
| 374 | if (_mesa_is_enum_format_signed_int(format) != |
| 375 | _mesa_is_format_integer_color(mesaFormat)) { |
| 376 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 377 | "%s(integer vs non-integer)", caller); |
| 378 | return MESA_FORMAT_NONE; |
| 379 | } |
| 380 | |
| 381 | if (!_mesa_is_color_format(format)) { |
| 382 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 383 | "%s(format is not a color format)", caller); |
| 384 | return MESA_FORMAT_NONE; |
| 385 | } |
| 386 | |
| 387 | errorFormatType = _mesa_error_check_format_and_type(ctx, format, type); |
| 388 | if (errorFormatType != GL_NO_ERROR) { |
| 389 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 390 | "%s(invalid format or type)", caller); |
| 391 | return MESA_FORMAT_NONE; |
| 392 | } |
| 393 | |
| 394 | return mesaFormat; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * Convert user-specified clear value to the specified internal format. |
| 400 | * |
| 401 | * \param ctx GL context. |
| 402 | * \param internalformat Format to which the data is converted. |
| 403 | * \param clearValue Points to the converted clear value. |
| 404 | * \param format Format of the supplied data. |
| 405 | * \param type Type of the supplied data. |
| 406 | * \param data Data which is to be converted to internalformat. |
| 407 | * \param caller Name of calling function for recording errors. |
| 408 | * \return true if data could be converted, false otherwise. |
| 409 | * |
| 410 | * \sa glClearBufferData, glClearBufferSubData |
| 411 | */ |
| 412 | static bool |
| 413 | convert_clear_buffer_data(struct gl_context *ctx, |
Mark Mueller | 71fe943 | 2014-01-04 14:11:43 -0800 | [diff] [blame] | 414 | mesa_format internalformat, |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 415 | GLubyte *clearValue, GLenum format, GLenum type, |
| 416 | const GLvoid *data, const char *caller) |
| 417 | { |
| 418 | GLenum internalformatBase = _mesa_get_format_base_format(internalformat); |
| 419 | |
| 420 | if (_mesa_texstore(ctx, 1, internalformatBase, internalformat, |
| 421 | 0, &clearValue, 1, 1, 1, |
| 422 | format, type, data, &ctx->Unpack)) { |
| 423 | return true; |
| 424 | } |
| 425 | else { |
| 426 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller); |
| 427 | return false; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /** |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 433 | * Allocate and initialize a new buffer object. |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 434 | * |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 435 | * Default callback for the \c dd_function_table::NewBufferObject() hook. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 436 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 437 | static struct gl_buffer_object * |
Kenneth Graunke | 63c6509 | 2014-10-15 13:00:39 -0700 | [diff] [blame] | 438 | _mesa_new_buffer_object(struct gl_context *ctx, GLuint name) |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 439 | { |
| 440 | struct gl_buffer_object *obj; |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 441 | |
| 442 | (void) ctx; |
| 443 | |
Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 444 | obj = MALLOC_STRUCT(gl_buffer_object); |
Kenneth Graunke | 63c6509 | 2014-10-15 13:00:39 -0700 | [diff] [blame] | 445 | _mesa_initialize_buffer_object(ctx, obj, name); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 446 | return obj; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /** |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 451 | * Delete a buffer object. |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 452 | * |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 453 | * Default callback for the \c dd_function_table::DeleteBuffer() hook. |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 454 | */ |
Nicolai Hähnle | 6aed083 | 2016-01-05 21:47:04 -0500 | [diff] [blame] | 455 | void |
Brian Paul | 79f4281 | 2011-03-19 14:17:40 -0600 | [diff] [blame] | 456 | _mesa_delete_buffer_object(struct gl_context *ctx, |
| 457 | struct gl_buffer_object *bufObj) |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 458 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 459 | (void) ctx; |
| 460 | |
Brian Paul | df21f31 | 2014-01-31 09:52:10 -0700 | [diff] [blame] | 461 | _mesa_align_free(bufObj->Data); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 462 | |
| 463 | /* assign strange values here to help w/ debugging */ |
| 464 | bufObj->RefCount = -1000; |
| 465 | bufObj->Name = ~0; |
| 466 | |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 467 | mtx_destroy(&bufObj->Mutex); |
Timothy Arceri | 6d8dd59 | 2013-08-26 17:16:08 +1000 | [diff] [blame] | 468 | free(bufObj->Label); |
Kristian Høgsberg | 32f2fd1 | 2010-02-19 11:58:49 -0500 | [diff] [blame] | 469 | free(bufObj); |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Set ptr to bufObj w/ reference counting. |
Brian Paul | 6214963 | 2011-07-14 08:09:21 -0600 | [diff] [blame] | 476 | * This is normally only called from the _mesa_reference_buffer_object() macro |
| 477 | * when there's a real pointer change. |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 478 | */ |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 479 | void |
Brian Paul | 6214963 | 2011-07-14 08:09:21 -0600 | [diff] [blame] | 480 | _mesa_reference_buffer_object_(struct gl_context *ctx, |
| 481 | struct gl_buffer_object **ptr, |
| 482 | struct gl_buffer_object *bufObj) |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 483 | { |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 484 | if (*ptr) { |
Brian Paul | 105c852 | 2009-05-07 13:10:48 -0600 | [diff] [blame] | 485 | /* Unreference the old buffer */ |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 486 | GLboolean deleteFlag = GL_FALSE; |
| 487 | struct gl_buffer_object *oldObj = *ptr; |
| 488 | |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 489 | mtx_lock(&oldObj->Mutex); |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 490 | assert(oldObj->RefCount > 0); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 491 | oldObj->RefCount--; |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 492 | deleteFlag = (oldObj->RefCount == 0); |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 493 | mtx_unlock(&oldObj->Mutex); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 494 | |
| 495 | if (deleteFlag) { |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 496 | assert(ctx->Driver.DeleteBuffer); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 497 | ctx->Driver.DeleteBuffer(ctx, oldObj); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 498 | } |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 499 | |
| 500 | *ptr = NULL; |
| 501 | } |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 502 | assert(!*ptr); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 503 | |
| 504 | if (bufObj) { |
Brian Paul | 105c852 | 2009-05-07 13:10:48 -0600 | [diff] [blame] | 505 | /* reference new buffer */ |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 506 | mtx_lock(&bufObj->Mutex); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 507 | if (bufObj->RefCount == 0) { |
| 508 | /* this buffer's being deleted (look just above) */ |
| 509 | /* Not sure this can every really happen. Warn if it does. */ |
| 510 | _mesa_problem(NULL, "referencing deleted buffer object"); |
| 511 | *ptr = NULL; |
| 512 | } |
| 513 | else { |
| 514 | bufObj->RefCount++; |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 515 | *ptr = bufObj; |
| 516 | } |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 517 | mtx_unlock(&bufObj->Mutex); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 522 | /** |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 523 | * Initialize a buffer object to default values. |
| 524 | */ |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 525 | void |
Kenneth Graunke | 63c6509 | 2014-10-15 13:00:39 -0700 | [diff] [blame] | 526 | _mesa_initialize_buffer_object(struct gl_context *ctx, |
| 527 | struct gl_buffer_object *obj, |
| 528 | GLuint name) |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 529 | { |
Brian Paul | 6bf1ea8 | 2010-02-19 08:32:36 -0700 | [diff] [blame] | 530 | memset(obj, 0, sizeof(struct gl_buffer_object)); |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 531 | mtx_init(&obj->Mutex, mtx_plain); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 532 | obj->RefCount = 1; |
| 533 | obj->Name = name; |
Brian Paul | 85ad44b | 2004-02-13 14:04:26 +0000 | [diff] [blame] | 534 | obj->Usage = GL_STATIC_DRAW_ARB; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | |
Brian Paul | 223654b | 2012-04-06 15:44:56 -0600 | [diff] [blame] | 538 | |
| 539 | /** |
| 540 | * Callback called from _mesa_HashWalk() |
| 541 | */ |
| 542 | static void |
| 543 | count_buffer_size(GLuint key, void *data, void *userData) |
| 544 | { |
| 545 | const struct gl_buffer_object *bufObj = |
| 546 | (const struct gl_buffer_object *) data; |
| 547 | GLuint *total = (GLuint *) userData; |
| 548 | |
Ian Romanick | 882aab0 | 2015-08-26 13:50:04 +0100 | [diff] [blame] | 549 | (void) key; |
Brian Paul | 223654b | 2012-04-06 15:44:56 -0600 | [diff] [blame] | 550 | *total = *total + bufObj->Size; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | /** |
| 555 | * Compute total size (in bytes) of all buffer objects for the given context. |
| 556 | * For debugging purposes. |
| 557 | */ |
| 558 | GLuint |
| 559 | _mesa_total_buffer_object_memory(struct gl_context *ctx) |
| 560 | { |
| 561 | GLuint total = 0; |
| 562 | |
| 563 | _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total); |
| 564 | |
| 565 | return total; |
| 566 | } |
| 567 | |
| 568 | |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 569 | /** |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 570 | * Allocate space for and store data in a buffer object. Any data that was |
| 571 | * previously stored in the buffer object is lost. If \c data is \c NULL, |
| 572 | * memory will be allocated, but no copy will occur. |
| 573 | * |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 574 | * This is the default callback for \c dd_function_table::BufferData() |
| 575 | * Note that all GL error checking will have been done already. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 576 | * |
| 577 | * \param ctx GL context. |
| 578 | * \param target Buffer object target on which to operate. |
| 579 | * \param size Size, in bytes, of the new data store. |
| 580 | * \param data Pointer to the data to store in the buffer object. This |
| 581 | * pointer may be \c NULL. |
| 582 | * \param usage Hints about how the data will be used. |
| 583 | * \param bufObj Object to be used. |
| 584 | * |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 585 | * \return GL_TRUE for success, GL_FALSE for failure |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 586 | * \sa glBufferDataARB, dd_function_table::BufferData. |
| 587 | */ |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 588 | static GLboolean |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 589 | buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size, |
| 590 | const GLvoid *data, GLenum usage, GLenum storageFlags, |
| 591 | struct gl_buffer_object *bufObj) |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 592 | { |
| 593 | void * new_data; |
| 594 | |
Siavash Eliasi | f772d51 | 2013-11-28 12:26:37 +0330 | [diff] [blame] | 595 | (void) target; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 596 | |
Siavash Eliasi | 61bc014 | 2014-01-30 11:38:55 +0330 | [diff] [blame] | 597 | _mesa_align_free( bufObj->Data ); |
Siavash Eliasi | f772d51 | 2013-11-28 12:26:37 +0330 | [diff] [blame] | 598 | |
| 599 | new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment ); |
Brian Paul | a789252 | 2004-11-22 20:01:25 +0000 | [diff] [blame] | 600 | if (new_data) { |
Brian Paul | e4fcea2 | 2003-09-19 15:38:15 +0000 | [diff] [blame] | 601 | bufObj->Data = (GLubyte *) new_data; |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 602 | bufObj->Size = size; |
| 603 | bufObj->Usage = usage; |
Marek Olšák | 7e548d0 | 2014-01-27 21:15:19 +0100 | [diff] [blame] | 604 | bufObj->StorageFlags = storageFlags; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 605 | |
Brian Paul | a789252 | 2004-11-22 20:01:25 +0000 | [diff] [blame] | 606 | if (data) { |
Kenneth Graunke | c7ac4862 | 2010-02-18 23:50:59 -0800 | [diff] [blame] | 607 | memcpy( bufObj->Data, data, size ); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 608 | } |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 609 | |
| 610 | return GL_TRUE; |
| 611 | } |
| 612 | else { |
| 613 | return GL_FALSE; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | |
| 618 | /** |
| 619 | * Replace data in a subrange of buffer object. If the data range |
| 620 | * specified by \c size + \c offset extends beyond the end of the buffer or |
| 621 | * if \c data is \c NULL, no copy is performed. |
| 622 | * |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 623 | * This is the default callback for \c dd_function_table::BufferSubData() |
| 624 | * Note that all GL error checking will have been done already. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 625 | * |
| 626 | * \param ctx GL context. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 627 | * \param offset Offset of the first byte to be modified. |
| 628 | * \param size Size, in bytes, of the data range. |
| 629 | * \param data Pointer to the data to store in the buffer object. |
| 630 | * \param bufObj Object to be used. |
| 631 | * |
| 632 | * \sa glBufferSubDataARB, dd_function_table::BufferSubData. |
| 633 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 634 | static void |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 635 | buffer_sub_data_fallback(struct gl_context *ctx, GLintptr offset, |
| 636 | GLsizeiptr size, const GLvoid *data, |
| 637 | struct gl_buffer_object *bufObj) |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 638 | { |
Ian Romanick | 92f3fca | 2011-08-21 17:23:58 -0700 | [diff] [blame] | 639 | (void) ctx; |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 640 | |
Brian Paul | 76785cb | 2006-09-21 22:59:50 +0000 | [diff] [blame] | 641 | /* this should have been caught in _mesa_BufferSubData() */ |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 642 | assert(size + offset <= bufObj->Size); |
Brian Paul | 76785cb | 2006-09-21 22:59:50 +0000 | [diff] [blame] | 643 | |
| 644 | if (bufObj->Data) { |
Kenneth Graunke | c7ac4862 | 2010-02-18 23:50:59 -0800 | [diff] [blame] | 645 | memcpy( (GLubyte *) bufObj->Data + offset, data, size ); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /** |
| 651 | * Retrieve data from a subrange of buffer object. If the data range |
| 652 | * specified by \c size + \c offset extends beyond the end of the buffer or |
| 653 | * if \c data is \c NULL, no copy is performed. |
| 654 | * |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 655 | * This is the default callback for \c dd_function_table::GetBufferSubData() |
| 656 | * Note that all GL error checking will have been done already. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 657 | * |
| 658 | * \param ctx GL context. |
| 659 | * \param target Buffer object target on which to operate. |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 660 | * \param offset Offset of the first byte to be fetched. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 661 | * \param size Size, in bytes, of the data range. |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 662 | * \param data Destination for data |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 663 | * \param bufObj Object to be used. |
| 664 | * |
| 665 | * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData. |
| 666 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 667 | static void |
Ian Romanick | 6c8aa34 | 2011-08-21 17:30:35 -0700 | [diff] [blame] | 668 | _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset, |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 669 | GLsizeiptrARB size, GLvoid * data, |
| 670 | struct gl_buffer_object * bufObj ) |
| 671 | { |
Ian Romanick | 6c8aa34 | 2011-08-21 17:30:35 -0700 | [diff] [blame] | 672 | (void) ctx; |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 673 | |
Brian Paul | c1f2f90 | 2005-03-03 02:05:33 +0000 | [diff] [blame] | 674 | if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) { |
Kenneth Graunke | c7ac4862 | 2010-02-18 23:50:59 -0800 | [diff] [blame] | 675 | memcpy( data, (GLubyte *) bufObj->Data + offset, size ); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | |
| 680 | /** |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 681 | * Clear a subrange of the buffer object with copies of the supplied data. |
| 682 | * If data is NULL the buffer is filled with zeros. |
| 683 | * |
| 684 | * This is the default callback for \c dd_function_table::ClearBufferSubData() |
| 685 | * Note that all GL error checking will have been done already. |
| 686 | * |
| 687 | * \param ctx GL context. |
| 688 | * \param offset Offset of the first byte to be cleared. |
| 689 | * \param size Size, in bytes, of the to be cleared range. |
| 690 | * \param clearValue Source of the data. |
| 691 | * \param clearValueSize Size, in bytes, of the supplied data. |
| 692 | * \param bufObj Object to be cleared. |
| 693 | * |
| 694 | * \sa glClearBufferSubData, glClearBufferData and |
| 695 | * dd_function_table::ClearBufferSubData. |
| 696 | */ |
Ilia Mirkin | f5ba1a1 | 2014-03-25 17:02:48 -0400 | [diff] [blame] | 697 | void |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 698 | _mesa_ClearBufferSubData_sw(struct gl_context *ctx, |
| 699 | GLintptr offset, GLsizeiptr size, |
| 700 | const GLvoid *clearValue, |
| 701 | GLsizeiptr clearValueSize, |
| 702 | struct gl_buffer_object *bufObj) |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 703 | { |
| 704 | GLsizeiptr i; |
| 705 | GLubyte *dest; |
| 706 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 707 | assert(ctx->Driver.MapBufferRange); |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 708 | dest = ctx->Driver.MapBufferRange(ctx, offset, size, |
| 709 | GL_MAP_WRITE_BIT | |
| 710 | GL_MAP_INVALIDATE_RANGE_BIT, |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 711 | bufObj, MAP_INTERNAL); |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 712 | |
| 713 | if (!dest) { |
| 714 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data"); |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | if (clearValue == NULL) { |
| 719 | /* Clear with zeros, per the spec */ |
| 720 | memset(dest, 0, size); |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 721 | ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL); |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 722 | return; |
| 723 | } |
| 724 | |
| 725 | for (i = 0; i < size/clearValueSize; ++i) { |
| 726 | memcpy(dest, clearValue, clearValueSize); |
| 727 | dest += clearValueSize; |
| 728 | } |
| 729 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 730 | ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL); |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
| 734 | /** |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 735 | * Default fallback for \c dd_function_table::MapBufferRange(). |
| 736 | * Called via glMapBufferRange(). |
| 737 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 738 | static void * |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 739 | map_buffer_range_fallback(struct gl_context *ctx, GLintptr offset, |
| 740 | GLsizeiptr length, GLbitfield access, |
| 741 | struct gl_buffer_object *bufObj, |
| 742 | gl_map_buffer_index index) |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 743 | { |
| 744 | (void) ctx; |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 745 | assert(!_mesa_bufferobj_mapped(bufObj, index)); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 746 | /* Just return a direct pointer to the data */ |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 747 | bufObj->Mappings[index].Pointer = bufObj->Data + offset; |
| 748 | bufObj->Mappings[index].Length = length; |
| 749 | bufObj->Mappings[index].Offset = offset; |
| 750 | bufObj->Mappings[index].AccessFlags = access; |
| 751 | return bufObj->Mappings[index].Pointer; |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
| 755 | /** |
| 756 | * Default fallback for \c dd_function_table::FlushMappedBufferRange(). |
| 757 | * Called via glFlushMappedBufferRange(). |
| 758 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 759 | static void |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 760 | flush_mapped_buffer_range_fallback(struct gl_context *ctx, |
| 761 | GLintptr offset, GLsizeiptr length, |
| 762 | struct gl_buffer_object *obj, |
| 763 | gl_map_buffer_index index) |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 764 | { |
| 765 | (void) ctx; |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 766 | (void) offset; |
| 767 | (void) length; |
| 768 | (void) obj; |
Ian Romanick | 882aab0 | 2015-08-26 13:50:04 +0100 | [diff] [blame] | 769 | (void) index; |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 770 | /* no-op */ |
| 771 | } |
| 772 | |
| 773 | |
| 774 | /** |
Laura Ekstrand | f7f5df9 | 2015-01-14 14:52:01 -0800 | [diff] [blame] | 775 | * Default callback for \c dd_function_table::UnmapBuffer(). |
Brian Paul | 2daa413 | 2004-10-31 00:17:42 +0000 | [diff] [blame] | 776 | * |
| 777 | * The input parameters will have been already tested for errors. |
| 778 | * |
| 779 | * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer |
| 780 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 781 | static GLboolean |
Laura Ekstrand | f7f5df9 | 2015-01-14 14:52:01 -0800 | [diff] [blame] | 782 | unmap_buffer_fallback(struct gl_context *ctx, struct gl_buffer_object *bufObj, |
| 783 | gl_map_buffer_index index) |
Brian Paul | 2daa413 | 2004-10-31 00:17:42 +0000 | [diff] [blame] | 784 | { |
| 785 | (void) ctx; |
Brian Paul | 2daa413 | 2004-10-31 00:17:42 +0000 | [diff] [blame] | 786 | /* XXX we might assert here that bufObj->Pointer is non-null */ |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 787 | bufObj->Mappings[index].Pointer = NULL; |
| 788 | bufObj->Mappings[index].Length = 0; |
| 789 | bufObj->Mappings[index].Offset = 0; |
| 790 | bufObj->Mappings[index].AccessFlags = 0x0; |
Brian Paul | 2daa413 | 2004-10-31 00:17:42 +0000 | [diff] [blame] | 791 | return GL_TRUE; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 794 | |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 795 | /** |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 796 | * Default fallback for \c dd_function_table::CopyBufferSubData(). |
Kenneth Graunke | 306c9f0 | 2012-06-08 22:03:50 -0700 | [diff] [blame] | 797 | * Called via glCopyBufferSubData(). |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 798 | */ |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 799 | static void |
Laura Ekstrand | 4adaad5 | 2015-01-13 13:28:08 -0800 | [diff] [blame] | 800 | copy_buffer_sub_data_fallback(struct gl_context *ctx, |
| 801 | struct gl_buffer_object *src, |
| 802 | struct gl_buffer_object *dst, |
| 803 | GLintptr readOffset, GLintptr writeOffset, |
| 804 | GLsizeiptr size) |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 805 | { |
José Fonseca | 3409118 | 2012-01-27 21:54:12 +0000 | [diff] [blame] | 806 | GLubyte *srcPtr, *dstPtr; |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 807 | |
Eric Anholt | 531e444 | 2012-01-25 16:29:28 -0800 | [diff] [blame] | 808 | if (src == dst) { |
| 809 | srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size, |
| 810 | GL_MAP_READ_BIT | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 811 | GL_MAP_WRITE_BIT, src, |
| 812 | MAP_INTERNAL); |
Eric Anholt | 531e444 | 2012-01-25 16:29:28 -0800 | [diff] [blame] | 813 | |
| 814 | if (!srcPtr) |
| 815 | return; |
| 816 | |
| 817 | srcPtr += readOffset; |
| 818 | dstPtr += writeOffset; |
| 819 | } else { |
| 820 | srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size, |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 821 | GL_MAP_READ_BIT, src, |
| 822 | MAP_INTERNAL); |
Eric Anholt | 531e444 | 2012-01-25 16:29:28 -0800 | [diff] [blame] | 823 | dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size, |
| 824 | (GL_MAP_WRITE_BIT | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 825 | GL_MAP_INVALIDATE_RANGE_BIT), dst, |
| 826 | MAP_INTERNAL); |
Eric Anholt | 531e444 | 2012-01-25 16:29:28 -0800 | [diff] [blame] | 827 | } |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 828 | |
Brian Paul | 4c502e0 | 2012-01-07 14:16:27 -0700 | [diff] [blame] | 829 | /* Note: the src and dst regions will never overlap. Trying to do so |
| 830 | * would generate GL_INVALID_VALUE earlier. |
| 831 | */ |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 832 | if (srcPtr && dstPtr) |
Brian Paul | 6aed626 | 2012-01-04 14:53:55 -0700 | [diff] [blame] | 833 | memcpy(dstPtr, srcPtr, size); |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 834 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 835 | ctx->Driver.UnmapBuffer(ctx, src, MAP_INTERNAL); |
Eric Anholt | 531e444 | 2012-01-25 16:29:28 -0800 | [diff] [blame] | 836 | if (dst != src) |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 837 | ctx->Driver.UnmapBuffer(ctx, dst, MAP_INTERNAL); |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
| 841 | |
| 842 | /** |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 843 | * Initialize the state associated with buffer objects |
| 844 | */ |
| 845 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 846 | _mesa_init_buffer_objects( struct gl_context *ctx ) |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 847 | { |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 848 | GLuint i; |
| 849 | |
Brian Paul | c552f27 | 2010-11-11 14:47:30 -0700 | [diff] [blame] | 850 | memset(&DummyBufferObject, 0, sizeof(DummyBufferObject)); |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 851 | mtx_init(&DummyBufferObject.Mutex, mtx_plain); |
Brian Paul | c552f27 | 2010-11-11 14:47:30 -0700 | [diff] [blame] | 852 | DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */ |
| 853 | |
Brian Paul | 0854b7e | 2009-06-10 13:45:01 -0600 | [diff] [blame] | 854 | _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, |
| 855 | ctx->Shared->NullBufferObj); |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 856 | |
Brian Paul | 0854b7e | 2009-06-10 13:45:01 -0600 | [diff] [blame] | 857 | _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, |
| 858 | ctx->Shared->NullBufferObj); |
| 859 | _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, |
| 860 | ctx->Shared->NullBufferObj); |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 861 | |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 862 | _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, |
| 863 | ctx->Shared->NullBufferObj); |
| 864 | |
Iago Toral Quiroga | 98a1a2c7 | 2015-03-19 11:50:51 +0100 | [diff] [blame] | 865 | _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, |
| 866 | ctx->Shared->NullBufferObj); |
| 867 | |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 868 | _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, |
| 869 | ctx->Shared->NullBufferObj); |
| 870 | |
Chris Forbes | a95236c | 2013-11-06 20:03:21 +1300 | [diff] [blame] | 871 | _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, |
| 872 | ctx->Shared->NullBufferObj); |
| 873 | |
Ilia Mirkin | 9327e2d | 2015-12-31 15:47:17 -0500 | [diff] [blame] | 874 | _mesa_reference_buffer_object(ctx, &ctx->ParameterBuffer, |
| 875 | ctx->Shared->NullBufferObj); |
| 876 | |
Jordan Justen | 12cf91d | 2015-09-17 10:10:07 -0700 | [diff] [blame] | 877 | _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer, |
| 878 | ctx->Shared->NullBufferObj); |
| 879 | |
Marek Olšák | 15a4b6d | 2013-05-14 17:58:32 +0200 | [diff] [blame] | 880 | for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) { |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 881 | _mesa_reference_buffer_object(ctx, |
| 882 | &ctx->UniformBufferBindings[i].BufferObject, |
| 883 | ctx->Shared->NullBufferObj); |
| 884 | ctx->UniformBufferBindings[i].Offset = -1; |
| 885 | ctx->UniformBufferBindings[i].Size = -1; |
| 886 | } |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 887 | |
Iago Toral Quiroga | 98a1a2c7 | 2015-03-19 11:50:51 +0100 | [diff] [blame] | 888 | for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) { |
| 889 | _mesa_reference_buffer_object(ctx, |
| 890 | &ctx->ShaderStorageBufferBindings[i].BufferObject, |
| 891 | ctx->Shared->NullBufferObj); |
| 892 | ctx->ShaderStorageBufferBindings[i].Offset = -1; |
| 893 | ctx->ShaderStorageBufferBindings[i].Size = -1; |
| 894 | } |
| 895 | |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 896 | for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) { |
| 897 | _mesa_reference_buffer_object(ctx, |
| 898 | &ctx->AtomicBufferBindings[i].BufferObject, |
| 899 | ctx->Shared->NullBufferObj); |
Marta Lofstedt | dd9d296 | 2015-08-13 12:59:40 +0200 | [diff] [blame] | 900 | ctx->AtomicBufferBindings[i].Offset = 0; |
| 901 | ctx->AtomicBufferBindings[i].Size = 0; |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 902 | } |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 905 | |
Michal Krol | 01d7e3d | 2010-02-09 14:25:41 +0100 | [diff] [blame] | 906 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 907 | _mesa_free_buffer_objects( struct gl_context *ctx ) |
Michal Krol | 01d7e3d | 2010-02-09 14:25:41 +0100 | [diff] [blame] | 908 | { |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 909 | GLuint i; |
| 910 | |
Michal Krol | 01d7e3d | 2010-02-09 14:25:41 +0100 | [diff] [blame] | 911 | _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL); |
Michal Krol | 01d7e3d | 2010-02-09 14:25:41 +0100 | [diff] [blame] | 912 | |
| 913 | _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL); |
| 914 | _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL); |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 915 | |
| 916 | _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL); |
| 917 | |
Iago Toral Quiroga | 98a1a2c7 | 2015-03-19 11:50:51 +0100 | [diff] [blame] | 918 | _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, NULL); |
| 919 | |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 920 | _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, NULL); |
| 921 | |
Aaron Watry | 1883835 | 2014-01-02 11:18:09 -0600 | [diff] [blame] | 922 | _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL); |
| 923 | |
Ilia Mirkin | 9327e2d | 2015-12-31 15:47:17 -0500 | [diff] [blame] | 924 | _mesa_reference_buffer_object(ctx, &ctx->ParameterBuffer, NULL); |
| 925 | |
Jordan Justen | 12cf91d | 2015-09-17 10:10:07 -0700 | [diff] [blame] | 926 | _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer, NULL); |
| 927 | |
Marek Olšák | 15a4b6d | 2013-05-14 17:58:32 +0200 | [diff] [blame] | 928 | for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) { |
Eric Anholt | 5527c2d | 2012-06-14 16:37:22 -0700 | [diff] [blame] | 929 | _mesa_reference_buffer_object(ctx, |
| 930 | &ctx->UniformBufferBindings[i].BufferObject, |
| 931 | NULL); |
| 932 | } |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 933 | |
Iago Toral Quiroga | 98a1a2c7 | 2015-03-19 11:50:51 +0100 | [diff] [blame] | 934 | for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) { |
| 935 | _mesa_reference_buffer_object(ctx, |
| 936 | &ctx->ShaderStorageBufferBindings[i].BufferObject, |
| 937 | NULL); |
| 938 | } |
| 939 | |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 940 | for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) { |
| 941 | _mesa_reference_buffer_object(ctx, |
| 942 | &ctx->AtomicBufferBindings[i].BufferObject, |
| 943 | NULL); |
| 944 | } |
| 945 | |
Michal Krol | 01d7e3d | 2010-02-09 14:25:41 +0100 | [diff] [blame] | 946 | } |
| 947 | |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 948 | bool |
| 949 | _mesa_handle_bind_buffer_gen(struct gl_context *ctx, |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 950 | GLuint buffer, |
| 951 | struct gl_buffer_object **buf_handle, |
| 952 | const char *caller) |
Eric Anholt | 56e82e3 | 2012-06-26 13:17:08 -0700 | [diff] [blame] | 953 | { |
| 954 | struct gl_buffer_object *buf = *buf_handle; |
| 955 | |
Marta Lofstedt | 2bcacc6 | 2016-01-08 14:55:55 +0100 | [diff] [blame] | 956 | if (!buf && (ctx->API == API_OPENGL_CORE)) { |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 957 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller); |
Eric Anholt | 947d8ff | 2012-10-31 15:33:41 -0700 | [diff] [blame] | 958 | return false; |
| 959 | } |
| 960 | |
Eric Anholt | 56e82e3 | 2012-06-26 13:17:08 -0700 | [diff] [blame] | 961 | if (!buf || buf == &DummyBufferObject) { |
| 962 | /* If this is a new buffer object id, or one which was generated but |
| 963 | * never used before, allocate a buffer object now. |
| 964 | */ |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 965 | assert(ctx->Driver.NewBufferObject); |
Kenneth Graunke | 63c6509 | 2014-10-15 13:00:39 -0700 | [diff] [blame] | 966 | buf = ctx->Driver.NewBufferObject(ctx, buffer); |
Eric Anholt | 56e82e3 | 2012-06-26 13:17:08 -0700 | [diff] [blame] | 967 | if (!buf) { |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 968 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller); |
Eric Anholt | 947d8ff | 2012-10-31 15:33:41 -0700 | [diff] [blame] | 969 | return false; |
Eric Anholt | 56e82e3 | 2012-06-26 13:17:08 -0700 | [diff] [blame] | 970 | } |
| 971 | _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf); |
| 972 | *buf_handle = buf; |
| 973 | } |
Eric Anholt | 947d8ff | 2012-10-31 15:33:41 -0700 | [diff] [blame] | 974 | |
| 975 | return true; |
Eric Anholt | 56e82e3 | 2012-06-26 13:17:08 -0700 | [diff] [blame] | 976 | } |
Michal Krol | 01d7e3d | 2010-02-09 14:25:41 +0100 | [diff] [blame] | 977 | |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 978 | /** |
| 979 | * Bind the specified target to buffer for the specified context. |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 980 | * Called by glBindBuffer() and other functions. |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 981 | */ |
| 982 | static void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 983 | bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer) |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 984 | { |
| 985 | struct gl_buffer_object *oldBufObj; |
| 986 | struct gl_buffer_object *newBufObj = NULL; |
| 987 | struct gl_buffer_object **bindTarget = NULL; |
| 988 | |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 989 | bindTarget = get_buffer_target(ctx, target); |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 990 | if (!bindTarget) { |
Eric Anholt | 86af037 | 2010-09-01 14:46:22 -0700 | [diff] [blame] | 991 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target); |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 992 | return; |
| 993 | } |
| 994 | |
| 995 | /* Get pointer to old buffer object (to be unbound) */ |
Brian Paul | dd5691e | 2009-10-27 20:10:30 -0600 | [diff] [blame] | 996 | oldBufObj = *bindTarget; |
Mathias Fröhlich | 9ed8898 | 2011-10-22 12:57:01 +0200 | [diff] [blame] | 997 | if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending) |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 998 | return; /* rebinding the same buffer object- no change */ |
| 999 | |
| 1000 | /* |
| 1001 | * Get pointer to new buffer object (newBufObj) |
| 1002 | */ |
| 1003 | if (buffer == 0) { |
| 1004 | /* The spec says there's not a buffer object named 0, but we use |
| 1005 | * one internally because it simplifies things. |
| 1006 | */ |
Brian Paul | 4f6b704 | 2009-05-07 13:30:39 -0600 | [diff] [blame] | 1007 | newBufObj = ctx->Shared->NullBufferObj; |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1008 | } |
| 1009 | else { |
| 1010 | /* non-default buffer object */ |
| 1011 | newBufObj = _mesa_lookup_bufferobj(ctx, buffer); |
Ian Romanick | 8ba3b76 | 2015-08-26 13:55:54 +0100 | [diff] [blame] | 1012 | if (!_mesa_handle_bind_buffer_gen(ctx, buffer, |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 1013 | &newBufObj, "glBindBuffer")) |
Ian Romanick | 91107b4 | 2012-08-17 17:14:02 -0700 | [diff] [blame] | 1014 | return; |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1015 | } |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 1016 | |
Nicolai Hähnle | fd7229b | 2016-01-07 14:48:27 -0500 | [diff] [blame^] | 1017 | /* record usage history */ |
| 1018 | switch (target) { |
| 1019 | case GL_PIXEL_PACK_BUFFER: |
| 1020 | newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER; |
| 1021 | break; |
| 1022 | default: |
| 1023 | break; |
| 1024 | } |
| 1025 | |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 1026 | /* bind new buffer */ |
| 1027 | _mesa_reference_buffer_object(ctx, bindTarget, newBufObj); |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | /** |
| 1032 | * Update the default buffer objects in the given context to reference those |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 1033 | * specified in the shared state and release those referencing the old |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1034 | * shared state. |
| 1035 | */ |
| 1036 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1037 | _mesa_update_default_objects_buffer_objects(struct gl_context *ctx) |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1038 | { |
| 1039 | /* Bind the NullBufferObj to remove references to those |
| 1040 | * in the shared context hash table. |
| 1041 | */ |
| 1042 | bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0); |
| 1043 | bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0); |
| 1044 | bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0); |
| 1045 | bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0); |
| 1046 | } |
| 1047 | |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 1048 | |
Brian | d933be6 | 2008-03-21 14:19:28 -0600 | [diff] [blame] | 1049 | |
Brian | a429a25 | 2008-03-21 13:41:00 -0600 | [diff] [blame] | 1050 | /** |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1051 | * Return the gl_buffer_object for the given ID. |
| 1052 | * Always return NULL for ID 0. |
| 1053 | */ |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 1054 | struct gl_buffer_object * |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1055 | _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer) |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1056 | { |
| 1057 | if (buffer == 0) |
| 1058 | return NULL; |
| 1059 | else |
| 1060 | return (struct gl_buffer_object *) |
| 1061 | _mesa_HashLookup(ctx->Shared->BufferObjects, buffer); |
| 1062 | } |
| 1063 | |
Brian Paul | 7a6b71e | 2004-03-13 18:21:40 +0000 | [diff] [blame] | 1064 | |
Fredrik Höglund | 28d7335 | 2013-11-15 19:47:20 +0100 | [diff] [blame] | 1065 | struct gl_buffer_object * |
| 1066 | _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer) |
| 1067 | { |
| 1068 | return (struct gl_buffer_object *) |
| 1069 | _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer); |
| 1070 | } |
| 1071 | |
Laura Ekstrand | 768ca8b | 2015-01-09 16:16:48 -0800 | [diff] [blame] | 1072 | /** |
| 1073 | * A convenience function for direct state access functions that throws |
Fredrik Höglund | 2fd21d8 | 2015-03-19 19:44:57 +0100 | [diff] [blame] | 1074 | * GL_INVALID_OPERATION if buffer is not the name of an existing |
| 1075 | * buffer object. |
Laura Ekstrand | 768ca8b | 2015-01-09 16:16:48 -0800 | [diff] [blame] | 1076 | */ |
| 1077 | struct gl_buffer_object * |
| 1078 | _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer, |
| 1079 | const char *caller) |
| 1080 | { |
| 1081 | struct gl_buffer_object *bufObj; |
| 1082 | |
| 1083 | bufObj = _mesa_lookup_bufferobj(ctx, buffer); |
Fredrik Höglund | 2fd21d8 | 2015-03-19 19:44:57 +0100 | [diff] [blame] | 1084 | if (!bufObj || bufObj == &DummyBufferObject) { |
Laura Ekstrand | 768ca8b | 2015-01-09 16:16:48 -0800 | [diff] [blame] | 1085 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Fredrik Höglund | 2fd21d8 | 2015-03-19 19:44:57 +0100 | [diff] [blame] | 1086 | "%s(non-existent buffer object %u)", caller, buffer); |
| 1087 | return NULL; |
| 1088 | } |
Laura Ekstrand | 768ca8b | 2015-01-09 16:16:48 -0800 | [diff] [blame] | 1089 | |
| 1090 | return bufObj; |
| 1091 | } |
| 1092 | |
Fredrik Höglund | 28d7335 | 2013-11-15 19:47:20 +0100 | [diff] [blame] | 1093 | |
| 1094 | void |
| 1095 | _mesa_begin_bufferobj_lookups(struct gl_context *ctx) |
| 1096 | { |
| 1097 | _mesa_HashLockMutex(ctx->Shared->BufferObjects); |
| 1098 | } |
| 1099 | |
| 1100 | |
| 1101 | void |
| 1102 | _mesa_end_bufferobj_lookups(struct gl_context *ctx) |
| 1103 | { |
| 1104 | _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | /** |
| 1109 | * Look up a buffer object for a multi-bind function. |
| 1110 | * |
| 1111 | * Unlike _mesa_lookup_bufferobj(), this function also takes care |
| 1112 | * of generating an error if the buffer ID is not zero or the name |
| 1113 | * of an existing buffer object. |
| 1114 | * |
| 1115 | * If the buffer ID refers to an existing buffer object, a pointer |
| 1116 | * to the buffer object is returned. If the ID is zero, a pointer |
| 1117 | * to the shared NullBufferObj is returned. If the ID is not zero |
| 1118 | * and does not refer to a valid buffer object, this function |
| 1119 | * returns NULL. |
| 1120 | * |
| 1121 | * This function assumes that the caller has already locked the |
| 1122 | * hash table mutex by calling _mesa_begin_bufferobj_lookups(). |
| 1123 | */ |
| 1124 | struct gl_buffer_object * |
| 1125 | _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx, |
| 1126 | const GLuint *buffers, |
| 1127 | GLuint index, const char *caller) |
| 1128 | { |
| 1129 | struct gl_buffer_object *bufObj; |
| 1130 | |
| 1131 | if (buffers[index] != 0) { |
| 1132 | bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]); |
| 1133 | |
| 1134 | /* The multi-bind functions don't create the buffer objects |
| 1135 | when they don't exist. */ |
| 1136 | if (bufObj == &DummyBufferObject) |
| 1137 | bufObj = NULL; |
| 1138 | } else |
| 1139 | bufObj = ctx->Shared->NullBufferObj; |
| 1140 | |
| 1141 | if (!bufObj) { |
| 1142 | /* The ARB_multi_bind spec says: |
| 1143 | * |
| 1144 | * "An INVALID_OPERATION error is generated if any value |
| 1145 | * in <buffers> is not zero or the name of an existing |
| 1146 | * buffer object (per binding)." |
| 1147 | */ |
| 1148 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1149 | "%s(buffers[%u]=%u is not zero or the name " |
| 1150 | "of an existing buffer object)", |
| 1151 | caller, index, buffers[index]); |
| 1152 | } |
| 1153 | |
| 1154 | return bufObj; |
| 1155 | } |
| 1156 | |
| 1157 | |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 1158 | /** |
| 1159 | * If *ptr points to obj, set ptr = the Null/default buffer object. |
| 1160 | * This is a helper for buffer object deletion. |
| 1161 | * The GL spec says that deleting a buffer object causes it to get |
| 1162 | * unbound from all arrays in the current context. |
| 1163 | */ |
| 1164 | static void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1165 | unbind(struct gl_context *ctx, |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 1166 | struct gl_buffer_object **ptr, |
| 1167 | struct gl_buffer_object *obj) |
| 1168 | { |
| 1169 | if (*ptr == obj) { |
Brian Paul | 4f6b704 | 2009-05-07 13:30:39 -0600 | [diff] [blame] | 1170 | _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj); |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 1175 | /** |
| 1176 | * Plug default/fallback buffer object functions into the device |
| 1177 | * driver hooks. |
| 1178 | */ |
| 1179 | void |
| 1180 | _mesa_init_buffer_object_functions(struct dd_function_table *driver) |
| 1181 | { |
| 1182 | /* GL_ARB_vertex/pixel_buffer_object */ |
| 1183 | driver->NewBufferObject = _mesa_new_buffer_object; |
| 1184 | driver->DeleteBuffer = _mesa_delete_buffer_object; |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1185 | driver->BufferData = buffer_data_fallback; |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 1186 | driver->BufferSubData = buffer_sub_data_fallback; |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 1187 | driver->GetBufferSubData = _mesa_buffer_get_subdata; |
Laura Ekstrand | f7f5df9 | 2015-01-14 14:52:01 -0800 | [diff] [blame] | 1188 | driver->UnmapBuffer = unmap_buffer_fallback; |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 1189 | |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1190 | /* GL_ARB_clear_buffer_object */ |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1191 | driver->ClearBufferSubData = _mesa_ClearBufferSubData_sw; |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1192 | |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 1193 | /* GL_ARB_map_buffer_range */ |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 1194 | driver->MapBufferRange = map_buffer_range_fallback; |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 1195 | driver->FlushMappedBufferRange = flush_mapped_buffer_range_fallback; |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 1196 | |
| 1197 | /* GL_ARB_copy_buffer */ |
Laura Ekstrand | 4adaad5 | 2015-01-13 13:28:08 -0800 | [diff] [blame] | 1198 | driver->CopyBufferSubData = copy_buffer_sub_data_fallback; |
Brian Paul | 331eb58f | 2009-06-19 10:00:03 -0600 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 1202 | void |
| 1203 | _mesa_buffer_unmap_all_mappings(struct gl_context *ctx, |
| 1204 | struct gl_buffer_object *bufObj) |
| 1205 | { |
| 1206 | int i; |
| 1207 | |
| 1208 | for (i = 0; i < MAP_COUNT; i++) { |
| 1209 | if (_mesa_bufferobj_mapped(bufObj, i)) { |
| 1210 | ctx->Driver.UnmapBuffer(ctx, bufObj, i); |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 1211 | assert(bufObj->Mappings[i].Pointer == NULL); |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 1212 | bufObj->Mappings[i].AccessFlags = 0; |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 1217 | |
| 1218 | /**********************************************************************/ |
| 1219 | /* API Functions */ |
| 1220 | /**********************************************************************/ |
| 1221 | |
Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 1222 | void GLAPIENTRY |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1223 | _mesa_BindBuffer(GLenum target, GLuint buffer) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1224 | { |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1225 | GET_CURRENT_CONTEXT(ctx); |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1226 | |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 1227 | if (MESA_VERBOSE & VERBOSE_API) { |
Brian Paul | 2634e92 | 2011-02-08 19:19:34 -0700 | [diff] [blame] | 1228 | _mesa_debug(ctx, "glBindBuffer(%s, %u)\n", |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 1229 | _mesa_enum_to_string(target), buffer); |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 1230 | } |
Brian Paul | 2634e92 | 2011-02-08 19:19:34 -0700 | [diff] [blame] | 1231 | |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1232 | bind_buffer_object(ctx, target, buffer); |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1235 | |
| 1236 | /** |
| 1237 | * Delete a set of buffer objects. |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 1238 | * |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1239 | * \param n Number of buffer objects to delete. |
Jose Fonseca | 375457b | 2004-09-09 22:23:24 +0000 | [diff] [blame] | 1240 | * \param ids Array of \c n buffer object IDs. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1241 | */ |
Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 1242 | void GLAPIENTRY |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1243 | _mesa_DeleteBuffers(GLsizei n, const GLuint *ids) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1244 | { |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1245 | GET_CURRENT_CONTEXT(ctx); |
Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 1246 | GLsizei i; |
Brian Paul | 4293a12 | 2011-03-11 09:25:21 -0700 | [diff] [blame] | 1247 | FLUSH_VERTICES(ctx, 0); |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (n < 0) { |
| 1250 | _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)"); |
| 1251 | return; |
| 1252 | } |
| 1253 | |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 1254 | mtx_lock(&ctx->Shared->Mutex); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1255 | |
| 1256 | for (i = 0; i < n; i++) { |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 1257 | struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]); |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1258 | if (bufObj) { |
Kenneth Graunke | aac1415 | 2014-02-01 19:36:59 -0800 | [diff] [blame] | 1259 | struct gl_vertex_array_object *vao = ctx->Array.VAO; |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1260 | GLuint j; |
Brian Paul | 94ec525 | 2004-03-04 14:46:00 +0000 | [diff] [blame] | 1261 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 1262 | assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject); |
Brian Paul | 94ec525 | 2004-03-04 14:46:00 +0000 | [diff] [blame] | 1263 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 1264 | _mesa_buffer_unmap_all_mappings(ctx, bufObj); |
Brian Paul | a7f434b | 2009-02-27 13:04:38 -0700 | [diff] [blame] | 1265 | |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 1266 | /* unbind any vertex pointers bound to this buffer */ |
Brian Paul | c2e130f | 2015-02-28 08:57:11 -0700 | [diff] [blame] | 1267 | for (j = 0; j < ARRAY_SIZE(vao->VertexBinding); j++) { |
Kenneth Graunke | 94e07c1 | 2014-02-01 19:31:22 -0800 | [diff] [blame] | 1268 | unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj); |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | if (ctx->Array.ArrayBufferObj == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1272 | _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 ); |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1273 | } |
Kenneth Graunke | e1b1f2a | 2014-02-01 19:46:45 -0800 | [diff] [blame] | 1274 | if (vao->IndexBufferObj == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1275 | _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Chris Forbes | a95236c | 2013-11-06 20:03:21 +1300 | [diff] [blame] | 1278 | /* unbind ARB_draw_indirect binding point */ |
| 1279 | if (ctx->DrawIndirectBuffer == bufObj) { |
| 1280 | _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 ); |
| 1281 | } |
| 1282 | |
Ilia Mirkin | 9327e2d | 2015-12-31 15:47:17 -0500 | [diff] [blame] | 1283 | /* unbind ARB_indirect_parameters binding point */ |
| 1284 | if (ctx->ParameterBuffer == bufObj) { |
| 1285 | _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB, 0); |
| 1286 | } |
| 1287 | |
Jordan Justen | 12cf91d | 2015-09-17 10:10:07 -0700 | [diff] [blame] | 1288 | /* unbind ARB_compute_shader binding point */ |
| 1289 | if (ctx->DispatchIndirectBuffer == bufObj) { |
| 1290 | _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0); |
| 1291 | } |
| 1292 | |
Kenneth Graunke | cb8ed93 | 2012-06-03 22:34:32 -0700 | [diff] [blame] | 1293 | /* unbind ARB_copy_buffer binding points */ |
| 1294 | if (ctx->CopyReadBuffer == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1295 | _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 ); |
Kenneth Graunke | cb8ed93 | 2012-06-03 22:34:32 -0700 | [diff] [blame] | 1296 | } |
| 1297 | if (ctx->CopyWriteBuffer == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1298 | _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 ); |
Kenneth Graunke | cb8ed93 | 2012-06-03 22:34:32 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Kenneth Graunke | 3edd2ba | 2012-06-04 00:51:34 -0700 | [diff] [blame] | 1301 | /* unbind transform feedback binding points */ |
Kenneth Graunke | cb8ed93 | 2012-06-03 22:34:32 -0700 | [diff] [blame] | 1302 | if (ctx->TransformFeedback.CurrentBuffer == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1303 | _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 ); |
Kenneth Graunke | cb8ed93 | 2012-06-03 22:34:32 -0700 | [diff] [blame] | 1304 | } |
Marek Olšák | 15ac66e | 2011-12-18 02:13:17 +0100 | [diff] [blame] | 1305 | for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) { |
Kenneth Graunke | 3edd2ba | 2012-06-04 00:51:34 -0700 | [diff] [blame] | 1306 | if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) { |
| 1307 | _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 ); |
| 1308 | } |
| 1309 | } |
Kenneth Graunke | cb8ed93 | 2012-06-03 22:34:32 -0700 | [diff] [blame] | 1310 | |
Eric Anholt | bfa046b | 2012-07-31 12:46:25 -0700 | [diff] [blame] | 1311 | /* unbind UBO binding points */ |
| 1312 | for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) { |
| 1313 | if (ctx->UniformBufferBindings[j].BufferObject == bufObj) { |
| 1314 | _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 ); |
| 1315 | } |
| 1316 | } |
| 1317 | |
Eric Anholt | c5c696e | 2012-06-18 11:17:04 -0700 | [diff] [blame] | 1318 | if (ctx->UniformBuffer == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1319 | _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 ); |
Eric Anholt | c5c696e | 2012-06-18 11:17:04 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Iago Toral Quiroga | e72f5ef | 2015-03-19 10:31:23 +0100 | [diff] [blame] | 1322 | /* unbind SSBO binding points */ |
| 1323 | for (j = 0; j < ctx->Const.MaxShaderStorageBufferBindings; j++) { |
| 1324 | if (ctx->ShaderStorageBufferBindings[j].BufferObject == bufObj) { |
| 1325 | _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER, j, 0); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | if (ctx->ShaderStorageBuffer == bufObj) { |
| 1330 | _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER, 0); |
| 1331 | } |
| 1332 | |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 1333 | /* unbind Atomci Buffer binding points */ |
| 1334 | for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) { |
| 1335 | if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) { |
| 1336 | _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 ); |
| 1337 | } |
| 1338 | } |
| 1339 | |
Marek Olšák | e8625a2 | 2015-02-10 01:35:23 +0100 | [diff] [blame] | 1340 | if (ctx->AtomicBuffer == bufObj) { |
Aditya Atluri | f455f34 | 2014-07-24 15:18:49 -0400 | [diff] [blame] | 1341 | _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER, 0 ); |
| 1342 | } |
| 1343 | |
Brian Paul | 395bcad | 2009-02-27 12:41:11 -0700 | [diff] [blame] | 1344 | /* unbind any pixel pack/unpack pointers bound to this buffer */ |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1345 | if (ctx->Pack.BufferObj == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1346 | _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 ); |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1347 | } |
| 1348 | if (ctx->Unpack.BufferObj == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1349 | _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 ); |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Kenneth Graunke | 45c21f8 | 2012-06-09 21:44:58 -0700 | [diff] [blame] | 1352 | if (ctx->Texture.BufferObject == bufObj) { |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1353 | _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 ); |
Kenneth Graunke | 45c21f8 | 2012-06-09 21:44:58 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1356 | if (ctx->ExternalVirtualMemoryBuffer == bufObj) { |
| 1357 | _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0); |
| 1358 | } |
| 1359 | |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 1360 | /* The ID is immediately freed for re-use */ |
Brian Paul | 78587ea | 2010-11-11 15:31:28 -0700 | [diff] [blame] | 1361 | _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]); |
Mathias Fröhlich | 9ed8898 | 2011-10-22 12:57:01 +0200 | [diff] [blame] | 1362 | /* Make sure we do not run into the classic ABA problem on bind. |
| 1363 | * We don't want to allow re-binding a buffer object that's been |
| 1364 | * "deleted" by glDeleteBuffers(). |
| 1365 | * |
| 1366 | * The explicit rebinding to the default object in the current context |
| 1367 | * prevents the above in the current context, but another context |
| 1368 | * sharing the same objects might suffer from this problem. |
| 1369 | * The alternative would be to do the hash lookup in any case on bind |
| 1370 | * which would introduce more runtime overhead than this. |
| 1371 | */ |
| 1372 | bufObj->DeletePending = GL_TRUE; |
Brian Paul | 37c74af | 2008-09-04 14:58:02 -0600 | [diff] [blame] | 1373 | _mesa_reference_buffer_object(ctx, &bufObj, NULL); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 1377 | mtx_unlock(&ctx->Shared->Mutex); |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1380 | |
| 1381 | /** |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1382 | * This is the implementation for glGenBuffers and glCreateBuffers. It is not |
| 1383 | * exposed to the rest of Mesa to encourage the use of nameless buffers in |
| 1384 | * driver internals. |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1385 | */ |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1386 | static void |
| 1387 | create_buffers(GLsizei n, GLuint *buffers, bool dsa) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1388 | { |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1389 | GET_CURRENT_CONTEXT(ctx); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1390 | GLuint first; |
| 1391 | GLint i; |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1392 | struct gl_buffer_object *buf; |
| 1393 | |
| 1394 | const char *func = dsa ? "glCreateBuffers" : "glGenBuffers"; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1395 | |
Brian Paul | 2634e92 | 2011-02-08 19:19:34 -0700 | [diff] [blame] | 1396 | if (MESA_VERBOSE & VERBOSE_API) |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1397 | _mesa_debug(ctx, "%s(%d)\n", func, n); |
Brian Paul | 2634e92 | 2011-02-08 19:19:34 -0700 | [diff] [blame] | 1398 | |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1399 | if (n < 0) { |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1400 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n); |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1401 | return; |
| 1402 | } |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1403 | |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1404 | if (!buffers) { |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | * This must be atomic (generation and allocation of buffer object IDs) |
| 1410 | */ |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 1411 | mtx_lock(&ctx->Shared->Mutex); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1412 | |
| 1413 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n); |
| 1414 | |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1415 | /* Insert the ID and pointer into the hash table. If non-DSA, insert a |
| 1416 | * DummyBufferObject. Otherwise, create a new buffer object and insert |
| 1417 | * it. |
| 1418 | */ |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1419 | for (i = 0; i < n; i++) { |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1420 | buffers[i] = first + i; |
| 1421 | if (dsa) { |
| 1422 | assert(ctx->Driver.NewBufferObject); |
| 1423 | buf = ctx->Driver.NewBufferObject(ctx, buffers[i]); |
| 1424 | if (!buf) { |
| 1425 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); |
Laura Ekstrand | 142909f | 2015-04-01 11:18:32 -0700 | [diff] [blame] | 1426 | mtx_unlock(&ctx->Shared->Mutex); |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1427 | return; |
| 1428 | } |
| 1429 | } |
| 1430 | else |
| 1431 | buf = &DummyBufferObject; |
| 1432 | |
| 1433 | _mesa_HashInsert(ctx->Shared->BufferObjects, buffers[i], buf); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 1436 | mtx_unlock(&ctx->Shared->Mutex); |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Laura Ekstrand | 2cf48c3 | 2014-12-18 17:10:06 -0800 | [diff] [blame] | 1439 | /** |
| 1440 | * Generate a set of unique buffer object IDs and store them in \c buffers. |
| 1441 | * |
| 1442 | * \param n Number of IDs to generate. |
| 1443 | * \param buffers Array of \c n locations to store the IDs. |
| 1444 | */ |
| 1445 | void GLAPIENTRY |
| 1446 | _mesa_GenBuffers(GLsizei n, GLuint *buffers) |
| 1447 | { |
| 1448 | create_buffers(n, buffers, false); |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Create a set of buffer objects and store their unique IDs in \c buffers. |
| 1453 | * |
| 1454 | * \param n Number of IDs to generate. |
| 1455 | * \param buffers Array of \c n locations to store the IDs. |
| 1456 | */ |
| 1457 | void GLAPIENTRY |
| 1458 | _mesa_CreateBuffers(GLsizei n, GLuint *buffers) |
| 1459 | { |
| 1460 | create_buffers(n, buffers, true); |
| 1461 | } |
| 1462 | |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1463 | |
| 1464 | /** |
| 1465 | * Determine if ID is the name of a buffer object. |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 1466 | * |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1467 | * \param id ID of the potential buffer object. |
Brian Paul | 8faed71 | 2015-09-15 14:04:58 -0600 | [diff] [blame] | 1468 | * \return \c GL_TRUE if \c id is the name of a buffer object, |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1469 | * \c GL_FALSE otherwise. |
| 1470 | */ |
Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 1471 | GLboolean GLAPIENTRY |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 1472 | _mesa_IsBuffer(GLuint id) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1473 | { |
Brian Paul | 4cb9fff | 2006-06-03 15:32:27 +0000 | [diff] [blame] | 1474 | struct gl_buffer_object *bufObj; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1475 | GET_CURRENT_CONTEXT(ctx); |
| 1476 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 1477 | |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 1478 | mtx_lock(&ctx->Shared->Mutex); |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 1479 | bufObj = _mesa_lookup_bufferobj(ctx, id); |
Brian Paul | d129ea7 | 2014-03-01 10:21:07 -0700 | [diff] [blame] | 1480 | mtx_unlock(&ctx->Shared->Mutex); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1481 | |
Brian Paul | c552f27 | 2010-11-11 14:47:30 -0700 | [diff] [blame] | 1482 | return bufObj && bufObj != &DummyBufferObject; |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1485 | |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1486 | void |
| 1487 | _mesa_buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj, |
| 1488 | GLenum target, GLsizeiptr size, const GLvoid *data, |
| 1489 | GLbitfield flags, const char *func) |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1490 | { |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1491 | if (size <= 0) { |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1492 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1493 | return; |
| 1494 | } |
| 1495 | |
| 1496 | if (flags & ~(GL_MAP_READ_BIT | |
| 1497 | GL_MAP_WRITE_BIT | |
| 1498 | GL_MAP_PERSISTENT_BIT | |
| 1499 | GL_MAP_COHERENT_BIT | |
| 1500 | GL_DYNAMIC_STORAGE_BIT | |
| 1501 | GL_CLIENT_STORAGE_BIT)) { |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1502 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1503 | return; |
| 1504 | } |
| 1505 | |
| 1506 | if (flags & GL_MAP_PERSISTENT_BIT && |
| 1507 | !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) { |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1508 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1509 | "%s(PERSISTENT and flags!=READ/WRITE)", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1510 | return; |
| 1511 | } |
| 1512 | |
| 1513 | if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) { |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1514 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1515 | "%s(COHERENT and flags!=PERSISTENT)", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1516 | return; |
| 1517 | } |
| 1518 | |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1519 | if (bufObj->Immutable) { |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1520 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1521 | return; |
| 1522 | } |
| 1523 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 1524 | /* Unmap the existing buffer. We'll replace it now. Not an error. */ |
| 1525 | _mesa_buffer_unmap_all_mappings(ctx, bufObj); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1526 | |
| 1527 | FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT); |
| 1528 | |
| 1529 | bufObj->Written = GL_TRUE; |
| 1530 | bufObj->Immutable = GL_TRUE; |
| 1531 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 1532 | assert(ctx->Driver.BufferData); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1533 | if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW, |
| 1534 | flags, bufObj)) { |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1535 | if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) { |
| 1536 | /* Even though the interaction between AMD_pinned_memory and |
| 1537 | * glBufferStorage is not described in the spec, Graham Sellers |
| 1538 | * said that it should behave the same as glBufferData. |
| 1539 | */ |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1540 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func); |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1541 | } |
| 1542 | else { |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1543 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1544 | } |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
Laura Ekstrand | a76808d | 2015-01-09 16:17:10 -0800 | [diff] [blame] | 1548 | void GLAPIENTRY |
| 1549 | _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data, |
| 1550 | GLbitfield flags) |
| 1551 | { |
| 1552 | GET_CURRENT_CONTEXT(ctx); |
| 1553 | struct gl_buffer_object *bufObj; |
| 1554 | |
| 1555 | bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION); |
| 1556 | if (!bufObj) |
| 1557 | return; |
| 1558 | |
| 1559 | _mesa_buffer_storage(ctx, bufObj, target, size, data, flags, |
| 1560 | "glBufferStorage"); |
| 1561 | } |
| 1562 | |
| 1563 | void GLAPIENTRY |
| 1564 | _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data, |
| 1565 | GLbitfield flags) |
| 1566 | { |
| 1567 | GET_CURRENT_CONTEXT(ctx); |
| 1568 | struct gl_buffer_object *bufObj; |
| 1569 | |
| 1570 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferStorage"); |
| 1571 | if (!bufObj) |
| 1572 | return; |
| 1573 | |
| 1574 | /* |
| 1575 | * In direct state access, buffer objects have an unspecified target since |
| 1576 | * they are not required to be bound. |
| 1577 | */ |
| 1578 | _mesa_buffer_storage(ctx, bufObj, GL_NONE, size, data, flags, |
| 1579 | "glNamedBufferStorage"); |
| 1580 | } |
| 1581 | |
| 1582 | |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1583 | void |
| 1584 | _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, |
| 1585 | GLenum target, GLsizeiptr size, const GLvoid *data, |
| 1586 | GLenum usage, const char *func) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1587 | { |
Ian Romanick | bd4e5dd | 2012-07-25 16:08:12 -0700 | [diff] [blame] | 1588 | bool valid_usage; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1589 | |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 1590 | if (MESA_VERBOSE & VERBOSE_API) { |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1591 | _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n", |
| 1592 | func, |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 1593 | _mesa_enum_to_string(target), |
Brian Paul | 2634e92 | 2011-02-08 19:19:34 -0700 | [diff] [blame] | 1594 | (long int) size, data, |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 1595 | _mesa_enum_to_string(usage)); |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 1596 | } |
Brian Paul | 2634e92 | 2011-02-08 19:19:34 -0700 | [diff] [blame] | 1597 | |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1598 | if (size < 0) { |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1599 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func); |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1600 | return; |
| 1601 | } |
| 1602 | |
| 1603 | switch (usage) { |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1604 | case GL_STREAM_DRAW_ARB: |
Ian Romanick | bd4e5dd | 2012-07-25 16:08:12 -0700 | [diff] [blame] | 1605 | valid_usage = (ctx->API != API_OPENGLES); |
| 1606 | break; |
| 1607 | |
| 1608 | case GL_STATIC_DRAW_ARB: |
| 1609 | case GL_DYNAMIC_DRAW_ARB: |
| 1610 | valid_usage = true; |
| 1611 | break; |
| 1612 | |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1613 | case GL_STREAM_READ_ARB: |
| 1614 | case GL_STREAM_COPY_ARB: |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1615 | case GL_STATIC_READ_ARB: |
| 1616 | case GL_STATIC_COPY_ARB: |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1617 | case GL_DYNAMIC_READ_ARB: |
| 1618 | case GL_DYNAMIC_COPY_ARB: |
Ian Romanick | bd4e5dd | 2012-07-25 16:08:12 -0700 | [diff] [blame] | 1619 | valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx); |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1620 | break; |
Ian Romanick | bd4e5dd | 2012-07-25 16:08:12 -0700 | [diff] [blame] | 1621 | |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1622 | default: |
Ian Romanick | bd4e5dd | 2012-07-25 16:08:12 -0700 | [diff] [blame] | 1623 | valid_usage = false; |
| 1624 | break; |
| 1625 | } |
| 1626 | |
| 1627 | if (!valid_usage) { |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1628 | _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func, |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 1629 | _mesa_enum_to_string(usage)); |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 1630 | return; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1633 | if (bufObj->Immutable) { |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1634 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1635 | return; |
| 1636 | } |
| 1637 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 1638 | /* Unmap the existing buffer. We'll replace it now. Not an error. */ |
| 1639 | _mesa_buffer_unmap_all_mappings(ctx, bufObj); |
Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 1640 | |
Brian Paul | b9d0f94 | 2009-05-06 15:17:25 -0600 | [diff] [blame] | 1641 | FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT); |
| 1642 | |
Brian Paul | dcca97a | 2009-05-06 15:23:09 -0600 | [diff] [blame] | 1643 | bufObj->Written = GL_TRUE; |
| 1644 | |
Brian Paul | e792762 | 2009-06-03 15:38:57 -0600 | [diff] [blame] | 1645 | #ifdef VBO_DEBUG |
Kristian Høgsberg | 298be2b | 2010-02-19 12:32:24 -0500 | [diff] [blame] | 1646 | printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n", |
Brian Paul | e792762 | 2009-06-03 15:38:57 -0600 | [diff] [blame] | 1647 | bufObj->Name, size, data, usage); |
| 1648 | #endif |
| 1649 | |
Brian Paul | 4a7fd63 | 2009-06-09 11:53:19 -0600 | [diff] [blame] | 1650 | #ifdef BOUNDS_CHECK |
| 1651 | size += 100; |
| 1652 | #endif |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 1653 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 1654 | assert(ctx->Driver.BufferData); |
Marek Olšák | 7e548d0 | 2014-01-27 21:15:19 +0100 | [diff] [blame] | 1655 | if (!ctx->Driver.BufferData(ctx, target, size, data, usage, |
| 1656 | GL_MAP_READ_BIT | |
| 1657 | GL_MAP_WRITE_BIT | |
| 1658 | GL_DYNAMIC_STORAGE_BIT, |
| 1659 | bufObj)) { |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1660 | if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) { |
| 1661 | /* From GL_AMD_pinned_memory: |
| 1662 | * |
| 1663 | * INVALID_OPERATION is generated by BufferData if <target> is |
| 1664 | * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be |
| 1665 | * mapped to the GPU address space. |
| 1666 | */ |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1667 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func); |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1668 | } |
| 1669 | else { |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1670 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); |
Marek Olšák | 11ebb03 | 2015-02-10 01:39:41 +0100 | [diff] [blame] | 1671 | } |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 1672 | } |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Laura Ekstrand | cb56835 | 2015-02-09 17:57:46 -0800 | [diff] [blame] | 1675 | void GLAPIENTRY |
| 1676 | _mesa_BufferData(GLenum target, GLsizeiptr size, |
| 1677 | const GLvoid *data, GLenum usage) |
| 1678 | { |
| 1679 | GET_CURRENT_CONTEXT(ctx); |
| 1680 | struct gl_buffer_object *bufObj; |
| 1681 | |
| 1682 | bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION); |
| 1683 | if (!bufObj) |
| 1684 | return; |
| 1685 | |
| 1686 | _mesa_buffer_data(ctx, bufObj, target, size, data, usage, |
| 1687 | "glBufferData"); |
| 1688 | } |
| 1689 | |
| 1690 | void GLAPIENTRY |
| 1691 | _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data, |
| 1692 | GLenum usage) |
| 1693 | { |
| 1694 | GET_CURRENT_CONTEXT(ctx); |
| 1695 | struct gl_buffer_object *bufObj; |
| 1696 | |
| 1697 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData"); |
| 1698 | if (!bufObj) |
| 1699 | return; |
| 1700 | |
| 1701 | /* In direct state access, buffer objects have an unspecified target since |
| 1702 | * they are not required to be bound. |
| 1703 | */ |
| 1704 | _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage, |
| 1705 | "glNamedBufferData"); |
| 1706 | } |
| 1707 | |
Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 1708 | |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 1709 | /** |
| 1710 | * Implementation for glBufferSubData and glNamedBufferSubData. |
| 1711 | * |
| 1712 | * \param ctx GL context. |
| 1713 | * \param bufObj The buffer object. |
| 1714 | * \param offset Offset of the first byte of the subdata range. |
| 1715 | * \param size Size, in bytes, of the subdata range. |
| 1716 | * \param data The data store. |
| 1717 | * \param func Name of calling function for recording errors. |
| 1718 | * |
| 1719 | */ |
| 1720 | void |
| 1721 | _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, |
| 1722 | GLintptr offset, GLsizeiptr size, const GLvoid *data, |
| 1723 | const char *func) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1724 | { |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 1725 | if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, |
| 1726 | false, func)) { |
Brian Paul | 2daa413 | 2004-10-31 00:17:42 +0000 | [diff] [blame] | 1727 | /* error already recorded */ |
Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 1728 | return; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1729 | } |
Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 1730 | |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1731 | if (bufObj->Immutable && |
| 1732 | !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) { |
Laura Ekstrand | 827da84 | 2015-03-17 13:27:31 -0700 | [diff] [blame] | 1733 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func); |
Marek Olšák | e592f11 | 2014-01-27 21:22:43 +0100 | [diff] [blame] | 1734 | return; |
| 1735 | } |
| 1736 | |
Brian Paul | 6e2e136 | 2010-11-09 12:24:49 -0700 | [diff] [blame] | 1737 | if (size == 0) |
| 1738 | return; |
| 1739 | |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 1740 | bufObj->NumSubDataCalls++; |
| 1741 | |
| 1742 | if ((bufObj->Usage == GL_STATIC_DRAW || |
| 1743 | bufObj->Usage == GL_STATIC_COPY) && |
| 1744 | bufObj->NumSubDataCalls >= BUFFER_WARNING_CALL_COUNT) { |
| 1745 | /* If the application declared the buffer as static draw/copy or stream |
| 1746 | * draw, it should not be frequently modified with glBufferSubData. |
| 1747 | */ |
Brian Paul | e1815bc | 2015-12-09 16:00:55 -0700 | [diff] [blame] | 1748 | BUFFER_USAGE_WARNING(ctx, |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 1749 | "using %s(buffer %u, offset %u, size %u) to " |
| 1750 | "update a %s buffer", |
| 1751 | func, bufObj->Name, offset, size, |
| 1752 | _mesa_enum_to_string(bufObj->Usage)); |
| 1753 | } |
| 1754 | |
Brian Paul | dcca97a | 2009-05-06 15:23:09 -0600 | [diff] [blame] | 1755 | bufObj->Written = GL_TRUE; |
| 1756 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 1757 | assert(ctx->Driver.BufferSubData); |
Laura Ekstrand | 9cb732b | 2015-02-11 11:06:42 -0800 | [diff] [blame] | 1758 | ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj); |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 1761 | void GLAPIENTRY |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 1762 | _mesa_BufferSubData(GLenum target, GLintptr offset, |
| 1763 | GLsizeiptr size, const GLvoid *data) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1764 | { |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1765 | GET_CURRENT_CONTEXT(ctx); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1766 | struct gl_buffer_object *bufObj; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1767 | |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 1768 | bufObj = get_buffer(ctx, "glBufferSubData", target, GL_INVALID_OPERATION); |
| 1769 | if (!bufObj) |
| 1770 | return; |
| 1771 | |
| 1772 | _mesa_buffer_sub_data(ctx, bufObj, offset, size, data, "glBufferSubData"); |
| 1773 | } |
| 1774 | |
| 1775 | void GLAPIENTRY |
| 1776 | _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset, |
| 1777 | GLsizeiptr size, const GLvoid *data) |
| 1778 | { |
| 1779 | GET_CURRENT_CONTEXT(ctx); |
| 1780 | struct gl_buffer_object *bufObj; |
| 1781 | |
| 1782 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferSubData"); |
| 1783 | if (!bufObj) |
| 1784 | return; |
| 1785 | |
| 1786 | _mesa_buffer_sub_data(ctx, bufObj, offset, size, data, |
| 1787 | "glNamedBufferSubData"); |
| 1788 | } |
| 1789 | |
| 1790 | |
| 1791 | void GLAPIENTRY |
| 1792 | _mesa_GetBufferSubData(GLenum target, GLintptr offset, |
| 1793 | GLsizeiptr size, GLvoid *data) |
| 1794 | { |
| 1795 | GET_CURRENT_CONTEXT(ctx); |
| 1796 | struct gl_buffer_object *bufObj; |
| 1797 | |
| 1798 | bufObj = get_buffer(ctx, "glGetBufferSubData", target, |
| 1799 | GL_INVALID_OPERATION); |
| 1800 | if (!bufObj) |
| 1801 | return; |
| 1802 | |
| 1803 | if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false, |
| 1804 | "glGetBufferSubData")) { |
Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 1805 | return; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1806 | } |
Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 1807 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 1808 | assert(ctx->Driver.GetBufferSubData); |
Laura Ekstrand | 579297c | 2015-02-11 16:53:46 -0800 | [diff] [blame] | 1809 | ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj); |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Laura Ekstrand | 23eab47 | 2015-01-20 15:24:53 -0800 | [diff] [blame] | 1812 | void GLAPIENTRY |
| 1813 | _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset, |
| 1814 | GLsizeiptr size, GLvoid *data) |
| 1815 | { |
| 1816 | GET_CURRENT_CONTEXT(ctx); |
| 1817 | struct gl_buffer_object *bufObj; |
| 1818 | |
| 1819 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, |
| 1820 | "glGetNamedBufferSubData"); |
| 1821 | if (!bufObj) |
| 1822 | return; |
| 1823 | |
| 1824 | if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false, |
| 1825 | "glGetNamedBufferSubData")) { |
| 1826 | return; |
| 1827 | } |
| 1828 | |
| 1829 | assert(ctx->Driver.GetBufferSubData); |
| 1830 | ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj); |
| 1831 | } |
| 1832 | |
| 1833 | |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1834 | /** |
| 1835 | * \param subdata true if caller is *SubData, false if *Data |
| 1836 | */ |
| 1837 | void |
| 1838 | _mesa_clear_buffer_sub_data(struct gl_context *ctx, |
| 1839 | struct gl_buffer_object *bufObj, |
| 1840 | GLenum internalformat, |
| 1841 | GLintptr offset, GLsizeiptr size, |
| 1842 | GLenum format, GLenum type, |
| 1843 | const GLvoid *data, |
| 1844 | const char *func, bool subdata) |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1845 | { |
Mark Mueller | 71fe943 | 2014-01-04 14:11:43 -0800 | [diff] [blame] | 1846 | mesa_format mesaFormat; |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1847 | GLubyte clearValue[MAX_PIXEL_BYTES]; |
| 1848 | GLsizeiptr clearValueSize; |
| 1849 | |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1850 | /* This checks for disallowed mappings. */ |
Laura Ekstrand | 566ccdf | 2015-01-13 11:28:17 -0800 | [diff] [blame] | 1851 | if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1852 | subdata, func)) { |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1853 | return; |
| 1854 | } |
| 1855 | |
| 1856 | mesaFormat = validate_clear_buffer_format(ctx, internalformat, |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1857 | format, type, func); |
| 1858 | |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1859 | if (mesaFormat == MESA_FORMAT_NONE) { |
| 1860 | return; |
| 1861 | } |
| 1862 | |
| 1863 | clearValueSize = _mesa_get_format_bytes(mesaFormat); |
| 1864 | if (offset % clearValueSize != 0 || size % clearValueSize != 0) { |
| 1865 | _mesa_error(ctx, GL_INVALID_VALUE, |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1866 | "%s(offset or size is not a multiple of " |
| 1867 | "internalformat size)", func); |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1868 | return; |
| 1869 | } |
| 1870 | |
| 1871 | if (data == NULL) { |
| 1872 | /* clear to zeros, per the spec */ |
Brian Paul | aff7c5e | 2014-03-03 09:59:45 -0700 | [diff] [blame] | 1873 | if (size > 0) { |
| 1874 | ctx->Driver.ClearBufferSubData(ctx, offset, size, |
Ilia Mirkin | f5ba1a1 | 2014-03-25 17:02:48 -0400 | [diff] [blame] | 1875 | NULL, clearValueSize, bufObj); |
Brian Paul | aff7c5e | 2014-03-03 09:59:45 -0700 | [diff] [blame] | 1876 | } |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1877 | return; |
| 1878 | } |
| 1879 | |
| 1880 | if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue, |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1881 | format, type, data, func)) { |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1882 | return; |
| 1883 | } |
| 1884 | |
Brian Paul | aff7c5e | 2014-03-03 09:59:45 -0700 | [diff] [blame] | 1885 | if (size > 0) { |
| 1886 | ctx->Driver.ClearBufferSubData(ctx, offset, size, |
| 1887 | clearValue, clearValueSize, bufObj); |
| 1888 | } |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1891 | void GLAPIENTRY |
| 1892 | _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format, |
| 1893 | GLenum type, const GLvoid *data) |
| 1894 | { |
| 1895 | GET_CURRENT_CONTEXT(ctx); |
| 1896 | struct gl_buffer_object *bufObj; |
| 1897 | |
| 1898 | bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE); |
| 1899 | if (!bufObj) |
| 1900 | return; |
| 1901 | |
| 1902 | _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size, |
| 1903 | format, type, data, |
| 1904 | "glClearBufferData", false); |
| 1905 | } |
| 1906 | |
Laura Ekstrand | 5030d0a | 2015-02-11 12:17:38 -0800 | [diff] [blame] | 1907 | void GLAPIENTRY |
| 1908 | _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat, |
| 1909 | GLenum format, GLenum type, const GLvoid *data) |
| 1910 | { |
| 1911 | GET_CURRENT_CONTEXT(ctx); |
| 1912 | struct gl_buffer_object *bufObj; |
| 1913 | |
| 1914 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData"); |
| 1915 | if (!bufObj) |
| 1916 | return; |
| 1917 | |
| 1918 | _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size, |
| 1919 | format, type, data, |
| 1920 | "glClearNamedBufferData", false); |
| 1921 | } |
| 1922 | |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1923 | |
| 1924 | void GLAPIENTRY |
| 1925 | _mesa_ClearBufferSubData(GLenum target, GLenum internalformat, |
| 1926 | GLintptr offset, GLsizeiptr size, |
| 1927 | GLenum format, GLenum type, |
Laura Ekstrand | 1624452 | 2015-02-11 11:45:57 -0800 | [diff] [blame] | 1928 | const GLvoid *data) |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1929 | { |
| 1930 | GET_CURRENT_CONTEXT(ctx); |
Laura Ekstrand | 1624452 | 2015-02-11 11:45:57 -0800 | [diff] [blame] | 1931 | struct gl_buffer_object *bufObj; |
Laura Ekstrand | 9fa6c363 | 2015-01-13 15:20:19 -0800 | [diff] [blame] | 1932 | |
| 1933 | bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE); |
| 1934 | if (!bufObj) |
| 1935 | return; |
| 1936 | |
| 1937 | _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, |
| 1938 | format, type, data, |
| 1939 | "glClearBufferSubData", true); |
| 1940 | } |
| 1941 | |
Laura Ekstrand | 5030d0a | 2015-02-11 12:17:38 -0800 | [diff] [blame] | 1942 | void GLAPIENTRY |
| 1943 | _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat, |
| 1944 | GLintptr offset, GLsizeiptr size, |
| 1945 | GLenum format, GLenum type, |
| 1946 | const GLvoid *data) |
| 1947 | { |
| 1948 | GET_CURRENT_CONTEXT(ctx); |
| 1949 | struct gl_buffer_object *bufObj; |
| 1950 | |
| 1951 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, |
| 1952 | "glClearNamedBufferSubData"); |
| 1953 | if (!bufObj) |
| 1954 | return; |
| 1955 | |
| 1956 | _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, |
| 1957 | format, type, data, |
| 1958 | "glClearNamedBufferSubData", true); |
| 1959 | } |
| 1960 | |
Pi Tabred | 5f7bc0c | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 1961 | |
Laura Ekstrand | f7f5df9 | 2015-01-14 14:52:01 -0800 | [diff] [blame] | 1962 | GLboolean |
| 1963 | _mesa_unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj, |
| 1964 | const char *func) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1965 | { |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1966 | GLboolean status = GL_TRUE; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 1967 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 1968 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 1969 | if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) { |
Laura Ekstrand | f7f5df9 | 2015-01-14 14:52:01 -0800 | [diff] [blame] | 1970 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1971 | "%s(buffer is not mapped)", func); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 1972 | return GL_FALSE; |
| 1973 | } |
| 1974 | |
Brian Paul | 4a7fd63 | 2009-06-09 11:53:19 -0600 | [diff] [blame] | 1975 | #ifdef BOUNDS_CHECK |
| 1976 | if (bufObj->Access != GL_READ_ONLY_ARB) { |
| 1977 | GLubyte *buf = (GLubyte *) bufObj->Pointer; |
| 1978 | GLuint i; |
| 1979 | /* check that last 100 bytes are still = magic value */ |
| 1980 | for (i = 0; i < 100; i++) { |
| 1981 | GLuint pos = bufObj->Size - i - 1; |
| 1982 | if (buf[pos] != 123) { |
| 1983 | _mesa_warning(ctx, "Out of bounds buffer object write detected" |
| 1984 | " at position %d (value = %u)\n", |
| 1985 | pos, buf[pos]); |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | #endif |
| 1990 | |
Brian Paul | e792762 | 2009-06-03 15:38:57 -0600 | [diff] [blame] | 1991 | #ifdef VBO_DEBUG |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 1992 | if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) { |
Brian Paul | e792762 | 2009-06-03 15:38:57 -0600 | [diff] [blame] | 1993 | GLuint i, unchanged = 0; |
| 1994 | GLubyte *b = (GLubyte *) bufObj->Pointer; |
| 1995 | GLint pos = -1; |
| 1996 | /* check which bytes changed */ |
| 1997 | for (i = 0; i < bufObj->Size - 1; i++) { |
| 1998 | if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) { |
| 1999 | unchanged++; |
| 2000 | if (pos == -1) |
| 2001 | pos = i; |
| 2002 | } |
| 2003 | } |
| 2004 | if (unchanged) { |
Kristian Høgsberg | 298be2b | 2010-02-19 12:32:24 -0500 | [diff] [blame] | 2005 | printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n", |
Brian Paul | e792762 | 2009-06-03 15:38:57 -0600 | [diff] [blame] | 2006 | bufObj->Name, unchanged, bufObj->Size, pos); |
| 2007 | } |
| 2008 | } |
| 2009 | #endif |
| 2010 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2011 | status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER); |
| 2012 | bufObj->Mappings[MAP_USER].AccessFlags = 0; |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 2013 | assert(bufObj->Mappings[MAP_USER].Pointer == NULL); |
| 2014 | assert(bufObj->Mappings[MAP_USER].Offset == 0); |
| 2015 | assert(bufObj->Mappings[MAP_USER].Length == 0); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 2016 | |
| 2017 | return status; |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Laura Ekstrand | f7f5df9 | 2015-01-14 14:52:01 -0800 | [diff] [blame] | 2020 | GLboolean GLAPIENTRY |
| 2021 | _mesa_UnmapBuffer(GLenum target) |
| 2022 | { |
| 2023 | GET_CURRENT_CONTEXT(ctx); |
| 2024 | struct gl_buffer_object *bufObj; |
| 2025 | |
| 2026 | bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION); |
| 2027 | if (!bufObj) |
| 2028 | return GL_FALSE; |
| 2029 | |
| 2030 | return _mesa_unmap_buffer(ctx, bufObj, "glUnmapBuffer"); |
| 2031 | } |
| 2032 | |
| 2033 | GLboolean GLAPIENTRY |
| 2034 | _mesa_UnmapNamedBuffer(GLuint buffer) |
| 2035 | { |
| 2036 | GET_CURRENT_CONTEXT(ctx); |
| 2037 | struct gl_buffer_object *bufObj; |
| 2038 | |
| 2039 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer"); |
| 2040 | if (!bufObj) |
| 2041 | return GL_FALSE; |
| 2042 | |
| 2043 | return _mesa_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer"); |
| 2044 | } |
Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 2045 | |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2046 | |
| 2047 | static bool |
| 2048 | get_buffer_parameter(struct gl_context *ctx, |
| 2049 | struct gl_buffer_object *bufObj, GLenum pname, |
| 2050 | GLint64 *params, const char *func) |
| 2051 | { |
| 2052 | switch (pname) { |
| 2053 | case GL_BUFFER_SIZE_ARB: |
| 2054 | *params = bufObj->Size; |
| 2055 | break; |
| 2056 | case GL_BUFFER_USAGE_ARB: |
| 2057 | *params = bufObj->Usage; |
| 2058 | break; |
| 2059 | case GL_BUFFER_ACCESS_ARB: |
| 2060 | *params = simplified_access_mode(ctx, |
| 2061 | bufObj->Mappings[MAP_USER].AccessFlags); |
| 2062 | break; |
| 2063 | case GL_BUFFER_MAPPED_ARB: |
| 2064 | *params = _mesa_bufferobj_mapped(bufObj, MAP_USER); |
| 2065 | break; |
| 2066 | case GL_BUFFER_ACCESS_FLAGS: |
| 2067 | if (!ctx->Extensions.ARB_map_buffer_range) |
| 2068 | goto invalid_pname; |
| 2069 | *params = bufObj->Mappings[MAP_USER].AccessFlags; |
| 2070 | break; |
| 2071 | case GL_BUFFER_MAP_OFFSET: |
| 2072 | if (!ctx->Extensions.ARB_map_buffer_range) |
| 2073 | goto invalid_pname; |
| 2074 | *params = bufObj->Mappings[MAP_USER].Offset; |
| 2075 | break; |
| 2076 | case GL_BUFFER_MAP_LENGTH: |
| 2077 | if (!ctx->Extensions.ARB_map_buffer_range) |
| 2078 | goto invalid_pname; |
| 2079 | *params = bufObj->Mappings[MAP_USER].Length; |
| 2080 | break; |
| 2081 | case GL_BUFFER_IMMUTABLE_STORAGE: |
| 2082 | if (!ctx->Extensions.ARB_buffer_storage) |
| 2083 | goto invalid_pname; |
| 2084 | *params = bufObj->Immutable; |
| 2085 | break; |
| 2086 | case GL_BUFFER_STORAGE_FLAGS: |
| 2087 | if (!ctx->Extensions.ARB_buffer_storage) |
| 2088 | goto invalid_pname; |
| 2089 | *params = bufObj->StorageFlags; |
| 2090 | break; |
| 2091 | default: |
| 2092 | goto invalid_pname; |
| 2093 | } |
| 2094 | |
| 2095 | return true; |
| 2096 | |
| 2097 | invalid_pname: |
| 2098 | _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func, |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 2099 | _mesa_enum_to_string(pname)); |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2100 | return false; |
| 2101 | } |
| 2102 | |
Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 2103 | void GLAPIENTRY |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 2104 | _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 2105 | { |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 2106 | GET_CURRENT_CONTEXT(ctx); |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 2107 | struct gl_buffer_object *bufObj; |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2108 | GLint64 parameter; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 2109 | |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2110 | bufObj = get_buffer(ctx, "glGetBufferParameteriv", target, |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 2111 | GL_INVALID_OPERATION); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 2112 | if (!bufObj) |
Brian Paul | 4277ea4 | 2006-08-25 22:06:02 +0000 | [diff] [blame] | 2113 | return; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 2114 | |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2115 | if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter, |
| 2116 | "glGetBufferParameteriv")) |
| 2117 | return; /* Error already recorded. */ |
Brian Paul | d6a9f5b | 2010-03-20 11:52:12 -0600 | [diff] [blame] | 2118 | |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2119 | *params = (GLint) parameter; |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Brian Paul | 1fbc719 | 2010-01-01 17:50:02 -0700 | [diff] [blame] | 2122 | void GLAPIENTRY |
| 2123 | _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params) |
| 2124 | { |
| 2125 | GET_CURRENT_CONTEXT(ctx); |
| 2126 | struct gl_buffer_object *bufObj; |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2127 | GLint64 parameter; |
Brian Paul | 1fbc719 | 2010-01-01 17:50:02 -0700 | [diff] [blame] | 2128 | |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 2129 | bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target, |
| 2130 | GL_INVALID_OPERATION); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 2131 | if (!bufObj) |
Brian Paul | 1fbc719 | 2010-01-01 17:50:02 -0700 | [diff] [blame] | 2132 | return; |
Brian Paul | 1fbc719 | 2010-01-01 17:50:02 -0700 | [diff] [blame] | 2133 | |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2134 | if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter, |
| 2135 | "glGetBufferParameteri64v")) |
| 2136 | return; /* Error already recorded. */ |
Brian Paul | d6a9f5b | 2010-03-20 11:52:12 -0600 | [diff] [blame] | 2137 | |
Laura Ekstrand | efcb830 | 2015-02-11 16:07:45 -0800 | [diff] [blame] | 2138 | *params = parameter; |
Brian Paul | 1fbc719 | 2010-01-01 17:50:02 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Laura Ekstrand | 1e45752 | 2015-02-11 16:10:20 -0800 | [diff] [blame] | 2141 | void GLAPIENTRY |
| 2142 | _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params) |
| 2143 | { |
| 2144 | GET_CURRENT_CONTEXT(ctx); |
| 2145 | struct gl_buffer_object *bufObj; |
| 2146 | GLint64 parameter; |
| 2147 | |
| 2148 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, |
| 2149 | "glGetNamedBufferParameteriv"); |
| 2150 | if (!bufObj) |
| 2151 | return; |
| 2152 | |
| 2153 | if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter, |
| 2154 | "glGetNamedBufferParameteriv")) |
| 2155 | return; /* Error already recorded. */ |
| 2156 | |
| 2157 | *params = (GLint) parameter; |
| 2158 | } |
| 2159 | |
| 2160 | void GLAPIENTRY |
| 2161 | _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname, |
| 2162 | GLint64 *params) |
| 2163 | { |
| 2164 | GET_CURRENT_CONTEXT(ctx); |
| 2165 | struct gl_buffer_object *bufObj; |
| 2166 | GLint64 parameter; |
| 2167 | |
| 2168 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, |
| 2169 | "glGetNamedBufferParameteri64v"); |
| 2170 | if (!bufObj) |
| 2171 | return; |
| 2172 | |
| 2173 | if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter, |
| 2174 | "glGetNamedBufferParameteri64v")) |
| 2175 | return; /* Error already recorded. */ |
| 2176 | |
| 2177 | *params = parameter; |
| 2178 | } |
| 2179 | |
Brian Paul | 1fbc719 | 2010-01-01 17:50:02 -0700 | [diff] [blame] | 2180 | |
Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 2181 | void GLAPIENTRY |
Paul Berry | 1a1db17 | 2012-11-06 08:57:59 -0800 | [diff] [blame] | 2182 | _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params) |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 2183 | { |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 2184 | GET_CURRENT_CONTEXT(ctx); |
Laura Ekstrand | 3706ace | 2015-03-16 16:08:36 -0700 | [diff] [blame] | 2185 | struct gl_buffer_object *bufObj; |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 2186 | |
Laura Ekstrand | 3706ace | 2015-03-16 16:08:36 -0700 | [diff] [blame] | 2187 | if (pname != GL_BUFFER_MAP_POINTER) { |
| 2188 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != " |
| 2189 | "GL_BUFFER_MAP_POINTER)"); |
Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 2190 | return; |
| 2191 | } |
| 2192 | |
Laura Ekstrand | 3706ace | 2015-03-16 16:08:36 -0700 | [diff] [blame] | 2193 | bufObj = get_buffer(ctx, "glGetBufferPointerv", target, |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 2194 | GL_INVALID_OPERATION); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 2195 | if (!bufObj) |
Brian Paul | 4277ea4 | 2006-08-25 22:06:02 +0000 | [diff] [blame] | 2196 | return; |
Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 2197 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2198 | *params = bufObj->Mappings[MAP_USER].Pointer; |
Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 2199 | } |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2200 | |
Laura Ekstrand | 105ddc6 | 2015-01-20 14:32:35 -0800 | [diff] [blame] | 2201 | void GLAPIENTRY |
| 2202 | _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params) |
| 2203 | { |
| 2204 | GET_CURRENT_CONTEXT(ctx); |
| 2205 | struct gl_buffer_object *bufObj; |
| 2206 | |
| 2207 | if (pname != GL_BUFFER_MAP_POINTER) { |
| 2208 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != " |
| 2209 | "GL_BUFFER_MAP_POINTER)"); |
| 2210 | return; |
| 2211 | } |
| 2212 | |
| 2213 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, |
| 2214 | "glGetNamedBufferPointerv"); |
| 2215 | if (!bufObj) |
| 2216 | return; |
| 2217 | |
| 2218 | *params = bufObj->Mappings[MAP_USER].Pointer; |
| 2219 | } |
| 2220 | |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2221 | |
Laura Ekstrand | 4adaad5 | 2015-01-13 13:28:08 -0800 | [diff] [blame] | 2222 | void |
| 2223 | _mesa_copy_buffer_sub_data(struct gl_context *ctx, |
| 2224 | struct gl_buffer_object *src, |
| 2225 | struct gl_buffer_object *dst, |
| 2226 | GLintptr readOffset, GLintptr writeOffset, |
| 2227 | GLsizeiptr size, const char *func) |
| 2228 | { |
| 2229 | if (_mesa_check_disallowed_mapping(src)) { |
| 2230 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 2231 | "%s(readBuffer is mapped)", func); |
| 2232 | return; |
| 2233 | } |
| 2234 | |
| 2235 | if (_mesa_check_disallowed_mapping(dst)) { |
| 2236 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 2237 | "%s(writeBuffer is mapped)", func); |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | if (readOffset < 0) { |
| 2242 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2243 | "%s(readOffset %d < 0)", func, (int) readOffset); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
| 2247 | if (writeOffset < 0) { |
| 2248 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2249 | "%s(writeOffset %d < 0)", func, (int) writeOffset); |
| 2250 | return; |
| 2251 | } |
| 2252 | |
| 2253 | if (size < 0) { |
| 2254 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2255 | "%s(size %d < 0)", func, (int) size); |
| 2256 | return; |
| 2257 | } |
| 2258 | |
| 2259 | if (readOffset + size > src->Size) { |
| 2260 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2261 | "%s(readOffset %d + size %d > src_buffer_size %d)", func, |
| 2262 | (int) readOffset, (int) size, (int) src->Size); |
| 2263 | return; |
| 2264 | } |
| 2265 | |
| 2266 | if (writeOffset + size > dst->Size) { |
| 2267 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2268 | "%s(writeOffset %d + size %d > dst_buffer_size %d)", func, |
| 2269 | (int) writeOffset, (int) size, (int) dst->Size); |
| 2270 | return; |
| 2271 | } |
| 2272 | |
| 2273 | if (src == dst) { |
| 2274 | if (readOffset + size <= writeOffset) { |
| 2275 | /* OK */ |
| 2276 | } |
| 2277 | else if (writeOffset + size <= readOffset) { |
| 2278 | /* OK */ |
| 2279 | } |
| 2280 | else { |
| 2281 | /* overlapping src/dst is illegal */ |
| 2282 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2283 | "%s(overlapping src/dst)", func); |
| 2284 | return; |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size); |
| 2289 | } |
| 2290 | |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2291 | void GLAPIENTRY |
| 2292 | _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget, |
| 2293 | GLintptr readOffset, GLintptr writeOffset, |
| 2294 | GLsizeiptr size) |
| 2295 | { |
| 2296 | GET_CURRENT_CONTEXT(ctx); |
| 2297 | struct gl_buffer_object *src, *dst; |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2298 | |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 2299 | src = get_buffer(ctx, "glCopyBufferSubData", readTarget, |
| 2300 | GL_INVALID_OPERATION); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 2301 | if (!src) |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2302 | return; |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2303 | |
Pi Tabred | 7d94653 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 2304 | dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget, |
| 2305 | GL_INVALID_OPERATION); |
Eric Anholt | f20fb80 | 2012-01-25 16:30:26 -0800 | [diff] [blame] | 2306 | if (!dst) |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2307 | return; |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2308 | |
Laura Ekstrand | 4adaad5 | 2015-01-13 13:28:08 -0800 | [diff] [blame] | 2309 | _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size, |
| 2310 | "glCopyBufferSubData"); |
| 2311 | } |
| 2312 | |
| 2313 | void GLAPIENTRY |
| 2314 | _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer, |
| 2315 | GLintptr readOffset, GLintptr writeOffset, |
| 2316 | GLsizeiptr size) |
| 2317 | { |
| 2318 | GET_CURRENT_CONTEXT(ctx); |
| 2319 | struct gl_buffer_object *src, *dst; |
| 2320 | |
| 2321 | src = _mesa_lookup_bufferobj_err(ctx, readBuffer, |
| 2322 | "glCopyNamedBufferSubData"); |
| 2323 | if (!src) |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2324 | return; |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2325 | |
Laura Ekstrand | 4adaad5 | 2015-01-13 13:28:08 -0800 | [diff] [blame] | 2326 | dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer, |
| 2327 | "glCopyNamedBufferSubData"); |
| 2328 | if (!dst) |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2329 | return; |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2330 | |
Laura Ekstrand | 4adaad5 | 2015-01-13 13:28:08 -0800 | [diff] [blame] | 2331 | _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size, |
| 2332 | "glCopyNamedBufferSubData"); |
Brian Paul | dc0b71f | 2009-06-02 20:29:57 -0600 | [diff] [blame] | 2333 | } |
| 2334 | |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2335 | |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2336 | void * |
| 2337 | _mesa_map_buffer_range(struct gl_context *ctx, |
| 2338 | struct gl_buffer_object *bufObj, |
| 2339 | GLintptr offset, GLsizeiptr length, |
| 2340 | GLbitfield access, const char *func) |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2341 | { |
Brian Paul | 92033a9 | 2009-08-31 08:50:15 -0600 | [diff] [blame] | 2342 | void *map; |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2343 | GLbitfield allowed_access; |
Brian Paul | 92033a9 | 2009-08-31 08:50:15 -0600 | [diff] [blame] | 2344 | |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2345 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL); |
| 2346 | |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2347 | if (offset < 0) { |
| 2348 | _mesa_error(ctx, GL_INVALID_VALUE, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2349 | "%s(offset %ld < 0)", func, (long) offset); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2350 | return NULL; |
| 2351 | } |
| 2352 | |
| 2353 | if (length < 0) { |
| 2354 | _mesa_error(ctx, GL_INVALID_VALUE, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2355 | "%s(length %ld < 0)", func, (long) length); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2356 | return NULL; |
| 2357 | } |
| 2358 | |
Fredrik Höglund | 762d9ac | 2012-10-10 16:27:31 +0200 | [diff] [blame] | 2359 | /* Page 38 of the PDF of the OpenGL ES 3.0 spec says: |
| 2360 | * |
| 2361 | * "An INVALID_OPERATION error is generated for any of the following |
| 2362 | * conditions: |
| 2363 | * |
| 2364 | * * <length> is zero." |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2365 | * |
| 2366 | * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec |
| 2367 | * (30.10.2014) also says this, so it's no longer allowed for desktop GL, |
| 2368 | * either. |
Fredrik Höglund | 762d9ac | 2012-10-10 16:27:31 +0200 | [diff] [blame] | 2369 | */ |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2370 | if (length == 0) { |
| 2371 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func); |
Fredrik Höglund | 762d9ac | 2012-10-10 16:27:31 +0200 | [diff] [blame] | 2372 | return NULL; |
| 2373 | } |
| 2374 | |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2375 | allowed_access = GL_MAP_READ_BIT | |
| 2376 | GL_MAP_WRITE_BIT | |
| 2377 | GL_MAP_INVALIDATE_RANGE_BIT | |
| 2378 | GL_MAP_INVALIDATE_BUFFER_BIT | |
| 2379 | GL_MAP_FLUSH_EXPLICIT_BIT | |
| 2380 | GL_MAP_UNSYNCHRONIZED_BIT; |
| 2381 | |
| 2382 | if (ctx->Extensions.ARB_buffer_storage) { |
| 2383 | allowed_access |= GL_MAP_PERSISTENT_BIT | |
| 2384 | GL_MAP_COHERENT_BIT; |
| 2385 | } |
| 2386 | |
| 2387 | if (access & ~allowed_access) { |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2388 | /* generate an error if any bits other than those allowed are set */ |
| 2389 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2390 | "%s(access has undefined bits set)", func); |
Yuanhan Liu | 099af9e | 2011-09-19 18:25:55 +0800 | [diff] [blame] | 2391 | return NULL; |
| 2392 | } |
| 2393 | |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2394 | if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) { |
| 2395 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2396 | "%s(access indicates neither read or write)", func); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2397 | return NULL; |
| 2398 | } |
| 2399 | |
Brian Paul | 707eadc | 2011-05-18 08:14:32 -0600 | [diff] [blame] | 2400 | if ((access & GL_MAP_READ_BIT) && |
| 2401 | (access & (GL_MAP_INVALIDATE_RANGE_BIT | |
| 2402 | GL_MAP_INVALIDATE_BUFFER_BIT | |
| 2403 | GL_MAP_UNSYNCHRONIZED_BIT))) { |
| 2404 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2405 | "%s(read access with disallowed bits)", func); |
Brian Paul | 707eadc | 2011-05-18 08:14:32 -0600 | [diff] [blame] | 2406 | return NULL; |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) && |
| 2410 | ((access & GL_MAP_WRITE_BIT) == 0)) { |
| 2411 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2412 | "%s(access has flush explicit without write)", func); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2413 | return NULL; |
| 2414 | } |
| 2415 | |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2416 | if (access & GL_MAP_READ_BIT && |
| 2417 | !(bufObj->StorageFlags & GL_MAP_READ_BIT)) { |
| 2418 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2419 | "%s(buffer does not allow read access)", func); |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2420 | return NULL; |
| 2421 | } |
| 2422 | |
| 2423 | if (access & GL_MAP_WRITE_BIT && |
| 2424 | !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) { |
| 2425 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2426 | "%s(buffer does not allow write access)", func); |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2427 | return NULL; |
| 2428 | } |
| 2429 | |
| 2430 | if (access & GL_MAP_COHERENT_BIT && |
| 2431 | !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) { |
| 2432 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2433 | "%s(buffer does not allow coherent access)", func); |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2434 | return NULL; |
| 2435 | } |
| 2436 | |
| 2437 | if (access & GL_MAP_PERSISTENT_BIT && |
| 2438 | !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) { |
| 2439 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2440 | "%s(buffer does not allow persistent access)", func); |
Marek Olšák | 4f78e17 | 2014-01-27 21:31:58 +0100 | [diff] [blame] | 2441 | return NULL; |
| 2442 | } |
| 2443 | |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2444 | if (offset + length > bufObj->Size) { |
| 2445 | _mesa_error(ctx, GL_INVALID_VALUE, |
Edward O'Callaghan | 86a72ee | 2015-07-29 21:49:45 +1000 | [diff] [blame] | 2446 | "%s(offset %td + length %td > buffer_size %td)", func, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2447 | offset, length, bufObj->Size); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2448 | return NULL; |
| 2449 | } |
| 2450 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2451 | if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) { |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2452 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2453 | "%s(buffer already mapped)", func); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2454 | return NULL; |
| 2455 | } |
Marek Olšák | 5572de8 | 2011-08-30 19:10:06 +0200 | [diff] [blame] | 2456 | |
| 2457 | if (!bufObj->Size) { |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2458 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func); |
Marek Olšák | 5572de8 | 2011-08-30 19:10:06 +0200 | [diff] [blame] | 2459 | return NULL; |
| 2460 | } |
| 2461 | |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 2462 | if (access & GL_MAP_WRITE_BIT) { |
| 2463 | bufObj->NumMapBufferWriteCalls++; |
| 2464 | if ((bufObj->Usage == GL_STATIC_DRAW || |
| 2465 | bufObj->Usage == GL_STATIC_COPY) && |
| 2466 | bufObj->NumMapBufferWriteCalls >= BUFFER_WARNING_CALL_COUNT) { |
Brian Paul | e1815bc | 2015-12-09 16:00:55 -0700 | [diff] [blame] | 2467 | BUFFER_USAGE_WARNING(ctx, |
Brian Paul | ab0651c | 2015-12-07 18:38:03 -0700 | [diff] [blame] | 2468 | "using %s(buffer %u, offset %u, length %u) to " |
| 2469 | "update a %s buffer", |
| 2470 | func, bufObj->Name, offset, length, |
| 2471 | _mesa_enum_to_string(bufObj->Usage)); |
| 2472 | } |
| 2473 | } |
Marek Olšák | 5572de8 | 2011-08-30 19:10:06 +0200 | [diff] [blame] | 2474 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 2475 | assert(ctx->Driver.MapBufferRange); |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2476 | map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj, |
| 2477 | MAP_USER); |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 2478 | if (!map) { |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2479 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func); |
Brian Paul | 2f6d2a9 | 2009-09-03 09:41:41 -0600 | [diff] [blame] | 2480 | } |
| 2481 | else { |
Brian Paul | 92033a9 | 2009-08-31 08:50:15 -0600 | [diff] [blame] | 2482 | /* The driver callback should have set all these fields. |
| 2483 | * This is important because other modules (like VBO) might call |
| 2484 | * the driver function directly. |
| 2485 | */ |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 2486 | assert(bufObj->Mappings[MAP_USER].Pointer == map); |
| 2487 | assert(bufObj->Mappings[MAP_USER].Length == length); |
| 2488 | assert(bufObj->Mappings[MAP_USER].Offset == offset); |
| 2489 | assert(bufObj->Mappings[MAP_USER].AccessFlags == access); |
Brian Paul | 92033a9 | 2009-08-31 08:50:15 -0600 | [diff] [blame] | 2490 | } |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2491 | |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2492 | if (access & GL_MAP_WRITE_BIT) |
| 2493 | bufObj->Written = GL_TRUE; |
| 2494 | |
| 2495 | #ifdef VBO_DEBUG |
| 2496 | if (strstr(func, "Range") == NULL) { /* If not MapRange */ |
| 2497 | printf("glMapBuffer(%u, sz %ld, access 0x%x)\n", |
| 2498 | bufObj->Name, bufObj->Size, access); |
| 2499 | /* Access must be write only */ |
| 2500 | if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) { |
| 2501 | GLuint i; |
| 2502 | GLubyte *b = (GLubyte *) bufObj->Pointer; |
| 2503 | for (i = 0; i < bufObj->Size; i++) |
| 2504 | b[i] = i & 0xff; |
| 2505 | } |
| 2506 | } |
| 2507 | #endif |
| 2508 | |
| 2509 | #ifdef BOUNDS_CHECK |
| 2510 | if (strstr(func, "Range") == NULL) { /* If not MapRange */ |
| 2511 | GLubyte *buf = (GLubyte *) bufObj->Pointer; |
| 2512 | GLuint i; |
| 2513 | /* buffer is 100 bytes larger than requested, fill with magic value */ |
| 2514 | for (i = 0; i < 100; i++) { |
| 2515 | buf[bufObj->Size - i - 1] = 123; |
| 2516 | } |
| 2517 | } |
| 2518 | #endif |
| 2519 | |
Brian Paul | 92033a9 | 2009-08-31 08:50:15 -0600 | [diff] [blame] | 2520 | return map; |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2521 | } |
| 2522 | |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2523 | void * GLAPIENTRY |
| 2524 | _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length, |
| 2525 | GLbitfield access) |
| 2526 | { |
| 2527 | GET_CURRENT_CONTEXT(ctx); |
| 2528 | struct gl_buffer_object *bufObj; |
| 2529 | |
| 2530 | if (!ctx->Extensions.ARB_map_buffer_range) { |
| 2531 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 2532 | "glMapBufferRange(ARB_map_buffer_range not supported)"); |
| 2533 | return NULL; |
| 2534 | } |
| 2535 | |
| 2536 | bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION); |
| 2537 | if (!bufObj) |
| 2538 | return NULL; |
| 2539 | |
| 2540 | return _mesa_map_buffer_range(ctx, bufObj, offset, length, access, |
| 2541 | "glMapBufferRange"); |
| 2542 | } |
| 2543 | |
Laura Ekstrand | a0cc039 | 2015-02-11 14:09:52 -0800 | [diff] [blame] | 2544 | void * GLAPIENTRY |
| 2545 | _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length, |
| 2546 | GLbitfield access) |
| 2547 | { |
| 2548 | GET_CURRENT_CONTEXT(ctx); |
| 2549 | struct gl_buffer_object *bufObj; |
| 2550 | |
| 2551 | if (!ctx->Extensions.ARB_map_buffer_range) { |
| 2552 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 2553 | "glMapNamedBufferRange(" |
| 2554 | "ARB_map_buffer_range not supported)"); |
| 2555 | return NULL; |
| 2556 | } |
| 2557 | |
| 2558 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange"); |
| 2559 | if (!bufObj) |
| 2560 | return NULL; |
| 2561 | |
| 2562 | return _mesa_map_buffer_range(ctx, bufObj, offset, length, access, |
| 2563 | "glMapNamedBufferRange"); |
| 2564 | } |
| 2565 | |
Laura Ekstrand | 4f513bc | 2015-01-14 12:44:39 -0800 | [diff] [blame] | 2566 | /** |
| 2567 | * Converts GLenum access from MapBuffer and MapNamedBuffer into |
| 2568 | * flags for input to _mesa_map_buffer_range. |
| 2569 | * |
| 2570 | * \return true if the type of requested access is permissible. |
| 2571 | */ |
| 2572 | static bool |
| 2573 | get_map_buffer_access_flags(struct gl_context *ctx, GLenum access, |
| 2574 | GLbitfield *flags) |
| 2575 | { |
| 2576 | switch (access) { |
| 2577 | case GL_READ_ONLY_ARB: |
| 2578 | *flags = GL_MAP_READ_BIT; |
| 2579 | return _mesa_is_desktop_gl(ctx); |
| 2580 | case GL_WRITE_ONLY_ARB: |
| 2581 | *flags = GL_MAP_WRITE_BIT; |
| 2582 | return true; |
| 2583 | case GL_READ_WRITE_ARB: |
| 2584 | *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT; |
| 2585 | return _mesa_is_desktop_gl(ctx); |
| 2586 | default: |
| 2587 | return false; |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | void * GLAPIENTRY |
| 2592 | _mesa_MapBuffer(GLenum target, GLenum access) |
| 2593 | { |
| 2594 | GET_CURRENT_CONTEXT(ctx); |
| 2595 | struct gl_buffer_object *bufObj; |
| 2596 | GLbitfield accessFlags; |
| 2597 | |
| 2598 | if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) { |
| 2599 | _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)"); |
| 2600 | return NULL; |
| 2601 | } |
| 2602 | |
| 2603 | bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION); |
| 2604 | if (!bufObj) |
| 2605 | return NULL; |
| 2606 | |
| 2607 | return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags, |
| 2608 | "glMapBuffer"); |
| 2609 | } |
| 2610 | |
Laura Ekstrand | a0cc039 | 2015-02-11 14:09:52 -0800 | [diff] [blame] | 2611 | void * GLAPIENTRY |
| 2612 | _mesa_MapNamedBuffer(GLuint buffer, GLenum access) |
| 2613 | { |
| 2614 | GET_CURRENT_CONTEXT(ctx); |
| 2615 | struct gl_buffer_object *bufObj; |
| 2616 | GLbitfield accessFlags; |
| 2617 | |
| 2618 | if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) { |
| 2619 | _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)"); |
| 2620 | return NULL; |
| 2621 | } |
| 2622 | |
| 2623 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer"); |
| 2624 | if (!bufObj) |
| 2625 | return NULL; |
| 2626 | |
| 2627 | return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags, |
| 2628 | "glMapNamedBuffer"); |
| 2629 | } |
| 2630 | |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2631 | |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2632 | void |
| 2633 | _mesa_flush_mapped_buffer_range(struct gl_context *ctx, |
| 2634 | struct gl_buffer_object *bufObj, |
| 2635 | GLintptr offset, GLsizeiptr length, |
| 2636 | const char *func) |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2637 | { |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2638 | if (!ctx->Extensions.ARB_map_buffer_range) { |
| 2639 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2640 | "%s(ARB_map_buffer_range not supported)", func); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2641 | return; |
| 2642 | } |
| 2643 | |
| 2644 | if (offset < 0) { |
| 2645 | _mesa_error(ctx, GL_INVALID_VALUE, |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2646 | "%s(offset %ld < 0)", func, (long) offset); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2647 | return; |
| 2648 | } |
| 2649 | |
| 2650 | if (length < 0) { |
| 2651 | _mesa_error(ctx, GL_INVALID_VALUE, |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2652 | "%s(length %ld < 0)", func, (long) length); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2653 | return; |
| 2654 | } |
| 2655 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2656 | if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) { |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2657 | /* buffer is not mapped */ |
| 2658 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2659 | "%s(buffer is not mapped)", func); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2660 | return; |
| 2661 | } |
| 2662 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2663 | if ((bufObj->Mappings[MAP_USER].AccessFlags & |
| 2664 | GL_MAP_FLUSH_EXPLICIT_BIT) == 0) { |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2665 | _mesa_error(ctx, GL_INVALID_OPERATION, |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2666 | "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2667 | return; |
| 2668 | } |
| 2669 | |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2670 | if (offset + length > bufObj->Mappings[MAP_USER].Length) { |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2671 | _mesa_error(ctx, GL_INVALID_VALUE, |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2672 | "%s(offset %ld + length %ld > mapped length %ld)", func, |
| 2673 | (long) offset, (long) length, |
| 2674 | (long) bufObj->Mappings[MAP_USER].Length); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2675 | return; |
| 2676 | } |
| 2677 | |
Matt Turner | bfcdb84 | 2015-02-20 20:18:47 -0800 | [diff] [blame] | 2678 | assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2679 | |
| 2680 | if (ctx->Driver.FlushMappedBufferRange) |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 2681 | ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj, |
| 2682 | MAP_USER); |
Brian Paul | e75b283 | 2009-06-08 17:02:36 -0600 | [diff] [blame] | 2683 | } |
Chris Wilson | 99864d5 | 2009-11-13 12:19:35 +0000 | [diff] [blame] | 2684 | |
Laura Ekstrand | ee5fae6 | 2015-01-14 17:01:20 -0800 | [diff] [blame] | 2685 | void GLAPIENTRY |
| 2686 | _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, |
| 2687 | GLsizeiptr length) |
| 2688 | { |
| 2689 | GET_CURRENT_CONTEXT(ctx); |
| 2690 | struct gl_buffer_object *bufObj; |
| 2691 | |
| 2692 | bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target, |
| 2693 | GL_INVALID_OPERATION); |
| 2694 | if (!bufObj) |
| 2695 | return; |
| 2696 | |
| 2697 | _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length, |
| 2698 | "glFlushMappedBufferRange"); |
| 2699 | } |
| 2700 | |
Laura Ekstrand | 1cfc18d | 2015-02-11 16:06:52 -0800 | [diff] [blame] | 2701 | void GLAPIENTRY |
| 2702 | _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset, |
| 2703 | GLsizeiptr length) |
| 2704 | { |
| 2705 | GET_CURRENT_CONTEXT(ctx); |
| 2706 | struct gl_buffer_object *bufObj; |
| 2707 | |
| 2708 | bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, |
| 2709 | "glFlushMappedNamedBufferRange"); |
| 2710 | if (!bufObj) |
| 2711 | return; |
| 2712 | |
| 2713 | _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length, |
| 2714 | "glFlushMappedNamedBufferRange"); |
| 2715 | } |
| 2716 | |
Brian Paul | e176ae5 | 2010-03-05 09:23:43 -0700 | [diff] [blame] | 2717 | |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2718 | /** |
| 2719 | * Binds a buffer object to a uniform buffer binding point. |
| 2720 | * |
| 2721 | * The caller is responsible for flushing vertices and updating |
| 2722 | * NewDriverState. |
| 2723 | */ |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2724 | static void |
| 2725 | set_ubo_binding(struct gl_context *ctx, |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2726 | struct gl_uniform_buffer_binding *binding, |
| 2727 | struct gl_buffer_object *bufObj, |
| 2728 | GLintptr offset, |
| 2729 | GLsizeiptr size, |
| 2730 | GLboolean autoSize) |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2731 | { |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2732 | _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2733 | |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2734 | binding->Offset = offset; |
| 2735 | binding->Size = size; |
| 2736 | binding->AutomaticSize = autoSize; |
Chris Forbes | fe3133f | 2014-10-01 19:27:11 +1300 | [diff] [blame] | 2737 | |
| 2738 | /* If this is a real buffer object, mark it has having been used |
| 2739 | * at some point as a UBO. |
| 2740 | */ |
| 2741 | if (size >= 0) |
| 2742 | bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER; |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | /** |
Iago Toral Quiroga | 0aa83f3 | 2015-03-19 10:47:17 +0100 | [diff] [blame] | 2746 | * Binds a buffer object to a shader storage buffer binding point. |
| 2747 | * |
| 2748 | * The caller is responsible for flushing vertices and updating |
| 2749 | * NewDriverState. |
| 2750 | */ |
| 2751 | static void |
| 2752 | set_ssbo_binding(struct gl_context *ctx, |
| 2753 | struct gl_shader_storage_buffer_binding *binding, |
| 2754 | struct gl_buffer_object *bufObj, |
| 2755 | GLintptr offset, |
| 2756 | GLsizeiptr size, |
| 2757 | GLboolean autoSize) |
| 2758 | { |
| 2759 | _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); |
| 2760 | |
| 2761 | binding->Offset = offset; |
| 2762 | binding->Size = size; |
| 2763 | binding->AutomaticSize = autoSize; |
| 2764 | |
| 2765 | /* If this is a real buffer object, mark it has having been used |
| 2766 | * at some point as a SSBO. |
| 2767 | */ |
| 2768 | if (size >= 0) |
| 2769 | bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER; |
| 2770 | } |
| 2771 | |
| 2772 | /** |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2773 | * Binds a buffer object to a uniform buffer binding point. |
| 2774 | * |
| 2775 | * Unlike set_ubo_binding(), this function also flushes vertices |
| 2776 | * and updates NewDriverState. It also checks if the binding |
| 2777 | * has actually changed before updating it. |
| 2778 | */ |
| 2779 | static void |
| 2780 | bind_uniform_buffer(struct gl_context *ctx, |
| 2781 | GLuint index, |
| 2782 | struct gl_buffer_object *bufObj, |
| 2783 | GLintptr offset, |
| 2784 | GLsizeiptr size, |
| 2785 | GLboolean autoSize) |
| 2786 | { |
| 2787 | struct gl_uniform_buffer_binding *binding = |
| 2788 | &ctx->UniformBufferBindings[index]; |
| 2789 | |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2790 | if (binding->BufferObject == bufObj && |
| 2791 | binding->Offset == offset && |
| 2792 | binding->Size == size && |
| 2793 | binding->AutomaticSize == autoSize) { |
| 2794 | return; |
| 2795 | } |
| 2796 | |
Marek Olšák | 77d8fbc | 2013-04-26 14:21:09 +0200 | [diff] [blame] | 2797 | FLUSH_VERTICES(ctx, 0); |
| 2798 | ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer; |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2799 | |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2800 | set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize); |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | /** |
Iago Toral Quiroga | 8a1d58b | 2015-03-19 11:37:43 +0100 | [diff] [blame] | 2804 | * Binds a buffer object to a shader storage buffer binding point. |
| 2805 | * |
| 2806 | * Unlike set_ssbo_binding(), this function also flushes vertices |
| 2807 | * and updates NewDriverState. It also checks if the binding |
| 2808 | * has actually changed before updating it. |
| 2809 | */ |
| 2810 | static void |
| 2811 | bind_shader_storage_buffer(struct gl_context *ctx, |
| 2812 | GLuint index, |
| 2813 | struct gl_buffer_object *bufObj, |
| 2814 | GLintptr offset, |
| 2815 | GLsizeiptr size, |
| 2816 | GLboolean autoSize) |
| 2817 | { |
| 2818 | struct gl_shader_storage_buffer_binding *binding = |
| 2819 | &ctx->ShaderStorageBufferBindings[index]; |
| 2820 | |
| 2821 | if (binding->BufferObject == bufObj && |
| 2822 | binding->Offset == offset && |
| 2823 | binding->Size == size && |
| 2824 | binding->AutomaticSize == autoSize) { |
| 2825 | return; |
| 2826 | } |
| 2827 | |
| 2828 | FLUSH_VERTICES(ctx, 0); |
| 2829 | ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer; |
| 2830 | |
| 2831 | set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize); |
| 2832 | } |
| 2833 | |
| 2834 | /** |
Brian Paul | 09af578 | 2012-06-22 08:43:58 -0600 | [diff] [blame] | 2835 | * Bind a region of a buffer object to a uniform block binding point. |
| 2836 | * \param index the uniform buffer binding point index |
| 2837 | * \param bufObj the buffer object |
| 2838 | * \param offset offset to the start of buffer object region |
| 2839 | * \param size size of the buffer object region |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2840 | */ |
| 2841 | static void |
| 2842 | bind_buffer_range_uniform_buffer(struct gl_context *ctx, |
| 2843 | GLuint index, |
| 2844 | struct gl_buffer_object *bufObj, |
| 2845 | GLintptr offset, |
| 2846 | GLsizeiptr size) |
| 2847 | { |
| 2848 | if (index >= ctx->Const.MaxUniformBufferBindings) { |
| 2849 | _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index); |
| 2850 | return; |
| 2851 | } |
| 2852 | |
| 2853 | if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) { |
| 2854 | _mesa_error(ctx, GL_INVALID_VALUE, |
Brian Paul | 4676c6c | 2014-10-20 11:49:17 -0600 | [diff] [blame] | 2855 | "glBindBufferRange(offset misaligned %d/%d)", (int) offset, |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2856 | ctx->Const.UniformBufferOffsetAlignment); |
| 2857 | return; |
| 2858 | } |
| 2859 | |
| 2860 | if (bufObj == ctx->Shared->NullBufferObj) { |
| 2861 | offset = -1; |
| 2862 | size = -1; |
| 2863 | } |
| 2864 | |
| 2865 | _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj); |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2866 | bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE); |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
Iago Toral Quiroga | 173ed05 | 2015-03-19 11:42:33 +0100 | [diff] [blame] | 2869 | /** |
| 2870 | * Bind a region of a buffer object to a shader storage block binding point. |
| 2871 | * \param index the shader storage buffer binding point index |
| 2872 | * \param bufObj the buffer object |
| 2873 | * \param offset offset to the start of buffer object region |
| 2874 | * \param size size of the buffer object region |
| 2875 | */ |
| 2876 | static void |
| 2877 | bind_buffer_range_shader_storage_buffer(struct gl_context *ctx, |
| 2878 | GLuint index, |
| 2879 | struct gl_buffer_object *bufObj, |
| 2880 | GLintptr offset, |
| 2881 | GLsizeiptr size) |
| 2882 | { |
| 2883 | if (index >= ctx->Const.MaxShaderStorageBufferBindings) { |
| 2884 | _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index); |
| 2885 | return; |
| 2886 | } |
| 2887 | |
| 2888 | if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) { |
| 2889 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 2890 | "glBindBufferRange(offset misaligned %d/%d)", (int) offset, |
| 2891 | ctx->Const.ShaderStorageBufferOffsetAlignment); |
| 2892 | return; |
| 2893 | } |
| 2894 | |
| 2895 | if (bufObj == ctx->Shared->NullBufferObj) { |
| 2896 | offset = -1; |
| 2897 | size = -1; |
| 2898 | } |
| 2899 | |
| 2900 | _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj); |
| 2901 | bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE); |
| 2902 | } |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2903 | |
| 2904 | /** |
Brian Paul | 09af578 | 2012-06-22 08:43:58 -0600 | [diff] [blame] | 2905 | * Bind a buffer object to a uniform block binding point. |
| 2906 | * As above, but offset = 0. |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2907 | */ |
| 2908 | static void |
| 2909 | bind_buffer_base_uniform_buffer(struct gl_context *ctx, |
| 2910 | GLuint index, |
| 2911 | struct gl_buffer_object *bufObj) |
| 2912 | { |
| 2913 | if (index >= ctx->Const.MaxUniformBufferBindings) { |
| 2914 | _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); |
| 2915 | return; |
| 2916 | } |
| 2917 | |
| 2918 | _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj); |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2919 | |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2920 | if (bufObj == ctx->Shared->NullBufferObj) |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2921 | bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2922 | else |
Fredrik Höglund | f65a0c1 | 2013-11-15 19:51:02 +0100 | [diff] [blame] | 2923 | bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 2924 | } |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 2925 | |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 2926 | /** |
Iago Toral Quiroga | 8a1d58b | 2015-03-19 11:37:43 +0100 | [diff] [blame] | 2927 | * Bind a buffer object to a shader storage block binding point. |
| 2928 | * As above, but offset = 0. |
| 2929 | */ |
| 2930 | static void |
| 2931 | bind_buffer_base_shader_storage_buffer(struct gl_context *ctx, |
| 2932 | GLuint index, |
| 2933 | struct gl_buffer_object *bufObj) |
| 2934 | { |
| 2935 | if (index >= ctx->Const.MaxShaderStorageBufferBindings) { |
| 2936 | _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); |
| 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj); |
| 2941 | |
| 2942 | if (bufObj == ctx->Shared->NullBufferObj) |
| 2943 | bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); |
| 2944 | else |
| 2945 | bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); |
| 2946 | } |
| 2947 | |
| 2948 | /** |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 2949 | * Binds a buffer object to an atomic buffer binding point. |
| 2950 | * |
| 2951 | * The caller is responsible for validating the offset, |
| 2952 | * flushing the vertices and updating NewDriverState. |
| 2953 | */ |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 2954 | static void |
| 2955 | set_atomic_buffer_binding(struct gl_context *ctx, |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 2956 | struct gl_atomic_buffer_binding *binding, |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 2957 | struct gl_buffer_object *bufObj, |
| 2958 | GLintptr offset, |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 2959 | GLsizeiptr size) |
| 2960 | { |
| 2961 | _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); |
| 2962 | |
| 2963 | if (bufObj == ctx->Shared->NullBufferObj) { |
| 2964 | binding->Offset = -1; |
| 2965 | binding->Size = -1; |
| 2966 | } else { |
| 2967 | binding->Offset = offset; |
| 2968 | binding->Size = size; |
Chris Forbes | 7bd6dfe | 2014-10-12 12:27:31 +1300 | [diff] [blame] | 2969 | bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER; |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | /** |
| 2974 | * Binds a buffer object to an atomic buffer binding point. |
| 2975 | * |
| 2976 | * Unlike set_atomic_buffer_binding(), this function also validates the |
| 2977 | * index and offset, flushes vertices, and updates NewDriverState. |
| 2978 | * It also checks if the binding has actually changing before |
| 2979 | * updating it. |
| 2980 | */ |
| 2981 | static void |
| 2982 | bind_atomic_buffer(struct gl_context *ctx, |
| 2983 | unsigned index, |
| 2984 | struct gl_buffer_object *bufObj, |
| 2985 | GLintptr offset, |
| 2986 | GLsizeiptr size, |
| 2987 | const char *name) |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 2988 | { |
| 2989 | struct gl_atomic_buffer_binding *binding; |
| 2990 | |
| 2991 | if (index >= ctx->Const.MaxAtomicBufferBindings) { |
| 2992 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index); |
| 2993 | return; |
| 2994 | } |
| 2995 | |
| 2996 | if (offset & (ATOMIC_COUNTER_SIZE - 1)) { |
| 2997 | _mesa_error(ctx, GL_INVALID_VALUE, |
Brian Paul | 4676c6c | 2014-10-20 11:49:17 -0600 | [diff] [blame] | 2998 | "%s(offset misaligned %d/%d)", name, (int) offset, |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 2999 | ATOMIC_COUNTER_SIZE); |
| 3000 | return; |
| 3001 | } |
| 3002 | |
| 3003 | _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj); |
| 3004 | |
| 3005 | binding = &ctx->AtomicBufferBindings[index]; |
| 3006 | if (binding->BufferObject == bufObj && |
| 3007 | binding->Offset == offset && |
| 3008 | binding->Size == size) { |
| 3009 | return; |
| 3010 | } |
| 3011 | |
| 3012 | FLUSH_VERTICES(ctx, 0); |
| 3013 | ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer; |
| 3014 | |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 3015 | set_atomic_buffer_binding(ctx, binding, bufObj, offset, size); |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3018 | static inline bool |
| 3019 | bind_buffers_check_offset_and_size(struct gl_context *ctx, |
| 3020 | GLuint index, |
| 3021 | const GLintptr *offsets, |
| 3022 | const GLsizeiptr *sizes) |
| 3023 | { |
| 3024 | if (offsets[index] < 0) { |
| 3025 | /* The ARB_multi_bind spec says: |
| 3026 | * |
| 3027 | * "An INVALID_VALUE error is generated by BindBuffersRange if any |
| 3028 | * value in <offsets> is less than zero (per binding)." |
| 3029 | */ |
| 3030 | _mesa_error(ctx, GL_INVALID_VALUE, |
Brian Paul | 80fa7fd | 2014-08-08 07:49:33 -0600 | [diff] [blame] | 3031 | "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)", |
| 3032 | index, (int64_t) offsets[index]); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3033 | return false; |
| 3034 | } |
| 3035 | |
| 3036 | if (sizes[index] <= 0) { |
| 3037 | /* The ARB_multi_bind spec says: |
| 3038 | * |
| 3039 | * "An INVALID_VALUE error is generated by BindBuffersRange if any |
| 3040 | * value in <sizes> is less than or equal to zero (per binding)." |
| 3041 | */ |
| 3042 | _mesa_error(ctx, GL_INVALID_VALUE, |
Brian Paul | 80fa7fd | 2014-08-08 07:49:33 -0600 | [diff] [blame] | 3043 | "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)", |
| 3044 | index, (int64_t) sizes[index]); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3045 | return false; |
| 3046 | } |
| 3047 | |
| 3048 | return true; |
| 3049 | } |
| 3050 | |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3051 | static bool |
| 3052 | error_check_bind_uniform_buffers(struct gl_context *ctx, |
| 3053 | GLuint first, GLsizei count, |
| 3054 | const char *caller) |
| 3055 | { |
| 3056 | if (!ctx->Extensions.ARB_uniform_buffer_object) { |
| 3057 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 3058 | "%s(target=GL_UNIFORM_BUFFER)", caller); |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
| 3062 | /* The ARB_multi_bind_spec says: |
| 3063 | * |
| 3064 | * "An INVALID_OPERATION error is generated if <first> + <count> is |
| 3065 | * greater than the number of target-specific indexed binding points, |
| 3066 | * as described in section 6.7.1." |
| 3067 | */ |
| 3068 | if (first + count > ctx->Const.MaxUniformBufferBindings) { |
| 3069 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3070 | "%s(first=%u + count=%d > the value of " |
| 3071 | "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)", |
| 3072 | caller, first, count, |
| 3073 | ctx->Const.MaxUniformBufferBindings); |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | return true; |
| 3078 | } |
| 3079 | |
Iago Toral Quiroga | 0aa83f3 | 2015-03-19 10:47:17 +0100 | [diff] [blame] | 3080 | static bool |
| 3081 | error_check_bind_shader_storage_buffers(struct gl_context *ctx, |
| 3082 | GLuint first, GLsizei count, |
| 3083 | const char *caller) |
| 3084 | { |
| 3085 | if (!ctx->Extensions.ARB_shader_storage_buffer_object) { |
| 3086 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 3087 | "%s(target=GL_SHADER_STORAGE_BUFFER)", caller); |
| 3088 | return false; |
| 3089 | } |
| 3090 | |
| 3091 | /* The ARB_multi_bind_spec says: |
| 3092 | * |
| 3093 | * "An INVALID_OPERATION error is generated if <first> + <count> is |
| 3094 | * greater than the number of target-specific indexed binding points, |
| 3095 | * as described in section 6.7.1." |
| 3096 | */ |
| 3097 | if (first + count > ctx->Const.MaxShaderStorageBufferBindings) { |
| 3098 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3099 | "%s(first=%u + count=%d > the value of " |
| 3100 | "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)", |
| 3101 | caller, first, count, |
| 3102 | ctx->Const.MaxShaderStorageBufferBindings); |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
| 3106 | return true; |
| 3107 | } |
| 3108 | |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3109 | /** |
| 3110 | * Unbind all uniform buffers in the range |
| 3111 | * <first> through <first>+<count>-1 |
| 3112 | */ |
| 3113 | static void |
| 3114 | unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count) |
| 3115 | { |
| 3116 | struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj; |
Jan Vesely | 3cb10cc | 2015-01-15 13:41:04 -0500 | [diff] [blame] | 3117 | GLint i; |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3118 | |
| 3119 | for (i = 0; i < count; i++) |
| 3120 | set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i], |
| 3121 | bufObj, -1, -1, GL_TRUE); |
| 3122 | } |
| 3123 | |
Iago Toral Quiroga | 0aa83f3 | 2015-03-19 10:47:17 +0100 | [diff] [blame] | 3124 | /** |
| 3125 | * Unbind all shader storage buffers in the range |
| 3126 | * <first> through <first>+<count>-1 |
| 3127 | */ |
| 3128 | static void |
| 3129 | unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first, |
| 3130 | GLsizei count) |
| 3131 | { |
| 3132 | struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj; |
| 3133 | GLint i; |
| 3134 | |
| 3135 | for (i = 0; i < count; i++) |
| 3136 | set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i], |
| 3137 | bufObj, -1, -1, GL_TRUE); |
| 3138 | } |
| 3139 | |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3140 | static void |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3141 | bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count, |
| 3142 | const GLuint *buffers, |
| 3143 | bool range, |
| 3144 | const GLintptr *offsets, const GLsizeiptr *sizes, |
| 3145 | const char *caller) |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3146 | { |
Jan Vesely | 3cb10cc | 2015-01-15 13:41:04 -0500 | [diff] [blame] | 3147 | GLint i; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3148 | |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3149 | if (!error_check_bind_uniform_buffers(ctx, first, count, caller)) |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3150 | return; |
| 3151 | |
| 3152 | /* Assume that at least one binding will be changed */ |
| 3153 | FLUSH_VERTICES(ctx, 0); |
| 3154 | ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer; |
| 3155 | |
| 3156 | if (!buffers) { |
| 3157 | /* The ARB_multi_bind spec says: |
| 3158 | * |
| 3159 | * "If <buffers> is NULL, all bindings from <first> through |
| 3160 | * <first>+<count>-1 are reset to their unbound (zero) state. |
| 3161 | * In this case, the offsets and sizes associated with the |
| 3162 | * binding points are set to default values, ignoring |
| 3163 | * <offsets> and <sizes>." |
| 3164 | */ |
| 3165 | unbind_uniform_buffers(ctx, first, count); |
| 3166 | return; |
| 3167 | } |
| 3168 | |
| 3169 | /* Note that the error semantics for multi-bind commands differ from |
| 3170 | * those of other GL commands. |
| 3171 | * |
| 3172 | * The Issues section in the ARB_multi_bind spec says: |
| 3173 | * |
| 3174 | * "(11) Typically, OpenGL specifies that if an error is generated by a |
| 3175 | * command, that command has no effect. This is somewhat |
| 3176 | * unfortunate for multi-bind commands, because it would require a |
| 3177 | * first pass to scan the entire list of bound objects for errors |
| 3178 | * and then a second pass to actually perform the bindings. |
| 3179 | * Should we have different error semantics? |
| 3180 | * |
| 3181 | * RESOLVED: Yes. In this specification, when the parameters for |
| 3182 | * one of the <count> binding points are invalid, that binding point |
| 3183 | * is not updated and an error will be generated. However, other |
| 3184 | * binding points in the same command will be updated if their |
| 3185 | * parameters are valid and no other error occurs." |
| 3186 | */ |
| 3187 | |
| 3188 | _mesa_begin_bufferobj_lookups(ctx); |
| 3189 | |
| 3190 | for (i = 0; i < count; i++) { |
| 3191 | struct gl_uniform_buffer_binding *binding = |
| 3192 | &ctx->UniformBufferBindings[first + i]; |
| 3193 | struct gl_buffer_object *bufObj; |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3194 | GLintptr offset = 0; |
| 3195 | GLsizeiptr size = 0; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3196 | |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3197 | if (range) { |
| 3198 | if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes)) |
| 3199 | continue; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3200 | |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3201 | /* The ARB_multi_bind spec says: |
| 3202 | * |
| 3203 | * "An INVALID_VALUE error is generated by BindBuffersRange if any |
| 3204 | * pair of values in <offsets> and <sizes> does not respectively |
| 3205 | * satisfy the constraints described for those parameters for the |
| 3206 | * specified target, as described in section 6.7.1 (per binding)." |
| 3207 | * |
| 3208 | * Section 6.7.1 refers to table 6.5, which says: |
| 3209 | * |
| 3210 | * "┌───────────────────────────────────────────────────────────────┐ |
| 3211 | * │ Uniform buffer array bindings (see sec. 7.6) │ |
| 3212 | * ├─────────────────────┬─────────────────────────────────────────┤ |
| 3213 | * │ ... │ ... │ |
| 3214 | * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │ |
| 3215 | * │ │ OFFSET_ALIGNMENT │ |
| 3216 | * │ ... │ ... │ |
| 3217 | * │ size restriction │ none │ |
| 3218 | * └─────────────────────┴─────────────────────────────────────────┘" |
| 3219 | */ |
| 3220 | if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) { |
| 3221 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3222 | "glBindBuffersRange(offsets[%u]=%" PRId64 |
| 3223 | " is misaligned; it must be a multiple of the value of " |
| 3224 | "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when " |
| 3225 | "target=GL_UNIFORM_BUFFER)", |
| 3226 | i, (int64_t) offsets[i], |
| 3227 | ctx->Const.UniformBufferOffsetAlignment); |
| 3228 | continue; |
| 3229 | } |
| 3230 | |
| 3231 | offset = offsets[i]; |
| 3232 | size = sizes[i]; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | if (binding->BufferObject && binding->BufferObject->Name == buffers[i]) |
| 3236 | bufObj = binding->BufferObject; |
| 3237 | else |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3238 | bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3239 | |
| 3240 | if (bufObj) { |
| 3241 | if (bufObj == ctx->Shared->NullBufferObj) |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3242 | set_ubo_binding(ctx, binding, bufObj, -1, -1, !range); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3243 | else |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3244 | set_ubo_binding(ctx, binding, bufObj, offset, size, !range); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | _mesa_end_bufferobj_lookups(ctx); |
| 3249 | } |
| 3250 | |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3251 | static void |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3252 | bind_shader_storage_buffers(struct gl_context *ctx, GLuint first, |
| 3253 | GLsizei count, const GLuint *buffers, |
| 3254 | bool range, |
| 3255 | const GLintptr *offsets, |
| 3256 | const GLsizeiptr *sizes, |
| 3257 | const char *caller) |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3258 | { |
| 3259 | GLint i; |
| 3260 | |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3261 | if (!error_check_bind_shader_storage_buffers(ctx, first, count, caller)) |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3262 | return; |
| 3263 | |
| 3264 | /* Assume that at least one binding will be changed */ |
| 3265 | FLUSH_VERTICES(ctx, 0); |
| 3266 | ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer; |
| 3267 | |
| 3268 | if (!buffers) { |
| 3269 | /* The ARB_multi_bind spec says: |
| 3270 | * |
| 3271 | * "If <buffers> is NULL, all bindings from <first> through |
| 3272 | * <first>+<count>-1 are reset to their unbound (zero) state. |
| 3273 | * In this case, the offsets and sizes associated with the |
| 3274 | * binding points are set to default values, ignoring |
| 3275 | * <offsets> and <sizes>." |
| 3276 | */ |
| 3277 | unbind_shader_storage_buffers(ctx, first, count); |
| 3278 | return; |
| 3279 | } |
| 3280 | |
| 3281 | /* Note that the error semantics for multi-bind commands differ from |
| 3282 | * those of other GL commands. |
| 3283 | * |
| 3284 | * The Issues section in the ARB_multi_bind spec says: |
| 3285 | * |
| 3286 | * "(11) Typically, OpenGL specifies that if an error is generated by a |
| 3287 | * command, that command has no effect. This is somewhat |
| 3288 | * unfortunate for multi-bind commands, because it would require a |
| 3289 | * first pass to scan the entire list of bound objects for errors |
| 3290 | * and then a second pass to actually perform the bindings. |
| 3291 | * Should we have different error semantics? |
| 3292 | * |
| 3293 | * RESOLVED: Yes. In this specification, when the parameters for |
| 3294 | * one of the <count> binding points are invalid, that binding point |
| 3295 | * is not updated and an error will be generated. However, other |
| 3296 | * binding points in the same command will be updated if their |
| 3297 | * parameters are valid and no other error occurs." |
| 3298 | */ |
| 3299 | |
| 3300 | _mesa_begin_bufferobj_lookups(ctx); |
| 3301 | |
| 3302 | for (i = 0; i < count; i++) { |
| 3303 | struct gl_shader_storage_buffer_binding *binding = |
| 3304 | &ctx->ShaderStorageBufferBindings[first + i]; |
| 3305 | struct gl_buffer_object *bufObj; |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3306 | GLintptr offset = 0; |
| 3307 | GLsizeiptr size = 0; |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3308 | |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3309 | if (range) { |
| 3310 | if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes)) |
| 3311 | continue; |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3312 | |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3313 | /* The ARB_multi_bind spec says: |
| 3314 | * |
| 3315 | * "An INVALID_VALUE error is generated by BindBuffersRange if any |
| 3316 | * pair of values in <offsets> and <sizes> does not respectively |
| 3317 | * satisfy the constraints described for those parameters for the |
| 3318 | * specified target, as described in section 6.7.1 (per binding)." |
| 3319 | * |
| 3320 | * Section 6.7.1 refers to table 6.5, which says: |
| 3321 | * |
| 3322 | * "┌───────────────────────────────────────────────────────────────┐ |
| 3323 | * │ Shader storage buffer array bindings (see sec. 7.8) │ |
| 3324 | * ├─────────────────────┬─────────────────────────────────────────┤ |
| 3325 | * │ ... │ ... │ |
| 3326 | * │ offset restriction │ multiple of value of SHADER_STORAGE_- │ |
| 3327 | * │ │ BUFFER_OFFSET_ALIGNMENT │ |
| 3328 | * │ ... │ ... │ |
| 3329 | * │ size restriction │ none │ |
| 3330 | * └─────────────────────┴─────────────────────────────────────────┘" |
| 3331 | */ |
| 3332 | if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) { |
| 3333 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3334 | "glBindBuffersRange(offsets[%u]=%" PRId64 |
| 3335 | " is misaligned; it must be a multiple of the value of " |
| 3336 | "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when " |
| 3337 | "target=GL_SHADER_STORAGE_BUFFER)", |
| 3338 | i, (int64_t) offsets[i], |
| 3339 | ctx->Const.ShaderStorageBufferOffsetAlignment); |
| 3340 | continue; |
| 3341 | } |
| 3342 | |
| 3343 | offset = offsets[i]; |
| 3344 | size = sizes[i]; |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | if (binding->BufferObject && binding->BufferObject->Name == buffers[i]) |
| 3348 | bufObj = binding->BufferObject; |
| 3349 | else |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3350 | bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3351 | |
| 3352 | if (bufObj) { |
| 3353 | if (bufObj == ctx->Shared->NullBufferObj) |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3354 | set_ssbo_binding(ctx, binding, bufObj, -1, -1, !range); |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3355 | else |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3356 | set_ssbo_binding(ctx, binding, bufObj, offset, size, !range); |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | _mesa_end_bufferobj_lookups(ctx); |
| 3361 | } |
| 3362 | |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3363 | static bool |
| 3364 | error_check_bind_xfb_buffers(struct gl_context *ctx, |
| 3365 | struct gl_transform_feedback_object *tfObj, |
| 3366 | GLuint first, GLsizei count, const char *caller) |
| 3367 | { |
| 3368 | if (!ctx->Extensions.EXT_transform_feedback) { |
| 3369 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 3370 | "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller); |
| 3371 | return false; |
| 3372 | } |
| 3373 | |
| 3374 | /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says: |
| 3375 | * |
| 3376 | * "An INVALID_OPERATION error is generated : |
| 3377 | * |
| 3378 | * ... |
| 3379 | * • by BindBufferRange or BindBufferBase if target is TRANSFORM_- |
| 3380 | * FEEDBACK_BUFFER and transform feedback is currently active." |
| 3381 | * |
| 3382 | * We assume that this is also meant to apply to BindBuffersRange |
| 3383 | * and BindBuffersBase. |
| 3384 | */ |
| 3385 | if (tfObj->Active) { |
| 3386 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3387 | "%s(Changing transform feedback buffers while " |
| 3388 | "transform feedback is active)", caller); |
| 3389 | return false; |
| 3390 | } |
| 3391 | |
| 3392 | /* The ARB_multi_bind_spec says: |
| 3393 | * |
| 3394 | * "An INVALID_OPERATION error is generated if <first> + <count> is |
| 3395 | * greater than the number of target-specific indexed binding points, |
| 3396 | * as described in section 6.7.1." |
| 3397 | */ |
| 3398 | if (first + count > ctx->Const.MaxTransformFeedbackBuffers) { |
| 3399 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3400 | "%s(first=%u + count=%d > the value of " |
| 3401 | "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)", |
| 3402 | caller, first, count, |
| 3403 | ctx->Const.MaxTransformFeedbackBuffers); |
| 3404 | return false; |
| 3405 | } |
| 3406 | |
| 3407 | return true; |
| 3408 | } |
| 3409 | |
| 3410 | /** |
| 3411 | * Unbind all transform feedback buffers in the range |
| 3412 | * <first> through <first>+<count>-1 |
| 3413 | */ |
| 3414 | static void |
| 3415 | unbind_xfb_buffers(struct gl_context *ctx, |
| 3416 | struct gl_transform_feedback_object *tfObj, |
| 3417 | GLuint first, GLsizei count) |
| 3418 | { |
| 3419 | struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj; |
Jan Vesely | 3cb10cc | 2015-01-15 13:41:04 -0500 | [diff] [blame] | 3420 | GLint i; |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3421 | |
| 3422 | for (i = 0; i < count; i++) |
| 3423 | _mesa_set_transform_feedback_binding(ctx, tfObj, first + i, |
| 3424 | bufObj, 0, 0); |
| 3425 | } |
| 3426 | |
| 3427 | static void |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3428 | bind_xfb_buffers(struct gl_context *ctx, |
| 3429 | GLuint first, GLsizei count, |
| 3430 | const GLuint *buffers, |
| 3431 | bool range, |
| 3432 | const GLintptr *offsets, |
| 3433 | const GLsizeiptr *sizes, |
| 3434 | const char *caller) |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3435 | { |
| 3436 | struct gl_transform_feedback_object *tfObj = |
| 3437 | ctx->TransformFeedback.CurrentObject; |
Jan Vesely | 3cb10cc | 2015-01-15 13:41:04 -0500 | [diff] [blame] | 3438 | GLint i; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3439 | |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3440 | if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count, caller)) |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3441 | return; |
| 3442 | |
| 3443 | /* Assume that at least one binding will be changed */ |
| 3444 | FLUSH_VERTICES(ctx, 0); |
| 3445 | ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; |
| 3446 | |
| 3447 | if (!buffers) { |
| 3448 | /* The ARB_multi_bind spec says: |
| 3449 | * |
| 3450 | * "If <buffers> is NULL, all bindings from <first> through |
| 3451 | * <first>+<count>-1 are reset to their unbound (zero) state. |
| 3452 | * In this case, the offsets and sizes associated with the |
| 3453 | * binding points are set to default values, ignoring |
| 3454 | * <offsets> and <sizes>." |
| 3455 | */ |
| 3456 | unbind_xfb_buffers(ctx, tfObj, first, count); |
| 3457 | return; |
| 3458 | } |
| 3459 | |
| 3460 | /* Note that the error semantics for multi-bind commands differ from |
| 3461 | * those of other GL commands. |
| 3462 | * |
| 3463 | * The Issues section in the ARB_multi_bind spec says: |
| 3464 | * |
| 3465 | * "(11) Typically, OpenGL specifies that if an error is generated by a |
| 3466 | * command, that command has no effect. This is somewhat |
| 3467 | * unfortunate for multi-bind commands, because it would require a |
| 3468 | * first pass to scan the entire list of bound objects for errors |
| 3469 | * and then a second pass to actually perform the bindings. |
| 3470 | * Should we have different error semantics? |
| 3471 | * |
| 3472 | * RESOLVED: Yes. In this specification, when the parameters for |
| 3473 | * one of the <count> binding points are invalid, that binding point |
| 3474 | * is not updated and an error will be generated. However, other |
| 3475 | * binding points in the same command will be updated if their |
| 3476 | * parameters are valid and no other error occurs." |
| 3477 | */ |
| 3478 | |
| 3479 | _mesa_begin_bufferobj_lookups(ctx); |
| 3480 | |
| 3481 | for (i = 0; i < count; i++) { |
| 3482 | const GLuint index = first + i; |
| 3483 | struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index]; |
| 3484 | struct gl_buffer_object *bufObj; |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3485 | GLintptr offset = 0; |
| 3486 | GLsizeiptr size = 0; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3487 | |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3488 | if (range) { |
| 3489 | offset = offsets[i]; |
| 3490 | size = sizes[i]; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3491 | |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3492 | if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes)) |
| 3493 | continue; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3494 | |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3495 | /* The ARB_multi_bind spec says: |
| 3496 | * |
| 3497 | * "An INVALID_VALUE error is generated by BindBuffersRange if any |
| 3498 | * pair of values in <offsets> and <sizes> does not respectively |
| 3499 | * satisfy the constraints described for those parameters for the |
| 3500 | * specified target, as described in section 6.7.1 (per binding)." |
| 3501 | * |
| 3502 | * Section 6.7.1 refers to table 6.5, which says: |
| 3503 | * |
| 3504 | * "┌───────────────────────────────────────────────────────────────┐ |
| 3505 | * │ Transform feedback array bindings (see sec. 13.2.2) │ |
| 3506 | * ├───────────────────────┬───────────────────────────────────────┤ |
| 3507 | * │ ... │ ... │ |
| 3508 | * │ offset restriction │ multiple of 4 │ |
| 3509 | * │ ... │ ... │ |
| 3510 | * │ size restriction │ multiple of 4 │ |
| 3511 | * └───────────────────────┴───────────────────────────────────────┘" |
| 3512 | */ |
| 3513 | if (offsets[i] & 0x3) { |
| 3514 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3515 | "glBindBuffersRange(offsets[%u]=%" PRId64 |
| 3516 | " is misaligned; it must be a multiple of 4 when " |
| 3517 | "target=GL_TRANSFORM_FEEDBACK_BUFFER)", |
| 3518 | i, (int64_t) offsets[i]); |
| 3519 | continue; |
| 3520 | } |
| 3521 | |
| 3522 | if (sizes[i] & 0x3) { |
| 3523 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3524 | "glBindBuffersRange(sizes[%u]=%" PRId64 |
| 3525 | " is misaligned; it must be a multiple of 4 when " |
| 3526 | "target=GL_TRANSFORM_FEEDBACK_BUFFER)", |
| 3527 | i, (int64_t) sizes[i]); |
| 3528 | continue; |
| 3529 | } |
| 3530 | |
| 3531 | offset = offsets[i]; |
| 3532 | size = sizes[i]; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | if (boundBufObj && boundBufObj->Name == buffers[i]) |
| 3536 | bufObj = boundBufObj; |
| 3537 | else |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3538 | bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3539 | |
| 3540 | if (bufObj) |
| 3541 | _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj, |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3542 | offset, size); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | _mesa_end_bufferobj_lookups(ctx); |
| 3546 | } |
| 3547 | |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3548 | static bool |
| 3549 | error_check_bind_atomic_buffers(struct gl_context *ctx, |
| 3550 | GLuint first, GLsizei count, |
| 3551 | const char *caller) |
| 3552 | { |
| 3553 | if (!ctx->Extensions.ARB_shader_atomic_counters) { |
| 3554 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 3555 | "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller); |
| 3556 | return false; |
| 3557 | } |
| 3558 | |
| 3559 | /* The ARB_multi_bind_spec says: |
| 3560 | * |
| 3561 | * "An INVALID_OPERATION error is generated if <first> + <count> is |
| 3562 | * greater than the number of target-specific indexed binding points, |
| 3563 | * as described in section 6.7.1." |
| 3564 | */ |
| 3565 | if (first + count > ctx->Const.MaxAtomicBufferBindings) { |
| 3566 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3567 | "%s(first=%u + count=%d > the value of " |
| 3568 | "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)", |
| 3569 | caller, first, count, ctx->Const.MaxAtomicBufferBindings); |
| 3570 | return false; |
| 3571 | } |
| 3572 | |
| 3573 | return true; |
| 3574 | } |
| 3575 | |
| 3576 | /** |
| 3577 | * Unbind all atomic counter buffers in the range |
| 3578 | * <first> through <first>+<count>-1 |
| 3579 | */ |
| 3580 | static void |
| 3581 | unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count) |
| 3582 | { |
| 3583 | struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj; |
Jan Vesely | 3cb10cc | 2015-01-15 13:41:04 -0500 | [diff] [blame] | 3584 | GLint i; |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3585 | |
| 3586 | for (i = 0; i < count; i++) |
| 3587 | set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i], |
| 3588 | bufObj, -1, -1); |
| 3589 | } |
| 3590 | |
| 3591 | static void |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3592 | bind_atomic_buffers(struct gl_context *ctx, |
| 3593 | GLuint first, |
| 3594 | GLsizei count, |
| 3595 | const GLuint *buffers, |
| 3596 | bool range, |
| 3597 | const GLintptr *offsets, |
| 3598 | const GLsizeiptr *sizes, |
| 3599 | const char *caller) |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3600 | { |
Jan Vesely | 3cb10cc | 2015-01-15 13:41:04 -0500 | [diff] [blame] | 3601 | GLint i; |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3602 | |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3603 | if (!error_check_bind_atomic_buffers(ctx, first, count, caller)) |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3604 | return; |
| 3605 | |
| 3606 | /* Assume that at least one binding will be changed */ |
| 3607 | FLUSH_VERTICES(ctx, 0); |
| 3608 | ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer; |
| 3609 | |
| 3610 | if (!buffers) { |
| 3611 | /* The ARB_multi_bind spec says: |
| 3612 | * |
| 3613 | * "If <buffers> is NULL, all bindings from <first> through |
| 3614 | * <first>+<count>-1 are reset to their unbound (zero) state. |
| 3615 | * In this case, the offsets and sizes associated with the |
| 3616 | * binding points are set to default values, ignoring |
| 3617 | * <offsets> and <sizes>." |
| 3618 | */ |
| 3619 | unbind_atomic_buffers(ctx, first, count); |
| 3620 | return; |
| 3621 | } |
| 3622 | |
| 3623 | /* Note that the error semantics for multi-bind commands differ from |
| 3624 | * those of other GL commands. |
| 3625 | * |
| 3626 | * The Issues section in the ARB_multi_bind spec says: |
| 3627 | * |
| 3628 | * "(11) Typically, OpenGL specifies that if an error is generated by a |
| 3629 | * command, that command has no effect. This is somewhat |
| 3630 | * unfortunate for multi-bind commands, because it would require a |
| 3631 | * first pass to scan the entire list of bound objects for errors |
| 3632 | * and then a second pass to actually perform the bindings. |
| 3633 | * Should we have different error semantics? |
| 3634 | * |
| 3635 | * RESOLVED: Yes. In this specification, when the parameters for |
| 3636 | * one of the <count> binding points are invalid, that binding point |
| 3637 | * is not updated and an error will be generated. However, other |
| 3638 | * binding points in the same command will be updated if their |
| 3639 | * parameters are valid and no other error occurs." |
| 3640 | */ |
| 3641 | |
| 3642 | _mesa_begin_bufferobj_lookups(ctx); |
| 3643 | |
| 3644 | for (i = 0; i < count; i++) { |
| 3645 | struct gl_atomic_buffer_binding *binding = |
| 3646 | &ctx->AtomicBufferBindings[first + i]; |
| 3647 | struct gl_buffer_object *bufObj; |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3648 | GLintptr offset = 0; |
| 3649 | GLsizeiptr size = 0; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3650 | |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3651 | if (range) { |
| 3652 | if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes)) |
| 3653 | continue; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3654 | |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3655 | /* The ARB_multi_bind spec says: |
| 3656 | * |
| 3657 | * "An INVALID_VALUE error is generated by BindBuffersRange if any |
| 3658 | * pair of values in <offsets> and <sizes> does not respectively |
| 3659 | * satisfy the constraints described for those parameters for the |
| 3660 | * specified target, as described in section 6.7.1 (per binding)." |
| 3661 | * |
| 3662 | * Section 6.7.1 refers to table 6.5, which says: |
| 3663 | * |
| 3664 | * "┌───────────────────────────────────────────────────────────────┐ |
| 3665 | * │ Atomic counter array bindings (see sec. 7.7.2) │ |
| 3666 | * ├───────────────────────┬───────────────────────────────────────┤ |
| 3667 | * │ ... │ ... │ |
| 3668 | * │ offset restriction │ multiple of 4 │ |
| 3669 | * │ ... │ ... │ |
| 3670 | * │ size restriction │ none │ |
| 3671 | * └───────────────────────┴───────────────────────────────────────┘" |
| 3672 | */ |
| 3673 | if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) { |
| 3674 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3675 | "glBindBuffersRange(offsets[%u]=%" PRId64 |
| 3676 | " is misaligned; it must be a multiple of %d when " |
| 3677 | "target=GL_ATOMIC_COUNTER_BUFFER)", |
| 3678 | i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE); |
| 3679 | continue; |
| 3680 | } |
| 3681 | |
| 3682 | offset = offsets[i]; |
| 3683 | size = sizes[i]; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | if (binding->BufferObject && binding->BufferObject->Name == buffers[i]) |
| 3687 | bufObj = binding->BufferObject; |
| 3688 | else |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3689 | bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3690 | |
| 3691 | if (bufObj) |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3692 | set_atomic_buffer_binding(ctx, binding, bufObj, offset, size); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | _mesa_end_bufferobj_lookups(ctx); |
| 3696 | } |
| 3697 | |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3698 | void GLAPIENTRY |
| 3699 | _mesa_BindBufferRange(GLenum target, GLuint index, |
| 3700 | GLuint buffer, GLintptr offset, GLsizeiptr size) |
| 3701 | { |
| 3702 | GET_CURRENT_CONTEXT(ctx); |
| 3703 | struct gl_buffer_object *bufObj; |
| 3704 | |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 3705 | if (MESA_VERBOSE & VERBOSE_API) { |
Jordan Justen | a2942d8 | 2016-01-01 16:58:49 -0800 | [diff] [blame] | 3706 | _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n", |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 3707 | _mesa_enum_to_string(target), index, buffer, offset, size); |
| 3708 | } |
| 3709 | |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3710 | if (buffer == 0) { |
| 3711 | bufObj = ctx->Shared->NullBufferObj; |
| 3712 | } else { |
| 3713 | bufObj = _mesa_lookup_bufferobj(ctx, buffer); |
| 3714 | } |
Ian Romanick | 8ba3b76 | 2015-08-26 13:55:54 +0100 | [diff] [blame] | 3715 | if (!_mesa_handle_bind_buffer_gen(ctx, buffer, |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 3716 | &bufObj, "glBindBufferRange")) |
Eric Anholt | 947d8ff | 2012-10-31 15:33:41 -0700 | [diff] [blame] | 3717 | return; |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3718 | |
| 3719 | if (!bufObj) { |
| 3720 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3721 | "glBindBufferRange(invalid buffer=%u)", buffer); |
| 3722 | return; |
| 3723 | } |
| 3724 | |
Matt Turner | 970ec8d | 2012-12-04 17:52:00 -0800 | [diff] [blame] | 3725 | if (buffer != 0) { |
| 3726 | if (size <= 0) { |
| 3727 | _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", |
| 3728 | (int) size); |
| 3729 | return; |
| 3730 | } |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | switch (target) { |
| 3734 | case GL_TRANSFORM_FEEDBACK_BUFFER: |
Martin Peres | a5d165a | 2015-01-20 16:30:32 +0200 | [diff] [blame] | 3735 | _mesa_bind_buffer_range_transform_feedback(ctx, |
| 3736 | ctx->TransformFeedback.CurrentObject, |
Martin Peres | 296d823 | 2015-01-21 12:22:11 +0200 | [diff] [blame] | 3737 | index, bufObj, offset, size, |
| 3738 | false); |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3739 | return; |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 3740 | case GL_UNIFORM_BUFFER: |
| 3741 | bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size); |
| 3742 | return; |
Iago Toral Quiroga | 173ed05 | 2015-03-19 11:42:33 +0100 | [diff] [blame] | 3743 | case GL_SHADER_STORAGE_BUFFER: |
| 3744 | bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size); |
| 3745 | return; |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 3746 | case GL_ATOMIC_COUNTER_BUFFER: |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 3747 | bind_atomic_buffer(ctx, index, bufObj, offset, size, |
| 3748 | "glBindBufferRange"); |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 3749 | return; |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3750 | default: |
| 3751 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)"); |
| 3752 | return; |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | void GLAPIENTRY |
| 3757 | _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer) |
| 3758 | { |
| 3759 | GET_CURRENT_CONTEXT(ctx); |
| 3760 | struct gl_buffer_object *bufObj; |
| 3761 | |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 3762 | if (MESA_VERBOSE & VERBOSE_API) { |
| 3763 | _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n", |
| 3764 | _mesa_enum_to_string(target), index, buffer); |
| 3765 | } |
| 3766 | |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3767 | if (buffer == 0) { |
| 3768 | bufObj = ctx->Shared->NullBufferObj; |
| 3769 | } else { |
| 3770 | bufObj = _mesa_lookup_bufferobj(ctx, buffer); |
| 3771 | } |
Ian Romanick | 8ba3b76 | 2015-08-26 13:55:54 +0100 | [diff] [blame] | 3772 | if (!_mesa_handle_bind_buffer_gen(ctx, buffer, |
Fredrik Höglund | ccb6286 | 2013-11-01 19:09:58 +0100 | [diff] [blame] | 3773 | &bufObj, "glBindBufferBase")) |
Eric Anholt | 947d8ff | 2012-10-31 15:33:41 -0700 | [diff] [blame] | 3774 | return; |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3775 | |
| 3776 | if (!bufObj) { |
| 3777 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3778 | "glBindBufferBase(invalid buffer=%u)", buffer); |
| 3779 | return; |
| 3780 | } |
| 3781 | |
Eric Anholt | cb9f35d | 2012-06-18 16:20:11 -0700 | [diff] [blame] | 3782 | /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with |
| 3783 | * regards to BindBufferBase. It says (GL 3.1 core spec, page 63): |
| 3784 | * |
| 3785 | * "BindBufferBase is equivalent to calling BindBufferRange with offset |
| 3786 | * zero and size equal to the size of buffer." |
| 3787 | * |
| 3788 | * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230): |
| 3789 | * |
| 3790 | * "If the parameter (starting offset or size) was not specified when the |
| 3791 | * buffer object was bound, zero is returned." |
| 3792 | * |
| 3793 | * What happens if the size of the buffer changes? Does the size of the |
| 3794 | * buffer at the moment glBindBufferBase was called still play a role, like |
| 3795 | * the first quote would imply, or is the size meaningless in the |
| 3796 | * glBindBufferBase case like the second quote would suggest? The GL 4.1 |
| 3797 | * core spec page 45 says: |
| 3798 | * |
| 3799 | * "It is equivalent to calling BindBufferRange with offset zero, while |
| 3800 | * size is determined by the size of the bound buffer at the time the |
| 3801 | * binding is used." |
| 3802 | * |
| 3803 | * My interpretation is that the GL 4.1 spec was a clarification of the |
| 3804 | * behavior, not a change. In particular, this choice will only make |
| 3805 | * rendering work in cases where it would have had undefined results. |
| 3806 | */ |
| 3807 | |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3808 | switch (target) { |
| 3809 | case GL_TRANSFORM_FEEDBACK_BUFFER: |
Martin Peres | a5d165a | 2015-01-20 16:30:32 +0200 | [diff] [blame] | 3810 | _mesa_bind_buffer_base_transform_feedback(ctx, |
| 3811 | ctx->TransformFeedback.CurrentObject, |
| 3812 | index, bufObj, false); |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3813 | return; |
Eric Anholt | fb76ddc | 2012-06-14 19:48:02 -0700 | [diff] [blame] | 3814 | case GL_UNIFORM_BUFFER: |
| 3815 | bind_buffer_base_uniform_buffer(ctx, index, bufObj); |
| 3816 | return; |
Iago Toral Quiroga | 8a1d58b | 2015-03-19 11:37:43 +0100 | [diff] [blame] | 3817 | case GL_SHADER_STORAGE_BUFFER: |
| 3818 | bind_buffer_base_shader_storage_buffer(ctx, index, bufObj); |
| 3819 | return; |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 3820 | case GL_ATOMIC_COUNTER_BUFFER: |
Fredrik Höglund | 19f7eeb | 2013-11-15 19:45:43 +0100 | [diff] [blame] | 3821 | bind_atomic_buffer(ctx, index, bufObj, 0, 0, |
| 3822 | "glBindBufferBase"); |
Francisco Jerez | 1c7dcfe | 2013-10-07 18:53:40 -0700 | [diff] [blame] | 3823 | return; |
Eric Anholt | b82c472 | 2012-06-14 16:55:56 -0700 | [diff] [blame] | 3824 | default: |
| 3825 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)"); |
| 3826 | return; |
| 3827 | } |
| 3828 | } |
| 3829 | |
Paul Berry | f7fa946 | 2012-10-19 09:47:11 -0700 | [diff] [blame] | 3830 | void GLAPIENTRY |
Fredrik Höglund | 6655e70 | 2013-11-13 19:02:10 +0100 | [diff] [blame] | 3831 | _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count, |
| 3832 | const GLuint *buffers, |
| 3833 | const GLintptr *offsets, const GLsizeiptr *sizes) |
| 3834 | { |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3835 | GET_CURRENT_CONTEXT(ctx); |
| 3836 | |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 3837 | if (MESA_VERBOSE & VERBOSE_API) { |
| 3838 | _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n", |
| 3839 | _mesa_enum_to_string(target), first, count, |
| 3840 | buffers, offsets, sizes); |
| 3841 | } |
| 3842 | |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3843 | switch (target) { |
| 3844 | case GL_TRANSFORM_FEEDBACK_BUFFER: |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3845 | bind_xfb_buffers(ctx, first, count, buffers, true, offsets, sizes, |
| 3846 | "glBindBuffersRange"); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3847 | return; |
| 3848 | case GL_UNIFORM_BUFFER: |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3849 | bind_uniform_buffers(ctx, first, count, buffers, true, offsets, sizes, |
| 3850 | "glBindBuffersRange"); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3851 | return; |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3852 | case GL_SHADER_STORAGE_BUFFER: |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3853 | bind_shader_storage_buffers(ctx, first, count, buffers, true, offsets, sizes, |
| 3854 | "glBindBuffersRange"); |
Iago Toral Quiroga | 7b0d0a2 | 2015-03-19 11:21:52 +0100 | [diff] [blame] | 3855 | return; |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3856 | case GL_ATOMIC_COUNTER_BUFFER: |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3857 | bind_atomic_buffers(ctx, first, count, buffers, true, offsets, sizes, |
| 3858 | "glBindBuffersRange"); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3859 | return; |
| 3860 | default: |
| 3861 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)", |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 3862 | _mesa_enum_to_string(target)); |
Fredrik Höglund | f0c36cf | 2013-11-15 19:57:41 +0100 | [diff] [blame] | 3863 | break; |
| 3864 | } |
Fredrik Höglund | 6655e70 | 2013-11-13 19:02:10 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | void GLAPIENTRY |
| 3868 | _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count, |
| 3869 | const GLuint *buffers) |
| 3870 | { |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3871 | GET_CURRENT_CONTEXT(ctx); |
| 3872 | |
Jordan Justen | 36db91c | 2015-12-29 14:24:09 -0800 | [diff] [blame] | 3873 | if (MESA_VERBOSE & VERBOSE_API) { |
| 3874 | _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n", |
| 3875 | _mesa_enum_to_string(target), first, count, buffers); |
| 3876 | } |
| 3877 | |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3878 | switch (target) { |
| 3879 | case GL_TRANSFORM_FEEDBACK_BUFFER: |
Nicolai Hähnle | b3ca26c | 2016-01-06 15:47:01 -0500 | [diff] [blame] | 3880 | bind_xfb_buffers(ctx, first, count, buffers, false, NULL, NULL, |
| 3881 | "glBindBuffersBase"); |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3882 | return; |
| 3883 | case GL_UNIFORM_BUFFER: |
Nicolai Hähnle | e8dd7cc | 2016-01-06 17:20:57 -0500 | [diff] [blame] | 3884 | bind_uniform_buffers(ctx, first, count, buffers, false, NULL, NULL, |
| 3885 | "glBindBuffersBase"); |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3886 | return; |
Iago Toral Quiroga | 0aa83f3 | 2015-03-19 10:47:17 +0100 | [diff] [blame] | 3887 | case GL_SHADER_STORAGE_BUFFER: |
Nicolai Hähnle | 5eb104d | 2016-01-06 17:26:14 -0500 | [diff] [blame] | 3888 | bind_shader_storage_buffers(ctx, first, count, buffers, false, NULL, NULL, |
| 3889 | "glBindBuffersBase"); |
Iago Toral Quiroga | 0aa83f3 | 2015-03-19 10:47:17 +0100 | [diff] [blame] | 3890 | return; |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3891 | case GL_ATOMIC_COUNTER_BUFFER: |
Nicolai Hähnle | da5d458 | 2016-01-06 17:30:18 -0500 | [diff] [blame] | 3892 | bind_atomic_buffers(ctx, first, count, buffers, false, NULL, NULL, |
| 3893 | "glBindBuffersBase"); |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3894 | return; |
| 3895 | default: |
| 3896 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)", |
Kenneth Graunke | 2f11e92 | 2015-07-18 01:22:00 -0700 | [diff] [blame] | 3897 | _mesa_enum_to_string(target)); |
Fredrik Höglund | 533cfa0 | 2013-11-15 19:56:07 +0100 | [diff] [blame] | 3898 | break; |
| 3899 | } |
Fredrik Höglund | 6655e70 | 2013-11-13 19:02:10 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | void GLAPIENTRY |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3903 | _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset, |
| 3904 | GLsizeiptr length) |
| 3905 | { |
| 3906 | GET_CURRENT_CONTEXT(ctx); |
| 3907 | struct gl_buffer_object *bufObj; |
| 3908 | const GLintptr end = offset + length; |
| 3909 | |
Nicolai Hähnle | 53c7749 | 2016-01-09 17:51:39 -0500 | [diff] [blame] | 3910 | /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility |
| 3911 | * Profile) spec says: |
| 3912 | * |
| 3913 | * "An INVALID_VALUE error is generated if buffer is zero or is not the |
| 3914 | * name of an existing buffer object." |
| 3915 | */ |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3916 | bufObj = _mesa_lookup_bufferobj(ctx, buffer); |
Nicolai Hähnle | 53c7749 | 2016-01-09 17:51:39 -0500 | [diff] [blame] | 3917 | if (!bufObj || bufObj == &DummyBufferObject) { |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3918 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3919 | "glInvalidateBufferSubData(name = 0x%x) invalid object", |
| 3920 | buffer); |
| 3921 | return; |
| 3922 | } |
| 3923 | |
| 3924 | /* The GL_ARB_invalidate_subdata spec says: |
| 3925 | * |
| 3926 | * "An INVALID_VALUE error is generated if <offset> or <length> is |
| 3927 | * negative, or if <offset> + <length> is greater than the value of |
| 3928 | * BUFFER_SIZE." |
| 3929 | */ |
Nicolai Hähnle | 53c7749 | 2016-01-09 17:51:39 -0500 | [diff] [blame] | 3930 | if (offset < 0 || length < 0 || end > bufObj->Size) { |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3931 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3932 | "glInvalidateBufferSubData(invalid offset or length)"); |
| 3933 | return; |
| 3934 | } |
| 3935 | |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 3936 | /* The OpenGL 4.4 (Core Profile) spec says: |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3937 | * |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 3938 | * "An INVALID_OPERATION error is generated if buffer is currently |
| 3939 | * mapped by MapBuffer or if the invalidate range intersects the range |
| 3940 | * currently mapped by MapBufferRange, unless it was mapped |
| 3941 | * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags." |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3942 | */ |
Marek Olšák | dca3502 | 2014-02-06 19:24:23 +0100 | [diff] [blame] | 3943 | if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) && |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 3944 | bufferobj_range_mapped(bufObj, offset, length)) { |
Pi Tabred | 84c4ea5 | 2013-12-14 10:32:00 -0700 | [diff] [blame] | 3945 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3946 | "glInvalidateBufferSubData(intersection with mapped " |
| 3947 | "range)"); |
| 3948 | return; |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Nicolai Hähnle | 6f4ae81 | 2016-01-09 17:53:07 -0500 | [diff] [blame] | 3951 | if (ctx->Driver.InvalidateBufferSubData) |
| 3952 | ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length); |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3953 | } |
| 3954 | |
Paul Berry | f7fa946 | 2012-10-19 09:47:11 -0700 | [diff] [blame] | 3955 | void GLAPIENTRY |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3956 | _mesa_InvalidateBufferData(GLuint buffer) |
| 3957 | { |
| 3958 | GET_CURRENT_CONTEXT(ctx); |
| 3959 | struct gl_buffer_object *bufObj; |
| 3960 | |
Nicolai Hähnle | 53c7749 | 2016-01-09 17:51:39 -0500 | [diff] [blame] | 3961 | /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility |
| 3962 | * Profile) spec says: |
| 3963 | * |
| 3964 | * "An INVALID_VALUE error is generated if buffer is zero or is not the |
| 3965 | * name of an existing buffer object." |
| 3966 | */ |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3967 | bufObj = _mesa_lookup_bufferobj(ctx, buffer); |
Nicolai Hähnle | 53c7749 | 2016-01-09 17:51:39 -0500 | [diff] [blame] | 3968 | if (!bufObj || bufObj == &DummyBufferObject) { |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3969 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 3970 | "glInvalidateBufferData(name = 0x%x) invalid object", |
| 3971 | buffer); |
| 3972 | return; |
| 3973 | } |
| 3974 | |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 3975 | /* The OpenGL 4.4 (Core Profile) spec says: |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3976 | * |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 3977 | * "An INVALID_OPERATION error is generated if buffer is currently |
| 3978 | * mapped by MapBuffer or if the invalidate range intersects the range |
| 3979 | * currently mapped by MapBufferRange, unless it was mapped |
| 3980 | * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags." |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3981 | */ |
Marek Olšák | d26a065 | 2014-01-27 21:36:53 +0100 | [diff] [blame] | 3982 | if (_mesa_check_disallowed_mapping(bufObj)) { |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3983 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 3984 | "glInvalidateBufferData(intersection with mapped " |
| 3985 | "range)"); |
| 3986 | return; |
| 3987 | } |
| 3988 | |
Nicolai Hähnle | 6f4ae81 | 2016-01-09 17:53:07 -0500 | [diff] [blame] | 3989 | if (ctx->Driver.InvalidateBufferSubData) |
| 3990 | ctx->Driver.InvalidateBufferSubData(ctx, bufObj, 0, bufObj->Size); |
Ian Romanick | f241ffd | 2012-08-13 10:27:33 -0700 | [diff] [blame] | 3991 | } |