ris-framework 0.1.0-dev.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +11 -0
  4. package/dist/assets/_ris-ktx2-node-module-stub-DwDS30PT.js +1 -0
  5. package/dist/assets/cat.jpg +0 -0
  6. package/dist/assets/index-BmeGit54.css +1 -0
  7. package/dist/assets/index-qyn4pzGw.js +151 -0
  8. package/dist/assets/libktx-Cjh8w7XG-Ci1Fm_MH.js +1 -0
  9. package/dist/assets/test.png +0 -0
  10. package/dist/index.html +16 -0
  11. package/dist/vite.svg +1 -0
  12. package/package.json +37 -0
  13. package/shaders/glsl/inspect_texture_mips_fs.glsl +18 -0
  14. package/shaders/glsl/inspect_texture_mips_vs.glsl +25 -0
  15. package/shaders/glsl/main_render_target_flip_y_fs.glsl +9 -0
  16. package/shaders/glsl/main_render_target_flip_y_vs.glsl +9 -0
  17. package/shaders/glsl/sprite_fs.glsl +14 -0
  18. package/shaders/glsl/sprite_vs.glsl +20 -0
  19. package/shaders/glsl/unlit_material_fs.glsl +19 -0
  20. package/shaders/glsl/unlit_material_vs.glsl +25 -0
  21. package/src/TextureFormat.ts +0 -0
  22. package/src/VertexFormat.ts +17 -0
  23. package/src/common/sampler-enums.ts +21 -0
  24. package/src/common/vertex-step-mode.ts +12 -0
  25. package/src/content/ShaderModuleContent.ts +47 -0
  26. package/src/core/Framework.ts +240 -0
  27. package/src/core/FrameworkConfig.ts +31 -0
  28. package/src/core/buffers/buffers-factory-interface.ts +0 -0
  29. package/src/core/buffers/vertex-buffer-interface.ts +0 -0
  30. package/src/core/content/ContentManager.ts +86 -0
  31. package/src/core/dependency-injection/register-services-interface.ts +15 -0
  32. package/src/core/framework-interface.ts +0 -0
  33. package/src/core/input/InputManager.ts +115 -0
  34. package/src/core/renderer/enums.ts +20 -0
  35. package/src/core/renderer/renderer-interface.ts +92 -0
  36. package/src/core/renderer/rendering-limits.ts +20 -0
  37. package/src/core/rendering/AGraphicsDevice.ts +119 -0
  38. package/src/core/rendering/ARenderer.ts +247 -0
  39. package/src/core/rendering/blending/blend-component-descriptor.ts +33 -0
  40. package/src/core/rendering/blending/blend-state-descriptor.ts +51 -0
  41. package/src/core/rendering/blending/enums.ts +50 -0
  42. package/src/core/rendering/enums.ts +22 -0
  43. package/src/core/rendering/primitive/PrimitiveStateDescriptor.ts +32 -0
  44. package/src/core/rendering/primitive/enums.ts +68 -0
  45. package/src/core/rendering/render-pass/render-pass-descriptor.ts +0 -0
  46. package/src/core/rendering/render-pass/render-pass-interface.ts +17 -0
  47. package/src/core/rendering/sampler/sampler-descriptor.ts +33 -0
  48. package/src/core/rendering/texture/texture.ts +146 -0
  49. package/src/core/rendering/vertex-attribute.ts +20 -0
  50. package/src/core/rendering/vertex-buffer-layout.ts +84 -0
  51. package/src/core/shader/shader-loader.ts +27 -0
  52. package/src/core/shader/shader-module-interface.ts +1 -0
  53. package/src/core/sprite-batch/SpriteBatch.ts +216 -0
  54. package/src/core/sprite-batch/SpriteBatchDrawable.ts +146 -0
  55. package/src/core/sprite-batch/SpriteBatchMesh.ts +243 -0
  56. package/src/core/window/window-manager.ts +76 -0
  57. package/src/counter.ts +9 -0
  58. package/src/geometry/BaseGeometry.ts +148 -0
  59. package/src/geometry/GeometryBuilder.ts +58 -0
  60. package/src/geometry/GeometryFormat.ts +37 -0
  61. package/src/main.ts +97 -0
  62. package/src/style.css +96 -0
  63. package/src/typescript.svg +1 -0
  64. package/src/webgl/WebGLGraphicsDeviceFeatures.ts +57 -0
  65. package/src/webgl/WebGlGpuInfo.ts +76 -0
  66. package/src/webgl/WebGlGraphicsDevice.ts +129 -0
  67. package/src/webgl/WebGlRenderer.ts +31 -0
  68. package/src/webgl/blending/webgl-blend-state.ts +59 -0
  69. package/src/webgl/buffers/WebGlBuffersFactory.ts +75 -0
  70. package/src/webgl/buffers/WebGlUniformBuffer.ts +87 -0
  71. package/src/webgl/buffers/WebGlVertexBuffer.ts +100 -0
  72. package/src/webgl/buffers/webgl-index-buffer.ts +110 -0
  73. package/src/webgl/cast/cast.ts +100 -0
  74. package/src/webgl/primitive/WebGlPrimitiveState.ts +60 -0
  75. package/src/webgl/render-pass/WebGlRenderPass.ts +186 -0
  76. package/src/webgl/render-pipelines/AWebGlRenderPipeline.ts +129 -0
  77. package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +95 -0
  78. package/src/webgl/render-pipelines/WebGlRenderPipelineFactory.ts +75 -0
  79. package/src/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.ts +200 -0
  80. package/src/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.ts +197 -0
  81. package/src/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.ts +148 -0
  82. package/src/webgl/rendering/webgl-vertex-attribute.ts +16 -0
  83. package/src/webgl/rendering/webgl-vertex-buffer-layout.ts +64 -0
  84. package/src/webgl/sampler/webgl-sampler.ts +110 -0
  85. package/src/webgl/shader/WebGlShaderModule.ts +110 -0
  86. package/src/webgl/shader/WebGlShaderModuleLoader.ts +40 -0
  87. package/src/webgl/swap-chain/WebGlSwapChain.ts +56 -0
  88. package/src/webgl/texture/WebGlTexture2D.ts +133 -0
  89. package/src/webgl/texture/WebGlTextureFactory.ts +184 -0
  90. package/src/webgl/utilities/WebGlBufferUtilities.ts +124 -0
  91. package/src/webgl/utilities/WebGlConverter.ts +391 -0
  92. package/src/webgl/utilities/WebGlFrameBufferUtilities.ts +240 -0
  93. package/src/webgl/utilities/WebGlSamplerUtilities.ts +81 -0
  94. package/src/webgl/utilities/WebGlShaderUtilities.ts +48 -0
  95. package/src/webgl/utilities/WebGlTextureUtilities.ts +162 -0
  96. package/src/webgl/utilities/WebGlUtilities.ts +71 -0
  97. package/src/webgl/utilities/spector-js-utilities.ts +13 -0
  98. package/src/webgl/utilities/webgl-blending-utilities.ts +85 -0
  99. package/src/webgl/utilities/webgl-culling-utilities.ts +23 -0
  100. package/src/webgl/utilities/webgl-filter-anisotropic-utilities.ts +37 -0
  101. package/src/webgl/utilities/webgl-program-utilities.ts +29 -0
  102. package/src/webgl/utilities/webgl-render-buffer-utilities.ts +23 -0
  103. package/src/webgl/utilities/webgl-renderbuffer-utilities.ts +35 -0
@@ -0,0 +1,20 @@
1
+
2
+ /**
3
+ * The culling mode.
4
+ */
5
+ export enum Culling {
6
+ /**
7
+ * The culling is disabled. Both front and back faces will be rendered.
8
+ */
9
+ None,
10
+
11
+ /**
12
+ * The back faces will be culled. Only front faces will be rendered.
13
+ */
14
+ Back,
15
+
16
+ /**
17
+ * The front faces will be culled. Only back faces will be rendered.
18
+ */
19
+ Front
20
+ }
@@ -0,0 +1,92 @@
1
+ import { vec2 } from "gl-matrix";
2
+ import { TextureSamplerFilteringPreset } from "../rendering/enums";
3
+ import type {RenderingLimits} from "./rendering-limits.ts";
4
+ import type {Color, IGraphicsDevice, IRenderer} from "ris-framework-api";
5
+ import type {TextureFormat} from "ris-framework-api";
6
+
7
+ export const RenderConfigurationSymbol = Symbol("RenderConfiguration");
8
+
9
+ /**
10
+ * The configuration for the renderer. This is used to initialize the renderer.
11
+ */
12
+ export class RenderConfiguration {
13
+
14
+ /**
15
+ * The size of the main frame buffer. This is used to initialize the main render target.
16
+ */
17
+ backBufferSize: vec2 = vec2.fromValues(800, 600);
18
+
19
+ /**
20
+ * The preset for texture sampler filtering.
21
+ * This is used to configure the default texture sampler in the graphics device.
22
+ * The default texture sampler is used when a texture is sampled without a specific sampler being bound.
23
+ */
24
+ textureFiltering = TextureSamplerFilteringPreset.BILINEAR;
25
+ }
26
+
27
+ /**
28
+ * The interface for renderers.
29
+ */
30
+ export interface ITempRenderer extends IRenderer {
31
+
32
+ /**
33
+ * The clear color used by the renderer.
34
+ */
35
+ clearColor: Color
36
+
37
+ /**
38
+ * The rendering limits of the renderer.
39
+ */
40
+ limits: RenderingLimits | null;
41
+
42
+ /**
43
+ * The graphics device used by the renderer.
44
+ * This is used to create resources and manage the rendering context.
45
+ */
46
+ readonly graphicsDevice: IGraphicsDevice;
47
+
48
+ /**
49
+ * The preferred texture format for the renderer.
50
+ * @returns The preferred texture format for the renderer.
51
+ */
52
+ get preferredTextureFormat(): TextureFormat;
53
+
54
+ /**
55
+ * The preferred depth stencil format for the renderer.
56
+ * @returns The preferred depth stencil format for the renderer.
57
+ */
58
+ get preferredDepthStencilFormat(): TextureFormat;
59
+
60
+ /**
61
+ * The size of the back buffer.
62
+ */
63
+ backBufferSize: vec2;
64
+
65
+ /**
66
+ * Whether the back buffer size matches the swap chain size.
67
+ * If <code>true</code>, the renderer will assume that the back buffer size matches the swap chain size,
68
+ * overriding the <code>backBufferSize</code> property.
69
+ */
70
+ backBufferMatchesSwapChain: boolean;
71
+
72
+ /**
73
+ * Initializes the renderer.
74
+ */
75
+ initialize(): void;
76
+
77
+ /**
78
+ * Performs any additional initialization steps after the main initialization is complete.
79
+ * This can be used to set up resources that depend on the graphics device or swap chain being initialized.
80
+ */
81
+ afterInitialize(): void;
82
+
83
+ /**
84
+ * Begins a new render pass.
85
+ */
86
+ beginRenderPass(): void;
87
+
88
+ /**
89
+ * Ends the current render pass.
90
+ */
91
+ endRenderPass(): void;
92
+ }
@@ -0,0 +1,20 @@
1
+ export class RenderingLimits {
2
+
3
+ /**
4
+ * The constructor.
5
+ * @param _maximumAnisotropyLevel The maximum anisotropic filtering level.
6
+ */
7
+ constructor(private _maximumAnisotropyLevel: number) {
8
+
9
+ }
10
+
11
+ /**
12
+ * The maximum anisotropy level supported by the renderer.
13
+ * This value is typically determined by the graphics hardware and driver capabilities.
14
+ * It indicates the highest level of anisotropic filtering that can be applied to textures,
15
+ * which can improve the visual quality of textures when viewed at oblique angles.
16
+ */
17
+ public get maximumAnisotropyLevel() : number {
18
+ return this._maximumAnisotropyLevel;
19
+ }
20
+ }
@@ -0,0 +1,119 @@
1
+ import {BlendStateDescriptor} from "./blending/blend-state-descriptor";
2
+ import {TextureSamplerFilteringPreset} from "./enums";
3
+ import type {PrimitiveStateDescriptor} from "./primitive/PrimitiveStateDescriptor.ts";
4
+ import type {IRenderPass} from "./render-pass/render-pass-interface";
5
+ import {SamplerDescriptor} from "./sampler/sampler-descriptor";
6
+ import {
7
+ type IGPUInfo,
8
+ type IGraphicsDeviceFeatures,
9
+ type ISampler,
10
+ type IBlendState,
11
+ type IPrimitiveState,
12
+ type IGraphicsDevice, SwapChainDescriptor, type RenderPassDescriptor, SamplerFilter, MipMapSamplerFilter,
13
+ type ISwapChain
14
+ } from "ris-framework-api";
15
+
16
+ /**
17
+ * The descriptor for the graphics device. This is used to configure the graphics device during initialization.
18
+ */
19
+ export class GraphicsDeviceDescriptor {
20
+
21
+ /**
22
+ * Defines the preset for texture sampler filtering.
23
+ * This is used to configure the default texture sampler in the graphics device.
24
+ * The default texture sampler is used when a texture is sampled without a specific sampler being bound.
25
+ */
26
+ public samplerFilteringPreset = TextureSamplerFilteringPreset.BILINEAR;
27
+ }
28
+
29
+ /**
30
+ * The abstract base class for graphics devices.
31
+ */
32
+ export abstract class AGraphicsDevice implements IGraphicsDevice {
33
+
34
+ protected readonly _descriptor: GraphicsDeviceDescriptor;
35
+ protected _defaultTextureSampler: ISampler = null!;
36
+ protected _defaultBlendState: IBlendState = null!;
37
+ protected _defaultPrimitiveState: IPrimitiveState = null!;
38
+
39
+ /**
40
+ * The constructor.
41
+ * @param descriptor The descriptor for the graphics device. This is used to configure the graphics device during initialization.
42
+ */
43
+ protected constructor(descriptor: GraphicsDeviceDescriptor) {
44
+ this._descriptor = descriptor;
45
+ }
46
+
47
+ /** @inheritdoc */
48
+ abstract readonly features : IGraphicsDeviceFeatures;
49
+
50
+ /** @inheritdoc */
51
+ abstract readonly gpuInfo: IGPUInfo;
52
+
53
+ /** @inheritdoc */
54
+ public get defaultTextureSampler(): ISampler {
55
+ return this._defaultTextureSampler;
56
+ }
57
+
58
+ /** @inheritdoc */
59
+ public get defaultBlendState(): IBlendState {
60
+ return this._defaultBlendState;
61
+ }
62
+
63
+ /** @inheritdoc */
64
+ public get defaultPrimitiveState(): IPrimitiveState {
65
+ return this._defaultPrimitiveState;
66
+ }
67
+
68
+ /**
69
+ * Configures and creates the default texture sampler based on the sampler filtering preset specified in the graphics device descriptor. The method creates a new sampler descriptor, sets its properties according to the selected filtering preset, and then creates and returns a new sampler using the configured descriptor.
70
+ * @returns The default texture sampler configured according to the sampler filtering preset specified in the graphics device descriptor.
71
+ */
72
+ protected configureAndCreateDefaultSampler(): ISampler {
73
+ var samplerDescriptor = new SamplerDescriptor();
74
+ samplerDescriptor.minFilter = this._descriptor.samplerFilteringPreset == TextureSamplerFilteringPreset.POINT ? SamplerFilter.NEAREST : SamplerFilter.LINEAR;
75
+ samplerDescriptor.magFilter = this._descriptor.samplerFilteringPreset == TextureSamplerFilteringPreset.POINT ? SamplerFilter.NEAREST : SamplerFilter.LINEAR;
76
+ samplerDescriptor.mipMapFilter = this._descriptor.samplerFilteringPreset == TextureSamplerFilteringPreset.TRILINEAR ? MipMapSamplerFilter.LINEAR : MipMapSamplerFilter.NONE;
77
+ return this.createSampler(samplerDescriptor);
78
+ }
79
+
80
+ /** @inheritdoc */
81
+ public initialize(): void {
82
+ this._defaultTextureSampler = this.configureAndCreateDefaultSampler();
83
+ this._defaultBlendState = this.createBlendState(new BlendStateDescriptor());
84
+ this._defaultPrimitiveState = this.createPrimitiveState();
85
+ }
86
+
87
+ /** @inheritdoc */
88
+ public abstract createSwapChain(canvas: HTMLCanvasElement, swapChainDescriptor: SwapChainDescriptor): ISwapChain;
89
+
90
+ /** @inheritdoc */
91
+ public abstract createSampler(descriptor?: SamplerDescriptor): ISampler;
92
+
93
+ /** @inheritdoc */
94
+ public abstract createRenderPass(descriptor: RenderPassDescriptor): IRenderPass;
95
+
96
+ /** @inheritdoc */
97
+ public abstract createBlendState(descriptor: BlendStateDescriptor): IBlendState;
98
+
99
+ /** @inheritdoc */
100
+ public abstract createPrimitiveState(descriptor?: PrimitiveStateDescriptor ): IPrimitiveState;
101
+
102
+ /** @inheritDoc */
103
+ public abstract setupDebugCallback(): void;
104
+
105
+ /** @inheritDoc */
106
+ public abstract frameEnd(): void;
107
+
108
+ /** @inheritDoc */
109
+ public abstract pushDebugGroup(groupName: string): void;
110
+
111
+ /** @inheritDoc */
112
+ public abstract popDebugGroup(): void;
113
+
114
+ /** @inheritDoc */
115
+ public dispose(): void {
116
+ this._defaultTextureSampler.dispose();
117
+ this._defaultBlendState.dispose();
118
+ }
119
+ }
@@ -0,0 +1,247 @@
1
+ import type { ITempRenderer } from "../renderer/renderer-interface.ts";
2
+ import type { IRenderPass } from "./render-pass/render-pass-interface.ts";
3
+ import type { vec2 } from "gl-matrix";
4
+ import type { RenderingLimits } from "../renderer/rendering-limits.ts";
5
+ import {
6
+ type IFramework, type IGraphicsDevice, type IMainRenderTargetRenderPipeline, type ISwapChain,
7
+ type ITexture2D, type IWindowManager,
8
+ RenderingBackend,
9
+ RenderPassColorAttachment, RenderPassDepthStencilAttachment,
10
+ RenderPassDescriptor, SwapChainDescriptor,
11
+ TextureUsage
12
+ } from "ris-framework-api";
13
+ import {TextureFormat, Color} from "ris-framework-api";
14
+
15
+ export abstract class ARenderer implements ITempRenderer {
16
+
17
+ protected readonly _framework: IFramework;
18
+
19
+ /**
20
+ * The resize listeners.
21
+ */
22
+ protected _resizeListeners: ((sender: any, size: vec2) => void)[] = [];
23
+
24
+ private readonly _windowManager: IWindowManager;
25
+ private _graphicsDevice: IGraphicsDevice = null!;
26
+ private _preferredTextureFormat: TextureFormat = TextureFormat.RGBA_8_UNORM;
27
+ private _preferredDepthStencilFormat: TextureFormat = TextureFormat.DEPTH_24_STENCIL_8;
28
+ private readonly _currentBackBufferSize: vec2 = [0, 0];
29
+ private _swapChainRequiresResize: boolean = false;
30
+ private _mainRenderPassDirty = true;
31
+ private _clearColor: Color = Color.lightPink();
32
+
33
+ protected _mainRenderTargetRequiresResize: boolean = false;
34
+ protected _swapChain: ISwapChain = null!;
35
+ protected _mainRenderTarget: ITexture2D = null!;
36
+ protected _depthStencilBuffer: ITexture2D = null!;
37
+ protected _swapChainRenderPass: IRenderPass = null!;
38
+ protected _mainRenderTargetRenderPass?: IRenderPass;
39
+ protected _mainRenderTargetPipeline?: IMainRenderTargetRenderPipeline;
40
+
41
+ /** @inheritdoc */
42
+ public get graphicsDevice(): IGraphicsDevice {
43
+ return this._graphicsDevice;
44
+ }
45
+
46
+ /** @inheritdoc */
47
+ public get clearColor(): Color {
48
+ return this._clearColor;
49
+ }
50
+
51
+ /** @inheritdoc */
52
+ public set clearColor(color: Color) {
53
+ this._clearColor = color;
54
+ this._mainRenderPassDirty = true;
55
+ }
56
+
57
+ public get limits(): RenderingLimits | null {
58
+ throw new Error("Method not implemented.");
59
+ }
60
+
61
+ /** @inheritdoc */
62
+ public get preferredTextureFormat(): TextureFormat {
63
+ return this._preferredTextureFormat;
64
+ }
65
+
66
+ /** @inheritdoc */
67
+ public get preferredDepthStencilFormat(): TextureFormat {
68
+ return this._preferredDepthStencilFormat;
69
+ }
70
+
71
+ /** @inheritdoc */
72
+ public get backBufferSize(): vec2 {
73
+ return this._currentBackBufferSize;
74
+ }
75
+
76
+ /** @inheritdoc */
77
+ public set backBufferSize(size: vec2) {
78
+ if (this.backBufferMatchesSwapChain) {
79
+ console.warn(`Attempting to set back buffer size to ${size[0]}x${size[1]}, but back buffer is currently matching the swap chain.
80
+ To set the back buffer size, either set backBufferMatchesSwapChain to false, or resize the swap chain using the setSize method on the swap chain.`);
81
+ } else {
82
+ this._currentBackBufferSize[0] = size[0];
83
+ this._currentBackBufferSize[1] = size[1];
84
+ this._mainRenderTargetRequiresResize = true;
85
+
86
+ // TODO: should we resize swapchain as well?
87
+ this._swapChainRequiresResize = true;
88
+ }
89
+ }
90
+
91
+ /** @inheritdoc */
92
+ public backBufferMatchesSwapChain: boolean = false;
93
+
94
+ /**
95
+ * The constructor.
96
+ * @param framework The framework.
97
+ */
98
+ constructor(framework: IFramework) {
99
+ this._framework = framework;
100
+ this._windowManager = framework.windowManager;
101
+ this._currentBackBufferSize[0] = this._windowManager.canvas.width;
102
+ this._currentBackBufferSize[1] = this._windowManager.canvas.height;
103
+ }
104
+
105
+ backend: RenderingBackend = RenderingBackend.WEB_GL;
106
+
107
+ /** @inheritDoc */
108
+ public addOnResizedListener(event: (sender: any, e: vec2) => void): void {
109
+ this._resizeListeners.push(event);
110
+ }
111
+
112
+ /** @inheritDoc */
113
+ public removeOnResizedListener(event: (sender: any, e: vec2) => void): void {
114
+ const index = this._resizeListeners.indexOf(event);
115
+ if (index >= 0) {
116
+ this._resizeListeners.splice(index, 1);
117
+ }
118
+ }
119
+ beginComputePass(): void {
120
+ throw new Error("Method not implemented.");
121
+ }
122
+ endComputePass(): void {
123
+ throw new Error("Method not implemented.");
124
+ }
125
+
126
+ /**
127
+ * Creates the graphics device for the renderer.
128
+ * This is an abstract method that must be implemented by subclasses to provide the specific graphics device implementation for the renderer.
129
+ * @returns The graphics device for the renderer.
130
+ */
131
+ protected abstract createGraphicsDevice(): IGraphicsDevice;
132
+
133
+ /** @inheritdoc */
134
+ public initialize(): void {
135
+ this._graphicsDevice = this.createGraphicsDevice();
136
+ this._graphicsDevice.initialize();
137
+ this._swapChain = this._graphicsDevice.createSwapChain(
138
+ this._windowManager.canvas,
139
+ new SwapChainDescriptor());
140
+ this._preferredTextureFormat = this._swapChain.textureFormat
141
+
142
+ // TODO: RESIZE EVENT
143
+
144
+ }
145
+
146
+ /** @inheritdoc */
147
+ public afterInitialize(): void {
148
+
149
+ let colorAttachment = new RenderPassColorAttachment();
150
+ colorAttachment.swapChain = this._swapChain;
151
+ this._swapChainRenderPass = this._graphicsDevice.createRenderPass({
152
+ colorAttachments: [colorAttachment],
153
+ });
154
+
155
+ this._setupMainRenderPass();
156
+ }
157
+
158
+ private _setupMainRenderPass(): void {
159
+
160
+ this._mainRenderTargetPipeline?.dispose();
161
+ this._depthStencilBuffer?.dispose();
162
+ this._mainRenderTarget?.dispose();
163
+
164
+ // Create a main frame buffer.
165
+ this._mainRenderTarget = this._framework.textureFactory.createEmpty(
166
+ this.backBufferSize[0], this.backBufferSize[1],
167
+ undefined,
168
+ TextureUsage.TEXTURE_BINDING | TextureUsage.RENDER_ATTACHMENT,
169
+ this.preferredTextureFormat
170
+ );
171
+
172
+ this._depthStencilBuffer = this._framework.textureFactory.createEmpty(
173
+ this.backBufferSize[0], this.backBufferSize[1],
174
+ undefined,
175
+ TextureUsage.RENDER_ATTACHMENT,
176
+ this.preferredDepthStencilFormat);
177
+
178
+ // Create main frame buffer pipeline.
179
+ this._mainRenderTargetPipeline = this._framework
180
+ .renderPipelineFactory
181
+ .createMainFrameBufferPipeline(this._mainRenderTarget);
182
+
183
+ this._createMainRenderPass();
184
+ this._mainRenderTargetRequiresResize = false;
185
+ }
186
+
187
+ private _createMainRenderPass(): void {
188
+ this._mainRenderTargetRenderPass?.dispose();
189
+ const renderPassDesc = new RenderPassDescriptor();
190
+ const colorAttachment = new RenderPassColorAttachment();
191
+ colorAttachment.texture = this._mainRenderTarget;
192
+ colorAttachment.clearColor = this._clearColor;
193
+ renderPassDesc.colorAttachments = [colorAttachment];
194
+ renderPassDesc.depthStencilAttachment = new RenderPassDepthStencilAttachment();
195
+ renderPassDesc.depthStencilAttachment.texture = this._depthStencilBuffer;
196
+ this._mainRenderTargetRenderPass = this._graphicsDevice.createRenderPass(renderPassDesc);
197
+ this._mainRenderPassDirty = false;
198
+ }
199
+
200
+ private _handleResizing(): void {
201
+ // If back buffer size is set to match swap chain size, then main render target needs
202
+ // to be resized whenever swap chain needs to be resized.
203
+ // If back buffer size is not set to match swap chain size,
204
+ // then main render target only needs to be resized when back buffer size is changed.
205
+ if ((this.backBufferMatchesSwapChain && this._swapChainRequiresResize) || this._mainRenderTargetRequiresResize) {
206
+ this._setupMainRenderPass();
207
+
208
+ for(const listener of this._resizeListeners){
209
+ listener(this, this.backBufferSize);
210
+ }
211
+ }
212
+
213
+ if (!this._swapChainRequiresResize) {
214
+ return;
215
+ }
216
+
217
+ // Note: surface texture and surface texture view must be released before trying to resize.
218
+ this._swapChain.resize(this.backBufferSize[0], this.backBufferSize[1]);
219
+
220
+ // ConfigureDepthStencil();
221
+ this._swapChainRequiresResize = false;
222
+ }
223
+
224
+ /** @inheritdoc */
225
+ public beginRenderPass(): void {
226
+ this._mainRenderTargetRenderPass!.beginPass();
227
+ }
228
+
229
+ /** @inheritdoc */
230
+ public endRenderPass(): void {
231
+ this._mainRenderTargetRenderPass!.endPass();
232
+
233
+ this._handleResizing();
234
+
235
+ this._swapChainRenderPass!.beginPass();
236
+ this._mainRenderTargetPipeline!.render();
237
+ this._swapChainRenderPass!.endPass();
238
+
239
+ this._swapChain.present();
240
+
241
+ // For example, clear color change.
242
+ if(this._mainRenderPassDirty) {
243
+ this._createMainRenderPass();
244
+ }
245
+ }
246
+
247
+ }
@@ -0,0 +1,33 @@
1
+ import { BlendFactor, BlendOperation } from "./enums";
2
+
3
+ /**
4
+ * Descriptor for a blend component, which defines how source and destination colors are blended together.
5
+ */
6
+ export class BlendComponentDescriptor {
7
+
8
+ /**
9
+ * Multiplier for the source color/alpha which is the output of the fragment shader.
10
+ */
11
+ public srcFactor: BlendFactor = BlendFactor.ONE;
12
+
13
+ /**
14
+ * Multiplier for the destination color/alpha which is the color/alpha already in the framebuffer.
15
+ */
16
+ public dstFactor: BlendFactor = BlendFactor.ONE_MINUS_SRC_ALPHA;
17
+
18
+ /**
19
+ * The operation to perform on the source and destination colors, multiplied by their respective factors.
20
+ */
21
+ public operation: BlendOperation = BlendOperation.ADD;
22
+
23
+ /**
24
+ * Compares this blend component descriptor with another for equality.
25
+ * @param other The other blend component descriptor to compare with.
26
+ * @returns True if the two descriptors are equal, false otherwise.
27
+ */
28
+ public equal(other: BlendComponentDescriptor): boolean {
29
+ return this.srcFactor === other.srcFactor &&
30
+ this.dstFactor === other.dstFactor &&
31
+ this.operation === other.operation;
32
+ }
33
+ }
@@ -0,0 +1,51 @@
1
+ import { BlendComponentDescriptor } from "./blend-component-descriptor";
2
+ import { BlendFactor, BlendOperation } from "./enums";
3
+
4
+ /**
5
+ * Describes the blend state of a render pipeline.
6
+ */
7
+ export class BlendStateDescriptor {
8
+
9
+ /**
10
+ * The constructor.
11
+ */
12
+ public constructor() {
13
+ const colorState = new BlendComponentDescriptor();
14
+ colorState.srcFactor = BlendFactor.SRC_ALPHA;
15
+ colorState.dstFactor = BlendFactor.ONE_MINUS_SRC_ALPHA;
16
+ colorState.operation = BlendOperation.ADD;
17
+ this.color = colorState
18
+
19
+ const alphaState = new BlendComponentDescriptor();
20
+ alphaState.srcFactor = BlendFactor.ONE;
21
+ alphaState.dstFactor = BlendFactor.ONE_MINUS_SRC_ALPHA;
22
+ alphaState.operation = BlendOperation.ADD;
23
+ this.alpha = alphaState;
24
+ }
25
+
26
+ /**
27
+ * Whether blending is enabled. If false, the source color is written to the render target without blending.
28
+ */
29
+ public blendingEnabled: boolean = true;
30
+
31
+ /**
32
+ * Color equation.
33
+ */
34
+ public color: BlendComponentDescriptor;
35
+
36
+ /**
37
+ * Alpha equation.
38
+ */
39
+ public alpha: BlendComponentDescriptor;
40
+
41
+ /**
42
+ * Compares this blend state descriptor with another for equality.
43
+ * @param other The other blend state descriptor to compare with.
44
+ * @returns True if the two descriptors are equal, false otherwise.
45
+ */
46
+ public equal(other: BlendStateDescriptor): boolean {
47
+ return this.blendingEnabled === other.blendingEnabled &&
48
+ this.color.equal(other.color) &&
49
+ this.alpha.equal(other.alpha);
50
+ }
51
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The blend operation.
3
+ */
4
+ export enum BlendOperation {
5
+ /**
6
+ * Add the source and destination colors together, multiplied by their respective factors.
7
+ */
8
+ ADD,
9
+
10
+ /**
11
+ * Subtract the destination color from the source color, multiplied by their respective factors.
12
+ */
13
+ SUBTRACT,
14
+
15
+ /**
16
+ * Reverse subtract the source color from the destination color, multiplied by their respective factors.
17
+ */
18
+ REVERSE_SUBTRACT,
19
+
20
+ /**
21
+ * Select the minimum of the source and destination colors, multiplied by their respective factors.
22
+ */
23
+ MIN,
24
+
25
+ /**
26
+ * Select the maximum of the source and destination colors, multiplied by their respective factors.
27
+ */
28
+ MAX,
29
+ }
30
+
31
+ /// <summary>
32
+ /// The blend factor.
33
+ /// </summary>
34
+ export enum BlendFactor {
35
+
36
+ /**
37
+ * <c>1.0</c>
38
+ */
39
+ ONE,
40
+
41
+ /**
42
+ * Source.Alpha, where Source is the color being drawn.
43
+ */
44
+ SRC_ALPHA,
45
+
46
+ /**
47
+ * <c>1.0 - Source.Alpha</c>, where Source is the color being drawn.
48
+ */
49
+ ONE_MINUS_SRC_ALPHA,
50
+ }
@@ -0,0 +1,22 @@
1
+
2
+ /**
3
+ * The preset for texture sampler filtering.
4
+ * This is used to configure the default texture sampler in the graphics device.
5
+ * The default texture sampler is used when a texture is sampled without a specific sampler being bound.
6
+ */
7
+ export enum TextureSamplerFilteringPreset {
8
+ /**
9
+ * Point filtering preset. This will configure the texture sampler to use point filtering for minification and magnification, and no mipmapping.
10
+ */
11
+ POINT,
12
+
13
+ /**
14
+ * Bilinear filtering preset. This will configure the texture sampler to use bilinear filtering for minification and magnification, and no mipmapping.
15
+ */
16
+ BILINEAR,
17
+
18
+ /**
19
+ * Trilinear filtering preset. This will configure the texture sampler to use bilinear filtering for minification and magnification, and linear mipmapping.
20
+ */
21
+ TRILINEAR
22
+ }
@@ -0,0 +1,32 @@
1
+ import { CullMode, FrontFace, PrimitiveTopology } from "./enums";
2
+
3
+ /**
4
+ * Describes the state of a primitive.
5
+ */
6
+ export class PrimitiveStateDescriptor {
7
+ /**
8
+ * The constructor.
9
+ */
10
+ public constructor() {
11
+ this.topology = PrimitiveTopology.TRIANGLE_LIST;
12
+ this.cullFace = CullMode.BACK;
13
+ this.frontFace = FrontFace.CCW;
14
+ }
15
+
16
+ /**
17
+ * The topology of the primitive.
18
+ * By default, it is set to <see cref="PrimitiveTopology.TRIANGLE_LIST"/>.
19
+ */
20
+ public topology: PrimitiveTopology;
21
+
22
+ /**
23
+ * The culling mode.
24
+ * By default, it is set to <see cref="CullMode.BACK"/>.
25
+ */
26
+ public cullFace: CullMode;
27
+ /**
28
+ * The front face.
29
+ * By default, it is set to <see cref="FrontFace.CCW"/>.
30
+ */
31
+ public frontFace: FrontFace;
32
+ }