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,81 @@
1
+ import { SamplerCompareFunction } from '../../common/sampler-enums';
2
+ import { SpectorJSUtilities } from './spector-js-utilities';
3
+ import { WebGlConverter } from './WebGlConverter.ts';
4
+ import {MipMapSamplerFilter, SamplerAddressMode, SamplerFilter} from "ris-framework-api";
5
+
6
+ /**
7
+ * The utility class for working with WebGLSampler object.
8
+ */
9
+ export class WebGlSamplerUtilities {
10
+
11
+ /**
12
+ * Creates a WebGLSampler with the specified parameters.
13
+ * @param gl The WebGL2RenderingContext.
14
+ * @param minFilter The minification filter of the sampler.
15
+ * @param magFilter The magnification filter of the sampler.
16
+ * @param mipMapFilter The mipmap filter of the sampler.
17
+ * @param addressModeU The address mode of the sampler for the U coordinate.
18
+ * @param addressModeV The address mode of the sampler for the V coordinate.
19
+ * @param addressModeW The address mode of the sampler for the W coordinate.
20
+ * @param compareFunction The compare function of the sampler. This is used for shadow samplers.
21
+ * @param _anisotropy The anisotropy level of the sampler. This is used for anisotropic filtering. The default value is 1, which means no anisotropic filtering.
22
+ * @param label The label of the sampler. This is used for debugging purposes and can be viewed in graphics debuggers like SpectorJS.
23
+ * @returns
24
+ */
25
+ public create(gl: WebGL2RenderingContext,
26
+ minFilter: SamplerFilter = SamplerFilter.LINEAR,
27
+ magFilter: SamplerFilter = SamplerFilter.LINEAR,
28
+ mipMapFilter: MipMapSamplerFilter = MipMapSamplerFilter.NONE,
29
+ addressModeU: SamplerAddressMode = SamplerAddressMode.CLAMP_TO_EDGE,
30
+ addressModeV: SamplerAddressMode = SamplerAddressMode.CLAMP_TO_EDGE,
31
+ addressModeW: SamplerAddressMode = SamplerAddressMode.CLAMP_TO_EDGE,
32
+ compareFunction: SamplerCompareFunction = SamplerCompareFunction.Never,
33
+ _anisotropy: number = 1,
34
+ label: string = "") : WebGLSampler
35
+ {
36
+ const sampler = gl.createSampler();
37
+
38
+ const glMinFilter = WebGlConverter.convertMinFIlter(gl, minFilter, mipMapFilter);
39
+ const glMagFilter = WebGlConverter.convertMagFilter(gl, magFilter);
40
+ const glAddressModeU = WebGlConverter.convertAddressMode(gl, addressModeU);
41
+ const glAddressModeV = WebGlConverter.convertAddressMode(gl, addressModeV);
42
+ const glAddressModeW = WebGlConverter.convertAddressMode(gl, addressModeW);
43
+
44
+
45
+ gl.samplerParameteri(sampler, gl.TEXTURE_MIN_FILTER, glMinFilter);
46
+ gl.samplerParameteri(sampler, gl.TEXTURE_MAG_FILTER, glMagFilter);
47
+ gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_S, glAddressModeU);
48
+ gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_T, glAddressModeV);
49
+ gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_R, glAddressModeW);
50
+ if (compareFunction != SamplerCompareFunction.Never)
51
+ {
52
+ // gl.samplerParameteri(sampler, gl.TEXTURE_COMPARE_FUNC, WebGLConverter.convertCompareFunction(gl, compareFunction));
53
+ gl.samplerParameteri(sampler, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);
54
+ }
55
+ else
56
+ {
57
+ gl.samplerParameteri(sampler, gl.TEXTURE_COMPARE_MODE, gl.NONE);
58
+ }
59
+
60
+ if(label && label.length > 0)
61
+ {
62
+ SpectorJSUtilities.setLabel(sampler, label);
63
+ }
64
+
65
+ //if (anisotropy > 1)
66
+ //{
67
+ // // Check if anisotropic filtering is supported
68
+ // if (gl.Extensions.Contains("GL_EXT_texture_filter_anisotropic"))
69
+ // {
70
+ // float maxAnisotropy = gl.GetFloat(GLEnum.MaxTextureMaxAnisotropyExt);
71
+ // float anisotropyLevel = Math.Min(anisotropy, maxAnisotropy);
72
+ // gl.SamplerParameter(sampler, GLEnum.TextureMaxAnisotropyExt, anisotropyLevel);
73
+ // }
74
+ // else
75
+ // {
76
+ // Console.WriteLine("Anisotropic filtering is not supported on this device.");
77
+ // }
78
+ //}
79
+ return sampler;
80
+ }
81
+ }
@@ -0,0 +1,48 @@
1
+
2
+ /**
3
+ * This module provides utility functions for working with WebGL shaders, including shader creation and compilation.
4
+ */
5
+ export class WebGlShaderUtilities {
6
+
7
+ /**
8
+ * Creates a WebGL shader of the specified type using the provided shader source code.
9
+ * @param gl The WebGL2 rendering context.
10
+ * @param shaderSource The source code of the shader.
11
+ * @param shaderType The type of the shader (e.g., gl.VERTEX_SHADER or gl.FRAGMENT_SHADER).
12
+ * @returns The compiled WebGLShader object.
13
+ */
14
+ public create(gl: WebGL2RenderingContext, shaderSource: string ,shaderType : GLenum): WebGLShader {
15
+ const shader = gl.createShader(shaderType);
16
+ if (!shader) {
17
+ throw new Error("Failed to create shader.");
18
+ }
19
+ gl.shaderSource(shader, shaderSource);
20
+ gl.compileShader(shader);
21
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
22
+ const infoLog = gl.getShaderInfoLog(shader);
23
+ gl.deleteShader(shader);
24
+ throw new Error(`Failed to compile shader: ${infoLog}`);
25
+ }
26
+ return shader;
27
+ }
28
+
29
+ /**
30
+ * Creates a vertex shader using the provided shader source code.
31
+ * @param gl The WebGL2 rendering context.
32
+ * @param shaderSource The source code of the vertex shader.
33
+ * @returns The compiled WebGLShader object representing the vertex shader.
34
+ */
35
+ public createVertexShader(gl: WebGL2RenderingContext, shaderSource: string): WebGLShader {
36
+ return this.create(gl, shaderSource, gl.VERTEX_SHADER);
37
+ }
38
+
39
+ /**
40
+ * Creates a fragment shader using the provided shader source code.
41
+ * @param gl The WebGL2 rendering context.
42
+ * @param shaderSource The source code of the fragment shader.
43
+ * @returns The compiled WebGLShader object representing the fragment shader.
44
+ */
45
+ public createFragmentShader(gl: WebGL2RenderingContext, shaderSource: string): WebGLShader {
46
+ return this.create(gl, shaderSource, gl.FRAGMENT_SHADER);
47
+ }
48
+ }
@@ -0,0 +1,162 @@
1
+ import {WebGlConverter} from "./WebGlConverter.ts";
2
+ import {TextureFormat} from "ris-framework-api";
3
+ import {vec2} from "gl-matrix";
4
+
5
+ /**
6
+ * The utilities for working with WebGL textures.
7
+ */
8
+ export class WebGlTextureUtilities {
9
+
10
+ /**
11
+ * Creates a 2D texture.
12
+ * @param gl The WebGL rendering context.
13
+ * @param width The width of the texture.
14
+ * @param height The height of the texture.
15
+ * @param data The texture data. If null, an uninitialized texture will be created.
16
+ * @param textureFormat The format of the texture. If not specified, BGRA_8_UNORM will be used.
17
+ * @param useMipMaps True to generate mipmaps for the texture, false otherwise. By default, it is false.
18
+ * @param anisotropy The level of anisotropic filtering to use when sampling the texture. A value of 1 means no anisotropic filtering, while higher values (e.g., 4, 8, 16) indicate increasing levels of anisotropic filtering. By default, it is 1 (no anisotropic filtering).
19
+ * @param label The label for the texture. This can be used for debugging purposes to identify the texture in graphics debuggers.
20
+ * @returns The created WebGL texture.
21
+ */
22
+ public createTexture2D(
23
+ gl: WebGL2RenderingContext,
24
+ width: number, height: number,
25
+ data: Uint8Array[] | HTMLImageElement[] | null = null,
26
+ textureFormat = TextureFormat.RGBA_8_UNORM,
27
+ useMipMaps = false,
28
+ anisotropy = 1.0,
29
+ label: string | null = null
30
+ ): WebGLTexture {
31
+
32
+ const texture = gl.createTexture();
33
+
34
+ if (label != null) {
35
+ (texture as any).__SPECTOR_Metadata = {
36
+ name: label,
37
+ };
38
+ }
39
+
40
+ gl.bindTexture(gl.TEXTURE_2D, texture);
41
+
42
+ const internalFormat = WebGlConverter.convertInternalFormat(gl, textureFormat);
43
+ const format = WebGlConverter.convertToPixelFormat(gl, textureFormat);
44
+
45
+ let mipLevels = data?.length ?? 1;
46
+ // We can generate only if mip levels are not provided.
47
+ const generateMipMaps = useMipMaps && mipLevels <= 1;
48
+ if (generateMipMaps) {
49
+ mipLevels = Math.floor(Math.log2(Math.max(width, height))) + 1;
50
+ }
51
+
52
+ gl.texStorage2D(gl.TEXTURE_2D, mipLevels, internalFormat, width, height);
53
+
54
+ if(data) {
55
+ for (let i = 0; i < mipLevels; i++) {
56
+
57
+ const mipLevelData = data[i];
58
+ const levelWidth = width >> i;
59
+ const levelHeight = height >> i;
60
+
61
+ if (mipLevelData instanceof HTMLImageElement) {
62
+ gl.texSubImage2D(gl.TEXTURE_2D, i, 0, 0, levelWidth, levelHeight, format, gl.UNSIGNED_BYTE, mipLevelData);
63
+ } else if (mipLevelData instanceof Uint8Array) {
64
+ gl.texSubImage2D(gl.TEXTURE_2D, i, 0, 0, levelWidth, levelHeight, format, gl.UNSIGNED_BYTE, mipLevelData);
65
+ } else if (mipLevelData) {
66
+ gl.texSubImage2D(gl.TEXTURE_2D, i, 0, 0, levelWidth, levelHeight, format, gl.UNSIGNED_BYTE, mipLevelData);
67
+ }
68
+ else if(!mipLevelData) {
69
+ // If not must be empty array, so we can safely ignore it.
70
+ }
71
+ else {
72
+ throw new Error("unsupported data type");
73
+ }
74
+ }
75
+ }
76
+
77
+ // Generate mipmaps.
78
+ if (generateMipMaps) {
79
+ gl.generateMipmap(gl.TEXTURE_2D);
80
+ }
81
+
82
+ // Set anisotropy.
83
+ if (anisotropy > 1.0) {
84
+ throw new Error("Not implemented yet.");
85
+ //OpenGLESUtilities.Anisotropy.SetAnisotropy(gl, texture, anisotropy);
86
+ }
87
+
88
+ return texture;
89
+ }
90
+
91
+ /**
92
+ * Creates a compressed 2D texture.
93
+ * @param gl The WebGL rendering context.
94
+ * @param dimension The dimensions of the texture.
95
+ * @param blockSize The block size of a compressed format.
96
+ * @param data The texture data. If null, an uninitialized texture will be created.
97
+ * @param textureFormat The format of the texture. If not specified, BGRA_8_UNORM will be used.
98
+ * @param anisotropy The level of anisotropic filtering to use when sampling the texture. A value of 1 means no anisotropic filtering, while higher values (e.g., 4, 8, 16) indicate increasing levels of anisotropic filtering. By default, it is 1 (no anisotropic filtering).
99
+ * @param label The label for the texture. This can be used for debugging purposes to identify the texture in graphics debuggers.
100
+ * @returns The created WebGL texture.
101
+ */
102
+ public createCompressedTexture2D(
103
+ gl: WebGL2RenderingContext,
104
+ dimension: vec2, _blockSize: vec2,
105
+ data: Uint8Array[] | null = null,
106
+ label: string | null = null,
107
+ textureFormat = TextureFormat.RGBA_8_UNORM,
108
+ anisotropy = 1.0,
109
+ ): WebGLTexture {
110
+
111
+ const baseWidth = dimension[0];
112
+ const baseHeight = dimension[1];
113
+ const mipLevels = data?.length ?? 1;
114
+
115
+ const texture = gl.createTexture();
116
+
117
+ if (label != null) {
118
+ (texture as any).__SPECTOR_Metadata = {
119
+ name: label,
120
+ };
121
+ }
122
+
123
+ gl.bindTexture(gl.TEXTURE_2D, texture);
124
+
125
+ const internalFormat = WebGlConverter.convertInternalFormat(gl, textureFormat);
126
+
127
+ gl.texStorage2D(gl.TEXTURE_2D, mipLevels, internalFormat, baseWidth, baseHeight);
128
+
129
+ if(data && data.length > 0) {
130
+
131
+ let width = baseWidth;
132
+ let height = baseHeight;
133
+
134
+ for (let i = 0; i < mipLevels; i++) {
135
+
136
+ const mipLevelData = data[i];
137
+
138
+ if (mipLevelData instanceof Uint8Array) {
139
+ gl.compressedTexSubImage2D(gl.TEXTURE_2D, i, 0, 0, width, height, internalFormat, mipLevelData, 0);
140
+ } else {
141
+ throw new Error("unsupported data type");
142
+ }
143
+
144
+ width /= 2;
145
+ height /= 2;
146
+ }
147
+ }
148
+
149
+ // Generate mipmaps.
150
+ if (mipLevels > 1) {
151
+ gl.generateMipmap(gl.TEXTURE_2D);
152
+ }
153
+
154
+ // Set anisotropy.
155
+ if (anisotropy > 1.0) {
156
+ throw new Error("Not implemented yet.");
157
+ //OpenGLESUtilities.Anisotropy.SetAnisotropy(gl, texture, anisotropy);
158
+ }
159
+
160
+ return texture;
161
+ }
162
+ }
@@ -0,0 +1,71 @@
1
+ import { WebGLBlendingUtilities } from "./webgl-blending-utilities";
2
+ import { WebGlBufferUtilities } from "./WebGlBufferUtilities.ts";
3
+ import { WebGLCullingUtilities } from "./webgl-culling-utilities";
4
+ import { WebGLFilterAnisotropicUtilities } from "./webgl-filter-anisotropic-utilities";
5
+ import { WebGlFrameBufferUtilities } from "./WebGlFrameBufferUtilities.ts";
6
+ import { WebGLProgramUtilities } from "./webgl-program-utilities";
7
+ import { WebGLRenderbufferUtilities } from "./webgl-renderbuffer-utilities";
8
+ import { WebGlSamplerUtilities } from "./WebGlSamplerUtilities.ts";
9
+ import { WebGlShaderUtilities } from "./WebGlShaderUtilities.ts";
10
+ import { WebGlTextureUtilities } from "./WebGlTextureUtilities.ts";
11
+
12
+ /**
13
+ * The utility class for WebGL operations.
14
+ */
15
+ export class WebGlUtilities {
16
+
17
+ /**
18
+ * The WebGL blending utilities.
19
+ */
20
+ public static readonly blending = new WebGLBlendingUtilities();
21
+
22
+ /**
23
+ * The WebGL anisotropic filtering utilities.
24
+ */
25
+ public static readonly anisotropy = new WebGLFilterAnisotropicUtilities();
26
+
27
+ /**
28
+ * The WebGL culling utilities.
29
+ */
30
+ public static readonly culling = new WebGLCullingUtilities();
31
+
32
+ /**
33
+ * The WebGL texture utilities.
34
+ */
35
+ public static readonly texture = new WebGlTextureUtilities();
36
+
37
+ /**
38
+ * The WebGL framebuffer utilities.
39
+ */
40
+ public static readonly framebuffer = new WebGlFrameBufferUtilities();
41
+
42
+ /**
43
+ * The WebGL render buffer utilities.
44
+ */
45
+ public static readonly renderbuffer = new WebGLRenderbufferUtilities();
46
+
47
+ /**
48
+ * The WebGL buffer utilities.
49
+ */
50
+ public static readonly buffer = new WebGlBufferUtilities();
51
+
52
+ /**
53
+ * The WebGL render buffer utilities.
54
+ */
55
+ public static readonly renderBuffer = new WebGLRenderbufferUtilities();
56
+
57
+ /**
58
+ * The WebGL sampler utilities.
59
+ */
60
+ public static readonly sampler = new WebGlSamplerUtilities();
61
+
62
+ /**
63
+ * The WebGL shader utilities.
64
+ */
65
+ public static readonly shader = new WebGlShaderUtilities();
66
+
67
+ /**
68
+ * The WebGL program utilities.
69
+ */
70
+ public static readonly program = new WebGLProgramUtilities();
71
+ }
@@ -0,0 +1,13 @@
1
+ export class SpectorJSUtilities {
2
+
3
+ /**
4
+ * Sets the label for a WebGL object to be used by Spector.js.
5
+ * @param target The WebGL object to set the label for (e.g., WebGLBuffer, WebGLTexture, WebGLFramebuffer, etc.).
6
+ * @param label The label to set for the WebGL object. This will be displayed in Spector.js for easier identification.
7
+ */
8
+ public static setLabel(target: any, label: string): void {
9
+ if (target) {
10
+ (target as any).__SPECTOR_Metadata = { name: label };
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The utility class for WebGL blending operations.
3
+ */
4
+ export class WebGLBlendingUtilities {
5
+ /**
6
+ * Enables blending with the specified parameters.
7
+ * @param gl The WebGL2RenderingContext context.
8
+ * @param colorFunc The color function. By default, it is FUNC_ADD.
9
+ * @param srcColorFactor The source color factor. By default, it is SRC_ALPHA.
10
+ * @param dstColorFactor The destination color factor. By default, it is ONE_MINUS_SRC_ALPHA.
11
+ * @param alphaFunc The alpha function. By default, it is FUNC_ADD.
12
+ * @param srcAlphaFactor The source alpha factor. By default, it is SRC_ALPHA.
13
+ * @param dstAlphaFactor The destination alpha factor. By default, it is ONE_MINUS_SRC_ALPHA.
14
+ */
15
+ public enableBlend(
16
+ gl: WebGL2RenderingContext,
17
+ colorFunc: number | null = null,
18
+ srcColorFactor: number | null = null,
19
+ dstColorFactor: number | null = null,
20
+ alphaFunc: number | null = null,
21
+ srcAlphaFactor: number | null = null,
22
+ dstAlphaFactor: number | null = null,
23
+ ): void {
24
+ colorFunc = colorFunc ?? gl.FUNC_ADD;
25
+ srcColorFactor = srcColorFactor ?? gl.SRC_ALPHA;
26
+ dstColorFactor = dstColorFactor ?? gl.ONE_MINUS_SRC_ALPHA;
27
+ alphaFunc = alphaFunc ?? gl.FUNC_ADD;
28
+ srcAlphaFactor = srcAlphaFactor ?? gl.SRC_ALPHA;
29
+ dstAlphaFactor = dstAlphaFactor ?? gl.ONE_MINUS_SRC_ALPHA;
30
+
31
+ gl.enable(gl.BLEND);
32
+ gl.blendFuncSeparate(
33
+ srcColorFactor,
34
+ dstColorFactor,
35
+ srcAlphaFactor,
36
+ dstAlphaFactor,
37
+ );
38
+ gl.blendEquationSeparate(colorFunc, alphaFunc);
39
+ }
40
+
41
+
42
+ /*
43
+ public setBlend(
44
+ gl: WebGL2RenderingContext,
45
+ blendState: WebGlBlendState,
46
+ ): void {
47
+ if (!blendState.blendingEnabled) {
48
+ gl.disable(gl.BLEND);
49
+ return;
50
+ }
51
+
52
+ const color = blendState.color;
53
+ const alpha = blendState.alpha;
54
+ const colorFunc = WebGLConverter.convertBlendOperation(gl, color.operation);
55
+ const alphaFunc = WebGLConverter.convertBlendOperation(gl, alpha.operation);
56
+ const srcAlphaFactor = WebGLConverter.convertBlendFactor(
57
+ gl,
58
+ alpha.srcFactor,
59
+ );
60
+ const dstAlphaFactor = WebGLConverter.convertBlendFactor(
61
+ gl,
62
+ alpha.dstFactor,
63
+ );
64
+ const srcColorFactor = WebGLConverter.convertBlendFactor(
65
+ gl,
66
+ color.srcFactor,
67
+ );
68
+ const dstColorFactor = WebGLConverter.convertBlendFactor(
69
+ gl,
70
+ color.dstFactor,
71
+ );
72
+
73
+ gl.enable(gl.BLEND);
74
+ gl.blendFuncSeparate(
75
+ srcColorFactor,
76
+ dstColorFactor,
77
+ srcAlphaFactor,
78
+ dstAlphaFactor,
79
+ );
80
+ gl.blendEquationSeparate(colorFunc, alphaFunc);
81
+ }
82
+
83
+
84
+ */
85
+ }
@@ -0,0 +1,23 @@
1
+ import { Culling } from "../../core/renderer/enums";
2
+ import { WebGlConverter } from "./WebGlConverter.ts";
3
+
4
+ /**
5
+ * The WebGL culling utilities.
6
+ */
7
+ export class WebGLCullingUtilities {
8
+
9
+ /**
10
+ * Sets the culling mode.
11
+ * @param gl The WebGL2RenderingContext.
12
+ * @param culling The culling mode.
13
+ */
14
+ public setCulling(gl: WebGL2RenderingContext, culling: Culling) {
15
+ if (culling == Culling.None) {
16
+ gl.disable(gl.CULL_FACE);
17
+ }
18
+ else {
19
+ gl.enable(gl.CULL_FACE);
20
+ gl.cullFace(WebGlConverter.convertCulling(gl, culling));
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The utility class for WebGL anisotropic filtering operations.
3
+ */
4
+ export class WebGLFilterAnisotropicUtilities {
5
+ /**
6
+ * The max anisotropy extension string.
7
+ */
8
+ private MAX_ANISOTROPY_EXT: string = "EXT_texture_filter_anisotropic";
9
+
10
+ private _extension: any;
11
+
12
+ /**
13
+ * Gets the max anisotropy.
14
+ * @param gl The WebGL2 rendering context.
15
+ * @returns The maximum supported level of anisotropic filtering. <c>0</c> if not supported.
16
+ */
17
+ public getMaxAnisotropy(gl: WebGL2RenderingContext): number {
18
+ this._extension = gl.getExtension(this.MAX_ANISOTROPY_EXT);
19
+
20
+ if (this._extension) {
21
+ return gl.getParameter(this._extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
22
+ }
23
+
24
+ return 0;
25
+ }
26
+
27
+ /**
28
+ * Sets the anisotropy filtering level for texture.
29
+ * @param gl The WebGL2 rendering context.
30
+ * @param texture The WebGL texture.
31
+ * @param anisotropy The level of anisotropic filtering
32
+ */
33
+ public setAnisotropy(gl: WebGL2RenderingContext, texture: WebGLTexture, anisotropy: number) {
34
+ gl.bindTexture(gl.TEXTURE_2D, texture);
35
+ gl.texParameterf(gl.TEXTURE_2D, this._extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
36
+ }
37
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * This module provides utility functions for working with WebGL programs, such as creating and linking shader programs.
3
+ */
4
+ export class WebGLProgramUtilities {
5
+
6
+ /**
7
+ * Creates a WebGL program with the given vertex shader and fragment shader.
8
+ * @param gl The WebGL2 rendering context.
9
+ * @param vertexShader The vertex shader to be attached to the program.
10
+ * @param fragmentShader The fragment shader to be attached to the program.
11
+ * @returns The linked WebGLProgram object.
12
+ */
13
+ public create(gl: WebGL2RenderingContext, vertexShader: WebGLShader, fragmentShader: WebGLShader): WebGLProgram {
14
+ const program = gl.createProgram();
15
+ if (!program) {
16
+ throw new Error("Failed to create program.");
17
+ }
18
+ gl.attachShader(program, vertexShader);
19
+ gl.attachShader(program, fragmentShader);
20
+ gl.linkProgram(program);
21
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
22
+ const infoLog = gl.getProgramInfoLog(program);
23
+ gl.deleteProgram(program);
24
+ throw new Error(`Failed to link program: ${infoLog}`);
25
+ }
26
+ return program;
27
+ }
28
+
29
+ }
@@ -0,0 +1,23 @@
1
+ import type { TextureFormat } from "ris-framework-api";
2
+ import { WebGlConverter } from "./WebGlConverter.ts";
3
+
4
+ export class WebGLRenderBufferUtilities {
5
+
6
+
7
+ public create(gl: WebGL2RenderingContext, internalFormat: TextureFormat, width: number, height: number, label: string | null = null): WebGLRenderbuffer {
8
+
9
+ const glFormat = WebGlConverter.convertInternalFormat(gl, internalFormat);
10
+ const renderBuffer = gl.createRenderbuffer();
11
+ gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer);
12
+ gl.renderbufferStorage(gl.RENDERBUFFER,
13
+ glFormat,
14
+ width,
15
+ height);
16
+
17
+ if (label !== null && label !== undefined && label !== "") {
18
+ (renderBuffer as any).__SPECTOR_Metadata = { name: label };
19
+ }
20
+
21
+ return renderBuffer;
22
+ }
23
+ }
@@ -0,0 +1,35 @@
1
+ import type { TextureFormat } from "ris-framework-api";
2
+ import { SpectorJSUtilities } from "./spector-js-utilities";
3
+ import { WebGlConverter } from "./WebGlConverter.ts";
4
+
5
+ /**
6
+ * The utility class for WebGL render buffer operations. It provides methods for creating render buffers with specified formats, dimensions, and labels. Render buffers are used in WebGL for off-screen rendering, particularly for depth and stencil attachments in framebuffers. This class abstracts the details of creating and configuring render buffers, making it easier to manage them in a WebGL application.
7
+ */
8
+ export class WebGLRenderbufferUtilities {
9
+ /**
10
+ * Creates a render buffer with the specified internal format, width, height, and label.
11
+ * @param gl The WebGL2 rendering context.
12
+ * @param internalFormat The internal format of the render buffer, which determines the type of data stored in the buffer (e.g., depth, stencil, or depth-stencil).
13
+ * @param width The width of the render buffer in pixels.
14
+ * @param height The height of the render buffer in pixels.
15
+ * @param label The label of the render buffer, which can be used for debugging purposes. It is optional and can be null or undefined.
16
+ * @returns The created WebGLRenderbuffer object.
17
+ */
18
+ public create(gl: WebGL2RenderingContext, internalFormat: TextureFormat, width: number, height: number, label: string | null | undefined): WebGLRenderbuffer {
19
+ const intFormat = WebGlConverter.convertInternalFormat(gl, internalFormat);
20
+
21
+ const renderBuffer = gl.createRenderbuffer();
22
+ gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer);
23
+ gl.renderbufferStorage(gl.RENDERBUFFER,
24
+ intFormat,
25
+ width,
26
+ height);
27
+
28
+ if (label) {
29
+ SpectorJSUtilities.setLabel(renderBuffer, label);
30
+ }
31
+
32
+ return renderBuffer;
33
+ }
34
+
35
+ }