forked from arrayfire/forge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglsl2cpp.cpp
184 lines (155 loc) · 4.75 KB
/
glsl2cpp.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
// Umar Arshad
// Copyright 2015-2019
//
// Modified by Pradeep Garigipati on Dec 30, 2015 for Forge
// Purpose of modification: To use the program to convert
// GLSL shader files into compile time constant string literals
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <memory>
using namespace std;
typedef map<string, string> opt_t;
static
void print_usage() {
cout << R"delimiter(GLSL2CPP
Converts OpenGL shader files to C++ headers. It is similar to bin2c and xxd but adds
support for namespaces.
| --name | name of the variable (default: var) |
| --file | input file |
| --output | output file (If no output is specified then it prints to stdout) |
| --type | Type of variable (default: char) |
| --namespace | A space seperated list of namespaces |
| --formatted | Tabs for formatting |
| --version | Prints my name |
| --help | Prints usage info |
Example
-------
Command:
./glsl2cpp --file blah.txt --namespace shaders --formatted --name image_vs
Will produce the following:
#pragma once
#include <cstddef>
namespace shaders {
static const char image_vs[] = R"shader(
#version 330
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 tex;
uniform mat4 matrix;
out vec2 texcoord;
void main() {
texcoord = tex;
gl_Position = matrix * vec4(pos,1.0);
}
)shader";
}
)delimiter";
exit(0);
}
static bool formatted;
static
void add_tabs(const int level)
{
if(formatted) {
for(int i =0; i < level; i++) {
cout << "\t";
}
}
}
static
opt_t parse_options(const vector<string>& args)
{
opt_t options;
options["--name"] = "";
options["--type"] = "";
options["--file"] = "";
options["--output"] = "";
options["--namespace"] = "";
options["--eof"] = "";
//Parse Arguments
string curr_opt;
bool verbose = false;
for(auto arg : args) {
if(arg == "--verbose") {
verbose = true;
} else if(arg == "--formatted") {
formatted = true;
} else if(arg == "--version") {
cout << args[0] << " Original Author: Umar Arshad;\n Modified later by: Pradeep Garigipati." << endl;
} else if(arg == "--help") {
print_usage();
} else if(options.find(arg) != options.end()) {
curr_opt = arg;
} else if(curr_opt.empty()) {
//cerr << "Invalid Argument: " << arg << endl;
} else {
if(options[curr_opt] != "") {
options[curr_opt] += " " + arg;
}
else {
options[curr_opt] += arg;
}
}
}
if(verbose) {
for(auto opts : options) {
cout << get<0>(opts) << " " << get<1>(opts) << endl;
}
}
return options;
}
int main(int argc, const char * const * const argv)
{
vector<string> args(argv, argv+argc);
opt_t&& options = parse_options(args);
//Save default cout buffer. Need this to prevent crash.
auto bak = cout.rdbuf();
unique_ptr<ofstream> outfile;
// Set defaults
if(options["--name"] == "") { options["--name"] = "var"; }
if(options["--output"] != "") {
//redirect stream if output file is specified
outfile.reset(new ofstream(options["--output"]));
cout.rdbuf(outfile->rdbuf());
}
cout << "#pragma once\n";
cout << "#include <string>\n"; // defines std::string
int ns_cnt = 0;
int level = 0;
if(options["--namespace"] != "") {
std::stringstream namespaces(options["--namespace"]);
string name;
namespaces >> name;
do {
add_tabs(level++);
cout << "namespace " << name << "\n{\n";
ns_cnt++;
namespaces >> name;
} while(!namespaces.fail());
}
if(options["--type"] == "") {
options["--type"] = "std::string";
}
add_tabs(level);
cout << "static const " << options["--type"] << " " << options["--name"] << " = R\"shader(\n";
level++;
ifstream input(options["--file"]);
for(std::string line; std::getline(input, line);) {
add_tabs(level);
cout << line << endl;
}
if (options["--eof"].c_str()[0] == '1') {
// Add end of file character
cout << "0x0";
}
add_tabs(--level);
cout << ")shader\";\n";
while(ns_cnt--) {
add_tabs(--level);
cout << "}\n";
}
cout.rdbuf(bak);
}