glsl: Extend lowering pass for gl_ClipDistance to support other arrays (v4)

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 <tobias.johannes.klausmann@mni.thm.de>
Signed-off-by: Dave Airlie <airlied@redhat.com>
Reviewed-by: Kristian Høgsberg <krh@bitplanet.net>
This commit is contained in:
Tobias Klausmann 2016-05-08 22:44:05 +02:00 committed by Dave Airlie
parent dd3390e12f
commit ad355652c2
3 changed files with 160 additions and 95 deletions

View File

@ -117,7 +117,9 @@ bool lower_variable_index_to_cond_assign(gl_shader_stage stage,
bool lower_temp, bool lower_uniform); bool lower_temp, bool lower_uniform);
bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz); bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz);
bool lower_const_arrays_to_uniforms(exec_list *instructions); 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); void lower_output_reads(unsigned stage, exec_list *instructions);
bool lower_packing_builtins(exec_list *instructions, int op_mask); bool lower_packing_builtins(exec_list *instructions, int op_mask);
void lower_shared_reference(struct gl_shader *shader, unsigned *shared_size); void lower_shared_reference(struct gl_shader *shader, unsigned *shared_size);

View File

@ -4561,7 +4561,9 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
goto done; goto done;
if (ctx->Const.ShaderCompilerOptions[i].LowerCombinedClipCullDistance) { 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) { if (ctx->Const.LowerTessLevel) {

View File

@ -25,14 +25,17 @@
* \file lower_distance.cpp * \file lower_distance.cpp
* *
* This pass accounts for the difference between the way * This pass accounts for the difference between the way
* gl_ClipDistance is declared in standard GLSL (as an array of * gl_ClipDistance or gl_CullDistance is declared in standard GLSL
* floats), and the way it is frequently implemented in hardware (as * (as an array of floats), and the way it is frequently implemented
* a pair of vec4s, with four clip distances packed into each). * 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 * The declarations of gl_ClipDistance or gl_CullDistance are replaced
* of gl_ClipDistanceMESA, and any references to gl_ClipDistance are * with a single declaration of gl_ClipDistanceMESA.
* translated to refer to gl_ClipDistanceMESA with the appropriate * Any references to the original gl_ClipDistance or gl_CullDistance
* swizzling of array indices. For instance: * are translated to refer to gl_ClipDistanceMESA with the appropriate
* swizzling of array indices.
* For instance:
* *
* gl_ClipDistance[i] * gl_ClipDistance[i]
* *
@ -40,9 +43,9 @@
* *
* gl_ClipDistanceMESA[i>>2][i&3] * gl_ClipDistanceMESA[i>>2][i&3]
* *
* Since some hardware may not internally represent gl_ClipDistance as a pair * Since some hardware may not internally represent these arrays as a
* of vec4's, this lowering pass is optional. To enable it, set the * pair of vec4's, this lowering pass is optional. To enable it, set
* LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true. * the LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true.
*/ */
#include "glsl_symbol_table.h" #include "glsl_symbol_table.h"
@ -54,10 +57,16 @@ namespace {
class lower_distance_visitor : public ir_rvalue_visitor { class lower_distance_visitor : public ir_rvalue_visitor {
public: 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), : progress(false), old_distance_out_var(NULL),
old_distance_in_var(NULL), new_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; bool progress;
/** /**
* Pointer to the declaration of gl_ClipDistance, if found. * Pointer to the declaration of ou arrays, if found.
* *
* Note: * Note:
* *
@ -91,7 +100,7 @@ public:
ir_variable *old_distance_in_var; 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_out_var;
ir_variable *new_distance_in_var; ir_variable *new_distance_in_var;
@ -100,13 +109,26 @@ public:
* Type of shader we are compiling (e.g. MESA_SHADER_VERTEX) * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
*/ */
const gl_shader_stage shader_stage; 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 */ } /* anonymous namespace */
/** /**
* Replace any declaration of gl_ClipDistance as an array of floats with a * Replace any declaration of in_name as an array of floats with a
* declaration of gl_ClipDistanceMESA as an array of vec4's. * declaration of out_name as an array of vec4's.
*/ */
ir_visitor_status ir_visitor_status
lower_distance_visitor::visit(ir_variable *ir) lower_distance_visitor::visit(ir_variable *ir)
@ -114,7 +136,7 @@ lower_distance_visitor::visit(ir_variable *ir)
ir_variable **old_var; ir_variable **old_var;
ir_variable **new_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; return visit_continue;
assert (ir->type->is_array()); assert (ir->type->is_array());
@ -135,8 +157,8 @@ lower_distance_visitor::visit(ir_variable *ir)
this->progress = true; this->progress = true;
if (!ir->type->fields.array->is_array()) { if (!ir->type->fields.array->is_array()) {
/* gl_ClipDistance (used for vertex, tessellation evaluation and /* gl_ClipDistance / gl_CullDistance (used for vertex, tessellation
* geometry output, and fragment input). * evaluation and geometry output, and fragment input).
*/ */
assert((ir->data.mode == ir_var_shader_in && assert((ir->data.mode == ir_var_shader_in &&
this->shader_stage == MESA_SHADER_FRAGMENT) || this->shader_stage == MESA_SHADER_FRAGMENT) ||
@ -147,21 +169,17 @@ lower_distance_visitor::visit(ir_variable *ir)
*old_var = ir; *old_var = ir;
assert (ir->type->fields.array == glsl_type::float_type); assert (ir->type->fields.array == glsl_type::float_type);
unsigned new_size = (ir->type->array_size() + 3) / 4; *new_var = out_var;
if (replace_var == true) {
/* Clone the old var so that we inherit all of its properties */ ir->replace_with(*new_var);
*new_var = ir->clone(ralloc_parent(ir), NULL); replace_var = false;
} else {
/* And change the properties that we need to change */ ir->remove();
(*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);
} else { } else {
/* 2D gl_ClipDistance (used for tessellation control, tessellation /* 2D gl_ClipDistance / gl_CullDistance (used for tessellation control,
* evaluation and geometry input, and tessellation control output). * tessellation evaluation and geometry input, and tessellation control
* output).
*/ */
assert((ir->data.mode == ir_var_shader_in && assert((ir->data.mode == ir_var_shader_in &&
(this->shader_stage == MESA_SHADER_GEOMETRY || (this->shader_stage == MESA_SHADER_GEOMETRY ||
@ -170,20 +188,13 @@ lower_distance_visitor::visit(ir_variable *ir)
*old_var = ir; *old_var = ir;
assert (ir->type->fields.array->fields.array == glsl_type::float_type); assert (ir->type->fields.array->fields.array == glsl_type::float_type);
unsigned new_size = (ir->type->fields.array->array_size() + 3) / 4; *new_var = out_var;
if (replace_var == true) {
/* Clone the old var so that we inherit all of its properties */ ir->replace_with(*new_var);
*new_var = ir->clone(ralloc_parent(ir), NULL); replace_var = false;
} else {
/* And change the properties that we need to change */ ir->remove();
(*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);
} }
return visit_continue; 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. * 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 * \param swizzle_index Selects a component within the vec4 selected by
* array_index. * 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(); ir_constant *old_index_constant = old_index->constant_expression_value();
if (old_index_constant) { if (old_index_constant) {
/* gl_ClipDistance is being accessed via a constant index. Don't bother /* gl_ClipDistance / gl_CullDistance is being accessed via a constant
* creating expressions to calculate the lowered indices. Just create * index. Don't bother creating expressions to calculate the lowered
* constants. * indices. Just create constants.
*/ */
int const_val = old_index_constant->get_int_component(0); int const_val = old_index_constant->get_int_component(0);
array_index = new(ctx) ir_constant(const_val / 4); uint8_t offset = is_cull ? num_clip_dist : 0;
swizzle_index = new(ctx) ir_constant(const_val % 4); array_index = new(ctx) ir_constant((const_val + offset) / 4);
swizzle_index = new(ctx) ir_constant((const_val + offset) % 4);
} else { } else {
/* Create a variable to hold the value of old_index (so that we /* Create a variable to hold the value of old_index (so that we
* don't compute it twice). * 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( this->base_ir->insert_before(new(ctx) ir_assignment(
new(ctx) ir_dereference_variable(old_index_var), old_index)); new(ctx) ir_dereference_variable(old_index_var), old_index));
/* Create the expression distance_index / 4. Do this as a bit /* Create the expression distance_index / 4. Do this as a bit shift
* shift because that's likely to be more efficient. * because that's likely to be more efficient.
*/ */
array_index = new(ctx) ir_expression( array_index = new(ctx) ir_expression(
ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var), ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var),
new(ctx) ir_constant(2)); new(ctx) ir_constant(2));
/* Create the expression distance_index % 4. Do this as a bitwise /* Create the expression distance_index % 4. Do this as a bitwise AND
* AND because that's likely to be more efficient. * because that's likely to be more efficient.
*/ */
swizzle_index = new(ctx) ir_expression( swizzle_index = new(ctx) ir_expression(
ir_binop_bit_and, new(ctx) ir_dereference_variable(old_index_var), 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 bool
lower_distance_visitor::is_distance_vec8(ir_rvalue *ir) lower_distance_visitor::is_distance_vec8(ir_rvalue *ir)
{ {
/* Note that geometry shaders contain gl_ClipDistance both as an input /* Note that geometry shaders contain in_name
* (which is a 2D array) and an output (which is a 1D array), so it's * both as an input (which is a 2D array) and an output (which is a 1D
* possible for both this->old_distance_out_var and * 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. * 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) if (array_deref == NULL)
return; return;
/* Replace any expression that indexes one of the floats in gl_ClipDistance /* Replace any expression that indexes one of the floats in in_name
* with an expression that indexes into one of the vec4's in * or in_name with an expression that indexes into one of the vec4's
* gl_ClipDistanceMESA and accesses the appropriate component. * in out_name and accesses the appropriate component.
*/ */
ir_rvalue *lowered_vec8 = ir_rvalue *lowered_vec8 =
this->lower_distance_vec8(array_deref->array); 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 * 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 * 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 * 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.: * 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) || if (this->is_distance_vec8(ir->lhs) ||
this->is_distance_vec8(ir->rhs)) { this->is_distance_vec8(ir->rhs)) {
/* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array /* LHS or RHS of the assignment is the entire 1D in_name array
* (or a 1D slice of a 2D gl_ClipDistance input array). Since we are * (or a 1D slice of a 2D in_name input array). Since we are
* reshaping gl_ClipDistance from an array of floats to an array of * 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 * 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. * 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 * 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 * is preceded and/or followed by assignments that copy the contents of the
* temporary variable to and/or from gl_ClipDistance. Each of these * temporary variable to and/or from in_name. Each of these
* assignments is then lowered to refer to gl_ClipDistanceMESA. * 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 * 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.: * 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; actual_param_node = actual_param_node->next;
if (this->is_distance_vec8(actual_param)) { if (this->is_distance_vec8(actual_param)) {
/* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D /* User is trying to pass the whole 1D in_name array (or a 1D
* slice of a 2D gl_ClipDistance array) to a function call. Since we * slice of a 2D in_name array) to a function call. Since we
* are reshaping gl_ClipDistance from an array of floats to an array * 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 * of vec4's, this isn't going to work anymore, so use a temporary
* array instead. * array instead.
*/ */
ir_variable *temp_clip_distance = new(ctx) ir_variable( ir_variable *temp_distance = new(ctx) ir_variable(
actual_param->type, "temp_clip_distance", ir_var_temporary); actual_param->type, "temp_distance", ir_var_temporary);
this->base_ir->insert_before(temp_clip_distance); this->base_ir->insert_before(temp_distance);
actual_param->replace_with( 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 if (formal_param->data.mode == ir_var_function_in
|| formal_param->data.mode == ir_var_function_inout) { || 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 * Since we are going to insert this copy before the current
* instruction, we need to visit it afterwards to make sure it * instruction, we need to visit it afterwards to make sure it
* gets lowered. * gets lowered.
*/ */
ir_assignment *new_assignment = new(ctx) ir_assignment( 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)); actual_param->clone(ctx, NULL));
this->base_ir->insert_before(new_assignment); this->base_ir->insert_before(new_assignment);
this->visit_new_assignment(new_assignment); this->visit_new_assignment(new_assignment);
} }
if (formal_param->data.mode == ir_var_function_out if (formal_param->data.mode == ir_var_function_out
|| formal_param->data.mode == ir_var_function_inout) { || 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 * Since visit_list_elements() has already decided which
* instruction it's going to visit next, we need to visit * instruction it's going to visit next, we need to visit
* afterwards to make sure it gets lowered. * afterwards to make sure it gets lowered.
*/ */
ir_assignment *new_assignment = new(ctx) ir_assignment( ir_assignment *new_assignment = new(ctx) ir_assignment(
actual_param->clone(ctx, NULL), 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->base_ir->insert_after(new_assignment);
this->visit_new_assignment(new_assignment); this->visit_new_assignment(new_assignment);
} }
@ -557,18 +569,67 @@ lower_distance_visitor::visit_leave(ir_call *ir)
return rvalue_visit(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 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;
visit_list_elements(&v, shader->ir); if (ClipDistanceArraySize == 0 && CullDistanceArraySize == 0)
return false;
if (v.new_distance_out_var) new_var = create_clip_distance_mesa(clipdist ? clipdist : culldist, new_size);
shader->symbols->add_variable(v.new_distance_out_var);
if (v.new_distance_in_var)
shader->symbols->add_variable(v.new_distance_in_var);
return v.progress; 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;
}
shader->symbols->add_variable(new_var);
validate_ir_tree(shader->ir);
return progress;
} }