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
25 #include "tgsi/tgsi_info.h"
26 #include "glsl/nir/nir_builder.h"
29 * Walks the NIR generated by TGSI-to-NIR to lower its io intrinsics into
30 * something amenable to the VC4 architecture.
32 * Currently, it split outputs into scalars, and drops any non-position values
33 * in coordinate shaders.
37 vc4_nir_lower_output(struct vc4_compile *c, nir_builder *b,
38 nir_intrinsic_instr *intr)
40 nir_variable *output_var = NULL;
41 foreach_list_typed(nir_variable, var, node, &c->s->outputs) {
42 if (var->data.driver_location == intr->const_index[0]) {
48 unsigned semantic_name = output_var->data.location;
50 if (c->stage == QSTAGE_COORD &&
51 (semantic_name != TGSI_SEMANTIC_POSITION &&
52 semantic_name != TGSI_SEMANTIC_PSIZE)) {
53 nir_instr_remove(&intr->instr);
57 /* All TGSI-to-NIR outputs are VEC4. */
58 assert(intr->num_components == 4);
60 nir_builder_insert_before_instr(b, &intr->instr);
62 for (unsigned i = 0; i < intr->num_components; i++) {
63 nir_intrinsic_instr *intr_comp =
64 nir_intrinsic_instr_create(c->s, nir_intrinsic_store_output);
65 intr_comp->num_components = 1;
66 intr_comp->const_index[0] = intr->const_index[0] * 4 + i;
68 assert(intr->src[0].is_ssa);
69 intr_comp->src[0] = nir_src_for_ssa(nir_swizzle(b,
72 nir_builder_instr_insert(b, &intr_comp->instr);
75 nir_instr_remove(&intr->instr);
79 vc4_nir_lower_io_instr(struct vc4_compile *c, nir_builder *b,
80 struct nir_instr *instr)
82 if (instr->type != nir_instr_type_intrinsic)
84 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
86 switch (intr->intrinsic) {
87 case nir_intrinsic_store_output:
88 vc4_nir_lower_output(c, b, intr);
97 vc4_nir_lower_io_block(nir_block *block, void *arg)
99 struct vc4_compile *c = arg;
100 nir_function_impl *impl =
101 nir_cf_node_get_function(&block->cf_node);
104 nir_builder_init(&b, impl);
106 nir_foreach_instr_safe(block, instr)
107 vc4_nir_lower_io_instr(c, &b, instr);
113 vc4_nir_lower_io_impl(struct vc4_compile *c, nir_function_impl *impl)
115 nir_foreach_block(impl, vc4_nir_lower_io_block, c);
117 nir_metadata_preserve(impl, nir_metadata_block_index |
118 nir_metadata_dominance);
124 vc4_nir_lower_io(struct vc4_compile *c)
126 nir_foreach_overload(c->s, overload) {
128 vc4_nir_lower_io_impl(c, overload->impl);