@luma.gl/webgl 9.0.0-beta.4 → 9.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (288) hide show
  1. package/dist/adapter/converters/device-parameters.d.ts.map +1 -1
  2. package/dist/adapter/converters/device-parameters.js +297 -155
  3. package/dist/adapter/converters/sampler-parameters.d.ts +0 -4
  4. package/dist/adapter/converters/sampler-parameters.d.ts.map +1 -1
  5. package/dist/adapter/converters/sampler-parameters.js +73 -67
  6. package/dist/adapter/converters/shader-formats.d.ts.map +1 -1
  7. package/dist/adapter/converters/shader-formats.js +53 -46
  8. package/dist/adapter/converters/texture-formats.d.ts +13 -19
  9. package/dist/adapter/converters/texture-formats.d.ts.map +1 -1
  10. package/dist/adapter/converters/texture-formats.js +474 -879
  11. package/dist/adapter/converters/vertex-formats.d.ts.map +1 -1
  12. package/dist/adapter/converters/vertex-formats.js +53 -61
  13. package/dist/adapter/device-helpers/device-features.d.ts +2 -5
  14. package/dist/adapter/device-helpers/device-features.d.ts.map +1 -1
  15. package/dist/adapter/device-helpers/device-features.js +56 -87
  16. package/dist/adapter/device-helpers/device-limits.d.ts +2 -4
  17. package/dist/adapter/device-helpers/device-limits.d.ts.map +1 -1
  18. package/dist/adapter/device-helpers/device-limits.js +88 -83
  19. package/dist/adapter/device-helpers/get-device-info.d.ts +1 -1
  20. package/dist/adapter/device-helpers/get-device-info.d.ts.map +1 -1
  21. package/dist/adapter/device-helpers/get-device-info.js +79 -63
  22. package/dist/adapter/device-helpers/webgl-device-features.d.ts +19 -0
  23. package/dist/adapter/device-helpers/webgl-device-features.d.ts.map +1 -0
  24. package/dist/adapter/device-helpers/webgl-device-features.js +86 -0
  25. package/dist/adapter/device-helpers/webgl-device-info.d.ts +5 -0
  26. package/dist/adapter/device-helpers/webgl-device-info.d.ts.map +1 -0
  27. package/dist/adapter/device-helpers/webgl-device-info.js +90 -0
  28. package/dist/adapter/device-helpers/webgl-device-limits.d.ts +35 -0
  29. package/dist/adapter/device-helpers/webgl-device-limits.d.ts.map +1 -0
  30. package/dist/adapter/device-helpers/webgl-device-limits.js +47 -0
  31. package/dist/adapter/helpers/decode-webgl-types.d.ts.map +1 -1
  32. package/dist/adapter/helpers/decode-webgl-types.js +88 -76
  33. package/dist/adapter/helpers/get-shader-layout.d.ts +1 -1
  34. package/dist/adapter/helpers/get-shader-layout.d.ts.map +1 -1
  35. package/dist/adapter/helpers/get-shader-layout.js +261 -225
  36. package/dist/adapter/helpers/parse-shader-compiler-log.d.ts.map +1 -1
  37. package/dist/adapter/helpers/parse-shader-compiler-log.js +47 -37
  38. package/dist/adapter/helpers/set-uniform.d.ts +1 -1
  39. package/dist/adapter/helpers/set-uniform.d.ts.map +1 -1
  40. package/dist/adapter/helpers/set-uniform.js +68 -82
  41. package/dist/adapter/helpers/webgl-topology-utils.d.ts.map +1 -1
  42. package/dist/adapter/helpers/webgl-topology-utils.js +78 -93
  43. package/dist/adapter/objects/constants-to-keys.d.ts +1 -1
  44. package/dist/adapter/objects/constants-to-keys.d.ts.map +1 -1
  45. package/dist/adapter/objects/constants-to-keys.js +19 -12
  46. package/dist/adapter/objects/webgl-renderbuffer.d.ts +2 -2
  47. package/dist/adapter/objects/webgl-renderbuffer.d.ts.map +1 -1
  48. package/dist/adapter/objects/webgl-renderbuffer.js +86 -77
  49. package/dist/adapter/objects/webgl-resource.d.ts +3 -25
  50. package/dist/adapter/objects/webgl-resource.d.ts.map +1 -1
  51. package/dist/adapter/objects/webgl-resource.js +102 -146
  52. package/dist/adapter/resources/webgl-buffer.d.ts +3 -4
  53. package/dist/adapter/resources/webgl-buffer.d.ts.map +1 -1
  54. package/dist/adapter/resources/webgl-buffer.js +161 -119
  55. package/dist/adapter/resources/webgl-command-buffer.d.ts +1 -1
  56. package/dist/adapter/resources/webgl-command-buffer.d.ts.map +1 -1
  57. package/dist/adapter/resources/webgl-command-buffer.js +266 -168
  58. package/dist/adapter/resources/webgl-command-encoder.d.ts +8 -3
  59. package/dist/adapter/resources/webgl-command-encoder.d.ts.map +1 -1
  60. package/dist/adapter/resources/webgl-command-encoder.js +33 -39
  61. package/dist/adapter/resources/webgl-external-texture.js +93 -1
  62. package/dist/adapter/resources/webgl-framebuffer.d.ts +8 -10
  63. package/dist/adapter/resources/webgl-framebuffer.d.ts.map +1 -1
  64. package/dist/adapter/resources/webgl-framebuffer.js +167 -137
  65. package/dist/adapter/resources/webgl-query-set.d.ts +44 -0
  66. package/dist/adapter/resources/webgl-query-set.d.ts.map +1 -0
  67. package/dist/adapter/resources/webgl-query-set.js +136 -0
  68. package/dist/adapter/resources/webgl-render-pass.d.ts +3 -1
  69. package/dist/adapter/resources/webgl-render-pass.d.ts.map +1 -1
  70. package/dist/adapter/resources/webgl-render-pass.js +124 -90
  71. package/dist/adapter/resources/webgl-render-pipeline.d.ts +15 -6
  72. package/dist/adapter/resources/webgl-render-pipeline.d.ts.map +1 -1
  73. package/dist/adapter/resources/webgl-render-pipeline.js +356 -221
  74. package/dist/adapter/resources/webgl-sampler.d.ts +1 -3
  75. package/dist/adapter/resources/webgl-sampler.d.ts.map +1 -1
  76. package/dist/adapter/resources/webgl-sampler.js +43 -33
  77. package/dist/adapter/resources/webgl-shader.d.ts +12 -2
  78. package/dist/adapter/resources/webgl-shader.d.ts.map +1 -1
  79. package/dist/adapter/resources/webgl-shader.js +114 -47
  80. package/dist/adapter/resources/webgl-texture-view.d.ts +14 -0
  81. package/dist/adapter/resources/webgl-texture-view.d.ts.map +1 -0
  82. package/dist/adapter/resources/webgl-texture-view.js +18 -0
  83. package/dist/adapter/resources/webgl-texture.d.ts +6 -9
  84. package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
  85. package/dist/adapter/resources/webgl-texture.js +615 -695
  86. package/dist/adapter/resources/webgl-transform-feedback.d.ts +2 -2
  87. package/dist/adapter/resources/webgl-transform-feedback.d.ts.map +1 -1
  88. package/dist/adapter/resources/webgl-transform-feedback.js +141 -143
  89. package/dist/adapter/resources/webgl-vertex-array.d.ts +3 -3
  90. package/dist/adapter/resources/webgl-vertex-array.d.ts.map +1 -1
  91. package/dist/adapter/resources/webgl-vertex-array.js +229 -157
  92. package/dist/adapter/webgl-canvas-context.d.ts +2 -2
  93. package/dist/adapter/webgl-canvas-context.d.ts.map +1 -1
  94. package/dist/adapter/webgl-canvas-context.js +58 -36
  95. package/dist/adapter/webgl-device.d.ts +34 -40
  96. package/dist/adapter/webgl-device.d.ts.map +1 -1
  97. package/dist/adapter/webgl-device.js +418 -363
  98. package/dist/classic/accessor.d.ts.map +1 -1
  99. package/dist/classic/accessor.js +132 -101
  100. package/dist/classic/clear.d.ts +2 -2
  101. package/dist/classic/clear.d.ts.map +1 -1
  102. package/dist/classic/clear.js +73 -72
  103. package/dist/classic/copy-and-blit.d.ts +1 -1
  104. package/dist/classic/copy-and-blit.d.ts.map +1 -1
  105. package/dist/classic/copy-and-blit.js +175 -175
  106. package/dist/classic/format-utils.d.ts +2 -2
  107. package/dist/classic/format-utils.d.ts.map +1 -1
  108. package/dist/classic/format-utils.js +39 -32
  109. package/dist/classic/typed-array-utils.d.ts.map +1 -1
  110. package/dist/classic/typed-array-utils.js +96 -81
  111. package/dist/context/context/context-data.d.ts +14 -0
  112. package/dist/context/context/context-data.d.ts.map +1 -0
  113. package/dist/context/context/context-data.js +33 -0
  114. package/dist/context/context/create-browser-context.d.ts +1 -6
  115. package/dist/context/context/create-browser-context.d.ts.map +1 -1
  116. package/dist/context/context/create-browser-context.js +62 -49
  117. package/dist/context/debug/spector.d.ts.map +1 -1
  118. package/dist/context/debug/spector.js +55 -50
  119. package/dist/context/debug/webgl-developer-tools.d.ts +1 -2
  120. package/dist/context/debug/webgl-developer-tools.d.ts.map +1 -1
  121. package/dist/context/debug/webgl-developer-tools.js +104 -77
  122. package/dist/context/helpers/create-browser-context.d.ts +35 -0
  123. package/dist/context/helpers/create-browser-context.d.ts.map +1 -0
  124. package/dist/context/helpers/create-browser-context.js +67 -0
  125. package/dist/context/{polyfill/context-data.d.ts → helpers/webgl-context-data.d.ts} +2 -2
  126. package/dist/context/helpers/webgl-context-data.d.ts.map +1 -0
  127. package/dist/context/helpers/webgl-context-data.js +21 -0
  128. package/dist/context/helpers/webgl-extensions.d.ts +4 -0
  129. package/dist/context/helpers/webgl-extensions.d.ts.map +1 -0
  130. package/dist/context/helpers/webgl-extensions.js +10 -0
  131. package/dist/context/parameters/unified-parameter-api.d.ts +3 -3
  132. package/dist/context/parameters/unified-parameter-api.d.ts.map +1 -1
  133. package/dist/context/parameters/unified-parameter-api.js +94 -44
  134. package/dist/context/parameters/webgl-parameter-tables.d.ts +120 -99
  135. package/dist/context/parameters/webgl-parameter-tables.d.ts.map +1 -1
  136. package/dist/context/parameters/webgl-parameter-tables.js +469 -404
  137. package/dist/context/state-tracker/deep-array-equal.d.ts.map +1 -1
  138. package/dist/context/state-tracker/deep-array-equal.js +19 -14
  139. package/dist/context/state-tracker/track-context-state.d.ts +4 -4
  140. package/dist/context/state-tracker/track-context-state.d.ts.map +1 -1
  141. package/dist/context/state-tracker/track-context-state.js +188 -123
  142. package/dist/context/state-tracker/with-parameters.d.ts +2 -2
  143. package/dist/context/state-tracker/with-parameters.d.ts.map +1 -1
  144. package/dist/context/state-tracker/with-parameters.js +43 -26
  145. package/dist/dist.dev.js +3135 -4142
  146. package/dist/index.cjs +1766 -2717
  147. package/dist/index.cjs.map +7 -0
  148. package/dist/index.d.ts +25 -28
  149. package/dist/index.d.ts.map +1 -1
  150. package/dist/index.js +16 -5
  151. package/dist/types.d.ts.map +1 -1
  152. package/dist/types.js +3 -1
  153. package/dist.min.js +9 -42
  154. package/package.json +11 -15
  155. package/src/adapter/converters/device-parameters.ts +105 -17
  156. package/src/adapter/converters/sampler-parameters.ts +12 -20
  157. package/src/adapter/converters/shader-formats.ts +47 -22
  158. package/src/adapter/converters/texture-formats.ts +138 -185
  159. package/src/adapter/converters/vertex-formats.ts +3 -3
  160. package/src/adapter/device-helpers/webgl-device-features.ts +101 -0
  161. package/src/adapter/device-helpers/{get-device-info.ts → webgl-device-info.ts} +30 -22
  162. package/src/adapter/device-helpers/webgl-device-limits.ts +53 -0
  163. package/src/adapter/helpers/decode-webgl-types.ts +13 -7
  164. package/src/adapter/helpers/get-shader-layout.ts +21 -31
  165. package/src/adapter/helpers/parse-shader-compiler-log.ts +10 -6
  166. package/src/adapter/helpers/set-uniform.ts +3 -4
  167. package/src/adapter/helpers/webgl-topology-utils.ts +10 -3
  168. package/src/adapter/objects/constants-to-keys.ts +3 -2
  169. package/src/adapter/objects/webgl-renderbuffer.ts +38 -16
  170. package/src/adapter/objects/webgl-resource.ts +7 -140
  171. package/src/adapter/resources/webgl-buffer.ts +10 -14
  172. package/src/adapter/resources/webgl-command-buffer.ts +24 -34
  173. package/src/adapter/resources/webgl-command-encoder.ts +14 -11
  174. package/src/adapter/resources/webgl-external-texture.ts +5 -5
  175. package/src/adapter/resources/webgl-framebuffer.ts +38 -34
  176. package/src/adapter/resources/webgl-query-set.ts +171 -0
  177. package/src/adapter/resources/webgl-render-pass.ts +24 -15
  178. package/src/adapter/resources/webgl-render-pipeline.ts +138 -70
  179. package/src/adapter/resources/webgl-sampler.ts +7 -10
  180. package/src/adapter/resources/webgl-shader.ts +65 -11
  181. package/src/adapter/resources/webgl-texture-view.ts +28 -0
  182. package/src/adapter/resources/webgl-texture.ts +38 -105
  183. package/src/adapter/resources/webgl-transform-feedback.ts +16 -22
  184. package/src/adapter/resources/webgl-vertex-array.ts +20 -21
  185. package/src/adapter/webgl-canvas-context.ts +7 -11
  186. package/src/adapter/webgl-device.ts +106 -151
  187. package/src/classic/accessor.ts +5 -4
  188. package/src/classic/clear.ts +25 -20
  189. package/src/classic/copy-and-blit.ts +12 -6
  190. package/src/classic/format-utils.ts +2 -1
  191. package/src/classic/typed-array-utils.ts +3 -7
  192. package/src/context/debug/spector.ts +9 -6
  193. package/src/context/debug/webgl-developer-tools.ts +31 -20
  194. package/src/context/{context → helpers}/create-browser-context.ts +9 -33
  195. package/src/context/{polyfill/context-data.ts → helpers/webgl-context-data.ts} +3 -2
  196. package/src/context/helpers/webgl-extensions.ts +17 -0
  197. package/src/context/parameters/unified-parameter-api.ts +5 -4
  198. package/src/context/parameters/webgl-parameter-tables.ts +118 -90
  199. package/src/context/state-tracker/deep-array-equal.ts +2 -1
  200. package/src/context/state-tracker/track-context-state.ts +29 -23
  201. package/src/context/state-tracker/with-parameters.ts +7 -2
  202. package/src/index.ts +4 -18
  203. package/src/types.ts +2 -1
  204. package/dist/adapter/converters/device-parameters.js.map +0 -1
  205. package/dist/adapter/converters/sampler-parameters.js.map +0 -1
  206. package/dist/adapter/converters/shader-formats.js.map +0 -1
  207. package/dist/adapter/converters/texture-formats.js.map +0 -1
  208. package/dist/adapter/converters/vertex-formats.js.map +0 -1
  209. package/dist/adapter/device-helpers/device-features.js.map +0 -1
  210. package/dist/adapter/device-helpers/device-limits.js.map +0 -1
  211. package/dist/adapter/device-helpers/get-device-info.js.map +0 -1
  212. package/dist/adapter/device-helpers/is-old-ie.d.ts +0 -2
  213. package/dist/adapter/device-helpers/is-old-ie.d.ts.map +0 -1
  214. package/dist/adapter/device-helpers/is-old-ie.js +0 -9
  215. package/dist/adapter/device-helpers/is-old-ie.js.map +0 -1
  216. package/dist/adapter/helpers/decode-webgl-types.js.map +0 -1
  217. package/dist/adapter/helpers/get-shader-layout.js.map +0 -1
  218. package/dist/adapter/helpers/parse-shader-compiler-log.js.map +0 -1
  219. package/dist/adapter/helpers/set-uniform.js.map +0 -1
  220. package/dist/adapter/helpers/webgl-topology-utils.js.map +0 -1
  221. package/dist/adapter/objects/constants-to-keys.js.map +0 -1
  222. package/dist/adapter/objects/webgl-renderbuffer.js.map +0 -1
  223. package/dist/adapter/objects/webgl-resource.js.map +0 -1
  224. package/dist/adapter/resources/webgl-buffer.js.map +0 -1
  225. package/dist/adapter/resources/webgl-command-buffer.js.map +0 -1
  226. package/dist/adapter/resources/webgl-command-encoder.js.map +0 -1
  227. package/dist/adapter/resources/webgl-external-texture.js.map +0 -1
  228. package/dist/adapter/resources/webgl-framebuffer.js.map +0 -1
  229. package/dist/adapter/resources/webgl-render-pass.js.map +0 -1
  230. package/dist/adapter/resources/webgl-render-pipeline.js.map +0 -1
  231. package/dist/adapter/resources/webgl-sampler.js.map +0 -1
  232. package/dist/adapter/resources/webgl-shader.js.map +0 -1
  233. package/dist/adapter/resources/webgl-texture.js.map +0 -1
  234. package/dist/adapter/resources/webgl-transform-feedback.js.map +0 -1
  235. package/dist/adapter/resources/webgl-vertex-array.js.map +0 -1
  236. package/dist/adapter/webgl-canvas-context.js.map +0 -1
  237. package/dist/adapter/webgl-device.js.map +0 -1
  238. package/dist/classic/accessor.js.map +0 -1
  239. package/dist/classic/clear.js.map +0 -1
  240. package/dist/classic/copy-and-blit.js.map +0 -1
  241. package/dist/classic/format-utils.js.map +0 -1
  242. package/dist/classic/typed-array-utils.js.map +0 -1
  243. package/dist/context/context/create-browser-context.js.map +0 -1
  244. package/dist/context/context/create-headless-context.d.ts +0 -9
  245. package/dist/context/context/create-headless-context.d.ts.map +0 -1
  246. package/dist/context/context/create-headless-context.js +0 -42
  247. package/dist/context/context/create-headless-context.js.map +0 -1
  248. package/dist/context/context/webgl-checks.d.ts +0 -13
  249. package/dist/context/context/webgl-checks.d.ts.map +0 -1
  250. package/dist/context/context/webgl-checks.js +0 -31
  251. package/dist/context/context/webgl-checks.js.map +0 -1
  252. package/dist/context/debug/spector.js.map +0 -1
  253. package/dist/context/debug/webgl-developer-tools.js.map +0 -1
  254. package/dist/context/parameters/unified-parameter-api.js.map +0 -1
  255. package/dist/context/parameters/webgl-parameter-tables.js.map +0 -1
  256. package/dist/context/polyfill/context-data.d.ts.map +0 -1
  257. package/dist/context/polyfill/context-data.js +0 -12
  258. package/dist/context/polyfill/context-data.js.map +0 -1
  259. package/dist/context/polyfill/get-parameter-polyfill.d.ts +0 -2
  260. package/dist/context/polyfill/get-parameter-polyfill.d.ts.map +0 -1
  261. package/dist/context/polyfill/get-parameter-polyfill.js +0 -85
  262. package/dist/context/polyfill/get-parameter-polyfill.js.map +0 -1
  263. package/dist/context/polyfill/polyfill-context.d.ts +0 -5
  264. package/dist/context/polyfill/polyfill-context.d.ts.map +0 -1
  265. package/dist/context/polyfill/polyfill-context.js +0 -87
  266. package/dist/context/polyfill/polyfill-context.js.map +0 -1
  267. package/dist/context/polyfill/polyfill-table.d.ts +0 -48
  268. package/dist/context/polyfill/polyfill-table.d.ts.map +0 -1
  269. package/dist/context/polyfill/polyfill-table.js +0 -137
  270. package/dist/context/polyfill/polyfill-table.js.map +0 -1
  271. package/dist/context/polyfill/polyfill-vertex-array-object.d.ts +0 -2
  272. package/dist/context/polyfill/polyfill-vertex-array-object.d.ts.map +0 -1
  273. package/dist/context/polyfill/polyfill-vertex-array-object.js +0 -265
  274. package/dist/context/polyfill/polyfill-vertex-array-object.js.map +0 -1
  275. package/dist/context/state-tracker/deep-array-equal.js.map +0 -1
  276. package/dist/context/state-tracker/track-context-state.js.map +0 -1
  277. package/dist/context/state-tracker/with-parameters.js.map +0 -1
  278. package/dist/index.js.map +0 -1
  279. package/dist/types.js.map +0 -1
  280. package/src/adapter/device-helpers/device-features.ts +0 -161
  281. package/src/adapter/device-helpers/device-limits.ts +0 -155
  282. package/src/adapter/device-helpers/is-old-ie.ts +0 -14
  283. package/src/context/context/create-headless-context.ts +0 -51
  284. package/src/context/context/webgl-checks.ts +0 -51
  285. package/src/context/polyfill/get-parameter-polyfill.ts +0 -122
  286. package/src/context/polyfill/polyfill-context.ts +0 -104
  287. package/src/context/polyfill/polyfill-table.ts +0 -167
  288. package/src/context/polyfill/polyfill-vertex-array-object.ts +0 -365
@@ -1,713 +1,633 @@
1
- import { Texture, log, assert, isPowerOfTwo, loadImage, isObjectEmpty } from '@luma.gl/core';
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { Texture, log, assert, loadImage, isObjectEmpty } from '@luma.gl/core';
5
+ import { GL } from '@luma.gl/constants';
2
6
  import { withGLParameters } from "../../context/state-tracker/with-parameters.js";
3
7
  import { convertTextureFormatToGL, getWebGLTextureParameters, getTextureFormatBytesPerPixel } from "../converters/texture-formats.js";
4
- import { convertSamplerParametersToWebGL, updateSamplerParametersForNPOT } from "../converters/sampler-parameters.js";
8
+ import { convertSamplerParametersToWebGL } from "../converters/sampler-parameters.js";
5
9
  import { WEBGLBuffer } from "./webgl-buffer.js";
6
10
  import { WEBGLSampler } from "./webgl-sampler.js";
11
+ import { WEBGLTextureView } from "./webgl-texture-view.js";
7
12
  export const DEFAULT_WEBGL_TEXTURE_PROPS = {
8
- parameters: {},
9
- pixelStore: {},
10
- pixels: null,
11
- border: 0,
12
- dataFormat: undefined,
13
- textureUnit: undefined,
14
- target: undefined
13
+ // deprecated
14
+ parameters: {},
15
+ pixelStore: {},
16
+ pixels: null,
17
+ border: 0,
18
+ dataFormat: undefined,
19
+ textureUnit: undefined,
20
+ target: undefined
15
21
  };
22
+ // Polyfill
16
23
  export class WEBGLTexture extends Texture {
17
- constructor(device, props) {
18
- var _this$props;
19
- super(device, {
20
- ...DEFAULT_WEBGL_TEXTURE_PROPS,
21
- format: 'rgba8unorm',
22
- ...props
23
- });
24
- this.MAX_ATTRIBUTES = void 0;
25
- this.device = void 0;
26
- this.gl = void 0;
27
- this.gl2 = void 0;
28
- this.handle = void 0;
29
- this.sampler = undefined;
30
- this.glFormat = undefined;
31
- this.type = undefined;
32
- this.dataFormat = undefined;
33
- this.mipmaps = undefined;
34
- this.target = void 0;
35
- this.textureUnit = undefined;
36
- this.loaded = false;
37
- this._video = void 0;
38
- this.device = device;
39
- this.gl = this.device.gl;
40
- this.gl2 = this.device.gl2;
41
- this.handle = this.props.handle || this.gl.createTexture();
42
- this.device.setSpectorMetadata(this.handle, {
43
- ...this.props,
44
- data: typeof this.props.data
45
- });
46
- this.glFormat = 6408;
47
- this.target = getWebGLTextureTarget(this.props);
48
- this.loaded = false;
49
- if (typeof ((_this$props = this.props) === null || _this$props === void 0 ? void 0 : _this$props.data) === 'string') {
50
- Object.assign(this.props, {
51
- data: loadImage(this.props.data)
52
- });
53
- }
54
- this.initialize(this.props);
55
- Object.seal(this);
56
- }
57
- destroy() {
58
- if (this.handle) {
59
- this.gl.deleteTexture(this.handle);
60
- this.removeStats();
61
- this.trackDeallocatedMemory('Texture');
62
- this.destroyed = true;
63
- }
64
- }
65
- toString() {
66
- return `Texture(${this.id},${this.width}x${this.height})`;
67
- }
68
- initialize() {
69
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
70
- if (this.props.dimension === 'cube') {
71
- return this.initializeCube(props);
72
- }
73
- let data = props.data;
74
- if (data instanceof Promise) {
75
- data.then(resolvedImageData => this.initialize(Object.assign({}, props, {
76
- pixels: resolvedImageData,
77
- data: resolvedImageData
78
- })));
79
- return this;
80
- }
81
- const isVideo = typeof HTMLVideoElement !== 'undefined' && data instanceof HTMLVideoElement;
82
- if (isVideo && data.readyState < HTMLVideoElement.HAVE_METADATA) {
83
- this._video = null;
84
- data.addEventListener('loadeddata', () => this.initialize(props));
85
- return this;
86
- }
87
- const {
88
- parameters = {}
89
- } = props;
90
- const {
91
- pixels = null,
92
- pixelStore = {},
93
- textureUnit = undefined
94
- } = props;
95
- if (!data) {
96
- data = pixels;
97
- }
98
- let {
99
- width,
100
- height,
101
- dataFormat,
102
- type,
103
- compressed = false,
104
- mipmaps = true
105
- } = props;
106
- const {
107
- depth = 0
108
- } = props;
109
- const glFormat = convertTextureFormatToGL(props.format, this.device.isWebGL2);
110
- ({
111
- width,
112
- height,
113
- compressed,
114
- dataFormat,
115
- type
116
- } = this._deduceParameters({
117
- format: props.format,
118
- type,
119
- dataFormat,
120
- compressed,
121
- data,
122
- width,
123
- height
124
- }));
125
- this.width = width;
126
- this.height = height;
127
- this.glFormat = glFormat;
128
- this.type = type;
129
- this.dataFormat = dataFormat;
130
- this.textureUnit = textureUnit;
131
- if (Number.isFinite(this.textureUnit)) {
132
- this.gl.activeTexture(33984 + this.textureUnit);
133
- this.gl.bindTexture(this.target, this.handle);
134
- }
135
- if (mipmaps && this.device.isWebGL1 && isNPOT(this.width, this.height)) {
136
- log.warn(`texture: ${this} is Non-Power-Of-Two, disabling mipmaps`)();
137
- mipmaps = false;
138
- }
139
- this.mipmaps = mipmaps;
140
- this.setImageData({
141
- data,
142
- width,
143
- height,
144
- depth,
145
- format: glFormat,
146
- type,
147
- dataFormat,
148
- parameters: pixelStore,
149
- compressed
150
- });
151
- this.setSampler(props.sampler);
152
- this._setSamplerParameters(parameters);
153
- if (mipmaps) {
154
- this.generateMipmap();
24
+ // TODO - remove?
25
+ static FACES = [
26
+ GL.TEXTURE_CUBE_MAP_POSITIVE_X,
27
+ GL.TEXTURE_CUBE_MAP_NEGATIVE_X,
28
+ GL.TEXTURE_CUBE_MAP_POSITIVE_Y,
29
+ GL.TEXTURE_CUBE_MAP_NEGATIVE_Y,
30
+ GL.TEXTURE_CUBE_MAP_POSITIVE_Z,
31
+ GL.TEXTURE_CUBE_MAP_NEGATIVE_Z
32
+ ];
33
+ MAX_ATTRIBUTES;
34
+ device;
35
+ gl;
36
+ handle;
37
+ // (TODO - currently unused in WebGL, but WebGL 2 does support sampler objects) */
38
+ sampler = undefined;
39
+ view = undefined;
40
+ // data;
41
+ glFormat = undefined;
42
+ type = undefined;
43
+ dataFormat = undefined;
44
+ mipmaps = undefined;
45
+ /**
46
+ * @note `target` cannot be modified by bind:
47
+ * textures are special because when you first bind them to a target,
48
+ * they get special information. When you first bind a texture as a
49
+ * GL_TEXTURE_2D, you are saying that this texture is a 2D texture.
50
+ * And it will always be a 2D texture; this state cannot be changed ever.
51
+ * A texture that was first bound as a GL_TEXTURE_2D, must always be bound as a GL_TEXTURE_2D;
52
+ * attempting to bind it as GL_TEXTURE_3D will give rise to a run-time error
53
+ * */
54
+ target;
55
+ textureUnit = undefined;
56
+ /**
57
+ * Program.draw() checks the loaded flag of all textures to avoid
58
+ * Textures that are still loading from promises
59
+ * Set to true as soon as texture has been initialized with valid data
60
+ */
61
+ loaded = false;
62
+ _video;
63
+ constructor(device, props) {
64
+ super(device, { ...DEFAULT_WEBGL_TEXTURE_PROPS, format: 'rgba8unorm', ...props });
65
+ this.device = device;
66
+ this.gl = this.device.gl;
67
+ this.handle = this.props.handle || this.gl.createTexture();
68
+ this.device.setSpectorMetadata(this.handle, { ...this.props, data: typeof this.props.data }); // {name: this.props.id};
69
+ this.glFormat = GL.RGBA;
70
+ this.target = getWebGLTextureTarget(this.props);
71
+ // Program.draw() checks the loaded flag of all textures
72
+ this.loaded = false;
73
+ // Signature: new Texture2D(gl, {data: url})
74
+ if (typeof this.props?.data === 'string') {
75
+ Object.assign(this.props, { data: loadImage(this.props.data) });
76
+ }
77
+ this.initialize(this.props);
78
+ Object.seal(this);
79
+ }
80
+ destroy() {
81
+ if (this.handle) {
82
+ this.gl.deleteTexture(this.handle);
83
+ this.removeStats();
84
+ this.trackDeallocatedMemory('Texture');
85
+ // this.handle = null;
86
+ this.destroyed = true;
87
+ }
155
88
  }
156
- if (isVideo) {
157
- this._video = {
158
- video: data,
159
- parameters,
160
- lastTime: data.readyState >= HTMLVideoElement.HAVE_CURRENT_DATA ? data.currentTime : -1
161
- };
89
+ toString() {
90
+ return `Texture(${this.id},${this.width}x${this.height})`;
162
91
  }
163
- return this;
164
- }
165
- initializeCube(props) {
166
- const {
167
- mipmaps = true,
168
- parameters = {}
169
- } = props;
170
- this.setCubeMapImageData(props).then(() => {
171
- this.loaded = true;
172
- if (mipmaps) {
173
- this.generateMipmap(props);
174
- }
175
- this.setSampler(props.sampler);
176
- this._setSamplerParameters(parameters);
177
- });
178
- return this;
179
- }
180
- setSampler() {
181
- let sampler = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
182
- let samplerProps;
183
- if (sampler instanceof WEBGLSampler) {
184
- this.sampler = sampler;
185
- samplerProps = sampler.props;
186
- } else {
187
- this.sampler = new WEBGLSampler(this.device, sampler);
188
- samplerProps = sampler;
92
+ // eslint-disable-next-line max-statements
93
+ initialize(props = {}) {
94
+ // Cube textures
95
+ if (this.props.dimension === 'cube') {
96
+ return this.initializeCube(props);
97
+ }
98
+ let data = props.data;
99
+ if (data instanceof Promise) {
100
+ data.then(resolvedImageData => this.initialize(Object.assign({}, props, {
101
+ pixels: resolvedImageData,
102
+ data: resolvedImageData
103
+ })));
104
+ return this;
105
+ }
106
+ const isVideo = typeof HTMLVideoElement !== 'undefined' && data instanceof HTMLVideoElement;
107
+ // @ts-expect-error
108
+ if (isVideo && data.readyState < HTMLVideoElement.HAVE_METADATA) {
109
+ this._video = null; // Declare member before the object is sealed
110
+ // @ts-expect-error
111
+ data.addEventListener('loadeddata', () => this.initialize(props));
112
+ return this;
113
+ }
114
+ const { parameters = {} } = props;
115
+ const { pixels = null, pixelStore = {}, textureUnit = undefined, mipmaps = true } = props;
116
+ // pixels variable is for API compatibility purpose
117
+ if (!data) {
118
+ // TODO - This looks backwards? Commenting out for now until we decide
119
+ // which prop to use
120
+ // log.deprecated('data', 'pixels')();
121
+ data = pixels;
122
+ }
123
+ let { width, height, dataFormat, type, compressed = false } = props;
124
+ const { depth = 0 } = props;
125
+ const glFormat = convertTextureFormatToGL(props.format);
126
+ // Deduce width and height
127
+ ({ width, height, compressed, dataFormat, type } = this._deduceParameters({
128
+ format: props.format,
129
+ type,
130
+ dataFormat,
131
+ compressed,
132
+ data,
133
+ width,
134
+ height
135
+ }));
136
+ // Store opts for accessors
137
+ this.width = width;
138
+ this.height = height;
139
+ // this.depth = depth;
140
+ this.glFormat = glFormat;
141
+ this.type = type;
142
+ this.dataFormat = dataFormat;
143
+ this.textureUnit = textureUnit;
144
+ if (Number.isFinite(this.textureUnit)) {
145
+ this.gl.activeTexture(GL.TEXTURE0 + this.textureUnit);
146
+ this.gl.bindTexture(this.target, this.handle);
147
+ }
148
+ this.mipmaps = mipmaps;
149
+ this.setImageData({
150
+ data,
151
+ width,
152
+ height,
153
+ depth,
154
+ format: glFormat,
155
+ type,
156
+ dataFormat,
157
+ // @ts-expect-error
158
+ parameters: pixelStore,
159
+ compressed
160
+ });
161
+ // Set texture sampler parameters
162
+ this.setSampler(props.sampler);
163
+ this._setSamplerParameters(parameters);
164
+ // @ts-ignore
165
+ this.view = new WEBGLTextureView(this.device, { ...this.props, texture: this });
166
+ if (mipmaps) {
167
+ this.generateMipmap();
168
+ }
169
+ if (isVideo) {
170
+ this._video = {
171
+ video: data,
172
+ parameters,
173
+ // @ts-expect-error
174
+ lastTime: data.readyState >= HTMLVideoElement.HAVE_CURRENT_DATA ? data.currentTime : -1
175
+ };
176
+ }
177
+ return this;
178
+ }
179
+ initializeCube(props) {
180
+ const { mipmaps = true, parameters = {} } = props;
181
+ // Store props for accessors
182
+ // this.props = props;
183
+ // @ts-expect-error
184
+ this.setCubeMapImageData(props).then(() => {
185
+ this.loaded = true;
186
+ // TODO - should genMipmap() be called on the cubemap or on the faces?
187
+ // TODO - without generateMipmap() cube textures do not work at all!!! Why?
188
+ if (mipmaps) {
189
+ this.generateMipmap(props);
190
+ }
191
+ this.setSampler(props.sampler);
192
+ this._setSamplerParameters(parameters);
193
+ });
194
+ return this;
195
+ }
196
+ setSampler(sampler = {}) {
197
+ let samplerProps;
198
+ if (sampler instanceof WEBGLSampler) {
199
+ this.sampler = sampler;
200
+ samplerProps = sampler.props;
201
+ }
202
+ else {
203
+ this.sampler = new WEBGLSampler(this.device, sampler);
204
+ samplerProps = sampler;
205
+ }
206
+ const parameters = convertSamplerParametersToWebGL(samplerProps);
207
+ this._setSamplerParameters(parameters);
208
+ return this;
209
+ }
210
+ /**
211
+ * If size has changed, reinitializes with current format
212
+ * @note note clears image and mipmaps
213
+ */
214
+ resize(options) {
215
+ const { height, width, mipmaps = false } = options;
216
+ if (width !== this.width || height !== this.height) {
217
+ return this.initialize({
218
+ width,
219
+ height,
220
+ format: this.format,
221
+ type: this.type,
222
+ dataFormat: this.dataFormat,
223
+ mipmaps
224
+ });
225
+ }
226
+ return this;
227
+ }
228
+ /** Update external texture (video frame) */
229
+ update() {
230
+ if (this._video) {
231
+ const { video, parameters, lastTime } = this._video;
232
+ // @ts-expect-error
233
+ if (lastTime === video.currentTime || video.readyState < HTMLVideoElement.HAVE_CURRENT_DATA) {
234
+ return;
235
+ }
236
+ this.setSubImageData({
237
+ data: video,
238
+ parameters
239
+ });
240
+ if (this.mipmaps) {
241
+ this.generateMipmap();
242
+ }
243
+ this._video.lastTime = video.currentTime;
244
+ }
189
245
  }
190
- const parameters = convertSamplerParametersToWebGL(samplerProps);
191
- this._setSamplerParameters(parameters);
192
- return this;
193
- }
194
- resize(options) {
195
- const {
196
- height,
197
- width,
198
- mipmaps = false
199
- } = options;
200
- if (width !== this.width || height !== this.height) {
201
- return this.initialize({
202
- width,
203
- height,
204
- format: this.format,
205
- type: this.type,
206
- dataFormat: this.dataFormat,
207
- mipmaps
208
- });
246
+ // Call to regenerate mipmaps after modifying texture(s)
247
+ generateMipmap(params = {}) {
248
+ this.mipmaps = true;
249
+ this.gl.bindTexture(this.target, this.handle);
250
+ withGLParameters(this.gl, params, () => {
251
+ this.gl.generateMipmap(this.target);
252
+ });
253
+ this.gl.bindTexture(this.target, null);
254
+ return this;
255
+ }
256
+ /*
257
+ * Allocates storage
258
+ * @param {*} pixels -
259
+ * null - create empty texture of specified format
260
+ * Typed array - init from image data in typed array
261
+ * Buffer|WebGLBuffer - (WEBGL2) init from image data in WebGLBuffer
262
+ * HTMLImageElement|Image - Inits with content of image. Auto width/height
263
+ * HTMLCanvasElement - Inits with contents of canvas. Auto width/height
264
+ * HTMLVideoElement - Creates video texture. Auto width/height
265
+ *
266
+ * @param width -
267
+ * @param height -
268
+ * @param mipMapLevel -
269
+ * @param {GLenum} format - format of image data.
270
+ * @param {GLenum} type
271
+ * - format of array (autodetect from type) or
272
+ * - (WEBGL2) format of buffer
273
+ * @param {Number} offset - (WEBGL2) offset from start of buffer
274
+ * @parameters - temporary settings to be applied, can be used to supply pixel store settings.
275
+ */
276
+ // eslint-disable-next-line max-statements, complexity
277
+ setImageData(options) {
278
+ if (this.props.dimension === '3d' || this.props.dimension === '2d-array') {
279
+ return this.setImageData3D(options);
280
+ }
281
+ this.trackDeallocatedMemory('Texture');
282
+ const { target = this.target, pixels = null, level = 0, glFormat = this.glFormat, offset = 0, parameters = {} } = options;
283
+ let { data = null, type = this.type, width = this.width, height = this.height, dataFormat = this.dataFormat, compressed = false } = options;
284
+ // pixels variable is for API compatibility purpose
285
+ if (!data) {
286
+ data = pixels;
287
+ }
288
+ ({ type, dataFormat, compressed, width, height } = this._deduceParameters({
289
+ format: this.props.format,
290
+ type,
291
+ dataFormat,
292
+ compressed,
293
+ data,
294
+ width,
295
+ height
296
+ }));
297
+ const { gl } = this;
298
+ gl.bindTexture(this.target, this.handle);
299
+ let dataType = null;
300
+ ({ data, dataType } = this._getDataType({ data, compressed }));
301
+ withGLParameters(this.gl, parameters, () => {
302
+ switch (dataType) {
303
+ case 'null':
304
+ gl.texImage2D(target, level, glFormat, width, height, 0 /* border*/, dataFormat, type, data);
305
+ break;
306
+ case 'typed-array':
307
+ gl.texImage2D(target, level, glFormat, width, height, 0, // border (must be 0)
308
+ dataFormat, type, data, offset);
309
+ break;
310
+ case 'buffer':
311
+ // WebGL2 enables creating textures directly from a WebGL buffer
312
+ this.device.gl.bindBuffer(GL.PIXEL_UNPACK_BUFFER, data.handle || data);
313
+ this.device.gl.texImage2D(target, level, glFormat, width, height, 0 /* border*/, dataFormat, type, offset);
314
+ this.device.gl.bindBuffer(GL.PIXEL_UNPACK_BUFFER, null);
315
+ break;
316
+ case 'browser-object':
317
+ gl.texImage2D(target, level, glFormat, width, height, 0 /* border*/, dataFormat, type, data);
318
+ break;
319
+ case 'compressed':
320
+ for (const [levelIndex, levelData] of data.entries()) {
321
+ gl.compressedTexImage2D(target, levelIndex, levelData.format, levelData.width, levelData.height, 0 /* border, must be 0 */, levelData.data);
322
+ }
323
+ break;
324
+ default:
325
+ assert(false, 'Unknown image data type');
326
+ }
327
+ });
328
+ if (data && data.byteLength) {
329
+ this.trackAllocatedMemory(data.byteLength, 'Texture');
330
+ }
331
+ else {
332
+ const bytesPerPixel = getTextureFormatBytesPerPixel(this.props.format);
333
+ this.trackAllocatedMemory(this.width * this.height * bytesPerPixel, 'Texture');
334
+ }
335
+ this.loaded = true;
336
+ return this;
337
+ }
338
+ /**
339
+ * Redefines an area of an existing texture
340
+ * Note: does not allocate storage
341
+ * Redefines an area of an existing texture
342
+ */
343
+ setSubImageData({ target = this.target, pixels = null, data = null, x = 0, y = 0, width = this.width, height = this.height, level = 0, glFormat = this.glFormat, type = this.type, dataFormat = this.dataFormat, compressed = false, offset = 0, parameters = {} }) {
344
+ ({ type, dataFormat, compressed, width, height } = this._deduceParameters({
345
+ format: this.props.format,
346
+ type,
347
+ dataFormat,
348
+ compressed,
349
+ data,
350
+ width,
351
+ height
352
+ }));
353
+ assert(this.depth === 1, 'texSubImage not supported for 3D textures');
354
+ // pixels variable is for API compatibility purpose
355
+ if (!data) {
356
+ data = pixels;
357
+ }
358
+ // Support ndarrays
359
+ if (data && data.data) {
360
+ const ndarray = data;
361
+ data = ndarray.data;
362
+ width = ndarray.shape[0];
363
+ height = ndarray.shape[1];
364
+ }
365
+ // Support buffers
366
+ if (data instanceof WEBGLBuffer) {
367
+ data = data.handle;
368
+ }
369
+ this.gl.bindTexture(this.target, this.handle);
370
+ withGLParameters(this.gl, parameters, () => {
371
+ // TODO - x,y parameters
372
+ if (compressed) {
373
+ this.gl.compressedTexSubImage2D(target, level, x, y, width, height, glFormat, data);
374
+ }
375
+ else if (data === null) {
376
+ this.gl.texSubImage2D(target, level, x, y, width, height, dataFormat, type, null);
377
+ }
378
+ else if (ArrayBuffer.isView(data)) {
379
+ this.gl.texSubImage2D(target, level, x, y, width, height, dataFormat, type, data, offset);
380
+ }
381
+ else if (typeof WebGLBuffer !== 'undefined' && data instanceof WebGLBuffer) {
382
+ // WebGL2 allows us to create texture directly from a WebGL buffer
383
+ // This texImage2D signature uses currently bound GL.PIXEL_UNPACK_BUFFER
384
+ this.device.gl.bindBuffer(GL.PIXEL_UNPACK_BUFFER, data);
385
+ this.device.gl.texSubImage2D(target, level, x, y, width, height, dataFormat, type, offset);
386
+ this.device.gl.bindBuffer(GL.PIXEL_UNPACK_BUFFER, null);
387
+ }
388
+ else {
389
+ // Assume data is a browser supported object (ImageData, Canvas, ...)
390
+ this.device.gl.texSubImage2D(target, level, x, y, width, height, dataFormat, type, data);
391
+ }
392
+ });
393
+ this.gl.bindTexture(this.target, null);
394
+ }
395
+ /**
396
+ * Defines a two-dimensional texture image or cube-map texture image with
397
+ * pixels from the current framebuffer (rather than from client memory).
398
+ * (gl.copyTexImage2D wrapper)
399
+ *
400
+ * Note that binding a texture into a Framebuffer's color buffer and
401
+ * rendering can be faster.
402
+ */
403
+ copyFramebuffer(opts = {}) {
404
+ log.error('Texture.copyFramebuffer({...}) is no logner supported, use copyToTexture(source, target, opts})')();
405
+ return null;
406
+ }
407
+ getActiveUnit() {
408
+ return this.gl.getParameter(GL.ACTIVE_TEXTURE) - GL.TEXTURE0;
409
+ }
410
+ bind(textureUnit = this.textureUnit) {
411
+ const { gl } = this;
412
+ if (textureUnit !== undefined) {
413
+ this.textureUnit = textureUnit;
414
+ gl.activeTexture(gl.TEXTURE0 + textureUnit);
415
+ }
416
+ gl.bindTexture(this.target, this.handle);
417
+ return textureUnit;
418
+ }
419
+ unbind(textureUnit = this.textureUnit) {
420
+ const { gl } = this;
421
+ if (textureUnit !== undefined) {
422
+ this.textureUnit = textureUnit;
423
+ gl.activeTexture(gl.TEXTURE0 + textureUnit);
424
+ }
425
+ gl.bindTexture(this.target, null);
426
+ return textureUnit;
209
427
  }
210
- return this;
211
- }
212
- update() {
213
- if (this._video) {
214
- const {
215
- video,
216
- parameters,
217
- lastTime
218
- } = this._video;
219
- if (lastTime === video.currentTime || video.readyState < HTMLVideoElement.HAVE_CURRENT_DATA) {
428
+ // PRIVATE METHODS
429
+ _getDataType({ data, compressed = false }) {
430
+ if (compressed) {
431
+ return { data, dataType: 'compressed' };
432
+ }
433
+ if (data === null) {
434
+ return { data, dataType: 'null' };
435
+ }
436
+ if (ArrayBuffer.isView(data)) {
437
+ return { data, dataType: 'typed-array' };
438
+ }
439
+ if (data instanceof WEBGLBuffer) {
440
+ return { data: data.handle, dataType: 'buffer' };
441
+ }
442
+ // Raw WebGL handle (not a luma wrapper)
443
+ if (typeof WebGLBuffer !== 'undefined' && data instanceof WebGLBuffer) {
444
+ return { data, dataType: 'buffer' };
445
+ }
446
+ // Assume data is a browser supported object (ImageData, Canvas, ...)
447
+ return { data, dataType: 'browser-object' };
448
+ }
449
+ // HELPER METHODS
450
+ _deduceParameters(opts) {
451
+ const { format, data } = opts;
452
+ let { width, height, dataFormat, type, compressed } = opts;
453
+ // Deduce format and type from format
454
+ const parameters = getWebGLTextureParameters(format);
455
+ dataFormat = dataFormat || parameters.dataFormat;
456
+ type = type || parameters.type;
457
+ compressed = compressed || parameters.compressed;
458
+ ({ width, height } = this._deduceImageSize(data, width, height));
459
+ return { dataFormat, type, compressed, width, height, format, data };
460
+ }
461
+ // eslint-disable-next-line complexity
462
+ _deduceImageSize(data, width, height) {
463
+ let size;
464
+ if (typeof ImageData !== 'undefined' && data instanceof ImageData) {
465
+ size = { width: data.width, height: data.height };
466
+ }
467
+ else if (typeof HTMLImageElement !== 'undefined' && data instanceof HTMLImageElement) {
468
+ size = { width: data.naturalWidth, height: data.naturalHeight };
469
+ }
470
+ else if (typeof HTMLCanvasElement !== 'undefined' && data instanceof HTMLCanvasElement) {
471
+ size = { width: data.width, height: data.height };
472
+ }
473
+ else if (typeof ImageBitmap !== 'undefined' && data instanceof ImageBitmap) {
474
+ size = { width: data.width, height: data.height };
475
+ }
476
+ else if (typeof HTMLVideoElement !== 'undefined' && data instanceof HTMLVideoElement) {
477
+ size = { width: data.videoWidth, height: data.videoHeight };
478
+ }
479
+ else if (!data) {
480
+ size = { width: width >= 0 ? width : 1, height: height >= 0 ? height : 1 };
481
+ }
482
+ else {
483
+ size = { width, height };
484
+ }
485
+ assert(size, 'Could not deduced texture size');
486
+ assert(width === undefined || size.width === width, 'Deduced texture width does not match supplied width');
487
+ assert(height === undefined || size.height === height, 'Deduced texture height does not match supplied height');
488
+ return size;
489
+ }
490
+ // CUBE MAP METHODS
491
+ /* eslint-disable max-statements, max-len */
492
+ async setCubeMapImageData(options) {
493
+ const { gl } = this;
494
+ const { width, height, pixels, data, format = GL.RGBA, type = GL.UNSIGNED_BYTE } = options;
495
+ const imageDataMap = pixels || data;
496
+ // pixel data (imageDataMap) is an Object from Face to Image or Promise.
497
+ // For example:
498
+ // {
499
+ // GL.TEXTURE_CUBE_MAP_POSITIVE_X : Image-or-Promise,
500
+ // GL.TEXTURE_CUBE_MAP_NEGATIVE_X : Image-or-Promise,
501
+ // ... }
502
+ // To provide multiple level-of-details (LODs) this can be Face to Array
503
+ // of Image or Promise, like this
504
+ // {
505
+ // GL.TEXTURE_CUBE_MAP_POSITIVE_X : [Image-or-Promise-LOD-0, Image-or-Promise-LOD-1],
506
+ // GL.TEXTURE_CUBE_MAP_NEGATIVE_X : [Image-or-Promise-LOD-0, Image-or-Promise-LOD-1],
507
+ // ... }
508
+ const resolvedFaces = await Promise.all(WEBGLTexture.FACES.map(face => {
509
+ const facePixels = imageDataMap[face];
510
+ return Promise.all(Array.isArray(facePixels) ? facePixels : [facePixels]);
511
+ }));
512
+ this.bind();
513
+ WEBGLTexture.FACES.forEach((face, index) => {
514
+ if (resolvedFaces[index].length > 1 && this.props.mipmaps !== false) {
515
+ // If the user provides multiple LODs, then automatic mipmap
516
+ // generation generateMipmap() should be disabled to avoid overwritting them.
517
+ log.warn(`${this.id} has mipmap and multiple LODs.`)();
518
+ }
519
+ resolvedFaces[index].forEach((image, lodLevel) => {
520
+ // TODO: adjust width & height for LOD!
521
+ if (width && height) {
522
+ gl.texImage2D(face, lodLevel, format, width, height, 0 /* border*/, format, type, image);
523
+ }
524
+ else {
525
+ gl.texImage2D(face, lodLevel, format, format, type, image);
526
+ }
527
+ });
528
+ });
529
+ this.unbind();
530
+ }
531
+ /** @todo update this method to accept LODs */
532
+ setImageDataForFace(options) {
533
+ const { face, width, height, pixels, data, format = GL.RGBA, type = GL.UNSIGNED_BYTE
534
+ // generateMipmap = false // TODO
535
+ } = options;
536
+ const { gl } = this;
537
+ const imageData = pixels || data;
538
+ this.bind();
539
+ if (imageData instanceof Promise) {
540
+ imageData.then(resolvedImageData => this.setImageDataForFace(Object.assign({}, options, {
541
+ face,
542
+ data: resolvedImageData,
543
+ pixels: resolvedImageData
544
+ })));
545
+ }
546
+ else if (this.width || this.height) {
547
+ gl.texImage2D(face, 0, format, width, height, 0 /* border*/, format, type, imageData);
548
+ }
549
+ else {
550
+ gl.texImage2D(face, 0, format, format, type, imageData);
551
+ }
552
+ return this;
553
+ }
554
+ /** Image 3D copies from Typed Array or WebGLBuffer */
555
+ setImageData3D(options) {
556
+ const { level = 0, dataFormat, format, type, // = GL.UNSIGNED_BYTE,
557
+ width, height, depth = 1, offset = 0, data, parameters = {} } = options;
558
+ this.trackDeallocatedMemory('Texture');
559
+ this.gl.bindTexture(this.target, this.handle);
560
+ const webglTextureFormat = getWebGLTextureParameters(format);
561
+ withGLParameters(this.gl, parameters, () => {
562
+ if (ArrayBuffer.isView(data)) {
563
+ this.gl.texImage3D(this.target, level, webglTextureFormat.format, width, height, depth, 0 /* border, must be 0 */, webglTextureFormat.dataFormat, webglTextureFormat.type, // dataType: getWebGL,
564
+ data);
565
+ }
566
+ if (data instanceof WEBGLBuffer) {
567
+ this.gl.bindBuffer(GL.PIXEL_UNPACK_BUFFER, data.handle);
568
+ this.gl.texImage3D(this.target, level, dataFormat, width, height, depth, 0 /* border, must be 0 */, format, type, offset);
569
+ }
570
+ });
571
+ if (data && data.byteLength) {
572
+ this.trackAllocatedMemory(data.byteLength, 'Texture');
573
+ }
574
+ else {
575
+ const bytesPerPixel = getTextureFormatBytesPerPixel(this.props.format);
576
+ this.trackAllocatedMemory(this.width * this.height * this.depth * bytesPerPixel, 'Texture');
577
+ }
578
+ this.loaded = true;
579
+ return this;
580
+ }
581
+ // RESOURCE METHODS
582
+ /**
583
+ * Sets sampler parameters on texture
584
+ */
585
+ _setSamplerParameters(parameters) {
586
+ // NPOT parameters may populate an empty object
587
+ if (isObjectEmpty(parameters)) {
588
+ return;
589
+ }
590
+ logParameters(parameters);
591
+ this.gl.bindTexture(this.target, this.handle);
592
+ for (const [pname, pvalue] of Object.entries(parameters)) {
593
+ const param = Number(pname);
594
+ const value = pvalue;
595
+ // Apparently there are integer/float conversion issues requires two parameter setting functions in JavaScript.
596
+ // For now, pick the float version for parameters specified as GLfloat.
597
+ switch (param) {
598
+ case GL.TEXTURE_MIN_LOD:
599
+ case GL.TEXTURE_MAX_LOD:
600
+ this.gl.texParameterf(this.target, param, value);
601
+ break;
602
+ default:
603
+ this.gl.texParameteri(this.target, param, value);
604
+ break;
605
+ }
606
+ }
607
+ this.gl.bindTexture(this.target, null);
220
608
  return;
221
- }
222
- this.setSubImageData({
223
- data: video,
224
- parameters
225
- });
226
- if (this.mipmaps) {
227
- this.generateMipmap();
228
- }
229
- this._video.lastTime = video.currentTime;
230
- }
231
- }
232
- generateMipmap() {
233
- let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
234
- if (this.device.isWebGL1 && isNPOT(this.width, this.height)) {
235
- log.warn(`texture: ${this} is Non-Power-Of-Two, disabling mipmaping`)();
236
- return this;
237
- }
238
- this.mipmaps = true;
239
- this.gl.bindTexture(this.target, this.handle);
240
- withGLParameters(this.gl, params, () => {
241
- this.gl.generateMipmap(this.target);
242
- });
243
- this.gl.bindTexture(this.target, null);
244
- return this;
245
- }
246
- setImageData(options) {
247
- if (this.props.dimension === '3d' || this.props.dimension === '2d-array') {
248
- return this.setImageData3D(options);
249
- }
250
- this.trackDeallocatedMemory('Texture');
251
- const {
252
- target = this.target,
253
- pixels = null,
254
- level = 0,
255
- glFormat = this.glFormat,
256
- offset = 0,
257
- parameters = {}
258
- } = options;
259
- let {
260
- data = null,
261
- type = this.type,
262
- width = this.width,
263
- height = this.height,
264
- dataFormat = this.dataFormat,
265
- compressed = false
266
- } = options;
267
- if (!data) {
268
- data = pixels;
269
- }
270
- ({
271
- type,
272
- dataFormat,
273
- compressed,
274
- width,
275
- height
276
- } = this._deduceParameters({
277
- format: this.props.format,
278
- type,
279
- dataFormat,
280
- compressed,
281
- data,
282
- width,
283
- height
284
- }));
285
- const {
286
- gl
287
- } = this;
288
- gl.bindTexture(this.target, this.handle);
289
- let dataType = null;
290
- ({
291
- data,
292
- dataType
293
- } = this._getDataType({
294
- data,
295
- compressed
296
- }));
297
- let gl2;
298
- withGLParameters(this.gl, parameters, () => {
299
- switch (dataType) {
300
- case 'null':
301
- gl.texImage2D(target, level, glFormat, width, height, 0, dataFormat, type, data);
302
- break;
303
- case 'typed-array':
304
- gl.texImage2D(target, level, glFormat, width, height, 0, dataFormat, type, data, offset);
305
- break;
306
- case 'buffer':
307
- gl2 = this.device.assertWebGL2();
308
- gl2.bindBuffer(35052, data.handle || data);
309
- gl2.texImage2D(target, level, glFormat, width, height, 0, dataFormat, type, offset);
310
- gl2.bindBuffer(35052, null);
311
- break;
312
- case 'browser-object':
313
- if (this.device.isWebGL2) {
314
- gl.texImage2D(target, level, glFormat, width, height, 0, dataFormat, type, data);
315
- } else {
316
- gl.texImage2D(target, level, glFormat, dataFormat, type, data);
317
- }
318
- break;
319
- case 'compressed':
320
- for (const [levelIndex, levelData] of data.entries()) {
321
- gl.compressedTexImage2D(target, levelIndex, levelData.format, levelData.width, levelData.height, 0, levelData.data);
322
- }
323
- break;
324
- default:
325
- assert(false, 'Unknown image data type');
326
- }
327
- });
328
- if (data && data.byteLength) {
329
- this.trackAllocatedMemory(data.byteLength, 'Texture');
330
- } else {
331
- const bytesPerPixel = getTextureFormatBytesPerPixel(this.props.format, this.device.isWebGL2);
332
- this.trackAllocatedMemory(this.width * this.height * bytesPerPixel, 'Texture');
333
- }
334
- this.loaded = true;
335
- return this;
336
- }
337
- setSubImageData(_ref) {
338
- let {
339
- target = this.target,
340
- pixels = null,
341
- data = null,
342
- x = 0,
343
- y = 0,
344
- width = this.width,
345
- height = this.height,
346
- level = 0,
347
- glFormat = this.glFormat,
348
- type = this.type,
349
- dataFormat = this.dataFormat,
350
- compressed = false,
351
- offset = 0,
352
- parameters = {}
353
- } = _ref;
354
- ({
355
- type,
356
- dataFormat,
357
- compressed,
358
- width,
359
- height
360
- } = this._deduceParameters({
361
- format: this.props.format,
362
- type,
363
- dataFormat,
364
- compressed,
365
- data,
366
- width,
367
- height
368
- }));
369
- assert(this.depth === 1, 'texSubImage not supported for 3D textures');
370
- if (!data) {
371
- data = pixels;
372
- }
373
- if (data && data.data) {
374
- const ndarray = data;
375
- data = ndarray.data;
376
- width = ndarray.shape[0];
377
- height = ndarray.shape[1];
378
- }
379
- if (data instanceof WEBGLBuffer) {
380
- data = data.handle;
381
609
  }
382
- this.gl.bindTexture(this.target, this.handle);
383
- withGLParameters(this.gl, parameters, () => {
384
- if (compressed) {
385
- this.gl.compressedTexSubImage2D(target, level, x, y, width, height, glFormat, data);
386
- } else if (data === null) {
387
- this.gl.texSubImage2D(target, level, x, y, width, height, dataFormat, type, null);
388
- } else if (ArrayBuffer.isView(data)) {
389
- this.gl.texSubImage2D(target, level, x, y, width, height, dataFormat, type, data, offset);
390
- } else if (typeof WebGLBuffer !== 'undefined' && data instanceof WebGLBuffer) {
391
- const gl2 = this.device.assertWebGL2();
392
- gl2.bindBuffer(35052, data);
393
- gl2.texSubImage2D(target, level, x, y, width, height, dataFormat, type, offset);
394
- gl2.bindBuffer(35052, null);
395
- } else if (this.device.isWebGL2) {
396
- const gl2 = this.device.assertWebGL2();
397
- gl2.texSubImage2D(target, level, x, y, width, height, dataFormat, type, data);
398
- } else {
399
- this.gl.texSubImage2D(target, level, x, y, dataFormat, type, data);
400
- }
401
- });
402
- this.gl.bindTexture(this.target, null);
403
- }
404
- copyFramebuffer() {
405
- let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
406
- log.error('Texture.copyFramebuffer({...}) is no logner supported, use copyToTexture(source, target, opts})')();
407
- return null;
408
- }
409
- getActiveUnit() {
410
- return this.gl.getParameter(34016) - 33984;
411
- }
412
- bind() {
413
- let textureUnit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.textureUnit;
414
- const {
415
- gl
416
- } = this;
417
- if (textureUnit !== undefined) {
418
- this.textureUnit = textureUnit;
419
- gl.activeTexture(33984 + textureUnit);
420
- }
421
- gl.bindTexture(this.target, this.handle);
422
- return textureUnit;
423
- }
424
- unbind() {
425
- let textureUnit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.textureUnit;
426
- const {
427
- gl
428
- } = this;
429
- if (textureUnit !== undefined) {
430
- this.textureUnit = textureUnit;
431
- gl.activeTexture(33984 + textureUnit);
432
- }
433
- gl.bindTexture(this.target, null);
434
- return textureUnit;
435
- }
436
- _getDataType(_ref2) {
437
- let {
438
- data,
439
- compressed = false
440
- } = _ref2;
441
- if (compressed) {
442
- return {
443
- data,
444
- dataType: 'compressed'
445
- };
446
- }
447
- if (data === null) {
448
- return {
449
- data,
450
- dataType: 'null'
451
- };
452
- }
453
- if (ArrayBuffer.isView(data)) {
454
- return {
455
- data,
456
- dataType: 'typed-array'
457
- };
458
- }
459
- if (data instanceof WEBGLBuffer) {
460
- return {
461
- data: data.handle,
462
- dataType: 'buffer'
463
- };
464
- }
465
- if (typeof WebGLBuffer !== 'undefined' && data instanceof WebGLBuffer) {
466
- return {
467
- data,
468
- dataType: 'buffer'
469
- };
470
- }
471
- return {
472
- data,
473
- dataType: 'browser-object'
474
- };
475
- }
476
- _deduceParameters(opts) {
477
- const {
478
- format,
479
- data
480
- } = opts;
481
- let {
482
- width,
483
- height,
484
- dataFormat,
485
- type,
486
- compressed
487
- } = opts;
488
- const parameters = getWebGLTextureParameters(format, this.device.isWebGL2);
489
- dataFormat = dataFormat || parameters.dataFormat;
490
- type = type || parameters.type;
491
- compressed = compressed || parameters.compressed;
492
- ({
493
- width,
494
- height
495
- } = this._deduceImageSize(data, width, height));
496
- return {
497
- dataFormat,
498
- type,
499
- compressed,
500
- width,
501
- height,
502
- format,
503
- data
504
- };
505
- }
506
- _deduceImageSize(data, width, height) {
507
- let size;
508
- if (typeof ImageData !== 'undefined' && data instanceof ImageData) {
509
- size = {
510
- width: data.width,
511
- height: data.height
512
- };
513
- } else if (typeof HTMLImageElement !== 'undefined' && data instanceof HTMLImageElement) {
514
- size = {
515
- width: data.naturalWidth,
516
- height: data.naturalHeight
517
- };
518
- } else if (typeof HTMLCanvasElement !== 'undefined' && data instanceof HTMLCanvasElement) {
519
- size = {
520
- width: data.width,
521
- height: data.height
522
- };
523
- } else if (typeof ImageBitmap !== 'undefined' && data instanceof ImageBitmap) {
524
- size = {
525
- width: data.width,
526
- height: data.height
527
- };
528
- } else if (typeof HTMLVideoElement !== 'undefined' && data instanceof HTMLVideoElement) {
529
- size = {
530
- width: data.videoWidth,
531
- height: data.videoHeight
532
- };
533
- } else if (!data) {
534
- size = {
535
- width: width >= 0 ? width : 1,
536
- height: height >= 0 ? height : 1
537
- };
538
- } else {
539
- size = {
540
- width,
541
- height
542
- };
543
- }
544
- assert(size, 'Could not deduced texture size');
545
- assert(width === undefined || size.width === width, 'Deduced texture width does not match supplied width');
546
- assert(height === undefined || size.height === height, 'Deduced texture height does not match supplied height');
547
- return size;
548
- }
549
- async setCubeMapImageData(options) {
550
- const {
551
- gl
552
- } = this;
553
- const {
554
- width,
555
- height,
556
- pixels,
557
- data,
558
- format = 6408,
559
- type = 5121
560
- } = options;
561
- const imageDataMap = pixels || data;
562
- const resolvedFaces = await Promise.all(WEBGLTexture.FACES.map(face => {
563
- const facePixels = imageDataMap[face];
564
- return Promise.all(Array.isArray(facePixels) ? facePixels : [facePixels]);
565
- }));
566
- this.bind();
567
- WEBGLTexture.FACES.forEach((face, index) => {
568
- if (resolvedFaces[index].length > 1 && this.props.mipmaps !== false) {
569
- log.warn(`${this.id} has mipmap and multiple LODs.`)();
570
- }
571
- resolvedFaces[index].forEach((image, lodLevel) => {
572
- if (width && height) {
573
- gl.texImage2D(face, lodLevel, format, width, height, 0, format, type, image);
574
- } else {
575
- gl.texImage2D(face, lodLevel, format, format, type, image);
576
- }
577
- });
578
- });
579
- this.unbind();
580
- }
581
- setImageDataForFace(options) {
582
- const {
583
- face,
584
- width,
585
- height,
586
- pixels,
587
- data,
588
- format = 6408,
589
- type = 5121
590
- } = options;
591
- const {
592
- gl
593
- } = this;
594
- const imageData = pixels || data;
595
- this.bind();
596
- if (imageData instanceof Promise) {
597
- imageData.then(resolvedImageData => this.setImageDataForFace(Object.assign({}, options, {
598
- face,
599
- data: resolvedImageData,
600
- pixels: resolvedImageData
601
- })));
602
- } else if (this.width || this.height) {
603
- gl.texImage2D(face, 0, format, width, height, 0, format, type, imageData);
604
- } else {
605
- gl.texImage2D(face, 0, format, format, type, imageData);
606
- }
607
- return this;
608
- }
609
- setImageData3D(options) {
610
- const {
611
- level = 0,
612
- dataFormat,
613
- format,
614
- type,
615
- width,
616
- height,
617
- depth = 1,
618
- offset = 0,
619
- data,
620
- parameters = {}
621
- } = options;
622
- this.trackDeallocatedMemory('Texture');
623
- this.gl.bindTexture(this.target, this.handle);
624
- const webglTextureFormat = getWebGLTextureParameters(format, this.device.isWebGL2);
625
- withGLParameters(this.gl, parameters, () => {
626
- if (ArrayBuffer.isView(data)) {
627
- this.gl.texImage3D(this.target, level, webglTextureFormat.format, width, height, depth, 0, webglTextureFormat.dataFormat, webglTextureFormat.type, data);
628
- }
629
- if (data instanceof WEBGLBuffer) {
630
- this.gl.bindBuffer(35052, data.handle);
631
- this.gl.texImage3D(this.target, level, dataFormat, width, height, depth, 0, format, type, offset);
632
- }
633
- });
634
- if (data && data.byteLength) {
635
- this.trackAllocatedMemory(data.byteLength, 'Texture');
636
- } else {
637
- const bytesPerPixel = getTextureFormatBytesPerPixel(this.props.format, this.device.isWebGL2);
638
- this.trackAllocatedMemory(this.width * this.height * this.depth * bytesPerPixel, 'Texture');
639
- }
640
- this.loaded = true;
641
- return this;
642
- }
643
- _setSamplerParameters(parameters) {
644
- if (this.device.isWebGL1 && isNPOT(this.width, this.height)) {
645
- parameters = updateSamplerParametersForNPOT(parameters);
646
- }
647
- if (isObjectEmpty(parameters)) {
648
- return;
649
- }
650
- logParameters(parameters);
651
- this.gl.bindTexture(this.target, this.handle);
652
- for (const [pname, pvalue] of Object.entries(parameters)) {
653
- const param = Number(pname);
654
- const value = pvalue;
655
- switch (param) {
656
- case 33082:
657
- case 33083:
658
- this.gl.texParameterf(this.target, param, value);
659
- break;
660
- default:
661
- this.gl.texParameteri(this.target, param, value);
662
- break;
663
- }
664
- }
665
- this.gl.bindTexture(this.target, null);
666
- return;
667
- }
668
- _getWebGL1NPOTParameterOverride(pname, value) {
669
- const npot = this.device.isWebGL1 && isNPOT(this.width, this.height);
670
- if (npot) {
671
- switch (pname) {
672
- case 10241:
673
- if (value !== 9729 && value !== 9728) {
674
- return 9729;
675
- }
676
- break;
677
- case 10242:
678
- case 10243:
679
- return 33071;
680
- default:
681
- break;
682
- }
683
- }
684
- return value;
685
- }
686
610
  }
687
- WEBGLTexture.FACES = [34069, 34070, 34071, 34072, 34073, 34074];
611
+ // HELPERS
688
612
  function getWebGLTextureTarget(props) {
689
- switch (props.dimension) {
690
- case '2d':
691
- return 3553;
692
- case 'cube':
693
- return 34067;
694
- case '2d-array':
695
- return 35866;
696
- case '3d':
697
- return 32879;
698
- case '1d':
699
- case 'cube-array':
700
- default:
701
- throw new Error(props.dimension);
702
- }
703
- }
704
- function isNPOT(width, height) {
705
- if (!width || !height) {
706
- return false;
707
- }
708
- return !isPowerOfTwo(width) || !isPowerOfTwo(height);
613
+ switch (props.dimension) {
614
+ // supported in WebGL
615
+ case '2d':
616
+ return GL.TEXTURE_2D;
617
+ case 'cube':
618
+ return GL.TEXTURE_CUBE_MAP;
619
+ // supported in WebGL2
620
+ case '2d-array':
621
+ return GL.TEXTURE_2D_ARRAY;
622
+ case '3d':
623
+ return GL.TEXTURE_3D;
624
+ // not supported in any WebGL version
625
+ case '1d':
626
+ case 'cube-array':
627
+ default:
628
+ throw new Error(props.dimension);
629
+ }
709
630
  }
710
631
  function logParameters(parameters) {
711
- log.log(1, 'texture sampler parameters', parameters)();
632
+ log.log(1, 'texture sampler parameters', parameters)();
712
633
  }
713
- //# sourceMappingURL=webgl-texture.js.map