@luma.gl/shaders 10.0.0-alpha.11 → 10.0.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luma.gl/shaders",
3
- "version": "10.0.0-alpha.11",
3
+ "version": "10.0.0-alpha.12",
4
4
  "description": "Shader module system for luma.gl",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -61,7 +61,7 @@
61
61
  "prepublishOnly": "npm run build-minified-bundle && npm run build-dev-bundle"
62
62
  },
63
63
  "dependencies": {
64
- "@math.gl/core": "~5.0.0-beta.5"
64
+ "@math.gl/core": "~5.0.0-beta.6"
65
65
  },
66
- "gitHead": "abf0b44182fb51a73368e71f1166ca1cf4de63d6"
66
+ "gitHead": "7a2a8a50666ead2337dac60f378dcd920587310e"
67
67
  }
package/src/index.ts CHANGED
@@ -305,3 +305,7 @@ export type {PBRContactShadowProps} from './modules/lighting/pbr-contact-shadow/
305
305
  export {clusteredLightGrid} from './modules/lighting/clustered-light-grid/clustered-light-grid';
306
306
  export {pbrAmbientOcclusion} from './modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion';
307
307
  export type {PBRAmbientOcclusionProps} from './modules/lighting/pbr-ambient-occlusion/pbr-ambient-occlusion';
308
+
309
+ export {lightmapBake} from './modules/lighting/lightmap/lightmap';
310
+ export type {LightmapBakeUniforms} from './modules/lighting/lightmap/lightmap';
311
+ export {lightmapFilter} from './modules/lighting/lightmap/lightmap-filter';
@@ -0,0 +1,73 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
3
+
4
+ import type {ShaderModule} from '../../../lib/shader-module/shader-module';
5
+
6
+ /** Internal accumulation/copy/dilation shader shared by the engine baker. */
7
+ export const lightmapFilter = {
8
+ name: 'lightmapFilter',
9
+ bindingLayout: [
10
+ {name: 'lightmapFilter', group: 0},
11
+ {name: 'currentTexture', group: 0},
12
+ {name: 'previousTexture', group: 0},
13
+ {name: 'chartTexture', group: 0}
14
+ ],
15
+ uniformTypes: {weight: 'f32', mode: 'i32'},
16
+ defaultUniforms: {weight: 1, mode: 0},
17
+ getUniforms: (props: {weight?: number; mode?: number}) => props,
18
+ source: `
19
+ struct LightmapFilterUniforms {weight: f32, mode: i32};
20
+ @group(0) @binding(auto) var<uniform> lightmapFilter: LightmapFilterUniforms;
21
+ @group(0) @binding(auto) var currentTexture: texture_2d<f32>;
22
+ @group(0) @binding(auto) var previousTexture: texture_2d<f32>;
23
+ @group(0) @binding(auto) var chartTexture: texture_2d<f32>;
24
+ fn lightmapFilter_getColor(coordinates: vec2i) -> vec4f {
25
+ let current = textureLoad(currentTexture, coordinates, 0);
26
+ if (lightmapFilter.mode == 0) {
27
+ let previous = textureLoad(previousTexture, coordinates, 0);
28
+ return vec4f(mix(previous.rgb, current.rgb, lightmapFilter.weight), current.a);
29
+ }
30
+ if (lightmapFilter.mode == 2 || current.a > 0.0) {return current;}
31
+ let chart = textureLoad(chartTexture, coordinates, 0).r;
32
+ if (chart <= 0.0) {return current;}
33
+ let size = vec2i(textureDimensions(currentTexture));
34
+ for (var y = -1; y <= 1; y++) {
35
+ for (var x = -1; x <= 1; x++) {
36
+ let neighbor = coordinates + vec2i(x, y);
37
+ if (all(neighbor >= vec2i(0)) && all(neighbor < size)) {
38
+ let candidate = textureLoad(currentTexture, neighbor, 0);
39
+ if (candidate.a == chart) {return candidate;}
40
+ }
41
+ }
42
+ }
43
+ return current;
44
+ }`,
45
+ fs: `
46
+ layout(std140) uniform lightmapFilterUniforms {
47
+ float weight;
48
+ int mode;
49
+ } lightmapFilter;
50
+ uniform sampler2D currentTexture;
51
+ uniform sampler2D previousTexture;
52
+ uniform sampler2D chartTexture;
53
+ vec4 lightmapFilter_getColor(ivec2 coordinates) {
54
+ vec4 current = texelFetch(currentTexture, coordinates, 0);
55
+ if (lightmapFilter.mode == 0) {
56
+ return vec4(mix(texelFetch(previousTexture, coordinates, 0).rgb, current.rgb, lightmapFilter.weight), current.a);
57
+ }
58
+ if (lightmapFilter.mode == 2 || current.a > 0.0) {return current;}
59
+ float chart = texelFetch(chartTexture, coordinates, 0).r;
60
+ ivec2 size = textureSize(currentTexture, 0);
61
+ if (chart <= 0.0) return current;
62
+ for (int y = -1; y <= 1; y++) {
63
+ for (int x = -1; x <= 1; x++) {
64
+ ivec2 neighbor = coordinates + ivec2(x,y);
65
+ if (all(greaterThanEqual(neighbor, ivec2(0))) && all(lessThan(neighbor,size))) {
66
+ vec4 candidate = texelFetch(currentTexture, neighbor, 0);
67
+ if (candidate.a == chart) {return candidate;}
68
+ }
69
+ }
70
+ }
71
+ return current;
72
+ }`
73
+ } as const satisfies ShaderModule<{weight?: number; mode?: number}, {weight: number; mode: number}>;
@@ -0,0 +1,76 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
3
+
4
+ import type {NumberArray9} from '@math.gl/core';
5
+ import type {ShaderModule} from '../../../lib/shader-module/shader-module';
6
+ import {pbrMaterial} from '../pbr-material/pbr-material';
7
+
8
+ export type LightmapBakeUniforms = {
9
+ uvSet: number;
10
+ uvTransform: Readonly<NumberArray9>;
11
+ chartId: number;
12
+ };
13
+
14
+ /** Texture-space rasterization controls. Chart IDs are positive integers exactly representable in the target format. */
15
+ export const lightmapBake = {
16
+ name: 'lightmapBake',
17
+ dependencies: [pbrMaterial],
18
+ bindingLayout: [{name: 'lightmapBake', group: 0}],
19
+ uniformTypes: {uvSet: 'i32', uvTransform: 'mat3x3<f32>', chartId: 'f32'},
20
+ defaultUniforms: {uvSet: 1, uvTransform: [1, 0, 0, 0, 1, 0, 0, 0, 1], chartId: 1},
21
+ getUniforms: (props: Partial<LightmapBakeUniforms>) => props,
22
+ source: `
23
+ struct LightmapBakeUniforms {uvSet: i32, uvTransform: mat3x3f, chartId: f32};
24
+ @group(0) @binding(auto) var<uniform> lightmapBake: LightmapBakeUniforms;
25
+
26
+ fn lightmap_getIrradiance(position: vec3f, normal: vec3f, directionalVisibility: f32, pointVisibility: vec4f, spotVisibility: vec4f) -> vec3f {
27
+ if (lighting.enabled == 0) {return vec3f(0.0);}
28
+ var irradiance = lighting.ambientColor;
29
+ for (var index = 0; index < lighting.directionalLightCount; index++) {
30
+ let light = lighting_getDirectionalLight(index);
31
+ irradiance += light.color * max(dot(normal, -normalize(light.direction)), 0.0) * select(1.0, directionalVisibility, index == 0);
32
+ }
33
+ for (var index = 0; index < lighting.pointLightCount; index++) {
34
+ let light = lighting_getPointLight(index);
35
+ let offset = light.position - position;
36
+ let visibility = select(1.0, pointVisibility[min(index, 3)], index < 4);
37
+ irradiance += light.color * max(dot(normal, normalize(offset)), 0.0) * visibility / max(getPointLightAttenuation(light, length(offset)), 0.0001);
38
+ }
39
+ for (var index = 0; index < lighting.spotLightCount; index++) {
40
+ let light = lighting_getSpotLight(index);
41
+ irradiance += light.color * max(dot(normal, normalize(light.position - position)), 0.0) * select(1.0, spotVisibility[min(index, 3)], index < 4) / max(getSpotLightAttenuation(light, position), 0.0001);
42
+ }
43
+ return irradiance;
44
+ }
45
+ `,
46
+ vs: `layout(std140) uniform lightmapBakeUniforms {
47
+ int uvSet;
48
+ mat3 uvTransform;
49
+ float chartId;
50
+ } lightmapBake;`,
51
+ fs: `layout(std140) uniform lightmapBakeUniforms {
52
+ int uvSet;
53
+ mat3 uvTransform;
54
+ float chartId;
55
+ } lightmapBake;
56
+
57
+ vec3 lightmap_getIrradiance(vec3 position, vec3 normal, float directionalVisibility, vec4 pointVisibility, vec4 spotVisibility) {
58
+ if (lighting.enabled == 0) {return vec3(0.0);}
59
+ vec3 irradiance = lighting.ambientColor;
60
+ for (int index = 0; index < lighting.directionalLightCount; index++) {
61
+ DirectionalLight light = lighting_getDirectionalLight(index);
62
+ irradiance += light.color * max(dot(normal, -normalize(light.direction)), 0.0) * (index == 0 ? directionalVisibility : 1.0);
63
+ }
64
+ for (int index = 0; index < lighting.pointLightCount; index++) {
65
+ PointLight light = lighting_getPointLight(index);
66
+ vec3 offset = light.position - position;
67
+ irradiance += light.color * max(dot(normal, normalize(offset)), 0.0) * (index < 4 ? pointVisibility[index] : 1.0) / max(getPointLightAttenuation(light, length(offset)), 0.0001);
68
+ }
69
+ for (int index = 0; index < lighting.spotLightCount; index++) {
70
+ SpotLight light = lighting_getSpotLight(index);
71
+ irradiance += light.color * max(dot(normal, normalize(light.position - position)), 0.0) * (index < 4 ? spotVisibility[index] : 1.0) / max(getSpotLightAttenuation(light, position), 0.0001);
72
+ }
73
+ return irradiance;
74
+ }
75
+ `
76
+ } as const satisfies ShaderModule<Partial<LightmapBakeUniforms>, LightmapBakeUniforms>;
@@ -12,22 +12,22 @@ import {
12
12
 
13
13
  /** Information extracted from a VariableShaderType constant */
14
14
  export type VariableShaderTypeInfo = {
15
- type: PrimitiveDataType;
16
- components: number;
15
+ readonly type: PrimitiveDataType;
16
+ readonly components: number;
17
17
  };
18
18
 
19
19
  /** Information extracted from a AttributeShaderType constant */
20
20
  export type AttributeShaderTypeInfo = {
21
21
  /** WGSL-style primitive data type, f32, i32, u32 */
22
- primitiveType: PrimitiveDataType;
22
+ readonly primitiveType: PrimitiveDataType;
23
23
  /** Whether this is a normalized integer (that must be used as float) */
24
- components: 1 | 2 | 3 | 4;
24
+ readonly components: 1 | 2 | 3 | 4;
25
25
  /** Length in bytes of the data for one vertex */
26
- byteLength?: number;
26
+ readonly byteLength?: number;
27
27
  /** Whether this is for integer or float vert */
28
- integer: boolean;
28
+ readonly integer: boolean;
29
29
  /** Whether this data type is signed */
30
- signed: boolean;
30
+ readonly signed: boolean;
31
31
  };
32
32
 
33
33
  /** Split a uniform type string into type and components */
@@ -42,11 +42,17 @@ export function getVariableShaderTypeInfo(
42
42
  return decoded;
43
43
  }
44
44
 
45
+ const attributeShaderTypeInfoCache = new Map<AttributeShaderType, AttributeShaderTypeInfo>();
46
+
45
47
  /** Decodes a vertex type, returning byte length and flags (integer, signed, normalized) */
46
48
  export function getAttributeShaderTypeInfo(
47
49
  attributeType: AttributeShaderType | AttributeShaderTypeAlias
48
50
  ): AttributeShaderTypeInfo {
49
51
  const resolvedAttributeType = resolveAttributeShaderTypeAlias(attributeType);
52
+ const cached = attributeShaderTypeInfoCache.get(resolvedAttributeType);
53
+ if (cached) {
54
+ return cached;
55
+ }
50
56
  const decoded = TYPE_INFO[resolvedAttributeType];
51
57
  if (!decoded) {
52
58
  throw new Error(`Unsupported attribute shader type: ${attributeType}`);
@@ -56,13 +62,15 @@ export function getAttributeShaderTypeInfo(
56
62
  const signed: boolean = primitiveType !== 'u32';
57
63
 
58
64
  const byteLength = PRIMITIVE_TYPE_SIZES[primitiveType] * components;
59
- return {
65
+ const result = Object.freeze({
60
66
  primitiveType,
61
67
  components,
62
68
  byteLength,
63
69
  integer,
64
70
  signed
65
- };
71
+ });
72
+ attributeShaderTypeInfoCache.set(resolvedAttributeType, result);
73
+ return result;
66
74
  }
67
75
 
68
76
  export class ShaderTypeDecoder {
@@ -211,6 +219,11 @@ const UNIFORM_FORMATS: Record<VariableShaderType, {type: PrimitiveDataType; comp
211
219
  'mat4x4<u32>': {type: 'u32', components: 16}
212
220
  };
213
221
 
222
+ // Variable metadata is already allocated in the table; freeze it once, without another cache.
223
+ for (const info of Object.values(UNIFORM_FORMATS)) {
224
+ Object.freeze(info);
225
+ }
226
+
214
227
  /** Predeclared aliases @see https://www.w3.org/TR/WGSL/#vector-types */
215
228
  export const WGSL_ATTRIBUTE_TYPE_ALIAS_MAP: Record<AttributeShaderTypeAlias, AttributeShaderType> =
216
229
  {