Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com> |
| 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 | |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 24 | #include "disassemble.h" |
| 25 | |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 26 | #include "main/mtypes.h" |
| 27 | #include "compiler/glsl/standalone.h" |
| 28 | #include "compiler/glsl/glsl_to_nir.h" |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 29 | #include "compiler/glsl/gl_nir.h" |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 30 | #include "compiler/nir_types.h" |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 31 | #include "util/u_dynarray.h" |
Alyssa Rosenzweig | d8d8b08 | 2019-08-14 12:28:01 -0700 | [diff] [blame] | 32 | #include "bifrost_compile.h" |
Alyssa Rosenzweig | bf1929e | 2020-03-24 13:48:06 -0400 | [diff] [blame] | 33 | #include "test/bit.h" |
Alyssa Rosenzweig | d8d8b08 | 2019-08-14 12:28:01 -0700 | [diff] [blame] | 34 | |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 35 | static panfrost_program |
| 36 | compile_shader(char **argv, bool vertex_only) |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 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 = { |
Alyssa Rosenzweig | 77e04eb | 2020-03-27 14:39:18 -0400 | [diff] [blame] | 46 | .glsl_version = 300, /* ES - needed for precision */ |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 47 | .do_link = true, |
Alyssa Rosenzweig | 77e04eb | 2020-03-27 14:39:18 -0400 | [diff] [blame] | 48 | .lower_precision = true |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 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 | |
Alyssa Rosenzweig | e6f5ae8 | 2020-03-10 16:09:44 -0400 | [diff] [blame] | 56 | panfrost_program compiled; |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 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); |
Alyssa Rosenzweig | 977a38c | 2020-03-05 16:52:29 -0500 | [diff] [blame] | 60 | NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0); |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 61 | NIR_PASS_V(nir[i], nir_split_var_copies); |
| 62 | NIR_PASS_V(nir[i], nir_lower_var_copies); |
| 63 | |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 64 | /* before buffers and vars_to_ssa */ |
Eric Anholt | 7342b859 | 2020-01-23 10:49:02 -0800 | [diff] [blame] | 65 | NIR_PASS_V(nir[i], gl_nir_lower_images, true); |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 66 | |
| 67 | NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog); |
| 68 | NIR_PASS_V(nir[i], nir_opt_constant_folding); |
Alyssa Rosenzweig | 0b26cb1 | 2020-03-03 14:27:05 -0500 | [diff] [blame] | 69 | |
Boris Brezillon | 0a74a04 | 2020-10-08 10:09:56 +0200 | [diff] [blame^] | 70 | struct panfrost_compile_inputs inputs = { |
| 71 | .gpu_id = 0x7212, /* Mali G52 */ |
| 72 | }; |
| 73 | |
| 74 | bifrost_compile_shader_nir(nir[i], &compiled, &inputs); |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 75 | |
| 76 | if (vertex_only) |
| 77 | return compiled; |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 78 | } |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 79 | |
| 80 | return compiled; |
Alyssa Rosenzweig | fca491c | 2019-08-14 12:21:23 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void |
Alyssa Rosenzweig | b150fa2 | 2020-03-31 13:08:16 -0400 | [diff] [blame] | 84 | disassemble(const char *filename, bool verbose) |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 85 | { |
| 86 | FILE *fp = fopen(filename, "rb"); |
| 87 | assert(fp); |
| 88 | |
| 89 | fseek(fp, 0, SEEK_END); |
Alyssa Rosenzweig | 5ebdd10 | 2019-08-21 09:39:04 -0700 | [diff] [blame] | 90 | unsigned filesize = ftell(fp); |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 91 | rewind(fp); |
| 92 | |
| 93 | unsigned char *code = malloc(filesize); |
Alyssa Rosenzweig | 5ebdd10 | 2019-08-21 09:39:04 -0700 | [diff] [blame] | 94 | unsigned res = fread(code, 1, filesize, fp); |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 95 | if (res != filesize) { |
| 96 | printf("Couldn't read full file\n"); |
| 97 | } |
| 98 | fclose(fp); |
| 99 | |
Alyssa Rosenzweig | b150fa2 | 2020-03-31 13:08:16 -0400 | [diff] [blame] | 100 | disassemble_bifrost(stdout, code, filesize, verbose); |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 101 | free(code); |
| 102 | } |
| 103 | |
Alyssa Rosenzweig | bf1929e | 2020-03-24 13:48:06 -0400 | [diff] [blame] | 104 | static void |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 105 | test_vertex(char **argv) |
Alyssa Rosenzweig | bf1929e | 2020-03-24 13:48:06 -0400 | [diff] [blame] | 106 | { |
| 107 | void *memctx = NULL; /* TODO */ |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 108 | struct panfrost_device *dev = bit_initialize(memctx); |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 109 | |
| 110 | float iubo[] = { |
| 111 | 0.1, 0.2, 0.3, 0.4 |
| 112 | }; |
| 113 | |
| 114 | float iattr[] = { |
| 115 | 0.5, 0.6, 0.7, 0.8 |
| 116 | }; |
| 117 | |
| 118 | float expected[] = { |
| 119 | 0.6, 0.8, 1.0, 1.2 |
| 120 | }; |
| 121 | |
| 122 | bit_vertex(dev, compile_shader(argv, true), |
| 123 | (uint32_t *) iubo, sizeof(iubo), |
| 124 | (uint32_t *) iattr, sizeof(iattr), |
| 125 | (uint32_t *) expected, sizeof(expected), |
| 126 | BIT_DEBUG_ALL); |
Alyssa Rosenzweig | bf1929e | 2020-03-24 13:48:06 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Alyssa Rosenzweig | b26214e | 2020-03-30 21:21:49 -0400 | [diff] [blame] | 129 | static void |
Alyssa Rosenzweig | 82597c4 | 2020-04-01 12:59:53 -0400 | [diff] [blame] | 130 | tests(void) |
| 131 | { |
| 132 | void *memctx = NULL; /* TODO */ |
| 133 | struct panfrost_device *dev = bit_initialize(memctx); |
Alyssa Rosenzweig | e9967e9 | 2020-04-06 13:03:06 -0400 | [diff] [blame] | 134 | bit_packing(dev, BIT_DEBUG_FAIL); |
Alyssa Rosenzweig | 82597c4 | 2020-04-01 12:59:53 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void |
Alyssa Rosenzweig | b26214e | 2020-03-30 21:21:49 -0400 | [diff] [blame] | 138 | run(const char *filename) |
| 139 | { |
| 140 | FILE *fp = fopen(filename, "rb"); |
| 141 | assert(fp); |
| 142 | |
| 143 | fseek(fp, 0, SEEK_END); |
| 144 | unsigned filesize = ftell(fp); |
| 145 | rewind(fp); |
| 146 | |
| 147 | unsigned char *code = malloc(filesize); |
| 148 | unsigned res = fread(code, 1, filesize, fp); |
| 149 | if (res != filesize) { |
| 150 | printf("Couldn't read full file\n"); |
| 151 | } |
| 152 | fclose(fp); |
| 153 | |
| 154 | void *memctx = NULL; /* TODO */ |
| 155 | struct panfrost_device *dev = bit_initialize(memctx); |
| 156 | |
| 157 | panfrost_program prog = { |
| 158 | .compiled = { |
| 159 | .data = code, |
| 160 | .size = filesize |
| 161 | }, |
| 162 | }; |
| 163 | |
Alyssa Rosenzweig | fc446dc | 2020-04-06 14:06:59 -0400 | [diff] [blame] | 164 | bit_vertex(dev, prog, NULL, 0, NULL, 0, NULL, 0, BIT_DEBUG_ALL); |
Alyssa Rosenzweig | b26214e | 2020-03-30 21:21:49 -0400 | [diff] [blame] | 165 | |
| 166 | free(code); |
| 167 | } |
| 168 | |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 169 | int |
| 170 | main(int argc, char **argv) |
| 171 | { |
| 172 | if (argc < 2) { |
| 173 | printf("Pass a command\n"); |
| 174 | exit(1); |
| 175 | } |
Alyssa Rosenzweig | 64720d1 | 2019-08-14 12:48:04 -0700 | [diff] [blame] | 176 | |
| 177 | if (strcmp(argv[1], "compile") == 0) |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 178 | compile_shader(&argv[2], false); |
Alyssa Rosenzweig | 64720d1 | 2019-08-14 12:48:04 -0700 | [diff] [blame] | 179 | else if (strcmp(argv[1], "disasm") == 0) |
Alyssa Rosenzweig | b150fa2 | 2020-03-31 13:08:16 -0400 | [diff] [blame] | 180 | disassemble(argv[2], false); |
| 181 | else if (strcmp(argv[1], "disasm-verbose") == 0) |
| 182 | disassemble(argv[2], true); |
Alyssa Rosenzweig | 82597c4 | 2020-04-01 12:59:53 -0400 | [diff] [blame] | 183 | else if (strcmp(argv[1], "tests") == 0) |
| 184 | tests(); |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 185 | else if (strcmp(argv[1], "test-vertex") == 0) |
| 186 | test_vertex(&argv[2]); |
Alyssa Rosenzweig | b26214e | 2020-03-30 21:21:49 -0400 | [diff] [blame] | 187 | else if (strcmp(argv[1], "run") == 0) |
| 188 | run(argv[2]); |
Alyssa Rosenzweig | 64720d1 | 2019-08-14 12:48:04 -0700 | [diff] [blame] | 189 | else |
| 190 | unreachable("Unknown command. Valid: compile/disasm"); |
| 191 | |
Ryan Houdek | 2cd1aa3 | 2019-04-21 20:41:09 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | } |