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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/assets/_ris-ktx2-node-module-stub-DwDS30PT.js +1 -0
- package/dist/assets/cat.jpg +0 -0
- package/dist/assets/index-BmeGit54.css +1 -0
- package/dist/assets/index-qyn4pzGw.js +151 -0
- package/dist/assets/libktx-Cjh8w7XG-Ci1Fm_MH.js +1 -0
- package/dist/assets/test.png +0 -0
- package/dist/index.html +16 -0
- package/dist/vite.svg +1 -0
- package/package.json +37 -0
- package/shaders/glsl/inspect_texture_mips_fs.glsl +18 -0
- package/shaders/glsl/inspect_texture_mips_vs.glsl +25 -0
- package/shaders/glsl/main_render_target_flip_y_fs.glsl +9 -0
- package/shaders/glsl/main_render_target_flip_y_vs.glsl +9 -0
- package/shaders/glsl/sprite_fs.glsl +14 -0
- package/shaders/glsl/sprite_vs.glsl +20 -0
- package/shaders/glsl/unlit_material_fs.glsl +19 -0
- package/shaders/glsl/unlit_material_vs.glsl +25 -0
- package/src/TextureFormat.ts +0 -0
- package/src/VertexFormat.ts +17 -0
- package/src/common/sampler-enums.ts +21 -0
- package/src/common/vertex-step-mode.ts +12 -0
- package/src/content/ShaderModuleContent.ts +47 -0
- package/src/core/Framework.ts +240 -0
- package/src/core/FrameworkConfig.ts +31 -0
- package/src/core/buffers/buffers-factory-interface.ts +0 -0
- package/src/core/buffers/vertex-buffer-interface.ts +0 -0
- package/src/core/content/ContentManager.ts +86 -0
- package/src/core/dependency-injection/register-services-interface.ts +15 -0
- package/src/core/framework-interface.ts +0 -0
- package/src/core/input/InputManager.ts +115 -0
- package/src/core/renderer/enums.ts +20 -0
- package/src/core/renderer/renderer-interface.ts +92 -0
- package/src/core/renderer/rendering-limits.ts +20 -0
- package/src/core/rendering/AGraphicsDevice.ts +119 -0
- package/src/core/rendering/ARenderer.ts +247 -0
- package/src/core/rendering/blending/blend-component-descriptor.ts +33 -0
- package/src/core/rendering/blending/blend-state-descriptor.ts +51 -0
- package/src/core/rendering/blending/enums.ts +50 -0
- package/src/core/rendering/enums.ts +22 -0
- package/src/core/rendering/primitive/PrimitiveStateDescriptor.ts +32 -0
- package/src/core/rendering/primitive/enums.ts +68 -0
- package/src/core/rendering/render-pass/render-pass-descriptor.ts +0 -0
- package/src/core/rendering/render-pass/render-pass-interface.ts +17 -0
- package/src/core/rendering/sampler/sampler-descriptor.ts +33 -0
- package/src/core/rendering/texture/texture.ts +146 -0
- package/src/core/rendering/vertex-attribute.ts +20 -0
- package/src/core/rendering/vertex-buffer-layout.ts +84 -0
- package/src/core/shader/shader-loader.ts +27 -0
- package/src/core/shader/shader-module-interface.ts +1 -0
- package/src/core/sprite-batch/SpriteBatch.ts +216 -0
- package/src/core/sprite-batch/SpriteBatchDrawable.ts +146 -0
- package/src/core/sprite-batch/SpriteBatchMesh.ts +243 -0
- package/src/core/window/window-manager.ts +76 -0
- package/src/counter.ts +9 -0
- package/src/geometry/BaseGeometry.ts +148 -0
- package/src/geometry/GeometryBuilder.ts +58 -0
- package/src/geometry/GeometryFormat.ts +37 -0
- package/src/main.ts +97 -0
- package/src/style.css +96 -0
- package/src/typescript.svg +1 -0
- package/src/webgl/WebGLGraphicsDeviceFeatures.ts +57 -0
- package/src/webgl/WebGlGpuInfo.ts +76 -0
- package/src/webgl/WebGlGraphicsDevice.ts +129 -0
- package/src/webgl/WebGlRenderer.ts +31 -0
- package/src/webgl/blending/webgl-blend-state.ts +59 -0
- package/src/webgl/buffers/WebGlBuffersFactory.ts +75 -0
- package/src/webgl/buffers/WebGlUniformBuffer.ts +87 -0
- package/src/webgl/buffers/WebGlVertexBuffer.ts +100 -0
- package/src/webgl/buffers/webgl-index-buffer.ts +110 -0
- package/src/webgl/cast/cast.ts +100 -0
- package/src/webgl/primitive/WebGlPrimitiveState.ts +60 -0
- package/src/webgl/render-pass/WebGlRenderPass.ts +186 -0
- package/src/webgl/render-pipelines/AWebGlRenderPipeline.ts +129 -0
- package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +95 -0
- package/src/webgl/render-pipelines/WebGlRenderPipelineFactory.ts +75 -0
- package/src/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.ts +200 -0
- package/src/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.ts +197 -0
- package/src/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.ts +148 -0
- package/src/webgl/rendering/webgl-vertex-attribute.ts +16 -0
- package/src/webgl/rendering/webgl-vertex-buffer-layout.ts +64 -0
- package/src/webgl/sampler/webgl-sampler.ts +110 -0
- package/src/webgl/shader/WebGlShaderModule.ts +110 -0
- package/src/webgl/shader/WebGlShaderModuleLoader.ts +40 -0
- package/src/webgl/swap-chain/WebGlSwapChain.ts +56 -0
- package/src/webgl/texture/WebGlTexture2D.ts +133 -0
- package/src/webgl/texture/WebGlTextureFactory.ts +184 -0
- package/src/webgl/utilities/WebGlBufferUtilities.ts +124 -0
- package/src/webgl/utilities/WebGlConverter.ts +391 -0
- package/src/webgl/utilities/WebGlFrameBufferUtilities.ts +240 -0
- package/src/webgl/utilities/WebGlSamplerUtilities.ts +81 -0
- package/src/webgl/utilities/WebGlShaderUtilities.ts +48 -0
- package/src/webgl/utilities/WebGlTextureUtilities.ts +162 -0
- package/src/webgl/utilities/WebGlUtilities.ts +71 -0
- package/src/webgl/utilities/spector-js-utilities.ts +13 -0
- package/src/webgl/utilities/webgl-blending-utilities.ts +85 -0
- package/src/webgl/utilities/webgl-culling-utilities.ts +23 -0
- package/src/webgl/utilities/webgl-filter-anisotropic-utilities.ts +37 -0
- package/src/webgl/utilities/webgl-program-utilities.ts +29 -0
- package/src/webgl/utilities/webgl-render-buffer-utilities.ts +23 -0
- package/src/webgl/utilities/webgl-renderbuffer-utilities.ts +35 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import {Culling} from "../../core/renderer/enums";
|
|
2
|
+
import {BlendFactor, BlendOperation} from "../../core/rendering/blending/enums";
|
|
3
|
+
import {CullMode, FrontFace, PrimitiveTopology} from "../../core/rendering/primitive/enums";
|
|
4
|
+
import {VertexFormat} from "../../VertexFormat.ts";
|
|
5
|
+
import {BufferUsage, MipMapSamplerFilter, SamplerAddressMode, SamplerFilter, TextureFormat} from "ris-framework-api";
|
|
6
|
+
|
|
7
|
+
export class WebGlConverter {
|
|
8
|
+
|
|
9
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants
|
|
10
|
+
|
|
11
|
+
/** BC3 */
|
|
12
|
+
private static GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
|
|
13
|
+
|
|
14
|
+
/** BC7 */
|
|
15
|
+
private static GL_COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C;
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/** ETC2 */
|
|
19
|
+
private static GL_COMPRESSED_RGBA8_ETC2_EAC = 0x9278;
|
|
20
|
+
|
|
21
|
+
private static GL_COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93B0;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Converts PrimitiveTopology to WebGL enum.
|
|
25
|
+
* @param topology The PrimitiveTopology.
|
|
26
|
+
* @returns The WebGL enum.
|
|
27
|
+
*/
|
|
28
|
+
public static convertPrimitiveType(topology: PrimitiveTopology): number {
|
|
29
|
+
|
|
30
|
+
switch (topology) {
|
|
31
|
+
case PrimitiveTopology.TRIANGLE_LIST:
|
|
32
|
+
return WebGL2RenderingContext.TRIANGLES;
|
|
33
|
+
case PrimitiveTopology.LINE_LIST:
|
|
34
|
+
return WebGL2RenderingContext.LINES;
|
|
35
|
+
case PrimitiveTopology.LINE_STRIP:
|
|
36
|
+
return WebGL2RenderingContext.LINE_STRIP;
|
|
37
|
+
case PrimitiveTopology.POINT_LIST:
|
|
38
|
+
return WebGL2RenderingContext.POINTS;
|
|
39
|
+
default:
|
|
40
|
+
throw new Error("Method not implemented.");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Converts CullMode to WebGL enum.
|
|
46
|
+
* @param cullFace The CullMode.
|
|
47
|
+
* @return The WebGL enum.
|
|
48
|
+
*/
|
|
49
|
+
public static convertCullFace(cullFace: CullMode): number {
|
|
50
|
+
|
|
51
|
+
switch (cullFace) {
|
|
52
|
+
case CullMode.NONE:
|
|
53
|
+
return WebGL2RenderingContext.NONE;
|
|
54
|
+
case CullMode.BACK:
|
|
55
|
+
return WebGL2RenderingContext.BACK;
|
|
56
|
+
case CullMode.FRONT:
|
|
57
|
+
return WebGL2RenderingContext.FRONT;
|
|
58
|
+
default: throw new Error("Method not implemented.");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Converts FrontFace to WebGL enum.
|
|
65
|
+
* @param frontFace The FrontFace.
|
|
66
|
+
* @return The WebGL enum.
|
|
67
|
+
*/
|
|
68
|
+
public static convertFrontFace(frontFace: FrontFace): number {
|
|
69
|
+
switch (frontFace) {
|
|
70
|
+
case FrontFace.CW:
|
|
71
|
+
return WebGL2RenderingContext.CW;
|
|
72
|
+
case FrontFace.CCW:
|
|
73
|
+
return WebGL2RenderingContext.CCW;
|
|
74
|
+
default:
|
|
75
|
+
throw new Error("Method not implemented.");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Converts BufferUsage to WebGL enum.
|
|
81
|
+
* @param usage The BufferUsage.
|
|
82
|
+
* @return The WebGL enum.
|
|
83
|
+
*/
|
|
84
|
+
public static convertBufferUsage(usage: BufferUsage): GLenum {
|
|
85
|
+
|
|
86
|
+
let bufferUsage: number = WebGL2RenderingContext.STATIC_DRAW;
|
|
87
|
+
|
|
88
|
+
if((usage & BufferUsage.COPY_DST) == BufferUsage.COPY_DST)
|
|
89
|
+
{
|
|
90
|
+
bufferUsage = WebGL2RenderingContext.DYNAMIC_DRAW;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return bufferUsage;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Conversts BlendOperation to WebGL enum.
|
|
97
|
+
* @parma gl The WebGL2RenderingContext.
|
|
98
|
+
* @param blendOperation The BlendOperation.
|
|
99
|
+
* @return The WebGL enum.
|
|
100
|
+
*/
|
|
101
|
+
public static convertBlendOperation(
|
|
102
|
+
gl: WebGL2RenderingContext,
|
|
103
|
+
blendOperation: BlendOperation,
|
|
104
|
+
): GLenum {
|
|
105
|
+
switch (blendOperation) {
|
|
106
|
+
case BlendOperation.ADD:
|
|
107
|
+
return gl.FUNC_ADD;
|
|
108
|
+
case BlendOperation.SUBTRACT:
|
|
109
|
+
return gl.FUNC_SUBTRACT;
|
|
110
|
+
case BlendOperation.REVERSE_SUBTRACT:
|
|
111
|
+
return gl.FUNC_REVERSE_SUBTRACT;
|
|
112
|
+
case BlendOperation.MIN:
|
|
113
|
+
return gl.MIN;
|
|
114
|
+
case BlendOperation.MAX:
|
|
115
|
+
return gl.MAX;
|
|
116
|
+
default:
|
|
117
|
+
throw new Error("NotImplementedException");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Converts BlendFactor to WebGL enum.
|
|
123
|
+
* @param gl The WebGL2RenderingContext.
|
|
124
|
+
* @param blendingFactor The BlendFactor.
|
|
125
|
+
* @returns The WebGL enum.
|
|
126
|
+
*/
|
|
127
|
+
public static convertBlendFactor(
|
|
128
|
+
gl: WebGL2RenderingContext,
|
|
129
|
+
blendingFactor: BlendFactor,
|
|
130
|
+
): number {
|
|
131
|
+
switch (blendingFactor) {
|
|
132
|
+
// case BlendFactor.ZERO:
|
|
133
|
+
// return gl.ZERO;
|
|
134
|
+
case BlendFactor.ONE:
|
|
135
|
+
return gl.ONE;
|
|
136
|
+
// case BlendFactor.SRC_COLOR:
|
|
137
|
+
// return gl.SRC_COLOR;
|
|
138
|
+
case BlendFactor.SRC_ALPHA:
|
|
139
|
+
return gl.SRC_ALPHA;
|
|
140
|
+
case BlendFactor.ONE_MINUS_SRC_ALPHA:
|
|
141
|
+
return gl.ONE_MINUS_SRC_ALPHA;
|
|
142
|
+
// case BlendFactor.DST_ALPHA:
|
|
143
|
+
// return gl.DST_ALPHA;
|
|
144
|
+
// case BlendFactor.ONE_MINUS_DST_ALPHA:
|
|
145
|
+
// return gl.ONE_MINUS_DST_ALPHA;
|
|
146
|
+
default:
|
|
147
|
+
throw new Error("NotImplementedException");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Converts Culling to WebGL enum.
|
|
153
|
+
* @param gl The WebGL2RenderingContext.
|
|
154
|
+
* @param culling The Culling.
|
|
155
|
+
* @returns The WebGL enum.
|
|
156
|
+
*/
|
|
157
|
+
public static convertCulling(gl: WebGL2RenderingContext, culling: Culling): number {
|
|
158
|
+
switch (culling) {
|
|
159
|
+
case Culling.Front:
|
|
160
|
+
return gl.FRONT;
|
|
161
|
+
case Culling.Back:
|
|
162
|
+
return gl.BACK;
|
|
163
|
+
case Culling.None:
|
|
164
|
+
return gl.NONE;
|
|
165
|
+
default:
|
|
166
|
+
throw new Error("NotImplementedException");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Converts TextureFormat to WebGL internal format, format, and type.
|
|
172
|
+
* @param gl The WebGL2RenderingContext.
|
|
173
|
+
* @param textureFormat The TextureFormat.
|
|
174
|
+
* @returns The WebGL internal format.
|
|
175
|
+
*/
|
|
176
|
+
public static convertInternalFormat(gl: WebGL2RenderingContext, textureFormat: TextureFormat): number {
|
|
177
|
+
switch (textureFormat) {
|
|
178
|
+
// Same format regardless.
|
|
179
|
+
case TextureFormat.RGBA_8_UNORM:
|
|
180
|
+
return gl.RGBA8;
|
|
181
|
+
case TextureFormat.RGBA_8_UNORM_SRGB:
|
|
182
|
+
return gl.SRGB8_ALPHA8;
|
|
183
|
+
case TextureFormat.DEPTH_32_FLOAT:
|
|
184
|
+
return gl.DEPTH_COMPONENT32F;
|
|
185
|
+
case TextureFormat.DEPTH_24_STENCIL_8:
|
|
186
|
+
return gl.DEPTH24_STENCIL8;
|
|
187
|
+
case TextureFormat.BC3_RGBA_UNORM:
|
|
188
|
+
return WebGlConverter.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
|
189
|
+
case TextureFormat.ETC2_RGBA8_UNORM:
|
|
190
|
+
return WebGlConverter.GL_COMPRESSED_RGBA8_ETC2_EAC;
|
|
191
|
+
case TextureFormat.BC7_RGBA_UNORM:
|
|
192
|
+
return WebGlConverter.GL_COMPRESSED_RGBA_BPTC_UNORM;
|
|
193
|
+
case TextureFormat.ASTC_4X4_RGBA:
|
|
194
|
+
return WebGlConverter.GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
|
|
195
|
+
|
|
196
|
+
default:
|
|
197
|
+
throw new Error("NotImplementedException");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Converts TextureFormat to WebGL format.
|
|
203
|
+
* @param gl The WebGL2RenderingContext.
|
|
204
|
+
* @param textureFormat The TextureFormat.
|
|
205
|
+
* @returns The WebGL format.
|
|
206
|
+
*/
|
|
207
|
+
public static convertToPixelFormat(gl: WebGL2RenderingContext, textureFormat: TextureFormat): number {
|
|
208
|
+
switch (textureFormat) {
|
|
209
|
+
case TextureFormat.RGBA_8_UNORM:
|
|
210
|
+
case TextureFormat.RGBA_8_UNORM_SRGB:
|
|
211
|
+
return gl.RGBA;
|
|
212
|
+
case TextureFormat.DEPTH_32_FLOAT:
|
|
213
|
+
return gl.DEPTH_COMPONENT;
|
|
214
|
+
case TextureFormat.DEPTH_24_STENCIL_8:
|
|
215
|
+
return gl.DEPTH_STENCIL;
|
|
216
|
+
default:
|
|
217
|
+
throw new Error("NotImplementedException");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Converts TextureFormat to WebGL texture type.
|
|
223
|
+
* @param gl The WebGL2RenderingContext.
|
|
224
|
+
* @param textureFormat The TextureFormat.
|
|
225
|
+
* @returns The WebGL texture type.
|
|
226
|
+
*/
|
|
227
|
+
public static convertToTextureType(gl: WebGL2RenderingContext, textureFormat: TextureFormat): number {
|
|
228
|
+
switch (textureFormat) {
|
|
229
|
+
case TextureFormat.RGBA_8_UNORM:
|
|
230
|
+
return gl.UNSIGNED_BYTE;
|
|
231
|
+
case TextureFormat.DEPTH_32_FLOAT:
|
|
232
|
+
return gl.FLOAT;
|
|
233
|
+
case TextureFormat.DEPTH_24_STENCIL_8:
|
|
234
|
+
return gl.UNSIGNED_INT_24_8;
|
|
235
|
+
default:
|
|
236
|
+
throw new Error("NotImplementedException");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Converts SamplerMinFilter to WebGL enum.
|
|
242
|
+
* @param gl The WebGL2RenderingContext.
|
|
243
|
+
* @param minFilter The SamplerMinFilter.
|
|
244
|
+
* @param mipMapFilter The MipmapSamplerFilter.
|
|
245
|
+
* @returns The WebGL enum.
|
|
246
|
+
*/
|
|
247
|
+
public static convertToMinFilter(gl: WebGL2RenderingContext,
|
|
248
|
+
minFilter: SamplerFilter,
|
|
249
|
+
mipMapFilter: MipMapSamplerFilter): number {
|
|
250
|
+
if (mipMapFilter == MipMapSamplerFilter.NONE) {
|
|
251
|
+
return this.convertMagFilter(gl, minFilter);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (minFilter == SamplerFilter.NEAREST) {
|
|
255
|
+
switch (mipMapFilter) {
|
|
256
|
+
case MipMapSamplerFilter.NEAREST:
|
|
257
|
+
return gl.NEAREST_MIPMAP_NEAREST;
|
|
258
|
+
case MipMapSamplerFilter.LINEAR:
|
|
259
|
+
return gl.NEAREST_MIPMAP_LINEAR;
|
|
260
|
+
default:
|
|
261
|
+
throw new Error("NotImplementedException");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if (minFilter == SamplerFilter.LINEAR) {
|
|
265
|
+
switch (mipMapFilter) {
|
|
266
|
+
case MipMapSamplerFilter.NEAREST:
|
|
267
|
+
return gl.LINEAR_MIPMAP_NEAREST;
|
|
268
|
+
case MipMapSamplerFilter.LINEAR:
|
|
269
|
+
return gl.LINEAR_MIPMAP_LINEAR;
|
|
270
|
+
default:
|
|
271
|
+
throw new Error("NotImplementedException");
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
throw new Error("NotImplementedException");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Converts SamplerMagFilter to WebGL enum.
|
|
280
|
+
* @param gl The WebGL2RenderingContext.
|
|
281
|
+
* @param magFilter The SamplerMagFilter.
|
|
282
|
+
* @returns The WebGL enum.
|
|
283
|
+
*/
|
|
284
|
+
public static convertToMagFilter(gl: WebGL2RenderingContext, magFilter: SamplerFilter): number {
|
|
285
|
+
switch (magFilter) {
|
|
286
|
+
case SamplerFilter.NEAREST:
|
|
287
|
+
return gl.NEAREST;
|
|
288
|
+
case SamplerFilter.LINEAR:
|
|
289
|
+
return gl.LINEAR;
|
|
290
|
+
default:
|
|
291
|
+
throw new Error("NotImplementedException");
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Converts VertexFormat to the number of components for WebGL vertex attribute pointer.
|
|
297
|
+
* @param vertexFormat The VertexFormat.
|
|
298
|
+
* @returns The number of components for WebGL vertex attribute pointer.
|
|
299
|
+
*/
|
|
300
|
+
public static convertVertexFormat(vertexFormat: VertexFormat): number {
|
|
301
|
+
switch (vertexFormat) {
|
|
302
|
+
case VertexFormat.FLOAT_32:
|
|
303
|
+
return 1;
|
|
304
|
+
case VertexFormat.FLOAT_32X2:
|
|
305
|
+
return 2;
|
|
306
|
+
case VertexFormat.FLOAT_32X3:
|
|
307
|
+
return 3;
|
|
308
|
+
case VertexFormat.FLOAT_32X4:
|
|
309
|
+
return 4;
|
|
310
|
+
case VertexFormat.FLOAT_32X16:
|
|
311
|
+
return 16;
|
|
312
|
+
default:
|
|
313
|
+
throw new Error("NotImplementedException");
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Converts SamplerFilter to WebGL enum.
|
|
319
|
+
* @param gl The WebGL2RenderingContext.
|
|
320
|
+
* @param filter The SamplerFilter.
|
|
321
|
+
* @returns The WebGL enum.
|
|
322
|
+
*/
|
|
323
|
+
public static convertMagFilter(gl: WebGL2RenderingContext, filter: SamplerFilter): number {
|
|
324
|
+
switch (filter) {
|
|
325
|
+
case SamplerFilter.NEAREST:
|
|
326
|
+
return gl.NEAREST;
|
|
327
|
+
case SamplerFilter.LINEAR:
|
|
328
|
+
return gl.LINEAR;
|
|
329
|
+
default:
|
|
330
|
+
throw new Error("NotImplementedException");
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Converts SamplerFilter and MipmapSamplerFilter to WebGL enum for minification filter.
|
|
336
|
+
* @param gl The WebGL2RenderingContext.
|
|
337
|
+
* @param filter The SamplerFilter.
|
|
338
|
+
* @param mipMapFilter The MipmapSamplerFilter.
|
|
339
|
+
* @returns The WebGL enum.
|
|
340
|
+
*/
|
|
341
|
+
public static convertMinFIlter(gl: WebGL2RenderingContext, filter: SamplerFilter, mipMapFilter: MipMapSamplerFilter): number {
|
|
342
|
+
|
|
343
|
+
if (mipMapFilter == MipMapSamplerFilter.NONE) {
|
|
344
|
+
return this.convertMagFilter(gl, filter);
|
|
345
|
+
}
|
|
346
|
+
switch (filter) {
|
|
347
|
+
case SamplerFilter.NEAREST:
|
|
348
|
+
switch (mipMapFilter) {
|
|
349
|
+
case MipMapSamplerFilter.NEAREST:
|
|
350
|
+
return gl.NEAREST_MIPMAP_NEAREST;
|
|
351
|
+
case MipMapSamplerFilter.LINEAR:
|
|
352
|
+
return gl.NEAREST_MIPMAP_LINEAR;
|
|
353
|
+
default:
|
|
354
|
+
throw new Error("NotImplementedException");
|
|
355
|
+
}
|
|
356
|
+
case SamplerFilter.LINEAR:
|
|
357
|
+
switch (mipMapFilter) {
|
|
358
|
+
case MipMapSamplerFilter.NEAREST:
|
|
359
|
+
return gl.LINEAR_MIPMAP_NEAREST;
|
|
360
|
+
case MipMapSamplerFilter.LINEAR:
|
|
361
|
+
return gl.LINEAR_MIPMAP_LINEAR;
|
|
362
|
+
default:
|
|
363
|
+
throw new Error("NotImplementedException");
|
|
364
|
+
}
|
|
365
|
+
default:
|
|
366
|
+
throw new Error("NotImplementedException");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Converts SamplerAddressMode to WebGL enum.
|
|
372
|
+
* @param gl The WebGL2RenderingContext.
|
|
373
|
+
* @param addressMode The SamplerAddressMode.
|
|
374
|
+
* @returns The WebGL enum.
|
|
375
|
+
*/
|
|
376
|
+
public static convertAddressMode(gl: WebGL2RenderingContext, addressMode: SamplerAddressMode): number {
|
|
377
|
+
switch (addressMode) {
|
|
378
|
+
case SamplerAddressMode.CLAMP_TO_EDGE:
|
|
379
|
+
return gl.CLAMP_TO_EDGE;
|
|
380
|
+
case SamplerAddressMode.REPEAT:
|
|
381
|
+
return gl.REPEAT;
|
|
382
|
+
case SamplerAddressMode.MIRROR_REPEAT:
|
|
383
|
+
return gl.MIRRORED_REPEAT;
|
|
384
|
+
case SamplerAddressMode.CLAMP_TO_BORDER:
|
|
385
|
+
return gl.CLAMP_TO_EDGE;
|
|
386
|
+
default:
|
|
387
|
+
throw new Error("NotImplementedException");
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import {TextureFormat} from "ris-framework-api";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This file defines the WebGLFrameBufferUtilities class, which provides utility functions for working with WebGL framebuffers.
|
|
6
|
+
* The WebGLFrameBufferUtilities class includes methods for creating framebuffers with specific configurations, such as attaching textures as color and depth attachments.
|
|
7
|
+
* These utilities simplify the process of setting up framebuffers for rendering to textures, which is a common technique in graphics programming for effects like render-to-texture, post-processing, and off-screen rendering.
|
|
8
|
+
*/
|
|
9
|
+
export class WebGlFrameBufferUtilities {
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a framebuffer and attaches the provided texture(s) as color attachments.
|
|
14
|
+
* This method is useful for setting up a framebuffer for rendering to one or more textures.
|
|
15
|
+
* @param gl The WebGL2 rendering context.
|
|
16
|
+
* @param textureIds The texture(s) to be attached as color attachments.
|
|
17
|
+
* @param framebufferLabel An optional label for the framebuffer, which can be used for debugging purposes.
|
|
18
|
+
* @returns The created framebuffer.
|
|
19
|
+
*/
|
|
20
|
+
public create(gl: WebGL2RenderingContext, textureIds: WebGLTexture[] | WebGLTexture, framebufferLabel: string | null = null): WebGLFramebuffer {
|
|
21
|
+
const framebuffer = gl.createFramebuffer();
|
|
22
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
23
|
+
|
|
24
|
+
// -- LABEL FRAMEBUFFER
|
|
25
|
+
if (framebufferLabel) {
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
framebuffer.__SPECTOR_Metadata = {
|
|
28
|
+
name: framebufferLabel,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
var textureIdsArray = [];
|
|
33
|
+
if (textureIds instanceof WebGLTexture) {
|
|
34
|
+
textureIdsArray.push(textureIds);
|
|
35
|
+
} else {
|
|
36
|
+
textureIdsArray = textureIds;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < textureIdsArray.length; i++) {
|
|
40
|
+
const textureId = textureIdsArray[i];
|
|
41
|
+
|
|
42
|
+
if(textureId == null) {
|
|
43
|
+
throw new Error("Cannot bind texture to the frame buffer. Texture id not specified");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Bind the texture to the framebuffer, so we can render to it.
|
|
47
|
+
gl.bindTexture(gl.TEXTURE_2D, textureId);
|
|
48
|
+
|
|
49
|
+
// Connect the texture to the framebuffer.
|
|
50
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER,
|
|
51
|
+
gl.COLOR_ATTACHMENT0 + i,
|
|
52
|
+
gl.TEXTURE_2D,
|
|
53
|
+
textureId,
|
|
54
|
+
0);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Check if the framebuffer is complete.
|
|
58
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
|
|
59
|
+
var error = gl.getError();
|
|
60
|
+
throw new Error("Framebuffer is not complete." + " WebGL error code: " + error);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return framebuffer;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates a framebuffer with texture being attached as color attachment 0 and depth render buffer.
|
|
68
|
+
* @param gl The WebGL2 rendering context.
|
|
69
|
+
* @param textureAttachment0 The texture to be attached as color attachment 0.
|
|
70
|
+
* @param depthAttachmentType The type of the depth attachment, which can be DepthAttachment, DepthStencilAttachment or StencilAttachment.
|
|
71
|
+
* @param depthStencilRenderBuffer The render buffer to be attached as the depth/stencil attachment.
|
|
72
|
+
* @param label An optional label for the framebuffer, which can be used for debugging purposes.
|
|
73
|
+
* @return The created framebuffer.
|
|
74
|
+
*/
|
|
75
|
+
public createWithRenderBufferAsDepthBuffer(gl: WebGL2RenderingContext,
|
|
76
|
+
textureAttachment0: WebGLTexture,
|
|
77
|
+
depthAttachmentType: GLenum,
|
|
78
|
+
depthStencilRenderBuffer: WebGLRenderbuffer, label: string | null | undefined): WebGLFramebuffer {
|
|
79
|
+
|
|
80
|
+
if (depthAttachmentType != gl.DEPTH_ATTACHMENT
|
|
81
|
+
&& depthAttachmentType != gl.DEPTH_STENCIL_ATTACHMENT
|
|
82
|
+
&& depthAttachmentType != gl.STENCIL_ATTACHMENT) {
|
|
83
|
+
const msg = "Invalid depth attachment type + " + depthAttachmentType;
|
|
84
|
+
throw new Error(msg);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const framebuffer = gl.createFramebuffer();
|
|
88
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
89
|
+
|
|
90
|
+
// Connect the texture to the framebuffer.
|
|
91
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER,
|
|
92
|
+
gl.COLOR_ATTACHMENT0,
|
|
93
|
+
gl.TEXTURE_2D,
|
|
94
|
+
textureAttachment0,
|
|
95
|
+
0);
|
|
96
|
+
|
|
97
|
+
// Connect the render buffer to the framebuffer.
|
|
98
|
+
gl.framebufferRenderbuffer(gl.FRAMEBUFFER,
|
|
99
|
+
depthAttachmentType,
|
|
100
|
+
gl.RENDERBUFFER,
|
|
101
|
+
depthStencilRenderBuffer);
|
|
102
|
+
|
|
103
|
+
// Check if the framebuffer is complete.
|
|
104
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
|
|
105
|
+
const msg = "Framebuffer is not complete.";
|
|
106
|
+
throw new Error(msg);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (label) {
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
framebuffer.__SPECTOR_Metadata = {
|
|
112
|
+
name: label,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
117
|
+
|
|
118
|
+
return framebuffer;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Creates a framebuffer with texture being attached as color attachment 0 and depth render buffer.
|
|
123
|
+
* @param gl The WebGL2 rendering context.
|
|
124
|
+
* @param textureAttachment0 The texture to be attached as color attachment 0.
|
|
125
|
+
* @param depthAttachmentType The type of the depth attachment, which can be DepthAttachment, DepthStencilAttachment or StencilAttachment.
|
|
126
|
+
* @param depthStencilTexture The texture to be attached as the depth/stencil attachment.
|
|
127
|
+
* @param label An optional label for the framebuffer, which can be used for debugging purposes.
|
|
128
|
+
* @returns The created framebuffer.
|
|
129
|
+
*/
|
|
130
|
+
public createWithRenderTextureAsDepthBuffer(gl: WebGL2RenderingContext,
|
|
131
|
+
textureAttachment0: WebGLTexture,
|
|
132
|
+
depthAttachmentType: GLenum,
|
|
133
|
+
depthStencilTexture: WebGLTexture,
|
|
134
|
+
label?: string): WebGLFramebuffer {
|
|
135
|
+
if (depthAttachmentType != gl.DEPTH_ATTACHMENT
|
|
136
|
+
&& depthAttachmentType != gl.DEPTH_STENCIL_ATTACHMENT
|
|
137
|
+
&& depthAttachmentType != gl.STENCIL_ATTACHMENT) {
|
|
138
|
+
const msg = "Invalid depth attachment type.";
|
|
139
|
+
throw new Error(msg);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const framebuffer = gl.createFramebuffer();
|
|
143
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
144
|
+
|
|
145
|
+
// Connect the texture to the framebuffer.
|
|
146
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER,
|
|
147
|
+
gl.COLOR_ATTACHMENT0,
|
|
148
|
+
gl.TEXTURE_2D,
|
|
149
|
+
textureAttachment0,
|
|
150
|
+
0);
|
|
151
|
+
|
|
152
|
+
// Connect the render buffer to the framebuffer.
|
|
153
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER,
|
|
154
|
+
depthAttachmentType,
|
|
155
|
+
gl.TEXTURE_2D,
|
|
156
|
+
depthStencilTexture,
|
|
157
|
+
0);
|
|
158
|
+
|
|
159
|
+
// Check if the framebuffer is complete.
|
|
160
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
|
|
161
|
+
const msg = "Framebuffer is not complete.";
|
|
162
|
+
throw new Error(msg);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (label) {
|
|
166
|
+
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
framebuffer.__SPECTOR_Metadata = {
|
|
169
|
+
name: label,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
174
|
+
return framebuffer;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Attaches a depth/stencil render buffer to the framebuffer.
|
|
179
|
+
* @param gl The WebGL2 rendering context.
|
|
180
|
+
* @param frameBuffer The framebuffer to which the depth/stencil render buffer will be attached.
|
|
181
|
+
* @param depthStencilRenderBuffer The depth/stencil render buffer to be attached.
|
|
182
|
+
* @param format The format of the depth/stencil render buffer, which determines the type of attachment (depth, stencil, or depth-stencil).
|
|
183
|
+
*/
|
|
184
|
+
public attachDepthStencilRenderBuffer(
|
|
185
|
+
gl: WebGL2RenderingContext,
|
|
186
|
+
frameBuffer: WebGLFramebuffer,
|
|
187
|
+
depthStencilRenderBuffer: WebGLRenderbuffer,
|
|
188
|
+
format: TextureFormat): void {
|
|
189
|
+
|
|
190
|
+
var depthStencilAttachmentType = null;
|
|
191
|
+
switch (format) {
|
|
192
|
+
case TextureFormat.DEPTH_32_FLOAT:
|
|
193
|
+
depthStencilAttachmentType = gl.DEPTH_ATTACHMENT;
|
|
194
|
+
break;
|
|
195
|
+
case TextureFormat.DEPTH_24_STENCIL_8:
|
|
196
|
+
depthStencilAttachmentType = gl.DEPTH_STENCIL_ATTACHMENT;
|
|
197
|
+
break;
|
|
198
|
+
default:
|
|
199
|
+
throw new Error(`Unsupported depth/stencil texture format.`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
|
203
|
+
// Connect the render buffer to the framebuffer.
|
|
204
|
+
gl.framebufferRenderbuffer(gl.FRAMEBUFFER,
|
|
205
|
+
depthStencilAttachmentType,
|
|
206
|
+
gl.RENDERBUFFER,
|
|
207
|
+
depthStencilRenderBuffer);
|
|
208
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Attaches a depth/stencil texture to the framebuffer.
|
|
214
|
+
* @param gl The WebGL2 rendering context.
|
|
215
|
+
* @param frameBuffer The framebuffer to which the depth/stencil texture will be attached.
|
|
216
|
+
* @param depthStencilTexture The depth/stencil texture to be attached.
|
|
217
|
+
* @param textureFormat The format of the depth/stencil texture, which determines the type of attachment (depth, stencil, or depth-stencil).
|
|
218
|
+
*/
|
|
219
|
+
public attachDepthStencilTexture(
|
|
220
|
+
gl: WebGL2RenderingContext,
|
|
221
|
+
frameBuffer: WebGLFramebuffer,
|
|
222
|
+
depthStencilTexture: WebGLTexture,
|
|
223
|
+
textureFormat: TextureFormat): void {
|
|
224
|
+
var depthStencilAttachmentType = null;
|
|
225
|
+
switch (textureFormat) {
|
|
226
|
+
case TextureFormat.DEPTH_32_FLOAT:
|
|
227
|
+
depthStencilAttachmentType = gl.DEPTH_ATTACHMENT;
|
|
228
|
+
break;
|
|
229
|
+
case TextureFormat.DEPTH_24_STENCIL_8:
|
|
230
|
+
depthStencilAttachmentType = gl.DEPTH_STENCIL_ATTACHMENT;
|
|
231
|
+
break;
|
|
232
|
+
default:
|
|
233
|
+
throw new Error(`Unsupported depth/stencil texture format.`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
|
237
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, depthStencilAttachmentType, gl.TEXTURE_2D, depthStencilTexture, 0);
|
|
238
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
239
|
+
}
|
|
240
|
+
}
|