Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/compiler/spirv/spirv2nir.c
4545 views
1
/*
2
* Copyright © 2015 Intel Corporation
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
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*
23
* Authors:
24
* Jason Ekstrand ([email protected])
25
*
26
*/
27
28
/*
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
31
* spirv_to_nir code.
32
*/
33
34
#include "spirv/nir_spirv.h"
35
36
#include <sys/mman.h>
37
#include <sys/types.h>
38
#include <fcntl.h>
39
#include <unistd.h>
40
#include <stdio.h>
41
#include <errno.h>
42
#include <string.h>
43
#include <getopt.h>
44
45
#define WORD_SIZE 4
46
47
static gl_shader_stage
48
stage_to_enum(char *stage)
49
{
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
65
return MESA_SHADER_NONE;
66
}
67
68
static void
69
print_usage(char *exec_name, FILE *f)
70
{
71
fprintf(f,
72
"Usage: %s [options] file\n"
73
"Options:\n"
74
" -h --help Print this help.\n"
75
" -s, --stage <stage> Specify the shader stage. Valid stages are:\n"
76
" vertex, tess-ctrl, tess-eval, geometry, fragment,\n"
77
" compute, and kernel (OpenCL-style compute).\n"
78
" -e, --entry <name> Specify the entry-point name.\n"
79
" -g, --opengl Use OpenGL environment instead of Vulkan for\n"
80
" graphics stages.\n"
81
, exec_name);
82
}
83
84
int main(int argc, char **argv)
85
{
86
gl_shader_stage shader_stage = MESA_SHADER_FRAGMENT;
87
char *entry_point = "main";
88
int ch;
89
enum nir_spirv_execution_environment env = NIR_SPIRV_VULKAN;
90
91
static struct option long_options[] =
92
{
93
{"help", no_argument, 0, 'h'},
94
{"stage", required_argument, 0, 's'},
95
{"entry", required_argument, 0, 'e'},
96
{"opengl", no_argument, 0, 'g'},
97
{0, 0, 0, 0}
98
};
99
100
while ((ch = getopt_long(argc, argv, "hs:e:g", long_options, NULL)) != -1)
101
{
102
switch (ch)
103
{
104
case 'h':
105
print_usage(argv[0], stdout);
106
return 0;
107
case 's':
108
shader_stage = stage_to_enum(optarg);
109
if (shader_stage == MESA_SHADER_NONE)
110
{
111
fprintf(stderr, "Unknown stage \"%s\"\n", optarg);
112
print_usage(argv[0], stderr);
113
return 1;
114
}
115
break;
116
case 'e':
117
entry_point = optarg;
118
break;
119
case 'g':
120
env = NIR_SPIRV_OPENGL;
121
break;
122
default:
123
fprintf(stderr, "Unrecognized option \"%s\".\n", optarg);
124
print_usage(argv[0], stderr);
125
return 1;
126
}
127
}
128
129
const char *filename = argv[optind];
130
int fd = open(filename, O_RDONLY);
131
if (fd < 0)
132
{
133
fprintf(stderr, "Failed to open %s\n", filename);
134
return 1;
135
}
136
137
off_t len = lseek(fd, 0, SEEK_END);
138
if (len % WORD_SIZE != 0)
139
{
140
fprintf(stderr, "File length isn't a multiple of the word size\n");
141
fprintf(stderr, "Are you sure this is a valid SPIR-V shader?\n");
142
close(fd);
143
return 1;
144
}
145
146
size_t word_count = len / WORD_SIZE;
147
148
const void *map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
149
if (map == MAP_FAILED)
150
{
151
fprintf(stderr, "Failed to mmap the file: errno=%d, %s\n",
152
errno, strerror(errno));
153
close(fd);
154
return 1;
155
}
156
157
glsl_type_singleton_init_or_ref();
158
159
struct spirv_to_nir_options spirv_opts = {
160
.environment = env,
161
.use_deref_buffer_array_length = env == NIR_SPIRV_OPENGL,
162
};
163
164
if (shader_stage == MESA_SHADER_KERNEL) {
165
spirv_opts.environment = NIR_SPIRV_OPENCL;
166
spirv_opts.caps.address = true;
167
spirv_opts.caps.float64 = true;
168
spirv_opts.caps.int8 = true;
169
spirv_opts.caps.int16 = true;
170
spirv_opts.caps.int64 = true;
171
spirv_opts.caps.kernel = true;
172
}
173
174
nir_shader *nir = spirv_to_nir(map, word_count, NULL, 0,
175
shader_stage, entry_point,
176
&spirv_opts, NULL);
177
178
if (nir)
179
nir_print_shader(nir, stderr);
180
else
181
fprintf(stderr, "SPIRV to NIR compilation failed\n");
182
183
glsl_type_singleton_decref();
184
185
return 0;
186
}
187
188