-
Notifications
You must be signed in to change notification settings - Fork 662
/
advanced-typescript.ts
66 lines (54 loc) · 1.53 KB
/
advanced-typescript.ts
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
/**
* This is an arbitrary example (overly complex with types for overly simplified kernel) to show type inheritance
* throughout the kernel's usage.
*
* The whole idea here is that you can define custom:
* - `constants`
* - `this` context
* - mapped kernels
* - arguments
* - kernel output
*/
import {
GPU,
Texture,
IKernelFunctionThis,
IConstantsThis,
KernelOutput,
ISubKernelsResults
} from '../src';
const gpu = new GPU();
interface IConstants extends IConstantsThis {
rotation: number,
}
interface IThis extends IKernelFunctionThis {
constants: IConstants,
}
function kernelFunction(this: IThis, degrees: number, divisors: [number, number]): [number, number] {
const bounds = subKernel(this.constants.rotation * degrees);
return [bounds[0] / divisors[0], bounds[1] / divisors[1]];
}
function subKernel(value: number): [number, number] {
return [-value, value];
}
interface IKernelMapResult extends ISubKernelsResults {
test: KernelOutput;
}
const kernelMap = gpu.createKernelMap<Parameters<typeof kernelFunction>>({
test: subKernel,
}, kernelFunction)
.setConstants<IConstants>({
rotation: 45,
})
.setOutput([1])
.setPrecision('single')
.setPipeline(true)
.setImmutable(true);
const { test, result } = kernelMap(360, [256, 512]);
const testTexture = test as Texture;
const resultTexture = result as Texture;
console.log(testTexture.toArray() as [number, number][]);
console.log(resultTexture.toArray() as [number, number][]);
testTexture.delete();
resultTexture.delete();
kernelMap.destroy();