ris-framework 0.1.0-dev.4

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 (103) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +11 -0
  4. package/dist/assets/_ris-ktx2-node-module-stub-DwDS30PT.js +1 -0
  5. package/dist/assets/cat.jpg +0 -0
  6. package/dist/assets/index-BmeGit54.css +1 -0
  7. package/dist/assets/index-qyn4pzGw.js +151 -0
  8. package/dist/assets/libktx-Cjh8w7XG-Ci1Fm_MH.js +1 -0
  9. package/dist/assets/test.png +0 -0
  10. package/dist/index.html +16 -0
  11. package/dist/vite.svg +1 -0
  12. package/package.json +37 -0
  13. package/shaders/glsl/inspect_texture_mips_fs.glsl +18 -0
  14. package/shaders/glsl/inspect_texture_mips_vs.glsl +25 -0
  15. package/shaders/glsl/main_render_target_flip_y_fs.glsl +9 -0
  16. package/shaders/glsl/main_render_target_flip_y_vs.glsl +9 -0
  17. package/shaders/glsl/sprite_fs.glsl +14 -0
  18. package/shaders/glsl/sprite_vs.glsl +20 -0
  19. package/shaders/glsl/unlit_material_fs.glsl +19 -0
  20. package/shaders/glsl/unlit_material_vs.glsl +25 -0
  21. package/src/TextureFormat.ts +0 -0
  22. package/src/VertexFormat.ts +17 -0
  23. package/src/common/sampler-enums.ts +21 -0
  24. package/src/common/vertex-step-mode.ts +12 -0
  25. package/src/content/ShaderModuleContent.ts +47 -0
  26. package/src/core/Framework.ts +240 -0
  27. package/src/core/FrameworkConfig.ts +31 -0
  28. package/src/core/buffers/buffers-factory-interface.ts +0 -0
  29. package/src/core/buffers/vertex-buffer-interface.ts +0 -0
  30. package/src/core/content/ContentManager.ts +86 -0
  31. package/src/core/dependency-injection/register-services-interface.ts +15 -0
  32. package/src/core/framework-interface.ts +0 -0
  33. package/src/core/input/InputManager.ts +115 -0
  34. package/src/core/renderer/enums.ts +20 -0
  35. package/src/core/renderer/renderer-interface.ts +92 -0
  36. package/src/core/renderer/rendering-limits.ts +20 -0
  37. package/src/core/rendering/AGraphicsDevice.ts +119 -0
  38. package/src/core/rendering/ARenderer.ts +247 -0
  39. package/src/core/rendering/blending/blend-component-descriptor.ts +33 -0
  40. package/src/core/rendering/blending/blend-state-descriptor.ts +51 -0
  41. package/src/core/rendering/blending/enums.ts +50 -0
  42. package/src/core/rendering/enums.ts +22 -0
  43. package/src/core/rendering/primitive/PrimitiveStateDescriptor.ts +32 -0
  44. package/src/core/rendering/primitive/enums.ts +68 -0
  45. package/src/core/rendering/render-pass/render-pass-descriptor.ts +0 -0
  46. package/src/core/rendering/render-pass/render-pass-interface.ts +17 -0
  47. package/src/core/rendering/sampler/sampler-descriptor.ts +33 -0
  48. package/src/core/rendering/texture/texture.ts +146 -0
  49. package/src/core/rendering/vertex-attribute.ts +20 -0
  50. package/src/core/rendering/vertex-buffer-layout.ts +84 -0
  51. package/src/core/shader/shader-loader.ts +27 -0
  52. package/src/core/shader/shader-module-interface.ts +1 -0
  53. package/src/core/sprite-batch/SpriteBatch.ts +216 -0
  54. package/src/core/sprite-batch/SpriteBatchDrawable.ts +146 -0
  55. package/src/core/sprite-batch/SpriteBatchMesh.ts +243 -0
  56. package/src/core/window/window-manager.ts +76 -0
  57. package/src/counter.ts +9 -0
  58. package/src/geometry/BaseGeometry.ts +148 -0
  59. package/src/geometry/GeometryBuilder.ts +58 -0
  60. package/src/geometry/GeometryFormat.ts +37 -0
  61. package/src/main.ts +97 -0
  62. package/src/style.css +96 -0
  63. package/src/typescript.svg +1 -0
  64. package/src/webgl/WebGLGraphicsDeviceFeatures.ts +57 -0
  65. package/src/webgl/WebGlGpuInfo.ts +76 -0
  66. package/src/webgl/WebGlGraphicsDevice.ts +129 -0
  67. package/src/webgl/WebGlRenderer.ts +31 -0
  68. package/src/webgl/blending/webgl-blend-state.ts +59 -0
  69. package/src/webgl/buffers/WebGlBuffersFactory.ts +75 -0
  70. package/src/webgl/buffers/WebGlUniformBuffer.ts +87 -0
  71. package/src/webgl/buffers/WebGlVertexBuffer.ts +100 -0
  72. package/src/webgl/buffers/webgl-index-buffer.ts +110 -0
  73. package/src/webgl/cast/cast.ts +100 -0
  74. package/src/webgl/primitive/WebGlPrimitiveState.ts +60 -0
  75. package/src/webgl/render-pass/WebGlRenderPass.ts +186 -0
  76. package/src/webgl/render-pipelines/AWebGlRenderPipeline.ts +129 -0
  77. package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +95 -0
  78. package/src/webgl/render-pipelines/WebGlRenderPipelineFactory.ts +75 -0
  79. package/src/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.ts +200 -0
  80. package/src/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.ts +197 -0
  81. package/src/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.ts +148 -0
  82. package/src/webgl/rendering/webgl-vertex-attribute.ts +16 -0
  83. package/src/webgl/rendering/webgl-vertex-buffer-layout.ts +64 -0
  84. package/src/webgl/sampler/webgl-sampler.ts +110 -0
  85. package/src/webgl/shader/WebGlShaderModule.ts +110 -0
  86. package/src/webgl/shader/WebGlShaderModuleLoader.ts +40 -0
  87. package/src/webgl/swap-chain/WebGlSwapChain.ts +56 -0
  88. package/src/webgl/texture/WebGlTexture2D.ts +133 -0
  89. package/src/webgl/texture/WebGlTextureFactory.ts +184 -0
  90. package/src/webgl/utilities/WebGlBufferUtilities.ts +124 -0
  91. package/src/webgl/utilities/WebGlConverter.ts +391 -0
  92. package/src/webgl/utilities/WebGlFrameBufferUtilities.ts +240 -0
  93. package/src/webgl/utilities/WebGlSamplerUtilities.ts +81 -0
  94. package/src/webgl/utilities/WebGlShaderUtilities.ts +48 -0
  95. package/src/webgl/utilities/WebGlTextureUtilities.ts +162 -0
  96. package/src/webgl/utilities/WebGlUtilities.ts +71 -0
  97. package/src/webgl/utilities/spector-js-utilities.ts +13 -0
  98. package/src/webgl/utilities/webgl-blending-utilities.ts +85 -0
  99. package/src/webgl/utilities/webgl-culling-utilities.ts +23 -0
  100. package/src/webgl/utilities/webgl-filter-anisotropic-utilities.ts +37 -0
  101. package/src/webgl/utilities/webgl-program-utilities.ts +29 -0
  102. package/src/webgl/utilities/webgl-render-buffer-utilities.ts +23 -0
  103. package/src/webgl/utilities/webgl-renderbuffer-utilities.ts +35 -0
@@ -0,0 +1,56 @@
1
+ import { vec2 } from "gl-matrix";
2
+ import {type IGraphicsDevice, type ISwapChain, TextureFormat} from "ris-framework-api";
3
+ import type {WebGlGraphicsDevice} from "../WebGlGraphicsDevice.ts";
4
+
5
+ /**
6
+ * The WebGL implementation of the ISwapChain interface, which manages the swap chain for rendering operations in a WebGL context.
7
+ */
8
+ export class WebGlSwapChain implements ISwapChain {
9
+
10
+ private readonly _canvas: HTMLCanvasElement
11
+ private readonly _graphicsDevice: WebGlGraphicsDevice;
12
+
13
+ private _backBufferSize: vec2;
14
+
15
+ /**
16
+ * The WebGLSwapChain constructor.
17
+ * @param canvas The HTML canvas element that the swap chain will use for rendering.
18
+ * @param graphicsDevice The graphics device.
19
+ */
20
+ public constructor(canvas: HTMLCanvasElement, graphicsDevice: IGraphicsDevice) {
21
+ this._canvas = canvas;
22
+ this._graphicsDevice = graphicsDevice as WebGlGraphicsDevice;
23
+ this._backBufferSize = vec2.fromValues(this._canvas.width, this._canvas.height);
24
+ this._canvas.addEventListener('resize', () => {
25
+ this._backBufferSize[0] = this._canvas.width;
26
+ this._backBufferSize[1] = this._canvas.height;
27
+ });
28
+ }
29
+
30
+
31
+ /** @inheritdoc */
32
+ public get textureFormat(): TextureFormat {
33
+ return TextureFormat.RGBA_8_UNORM;
34
+ }
35
+
36
+ /** @inheritdoc */
37
+ public get backBufferSize(): vec2 {
38
+ return this._backBufferSize;
39
+ }
40
+
41
+ /** @inheritdoc */
42
+ public resize(_width: number, _height: number): void {
43
+ // Nothing to do here since the canvas size is managed by the application and WebGL will automatically adjust the viewport accordingly.
44
+ }
45
+
46
+ /** @inheritdoc */
47
+ public present(): void {
48
+ const gl = this._graphicsDevice.gl;
49
+ gl.viewport(0, 0, this._canvas.width, this._canvas.height);
50
+ }
51
+
52
+ /** @inheritdoc */
53
+ public dispose(): void {
54
+
55
+ }
56
+ }
@@ -0,0 +1,133 @@
1
+ import {WebGlUtilities} from "../utilities/WebGlUtilities.ts";
2
+ import {
3
+ TextureDescriptor,
4
+ type IFramework, State,
5
+ type ITextureView,
6
+ type TextureViewDescriptor, TextureUtilities
7
+ } from "ris-framework-api";
8
+ import type {WebGlGraphicsDevice} from "../WebGlGraphicsDevice.ts";
9
+ import {ATexture2D} from "../../core/rendering/texture/texture.ts";
10
+ import {vec2} from "gl-matrix";
11
+
12
+ /**
13
+ * The WebGL implementation of ITexture2D.
14
+ */
15
+ export class WebGlTexture2D extends ATexture2D {
16
+
17
+ private readonly _graphicsDevice: WebGlGraphicsDevice;
18
+ private readonly _gl: WebGL2RenderingContext;
19
+ private _glTexture: WebGLTexture | null = null;
20
+
21
+ /**
22
+ * The constructor.
23
+ * @param _framework The framework instance.
24
+ * @param textureDescriptor The texture descriptor.
25
+ */
26
+ constructor(
27
+ private readonly _framework: IFramework,
28
+ textureDescriptor: TextureDescriptor
29
+ ) {
30
+ super(textureDescriptor);
31
+ this._graphicsDevice = this._framework.renderer.graphicsDevice as WebGlGraphicsDevice;
32
+ this._gl = this._graphicsDevice.gl!;
33
+ }
34
+
35
+ /**
36
+ * Gets the underlying WebGL texture.
37
+ * @returns The WebGL texture.
38
+ */
39
+ public get glTexture(): WebGLTexture {
40
+ return this._glTexture!;
41
+ }
42
+
43
+ /** @inheritdoc */
44
+ public initialize(): void {
45
+ if (this._state == State.INITIALIZED) {
46
+ throw new Error("Texture is already initialized.");
47
+ } else if (this._state == State.DISPOSED) {
48
+ throw new Error("Texture is already disposed.");
49
+ }
50
+
51
+ if (TextureUtilities.isCompressedTextureFormat(this.textureFormat)) {
52
+ this._glTexture = WebGlUtilities.texture.createCompressedTexture2D(
53
+ this._gl, vec2.fromValues(this.width, this.height),
54
+ this._blockSize, this._data, this.label, this.textureFormat, 0);
55
+ } else {
56
+ this._glTexture = WebGlUtilities.texture.createTexture2D(
57
+ this._gl,
58
+ this.width, this.height,
59
+ this._data,
60
+ this.textureFormat,
61
+ this._generateMipMaps,
62
+ 0,
63
+ this.label
64
+ );
65
+ }
66
+
67
+ this._state = State.INITIALIZED;
68
+
69
+
70
+ let width = this.width;
71
+ let height = this.height;
72
+
73
+ // If we can generate mipmaps, we need to calculate the number of mip levels.
74
+ if(this._generateMipMaps) {
75
+ this._mipLevels = TextureUtilities.mipLevels(width, height);
76
+ }
77
+ this._size = 0;
78
+
79
+ for (let i = 0; i < this.mipLevels; i++) {
80
+ this._size += TextureUtilities.getVRamSize(this.textureFormat, width, height);
81
+ width /= 2;
82
+ height /= 2;
83
+ }
84
+ }
85
+
86
+ /** @inheritdoc */
87
+ public createView(_descriptor?: TextureViewDescriptor): ITextureView {
88
+ throw new Error("Method not implemented.");
89
+ }
90
+
91
+
92
+ /** @inheritdoc */
93
+ public dispose(): void {
94
+
95
+ if (this._state == State.DISPOSING || this._state == State.DISPOSED) {
96
+ return;
97
+ }
98
+
99
+ this._gl.deleteTexture(this._glTexture);
100
+ this._glTexture = null;
101
+ this._state = State.DISPOSED;
102
+
103
+ for (const listener of this._disposedListeners) {
104
+ listener(this);
105
+ }
106
+ }
107
+
108
+ private static _defaultFilled: WebGlTexture2D | null = null;
109
+
110
+ /**
111
+ * Creates a default 1x1 white texture if it doesn't already exist and returns it.
112
+ * This can be used as a placeholder texture when a texture is expected but not available.
113
+ * @param framework The framework instance.
114
+ * @returns The default 1x1 white texture.
115
+ */
116
+ public static getOrCreateDefault(framework: IFramework): WebGlTexture2D {
117
+ if (this._defaultFilled === null || this._defaultFilled.state === State.DISPOSED) {
118
+
119
+ const desc = new TextureDescriptor();
120
+ desc.width = 1;
121
+ desc.height = 1;
122
+ desc.data = [new Uint8Array([255, 255, 255, 255])];
123
+ desc.textureFormat = framework.renderer.preferredTextureFormat;
124
+ desc.label = "RisDefaultFilledTexture";
125
+
126
+ this._defaultFilled = new WebGlTexture2D(framework, desc);
127
+ this._defaultFilled.initialize();
128
+ }
129
+
130
+ return this._defaultFilled;
131
+ }
132
+
133
+ }
@@ -0,0 +1,184 @@
1
+ import {
2
+ Color,
3
+ type IFramework,
4
+ type ITexture2D,
5
+ type ITextureFactory,
6
+ TextureDescriptor,
7
+ TextureFormat,
8
+ TextureUsage,
9
+ TextureUtilities
10
+ } from "ris-framework-api"
11
+ import {WebGlTexture2D} from "./WebGlTexture2D.ts";
12
+ import {type IKtx2Texture, KtxTranscodeFlags, KtxTranscodeFormat, TextureFormatInfo, VkFormat} from "ris-ktx2-api";
13
+ import {vec2} from "gl-matrix";
14
+
15
+ export class WebGlTextureFactory implements ITextureFactory {
16
+
17
+ /**
18
+ * The constructor for the WebGLTextureFactory class.
19
+ * @param _framework The framework instance.
20
+ */
21
+ constructor(private readonly _framework: IFramework) {
22
+ }
23
+
24
+ /** @inheritDoc */
25
+ public createFromKtx2(ktxTexture: IKtx2Texture, descriptor?: TextureDescriptor): ITexture2D {
26
+ descriptor = descriptor ?? new TextureDescriptor();
27
+ const vkFormat = ktxTexture.vkFormat;
28
+
29
+ // Ktx2 will have different cases, for now we consider simple 2d case.
30
+ const numLevels = ktxTexture.numLevels;
31
+ let transcodeFormat = KtxTranscodeFormat.NO_SELECTION;
32
+ let textureFormat = descriptor.textureFormat;
33
+ const gpuFeatures = this._framework.graphicsDevice.features;
34
+ let texFormatInfo: TextureFormatInfo | null = null;
35
+
36
+ if (ktxTexture.needsTranscoding && vkFormat == VkFormat.UNDEFINED)
37
+ {
38
+ // If descriptor format is UNDEFINED, we will use format according to the GPU features.
39
+ if(descriptor.textureFormat == TextureFormat.UNDEFINED)
40
+ {
41
+ if(gpuFeatures.supportsTextureCompressionBC)
42
+ {
43
+ transcodeFormat = KtxTranscodeFormat.BC7_RGBA;
44
+ textureFormat = TextureFormat.BC7_RGBA_UNORM;
45
+ }
46
+ else if(gpuFeatures.supportsTextureCompressionASTC)
47
+ {
48
+ transcodeFormat = KtxTranscodeFormat.ASTC_4X4_RGBA;
49
+ textureFormat = TextureFormat.ASTC_4X4_RGBA
50
+ }
51
+ else if(gpuFeatures.supportsTextureCompressionS3TC)
52
+ {
53
+ transcodeFormat = KtxTranscodeFormat.BC3_RGBA;
54
+ textureFormat = TextureFormat.BC3_RGBA_UNORM;
55
+ }
56
+ else if(gpuFeatures.supportsTextureCompressionETC2)
57
+ {
58
+ transcodeFormat = KtxTranscodeFormat.ETC2_RGBA;
59
+ textureFormat = TextureFormat.ETC2_RGBA8_UNORM;
60
+ }
61
+ }
62
+ else
63
+ {
64
+ transcodeFormat = TextureUtilities.convertTextureFormatToKtxTranscodeFormat(textureFormat);
65
+ }
66
+
67
+ ktxTexture.transcodeBasis(transcodeFormat, KtxTranscodeFlags.HIGH_QUALITY);
68
+ texFormatInfo = ktxTexture.getTextureFormatInfo(transcodeFormat);
69
+ }
70
+ else {
71
+ // No transcoding path, but still get info about the texture format.
72
+ textureFormat = TextureUtilities.convertVkFormatToTextureFormat(vkFormat);
73
+ texFormatInfo = ktxTexture.getTextureFormatInfo(vkFormat);
74
+ }
75
+
76
+ const data: Uint8Array[] = [];
77
+ for (let i = 0; i < numLevels; i++)
78
+ {
79
+ data.push(ktxTexture.getImage(i));
80
+ }
81
+
82
+ const textureDescriptor = new TextureDescriptor();
83
+ textureDescriptor.width = ktxTexture.width;
84
+ textureDescriptor.height = ktxTexture.height;
85
+ textureDescriptor.data = data;
86
+ textureDescriptor.textureFormat = textureFormat;
87
+ textureDescriptor.generateMipmaps = descriptor.generateMipmaps;
88
+ textureDescriptor.blockSize = vec2.fromValues(texFormatInfo.blockWidth, texFormatInfo.blockHeight);
89
+
90
+ const texture = new WebGlTexture2D(this._framework, textureDescriptor);
91
+ texture.initialize();
92
+ return texture;
93
+ }
94
+
95
+ /** @inheritdoc */
96
+ public create(
97
+ width: number, height: number,
98
+ data?: Uint8Array | HTMLImageElement,
99
+ channels: number = 4,
100
+ label: string | null | undefined = null,
101
+ textureUsage = TextureUsage.COPY_DST | TextureUsage.TEXTURE_BINDING,
102
+ textureFormat= TextureFormat.RGBA_8_UNORM,
103
+ useMipMaps = false): ITexture2D {
104
+
105
+ if (!data) {
106
+ throw new Error("Data must not be null.");
107
+ }
108
+ if (width == 0 || height == 0) {
109
+ throw new Error("width and height must be greater than 0.");
110
+ }
111
+
112
+ if (channels == 0) {
113
+ throw new Error("channels must be greater than 0.");
114
+ }
115
+
116
+ const desc = new TextureDescriptor();
117
+ desc.width = width;
118
+ desc.height = height;
119
+ // @ts-ignore
120
+ desc.data = data ? [data] : null;
121
+ desc.textureFormat = textureFormat;
122
+ desc.textureUsage = textureUsage;
123
+ desc.generateMipmaps = useMipMaps;
124
+ desc.label = label;
125
+
126
+ const texture = new WebGlTexture2D(
127
+ this._framework,
128
+ desc
129
+ );
130
+
131
+ texture.initialize();
132
+ return texture;
133
+ }
134
+
135
+ /** @inheritdoc */
136
+ public createEmpty(
137
+ width: number, height: number,
138
+ color: Color | null = null,
139
+ textureUsage = TextureUsage.COPY_DST | TextureUsage.TEXTURE_BINDING,
140
+ textureFormat = TextureFormat.UNDEFINED,
141
+ label: string | null = null,
142
+ useMipmap = false): ITexture2D {
143
+
144
+ if (width == 0 || height == 0) {
145
+ throw new Error("Width and height must be greater than 0.");
146
+ }
147
+
148
+ if (textureFormat == TextureFormat.UNDEFINED) {
149
+ textureFormat = this._framework.renderer.preferredTextureFormat;
150
+ }
151
+
152
+ const desc = new TextureDescriptor();
153
+ desc.width = width;
154
+ desc.height = height;
155
+ desc.textureUsage = textureUsage;
156
+ desc.textureFormat = textureFormat;
157
+ desc.label = label;
158
+ desc.generateMipmaps = useMipmap;
159
+
160
+ // Fast path: avoid generating & uploading a big CPU-side buffer when not needed.
161
+ if (!color || color.equals(Color.black())) {
162
+ const texture = new WebGlTexture2D(
163
+ this._framework,
164
+ desc
165
+ );
166
+
167
+ texture.initialize();
168
+ return texture;
169
+ }
170
+
171
+ // Fill CPU-side pixels (BGRA layout to match TextureFormat.BGRA_8_Unorm usage elsewhere).
172
+ const byteSize = width * height * 4;
173
+ const bytes = new Uint8Array(byteSize);
174
+
175
+ for (let i = 0; i < byteSize; i += 4) {
176
+ bytes[i + 0] = color.b * 255;
177
+ bytes[i + 1] = color.g * 255;
178
+ bytes[i + 2] = color.r * 255;
179
+ bytes[i + 3] = color.a * 255;
180
+ }
181
+
182
+ return this.create( width, height, bytes, 4, label, textureUsage, textureFormat, useMipmap );
183
+ }
184
+ }
@@ -0,0 +1,124 @@
1
+ import { WebGlConverter } from "./WebGlConverter.ts";
2
+ import type {BufferUsage} from "ris-framework-api";
3
+
4
+ /**
5
+ * This file contains utility functions for working with WebGL buffers,
6
+ * such as creating and managing vertex buffers, index buffers, and uniform buffers.
7
+ * It provides a higher-level abstraction over the raw WebGL API to simplify buffer management in a WebGL application.
8
+ */
9
+ export class WebGlBufferUtilities {
10
+
11
+ /**
12
+ * Creates a vertex buffer in WebGL with the given data and usage.
13
+ * @param gl The WebGL2RenderingContext to use for creating the buffer.
14
+ * @param dataOrByteSize The data to upload to the vertex buffer, typically a Float32Array containing vertex attributes, or a number representing the byte size of the buffer to create.
15
+ * @param usage The intended usage of the buffer, which can affect how the buffer is created and optimized by the graphics API. For example, a vertex buffer may be optimized for use as a vertex buffer in rendering operations.
16
+ * @returns The created WebGLBuffer object that represents the vertex buffer in WebGL.
17
+ */
18
+ public createVertexBuffer(gl: WebGL2RenderingContext, dataOrByteSize: Float32Array | number, usage: BufferUsage): WebGLBuffer {
19
+ const buffer = gl.createBuffer();
20
+ if (!buffer) {
21
+ throw new Error("Failed to create vertex buffer.");
22
+ }
23
+
24
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
25
+ if (typeof dataOrByteSize === "number") {
26
+ gl.bufferData(gl.ARRAY_BUFFER, dataOrByteSize, WebGlConverter.convertBufferUsage(usage));
27
+ } else {
28
+ gl.bufferData(gl.ARRAY_BUFFER, dataOrByteSize, WebGlConverter.convertBufferUsage(usage));
29
+ }
30
+
31
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
32
+ return buffer;
33
+ }
34
+
35
+ /**
36
+ * Creates an index buffer in WebGL with the given data and usage.
37
+ * @param gl The WebGL2RenderingContext to use for creating the buffer.
38
+ * @param dataOrByteSize The data to upload to the index buffer, typically a Uint16Array or Uint32Array containing index data, or a number representing the byte size of the buffer to create.
39
+ * @param usage The intended usage of the buffer, which can affect how the buffer is created and optimized by the graphics API. For example, an index buffer may be optimized for use as an index buffer in rendering operations.
40
+ * @param label The label.
41
+ * @returns The created WebGLBuffer object that represents the index buffer in WebGL.
42
+ */
43
+ public createIndexBuffer(gl: WebGL2RenderingContext,
44
+ dataOrByteSize: Uint16Array | Uint32Array | number,
45
+ usage: BufferUsage,
46
+ label?: string
47
+ ): WebGLBuffer {
48
+ const buffer = gl.createBuffer();
49
+ if (!buffer) {
50
+ throw new Error("Failed to create index buffer.");
51
+ }
52
+
53
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
54
+ if (typeof dataOrByteSize === "number") {
55
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, dataOrByteSize, WebGlConverter.convertBufferUsage(usage));
56
+ } else {
57
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, dataOrByteSize, WebGlConverter.convertBufferUsage(usage));
58
+ }
59
+
60
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
61
+
62
+ if(label) {
63
+ // @ts-ignore
64
+ buffer["__SPECTOR_Metadata"] = {
65
+ "name": label,
66
+ };
67
+ }
68
+
69
+ return buffer;
70
+ }
71
+
72
+ /**
73
+ * Creates a uniform buffer in WebGL with the given data and usage.
74
+ * @param gl The WebGL2RenderingContext to use for creating the buffer.
75
+ * @param dataOrByteSize The data to upload to the uniform buffer, typically an ArrayBuffer or a TypedArray containing uniform data, or a number representing the byte size of the buffer to create.
76
+ * @param usage The intended usage of the buffer, which can affect how the buffer is created and optimized by the graphics API. For example, a uniform buffer may be optimized for use as a uniform buffer in rendering operations.
77
+ * @param label An optional label for the buffer, which can be used for debugging purposes to identify the buffer in graphics debugging tools.
78
+ * @returns The created WebGLBuffer object that represents the uniform buffer in WebGL.
79
+ */
80
+ public createUniformBuffer(gl: WebGL2RenderingContext, dataOrByteSize: ArrayBuffer | ArrayBufferView | number, usage: BufferUsage, label?: string): WebGLBuffer {
81
+ const buffer = gl.createBuffer();
82
+ if (!buffer) {
83
+ throw new Error("Failed to create uniform buffer.");
84
+ }
85
+
86
+ if(label) {
87
+ (buffer as any)["__SPECTOR_Metadata"] = { name: label };
88
+ }
89
+
90
+ gl.bindBuffer(gl.UNIFORM_BUFFER, buffer);
91
+ if (typeof dataOrByteSize === "number") {
92
+ gl.bufferData(gl.UNIFORM_BUFFER, dataOrByteSize, WebGlConverter.convertBufferUsage(usage));
93
+ } else {
94
+ gl.bufferData(gl.UNIFORM_BUFFER, dataOrByteSize, WebGlConverter.convertBufferUsage(usage));
95
+ }
96
+
97
+ gl.bindBuffer(gl.UNIFORM_BUFFER, null);
98
+ return buffer;
99
+ }
100
+
101
+ /**
102
+ * Updates the vertex buffer.
103
+ * @param gl
104
+ * @param buffer
105
+ * @param data The data.
106
+ * @param destOffset Offset into destination.
107
+ * @param srcOffset
108
+ * @param srcLength
109
+ */
110
+ public updateVertexBuffer(
111
+ gl: WebGL2RenderingContext,
112
+ buffer: WebGLBuffer,
113
+ data: Float32Array,
114
+ destOffset: number = 0,
115
+ srcOffset: number = 0,
116
+ srcLength: number = 0) {
117
+
118
+ srcLength = srcLength > 0 ? srcLength : data.length;
119
+
120
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
121
+ gl.bufferSubData(gl.ARRAY_BUFFER, destOffset, data, srcOffset, srcLength );
122
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
123
+ }
124
+ }