1/*
2 * © Copyright 2018 Alyssa Rosenzweig
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 <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include "pan_context.h"
29
30#include "compiler/nir/nir.h"
31#include "nir/tgsi_to_nir.h"
32#include "midgard/midgard_compile.h"
33#include "util/u_dynarray.h"
34
35#include "tgsi/tgsi_dump.h"
36
37void
38panfrost_shader_compile(struct panfrost_context *ctx, struct mali_shader_meta *meta, const char *src, int type, struct panfrost_shader_state *state)
39{
40        uint8_t *dst;
41
42        nir_shader *s;
43
44        struct pipe_shader_state *cso = state->base;
45
46        if (cso->type == PIPE_SHADER_IR_NIR) {
47                s = nir_shader_clone(NULL, cso->ir.nir);
48        } else {
49                assert (cso->type == PIPE_SHADER_IR_TGSI);
50                //tgsi_dump(cso->tokens, 0);
51                s = tgsi_to_nir(cso->tokens, ctx->base.screen);
52        }
53
54        s->info.stage = type == JOB_TYPE_VERTEX ? MESA_SHADER_VERTEX : MESA_SHADER_FRAGMENT;
55
56        if (s->info.stage == MESA_SHADER_FRAGMENT) {
57                /* Inject the alpha test now if we need to */
58
59                if (state->alpha_state.enabled) {
60                        NIR_PASS_V(s, nir_lower_alpha_test, state->alpha_state.func, false);
61                }
62        }
63
64        /* Call out to Midgard compiler given the above NIR */
65
66        midgard_program program = {
67                .alpha_ref = state->alpha_state.ref_value
68        };
69
70        midgard_compile_shader_nir(s, &program, false);
71
72        /* Prepare the compiled binary for upload */
73        int size = program.compiled.size;
74        dst = program.compiled.data;
75
76        /* Upload the shader. The lookahead tag is ORed on as a tagged pointer.
77         * I bet someone just thought that would be a cute pun. At least,
78         * that's how I'd do it. */
79
80        meta->shader = panfrost_upload(&ctx->shaders, dst, size, true) | program.first_tag;
81
82        util_dynarray_fini(&program.compiled);
83
84        /* Sysvals are prepended */
85        program.uniform_count += program.sysval_count;
86        state->sysval_count = program.sysval_count;
87        memcpy(state->sysval, program.sysvals, sizeof(state->sysval[0]) * state->sysval_count);
88
89        meta->midgard1.uniform_count = MIN2(program.uniform_count, program.uniform_cutoff);
90        meta->attribute_count = program.attribute_count;
91        meta->varying_count = program.varying_count;
92        meta->midgard1.work_count = program.work_register_count;
93
94        state->can_discard = program.can_discard;
95        state->writes_point_size = program.writes_point_size;
96        state->reads_point_coord = false;
97
98        /* Separate as primary uniform count is truncated */
99        state->uniform_count = program.uniform_count;
100
101        meta->midgard1.unknown2 = 8; /* XXX */
102
103        unsigned default_vec1_swizzle = panfrost_get_default_swizzle(1);
104        unsigned default_vec2_swizzle = panfrost_get_default_swizzle(2);
105        unsigned default_vec4_swizzle = panfrost_get_default_swizzle(4);
106
107        /* Iterate the varyings and emit the corresponding descriptor */
108        unsigned general_purpose_count = 0;
109
110        for (unsigned i = 0; i < program.varying_count; ++i) {
111                unsigned location = program.varyings[i];
112
113                /* Default to a vec4 varying */
114                struct mali_attr_meta v = {
115                        .format = MALI_RGBA32F,
116                        .swizzle = default_vec4_swizzle,
117                        .unknown1 = 0x2,
118                };
119
120                /* Check for special cases, otherwise assume general varying */
121
122                if (location == VARYING_SLOT_POS) {
123                        v.index = 1;
124                        v.format = MALI_VARYING_POS;
125                } else if (location == VARYING_SLOT_PSIZ) {
126                        v.index = 2;
127                        v.format = MALI_R16F;
128                        v.swizzle = default_vec1_swizzle;
129
130                        state->writes_point_size = true;
131                } else if (location == VARYING_SLOT_PNTC) {
132                        v.index = 3;
133                        v.format = MALI_RG16F;
134                        v.swizzle = default_vec2_swizzle;
135
136                        state->reads_point_coord = true;
137                } else {
138                        v.index = 0;
139                        v.src_offset = 16 * (general_purpose_count++);
140                }
141
142                state->varyings[i] = v;
143        }
144
145        /* Set the stride for the general purpose fp32 vec4 varyings */
146        state->general_varying_stride = (4 * 4) * general_purpose_count;
147}
148