Skip to content

Commit 2ebd79f

Browse files
author
Robert Konrad
committed
Added very basic GLSL translator.
0 parents  commit 2ebd79f

13 files changed

+1258
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Sources/AgalTranslator.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "AgalTranslator.h"

Sources/AgalTranslator.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "Translator.h"
4+
5+
namespace krafix {
6+
class AgalTranslator : public Translator {
7+
public:
8+
AgalTranslator(std::vector<unsigned>& spirv) : Translator(spirv) {}
9+
void outputCode(const char* name) {}
10+
};
11+
}

Sources/GlslTranslator.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

Sources/GlslTranslator.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "Translator.h"
4+
5+
namespace krafix {
6+
class GlslTranslator : public Translator {
7+
public:
8+
GlslTranslator(std::vector<unsigned>& spirv) : Translator(spirv) {}
9+
void outputCode(const char* name);
10+
};
11+
}

Sources/HlslTranslator.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "HlslTranslator.h"

Sources/HlslTranslator.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "Translator.h"
4+
5+
namespace krafix {
6+
class HlslTranslator : public Translator {
7+
public:
8+
HlslTranslator(std::vector<unsigned>& spirv) : Translator(spirv) {}
9+
void outputCode(const char* name) {}
10+
};
11+
}

Sources/MetalTranslator.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "MetalTranslator.h"
2+
#include <fstream>
3+
4+
using namespace krafix;
5+
6+
void MetalTranslator::outputCode(const char* baseName) {
7+
std::ofstream out("test.metal");
8+
out << "#include <metal_stdlib>\n";
9+
out << "using namespace metal;\n";
10+
out << "\n";
11+
out << "float4 gl_FragColor;\n";
12+
out << "\n";
13+
14+
out << "\n";
15+
out << "fragment float4 render_pixel() {\n";
16+
out << "kore();\n";
17+
out << "return gl_FragColor;\n";
18+
out << "}\n";
19+
}

Sources/MetalTranslator.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "Translator.h"
4+
5+
namespace krafix {
6+
class MetalTranslator : public Translator {
7+
public:
8+
MetalTranslator(std::vector<unsigned>& spirv) : Translator(spirv) {}
9+
void outputCode(const char* name);
10+
};
11+
}

Sources/Translator.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "Translator.h"
2+
3+
using namespace krafix;
4+
5+
Instruction::Instruction(std::vector<unsigned>& spirv, unsigned& index) {
6+
using namespace spv;
7+
8+
int wordCount = spirv[index] >> 16;
9+
opcode = (OpCode)(spirv[index] & 0xffff);
10+
11+
operands = wordCount > 1 ? &spirv[index + 1] : NULL;
12+
13+
switch (opcode) {
14+
case OpString:
15+
string = (char*)&spirv[index + 2];
16+
break;
17+
case OpName:
18+
string = (char*)&spirv[index + 2];
19+
break;
20+
default:
21+
string = NULL;
22+
break;
23+
}
24+
25+
index += wordCount;
26+
}
27+
28+
Translator::Translator(std::vector<unsigned>& spirv) {
29+
unsigned index = 0;
30+
unsigned magicNumber = spirv[index++];
31+
unsigned version = spirv[index++];
32+
unsigned generator = spirv[index++];
33+
unsigned bound = spirv[index++];
34+
index++;
35+
36+
while (index < spirv.size()) {
37+
instructions.push_back(Instruction(spirv, index));
38+
}
39+
40+
printf("Read %i instructions.\n", instructions.size());
41+
}

Sources/Translator.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <SPIRV/spirv.h>
4+
#include <vector>
5+
6+
namespace krafix {
7+
class Instruction {
8+
public:
9+
Instruction(std::vector<unsigned>& spirv, unsigned& index);
10+
11+
spv::OpCode opcode;
12+
unsigned* operands;
13+
const char* string;
14+
};
15+
16+
class Translator {
17+
public:
18+
Translator(std::vector<unsigned>& spirv);
19+
virtual void outputCode(const char* name) = 0;
20+
protected:
21+
std::vector<Instruction> instructions;
22+
};
23+
}

0 commit comments

Comments
 (0)