1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2020 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 * Authors (Collabora):
24bf215546Sopenharmony_ci *      Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pan_ir.h"
28bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/* TODO: ssbo_size */
31bf215546Sopenharmony_cistatic int
32bf215546Sopenharmony_cipanfrost_sysval_for_ssbo(nir_intrinsic_instr *instr)
33bf215546Sopenharmony_ci{
34bf215546Sopenharmony_ci        nir_src index = instr->src[0];
35bf215546Sopenharmony_ci        assert(nir_src_is_const(index));
36bf215546Sopenharmony_ci        uint32_t uindex = nir_src_as_uint(index);
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci        return PAN_SYSVAL(SSBO, uindex);
39bf215546Sopenharmony_ci}
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistatic int
42bf215546Sopenharmony_cipanfrost_sysval_for_sampler(nir_intrinsic_instr *instr)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci        /* TODO: indirect samplers !!! */
45bf215546Sopenharmony_ci        nir_src index = instr->src[0];
46bf215546Sopenharmony_ci        assert(nir_src_is_const(index));
47bf215546Sopenharmony_ci        uint32_t uindex = nir_src_as_uint(index);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci        return PAN_SYSVAL(SAMPLER, uindex);
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic int
53bf215546Sopenharmony_cipanfrost_sysval_for_image_size(nir_intrinsic_instr *instr)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci        nir_src index = instr->src[0];
56bf215546Sopenharmony_ci        assert(nir_src_is_const(index));
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci        bool is_array = nir_intrinsic_image_array(instr);
59bf215546Sopenharmony_ci        uint32_t uindex = nir_src_as_uint(index);
60bf215546Sopenharmony_ci        unsigned dim = nir_intrinsic_dest_components(instr) - is_array;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci        return PAN_SYSVAL(IMAGE_SIZE, PAN_TXS_SYSVAL_ID(uindex, dim, is_array));
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic unsigned
66bf215546Sopenharmony_cipanfrost_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci        switch (instr->intrinsic) {
69bf215546Sopenharmony_ci        case nir_intrinsic_load_viewport_scale:
70bf215546Sopenharmony_ci                return PAN_SYSVAL_VIEWPORT_SCALE;
71bf215546Sopenharmony_ci        case nir_intrinsic_load_viewport_offset:
72bf215546Sopenharmony_ci                return PAN_SYSVAL_VIEWPORT_OFFSET;
73bf215546Sopenharmony_ci        case nir_intrinsic_load_num_workgroups:
74bf215546Sopenharmony_ci                return PAN_SYSVAL_NUM_WORK_GROUPS;
75bf215546Sopenharmony_ci        case nir_intrinsic_load_workgroup_size:
76bf215546Sopenharmony_ci                return PAN_SYSVAL_LOCAL_GROUP_SIZE;
77bf215546Sopenharmony_ci        case nir_intrinsic_load_work_dim:
78bf215546Sopenharmony_ci                return PAN_SYSVAL_WORK_DIM;
79bf215546Sopenharmony_ci        case nir_intrinsic_load_sample_positions_pan:
80bf215546Sopenharmony_ci                return PAN_SYSVAL_SAMPLE_POSITIONS;
81bf215546Sopenharmony_ci        case nir_intrinsic_load_first_vertex:
82bf215546Sopenharmony_ci        case nir_intrinsic_load_base_vertex:
83bf215546Sopenharmony_ci        case nir_intrinsic_load_base_instance:
84bf215546Sopenharmony_ci                return PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS;
85bf215546Sopenharmony_ci        case nir_intrinsic_load_draw_id:
86bf215546Sopenharmony_ci                return PAN_SYSVAL_DRAWID;
87bf215546Sopenharmony_ci        case nir_intrinsic_load_ssbo_address:
88bf215546Sopenharmony_ci        case nir_intrinsic_get_ssbo_size:
89bf215546Sopenharmony_ci                return panfrost_sysval_for_ssbo(instr);
90bf215546Sopenharmony_ci        case nir_intrinsic_load_xfb_address:
91bf215546Sopenharmony_ci                return PAN_SYSVAL(XFB, nir_intrinsic_base(instr));
92bf215546Sopenharmony_ci        case nir_intrinsic_load_num_vertices:
93bf215546Sopenharmony_ci                return PAN_SYSVAL_NUM_VERTICES;
94bf215546Sopenharmony_ci        case nir_intrinsic_load_sampler_lod_parameters_pan:
95bf215546Sopenharmony_ci                return panfrost_sysval_for_sampler(instr);
96bf215546Sopenharmony_ci        case nir_intrinsic_image_size:
97bf215546Sopenharmony_ci                return panfrost_sysval_for_image_size(instr);
98bf215546Sopenharmony_ci        case nir_intrinsic_load_blend_const_color_rgba:
99bf215546Sopenharmony_ci                return PAN_SYSVAL_BLEND_CONSTANTS;
100bf215546Sopenharmony_ci        default:
101bf215546Sopenharmony_ci                return ~0;
102bf215546Sopenharmony_ci        }
103bf215546Sopenharmony_ci}
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ciint
106bf215546Sopenharmony_cipanfrost_sysval_for_instr(nir_instr *instr, nir_dest *dest)
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci        nir_intrinsic_instr *intr;
109bf215546Sopenharmony_ci        nir_dest *dst = NULL;
110bf215546Sopenharmony_ci        nir_tex_instr *tex;
111bf215546Sopenharmony_ci        unsigned sysval = ~0;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci        switch (instr->type) {
114bf215546Sopenharmony_ci        case nir_instr_type_intrinsic:
115bf215546Sopenharmony_ci                intr = nir_instr_as_intrinsic(instr);
116bf215546Sopenharmony_ci                sysval = panfrost_nir_sysval_for_intrinsic(intr);
117bf215546Sopenharmony_ci                dst = &intr->dest;
118bf215546Sopenharmony_ci                break;
119bf215546Sopenharmony_ci        case nir_instr_type_tex:
120bf215546Sopenharmony_ci                tex = nir_instr_as_tex(instr);
121bf215546Sopenharmony_ci                if (tex->op != nir_texop_txs)
122bf215546Sopenharmony_ci                        break;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci                sysval = PAN_SYSVAL(TEXTURE_SIZE,
125bf215546Sopenharmony_ci                                    PAN_TXS_SYSVAL_ID(tex->texture_index,
126bf215546Sopenharmony_ci                                                      nir_tex_instr_dest_size(tex) -
127bf215546Sopenharmony_ci                                                      (tex->is_array ? 1 : 0),
128bf215546Sopenharmony_ci                                                      tex->is_array));
129bf215546Sopenharmony_ci                dst  = &tex->dest;
130bf215546Sopenharmony_ci                break;
131bf215546Sopenharmony_ci        default:
132bf215546Sopenharmony_ci                break;
133bf215546Sopenharmony_ci        }
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci        if (dest && dst)
136bf215546Sopenharmony_ci                *dest = *dst;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci        return sysval;
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_cistatic unsigned
142bf215546Sopenharmony_cipan_add_sysval(struct hash_table_u64 *sysval_to_id,
143bf215546Sopenharmony_ci               struct panfrost_sysvals *sysvals,
144bf215546Sopenharmony_ci               int sysval, unsigned id)
145bf215546Sopenharmony_ci{
146bf215546Sopenharmony_ci        assert(id < MAX_SYSVAL_COUNT);
147bf215546Sopenharmony_ci        _mesa_hash_table_u64_insert(sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));
148bf215546Sopenharmony_ci        sysvals->sysvals[id] = sysval;
149bf215546Sopenharmony_ci        return id;
150bf215546Sopenharmony_ci}
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ciunsigned
153bf215546Sopenharmony_cipan_lookup_sysval(struct hash_table_u64 *sysval_to_id,
154bf215546Sopenharmony_ci                  struct panfrost_sysvals *sysvals,
155bf215546Sopenharmony_ci                  int sysval)
156bf215546Sopenharmony_ci{
157bf215546Sopenharmony_ci        /* Try to lookup */
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci        void *cached = _mesa_hash_table_u64_search(sysval_to_id, sysval);
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci        if (cached) {
162bf215546Sopenharmony_ci                unsigned id = ((uintptr_t) cached) - 1;
163bf215546Sopenharmony_ci                assert(id < MAX_SYSVAL_COUNT);
164bf215546Sopenharmony_ci                assert(sysvals->sysvals[id] == sysval);
165bf215546Sopenharmony_ci                return id;
166bf215546Sopenharmony_ci        }
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci        /* Else assign */
169bf215546Sopenharmony_ci        return pan_add_sysval(sysval_to_id, sysvals, sysval,
170bf215546Sopenharmony_ci                              sysvals->sysval_count++);
171bf215546Sopenharmony_ci}
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_cistruct hash_table_u64 *
174bf215546Sopenharmony_cipanfrost_init_sysvals(struct panfrost_sysvals *sysvals,
175bf215546Sopenharmony_ci                      struct panfrost_sysvals *fixed_sysvals,
176bf215546Sopenharmony_ci                      void *memctx)
177bf215546Sopenharmony_ci{
178bf215546Sopenharmony_ci        memset(sysvals, 0, sizeof(*sysvals));
179bf215546Sopenharmony_ci        struct hash_table_u64 *sysval_to_id =
180bf215546Sopenharmony_ci                _mesa_hash_table_u64_create(memctx);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci        if (fixed_sysvals) {
183bf215546Sopenharmony_ci                for (unsigned i = 0; i < fixed_sysvals->sysval_count; i++) {
184bf215546Sopenharmony_ci                        if (!fixed_sysvals->sysvals[i])
185bf215546Sopenharmony_ci                                continue;
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci                        pan_add_sysval(sysval_to_id, sysvals,
188bf215546Sopenharmony_ci                                       fixed_sysvals->sysvals[i], i);
189bf215546Sopenharmony_ci                }
190bf215546Sopenharmony_ci                sysvals->sysval_count = fixed_sysvals->sysval_count;
191bf215546Sopenharmony_ci        }
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci        return sysval_to_id;
194bf215546Sopenharmony_ci}
195