X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;ds=inline;f=src%2Fglsl%2Fopt_algebraic.cpp;h=c6f4a9c786df31315a910b9242735634079639f9;hb=f86902e75d989b781be36ced5dc98dfc0cd34b7b;hp=448af6b505b5edaafc366367216e712e858dd013;hpb=e974781301601f411c376f9a7f5574adf9f0238a;p=mesa.git diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp index 448af6b505b..c6f4a9c786d 100644 --- a/src/glsl/opt_algebraic.cpp +++ b/src/glsl/opt_algebraic.cpp @@ -105,9 +105,45 @@ is_vec_negative_one(ir_constant *ir) } static inline bool -is_vec_basis(ir_constant *ir) +is_valid_vec_const(ir_constant *ir) { - return (ir == NULL) ? false : ir->is_basis(); + if (ir == NULL) + return false; + + if (!ir->type->is_scalar() && !ir->type->is_vector()) + return false; + + return true; +} + +static inline bool +is_less_than_one(ir_constant *ir) +{ + if (!is_valid_vec_const(ir)) + return false; + + unsigned component = 0; + for (int c = 0; c < ir->type->vector_elements; c++) { + if (ir->get_float_component(c) < 1.0f) + component++; + } + + return (component == ir->type->vector_elements); +} + +static inline bool +is_greater_than_zero(ir_constant *ir) +{ + if (!is_valid_vec_const(ir)) + return false; + + unsigned component = 0; + for (int c = 0; c < ir->type->vector_elements; c++) { + if (ir->get_float_component(c) > 0.0f) + component++; + } + + return (component == ir->type->vector_elements); } static void @@ -124,7 +160,9 @@ static ir_expression * try_replace_with_dot(ir_expression *expr0, ir_expression *expr1, void *mem_ctx) { if (expr0 && expr0->operation == ir_binop_add && - expr1 && expr1->operation == ir_binop_add) { + expr0->type->is_float() && + expr1 && expr1->operation == ir_binop_add && + expr1->type->is_float()) { ir_swizzle *x = expr0->operands[0]->as_swizzle(); ir_swizzle *y = expr0->operands[1]->as_swizzle(); ir_swizzle *z = expr1->operands[0]->as_swizzle(); @@ -313,6 +351,20 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) if (op_expr[0]->operation == ir_unop_log2) { return op_expr[0]->operands[0]; } + + if (!options->EmitNoPow && op_expr[0]->operation == ir_binop_mul) { + for (int log2_pos = 0; log2_pos < 2; log2_pos++) { + ir_expression *log2_expr = + op_expr[0]->operands[log2_pos]->as_expression(); + + if (log2_expr && log2_expr->operation == ir_unop_log2) { + return new(mem_ctx) ir_expression(ir_binop_pow, + ir->type, + log2_expr->operands[0], + op_expr[0]->operands[1 - log2_pos]); + } + } + } break; case ir_unop_log2: @@ -479,21 +531,34 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) return ir_constant::zero(mem_ctx, ir->type); - if (is_vec_basis(op_const[0])) { - unsigned component = 0; - for (unsigned c = 0; c < op_const[0]->type->vector_elements; c++) { - if (op_const[0]->value.f[c] == 1.0) - component = c; - } - return new(mem_ctx) ir_swizzle(ir->operands[1], component, 0, 0, 0, 1); - } - if (is_vec_basis(op_const[1])) { - unsigned component = 0; - for (unsigned c = 0; c < op_const[1]->type->vector_elements; c++) { - if (op_const[1]->value.f[c] == 1.0) - component = c; - } - return new(mem_ctx) ir_swizzle(ir->operands[0], component, 0, 0, 0, 1); + for (int i = 0; i < 2; i++) { + if (!op_const[i]) + continue; + + unsigned components[4] = { 0 }, count = 0; + + for (unsigned c = 0; c < op_const[i]->type->vector_elements; c++) { + if (op_const[i]->value.f[c] == 0.0) + continue; + + components[count] = c; + count++; + } + + /* No channels had zero values; bail. */ + if (count >= op_const[i]->type->vector_elements) + break; + + ir_expression_operation op = count == 1 ? + ir_binop_mul : ir_binop_dot; + + /* Swizzle both operands to remove the channels that were zero. */ + return new(mem_ctx) + ir_expression(op, glsl_type::float_type, + new(mem_ctx) ir_swizzle(ir->operands[0], + components, count), + new(mem_ctx) ir_swizzle(ir->operands[1], + components, count)); } break; @@ -519,6 +584,16 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) } break; + case ir_binop_all_equal: + case ir_binop_any_nequal: + if (ir->operands[0]->type->is_scalar() && + ir->operands[1]->type->is_scalar()) + return new(mem_ctx) ir_expression(ir->operation == ir_binop_all_equal + ? ir_binop_equal : ir_binop_nequal, + ir->operands[0], + ir->operands[1]); + break; + case ir_binop_rshift: case ir_binop_lshift: /* 0 >> x == 0 */ @@ -612,6 +687,62 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) break; + case ir_binop_min: + case ir_binop_max: + if (ir->type->base_type != GLSL_TYPE_FLOAT || options->EmitNoSat) + break; + + /* Replace min(max) operations and its commutative combinations with + * a saturate operation + */ + for (int op = 0; op < 2; op++) { + ir_expression *minmax = op_expr[op]; + ir_constant *outer_const = op_const[1 - op]; + ir_expression_operation op_cond = (ir->operation == ir_binop_max) ? + ir_binop_min : ir_binop_max; + + if (!minmax || !outer_const || (minmax->operation != op_cond)) + continue; + + /* Found a min(max) combination. Now try to see if its operands + * meet our conditions that we can do just a single saturate operation + */ + for (int minmax_op = 0; minmax_op < 2; minmax_op++) { + ir_rvalue *inner_val_a = minmax->operands[minmax_op]; + ir_rvalue *inner_val_b = minmax->operands[1 - minmax_op]; + + if (!inner_val_a || !inner_val_b) + continue; + + /* Found a {min|max} ({max|min} (x, 0.0), 1.0) operation and its variations */ + if ((outer_const->is_one() && inner_val_a->is_zero()) || + (inner_val_a->is_one() && outer_const->is_zero())) + return saturate(inner_val_b); + + /* Found a {min|max} ({max|min} (x, 0.0), b) where b < 1.0 + * and its variations + */ + if (is_less_than_one(outer_const) && inner_val_b->is_zero()) + return expr(ir_binop_min, saturate(inner_val_a), outer_const); + + if (!inner_val_b->as_constant()) + continue; + + if (is_less_than_one(inner_val_b->as_constant()) && outer_const->is_zero()) + return expr(ir_binop_min, saturate(inner_val_a), inner_val_b); + + /* Found a {min|max} ({max|min} (x, b), 1.0), where b > 0.0 + * and its variations + */ + if (outer_const->is_one() && is_greater_than_zero(inner_val_b->as_constant())) + return expr(ir_binop_max, saturate(inner_val_a), inner_val_b); + if (inner_val_b->as_constant()->is_one() && is_greater_than_zero(outer_const)) + return expr(ir_binop_max, saturate(inner_val_a), outer_const); + } + } + + break; + case ir_unop_rcp: if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp) return op_expr[0]->operands[0];