2019-04-22 04:41:09 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-08-14 20:21:23 +01:00
|
|
|
#include "disassemble.h"
|
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
#include "main/mtypes.h"
|
|
|
|
#include "compiler/glsl/standalone.h"
|
|
|
|
#include "compiler/glsl/glsl_to_nir.h"
|
2019-08-14 20:21:23 +01:00
|
|
|
#include "compiler/glsl/gl_nir.h"
|
2019-04-22 04:41:09 +01:00
|
|
|
#include "compiler/nir_types.h"
|
|
|
|
#include "util/u_dynarray.h"
|
2019-08-14 20:28:01 +01:00
|
|
|
#include "bifrost_compile.h"
|
|
|
|
|
2020-10-17 11:08:17 +01:00
|
|
|
static panfrost_program *
|
2020-03-25 18:56:06 +00:00
|
|
|
compile_shader(char **argv, bool vertex_only)
|
2019-08-14 20:21:23 +01:00
|
|
|
{
|
|
|
|
struct gl_shader_program *prog;
|
|
|
|
nir_shader *nir[2];
|
|
|
|
unsigned shader_types[2] = {
|
|
|
|
MESA_SHADER_VERTEX,
|
|
|
|
MESA_SHADER_FRAGMENT,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct standalone_options options = {
|
2020-03-27 18:39:18 +00:00
|
|
|
.glsl_version = 300, /* ES - needed for precision */
|
2019-08-14 20:21:23 +01:00
|
|
|
.do_link = true,
|
2020-03-27 18:39:18 +00:00
|
|
|
.lower_precision = true
|
2019-08-14 20:21:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-10-17 11:08:17 +01:00
|
|
|
panfrost_program *compiled;
|
2019-08-14 20:21:23 +01:00
|
|
|
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);
|
2020-03-05 21:52:29 +00:00
|
|
|
NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0);
|
2019-08-14 20:21:23 +01:00
|
|
|
NIR_PASS_V(nir[i], nir_split_var_copies);
|
|
|
|
NIR_PASS_V(nir[i], nir_lower_var_copies);
|
|
|
|
|
|
|
|
/* before buffers and vars_to_ssa */
|
2020-01-23 18:49:02 +00:00
|
|
|
NIR_PASS_V(nir[i], gl_nir_lower_images, true);
|
2019-08-14 20:21:23 +01:00
|
|
|
|
|
|
|
NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);
|
|
|
|
NIR_PASS_V(nir[i], nir_opt_constant_folding);
|
2020-03-03 19:27:05 +00:00
|
|
|
|
2020-10-08 09:09:56 +01:00
|
|
|
struct panfrost_compile_inputs inputs = {
|
|
|
|
.gpu_id = 0x7212, /* Mali G52 */
|
|
|
|
};
|
|
|
|
|
2020-10-17 11:08:17 +01:00
|
|
|
compiled = bifrost_compile_shader_nir(NULL, nir[i], &inputs);
|
2020-03-25 18:56:06 +00:00
|
|
|
|
|
|
|
if (vertex_only)
|
|
|
|
return compiled;
|
2019-08-14 20:21:23 +01:00
|
|
|
}
|
2020-03-25 18:56:06 +00:00
|
|
|
|
|
|
|
return compiled;
|
2019-08-14 20:21:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:04:02 +00:00
|
|
|
#define BI_FOURCC(ch0, ch1, ch2, ch3) ( \
|
|
|
|
(uint32_t)(ch0) | (uint32_t)(ch1) << 8 | \
|
|
|
|
(uint32_t)(ch2) << 16 | (uint32_t)(ch3) << 24)
|
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
static void
|
2020-03-31 18:08:16 +01:00
|
|
|
disassemble(const char *filename, bool verbose)
|
2019-04-22 04:41:09 +01:00
|
|
|
{
|
|
|
|
FILE *fp = fopen(filename, "rb");
|
|
|
|
assert(fp);
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_END);
|
2019-08-21 17:39:04 +01:00
|
|
|
unsigned filesize = ftell(fp);
|
2019-04-22 04:41:09 +01:00
|
|
|
rewind(fp);
|
|
|
|
|
2020-12-22 10:04:02 +00:00
|
|
|
uint32_t *code = malloc(filesize);
|
2019-08-21 17:39:04 +01:00
|
|
|
unsigned res = fread(code, 1, filesize, fp);
|
2019-04-22 04:41:09 +01:00
|
|
|
if (res != filesize) {
|
|
|
|
printf("Couldn't read full file\n");
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2020-12-22 10:04:02 +00:00
|
|
|
if (filesize && code[0] == BI_FOURCC('M', 'B', 'S', '2')) {
|
|
|
|
for (int i = 0; i < filesize / 4; ++i) {
|
|
|
|
if (code[i] != BI_FOURCC('O', 'B', 'J', 'C'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned size = code[i + 1];
|
|
|
|
unsigned offset = i + 2;
|
|
|
|
|
|
|
|
disassemble_bifrost(stdout, (uint8_t*)(code + offset), size, verbose);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
disassemble_bifrost(stdout, (uint8_t*)code, filesize, verbose);
|
|
|
|
}
|
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
free(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("Pass a command\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-08-14 20:48:04 +01:00
|
|
|
|
|
|
|
if (strcmp(argv[1], "compile") == 0)
|
2020-03-25 18:56:06 +00:00
|
|
|
compile_shader(&argv[2], false);
|
2019-08-14 20:48:04 +01:00
|
|
|
else if (strcmp(argv[1], "disasm") == 0)
|
2020-03-31 18:08:16 +01:00
|
|
|
disassemble(argv[2], false);
|
|
|
|
else if (strcmp(argv[1], "disasm-verbose") == 0)
|
|
|
|
disassemble(argv[2], true);
|
2019-08-14 20:48:04 +01:00
|
|
|
else
|
|
|
|
unreachable("Unknown command. Valid: compile/disasm");
|
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|