cmdline.c 5.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

24
25
#include "disassemble.h"

26
27
28
#include "main/mtypes.h"
#include "compiler/glsl/standalone.h"
#include "compiler/glsl/glsl_to_nir.h"
29
#include "compiler/glsl/gl_nir.h"
30
31
#include "compiler/nir_types.h"
#include "util/u_dynarray.h"
32
#include "bifrost_compile.h"
33
#include "test/bit.h"
34

35
36
static panfrost_program
compile_shader(char **argv, bool vertex_only)
37
38
39
40
41
42
43
44
45
{
        struct gl_shader_program *prog;
        nir_shader *nir[2];
        unsigned shader_types[2] = {
                MESA_SHADER_VERTEX,
                MESA_SHADER_FRAGMENT,
        };

        struct standalone_options options = {
46
                .glsl_version = 300, /* ES - needed for precision */
47
                .do_link = true,
48
                .lower_precision = true
49
50
51
52
53
54
55
        };

        static struct gl_context local_ctx;

        prog = standalone_compile_shader(&options, 2, argv, &local_ctx);
        prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;

56
        panfrost_program compiled;
57
58
59
        for (unsigned i = 0; i < 2; ++i) {
                nir[i] = glsl_to_nir(&local_ctx, prog, shader_types[i], &bifrost_nir_options);
                NIR_PASS_V(nir[i], nir_lower_global_vars_to_local);
60
                NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0);
61
62
63
64
                NIR_PASS_V(nir[i], nir_split_var_copies);
                NIR_PASS_V(nir[i], nir_lower_var_copies);

                /* before buffers and vars_to_ssa */
65
                NIR_PASS_V(nir[i], gl_nir_lower_images, true);
66
67
68

                NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);
                NIR_PASS_V(nir[i], nir_opt_constant_folding);
Alyssa Rosenzweig's avatar
Alyssa Rosenzweig committed
69
70
71

                unsigned product_id = 0x7212; /* Mali G52 */
                bifrost_compile_shader_nir(nir[i], &compiled, product_id);
72
73
74

                if (vertex_only)
                        return compiled;
75
        }
76
77

        return compiled;
78
79
}

80
static void
81
disassemble(const char *filename, bool verbose)
82
83
84
85
86
{
        FILE *fp = fopen(filename, "rb");
        assert(fp);

        fseek(fp, 0, SEEK_END);
87
        unsigned filesize = ftell(fp);
88
89
90
        rewind(fp);

        unsigned char *code = malloc(filesize);
91
        unsigned res = fread(code, 1, filesize, fp);
92
93
94
95
96
        if (res != filesize) {
                printf("Couldn't read full file\n");
        }
        fclose(fp);

97
        disassemble_bifrost(stdout, code, filesize, verbose);
98
99
100
        free(code);
}

101
static void
102
test_vertex(char **argv)
103
104
{
        void *memctx = NULL; /* TODO */
105
        struct panfrost_device *dev = bit_initialize(memctx);
Alyssa Rosenzweig's avatar
Alyssa Rosenzweig committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

        float iubo[] = {
                0.1, 0.2, 0.3, 0.4
        };

        float iattr[] = {
                0.5, 0.6, 0.7, 0.8
        };

        float expected[] = {
                0.6, 0.8, 1.0, 1.2
        };

        bit_vertex(dev, compile_shader(argv, true),
                        (uint32_t *) iubo, sizeof(iubo),
                        (uint32_t *) iattr, sizeof(iattr),
                        (uint32_t *) expected, sizeof(expected),
                        BIT_DEBUG_ALL);
124
125
}

126
127
128
129
130
static void
tests(void)
{
        void *memctx = NULL; /* TODO */
        struct panfrost_device *dev = bit_initialize(memctx);
131
        bit_packing(dev, BIT_DEBUG_FAIL);
132
133
}

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
static void
run(const char *filename)
{
        FILE *fp = fopen(filename, "rb");
        assert(fp);

        fseek(fp, 0, SEEK_END);
        unsigned filesize = ftell(fp);
        rewind(fp);

        unsigned char *code = malloc(filesize);
        unsigned res = fread(code, 1, filesize, fp);
        if (res != filesize) {
                printf("Couldn't read full file\n");
        }
        fclose(fp);

        void *memctx = NULL; /* TODO */
        struct panfrost_device *dev = bit_initialize(memctx);

        panfrost_program prog = {
                .compiled = {
                        .data = code,
                        .size = filesize
                },
        };

161
        bit_vertex(dev, prog, NULL, 0, NULL, 0, NULL, 0, BIT_DEBUG_ALL);
162
163
164
165

        free(code);
}

166
167
168
169
170
171
172
int
main(int argc, char **argv)
{
        if (argc < 2) {
                printf("Pass a command\n");
                exit(1);
        }
173
174

        if (strcmp(argv[1], "compile") == 0)
175
                compile_shader(&argv[2], false);
176
        else if (strcmp(argv[1], "disasm") == 0)
177
178
179
                disassemble(argv[2], false);
        else if (strcmp(argv[1], "disasm-verbose") == 0)
                disassemble(argv[2], true);
180
181
        else if (strcmp(argv[1], "tests") == 0)
                tests();
182
183
        else if (strcmp(argv[1], "test-vertex") == 0)
                test_vertex(&argv[2]);
184
185
        else if (strcmp(argv[1], "run") == 0)
                run(argv[2]);
186
187
188
        else
                unreachable("Unknown command. Valid: compile/disasm");

189
190
        return 0;
}