@luma.gl/webgl 9.0.0-beta.3 → 9.0.0-beta.5

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