forked from PythonJS/PythonJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_class.py
More file actions
55 lines (42 loc) · 942 Bytes
/
Copy pathgpu_class.py
File metadata and controls
55 lines (42 loc) · 942 Bytes
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
'''@gpu class'''
@gpu.object
class MyObject:
## below `self` is not `this` in javascript
## `self` is a GLSL struct of MyObject
@gpu.method
float def subroutine(self, x,y):
float x
float y
return x + y * self.attr2
## subroutines must be defined ahead of where they are used
@gpu.method
float def mymethod(self, x,y):
float x
float y
if self.index == 0:
return -20.5
elif self.index == 0:
return 0.6
else:
return self.subroutine(x,y) * self.attr1
## here `self` is javascript's `this`
def __init__(self, a, b, i):
self.attr1 = a
self.attr2 = b
self.index = int16(i)
class myclass:
def run(self, w):
self.array = [ MyObject( 1.1, 1.2, x ) for x in range(w) ]
@returns( array=64 )
@gpu.main
def gpufunc():
struct* A = self.array
float b = 0.0
for s in iter(A):
b += s.mymethod(1.1, 2.2)
return b
return gpufunc()
def main():
m = myclass()
r = m.run(8)
print(r)