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)
51 /* set new label string */
54 if (length >= MAX_LABEL_LENGTH)
55 _mesa_error(ctx, GL_INVALID_VALUE,
56 "%s(length=%d, which is not less than "
57 "GL_MAX_LABEL_LENGTH=%d)", caller, length,
61 *labelPtr = malloc(length+1);
63 memcpy(*labelPtr, label, length);
64 /* length is not required to include the null terminator so
65 * add one just in case
67 (*labelPtr)[length] = '\0';
71 int len = strlen(label);
72 if (len >= MAX_LABEL_LENGTH)
73 _mesa_error(ctx, GL_INVALID_VALUE,
74 "%s(label length=%d, which is not less than "
75 "GL_MAX_LABEL_LENGTH=%d)", caller, len,
78 /* null-terminated string */
79 *labelPtr = _mesa_strdup(label);
85 * Helper for _mesa_GetObjectLabel() and _mesa_GetObjectPtrLabel().
86 * \param src the src label (may be null)
87 * \param dst pointer to dest buffer (may be null)
88 * \param length returns length of label (may be null)
89 * \param bufsize size of dst buffer
92 copy_label(const GLchar *src, GLchar *dst, GLsizei *length, GLsizei bufSize)
96 /* From http://www.opengl.org/registry/specs/KHR/debug.txt:
97 * "If <length> is NULL, no length is returned. The maximum number of
98 * characters that may be written into <label>, including the null
99 * terminator, is specified by <bufSize>. If no debug label was specified
100 * for the object then <label> will contain a null-terminated empty string,
101 * and zero will be returned in <length>. If <label> is NULL and <length>
102 * is non-NULL then no string will be returned and the length of the label
103 * will be returned in <length>."
107 labelLen = strlen(src);
111 if (bufSize <= labelLen)
112 labelLen = bufSize - 1;
114 memcpy(dst, src, labelLen);
117 dst[labelLen] = '\0';
125 * Helper for _mesa_ObjectLabel() and _mesa_GetObjectLabel().
128 get_label_pointer(struct gl_context *ctx, GLenum identifier, GLuint name,
131 char **labelPtr = NULL;
133 switch (identifier) {
136 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
138 labelPtr = &bufObj->Label;
143 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
145 labelPtr = &shader->Label;
150 struct gl_shader_program *program =
151 _mesa_lookup_shader_program(ctx, name);
153 labelPtr = &program->Label;
156 case GL_VERTEX_ARRAY:
158 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, name);
160 labelPtr = &obj->Label;
165 struct gl_query_object *query = _mesa_lookup_query_object(ctx, name);
167 labelPtr = &query->Label;
170 case GL_TRANSFORM_FEEDBACK:
172 struct gl_transform_feedback_object *tfo =
173 _mesa_lookup_transform_feedback_object(ctx, name);
175 labelPtr = &tfo->Label;
180 struct gl_sampler_object *so = _mesa_lookup_samplerobj(ctx, name);
182 labelPtr = &so->Label;
187 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
189 labelPtr = &texObj->Label;
192 case GL_RENDERBUFFER:
194 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
196 labelPtr = &rb->Label;
201 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, name);
203 labelPtr = &rb->Label;
206 case GL_DISPLAY_LIST:
207 if (ctx->API == API_OPENGL_COMPAT) {
208 struct gl_display_list *list = _mesa_lookup_list(ctx, name);
210 labelPtr = &list->Label;
216 case GL_PROGRAM_PIPELINE:
217 /* requires GL 4.2 */
223 if (NULL == labelPtr) {
224 _mesa_error(ctx, GL_INVALID_VALUE, "%s(name = %u)", caller, name);
230 _mesa_error(ctx, GL_INVALID_ENUM, "%s(identifier = %s)",
231 caller, _mesa_lookup_enum_by_nr(identifier));
236 _mesa_ObjectLabel(GLenum identifier, GLuint name, GLsizei length,
239 GET_CURRENT_CONTEXT(ctx);
242 labelPtr = get_label_pointer(ctx, identifier, name, "glObjectLabel");
246 set_label(ctx, labelPtr, label, length, "glObjectLabel");
250 _mesa_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize,
251 GLsizei *length, GLchar *label)
253 GET_CURRENT_CONTEXT(ctx);
257 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectLabel(bufSize = %d)",
262 labelPtr = get_label_pointer(ctx, identifier, name, "glGetObjectLabel");
266 copy_label(*labelPtr, label, length, bufSize);
270 _mesa_ObjectPtrLabel(const void *ptr, GLsizei length, const GLchar *label)
272 GET_CURRENT_CONTEXT(ctx);
274 struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
276 if (!_mesa_validate_sync(ctx, syncObj)) {
277 _mesa_error(ctx, GL_INVALID_VALUE, "glObjectPtrLabel (not a valid sync object)");
281 labelPtr = &syncObj->Label;
283 set_label(ctx, labelPtr, label, length, "glObjectPtrLabel");
287 _mesa_GetObjectPtrLabel(const void *ptr, GLsizei bufSize, GLsizei *length,
290 GET_CURRENT_CONTEXT(ctx);
292 struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
295 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectPtrLabel(bufSize = %d)",
300 if (!_mesa_validate_sync(ctx, syncObj)) {
301 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectPtrLabel (not a valid sync object)");
305 labelPtr = &syncObj->Label;
307 copy_label(*labelPtr, label, length, bufSize);