• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Collabora Ltd.
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 
25 #include "pan_ir.h"
26 #include "compiler/nir/nir_builder.h"
27 
28 static void
lower_xfb_output(nir_builder * b,nir_intrinsic_instr * intr,unsigned start_component,unsigned num_components,unsigned buffer,unsigned offset_words)29 lower_xfb_output(nir_builder *b, nir_intrinsic_instr *intr,
30                  unsigned start_component, unsigned num_components,
31                  unsigned buffer, unsigned offset_words)
32 {
33         assert(buffer < MAX_XFB_BUFFERS);
34         assert(nir_intrinsic_component(intr) == 0); // TODO
35 
36         /* Transform feedback info in units of words, convert to bytes. */
37         uint16_t stride = b->shader->info.xfb_stride[buffer] * 4;
38         assert(stride != 0);
39 
40         uint16_t offset = offset_words * 4;
41 
42         nir_ssa_def *index = nir_iadd(b,
43                 nir_imul(b, nir_load_instance_id(b),
44                             nir_load_num_vertices(b)),
45                 nir_load_vertex_id_zero_base(b));
46 
47         BITSET_SET(b->shader->info.system_values_read, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
48         BITSET_SET(b->shader->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID);
49 
50         nir_ssa_def *buf = nir_load_xfb_address(b, 64, .base = buffer);
51         nir_ssa_def *addr =
52                 nir_iadd(b, buf, nir_u2u64(b,
53                                     nir_iadd_imm(b,
54                                                  nir_imul_imm(b, index, stride),
55                                                  offset)));
56 
57         assert(intr->src[0].is_ssa && "must lower XFB before lowering SSA");
58         nir_ssa_def *src = intr->src[0].ssa;
59         nir_ssa_def *value = nir_channels(b, src, BITFIELD_MASK(num_components) << start_component);
60         nir_store_global(b, addr, 4, value, BITFIELD_MASK(num_components));
61 }
62 
63 static bool
lower_xfb(nir_builder * b,nir_instr * instr,UNUSED void * data)64 lower_xfb(nir_builder *b, nir_instr *instr, UNUSED void *data)
65 {
66         if (instr->type != nir_instr_type_intrinsic)
67                 return false;
68 
69         nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
70         if (intr->intrinsic != nir_intrinsic_store_output)
71                 return false;
72 
73         bool progress = false;
74 
75         b->cursor = nir_before_instr(&intr->instr);
76 
77         for (unsigned i = 0; i < 2; ++i) {
78                 nir_io_xfb xfb = i ? nir_intrinsic_io_xfb2(intr) : nir_intrinsic_io_xfb(intr);
79                 for (unsigned j = 0; j < 2; ++j) {
80                         if (!xfb.out[j].num_components) continue;
81 
82                         lower_xfb_output(b, intr, i*2 + j,
83                                                      xfb.out[j].num_components,
84                                                      xfb.out[j].buffer,
85                                                      xfb.out[j].offset);
86                         progress = true;
87                 }
88         }
89 
90         nir_instr_remove(instr);
91         return progress;
92 }
93 
94 bool
pan_lower_xfb(nir_shader * nir)95 pan_lower_xfb(nir_shader *nir)
96 {
97         return nir_shader_instructions_pass(nir, lower_xfb,
98                                             nir_metadata_block_index |
99                                             nir_metadata_dominance, NULL);
100 }
101 
102