forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestroy.js
More file actions
185 lines (170 loc) · 4.63 KB
/
destroy.js
File metadata and controls
185 lines (170 loc) · 4.63 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
181
182
183
184
185
const { assert, skip, test, module: describe } = require('qunit');
const { GPU, WebGLKernel } = require('../../src');
const sinon = require('sinon');
describe('features: destroy');
function testWithoutDestroyContext(done, mode) {
const gpu = new GPU({ mode });
const destroyKernel = sinon.spy();
gpu.kernels.push({
kernel: {
constructor: {
destroyContext: null
}
},
destroy: destroyKernel
});
gpu.destroy();
gpu.destroy()
.then(() => {
assert.equal(destroyKernel.callCount, 2);
assert.ok(true);
done();
});
}
test('without destroy context', (t) => {
const done = t.async();
testWithoutDestroyContext(done);
});
function testWithDestroyContext(done, mode) {
const gpu = new GPU({ mode });
const destroyKernel = sinon.spy();
const destroyContextSpy = sinon.spy();
gpu.kernels.push({
kernel: {
constructor: {
destroyContext: destroyContextSpy
}
},
destroy: destroyKernel
});
gpu.destroy();
gpu.destroy()
.then(() => {
assert.equal(destroyKernel.callCount, 2);
assert.equal(destroyContextSpy.callCount, 2);
assert.ok(true);
done();
});
}
test('with destroy context', (t) => {
const done = t.async();
testWithDestroyContext(done);
});
function testTexturesAreDestroyed(done, mode) {
const mockTexture1 = {};
const mockTexture2 = {};
const mockTexture3 = {};
const deleteTextureMock = sinon.spy();
const mockContext = {
deleteTexture: deleteTextureMock,
};
const mockKernelInstance = {
textureCache: [mockTexture1, mockTexture2, mockTexture3],
context: mockContext,
destroyExtensions: () => {},
};
mockKernelInstance.destroy = WebGLKernel.prototype.destroy.bind(mockKernelInstance);
GPU.prototype.destroy.call({ kernels: [mockKernelInstance] })
.then(() => {
assert.equal(deleteTextureMock.callCount, 3);
assert.ok(true);
done();
});
}
test('textures are destroyed', (t) => {
const done = t.async();
testTexturesAreDestroyed(done);
});
function testKernelTextureIsDeleted(done) {
const webGLTexture = {};
const mockTextureDelete = sinon.spy();
const kernelTexture = {
texture: webGLTexture,
delete: mockTextureDelete,
};
const mockContext = {};
const mockKernelInstance = {
texture: kernelTexture,
textureCache: [],
context: mockContext,
destroyExtensions: () => {},
};
mockKernelInstance.destroy = WebGLKernel.prototype.destroy.bind(mockKernelInstance);
GPU.prototype.destroy.call({ kernels: [mockKernelInstance] })
.then(() => {
assert.equal(mockTextureDelete.callCount, 1);
assert.ok(true);
done();
});
}
test('kernel.texture is deleted', (t) => {
const done = t.async();
testKernelTextureIsDeleted(done);
});
function testKernelMappedTexturesAreDeleted(done) {
const mockGPU = {
kernels: []
};
const webGLTexture1 = {};
const mockTextureDelete1 = sinon.spy();
const kernelTexture1 = {
texture: webGLTexture1,
delete: mockTextureDelete1,
};
const webGLTexture2 = {};
const mockTextureDelete2 = sinon.spy();
const kernelTexture2 = {
texture: webGLTexture2,
delete: mockTextureDelete2,
};
const mockContext = {};
const mockKernelInstance = {
gpu: mockGPU,
mappedTextures: [kernelTexture1, kernelTexture2],
textureCache: [],
context: mockContext,
destroyExtensions: () => {},
};
mockGPU.kernels.push(mockKernelInstance);
mockKernelInstance.destroy = WebGLKernel.prototype.destroy.bind(mockKernelInstance);
GPU.prototype.destroy.call({ kernels: [mockKernelInstance] })
.then(() => {
assert.equal(mockTextureDelete1.callCount, 1);
assert.equal(mockTextureDelete2.callCount, 1);
assert.equal(mockGPU.kernels.length, 0);
assert.ok(true);
done();
})
.catch((e) => {
console.error(e);
});
}
test('kernel.mappedTextures are deleted', (t) => {
const done = t.async();
testKernelMappedTexturesAreDeleted(done);
});
test('gpu.kernels is populated and removed by kernel', () => {
const gpu = new GPU();
const kernel = gpu.createKernel(function() {
return 1;
});
assert.equal(gpu.kernels.length, 1);
assert.equal(gpu.kernels.indexOf(kernel.kernel), 0);
kernel.destroy();
assert.equal(gpu.kernels.length, 0);
gpu.destroy();
});
test('gpu.kernels is populated and removed by gpu', t => {
const done = t.async();
const gpu = new GPU();
const kernel = gpu.createKernel(function() {
return 1;
});
assert.equal(gpu.kernels.length, 1);
assert.equal(gpu.kernels.indexOf(kernel.kernel), 0);
gpu.destroy()
.then(() => {
assert.equal(gpu.kernels.length, 0);
done();
});
});