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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* This program is used to pre-compile some dummy shaders from
* nshader.fx to be used in directx_shaders.c file. You will need
* fxc.exe and vsa.exe to run this program, which can be found in
* the April 2008 DirectX SDK (it has been removed in the newer SDK
* versions).
*
* Usage: Just run this program, and copy the output (tables.h)
* into the appropriate location in directx_shaders.c.
*
* Note, that this is not supposed to be compiled into the library.
* Compile this manually if needed.
*
* By Michał Cichoń.
*
* See readme.txt for copyright information.
*/
# include <iostream>
# include <algorithm>
# include <fstream>
# include <cstdlib>
# include <vector>
# include <string>
# include <sstream>
# include <cstddef>
# include <iomanip>
using namespace std;
typedef unsigned char byte_t;
typedef vector<byte_t> byte_vector_t;
struct technique_t
{
string name;
string shader;
byte_vector_t binary;
};
typedef vector<technique_t> technique_vector_t;
int compile(const string& source, const string& dest)
{
return system(("fxc /nologo /Tfx_2_0 /Op /O3 /Fc " + dest + " " + source + " > nul").c_str());
}
int assemble(const string& source, const string& dest)
{
return system(("vsa /nologo /Fo " + dest + " " + source + " > nul").c_str());
}
bool extract_shader(technique_t& technique, istream& stream)
{
technique.shader.clear();
bool inside = false;
bool record = false;
string line;
while (!getline(stream, line).fail())
{
if (!inside && !record)
{
string::size_type pos = line.find_first_of("asm");
if (pos == string::npos || line.compare(pos, 3, "asm") != 0)
continue;
// strip everything to including "asm" keyword
line = line.substr(pos + 3);
inside = true;
}
if (inside && !record)
{
string::size_type pos = line.find_first_of("{");
if (pos == string::npos)
continue;
record = true;
continue;
}
if (inside && record)
{
string::size_type pos = line.find_first_of("}");
if (pos != string::npos)
return true;
string::size_type non_white = line.find_first_not_of(" ");
if (non_white != string::npos)
technique.shader += line.substr(non_white) + "\n";
else
technique.shader += "\n";
}
}
return false;
}
int extract_techniques(technique_vector_t& techniques, const string& filename)
{
techniques.clear();
ifstream file(filename.c_str());
if (!file)
return 0;
technique_t technique;
string line;
while (!getline(file, line).fail())
{
string::size_type pos = line.find_first_of("technique");
if (pos != 0 || line.compare(pos, 9, "technique") != 0)
continue;
technique.name = line.substr(10);
if (technique.name.empty())
{
cerr << " Unnamed technique. Skipping..." << endl;
continue;
}
cout << " Found: " << technique.name << endl;
if (!extract_shader(technique, file))
{
cerr << " Failed! Skipping..." << endl;
continue;
}
techniques.push_back(technique);
}
return (int)techniques.size();
}
int assemble_technique(technique_t& technique)
{
const string source = "vshader.vs";
const string dest = "vshader.vso";
{
ofstream file(source.c_str());
file << technique.shader;
}
int result = assemble(source, dest);
if (result)
return result;
ifstream file(dest.c_str(), ios::in | ios::binary);
if (!file)
{
_unlink(dest.c_str());
_unlink(source.c_str());
return 1;
}
file.seekg(0, ios::end);
technique.binary.resize(file.tellg());
file.seekg(0, ios::beg);
file.read((char*)&technique.binary.front(), technique.binary.size());
file.close();
_unlink(dest.c_str());
_unlink(source.c_str());
return 0;
}
int assemble_techniques(technique_vector_t& techniques)
{
int count = 0;
technique_vector_t::iterator it, it_end = techniques.end();
for (it = techniques.begin(); it != it_end; ++it)
{
technique_t& technique = *it;
cout << " Processing: " << technique.name << endl;
int assembled = assemble_technique(technique);
if (assembled)
{
cerr << " Failed." << endl;
continue;
}
++count;
}
return count;
}
char toupper2(char c)
{
return toupper(c);
}
int generate_table(ostream& output, technique_t& technique)
{
const int bytes_per_line = 16;
string::size_type pos = 0;
string name = technique.name, line;
while ((pos = name.find_first_of('_', pos)) != string::npos)
name[pos] = ' ';
transform(name.begin(), name.end(), name.begin(), toupper2);
output << "/*" << endl;
output << " " << name << endl << endl;
istringstream shader(technique.shader);
while (!getline(shader, line).fail())
output << " " << line << endl;
output << "*/" << endl;
output << "static const uint8_t _al_vs_" << technique.name << "[] = {" << endl;
for (size_t i = 0; i < technique.binary.size(); ++i)
{
if ((i % bytes_per_line) == 0)
output << " ";
output << "0x" << setw(2) << setfill('0') << hex << (int)technique.binary[i];
if (i != technique.binary.size() - 1)
{
if ((i % bytes_per_line) == (bytes_per_line - 1))
output << "," << endl;
else
output << ", ";
}
}
output << endl;
output.unsetf(ios::hex);
output << "};" << endl;
return 0;
}
int generate_tables(ostream& output, technique_vector_t& techniques)
{
int count = 0;
technique_vector_t::iterator it, it_end = techniques.end();
for (it = techniques.begin(); it != it_end; ++it)
{
technique_t& technique = *it;
cout << " Processing: " << technique.name << endl;
int generated = generate_table(output, technique);
if (generated)
{
cerr << " Failed." << endl;
continue;
}
output << endl;
++count;
}
return count;
}
int main()
{
const string source = "nshader.fx";
const string compiled = "nshader.fxc";
const string output = "tables.h";
cout << "Compiling NULL shader..." << endl;
if (int result = compile(source, compiled))
{
cout << " Failed!" << endl;
return result;
}
cout << "Done." << endl << endl;
cout << "Extracting techniques..." << endl;
technique_vector_t techniques;
int found = extract_techniques(techniques, compiled);
if (found)
cout << "Done. " << found << " technique(s) extracted." << endl << endl;
else
cout << "Done. No techniques extracted." << endl << endl;
_unlink(compiled.c_str());
if (found == 0)
return 0;
cout << "Assembling techniques..." << endl;
int assembled = assemble_techniques(techniques);
if (assembled)
cout << "Done. " << assembled << " technique(s) assembled." << endl << endl;
else
cout << "Done. No assembled techniques." << endl << endl;
if (assembled == 0)
return 0;
cout << "Generating tables..." << endl;
ofstream output_file(output.c_str());
int tables = generate_tables(output_file, techniques);
cout << "Done. " << tables << " table(s) generated." << endl << endl;
return 0;
}
|