On 19 September 2011 11:10, Ian Romanick <span dir="ltr"><<a href="mailto:idr@freedesktop.org">idr@freedesktop.org</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
-----BEGIN PGP SIGNED MESSAGE-----<br>
Hash: SHA1<br>
<div><div></div><div class="h5"><br>
On 09/15/2011 04:40 PM, Paul Berry wrote:<br>
> In i965 GEN6+ (and I suspect most other hardware), gl_ClipDistance<br>
> needs to be laid out as a pair of vec4's (the first containing<br>
> clip distances 0-3, and the second containing clip distances 4-7).<br>
> However, it is declared in GLSL as an array of 8 floats.<br>
><br>
> This lowering pass acts at the GLSL level, modifying the<br>
> declaration of gl_ClipDistance so that it is an array of vec4's<br>
> rather than an array of floats, and renaming it to<br>
> gl_ClipDistanceMESA. In addition, it modifies all accesses to the<br>
> array so that they access the appropiate component of one of the<br>
> vec4's.<br>
><br>
> Since some hardware may not internally represent gl_ClipDistance as<br>
> a pair of vec4's, this lowering pass is optional. To enable it,<br>
> set the LowerClipDistance flag in gl_shader_compiler_options to<br>
> true. --- src/glsl/Makefile | 1 +<br>
> src/glsl/SConscript | 1 +<br>
> src/glsl/ir_optimization.h | 1 + src/glsl/linker.cpp<br>
> | 3 + src/glsl/lower_clip_distance.cpp | 347<br>
> ++++++++++++++++++++++++++++++++++++++ src/mesa/main/mtypes.h<br>
> | 1 + 6 files changed, 354 insertions(+), 0 deletions(-) create<br>
> mode 100644 src/glsl/lower_clip_distance.cpp<br>
><br>
> diff --git a/src/glsl/Makefile b/src/glsl/Makefile index<br>
> c20a6c9..bee21c2 100644 --- a/src/glsl/Makefile +++<br>
> b/src/glsl/Makefile @@ -56,6 +56,7 @@ CXX_SOURCES = \<br>
> loop_analysis.cpp \ loop_controls.cpp \ loop_unroll.cpp \ +<br>
> lower_clip_distance.cpp \ lower_discard.cpp \<br>
> lower_if_to_cond_assign.cpp \ lower_instructions.cpp \ diff --git<br>
> a/src/glsl/SConscript b/src/glsl/SConscript index 1da58a9..b4786c5<br>
> 100644 --- a/src/glsl/SConscript +++ b/src/glsl/SConscript @@ -67,6<br>
> +67,7 @@ glsl_sources = [ 'loop_analysis.cpp',<br>
> 'loop_controls.cpp', 'loop_unroll.cpp', +<br>
> 'lower_clip_distance.cpp', 'lower_discard.cpp',<br>
> 'lower_if_to_cond_assign.cpp', 'lower_instructions.cpp', diff --git<br>
> a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h index<br>
> 48448d4..af80e26 100644 --- a/src/glsl/ir_optimization.h +++<br>
> b/src/glsl/ir_optimization.h @@ -69,6 +69,7 @@ bool<br>
> lower_noise(exec_list *instructions); bool<br>
> lower_variable_index_to_cond_assign(exec_list *instructions, bool<br>
> lower_input, bool lower_output, bool lower_temp, bool<br>
> lower_uniform); bool lower_quadop_vector(exec_list *instructions,<br>
> bool dont_lower_swz); +bool lower_clip_distance(exec_list<br>
> *instructions); bool optimize_redundant_jumps(exec_list<br>
> *instructions);<br>
><br>
> ir_rvalue * diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp<br>
> index 195f58f..ceb49fd 100644 --- a/src/glsl/linker.cpp +++<br>
> b/src/glsl/linker.cpp @@ -1743,6 +1743,9 @@ link_shaders(struct<br>
> gl_context *ctx, struct gl_shader_program *prog) if<br>
> (!prog->LinkStatus) goto done;<br>
><br>
> + if (ctx->ShaderCompilerOptions[i].LowerClipDistance) +<br>
> lower_clip_distance(prog->_LinkedShaders[i]->ir); + while<br>
> (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32)) ;<br>
> } diff --git a/src/glsl/lower_clip_distance.cpp<br>
> b/src/glsl/lower_clip_distance.cpp new file mode 100644 index<br>
> 0000000..55a9e9d --- /dev/null +++<br>
> b/src/glsl/lower_clip_distance.cpp @@ -0,0 +1,347 @@ +/* + *<br>
> Copyright © 2011 Intel Corporation + * + * Permission is hereby<br>
> granted, free of charge, to any person obtaining a + * copy of this<br>
> software and associated documentation files (the "Software"), + *<br>
> to deal in the Software without restriction, including without<br>
> limitation + * the rights to use, copy, modify, merge, publish,<br>
> distribute, sublicense, + * and/or sell copies of the Software, and<br>
> to permit persons to whom the + * Software is furnished to do so,<br>
> subject to the following conditions: + * + * The above copyright<br>
> notice and this permission notice (including the next + *<br>
> paragraph) shall be included in all copies or substantial portions<br>
> of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS",<br>
> WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT<br>
> NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A<br>
> PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE<br>
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
> OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR<br>
> OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE<br>
> SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +<br>
> +/** + * \file lower_clip_distance.cpp + * + * This pass accounts<br>
> for the difference between the way + * gl_ClipDistance is declared<br>
> in standard GLSL (as an array of + * floats), and the way it is<br>
> frequently implemented in hardware (as + * a pair of vec4s, with<br>
> four clip distances packed into each). + * + * The declaration of<br>
> gl_ClipDistance is replaced with a declaration + * of<br>
> gl_ClipDistanceMESA, and any references to gl_ClipDistance are + *<br>
> translated to refer to gl_ClipDistanceMESA with the appropriate + *<br>
> swizzling of array indices. For instance: + * + *<br>
> gl_ClipDistance[i] + * + * is translated into: + * + *<br>
> gl_ClipDistanceMESA[i>>2][i&3] + * + * Since some hardware may not<br>
> internally represent gl_ClipDistance as a pair + * of vec4's, this<br>
> lowering pass is optional. To enable it, set the + *<br>
> LowerClipDistance flag in gl_shader_compiler_options to true. + */<br>
> + +#include "ir_hierarchical_visitor.h" +#include "ir.h" + +class<br>
> lower_clip_distance_visitor : public ir_hierarchical_visitor {<br>
> +public: + lower_clip_distance_visitor() + :<br>
> progress(false), old_clip_distance_var(NULL), +<br>
> new_clip_distance_var(NULL) + { + } + + virtual<br>
> ir_visitor_status visit(ir_variable *); + void<br>
> create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&); + virtual<br>
> ir_visitor_status visit_leave(ir_dereference_array *); + virtual<br>
> ir_visitor_status visit_leave(ir_assignment *); + void<br>
> visit_new_assignment(ir_assignment *ir); + virtual<br>
> ir_visitor_status visit_leave(ir_call *); + + bool progress; + +<br>
> /** + * Pointer to the declaration of gl_ClipDistance, if<br>
> found. + */ + ir_variable *old_clip_distance_var; + + /** +<br>
> * Pointer to the newly-created gl_ClipDistanceMESA variable. +<br>
> */ + ir_variable *new_clip_distance_var; +}; + + +/** + * Replace<br>
> any declaration of gl_ClipDistance as an array of floats with a + *<br>
> declaration of gl_ClipDistanceMESA as an array of vec4's. + */<br>
> +ir_visitor_status +lower_clip_distance_visitor::visit(ir_variable<br>
> *ir) +{ + /* No point in looking for the declaration of<br>
> gl_ClipDistance if + * we've already found it. + */ + if<br>
> (this->old_clip_distance_var) + return visit_continue; + +<br>
> if (ir->name && strcmp(ir->name, "gl_ClipDistance") == 0) { +<br>
> this->progress = true; + this->old_clip_distance_var = ir; +<br>
> assert (ir->type->is_array()); + assert<br>
> (ir->type->element_type() == glsl_type::float_type); +<br>
> unsigned new_size = (ir->type->array_size() + 3) / 4; + + /*<br>
> Clone the old var so that we inherit all of its properties */ +<br>
> this->new_clip_distance_var = ir->clone(ralloc_parent(ir), NULL);<br>
> + + /* And change the properties that we need to change */ +<br>
> this->new_clip_distance_var->name + =<br>
> ralloc_strdup(this->new_clip_distance_var, "gl_ClipDistanceMESA");<br>
> + this->new_clip_distance_var->type + =<br>
> glsl_type::get_array_instance(glsl_type::vec4_type, new_size); +<br>
> this->new_clip_distance_var->max_array_access =<br>
> ir->max_array_access / 4; + +<br>
> ir->replace_with(this->new_clip_distance_var); + } + return<br>
> visit_continue; +} + + +/** + * Create the necessary GLSL rvalues<br>
> to index into gl_ClipDistanceMESA based + * on the rvalue<br>
> previously used to index into gl_ClipDistance. The first, + *<br>
> "outer" index, selects one of the vec4's in gl_ClipDistanceMESA,<br>
> and the + * second, "inner" index, selects a component within that<br>
> vec4. + */<br>
<br>
</div></div>I'd call these array_index and swizzle_index instead of outer_index<br>
and inner_index. Also, documenting function parameters should be done<br>
in the Doxygen magic format:<br>
<br>
/**<br>
*<br>
* \param array_index Selects one of the vec4's in gl_ClipDistanceMESA<br>
* \param swizzle_index Selects a component within the vec4 selected by<br>
* array_index.<br>
<div class="im"> */<br></div></blockquote><div><br>Ok, sounds reasonable.<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
<br>
> +void +lower_clip_distance_visitor::create_indices(ir_rvalue<br>
> *old_index, + ir_rvalue<br>
> *&outer_index, +<br>
> ir_rvalue *&inner_index) +{ + void *ctx =<br>
> ralloc_parent(old_index); + + /* Make sure old_index is a signed<br>
> int so that the bitwise "shift" and + * "and" operations below<br>
> type check properly. + */ + if (old_index->type !=<br>
> glsl_type::int_type) { + assert (old_index->type ==<br>
> glsl_type::uint_type); + old_index = new(ctx)<br>
> ir_expression(ir_unop_u2i, old_index); + }<br>
<br>
</div>Does this matter? The only case where it would be different is if the<br>
index is negative. In that case, the array look-up already has<br>
undefined results.<br></blockquote><div><br>The reason for this is that according to GLSL IR type validation rules, the two operands to a binary AND need to be the same type, so in addition to using old_index->type for old_index_var (as you mention below), I would have to dynamically choose between ir_constant(3) and ir_constant(3u) based on the type of the index. Also, the call to get_int_component() would feel more fragile, since technically one *ought* to use get_uint_component() to get the value of a uint (even though get_int_component() works in practice due to int and uint constants being stored in the same union). Finally, there are bugs in lower_vec_index_to_swizzle.cpp and lower_variable_index_to_cond_assign.cpp that prevent them from working on uint array indices right now. Those should be pretty easy to fix but I would rather get gl_ClipDistance to work first.<br>
<br>Considering that on most back-ends, converting an int to a uint should not cost any GPU instructions (since it's just a reinterpretation of bits), just doing the conversion seemed easier than dealing with all the stuff above.<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
> + + ir_constant *old_index_constant =<br>
<div class="im">> old_index->constant_expression_value(); + if (old_index_constant)<br>
> { + /* gl_ClipDistance is being accessed via a constant index.<br>
> Don't bother + * creating expressions to calculate the<br>
> lowered indices. Just create + * constants. + */ +<br>
> int const_val = old_index_constant->get_int_component(0); +<br>
> outer_index = new(ctx) ir_constant(const_val / 4); +<br>
> inner_index = new(ctx) ir_constant(const_val % 4); + } else { +<br>
> /* Create a variable to hold the value of old_index (so that we +<br>
> * don't compute it twice). + */ + ir_variable<br>
> *old_index_var = new(ctx) ir_variable( +<br>
> glsl_type::int_type, "clip_distance_index", ir_var_temporary);<br>
<br>
</div>Based on my comment above, I think this should use old_index->type<br>
instead of hard-coding glsl_type::int_type.<br>
<div><div></div><div class="h5"><br>
> + this->base_ir->insert_before(old_index_var); +<br>
> this->base_ir->insert_before(new(ctx) ir_assignment( +<br>
> new(ctx) ir_dereference_variable(old_index_var), old_index)); + +<br>
> /* Create the expression clip_distance_index / 4. Do this as a<br>
> bit + * shift because that's likely to be more efficient. +<br>
> */ + outer_index = new(ctx) ir_expression( +<br>
> ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var), +<br>
> new(ctx) ir_constant(2)); + + /* Create the expression<br>
> clip_distance_index % 4. Do this as a bitwise + * AND<br>
> because that's likely to be more efficient. + */ +<br>
> inner_index = new(ctx) ir_expression( + ir_binop_bit_and,<br>
> new(ctx) ir_dereference_variable(old_index_var), + new(ctx)<br>
> ir_constant(3)); + } +} + + +/** + * Replace any expression that<br>
> indexes into the gl_ClipDistance array with an + * expression that<br>
> indexes into one of the vec4's in gl_ClipDistanceMESA and + *<br>
> accesses the appropriate component. + */ +ir_visitor_status<br>
> +lower_clip_distance_visitor::visit_leave(ir_dereference_array<br>
> *ir) +{ + /* If the gl_ClipDistance var hasn't been declared yet,<br>
> then + * there's no way this deref can refer to it. + */ +<br>
> if (!this->old_clip_distance_var) + return visit_continue; + +<br>
> ir_dereference_variable *old_var_ref =<br>
> ir->array->as_dereference_variable(); + if (old_var_ref &&<br>
> old_var_ref->variable_referenced() + ==<br>
> this->old_clip_distance_var) { + this->progress = true; +<br>
> ir_rvalue *outer_index; + ir_rvalue *inner_index; +<br>
> this->create_indices(ir->array_index, outer_index, inner_index); +<br>
> void *mem_ctx = ralloc_parent(ir); + ir->array = new(mem_ctx)<br>
> ir_dereference_array( + this->new_clip_distance_var,<br>
> outer_index); + ir->array_index = inner_index; + } + +<br>
> return visit_continue; +} + + +/** + * Replace any assignment<br>
> having gl_ClipDistance (undereferenced) as its LHS + * or RHS with<br>
> a sequence of assignments, one for each component of the array. + *<br>
> Each of these assignments is lowered to refer to<br>
> gl_ClipDistanceMESA as + * appropriate. + */<br>
<br>
</div></div>Don't we already have a lowering pass that does this? If not, it<br>
seems like this could / should be it's own pass (that operates on<br>
general arrays). Perhaps Eric will have an opinion...<br></blockquote><div><br>I'm not aware of such a pass at the GLSL level (I think we may have something like this in i965, but that wouldn't be useful here). Eric, are you aware of anything?<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5"><br>
> +ir_visitor_status<br>
> +lower_clip_distance_visitor::visit_leave(ir_assignment *ir) +{ +<br>
> ir_dereference_variable *lhs_var =<br>
> ir->lhs->as_dereference_variable(); + ir_dereference_variable<br>
> *rhs_var = ir->rhs->as_dereference_variable(); + if ((lhs_var &&<br>
> lhs_var->variable_referenced() + ==<br>
> this->old_clip_distance_var) + || (rhs_var &&<br>
> rhs_var->variable_referenced() + ==<br>
> this->old_clip_distance_var)) { + /* LHS or RHS of the<br>
> assignment is the entire gl_ClipDistance array. + * Since we<br>
> are reshaping gl_ClipDistance from an array of floats to an +<br>
> * array of vec4's, this isn't going to work as a bulk assignment +<br>
> * anymore, so unroll it to element-by-element assignments and<br>
> lower + * each of them. + * + * Note: to unroll<br>
> into element-by-element assignments, we need to make + *<br>
> clones of the LHS and RHS. This is only safe if the LHS and RHS<br>
> are + * side-effect free. Fortunately, we know that they<br>
> are, because the + * only kind of rvalue that can have side<br>
> effects is an ir_call, and + * ir_calls only appear (a) as a<br>
> statement on their own, or (b) as the + * RHS of an<br>
> assignment that stores the result of the call in a + *<br>
> temporary variable. + */ + void *ctx =<br>
> ralloc_parent(ir); + int array_size =<br>
> this->old_clip_distance_var->type->array_size(); + for (int i<br>
> = 0; i < array_size; ++i) { + ir_dereference_array *new_lhs<br>
> = new(ctx) ir_dereference_array( + ir->lhs->clone(ctx,<br>
> NULL), new(ctx) ir_constant(i)); +<br>
> this->visit_leave(new_lhs); + ir_dereference_array *new_rhs<br>
> = new(ctx) ir_dereference_array( + ir->rhs->clone(ctx,<br>
> NULL), new(ctx) ir_constant(i)); +<br>
> this->visit_leave(new_rhs); +<br>
> this->base_ir->insert_before( + new(ctx)<br>
> ir_assignment(new_lhs, new_rhs)); + } + ir->remove(); +<br>
> } + + return visit_continue; +} + + +/** + * Set up base_ir<br>
> properly and call visit_leave() on a newly created + *<br>
> ir_assignment node. This is used in cases where we have to insert<br>
> an + * ir_assignment in a place where we know the hierarchical<br>
> visitor won't see + * it. + */ +void<br>
> +lower_clip_distance_visitor::visit_new_assignment(ir_assignment<br>
> *ir) +{ + ir_instruction *old_base_ir = this->base_ir; +<br>
> this->base_ir = ir; + this->visit_leave(ir); + this->base_ir =<br>
> old_base_ir; +} + + +/** + * If gl_ClipDistance appears as an<br>
> argument in an ir_call expression, replace + * it with a temporary<br>
> variable, and make sure the ir_call is preceded and/or + * followed<br>
> by assignments that copy the contents of the temporary variable to<br>
> + * and/or from gl_ClipDistance. Each of these assignments is then<br>
> lowered to + * refer to gl_ClipDistanceMESA. + */<br>
> +ir_visitor_status<br>
> +lower_clip_distance_visitor::visit_leave(ir_call *ir) +{ + void<br>
> *ctx = ralloc_parent(ir); + + const exec_node *formal_param_node<br>
> = ir->get_callee()->parameters.head; + const exec_node<br>
> *actual_param_node = ir->actual_parameters.head; + while<br>
> (!actual_param_node->is_tail_sentinel()) { + ir_variable<br>
> *formal_param = (ir_variable *) formal_param_node; + ir_rvalue<br>
> *actual_param = (ir_rvalue *) actual_param_node; + + /*<br>
> Advance formal_param_node and actual_param_node now so that we can<br>
> + * safely replace actual_param with another node, if<br>
> necessary, below. + */ + formal_param_node =<br>
> formal_param_node->next; + actual_param_node =<br>
> actual_param_node->next; + + ir_dereference_variable *deref =<br>
> actual_param->as_dereference_variable(); + if (deref &&<br>
> deref->variable_referenced() + ==<br>
> this->old_clip_distance_var) { + /* User is trying to pass<br>
> gl_ClipDistance as an in, out, or inout + * parameter to a<br>
> function call. Since we are reshaping + * gl_ClipDistance<br>
> from an array of floats to an array of vec4's, + * this<br>
> isn't going to work anymore, so use a temporary array + *<br>
> instead. + */ + ir_variable *temp_clip_distance =<br>
> new(ctx) ir_variable( + actual_param->type,<br>
> "temp_clip_distance", ir_var_temporary); +<br>
> this->base_ir->insert_before(temp_clip_distance); +<br>
> actual_param->replace_with( + new(ctx)<br>
> ir_dereference_variable(temp_clip_distance)); + if<br>
> (formal_param->mode == ir_var_in + ||<br>
> formal_param->mode == ir_var_inout) { + /* Copy from<br>
> gl_ClipDistance to the temporary before the call. + *<br>
> Since we are going to insert this copy before the current +<br>
> * instruction, we need to visit it afterwards to make sure it +<br>
> * gets lowered. + */ + ir_assignment<br>
> *new_assignment = new(ctx) ir_assignment( + new(ctx)<br>
> ir_dereference_variable(temp_clip_distance), +<br>
> actual_param->clone(ctx, NULL)); +<br>
> this->base_ir->insert_before(new_assignment); +<br>
> this->visit_new_assignment(new_assignment); + } +<br>
> if (formal_param->mode == ir_var_out + ||<br>
> formal_param->mode == ir_var_inout) { + /* Copy from the<br>
> temporary to gl_ClipDistance after the call. + * Since<br>
> visit_list_elements() has already decided which + *<br>
> instruction it's going to visit next, we need to visit +<br>
> * afterwards to make sure it gets lowered. + */ +<br>
> ir_assignment *new_assignment = new(ctx) ir_assignment( +<br>
> actual_param->clone(ctx, NULL), + new(ctx)<br>
> ir_dereference_variable(temp_clip_distance)); +<br>
> this->base_ir->insert_after(new_assignment); +<br>
> this->visit_new_assignment(new_assignment); + } + } +<br>
> } + + return visit_continue; +} + + +bool<br>
> +lower_clip_distance(exec_list *instructions) +{ +<br>
> lower_clip_distance_visitor v; + + visit_list_elements(&v,<br>
> instructions); + + return v.progress; +} diff --git<br>
> a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h index<br>
> 8671ecd..ef04749 100644 --- a/src/mesa/main/mtypes.h +++<br>
> b/src/mesa/main/mtypes.h @@ -2297,6 +2297,7 @@ struct<br>
> gl_shader_compiler_options GLboolean EmitNoMainReturn;<br>
> /**< Emit CONT/RET opcodes? */ GLboolean EmitNoNoise;<br>
> /**< Emit NOISE opcodes? */ GLboolean EmitNoPow;<br>
> /**< Emit POW opcodes? */ + GLboolean LowerClipDistance; /**<<br>
> Lower gl_ClipDistance from float[8] to vec4[2]? */<br>
><br>
> /** * \name Forms of indirect addressing the driver cannot do.<br>
<br>
</div></div>-----BEGIN PGP SIGNATURE-----<br>
Version: GnuPG v1.4.11 (GNU/Linux)<br>
Comment: Using GnuPG with Mozilla - <a href="http://enigmail.mozdev.org/" target="_blank">http://enigmail.mozdev.org/</a><br>
<br>
iEYEARECAAYFAk53hZUACgkQX1gOwKyEAw/wVQCfbjFA83RlnUOSXd57EbPCGDQl<br>
WvYAnRyiiNA6JL1K6q3wKdLgkg/vyjFh<br>
=9UAG<br>
-----END PGP SIGNATURE-----<br>
</blockquote></div><br>