2 * Mesa 3-D graphics library
4 * Copyright (C) 2013 Timothy Arceri All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "bufferobj.h"
32 #include "objectlabel.h"
34 #include "samplerobj.h"
35 #include "shaderobj.h"
38 #include "transformfeedback.h"
42 * Helper for _mesa_ObjectLabel() and _mesa_ObjectPtrLabel().
45 set_label(struct gl_context *ctx, char **labelPtr, const char *label,
46 int length, const char *caller)
49 /* free old label string */
54 /* set new label string */
57 if (length >= MAX_LABEL_LENGTH)
58 _mesa_error(ctx, GL_INVALID_VALUE,
59 "%s(length=%d, which is not less than "
60 "GL_MAX_LABEL_LENGTH=%d)", caller, length,
64 *labelPtr = (char *) malloc(length+1);
66 memcpy(*labelPtr, label, length);
67 /* length is not required to include the null terminator so
68 * add one just in case
70 (*labelPtr)[length] = '\0';
74 int len = strlen(label);
75 if (len >= MAX_LABEL_LENGTH)
76 _mesa_error(ctx, GL_INVALID_VALUE,
77 "%s(label length=%d, which is not less than "
78 "GL_MAX_LABEL_LENGTH=%d)", caller, len,
81 /* null-terminated string */
82 *labelPtr = _mesa_strdup(label);
88 * Helper for _mesa_GetObjectLabel() and _mesa_GetObjectPtrLabel().
89 * \param src the src label (may be null)
90 * \param dst pointer to dest buffer (may be null)
91 * \param length returns length of label (may be null)
92 * \param bufsize size of dst buffer
95 copy_label(const GLchar *src, GLchar *dst, GLsizei *length, GLsizei bufSize)
99 /* From http://www.opengl.org/registry/specs/KHR/debug.txt:
100 * "If <length> is NULL, no length is returned. The maximum number of
101 * characters that may be written into <label>, including the null
102 * terminator, is specified by <bufSize>. If no debug label was specified
103 * for the object then <label> will contain a null-terminated empty string,
104 * and zero will be returned in <length>. If <label> is NULL and <length>
105 * is non-NULL then no string will be returned and the length of the label
106 * will be returned in <length>."
110 labelLen = strlen(src);
114 if (bufSize <= labelLen)
115 labelLen = bufSize - 1;
117 memcpy(dst, src, labelLen);
120 dst[labelLen] = '\0';
128 * Helper for _mesa_ObjectLabel() and _mesa_GetObjectLabel().
131 get_label_pointer(struct gl_context *ctx, GLenum identifier, GLuint name,
134 char **labelPtr = NULL;
136 switch (identifier) {
139 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
141 labelPtr = &bufObj->Label;
146 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
148 labelPtr = &shader->Label;
153 struct gl_shader_program *program =
154 _mesa_lookup_shader_program(ctx, name);
156 labelPtr = &program->Label;
159 case GL_VERTEX_ARRAY:
161 struct gl_array_object *obj = _mesa_lookup_arrayobj(ctx, name);
163 labelPtr = &obj->Label;
168 struct gl_query_object *query = _mesa_lookup_query_object(ctx, name);
170 labelPtr = &query->Label;
173 case GL_TRANSFORM_FEEDBACK:
175 struct gl_transform_feedback_object *tfo =
176 _mesa_lookup_transform_feedback_object(ctx, name);
178 labelPtr = &tfo->Label;
183 struct gl_sampler_object *so = _mesa_lookup_samplerobj(ctx, name);
185 labelPtr = &so->Label;
190 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
192 labelPtr = &texObj->Label;
195 case GL_RENDERBUFFER:
197 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
199 labelPtr = &rb->Label;
204 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, name);
206 labelPtr = &rb->Label;
209 case GL_DISPLAY_LIST:
210 if (ctx->API == API_OPENGL_COMPAT) {
211 struct gl_display_list *list = _mesa_lookup_list(ctx, name);
213 labelPtr = &list->Label;
219 case GL_PROGRAM_PIPELINE:
220 /* requires GL 4.2 */
226 if (NULL == labelPtr) {
227 _mesa_error(ctx, GL_INVALID_VALUE, "%s(name = %u)", caller, name);
233 _mesa_error(ctx, GL_INVALID_ENUM, "%s(identifier = %s)",
234 caller, _mesa_lookup_enum_by_nr(identifier));
239 _mesa_ObjectLabel(GLenum identifier, GLuint name, GLsizei length,
242 GET_CURRENT_CONTEXT(ctx);
245 labelPtr = get_label_pointer(ctx, identifier, name, "glObjectLabel");
249 set_label(ctx, labelPtr, label, length, "glObjectLabel");
253 _mesa_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize,
254 GLsizei *length, GLchar *label)
256 GET_CURRENT_CONTEXT(ctx);
260 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectLabel(bufSize = %d)",
265 labelPtr = get_label_pointer(ctx, identifier, name, "glGetObjectLabel");
269 copy_label(*labelPtr, label, length, bufSize);
273 _mesa_ObjectPtrLabel(const void *ptr, GLsizei length, const GLchar *label)
275 GET_CURRENT_CONTEXT(ctx);
277 struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
279 if (!_mesa_validate_sync(ctx, syncObj)) {
280 _mesa_error(ctx, GL_INVALID_VALUE, "glObjectPtrLabel (not a valid sync object)");
284 labelPtr = &syncObj->Label;
286 set_label(ctx, labelPtr, label, length, "glObjectPtrLabel");
290 _mesa_GetObjectPtrLabel(const void *ptr, GLsizei bufSize, GLsizei *length,
293 GET_CURRENT_CONTEXT(ctx);
295 struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
298 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectPtrLabel(bufSize = %d)",
303 if (!_mesa_validate_sync(ctx, syncObj)) {
304 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectPtrLabel (not a valid sync object)");
308 labelPtr = &syncObj->Label;
310 copy_label(*labelPtr, label, length, bufSize);