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