1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2022 Collabora Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "pan_ir.h"
26bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_cistatic void
29bf215546Sopenharmony_cilower_xfb_output(nir_builder *b, nir_intrinsic_instr *intr,
30bf215546Sopenharmony_ci                 unsigned start_component, unsigned num_components,
31bf215546Sopenharmony_ci                 unsigned buffer, unsigned offset_words)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci        assert(buffer < MAX_XFB_BUFFERS);
34bf215546Sopenharmony_ci        assert(nir_intrinsic_component(intr) == 0); // TODO
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci        /* Transform feedback info in units of words, convert to bytes. */
37bf215546Sopenharmony_ci        uint16_t stride = b->shader->info.xfb_stride[buffer] * 4;
38bf215546Sopenharmony_ci        assert(stride != 0);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci        uint16_t offset = offset_words * 4;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci        nir_ssa_def *index = nir_iadd(b,
43bf215546Sopenharmony_ci                nir_imul(b, nir_load_instance_id(b),
44bf215546Sopenharmony_ci                            nir_load_num_vertices(b)),
45bf215546Sopenharmony_ci                nir_load_vertex_id_zero_base(b));
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci        BITSET_SET(b->shader->info.system_values_read, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
48bf215546Sopenharmony_ci        BITSET_SET(b->shader->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID);
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci        nir_ssa_def *buf = nir_load_xfb_address(b, 64, .base = buffer);
51bf215546Sopenharmony_ci        nir_ssa_def *addr =
52bf215546Sopenharmony_ci                nir_iadd(b, buf, nir_u2u64(b,
53bf215546Sopenharmony_ci                                    nir_iadd_imm(b,
54bf215546Sopenharmony_ci                                                 nir_imul_imm(b, index, stride),
55bf215546Sopenharmony_ci                                                 offset)));
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci        assert(intr->src[0].is_ssa && "must lower XFB before lowering SSA");
58bf215546Sopenharmony_ci        nir_ssa_def *src = intr->src[0].ssa;
59bf215546Sopenharmony_ci        nir_ssa_def *value = nir_channels(b, src, BITFIELD_MASK(num_components) << start_component);
60bf215546Sopenharmony_ci        nir_store_global(b, addr, 4, value, BITFIELD_MASK(num_components));
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistatic bool
64bf215546Sopenharmony_cilower_xfb(nir_builder *b, nir_instr *instr, UNUSED void *data)
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci        if (instr->type != nir_instr_type_intrinsic)
67bf215546Sopenharmony_ci                return false;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci        nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
70bf215546Sopenharmony_ci        if (intr->intrinsic != nir_intrinsic_store_output)
71bf215546Sopenharmony_ci                return false;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci        bool progress = false;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci        b->cursor = nir_before_instr(&intr->instr);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci        for (unsigned i = 0; i < 2; ++i) {
78bf215546Sopenharmony_ci                nir_io_xfb xfb = i ? nir_intrinsic_io_xfb2(intr) : nir_intrinsic_io_xfb(intr);
79bf215546Sopenharmony_ci                for (unsigned j = 0; j < 2; ++j) {
80bf215546Sopenharmony_ci                        if (!xfb.out[j].num_components) continue;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci                        lower_xfb_output(b, intr, i*2 + j,
83bf215546Sopenharmony_ci                                                     xfb.out[j].num_components,
84bf215546Sopenharmony_ci                                                     xfb.out[j].buffer,
85bf215546Sopenharmony_ci                                                     xfb.out[j].offset);
86bf215546Sopenharmony_ci                        progress = true;
87bf215546Sopenharmony_ci                }
88bf215546Sopenharmony_ci        }
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci        nir_instr_remove(instr);
91bf215546Sopenharmony_ci        return progress;
92bf215546Sopenharmony_ci}
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cibool
95bf215546Sopenharmony_cipan_lower_xfb(nir_shader *nir)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci        return nir_shader_instructions_pass(nir, lower_xfb,
98bf215546Sopenharmony_ci                                            nir_metadata_block_index |
99bf215546Sopenharmony_ci                                            nir_metadata_dominance, NULL);
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102