-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertexArray.t
71 lines (62 loc) · 1.96 KB
/
VertexArray.t
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
require "lib/gl"
local std = require "util/std"
local struct VertexArray(std.Object) {
vao: GL.uint
}
terra VertexArray.methods.create()
return VertexArray {
vao = gl.CreateVertexArray()
}
end
terra VertexArray:__destruct()
gl.DeleteVertexArray(self.vao)
end
terra VertexArray:rawget()
return self.vao
end
terra VertexArray:use()
gl.BindVertexArray(self.vao)
end
local terraTypeToGL = {
[float] = gl.FLOAT;
[double] = gl.DOUBLE;
[int8] = gl.BYTE;
[int16] = gl.SHORT;
[int] = gl.INT;
[uint8] = gl.UNSIGNED_BYTE;
[uint16] = gl.UNSIGNED_SHORT;
[uint] = gl.UNSIGNED_INT;
}
local initFormatM = macro(function(vao, T, prog)
T = T:astype()
local output = terralib.newlist()
for i,entry in ipairs(T:getentries()) do
local type = entry.type
local n = 1
if type.T and type.N then
n = type.N
type = type.T
end
local glType = terraTypeToGL[type]
output:insert(quote
var attribIdx = gl.GetAttribLocation(prog, [entry.field])
if attribIdx ~= -1 then
gl.VertexArrayAttribFormat(vao, attribIdx, n, glType, gl.FALSE, [terralib.offsetof(T, entry.field)])
gl.VertexArrayAttribBinding(vao, attribIdx, 0)
gl.EnableVertexArrayAttrib(vao, attribIdx)
end
end)
end
return output
end)
local initFormat = function(T)
return terra(vao: GL.uint, prog: GL.uint)
initFormatM(vao, T, prog)
end
end
initFormat = terralib.memoize(initFormat)
VertexArray.methods.formatVertexAttribs = macro(function(self,T, prog)
T = T:astype()
return `[initFormat(T)](self.vao, prog)
end)
return VertexArray