OSDN Git Service

43376888248936ca05ab6c49630cd9e8845d85dd
[android-x86/external-mesa.git] / src / gallium / drivers / vc4 / vc4_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 "vc4_qir.h"
25 #include "tgsi/tgsi_info.h"
26 #include "glsl/nir/nir_builder.h"
27
28 /**
29  * Walks the NIR generated by TGSI-to-NIR to lower its io intrinsics into
30  * something amenable to the VC4 architecture.
31  *
32  * Currently, it split outputs into scalars, and drops any non-position values
33  * in coordinate shaders.
34  */
35
36 static void
37 vc4_nir_lower_output(struct vc4_compile *c, nir_builder *b,
38                      nir_intrinsic_instr *intr)
39 {
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]) {
43                         output_var = var;
44                         break;
45                 }
46         }
47         assert(output_var);
48         unsigned semantic_name = output_var->data.location;
49
50         if (c->stage == QSTAGE_COORD &&
51             (semantic_name != TGSI_SEMANTIC_POSITION &&
52              semantic_name != TGSI_SEMANTIC_PSIZE)) {
53                 nir_instr_remove(&intr->instr);
54                 return;
55         }
56
57         /* All TGSI-to-NIR outputs are VEC4. */
58         assert(intr->num_components == 4);
59
60         nir_builder_insert_before_instr(b, &intr->instr);
61
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;
67
68                 assert(intr->src[0].is_ssa);
69                 intr_comp->src[0] = nir_src_for_ssa(nir_swizzle(b,
70                                                                 intr->src[0].ssa,
71                                                                 &i, 1, false));
72                 nir_builder_instr_insert(b, &intr_comp->instr);
73         }
74
75         nir_instr_remove(&intr->instr);
76 }
77
78 static void
79 vc4_nir_lower_io_instr(struct vc4_compile *c, nir_builder *b,
80                        struct nir_instr *instr)
81 {
82         if (instr->type != nir_instr_type_intrinsic)
83                 return;
84         nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
85
86         switch (intr->intrinsic) {
87         case nir_intrinsic_store_output:
88                 vc4_nir_lower_output(c, b, intr);
89                 break;
90
91         default:
92                 break;
93         }
94 }
95
96 static bool
97 vc4_nir_lower_io_block(nir_block *block, void *arg)
98 {
99         struct vc4_compile *c = arg;
100         nir_function_impl *impl =
101                 nir_cf_node_get_function(&block->cf_node);
102
103         nir_builder b;
104         nir_builder_init(&b, impl);
105
106         nir_foreach_instr_safe(block, instr)
107                 vc4_nir_lower_io_instr(c, &b, instr);
108
109         return true;
110 }
111
112 static bool
113 vc4_nir_lower_io_impl(struct vc4_compile *c, nir_function_impl *impl)
114 {
115         nir_foreach_block(impl, vc4_nir_lower_io_block, c);
116
117         nir_metadata_preserve(impl, nir_metadata_block_index |
118                               nir_metadata_dominance);
119
120         return true;
121 }
122
123 void
124 vc4_nir_lower_io(struct vc4_compile *c)
125 {
126         nir_foreach_overload(c->s, overload) {
127                 if (overload->impl)
128                         vc4_nir_lower_io_impl(c, overload->impl);
129         }
130 }