2 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
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:
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
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
24 #include "disassemble.h"
26 #include "main/mtypes.h"
27 #include "compiler/glsl/standalone.h"
28 #include "compiler/glsl/glsl_to_nir.h"
29 #include "compiler/glsl/gl_nir.h"
30 #include "compiler/nir_types.h"
31 #include "util/u_dynarray.h"
32 #include "bifrost_compile.h"
35 static panfrost_program
36 compile_shader(char **argv
, bool vertex_only
)
38 struct gl_shader_program
*prog
;
40 unsigned shader_types
[2] = {
45 struct standalone_options options
= {
46 .glsl_version
= 300, /* ES - needed for precision */
48 .lower_precision
= true
51 static struct gl_context local_ctx
;
53 prog
= standalone_compile_shader(&options
, 2, argv
, &local_ctx
);
54 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->info
.stage
= MESA_SHADER_FRAGMENT
;
56 panfrost_program compiled
;
57 for (unsigned i
= 0; i
< 2; ++i
) {
58 nir
[i
] = glsl_to_nir(&local_ctx
, prog
, shader_types
[i
], &bifrost_nir_options
);
59 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 NIR_PASS_V(nir
[i
], nir_split_var_copies
);
62 NIR_PASS_V(nir
[i
], nir_lower_var_copies
);
64 /* before buffers and vars_to_ssa */
65 NIR_PASS_V(nir
[i
], gl_nir_lower_images
, true);
67 NIR_PASS_V(nir
[i
], gl_nir_lower_buffers
, prog
);
68 NIR_PASS_V(nir
[i
], nir_opt_constant_folding
);
70 unsigned product_id
= 0x7212; /* Mali G52 */
71 bifrost_compile_shader_nir(nir
[i
], &compiled
, product_id
);
81 disassemble(const char *filename
, bool verbose
)
83 FILE *fp
= fopen(filename
, "rb");
86 fseek(fp
, 0, SEEK_END
);
87 unsigned filesize
= ftell(fp
);
90 unsigned char *code
= malloc(filesize
);
91 unsigned res
= fread(code
, 1, filesize
, fp
);
92 if (res
!= filesize
) {
93 printf("Couldn't read full file\n");
97 disassemble_bifrost(stdout
, code
, filesize
, verbose
);
102 test_vertex(char **argv
)
104 void *memctx
= NULL
; /* TODO */
105 struct panfrost_device
*dev
= bit_initialize(memctx
);
119 bit_vertex(dev
, compile_shader(argv
, true),
120 (uint32_t *) iubo
, sizeof(iubo
),
121 (uint32_t *) iattr
, sizeof(iattr
),
122 (uint32_t *) expected
, sizeof(expected
),
129 void *memctx
= NULL
; /* TODO */
130 struct panfrost_device
*dev
= bit_initialize(memctx
);
131 bit_packing(dev
, BIT_DEBUG_FAIL
);
135 run(const char *filename
)
137 FILE *fp
= fopen(filename
, "rb");
140 fseek(fp
, 0, SEEK_END
);
141 unsigned filesize
= ftell(fp
);
144 unsigned char *code
= malloc(filesize
);
145 unsigned res
= fread(code
, 1, filesize
, fp
);
146 if (res
!= filesize
) {
147 printf("Couldn't read full file\n");
151 void *memctx
= NULL
; /* TODO */
152 struct panfrost_device
*dev
= bit_initialize(memctx
);
154 panfrost_program prog
= {
161 bit_vertex(dev
, prog
, NULL
, 0, NULL
, 0, NULL
, 0, BIT_DEBUG_ALL
);
167 main(int argc
, char **argv
)
170 printf("Pass a command\n");
174 if (strcmp(argv
[1], "compile") == 0)
175 compile_shader(&argv
[2], false);
176 else if (strcmp(argv
[1], "disasm") == 0)
177 disassemble(argv
[2], false);
178 else if (strcmp(argv
[1], "disasm-verbose") == 0)
179 disassemble(argv
[2], true);
180 else if (strcmp(argv
[1], "tests") == 0)
182 else if (strcmp(argv
[1], "test-vertex") == 0)
183 test_vertex(&argv
[2]);
184 else if (strcmp(argv
[1], "run") == 0)
187 unreachable("Unknown command. Valid: compile/disasm");