ris-framework 0.2.0-dev.11 → 0.2.0-dev.13

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 (257) hide show
  1. package/CHANGELOG.md +21 -29
  2. package/README.md +99 -150
  3. package/dist/Framework.d.ts +26 -8
  4. package/dist/Framework.d.ts.map +1 -1
  5. package/dist/api/IFramework.d.ts +43 -0
  6. package/dist/api/IFramework.d.ts.map +1 -1
  7. package/dist/api/_export.d.ts +1 -1
  8. package/dist/api/_export.d.ts.map +1 -1
  9. package/dist/api/content/ContentManager.d.ts +5 -57
  10. package/dist/api/content/ContentManager.d.ts.map +1 -1
  11. package/dist/api/content/IContentManager.d.ts +24 -21
  12. package/dist/api/content/IContentManager.d.ts.map +1 -1
  13. package/dist/api/lights/LightsBuffer.d.ts +13 -6
  14. package/dist/api/lights/LightsBuffer.d.ts.map +1 -1
  15. package/dist/api/loaders/ObjGeometryMapper.d.ts +2 -2
  16. package/dist/api/loaders/ObjLoader.d.ts +61 -0
  17. package/dist/api/loaders/ObjLoader.d.ts.map +1 -0
  18. package/dist/api/loaders/_export.d.ts +1 -0
  19. package/dist/api/loaders/_export.d.ts.map +1 -1
  20. package/dist/api/loaders/obj/{ObjLoader.d.ts → ObjParser.d.ts} +20 -5
  21. package/dist/api/loaders/obj/ObjParser.d.ts.map +1 -0
  22. package/dist/api/loaders/obj/ObjTypes.d.ts +17 -1
  23. package/dist/api/loaders/obj/ObjTypes.d.ts.map +1 -1
  24. package/dist/api/loaders/obj/index.d.ts +1 -1
  25. package/dist/api/material/ForwardBlinnPhongMaterial.d.ts +22 -11
  26. package/dist/api/material/ForwardBlinnPhongMaterial.d.ts.map +1 -1
  27. package/dist/api/material/ICameraMaterial.d.ts +18 -0
  28. package/dist/api/material/ICameraMaterial.d.ts.map +1 -0
  29. package/dist/api/material/ILightsMaterial.d.ts +26 -0
  30. package/dist/api/material/ILightsMaterial.d.ts.map +1 -0
  31. package/dist/api/material/IMaterial.d.ts +2 -8
  32. package/dist/api/material/IMaterial.d.ts.map +1 -1
  33. package/dist/api/material/InspectTextureMipsMaterial.d.ts +2 -2
  34. package/dist/api/material/InspectTextureMipsMaterial.d.ts.map +1 -1
  35. package/dist/api/material/SpriteMaterial.d.ts +2 -2
  36. package/dist/api/material/SpriteMaterial.d.ts.map +1 -1
  37. package/dist/api/material/UnlitMaterial.d.ts +4 -3
  38. package/dist/api/material/UnlitMaterial.d.ts.map +1 -1
  39. package/dist/api/material/_export.d.ts +2 -0
  40. package/dist/api/material/_export.d.ts.map +1 -1
  41. package/dist/api/meshes/IMesh.d.ts +29 -34
  42. package/dist/api/meshes/IMesh.d.ts.map +1 -1
  43. package/dist/api/meshes/IMeshFactory.d.ts +13 -8
  44. package/dist/api/meshes/IMeshFactory.d.ts.map +1 -1
  45. package/dist/api/meshes/IRenderableMesh.d.ts +47 -0
  46. package/dist/api/meshes/IRenderableMesh.d.ts.map +1 -0
  47. package/dist/api/meshes/Mesh.d.ts +11 -57
  48. package/dist/api/meshes/Mesh.d.ts.map +1 -1
  49. package/dist/api/meshes/MeshFactory.d.ts +6 -3
  50. package/dist/api/meshes/MeshFactory.d.ts.map +1 -1
  51. package/dist/api/meshes/QuadMesh.d.ts +3 -5
  52. package/dist/api/meshes/QuadMesh.d.ts.map +1 -1
  53. package/dist/api/meshes/RenderableMesh.d.ts +95 -0
  54. package/dist/api/meshes/RenderableMesh.d.ts.map +1 -0
  55. package/dist/api/meshes/_export.d.ts +2 -1
  56. package/dist/api/meshes/_export.d.ts.map +1 -1
  57. package/dist/api/rendering/IGraphicsDeviceFeatures.d.ts +8 -2
  58. package/dist/api/rendering/IGraphicsDeviceFeatures.d.ts.map +1 -1
  59. package/dist/api/rendering/IRenderer.d.ts +28 -1
  60. package/dist/api/rendering/IRenderer.d.ts.map +1 -1
  61. package/dist/api/rendering/enums.d.ts +7 -0
  62. package/dist/api/rendering/enums.d.ts.map +1 -1
  63. package/dist/api/rendering/renderer-interface.d.ts +1 -31
  64. package/dist/api/rendering/renderer-interface.d.ts.map +1 -1
  65. package/dist/api/rendering/renderpass/RenderPassDescriptor.d.ts +2 -6
  66. package/dist/api/rendering/renderpass/RenderPassDescriptor.d.ts.map +1 -1
  67. package/dist/api/rendering/swapchain/ISwapChain.d.ts +1 -3
  68. package/dist/api/rendering/swapchain/ISwapChain.d.ts.map +1 -1
  69. package/dist/api/rendering/texture/TextureFormat.d.ts +88 -14
  70. package/dist/api/rendering/texture/TextureFormat.d.ts.map +1 -1
  71. package/dist/api/rendering/texture/TextureUtilities.d.ts +23 -0
  72. package/dist/api/rendering/texture/TextureUtilities.d.ts.map +1 -1
  73. package/dist/api/scene/Scene.d.ts +30 -21
  74. package/dist/api/scene/Scene.d.ts.map +1 -1
  75. package/dist/api/scene/SceneMesh.d.ts +27 -0
  76. package/dist/api/scene/SceneMesh.d.ts.map +1 -0
  77. package/dist/api/utilities/AlignUtilities.d.ts.map +1 -1
  78. package/dist/api/window/WindowManager.d.ts.map +1 -1
  79. package/dist/core/rendering/ARenderer.d.ts +5 -5
  80. package/dist/core/rendering/ARenderer.d.ts.map +1 -1
  81. package/dist/index.d.ts +0 -1
  82. package/dist/index.d.ts.map +1 -1
  83. package/dist/index.js +4006 -1355
  84. package/dist/webgl/WebGLGraphicsDeviceFeatures.d.ts +4 -0
  85. package/dist/webgl/WebGLGraphicsDeviceFeatures.d.ts.map +1 -1
  86. package/dist/webgl/WebGlRenderer.d.ts +1 -3
  87. package/dist/webgl/WebGlRenderer.d.ts.map +1 -1
  88. package/dist/webgl/lights/WebGlLightsBuffer.d.ts +8 -4
  89. package/dist/webgl/lights/WebGlLightsBuffer.d.ts.map +1 -1
  90. package/dist/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.d.ts +2 -0
  91. package/dist/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.d.ts.map +1 -1
  92. package/dist/webgl/swap-chain/WebGlSwapChain.d.ts +3 -2
  93. package/dist/webgl/swap-chain/WebGlSwapChain.d.ts.map +1 -1
  94. package/dist/webgl/texture/WebGlTextureFactory.d.ts +7 -0
  95. package/dist/webgl/texture/WebGlTextureFactory.d.ts.map +1 -1
  96. package/dist/webgl/utilities/WebGlConstants.d.ts +91 -0
  97. package/dist/webgl/utilities/WebGlConstants.d.ts.map +1 -0
  98. package/dist/webgl/utilities/WebGlConverter.d.ts +0 -7
  99. package/dist/webgl/utilities/WebGlConverter.d.ts.map +1 -1
  100. package/dist/webgl/utilities/WebGlErrorUtilities.d.ts +10 -0
  101. package/dist/webgl/utilities/WebGlErrorUtilities.d.ts.map +1 -0
  102. package/dist/webgl/utilities/WebGlTextureUtilities.d.ts.map +1 -1
  103. package/dist/webgl/utilities/WebGlUtilities.d.ts +3 -0
  104. package/dist/webgl/utilities/WebGlUtilities.d.ts.map +1 -1
  105. package/dist/webgpu/WebGpuGpuInfo.d.ts +28 -0
  106. package/dist/webgpu/WebGpuGpuInfo.d.ts.map +1 -0
  107. package/dist/webgpu/WebGpuGraphicsDevice.d.ts +80 -0
  108. package/dist/webgpu/WebGpuGraphicsDevice.d.ts.map +1 -0
  109. package/dist/webgpu/WebGpuGraphicsDeviceFeatures.d.ts +26 -0
  110. package/dist/webgpu/WebGpuGraphicsDeviceFeatures.d.ts.map +1 -0
  111. package/dist/webgpu/WebGpuRenderer.d.ts +26 -0
  112. package/dist/webgpu/WebGpuRenderer.d.ts.map +1 -0
  113. package/dist/webgpu/WebGpuTypes.d.ts +355 -0
  114. package/dist/webgpu/WebGpuTypes.d.ts.map +1 -0
  115. package/dist/webgpu/blending/WebGpuBlendState.d.ts +30 -0
  116. package/dist/webgpu/blending/WebGpuBlendState.d.ts.map +1 -0
  117. package/dist/webgpu/buffers/WebGpuBuffersFactory.d.ts +20 -0
  118. package/dist/webgpu/buffers/WebGpuBuffersFactory.d.ts.map +1 -0
  119. package/dist/webgpu/buffers/WebGpuIndexBuffer.d.ts +46 -0
  120. package/dist/webgpu/buffers/WebGpuIndexBuffer.d.ts.map +1 -0
  121. package/dist/webgpu/buffers/WebGpuUniformBuffer.d.ts +43 -0
  122. package/dist/webgpu/buffers/WebGpuUniformBuffer.d.ts.map +1 -0
  123. package/dist/webgpu/buffers/WebGpuVertexBuffer.d.ts +49 -0
  124. package/dist/webgpu/buffers/WebGpuVertexBuffer.d.ts.map +1 -0
  125. package/dist/webgpu/primitive/WebGpuPrimitiveState.d.ts +32 -0
  126. package/dist/webgpu/primitive/WebGpuPrimitiveState.d.ts.map +1 -0
  127. package/dist/webgpu/render-pass/WebGpuRenderPass.d.ts +28 -0
  128. package/dist/webgpu/render-pass/WebGpuRenderPass.d.ts.map +1 -0
  129. package/dist/webgpu/render-pipelines/AWebGpuRenderPipeline.d.ts +50 -0
  130. package/dist/webgpu/render-pipelines/AWebGpuRenderPipeline.d.ts.map +1 -0
  131. package/dist/webgpu/render-pipelines/WebGpuMainRenderTargetRenderPipeline.d.ts +34 -0
  132. package/dist/webgpu/render-pipelines/WebGpuMainRenderTargetRenderPipeline.d.ts.map +1 -0
  133. package/dist/webgpu/render-pipelines/WebGpuRenderPipelineFactory.d.ts +26 -0
  134. package/dist/webgpu/render-pipelines/WebGpuRenderPipelineFactory.d.ts.map +1 -0
  135. package/dist/webgpu/render-pipelines/material/AWebGpuMeshRenderPipeline.d.ts +72 -0
  136. package/dist/webgpu/render-pipelines/material/AWebGpuMeshRenderPipeline.d.ts.map +1 -0
  137. package/dist/webgpu/render-pipelines/material/WebGpuUnlitRenderPipeline.d.ts +40 -0
  138. package/dist/webgpu/render-pipelines/material/WebGpuUnlitRenderPipeline.d.ts.map +1 -0
  139. package/dist/webgpu/sampler/WebGpuSampler.d.ts +39 -0
  140. package/dist/webgpu/sampler/WebGpuSampler.d.ts.map +1 -0
  141. package/dist/webgpu/shader/WebGpuShaderModule.d.ts +29 -0
  142. package/dist/webgpu/shader/WebGpuShaderModule.d.ts.map +1 -0
  143. package/dist/webgpu/shader/WebGpuShaderModuleLoader.d.ts +21 -0
  144. package/dist/webgpu/shader/WebGpuShaderModuleLoader.d.ts.map +1 -0
  145. package/dist/webgpu/shader/WgslShaderContent.d.ts +9 -0
  146. package/dist/webgpu/shader/WgslShaderContent.d.ts.map +1 -0
  147. package/dist/webgpu/swap-chain/WebGpuSwapChain.d.ts +41 -0
  148. package/dist/webgpu/swap-chain/WebGpuSwapChain.d.ts.map +1 -0
  149. package/dist/webgpu/texture/WebGpuTexture2D.d.ts +47 -0
  150. package/dist/webgpu/texture/WebGpuTexture2D.d.ts.map +1 -0
  151. package/dist/webgpu/texture/WebGpuTextureFactory.d.ts +21 -0
  152. package/dist/webgpu/texture/WebGpuTextureFactory.d.ts.map +1 -0
  153. package/dist/webgpu/texture/WebGpuTextureView.d.ts +25 -0
  154. package/dist/webgpu/texture/WebGpuTextureView.d.ts.map +1 -0
  155. package/dist/webgpu/utilities/WebGpuConverter.d.ts +45 -0
  156. package/dist/webgpu/utilities/WebGpuConverter.d.ts.map +1 -0
  157. package/package.json +2 -2
  158. package/shaders/glsl/main_render_target_flip_y_fs.glsl +35 -1
  159. package/shaders/wgsl/forward_blinn_phong_material.wgsl +150 -0
  160. package/shaders/wgsl/inspect_texture_mips.wgsl +51 -0
  161. package/shaders/wgsl/main_frame_buffer.wgsl +35 -0
  162. package/shaders/wgsl/main_render_target_flip_y.wgsl +35 -0
  163. package/shaders/wgsl/sprite.wgsl +40 -0
  164. package/shaders/wgsl/unlit_material.wgsl +49 -0
  165. package/src/Framework.ts +121 -37
  166. package/src/api/IFramework.ts +55 -0
  167. package/src/api/_export.ts +1 -1
  168. package/src/api/content/ContentManager.ts +46 -180
  169. package/src/api/content/IContentManager.ts +25 -21
  170. package/src/api/lights/LightsBuffer.ts +13 -6
  171. package/src/api/loaders/ObjGeometryMapper.ts +2 -2
  172. package/src/api/loaders/ObjLoader.ts +126 -0
  173. package/src/api/loaders/_export.ts +1 -0
  174. package/src/api/loaders/obj/{ObjLoader.ts → ObjParser.ts} +35 -4
  175. package/src/api/loaders/obj/ObjTypes.ts +19 -1
  176. package/src/api/loaders/obj/index.ts +1 -1
  177. package/src/api/material/ForwardBlinnPhongMaterial.ts +48 -22
  178. package/src/api/material/ICameraMaterial.ts +22 -0
  179. package/src/api/material/ILightsMaterial.ts +32 -0
  180. package/src/api/material/IMaterial.ts +2 -9
  181. package/src/api/material/InspectTextureMipsMaterial.ts +2 -2
  182. package/src/api/material/SpriteMaterial.ts +2 -2
  183. package/src/api/material/UnlitMaterial.ts +4 -3
  184. package/src/api/material/_export.ts +2 -0
  185. package/src/api/meshes/IMesh.ts +32 -37
  186. package/src/api/meshes/IMeshFactory.ts +14 -8
  187. package/src/api/meshes/IRenderableMesh.ts +58 -0
  188. package/src/api/meshes/Mesh.ts +17 -132
  189. package/src/api/meshes/MeshFactory.ts +14 -5
  190. package/src/api/meshes/QuadMesh.ts +4 -6
  191. package/src/api/meshes/RenderableMesh.ts +207 -0
  192. package/src/api/meshes/_export.ts +6 -5
  193. package/src/api/rendering/IGraphicsDeviceFeatures.ts +28 -21
  194. package/src/api/rendering/IRenderer.ts +34 -1
  195. package/src/api/rendering/enums.ts +10 -0
  196. package/src/api/rendering/renderer-interface.ts +1 -36
  197. package/src/api/rendering/renderpass/RenderPassDescriptor.ts +10 -14
  198. package/src/api/rendering/swapchain/ISwapChain.ts +1 -3
  199. package/src/api/rendering/texture/TextureFormat.ts +128 -14
  200. package/src/api/rendering/texture/TextureUtilities.ts +225 -10
  201. package/src/api/scene/Scene.ts +102 -69
  202. package/src/api/scene/SceneMesh.ts +61 -0
  203. package/src/api/sprites/SpriteBatchMesh.ts +1 -1
  204. package/src/api/utilities/AlignUtilities.ts +2 -1
  205. package/src/api/window/WindowManager.ts +1 -1
  206. package/src/core/rendering/ARenderer.ts +24 -15
  207. package/src/index.ts +0 -1
  208. package/src/main.ts +2 -3
  209. package/src/webgl/WebGLGraphicsDeviceFeatures.ts +12 -2
  210. package/src/webgl/WebGlGpuInfo.ts +1 -1
  211. package/src/webgl/WebGlRenderer.ts +2 -5
  212. package/src/webgl/lights/WebGlLightsBuffer.ts +14 -4
  213. package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +18 -1
  214. package/src/webgl/swap-chain/WebGlSwapChain.ts +7 -6
  215. package/src/webgl/texture/WebGlTextureFactory.ts +49 -7
  216. package/src/webgl/utilities/WebGlConstants.ts +136 -0
  217. package/src/webgl/utilities/WebGlConverter.ts +91 -22
  218. package/src/webgl/utilities/WebGlErrorUtilities.ts +15 -0
  219. package/src/webgl/utilities/WebGlTextureUtilities.ts +5 -2
  220. package/src/webgl/utilities/WebGlUtilities.ts +4 -0
  221. package/src/webgpu/WebGpuGpuInfo.ts +61 -0
  222. package/src/webgpu/WebGpuGraphicsDevice.ts +199 -0
  223. package/src/webgpu/WebGpuGraphicsDeviceFeatures.ts +41 -0
  224. package/src/webgpu/WebGpuRenderer.ts +49 -0
  225. package/src/webgpu/WebGpuTypes.ts +288 -0
  226. package/src/webgpu/blending/WebGpuBlendState.ts +53 -0
  227. package/src/webgpu/buffers/WebGpuBuffersFactory.ts +46 -0
  228. package/src/webgpu/buffers/WebGpuIndexBuffer.ts +118 -0
  229. package/src/webgpu/buffers/WebGpuUniformBuffer.ts +102 -0
  230. package/src/webgpu/buffers/WebGpuVertexBuffer.ts +119 -0
  231. package/src/webgpu/primitive/WebGpuPrimitiveState.ts +52 -0
  232. package/src/webgpu/render-pass/WebGpuRenderPass.ts +131 -0
  233. package/src/webgpu/render-pipelines/AWebGpuRenderPipeline.ts +123 -0
  234. package/src/webgpu/render-pipelines/WebGpuMainRenderTargetRenderPipeline.ts +119 -0
  235. package/src/webgpu/render-pipelines/WebGpuRenderPipelineFactory.ts +66 -0
  236. package/src/webgpu/render-pipelines/material/AWebGpuMeshRenderPipeline.ts +249 -0
  237. package/src/webgpu/render-pipelines/material/WebGpuUnlitRenderPipeline.ts +101 -0
  238. package/src/webgpu/sampler/WebGpuSampler.ts +84 -0
  239. package/src/webgpu/shader/WebGpuShaderModule.ts +65 -0
  240. package/src/webgpu/shader/WebGpuShaderModuleLoader.ts +38 -0
  241. package/src/webgpu/shader/WgslShaderContent.ts +20 -0
  242. package/src/webgpu/swap-chain/WebGpuSwapChain.ts +93 -0
  243. package/src/webgpu/texture/WebGpuTexture2D.ts +197 -0
  244. package/src/webgpu/texture/WebGpuTextureFactory.ts +118 -0
  245. package/src/webgpu/texture/WebGpuTextureView.ts +43 -0
  246. package/src/webgpu/utilities/WebGpuConverter.ts +299 -0
  247. package/src/wgsl-raw.d.ts +4 -0
  248. package/dist/api/FrameworkConfiguration.d.ts +0 -23
  249. package/dist/api/FrameworkConfiguration.d.ts.map +0 -1
  250. package/dist/api/IFrameworkConfiguration.d.ts +0 -25
  251. package/dist/api/IFrameworkConfiguration.d.ts.map +0 -1
  252. package/dist/api/loaders/obj/ObjLoader.d.ts.map +0 -1
  253. package/dist/api/meshes/SubMesh.d.ts +0 -47
  254. package/dist/api/meshes/SubMesh.d.ts.map +0 -1
  255. package/src/api/FrameworkConfiguration.ts +0 -29
  256. package/src/api/IFrameworkConfiguration.ts +0 -30
  257. package/src/api/meshes/SubMesh.ts +0 -71
@@ -0,0 +1,119 @@
1
+ import {vec2} from "gl-matrix";
2
+ import {
3
+ BufferUsage,
4
+ GeometryFormat,
5
+ type IFramework,
6
+ type IMainRenderTargetRenderPipeline,
7
+ type ITexture2D,
8
+ } from "../../api/index.ts";
9
+ import {VertexBufferLayout} from "../../core/rendering/vertex-buffer-layout.ts";
10
+ import {AWebGpuRenderPipeline} from "./AWebGpuRenderPipeline.ts";
11
+ import type {WebGpuTexture2D} from "../texture/WebGpuTexture2D.ts";
12
+ import type {WebGpuVertexBuffer} from "../buffers/WebGpuVertexBuffer.ts";
13
+ import type {WebGpuIndexBuffer} from "../buffers/WebGpuIndexBuffer.ts";
14
+ import type {WebGpuShaderModule} from "../shader/WebGpuShaderModule.ts";
15
+ import {GpuShaderStage, type GpuBindGroup, type GpuBindGroupLayout} from "../WebGpuTypes.ts";
16
+
17
+ /**
18
+ * Draws the main render target to the swap chain with `main_frame_buffer.wgsl`.
19
+ * No Y flip: in WebGPU texture (0,0) is the top-left, which matches the quad's UVs (same as the C# backend).
20
+ * @internal
21
+ */
22
+ export class WebGpuMainRenderTargetRenderPipeline extends AWebGpuRenderPipeline implements IMainRenderTargetRenderPipeline {
23
+
24
+ private _vertexBuffer: WebGpuVertexBuffer = null!;
25
+ private _indexBuffer: WebGpuIndexBuffer = null!;
26
+ private _mainRenderTarget: WebGpuTexture2D;
27
+ private _bindGroupLayout: GpuBindGroupLayout = null!;
28
+ private _bindGroup: GpuBindGroup | null = null;
29
+
30
+ /**
31
+ * The constructor.
32
+ * @param framework The framework.
33
+ * @param mainRenderTarget The main render target.
34
+ */
35
+ public constructor(framework: IFramework, mainRenderTarget: ITexture2D) {
36
+ super(framework);
37
+ this._mainRenderTarget = mainRenderTarget as WebGpuTexture2D;
38
+ }
39
+
40
+ /** @inheritDoc */
41
+ public readonly geometryFormat = GeometryFormat.POS3_TEXTURECOORDS2;
42
+
43
+ /** @inheritDoc */
44
+ public get mainRenderTarget(): ITexture2D {
45
+ return this._mainRenderTarget;
46
+ }
47
+
48
+ /** Sets the main render target and rebuilds the bind group. */
49
+ public set mainRenderTarget(value: ITexture2D) {
50
+ this._mainRenderTarget = value as WebGpuTexture2D;
51
+ this._bindGroup = null;
52
+ }
53
+
54
+ /** @inheritDoc */
55
+ public initialize(): void {
56
+ const module = this._framework.content.loadShaderModule("main_frame_buffer") as WebGpuShaderModule;
57
+
58
+ const geometry = this._framework.geometryBuilder.quadGeometry(vec2.fromValues(2, 2));
59
+ const data = geometry.toInterleaved(GeometryFormat.POS3_TEXTURECOORDS2);
60
+ this._vertexBuffer = this._framework.bufferFactory.createVertexBuffer(
61
+ data, (3 + 2) * Float32Array.BYTES_PER_ELEMENT, BufferUsage.VERTEX,
62
+ "Main Frame Buffer Render Pipeline Vertex Buffer") as WebGpuVertexBuffer;
63
+ this._indexBuffer = this._framework.bufferFactory.createIndexBuffer(
64
+ geometry.indices!, "Main Frame Buffer Render Pipeline Index Buffer") as WebGpuIndexBuffer;
65
+
66
+ this.vertexBufferLayouts = [VertexBufferLayout.createFloat3Float2Layout()];
67
+
68
+ this._bindGroupLayout = this._graphicsDevice.device.createBindGroupLayout({
69
+ label: "Main Frame Buffer bind group layout",
70
+ entries: [
71
+ {binding: 0, visibility: GpuShaderStage.FRAGMENT, texture: {sampleType: "float"}},
72
+ {binding: 1, visibility: GpuShaderStage.FRAGMENT, sampler: {type: "filtering"}},
73
+ ],
74
+ });
75
+
76
+ // Drawn in the swap chain pass, which has no depth attachment.
77
+ this._createGpuPipeline(
78
+ "Main Frame Buffer Render Pipeline",
79
+ module.gpuShaderModule,
80
+ [this._bindGroupLayout],
81
+ this._graphicsDevice.swapChain?.textureFormat ?? this._framework.renderer.preferredTextureFormat);
82
+ }
83
+
84
+ private _getBindGroup(): GpuBindGroup {
85
+ if (!this._bindGroup) {
86
+ this._bindGroup = this._graphicsDevice.device.createBindGroup({
87
+ label: "Main Frame Buffer bind group",
88
+ layout: this._bindGroupLayout,
89
+ entries: [
90
+ {binding: 0, resource: this._mainRenderTarget.gpuDefaultView},
91
+ {binding: 1, resource: this._defaultTextureSampler.gpuSampler},
92
+ ],
93
+ });
94
+ }
95
+ return this._bindGroup;
96
+ }
97
+
98
+ /** @inheritDoc */
99
+ public render(): void {
100
+ if (!this._gpuPipeline) {
101
+ return;
102
+ }
103
+ const encoder = this._graphicsDevice.renderPassEncoder;
104
+ encoder.setPipeline(this._gpuPipeline);
105
+ encoder.setVertexBuffer(0, this._vertexBuffer.gpuBuffer, 0, this._vertexBuffer.byteSize);
106
+ encoder.setIndexBuffer(this._indexBuffer.gpuBuffer, this._indexBuffer.gpuIndexFormat, 0, this._indexBuffer.paddedByteSize);
107
+ encoder.setBindGroup(0, this._getBindGroup());
108
+ encoder.drawIndexed(this._indexBuffer.indicesCount, 1, 0);
109
+ }
110
+
111
+ /** @inheritDoc */
112
+ public override dispose(): void {
113
+ // ARenderer recreates this pipeline on every resize, so release the quad buffers too.
114
+ this._vertexBuffer?.dispose();
115
+ this._indexBuffer?.dispose();
116
+ this._bindGroup = null;
117
+ super.dispose();
118
+ }
119
+ }
@@ -0,0 +1,66 @@
1
+ import type {
2
+ ForwardBlinnPhongRenderPipelineDescriptor,
3
+ IForwardBlinnPhongRenderPipeline,
4
+ IFramework,
5
+ IInspectTextureMipsRenderPipeline,
6
+ IMainRenderTargetRenderPipeline,
7
+ IPrimitiveState,
8
+ IRenderPipelineFactory,
9
+ ISpriteRenderPipeline,
10
+ ITexture2D,
11
+ IUniformBuffer,
12
+ IUnlitRenderPipeline,
13
+ } from "../../api/index.ts";
14
+ import {WebGpuMainRenderTargetRenderPipeline} from "./WebGpuMainRenderTargetRenderPipeline.ts";
15
+ import {WebGpuUnlitRenderPipeline} from "./material/WebGpuUnlitRenderPipeline.ts";
16
+
17
+ /**
18
+ * The WebGPU render pipeline factory. Slice 1 implements the main render target and unlit pipelines.
19
+ * @internal
20
+ */
21
+ export class WebGpuRenderPipelineFactory implements IRenderPipelineFactory {
22
+
23
+ /**
24
+ * The constructor.
25
+ * @param _framework The framework.
26
+ */
27
+ public constructor(private readonly _framework: IFramework) {
28
+ }
29
+
30
+ /** @inheritDoc */
31
+ public createMainFrameBufferPipeline(mainFrameBuffer: ITexture2D): IMainRenderTargetRenderPipeline {
32
+ const pipeline = new WebGpuMainRenderTargetRenderPipeline(this._framework, mainFrameBuffer);
33
+ pipeline.initialize();
34
+ return pipeline;
35
+ }
36
+
37
+ /** Same as {@link createMainFrameBufferPipeline}, mirroring the WebGL factory. */
38
+ public createMainRenderTargetRenderPipeline(renderTarget: ITexture2D): IMainRenderTargetRenderPipeline {
39
+ return this.createMainFrameBufferPipeline(renderTarget);
40
+ }
41
+
42
+ /** @inheritDoc */
43
+ public createUnlitRenderPipeline(projectionViewBuffer: IUniformBuffer,
44
+ modelBuffer: IUniformBuffer,
45
+ materialBuffer: IUniformBuffer,
46
+ primitiveState?: IPrimitiveState): IUnlitRenderPipeline {
47
+ const pipeline = new WebGpuUnlitRenderPipeline(this._framework, projectionViewBuffer, modelBuffer, materialBuffer, primitiveState);
48
+ pipeline.initialize();
49
+ return pipeline;
50
+ }
51
+
52
+ /** @inheritDoc */
53
+ public createSpriteRenderPipeline(_projectionViewBuffer: IUniformBuffer): ISpriteRenderPipeline {
54
+ throw new Error("The sprite render pipeline is not implemented for WebGPU.");
55
+ }
56
+
57
+ /** @inheritDoc */
58
+ public createInspectTextureMipsRenderPipeline(_projectionViewBuffer: IUniformBuffer, _modelBuffer: IUniformBuffer, _textureConstantsBuffer: IUniformBuffer): IInspectTextureMipsRenderPipeline {
59
+ throw new Error("The inspect texture mips render pipeline is not implemented for WebGPU.");
60
+ }
61
+
62
+ /** @inheritDoc */
63
+ public createForwardBlinnPhongRenderPipeline(_descriptor: ForwardBlinnPhongRenderPipelineDescriptor): IForwardBlinnPhongRenderPipeline {
64
+ throw new Error("The forward Blinn-Phong render pipeline is not implemented for WebGPU.");
65
+ }
66
+ }
@@ -0,0 +1,249 @@
1
+ import {
2
+ type IFramework,
3
+ type IIndexBuffer,
4
+ type IPrimitiveState,
5
+ type ISampler,
6
+ type ITexture2D,
7
+ type IUniformBuffer,
8
+ type IVertexBuffer,
9
+ State,
10
+ } from "../../../api/index.ts";
11
+ import {AWebGpuRenderPipeline} from "../AWebGpuRenderPipeline.ts";
12
+ import {WebGpuTexture2D} from "../../texture/WebGpuTexture2D.ts";
13
+ import type {WebGpuSampler} from "../../sampler/WebGpuSampler.ts";
14
+ import type {WebGpuUniformBuffer} from "../../buffers/WebGpuUniformBuffer.ts";
15
+ import type {WebGpuVertexBuffer} from "../../buffers/WebGpuVertexBuffer.ts";
16
+ import type {WebGpuIndexBuffer} from "../../buffers/WebGpuIndexBuffer.ts";
17
+ import type {WebGpuShaderModule} from "../../shader/WebGpuShaderModule.ts";
18
+ import {
19
+ GpuShaderStage,
20
+ type GpuBindGroup,
21
+ type GpuBindGroupLayout,
22
+ type GpuBuffer,
23
+ } from "../../WebGpuTypes.ts";
24
+
25
+ /**
26
+ * One uniform buffer binding of a uniform bind group.
27
+ * @internal
28
+ */
29
+ export interface IWebGpuUniformBinding {
30
+ /** The WGSL `@binding`. */
31
+ binding: number;
32
+ /** The shader stages that read it. */
33
+ visibility: number;
34
+ /** Returns the current buffer. */
35
+ buffer: () => IUniformBuffer;
36
+ }
37
+
38
+ /**
39
+ * A uniform-buffer-only bind group. The bind group is rebuilt when any of its buffers change (dirty check).
40
+ * @internal
41
+ */
42
+ export interface IWebGpuUniformGroup {
43
+ /** The WGSL `@group`. */
44
+ group: number;
45
+ /** The bindings. */
46
+ bindings: IWebGpuUniformBinding[];
47
+ }
48
+
49
+ interface UniformGroupState {
50
+ descriptor: IWebGpuUniformGroup;
51
+ layout: GpuBindGroupLayout;
52
+ bindGroup: GpuBindGroup | null;
53
+ lastBuffers: GpuBuffer[];
54
+ }
55
+
56
+ /**
57
+ * The base of WebGPU mesh pipelines, like the C# `AWebGPUMeshRenderPipeline`:
58
+ * uniform groups with dirty checks, plus one texture + sampler group cached per texture and sampler id.
59
+ * A 1x1 white texture is used when no texture is set.
60
+ * @internal
61
+ */
62
+ export abstract class AWebGpuMeshRenderPipeline extends AWebGpuRenderPipeline {
63
+
64
+ private _uniformGroups: UniformGroupState[] = [];
65
+ private _textureGroupIndex = 1;
66
+ private _textureGroupLayout: GpuBindGroupLayout = null!;
67
+ private readonly _textureBindGroups = new Map<string, GpuBindGroup>();
68
+ private readonly _watchedTextures = new Set<ITexture2D>();
69
+ private readonly _onTextureDisposed = (texture: ITexture2D) => this._forgetTexture(texture);
70
+
71
+ protected _texture: WebGpuTexture2D | null = null;
72
+ protected _sampler?: WebGpuSampler;
73
+
74
+ /**
75
+ * The constructor.
76
+ * @param framework The framework.
77
+ * @param primitiveState The optional primitive state.
78
+ */
79
+ protected constructor(framework: IFramework, primitiveState?: IPrimitiveState) {
80
+ super(framework, primitiveState);
81
+ }
82
+
83
+ /** The shader id to load. */
84
+ protected abstract readonly _shaderId: string;
85
+
86
+ /** The uniform groups. */
87
+ protected abstract _describeUniformGroups(): IWebGpuUniformGroup[];
88
+
89
+ /** The group index used for texture + sampler. */
90
+ protected get _textureGroup(): number {
91
+ return 1;
92
+ }
93
+
94
+ /** @inheritDoc */
95
+ public get diffuseTexture(): ITexture2D {
96
+ return this._texture ?? WebGpuTexture2D.getOrCreateDefault(this._framework);
97
+ }
98
+
99
+ /** @inheritDoc */
100
+ public set diffuseTexture(value: ITexture2D) {
101
+ this._texture = (value as WebGpuTexture2D) ?? null;
102
+ }
103
+
104
+ /** @inheritDoc */
105
+ public get diffuseTextureSampler(): ISampler | undefined {
106
+ return this._sampler;
107
+ }
108
+
109
+ /** @inheritDoc */
110
+ public set diffuseTextureSampler(value: ISampler | undefined) {
111
+ this._sampler = value as WebGpuSampler | undefined;
112
+ }
113
+
114
+ /** @inheritDoc */
115
+ public initialize(): void {
116
+ const device = this._graphicsDevice.device;
117
+ const module = this._framework.content.loadShaderModule(this._shaderId) as WebGpuShaderModule;
118
+
119
+ this._textureGroupIndex = this._textureGroup;
120
+ this._textureGroupLayout = device.createBindGroupLayout({
121
+ label: `${this._shaderId} texture group layout`,
122
+ entries: [
123
+ {binding: 0, visibility: GpuShaderStage.FRAGMENT, texture: {sampleType: "float"}},
124
+ {binding: 1, visibility: GpuShaderStage.FRAGMENT, sampler: {type: "filtering"}},
125
+ ],
126
+ });
127
+
128
+ this._uniformGroups = this._describeUniformGroups().map(descriptor => ({
129
+ descriptor,
130
+ layout: device.createBindGroupLayout({
131
+ label: `${this._shaderId} group ${descriptor.group} layout`,
132
+ entries: descriptor.bindings.map(b => ({binding: b.binding, visibility: b.visibility, buffer: {type: "uniform"}})),
133
+ }),
134
+ bindGroup: null,
135
+ lastBuffers: [],
136
+ }));
137
+
138
+ const groupCount = Math.max(this._textureGroupIndex, ...this._uniformGroups.map(g => g.descriptor.group)) + 1;
139
+ const layouts: GpuBindGroupLayout[] = [];
140
+ for (let i = 0; i < groupCount; i++) {
141
+ const layout = i === this._textureGroupIndex
142
+ ? this._textureGroupLayout
143
+ : this._uniformGroups.find(g => g.descriptor.group === i)?.layout;
144
+ if (!layout) {
145
+ throw new Error(`${this._shaderId}: bind group ${i} has no layout.`);
146
+ }
147
+ layouts.push(layout);
148
+ }
149
+
150
+ const renderer = this._framework.renderer;
151
+ this._createGpuPipeline(
152
+ `${this._shaderId} pipeline`,
153
+ module.gpuShaderModule,
154
+ layouts,
155
+ renderer.preferredTextureFormat,
156
+ renderer.preferredDepthStencilFormat);
157
+ }
158
+
159
+ private _getUniformBindGroup(state: UniformGroupState): GpuBindGroup {
160
+ const buffers = state.descriptor.bindings.map(b => (b.buffer() as WebGpuUniformBuffer).gpuBuffer);
161
+ const dirty = !state.bindGroup
162
+ || buffers.length !== state.lastBuffers.length
163
+ || buffers.some((buffer, i) => buffer !== state.lastBuffers[i]);
164
+ if (dirty) {
165
+ state.bindGroup = this._graphicsDevice.device.createBindGroup({
166
+ label: `${this._shaderId} group ${state.descriptor.group}`,
167
+ layout: state.layout,
168
+ entries: state.descriptor.bindings.map((b, i) => ({
169
+ binding: b.binding,
170
+ resource: {buffer: buffers[i], offset: 0, size: buffers[i].size},
171
+ })),
172
+ });
173
+ state.lastBuffers = buffers;
174
+ }
175
+ return state.bindGroup!;
176
+ }
177
+
178
+ private _getTextureBindGroup(): GpuBindGroup {
179
+ const texture = (this._texture && this._texture.textureState !== State.DISPOSED)
180
+ ? this._texture
181
+ : WebGpuTexture2D.getOrCreateDefault(this._framework);
182
+ const sampler = this._sampler ?? this._defaultTextureSampler;
183
+ const key = `${texture.id}:${sampler.id}`;
184
+ let bindGroup = this._textureBindGroups.get(key);
185
+ if (!bindGroup) {
186
+ bindGroup = this._graphicsDevice.device.createBindGroup({
187
+ label: `${this._shaderId} texture group ${key}`,
188
+ layout: this._textureGroupLayout,
189
+ entries: [
190
+ {binding: 0, resource: texture.gpuDefaultView},
191
+ {binding: 1, resource: sampler.gpuSampler},
192
+ ],
193
+ });
194
+ this._textureBindGroups.set(key, bindGroup);
195
+ if (!this._watchedTextures.has(texture)) {
196
+ this._watchedTextures.add(texture);
197
+ texture.addOnDisposedListener(this._onTextureDisposed);
198
+ }
199
+ }
200
+ return bindGroup;
201
+ }
202
+
203
+ private _forgetTexture(texture: ITexture2D): void {
204
+ const prefix = `${texture.id}:`;
205
+ for (const key of [...this._textureBindGroups.keys()]) {
206
+ if (key.startsWith(prefix)) {
207
+ this._textureBindGroups.delete(key);
208
+ }
209
+ }
210
+ this._watchedTextures.delete(texture);
211
+ }
212
+
213
+ /** @inheritDoc */
214
+ public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount: number = -1, indicesOffset: number = 0): void {
215
+ if (indicesCount == 0 || !this._gpuPipeline) {
216
+ return;
217
+ }
218
+ const vb = vertexBuffer as WebGpuVertexBuffer;
219
+ const ib = indexBuffer as WebGpuIndexBuffer;
220
+ const encoder = this._graphicsDevice.renderPassEncoder;
221
+
222
+ encoder.setPipeline(this._gpuPipeline);
223
+ encoder.setVertexBuffer(0, vb.gpuBuffer, 0, vb.byteSize);
224
+ // The index buffer was allocated padded to 4 bytes; bind the padded size.
225
+ encoder.setIndexBuffer(ib.gpuBuffer, ib.gpuIndexFormat, 0, ib.paddedByteSize);
226
+
227
+ for (const state of this._uniformGroups) {
228
+ encoder.setBindGroup(state.descriptor.group, this._getUniformBindGroup(state));
229
+ }
230
+ encoder.setBindGroup(this._textureGroupIndex, this._getTextureBindGroup());
231
+
232
+ // Range draws: firstIndex is the index offset, count defaults to the rest of the buffer.
233
+ const count = indicesCount < 0 ? ib.indicesCount - indicesOffset : indicesCount;
234
+ encoder.drawIndexed(count, 1, indicesOffset);
235
+ }
236
+
237
+ /** @inheritDoc */
238
+ public override dispose(): void {
239
+ for (const texture of this._watchedTextures) {
240
+ texture.removeOnDisposedListener(this._onTextureDisposed);
241
+ }
242
+ this._watchedTextures.clear();
243
+ this._textureBindGroups.clear();
244
+ for (const state of this._uniformGroups) {
245
+ state.bindGroup = null;
246
+ }
247
+ super.dispose();
248
+ }
249
+ }
@@ -0,0 +1,101 @@
1
+ import {
2
+ GeometryFormat,
3
+ type IFramework,
4
+ type IPrimitiveState,
5
+ type IUniformBuffer,
6
+ type IUnlitRenderPipeline,
7
+ } from "../../../api/index.ts";
8
+ import {VertexBufferLayout} from "../../../core/rendering/vertex-buffer-layout.ts";
9
+ import {AWebGpuMeshRenderPipeline, type IWebGpuUniformGroup} from "./AWebGpuMeshRenderPipeline.ts";
10
+ import {GpuShaderStage} from "../../WebGpuTypes.ts";
11
+
12
+ /**
13
+ * The WebGPU unlit pipeline (`unlit_material.wgsl`).
14
+ * Group 0: projection-view (0), model (1). Group 1: texture (0), sampler (1). Group 2: material (2).
15
+ * @internal
16
+ */
17
+ export class WebGpuUnlitRenderPipeline extends AWebGpuMeshRenderPipeline implements IUnlitRenderPipeline {
18
+
19
+ private _projectionViewBuffer: IUniformBuffer;
20
+ private _modelBuffer: IUniformBuffer;
21
+ private _materialBuffer: IUniformBuffer;
22
+
23
+ /**
24
+ * The constructor.
25
+ * @param framework The framework.
26
+ * @param projectionViewBuffer The projection-view buffer.
27
+ * @param modelBuffer The model buffer.
28
+ * @param materialBuffer The material buffer.
29
+ * @param primitiveState The optional primitive state.
30
+ */
31
+ public constructor(framework: IFramework,
32
+ projectionViewBuffer: IUniformBuffer,
33
+ modelBuffer: IUniformBuffer,
34
+ materialBuffer: IUniformBuffer,
35
+ primitiveState?: IPrimitiveState) {
36
+ if (!modelBuffer) {
37
+ throw new Error("WebGpuUnlitRenderPipeline: modelBuffer is required (the mesh's model buffer, mat4).");
38
+ }
39
+ super(framework, primitiveState);
40
+ this._projectionViewBuffer = projectionViewBuffer;
41
+ this._modelBuffer = modelBuffer;
42
+ this._materialBuffer = materialBuffer;
43
+ this.vertexBufferLayouts = [VertexBufferLayout.createFloat3Float4Float2Layout()];
44
+ }
45
+
46
+ /** @inheritDoc */
47
+ public readonly geometryFormat = GeometryFormat.POS3_COLOR4_TEXTURECOORDS2;
48
+
49
+ /** @inheritDoc */
50
+ protected readonly _shaderId = "unlit_material";
51
+
52
+ /** @inheritDoc */
53
+ protected _describeUniformGroups(): IWebGpuUniformGroup[] {
54
+ return [
55
+ {
56
+ group: 0, bindings: [
57
+ {binding: 0, visibility: GpuShaderStage.VERTEX, buffer: () => this._projectionViewBuffer},
58
+ {binding: 1, visibility: GpuShaderStage.VERTEX, buffer: () => this._modelBuffer},
59
+ ],
60
+ },
61
+ {
62
+ group: 2, bindings: [
63
+ {binding: 2, visibility: GpuShaderStage.FRAGMENT, buffer: () => this._materialBuffer},
64
+ ],
65
+ },
66
+ ];
67
+ }
68
+
69
+ /** @inheritDoc */
70
+ public get projectionViewBuffer(): IUniformBuffer {
71
+ return this._projectionViewBuffer;
72
+ }
73
+
74
+ /** @inheritDoc */
75
+ public set projectionViewBuffer(value: IUniformBuffer) {
76
+ this._projectionViewBuffer = value;
77
+ }
78
+
79
+ /** @inheritDoc */
80
+ public get modelBuffer(): IUniformBuffer {
81
+ return this._modelBuffer;
82
+ }
83
+
84
+ /** @inheritDoc */
85
+ public set modelBuffer(value: IUniformBuffer) {
86
+ if (!value) {
87
+ throw new Error("WebGpuUnlitRenderPipeline: modelBuffer is required (the mesh's model buffer, mat4).");
88
+ }
89
+ this._modelBuffer = value;
90
+ }
91
+
92
+ /** @inheritDoc */
93
+ public get materialBuffer(): IUniformBuffer {
94
+ return this._materialBuffer;
95
+ }
96
+
97
+ /** @inheritDoc */
98
+ public set materialBuffer(value: IUniformBuffer) {
99
+ this._materialBuffer = value;
100
+ }
101
+ }
@@ -0,0 +1,84 @@
1
+ import {SamplerDescriptor} from "../../core/rendering/sampler/sampler-descriptor.ts";
2
+ import {type ISampler, MipMapSamplerFilter} from "../../api/index.ts";
3
+ import type {WebGpuGraphicsDevice} from "../WebGpuGraphicsDevice.ts";
4
+ import type {GpuSampler} from "../WebGpuTypes.ts";
5
+ import {WebGpuConverter} from "../utilities/WebGpuConverter.ts";
6
+
7
+ /**
8
+ * The WebGPU sampler.
9
+ * @internal
10
+ */
11
+ export class WebGpuSampler implements ISampler {
12
+
13
+ private static _nextId = 0;
14
+
15
+ private _gpuSampler: GpuSampler | null;
16
+
17
+ /**
18
+ * The constructor.
19
+ * @param graphicsDevice The graphics device.
20
+ * @param descriptor The sampler descriptor.
21
+ */
22
+ public constructor(graphicsDevice: WebGpuGraphicsDevice, descriptor?: SamplerDescriptor) {
23
+ descriptor = descriptor ?? new SamplerDescriptor();
24
+ this.id = WebGpuSampler._nextId++;
25
+ this.minFilter = descriptor.minFilter;
26
+ this.magFilter = descriptor.magFilter;
27
+ this.mipmapFilter = descriptor.mipMapFilter;
28
+ this.addressModeU = descriptor.addressModeU;
29
+ this.addressModeV = descriptor.addressModeV;
30
+ this.addressModeW = descriptor.addressModeW;
31
+
32
+ this._gpuSampler = graphicsDevice.device.createSampler({
33
+ label: `WebGpuSampler ${this.id}`,
34
+ minFilter: WebGpuConverter.convertFilter(this.minFilter),
35
+ magFilter: WebGpuConverter.convertFilter(this.magFilter),
36
+ mipmapFilter: WebGpuConverter.convertMipMapFilter(this.mipmapFilter),
37
+ addressModeU: WebGpuConverter.convertAddressMode(this.addressModeU),
38
+ addressModeV: WebGpuConverter.convertAddressMode(this.addressModeV),
39
+ addressModeW: WebGpuConverter.convertAddressMode(this.addressModeW),
40
+ // Without mip filtering only the base level is sampled.
41
+ lodMaxClamp: this.mipmapFilter === MipMapSamplerFilter.NONE ? 0 : 32,
42
+ });
43
+ }
44
+
45
+ /** @inheritDoc */
46
+ public readonly id: number;
47
+
48
+ /** @inheritDoc */
49
+ public readonly minFilter;
50
+
51
+ /** @inheritDoc */
52
+ public readonly magFilter;
53
+
54
+ /** @inheritDoc */
55
+ public readonly mipmapFilter;
56
+
57
+ /** @inheritDoc */
58
+ public readonly addressModeU;
59
+
60
+ /** @inheritDoc */
61
+ public readonly addressModeV;
62
+
63
+ /** @inheritDoc */
64
+ public readonly addressModeW;
65
+
66
+ /** The WebGPU sampler. */
67
+ public get gpuSampler(): GpuSampler {
68
+ if (!this._gpuSampler) {
69
+ throw new Error("WebGpuSampler: the sampler is disposed.");
70
+ }
71
+ return this._gpuSampler;
72
+ }
73
+
74
+ /** @inheritDoc */
75
+ public get handle(): any {
76
+ return this._gpuSampler;
77
+ }
78
+
79
+ /** @inheritDoc */
80
+ public dispose(): void {
81
+ // WebGPU samplers have no destroy(); dropping the reference lets the browser collect it.
82
+ this._gpuSampler = null;
83
+ }
84
+ }
@@ -0,0 +1,65 @@
1
+ import {type IFramework, type IShaderModule, ShaderStage} from "../../api/index.ts";
2
+ import type {WebGpuGraphicsDevice} from "../WebGpuGraphicsDevice.ts";
3
+ import type {GpuShaderModule} from "../WebGpuTypes.ts";
4
+ import {WgslShaderContent} from "./WgslShaderContent.ts";
5
+
6
+ /**
7
+ * A WGSL shader module. One module holds both stages (`main_vs` and `main_fs`).
8
+ * @internal
9
+ */
10
+ export class WebGpuShaderModule implements IShaderModule {
11
+
12
+ private _gpuShaderModule: GpuShaderModule | null;
13
+
14
+ /**
15
+ * The constructor.
16
+ * @param framework The framework.
17
+ * @param id The built-in shader id, for example `unlit_material`.
18
+ * @param keyValueDictionary Optional `##KEY##` replacements, like the WebGL loader.
19
+ */
20
+ public constructor(framework: IFramework, id: string, keyValueDictionary?: { [id: string]: number }) {
21
+ let code = WgslShaderContent[id];
22
+ if (code === undefined) {
23
+ throw new Error(`WGSL shader '${id}' is not implemented for WebGPU.`);
24
+ }
25
+ if (keyValueDictionary) {
26
+ for (const key in keyValueDictionary) {
27
+ code = code.split(`##${key}##`).join(String(keyValueDictionary[key]));
28
+ }
29
+ }
30
+ const device = (framework.renderer.graphicsDevice as WebGpuGraphicsDevice).device;
31
+ this._gpuShaderModule = device.createShaderModule({label: id, code});
32
+ this.label = id;
33
+ this._gpuShaderModule.getCompilationInfo?.().then(info => {
34
+ for (const message of info.messages) {
35
+ if (message.type === "error") {
36
+ console.error(`WGSL ${id}:${message.lineNum}:${message.linePos} ${message.message}`);
37
+ }
38
+ }
39
+ }).catch(() => { /* ignore */ });
40
+ }
41
+
42
+ /** The shader id. */
43
+ public readonly label: string;
44
+
45
+ /** @inheritDoc */
46
+ public readonly stages: ShaderStage[] = [ShaderStage.VERTEX, ShaderStage.FRAGMENT];
47
+
48
+ /** The WebGPU shader module. */
49
+ public get gpuShaderModule(): GpuShaderModule {
50
+ if (!this._gpuShaderModule) {
51
+ throw new Error("WebGpuShaderModule: the module is disposed.");
52
+ }
53
+ return this._gpuShaderModule;
54
+ }
55
+
56
+ /** @inheritDoc */
57
+ public get nativeHandle(): any {
58
+ return this._gpuShaderModule;
59
+ }
60
+
61
+ /** @inheritDoc */
62
+ public dispose(): void {
63
+ this._gpuShaderModule = null;
64
+ }
65
+ }