无论如何检测一个 "Rats, WebGL hit a Snag?"

Is there anyway to detect a "Rats, WebGL hit a Snag?"

我们知道我们的 WebGL 应用程序中出现了 "Rats" 错误。我们不完全知道为什么。我们确实要求他们报告他们拥有的硬件配置。我们的 javascript 日志系统似乎没有记录相应的错误。因此,我们的假设是 WebGL 错误主要被控制台抑制。因此,我们无法检测到 "Rats" 类型错误。

我们有一个针对非 webgl 支持的浏览器的回退策略,但要在 "Rats" 情况下激活它,我们需要知道发生了 "Rats"。我们该怎么做?

您应该能够通过检查 webglcontextlost 事件

来检测它
canvas.addEventListener("webglcontextlost", reportRats);

如果您想尝试处理自动恢复,您也可以这样做

canvas.addEventListener("webglcontextlost", function(e) {
   // prevent the default (the default is don't recover)
   e.preventDefault(); 
}); 

要处理恢复,如果浏览器决定恢复 WebGL 上下文,您需要检查 webglcontextrestored 事件

canvas.addEventListener("webglcontextrestored", function(e) {
   // recreate all WebGL resources
}); 

至于报告,至少在 Chrome 中,您还可以使用 the WEBGL_debug_renderer_info extension 检查 GPU/Driver。显然 Google 地图使用它来不在支持 WebGL 的某些旧 GPUs/Drivers 上使用 WebGL,但对于 Google 地图在他们的分析中显示出来显然太慢了。