xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_tess_coord_z.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2023 Valve Corporation
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include "nir.h"
7 #include "nir_builder.h"
8 #include "nir_intrinsics.h"
9 #include "shader_enums.h"
10 
11 static bool
lower_tess_coord_z(nir_builder * b,nir_intrinsic_instr * intr,void * state)12 lower_tess_coord_z(nir_builder *b, nir_intrinsic_instr *intr, void *state)
13 {
14    if (intr->intrinsic != nir_intrinsic_load_tess_coord)
15       return false;
16 
17    b->cursor = nir_instr_remove(&intr->instr);
18    nir_def *xy = nir_load_tess_coord_xy(b);
19    nir_def *x = nir_channel(b, xy, 0);
20    nir_def *y = nir_channel(b, xy, 1);
21    nir_def *z;
22 
23    bool *triangles = state;
24    if (*triangles)
25       z = nir_fsub(b, nir_fsub_imm(b, 1.0f, y), x);
26    else
27       z = nir_imm_float(b, 0.0f);
28 
29    nir_def_rewrite_uses(&intr->def, nir_vec3(b, x, y, z));
30    return true;
31 }
32 
33 bool
nir_lower_tess_coord_z(nir_shader * shader,bool triangles)34 nir_lower_tess_coord_z(nir_shader *shader, bool triangles)
35 {
36    return nir_shader_intrinsics_pass(shader, lower_tess_coord_z,
37                                        nir_metadata_control_flow,
38                                        &triangles);
39 }
40