-
Notifications
You must be signed in to change notification settings - Fork 147
/
main.cpp
203 lines (179 loc) · 7.26 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright 2017-2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <cstdint>
#include <cstdio>
#if defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>
#endif
#include <cassert>
#include <cstring>
#include <fstream>
#include <iostream>
#include "common/output_stream.h"
#include "examples/arg_parser.h"
#include "spirv_reflect.h"
// =================================================================================================
// PrintUsage()
// =================================================================================================
void PrintUsage() {
std::cout << "Usage: spirv-reflect [OPTIONS] path/to/SPIR-V/bytecode.spv" << std::endl
<< "Prints a summary of the reflection data extracted from SPIR-V "
"bytecode."
<< std::endl
<< "Options:" << std::endl
<< " --help Display this message" << std::endl
<< " -o,--output Print output to file. [default: stdout]" << std::endl
<< " -y,--yaml Format output as YAML. [default: disabled]" << std::endl
<< " -v VERBOSITY Specify output verbosity (YAML output "
"only):"
<< std::endl
<< " 0: shader info, block variables, interface "
"variables,"
<< std::endl
<< " descriptor bindings. No type "
"descriptions. [default]"
<< std::endl
<< " 1: Everything above, plus type "
"descriptions."
<< std::endl
<< " 2: Everything above, plus SPIR-V bytecode "
"and all internal"
<< std::endl
<< " type descriptions. If you're not "
"working on SPIRV-Reflect"
<< std::endl
<< " itself, you probably don't want this." << std::endl
<< "-e,--entrypoint Prints the entry point found in shader "
"module."
<< std::endl
<< "-s,--stage Prints the Vulkan shader stage found in "
"shader module."
<< std::endl
<< "-f,--file Prints the source file found in shader "
"module."
<< std::endl
<< "-fcb,--flatten_cbuffers Flatten constant buffers on non-YAML "
"output."
<< std::endl;
}
// =================================================================================================
// main()
// =================================================================================================
int main(int argn, char** argv) {
ArgParser arg_parser;
arg_parser.AddFlag("h", "help", "");
arg_parser.AddOptionString("o", "output", "");
arg_parser.AddFlag("y", "yaml", "");
arg_parser.AddOptionInt("v", "verbosity", "", 0);
arg_parser.AddFlag("e", "entrypoint", "");
arg_parser.AddFlag("s", "stage", "");
arg_parser.AddFlag("f", "file", "");
arg_parser.AddFlag("fcb", "flatten_cbuffers", "");
arg_parser.AddFlag("ci", "ci", ""); // Not advertised
if (!arg_parser.Parse(argn, argv, std::cerr)) {
PrintUsage();
return EXIT_FAILURE;
}
if (arg_parser.GetFlag("h", "help")) {
PrintUsage();
return EXIT_SUCCESS;
}
std::string output_file;
arg_parser.GetString("o", "output", &output_file);
FILE* output_fp = output_file.empty() ? NULL : freopen(output_file.c_str(), "w", stdout);
bool output_as_yaml = arg_parser.GetFlag("y", "yaml");
int yaml_verbosity = 0;
arg_parser.GetInt("v", "verbosity", &yaml_verbosity);
bool print_entry_point = arg_parser.GetFlag("e", "entrypoint");
bool print_shader_stage = arg_parser.GetFlag("s", "stage");
bool print_source_file = arg_parser.GetFlag("f", "file");
bool flatten_cbuffers = arg_parser.GetFlag("fcb", "flatten_cbuffers");
bool ci_mode = arg_parser.GetFlag("ci", "ci");
std::string input_spv_path;
std::vector<uint8_t> spv_data;
// Get SPIR-V data/input
if (arg_parser.GetArg(0, &input_spv_path)) {
std::ifstream spv_ifstream(input_spv_path.c_str(), std::ios::binary);
if (!spv_ifstream.is_open()) {
std::cerr << "ERROR: could not open '" << input_spv_path << "' for reading" << std::endl;
return EXIT_FAILURE;
}
spv_ifstream.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(spv_ifstream.tellg());
spv_ifstream.seekg(0, std::ios::beg);
spv_data.resize(size);
spv_ifstream.read((char*)spv_data.data(), size);
} else {
uint8_t buffer[4096];
size_t bytes_read = fread(buffer, 1, sizeof(buffer), stdin);
if (bytes_read == 0) {
std::cerr << "ERROR: no SPIR-V file specified" << std::endl;
return EXIT_FAILURE;
}
spv_data.insert(spv_data.end(), buffer, buffer + bytes_read);
while ((bytes_read = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
spv_data.insert(spv_data.end(), buffer, buffer + bytes_read);
}
}
// run reflection with input
{
spv_reflect::ShaderModule reflection(spv_data.size(), spv_data.data());
if (reflection.GetResult() != SPV_REFLECT_RESULT_SUCCESS) {
std::cerr << "ERROR: could not process '" << input_spv_path << "' (is it a valid SPIR-V bytecode?)" << std::endl;
return EXIT_FAILURE;
}
if (ci_mode) {
// When running CI we want to just test that SPIRV-Reflect doesn't crash,
// The output is not important (as there is nothing to compare it too)
// This hidden flag is here to allow a way to suppress the logging in CI
// to only the shader name (otherwise the logs noise and GBs large)
std::cout << input_spv_path << std::endl;
return EXIT_SUCCESS;
}
if (print_entry_point || print_shader_stage || print_source_file) {
size_t printed_count = 0;
if (print_entry_point) {
std::cout << reflection.GetEntryPointName();
++printed_count;
}
if (print_shader_stage) {
if (printed_count > 0) {
std::cout << ";";
}
std::cout << ToStringShaderStage(reflection.GetShaderStage());
}
if (print_source_file) {
if (printed_count > 0) {
std::cout << ";";
}
std::cout << (reflection.GetSourceFile() != NULL ? reflection.GetSourceFile() : "");
}
std::cout << std::endl;
} else {
if (output_as_yaml) {
SpvReflectToYaml yamlizer(reflection.GetShaderModule(), yaml_verbosity);
std::cout << yamlizer;
} else {
WriteReflection(reflection, flatten_cbuffers, std::cout);
std::cout << std::endl;
std::cout << std::endl;
}
}
}
if (output_fp) {
fclose(output_fp);
}
return EXIT_SUCCESS;
}