detect-gpu
Version:
Classify GPU's based on their benchmark score in order to provide an adaptive experience.
3 lines (2 loc) • 9.29 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).DetectGPU={})}(this,(function(e){"use strict";var r=function(e,n){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])})(e,n)};function n(e,r,n,t){return new(n||(n=Promise))((function(o,a){function i(e){try{u(t.next(e))}catch(e){a(e)}}function c(e){try{u(t.throw(e))}catch(e){a(e)}}function u(e){var r;e.done?o(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r)}))).then(i,c)}u((t=t.apply(e,r||[])).next())}))}function t(e,r){var n,t,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,t&&(o=2&a[0]?t.return:a[0]?t.throw||((o=t.return)&&o.call(t),0):t.next)&&!(o=o.call(t,a[1])).done)return o;switch(t=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,t=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=r.call(e,i)}catch(e){a=[6,e],t=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}var o=["geforce 320m","geforce 8600","geforce 8600m gt","geforce 8800 gs","geforce 8800 gt","geforce 9400","geforce 9400m g","geforce 9400m","geforce 9600m gt","geforce 9600m","geforce fx go5200","geforce gt 120","geforce gt 130","geforce gt 330m","geforce gtx 285","google swiftshader","intel g41","intel g45","intel gma 4500mhd","intel gma x3100","intel hd 3000","intel q45","legacy","mali-2","mali-3","mali-4","quadro fx 1500","quadro fx 4","quadro fx 5","radeon hd 2400","radeon hd 2600","radeon hd 4670","radeon hd 4850","radeon hd 4870","radeon hd 5670","radeon hd 5750","radeon hd 6290","radeon hd 6300","radeon hd 6310","radeon hd 6320","radeon hd 6490m","radeon hd 6630m","radeon hd 6750m","radeon hd 6770m","radeon hd 6970m","sgx 543","sgx543"];function a(e){return e=e.toLowerCase().replace(/^angle ?\((.+)\)*$/,"$1").replace(/\s(\d{1,2}gb|direct3d.+$)|\(r\)| \([^)]+\)$/g,"")}const i=34962;var c="undefined"==typeof window,u=function(){if(!c){var e=window.navigator,r=e.userAgent,n=e.platform,t=e.maxTouchPoints,o=/(iphone|ipod|ipad)/i.test(r),a="iPad"===n||"MacIntel"===n&&t>0&&!window.MSStream;return{isIpad:a,isMobile:/android/i.test(r)||o||a,isSafari12:/Version\/12.+Safari/.test(r)}}}();function f(e,r,n){if(!n)return[r];var t,o=function(e){var r="\n precision highp float;\n attribute vec3 aPosition;\n varying float vvv;\n void main() {\n vvv = 0.31622776601683794;\n gl_Position = vec4(aPosition, 1.0);\n }\n ",n="\n precision highp float;\n varying float vvv;\n void main() {\n vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * vvv;\n enc = fract(enc);\n enc -= enc.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);\n gl_FragColor = enc;\n }\n ",t=e.createShader(35633),o=e.createShader(35632),a=e.createProgram();if(!(o&&t&&a))return;e.shaderSource(t,r),e.shaderSource(o,n),e.compileShader(t),e.compileShader(o),e.attachShader(a,t),e.attachShader(a,o),e.linkProgram(a),e.detachShader(a,t),e.detachShader(a,o),e.deleteShader(t),e.deleteShader(o),e.useProgram(a);var c=e.createBuffer();e.bindBuffer(i,c),e.bufferData(i,new Float32Array([-1,-1,0,3,-1,0,-1,3,0]),35044);var u=e.getAttribLocation(a,"aPosition");e.vertexAttribPointer(u,3,5126,!1,0,0),e.enableVertexAttribArray(u),e.clearColor(1,1,1,1),e.clear(16384),e.viewport(0,0,1,1),e.drawArrays(4,0,3);var f=new Uint8Array(4);return e.readPixels(0,0,1,1,6408,5121,f),e.deleteProgram(a),e.deleteBuffer(c),f.join("")}(e),a="801621810",c="8016218135",f="80162181161",l=(null==u?void 0:u.isIpad)?[["a7",f,12],["a8",c,15],["a8x",c,15],["a9",c,15],["a9x",c,15],["a10",c,15],["a10x",c,15],["a12",a,15],["a12x",a,15],["a12z",a,15],["a14",a,15],["m1",a,15]]:[["a7",f,12],["a8",c,12],["a9",c,15],["a10",c,15],["a11",a,15],["a12",a,15],["a13",a,15],["a14",a,15]];return"80162181255"===o?t=l.filter((function(e){return e[2]>=14})):(t=l.filter((function(e){return e[1]===o}))).length||(t=l),t.map((function(e){var r=e[0];return"apple ".concat(r," gpu")}))}var l=[],d=[];function s(e,r){if(e===r)return 0;var n=e;e.length>r.length&&(e=r,r=n);for(var t=e.length,o=r.length;t>0&&e.charCodeAt(~-t)===r.charCodeAt(~-o);)t--,o--;for(var a,i=0;i<t&&e.charCodeAt(i)===r.charCodeAt(i);)i++;if(o-=i,0===(t-=i))return o;for(var c,u,f=0,s=0,h=0;s<t;)d[s]=e.charCodeAt(i+s),l[s]=++s;for(;h<o;)for(a=r.charCodeAt(i+h),c=h++,f=h,s=0;s<t;s++)u=a===d[s]?c:c+1,c=l[s],f=l[s]=c>f?u>f?f+1:u:u>c?c+1:u;return f}function h(e){return null!=e}var v=function(e){function n(r){var n=this.constructor,t=e.call(this,r)||this;return Object.setPrototypeOf(t,n.prototype),t}return function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function t(){this.constructor=e}r(e,n),e.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)}(n,e),n}(Error);e.getGPUTier=function(e){var r=void 0===e?{}:e,i=r.mobileTiers,l=void 0===i?[0,15,30,60]:i,d=r.desktopTiers,p=void 0===d?[0,15,30,60]:d,g=r.override,m=void 0===g?{}:g,b=r.glContext,w=r.failIfMajorPerformanceCaveat,y=void 0!==w&&w,x=r.benchmarksURL,P=void 0===x?"https://unpkg.com/detect-gpu@".concat("4.0.14","/dist/benchmarks"):x;return n(void 0,void 0,void 0,(function(){var e,r,i,d,g,w,x,A,S,_,C,E,L,M,j,B,k,O,T,U,I,D,R,G,N,K;return t(this,(function(W){switch(W.label){case 0:if(e={},c)return[2,{tier:0,type:"SSR"}];if(r=m.isIpad,i=void 0===r?!!(null==u?void 0:u.isIpad):r,d=m.isMobile,g=void 0===d?!!(null==u?void 0:u.isMobile):d,w=m.screenSize,x=void 0===w?window.screen:w,A=m.loadBenchmarks,S=void 0===A?function(e){return n(void 0,void 0,void 0,(function(){var r;return t(this,(function(n){switch(n.label){case 0:return[4,fetch("".concat(P,"/").concat(e)).then((function(e){return e.json()}))];case 1:if(r=n.sent(),parseInt(r.shift().split(".")[0],10)<4)throw new v("Detect GPU benchmark data is out of date. Please update to version 4x");return[2,r]}}))}))}:A,_=m.renderer,C=function(e){for(var r=0,n=g?["adreno","apple","mali-t","mali","nvidia","powervr"]:["intel","apple","amd","radeon","nvidia","geforce"];r<n.length;r++){var t=n[r];if(e.includes(t))return t}},E=function(r){return n(void 0,void 0,void 0,(function(){var n,o,a,c,u,f,l,d,h,p,m,b,w,y,P,A,_,E,L,M,j,B,k,O,T,U;return t(this,(function(t){switch(t.label){case 0:if(!(n=C(r)))return[2];o="".concat(g?"m":"d","-").concat(n).concat(i?"-ipad":"",".json"),a=e[o]=null!==(U=e[o])&&void 0!==U?U:S(o),t.label=1;case 1:return t.trys.push([1,3,,4]),[4,a];case 2:return c=t.sent(),[3,4];case 3:if((u=t.sent())instanceof v)throw u;return[2];case 4:if(f=function(e){var r,n=(e=e.replace(/\([^)]+\)/,"")).match(/\d+/)||e.match(/(\W|^)([A-Za-z]{1,3})(\W|$)/g);return null!==(r=null==n?void 0:n.join("").replace(/\W|amd/g,""))&&void 0!==r?r:""}(r),(l=c.filter((function(e){return e[1]===f}))).length||(l=c.filter((function(e){return e[0].includes(r)}))),0===(d=l.length))return[2];for(h=d>1?l.map((function(e){return[e,s(r,e[0])]})).sort((function(e,r){return e[1]-r[1]}))[0][0]:l[0],p=h[0],m=h[3],b=Number.MAX_VALUE,y=window.devicePixelRatio,P=x.width*y*x.height*y,A=0,_=m;A<_.length;A++)E=_[A],L=E[0],M=E[1],j=L*M,(B=Math.abs(P-j))<b&&(b=B,w=E);return w?(O=(k=w)[2],T=k[3],[2,[b,O,p,T]]):[2]}}))}))},L=function(e,r,n,t,o){return{device:o,fps:t,gpu:n,isMobile:g,tier:e,type:r}},j="",_)_=a(_),M=[_];else{if(!(B=b||function(e,r){void 0===r&&(r=!1);var n={alpha:!1,antialias:!1,depth:!1,failIfMajorPerformanceCaveat:r,powerPreference:"high-performance",stencil:!1};e&&delete n.powerPreference;var t=window.document.createElement("canvas"),o=t.getContext("webgl",n)||t.getContext("experimental-webgl",n);return null!=o?o:void 0}(null==u?void 0:u.isSafari12,y)))return[2,L(0,"WEBGL_UNSUPPORTED")];if((k=B.getExtension("WEBGL_debug_renderer_info"))&&(_=B.getParameter(k.UNMASKED_RENDERER_WEBGL)),!_)return[2,L(1,"FALLBACK")];j=_,_=a(_),M=function(e,r,n){return"apple gpu"===r?f(e,r,n):[r]}(B,_,g)}return[4,Promise.all(M.map(E))];case 1:if(!(O=W.sent().filter(h).sort((function(e,r){var n=e[0],t=void 0===n?Number.MAX_VALUE:n,o=e[1],a=r[0],i=void 0===a?Number.MAX_VALUE:a,c=r[1];return t===i?o-c:t-i}))).length)return[2,(T=o.find((function(e){return _.includes(e)})))?L(0,"BLOCKLISTED",T):L(1,"FALLBACK","".concat(_," (").concat(j,")"))];if(U=O[0],I=U[1],D=U[2],R=U[3],-1===I)return[2,L(0,"BLOCKLISTED",D,I,R)];for(G=g?l:p,N=0,K=0;K<G.length;K++)I>=G[K]&&(N=K);return[2,L(N,"BENCHMARK",D,I,R)]}}))}))},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=detect-gpu.umd.js.map