2 * Copyright © 2015 Broadcom
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:
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
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
24 #include "compiler/v3d_compiler.h"
25 #include "compiler/nir/nir_builder.h"
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.
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().
38 replace_intrinsic_with_vec(nir_builder
*b
, nir_intrinsic_instr
*intr
,
42 /* Batch things back together into a vector. This will get split by
43 * the later ALU scalarization pass.
45 nir_ssa_def
*vec
= nir_vec(b
, comps
, intr
->num_components
);
47 /* Replace the old intrinsic with a reference to our reconstructed
50 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
, nir_src_for_ssa(vec
));
51 nir_instr_remove(&intr
->instr
);
55 v3d_nir_lower_uniform(struct v3d_compile
*c
, nir_builder
*b
,
56 nir_intrinsic_instr
*intr
)
58 b
->cursor
= nir_before_instr(&intr
->instr
);
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
);
69 /* Convert the uniform offset to bytes. If it happens
70 * to be a constant, constant-folding will clean up
73 nir_intrinsic_set_base(intr_comp
,
74 nir_intrinsic_base(intr
) * 16 +
78 nir_src_for_ssa(nir_ishl(b
, intr
->src
[0].ssa
,
81 dests
[i
] = &intr_comp
->dest
.ssa
;
83 nir_builder_instr_insert(b
, &intr_comp
->instr
);
86 replace_intrinsic_with_vec(b
, intr
, dests
);
90 v3d_nir_lower_io_instr(struct v3d_compile
*c
, nir_builder
*b
,
91 struct nir_instr
*instr
)
93 if (instr
->type
!= nir_instr_type_intrinsic
)
95 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
97 switch (intr
->intrinsic
) {
98 case nir_intrinsic_load_input
:
101 case nir_intrinsic_load_uniform
:
102 v3d_nir_lower_uniform(c
, b
, intr
);
105 case nir_intrinsic_load_user_clip_plane
:
112 v3d_nir_lower_io_impl(struct v3d_compile
*c
, nir_function_impl
*impl
)
115 nir_builder_init(&b
, impl
);
117 nir_foreach_block(block
, impl
) {
118 nir_foreach_instr_safe(instr
, block
)
119 v3d_nir_lower_io_instr(c
, &b
, instr
);
122 nir_metadata_preserve(impl
, nir_metadata_block_index
|
123 nir_metadata_dominance
);
129 v3d_nir_lower_io(nir_shader
*s
, struct v3d_compile
*c
)
131 nir_foreach_function(function
, s
) {
133 v3d_nir_lower_io_impl(c
, function
->impl
);