v3d: Drop shadow comparison state from shader variant key.
[mesa.git] / src / broadcom / compiler / v3d_nir_lower_io.c
1 /*
2 * Copyright © 2015 Broadcom
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
24 #include "compiler/v3d_compiler.h"
25 #include "compiler/nir/nir_builder.h"
26
27 /**
28 * Walks the NIR generated by TGSI-to-NIR or GLSL-to-NIR to lower its io
29 * intrinsics into something amenable to the V3D architecture.
30 *
31 * Currently, it splits VS inputs and uniforms into scalars, drops any
32 * non-position outputs in coordinate shaders, and fixes up the addressing on
33 * indirect uniform loads. FS input and VS output scalarization is handled by
34 * nir_lower_io_to_scalar().
35 */
36
37 static void
38 replace_intrinsic_with_vec(nir_builder *b, nir_intrinsic_instr *intr,
39 nir_ssa_def **comps)
40 {
41
42 /* Batch things back together into a vector. This will get split by
43 * the later ALU scalarization pass.
44 */
45 nir_ssa_def *vec = nir_vec(b, comps, intr->num_components);
46
47 /* Replace the old intrinsic with a reference to our reconstructed
48 * vector.
49 */
50 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(vec));
51 nir_instr_remove(&intr->instr);
52 }
53
54 static void
55 v3d_nir_lower_uniform(struct v3d_compile *c, nir_builder *b,
56 nir_intrinsic_instr *intr)
57 {
58 b->cursor = nir_before_instr(&intr->instr);
59
60 /* Generate scalar loads equivalent to the original vector. */
61 nir_ssa_def *dests[4];
62 for (unsigned i = 0; i < intr->num_components; i++) {
63 nir_intrinsic_instr *intr_comp =
64 nir_intrinsic_instr_create(c->s, intr->intrinsic);
65 intr_comp->num_components = 1;
66 nir_ssa_dest_init(&intr_comp->instr, &intr_comp->dest, 1,
67 intr->dest.ssa.bit_size, NULL);
68
69 /* Convert the uniform offset to bytes. If it happens
70 * to be a constant, constant-folding will clean up
71 * the shift for us.
72 */
73 nir_intrinsic_set_base(intr_comp,
74 nir_intrinsic_base(intr) * 16 +
75 i * 4);
76
77 intr_comp->src[0] =
78 nir_src_for_ssa(nir_ishl(b, intr->src[0].ssa,
79 nir_imm_int(b, 4)));
80
81 dests[i] = &intr_comp->dest.ssa;
82
83 nir_builder_instr_insert(b, &intr_comp->instr);
84 }
85
86 replace_intrinsic_with_vec(b, intr, dests);
87 }
88
89 static void
90 v3d_nir_lower_io_instr(struct v3d_compile *c, nir_builder *b,
91 struct nir_instr *instr)
92 {
93 if (instr->type != nir_instr_type_intrinsic)
94 return;
95 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
96
97 switch (intr->intrinsic) {
98 case nir_intrinsic_load_input:
99 break;
100
101 case nir_intrinsic_load_uniform:
102 v3d_nir_lower_uniform(c, b, intr);
103 break;
104
105 case nir_intrinsic_load_user_clip_plane:
106 default:
107 break;
108 }
109 }
110
111 static bool
112 v3d_nir_lower_io_impl(struct v3d_compile *c, nir_function_impl *impl)
113 {
114 nir_builder b;
115 nir_builder_init(&b, impl);
116
117 nir_foreach_block(block, impl) {
118 nir_foreach_instr_safe(instr, block)
119 v3d_nir_lower_io_instr(c, &b, instr);
120 }
121
122 nir_metadata_preserve(impl, nir_metadata_block_index |
123 nir_metadata_dominance);
124
125 return true;
126 }
127
128 void
129 v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c)
130 {
131 nir_foreach_function(function, s) {
132 if (function->impl)
133 v3d_nir_lower_io_impl(c, function->impl);
134 }
135 }