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_BindAttribLocation(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
.Program
[MESA_SHADER_VERTEX
].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 is_active_attrib(const ir_variable
*var
)
85 switch (var
->data
.mode
) {
86 case ir_var_shader_in
:
87 return var
->data
.location
!= -1;
89 case ir_var_system_value
:
90 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
91 * "For GetActiveAttrib, all active vertex shader input variables
92 * are enumerated, including the special built-in inputs gl_VertexID
95 return var
->data
.location
== SYSTEM_VALUE_VERTEX_ID
||
96 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
||
97 var
->data
.location
== SYSTEM_VALUE_INSTANCE_ID
;
105 _mesa_GetActiveAttrib(GLhandleARB program
, GLuint desired_index
,
106 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
107 GLenum
* type
, GLcharARB
* name
)
109 GET_CURRENT_CONTEXT(ctx
);
110 struct gl_shader_program
*shProg
;
112 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
116 if (!shProg
->LinkStatus
) {
117 _mesa_error(ctx
, GL_INVALID_VALUE
,
118 "glGetActiveAttrib(program not linked)");
122 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
123 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
127 exec_list
*const ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
128 unsigned current_index
= 0;
130 foreach_in_list(ir_instruction
, node
, ir
) {
131 const ir_variable
*const var
= node
->as_variable();
133 if (!is_active_attrib(var
))
136 if (current_index
== desired_index
) {
137 const char *var_name
= var
->name
;
139 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
140 * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
143 if (var
->data
.mode
== ir_var_system_value
&&
144 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
145 var_name
= "gl_VertexID";
148 _mesa_copy_string(name
, maxLength
, length
, var_name
);
151 *size
= (var
->type
->is_array()) ? var
->type
->length
: 1;
154 *type
= var
->type
->gl_type
;
162 /* If the loop did not return early, the caller must have asked for
163 * an index that did not exit. Set an error.
165 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
168 /* Locations associated with shader variables (array or non-array) can be
169 * queried using its base name or using the base name appended with the
170 * valid array index. For example, in case of below vertex shader, valid
171 * queries can be made to know the location of "xyz", "array", "array[0]",
172 * "array[1]", "array[2]" and "array[3]". In this example index reurned
173 * will be 0, 0, 0, 1, 2, 3 respectively.
176 * layout(location=0) in vec4 xyz;
177 * layout(location=1) in vec4[4] array;
181 * This requirement came up with the addition of ARB_program_interface_query
182 * to OpenGL 4.3 specification. See page 101 (page 122 of the PDF) for details.
184 * This utility function is used by:
185 * _mesa_GetAttribLocation
186 * _mesa_GetFragDataLocation
187 * _mesa_GetFragDataIndex
190 * if the 'name' string matches var->name.
191 * Returns 'matched index':
192 * if the 'name' string matches var->name appended with valid array index.
195 get_matching_index(const ir_variable
*const var
, const char *name
) {
197 const char *const paren
= strchr(name
, '[');
198 const unsigned len
= (paren
!= NULL
) ? paren
- name
: strlen(name
);
201 if (!var
->type
->is_array())
205 idx
= (unsigned) strtol(paren
+ 1, &endptr
, 10);
206 const unsigned idx_len
= endptr
!= (paren
+ 1) ? endptr
- paren
- 1 : 0;
208 /* Validate the sub string representing index in 'name' string */
209 if ((idx
> 0 && paren
[1] == '0') /* leading zeroes */
210 || (idx
== 0 && idx_len
> 1) /* all zeroes */
211 || paren
[1] == ' ' /* whitespace */
212 || endptr
[0] != ']' /* closing brace */
213 || endptr
[1] != '\0' /* null char */
214 || idx_len
== 0 /* missing index */
215 || idx
>= var
->type
->length
) /* exceeding array bound */
219 if (strncmp(var
->name
, name
, len
) == 0 && var
->name
[len
] == '\0')
226 _mesa_GetAttribLocation(GLhandleARB program
, const GLcharARB
* name
)
228 GET_CURRENT_CONTEXT(ctx
);
229 struct gl_shader_program
*const shProg
=
230 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
236 if (!shProg
->LinkStatus
) {
237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
238 "glGetAttribLocation(program not linked)");
245 /* Not having a vertex shader is not an error.
247 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
250 exec_list
*ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
251 foreach_in_list(ir_instruction
, node
, ir
) {
252 const ir_variable
*const var
= node
->as_variable();
254 /* The extra check against VERT_ATTRIB_GENERIC0 is because
255 * glGetAttribLocation cannot be used on "conventional" attributes.
257 * From page 95 of the OpenGL 3.0 spec:
259 * "If name is not an active attribute, if name is a conventional
260 * attribute, or if an error occurs, -1 will be returned."
263 || var
->data
.mode
!= ir_var_shader_in
264 || var
->data
.location
== -1
265 || var
->data
.location
< VERT_ATTRIB_GENERIC0
)
268 int index
= get_matching_index(var
, (const char *) name
);
271 return var
->data
.location
+ index
- VERT_ATTRIB_GENERIC0
;
279 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
281 if (!shProg
->LinkStatus
282 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
286 exec_list
*const ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
289 foreach_in_list(ir_instruction
, node
, ir
) {
290 const ir_variable
*const var
= node
->as_variable();
292 if (!is_active_attrib(var
))
303 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
305 if (!shProg
->LinkStatus
306 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
310 exec_list
*const ir
= shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->ir
;
313 foreach_in_list(ir_instruction
, node
, ir
) {
314 const ir_variable
*const var
= node
->as_variable();
317 || var
->data
.mode
!= ir_var_shader_in
318 || var
->data
.location
== -1)
321 const size_t len
= strlen(var
->name
);
330 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
333 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
337 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
338 GLuint index
, const GLchar
*name
)
340 GET_CURRENT_CONTEXT(ctx
);
342 struct gl_shader_program
*const shProg
=
343 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
350 if (strncmp(name
, "gl_", 3) == 0) {
351 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
356 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
360 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
361 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
365 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
366 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
370 /* Replace the current value if it's already in the list. Add
371 * FRAG_RESULT_DATA0 because that's how the linker differentiates
372 * between built-in attributes and user-defined attributes.
374 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
375 shProg
->FragDataIndexBindings
->put(index
, name
);
377 * Note that this binding won't go into effect until
378 * glLinkProgram is called again.
384 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
386 GET_CURRENT_CONTEXT(ctx
);
387 struct gl_shader_program
*const shProg
=
388 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
394 if (!shProg
->LinkStatus
) {
395 _mesa_error(ctx
, GL_INVALID_OPERATION
,
396 "glGetFragDataIndex(program not linked)");
403 if (strncmp(name
, "gl_", 3) == 0) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glGetFragDataIndex(illegal name)");
409 /* Not having a fragment shader is not an error.
411 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
414 exec_list
*ir
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
415 foreach_in_list(ir_instruction
, node
, ir
) {
416 const ir_variable
*const var
= node
->as_variable();
418 /* The extra check against FRAG_RESULT_DATA0 is because
419 * glGetFragDataLocation cannot be used on "conventional" attributes.
421 * From page 95 of the OpenGL 3.0 spec:
423 * "If name is not an active attribute, if name is a conventional
424 * attribute, or if an error occurs, -1 will be returned."
427 || var
->data
.mode
!= ir_var_shader_out
428 || var
->data
.location
== -1
429 || var
->data
.location
< FRAG_RESULT_DATA0
)
432 if (get_matching_index(var
, (const char *) name
) >= 0)
433 return var
->data
.index
;
440 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
442 GET_CURRENT_CONTEXT(ctx
);
443 struct gl_shader_program
*const shProg
=
444 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
450 if (!shProg
->LinkStatus
) {
451 _mesa_error(ctx
, GL_INVALID_OPERATION
,
452 "glGetFragDataLocation(program not linked)");
459 if (strncmp(name
, "gl_", 3) == 0) {
460 _mesa_error(ctx
, GL_INVALID_OPERATION
,
461 "glGetFragDataLocation(illegal name)");
465 /* Not having a fragment shader is not an error.
467 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
470 exec_list
*ir
= shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->ir
;
471 foreach_in_list(ir_instruction
, node
, ir
) {
472 const ir_variable
*const var
= node
->as_variable();
474 /* The extra check against FRAG_RESULT_DATA0 is because
475 * glGetFragDataLocation cannot be used on "conventional" attributes.
477 * From page 95 of the OpenGL 3.0 spec:
479 * "If name is not an active attribute, if name is a conventional
480 * attribute, or if an error occurs, -1 will be returned."
483 || var
->data
.mode
!= ir_var_shader_out
484 || var
->data
.location
== -1
485 || var
->data
.location
< FRAG_RESULT_DATA0
)
488 int index
= get_matching_index(var
, (const char *) name
);
491 return var
->data
.location
+ index
- FRAG_RESULT_DATA0
;