glsl: Add opcodes for atan and atan2
authorNeil Roberts <nroberts@igalia.com>
Fri, 11 Oct 2019 14:02:25 +0000 (16:02 +0200)
committerNeil Roberts <nroberts@igalia.com>
Sat, 12 Oct 2019 07:43:18 +0000 (09:43 +0200)
Adds ir_binop_atan2 and ir_unop_atan. When converting to NIR these are
expanded out using the appropriate builtin generator. If they are used
with anything else then it will just hit an assert.

Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
src/compiler/glsl/glsl_to_nir.cpp
src/compiler/glsl/ir.cpp
src/compiler/glsl/ir_expression_operation.py
src/compiler/glsl/ir_validate.cpp
src/mesa/program/ir_to_mesa.cpp
src/mesa/state_tracker/st_glsl_to_tgsi.cpp

index 2238bf68044a5fdffa0654aa538790a31a772288..c9bba912622f7b109c616b04b8b3ca9299fc2035 100644 (file)
@@ -34,6 +34,7 @@
 #include "program.h"
 #include "compiler/nir/nir_control_flow.h"
 #include "compiler/nir/nir_builder.h"
+#include "compiler/nir/nir_builtin_builder.h"
 #include "compiler/nir/nir_deref.h"
 #include "main/errors.h"
 #include "main/imports.h"
@@ -2187,6 +2188,10 @@ nir_visitor::visit(ir_expression *ir)
       return;
    }
 
+   case ir_unop_atan:
+      result = nir_atan(&b, srcs[0]);
+      break;
+
    case ir_binop_add:
       result = type_is_float(out_type) ? nir_fadd(&b, srcs[0], srcs[1])
                                        : nir_iadd(&b, srcs[0], srcs[1]);
@@ -2350,6 +2355,10 @@ nir_visitor::visit(ir_expression *ir)
       break;
    }
 
+   case ir_binop_atan2:
+      result = nir_atan2(&b, srcs[0], srcs[1]);
+      break;
+
    case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
    case ir_triop_fma:
       result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
index f25ee3ee14413bd17e903d2bff6949f3d346c17c..1144cbcae8dd0524e74120a3d6817885914b4878 100644 (file)
@@ -258,6 +258,7 @@ ir_expression::ir_expression(int op, ir_rvalue *op0)
    case ir_unop_bitfield_reverse:
    case ir_unop_interpolate_at_centroid:
    case ir_unop_saturate:
+   case ir_unop_atan:
       this->type = op0->type;
       break;
 
@@ -452,6 +453,7 @@ ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
    case ir_binop_mul:
    case ir_binop_div:
    case ir_binop_mod:
+   case ir_binop_atan2:
       if (op0->type->is_scalar()) {
         this->type = op1->type;
       } else if (op1->type->is_scalar()) {
index c80314a10dde58e4697440c300c3ed4baa966047..757546d28e198e57f083ecdada1a471d5787bf6a 100644 (file)
@@ -512,6 +512,7 @@ ir_expression_operation = [
    # Trigonometric operations.
    operation("sin", 1, source_types=(float_type,), c_expression="sinf({src0})"),
    operation("cos", 1, source_types=(float_type,), c_expression="cosf({src0})"),
+   operation("atan", 1, source_types=(float_type,), c_expression="atan({src0})"),
 
    # Partial derivatives.
    operation("dFdx", 1, source_types=(float_type,), c_expression="0.0f"),
@@ -664,6 +665,8 @@ ir_expression_operation = [
    # operand1 is the sample ID
    operation("interpolate_at_sample", 2),
 
+   operation("atan2", 2, source_types=(float_type,), c_expression="atan2({src0}, {src1})"),
+
    # Fused floating-point multiply-add, part of ARB_gpu_shader5.
    operation("fma", 3, source_types=real_types, c_expression="{src0} * {src1} + {src2}"),
 
index dea98f2fa77620681dbf1800d91ae46145b76292..d0ffad0bd39b57e42ce07afef0ae8dc41f1406f3 100644 (file)
@@ -610,6 +610,12 @@ ir_validate::visit_leave(ir_expression *ir)
       assert(ir->type->base_type == GLSL_TYPE_INT);
       break;
 
+   case ir_unop_atan:
+      assert(ir->operands[0]->type->is_float() ||
+             ir->operands[0]->type->is_double());
+      assert(ir->type == ir->operands[0]->type);
+      break;
+
    case ir_binop_add:
    case ir_binop_sub:
    case ir_binop_mul:
@@ -761,6 +767,13 @@ ir_validate::visit_leave(ir_expression *ir)
       assert(ir->operands[1]->type == glsl_type::int_type);
       break;
 
+   case ir_binop_atan2:
+      assert(ir->operands[0]->type->is_float() ||
+             ir->operands[0]->type->is_double());
+      assert(ir->operands[1]->type == ir->operands[0]->type);
+      assert(ir->type == ir->operands[0]->type);
+      break;
+
    case ir_triop_fma:
       assert(ir->type->is_float() ||
              ir->type->is_double());
index 679b46ba7eee4fa2077580e54b78351842795420..453dff09e453c977fb45b3b7d369605439f7af28 100644 (file)
@@ -1412,6 +1412,8 @@ ir_to_mesa_visitor::visit(ir_expression *ir)
    case ir_unop_unpack_sampler_2x32:
    case ir_unop_pack_image_2x32:
    case ir_unop_unpack_image_2x32:
+   case ir_unop_atan:
+   case ir_binop_atan2:
       assert(!"not supported");
       break;
 
index 5a3226e43517eda5f15619adc32c0054e77240fd..1e9138ac1d43f3a76c133152cca70f0df5036e8d 100644 (file)
@@ -2389,6 +2389,8 @@ glsl_to_tgsi_visitor::visit_expression(ir_expression* ir, st_src_reg *op)
    case ir_binop_carry:
    case ir_binop_borrow:
    case ir_unop_ssbo_unsized_array_length:
+   case ir_unop_atan:
+   case ir_binop_atan2:
       /* This operation is not supported, or should have already been handled.
        */
       assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");