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,197 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IFramework, IPrimitiveState,
|
|
3
|
+
ISampler,
|
|
4
|
+
ITexture2D,
|
|
5
|
+
IUniformBuffer, IUnlitRenderPipeline,
|
|
6
|
+
IVertexBuffer
|
|
7
|
+
} from "ris-framework-api";
|
|
8
|
+
import {VertexBufferLayout} from "../../../core/rendering/vertex-buffer-layout";
|
|
9
|
+
import type {WebGlUniformBuffer} from "../../buffers/WebGlUniformBuffer.ts";
|
|
10
|
+
import { asWebGLUniformBuffer} from "../../cast/cast";
|
|
11
|
+
import WebGlShaderModule from "../../shader/WebGlShaderModule.ts";
|
|
12
|
+
import {WebGlTexture2D} from "../../texture/WebGlTexture2D.ts";
|
|
13
|
+
import {WebGlVertexBuffer} from '../../buffers/WebGlVertexBuffer.ts';
|
|
14
|
+
import {AWebGlRenderPipeline} from "../AWebGlRenderPipeline.ts";
|
|
15
|
+
import type {WebGLIndexBuffer} from "../../buffers/webgl-index-buffer";
|
|
16
|
+
import {type IIndexBuffer, IndexBufferType} from "ris-framework-api";
|
|
17
|
+
import type {WebGlSampler} from "../../sampler/webgl-sampler.ts";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The WebGL implementation of the sprite render pipeline.
|
|
21
|
+
*/
|
|
22
|
+
export class WebGlUnlitRenderPipeline extends AWebGlRenderPipeline implements IUnlitRenderPipeline {
|
|
23
|
+
|
|
24
|
+
private static readonly CAMERA_BINDING_POINT = 0;
|
|
25
|
+
private static readonly MODEL_BINDING_POINT = 1;
|
|
26
|
+
private static readonly MATERIAL_BINDING_POINT = 2;
|
|
27
|
+
|
|
28
|
+
private _texture: WebGlTexture2D = null!;
|
|
29
|
+
private _sampler?: WebGlSampler;
|
|
30
|
+
private _projectionViewBuffer: WebGlUniformBuffer;
|
|
31
|
+
private _modelBuffer: WebGlUniformBuffer;
|
|
32
|
+
private _materialBuffer: WebGlUniformBuffer;
|
|
33
|
+
private _cameraBlockIndex: number = -1;
|
|
34
|
+
private _modelBlockIndex: number = -1;
|
|
35
|
+
private _materialBlockIndex : number = -1;
|
|
36
|
+
private _buffersArray: WebGLBuffer[] = [null!];
|
|
37
|
+
private _lastVertexBuffer: WebGlVertexBuffer | null = null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The constructor.
|
|
41
|
+
* @param framework The framework.
|
|
42
|
+
* @param projectionViewBuffer The projection view buffer.
|
|
43
|
+
* @param modelBuffer The model buffer.
|
|
44
|
+
* @param materialBuffer The material buffer.
|
|
45
|
+
* @param primitiveState The primitive state.
|
|
46
|
+
*/
|
|
47
|
+
constructor(framework: IFramework,
|
|
48
|
+
projectionViewBuffer: IUniformBuffer,
|
|
49
|
+
modelBuffer: IUniformBuffer,
|
|
50
|
+
materialBuffer: IUniformBuffer,
|
|
51
|
+
primitiveState?: IPrimitiveState,
|
|
52
|
+
) {
|
|
53
|
+
super(framework, primitiveState);
|
|
54
|
+
this._projectionViewBuffer = projectionViewBuffer as WebGlUniformBuffer;
|
|
55
|
+
this._modelBuffer = modelBuffer as WebGlUniformBuffer;
|
|
56
|
+
this._materialBuffer = materialBuffer as WebGlUniformBuffer;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
/** @inheritdoc */
|
|
61
|
+
public get projectionViewBuffer(): IUniformBuffer {
|
|
62
|
+
return this._projectionViewBuffer;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @inheritdoc */
|
|
66
|
+
public set projectionViewBuffer(value: IUniformBuffer) {
|
|
67
|
+
this._projectionViewBuffer = asWebGLUniformBuffer(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @inheritdoc */
|
|
71
|
+
public get modelBuffer(): IUniformBuffer {
|
|
72
|
+
return this._modelBuffer;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @inheritdoc */
|
|
76
|
+
public set modelBuffer(value: IUniformBuffer) {
|
|
77
|
+
this._modelBuffer = asWebGLUniformBuffer(value);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @inheritdoc */
|
|
81
|
+
public get materialBuffer(): IUniformBuffer {
|
|
82
|
+
return this._materialBuffer;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @inheritdoc */
|
|
86
|
+
public set materialBuffer(value: IUniformBuffer) {
|
|
87
|
+
this._materialBuffer = asWebGLUniformBuffer(value);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @inheritdoc */
|
|
91
|
+
public get diffuseTexture(): ITexture2D {
|
|
92
|
+
return this._texture;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** @inheritdoc */
|
|
96
|
+
public set diffuseTexture(value: ITexture2D ) {
|
|
97
|
+
this._texture = value as WebGlTexture2D;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @inheritdoc */
|
|
101
|
+
public get diffuseTextureSampler(): ISampler | undefined {
|
|
102
|
+
return this._sampler;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public set diffuseTextureSampler(value: ISampler | undefined) {
|
|
106
|
+
this._sampler = value as WebGlSampler | undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @inheritdoc */
|
|
110
|
+
public override initialize(): void {
|
|
111
|
+
|
|
112
|
+
const module = this._framework.content.loadShaderModule("unlit_material") as WebGlShaderModule;
|
|
113
|
+
this._program = module.program!;
|
|
114
|
+
this._createResources();
|
|
115
|
+
this.vertexBufferLayouts = [VertexBufferLayout.createFloat3Float4Float2Layout()];
|
|
116
|
+
super.initialize();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private _createResources(): void {
|
|
120
|
+
{
|
|
121
|
+
this._texture = WebGlTexture2D.getOrCreateDefault(this._framework);
|
|
122
|
+
|
|
123
|
+
this._cameraBlockIndex = this._gl.getUniformBlockIndex(this._program, "CameraBuffer");
|
|
124
|
+
this._gl.uniformBlockBinding(this._program, this._cameraBlockIndex, WebGlUnlitRenderPipeline.CAMERA_BINDING_POINT);
|
|
125
|
+
|
|
126
|
+
this._modelBlockIndex = this._gl.getUniformBlockIndex(this._program, "ModelBuffer");
|
|
127
|
+
this._gl.uniformBlockBinding(this._program, this._modelBlockIndex, WebGlUnlitRenderPipeline.MODEL_BINDING_POINT);
|
|
128
|
+
|
|
129
|
+
this._materialBlockIndex = this._gl.getUniformBlockIndex(this._program, "MaterialBuffer");
|
|
130
|
+
this._gl.uniformBlockBinding(this._program, this._materialBlockIndex, WebGlUnlitRenderPipeline.MATERIAL_BINDING_POINT);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
/** @inheritdoc */
|
|
136
|
+
protected _provideBuffers(): WebGLBuffer[] | undefined | null {
|
|
137
|
+
if (this._lastVertexBuffer === null) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this._buffersArray[0] = this._lastVertexBuffer.buffer!;
|
|
142
|
+
return this._buffersArray;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** @inheritdoc */
|
|
146
|
+
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount: number = -1, indicesOffset: number = 0): void {
|
|
147
|
+
|
|
148
|
+
if (indicesCount == 0) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const webGlVertexBuffer = vertexBuffer as WebGlVertexBuffer;
|
|
153
|
+
const webGlIndexBuffer = indexBuffer as WebGLIndexBuffer;
|
|
154
|
+
const webGlSampler = this._sampler ?? this._defaultTextureSampler;
|
|
155
|
+
|
|
156
|
+
this.primitiveState.apply(this._gl);
|
|
157
|
+
this._blendState.apply(this._gl);
|
|
158
|
+
|
|
159
|
+
// if it was changed, we need to create a new vao.
|
|
160
|
+
if (this._lastVertexBuffer != webGlVertexBuffer) {
|
|
161
|
+
// Create a new VAO. Internally deletes the old one.
|
|
162
|
+
this._lastVertexBuffer = webGlVertexBuffer;
|
|
163
|
+
this._createVertexArrayObject();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
this._gl.useProgram(this._program);
|
|
167
|
+
// Bind the vao. It contains all the information about the vertex buffer layout ( vertices + instances)
|
|
168
|
+
this._gl.bindVertexArray(this._vertexArrayObject);
|
|
169
|
+
this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, webGlIndexBuffer.buffer);
|
|
170
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,
|
|
171
|
+
WebGlUnlitRenderPipeline.CAMERA_BINDING_POINT,
|
|
172
|
+
this._projectionViewBuffer.glBuffer!);
|
|
173
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,
|
|
174
|
+
WebGlUnlitRenderPipeline.MODEL_BINDING_POINT,
|
|
175
|
+
this._modelBuffer.glBuffer!);
|
|
176
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,
|
|
177
|
+
WebGlUnlitRenderPipeline.MATERIAL_BINDING_POINT,
|
|
178
|
+
this._materialBuffer.glBuffer!);
|
|
179
|
+
this._gl.activeTexture(this._gl.TEXTURE0);
|
|
180
|
+
this._gl.bindTexture(this._gl.TEXTURE_2D, this._texture!.glTexture);
|
|
181
|
+
this._gl.bindSampler(0, webGlSampler.glSampler);
|
|
182
|
+
|
|
183
|
+
// We can only really use two types uint16 and uint32. Boolean check to see which one to use.
|
|
184
|
+
const type = indexBuffer.type == IndexBufferType.UINT_16
|
|
185
|
+
? this._gl.UNSIGNED_SHORT
|
|
186
|
+
: this._gl.UNSIGNED_INT;
|
|
187
|
+
|
|
188
|
+
// api.PolygonMode(GLEnum.FrontAndBack, GLEnum.Line);
|
|
189
|
+
|
|
190
|
+
// Either draw all indices or a specific amount. If indicesCount is -1, draw all as defined by the index buffer.
|
|
191
|
+
const toIndices = indicesCount > 0 ? indicesCount : indexBuffer.indicesCount;
|
|
192
|
+
const fromIndices = indicesOffset * indexBuffer.elementByteSize;
|
|
193
|
+
|
|
194
|
+
this._gl.drawElements(this.primitiveState.glPrimitiveType, toIndices, type, fromIndices);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IFramework,
|
|
3
|
+
ISampler,
|
|
4
|
+
ISpriteRenderPipeline,
|
|
5
|
+
ITexture2D,
|
|
6
|
+
IUniformBuffer,
|
|
7
|
+
IVertexBuffer
|
|
8
|
+
} from "ris-framework-api";
|
|
9
|
+
import {VertexBufferLayout} from "../../../core/rendering/vertex-buffer-layout";
|
|
10
|
+
import type {WebGlUniformBuffer} from "../../buffers/WebGlUniformBuffer.ts";
|
|
11
|
+
import {asWebGLTexture2D, asWebGLUniformBuffer} from "../../cast/cast";
|
|
12
|
+
import WebGlShaderModule from "../../shader/WebGlShaderModule.ts";
|
|
13
|
+
import {WebGlTexture2D} from "../../texture/WebGlTexture2D.ts";
|
|
14
|
+
import {WebGlVertexBuffer} from '../../buffers/WebGlVertexBuffer.ts';
|
|
15
|
+
import {AWebGlRenderPipeline} from "../AWebGlRenderPipeline.ts";
|
|
16
|
+
import type {WebGLIndexBuffer} from "../../buffers/webgl-index-buffer";
|
|
17
|
+
import {type IIndexBuffer, IndexBufferType} from "ris-framework-api";
|
|
18
|
+
import type {WebGlSampler} from "../../sampler/webgl-sampler.ts";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The WebGL implementation of the sprite render pipeline.
|
|
22
|
+
*/
|
|
23
|
+
export class WebGlSpriteRenderPipeline extends AWebGlRenderPipeline implements ISpriteRenderPipeline {
|
|
24
|
+
|
|
25
|
+
private static readonly CAMERA_BINDING_POINT: number = 0;
|
|
26
|
+
|
|
27
|
+
private _texture: WebGlTexture2D = null!;
|
|
28
|
+
private _sampler?: WebGlSampler;
|
|
29
|
+
private _projectionViewBuffer: WebGlUniformBuffer;
|
|
30
|
+
private _cameraBlockIndex: number = -1;
|
|
31
|
+
private _buffersArray: WebGLBuffer[] = [null!];
|
|
32
|
+
private _lastVertexBuffer: WebGlVertexBuffer | null = null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The constructor.
|
|
36
|
+
* @param framework The framework.
|
|
37
|
+
* @param projectionViewBuffer The projection view buffer.
|
|
38
|
+
*/
|
|
39
|
+
constructor(framework: IFramework, projectionViewBuffer: IUniformBuffer) {
|
|
40
|
+
super(framework);
|
|
41
|
+
this._projectionViewBuffer = asWebGLUniformBuffer(projectionViewBuffer);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @inheritdoc */
|
|
45
|
+
public get projectionViewBuffer(): IUniformBuffer {
|
|
46
|
+
return this._projectionViewBuffer;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @inheritdoc */
|
|
50
|
+
public set projectionViewBuffer(value: IUniformBuffer) {
|
|
51
|
+
this._projectionViewBuffer = asWebGLUniformBuffer(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @inheritdoc */
|
|
55
|
+
public get spriteTexture(): ITexture2D {
|
|
56
|
+
return this._texture;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @inheritdoc */
|
|
60
|
+
public set spriteTexture(value: ITexture2D ) {
|
|
61
|
+
this._texture = asWebGLTexture2D(value!);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @inheritdoc */
|
|
65
|
+
public get textureSampler(): ISampler | undefined {
|
|
66
|
+
return this._sampler;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public set textureSampler(value: ISampler | undefined) {
|
|
70
|
+
this._sampler = value as WebGlSampler | undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @inheritdoc */
|
|
74
|
+
public override initialize(): void {
|
|
75
|
+
|
|
76
|
+
const module = this._framework.content.loadShaderModule("sprite") as WebGlShaderModule;
|
|
77
|
+
this._program = module.program!;
|
|
78
|
+
this._createResources();
|
|
79
|
+
this.vertexBufferLayouts = [VertexBufferLayout.createFloat3Float4Float2Layout()];
|
|
80
|
+
super.initialize();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private _createResources(): void {
|
|
84
|
+
{
|
|
85
|
+
this._texture = WebGlTexture2D.getOrCreateDefault(this._framework);
|
|
86
|
+
|
|
87
|
+
this._cameraBlockIndex = this._gl.getUniformBlockIndex(this._program, "CameraBuffer");
|
|
88
|
+
this._gl.uniformBlockBinding(this._program, this._cameraBlockIndex, WebGlSpriteRenderPipeline.CAMERA_BINDING_POINT);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/** @inheritdoc */
|
|
94
|
+
protected _provideBuffers(): WebGLBuffer[] | undefined | null {
|
|
95
|
+
if (this._lastVertexBuffer === null) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this._buffersArray[0] = this._lastVertexBuffer.buffer!;
|
|
100
|
+
return this._buffersArray;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** @inheritdoc */
|
|
104
|
+
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount: number = -1, indicesOffset: number = 0): void {
|
|
105
|
+
|
|
106
|
+
if (indicesCount == 0) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const webGlVertexBuffer = vertexBuffer as WebGlVertexBuffer;
|
|
111
|
+
const webGlIndexBuffer = indexBuffer as WebGLIndexBuffer;
|
|
112
|
+
const webGlSampler = this._sampler ?? this._defaultTextureSampler;
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
// this.primitiveState.apply(this._gl);
|
|
116
|
+
// this._blendState.apply(this._gl);
|
|
117
|
+
|
|
118
|
+
// if it was changed, we need to create a new vao.
|
|
119
|
+
if (this._lastVertexBuffer != webGlVertexBuffer) {
|
|
120
|
+
// Create a new VAO. Internally deletes the old one.
|
|
121
|
+
this._lastVertexBuffer = webGlVertexBuffer;
|
|
122
|
+
this._createVertexArrayObject();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this._gl.useProgram(this._program);
|
|
126
|
+
// Bind the vao. It contains all the information about the vertex buffer layout ( vertices + instances)
|
|
127
|
+
this._gl.bindVertexArray(this._vertexArrayObject);
|
|
128
|
+
this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, webGlIndexBuffer.buffer);
|
|
129
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER, WebGlSpriteRenderPipeline.CAMERA_BINDING_POINT, this._projectionViewBuffer.glBuffer!);
|
|
130
|
+
this._gl.activeTexture(this._gl.TEXTURE0);
|
|
131
|
+
this._gl.bindTexture(this._gl.TEXTURE_2D, this._texture!.glTexture);
|
|
132
|
+
this._gl.bindSampler(0, webGlSampler.glSampler);
|
|
133
|
+
|
|
134
|
+
// We can only really use two types uint16 and uint32. Boolean check to see which one to use.
|
|
135
|
+
const type = indexBuffer.type == IndexBufferType.UINT_16
|
|
136
|
+
? this._gl.UNSIGNED_SHORT
|
|
137
|
+
: this._gl.UNSIGNED_INT;
|
|
138
|
+
|
|
139
|
+
// api.PolygonMode(GLEnum.FrontAndBack, GLEnum.Line);
|
|
140
|
+
|
|
141
|
+
// Either draw all indices or a specific amount. If indicesCount is -1, draw all as defined by the index buffer.
|
|
142
|
+
const toIndices = indicesCount > 0 ? indicesCount : indexBuffer.indicesCount;
|
|
143
|
+
const fromIndices = indicesOffset * indexBuffer.elementByteSize;
|
|
144
|
+
|
|
145
|
+
this._gl.drawElements(this.primitiveState.glPrimitiveType, toIndices, type, fromIndices);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* The WebGL implementation of a vertex attribute.
|
|
4
|
+
*/
|
|
5
|
+
export class WebGLVertexAttribute {
|
|
6
|
+
constructor(
|
|
7
|
+
public gl: WebGL2RenderingContext,
|
|
8
|
+
) {
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @inheritdoc */
|
|
13
|
+
initialize(): void {
|
|
14
|
+
// No initialization is needed for WebGL vertex attributes.
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebGL implementation of a vertex buffer layout.
|
|
3
|
+
*/
|
|
4
|
+
export class WebGLVertexBufferLayout {
|
|
5
|
+
|
|
6
|
+
private readonly _gl: WebGL2RenderingContext;
|
|
7
|
+
private _bufferTarget: number = 0;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The constructor for the WebGLVertexBufferLayout class.
|
|
11
|
+
* @param gl The WebGL2 rendering context.
|
|
12
|
+
* @param buffer The WebGL buffer that is used to store the vertex data. This is used to determine the buffer target for the vertex buffer layout.
|
|
13
|
+
* @param bufferTarget The buffer target for the vertex buffer layout. This is used to determine how the vertex buffer is bound to the WebGL context. This is typically either gl.ARRAY_BUFFER or gl.ELEMENT_ARRAY_BUFFER.
|
|
14
|
+
*/
|
|
15
|
+
constructor(
|
|
16
|
+
gl: WebGL2RenderingContext,
|
|
17
|
+
buffer: WebGLBuffer,
|
|
18
|
+
bufferTarget: number
|
|
19
|
+
) {
|
|
20
|
+
this._gl = gl;
|
|
21
|
+
this.buffer = buffer;
|
|
22
|
+
this._bufferTarget = bufferTarget;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The WebGL buffer that is used to store the vertex data.
|
|
27
|
+
*/
|
|
28
|
+
public buffer: WebGLBuffer | null = null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Binds the vertex buffer to the WebGL context using the buffer target specified in the constructor.
|
|
32
|
+
*/
|
|
33
|
+
public bindBuffer(): void {
|
|
34
|
+
this._gl.bindBuffer(this._bufferTarget, this.buffer);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Initializes the vertex buffer layout. This method should be called after the buffer has been created and bound.
|
|
39
|
+
*/
|
|
40
|
+
public initialize(): void {
|
|
41
|
+
this._gl.bindBuffer(this._bufferTarget, this.buffer);
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
for (const attribute of this.attributes) {
|
|
45
|
+
|
|
46
|
+
const index = attribute.shaderLocation;
|
|
47
|
+
const size = WebGLConverter.convertVertexFormat(attribute.vertexFormat);
|
|
48
|
+
const stride = this.arrayStride;
|
|
49
|
+
const offset = attribute.offset;
|
|
50
|
+
|
|
51
|
+
this._gl.enableVertexAttribArray(index);
|
|
52
|
+
this._gl.vertexAttribPointer(
|
|
53
|
+
index,
|
|
54
|
+
size,
|
|
55
|
+
this._gl.FLOAT, // Assuming all vertex formats are float for simplicity. In a complete implementation, this should be determined based on the vertex format.
|
|
56
|
+
false,
|
|
57
|
+
stride,
|
|
58
|
+
offset
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
*/
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { SamplerCompareFunction } from "../../common/sampler-enums";
|
|
2
|
+
import { SamplerDescriptor } from "../../core/rendering/sampler/sampler-descriptor";
|
|
3
|
+
import { WebGlUtilities } from "../utilities/WebGlUtilities.ts";
|
|
4
|
+
import type { WebGlGraphicsDevice } from "../WebGlGraphicsDevice.ts";
|
|
5
|
+
import {type ISampler, MipMapSamplerFilter, SamplerAddressMode, SamplerFilter} from "ris-framework-api";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The WebGL implementation of the ISampler interface.
|
|
9
|
+
*/
|
|
10
|
+
export class WebGlSampler implements ISampler {
|
|
11
|
+
|
|
12
|
+
private static _id: number = 0;
|
|
13
|
+
private readonly _gl: WebGL2RenderingContext;
|
|
14
|
+
private _minFilter = SamplerFilter.NEAREST;
|
|
15
|
+
private _magFilter = SamplerFilter.NEAREST;
|
|
16
|
+
private _mipMapFilter = MipMapSamplerFilter.NONE;
|
|
17
|
+
private _addressModueU = SamplerAddressMode.CLAMP_TO_EDGE
|
|
18
|
+
private _addressModueV = SamplerAddressMode.CLAMP_TO_EDGE;
|
|
19
|
+
private _addressModueW = SamplerAddressMode.CLAMP_TO_EDGE;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates an instance of WebGlSampler.
|
|
23
|
+
* @param graphicsDevice The WebGL graphics device that will be used to create the sampler. The graphics device provides access to the WebGL rendering context and other resources needed for creating and managing the sampler.
|
|
24
|
+
* @param descriptor The descriptor containing configuration details for the sampler. This parameter is optional, and if not provided, default values will be used for the sampler configuration. The descriptor allows you to specify various properties of the sampler, such as filtering methods and address modes, which determine how textures are sampled in the shader.
|
|
25
|
+
*/
|
|
26
|
+
constructor(graphicsDevice: WebGlGraphicsDevice, descriptor?: SamplerDescriptor) {
|
|
27
|
+
|
|
28
|
+
this._gl = graphicsDevice.gl;
|
|
29
|
+
|
|
30
|
+
descriptor = descriptor ?? new SamplerDescriptor();
|
|
31
|
+
|
|
32
|
+
this.id = WebGlSampler._id++;
|
|
33
|
+
this._minFilter = descriptor.minFilter;
|
|
34
|
+
this._magFilter = descriptor.magFilter;
|
|
35
|
+
this._mipMapFilter = descriptor.mipMapFilter;
|
|
36
|
+
this._addressModueU = descriptor.addressModeU;
|
|
37
|
+
this._addressModueV = descriptor.addressModeV;
|
|
38
|
+
this._addressModueW = descriptor.addressModeW;
|
|
39
|
+
|
|
40
|
+
this._createSampler();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @inheritDoc */
|
|
44
|
+
readonly id: number;
|
|
45
|
+
|
|
46
|
+
/** @inheritdoc */
|
|
47
|
+
public get minFilter() {
|
|
48
|
+
return this._minFilter;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @inheritdoc */
|
|
52
|
+
public get magFilter() {
|
|
53
|
+
return this._magFilter;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @inheritdoc */
|
|
57
|
+
public get mipmapFilter() {
|
|
58
|
+
return this._mipMapFilter;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @inheritdoc */
|
|
62
|
+
public get addressModeU() {
|
|
63
|
+
return this._addressModueU;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @inheritdoc */
|
|
67
|
+
public get addressModeV() {
|
|
68
|
+
return this._addressModueV;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @inheritdoc */
|
|
72
|
+
public get addressModeW() {
|
|
73
|
+
return this._addressModueW;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @inheritdoc */
|
|
77
|
+
public get handle(): any {
|
|
78
|
+
return this.glSampler;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The handle of the sampler.
|
|
83
|
+
* This is used to bind the sampler to the graphics pipeline.
|
|
84
|
+
*/
|
|
85
|
+
public glSampler: globalThis.WebGLSampler = null!;
|
|
86
|
+
|
|
87
|
+
/** @inheritdoc */
|
|
88
|
+
public _createSampler(): void {
|
|
89
|
+
|
|
90
|
+
this.glSampler = WebGlUtilities.sampler.create(this._gl,
|
|
91
|
+
this._minFilter,
|
|
92
|
+
this._magFilter,
|
|
93
|
+
this._mipMapFilter,
|
|
94
|
+
this._addressModueU,
|
|
95
|
+
this._addressModueV,
|
|
96
|
+
this._addressModueW,
|
|
97
|
+
SamplerCompareFunction.Never,
|
|
98
|
+
1,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @inheritdoc */
|
|
103
|
+
public dispose(): void {
|
|
104
|
+
if (this.glSampler) {
|
|
105
|
+
this._gl.deleteSampler(this.glSampler);
|
|
106
|
+
this.glSampler = null!;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type {WebGlGraphicsDevice} from "../WebGlGraphicsDevice.ts";
|
|
2
|
+
import {WebGlUtilities} from "../utilities/WebGlUtilities.ts";
|
|
3
|
+
import {type IFramework, type IShaderModule, ShaderStage} from "ris-framework-api";
|
|
4
|
+
import {ShaderModuleContent} from "../../content/ShaderModuleContent.ts";
|
|
5
|
+
|
|
6
|
+
interface WebGlInternalShader {
|
|
7
|
+
vertex: string;
|
|
8
|
+
fragment: string;
|
|
9
|
+
reflection: any; // This will be type in the future.
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The WebGL shader module.
|
|
14
|
+
*/
|
|
15
|
+
export class WebGlShaderModule implements IShaderModule {
|
|
16
|
+
|
|
17
|
+
private readonly _graphicsDevice: WebGlGraphicsDevice;
|
|
18
|
+
private readonly _gl: WebGL2RenderingContext;
|
|
19
|
+
private _program?: WebGLProgram;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The constructor of the WebGL shader module.
|
|
23
|
+
* @param framework The framework.
|
|
24
|
+
* @param shaderFilePath
|
|
25
|
+
*/
|
|
26
|
+
public constructor(framework: IFramework, shaderFilePath: string) {
|
|
27
|
+
this._graphicsDevice = framework.renderer.graphicsDevice as WebGlGraphicsDevice;
|
|
28
|
+
this._gl = this._graphicsDevice.gl;
|
|
29
|
+
this._program = this._loadShaders(shaderFilePath);
|
|
30
|
+
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @inheritDoc */
|
|
34
|
+
public stages: ShaderStage[] = [];
|
|
35
|
+
|
|
36
|
+
/** @inheritDoc */
|
|
37
|
+
public nativeHandle?: any;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Tries to framework shader.
|
|
41
|
+
* These shaders are part of framework.
|
|
42
|
+
* @param id The id of internal shader.
|
|
43
|
+
*/
|
|
44
|
+
private _tryResolveFrameworkShader(id: string): WebGlInternalShader | null {
|
|
45
|
+
if (ShaderModuleContent[id]) {
|
|
46
|
+
|
|
47
|
+
this.stages = [ShaderStage.VERTEX, ShaderStage.FRAGMENT];
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
vertex: ShaderModuleContent[id]["vertex"],
|
|
51
|
+
fragment: ShaderModuleContent[id]["fragment"],
|
|
52
|
+
reflection: ShaderModuleContent[id]["reflection"],
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The program of this shader module
|
|
61
|
+
*/
|
|
62
|
+
public get program(): WebGLProgram | null | undefined {
|
|
63
|
+
return this._program;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private _loadShaders(shaderFilePath: string): WebGLProgram {
|
|
67
|
+
|
|
68
|
+
const internalShader = this._tryResolveFrameworkShader(shaderFilePath);
|
|
69
|
+
|
|
70
|
+
let vertexShaderSource = "";
|
|
71
|
+
let fragmentShaderSource = "";
|
|
72
|
+
if (internalShader) {
|
|
73
|
+
vertexShaderSource = internalShader.vertex;
|
|
74
|
+
fragmentShaderSource = internalShader.fragment;
|
|
75
|
+
} else {
|
|
76
|
+
|
|
77
|
+
throw new Error("Not implemented");
|
|
78
|
+
|
|
79
|
+
// TODO: THIS API SUCKS, WE NEED BETTER WAY TO LOAD SHADER
|
|
80
|
+
|
|
81
|
+
// TODO: this is not complete we need to determine how it will be loaded from files.
|
|
82
|
+
// Most likely content pipeline will be source of truth
|
|
83
|
+
// vertexShaderSource = await this._shaderLoader.load(shaderFilePath, RenderingBackend.WEB_GL, [ShaderStage.VERTEX]);
|
|
84
|
+
// fragmentShaderSource = await this._shaderLoader.load(shaderFilePath, RenderingBackend.WEB_GL, [ShaderStage.FRAGMENT]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
const vertexShader = WebGlUtilities.shader.createVertexShader(this._gl, vertexShaderSource);
|
|
89
|
+
const fragmentShader = WebGlUtilities.shader.createFragmentShader(this._gl, fragmentShaderSource);
|
|
90
|
+
|
|
91
|
+
const program = WebGlUtilities.program.create(this._gl, vertexShader, fragmentShader);
|
|
92
|
+
|
|
93
|
+
// Clean up shaders after linking.
|
|
94
|
+
this._gl.deleteShader(vertexShader);
|
|
95
|
+
this._gl.deleteShader(fragmentShader);
|
|
96
|
+
|
|
97
|
+
return program;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @inheritdoc */
|
|
101
|
+
public dispose(): void {
|
|
102
|
+
|
|
103
|
+
if (this._program) {
|
|
104
|
+
this._gl.deleteProgram(this._program);
|
|
105
|
+
this._program = undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default WebGlShaderModule
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import WebGlShaderModule from "./WebGlShaderModule.ts";
|
|
2
|
+
import type {IFramework, IShaderModule} from "ris-framework-api";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The content WebGL implementation of ShaderContentModule.
|
|
7
|
+
*/
|
|
8
|
+
export class WebGlShaderModuleLoader {
|
|
9
|
+
private readonly _shaderModules: Map<string, WebGlShaderModule> = new Map<string, WebGlShaderModule>();
|
|
10
|
+
private readonly _framework: IFramework;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The WebGL shader content module.
|
|
14
|
+
* @param framework The framework.
|
|
15
|
+
*/
|
|
16
|
+
public constructor(framework: IFramework) {
|
|
17
|
+
this._framework = framework;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The content types provided by this content module.
|
|
22
|
+
* @param assetFilePathOrIdentifier The asset file path or identifier of the content to load.
|
|
23
|
+
*/
|
|
24
|
+
public load(assetFilePathOrIdentifier: string): IShaderModule {
|
|
25
|
+
|
|
26
|
+
// Here something like 'sprite' is provided, we must resolve it.
|
|
27
|
+
|
|
28
|
+
// Is it already cached?
|
|
29
|
+
let shaderModule = this._shaderModules.get(assetFilePathOrIdentifier);
|
|
30
|
+
if (!shaderModule) {
|
|
31
|
+
|
|
32
|
+
// If it does not exist create one and set it.
|
|
33
|
+
shaderModule = new WebGlShaderModule(this._framework, assetFilePathOrIdentifier);
|
|
34
|
+
this._shaderModules.set(assetFilePathOrIdentifier, shaderModule);
|
|
35
|
+
}
|
|
36
|
+
return shaderModule;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
}
|