forked from BabylonJS/BabylonNative
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_native.js
More file actions
419 lines (362 loc) · 16.6 KB
/
validation_native.js
File metadata and controls
419 lines (362 loc) · 16.6 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
(function () {
let currentScene;
let config;
const justOnce = false;
const saveResult = true;
const testWidth = 600;
const testHeight = 400;
const generateReferences = false;
const engine = new BABYLON.NativeEngine();
engine.getCaps().parallelShaderCompile = undefined;
engine.getRenderingCanvas = function () {
return window;
}
engine.getInputElement = function () {
return 0;
}
const canvas = window;
// Random replacement
let seed = 1;
Math.random = function () {
const x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
function compare(test, renderData, referenceImage, threshold, errorRatio) {
const referenceData = TestUtils.getImageData(referenceImage);
if (referenceData.length != renderData.length) {
throw new Error(`Reference data length (${referenceData.length}) must match render data length (${renderData.length})`);
}
const size = renderData.length;
let differencesCount = 0;
for (let index = 0; index < size; index += 4) {
if (Math.abs(renderData[index] - referenceData[index]) < threshold &&
Math.abs(renderData[index + 1] - referenceData[index + 1]) < threshold &&
Math.abs(renderData[index + 2] - referenceData[index + 2]) < threshold) {
continue;
}
if (differencesCount === 0) {
console.log(`First pixel off at ${index}: Value: (${renderData[index]}, ${renderData[index + 1]}, ${renderData[index] + 2}) - Expected: (${referenceData[index]}, ${referenceData[index + 1]}, ${referenceData[index + 2]}) `);
}
referenceData[index] = 255;
referenceData[index + 1] *= 0.5;
referenceData[index + 2] *= 0.5;
differencesCount++;
}
if (differencesCount) {
console.log("Pixel difference: " + differencesCount + " pixels.");
} else {
console.log("No pixel difference!");
}
const error = (differencesCount * 100) / (size / 4) > errorRatio;
const width = testWidth / engine.getHardwareScalingLevel();
const height = testHeight / engine.getHardwareScalingLevel();
if (error) {
TestUtils.writePNG(referenceData, width, height, TestUtils.getOutputDirectory() + "/Errors/" + test.referenceImage);
}
if (saveResult || error) {
TestUtils.writePNG(renderData, width, height, TestUtils.getOutputDirectory() + "/Results/" + test.referenceImage);
}
return error;
}
function saveRenderedResult(test, renderData) {
const width = testWidth / engine.getHardwareScalingLevel();
const height = testHeight / engine.getHardwareScalingLevel();
TestUtils.writePNG(renderData, width, height, TestUtils.getOutputDirectory() + "/Results/" + test.referenceImage);
return false; // no error
}
function evaluate(test, referenceImage, done, compareFunction) {
TestUtils.getFrameBufferData(function (screenshot) {
let testRes = true;
if (!test.onlyVisual) {
const defaultErrorRatio = 2.5;
if (compareFunction(test, screenshot, referenceImage, test.threshold || 25, test.errorRatio || defaultErrorRatio)) {
testRes = false;
console.log('failed');
} else {
testRes = true;
console.log('validated');
}
}
currentScene.dispose();
currentScene = null;
engine.setHardwareScalingLevel(1);
// This is necessary because of https://github.com/BabylonJS/Babylon.js/pull/15217 so that each test starts fresh.
engine.releaseEffects();
done(testRes);
});
}
function processCurrentScene(test, renderImage, done, compareFunction) {
currentScene.useConstantAnimationDeltaTime = true;
let renderCount = test.renderCount || 1;
currentScene.executeWhenReady(function () {
if (currentScene.activeCamera && currentScene.activeCamera.useAutoRotationBehavior) {
currentScene.activeCamera.useAutoRotationBehavior = false;
}
engine.runRenderLoop(function () {
try {
if (test.capture && renderCount === 1 && TestUtils.captureNextFrame) {
TestUtils.captureNextFrame();
}
currentScene.render();
renderCount--;
if (renderCount === 0) {
engine.stopRenderLoop();
evaluate(test, renderImage, done, compareFunction);
}
}
catch (e) {
console.error(e);
done(false);
}
});
}, true);
}
function loadPlayground(test, done, referenceImage, compareFunction) {
if (test.sceneFolder) {
BABYLON.SceneLoader.Load(config.root + test.sceneFolder, test.sceneFilename, engine, function (newScene) {
currentScene = newScene;
processCurrentScene(test, referenceImage, done, compareFunction);
},
null,
function (loadedScene, msg) {
console.error(msg);
done(false);
});
}
else if (test.playgroundId) {
if (test.playgroundId[0] !== "#" || test.playgroundId.indexOf("#", 1) === -1) {
test.playgroundId += "#0";
}
const snippetUrl = "https://snippet.babylonjs.com";
const pgRoot = "https://playground.babylonjs.com";
const initialRetryTime = 500;
const maxRetry = 8;
let retry = 0;
const onError = function () {
retry++;
if (retry < maxRetry) {
// Exponential backoff capped at 30s. The snippet service can be briefly
// unavailable (5xx, 429, gateway timeout); 8 attempts over ~60s gives the
// upstream room to recover before we fail the whole run.
const delay = Math.min(initialRetryTime * Math.pow(2, retry - 1), 30000);
setTimeout(function () {
loadPG();
}, delay);
}
else {
// Fail the test, something wrong happen
console.log("Running the playground failed after " + maxRetry + " attempts.");
done(false);
}
}
const loadPG = function () {
const xmlHttp = new XMLHttpRequest();
xmlHttp.addEventListener("readystatechange", function () {
if (xmlHttp.readyState === 4) {
// Treat any non-200 (5xx, 429, empty body, etc.) as retryable rather
// than feeding the response body to JSON.parse and surfacing it as a
// SyntaxError.
if (xmlHttp.status !== 200) {
console.error("Snippet fetch returned status " + xmlHttp.status + " for " + test.playgroundId);
onError();
return;
}
try {
const snippet = JSON.parse(xmlHttp.responseText);
let code = JSON.parse(snippet.jsonPayload).code.toString();
// Check if this is a v2 manifest and extract the entry file's code
// TODO: Handle multi-file playgrounds
try {
const manifestPayload = JSON.parse(code);
if (manifestPayload.v === 2) {
code = manifestPayload.files[manifestPayload.entry]
.replace(/export +default +/g, "")
.replace(/export +/g, "");
}
} catch (e) {
// Not a manifest, proceed as usual
}
code = code
.replace(/"\/textures\//g, '"' + pgRoot + "/textures/")
.replace(/"textures\//g, '"' + pgRoot + "/textures/")
.replace(/\/scenes\//g, pgRoot + "/scenes/")
.replace(/"scenes\//g, '"' + pgRoot + "/scenes/")
.replace(/"\.\.\/\.\.https/g, '"' + "https")
.replace("http://", "https://");
if (test.replace) {
const split = test.replace.split(",");
for (let i = 0; i < split.length; i += 2) {
const source = split[i].trim();
const destination = split[i + 1].trim();
code = code.replace(source, destination);
}
}
currentScene = eval(code + "\r\ncreateScene(engine)");
if (currentScene.then) {
// Handle if createScene returns a promise
currentScene.then(function (scene) {
currentScene = scene;
processCurrentScene(test, referenceImage, done, compareFunction);
}).catch(function (e) {
console.error(e);
onError();
})
} else {
// Handle if createScene returns a scene
processCurrentScene(test, referenceImage, done, compareFunction);
}
}
catch (e) {
console.error(e);
onError();
}
}
}, false);
xmlHttp.onerror = function () {
console.error("Network error during test load.");
onError();
}
xmlHttp.open("GET", snippetUrl + test.playgroundId.replace(/#/g, "/"));
xmlHttp.send();
}
loadPG();
} else {
// Fix references
if (test.specificRoot) {
BABYLON.Tools.BaseUrl = config.root + test.specificRoot;
}
const request = new XMLHttpRequest();
request.open('GET', config.root + test.scriptToRun, true);
request.onreadystatechange = function () {
if (request.readyState === 4) {
try {
request.onreadystatechange = null;
const scriptToRun = request.responseText.replace(/..\/..\/assets\//g, config.root + "/Assets/");
scriptToRun = scriptToRun.replace(/..\/..\/Assets\//g, config.root + "/Assets/");
scriptToRun = scriptToRun.replace(/\/assets\//g, config.root + "/Assets/");
if (test.replace) {
const split = test.replace.split(",");
for (let i = 0; i < split.length; i += 2) {
const source = split[i].trim();
const destination = split[i + 1].trim();
scriptToRun = scriptToRun.replace(source, destination);
}
}
if (test.replaceUrl) {
const split = test.replaceUrl.split(",");
for (let i = 0; i < split.length; i++) {
const source = split[i].trim();
const regex = new RegExp(source, "g");
scriptToRun = scriptToRun.replace(regex, config.root + test.rootPath + source);
}
}
currentScene = eval(scriptToRun + test.functionToCall + "(engine)");
processCurrentScene(test, renderImage, done, compareFunction);
}
catch (e) {
console.error(e);
done(false);
}
}
};
request.onerror = function () {
console.error("Network error during test load.");
done(false);
}
request.send(null);
}
}
function runTest(index, done) {
if (index >= config.tests.length) {
done(false);
}
const test = config.tests[index];
if (test.onlyVisual || test.excludeFromAutomaticTesting) {
done(true);
return;
}
if (test.excludedGraphicsApis && test.excludedGraphicsApis.includes(TestUtils.getGraphicsApiName())) {
done(true);
return;
}
const testInfo = "Running " + test.title;
console.log(testInfo);
TestUtils.setTitle(testInfo);
seed = 1;
if (generateReferences) {
loadPlayground(test, done, undefined, saveRenderedResult);
} else {
const onLoadFileError = function (request, exception) {
console.error("Failed to retrieve " + url + ".", exception);
done(false);
};
const onload = function (data, responseURL) {
if (typeof (data) === "string") {
throw new Error("Decode Image from string data not yet implemented.");
}
const referenceImage = TestUtils.decodeImage(data);
loadPlayground(test, done, referenceImage, compare);
};
// run test and image comparison
const url = "app:///ReferenceImages/" + test.referenceImage;
BABYLON.Tools.LoadFile(url, onload, undefined, undefined, /*useArrayBuffer*/true, onLoadFileError);
}
}
OffscreenCanvas = function (width, height) {
return {
width: width
, height: height
, getContext: function (type) {
return {
fillRect: function (x, y, w, h) { }
, measureText: function (text) { return 8; }
, fillText: function (text, x, y) { }
};
}
};
}
document = {
createElement: function (type) {
if (type === "canvas") {
return new OffscreenCanvas(64, 64);
}
return {};
},
removeEventListener: function () { }
}
const xhr = new XMLHttpRequest();
xhr.open("GET", "app:///Scripts/config.json", true);
xhr.addEventListener("readystatechange", function () {
if (xhr.status === 200) {
config = JSON.parse(xhr.responseText);
// Run tests
const recursiveRunTest = function (i) {
runTest(i, function (status) {
if (!status) {
TestUtils.exit(-1);
return;
}
i++;
if (justOnce || i >= config.tests.length) {
engine.dispose();
TestUtils.exit(0);
return;
}
// Defer the next iteration via setTimeout to avoid
// blowing Chakra's recursion stack on long test lists.
setTimeout(function () { recursiveRunTest(i); }, 0);
});
}
recursiveRunTest(0);
}
}, false);
BABYLON.Tools.LoadFile("https://raw.githubusercontent.com/CedricGuillemet/dump/master/droidsans.ttf", (data) => {
_native.Canvas.loadTTFAsync("droidsans", data).then(function () {
_native.RootUrl = "https://playground.babylonjs.com";
console.log("Starting");
TestUtils.setTitle("Starting Native Validation Tests");
TestUtils.updateSize(testWidth, testHeight);
xhr.send();
});
}, undefined, undefined, true);
})();