-
Notifications
You must be signed in to change notification settings - Fork 5
/
GpuProgram.py
93 lines (63 loc) · 1.87 KB
/
GpuProgram.py
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
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
from GpuShader import *
from ZMath import *
from Utility import *
class Program:
def __init__(self):
self.program = glCreateProgram()
self.uniforms = {}
self.attributes = {}
def attachShader(self,shader):
glAttachShader(self.program,shader.shader)
def detachShader(self,shader):
glDetachShader(self.program,shader.shader)
def link(self):
glLinkProgram(self.program)
def use(self):
glUseProgram(self.program)
def unuse(self):
glUseProgram(0)
def check(self):
print(glGetProgramInfoLog(self.program))
def getUniformLocation(self,uniform):
res = glGetUniformLocation(self.program,uniform)
self.uniforms[uniform] = res
return res
def getAttributeLocation(self,attr):
res = glGetAttribLocation(self.program,attr)
self.attributes[attr] = res
return res
def updateUniform(self,name,val):
if name not in self.uniforms:
self.getUniformLocation(name)
single = 1
if not isnumber(val):
single = len(val)
typ = 'f'
if isinstance(val, int):
typ = 'i'
globals()['glUniform%d%s'%(single,typ)](self.uniforms[name],val)
def updateUniformArray(self,name,val):
if name not in self.uniforms:
self.getUniformLocation(name)
count = len(val)
single = len(val[0])
globals()['glUniform%dfv'%single](self.uniforms[name],count,val)
def updateUniformMatrix(self,name,mats,transpose = 0):
if name not in self.uniforms:
self.getUniformLocation(name)
if isinstance(mats,np.ndarray):
count = len(mats)//16
single = 4
elif isinstance(mats, list):
count = len(mats)
single = int(sqrt(len(mats[0])))
tmp = np.array(range(len(mats)*16),dtype='f')
for i in range(len(mats)):
for r in range(4):
for c in range(4):
tmp[i*16+r*4+c] = mats[i][r][c]
mats = tmp
globals()['glUniformMatrix%dfv'%single](self.uniforms[name],count,transpose,mats)