|
| 1 | +#include "GlslTranslator.h" |
| 2 | +#include <fstream> |
| 3 | +#include <map> |
| 4 | + |
| 5 | +using namespace krafix; |
| 6 | + |
| 7 | +namespace { |
| 8 | + struct Variable { |
| 9 | + unsigned type; |
| 10 | + spv::StorageClass storage; |
| 11 | + }; |
| 12 | + |
| 13 | + struct Type { |
| 14 | + const char* name; |
| 15 | + }; |
| 16 | + |
| 17 | + struct Name { |
| 18 | + const char* name; |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +void GlslTranslator::outputCode(const char* baseName) { |
| 23 | + using namespace spv; |
| 24 | + |
| 25 | + std::map<unsigned, Name> names; |
| 26 | + std::map<unsigned, Type> types; |
| 27 | + std::map<unsigned, Variable> variables; |
| 28 | + |
| 29 | + std::ofstream out; |
| 30 | + std::string fileName(baseName); |
| 31 | + fileName.append(".glsl"); |
| 32 | + out.open(fileName.c_str(), std::ios::binary | std::ios::out); |
| 33 | + |
| 34 | + for (unsigned i = 0; i < instructions.size(); ++i) { |
| 35 | + Instruction& inst = instructions[i]; |
| 36 | + switch (inst.opcode) { |
| 37 | + case OpName: { |
| 38 | + Name n; |
| 39 | + unsigned id = inst.operands[0]; |
| 40 | + n.name = inst.string; |
| 41 | + names[id] = n; |
| 42 | + break; |
| 43 | + } |
| 44 | + case OpTypePointer: { |
| 45 | + Type t; |
| 46 | + unsigned id = inst.operands[0]; |
| 47 | + Type subtype = types[inst.operands[2]]; |
| 48 | + t.name = subtype.name; |
| 49 | + types[id] = t; |
| 50 | + break; |
| 51 | + } |
| 52 | + case OpTypeFloat: { |
| 53 | + Type t; |
| 54 | + unsigned id = inst.operands[0]; |
| 55 | + t.name = "float"; |
| 56 | + types[id] = t; |
| 57 | + break; |
| 58 | + } |
| 59 | + case OpTypeVector: { |
| 60 | + Type t; |
| 61 | + unsigned id = inst.operands[0]; |
| 62 | + Type subtype = types[inst.operands[1]]; |
| 63 | + if (subtype.name != NULL) { |
| 64 | + if (strcmp(subtype.name, "float") == 0 && inst.operands[2] == 4) { |
| 65 | + t.name = "vec4"; |
| 66 | + types[id] = t; |
| 67 | + } |
| 68 | + } |
| 69 | + break; |
| 70 | + } |
| 71 | + case OpVariable: { |
| 72 | + Variable v; |
| 73 | + unsigned id = inst.operands[1]; |
| 74 | + v.type = inst.operands[0]; |
| 75 | + v.storage = (StorageClass)inst.operands[2]; |
| 76 | + variables[id] = v; |
| 77 | + |
| 78 | + Type t = types[v.type]; |
| 79 | + Name n = names[id]; |
| 80 | + |
| 81 | + if (v.storage == StorageInput) { |
| 82 | + out << "varying " << t.name << " " << n.name << ";\n"; |
| 83 | + } |
| 84 | + |
| 85 | + break; |
| 86 | + } |
| 87 | + case OpFunction: |
| 88 | + out << "\nvoid main() {\n"; |
| 89 | + break; |
| 90 | + case OpFunctionEnd: |
| 91 | + out << "}\n"; |
| 92 | + break; |
| 93 | + case OpLoad: { |
| 94 | + Type t = types[inst.operands[0]]; |
| 95 | + if (names.find(inst.operands[2]) != names.end()) { |
| 96 | + Name n = names[inst.operands[2]]; |
| 97 | + out << "\t" << t.name << " _" << inst.operands[1] << " = " << n.name << ";\n"; |
| 98 | + } |
| 99 | + else { |
| 100 | + out << "\t" << t.name << " _" << inst.operands[1] << " = _" << inst.operands[2] << ";\n"; |
| 101 | + } |
| 102 | + break; |
| 103 | + } |
| 104 | + case OpStore: { |
| 105 | + Variable v = variables[inst.operands[0]]; |
| 106 | + if (v.storage == StorageOutput) { |
| 107 | + out << "\tgl_FragColor" << " = _" << inst.operands[1] << ";\n"; |
| 108 | + } |
| 109 | + else { |
| 110 | + out << "\t*" << inst.operands[0] << " = _" << inst.operands[1] << ";\n"; |
| 111 | + } |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + out.close(); |
| 118 | +} |
0 commit comments