2025-01-09 18:41:38 -06:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2021 Valve Corporation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-20 15:43:54 +01:00
|
|
|
#include "nir.h"
|
2025-01-09 18:41:38 -06:00
|
|
|
#include "nir_builder.h"
|
|
|
|
|
|
|
|
|
|
static bool
|
2026-03-20 15:43:54 +01:00
|
|
|
normalize_sin_cos(struct nir_builder *b, nir_alu_instr *sincos, UNUSED void *_)
|
2025-01-09 18:41:38 -06:00
|
|
|
{
|
2026-01-07 11:38:02 +01:00
|
|
|
if (sincos->op != nir_op_fsin && sincos->op != nir_op_fcos)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
b->cursor = nir_before_instr(&sincos->instr);
|
2026-01-07 11:39:12 +01:00
|
|
|
b->fp_math_ctrl = sincos->fp_math_ctrl;
|
2025-01-09 18:41:38 -06:00
|
|
|
|
|
|
|
|
nir_def *src = nir_fmul_imm(b, nir_ssa_for_alu_src(b, sincos, 0), 0.15915493667125702);
|
2026-03-20 14:38:43 +01:00
|
|
|
nir_def *replace = sincos->op == nir_op_fsin ? nir_fsin_normalized_2_pi(b, src) : nir_fcos_normalized_2_pi(b, src);
|
2026-01-07 11:38:02 +01:00
|
|
|
nir_def_replace(&sincos->def, replace);
|
|
|
|
|
|
|
|
|
|
return true;
|
2025-01-09 18:41:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2026-03-20 15:43:54 +01:00
|
|
|
nir_normalize_sin_cos(nir_shader *shader)
|
2025-01-09 18:41:38 -06:00
|
|
|
{
|
2026-03-20 15:43:54 +01:00
|
|
|
return nir_shader_alu_pass(shader, normalize_sin_cos, nir_metadata_control_flow, NULL);
|
2025-01-09 18:41:38 -06:00
|
|
|
}
|