Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support emitting of native acos and atan functions #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/glsl/builtin_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,11 @@ class builtin_builder {
B1(tan)
B1(asin)
B1(acos)
B1(acos_native)
B1(atan2)
B1(atan)
B1(atan2_native)
B1(atan_native)
B1(sinh)
B1(cosh)
B1(tanh)
Expand Down Expand Up @@ -743,7 +746,15 @@ builtin_builder::find(_mesa_glsl_parse_state *state,
*/
state->uses_builtin_functions = true;

ir_function *f = shader->symbols->get_function(name);
ir_function *f;
if (state->emit_native_acos && strcmp(name,"acos")==0)
f = shader->symbols->get_function("acos_native");
else
if (state->emit_native_atan && strcmp(name,"atan")==0)
f = shader->symbols->get_function("atan_native");
else
f = shader->symbols->get_function(name);

if (f == NULL)
return NULL;

Expand Down Expand Up @@ -944,6 +955,7 @@ builtin_builder::create_builtins()
F(tan)
F(asin)
F(acos)
F(acos_native)

add_function("atan",
_atan(glsl_type::float_type),
Expand All @@ -956,6 +968,17 @@ builtin_builder::create_builtins()
_atan2(glsl_type::vec4_type),
NULL);

add_function("atan_native",
_atan_native(glsl_type::float_type),
_atan_native(glsl_type::vec2_type),
_atan_native(glsl_type::vec3_type),
_atan_native(glsl_type::vec4_type),
_atan2_native(glsl_type::float_type),
_atan2_native(glsl_type::vec2_type),
_atan2_native(glsl_type::vec3_type),
_atan2_native(glsl_type::vec4_type),
NULL);

F(sinh)
F(cosh)
F(tanh)
Expand Down Expand Up @@ -2711,12 +2734,31 @@ builtin_builder::_acos(const glsl_type *type)
{
ir_variable *x = in_var(type, "x");
MAKE_SIG(type, always_available, 1, x);

body.emit(ret(sub(imm(M_PI_2f), asin_expr(x))));
return sig;
}

ir_function_signature *
builtin_builder::_acos_native(const glsl_type *type)
{
ir_variable *x = in_var(type, "x");
MAKE_SIG(type, always_available, 1, x);
body.emit(ret(acos(x)));
return sig;
}


ir_function_signature *
builtin_builder::_atan2_native(const glsl_type *type)
{
ir_variable *vec_y = in_var(type, "vec_y");
ir_variable *vec_x = in_var(type, "vec_x");
MAKE_SIG(type, always_available, 2, vec_y, vec_x);
body.emit(ret(atan2(vec_y, vec_x)));
return sig;
}


ir_function_signature *
builtin_builder::_atan2(const glsl_type *type)
{
Expand Down Expand Up @@ -2825,6 +2867,17 @@ builtin_builder::_atan(const glsl_type *type)
return sig;
}

ir_function_signature *
builtin_builder::_atan_native(const glsl_type *type)
{
ir_variable *y_over_x = in_var(type, "y_over_x");
MAKE_SIG(type, always_available, 1, y_over_x);

body.emit(ret(atan(y_over_x)));

return sig;
}

ir_function_signature *
builtin_builder::_sinh(const glsl_type *type)
{
Expand Down
3 changes: 3 additions & 0 deletions src/glsl/glsl_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ glslopt_shader* glslopt_optimize (glslopt_ctx* ctx, glslopt_shader_type type, co
state->metal_target = true;
state->error = 0;

state->emit_native_acos = (options & kGlslOptionsEmitNativeAcos)!=0;
state->emit_native_atan = (options & kGlslOptionsEmitNativeAtan)!=0;

if (!(options & kGlslOptionSkipPreprocessor))
{
state->error = !!glcpp_preprocess (state, &shaderSource, &state->info_log, state->extensions, &ctx->mesa_ctx);
Expand Down
2 changes: 2 additions & 0 deletions src/glsl/glsl_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ enum glslopt_shader_type {
enum glslopt_options {
kGlslOptionSkipPreprocessor = (1<<0), // Skip preprocessing shader source. Saves some time if you know you don't need it.
kGlslOptionNotFullShader = (1<<1), // Passed shader is not the full shader source. This makes some optimizations weaker.
kGlslOptionsEmitNativeAcos = (1<<2), // don't emit approximation of acos but use acos function instead
kGlslOptionsEmitNativeAtan = (1<<3), // don't emit approximation of atan but use atan function instead
};

// Optimizer target language
Expand Down
2 changes: 2 additions & 0 deletions src/glsl/glsl_parser_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ struct _mesa_glsl_parse_state {
unsigned language_version;
bool had_version_string;
bool had_float_precision;
bool emit_native_acos;
bool emit_native_atan;
gl_shader_stage stage;

/**
Expand Down
11 changes: 10 additions & 1 deletion src/glsl/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,8 @@ enum ir_expression_operation {
ir_unop_cos,
ir_unop_sin_reduced, /**< Reduced range sin. [-pi, pi] */
ir_unop_cos_reduced, /**< Reduced range cos. [-pi, pi] */
ir_unop_acos,
ir_unop_atan,
/*@}*/

/**
Expand Down Expand Up @@ -1514,10 +1516,17 @@ enum ir_expression_operation {
*/
ir_binop_interpolate_at_sample,

/**
* \name Trigonometric operations.
*/
/*@{*/
ir_binop_atan2,
/*@}*/

/**
* A sentinel marking the last of the binary operations.
*/
ir_last_binop = ir_binop_interpolate_at_sample,
ir_last_binop = ir_binop_atan2,

/**
* \name Fused floating-point multiply-add, part of ARB_gpu_shader5.
Expand Down
18 changes: 18 additions & 0 deletions src/glsl/ir_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,24 @@ cos(operand a)
return expr(ir_unop_cos, a);
}

ir_expression *
acos(operand a)
{
return expr(ir_unop_acos, a);
}

ir_expression *
atan(operand a)
{
return expr(ir_unop_atan, a);
}

ir_expression *
atan2(operand a, operand b)
{
return expr(ir_binop_atan2, a, b);
}

ir_expression *
exp(operand a)
{
Expand Down
3 changes: 3 additions & 0 deletions src/glsl/ir_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ ir_expression *abs(operand a);
ir_expression *neg(operand a);
ir_expression *sin(operand a);
ir_expression *cos(operand a);
ir_expression *acos(operand a);
ir_expression *atan(operand a);
ir_expression *atan2(operand a, operand b);
ir_expression *exp(operand a);
ir_expression *rsq(operand a);
ir_expression *sqrt(operand a);
Expand Down
4 changes: 4 additions & 0 deletions src/glsl/ir_print_glsl_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ static const char *const operator_glsl_strs[] = {
"cos",
"sin", // reduced
"cos", // reduced
"acos", // native
"atan", // native
"dFdx",
"dFdxCoarse",
"dFdxFine",
Expand Down Expand Up @@ -698,6 +700,7 @@ static const char *const operator_glsl_strs[] = {
"vectorExtract_TODO",
"interpolateAtOffset",
"interpolateAtSample",
"atan", //atan2 native
"fma",
"clamp",
"mix",
Expand All @@ -724,6 +727,7 @@ static bool is_binop_func_like(ir_expression_operation op, const glsl_type* type
if (op == ir_binop_equal ||
op == ir_binop_nequal ||
op == ir_binop_mod ||
op == ir_binop_atan2 ||
(op >= ir_binop_dot && op <= ir_binop_pow))
return true;
if (type->is_vector() && (op >= ir_binop_less && op <= ir_binop_nequal))
Expand Down
3 changes: 3 additions & 0 deletions src/glsl/ir_print_metal_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ static const char *const operator_glsl_strs[] = {
"cos",
"fast::sin", // reduced
"fast::cos", // reduced
"acos", // native
"atan", // native
"dfdx",
"dfdx", // coarse
"dfdx", // fine
Expand Down Expand Up @@ -889,6 +891,7 @@ static const char *const operator_glsl_strs[] = {
"vectorExtract_TODO",
"interpolateAtOffset_TODO",
"interpolateAtSample_TODO",
"atan2TODO", //atan2 native
"fma",
"clamp",
"mix",
Expand Down