mirror of
https://github.com/gpujs/gpu.js.git
synced 2026-01-18 16:04:10 +00:00
239 lines
6.8 KiB
JavaScript
239 lines
6.8 KiB
JavaScript
/**
|
|
* gpu.js
|
|
* http://gpu.rocks/
|
|
*
|
|
* GPU Accelerated JavaScript
|
|
*
|
|
* @version 1.4.13
|
|
* @date Wed Jul 11 2018 18:51:55 GMT-0400 (EDT)
|
|
*
|
|
* @license MIT
|
|
* The MIT License
|
|
*
|
|
* Copyright (c) 2018 gpu.js Team
|
|
*/
|
|
"use strict";(function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
|
|
'use strict';
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var UtilsCore = require("./utils-core");
|
|
|
|
module.exports = function () {
|
|
function GPUCore() {
|
|
_classCallCheck(this, GPUCore);
|
|
}
|
|
|
|
_createClass(GPUCore, null, [{
|
|
key: "validateKernelObj",
|
|
|
|
|
|
value: function validateKernelObj(kernelObj) {
|
|
|
|
if (kernelObj === null) {
|
|
throw "KernelObj being validated is NULL";
|
|
}
|
|
|
|
if (typeof kernelObj === "string") {
|
|
try {
|
|
kernelObj = JSON.parse(kernelObj);
|
|
} catch (e) {
|
|
console.error(e);
|
|
throw "Failed to convert KernelObj from JSON string";
|
|
}
|
|
|
|
if (kernelObj === null) {
|
|
throw "Invalid (NULL) KernelObj JSON string representation";
|
|
}
|
|
}
|
|
|
|
if (kernelObj.isKernelObj !== true) {
|
|
throw "Failed missing isKernelObj flag check";
|
|
}
|
|
|
|
return kernelObj;
|
|
}
|
|
|
|
|
|
}, {
|
|
key: "loadKernelObj",
|
|
value: function loadKernelObj(kernelObj, inOpt) {
|
|
|
|
kernelObj = validateKernelObj(kernelObj);
|
|
}
|
|
}]);
|
|
|
|
return GPUCore;
|
|
}();
|
|
},{"./utils-core":2}],2:[function(require,module,exports){
|
|
'use strict';
|
|
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var UtilsCore = function () {
|
|
function UtilsCore() {
|
|
_classCallCheck(this, UtilsCore);
|
|
}
|
|
|
|
_createClass(UtilsCore, null, [{
|
|
key: 'isCanvas',
|
|
|
|
|
|
|
|
|
|
|
|
value: function isCanvas(canvasObj) {
|
|
return canvasObj !== null && canvasObj.nodeName && canvasObj.getContext && canvasObj.nodeName.toUpperCase() === 'CANVAS';
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'isCanvasSupported',
|
|
value: function isCanvasSupported() {
|
|
return _isCanvasSupported;
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'initCanvas',
|
|
value: function initCanvas() {
|
|
if (!_isCanvasSupported) {
|
|
return null;
|
|
}
|
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.width = 2;
|
|
canvas.height = 2;
|
|
|
|
return canvas;
|
|
}
|
|
|
|
|
|
|
|
|
|
}, {
|
|
key: 'isWebGl',
|
|
value: function isWebGl(webGlObj) {
|
|
return webGlObj && typeof webGlObj.getExtension === 'function';
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'isWebGlSupported',
|
|
value: function isWebGlSupported() {
|
|
return _isWebGlSupported;
|
|
}
|
|
}, {
|
|
key: 'isWebGlDrawBuffersSupported',
|
|
value: function isWebGlDrawBuffersSupported() {
|
|
return _isWebGlDrawBuffersSupported;
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'initWebGlDefaultOptions',
|
|
value: function initWebGlDefaultOptions() {
|
|
return {
|
|
alpha: false,
|
|
depth: false,
|
|
antialias: false
|
|
};
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'initWebGl',
|
|
value: function initWebGl(canvasObj) {
|
|
|
|
if (typeof _isCanvasSupported !== 'undefined' || canvasObj === null) {
|
|
if (!_isCanvasSupported) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if (!UtilsCore.isCanvas(canvasObj)) {
|
|
throw new Error('Invalid canvas object - ' + canvasObj);
|
|
}
|
|
|
|
var webGl = canvasObj.getContext('experimental-webgl', UtilsCore.initWebGlDefaultOptions()) || canvasObj.getContext('webgl', UtilsCore.initWebGlDefaultOptions());
|
|
|
|
if (webGl) {
|
|
webGl.OES_texture_float = webGl.getExtension('OES_texture_float');
|
|
webGl.OES_texture_float_linear = webGl.getExtension('OES_texture_float_linear');
|
|
webGl.OES_element_index_uint = webGl.getExtension('OES_element_index_uint');
|
|
}
|
|
|
|
return webGl;
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'initWebGl2',
|
|
value: function initWebGl2(canvasObj) {
|
|
|
|
if (typeof _isCanvasSupported !== 'undefined' || canvasObj === null) {
|
|
if (!_isCanvasSupported) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if (!UtilsCore.isCanvas(canvasObj)) {
|
|
throw new Error('Invalid canvas object - ' + canvasObj);
|
|
}
|
|
|
|
return canvasObj.getContext('webgl2', UtilsCore.initWebGlDefaultOptions());
|
|
}
|
|
|
|
|
|
}, {
|
|
key: 'checkOutput',
|
|
value: function checkOutput(output) {
|
|
for (var i = 0; i < output.length; i++) {
|
|
if (isNaN(output[i]) || output[i] < 1) {
|
|
throw new Error('kernel.output[' + i + '] incorrectly defined as `' + output[i] + '`, needs to be numeric, and greater than 0');
|
|
}
|
|
}
|
|
}
|
|
}]);
|
|
|
|
return UtilsCore;
|
|
}();
|
|
|
|
|
|
var _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : false;
|
|
var _testingWebGl = UtilsCore.initWebGl(UtilsCore.initCanvas());
|
|
var _isWebGlSupported = UtilsCore.isWebGl(_testingWebGl);
|
|
var _isWebGlDrawBuffersSupported = _isWebGlSupported && Boolean(_testingWebGl.getExtension('WEBGL_draw_buffers'));
|
|
|
|
if (_isWebGlSupported) {
|
|
UtilsCore.OES_texture_float = _testingWebGl.OES_texture_float;
|
|
UtilsCore.OES_texture_float_linear = _testingWebGl.OES_texture_float_linear;
|
|
UtilsCore.OES_element_index_uint = _testingWebGl.OES_element_index_uint;
|
|
} else {
|
|
UtilsCore.OES_texture_float = false;
|
|
UtilsCore.OES_texture_float_linear = false;
|
|
UtilsCore.OES_element_index_uint = false;
|
|
}
|
|
|
|
module.exports = UtilsCore;
|
|
},{}],3:[function(require,module,exports){
|
|
'use strict';
|
|
|
|
var GPUCore = require("./core/gpu-core");
|
|
if (typeof module !== 'undefined') {
|
|
module.exports = GPUCore;
|
|
}
|
|
if (typeof window !== 'undefined') {
|
|
window.GPUCore = GPUCore;
|
|
if (window.GPU === null) {
|
|
window.GPU = GPUCore;
|
|
}
|
|
}
|
|
},{"./core/gpu-core":1}]},{},[3]);
|