Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/bifrost/cmdline.c
4564 views
1
/*
2
* Copyright (C) 2019 Ryan Houdek <[email protected]>
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 "disassemble.h"
25
#include "compiler.h"
26
27
#include "main/mtypes.h"
28
#include "compiler/glsl/standalone.h"
29
#include "compiler/glsl/glsl_to_nir.h"
30
#include "compiler/glsl/gl_nir.h"
31
#include "compiler/nir_types.h"
32
#include "util/u_dynarray.h"
33
#include "bifrost_compile.h"
34
35
static void
36
compile_shader(char **argv, bool vertex_only)
37
{
38
struct gl_shader_program *prog;
39
nir_shader *nir[2];
40
unsigned shader_types[2] = {
41
MESA_SHADER_VERTEX,
42
MESA_SHADER_FRAGMENT,
43
};
44
45
struct standalone_options options = {
46
.glsl_version = 300, /* ES - needed for precision */
47
.do_link = true,
48
.lower_precision = true
49
};
50
51
static struct gl_context local_ctx;
52
53
prog = standalone_compile_shader(&options, 2, argv, &local_ctx);
54
prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;
55
56
struct util_dynarray binary;
57
58
util_dynarray_init(&binary, NULL);
59
60
for (unsigned i = 0; i < 2; ++i) {
61
nir[i] = glsl_to_nir(&local_ctx, prog, shader_types[i], &bifrost_nir_options);
62
NIR_PASS_V(nir[i], nir_lower_global_vars_to_local);
63
NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0);
64
NIR_PASS_V(nir[i], nir_split_var_copies);
65
NIR_PASS_V(nir[i], nir_lower_var_copies);
66
67
/* before buffers and vars_to_ssa */
68
NIR_PASS_V(nir[i], gl_nir_lower_images, true);
69
70
NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);
71
NIR_PASS_V(nir[i], nir_opt_constant_folding);
72
73
struct panfrost_compile_inputs inputs = {
74
.gpu_id = 0x7212, /* Mali G52 */
75
};
76
struct pan_shader_info info;
77
78
util_dynarray_clear(&binary);
79
bifrost_compile_shader_nir(nir[i], &inputs, &binary, &info);
80
81
if (vertex_only)
82
break;
83
}
84
85
util_dynarray_fini(&binary);
86
}
87
88
#define BI_FOURCC(ch0, ch1, ch2, ch3) ( \
89
(uint32_t)(ch0) | (uint32_t)(ch1) << 8 | \
90
(uint32_t)(ch2) << 16 | (uint32_t)(ch3) << 24)
91
92
static void
93
disassemble(const char *filename, bool verbose)
94
{
95
FILE *fp = fopen(filename, "rb");
96
assert(fp);
97
98
fseek(fp, 0, SEEK_END);
99
unsigned filesize = ftell(fp);
100
rewind(fp);
101
102
uint32_t *code = malloc(filesize);
103
unsigned res = fread(code, 1, filesize, fp);
104
if (res != filesize) {
105
printf("Couldn't read full file\n");
106
}
107
fclose(fp);
108
109
if (filesize && code[0] == BI_FOURCC('M', 'B', 'S', '2')) {
110
for (int i = 0; i < filesize / 4; ++i) {
111
if (code[i] != BI_FOURCC('O', 'B', 'J', 'C'))
112
continue;
113
114
unsigned size = code[i + 1];
115
unsigned offset = i + 2;
116
117
disassemble_bifrost(stdout, (uint8_t*)(code + offset), size, verbose);
118
}
119
} else {
120
disassemble_bifrost(stdout, (uint8_t*)code, filesize, verbose);
121
}
122
123
free(code);
124
}
125
126
static int
127
bi_tests()
128
{
129
#ifndef NDEBUG
130
bi_test_scheduler();
131
bi_test_packing();
132
bi_test_packing_formats();
133
printf("Pass.\n");
134
return 0;
135
#else
136
printf("Tests omitted in release mode.");
137
return 1;
138
#endif
139
}
140
141
int
142
main(int argc, char **argv)
143
{
144
if (argc < 2) {
145
printf("Pass a command\n");
146
exit(1);
147
}
148
149
if (strcmp(argv[1], "compile") == 0)
150
compile_shader(&argv[2], false);
151
else if (strcmp(argv[1], "disasm") == 0)
152
disassemble(argv[2], false);
153
else if (strcmp(argv[1], "disasm-verbose") == 0)
154
disassemble(argv[2], true);
155
else if (strcmp(argv[1], "test") == 0)
156
bi_tests();
157
else
158
unreachable("Unknown command. Valid: compile/disasm");
159
160
return 0;
161
}
162
163