mesa: Move _mesa_GetAttribLocationARB to shader_query.cpp
[mesa.git] / src / mesa / main / shader_query.cpp
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30
31 #include "main/core.h"
32 #include "glsl_symbol_table.h"
33 #include "ir.h"
34 #include "shaderobj.h"
35
36 extern "C" {
37 #include "shaderapi.h"
38 }
39
40 GLint GLAPIENTRY
41 _mesa_GetAttribLocationARB(GLhandleARB program, const GLcharARB * name)
42 {
43 GET_CURRENT_CONTEXT(ctx);
44 struct gl_shader_program *const shProg =
45 _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
46
47 if (!shProg) {
48 return -1;
49 }
50
51 if (!shProg->LinkStatus) {
52 _mesa_error(ctx, GL_INVALID_OPERATION,
53 "glGetAttribLocation(program not linked)");
54 return -1;
55 }
56
57 if (!name)
58 return -1;
59
60 /* Not having a vertex shader is not an error.
61 */
62 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
63 return -1;
64
65 exec_list *ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
66 foreach_list(node, ir) {
67 const ir_variable *const var = ((ir_instruction *) node)->as_variable();
68
69 /* The extra check against VERT_ATTRIB_GENERIC0 is because
70 * glGetAttribLocation cannot be used on "conventional" attributes.
71 *
72 * From page 95 of the OpenGL 3.0 spec:
73 *
74 * "If name is not an active attribute, if name is a conventional
75 * attribute, or if an error occurs, -1 will be returned."
76 */
77 if (var == NULL
78 || var->mode != ir_var_in
79 || var->location == -1
80 || var->location < VERT_ATTRIB_GENERIC0)
81 continue;
82
83 if (strcmp(var->name, name) == 0)
84 return var->location - VERT_ATTRIB_GENERIC0;
85 }
86
87 return -1;
88 }