Merge branch 'wip/i965-separate-sampler-tex' into vulkan
[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 #include "main/mtypes.h"
30
31 static bool
32 convert_instr(nir_intrinsic_instr *instr)
33 {
34 if (instr->intrinsic != nir_intrinsic_load_var)
35 return false;
36
37 nir_variable *var = instr->variables[0]->var;
38 if (var->data.mode != nir_var_system_value)
39 return false;
40
41 void *mem_ctx = ralloc_parent(instr);
42
43 nir_intrinsic_op op = nir_intrinsic_from_system_value(var->data.location);
44 nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
45
46 if (instr->dest.is_ssa) {
47 nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
48 instr->dest.ssa.num_components, NULL);
49 nir_ssa_def_rewrite_uses(&instr->dest.ssa,
50 nir_src_for_ssa(&new_instr->dest.ssa));
51 } else {
52 nir_dest_copy(&new_instr->dest, &instr->dest, mem_ctx);
53 }
54
55 nir_instr_insert_before(&instr->instr, &new_instr->instr);
56 nir_instr_remove(&instr->instr);
57
58 return true;
59 }
60
61 static bool
62 convert_block(nir_block *block, void *state)
63 {
64 bool *progress = state;
65
66 nir_foreach_instr_safe(block, instr) {
67 if (instr->type == nir_instr_type_intrinsic)
68 *progress = convert_instr(nir_instr_as_intrinsic(instr)) || *progress;
69 }
70
71 return true;
72 }
73
74 static bool
75 convert_impl(nir_function_impl *impl)
76 {
77 bool progress = false;
78
79 nir_foreach_block(impl, convert_block, &progress);
80 nir_metadata_preserve(impl, nir_metadata_block_index |
81 nir_metadata_dominance);
82 return progress;
83 }
84
85 bool
86 nir_lower_system_values(nir_shader *shader)
87 {
88 bool progress = false;
89
90 nir_foreach_overload(shader, overload) {
91 if (overload->impl)
92 progress = convert_impl(overload->impl) || progress;
93 }
94
95 exec_list_make_empty(&shader->system_values);
96
97 return progress;
98 }