From ad355652c20b245f5f2faa8622e71461e3121a7f Mon Sep 17 00:00:00 2001 From: Tobias Klausmann Date: Sun, 8 May 2016 22:44:05 +0200 Subject: [PATCH] glsl: Extend lowering pass for gl_ClipDistance to support other arrays (v4) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will come in handy when we want to lower gl_CullDistance into gl_CullDistanceMESA. [airlied: drop separate APIs for clip/cull - just use single API to call both passes.] v3: reexamine my sanity, this was pretty broken, the new code creates one copy of gl_ClipDistanceMESA, as the clip distance varying and lowers everything into that in two passes, one for clips one for culls. v4: rework using the passes in clip/cull sizes, instead of the array sizes. Signed-off-by: Tobias Klausmann Signed-off-by: Dave Airlie Reviewed-by: Kristian Høgsberg --- src/compiler/glsl/ir_optimization.h | 4 +- src/compiler/glsl/linker.cpp | 4 +- src/compiler/glsl/lower_distance.cpp | 247 ++++++++++++++++++++++------------- 3 files changed, 160 insertions(+), 95 deletions(-) diff --git a/src/compiler/glsl/ir_optimization.h b/src/compiler/glsl/ir_optimization.h index f9599a3..2a1b709 100644 --- a/src/compiler/glsl/ir_optimization.h +++ b/src/compiler/glsl/ir_optimization.h @@ -117,7 +117,9 @@ bool lower_variable_index_to_cond_assign(gl_shader_stage stage, bool lower_temp, bool lower_uniform); bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz); bool lower_const_arrays_to_uniforms(exec_list *instructions); -bool lower_clip_distance(gl_shader *shader); +bool lower_combined_clip_cull_distance(gl_shader *shader, + uint8_t clipDistanceArraySize, + uint8_t cullDistanceArraySize); void lower_output_reads(unsigned stage, exec_list *instructions); bool lower_packing_builtins(exec_list *instructions, int op_mask); void lower_shared_reference(struct gl_shader *shader, unsigned *shared_size); diff --git a/src/compiler/glsl/linker.cpp b/src/compiler/glsl/linker.cpp index 2a520bc..a85072d 100644 --- a/src/compiler/glsl/linker.cpp +++ b/src/compiler/glsl/linker.cpp @@ -4561,7 +4561,9 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) goto done; if (ctx->Const.ShaderCompilerOptions[i].LowerCombinedClipCullDistance) { - lower_clip_distance(prog->_LinkedShaders[i]); + lower_combined_clip_cull_distance(prog->_LinkedShaders[i], + (uint8_t)prog->LastClipDistanceArraySize, + (uint8_t)prog->LastCullDistanceArraySize); } if (ctx->Const.LowerTessLevel) { diff --git a/src/compiler/glsl/lower_distance.cpp b/src/compiler/glsl/lower_distance.cpp index 301afe4..9546434 100644 --- a/src/compiler/glsl/lower_distance.cpp +++ b/src/compiler/glsl/lower_distance.cpp @@ -25,14 +25,17 @@ * \file lower_distance.cpp * * This pass accounts for the difference between the way - * gl_ClipDistance is declared in standard GLSL (as an array of - * floats), and the way it is frequently implemented in hardware (as - * a pair of vec4s, with four clip distances packed into each). + * gl_ClipDistance or gl_CullDistance is declared in standard GLSL + * (as an array of floats), and the way it is frequently implemented + * in hardware (as a pair of vec4s, with four clip or cull distances + * packed into each). * - * The declaration of gl_ClipDistance is replaced with a declaration - * of gl_ClipDistanceMESA, and any references to gl_ClipDistance are - * translated to refer to gl_ClipDistanceMESA with the appropriate - * swizzling of array indices. For instance: + * The declarations of gl_ClipDistance or gl_CullDistance are replaced + * with a single declaration of gl_ClipDistanceMESA. + * Any references to the original gl_ClipDistance or gl_CullDistance + * are translated to refer to gl_ClipDistanceMESA with the appropriate + * swizzling of array indices. + * For instance: * * gl_ClipDistance[i] * @@ -40,9 +43,9 @@ * * gl_ClipDistanceMESA[i>>2][i&3] * - * Since some hardware may not internally represent gl_ClipDistance as a pair - * of vec4's, this lowering pass is optional. To enable it, set the - * LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true. + * Since some hardware may not internally represent these arrays as a + * pair of vec4's, this lowering pass is optional. To enable it, set + * the LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true. */ #include "glsl_symbol_table.h" @@ -54,10 +57,16 @@ namespace { class lower_distance_visitor : public ir_rvalue_visitor { public: - explicit lower_distance_visitor(gl_shader_stage shader_stage) + explicit lower_distance_visitor(gl_shader_stage shader_stage, + const char *in_name, ir_variable *out_var, + int num_clip_dist, int num_cull_dist, + bool is_cull, bool replace_var) : progress(false), old_distance_out_var(NULL), old_distance_in_var(NULL), new_distance_out_var(NULL), - new_distance_in_var(NULL), shader_stage(shader_stage) + new_distance_in_var(NULL), shader_stage(shader_stage), + in_name(in_name), out_var(out_var), + num_clip_dist(num_clip_dist), num_cull_dist(num_cull_dist), + is_cull(is_cull), replace_var(replace_var) { } @@ -76,7 +85,7 @@ public: bool progress; /** - * Pointer to the declaration of gl_ClipDistance, if found. + * Pointer to the declaration of ou arrays, if found. * * Note: * @@ -91,7 +100,7 @@ public: ir_variable *old_distance_in_var; /** - * Pointer to the newly-created gl_ClipDistanceMESA variable. + * Pointer to the newly-created variable. */ ir_variable *new_distance_out_var; ir_variable *new_distance_in_var; @@ -100,13 +109,26 @@ public: * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX) */ const gl_shader_stage shader_stage; + + /** + * Identifier of the variables we manipulate + */ + + const char *in_name; + + ir_variable *out_var; + uint8_t num_clip_dist; + uint8_t num_cull_dist; + bool is_cull; + bool replace_var; }; } /* anonymous namespace */ + /** - * Replace any declaration of gl_ClipDistance as an array of floats with a - * declaration of gl_ClipDistanceMESA as an array of vec4's. + * Replace any declaration of in_name as an array of floats with a + * declaration of out_name as an array of vec4's. */ ir_visitor_status lower_distance_visitor::visit(ir_variable *ir) @@ -114,7 +136,7 @@ lower_distance_visitor::visit(ir_variable *ir) ir_variable **old_var; ir_variable **new_var; - if (!ir->name || strcmp(ir->name, "gl_ClipDistance") != 0) + if (!ir->name || strcmp(in_name, ir->name) != 0) return visit_continue; assert (ir->type->is_array()); @@ -135,8 +157,8 @@ lower_distance_visitor::visit(ir_variable *ir) this->progress = true; if (!ir->type->fields.array->is_array()) { - /* gl_ClipDistance (used for vertex, tessellation evaluation and - * geometry output, and fragment input). + /* gl_ClipDistance / gl_CullDistance (used for vertex, tessellation + * evaluation and geometry output, and fragment input). */ assert((ir->data.mode == ir_var_shader_in && this->shader_stage == MESA_SHADER_FRAGMENT) || @@ -147,21 +169,17 @@ lower_distance_visitor::visit(ir_variable *ir) *old_var = ir; assert (ir->type->fields.array == glsl_type::float_type); - unsigned new_size = (ir->type->array_size() + 3) / 4; - - /* Clone the old var so that we inherit all of its properties */ - *new_var = ir->clone(ralloc_parent(ir), NULL); - - /* And change the properties that we need to change */ - (*new_var)->name = ralloc_strdup(*new_var, "gl_ClipDistanceMESA"); - (*new_var)->type = glsl_type::get_array_instance(glsl_type::vec4_type, - new_size); - (*new_var)->data.max_array_access = ir->data.max_array_access / 4; - - ir->replace_with(*new_var); + *new_var = out_var; + if (replace_var == true) { + ir->replace_with(*new_var); + replace_var = false; + } else { + ir->remove(); + } } else { - /* 2D gl_ClipDistance (used for tessellation control, tessellation - * evaluation and geometry input, and tessellation control output). + /* 2D gl_ClipDistance / gl_CullDistance (used for tessellation control, + * tessellation evaluation and geometry input, and tessellation control + * output). */ assert((ir->data.mode == ir_var_shader_in && (this->shader_stage == MESA_SHADER_GEOMETRY || @@ -170,20 +188,13 @@ lower_distance_visitor::visit(ir_variable *ir) *old_var = ir; assert (ir->type->fields.array->fields.array == glsl_type::float_type); - unsigned new_size = (ir->type->fields.array->array_size() + 3) / 4; - - /* Clone the old var so that we inherit all of its properties */ - *new_var = ir->clone(ralloc_parent(ir), NULL); - - /* And change the properties that we need to change */ - (*new_var)->name = ralloc_strdup(*new_var, "gl_ClipDistanceMESA"); - (*new_var)->type = glsl_type::get_array_instance( - glsl_type::get_array_instance(glsl_type::vec4_type, - new_size), - ir->type->array_size()); - (*new_var)->data.max_array_access = ir->data.max_array_access / 4; - - ir->replace_with(*new_var); + *new_var = out_var; + if (replace_var == true) { + ir->replace_with(*new_var); + replace_var = false; + } else { + ir->remove(); + } } return visit_continue; @@ -191,10 +202,10 @@ lower_distance_visitor::visit(ir_variable *ir) /** - * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based + * Create the necessary GLSL rvalues to index into out_name based * on the rvalue previously used to index into gl_ClipDistance. * - * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA + * \param array_index Selects one of the vec4's in out_name * \param swizzle_index Selects a component within the vec4 selected by * array_index. */ @@ -215,13 +226,14 @@ lower_distance_visitor::create_indices(ir_rvalue *old_index, ir_constant *old_index_constant = old_index->constant_expression_value(); if (old_index_constant) { - /* gl_ClipDistance is being accessed via a constant index. Don't bother - * creating expressions to calculate the lowered indices. Just create - * constants. + /* gl_ClipDistance / gl_CullDistance is being accessed via a constant + * index. Don't bother creating expressions to calculate the lowered + * indices. Just create constants. */ int const_val = old_index_constant->get_int_component(0); - array_index = new(ctx) ir_constant(const_val / 4); - swizzle_index = new(ctx) ir_constant(const_val % 4); + uint8_t offset = is_cull ? num_clip_dist : 0; + array_index = new(ctx) ir_constant((const_val + offset) / 4); + swizzle_index = new(ctx) ir_constant((const_val + offset) % 4); } else { /* Create a variable to hold the value of old_index (so that we * don't compute it twice). @@ -232,15 +244,15 @@ lower_distance_visitor::create_indices(ir_rvalue *old_index, this->base_ir->insert_before(new(ctx) ir_assignment( new(ctx) ir_dereference_variable(old_index_var), old_index)); - /* Create the expression distance_index / 4. Do this as a bit - * shift because that's likely to be more efficient. + /* Create the expression distance_index / 4. Do this as a bit shift + * because that's likely to be more efficient. */ array_index = new(ctx) ir_expression( ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var), new(ctx) ir_constant(2)); - /* Create the expression distance_index % 4. Do this as a bitwise - * AND because that's likely to be more efficient. + /* Create the expression distance_index % 4. Do this as a bitwise AND + * because that's likely to be more efficient. */ swizzle_index = new(ctx) ir_expression( ir_binop_bit_and, new(ctx) ir_dereference_variable(old_index_var), @@ -260,9 +272,9 @@ lower_distance_visitor::create_indices(ir_rvalue *old_index, bool lower_distance_visitor::is_distance_vec8(ir_rvalue *ir) { - /* Note that geometry shaders contain gl_ClipDistance both as an input - * (which is a 2D array) and an output (which is a 1D array), so it's - * possible for both this->old_distance_out_var and + /* Note that geometry shaders contain in_name + * both as an input (which is a 2D array) and an output (which is a 1D + * array), so it's possible for both this->old_distance_out_var and * this->old_distance_in_var to be non-NULL in the same shader. */ @@ -340,9 +352,9 @@ lower_distance_visitor::handle_rvalue(ir_rvalue **rv) if (array_deref == NULL) return; - /* Replace any expression that indexes one of the floats in gl_ClipDistance - * with an expression that indexes into one of the vec4's in - * gl_ClipDistanceMESA and accesses the appropriate component. + /* Replace any expression that indexes one of the floats in in_name + * or in_name with an expression that indexes into one of the vec4's + * in out_name and accesses the appropriate component. */ ir_rvalue *lowered_vec8 = this->lower_distance_vec8(array_deref->array); @@ -392,12 +404,12 @@ lower_distance_visitor::fix_lhs(ir_assignment *ir) } /** - * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as + * Replace any assignment having the 1D in_name (undereferenced) as * its LHS or RHS with a sequence of assignments, one for each component of * the array. Each of these assignments is lowered to refer to - * gl_ClipDistanceMESA as appropriate. + * out_name as appropriate. * - * We need to do a similar replacement for 2D gl_ClipDistance, however since + * We need to do a similar replacement for 2D in_name, however since * it's an input, the only case we need to address is where a 1D slice of it * is the entire RHS of an assignment, e.g.: * @@ -413,9 +425,9 @@ lower_distance_visitor::visit_leave(ir_assignment *ir) if (this->is_distance_vec8(ir->lhs) || this->is_distance_vec8(ir->rhs)) { - /* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array - * (or a 1D slice of a 2D gl_ClipDistance input array). Since we are - * reshaping gl_ClipDistance from an array of floats to an array of + /* LHS or RHS of the assignment is the entire 1D in_name array + * (or a 1D slice of a 2D in_name input array). Since we are + * reshaping in_name from an array of floats to an array of * vec4's, this isn't going to work as a bulk assignment anymore, so * unroll it to element-by-element assignments and lower each of them. * @@ -484,13 +496,13 @@ lower_distance_visitor::visit_new_assignment(ir_assignment *ir) /** - * If a 1D gl_ClipDistance variable appears as an argument in an ir_call + * If a 1D in_name variable appears as an argument in an ir_call * expression, replace it with a temporary variable, and make sure the ir_call * is preceded and/or followed by assignments that copy the contents of the - * temporary variable to and/or from gl_ClipDistance. Each of these - * assignments is then lowered to refer to gl_ClipDistanceMESA. + * temporary variable to and/or from in_name. Each of these + * assignments is then lowered to refer to out_name. * - * We need to do a similar replacement for 2D gl_ClipDistance, however since + * We need to do a similar replacement for 2D in_name, however since * it's an input, the only case we need to address is where a 1D slice of it * is passed as an "in" parameter to an ir_call, e.g.: * @@ -514,40 +526,40 @@ lower_distance_visitor::visit_leave(ir_call *ir) actual_param_node = actual_param_node->next; if (this->is_distance_vec8(actual_param)) { - /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D - * slice of a 2D gl_ClipDistance array) to a function call. Since we - * are reshaping gl_ClipDistance from an array of floats to an array + /* User is trying to pass the whole 1D in_name array (or a 1D + * slice of a 2D in_name array) to a function call. Since we + * are reshaping in_name from an array of floats to an array * of vec4's, this isn't going to work anymore, so use a temporary * array instead. */ - ir_variable *temp_clip_distance = new(ctx) ir_variable( - actual_param->type, "temp_clip_distance", ir_var_temporary); - this->base_ir->insert_before(temp_clip_distance); + ir_variable *temp_distance = new(ctx) ir_variable( + actual_param->type, "temp_distance", ir_var_temporary); + this->base_ir->insert_before(temp_distance); actual_param->replace_with( - new(ctx) ir_dereference_variable(temp_clip_distance)); + new(ctx) ir_dereference_variable(temp_distance)); if (formal_param->data.mode == ir_var_function_in || formal_param->data.mode == ir_var_function_inout) { - /* Copy from gl_ClipDistance to the temporary before the call. + /* Copy from in_name to the temporary before the call. * Since we are going to insert this copy before the current * instruction, we need to visit it afterwards to make sure it * gets lowered. */ ir_assignment *new_assignment = new(ctx) ir_assignment( - new(ctx) ir_dereference_variable(temp_clip_distance), + new(ctx) ir_dereference_variable(temp_distance), actual_param->clone(ctx, NULL)); this->base_ir->insert_before(new_assignment); this->visit_new_assignment(new_assignment); } if (formal_param->data.mode == ir_var_function_out || formal_param->data.mode == ir_var_function_inout) { - /* Copy from the temporary to gl_ClipDistance after the call. + /* Copy from the temporary to in_name after the call. * Since visit_list_elements() has already decided which * instruction it's going to visit next, we need to visit * afterwards to make sure it gets lowered. */ ir_assignment *new_assignment = new(ctx) ir_assignment( actual_param->clone(ctx, NULL), - new(ctx) ir_dereference_variable(temp_clip_distance)); + new(ctx) ir_dereference_variable(temp_distance)); this->base_ir->insert_after(new_assignment); this->visit_new_assignment(new_assignment); } @@ -557,18 +569,67 @@ lower_distance_visitor::visit_leave(ir_call *ir) return rvalue_visit(ir); } +static ir_variable * +create_clip_distance_mesa(ir_variable *base, int new_size) +{ + ir_variable *new_var; + + if (!base->type->fields.array->is_array()) { + new_var = base->clone(ralloc_parent(base), NULL); + new_var->name = ralloc_strdup(new_var, "gl_ClipDistanceMESA"); + new_var->type = glsl_type::get_array_instance(glsl_type::vec4_type, + new_size); + new_var->data.max_array_access = base->data.max_array_access / 4; + } else { + /* Clone the old var so that we inherit all of its properties */ + new_var = base->clone(ralloc_parent(base), NULL); + /* And change the properties that we need to change */ + new_var->name = ralloc_strdup(new_var, "gl_ClipDistanceMESA"); + new_var->type = glsl_type::get_array_instance( + glsl_type::get_array_instance(glsl_type::vec4_type, + new_size), + base->type->array_size()); + new_var->data.max_array_access = base->data.max_array_access / 4; + } + return new_var; +} bool -lower_clip_distance(gl_shader *shader) +lower_combined_clip_cull_distance(gl_shader *shader, + uint8_t ClipDistanceArraySize, + uint8_t CullDistanceArraySize) { - lower_distance_visitor v(shader->Stage); + ir_variable *clipdist = shader->symbols->get_variable("gl_ClipDistance"); + ir_variable *culldist = shader->symbols->get_variable("gl_CullDistance"); + ir_variable *new_var; + unsigned new_size = ((ClipDistanceArraySize + CullDistanceArraySize) + 3) / 4; + bool progress = false; + bool replace_var = true; + + if (ClipDistanceArraySize == 0 && CullDistanceArraySize == 0) + return false; + + new_var = create_clip_distance_mesa(clipdist ? clipdist : culldist, new_size); - visit_list_elements(&v, shader->ir); + if (ClipDistanceArraySize) { + lower_distance_visitor v(shader->Stage, "gl_ClipDistance", + new_var, ClipDistanceArraySize, CullDistanceArraySize, false, replace_var); + + visit_list_elements(&v, shader->ir); + replace_var = v.replace_var; + progress = v.progress; + } + + if (CullDistanceArraySize) { + lower_distance_visitor v2(shader->Stage, "gl_CullDistance", + new_var, ClipDistanceArraySize, CullDistanceArraySize, true, replace_var); + + visit_list_elements(&v2, shader->ir); + progress |= v2.progress; + } - if (v.new_distance_out_var) - shader->symbols->add_variable(v.new_distance_out_var); - if (v.new_distance_in_var) - shader->symbols->add_variable(v.new_distance_in_var); + shader->symbols->add_variable(new_var); - return v.progress; + validate_ir_tree(shader->ir); + return progress; } -- 2.7.4