]> git.tdb.fi Git - libs/gl.git/blob - source/programcompiler.cpp
Begin implementing a new shader program generator system
[libs/gl.git] / source / programcompiler.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/utils.h>
3 #include "error.h"
4 #include "program.h"
5 #include "programcompiler.h"
6 #include "shader.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 using namespace ProgramSyntax;
14
15 ProgramCompiler::ProgramCompiler():
16         module(0)
17 { }
18
19 void ProgramCompiler::compile(const string &source)
20 {
21         module = &parser.parse(source);
22 }
23
24 void ProgramCompiler::compile(IO::Base &io)
25 {
26         module = &parser.parse(io);
27 }
28
29 void ProgramCompiler::add_shaders(Program &program)
30 {
31         if(!module)
32                 throw invalid_operation("ProgramCompiler::add_shaders");
33
34         string global_source = "#version 150\n"+format_context(module->global_context);
35         if(module->vertex_context.present)
36                 program.attach_shader_owned(new VertexShader(global_source+"\n"+format_context(module->vertex_context)));
37         if(module->geometry_context.present)
38                 program.attach_shader_owned(new GeometryShader(global_source+"\n"+format_context(module->geometry_context)));
39         if(module->fragment_context.present)
40                 program.attach_shader_owned(new FragmentShader(global_source+"\n"+format_context(module->fragment_context)));
41
42         program.bind_attribute(VERTEX4, "vertex");
43         program.bind_attribute(NORMAL3, "normal");
44         program.bind_attribute(COLOR4_FLOAT, "color");
45         program.bind_attribute(TEXCOORD4, "texcoord");
46 }
47
48 string ProgramCompiler::format_context(Context &context)
49 {
50         Formatter formatter;
51         context.content.visit(formatter);
52         return formatter.formatted;
53 }
54
55
56 ProgramCompiler::Formatter::Formatter():
57         indent(0),
58         parameter_list(false),
59         else_if(false)
60 { }
61
62 string ProgramCompiler::Formatter::format_expression(Expression &expr)
63 {
64         return join(expr.tokens.begin(), expr.tokens.end(), string());
65 }
66
67 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
68 {
69         formatted += format("%s;", format_expression(expr.expression));
70 }
71
72 void ProgramCompiler::Formatter::visit(Block &block)
73 {
74         if(block.use_braces)
75         {
76                 if(else_if)
77                 {
78                         formatted += '\n';
79                         else_if = false;
80                 }
81                 formatted += format("%s{\n", string(indent*2, ' '));
82         }
83
84         bool change_indent = (!formatted.empty() && !else_if);
85         indent += change_indent;
86         string spaces(indent*2, ' ');
87         for(vector<Node *>::const_iterator i=block.body.begin(); i!=block.body.end(); ++i)
88         {
89                 if(i!=block.body.begin())
90                         formatted += '\n';
91                 if(!else_if)
92                         formatted += spaces;
93                 (*i)->visit(*this);
94         }
95         indent -= change_indent;
96
97         if(block.use_braces)
98                 formatted += format("\n%s}", string(indent*2, ' '));
99 }
100
101 void ProgramCompiler::Formatter::visit(Layout &layout)
102 {
103         formatted += "layout(";
104         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
105         {
106                 if(i!=layout.qualifiers.begin())
107                         formatted += ", ";
108                 formatted += i->identifier;
109                 if(!i->value.empty())
110                         formatted += format("=%s", i->value);
111         }
112         formatted += format(") %s;", layout.interface);
113 }
114
115 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
116 {
117         formatted += format("struct %s\n", strct.name);
118         strct.members.visit(*this);
119         formatted += ';';
120 }
121
122 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
123 {
124         if(var.constant)
125                 formatted += "const ";
126         if(!var.sampling.empty())
127                 formatted += format("%s ", var.sampling);
128         if(!var.interface.empty())
129                 formatted += format("%s ", var.interface);
130         formatted += format("%s %s", var.type, var.name);
131         if(var.array)
132                 formatted += format("[%s]", format_expression(var.array_size));
133         if(!var.init_expression.empty())
134                 formatted += format(" = %s", format_expression(var.init_expression));
135         if(!parameter_list)
136                 formatted += ';';
137 }
138
139 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
140 {
141         formatted += format("%s %s\n", iface.interface, iface.name);
142         iface.members.visit(*this);
143         formatted += ';';
144 }
145
146 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
147 {
148         formatted += format("%s %s(", func.return_type, func.name);
149         parameter_list = true;
150         for(vector<VariableDeclaration *>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
151         {
152                 if(i!=func.parameters.begin())
153                         formatted += ", ";
154                 (*i)->visit(*this);
155         }
156         parameter_list = false;
157         formatted += ')';
158         if(func.definition)
159         {
160                 formatted += '\n';
161                 func.body.visit(*this);
162         }
163         else
164                 formatted += ';';
165 }
166
167 void ProgramCompiler::Formatter::visit(Conditional &cond)
168 {
169         if(else_if)
170         {
171                 formatted += ' ';
172                 else_if = false;
173         }
174         formatted += format("if(%s)\n", format_expression(cond.condition));
175         cond.body.visit(*this);
176         if(!cond.else_body.body.empty())
177         {
178                 formatted += format("\n%selse", string(indent*2, ' '));
179                 else_if = true;
180                 cond.else_body.visit(*this);
181                 else_if = false;
182         }
183 }
184
185 void ProgramCompiler::Formatter::visit(Iteration &iter)
186 {
187         formatted += "for(";
188         iter.init_statement->visit(*this);
189         formatted += format(" %s; %s)\n", format_expression(iter.condition), format_expression(iter.loop_expression));
190         iter.body.visit(*this);
191 }
192
193 void ProgramCompiler::Formatter::visit(Return &ret)
194 {
195         formatted += format("return %s;", format_expression(ret.expression));
196 }
197
198 } // namespace GL
199 } // namespace Msp