rhodonite 0.20.2 → 0.22.0

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 (89) hide show
  1. package/README.md +10 -2
  2. package/VERSION-FILE +1 -1
  3. package/dist/esm/foundation/components/Camera/CameraComponent.d.ts +15 -0
  4. package/dist/esm/foundation/components/Light/LightComponent.d.ts +109 -4
  5. package/dist/esm/foundation/components/Physics/PhysicsComponent.d.ts +1 -1
  6. package/dist/esm/foundation/core/Component.d.ts +1 -8
  7. package/dist/esm/foundation/core/Config.d.ts +165 -6
  8. package/dist/esm/foundation/core/GlobalDataRepository.d.ts +12 -8
  9. package/dist/esm/foundation/core/MemoryManager.d.ts +12 -0
  10. package/dist/esm/foundation/definitions/ProcessApproach.d.ts +1 -6
  11. package/dist/esm/foundation/definitions/ShaderSemantics.d.ts +0 -1
  12. package/dist/esm/foundation/helpers/Shadow/PointShadowMap.d.ts +3 -1
  13. package/dist/esm/foundation/helpers/Shadow/ShadowSystem.d.ts +13 -0
  14. package/dist/esm/foundation/materials/contents/CustomMaterialContent.d.ts +1 -1
  15. package/dist/esm/foundation/materials/core/AbstractMaterialContent.d.ts +1 -23
  16. package/dist/esm/foundation/materials/core/Material.d.ts +10 -14
  17. package/dist/esm/foundation/materials/core/MaterialRepository.d.ts +5 -0
  18. package/dist/esm/foundation/physics/PhysicsProperty.d.ts +9 -2
  19. package/dist/esm/foundation/physics/index.d.ts +0 -1
  20. package/dist/esm/foundation/renderer/index.d.ts +1 -0
  21. package/dist/esm/foundation/renderer/lightCluster/LightClusterBuilder.d.ts +97 -0
  22. package/dist/esm/foundation/renderer/lightCluster/LightClusterLayout.d.ts +98 -0
  23. package/dist/esm/foundation/renderer/lightCluster/LightClusterSystem.d.ts +171 -0
  24. package/dist/esm/foundation/renderer/lightCluster/index.d.ts +1 -0
  25. package/dist/esm/foundation/system/Engine.d.ts +9 -2
  26. package/dist/esm/foundation/system/EngineState.d.ts +1 -1
  27. package/dist/esm/foundation/textures/TextureArray.d.ts +1 -1
  28. package/dist/esm/index.js +1181 -824
  29. package/dist/esm/webgl/MultiDraw.d.ts +1 -1
  30. package/dist/esm/webgl/WebGLResourceRepository.d.ts +1 -2
  31. package/dist/{esmdev/webgl/WebGLStrategyDataTexture.d.ts → esm/webgl/WebGLStrategyWebGL2.d.ts} +20 -4
  32. package/dist/esm/webgl/debug/BufferLayoutDecoder.d.ts +83 -0
  33. package/dist/esm/webgl/debug/ShaderParameterDebugger.d.ts +47 -0
  34. package/dist/esm/webgl/debug/index.d.ts +3 -0
  35. package/dist/esm/webgl/debug/types.d.ts +114 -0
  36. package/dist/esm/webgl/index.d.ts +2 -2
  37. package/dist/esm/webgl/main.d.ts +2 -4
  38. package/dist/esm/webgl/shaders/StandardShaderPart.d.ts +1 -1
  39. package/dist/esm/webgl/types/CommonTypes.d.ts +0 -1
  40. package/dist/esm/webgpu/WebGpuLightClusterBuilder.d.ts +34 -0
  41. package/dist/esm/webgpu/WebGpuResourceRepository.d.ts +28 -3
  42. package/dist/esm/webgpu/WebGpuStrategyBasic.d.ts +18 -1
  43. package/dist/esmdev/foundation/components/Camera/CameraComponent.d.ts +15 -0
  44. package/dist/esmdev/foundation/components/Light/LightComponent.d.ts +109 -4
  45. package/dist/esmdev/foundation/components/Physics/PhysicsComponent.d.ts +1 -1
  46. package/dist/esmdev/foundation/core/Component.d.ts +1 -8
  47. package/dist/esmdev/foundation/core/Config.d.ts +165 -6
  48. package/dist/esmdev/foundation/core/GlobalDataRepository.d.ts +12 -8
  49. package/dist/esmdev/foundation/core/MemoryManager.d.ts +12 -0
  50. package/dist/esmdev/foundation/definitions/ProcessApproach.d.ts +1 -6
  51. package/dist/esmdev/foundation/definitions/ShaderSemantics.d.ts +0 -1
  52. package/dist/esmdev/foundation/helpers/Shadow/PointShadowMap.d.ts +3 -1
  53. package/dist/esmdev/foundation/helpers/Shadow/ShadowSystem.d.ts +13 -0
  54. package/dist/esmdev/foundation/materials/contents/CustomMaterialContent.d.ts +1 -1
  55. package/dist/esmdev/foundation/materials/core/AbstractMaterialContent.d.ts +1 -23
  56. package/dist/esmdev/foundation/materials/core/Material.d.ts +10 -14
  57. package/dist/esmdev/foundation/materials/core/MaterialRepository.d.ts +5 -0
  58. package/dist/esmdev/foundation/physics/PhysicsProperty.d.ts +9 -2
  59. package/dist/esmdev/foundation/physics/index.d.ts +0 -1
  60. package/dist/esmdev/foundation/renderer/index.d.ts +1 -0
  61. package/dist/esmdev/foundation/renderer/lightCluster/LightClusterBuilder.d.ts +97 -0
  62. package/dist/esmdev/foundation/renderer/lightCluster/LightClusterLayout.d.ts +98 -0
  63. package/dist/esmdev/foundation/renderer/lightCluster/LightClusterSystem.d.ts +171 -0
  64. package/dist/esmdev/foundation/renderer/lightCluster/index.d.ts +1 -0
  65. package/dist/esmdev/foundation/system/Engine.d.ts +9 -2
  66. package/dist/esmdev/foundation/system/EngineState.d.ts +1 -1
  67. package/dist/esmdev/foundation/textures/TextureArray.d.ts +1 -1
  68. package/dist/esmdev/index.js +3419 -2172
  69. package/dist/esmdev/webgl/MultiDraw.d.ts +1 -1
  70. package/dist/esmdev/webgl/WebGLResourceRepository.d.ts +1 -2
  71. package/dist/{esm/webgl/WebGLStrategyDataTexture.d.ts → esmdev/webgl/WebGLStrategyWebGL2.d.ts} +20 -4
  72. package/dist/esmdev/webgl/debug/BufferLayoutDecoder.d.ts +83 -0
  73. package/dist/esmdev/webgl/debug/ShaderParameterDebugger.d.ts +47 -0
  74. package/dist/esmdev/webgl/debug/index.d.ts +3 -0
  75. package/dist/esmdev/webgl/debug/types.d.ts +114 -0
  76. package/dist/esmdev/webgl/index.d.ts +2 -2
  77. package/dist/esmdev/webgl/main.d.ts +2 -4
  78. package/dist/esmdev/webgl/shaders/StandardShaderPart.d.ts +1 -1
  79. package/dist/esmdev/webgl/types/CommonTypes.d.ts +0 -1
  80. package/dist/esmdev/webgpu/WebGpuLightClusterBuilder.d.ts +34 -0
  81. package/dist/esmdev/webgpu/WebGpuResourceRepository.d.ts +28 -3
  82. package/dist/esmdev/webgpu/WebGpuStrategyBasic.d.ts +18 -1
  83. package/package.json +2 -1
  84. package/dist/esm/foundation/physics/Oimo/OimoPhysicsStrategy.d.ts +0 -124
  85. package/dist/esm/foundation/physics/Oimo/index.d.ts +0 -1
  86. package/dist/esm/webgl/WebGLStrategyUniform.d.ts +0 -193
  87. package/dist/esmdev/foundation/physics/Oimo/OimoPhysicsStrategy.d.ts +0 -124
  88. package/dist/esmdev/foundation/physics/Oimo/index.d.ts +0 -1
  89. package/dist/esmdev/webgl/WebGLStrategyUniform.d.ts +0 -193
@@ -146,7 +146,7 @@ export declare class Material extends RnObject {
146
146
  private __multiDrawStateKeyVersion;
147
147
  /**
148
148
  * Signature of the texture/sampler bindings and the true uniforms that are set per
149
- * material in DataTexture mode. Materials with the same signature can share one
149
+ * material in WebGL2 mode. Materials with the same signature can share one
150
150
  * multi-draw batch (their per-draw state differs only in the data texture row).
151
151
  * @internal
152
152
  */
@@ -206,11 +206,10 @@ export declare class Material extends RnObject {
206
206
  isShaderProgramReady(primitive: Primitive): boolean;
207
207
  /**
208
208
  * Sets up uniform locations for material nodes in the shader program.
209
- * @internal Called from WebGLStrategyDataTexture and WebGLStrategyUniform only
210
- * @param isUniformOnlyMode - Whether to operate in uniform-only mode
209
+ * @internal Called from WebGLStrategyWebGL2 only
211
210
  * @param primitive - The primitive to set up uniforms for
212
211
  */
213
- _setUniformLocationsOfMaterialNodes(isUniformOnlyMode: boolean, primitive: Primitive): void;
212
+ _setUniformLocationsOfMaterialNodes(primitive: Primitive): void;
214
213
  /**
215
214
  * Gets the shader program UID for the given primitive.
216
215
  * @param primitive - The primitive to get the shader program UID for
@@ -230,7 +229,7 @@ export declare class Material extends RnObject {
230
229
  getBelongPrimitives(): Map<number, Primitive>;
231
230
  /**
232
231
  * Creates a WebGL shader program for this material and the given primitive.
233
- * @internal Called from WebGLStrategyDataTexture and WebGLStrategyUniform
232
+ * @internal Called from WebGLStrategyWebGL2
234
233
  * @param componentDataAccessMethodDefinitionsForVertexShader - method definitions for component data access for vertex shader
235
234
  * @param componentDataAccessMethodDefinitionsForPixelShader - method definitions for component data access for pixel shader
236
235
  * @param propertySetterOfGlobalDataRepository - Function to set shader properties of global data repository
@@ -252,7 +251,7 @@ export declare class Material extends RnObject {
252
251
  _createProgramWebGpu(engine: Engine, primitive: Primitive, componentDataAccessMethodDefinitionsForVertexShader: string, componentDataAccessMethodDefinitionsForPixelShader: string, propertySetterOfGlobalDataRepository: getShaderPropertyFuncOfGlobalDataRepository, propertySetterOfMaterial: getShaderPropertyFuncOfMaterial, morphedPositionGetter: string): void;
253
252
  /**
254
253
  * Creates a shader program using updated shader source code.
255
- * @internal Called from WebGLStrategyDataTexture and WebGLStrategyUniform
254
+ * @internal Called from WebGLStrategyWebGL2
256
255
  * @param updatedShaderSources - The updated shader source code
257
256
  * @param primitive - The primitive to create the program for
258
257
  * @param onError - Optional error callback function
@@ -261,18 +260,17 @@ export declare class Material extends RnObject {
261
260
  _createProgramByUpdatedSources(updatedShaderSources: ShaderSources, primitive: Primitive, onError?: (message: string) => void): [CGAPIResourceHandle, boolean];
262
261
  /**
263
262
  * Sets up basic uniform locations in the shader program.
264
- * @internal Called by WebGLStrategyDataTexture and WebGLStrategyUniform only
263
+ * @internal Called by WebGLStrategyWebGL2 only
265
264
  * @param primitive - The primitive to set up uniforms for
266
265
  */
267
266
  _setupBasicUniformsLocations(primitive: Primitive): void;
268
267
  /**
269
268
  * Sets up additional uniform locations in the shader program.
270
- * @internal Called by WebGLStrategyDataTexture and WebGLStrategyUniform only
269
+ * @internal Called by WebGLStrategyWebGL2 only
271
270
  * @param shaderSemantics - Array of shader semantics to set up
272
- * @param isUniformOnlyMode - Whether to operate in uniform-only mode
273
271
  * @param primitive - The primitive to set up uniforms for
274
272
  */
275
- _setupAdditionalUniformLocations(shaderSemantics: ShaderSemanticsInfo[], isUniformOnlyMode: boolean, primitive: Primitive): void;
273
+ _setupAdditionalUniformLocations(shaderSemantics: ShaderSemanticsInfo[], primitive: Primitive): void;
276
274
  /**
277
275
  * Sets internal setting parameters to GPU for WebGPU rendering.
278
276
  * @param params - Object containing material and rendering arguments
@@ -284,7 +282,7 @@ export declare class Material extends RnObject {
284
282
  }): void;
285
283
  /**
286
284
  * Sets parameters to GPU for WebGL rendering per material.
287
- * @internal Called from WebGLStrategyDataTexture and WebGLStrategyUniform only
285
+ * @internal Called from WebGLStrategyWebGL2 only
288
286
  * @param params - Object containing rendering parameters
289
287
  */
290
288
  _setParametersToGpuWebGL({ engine, material, shaderProgram, firstTime, args, }: {
@@ -319,10 +317,9 @@ export declare class Material extends RnObject {
319
317
  * Sets parameters to GPU for WebGL rendering without internal settings.
320
318
  * @param params - Object containing rendering parameters
321
319
  */
322
- _setParametersToGpuWebGLWithOutInternalSetting({ shaderProgram, firstTime, isUniformMode, }: {
320
+ _setParametersToGpuWebGLWithOutInternalSetting({ shaderProgram, firstTime, }: {
323
321
  shaderProgram: WebGLProgram;
324
322
  firstTime: boolean;
325
- isUniformMode: boolean;
326
323
  }): void;
327
324
  /**
328
325
  * Gets shader property strings for vertex and pixel shaders.
@@ -337,7 +334,6 @@ export declare class Material extends RnObject {
337
334
  /**
338
335
  * Sets automatic parameters to GPU for WebGL rendering.
339
336
  * @private
340
- * @param isUniformMode - Whether to operate in uniform mode
341
337
  * @param firstTime - Whether this is the first time setting parameters
342
338
  * @param shaderProgram - The WebGL shader program
343
339
  */
@@ -141,6 +141,11 @@ export declare class MaterialRepository {
141
141
  */
142
142
  private __allocateBufferView;
143
143
  _getMaterialCountPerBufferView(materialTypeName: string): Count | undefined;
144
+ /**
145
+ * Public accessor used by debugging tools. The material SID selects a buffer view
146
+ * with `floor(materialSID / materialCountPerBufferView)`.
147
+ */
148
+ getMaterialCountPerBufferView(materialTypeName: string): Count | undefined;
144
149
  _getBufferViewVersion(materialTypeName: string): number;
145
150
  /**
146
151
  * Invalidates all shader programs for all registered materials.
@@ -1,7 +1,11 @@
1
1
  import type { PhysicsShapeTypeEnum } from '../definitions/PhysicsShapeType';
2
2
  import type { IVector3 } from '../math';
3
3
  import type { IQuaternion } from '../math/IQuaternion';
4
- export type PhysicsEngineType = 'oimo' | 'rapier';
4
+ /**
5
+ * Physics engine selection.
6
+ * @deprecated Only 'rapier' is supported. Oimo.js support was removed. Retained for source compatibility.
7
+ */
8
+ export type PhysicsEngineType = 'rapier';
5
9
  export type PhysicsBodyProperty = {
6
10
  /** Whether the body is dynamic. False creates a fixed body. */
7
11
  move: boolean;
@@ -73,7 +77,10 @@ export type PhysicsPropertyInner = {
73
77
  export type PhysicsProperty = {
74
78
  /** Whether physics simulation is enabled for the entity */
75
79
  use: boolean;
76
- /** Physics engine to use. Defaults to 'rapier'. */
80
+ /**
81
+ * Physics engine to use.
82
+ * @deprecated Rapier is always used. Omit this field.
83
+ */
77
84
  engine?: PhysicsEngineType;
78
85
  /** Whether the physics body can move (dynamic) or is static */
79
86
  move: boolean;
@@ -1,5 +1,4 @@
1
1
  export * from './CharacterControllerStrategy';
2
- export * from './Oimo';
3
2
  export * from './PhysicsProperty';
4
3
  export * from './PhysicsStrategy';
5
4
  export * from './PhysicsWorldProperty';
@@ -2,6 +2,7 @@ export * from './CGAPIResourceRepository';
2
2
  export * from './Expression';
3
3
  export * from './Frame';
4
4
  export * from './FrameBuffer';
5
+ export * from './lightCluster';
5
6
  export * from './pipelines';
6
7
  export * from './RenderingCommonMethods';
7
8
  export * from './RenderPass';
@@ -0,0 +1,97 @@
1
+ import { type LightClusterViewLayout } from './LightClusterLayout';
2
+ /**
3
+ * Pure functions that build the Clustered Forward light clusters of a view on the CPU.
4
+ * The memory layout is described in LightClusterLayout.ts.
5
+ *
6
+ * A light is assigned to a cluster `(x, y, z)` when its bounding sphere `(c, r)` passes all of these tests
7
+ * (the WebGPU compute shader in WebGpuLightClusterBuilder.ts implements the same tests):
8
+ * - The sphere is not entirely behind the eye plane: `dot(rowW, c) / |rowW.xyz| > -r` (skipped when rowW.xyz is 0).
9
+ * - Column: `x == 0 || dist(rowX - x * rowW, c) >= -r` and `x == gridX - 1 || dist((x + 1) * rowW - rowX, c) >= -r`.
10
+ * - Row: the same with rowY and y.
11
+ * - Slice: `z == 0 || d + r >= zs(z)` and `z == gridZ - 1 || d - r <= zs(z + 1)`,
12
+ * where `d = dot(rowDepth, c)` and `zs(k) = exp((k - sliceBias) / sliceScale)`.
13
+ *
14
+ * `dist(plane, c)` is the signed distance `(dot(plane.xyz, c) + plane.w) / |plane.xyz|`.
15
+ * The clusters at the borders of the grid extend to infinity, so a fragment whose cluster coordinates are clamped
16
+ * is still inside its cluster.
17
+ */
18
+ /**
19
+ * The result of a light cluster build.
20
+ * - `'built'`: every light was stored.
21
+ * - `'truncated'`: some clusters had more lights than `clusterLightCapacity` and kept only their nearest lights.
22
+ */
23
+ export type LightClusterBuildResult = 'built' | 'truncated';
24
+ /** Scratch memory reused between builds. */
25
+ export type LightClusterBuildScratch = {
26
+ /** The number of vec4 of the light index list used by the last build. */
27
+ usedIndexVec4Count: number;
28
+ counts: Uint32Array;
29
+ offsets: Uint32Array;
30
+ sliceBoundaries: Float64Array;
31
+ /** The normalized tile planes `rowX - k * rowW` (k = 0..gridX), (nx, ny, nz, w) each. Degenerate planes are 0. */
32
+ planesX: Float64Array;
33
+ /** The normalized tile planes `rowY - k * rowW` (k = 0..gridY). */
34
+ planesY: Float64Array;
35
+ /**
36
+ * The columns, rows and slices each light passes (gridX + gridY + gridZ flags per light), evaluated once and
37
+ * reused by the count and fill passes. Grown on demand.
38
+ */
39
+ axisPass: Uint8Array;
40
+ /** Whether each light touches any cluster. Grown on demand. */
41
+ isLightInView: Uint8Array;
42
+ /** The first and last passing column, row and slice of each light (6 per light). Grown on demand. */
43
+ axisRanges: Int32Array;
44
+ };
45
+ /**
46
+ * Creates the scratch memory for a layout.
47
+ */
48
+ export declare function createLightClusterBuildScratch(layout: LightClusterViewLayout): LightClusterBuildScratch;
49
+ /**
50
+ * Writes the header of a view region: the clip rows, the depth row, the grid size and the depth slices.
51
+ *
52
+ * The depth range of the slices is the intersection of the depth range of the light spheres and the visible depth
53
+ * range of the camera: the slices are not wasted on depths where no light reaches, nor in front of the camera near
54
+ * plane or beyond the far plane.
55
+ *
56
+ * @param dst - The memory of the view region
57
+ * @param viewOffset - The float offset of the view region in `dst`
58
+ * @param viewMatrix - The view matrix (column-major, as uploaded to the GPU)
59
+ * @param projectionMatrix - The projection matrix (column-major, as uploaded to the GPU)
60
+ * @param spheres - The light spheres in world space (cx, cy, cz, r) * localCount
61
+ * @param localCount - The number of light spheres
62
+ * @param layout - The layout of the view region
63
+ * @param visibleNearDepth - The view depth of the camera near plane (0 disables the clamp)
64
+ * @param visibleFarDepth - The view depth of the camera far plane (Infinity disables the clamp)
65
+ */
66
+ export declare function writeLightClusterViewHeader(dst: Float32Array, viewOffset: number, viewMatrix: ArrayLike<number>, projectionMatrix: ArrayLike<number>, spheres: ArrayLike<number>, localCount: number, layout: LightClusterViewLayout, visibleNearDepth?: number, visibleFarDepth?: number): void;
67
+ /**
68
+ * Builds the cluster table and the light index list of a view whose header has been written.
69
+ *
70
+ * The lights of each cluster are stored in the order of `localEntries`. The caller passes the lights sorted by their
71
+ * view depth (nearest first) when the number of local lights exceeds the capacity, so that a cluster which is full
72
+ * keeps its nearest lights. Every cluster keeps at most `layout.clusterLightCapacity` lights, so the list cannot
73
+ * overflow.
74
+ */
75
+ export declare function buildLightClustersOnCpu(dst: Float32Array, viewOffset: number, layout: LightClusterViewLayout, spheres: ArrayLike<number>, localEntries: ArrayLike<number>, localCount: number, scratch: LightClusterBuildScratch): LightClusterBuildResult;
76
+ /**
77
+ * The cluster-centric form of the assignment test (the form the WebGPU compute shader uses).
78
+ * It must agree with the light-centric build of `buildLightClustersOnCpu`.
79
+ */
80
+ export declare function sphereIntersectsLightCluster(dst: Float32Array, viewOffset: number, layout: LightClusterViewLayout, x: number, y: number, z: number, cx: number, cy: number, cz: number, r: number): boolean;
81
+ /**
82
+ * The reference implementation of the cluster lookup of the shaders (`getLightList`).
83
+ *
84
+ * @returns The cluster index, or -1 when the position uses the all-light list (behind the eye plane or overflow).
85
+ */
86
+ export declare function computeLightClusterIndex(dst: Float32Array, viewOffset: number, px: number, py: number, pz: number): number;
87
+ /**
88
+ * Reads the light entries of a cluster built by `buildLightClustersOnCpu` (or by the compute shader).
89
+ */
90
+ export declare function readLightClusterEntries(dst: Float32Array, viewOffset: number, layout: LightClusterViewLayout, clusterIndex: number): number[];
91
+ /**
92
+ * Computes the bounding sphere of a light from the values uploaded to the GPU.
93
+ * Returns the radius, or -1 when the light can not be culled (directional lights and lights without range).
94
+ *
95
+ * @param out - Receives (cx, cy, cz, r) at `outOffset`
96
+ */
97
+ export declare function computeLightBoundingSphere(typeValue: number, position: ArrayLike<number>, direction: ArrayLike<number>, range: number, outerConeCos: number, out: Float64Array, outOffset: number): number;
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Memory layout of the Clustered Forward light data.
3
+ *
4
+ * All values are stored as 32-bit floats in the GPU instance data (the data texture in WebGL2,
5
+ * the storage buffer in WebGPU) and read with `fetchElement(vec4Index)` / `get_clusteredLightData(0u, idx)`.
6
+ * Integers are below 2^24 and are therefore represented exactly.
7
+ *
8
+ * ## Global property `clusteredLightData` (always present)
9
+ * ```
10
+ * [0] (allCount, globalCount, localCount, sphereBase)
11
+ * [1..] allList: light entries, 4 entries per vec4.
12
+ * The lights that can not be culled (directional lights, lights without range) come first,
13
+ * then the local lights. Both groups are sorted by the light component SID.
14
+ * ```
15
+ * `sphereBase` is -1 (the GPU cluster build receives its own sorted light input in a separate storage buffer).
16
+ *
17
+ * ## Light entry
18
+ * `entry = lightComponentSID + 65536 * (shadowSlot + 1)` (`shadowSlot` is -1 when the light has no shadow slot).
19
+ *
20
+ * ## View region (one per camera, allocated on demand, referenced by `CameraComponent.lightClusterViewInfo.x`)
21
+ * ```
22
+ * [0] rowX 0.5 * gridX * (clipRow0 + clipRow3): tileX = floor(dot(rowX, p) / dot(rowW, p))
23
+ * [1] rowY 0.5 * gridY * (clipRow1 + clipRow3)
24
+ * [2] rowW clipRow3
25
+ * [3] rowDepth normalized view depth row: depth = dot(rowDepth, p) (positive in front of the camera)
26
+ * [4] (gridX, gridY, gridZ, state) state: 0 (valid; reserved)
27
+ * [5] (sliceScale, sliceBias, zNear, zFar) slice = floor(log(depth) * sliceScale + sliceBias)
28
+ * [6..] cluster table: one vec4 per cluster, (offset, count, 0, 0).
29
+ * offset is the vec4 index of the list of the cluster from the index list start.
30
+ * [6 + clusterCount..] index list: 4 entries per vec4.
31
+ * The CPU build packs the lists densely (offset is the exclusive prefix sum of the cluster counts); the WebGPU
32
+ * build gives every cluster its own `ceil(clusterLightCapacity / 4)` vec4s (offset = cluster * that). The rest
33
+ * of the last vec4 of a list is zero.
34
+ * ```
35
+ * The cluster index is `(z * gridY + y) * gridX + x`. The clusters at the borders of the grid extend to infinity,
36
+ * so clamping the cluster coordinates of a fragment is always correct.
37
+ *
38
+ * Every cluster owns whole vec4s of the table and of the index list, so the compute shaders never write to a vec4
39
+ * that another invocation writes (some backends store a vector component by rewriting the whole vector).
40
+ */
41
+ export declare const LightClusterLayout: {
42
+ /** The radix used to pack the shadow slot into a light entry. */
43
+ readonly entryRadix: 65536;
44
+ /** The maximum number of lights in the light list (light component SIDs must be below the radix). */
45
+ readonly maxLightListLength: 65535;
46
+ /** The number of vec4 of the header of the global property. */
47
+ readonly globalHeaderVec4Count: 1;
48
+ /** The number of vec4 of the header of a view region. */
49
+ readonly viewHeaderVec4Count: 6;
50
+ /** The vec4 offset of the grid info (gridX, gridY, gridZ, state) in a view region. */
51
+ readonly viewGridOffset: 4;
52
+ /** The vec4 offset of the slice info (sliceScale, sliceBias, zNear, zFar) in a view region. */
53
+ readonly viewSliceOffset: 5;
54
+ };
55
+ /**
56
+ * Clamps the length of the light list to the supported range.
57
+ */
58
+ export declare function getLightListCapacity(maxLightListLength: number): number;
59
+ /**
60
+ * Returns the array length (in vec4) of the `clusteredLightData` global property.
61
+ */
62
+ export declare function getLightClusterGlobalPropertyLength(maxLightListLength: number): number;
63
+ /**
64
+ * Packs a light component SID and a shadow slot into a light entry.
65
+ */
66
+ export declare function packLightEntry(lightComponentSid: number, shadowSlot: number): number;
67
+ /**
68
+ * Unpacks a light entry into the light component SID and the shadow slot (-1 = no shadow slot).
69
+ */
70
+ export declare function unpackLightEntry(entry: number): [number, number];
71
+ /**
72
+ * The sizes of a view region (in vec4).
73
+ */
74
+ export type LightClusterViewLayout = {
75
+ gridX: number;
76
+ gridY: number;
77
+ gridZ: number;
78
+ clusterCount: number;
79
+ tableVec4Count: number;
80
+ indexCapacity: number;
81
+ indexVec4Count: number;
82
+ /** The maximum number of lights stored in one cluster. */
83
+ clusterLightCapacity: number;
84
+ viewVec4Count: number;
85
+ };
86
+ /**
87
+ * Computes the sizes of a view region from the grid size and the number of lights per cluster the light index list
88
+ * is sized for. The list is shared by the clusters, and each cluster pads its list to a multiple of 4 entries, so
89
+ * the list never overflows while no cluster has more lights than `lightCapacityPerCluster`.
90
+ */
91
+ export declare function getLightClusterViewLayout(gridSize: readonly [number, number, number], lightCapacityPerCluster: number): LightClusterViewLayout;
92
+ /**
93
+ * Splits the byte range `[start, end)` into the parts outside the excluded ranges.
94
+ *
95
+ * @param excluded - Ranges `[start, end)` sorted by their start
96
+ * @returns The remaining ranges `[start, end)` in ascending order
97
+ */
98
+ export declare function subtractByteRanges(start: number, end: number, excluded: readonly [number, number][]): [number, number][];
@@ -0,0 +1,171 @@
1
+ import type { Engine } from '../../system/Engine';
2
+ import type { Expression } from '../Expression';
3
+ import { type LightClusterViewLayout } from './LightClusterLayout';
4
+ /**
5
+ * The parameters of the light cluster build on the GPU (WebGPU compute shaders).
6
+ *
7
+ * @internal
8
+ */
9
+ export type LightClusterGpuBuildView = {
10
+ /** The vec4 index of the view region to build. */
11
+ viewVec4Base: number;
12
+ /** The number of local lights of the view. */
13
+ localCount: number;
14
+ /** The float offset of the view in `data`. */
15
+ dataOffset: number;
16
+ };
17
+ export type LightClusterGpuBuildParams = {
18
+ views: readonly LightClusterGpuBuildView[];
19
+ /** The sorted light spheres, entries and Z slice ranges of the views, `dataStride` floats per view. */
20
+ data: Float32Array;
21
+ dataStride: number;
22
+ /** The maximum number of local lights stored in the build data of a view. */
23
+ localCapacity: number;
24
+ layout: LightClusterViewLayout;
25
+ };
26
+ /**
27
+ * Builds the light lists of Clustered Forward rendering once per frame.
28
+ *
29
+ * - Every enabled light is written to the all-light list of the `clusteredLightData` global property
30
+ * (lights that can not be culled first, then the local lights, each sorted by the component SID).
31
+ * - For each camera used by the render passes of the frame (up to `Config.clusteredForwardMaxViewNumber`),
32
+ * a view region with the cluster grid is built when there are local lights, and the camera's
33
+ * `lightClusterViewInfo` points to it. Other cameras use the all-light list.
34
+ *
35
+ * The light data is read from the GPU instance data of the light components (the values the shaders see),
36
+ * so the clusters always match the lights the shaders evaluate.
37
+ *
38
+ * The data lives in the GPU instance data, so it is uploaded by the rendering strategy. `dataVersion` changes
39
+ * whenever this system writes the data, and the strategies upload the data when it changes.
40
+ */
41
+ export declare class LightClusterSystem {
42
+ private __engine;
43
+ private __dataVersion;
44
+ private __globalData?;
45
+ private __globalDataVec4Base;
46
+ private __capacity;
47
+ private __layout?;
48
+ private __scratch?;
49
+ private __entries;
50
+ private __prevEntries;
51
+ private __spheres;
52
+ private __prevSpheres;
53
+ private __localEntries;
54
+ private __allCount;
55
+ private __globalCount;
56
+ private __localCount;
57
+ /** The number of local lights that emit light (counted for Config.clusteredForwardMinLocalLightNumber). */
58
+ private __emittingLocalCount;
59
+ private __prevAllCount;
60
+ private __prevGlobalCount;
61
+ private __prevSphereVec4Base;
62
+ private __viewRegions;
63
+ /** The camera component SID of each view slot (-1: free). */
64
+ private __slotCameraSids;
65
+ /** Whether each view slot holds valid clusters for its camera, the current lights and the matrices below. */
66
+ private __slotValid;
67
+ /** The view and projection matrices each view slot was built with. */
68
+ private __slotMatrices;
69
+ /** The cameras whose `lightClusterViewInfo` currently points to a view region. */
70
+ private __camerasWithView;
71
+ private __isAllocationFailed;
72
+ private __isCapacityWarned;
73
+ /** The order of the local lights of the view being built, sorted by view depth when the number of lights is large. */
74
+ private __sortOrder;
75
+ private __sortDepths;
76
+ private __sortedSpheres;
77
+ private __sortedEntries;
78
+ private __sliceRanges;
79
+ /** The GPU build inputs (sorted spheres, entries and slice ranges) of the views, stored per view slot. */
80
+ private __gpuBuildData;
81
+ private __gpuBuildStride;
82
+ private __gpuBuildViews;
83
+ /** The number of local lights of the view built in each slot. */
84
+ private __slotLocalCounts;
85
+ /**
86
+ * The byte ranges (in the GPU instance data) of the cluster tables and light index lists of the views. They are
87
+ * large and written by this system or the compute shaders, so the generic upload of the GPU instance data skips them.
88
+ */
89
+ private __excludedByteRanges;
90
+ /** The byte ranges built on the CPU since the last upload, which the rendering strategy uploads separately. */
91
+ private __dirtyByteRanges;
92
+ /** The data of `u_lightClusterView` of the WebGL2 shaders (7 vec4). */
93
+ private __viewUniformData;
94
+ constructor(engine: Engine);
95
+ /**
96
+ * Incremented whenever this system writes to the GPU instance data.
97
+ */
98
+ get dataVersion(): number;
99
+ /**
100
+ * Whether the cluster tables are built by WebGPU compute shaders instead of the CPU.
101
+ */
102
+ get isGpuBuild(): boolean;
103
+ /**
104
+ * The parameters of the views to build on the GPU, or undefined when nothing has to be built on the GPU.
105
+ *
106
+ * @internal
107
+ */
108
+ _getGpuBuildParams(): LightClusterGpuBuildParams | undefined;
109
+ /**
110
+ * The byte ranges of the GPU instance data that the generic upload must skip, sorted by the start.
111
+ * The ranges are uploaded by `_takeDirtyByteRanges` (CPU build) or written by the compute shaders (GPU build).
112
+ *
113
+ * @internal
114
+ */
115
+ _getExcludedByteRanges(): readonly [number, number][];
116
+ /**
117
+ * Returns the byte ranges of the GPU instance data built on the CPU since the last call, and clears them.
118
+ * The rendering strategy uploads them after the generic upload.
119
+ *
120
+ * @internal
121
+ */
122
+ _takeDirtyByteRanges(): [number, number][];
123
+ /**
124
+ * Returns the data of the `u_lightClusterView` uniform of the WebGL2 shaders for a camera: the view header
125
+ * (6 vec4) and (vec4 index of the view, 0, 0, 0), or a view index of -1 when the camera has no light clusters.
126
+ * The returned array is reused.
127
+ *
128
+ * @internal
129
+ */
130
+ _getViewUniformData(cameraSid: number): Float32Array;
131
+ /**
132
+ * Updates the light lists for the render passes of this frame.
133
+ * This must be called after the Logic stage (the light and camera data are final) and before the strategy
134
+ * uploads the GPU instance data.
135
+ *
136
+ * @param expressions - The expressions rendered in this frame
137
+ */
138
+ update(expressions: readonly Expression[]): void;
139
+ private __initialize;
140
+ /**
141
+ * Collects the enabled lights into the all-light list and the local light spheres.
142
+ * @returns true if the lists changed since the previous frame
143
+ */
144
+ private __collectLights;
145
+ private __writeGlobalData;
146
+ /**
147
+ * Lists the cameras of the render passes that may draw lit materials, in the order of the render passes.
148
+ * The rules are the same as the camera selection of the rendering strategies.
149
+ */
150
+ private __collectCameras;
151
+ /**
152
+ * Assigns the view slots to the cameras, builds the views whose inputs changed and updates the
153
+ * `lightClusterViewInfo` of the cameras.
154
+ * @returns true if anything was written
155
+ */
156
+ private __assignViews;
157
+ /**
158
+ * Prepares the light order and the Z slice ranges of a view for the builders.
159
+ *
160
+ * The lights are sorted by their view depth when there are more local lights than the capacity of a cluster, so
161
+ * that a cluster which is full keeps its nearest lights. Otherwise the light component SID order is kept (no
162
+ * cluster can be full), which keeps the result identical to a build without a capacity.
163
+ *
164
+ * For the GPU build, the sorted spheres, entries and ranges are copied to the GPU build data of the view.
165
+ */
166
+ private __prepareViewBuild;
167
+ /**
168
+ * Allocates a region in the GPU instance data. Returns undefined (and disables the clusters) when it fails.
169
+ */
170
+ private __allocate;
171
+ }
@@ -0,0 +1 @@
1
+ export * from './LightClusterSystem';
@@ -14,6 +14,7 @@ import { Logger } from '../misc/Logger';
14
14
  import { type ICGAPIResourceRepository } from '../renderer/CGAPIResourceRepository';
15
15
  import { Expression } from '../renderer/Expression';
16
16
  import { Frame } from '../renderer/Frame';
17
+ import { LightClusterSystem } from '../renderer/lightCluster/LightClusterSystem';
17
18
  import { EngineState } from './EngineState';
18
19
  import type { WebARSystem } from '../../xr/WebARSystem';
19
20
  import type { WebXRSystem } from '../../xr/WebXRSystem';
@@ -35,7 +36,7 @@ interface EngineInitDescription {
35
36
  * @example
36
37
  * ```
37
38
  * const engine = await Rn.Engine.init({
38
- * approach: Rn.ProcessApproach.DataTexture,
39
+ * approach: Rn.ProcessApproach.WebGL2,
39
40
  * canvas: document.getElementById('world') as HTMLCanvasElement,
40
41
  * });
41
42
  *
@@ -68,6 +69,7 @@ export declare class Engine extends RnObject {
68
69
  private __memoryManager?;
69
70
  private __materialRepository;
70
71
  private __globalDataRepository;
72
+ private __lightClusterSystem;
71
73
  private __config;
72
74
  private __logger;
73
75
  private __dummyTextures?;
@@ -75,6 +77,7 @@ export declare class Engine extends RnObject {
75
77
  private __lastCameraControllerComponentsUpdateCount;
76
78
  private __lastTransformComponentsUpdateCount;
77
79
  private __lastPrimitiveCount;
80
+ private __lastLightComponentsUpdateCount;
78
81
  /** Shader program cache map for this engine instance. Maps shader text to program UIDs. */
79
82
  private __shaderProgramCache;
80
83
  private __gpuTimerEnabled;
@@ -218,7 +221,7 @@ export declare class Engine extends RnObject {
218
221
  * @example
219
222
  * ```
220
223
  * const engine = await Rn.Engine.init({
221
- * approach: Rn.ProcessApproach.DataTexture,
224
+ * approach: Rn.ProcessApproach.WebGL2,
222
225
  * canvas: document.getElementById('world') as HTMLCanvasElement,
223
226
  * });
224
227
  * ```
@@ -238,6 +241,10 @@ export declare class Engine extends RnObject {
238
241
  get webARSystem(): WebARSystem;
239
242
  get memoryManager(): MemoryManager;
240
243
  get globalDataRepository(): GlobalDataRepository;
244
+ /**
245
+ * The system that builds the light lists of Clustered Forward rendering.
246
+ */
247
+ get lightClusterSystem(): LightClusterSystem;
241
248
  get materialRepository(): MaterialRepository;
242
249
  get webglResourceRepository(): WebGLResourceRepository;
243
250
  get cgApiResourceRepository(): ICGAPIResourceRepository;
@@ -6,7 +6,7 @@ import { type ProcessApproachEnum } from '../definitions/ProcessApproach';
6
6
  export declare class EngineState {
7
7
  /**
8
8
  * The current process approach being used for rendering operations.
9
- * Determines whether to use WebGL2 uniform approach, data texture approach, WebGPU, or none.
9
+ * Determines whether to use the WebGL2 data texture approach, WebGPU, or none.
10
10
  */
11
11
  currentProcessApproach: ProcessApproachEnum;
12
12
  /**
@@ -29,7 +29,7 @@ export declare class TextureArray extends AbstractTexture implements Disposable
29
29
  private static __deleteInternalTexture;
30
30
  /**
31
31
  * Loads a 1x1 texture array with a solid color.
32
- * This creates a texture array with the specified number of layers (Config.maxLightNumber),
32
+ * This creates a texture array with the specified number of layers (Config.maxLightNumberOrOne),
33
33
  * each containing a single pixel of the specified color.
34
34
  *
35
35
  * @param rgbaStr - CSS color string in rgba format (default: 'rgba(0,0,0,1)' for black)