forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.js
More file actions
180 lines (160 loc) · 5.61 KB
/
casting.js
File metadata and controls
180 lines (160 loc) · 5.61 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const { assert, skip, test, module: describe, only } = require('qunit');
const { GPU } = require('../../src');
describe('internal: casting');
function castingOffsetByThreadXAndOutputX(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function (value) {
// value will be a number
// this.thread.x is an integer
// this.output.x is treated as a literal, so can be either integer or float
// return value will be float
return this.thread.x + (this.output.x * value);
}, {
output: [1],
strictIntegers: true,
});
const result = kernel(1);
assert.equal(result[0], 1);
assert.deepEqual(kernel.argumentTypes, ['Integer']);
gpu.destroy();
}
(GPU.isWebGLSupported ? test : skip)('casting offset by this.thread.x and this.output.x webgl', () => {
castingOffsetByThreadXAndOutputX('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('casting offset by this.thread.x and this.output.x webgl2', () => {
castingOffsetByThreadXAndOutputX('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('casting offset by this.thread.x and this.output.x headlessgl', () => {
castingOffsetByThreadXAndOutputX('headlessgl');
});
function handleCastingIntsWithNativeFunctions(mode) {
const gpu = new GPU({ mode });
gpu.addNativeFunction('add', `
int add(int value1, int value2) {
return value1 + value2;
}
`);
const kernel = gpu.createKernel(function(value1, value2) {
return add(value1, value2);
}, { output: [1] });
const result = kernel(0.5, 2.5);
assert.deepEqual(Array.from(result), [2]);
assert.deepEqual(kernel.argumentTypes, ['Float', 'Float']);
gpu.destroy();
}
(GPU.isWebGLSupported ? test : skip)('handle casting ints with native functions webgl', () => {
handleCastingIntsWithNativeFunctions('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('handle casting ints with native functions webgl2', () => {
handleCastingIntsWithNativeFunctions('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('handle casting ints with native functions headlessgl', () => {
handleCastingIntsWithNativeFunctions('headlessgl');
});
function handleCastingFloatsWithNativeFunctions(mode) {
const gpu = new GPU({ mode });
gpu.addNativeFunction('add', `
float add(float value1, float value2) {
return value1 + value2;
}
`);
const kernel = gpu.createKernel(function(value1, value2) {
return add(value1, value2);
}, {
argumentTypes: ['Integer', 'Integer'],
output: [1],
strictIntegers: true,
});
const result = kernel(1, 2);
assert.deepEqual(Array.from(result), [3]);
assert.deepEqual(kernel.argumentTypes, ['Integer', 'Integer']);
gpu.destroy();
}
(GPU.isWebGLSupported ? test : skip)('handle casting floats with native functions webgl', () => {
handleCastingFloatsWithNativeFunctions('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('handle casting floats with native functions webgl2', () => {
handleCastingFloatsWithNativeFunctions('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('handle casting floats with native functions headlessgl', () => {
handleCastingFloatsWithNativeFunctions('headlessgl');
});
function handleCastingMixedWithNativeFunctions(mode) {
const gpu = new GPU({ mode });
gpu.addNativeFunction('add', `
float add(float value1, int value2) {
return value1 + float(value2);
}
`);
const kernel = gpu.createKernel(function(value1, value2) {
return add(value1, value2);
}, {
output: [1],
strictIntegers: true,
});
const result = kernel(1, 2.5);
assert.deepEqual(Array.from(result), [3]);
assert.deepEqual(kernel.argumentTypes, ['Integer', 'Float']);
gpu.destroy();
}
(GPU.isWebGLSupported ? test : skip)('handle casting mixed with native functions webgl', () => {
handleCastingMixedWithNativeFunctions('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('handle casting mixed with native functions webgl2', () => {
handleCastingMixedWithNativeFunctions('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('handle casting mixed with native functions headlessgl', () => {
handleCastingMixedWithNativeFunctions('headlessgl');
});
function handleCastingFloat(mode) {
const gpu = new GPU({ mode });
function add(value1, value2) {
return value1 + value2;
}
gpu.addFunction(add, {
argumentTypes: ['Float', 'Float'],
returnType: 'Float'
});
const kernel = gpu.createKernel(function(value1, value2) {
return add(value1, value2);
}, {
output: [1],
argumentTypes: ['Integer', 'Integer'],
});
const result = kernel(1, 2);
assert.equal(result[0], 3);
gpu.destroy();
}
(GPU.isWebGLSupported ? test : skip)('handle casting float webgl', () => {
handleCastingFloat('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('handle casting float webgl2', () => {
handleCastingFloat('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('handle casting float headlessgl', () => {
handleCastingFloat('headlessgl');
});
function handleCastingBeforeReturn(mode) {
const gpu = new GPU({ mode });
function addOne(v) {
return v + v;
}
gpu.addFunction(addOne, {
argumentTypes: { v: 'Float' },
returnType: 'Integer',
});
const kernel = gpu.createKernel(function(v) {
return addOne(v);
}, { output: [1] });
assert.equal(kernel(1)[0], 2);
gpu.destroy();
}
(GPU.isWebGLSupported ? test : skip)('handle casting before return webgl', () => {
handleCastingBeforeReturn('webgl');
});
(GPU.isWebGL2Supported ? test : skip)('handle casting before return webgl2', () => {
handleCastingBeforeReturn('webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('handle casting before return headlessgl', () => {
handleCastingBeforeReturn('headlessgl');
});