Skip to content

Commit 066cb33

Browse files
mournergithub-actions[bot]
authored andcommitted
Minor simplifications in style & program
GitOrigin-RevId: cb13ab9c509778968bfeb7099fdf1b4b944a75cd
1 parent e135823 commit 066cb33

2 files changed

Lines changed: 45 additions & 123 deletions

File tree

src/render/program.ts

Lines changed: 26 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type InstancingUniformType = {
7272
const instancingUniforms = (context: Context): InstancingUniformType => ({
7373
'u_instanceID': new Uniform1i(context)});
7474

75+
type UniformGroupKey = 'terrainUniforms' | 'globeUniforms' | 'fogUniforms' | 'cutoffUniforms' | 'lightsUniforms' | 'shadowUniforms';
76+
7577
class Program<Us extends UniformBindings> {
7678
program: WebGLProgram;
7779
attributes: Record<string, number>;
@@ -80,20 +82,20 @@ class Program<Us extends UniformBindings> {
8082
fixedUniformsEntries: ReadonlyArray<[string, IUniform<unknown>]>;
8183
binderUniforms: Array<BinderUniform>;
8284
failedToCreate: boolean;
83-
terrainUniforms: TerrainUniformsType | null | undefined;
84-
fogUniforms: FogUniformsType | null | undefined;
85-
cutoffUniforms: CutoffUniformsType | null | undefined;
86-
lightsUniforms: LightsUniformsType | null | undefined;
87-
globeUniforms: GlobeUniformsType | null | undefined;
88-
shadowUniforms: ShadowUniformsType | null | undefined;
85+
terrainUniforms: TerrainUniformsType | undefined;
86+
fogUniforms: FogUniformsType | undefined;
87+
cutoffUniforms: CutoffUniformsType | undefined;
88+
lightsUniforms: LightsUniformsType | undefined;
89+
globeUniforms: GlobeUniformsType | undefined;
90+
shadowUniforms: ShadowUniformsType | undefined;
8991

9092
name: ProgramName;
9193
configuration: ProgramConfiguration | null | undefined;
9294
fixedDefines: ReadonlyArray<DynamicDefinesType>;
9395

9496
// Manually handle instancing by issuing draw calls and replacing gl_InstanceID with uniform
9597
forceManualRenderingForInstanceIDShaders: boolean;
96-
instancingUniforms: InstancingUniformType | null | undefined;
98+
instancingUniforms: InstancingUniformType | undefined;
9799

98100
_pending: boolean;
99101
_context: Context;
@@ -319,91 +321,42 @@ class Program<Us extends UniformBindings> {
319321
return location;
320322
}
321323

322-
setTerrainUniformValues(context: Context, terrainUniformValues: UniformValues<TerrainUniformsType>) {
324+
// Reads the uniform binding via `key` rather than taking it as an argument, because
325+
// the binding is created lazily in _finalize() — it must be read after _ensureReady().
326+
_setUniformGroup(context: Context, key: UniformGroupKey, values: UniformValues<UniformBindings>) {
323327
this._ensureReady();
324-
if (!this.terrainUniforms) return;
325-
const uniforms: TerrainUniformsType = this.terrainUniforms;
326-
327-
if (this.failedToCreate) return;
328+
const uniforms = this[key] as UniformBindings | undefined;
329+
if (this.failedToCreate || !uniforms) return;
328330
context.program.set(this.program);
329331

330-
for (const name in terrainUniformValues) {
331-
if (uniforms[name]) {
332-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
333-
uniforms[name].set(this.program, name, terrainUniformValues[name]);
334-
}
332+
for (const name in values) {
333+
const uniform = uniforms[name];
334+
if (uniform) uniform.set(this.program, name, values[name]);
335335
}
336336
}
337337

338-
setGlobeUniformValues(context: Context, globeUniformValues: UniformValues<GlobeUniformsType>) {
339-
this._ensureReady();
340-
if (!this.globeUniforms) return;
341-
const uniforms: GlobeUniformsType = this.globeUniforms;
342-
343-
if (this.failedToCreate) return;
344-
context.program.set(this.program);
338+
setTerrainUniformValues(context: Context, terrainUniformValues: UniformValues<TerrainUniformsType>) {
339+
this._setUniformGroup(context, 'terrainUniforms', terrainUniformValues);
340+
}
345341

346-
for (const name in globeUniformValues) {
347-
if (uniforms[name]) {
348-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
349-
uniforms[name].set(this.program, name, globeUniformValues[name]);
350-
}
351-
}
342+
setGlobeUniformValues(context: Context, globeUniformValues: UniformValues<GlobeUniformsType>) {
343+
this._setUniformGroup(context, 'globeUniforms', globeUniformValues);
352344
}
353345

354346
setFogUniformValues(context: Context, fogUniformValues: UniformValues<FogUniformsType>) {
355-
this._ensureReady();
356-
if (!this.fogUniforms) return;
357-
const uniforms: FogUniformsType = this.fogUniforms;
358-
359-
if (this.failedToCreate) return;
360-
context.program.set(this.program);
361-
362-
for (const name in fogUniformValues) {
363-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
364-
uniforms[name].set(this.program, name, fogUniformValues[name]);
365-
}
347+
this._setUniformGroup(context, 'fogUniforms', fogUniformValues);
366348
}
367349

368350
setCutoffUniformValues(context: Context, cutoffUniformValues: UniformValues<CutoffUniformsType>) {
369-
this._ensureReady();
370-
if (!this.cutoffUniforms) return;
371-
const uniforms: CutoffUniformsType = this.cutoffUniforms;
372-
373-
if (this.failedToCreate) return;
374-
context.program.set(this.program);
375-
376-
for (const name in cutoffUniformValues) {
377-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
378-
uniforms[name].set(this.program, name, cutoffUniformValues[name]);
379-
}
351+
this._setUniformGroup(context, 'cutoffUniforms', cutoffUniformValues);
380352
}
381353

382354
setLightsUniformValues(context: Context, lightsUniformValues: UniformValues<LightsUniformsType>) {
383-
this._ensureReady();
384-
if (!this.lightsUniforms) return;
385-
const uniforms: LightsUniformsType = this.lightsUniforms;
386-
387-
if (this.failedToCreate) return;
388-
context.program.set(this.program);
389-
390-
for (const name in lightsUniformValues) {
391-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
392-
uniforms[name].set(this.program, name, lightsUniformValues[name]);
393-
}
355+
this._setUniformGroup(context, 'lightsUniforms', lightsUniformValues);
394356
}
395357

396358
setShadowUniformValues(context: Context, shadowUniformValues: UniformValues<ShadowUniformsType>) {
397-
this._ensureReady();
398-
if (this.failedToCreate || !this.shadowUniforms) return;
399-
400-
const uniforms: ShadowUniformsType = this.shadowUniforms;
401-
context.program.set(this.program);
402-
403-
for (const name in shadowUniformValues) {
404-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
405-
uniforms[name].set(this.program, name, shadowUniformValues[name]);
406-
}
359+
this._setUniformGroup(context, 'shadowUniforms', shadowUniformValues);
407360
}
408361

409362
_drawDebugWireframe(painter: Painter, depthMode: Readonly<DepthMode>,

src/style/style.ts

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,13 +2611,11 @@ class Style extends Evented<MapEvents> {
26112611
}
26122612

26132613
getOwnSources(): Source[] {
2614-
const sources = [];
2614+
const sources: Source[] = [];
26152615
for (const id in this._otherSourceCaches) {
26162616
const sourceCache = this.getOwnSourceCache(id);
26172617
if (sourceCache) sources.push(sourceCache.getSource());
26182618
}
2619-
2620-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
26212619
return sources;
26222620
}
26232621

@@ -2736,14 +2734,13 @@ class Style extends Evented<MapEvents> {
27362734

27372735
getLights(): Array<LightsSpecification> | null | undefined {
27382736
if (!this.enable3dLights()) return null;
2739-
const lights = [];
2737+
const lights: LightsSpecification[] = [];
27402738
if (this.directionalLight) {
27412739
lights.push(this.directionalLight.get());
27422740
}
27432741
if (this.ambientLight) {
27442742
lights.push(this.ambientLight.get());
27452743
}
2746-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
27472744
return lights;
27482745
}
27492746

@@ -2840,13 +2837,11 @@ class Style extends Evented<MapEvents> {
28402837
return [];
28412838
}
28422839

2843-
const layers = [];
2840+
const layers: TypedStyleLayer[] = [];
28442841
for (const selector of featuresets[featuresetId].selectors) {
28452842
const layer = style.getOwnLayer(selector.layer);
28462843
if (layer) layers.push(layer);
28472844
}
2848-
2849-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
28502845
return layers;
28512846
}
28522847

@@ -3903,7 +3898,7 @@ class Style extends Evented<MapEvents> {
39033898

39043899
const features = this.queryRenderedTargets(queryGeometry, targets, transform);
39053900

3906-
const targetFeatures = [];
3901+
const targetFeatures: TargetFeature[] = [];
39073902
const uniqueFeatureSet = new Set<string>();
39083903
for (const feature of features) {
39093904
for (const variant of feature.variants[targetId]) {
@@ -3913,8 +3908,6 @@ class Style extends Evented<MapEvents> {
39133908
targetFeatures.push(new TargetFeature(feature, variant));
39143909
}
39153910
}
3916-
3917-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
39183911
return targetFeatures;
39193912
}
39203913

@@ -4938,13 +4931,11 @@ class Style extends Evented<MapEvents> {
49384931
}
49394932

49404933
getSources(): Source[] {
4941-
const sources = [];
4934+
const sources: Source[] = [];
49424935
for (const id in this._mergedOtherSourceCaches) {
49434936
const sourceCache = this._mergedOtherSourceCaches[id];
49444937
if (sourceCache) sources.push(sourceCache.getSource());
49454938
}
4946-
4947-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
49484939
return sources;
49494940
}
49504941

@@ -4991,24 +4982,13 @@ class Style extends Evented<MapEvents> {
49914982
if (fqid == null)
49924983
return Object.values(this._mergedSourceCaches);
49934984

4994-
const sourceCaches = [];
4995-
if (this._mergedOtherSourceCaches[fqid]) {
4996-
sourceCaches.push(this._mergedOtherSourceCaches[fqid]);
4997-
}
4998-
if (this._mergedSymbolSourceCaches[fqid]) {
4999-
sourceCaches.push(this._mergedSymbolSourceCaches[fqid]);
5000-
}
5001-
if (this._mergedFillExtrusionSourceCaches[fqid]) {
5002-
sourceCaches.push(this._mergedFillExtrusionSourceCaches[fqid]);
5003-
}
5004-
if (this._mergedHdRoadCoverageSourceCaches[fqid]) {
5005-
sourceCaches.push(this._mergedHdRoadCoverageSourceCaches[fqid]);
5006-
}
5007-
if (this._mergedHdRoadElevationSourceCaches[fqid]) {
5008-
sourceCaches.push(this._mergedHdRoadElevationSourceCaches[fqid]);
5009-
}
5010-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
5011-
return sourceCaches;
4985+
return [
4986+
this._mergedOtherSourceCaches[fqid],
4987+
this._mergedSymbolSourceCaches[fqid],
4988+
this._mergedFillExtrusionSourceCaches[fqid],
4989+
this._mergedHdRoadCoverageSourceCaches[fqid],
4990+
this._mergedHdRoadElevationSourceCaches[fqid],
4991+
].filter(Boolean);
50124992
}
50134993

50144994
updateSourceCaches() {
@@ -5052,24 +5032,13 @@ class Style extends Evented<MapEvents> {
50525032
}
50535033

50545034
getOwnSourceCaches(source: string): Array<SourceCache> {
5055-
const sourceCaches = [];
5056-
if (this._otherSourceCaches[source]) {
5057-
sourceCaches.push(this._otherSourceCaches[source]);
5058-
}
5059-
if (this._symbolSourceCaches[source]) {
5060-
sourceCaches.push(this._symbolSourceCaches[source]);
5061-
}
5062-
if (this._fillExtrusionSourceCaches[source]) {
5063-
sourceCaches.push(this._fillExtrusionSourceCaches[source]);
5064-
}
5065-
if (this._hdCoverage && this._hdCoverage.coverageSourceCaches[source]) {
5066-
sourceCaches.push(this._hdCoverage.coverageSourceCaches[source]);
5067-
}
5068-
if (this._hdElevation && this._hdElevation.elevationSourceCaches[makeFQID(source, this.scope)]) {
5069-
sourceCaches.push(this._hdElevation.elevationSourceCaches[makeFQID(source, this.scope)]);
5070-
}
5071-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
5072-
return sourceCaches;
5035+
return [
5036+
this._otherSourceCaches[source],
5037+
this._symbolSourceCaches[source],
5038+
this._fillExtrusionSourceCaches[source],
5039+
this._hdCoverage && this._hdCoverage.coverageSourceCaches[source],
5040+
this._hdElevation && this._hdElevation.elevationSourceCaches[makeFQID(source, this.scope)],
5041+
].filter(Boolean);
50735042
}
50745043

50755044
// Returns true if HD module isn't loaded yet and the layer needs a coverage source

0 commit comments

Comments
 (0)