0694b48aeb1578477b79c6401e415d412d4006f0
2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "main/core.h"
32 #include "glsl_symbol_table.h"
34 #include "shaderobj.h"
35 #include "program/hash_table.h"
36 #include "../glsl/program.h"
39 #include "shaderapi.h"
43 _mesa_BindAttribLocationARB(GLhandleARB program
, GLuint index
,
44 const GLcharARB
*name
)
46 GET_CURRENT_CONTEXT(ctx
);
48 struct gl_shader_program
*const shProg
=
49 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
56 if (strncmp(name
, "gl_", 3) == 0) {
57 _mesa_error(ctx
, GL_INVALID_OPERATION
,
58 "glBindAttribLocation(illegal name)");
62 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
63 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
67 /* Replace the current value if it's already in the list. Add
68 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
69 * between built-in attributes and user-defined attributes.
71 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
74 * Note that this attribute binding won't go into effect until
75 * glLinkProgram is called again.
80 _mesa_GetActiveAttribARB(GLhandleARB program
, GLuint desired_index
,
81 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
82 GLenum
* type
, GLcharARB
* name
)
84 GET_CURRENT_CONTEXT(ctx
);
85 struct gl_shader_program
*shProg
;
87 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
91 if (!shProg
->LinkStatus
) {
92 _mesa_error(ctx
, GL_INVALID_VALUE
,
93 "glGetActiveAttrib(program not linked)");
97 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
98 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
102 exec_list
*const ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
103 unsigned current_index
= 0;
105 foreach_list(node
, ir
) {
106 const ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
109 || var
->mode
!= ir_var_in
110 || var
->location
== -1
111 || var
->location
< VERT_ATTRIB_GENERIC0
)
114 if (current_index
== desired_index
) {
115 _mesa_copy_string(name
, maxLength
, length
, var
->name
);
118 *size
= (var
->type
->is_array()) ? var
->type
->length
: 1;
121 *type
= var
->type
->gl_type
;
129 /* If the loop did not return early, the caller must have asked for
130 * an index that did not exit. Set an error.
132 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
136 _mesa_GetAttribLocationARB(GLhandleARB program
, const GLcharARB
* name
)
138 GET_CURRENT_CONTEXT(ctx
);
139 struct gl_shader_program
*const shProg
=
140 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
146 if (!shProg
->LinkStatus
) {
147 _mesa_error(ctx
, GL_INVALID_OPERATION
,
148 "glGetAttribLocation(program not linked)");
155 /* Not having a vertex shader is not an error.
157 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
160 exec_list
*ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
161 foreach_list(node
, ir
) {
162 const ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
164 /* The extra check against VERT_ATTRIB_GENERIC0 is because
165 * glGetAttribLocation cannot be used on "conventional" attributes.
167 * From page 95 of the OpenGL 3.0 spec:
169 * "If name is not an active attribute, if name is a conventional
170 * attribute, or if an error occurs, -1 will be returned."
173 || var
->mode
!= ir_var_in
174 || var
->location
== -1
175 || var
->location
< VERT_ATTRIB_GENERIC0
)
178 if (strcmp(var
->name
, name
) == 0)
179 return var
->location
- VERT_ATTRIB_GENERIC0
;
187 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
189 if (!shProg
->LinkStatus
190 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
194 exec_list
*const ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
197 foreach_list(node
, ir
) {
198 const ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
201 || var
->mode
!= ir_var_in
202 || var
->location
== -1
203 || var
->location
< VERT_ATTRIB_GENERIC0
)
214 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
216 if (!shProg
->LinkStatus
217 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
221 exec_list
*const ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
224 foreach_list(node
, ir
) {
225 const ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
228 || var
->mode
!= ir_var_in
229 || var
->location
== -1
230 || var
->location
< VERT_ATTRIB_GENERIC0
)
233 const size_t len
= strlen(var
->name
);
242 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
245 GET_CURRENT_CONTEXT(ctx
);
247 struct gl_shader_program
*const shProg
=
248 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocation");
255 if (strncmp(name
, "gl_", 3) == 0) {
256 _mesa_error(ctx
, GL_INVALID_OPERATION
,
257 "glBindFragDataLocation(illegal name)");
261 if (colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
262 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocation(index)");
266 /* Replace the current value if it's already in the list. Add
267 * FRAG_RESULT_DATA0 because that's how the linker differentiates
268 * between built-in attributes and user-defined attributes.
273 * Note that this binding won't go into effect until
274 * glLinkProgram is called again.