This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgen_shaders.rb
executable file
·305 lines (262 loc) · 9.09 KB
/
gen_shaders.rb
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
#!/usr/bin/ruby
# I hate writting OpenGL shader glue code in C++, so I wrote this.
# the input is a set of uniforms and attribs
# the output is a c++ class that will:
# 1) provide a nice interface to get and set these values.
# 2) cache the program indices for each uniform and attrib.
# 3) bind the program, uniform and attribs for rendering.
require('erb')
def assert &block
raise AssersionFailure unless yield
end
class Object
def camelcase
to_s.split('_').map() {|s| s[0,1].upcase + s[1..-1]}.join
end
end
class String
def uncapitalize
self[0, 1].downcase + self[1..-1]
end
end
$cpp_ref_type = {
:float => 'float',
:int => 'int',
:vec2 => 'const Vector2f&',
:vec3 => 'const Vector3f&',
:vec4 => 'const Vector4f&',
:mat4 => 'const Matrixf&',
:sampler2D => 'int'
}
$cpp_decl_type = {
:float => 'float',
:int => 'int',
:vec2 => 'Vector2f',
:vec3 => 'Vector3f',
:vec4 => 'Vector4f',
:mat4 => 'Matrixf',
:sampler2D => 'int'
}
$uniform_call = {
:float => 'glUniform1fv',
:int => 'glUniform1iv',
:vec2 => 'glUniform2fv',
:vec3 => 'glUniform3fv',
:vec4 => 'glUniform4fv',
:mat4 => 'glUniformMatrix4fv',
:sampler2D => 'glUniform1iv',
}
$attrib_size = {
:float => 1,
:vec2 => 2,
:vec3 => 3,
:vec4 => 4,
}
$cpp_header = <<CODE
#ifndef <%= prog.name.upcase %>_H
#define <%= prog.name.upcase %>_H
#include "shader.h"
#include "abaci.h"
#include <vector>
class <%= prog.name.camelcase %> : public Shader
{
public:
enum Locs {
<% prog.uniforms.each do |u| %>
<%= u.name.camelcase %>UniformLoc,
<% end %>
<% prog.attribs.each do |a| %>
<%= a.name.camelcase %>AttribLoc,
<% end %>
NumLocs
};
<%= prog.name.camelcase %>() : Shader()
{
for (int i = 0; i < NumLocs; i++)
m_locs[i] = -1;
}
bool compileAndLink()
{
return compileAndLinkFromFiles("<%= prog.vsh %>", "<%= prog.fsh %>");
}
void apply(const Shader* prevShader, const float* attribPtr) const
{
<% prog.uniforms.each do |u| %>
if (m_locs[<%= u.name.camelcase %>UniformLoc] < 0)
m_locs[<%= u.name.camelcase %>UniformLoc] = getUniformLoc("<%= u.name %><%= u.class == BasicType ? "" : "[0]" %>");
assert(m_locs[<%= u.name.camelcase %>UniformLoc] >= 0);
<% end %>
<% prog.attribs.each do |a| %>
if (m_locs[<%= a.name.camelcase %>AttribLoc])
m_locs[<%= a.name.camelcase %>AttribLoc] = getAttribLoc("<%= a.name %>");
assert(m_locs[<%= a.name.camelcase %>AttribLoc] >= 0);
<% end %>
Shader::apply(prevShader);
<% prog.uniforms.each do |u| %>
<%= prog.gen_uniform_call(u) %>
<% end %>
<% stride = prog.attribs.map {|a| 4 * prog.calc_attrib_size(a)}.reduce {|a, b| a + b} %>
<% offset = 0 %>
<% prog.attribs.each do |a| %>
<%= prog.gen_attrib_call(a, offset, stride) %>
<% offset += prog.calc_attrib_size(a) %>
<% end %>
}
// uniform accessors
<% prog.uniforms.each do |u| %>
<%= prog.uniform_ref_type(u) %> get<%= u.name.camelcase %>() const { return m_<%= u.name.camelcase.uncapitalize %>; }
void set<%= u.name.camelcase %>(<%= prog.uniform_ref_type(u) %> v) { m_<%= u.name.camelcase.uncapitalize %> = v; }
<% end %>
protected:
mutable int m_locs[NumLocs];
<% prog.uniforms.each do |u| %>
<%= prog.uniform_decl_type(u) %> m_<%= u.name.camelcase.uncapitalize %>;
<% end %>
};
#endif // #define <%= prog.name.upcase %>_H
CODE
BasicType = Struct.new(:type, :name)
ArrayType = Struct.new(:type, :name, :count)
class Prog < Struct.new(:name, :vsh, :fsh, :uniforms, :attribs)
def build
filename = "#{name.camelcase}.h"
# writable
File.chmod(0666, filename) if File.exists?(filename)
File.open(filename, "w") do |f|
prog = self
erb = ERB.new($cpp_header, 0, "<>")
f.write erb.result(binding)
end
# readonly
File.chmod(0444, filename)
end
def uniform_to_tex_stage uniform
assert {uniform.class == BasicType}
textures = uniforms.find_all {|u| u.type == :sampler2D}
textures.find_index {|u| u.name == uniform.name}
end
def gen_uniform_call(u)
loc = "m_locs[#{u.name.camelcase}UniformLoc]"
count = u.class == BasicType ? 1 : u.count
value = "m_#{u.name.camelcase.uncapitalize}"
assert {$uniform_call[u.type]}
case u.type
when :int
if u.class == BasicType
"#{$uniform_call[u.type]}(#{loc}, #{count}, (int*)&#{value});"
else
"#{$uniform_call[u.type]}(#{loc}, #{count}, (int*)&#{value}[0]);"
end
when :sampler2D
stage = uniform_to_tex_stage(u)
assert {stage && u.class == BasicType} # ArrayType not supported on samplers!
"glUniform1i(#{loc}, #{stage}); glActiveTexture(GL_TEXTURE0 + #{stage}); glBindTexture(GL_TEXTURE_2D, #{value});"
when :mat4
if u.class == BasicType
"#{$uniform_call[u.type]}(#{loc}, #{count}, false, (float*)&#{value});"
else
"#{$uniform_call[u.type]}(#{loc}, #{count}, false, (float*)&#{value}[0]);"
end
else
if u.class == BasicType
"#{$uniform_call[u.type]}(#{loc}, #{count}, (float*)&#{value});"
else
"#{$uniform_call[u.type]}(#{loc}, #{count}, (float*)&#{value}[0]);"
end
end
end
def gen_attrib_call(a, offset, stride)
assert {a.class == BasicType} # array types not supported on attribs
assert {a && offset && stride}
size = calc_attrib_size(a)
assert {size}
loc = "m_locs[#{a.name.camelcase}AttribLoc]"
"glVertexAttribPointer(#{loc}, #{size}, GL_FLOAT, false, #{stride}, attribPtr + #{offset});"
end
def calc_attrib_size(a)
assert {a.class == BasicType} # array types not supported on attribs
assert {a}
size = $attrib_size[a.type]
assert {size}
size
end
def uniform_decl_type(u)
if u.class == BasicType
$cpp_decl_type[u.type]
else
"std::vector<#{$cpp_decl_type[u.type]}>"
end
end
def uniform_ref_type(u)
if u.class == BasicType
$cpp_ref_type[u.type]
else
"const std::vector<#{$cpp_decl_type[u.type]}>&"
end
end
end
progs = [Prog.new("fullbright_shader",
"shader/fullbright.vsh",
"shader/fullbright.fsh",
[BasicType.new(:vec4, :color),
BasicType.new(:mat4, :mat)],
[BasicType.new(:vec3, :pos)]),
Prog.new("fullbright_vert_color_shader",
"shader/fullbright_vert_color.vsh",
"shader/fullbright_vert_color.fsh",
[BasicType.new(:mat4, :mat)],
[BasicType.new(:vec3, :pos),
BasicType.new(:vec4, :color)]),
Prog.new("fullbright_textured_shader",
"shader/fullbright_textured.vsh",
"shader/fullbright_textured.fsh",
[BasicType.new(:vec4, :color),
BasicType.new(:mat4, :mat),
BasicType.new(:sampler2D, :tex)],
[BasicType.new(:vec3, :pos),
BasicType.new(:vec2, :uv)]),
Prog.new("fullbright_textured_text_shader",
"shader/fullbright_textured.vsh",
"shader/fullbright_textured_text.fsh",
[BasicType.new(:vec4, :color),
BasicType.new(:mat4, :mat),
BasicType.new(:sampler2D, :tex),
BasicType.new(:float, :lod_bias)],
[BasicType.new(:vec3, :pos),
BasicType.new(:vec2, :uv)]),
Prog.new("fullbright_textured_vert_color_shader",
"shader/fullbright_textured_vert_color.vsh",
"shader/fullbright_textured_vert_color.fsh",
[BasicType.new(:mat4, :mat),
BasicType.new(:sampler2D, :tex)],
[BasicType.new(:vec3, :pos),
BasicType.new(:vec2, :uv),
BasicType.new(:vec4, :color)]),
Prog.new("fullbright_textured_vert_color_text_shader",
"shader/fullbright_textured_vert_color.vsh",
"shader/fullbright_textured_vert_color_text.fsh",
[BasicType.new(:mat4, :mat),
BasicType.new(:sampler2D, :tex),
BasicType.new(:float, :lod_bias)],
[BasicType.new(:vec3, :pos),
BasicType.new(:vec2, :uv),
BasicType.new(:vec4, :color)]),
Prog.new("phong_textured_shader",
"shader/phong_textured.vsh",
"shader/phong_textured.fsh",
[BasicType.new(:vec4, :color),
BasicType.new(:mat4, :full_mat),
BasicType.new(:mat4, :world_mat),
BasicType.new(:mat4, :world_normal_mat),
BasicType.new(:sampler2D, :tex),
BasicType.new(:int, :num_lights),
ArrayType.new(:vec3, :light_world_pos, 8),
ArrayType.new(:vec3, :light_color, 8),
ArrayType.new(:float, :light_strength, 8)],
[BasicType.new(:vec3, :pos),
BasicType.new(:vec2, :uv),
BasicType.new(:vec3, :normal)])
]
progs.each do |prog|
prog.build
end