1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-01 02:48:44 +01:00
CanvasBlocker/test/webGL-Test.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-08-13 23:44:31 +02:00
(function(){
2017-10-03 15:35:31 +02:00
"use strict";
2019-11-28 01:26:35 +01:00
function getParameters(context){
const parameters = [];
2019-12-16 19:27:28 +01:00
for (let name in context){
2019-11-28 01:26:35 +01:00
if (name.toUpperCase() === name){
2019-12-16 19:27:28 +01:00
const value = context.getParameter(context[name]);
2019-11-28 01:26:35 +01:00
if (value !== null){
parameters.push({name: name, value: value});
}
}
}
const debugExtension = context.getExtension("WEBGL_debug_renderer_info");
2019-12-16 19:27:28 +01:00
for (let name in debugExtension){
2019-11-28 01:26:35 +01:00
if (name.toUpperCase() === name){
2019-12-16 19:27:28 +01:00
const value = context.getParameter(debugExtension[name]);
2019-11-28 01:26:35 +01:00
if (value !== null){
parameters.push({name: name, value: value});
}
}
}
2019-12-16 19:27:28 +01:00
const frontParameters = ["VENDOR", "RENDERER", "UNMASKED_VENDOR_WEBGL", "UNMASKED_RENDERER_WEBGL"];
2019-11-28 01:26:35 +01:00
parameters.sort(function(a, b){
2019-12-16 19:27:28 +01:00
const frontA = frontParameters.indexOf(a.name);
const frontB = frontParameters.indexOf(b.name);
2019-11-28 01:26:35 +01:00
if (frontA !== -1){
if (frontB !== -1){
return frontA - frontB;
}
else {
return -1;
}
}
else {
if (frontB !== -1){
return 1;
}
else {
return a.name < b.name? -1: 1;
}
}
});
return parameters;
}
2019-12-16 19:27:28 +01:00
["webgl", "webgl2"].forEach(async function(context, index){
const output = document.createElement("div");
document.getElementById("output").appendChild(output);
try {
2019-12-16 19:27:28 +01:00
const canvas = document.createElement("canvas");
canvas.width = 11;
canvas.height = 13;
2019-12-16 19:27:28 +01:00
const gl = canvas.getContext(context) || canvas.getContext("experimental-" + context);
// paint it completely black
gl.clearColor(index * 0.25, index * 0.25, index * 0.25, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
2019-12-16 19:27:28 +01:00
const pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
2019-12-16 19:27:28 +01:00
const values = {};
let max = 0;
for (let i = 0; i < pixels.length; i += 1){
values[pixels[i]] = (values[pixels[i]] || 0) + 1;
max = Math.max(max, values[pixels[i]]);
}
2019-09-19 00:41:20 +02:00
2019-11-28 01:26:35 +01:00
const parameters = getParameters(gl);
2019-09-19 00:41:20 +02:00
if (context === "webgl2"){
2019-12-16 19:27:28 +01:00
const parameterOutput = document.createElement("table");
2019-09-19 00:41:20 +02:00
document.getElementById("parameters").appendChild(parameterOutput);
parameters.forEach(function(parameter){
2019-12-16 19:27:28 +01:00
const parameterRow = document.createElement("tr");
2019-09-19 00:41:20 +02:00
parameterRow.innerHTML = "<td>" + parameter.name + "</td><td>" + parameter.value + "</td>";
parameterOutput.appendChild(parameterRow);
});
}
2019-12-16 19:27:28 +01:00
const hashBytes = await crypto.subtle.digest("SHA-256", new TextEncoder("utf-8")
.encode(parameters.map(function(parameter){
return parameter.name + ": " + parameter.value;
}).join(",")));
const chunks = [];
(new Uint32Array(hashBytes)).forEach(function(num){
chunks.push(num.toString(16));
});
const hash = chunks.map(function(chunk){
return "0".repeat(8 - chunk.length) + chunk;
}).join("");
output.textContent = context + ": " +
(max !== 3 * values[255]? "": "not ") + "supported " +
"(parameter hash: " + hash + ")";
output.title = JSON.stringify(values);
}
2019-11-28 01:26:35 +01:00
catch (error){
output.textContent = context + ": ERROR";
2019-11-28 01:26:35 +01:00
output.title = error;
}
});
2017-08-13 23:44:31 +02:00
}());