blob: 24dd3c2d5a5582050af07b72467e32a0608803a1 [file] [log] [blame]
Ryan Houdek2cd1aa32019-04-21 20:41:09 -07001/*
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 Rosenzweigfca491c2019-08-14 12:21:23 -070024#include "disassemble.h"
25
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070026#include "main/mtypes.h"
27#include "compiler/glsl/standalone.h"
28#include "compiler/glsl/glsl_to_nir.h"
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070029#include "compiler/glsl/gl_nir.h"
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070030#include "compiler/nir_types.h"
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070031#include "util/u_dynarray.h"
Alyssa Rosenzweigd8d8b082019-08-14 12:28:01 -070032#include "bifrost_compile.h"
Alyssa Rosenzweigbf1929e2020-03-24 13:48:06 -040033#include "test/bit.h"
Alyssa Rosenzweigd8d8b082019-08-14 12:28:01 -070034
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -040035static panfrost_program
36compile_shader(char **argv, bool vertex_only)
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070037{
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 Rosenzweig77e04eb2020-03-27 14:39:18 -040046 .glsl_version = 300, /* ES - needed for precision */
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070047 .do_link = true,
Alyssa Rosenzweig77e04eb2020-03-27 14:39:18 -040048 .lower_precision = true
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070049 };
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 Rosenzweige6f5ae82020-03-10 16:09:44 -040056 panfrost_program compiled;
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070057 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 Rosenzweig977a38c2020-03-05 16:52:29 -050060 NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0);
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070061 NIR_PASS_V(nir[i], nir_split_var_copies);
62 NIR_PASS_V(nir[i], nir_lower_var_copies);
63
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070064 /* before buffers and vars_to_ssa */
Eric Anholt7342b8592020-01-23 10:49:02 -080065 NIR_PASS_V(nir[i], gl_nir_lower_images, true);
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070066
67 NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);
68 NIR_PASS_V(nir[i], nir_opt_constant_folding);
Alyssa Rosenzweig0b26cb12020-03-03 14:27:05 -050069
70 unsigned product_id = 0x7212; /* Mali G52 */
71 bifrost_compile_shader_nir(nir[i], &compiled, product_id);
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -040072
73 if (vertex_only)
74 return compiled;
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070075 }
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -040076
77 return compiled;
Alyssa Rosenzweigfca491c2019-08-14 12:21:23 -070078}
79
80static void
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070081disassemble(const char *filename)
82{
83 FILE *fp = fopen(filename, "rb");
84 assert(fp);
85
86 fseek(fp, 0, SEEK_END);
Alyssa Rosenzweig5ebdd102019-08-21 09:39:04 -070087 unsigned filesize = ftell(fp);
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070088 rewind(fp);
89
90 unsigned char *code = malloc(filesize);
Alyssa Rosenzweig5ebdd102019-08-21 09:39:04 -070091 unsigned res = fread(code, 1, filesize, fp);
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070092 if (res != filesize) {
93 printf("Couldn't read full file\n");
94 }
95 fclose(fp);
96
Icecream9520a89572020-01-23 09:59:57 +130097 disassemble_bifrost(stdout, code, filesize, false);
Ryan Houdek2cd1aa32019-04-21 20:41:09 -070098 free(code);
99}
100
Alyssa Rosenzweigbf1929e2020-03-24 13:48:06 -0400101static void
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -0400102test_vertex(char **argv)
Alyssa Rosenzweigbf1929e2020-03-24 13:48:06 -0400103{
104 void *memctx = NULL; /* TODO */
Alyssa Rosenzweiga0d1be32020-03-24 14:08:16 -0400105 struct panfrost_device *dev = bit_initialize(memctx);
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -0400106 bit_vertex(dev, compile_shader(argv, true));
Alyssa Rosenzweigbf1929e2020-03-24 13:48:06 -0400107}
108
Ryan Houdek2cd1aa32019-04-21 20:41:09 -0700109int
110main(int argc, char **argv)
111{
112 if (argc < 2) {
113 printf("Pass a command\n");
114 exit(1);
115 }
Alyssa Rosenzweig64720d12019-08-14 12:48:04 -0700116
117 if (strcmp(argv[1], "compile") == 0)
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -0400118 compile_shader(&argv[2], false);
Alyssa Rosenzweig64720d12019-08-14 12:48:04 -0700119 else if (strcmp(argv[1], "disasm") == 0)
Ryan Houdek2cd1aa32019-04-21 20:41:09 -0700120 disassemble(argv[2]);
Alyssa Rosenzweig3d7166f2020-03-25 14:56:06 -0400121 else if (strcmp(argv[1], "test-vertex") == 0)
122 test_vertex(&argv[2]);
Alyssa Rosenzweig64720d12019-08-14 12:48:04 -0700123 else
124 unreachable("Unknown command. Valid: compile/disasm");
125
Ryan Houdek2cd1aa32019-04-21 20:41:09 -0700126 return 0;
127}