From 77f3fbb4aa3853785885de4f64e3d6e325b97af8 Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Fri, 11 Oct 2019 16:02:25 +0200 Subject: [PATCH] glsl: Add opcodes for atan and atan2 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 --- src/compiler/glsl/glsl_to_nir.cpp | 9 +++++++++ src/compiler/glsl/ir.cpp | 2 ++ src/compiler/glsl/ir_expression_operation.py | 3 +++ src/compiler/glsl/ir_validate.cpp | 13 +++++++++++++ src/mesa/program/ir_to_mesa.cpp | 2 ++ src/mesa/state_tracker/st_glsl_to_tgsi.cpp | 2 ++ 6 files changed, 31 insertions(+) diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp index 2238bf68044..c9bba912622 100644 --- a/src/compiler/glsl/glsl_to_nir.cpp +++ b/src/compiler/glsl/glsl_to_nir.cpp @@ -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]); diff --git a/src/compiler/glsl/ir.cpp b/src/compiler/glsl/ir.cpp index f25ee3ee144..1144cbcae8d 100644 --- a/src/compiler/glsl/ir.cpp +++ b/src/compiler/glsl/ir.cpp @@ -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()) { diff --git a/src/compiler/glsl/ir_expression_operation.py b/src/compiler/glsl/ir_expression_operation.py index c80314a10dd..757546d28e1 100644 --- a/src/compiler/glsl/ir_expression_operation.py +++ b/src/compiler/glsl/ir_expression_operation.py @@ -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}"), diff --git a/src/compiler/glsl/ir_validate.cpp b/src/compiler/glsl/ir_validate.cpp index dea98f2fa77..d0ffad0bd39 100644 --- a/src/compiler/glsl/ir_validate.cpp +++ b/src/compiler/glsl/ir_validate.cpp @@ -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()); diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp index 679b46ba7ee..453dff09e45 100644 --- a/src/mesa/program/ir_to_mesa.cpp +++ b/src/mesa/program/ir_to_mesa.cpp @@ -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; diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp index 5a3226e4351..1e9138ac1d4 100644 --- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp +++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp @@ -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()"); -- 2.30.2