2019-04-22 04:41:09 +01:00
|
|
|
/*
|
2021-07-19 18:41:49 +01:00
|
|
|
* Copyright (C) 2021 Collabora, Ltd.
|
2019-04-22 04:41:09 +01:00
|
|
|
* Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
|
2021-07-19 18:41:49 +01:00
|
|
|
* Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
|
|
|
|
* Copyright © 2015 Red Hat
|
2019-04-22 04:41:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-07-19 20:52:19 +01:00
|
|
|
#include <getopt.h>
|
2021-07-19 18:35:17 +01:00
|
|
|
#include <string.h>
|
2019-08-14 20:21:23 +01:00
|
|
|
#include "disassemble.h"
|
2021-11-22 00:01:31 +00:00
|
|
|
#include "valhall/disassemble.h"
|
2020-12-21 20:20:37 +00:00
|
|
|
#include "compiler.h"
|
2019-08-14 20:21:23 +01:00
|
|
|
|
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"
|
|
|
|
|
2021-07-19 20:52:19 +01:00
|
|
|
unsigned gpu_id = 0x7212;
|
|
|
|
int verbose = 0;
|
|
|
|
|
2021-07-19 18:35:17 +01:00
|
|
|
static gl_shader_stage
|
|
|
|
filename_to_stage(const char *stage)
|
|
|
|
{
|
|
|
|
const char *ext = strrchr(stage, '.');
|
|
|
|
|
|
|
|
if (ext == NULL) {
|
|
|
|
fprintf(stderr, "No extension found in %s\n", stage);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(ext, ".cs") || !strcmp(ext, ".comp"))
|
|
|
|
return MESA_SHADER_COMPUTE;
|
|
|
|
else if (!strcmp(ext, ".vs") || !strcmp(ext, ".vert"))
|
|
|
|
return MESA_SHADER_VERTEX;
|
|
|
|
else if (!strcmp(ext, ".fs") || !strcmp(ext, ".frag"))
|
|
|
|
return MESA_SHADER_FRAGMENT;
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Invalid extension %s\n", ext);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable("Should've returned or bailed");
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:41:49 +01:00
|
|
|
static int
|
|
|
|
st_packed_uniforms_type_size(const struct glsl_type *type, bool bindless)
|
|
|
|
{
|
|
|
|
return glsl_count_dword_slots(type, bindless);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
glsl_type_size(const struct glsl_type *type, bool bindless)
|
|
|
|
{
|
|
|
|
return glsl_count_attribute_slots(type, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
insert_sorted(struct exec_list *var_list, nir_variable *new_var)
|
|
|
|
{
|
|
|
|
nir_foreach_variable_in_list (var, var_list) {
|
|
|
|
if (var->data.location > new_var->data.location) {
|
|
|
|
exec_node_insert_node_before(&var->node, &new_var->node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exec_list_push_tail(var_list, &new_var->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sort_varyings(nir_shader *nir, nir_variable_mode mode)
|
|
|
|
{
|
|
|
|
struct exec_list new_list;
|
|
|
|
exec_list_make_empty(&new_list);
|
|
|
|
nir_foreach_variable_with_modes_safe (var, nir, mode) {
|
|
|
|
exec_node_remove(&var->node);
|
|
|
|
insert_sorted(&new_list, var);
|
|
|
|
}
|
|
|
|
exec_list_append(&nir->variables, &new_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)
|
|
|
|
{
|
|
|
|
nir_foreach_variable_with_modes (var, nir, mode) {
|
|
|
|
if (var->data.location >= VARYING_SLOT_VAR0) {
|
|
|
|
var->data.location += 9;
|
|
|
|
} else if ((var->data.location >= VARYING_SLOT_TEX0) &&
|
|
|
|
(var->data.location <= VARYING_SLOT_TEX7)) {
|
|
|
|
var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 07:24:03 +00:00
|
|
|
static void
|
2021-07-19 18:35:17 +01:00
|
|
|
compile_shader(int stages, char **files)
|
2019-08-14 20:21:23 +01:00
|
|
|
{
|
|
|
|
struct gl_shader_program *prog;
|
2021-07-19 18:35:17 +01:00
|
|
|
nir_shader *nir[MESA_SHADER_COMPUTE + 1];
|
|
|
|
unsigned shader_types[MESA_SHADER_COMPUTE + 1];
|
|
|
|
|
|
|
|
if (stages > MESA_SHADER_COMPUTE) {
|
|
|
|
fprintf(stderr, "Too many stages");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < stages; ++i)
|
|
|
|
shader_types[i] = filename_to_stage(files[i]);
|
2019-08-14 20:21:23 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-07-19 18:35:17 +01:00
|
|
|
prog = standalone_compile_shader(&options, stages, files, &local_ctx);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < stages; ++i) {
|
|
|
|
gl_shader_stage stage = shader_types[i];
|
|
|
|
prog->_LinkedShaders[stage]->Program->info.stage = stage;
|
|
|
|
}
|
2019-08-14 20:21:23 +01:00
|
|
|
|
2021-02-13 07:24:03 +00:00
|
|
|
struct util_dynarray binary;
|
|
|
|
|
|
|
|
util_dynarray_init(&binary, NULL);
|
|
|
|
|
2021-07-19 18:35:17 +01:00
|
|
|
for (unsigned i = 0; i < stages; ++i) {
|
2022-01-07 02:47:08 +00:00
|
|
|
nir[i] = glsl_to_nir(&local_ctx.Const, prog, shader_types[i], &bifrost_nir_options);
|
2021-07-19 18:41:49 +01:00
|
|
|
|
|
|
|
if (shader_types[i] == MESA_SHADER_VERTEX) {
|
|
|
|
nir_assign_var_locations(nir[i], nir_var_shader_in, &nir[i]->num_inputs,
|
|
|
|
glsl_type_size);
|
|
|
|
sort_varyings(nir[i], nir_var_shader_out);
|
|
|
|
nir_assign_var_locations(nir[i], nir_var_shader_out, &nir[i]->num_outputs,
|
|
|
|
glsl_type_size);
|
|
|
|
fixup_varying_slots(nir[i], nir_var_shader_out);
|
|
|
|
} else if (shader_types[i] == MESA_SHADER_FRAGMENT) {
|
|
|
|
sort_varyings(nir[i], nir_var_shader_in);
|
|
|
|
nir_assign_var_locations(nir[i], nir_var_shader_in, &nir[i]->num_inputs,
|
|
|
|
glsl_type_size);
|
|
|
|
fixup_varying_slots(nir[i], nir_var_shader_in);
|
|
|
|
nir_assign_var_locations(nir[i], nir_var_shader_out, &nir[i]->num_outputs,
|
|
|
|
glsl_type_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
nir_assign_var_locations(nir[i], nir_var_uniform, &nir[i]->num_uniforms,
|
|
|
|
glsl_type_size);
|
|
|
|
|
2019-08-14 20:21:23 +01:00
|
|
|
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);
|
2021-07-19 18:41:49 +01:00
|
|
|
NIR_PASS_V(nir[i], nir_opt_copy_prop_vars);
|
|
|
|
NIR_PASS_V(nir[i], nir_opt_combine_stores, nir_var_all);
|
|
|
|
|
|
|
|
NIR_PASS_V(nir[i], nir_lower_system_values);
|
|
|
|
NIR_PASS_V(nir[i], gl_nir_lower_samplers, prog);
|
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);
|
|
|
|
|
2021-07-19 18:41:49 +01:00
|
|
|
NIR_PASS_V(nir[i], nir_lower_io, nir_var_uniform,
|
|
|
|
st_packed_uniforms_type_size,
|
|
|
|
(nir_lower_io_options)0);
|
|
|
|
NIR_PASS_V(nir[i], nir_lower_uniforms_to_ubo, true, false);
|
|
|
|
|
2019-08-14 20:21:23 +01:00
|
|
|
/* 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 = {
|
2021-07-19 20:52:19 +01:00
|
|
|
.gpu_id = gpu_id,
|
2022-05-04 17:25:33 +01:00
|
|
|
.fixed_sysval_ubo = -1,
|
2020-10-08 09:09:56 +01:00
|
|
|
};
|
2021-07-19 18:41:32 +01:00
|
|
|
struct pan_shader_info info = { 0 };
|
2020-10-08 09:09:56 +01:00
|
|
|
|
2021-02-13 07:24:03 +00:00
|
|
|
util_dynarray_clear(&binary);
|
|
|
|
bifrost_compile_shader_nir(nir[i], &inputs, &binary, &info);
|
2021-07-20 01:00:38 +01:00
|
|
|
|
|
|
|
char *fn = NULL;
|
|
|
|
asprintf(&fn, "shader_%u.bin", i);
|
|
|
|
assert(fn != NULL);
|
|
|
|
FILE *fp = fopen(fn, "wb");
|
|
|
|
fwrite(binary.data, 1, binary.size, fp);
|
|
|
|
fclose(fp);
|
|
|
|
free(fn);
|
2019-08-14 20:21:23 +01:00
|
|
|
}
|
2020-03-25 18:56:06 +00:00
|
|
|
|
2021-02-13 07:24:03 +00:00
|
|
|
util_dynarray_fini(&binary);
|
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
|
2021-07-19 20:52:19 +01:00
|
|
|
disassemble(const char *filename)
|
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");
|
|
|
|
}
|
2021-07-19 20:52:19 +01:00
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
fclose(fp);
|
|
|
|
|
2021-11-22 00:01:31 +00:00
|
|
|
void *entrypoint = code;
|
|
|
|
|
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;
|
|
|
|
|
2021-11-22 00:01:31 +00:00
|
|
|
entrypoint = code + offset;
|
|
|
|
filesize = size;
|
2020-12-22 10:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 00:01:31 +00:00
|
|
|
if ((gpu_id >> 12) >= 9)
|
|
|
|
disassemble_valhall(stdout, entrypoint, filesize, verbose);
|
|
|
|
else
|
|
|
|
disassemble_bifrost(stdout, entrypoint, filesize, verbose);
|
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
free(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2021-07-31 02:21:54 +01:00
|
|
|
int c;
|
2021-07-19 20:52:19 +01:00
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
if (argc < 2) {
|
|
|
|
printf("Pass a command\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-08-14 20:48:04 +01:00
|
|
|
|
2021-07-19 20:52:19 +01:00
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "id", optional_argument, NULL, 'i' },
|
|
|
|
{ "gpu", optional_argument, NULL, 'g' },
|
|
|
|
{ "verbose", no_argument, &verbose, 'v' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned major, minor;
|
|
|
|
} gpus[] = {
|
|
|
|
{ "G71", 6, 0 },
|
|
|
|
{ "G72", 6, 2 },
|
|
|
|
{ "G51", 7, 0 },
|
|
|
|
{ "G76", 7, 1 },
|
|
|
|
{ "G52", 7, 2 },
|
|
|
|
{ "G31", 7, 3 },
|
|
|
|
{ "G77", 9, 0 },
|
|
|
|
{ "G57", 9, 1 },
|
|
|
|
{ "G78", 9, 2 },
|
|
|
|
{ "G57", 9, 3 },
|
|
|
|
{ "G68", 9, 4 },
|
|
|
|
{ "G78AE", 9, 5 },
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "v:", longopts, NULL)) != -1) {
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'i':
|
|
|
|
gpu_id = atoi(optarg);
|
|
|
|
|
|
|
|
if (!gpu_id) {
|
|
|
|
fprintf(stderr, "Expected GPU ID, got %s\n", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gpu_id = 0;
|
|
|
|
|
|
|
|
/* Compatibility with the Arm compiler */
|
|
|
|
if (strncmp(optarg, "Mali-", 5) == 0) optarg += 5;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(gpus); ++i) {
|
|
|
|
if (strcmp(gpus[i].name, optarg)) continue;
|
|
|
|
|
|
|
|
unsigned major = gpus[i].major;
|
|
|
|
unsigned minor = gpus[i].minor;
|
|
|
|
|
|
|
|
gpu_id = (major << 12) | (minor << 8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gpu_id) {
|
|
|
|
fprintf(stderr, "Unknown GPU %s\n", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[optind], "compile") == 0)
|
|
|
|
compile_shader(argc - optind - 1, &argv[optind + 1]);
|
|
|
|
else if (strcmp(argv[optind], "disasm") == 0)
|
|
|
|
disassemble(argv[optind + 1]);
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Unknown command. Valid: compile/disasm\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-08-14 20:48:04 +01:00
|
|
|
|
2019-04-22 04:41:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|