2
* Copyright © 2015 Intel Corporation
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
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
* Jason Ekstrand (jason@jlekstrand.net)
29
* A simple executable that opens a SPIR-V shader, converts it to NIR, and
30
* dumps out the result. This should be useful for testing the
34
#include "spirv/nir_spirv.h"
37
#include <sys/types.h>
47
static gl_shader_stage
48
stage_to_enum(char *stage)
50
if (!strcmp(stage, "vertex"))
51
return MESA_SHADER_VERTEX;
52
else if (!strcmp(stage, "tess-ctrl"))
53
return MESA_SHADER_TESS_CTRL;
54
else if (!strcmp(stage, "tess-eval"))
55
return MESA_SHADER_TESS_EVAL;
56
else if (!strcmp(stage, "geometry"))
57
return MESA_SHADER_GEOMETRY;
58
else if (!strcmp(stage, "fragment"))
59
return MESA_SHADER_FRAGMENT;
60
else if (!strcmp(stage, "compute"))
61
return MESA_SHADER_COMPUTE;
62
else if (!strcmp(stage, "kernel"))
63
return MESA_SHADER_KERNEL;
64
else if (!strcmp(stage, "task"))
65
return MESA_SHADER_TASK;
66
else if (!strcmp(stage, "mesh"))
67
return MESA_SHADER_MESH;
69
return MESA_SHADER_NONE;
73
print_usage(char *exec_name, FILE *f)
76
"Usage: %s [options] file\n"
78
" -h --help Print this help.\n"
79
" -s, --stage <stage> Specify the shader stage. Valid stages are:\n"
80
" vertex, tess-ctrl, tess-eval, geometry, fragment,\n"
81
" task, mesh, compute, and kernel (OpenCL-style compute).\n"
82
" -e, --entry <name> Specify the entry-point name.\n"
83
" -g, --opengl Use OpenGL environment instead of Vulkan for\n"
88
int main(int argc, char **argv)
90
gl_shader_stage shader_stage = MESA_SHADER_FRAGMENT;
91
char *entry_point = "main";
93
enum nir_spirv_execution_environment env = NIR_SPIRV_VULKAN;
95
static struct option long_options[] =
97
{"help", no_argument, 0, 'h'},
98
{"stage", required_argument, 0, 's'},
99
{"entry", required_argument, 0, 'e'},
100
{"opengl", no_argument, 0, 'g'},
104
while ((ch = getopt_long(argc, argv, "hs:e:g", long_options, NULL)) != -1)
109
print_usage(argv[0], stdout);
112
shader_stage = stage_to_enum(optarg);
113
if (shader_stage == MESA_SHADER_NONE)
115
fprintf(stderr, "Unknown stage \"%s\"\n", optarg);
116
print_usage(argv[0], stderr);
121
entry_point = optarg;
124
env = NIR_SPIRV_OPENGL;
127
fprintf(stderr, "Unrecognized option \"%s\".\n", optarg);
128
print_usage(argv[0], stderr);
133
const char *filename = argv[optind];
134
int fd = open(filename, O_RDONLY);
137
fprintf(stderr, "Failed to open %s\n", filename);
141
off_t len = lseek(fd, 0, SEEK_END);
142
if (len % WORD_SIZE != 0)
144
fprintf(stderr, "File length isn't a multiple of the word size\n");
145
fprintf(stderr, "Are you sure this is a valid SPIR-V shader?\n");
150
size_t word_count = len / WORD_SIZE;
152
const void *map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
153
if (map == MAP_FAILED)
155
fprintf(stderr, "Failed to mmap the file: errno=%d, %s\n",
156
errno, strerror(errno));
161
glsl_type_singleton_init_or_ref();
163
struct spirv_to_nir_options spirv_opts = {
165
.use_deref_buffer_array_length = env == NIR_SPIRV_OPENGL,
168
if (shader_stage == MESA_SHADER_KERNEL) {
169
spirv_opts.environment = NIR_SPIRV_OPENCL;
170
spirv_opts.caps.address = true;
171
spirv_opts.caps.float64 = true;
172
spirv_opts.caps.int8 = true;
173
spirv_opts.caps.int16 = true;
174
spirv_opts.caps.int64 = true;
175
spirv_opts.caps.kernel = true;
178
nir_shader *nir = spirv_to_nir(map, word_count, NULL, 0,
179
shader_stage, entry_point,
183
nir_print_shader(nir, stderr);
185
fprintf(stderr, "SPIRV to NIR compilation failed\n");
187
glsl_type_singleton_decref();