nir: Add a lower_vec_to_movs pass
[mesa.git] / src / glsl / nir / nir_lower_system_values.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29
30 static void
31 convert_instr(nir_intrinsic_instr *instr)
32 {
33 if (instr->intrinsic != nir_intrinsic_load_var_vec1 &&
34 instr->intrinsic != nir_intrinsic_load_var_vec2)
35 return;
36
37 nir_variable *var = instr->variables[0]->var;
38 if (var->data.mode != nir_var_system_value)
39 return;
40
41 void *mem_ctx = ralloc_parent(instr);
42
43 nir_intrinsic_op op;
44
45 switch (var->data.location) {
46 case SYSTEM_VALUE_FRONT_FACE:
47 op = nir_intrinsic_load_front_face;
48 break;
49 case SYSTEM_VALUE_VERTEX_ID:
50 op = nir_intrinsic_load_vertex_id;
51 break;
52 case SYSTEM_VALUE_INSTANCE_ID:
53 op = nir_intrinsic_load_instance_id;
54 break;
55 case SYSTEM_VALUE_SAMPLE_ID:
56 op = nir_intrinsic_load_sample_id;
57 break;
58 case SYSTEM_VALUE_SAMPLE_POS:
59 op = nir_intrinsic_load_sample_pos;
60 break;
61 case SYSTEM_VALUE_SAMPLE_MASK_IN:
62 op = nir_intrinsic_load_sample_mask_in;
63 break;
64 case SYSTEM_VALUE_INVOCATION_ID:
65 op = nir_intrinsic_load_invocation_id;
66 break;
67 default:
68 assert(0);
69 break;
70 }
71
72 nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
73 new_instr->dest = nir_dest_copy(instr->dest, mem_ctx);
74 nir_instr_insert_before(&instr->instr, &new_instr->instr);
75 nir_instr_remove(&instr->instr);
76 }
77
78 static bool
79 convert_block(nir_block *block, void *state)
80 {
81 (void) state;
82
83 nir_foreach_instr_safe(block, instr) {
84 if (instr->type == nir_instr_type_intrinsic)
85 convert_instr(nir_instr_as_intrinsic(instr));
86 }
87
88 return true;
89 }
90
91 static void
92 convert_impl(nir_function_impl *impl)
93 {
94 nir_foreach_block(impl, convert_block, NULL);
95 }
96
97 void
98 nir_lower_system_values(nir_shader *shader)
99 {
100 nir_foreach_overload(shader, overload) {
101 if (overload->impl)
102 convert_impl(overload->impl);
103 }
104
105 exec_list_make_empty(&shader->system_values);
106 }