Skip to content

Commit 506c483

Browse files
committed
Merge branch 'master' of github.com:PythonJS/PythonJS
2 parents 07f1fbb + c184ba2 commit 506c483

9 files changed

Lines changed: 793 additions & 385 deletions

File tree

doc/gpu.md

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ micro language:
1010
. basic math ops and logic: if, elif, else, for i in range(n)
1111
. list of lists iteration with dynamic size
1212
. iterate over list of structs (dicts)
13+
. simple classes
1314

1415
. define GPU `main` function with input arguments and subroutines
1516
. `gpu.main` can take arguments typed as: int, float, and float*
@@ -235,15 +236,17 @@ list of dicts
235236
Use the for-iter loop to iterate over a list of dicts `for s in iter(A):`
236237
Regular JavaScript objects and Python dicts are uploaded to the shader as GLSL structs.
237238
The struct type name and GLSL code are generated at runtime based on the contents of
238-
each dict. A struct may contain: floats and array of floats attributes.
239+
each dict. A struct may contain: ints, floats and array of floats attributes.
240+
To use an integer value wrap it in a Int16Array with a single item,
241+
or call `int16(n)` which takes care of that for you.
239242

240243
```
241244
class myclass:
242245
243246
def new_struct(self, g):
244247
return {
245248
'attr1' : 0.6 + g,
246-
'attr2' : 0.4 + g
249+
'attr2' : int16(g)
247250
}
248251
249252
@@ -256,7 +259,7 @@ class myclass:
256259
struct* A = self.array
257260
float b = 0.0
258261
for s in iter(A):
259-
b += s.attr1 + s.attr2
262+
b += s.attr1 + float(s.attr2)
260263
return b
261264
262265
return gpufunc()
@@ -266,6 +269,7 @@ class myclass:
266269
external method calls
267270
---------------------
268271
Methods on external objects can be called within the shader function.
272+
This is useful for getting runtime data that is loop invariant.
269273

270274
```
271275
class myclass:
@@ -291,4 +295,82 @@ def main():
291295
m = myclass(10)
292296
r = m.run(64)
293297
298+
```
299+
300+
user defined classes
301+
-------
302+
A class decorated with `@gpu.object` will have its methods marked with `@gpu.method` translated into shader code. GPU methods must be defined in use order, a method is defined before it is used by another method. Recursive calls are not allowed.
303+
304+
305+
```
306+
@gpu.object
307+
class MyObject:
308+
@gpu.method
309+
float def subroutine(self, x,y):
310+
float x
311+
float y
312+
return x + y * self.attr2
313+
314+
@gpu.method
315+
float def mymethod(self, x,y):
316+
float x
317+
float y
318+
if self.index == 0:
319+
return -20.5
320+
elif self.index == 0:
321+
return 0.6
322+
else:
323+
return self.subroutine(x,y) * self.attr1
324+
325+
def __init__(self, a, b, i):
326+
self.attr1 = a
327+
self.attr2 = b
328+
self.index = int16(i)
329+
330+
331+
class myclass:
332+
def run(self, w):
333+
self.array = [ MyObject( 1.1, 1.2, x ) for x in range(w) ]
334+
335+
@returns( array=64 )
336+
@gpu.main
337+
def gpufunc():
338+
struct* A = self.array
339+
float b = 0.0
340+
341+
for s in iter(A):
342+
b += s.mymethod(1.1, 2.2)
343+
344+
return b
345+
346+
return gpufunc()
347+
348+
```
349+
350+
external type conversion
351+
------------------------
352+
@gpu.object classes can also contain sub-structures and GLSL types: `vec3`.
353+
To define a sub structure call the `gpu.object(class, name)` function.
354+
The example below types THREE.js Vector3 as a GLSL `vec` type.
355+
356+
```
357+
import three
358+
gpu.object( three.Vector3, 'vec3' )
359+
360+
```
361+
362+
Then when a `three.Vector3` is assigned to an attribute in the __init__ function of the @gpu.object
363+
class it will be inlined into the shader as a `vec3`.
364+
365+
```
366+
@gpu.object
367+
class MyObject:
368+
def __init__(self, x,y,z, a,b,c):
369+
self.vec1 = new( three.Vector3(x,y,z) )
370+
self.vec2 = new( three.Vector3(a,b,c) )
371+
372+
@gpu.method
373+
float def mymethod(self):
374+
return self.vec1.x + self.vec2.y
375+
294376
```

pythonjs/ast_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ def retrieve_vars(body):
6464
pass ## skips assignment to an attribute `a.x = y`
6565

6666
if user_typedef: ## `int x`
67-
if not isinstance(n.value, ast.Name):
67+
if isinstance(n.value, ast.Name):
68+
local_vars.add( '%s=%s' %(user_typedef, n.value.id))
69+
elif isinstance(n.value, ast.Num):
70+
local_vars.add( '%s=%s' %(user_typedef, n.value.n))
71+
else:
6872
raise SyntaxError(n.value)
69-
local_vars.add( '%s=%s' %(user_typedef, n.value.id))
73+
7074

7175
elif isinstance(n, ast.Global):
7276
global_vars.update( n.names )

0 commit comments

Comments
 (0)