You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/gpu.md
+85-3Lines changed: 85 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ micro language:
10
10
. basic math ops and logic: if, elif, else, for i in range(n)
11
11
. list of lists iteration with dynamic size
12
12
. iterate over list of structs (dicts)
13
+
. simple classes
13
14
14
15
. define GPU `main` function with input arguments and subroutines
15
16
. `gpu.main` can take arguments typed as: int, float, and float*
@@ -235,15 +236,17 @@ list of dicts
235
236
Use the for-iter loop to iterate over a list of dicts `for s in iter(A):`
236
237
Regular JavaScript objects and Python dicts are uploaded to the shader as GLSL structs.
237
238
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.
239
242
240
243
```
241
244
class myclass:
242
245
243
246
def new_struct(self, g):
244
247
return {
245
248
'attr1' : 0.6 + g,
246
-
'attr2' : 0.4 + g
249
+
'attr2' : int16(g)
247
250
}
248
251
249
252
@@ -256,7 +259,7 @@ class myclass:
256
259
struct* A = self.array
257
260
float b = 0.0
258
261
for s in iter(A):
259
-
b += s.attr1 + s.attr2
262
+
b += s.attr1 + float(s.attr2)
260
263
return b
261
264
262
265
return gpufunc()
@@ -266,6 +269,7 @@ class myclass:
266
269
external method calls
267
270
---------------------
268
271
Methods on external objects can be called within the shader function.
272
+
This is useful for getting runtime data that is loop invariant.
269
273
270
274
```
271
275
class myclass:
@@ -291,4 +295,82 @@ def main():
291
295
m = myclass(10)
292
296
r = m.run(64)
293
297
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`.
0 commit comments