*/
#include "main/context.h"
-#include "main/core.h"
#include "main/enums.h"
#include "main/shaderapi.h"
#include "main/shaderobj.h"
#include "compiler/glsl/glsl_symbol_table.h"
#include "compiler/glsl/ir.h"
#include "compiler/glsl/program.h"
-#include "program/hash_table.h"
-#include "util/strndup.h"
+#include "compiler/glsl/string_to_uint_map.h"
static GLint
-program_resource_location(struct gl_shader_program *shProg,
- struct gl_program_resource *res, const char *name,
+program_resource_location(struct gl_program_resource *res,
unsigned array_index);
/**
DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_buffer);
DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
-void GLAPIENTRY
-_mesa_BindAttribLocation(GLuint program, GLuint index,
- const GLchar *name)
+static void
+bind_attrib_location(struct gl_context *ctx,
+ struct gl_shader_program *const shProg, GLuint index,
+ const GLchar *name, bool no_error)
{
- GET_CURRENT_CONTEXT(ctx);
-
- struct gl_shader_program *const shProg =
- _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
- if (!shProg)
- return;
-
if (!name)
return;
- if (strncmp(name, "gl_", 3) == 0) {
- _mesa_error(ctx, GL_INVALID_OPERATION,
- "glBindAttribLocation(illegal name)");
- return;
- }
+ if (!no_error) {
+ if (strncmp(name, "gl_", 3) == 0) {
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "glBindAttribLocation(illegal name)");
+ return;
+ }
- if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
- _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
- return;
+ if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
+ _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(%u >= %u)",
+ index, ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs);
+ return;
+ }
}
/* Replace the current value if it's already in the list. Add
*/
}
-static bool
-is_active_attrib(const gl_shader_variable *var)
+void GLAPIENTRY
+_mesa_BindAttribLocation_no_error(GLuint program, GLuint index,
+ const GLchar *name)
{
- if (!var)
- return false;
+ GET_CURRENT_CONTEXT(ctx);
- switch (var->mode) {
- case ir_var_shader_in:
- return var->location != -1;
+ struct gl_shader_program *const shProg =
+ _mesa_lookup_shader_program(ctx, program);
+ bind_attrib_location(ctx, shProg, index, name, true);
+}
- case ir_var_system_value:
- /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
- * "For GetActiveAttrib, all active vertex shader input variables
- * are enumerated, including the special built-in inputs gl_VertexID
- * and gl_InstanceID."
- */
- return var->location == SYSTEM_VALUE_VERTEX_ID ||
- var->location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE ||
- var->location == SYSTEM_VALUE_INSTANCE_ID;
+void GLAPIENTRY
+_mesa_BindAttribLocation(GLuint program, GLuint index,
+ const GLchar *name)
+{
+ GET_CURRENT_CONTEXT(ctx);
- default:
- return false;
- }
+ struct gl_shader_program *const shProg =
+ _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
+ if (!shProg)
+ return;
+
+ bind_attrib_location(ctx, shProg, index, name, false);
}
void GLAPIENTRY
if (!shProg)
return;
- if (!shProg->LinkStatus) {
+ if (!shProg->data->LinkStatus) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glGetActiveAttrib(program not linked)");
return;
const gl_shader_variable *const var = RESOURCE_VAR(res);
- if (!is_active_attrib(var))
- return;
-
const char *var_name = var->name;
- /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
- * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
- * active attributes.
- */
- if (var->mode == ir_var_system_value &&
- var->location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
- var_name = "gl_VertexID";
- }
-
_mesa_copy_string(name, maxLength, length, var_name);
if (size)
return -1;
}
- if (!shProg->LinkStatus) {
+ if (!shProg->data->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetAttribLocation(program not linked)");
return -1;
if (!res)
return -1;
- GLint loc = program_resource_location(shProg, res, name, array_index);
-
- /* The extra check against against 0 is made because of builtin-attribute
- * locations that have offset applied. Function program_resource_location
- * can return built-in attribute locations < 0 and glGetAttribLocation
- * cannot be used on "conventional" attributes.
- *
- * From page 95 of the OpenGL 3.0 spec:
- *
- * "If name is not an active attribute, if name is a conventional
- * attribute, or if an error occurs, -1 will be returned."
- */
- return (loc >= 0) ? loc : -1;
+ return program_resource_location(res, array_index);
}
unsigned
_mesa_count_active_attribs(struct gl_shader_program *shProg)
{
- if (!shProg->LinkStatus
+ if (!shProg->data->LinkStatus
|| shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
return 0;
}
- struct gl_program_resource *res = shProg->ProgramResourceList;
+ struct gl_program_resource *res = shProg->data->ProgramResourceList;
unsigned count = 0;
- for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
+ for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
+ j++, res++) {
if (res->Type == GL_PROGRAM_INPUT &&
- res->StageReferences & (1 << MESA_SHADER_VERTEX) &&
- is_active_attrib(RESOURCE_VAR(res)))
+ res->StageReferences & (1 << MESA_SHADER_VERTEX))
count++;
}
return count;
size_t
_mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
{
- if (!shProg->LinkStatus
+ if (!shProg->data->LinkStatus
|| shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
return 0;
}
- struct gl_program_resource *res = shProg->ProgramResourceList;
+ struct gl_program_resource *res = shProg->data->ProgramResourceList;
size_t longest = 0;
- for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
+ for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
+ j++, res++) {
if (res->Type == GL_PROGRAM_INPUT &&
res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
return longest;
}
+void static
+bind_frag_data_location(struct gl_shader_program *const shProg,
+ const char *name, unsigned colorNumber,
+ unsigned index)
+{
+ /* Replace the current value if it's already in the list. Add
+ * FRAG_RESULT_DATA0 because that's how the linker differentiates
+ * between built-in attributes and user-defined attributes.
+ */
+ shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
+ shProg->FragDataIndexBindings->put(index, name);
+
+ /*
+ * Note that this binding won't go into effect until
+ * glLinkProgram is called again.
+ */
+}
+
void GLAPIENTRY
_mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
const GLchar *name)
_mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
}
+void GLAPIENTRY
+_mesa_BindFragDataLocation_no_error(GLuint program, GLuint colorNumber,
+ const GLchar *name)
+{
+ GET_CURRENT_CONTEXT(ctx);
+
+ if (!name)
+ return;
+
+ struct gl_shader_program *const shProg =
+ _mesa_lookup_shader_program(ctx, program);
+
+ bind_frag_data_location(shProg, name, colorNumber, 0);
+}
+
void GLAPIENTRY
_mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
GLuint index, const GLchar *name)
return;
}
- /* Replace the current value if it's already in the list. Add
- * FRAG_RESULT_DATA0 because that's how the linker differentiates
- * between built-in attributes and user-defined attributes.
- */
- shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
- shProg->FragDataIndexBindings->put(index, name);
- /*
- * Note that this binding won't go into effect until
- * glLinkProgram is called again.
- */
+ bind_frag_data_location(shProg, name, colorNumber, index);
+}
+
+void GLAPIENTRY
+_mesa_BindFragDataLocationIndexed_no_error(GLuint program, GLuint colorNumber,
+ GLuint index, const GLchar *name)
+{
+ GET_CURRENT_CONTEXT(ctx);
+ if (!name)
+ return;
+
+ struct gl_shader_program *const shProg =
+ _mesa_lookup_shader_program(ctx, program);
+
+ bind_frag_data_location(shProg, name, colorNumber, index);
}
GLint GLAPIENTRY
return -1;
}
- if (!shProg->LinkStatus) {
+ if (!shProg->data->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFragDataIndex(program not linked)");
return -1;
return -1;
}
- if (!shProg->LinkStatus) {
+ if (!shProg->data->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFragDataLocation(program not linked)");
return -1;
if (!res)
return -1;
- GLint loc = program_resource_location(shProg, res, name, array_index);
-
- /* The extra check against against 0 is made because of builtin-attribute
- * locations that have offset applied. Function program_resource_location
- * can return built-in attribute locations < 0 and glGetFragDataLocation
- * cannot be used on "conventional" attributes.
- *
- * From page 95 of the OpenGL 3.0 spec:
- *
- * "If name is not an active attribute, if name is a conventional
- * attribute, or if an error occurs, -1 will be returned."
- */
- return (loc >= 0) ? loc : -1;
+ return program_resource_location(res, array_index);
}
const char*
_mesa_program_resource_name(struct gl_program_resource *res)
{
- const gl_shader_variable *var;
switch (res->Type) {
case GL_UNIFORM_BLOCK:
case GL_SHADER_STORAGE_BLOCK:
case GL_TRANSFORM_FEEDBACK_VARYING:
return RESOURCE_XFV(res)->Name;
case GL_PROGRAM_INPUT:
- var = RESOURCE_VAR(res);
- /* Special case gl_VertexIDMESA -> gl_VertexID. */
- if (var->mode == ir_var_system_value &&
- var->location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
- return "gl_VertexID";
- }
- /* fallthrough */
case GL_PROGRAM_OUTPUT:
return RESOURCE_VAR(res)->name;
case GL_UNIFORM:
GLenum programInterface, const char *name,
unsigned *array_index)
{
- struct gl_program_resource *res = shProg->ProgramResourceList;
- for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+ struct gl_program_resource *res = shProg->data->ProgramResourceList;
+ for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
+ i++, res++) {
if (res->Type != programInterface)
continue;
/* Resource basename. */
const char *rname = _mesa_program_resource_name(res);
+
+ /* Since ARB_gl_spirv lack of name reflections is a possibility */
+ if (rname == NULL)
+ continue;
+
unsigned baselen = strlen(rname);
unsigned baselen_without_array_index = baselen;
const char *rname_last_square_bracket = strrchr(rname, '[');
if (rname_last_square_bracket) {
baselen_without_array_index -= strlen(rname_last_square_bracket);
rname_has_array_index_zero =
- (strncmp(rname_last_square_bracket, "[0]\0", 4) == 0) &&
+ (strcmp(rname_last_square_bracket, "[0]") == 0) &&
(baselen_without_array_index == strlen(name));
}
{
unsigned i;
GLuint index = 0;
- for (i = 0; i < shProg->NumProgramResourceList; i++) {
- if (&shProg->ProgramResourceList[i] == res)
+ for (i = 0; i < shProg->data->NumProgramResourceList; i++) {
+ if (&shProg->data->ProgramResourceList[i] == res)
return index;
- if (shProg->ProgramResourceList[i].Type == res->Type)
+ if (shProg->data->ProgramResourceList[i].Type == res->Type)
index++;
}
return GL_INVALID_INDEX;
switch (res->Type) {
case GL_ATOMIC_COUNTER_BUFFER:
- return RESOURCE_ATC(res) - shProg->AtomicBuffers;
+ return RESOURCE_ATC(res) - shProg->data->AtomicBuffers;
case GL_VERTEX_SUBROUTINE:
case GL_GEOMETRY_SUBROUTINE:
case GL_FRAGMENT_SUBROUTINE:
static struct gl_program_resource*
program_resource_find_data(struct gl_shader_program *shProg, void *data)
{
- struct gl_program_resource *res = shProg->ProgramResourceList;
- for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+ struct gl_program_resource *res = shProg->data->ProgramResourceList;
+ for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
+ i++, res++) {
if (res->Data == data)
return res;
}
_mesa_program_resource_find_index(struct gl_shader_program *shProg,
GLenum programInterface, GLuint index)
{
- struct gl_program_resource *res = shProg->ProgramResourceList;
+ struct gl_program_resource *res = shProg->data->ProgramResourceList;
int idx = -1;
- for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+ for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
+ i++, res++) {
if (res->Type != programInterface)
continue;
* ambiguous in this regard. However, either name can later be passed
* to glGetUniformLocation (and related APIs), so there shouldn't be any
* harm in always appending "[0]" to uniform array names.
- *
- * Geometry shader stage has different naming convention where the 'normal'
- * condition is an array, therefore for variables referenced in geometry
- * stage we do not add '[0]'.
- *
- * Note, that TCS outputs and TES inputs should not have index appended
- * either.
*/
static bool
add_index_to_name(struct gl_program_resource *res)
{
- bool add_index = !((res->Type == GL_PROGRAM_INPUT &&
- res->StageReferences & (1 << MESA_SHADER_GEOMETRY |
- 1 << MESA_SHADER_TESS_CTRL |
- 1 << MESA_SHADER_TESS_EVAL)) ||
- (res->Type == GL_PROGRAM_OUTPUT &&
- res->StageReferences & 1 << MESA_SHADER_TESS_CTRL));
-
/* Transform feedback varyings have array index already appended
* in their names.
*/
- if (res->Type == GL_TRANSFORM_FEEDBACK_VARYING)
- add_index = false;
-
- return add_index;
+ return res->Type != GL_TRANSFORM_FEEDBACK_VARYING;
}
/* Get name length of a program resource. This consists of
}
static GLint
-program_resource_location(struct gl_shader_program *shProg,
- struct gl_program_resource *res, const char *name,
- unsigned array_index)
+program_resource_location(struct gl_program_resource *res, unsigned array_index)
{
- /* Built-in locations should report GL_INVALID_INDEX. */
- if (is_gl_identifier(name))
- return GL_INVALID_INDEX;
-
- /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
- * offsets are used internally to differentiate between built-in attributes
- * and user-defined attributes.
- */
switch (res->Type) {
case GL_PROGRAM_INPUT: {
const gl_shader_variable *var = RESOURCE_VAR(res);
+ if (var->location == -1)
+ return -1;
+
/* If the input is an array, fail if the index is out of bounds. */
if (array_index > 0
&& array_index >= var->type->length) {
return -1;
}
- return (var->location +
- (array_index * var->type->without_array()->matrix_columns) -
- VERT_ATTRIB_GENERIC0);
+ return var->location +
+ (array_index * var->type->without_array()->matrix_columns);
}
case GL_PROGRAM_OUTPUT:
+ if (RESOURCE_VAR(res)->location == -1)
+ return -1;
+
/* If the output is an array, fail if the index is out of bounds. */
if (array_index > 0
&& array_index >= RESOURCE_VAR(res)->type->length) {
return -1;
}
- return RESOURCE_VAR(res)->location + array_index - FRAG_RESULT_DATA0;
+ return RESOURCE_VAR(res)->location + array_index;
case GL_UNIFORM:
/* If the uniform is built-in, fail. */
if (RESOURCE_UNI(res)->builtin)
if (!res)
return -1;
- return program_resource_location(shProg, res, name, array_index);
+ return program_resource_location(res, array_index);
}
/**
if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
return -1;
+ /* From OpenGL 4.5 spec, 7.3 Program Objects
+ * "The value -1 will be returned by either command...
+ * ... or if name identifies an active variable that does not have a
+ * valid location assigned.
+ */
+ if (RESOURCE_VAR(res)->location == -1)
+ return -1;
return RESOURCE_VAR(res)->index;
}
if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
return RESOURCE_ATC(res)->StageReferences[stage];
- if (res->Type == GL_UNIFORM_BLOCK || res->Type == GL_SHADER_STORAGE_BLOCK)
- return shProg->InterfaceBlockStageIndex[stage][index] != -1;
+ if (res->Type == GL_UNIFORM_BLOCK)
+ return shProg->data->UniformBlocks[index].stageref & (1 << stage);
+
+ if (res->Type == GL_SHADER_STORAGE_BLOCK)
+ return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
return res->StageReferences & (1 << stage);
}
unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
struct gl_program_resource *uni =
program_resource_find_data(shProg,
- &shProg->UniformStorage[idx]);
+ &shProg->data->UniformStorage[idx]);
assert(uni);
*val++ = _mesa_program_resource_index(shProg, uni);
}
*val = RESOURCE_XFB(res)->NumVaryings;
return 1;
case GL_ACTIVE_VARIABLES:
- int i = 0;
- for ( ; i < shProg->LinkedTransformFeedback.NumVarying; i++) {
- unsigned index =
- shProg->LinkedTransformFeedback.Varyings[i].BufferIndex;
+ struct gl_transform_feedback_info *linked_xfb =
+ shProg->last_vert_prog->sh.LinkedTransformFeedback;
+ for (int i = 0; i < linked_xfb->NumVarying; i++) {
+ unsigned index = linked_xfb->Varyings[i].BufferIndex;
struct gl_program_resource *buf_res =
_mesa_program_resource_find_index(shProg,
GL_TRANSFORM_FEEDBACK_BUFFER,
switch (res->Type) {
case GL_UNIFORM:
case GL_BUFFER_VARIABLE:
+ case GL_VERTEX_SUBROUTINE_UNIFORM:
+ case GL_GEOMETRY_SUBROUTINE_UNIFORM:
+ case GL_FRAGMENT_SUBROUTINE_UNIFORM:
+ case GL_COMPUTE_SUBROUTINE_UNIFORM:
+ case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
+ case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
+
/* Test if a buffer variable is an array or an unsized array.
* Unsized arrays return zero as array size.
*/
*val = MAX2(_mesa_program_resource_array_size(res), 1);
return 1;
case GL_TRANSFORM_FEEDBACK_VARYING:
- *val = MAX2(RESOURCE_XFV(res)->Size, 1);
+ *val = RESOURCE_XFV(res)->Size;
return 1;
default:
goto invalid_operation;
case GL_LOCATION:
switch (res->Type) {
case GL_UNIFORM:
+ case GL_VERTEX_SUBROUTINE_UNIFORM:
+ case GL_GEOMETRY_SUBROUTINE_UNIFORM:
+ case GL_FRAGMENT_SUBROUTINE_UNIFORM:
+ case GL_COMPUTE_SUBROUTINE_UNIFORM:
+ case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
+ case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
case GL_PROGRAM_INPUT:
case GL_PROGRAM_OUTPUT:
- *val = program_resource_location(shProg, res,
- _mesa_program_resource_name(res),
- 0);
+ *val = program_resource_location(res, 0);
return 1;
default:
goto invalid_operation;
}
- case GL_LOCATION_INDEX:
+ case GL_LOCATION_COMPONENT:
+ switch (res->Type) {
+ case GL_PROGRAM_INPUT:
+ case GL_PROGRAM_OUTPUT:
+ *val = RESOURCE_VAR(res)->component;
+ return 1;
+ default:
+ goto invalid_operation;
+ }
+ case GL_LOCATION_INDEX: {
+ int tmp;
if (res->Type != GL_PROGRAM_OUTPUT)
goto invalid_operation;
- *val = RESOURCE_VAR(res)->index;
+ tmp = program_resource_location(res, 0);
+ if (tmp == -1)
+ *val = -1;
+ else
+ *val = _mesa_program_resource_location_index(shProg, res->Type,
+ RESOURCE_VAR(res)->name);
return 1;
-
+ }
case GL_NUM_COMPATIBLE_SUBROUTINES:
if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
return 1;
case GL_COMPATIBLE_SUBROUTINES: {
const struct gl_uniform_storage *uni;
- struct gl_shader *sh;
+ struct gl_program *p;
unsigned count, i;
int j;
goto invalid_operation;
uni = RESOURCE_UNI(res);
- sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
+ p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
count = 0;
- for (i = 0; i < sh->NumSubroutineFunctions; i++) {
- struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
+ for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
+ struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
for (j = 0; j < fn->num_compat_types; j++) {
if (fn->types[j] == uni->type) {
val[count++] = i;
}
static bool
-validate_io(const struct gl_shader *producer,
- const struct gl_shader *consumer, bool isES)
+validate_io(struct gl_program *producer, struct gl_program *consumer)
{
- assert(producer && consumer);
- unsigned inputs = 0, outputs = 0;
+ if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
+ return true;
- /* From OpenGL ES 3.1 spec (Interface matching):
- *
- * "An output variable is considered to match an input variable in the
- * subsequent shader if:
- *
- * - the two variables match in name, type, and qualification; or
- * - the two variables are declared with the same location qualifier and
- * match in type and qualification.
- *
- * ...
+ const bool producer_is_array_stage =
+ producer->info.stage == MESA_SHADER_TESS_CTRL;
+ const bool consumer_is_array_stage =
+ consumer->info.stage == MESA_SHADER_GEOMETRY ||
+ consumer->info.stage == MESA_SHADER_TESS_CTRL ||
+ consumer->info.stage == MESA_SHADER_TESS_EVAL;
+
+ bool valid = true;
+
+ gl_shader_variable const **outputs =
+ (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
+ sizeof(gl_shader_variable *));
+ if (outputs == NULL)
+ return false;
+
+ /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
+ * says:
*
- * At an interface between program objects, the set of inputs and outputs
- * are considered to match exactly if and only if:
+ * At an interface between program objects, the set of inputs and
+ * outputs are considered to match exactly if and only if:
*
* - Every declared input variable has a matching output, as described
- * above.
- *
+ * above.
* - There are no user-defined output variables declared without a
- * matching input variable declaration.
- *
- * - All matched input and output variables have identical precision
- * qualification.
+ * matching input variable declaration.
*
- * When the set of inputs and outputs on an interface between programs
- * matches exactly, all inputs are well-defined except when the
- * corresponding outputs were not written in the previous shader. However,
- * any mismatch between inputs and outputs will result in a validation
- * failure."
- *
- * OpenGL Core 4.5 spec includes same paragraph as above but without check
- * for precision and the last 'validation failure' clause. Therefore
- * behaviour is more relaxed, input and output amount is not required by the
- * spec to be validated.
- *
- * FIXME: Update once Khronos spec bug #15331 is resolved.
- * FIXME: Add validation by type, currently information loss during varying
- * packing makes this challenging.
+ * Every input has an output, and every output has an input. Scan the list
+ * of producer resources once, and generate the list of outputs. As inputs
+ * and outputs are matched, remove the matched outputs from the set. At
+ * the end, the set must be empty. If the set is not empty, then there is
+ * some output that did not have an input.
*/
+ unsigned num_outputs = 0;
+ for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
+ struct gl_program_resource *res =
+ &producer->sh.data->ProgramResourceList[i];
- /* Currently no matching done for desktop. */
- if (!isES)
- return true;
+ if (res->Type != GL_PROGRAM_OUTPUT)
+ continue;
- /* For each output in a, find input in b and do any required checks. */
- foreach_in_list(ir_instruction, out, producer->ir) {
- ir_variable *out_var = out->as_variable();
- if (!out_var || out_var->data.mode != ir_var_shader_out ||
- is_gl_identifier(out_var->name))
+ gl_shader_variable const *const var = RESOURCE_VAR(res);
+
+ /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
+ * says:
+ *
+ * Built-in inputs or outputs do not affect interface matching.
+ */
+ if (is_gl_identifier(var->name))
continue;
- outputs++;
+ outputs[num_outputs++] = var;
+ }
- inputs = 0;
- foreach_in_list(ir_instruction, in, consumer->ir) {
- ir_variable *in_var = in->as_variable();
- if (!in_var || in_var->data.mode != ir_var_shader_in ||
- is_gl_identifier(in_var->name))
- continue;
+ unsigned match_index = 0;
+ for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
+ struct gl_program_resource *res =
+ &consumer->sh.data->ProgramResourceList[i];
- inputs++;
+ if (res->Type != GL_PROGRAM_INPUT)
+ continue;
- /* Match by location qualifier and precision.
- *
- * FIXME: Add explicit location matching validation here. Be careful
- * not to match varyings with explicit locations to varyings without
- * explicit locations.
- */
- if ((in_var->data.explicit_location &&
- out_var->data.explicit_location) &&
- in_var->data.location == out_var->data.location &&
- in_var->data.precision == out_var->data.precision)
- continue;
-
- unsigned len = strlen(in_var->name);
-
- /* Handle input swizzle in variable name. */
- const char *dot = strchr(in_var->name, '.');
- if (dot)
- len = dot - in_var->name;
-
- /* Match by name and precision. */
- if (strncmp(in_var->name, out_var->name, len) == 0) {
- /* From OpenGL ES 3.1 spec:
- * "When both shaders are in separate programs, mismatched
- * precision qualifiers will result in a program interface
- * mismatch that will result in program pipeline validation
- * failures, as described in section 7.4.1 (“Shader Interface
- * Matching”) of the OpenGL ES 3.1 Specification."
- */
- if (in_var->data.precision != out_var->data.precision)
- return false;
+ gl_shader_variable const *const consumer_var = RESOURCE_VAR(res);
+ gl_shader_variable const *producer_var = NULL;
+
+ if (is_gl_identifier(consumer_var->name))
+ continue;
+
+ /* Inputs with explicit locations match other outputs with explicit
+ * locations by location instead of by name.
+ */
+ if (consumer_var->explicit_location) {
+ for (unsigned j = 0; j < num_outputs; j++) {
+ const gl_shader_variable *const var = outputs[j];
+
+ if (var->explicit_location &&
+ consumer_var->location == var->location) {
+ producer_var = var;
+ match_index = j;
+ break;
+ }
+ }
+ } else {
+ for (unsigned j = 0; j < num_outputs; j++) {
+ const gl_shader_variable *const var = outputs[j];
+
+ if (!var->explicit_location &&
+ strcmp(consumer_var->name, var->name) == 0) {
+ producer_var = var;
+ match_index = j;
+ break;
+ }
+ }
+ }
+
+ /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
+ * says:
+ *
+ * - An output variable is considered to match an input variable in
+ * the subsequent shader if:
+ *
+ * - the two variables match in name, type, and qualification; or
+ *
+ * - the two variables are declared with the same location
+ * qualifier and match in type and qualification.
+ */
+ if (producer_var == NULL) {
+ valid = false;
+ goto out;
+ }
+
+ /* An output cannot match more than one input, so remove the output from
+ * the set of possible outputs.
+ */
+ outputs[match_index] = NULL;
+ num_outputs--;
+ if (match_index < num_outputs)
+ outputs[match_index] = outputs[num_outputs];
+
+ /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
+ *
+ * "Tessellation control shader per-vertex output variables and
+ * blocks and tessellation control, tessellation evaluation, and
+ * geometry shader per-vertex input variables and blocks are
+ * required to be declared as arrays, with each element representing
+ * input or output values for a single vertex of a multi-vertex
+ * primitive. For the purposes of interface matching, such variables
+ * and blocks are treated as though they were not declared as
+ * arrays."
+ *
+ * So we unwrap those types before matching.
+ */
+ const glsl_type *consumer_type = consumer_var->type;
+ const glsl_type *consumer_interface_type = consumer_var->interface_type;
+ const glsl_type *producer_type = producer_var->type;
+ const glsl_type *producer_interface_type = producer_var->interface_type;
+
+ if (consumer_is_array_stage) {
+ if (consumer_interface_type) {
+ /* the interface is the array; the underlying types should match */
+ if (consumer_interface_type->is_array() && !consumer_var->patch)
+ consumer_interface_type = consumer_interface_type->fields.array;
+ } else {
+ if (consumer_type->is_array() && !consumer_var->patch)
+ consumer_type = consumer_type->fields.array;
}
}
+
+ if (producer_is_array_stage) {
+ if (producer_interface_type) {
+ /* the interface is the array; the underlying types should match */
+ if (producer_interface_type->is_array() && !producer_var->patch)
+ producer_interface_type = producer_interface_type->fields.array;
+ } else {
+ if (producer_type->is_array() && !producer_var->patch)
+ producer_type = producer_type->fields.array;
+ }
+ }
+
+ if (producer_type != consumer_type) {
+ valid = false;
+ goto out;
+ }
+
+ if (producer_interface_type != consumer_interface_type) {
+ valid = false;
+ goto out;
+ }
+
+ /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
+ *
+ * Qualifier Class| Qualifier |in/out
+ * ---------------+-------------+------
+ * Storage | in |
+ * | out | N/A
+ * | uniform |
+ * ---------------+-------------+------
+ * Auxiliary | centroid | No
+ * ---------------+-------------+------
+ * | location | Yes
+ * | Block layout| N/A
+ * | binding | N/A
+ * | offset | N/A
+ * | format | N/A
+ * ---------------+-------------+------
+ * Interpolation | smooth |
+ * | flat | Yes
+ * ---------------+-------------+------
+ * | lowp |
+ * Precision | mediump | Yes
+ * | highp |
+ * ---------------+-------------+------
+ * Variance | invariant | No
+ * ---------------+-------------+------
+ * Memory | all | N/A
+ *
+ * Note that location mismatches are detected by the loops above that
+ * find the producer variable that goes with the consumer variable.
+ */
+ unsigned producer_interpolation = producer_var->interpolation;
+ unsigned consumer_interpolation = consumer_var->interpolation;
+ if (producer_interpolation == INTERP_MODE_NONE)
+ producer_interpolation = INTERP_MODE_SMOOTH;
+ if (consumer_interpolation == INTERP_MODE_NONE)
+ consumer_interpolation = INTERP_MODE_SMOOTH;
+ if (producer_interpolation != consumer_interpolation) {
+ valid = false;
+ goto out;
+ }
+
+ if (producer_var->precision != consumer_var->precision) {
+ valid = false;
+ goto out;
+ }
+
+ if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
+ valid = false;
+ goto out;
+ }
}
- return inputs == outputs;
+
+ out:
+ free(outputs);
+ return valid && num_outputs == 0;
}
/**
extern "C" bool
_mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
{
- struct gl_shader_program **shProg =
- (struct gl_shader_program **) pipeline->CurrentProgram;
+ struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
/* Find first active stage in pipeline. */
unsigned idx, prev = 0;
for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
- if (shProg[idx]) {
+ if (prog[idx]) {
prev = idx;
break;
}
}
for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
- if (shProg[idx]) {
+ if (prog[idx]) {
/* Pipeline might include both non-compute and a compute program, do
* not attempt to validate varyings between non-compute and compute
* stage.
*/
- if (shProg[idx]->_LinkedShaders[idx]->Stage == MESA_SHADER_COMPUTE)
+ if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
break;
- if (!validate_io(shProg[prev]->_LinkedShaders[prev],
- shProg[idx]->_LinkedShaders[idx],
- shProg[prev]->IsES || shProg[idx]->IsES))
+ if (!validate_io(prog[prev], prog[idx]))
return false;
+
prev = idx;
}
}