RenderingContext.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. import GLenum from './GLenum';
  2. import ActiveInfo from './ActiveInfo';
  3. import Buffer from './Buffer';
  4. import Framebuffer from './Framebuffer';
  5. import Renderbuffer from './Renderbuffer';
  6. import Texture from './Texture';
  7. import Program from './Program';
  8. import Shader from './Shader';
  9. import ShaderPrecisionFormat from './ShaderPrecisionFormat';
  10. import UniformLocation from './UniformLocation';
  11. import GLmethod from './GLmethod';
  12. const processArray = (array, checkArrayType = false) => {
  13. function joinArray(arr, sep) {
  14. let res = '';
  15. for (let i = 0; i < arr.length; i++) {
  16. if (i !== 0) {
  17. res += sep;
  18. }
  19. res += arr[i];
  20. }
  21. return res;
  22. }
  23. let type = 'Float32Array';
  24. if (checkArrayType) {
  25. if (array instanceof Uint8Array) {
  26. type = 'Uint8Array'
  27. } else if (array instanceof Uint16Array) {
  28. type = 'Uint16Array';
  29. } else if (array instanceof Uint32Array) {
  30. type = 'Uint32Array';
  31. } else if (array instanceof Float32Array) {
  32. type = 'Float32Array';
  33. } else {
  34. throw new Error('Check array type failed. Array type is ' + typeof array);
  35. }
  36. }
  37. const ArrayTypes = {
  38. Uint8Array: 1,
  39. Uint16Array: 2,
  40. Uint32Array: 4,
  41. Float32Array: 14
  42. };
  43. return ArrayTypes[type] + ',' + btoa(joinArray(array, ','))
  44. }
  45. export default class WebGLRenderingContext {
  46. // static GBridge = null;
  47. className = 'WebGLRenderingContext';
  48. constructor(canvas, type, attrs) {
  49. this._canvas = canvas;
  50. this._type = type;
  51. this._version = 'WebGL 1.0';
  52. this._attrs = attrs;
  53. this._map = new Map();
  54. Object.keys(GLenum)
  55. .forEach(name => Object.defineProperty(this, name, {
  56. value: GLenum[name]
  57. }));
  58. }
  59. get canvas() {
  60. return this._canvas;
  61. }
  62. activeTexture = function (textureUnit) {
  63. WebGLRenderingContext.GBridge.callNative(
  64. this._canvas.id,
  65. GLmethod.activeTexture + ',' + textureUnit,
  66. true
  67. );
  68. }
  69. attachShader = function (progarm, shader) {
  70. WebGLRenderingContext.GBridge.callNative(
  71. this._canvas.id,
  72. GLmethod.attachShader + ',' + progarm.id + ',' + shader.id,
  73. true
  74. );
  75. }
  76. bindAttribLocation = function (program, index, name) {
  77. WebGLRenderingContext.GBridge.callNative(
  78. this._canvas.id,
  79. GLmethod.bindAttribLocation + ',' + program.id + ',' + index + ',' + name,
  80. true
  81. )
  82. }
  83. bindBuffer = function (target, buffer) {
  84. WebGLRenderingContext.GBridge.callNative(
  85. this._canvas.id,
  86. GLmethod.bindBuffer + ',' + target + ',' + (buffer ? buffer.id : 0),
  87. true
  88. );
  89. }
  90. bindFramebuffer = function (target, framebuffer) {
  91. WebGLRenderingContext.GBridge.callNative(
  92. this._canvas.id,
  93. GLmethod.bindFramebuffer + ',' + target + ',' + (framebuffer ? framebuffer.id : 0),
  94. true
  95. )
  96. }
  97. bindRenderbuffer = function (target, renderBuffer) {
  98. WebGLRenderingContext.GBridge.callNative(
  99. this._canvas.id,
  100. GLmethod.bindRenderbuffer + ',' + target + ',' + (renderBuffer ? renderBuffer.id : 0),
  101. true
  102. )
  103. }
  104. bindTexture = function (target, texture) {
  105. WebGLRenderingContext.GBridge.callNative(
  106. this._canvas.id,
  107. GLmethod.bindTexture + ',' + target + ',' + (texture ? texture.id : 0),
  108. true
  109. )
  110. }
  111. blendColor = function (r, g, b, a) {
  112. WebGLRenderingContext.GBridge.callNative(
  113. this._canvas.id,
  114. GLmethod.blendColor + ',' + target + ',' + r + ',' + g + ',' + b + ',' + a,
  115. true
  116. )
  117. }
  118. blendEquation = function (mode) {
  119. WebGLRenderingContext.GBridge.callNative(
  120. this._canvas.id,
  121. GLmethod.blendEquation + ',' + mode,
  122. true
  123. )
  124. }
  125. blendEquationSeparate = function (modeRGB, modeAlpha) {
  126. WebGLRenderingContext.GBridge.callNative(
  127. this._canvas.id,
  128. GLmethod.blendEquationSeparate + ',' + modeRGB + ',' + modeAlpha,
  129. true
  130. )
  131. }
  132. blendFunc = function (sfactor, dfactor) {
  133. WebGLRenderingContext.GBridge.callNative(
  134. this._canvas.id,
  135. GLmethod.blendFunc + ',' + sfactor + ',' + dfactor,
  136. true
  137. );
  138. }
  139. blendFuncSeparate = function (srcRGB, dstRGB, srcAlpha, dstAlpha) {
  140. WebGLRenderingContext.GBridge.callNative(
  141. this._canvas.id,
  142. GLmethod.blendFuncSeparate + ',' + srcRGB + ',' + dstRGB + ',' + srcAlpha + ',' + dstAlpha,
  143. true
  144. );
  145. }
  146. bufferData = function (target, data, usage) {
  147. WebGLRenderingContext.GBridge.callNative(
  148. this._canvas.id,
  149. GLmethod.bufferData + ',' + target + ',' + processArray(data, true) + ',' + usage,
  150. true
  151. )
  152. }
  153. bufferSubData = function (target, offset, data) {
  154. WebGLRenderingContext.GBridge.callNative(
  155. this._canvas.id,
  156. GLmethod.bufferSubData + ',' + target + ',' + offset + ',' + processArray(data, true),
  157. true
  158. )
  159. }
  160. checkFramebufferStatus = function (target) {
  161. const result = WebGLRenderingContext.GBridge.callNative(
  162. this._canvas.id,
  163. GLmethod.checkFramebufferStatus + ',' + target
  164. );
  165. return Number(result);
  166. }
  167. clear = function (mask) {
  168. WebGLRenderingContext.GBridge.callNative(
  169. this._canvas.id,
  170. GLmethod.clear + ',' + mask
  171. );
  172. this._canvas._needRender = true;
  173. }
  174. clearColor = function (r, g, b, a) {
  175. WebGLRenderingContext.GBridge.callNative(
  176. this._canvas.id,
  177. GLmethod.clearColor + ',' + r + ',' + g + ',' + b,
  178. true
  179. )
  180. }
  181. clearDepth = function (depth) {
  182. WebGLRenderingContext.GBridge.callNative(
  183. this._canvas.id,
  184. GLmethod.clearDepth + ',' + depth,
  185. true
  186. )
  187. }
  188. clearStencil = function (s) {
  189. WebGLRenderingContext.GBridge.callNative(
  190. this._canvas.id,
  191. GLmethod.clearStencil + ',' + s
  192. );
  193. }
  194. colorMask = function (r, g, b, a) {
  195. WebGLRenderingContext.GBridge.callNative(
  196. this._canvas.id,
  197. GLmethod.colorMask + ',' + r + ',' + g + ',' + b + ',' + a
  198. )
  199. }
  200. compileShader = function (shader) {
  201. WebGLRenderingContext.GBridge.callNative(
  202. this._canvas.id,
  203. GLmethod.compileShader + ',' + shader.id,
  204. true
  205. )
  206. }
  207. compressedTexImage2D = function (target, level, internalformat, width, height, border, pixels) {
  208. WebGLRenderingContext.GBridge.callNative(
  209. this._canvas.id,
  210. GLmethod.compressedTexImage2D + ',' + target + ',' + level + ',' + internalformat + ',' +
  211. width + ',' + height + ',' + border + ',' + processArray(pixels),
  212. true
  213. )
  214. }
  215. compressedTexSubImage2D = function (target, level, xoffset, yoffset, width, height, format, pixels) {
  216. WebGLRenderingContext.GBridge.callNative(
  217. this._canvas.id,
  218. GLmethod.compressedTexSubImage2D + ',' + target + ',' + level + ',' + xoffset + ',' + yoffset + ',' +
  219. width + ',' + height + ',' + format + ',' + processArray(pixels),
  220. true
  221. )
  222. }
  223. copyTexImage2D = function (target, level, internalformat, x, y, width, height, border) {
  224. WebGLRenderingContext.GBridge.callNative(
  225. this._canvas.id,
  226. GLmethod.copyTexImage2D + ',' + target + ',' + level + ',' + internalformat + ',' + x + ',' + y + ',' +
  227. width + ',' + height + ',' + border,
  228. true
  229. );
  230. }
  231. copyTexSubImage2D = function (target, level, xoffset, yoffset, x, y, width, height) {
  232. WebGLRenderingContext.GBridge.callNative(
  233. this._canvas.id,
  234. GLmethod.copyTexSubImage2D + ',' + target + ',' + level + ',' + xoffset + ',' + yoffset + ',' + x + ',' + y + ',' +
  235. width + ',' + height
  236. );
  237. }
  238. createBuffer = function () {
  239. const result = WebGLRenderingContext.GBridge.callNative(
  240. this._canvas.id,
  241. GLmethod.createBuffer + ''
  242. );
  243. const buffer = new Buffer(result);
  244. this._map.set(buffer.uuid(), buffer);
  245. return buffer;
  246. }
  247. createFramebuffer = function () {
  248. const result = WebGLRenderingContext.GBridge.callNative(
  249. this._canvas.id,
  250. GLmethod.createFramebuffer + ''
  251. );
  252. const framebuffer = new Framebuffer(result);
  253. this._map.set(framebuffer.uuid(), framebuffer);
  254. return framebuffer;
  255. }
  256. createProgram = function () {
  257. const id = WebGLRenderingContext.GBridge.callNative(
  258. this._canvas.id,
  259. GLmethod.createProgram + ''
  260. );
  261. const program = new Program(id);
  262. this._map.set(program.uuid(), program);
  263. return program;
  264. }
  265. createRenderbuffer = function () {
  266. const id = WebGLRenderingContext.GBridge.callNative(
  267. this._canvas.id,
  268. GLmethod.createRenderbuffer + ''
  269. )
  270. const renderBuffer = new Renderbuffer(id);
  271. this._map.set(renderBuffer.uuid(), renderBuffer);
  272. return renderBuffer;
  273. }
  274. createShader = function (type) {
  275. const id = WebGLRenderingContext.GBridge.callNative(
  276. this._canvas.id,
  277. GLmethod.createShader + ',' + type
  278. )
  279. const shader = new Shader(id, type);
  280. this._map.set(shader.uuid(), shader);
  281. return shader;
  282. }
  283. createTexture = function () {
  284. const id = WebGLRenderingContext.GBridge.callNative(
  285. this._canvas.id,
  286. GLmethod.createTexture + ''
  287. );
  288. const texture = new Texture(id);
  289. this._map.set(texture.uuid(), texture);
  290. return texture;
  291. }
  292. cullFace = function (mode) {
  293. WebGLRenderingContext.GBridge.callNative(
  294. this._canvas.id,
  295. GLmethod.cullFace + ',' + mode,
  296. true
  297. )
  298. }
  299. deleteBuffer = function (buffer) {
  300. WebGLRenderingContext.GBridge.callNative(
  301. this._canvas.id,
  302. GLmethod.deleteBuffer + ',' + buffer.id,
  303. true
  304. )
  305. }
  306. deleteFramebuffer = function (framebuffer) {
  307. WebGLRenderingContext.GBridge.callNative(
  308. this._canvas.id,
  309. GLmethod.deleteFramebuffer + ',' + framebuffer.id,
  310. true
  311. )
  312. }
  313. deleteProgram = function (program) {
  314. WebGLRenderingContext.GBridge.callNative(
  315. this._canvas.id,
  316. GLmethod.deleteProgram + ',' + program.id,
  317. true
  318. )
  319. }
  320. deleteRenderbuffer = function (renderbuffer) {
  321. WebGLRenderingContext.GBridge.callNative(
  322. this._canvas.id,
  323. GLmethod.deleteRenderbuffer + ',' + renderbuffer.id,
  324. true
  325. )
  326. }
  327. deleteShader = function (shader) {
  328. WebGLRenderingContext.GBridge.callNative(
  329. this._canvas.id,
  330. GLmethod.deleteShader + ',' + shader.id,
  331. true
  332. )
  333. }
  334. deleteTexture = function (texture) {
  335. WebGLRenderingContext.GBridge.callNative(
  336. this._canvas.id,
  337. GLmethod.deleteTexture + ',' + texture.id,
  338. true
  339. )
  340. }
  341. depthFunc = function (func) {
  342. WebGLRenderingContext.GBridge.callNative(
  343. this._canvas.id,
  344. GLmethod.depthFunc + ',' + func
  345. )
  346. }
  347. depthMask = function (flag) {
  348. WebGLRenderingContext.GBridge.callNative(
  349. this._canvas.id,
  350. GLmethod.depthMask + ',' + Number(flag),
  351. true
  352. )
  353. }
  354. depthRange = function (zNear, zFar) {
  355. WebGLRenderingContext.GBridge.callNative(
  356. this._canvas.id,
  357. GLmethod.depthRange + ',' + zNear + ',' + zFar,
  358. true
  359. )
  360. }
  361. detachShader = function (program, shader) {
  362. WebGLRenderingContext.GBridge.callNative(
  363. this._canvas.id,
  364. GLmethod.detachShader + ',' + program.id + ',' + shader.id,
  365. true
  366. )
  367. }
  368. disable = function (cap) {
  369. WebGLRenderingContext.GBridge.callNative(
  370. this._canvas.id,
  371. GLmethod.disable + ',' + cap,
  372. true
  373. )
  374. }
  375. disableVertexAttribArray = function (index) {
  376. WebGLRenderingContext.GBridge.callNative(
  377. this._canvas.id,
  378. GLmethod.disableVertexAttribArray + ',' + index,
  379. true
  380. );
  381. }
  382. drawArrays = function (mode, first, count) {
  383. WebGLRenderingContext.GBridge.callNative(
  384. this._canvas.id,
  385. GLmethod.drawArrays + ',' + mode + ',' + first + ',' + count
  386. )
  387. this._canvas._needRender = true;
  388. }
  389. drawElements = function (mode, count, type, offset) {
  390. WebGLRenderingContext.GBridge.callNative(
  391. this._canvas.id,
  392. GLmethod.drawElements + ',' + mode + ',' + count + ',' + type + ',' + offset + ';'
  393. );
  394. this._canvas._needRender = true;
  395. }
  396. enable = function (cap) {
  397. WebGLRenderingContext.GBridge.callNative(
  398. this._canvas.id,
  399. GLmethod.enable + ',' + cap,
  400. true
  401. );
  402. }
  403. enableVertexAttribArray = function (index) {
  404. WebGLRenderingContext.GBridge.callNative(
  405. this._canvas.id,
  406. GLmethod.enableVertexAttribArray + ',' + index,
  407. true
  408. )
  409. }
  410. flush = function () {
  411. WebGLRenderingContext.GBridge.callNative(
  412. this._canvas.id,
  413. GLmethod.flush + ''
  414. )
  415. }
  416. framebufferRenderbuffer = function (target, attachment, textarget, texture, level) {
  417. WebGLRenderingContext.GBridge.callNative(
  418. this._canvas.id,
  419. GLmethod.framebufferRenderbuffer + ',' + target + ',' + attachment + ',' + textarget + ',' + (texture ? texture.id : 0) + ',' + level,
  420. true
  421. )
  422. }
  423. framebufferTexture2D = function (target, attachment, textarget, texture, level) {
  424. WebGLRenderingContext.GBridge.callNative(
  425. this._canvas.id,
  426. GLmethod.framebufferTexture2D + ',' + target + ',' + attachment + ',' + textarget + ',' + (texture ? texture.id : 0) + ',' + level,
  427. true
  428. )
  429. }
  430. frontFace = function (mode) {
  431. WebGLRenderingContext.GBridge.callNative(
  432. this._canvas.id,
  433. GLmethod.frontFace + ',' + mode,
  434. true
  435. )
  436. }
  437. generateMipmap = function (target) {
  438. WebGLRenderingContext.GBridge.callNative(
  439. this._canvas.id,
  440. GLmethod.generateMipmap + ',' + target,
  441. true
  442. )
  443. }
  444. getActiveAttrib = function (progarm, index) {
  445. const resultString = WebGLRenderingContext.GBridge.callNative(
  446. this._canvas.id,
  447. GLmethod.getActiveAttrib + ',' + progarm.id + ',' + index
  448. )
  449. const [type, size, name] = resultString.split(',');
  450. return new ActiveInfo({
  451. type: Number(type),
  452. size: Number(size),
  453. name
  454. });
  455. }
  456. getActiveUniform = function (progarm, index) {
  457. const resultString = WebGLRenderingContext.GBridge.callNative(
  458. this._canvas.id,
  459. GLmethod.getActiveUniform + ',' + progarm.id + ',' + index
  460. );
  461. const [type, size, name] = resultString.split(',');
  462. return new ActiveInfo({
  463. type: Number(type),
  464. size: Number(size),
  465. name
  466. })
  467. }
  468. getAttachedShaders = function (progarm) {
  469. const result = WebGLRenderingContext.GBridge.callNative(
  470. this._canvas.id,
  471. GLmethod.getAttachedShaders + ',' + progarm.id
  472. );
  473. const [type, ...ids] = result;
  474. return ids.map(id => this._map.get(Shader.uuid(id)));
  475. }
  476. getAttribLocation = function (progarm, name) {
  477. return WebGLRenderingContext.GBridge.callNative(
  478. this._canvas.id,
  479. GLmethod.getAttribLocation + ',' + progarm.id + ',' + name
  480. )
  481. }
  482. getBufferParameter = function (target, pname) {
  483. const result = WebGLRenderingContext.GBridge.callNative(
  484. this._canvas.id,
  485. GLmethod.getBufferParameter + ',' + target + ',' + pname
  486. );
  487. const [type, res] = getBufferParameter;
  488. return res;
  489. }
  490. getError = function () {
  491. const result = WebGLRenderingContext.GBridge.callNative(
  492. this._canvas.id,
  493. GLmethod.getError + ''
  494. )
  495. return result;
  496. }
  497. getExtension = function (name) {
  498. return null;
  499. }
  500. getFramebufferAttachmentParameter = function (target, attachment, pname) {
  501. const result = WebGLRenderingContext.GBridge.callNative(
  502. this._canvas.id,
  503. GLmethod.getFramebufferAttachmentParameter + ',' + target + ',' + attachment + ',' + pname
  504. )
  505. switch (pname) {
  506. case GLenum.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
  507. return this._map.get(Renderbuffer.uuid(result)) || this._map.get(Texture.uuid(result)) || null;
  508. default:
  509. return result;
  510. }
  511. }
  512. getParameter = function (pname) {
  513. const result = WebGLRenderingContext.GBridge.callNative(
  514. this._canvas.id,
  515. GLmethod.getParameter + ',' + pname
  516. )
  517. switch (pname) {
  518. case GLenum.VERSION:
  519. return this._version;
  520. case GLenum.ARRAY_BUFFER_BINDING: // buffer
  521. case GLenum.ELEMENT_ARRAY_BUFFER_BINDING: // buffer
  522. return this._map.get(Buffer.uuid(result)) || null;
  523. case GLenum.CURRENT_PROGRAM: // program
  524. return this._map.get(Program.uuid(result)) || null;
  525. case GLenum.FRAMEBUFFER_BINDING: // framebuffer
  526. return this._map.get(Framebuffer.uuid(result)) || null;
  527. case GLenum.RENDERBUFFER_BINDING: // renderbuffer
  528. return this._map.get(Renderbuffer.uuid(result)) || null;
  529. case GLenum.TEXTURE_BINDING_2D: // texture
  530. case GLenum.TEXTURE_BINDING_CUBE_MAP: // texture
  531. return this._map.get(Texture.uuid(result)) || null;
  532. case GLenum.ALIASED_LINE_WIDTH_RANGE: // Float32Array
  533. case GLenum.ALIASED_POINT_SIZE_RANGE: // Float32Array
  534. case GLenum.BLEND_COLOR: // Float32Array
  535. case GLenum.COLOR_CLEAR_VALUE: // Float32Array
  536. case GLenum.DEPTH_RANGE: // Float32Array
  537. case GLenum.MAX_VIEWPORT_DIMS: // Int32Array
  538. case GLenum.SCISSOR_BOX: // Int32Array
  539. case GLenum.VIEWPORT: // Int32Array
  540. case GLenum.COMPRESSED_TEXTURE_FORMATS: // Uint32Array
  541. default:
  542. const [type, ...res] = result.split(',');
  543. if (res.length === 1) {
  544. return Number(res[0]);
  545. } else {
  546. return res.map(Number);
  547. }
  548. }
  549. }
  550. getProgramInfoLog = function (progarm) {
  551. return WebGLRenderingContext.GBridge.callNative(
  552. this._canvas.id,
  553. GLmethod.getProgramInfoLog + ',' + progarm.id
  554. )
  555. }
  556. getProgramParameter = function (program, pname) {
  557. const res = WebGLRenderingContext.GBridge.callNative(
  558. this._canvas.id,
  559. GLmethod.getProgramParameter + ',' + program.id + ',' + pname
  560. );
  561. const [type, result] = res.split(',').map(i => parseInt(i));
  562. if (type === 1) {
  563. return Boolean(result);
  564. } else if (type === 2) {
  565. return result;
  566. } else {
  567. throw new Error('Unrecongized program paramater ' + res + ', type: ' + typeof res);
  568. }
  569. }
  570. getRenderbufferParameter = function (target, pname) {
  571. const result = WebGLRenderingContext.GBridge.callNative(
  572. this._canvas.id,
  573. GLmethod.getRenderbufferParameter + ',' + target + ',' + pname
  574. )
  575. return result;
  576. }
  577. getShaderInfoLog = function (shader) {
  578. return WebGLRenderingContext.GBridge.callNative(
  579. this._canvas.id,
  580. GLmethod.getShaderInfoLog + ',' + shader.id
  581. );
  582. }
  583. getShaderParameter = function (shader, pname) {
  584. return WebGLRenderingContext.GBridge.callNative(
  585. this._canvas.id,
  586. GLmethod.getShaderParameter + ',' + shader.id + ',' + pname
  587. )
  588. }
  589. getShaderPrecisionFormat = function (shaderType, precisionType) {
  590. const [rangeMin, rangeMax, precision] = WebGLRenderingContext.GBridge.callNative(
  591. this._canvas.id,
  592. GLmethod.getShaderPrecisionFormat + ',' + shaderType + ',' + precisionType
  593. );
  594. const shaderPrecisionFormat = new ShaderPrecisionFormat({
  595. rangeMin: Number(rangeMin),
  596. rangeMax: Number(rangeMax),
  597. precision: Number(precision)
  598. });
  599. return shaderPrecisionFormat;
  600. }
  601. getShaderSource = function (shader) {
  602. const result = WebGLRenderingContext.GBridge.callNative(
  603. this._canvas.id,
  604. GLmethod.getShaderSource + ',' + shader.id
  605. );
  606. return result;
  607. }
  608. getSupportedExtensions = function () {
  609. return Object.keys({});
  610. }
  611. getTexParameter = function (target, pname) {
  612. const result = WebGLRenderingContext.GBridge.callNative(
  613. this._canvas.id,
  614. GLmethod.getTexParameter + ',' + target + ',' + pname
  615. )
  616. return result;
  617. }
  618. getUniformLocation = function (program, name) {
  619. const id = WebGLRenderingContext.GBridge.callNative(
  620. this._canvas.id,
  621. GLmethod.getUniformLocation + ',' + program.id + ',' + name
  622. );
  623. if (id === -1) {
  624. return null;
  625. } else {
  626. return new UniformLocation(Number(id));
  627. }
  628. }
  629. getVertexAttrib = function (index, pname) {
  630. const result = WebGLRenderingContext.GBridge.callNative(
  631. this._canvas.id,
  632. GLmethod.getVertexAttrib + ',' + index + ',' + pname
  633. );
  634. switch (pname) {
  635. case GLenum.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
  636. return this._map.get(Buffer.uuid(result)) || null;
  637. case GLenum.CURRENT_VERTEX_ATTRIB: // Float32Array
  638. default:
  639. return result;
  640. }
  641. }
  642. getVertexAttribOffset = function (index, pname) {
  643. const result = WebGLRenderingContext.GBridge.callNative(
  644. this._canvas.id,
  645. GLmethod.getVertexAttribOffset + ',' + index + ',' + pname
  646. )
  647. return Number(result);
  648. }
  649. isBuffer = function (buffer) {
  650. const result = WebGLRenderingContext.GBridge.callNative(
  651. this._canvas.id,
  652. GLmethod.isBuffer + ',' + buffer.id
  653. )
  654. return Boolean(result);
  655. }
  656. isContextLost = function () {
  657. return false;
  658. }
  659. isEnabled = function (cap) {
  660. const result = WebGLRenderingContext.GBridge.callNative(
  661. this._canvas.id,
  662. GLmethod.isEnabled + ',' + cap
  663. )
  664. return Boolean(result);
  665. }
  666. isFramebuffer = function (framebuffer) {
  667. const result = WebGLRenderingContext.GBridge.callNative(
  668. this._canvas.id,
  669. GLmethod.isFramebuffer + ',' + framebuffer.id
  670. )
  671. return Boolean(result);
  672. }
  673. isProgram = function (program) {
  674. const result = WebGLRenderingContext.GBridge.callNative(
  675. this._canvas.id,
  676. GLmethod.isProgram + ',' + program.id
  677. )
  678. return Boolean(result);
  679. }
  680. isRenderbuffer = function (renderBuffer) {
  681. const result = WebGLRenderingContext.GBridge.callNative(
  682. this._canvas.id,
  683. GLmethod.isRenderbuffer + ',' + renderbuffer.id
  684. )
  685. return Boolean(result);
  686. }
  687. isShader = function (shader) {
  688. const result = WebGLRenderingContext.GBridge.callNative(
  689. this._canvas.id,
  690. GLmethod.isShader + ',' + shader.id
  691. )
  692. return Boolean(result);
  693. }
  694. isTexture = function (texture) {
  695. const result = WebGLRenderingContext.GBridge.callNative(
  696. this._canvas.id,
  697. GLmethod.isTexture + ',' + texture.id
  698. );
  699. return Boolean(result);
  700. }
  701. lineWidth = function (width) {
  702. WebGLRenderingContext.GBridge.callNative(
  703. this._canvas.id,
  704. GLmethod.lineWidth + ',' + width,
  705. true
  706. )
  707. }
  708. linkProgram = function (program) {
  709. WebGLRenderingContext.GBridge.callNative(
  710. this._canvas.id,
  711. GLmethod.linkProgram + ',' + program.id,
  712. true
  713. );
  714. }
  715. pixelStorei = function (pname, param) {
  716. WebGLRenderingContext.GBridge.callNative(
  717. this._canvas.id,
  718. GLmethod.pixelStorei + ',' + pname + ',' + Number(param)
  719. )
  720. }
  721. polygonOffset = function (factor, units) {
  722. WebGLRenderingContext.GBridge.callNative(
  723. this._canvas.id,
  724. GLmethod.polygonOffset + ',' + factor + ',' + units
  725. )
  726. }
  727. readPixels = function (x, y, width, height, format, type, pixels) {
  728. const result = WebGLRenderingContext.GBridge.callNative(
  729. this._canvas.id,
  730. GLmethod.readPixels + ',' + x + ',' + y + ',' + width + ',' + height + ',' + format + ',' + type
  731. )
  732. return result;
  733. }
  734. renderbufferStorage = function (target, internalFormat, width, height) {
  735. WebGLRenderingContext.GBridge.callNative(
  736. this._canvas.id,
  737. GLmethod.renderbufferStorage + ',' + target + ',' + internalFormat + ',' + width + ',' + height,
  738. true
  739. )
  740. }
  741. sampleCoverage = function (value, invert) {
  742. WebGLRenderingContext.GBridge.callNative(
  743. this._canvas.id,
  744. GLmethod.sampleCoverage + ',' + value + ',' + Number(invert),
  745. true
  746. )
  747. }
  748. scissor = function (x, y, width, height) {
  749. WebGLRenderingContext.GBridge.callNative(
  750. this._canvas.id,
  751. GLmethod.scissor + ',' + x + ',' + y + ',' + width + ',' + height,
  752. true
  753. )
  754. }
  755. shaderSource = function (shader, source) {
  756. WebGLRenderingContext.GBridge.callNative(
  757. this._canvas.id,
  758. GLmethod.shaderSource + ',' + shader.id + ',' + source
  759. )
  760. }
  761. stencilFunc = function (func, ref, mask) {
  762. WebGLRenderingContext.GBridge.callNative(
  763. this._canvas.id,
  764. GLmethod.stencilFunc + ',' + func + ',' + ref + ',' + mask,
  765. true
  766. )
  767. }
  768. stencilFuncSeparate = function (face, func, ref, mask) {
  769. WebGLRenderingContext.GBridge.callNative(
  770. this._canvas.id,
  771. GLmethod.stencilFuncSeparate + ',' + face + ',' + func + ',' + ref + ',' + mask,
  772. true
  773. )
  774. }
  775. stencilMask = function (mask) {
  776. WebGLRenderingContext.GBridge.callNative(
  777. this._canvas.id,
  778. GLmethod.stencilMask + ',' + mask,
  779. true
  780. )
  781. }
  782. stencilMaskSeparate = function (face, mask) {
  783. WebGLRenderingContext.GBridge.callNative(
  784. this._canvas.id,
  785. GLmethod.stencilMaskSeparate + ',' + face + ',' + mask,
  786. true
  787. )
  788. }
  789. stencilOp = function (fail, zfail, zpass) {
  790. WebGLRenderingContext.GBridge.callNative(
  791. this._canvas.id,
  792. GLmethod.stencilOp + ',' + fail + ',' + zfail + ',' + zpass
  793. )
  794. }
  795. stencilOpSeparate = function (face, fail, zfail, zpass) {
  796. WebGLRenderingContext.GBridge.callNative(
  797. this._canvas.id,
  798. GLmethod.stencilOp + ',' + face + ',' + fail + ',' + zfail + ',' + zpass,
  799. true
  800. )
  801. }
  802. texImage2D = function (...args) {
  803. WebGLRenderingContext.GBridge.texImage2D(this._canvas.id, ...args);
  804. }
  805. texParameterf = function (target, pname, param) {
  806. WebGLRenderingContext.GBridge.callNative(
  807. this._canvas.id,
  808. GLmethod.texParameterf + ',' + target + ',' + pname + ',' + param,
  809. true
  810. )
  811. }
  812. texParameteri = function (target, pname, param) {
  813. WebGLRenderingContext.GBridge.callNative(
  814. this._canvas.id,
  815. GLmethod.texParameteri + ',' + target + ',' + pname + ',' + param
  816. )
  817. }
  818. texSubImage2D = function (...args) {
  819. WebGLRenderingContext.GBridge.texSubImage2D(this._canvas.id, ...args);
  820. }
  821. uniform1f = function (location, v0) {
  822. WebGLRenderingContext.GBridge.callNative(
  823. this._canvas.id,
  824. GLmethod.uniform1f + ',' + location.id + ',' + v0
  825. )
  826. }
  827. uniform1fv = function (location, value) {
  828. WebGLRenderingContext.GBridge.callNative(
  829. this._canvas.id,
  830. GLmethod.uniform1fv + ',' + location.id + ',' + processArray(value),
  831. true
  832. )
  833. }
  834. uniform1i = function (location, v0) {
  835. WebGLRenderingContext.GBridge.callNative(
  836. this._canvas.id,
  837. GLmethod.uniform1i + ',' + location.id + ',' + v0,
  838. // true
  839. )
  840. }
  841. uniform1iv = function (location, value) {
  842. WebGLRenderingContext.GBridge.callNative(
  843. this._canvas.id,
  844. GLmethod.uniform1iv + ',' + location.id + ',' + processArray(value),
  845. true
  846. )
  847. }
  848. uniform2f = function (location, v0, v1) {
  849. WebGLRenderingContext.GBridge.callNative(
  850. this._canvas.id,
  851. GLmethod.uniform2f + ',' + location.id + ',' + v0 + ',' + v1,
  852. true
  853. )
  854. }
  855. uniform2fv = function (location, value) {
  856. WebGLRenderingContext.GBridge.callNative(
  857. this._canvas.id,
  858. GLmethod.uniform2fv + ',' + location.id + ',' + processArray(value),
  859. true
  860. )
  861. }
  862. uniform2i = function (location, v0, v1) {
  863. WebGLRenderingContext.GBridge.callNative(
  864. this._canvas.id,
  865. GLmethod.uniform2i + ',' + location.id + ',' + v0 + ',' + v1,
  866. true
  867. )
  868. }
  869. uniform2iv = function (location, value) {
  870. WebGLRenderingContext.GBridge.callNative(
  871. this._canvas.id,
  872. GLmethod.uniform2iv + ',' + location.id + ',' + processArray(value),
  873. true
  874. )
  875. }
  876. uniform3f = function (location, v0, v1, v2) {
  877. WebGLRenderingContext.GBridge.callNative(
  878. this._canvas.id,
  879. GLmethod.uniform3f + ',' + location.id + ',' + v0 + ',' + v1 + ',' + v2,
  880. true
  881. )
  882. }
  883. uniform3fv = function (location, value) {
  884. WebGLRenderingContext.GBridge.callNative(
  885. this._canvas.id,
  886. GLmethod.uniform3fv + ',' + location.id + ',' + processArray(value),
  887. true
  888. )
  889. }
  890. uniform3i = function (location, v0, v1, v2) {
  891. WebGLRenderingContext.GBridge.callNative(
  892. this._canvas.id,
  893. GLmethod.uniform3i + ',' + location.id + ',' + v0 + ',' + v1 + ',' + v2,
  894. true
  895. )
  896. }
  897. uniform3iv = function (location, value) {
  898. WebGLRenderingContext.GBridge.callNative(
  899. this._canvas.id,
  900. GLmethod.uniform3iv + ',' + location.id + ',' + processArray(value),
  901. true
  902. )
  903. }
  904. uniform4f = function (location, v0, v1, v2, v3) {
  905. WebGLRenderingContext.GBridge.callNative(
  906. this._canvas.id,
  907. GLmethod.uniform4f + ',' + location.id + ',' + v0 + ',' + v1 + ',' + v2 + ',' + v3,
  908. true
  909. )
  910. }
  911. uniform4fv = function (location, value) {
  912. WebGLRenderingContext.GBridge.callNative(
  913. this._canvas.id,
  914. GLmethod.uniform4fv + ',' + location.id + ',' + processArray(value),
  915. true
  916. )
  917. }
  918. uniform4i = function (location, v0, v1, v2, v3) {
  919. WebGLRenderingContext.GBridge.callNative(
  920. this._canvas.id,
  921. GLmethod.uniform4i + ',' + location.id + ',' + v0 + ',' + v1 + ',' + v2 + ',' + v3,
  922. true
  923. )
  924. }
  925. uniform4iv = function (location, value) {
  926. WebGLRenderingContext.GBridge.callNative(
  927. this._canvas.id,
  928. GLmethod.uniform4iv + ',' + location.id + ',' + processArray(value, true),
  929. true
  930. )
  931. }
  932. uniformMatrix2fv = function (location, transpose, value) {
  933. WebGLRenderingContext.GBridge.callNative(
  934. this._canvas.id,
  935. GLmethod.uniformMatrix2fv + ',' + location.id + ',' + Number(transpose) + ',' + processArray(value),
  936. true
  937. )
  938. }
  939. uniformMatrix3fv = function (location, transpose, value) {
  940. WebGLRenderingContext.GBridge.callNative(
  941. this._canvas.id,
  942. GLmethod.uniformMatrix3fv + ',' + location.id + ',' + Number(transpose) + ',' + processArray(value),
  943. true
  944. )
  945. }
  946. uniformMatrix4fv = function (location, transpose, value) {
  947. WebGLRenderingContext.GBridge.callNative(
  948. this._canvas.id,
  949. GLmethod.uniformMatrix4fv + ',' + location.id + ',' + Number(transpose) + ',' + processArray(value),
  950. true
  951. );
  952. }
  953. useProgram = function (progarm) {
  954. WebGLRenderingContext.GBridge.callNative(
  955. this._canvas.id,
  956. GLmethod.useProgram + ',' + progarm.id + '',
  957. true
  958. )
  959. }
  960. validateProgram = function (program) {
  961. WebGLRenderingContext.GBridge.callNative(
  962. this._canvas.id,
  963. GLmethod.validateProgram + ',' + program.id,
  964. true
  965. )
  966. }
  967. vertexAttrib1f = function (index, v0) {
  968. WebGLRenderingContext.GBridge.callNative(
  969. this._canvas.id,
  970. GLmethod.vertexAttrib1f + ',' + index + ',' + v0,
  971. true
  972. )
  973. }
  974. vertexAttrib2f = function (index, v0, v1) {
  975. WebGLRenderingContext.GBridge.callNative(
  976. this._canvas.id,
  977. GLmethod.vertexAttrib2f + ',' + index + ',' + v0 + ',' + v1,
  978. true
  979. )
  980. }
  981. vertexAttrib3f = function (index, v0, v1, v2) {
  982. WebGLRenderingContext.GBridge.callNative(
  983. this._canvas.id,
  984. GLmethod.vertexAttrib3f + ',' + index + ',' + v0 + ',' + v1 + ',' + v2,
  985. true
  986. )
  987. }
  988. vertexAttrib4f = function (index, v0, v1, v2, v3) {
  989. WebGLRenderingContext.GBridge.callNative(
  990. this._canvas.id,
  991. GLmethod.vertexAttrib4f + ',' + index + ',' + v0 + ',' + v1 + ',' + v2 + ',' + v3,
  992. true
  993. )
  994. }
  995. vertexAttrib1fv = function (index, value) {
  996. WebGLRenderingContext.GBridge.callNative(
  997. this._canvas.id,
  998. GLmethod.vertexAttrib1fv + ',' + index + ',' + processArray(value),
  999. true
  1000. )
  1001. }
  1002. vertexAttrib2fv = function (index, value) {
  1003. WebGLRenderingContext.GBridge.callNative(
  1004. this._canvas.id,
  1005. GLmethod.vertexAttrib2fv + ',' + index + ',' + processArray(value),
  1006. true
  1007. )
  1008. }
  1009. vertexAttrib3fv = function (index, value) {
  1010. WebGLRenderingContext.GBridge.callNative(
  1011. this._canvas.id,
  1012. GLmethod.vertexAttrib3fv + ',' + index + ',' + processArray(value),
  1013. true
  1014. )
  1015. }
  1016. vertexAttrib4fv = function (index, value) {
  1017. WebGLRenderingContext.GBridge.callNative(
  1018. this._canvas.id,
  1019. GLmethod.vertexAttrib4fv + ',' + index + ',' + processArray(value),
  1020. true
  1021. )
  1022. }
  1023. vertexAttribPointer = function (index, size, type, normalized, stride, offset) {
  1024. WebGLRenderingContext.GBridge.callNative(
  1025. this._canvas.id,
  1026. GLmethod.vertexAttribPointer + ',' + index + ',' + size + ',' + type + ',' + Number(normalized) + ',' + stride + ',' + offset,
  1027. true
  1028. )
  1029. }
  1030. viewport = function (x, y, width, height) {
  1031. WebGLRenderingContext.GBridge.callNative(
  1032. this._canvas.id,
  1033. GLmethod.viewport + ',' + x + ',' + y + ',' + width + ',' + height,
  1034. true
  1035. )
  1036. }
  1037. }