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,129 @@
|
|
|
1
|
+
import type {IBlendState, IFramework, IIndexBuffer, IPrimitiveState, IRenderPipeline, IVertexBuffer} from "ris-framework-api";
|
|
2
|
+
import type {VertexBufferLayout} from '../../core/rendering/vertex-buffer-layout';
|
|
3
|
+
import type {WebGlBlendState} from '../blending/webgl-blend-state';
|
|
4
|
+
import type {WebGlPrimitiveState} from '../primitive/WebGlPrimitiveState.ts';
|
|
5
|
+
import type {WebGlSampler} from '../sampler/webgl-sampler';
|
|
6
|
+
import {WebGlConverter} from '../utilities/WebGlConverter.ts';
|
|
7
|
+
import type {WebGlGraphicsDevice} from "../WebGlGraphicsDevice.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The base class for WebGL render pipelines. This class provides common functionality for all WebGL render pipelines.
|
|
11
|
+
*/
|
|
12
|
+
export abstract class AWebGlRenderPipeline implements IRenderPipeline {
|
|
13
|
+
|
|
14
|
+
/** The framework instance. */
|
|
15
|
+
protected readonly _framework: IFramework;
|
|
16
|
+
protected readonly _gl: WebGL2RenderingContext;
|
|
17
|
+
protected readonly _defaultTextureSampler: WebGlSampler;
|
|
18
|
+
protected readonly _blendState: WebGlBlendState;
|
|
19
|
+
protected _vertexArrayObject: WebGLVertexArrayObject | null = null;
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
protected _program: WebGLProgram = null!;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The constructor.
|
|
26
|
+
* @param framework The framework.
|
|
27
|
+
* @param primitiveState The primitive state.
|
|
28
|
+
*/
|
|
29
|
+
public constructor(framework: IFramework, primitiveState?: IPrimitiveState) {
|
|
30
|
+
this._framework = framework;
|
|
31
|
+
const graphicsDevice = framework.renderer.graphicsDevice as WebGlGraphicsDevice;
|
|
32
|
+
this._gl = graphicsDevice.gl;
|
|
33
|
+
this._blendState = graphicsDevice.defaultBlendState as WebGlBlendState;
|
|
34
|
+
this._defaultTextureSampler = graphicsDevice.defaultTextureSampler as WebGlSampler;
|
|
35
|
+
this.primitiveState = (primitiveState ?? graphicsDevice.defaultPrimitiveState) as WebGlPrimitiveState;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @inheritDoc */
|
|
39
|
+
public get blendState(): IBlendState {
|
|
40
|
+
return this._blendState;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @inheritDoc */
|
|
44
|
+
public readonly primitiveState: WebGlPrimitiveState;
|
|
45
|
+
|
|
46
|
+
/** @inheritDoc */
|
|
47
|
+
public render(
|
|
48
|
+
_vertexBuffer: IVertexBuffer,
|
|
49
|
+
_indexBuffer: IIndexBuffer,
|
|
50
|
+
_indicesCount = -1,
|
|
51
|
+
_indicesOffset = 0
|
|
52
|
+
): void {
|
|
53
|
+
void _vertexBuffer;
|
|
54
|
+
void _indexBuffer;
|
|
55
|
+
void _indicesCount;
|
|
56
|
+
void _indicesOffset;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
/** @inheritDoc */
|
|
61
|
+
public vertexBufferLayouts: VertexBufferLayout[] = null!;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Provide WebGL buffers for the render pipeline.
|
|
65
|
+
* This method should be implemented by subclasses to provide the necessary buffers
|
|
66
|
+
* for creatine the vertex array object.
|
|
67
|
+
* @returns An array of WebGLBuffer objects to be used in the vertex array object.
|
|
68
|
+
*/
|
|
69
|
+
protected abstract _provideBuffers(): WebGLBuffer[] | null | undefined;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates the vertex array object for the render pipeline.
|
|
73
|
+
* This method binds the provided buffers and sets up the vertex attribute pointers based on the vertex buffer layouts.
|
|
74
|
+
*/
|
|
75
|
+
protected _createVertexArrayObject(): void {
|
|
76
|
+
this._vertexArrayObject = this._gl.createVertexArray();
|
|
77
|
+
const buffers = this._provideBuffers();
|
|
78
|
+
|
|
79
|
+
if (!buffers || buffers.length === 0) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (let i = 0; i < this.vertexBufferLayouts.length; i++) {
|
|
84
|
+
const layout = this.vertexBufferLayouts[i];
|
|
85
|
+
const buffer = buffers[i];
|
|
86
|
+
|
|
87
|
+
this._gl.bindVertexArray(this._vertexArrayObject);
|
|
88
|
+
this._gl.bindBuffer(this._gl.ARRAY_BUFFER, buffer);
|
|
89
|
+
|
|
90
|
+
const stride = layout.arrayStride;
|
|
91
|
+
for (const attribute of layout.attributes) {
|
|
92
|
+
|
|
93
|
+
const size = WebGlConverter.convertVertexFormat(attribute.format);
|
|
94
|
+
const index = attribute.shaderLocation;
|
|
95
|
+
const offset = attribute.offset;
|
|
96
|
+
|
|
97
|
+
this._gl.enableVertexAttribArray(index);
|
|
98
|
+
this._gl.vertexAttribPointer(index, size, this._gl.FLOAT, false, stride, offset);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Set up the pipeline state.
|
|
105
|
+
* This method should be called before rendering to set up the necessary pipeline state, such as blending, depth testing, etc.
|
|
106
|
+
*/
|
|
107
|
+
protected _setupPipeline(): void {
|
|
108
|
+
this._blendState.apply(this._gl);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** @inheritDoc */
|
|
112
|
+
public initialize(): void {
|
|
113
|
+
|
|
114
|
+
if (!this.vertexBufferLayouts || this.vertexBufferLayouts.length === 0) {
|
|
115
|
+
throw new Error('At least one vertex buffer layout must be provided for the render pipeline.');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this._createVertexArrayObject();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @inheritDoc */
|
|
122
|
+
public dispose(): void {
|
|
123
|
+
if (this._vertexArrayObject) {
|
|
124
|
+
this._gl.deleteVertexArray(this._vertexArrayObject);
|
|
125
|
+
this._vertexArrayObject = null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {vec2} from "gl-matrix";
|
|
2
|
+
import type {WebGlVertexBuffer} from "../buffers/WebGlVertexBuffer.ts";
|
|
3
|
+
import {GeometryFormat} from "../../geometry/GeometryFormat.ts";
|
|
4
|
+
import type {WebGLIndexBuffer} from "../buffers/webgl-index-buffer";
|
|
5
|
+
import {VertexBufferLayout} from "../../core/rendering/vertex-buffer-layout";
|
|
6
|
+
import type {WebGlTexture2D} from "../texture/WebGlTexture2D.ts";
|
|
7
|
+
import WebGlShaderModule from "../shader/WebGlShaderModule.ts";
|
|
8
|
+
import {AWebGlRenderPipeline} from "./AWebGlRenderPipeline.ts";
|
|
9
|
+
import {BufferUsage, type IFramework, type IMainRenderTargetRenderPipeline, type ITexture2D} from "ris-framework-api";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The WebGL implementation of the main render target render pipeline.
|
|
13
|
+
*/
|
|
14
|
+
export class WebGlMainRenderTargetRenderPipeline extends AWebGlRenderPipeline implements IMainRenderTargetRenderPipeline {
|
|
15
|
+
|
|
16
|
+
private _vertexBuffer: WebGlVertexBuffer = null!;
|
|
17
|
+
private _indexBuffer: WebGLIndexBuffer = null!;
|
|
18
|
+
private _mainRenderTarget: WebGlTexture2D = null!;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The constructor.
|
|
22
|
+
* @param framework The framework.
|
|
23
|
+
* @param mainRenderTarget The main render target texture. This is the texture that will be rendered to by this render pipeline. It should be the same texture as the one used in the swap chain's render pass descriptor for the main render target.
|
|
24
|
+
*/
|
|
25
|
+
public constructor(framework: IFramework, mainRenderTarget: ITexture2D) {
|
|
26
|
+
super(framework);
|
|
27
|
+
this._mainRenderTarget = mainRenderTarget as WebGlTexture2D;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @inheritdoc */
|
|
31
|
+
protected _provideBuffers(): WebGLBuffer[] {
|
|
32
|
+
return [this._vertexBuffer.buffer!];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @inheritdoc */
|
|
36
|
+
public get mainRenderTarget(): ITexture2D {
|
|
37
|
+
if (!this._mainRenderTarget) {
|
|
38
|
+
throw new Error("Render target is not set.");
|
|
39
|
+
}
|
|
40
|
+
return this._mainRenderTarget;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @inheritdoc */
|
|
44
|
+
public set mainRenderTarget(value: ITexture2D) {
|
|
45
|
+
this._mainRenderTarget = value as WebGlTexture2D;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @inheritdoc */
|
|
49
|
+
public initialize(): void {
|
|
50
|
+
const shaderModule = this._framework.content.loadShaderModule("main_render_target_flip_y") as WebGlShaderModule;
|
|
51
|
+
this._program = shaderModule.program!;
|
|
52
|
+
this._createResources();
|
|
53
|
+
this.vertexBufferLayouts = [VertexBufferLayout.createFloat3Float2Layout()];
|
|
54
|
+
super.initialize();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private _createResources(): void {
|
|
58
|
+
|
|
59
|
+
// By default, quad is from [-0.5, 0.5] space. We want to move it to
|
|
60
|
+
// [-1, 1] space to cover the whole screen.
|
|
61
|
+
const geometry = this._framework.geometryBuilder.quadGeometry(vec2.fromValues(2, 2));
|
|
62
|
+
const data = geometry.toInterleaved(GeometryFormat.POS3_TEXTURECOORDS2);
|
|
63
|
+
|
|
64
|
+
this._vertexBuffer = this._framework.bufferFactory.createVertexBuffer(
|
|
65
|
+
data,
|
|
66
|
+
(3 + 2) * Float32Array.BYTES_PER_ELEMENT,
|
|
67
|
+
BufferUsage.VERTEX,
|
|
68
|
+
"Main Frame Buffer Render Pipeline Vertex Buffer"
|
|
69
|
+
) as WebGlVertexBuffer;
|
|
70
|
+
|
|
71
|
+
this._indexBuffer = this._framework.bufferFactory.createIndexBuffer(
|
|
72
|
+
geometry.indices!, "Main Frame Buffer Render Pipeline Index Buffer",
|
|
73
|
+
) as WebGLIndexBuffer;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @inheritdoc */
|
|
77
|
+
public render(): void {
|
|
78
|
+
|
|
79
|
+
if (this._program == null) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this._setupPipeline();
|
|
84
|
+
|
|
85
|
+
this._gl.useProgram(this._program);
|
|
86
|
+
this._gl.bindVertexArray(this._vertexArrayObject);
|
|
87
|
+
this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, this._indexBuffer.buffer);
|
|
88
|
+
|
|
89
|
+
this._gl.activeTexture(this._gl.TEXTURE0);
|
|
90
|
+
this._gl.bindTexture(this._gl.TEXTURE_2D, this._mainRenderTarget.glTexture);
|
|
91
|
+
this._gl.bindSampler(0, this._defaultTextureSampler.glSampler);
|
|
92
|
+
|
|
93
|
+
this._gl.drawElements(this._gl.TRIANGLES, this._indexBuffer.indicesCount, this._gl.UNSIGNED_SHORT, 0);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { WebGlMainRenderTargetRenderPipeline } from "./WebGlMainRenderTargetRenderPipeline.ts";
|
|
2
|
+
import { WebGlSpriteRenderPipeline } from "./sprite/WebGlSpriteRenderPipeline.ts";
|
|
3
|
+
import type {
|
|
4
|
+
IFramework,
|
|
5
|
+
IInspectTextureMipsRenderPipeline,
|
|
6
|
+
IMainRenderTargetRenderPipeline, IPrimitiveState, IRenderPipelineFactory,
|
|
7
|
+
ISpriteRenderPipeline, ITexture2D,
|
|
8
|
+
IUniformBuffer,
|
|
9
|
+
IUnlitRenderPipeline
|
|
10
|
+
} from "ris-framework-api";
|
|
11
|
+
import {WebGlInspectTextureMipsRenderPipeline} from "./inspect/WebGlInspectTextureMipsRenderPipeline.ts";
|
|
12
|
+
import {WebGlUnlitRenderPipeline} from "./material/WebGlUnlitRenderPipeline.ts";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The WebGL implementation of the IRenderPipelineFactory interface.
|
|
16
|
+
* This factory is responsible for creating render pipelines for WebGL rendering.
|
|
17
|
+
*/
|
|
18
|
+
export class WebGlRenderPipelineFactory implements IRenderPipelineFactory {
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The constructor for the WebGLRenderPipelineFactory class.
|
|
22
|
+
* @param _framework The framework instance.
|
|
23
|
+
*/
|
|
24
|
+
constructor(private readonly _framework: IFramework) {
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @inheritDoc */
|
|
28
|
+
public createUnlitRenderPipeline(projectionViewBuffer: IUniformBuffer,
|
|
29
|
+
modelBuffer: IUniformBuffer,
|
|
30
|
+
materialBuffer: IUniformBuffer,
|
|
31
|
+
primitiveState?: IPrimitiveState
|
|
32
|
+
): IUnlitRenderPipeline {
|
|
33
|
+
const pipeline = new WebGlUnlitRenderPipeline(this._framework,
|
|
34
|
+
projectionViewBuffer, modelBuffer, materialBuffer, primitiveState
|
|
35
|
+
);
|
|
36
|
+
pipeline.initialize();
|
|
37
|
+
return pipeline;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @inheritDoc */
|
|
41
|
+
public createMainFrameBufferPipeline(mainFrameBuffer: ITexture2D): IMainRenderTargetRenderPipeline {
|
|
42
|
+
const renderPipeline = new WebGlMainRenderTargetRenderPipeline(this._framework, mainFrameBuffer);
|
|
43
|
+
renderPipeline.initialize();
|
|
44
|
+
return renderPipeline;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @inheritDoc */
|
|
48
|
+
public createInspectTextureMipsRenderPipeline(
|
|
49
|
+
projectionViewBuffer: IUniformBuffer,
|
|
50
|
+
modelBuffer: IUniformBuffer,
|
|
51
|
+
textureConstantsBuffer: IUniformBuffer,
|
|
52
|
+
primitiveState?: IPrimitiveState,
|
|
53
|
+
): IInspectTextureMipsRenderPipeline {
|
|
54
|
+
const pipeline = new WebGlInspectTextureMipsRenderPipeline(
|
|
55
|
+
this._framework, projectionViewBuffer, modelBuffer, textureConstantsBuffer, primitiveState
|
|
56
|
+
);
|
|
57
|
+
pipeline.initialize();
|
|
58
|
+
return pipeline;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @inheritdoc */
|
|
62
|
+
public createSpriteRenderPipeline(projectionViewBuffer: IUniformBuffer): ISpriteRenderPipeline {
|
|
63
|
+
const renderPipeline = new WebGlSpriteRenderPipeline(this._framework, projectionViewBuffer);
|
|
64
|
+
renderPipeline.initialize();
|
|
65
|
+
return renderPipeline;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @inheritdoc */
|
|
69
|
+
public createMainRenderTargetRenderPipeline(renderTarget: ITexture2D): IMainRenderTargetRenderPipeline {
|
|
70
|
+
const renderPipeline = new WebGlMainRenderTargetRenderPipeline(this._framework, renderTarget);
|
|
71
|
+
renderPipeline.initialize();
|
|
72
|
+
return renderPipeline;
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IFramework, IPrimitiveState,
|
|
3
|
+
ISampler,
|
|
4
|
+
ITexture2D,
|
|
5
|
+
IUniformBuffer,
|
|
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 {asWebGLTexture2D, 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
|
+
import type {
|
|
19
|
+
IInspectTextureMipsRenderPipeline
|
|
20
|
+
} from "ris-framework-api";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The WebGL implementation of the sprite render pipeline.
|
|
24
|
+
*/
|
|
25
|
+
export class WebGlInspectTextureMipsRenderPipeline extends AWebGlRenderPipeline implements IInspectTextureMipsRenderPipeline {
|
|
26
|
+
|
|
27
|
+
private static readonly CAMERA_BINDING_POINT = 0;
|
|
28
|
+
private static readonly MODEL_BINDING_POINT = 1;
|
|
29
|
+
private static readonly TEXTURE_CONSTANTS_BINDING_POINT = 2;
|
|
30
|
+
|
|
31
|
+
private _texture: WebGlTexture2D = null!;
|
|
32
|
+
private _sampler?: WebGlSampler;
|
|
33
|
+
private _projectionViewBuffer: WebGlUniformBuffer;
|
|
34
|
+
private _modelBuffer: WebGlUniformBuffer;
|
|
35
|
+
private _textureConstantsBuffer: WebGlUniformBuffer;
|
|
36
|
+
private _cameraBlockIndex: number = -1;
|
|
37
|
+
private _modelBlockIndex: number = -1;
|
|
38
|
+
private _textureConstantsBlockIndex : number = -1;
|
|
39
|
+
private _buffersArray: WebGLBuffer[] = [null!];
|
|
40
|
+
private _lastVertexBuffer: WebGlVertexBuffer | null = null;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The constructor.
|
|
44
|
+
* @param framework The framework.
|
|
45
|
+
* @param projectionViewBuffer The projection view buffer.
|
|
46
|
+
* @param modelBuffer The model buffer.
|
|
47
|
+
* @param textureConstantsBuffer The texture constants buffer.
|
|
48
|
+
*/
|
|
49
|
+
constructor(framework: IFramework,
|
|
50
|
+
projectionViewBuffer: IUniformBuffer,
|
|
51
|
+
modelBuffer: IUniformBuffer,
|
|
52
|
+
textureConstantsBuffer: IUniformBuffer,
|
|
53
|
+
primitiveState? : IPrimitiveState
|
|
54
|
+
) {
|
|
55
|
+
super(framework, primitiveState);
|
|
56
|
+
this._projectionViewBuffer = projectionViewBuffer as WebGlUniformBuffer;
|
|
57
|
+
this._modelBuffer = modelBuffer as WebGlUniformBuffer;
|
|
58
|
+
this._textureConstantsBuffer = textureConstantsBuffer as WebGlUniformBuffer;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
/** @inheritdoc */
|
|
63
|
+
public get projectionViewBuffer(): IUniformBuffer {
|
|
64
|
+
return this._projectionViewBuffer;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @inheritdoc */
|
|
68
|
+
public set projectionViewBuffer(value: IUniformBuffer) {
|
|
69
|
+
this._projectionViewBuffer = asWebGLUniformBuffer(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @inheritdoc */
|
|
73
|
+
public get modelBuffer(): IUniformBuffer {
|
|
74
|
+
return this._modelBuffer;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** @inheritdoc */
|
|
78
|
+
public set modelBuffer(value: IUniformBuffer) {
|
|
79
|
+
this._modelBuffer = asWebGLUniformBuffer(value);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @inheritdoc */
|
|
83
|
+
public get textureConstantsBuffer(): IUniformBuffer {
|
|
84
|
+
return this._textureConstantsBuffer;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @inheritdoc */
|
|
88
|
+
public set textureConstantsBuffer(value: IUniformBuffer) {
|
|
89
|
+
this._textureConstantsBuffer = asWebGLUniformBuffer(value);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @inheritdoc */
|
|
93
|
+
public get spriteTexture(): ITexture2D {
|
|
94
|
+
return this._texture;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @inheritdoc */
|
|
98
|
+
public set spriteTexture(value: ITexture2D ) {
|
|
99
|
+
this._texture = asWebGLTexture2D(value!);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @inheritdoc */
|
|
103
|
+
public get textureSampler(): ISampler | undefined {
|
|
104
|
+
return this._sampler;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public set textureSampler(value: ISampler | undefined) {
|
|
108
|
+
this._sampler = value as WebGlSampler | undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** @inheritdoc */
|
|
112
|
+
public override initialize(): void {
|
|
113
|
+
|
|
114
|
+
const module = this._framework.content.loadShaderModule("inspect_texture_mips") as WebGlShaderModule;
|
|
115
|
+
this._program = module.program!;
|
|
116
|
+
this._createResources();
|
|
117
|
+
this.vertexBufferLayouts = [VertexBufferLayout.createFloat3Float4Float2Layout()];
|
|
118
|
+
super.initialize();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private _createResources(): void {
|
|
122
|
+
{
|
|
123
|
+
this._texture = WebGlTexture2D.getOrCreateDefault(this._framework);
|
|
124
|
+
|
|
125
|
+
this._cameraBlockIndex = this._gl.getUniformBlockIndex(this._program, "CameraBuffer");
|
|
126
|
+
this._gl.uniformBlockBinding(this._program, this._cameraBlockIndex, WebGlInspectTextureMipsRenderPipeline.CAMERA_BINDING_POINT);
|
|
127
|
+
|
|
128
|
+
this._modelBlockIndex = this._gl.getUniformBlockIndex(this._program, "ModelBuffer");
|
|
129
|
+
this._gl.uniformBlockBinding(this._program, this._modelBlockIndex, WebGlInspectTextureMipsRenderPipeline.MODEL_BINDING_POINT);
|
|
130
|
+
|
|
131
|
+
this._textureConstantsBlockIndex = this._gl.getUniformBlockIndex(this._program, "TextureConstantsBuffer");
|
|
132
|
+
this._gl.uniformBlockBinding(this._program, this._textureConstantsBlockIndex, WebGlInspectTextureMipsRenderPipeline.TEXTURE_CONSTANTS_BINDING_POINT);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
/** @inheritdoc */
|
|
138
|
+
protected _provideBuffers(): WebGLBuffer[] | undefined | null {
|
|
139
|
+
if (this._lastVertexBuffer === null) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
this._buffersArray[0] = this._lastVertexBuffer.buffer!;
|
|
144
|
+
return this._buffersArray;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** @inheritdoc */
|
|
148
|
+
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount: number = -1, indicesOffset: number = 0): void {
|
|
149
|
+
|
|
150
|
+
if (indicesCount == 0) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const webGlVertexBuffer = vertexBuffer as WebGlVertexBuffer;
|
|
155
|
+
const webGlIndexBuffer = indexBuffer as WebGLIndexBuffer;
|
|
156
|
+
const webGlSampler = this._sampler ?? this._defaultTextureSampler;
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
this.primitiveState.apply(this._gl);
|
|
160
|
+
this._blendState.apply(this._gl);
|
|
161
|
+
|
|
162
|
+
// if it was changed, we need to create a new vao.
|
|
163
|
+
if (this._lastVertexBuffer != webGlVertexBuffer) {
|
|
164
|
+
// Create a new VAO. Internally deletes the old one.
|
|
165
|
+
this._lastVertexBuffer = webGlVertexBuffer;
|
|
166
|
+
this._createVertexArrayObject();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this._gl.useProgram(this._program);
|
|
170
|
+
// Bind the vao. It contains all the information about the vertex buffer layout ( vertices + instances)
|
|
171
|
+
this._gl.bindVertexArray(this._vertexArrayObject);
|
|
172
|
+
this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, webGlIndexBuffer.buffer);
|
|
173
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,
|
|
174
|
+
WebGlInspectTextureMipsRenderPipeline.CAMERA_BINDING_POINT,
|
|
175
|
+
this._projectionViewBuffer.glBuffer!);
|
|
176
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,
|
|
177
|
+
WebGlInspectTextureMipsRenderPipeline.MODEL_BINDING_POINT,
|
|
178
|
+
this._modelBuffer.glBuffer!);
|
|
179
|
+
this._gl.bindBufferBase(this._gl.UNIFORM_BUFFER,
|
|
180
|
+
WebGlInspectTextureMipsRenderPipeline.TEXTURE_CONSTANTS_BINDING_POINT,
|
|
181
|
+
this._textureConstantsBuffer.glBuffer!);
|
|
182
|
+
this._gl.activeTexture(this._gl.TEXTURE0);
|
|
183
|
+
this._gl.bindTexture(this._gl.TEXTURE_2D, this._texture!.glTexture);
|
|
184
|
+
this._gl.bindSampler(0, webGlSampler.glSampler);
|
|
185
|
+
|
|
186
|
+
// We can only really use two types uint16 and uint32. Boolean check to see which one to use.
|
|
187
|
+
const type = indexBuffer.type == IndexBufferType.UINT_16
|
|
188
|
+
? this._gl.UNSIGNED_SHORT
|
|
189
|
+
: this._gl.UNSIGNED_INT;
|
|
190
|
+
|
|
191
|
+
// api.PolygonMode(GLEnum.FrontAndBack, GLEnum.Line);
|
|
192
|
+
|
|
193
|
+
// Either draw all indices or a specific amount. If indicesCount is -1, draw all as defined by the index buffer.
|
|
194
|
+
const toIndices = indicesCount > 0 ? indicesCount : indexBuffer.indicesCount;
|
|
195
|
+
const fromIndices = indicesOffset * indexBuffer.elementByteSize;
|
|
196
|
+
|
|
197
|
+
this._gl.drawElements(this.primitiveState.glPrimitiveType, toIndices, type, fromIndices);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
}
|