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 #include "compiler/nir/nir_builder.h"
25 #include "pan_ir.h"
26
27 static void
lower_xfb_output(nir_builder * b,nir_intrinsic_instr * intr,unsigned start_component,unsigned num_components,unsigned buffer,unsigned offset_words)28 lower_xfb_output(nir_builder *b, nir_intrinsic_instr *intr,
29 unsigned start_component, unsigned num_components,
30 unsigned buffer, unsigned offset_words)
31 {
32 assert(buffer < MAX_XFB_BUFFERS);
33 assert(nir_intrinsic_component(intr) == 0); // TODO
34
35 /* Transform feedback info in units of words, convert to bytes. */
36 uint16_t stride = b->shader->info.xfb_stride[buffer] * 4;
37 assert(stride != 0);
38
39 uint16_t offset = offset_words * 4;
40
41 nir_def *index = nir_iadd(
42 b, nir_imul(b, nir_load_instance_id(b), nir_load_num_vertices(b)),
43 nir_load_vertex_id_zero_base(b));
44
45 BITSET_SET(b->shader->info.system_values_read,
46 SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
47 BITSET_SET(b->shader->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID);
48
49 nir_def *buf = nir_load_xfb_address(b, 64, .base = buffer);
50 nir_def *addr = nir_iadd(
51 b, buf,
52 nir_u2u64(b, nir_iadd_imm(b, nir_imul_imm(b, index, stride), offset)));
53
54 nir_def *src = intr->src[0].ssa;
55 nir_def *value =
56 nir_channels(b, src, BITFIELD_MASK(num_components) << start_component);
57 nir_store_global(b, addr, 4, value, BITFIELD_MASK(num_components));
58 }
59
60 static bool
lower_xfb(nir_builder * b,nir_intrinsic_instr * intr,UNUSED void * data)61 lower_xfb(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data)
62 {
63 /* In transform feedback programs, vertex ID becomes zero-based, so apply
64 * that lowering even on Valhall.
65 */
66 if (intr->intrinsic == nir_intrinsic_load_vertex_id) {
67 b->cursor = nir_instr_remove(&intr->instr);
68
69 nir_def *repl =
70 nir_iadd(b, nir_load_vertex_id_zero_base(b), nir_load_first_vertex(b));
71
72 nir_def_rewrite_uses(&intr->def, repl);
73 return true;
74 }
75
76 if (intr->intrinsic != nir_intrinsic_store_output)
77 return false;
78
79 bool progress = false;
80
81 b->cursor = nir_before_instr(&intr->instr);
82
83 for (unsigned i = 0; i < 2; ++i) {
84 nir_io_xfb xfb =
85 i ? nir_intrinsic_io_xfb2(intr) : nir_intrinsic_io_xfb(intr);
86 for (unsigned j = 0; j < 2; ++j) {
87 if (!xfb.out[j].num_components)
88 continue;
89
90 lower_xfb_output(b, intr, i * 2 + j, xfb.out[j].num_components,
91 xfb.out[j].buffer, xfb.out[j].offset);
92 progress = true;
93 }
94 }
95
96 nir_instr_remove(&intr->instr);
97 return progress;
98 }
99
100 bool
pan_lower_xfb(nir_shader * nir)101 pan_lower_xfb(nir_shader *nir)
102 {
103 return nir_shader_intrinsics_pass(
104 nir, lower_xfb, nir_metadata_control_flow, NULL);
105 }
106