<div class="gmail_quote">On 8 November 2011 05:15, Marek Olšák <span dir="ltr"><<a href="mailto:maraeo@gmail.com">maraeo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="HOEnZb"><div class="h5">On Tue, Nov 8, 2011 at 7:28 AM, Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>> wrote:<br>
> This patch modifies the GLSL linker to assign additional slots for<br>
> varying variables used by transform feedback, and record the varying<br>
> slots used by transform feedback for use by the driver back-end.<br>
><br>
> This required modifying assign_varying_locations() so that it assigns<br>
> a varying location if either (a) the varying is used by the next stage<br>
> of the GL pipeline, or (b) the varying is required by transform<br>
> feedback. In order to avoid duplicating the code to assign a single<br>
> varying location, I moved it into its own function,<br>
> assign_varying_location().<br>
><br>
> In addition, to support transform feedback in the case where there is<br>
> no fragment shader, it is now possible to call<br>
> assign_varying_locations() with a consumer of NULL.<br>
> ---<br>
> Changes from v1:<br>
><br>
> - Fixed loop bound in tfeedback_decl::store() (was this->vector_elements,<br>
> should have been this->matrix_columns).<br>
><br>
> - Fixed the case where transform feedback is in use but there is no fragment<br>
> shader.<br>
><br>
> src/glsl/linker.cpp | 552 ++++++++++++++++++++++++++++++++++++++++++------<br>
> src/mesa/main/mtypes.h | 13 ++<br>
> 2 files changed, 502 insertions(+), 63 deletions(-)<br>
><br>
> diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp<br>
> index 915d5bb..5cccd7f 100644<br>
> --- a/src/glsl/linker.cpp<br>
> +++ b/src/glsl/linker.cpp<br>
> @@ -1519,10 +1519,358 @@ demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)<br>
> }<br>
><br>
><br>
> +/**<br>
> + * Data structure tracking information about a transform feedback declaration<br>
> + * during linking.<br>
> + */<br>
> +class tfeedback_decl<br>
> +{<br>
> +public:<br>
> + bool init(struct gl_shader_program *prog, const void *mem_ctx,<br>
> + const char *input);<br>
> + static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);<br>
> + bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,<br>
> + ir_variable *output_var);<br>
> + bool store(struct gl_shader_program *prog,<br>
> + struct gl_transform_feedback_info *info, unsigned buffer) const;<br>
> +<br>
> +<br>
> + /**<br>
> + * True if assign_location() has been called for this object.<br>
> + */<br>
> + bool is_assigned() const<br>
> + {<br>
> + return this->location != -1;<br>
> + }<br>
> +<br>
> + /**<br>
> + * Determine whether this object refers to the variable var.<br>
> + */<br>
> + bool matches_var(ir_variable *var) const<br>
> + {<br>
> + return strcmp(var->name, this->var_name) == 0;<br>
> + }<br>
> +<br>
> + /**<br>
> + * The total number of varying components taken up by this variable. Only<br>
> + * valid if is_assigned() is true.<br>
> + */<br>
> + unsigned num_components() const<br>
> + {<br>
> + return this->vector_elements * this->matrix_columns;<br>
> + }<br>
> +<br>
> +private:<br>
> + /**<br>
> + * The name that was supplied to glTransformFeedbackVaryings. Used for<br>
> + * error reporting.<br>
> + */<br>
> + const char *orig_name;<br>
> +<br>
> + /**<br>
> + * The name of the variable, parsed from orig_name.<br>
> + */<br>
> + char *var_name;<br>
> +<br>
> + /**<br>
> + * True if the declaration in orig_name represents an array.<br>
> + */<br>
> + bool is_array;<br>
> +<br>
> + /**<br>
> + * If is_array is true, the array index that was specified in orig_name.<br>
> + */<br>
> + unsigned array_index;<br>
> +<br>
> + /**<br>
> + * The vertex shader output location that the linker assigned for this<br>
> + * variable. -1 if a location hasn't been assigned yet.<br>
> + */<br>
> + int location;<br>
> +<br>
> + /**<br>
> + * If location != -1, the number of vector elements in this variable, or 1<br>
> + * if this variable is a scalar.<br>
> + */<br>
> + unsigned vector_elements;<br>
> +<br>
> + /**<br>
> + * If location != -1, the number of matrix columns in this variable, or 1<br>
> + * if this variable is not a matrix.<br>
> + */<br>
> + unsigned matrix_columns;<br>
> +};<br>
> +<br>
> +<br>
> +/**<br>
> + * Initialize this object based on a string that was passed to<br>
> + * glTransformFeedbackVaryings. If there is a parse error, the error is<br>
> + * reported using linker_error(), and false is returned.<br>
> + */<br>
> +bool<br>
> +tfeedback_decl::init(struct gl_shader_program *prog, const void *mem_ctx,<br>
> + const char *input)<br>
> +{<br>
> + /* We don't have to be pedantic about what is a valid GLSL variable name,<br>
> + * because any variable with an invalid name can't exist in the IR anyway.<br>
> + */<br>
> +<br>
> + this->location = -1;<br>
> + this->orig_name = input;<br>
> +<br>
> + const char *bracket = strrchr(input, '[');<br>
> +<br>
> + if (bracket) {<br>
> + this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);<br>
> + if (sscanf(bracket, "[%u]", &this->array_index) == 1) {<br>
> + this->is_array = true;<br>
> + return true;<br>
> + }<br>
> + } else {<br>
> + this->var_name = ralloc_strdup(mem_ctx, input);<br>
> + this->is_array = false;<br>
> + return true;<br>
> + }<br>
> +<br>
> + linker_error(prog, "Cannot parse transform feedback varying %s", input);<br>
> + return false;<br>
> +}<br>
> +<br>
> +<br>
> +/**<br>
> + * Determine whether two tfeedback_decl objects refer to the same variable and<br>
> + * array index (if applicable).<br>
> + */<br>
> +bool<br>
> +tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)<br>
> +{<br>
> + if (strcmp(x.var_name, y.var_name) != 0)<br>
> + return false;<br>
> + if (x.is_array != y.is_array)<br>
> + return false;<br>
> + if (x.is_array && x.array_index != y.array_index)<br>
> + return false;<br>
> + return true;<br>
> +}<br>
> +<br>
> +<br>
> +/**<br>
> + * Assign a location for this tfeedback_decl object based on the location<br>
> + * assignment in output_var.<br>
> + *<br>
> + * If an error occurs, the error is reported through linker_error() and false<br>
> + * is returned.<br>
> + */<br>
> +bool<br>
> +tfeedback_decl::assign_location(struct gl_context *ctx,<br>
> + struct gl_shader_program *prog,<br>
> + ir_variable *output_var)<br>
> +{<br>
> + if (output_var->type->is_array()) {<br>
> + /* Array variable */<br>
> + if (!this->is_array) {<br>
> + linker_error(prog, "Transform feedback varying %s found, "<br>
> + "but it's not an array ([] not expected).",<br>
> + this->orig_name);<br>
> + return false;<br>
> + }<br>
> + /* Check array bounds. */<br>
> + if (this->array_index >=<br>
> + (unsigned) output_var->type->array_size()) {<br>
> + linker_error(prog, "Transform feedback varying %s has index "<br>
> + "%i, but the array size is %i.",<br>
> + this->orig_name, this->array_index,<br>
> + output_var->type->array_size());<br>
> + }<br>
> + const unsigned matrix_cols =<br>
> + output_var->type->fields.array->matrix_columns;<br>
> + this->location = output_var->location + this->array_index * matrix_cols;<br>
> + this->vector_elements = output_var->type->fields.array->vector_elements;<br>
> + this->matrix_columns = matrix_cols;<br>
> + } else {<br>
> + /* Regular variable (scalar, vector, or matrix) */<br>
> + if (this->is_array) {<br>
> + linker_error(prog, "Transform feedback varying %s found, "<br>
> + "but it's an array ([] expected).",<br>
> + this->orig_name);<br>
> + return false;<br>
> + }<br>
> + this->location = output_var->location;<br>
> + this->vector_elements = output_var->type->vector_elements;<br>
> + this->matrix_columns = output_var->type->matrix_columns;<br>
> + }<br>
> + /* From GL_EXT_transform_feedback:<br>
> + * A program will fail to link if:<br>
> + *<br>
> + * * the total number of components to capture in any varying<br>
> + * variable in <varyings> is greater than the constant<br>
> + * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the<br>
> + * buffer mode is SEPARATE_ATTRIBS_EXT;<br>
> + */<br>
> + if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&<br>
> + this->num_components() ><br>
> + ctx->Const.MaxTransformFeedbackSeparateComponents) {<br>
> + linker_error(prog, "Transform feedback varying %s exceeds "<br>
> + "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",<br>
> + this->orig_name);<br>
> + return false;<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +<br>
> +/**<br>
> + * Update gl_transform_feedback_info to reflect this tfeedback_decl.<br>
> + *<br>
> + * If an error occurs, the error is reported through linker_error() and false<br>
> + * is returned.<br>
> + */<br>
> +bool<br>
> +tfeedback_decl::store(struct gl_shader_program *prog,<br>
> + struct gl_transform_feedback_info *info,<br>
> + unsigned buffer) const<br>
> +{<br>
> + if (!this->is_assigned()) {<br>
> + /* From GL_EXT_transform_feedback:<br>
> + * A program will fail to link if:<br>
> + *<br>
> + * * any variable name specified in the <varyings> array is not<br>
> + * declared as an output in the geometry shader (if present) or<br>
> + * the vertex shader (if no geometry shader is present);<br>
> + */<br>
> + linker_error(prog, "Transform feedback varying %s undeclared.",<br>
> + this->orig_name);<br>
> + return false;<br>
> + }<br>
> + for (unsigned v = 0; v < this->matrix_columns; ++v) {<br>
> + info->Outputs[info->NumOutputs].OutputRegister = this->location + v;<br>
> + info->Outputs[info->NumOutputs].NumComponents = this->vector_elements;<br>
> + info->Outputs[info->NumOutputs].OutputBuffer = buffer;<br>
> + ++info->NumOutputs;<br>
> + }<br>
> + return true;<br>
> +}<br>
> +<br>
> +<br>
> +/**<br>
> + * Parse all the transform feedback declarations that were passed to<br>
> + * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.<br>
> + *<br>
> + * If an error occurs, the error is reported through linker_error() and false<br>
> + * is returned.<br>
> + */<br>
> +static bool<br>
> +parse_tfeedback_decls(struct gl_shader_program *prog, const void *mem_ctx,<br>
> + unsigned num_names, char **varying_names,<br>
> + tfeedback_decl *decls)<br>
> +{<br>
> + for (unsigned i = 0; i < num_names; ++i) {<br>
> + if (!decls[i].init(prog, mem_ctx, varying_names[i]))<br>
> + return false;<br>
> + /* From GL_EXT_transform_feedback:<br>
> + * A program will fail to link if:<br>
> + *<br>
> + * * any two entries in the <varyings> array specify the same varying<br>
> + * variable;<br>
> + *<br>
> + * We interpret this to mean "any two entries in the <varyings> array<br>
> + * specify the same varying variable and array index", since transform<br>
> + * feedback of arrays would be useless otherwise.<br>
> + */<br>
> + for (unsigned j = 0; j < i; ++j) {<br>
> + if (tfeedback_decl::is_same(decls[i], decls[j])) {<br>
> + linker_error(prog, "Transform feedback varying %s specified "<br>
> + "more than once.", varying_names[i]);<br>
> + }<br>
> + return false;<br>
<br>
</div></div>The "return false" statement should be put next to linker_error.<br>
Otherwise it always fails when the number of TFB varyings is at least<br>
2.<br></blockquote><div><br></div><div>Whoops, you're right of course.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
With that fixed, this patch is:<br>
<br>
Reviewed-by: Marek Olšák <<a href="mailto:maraeo@gmail.com">maraeo@gmail.com</a>><br>
Tested-by: Marek Olšák <<a href="mailto:maraeo@gmail.com">maraeo@gmail.com</a>><br></blockquote><div><br></div><div>Thanks for the quick turnaround, Marek. I'll wait until the end of the day to push this upstream, in case anyone else has any review comments.</div>
<div><br></div><div>Paul</div></div>