@@ -72,6 +72,8 @@ type InstancingUniformType = {
7272const instancingUniforms = ( context : Context ) : InstancingUniformType => ( {
7373 'u_instanceID' : new Uniform1i ( context ) } ) ;
7474
75+ type UniformGroupKey = 'terrainUniforms' | 'globeUniforms' | 'fogUniforms' | 'cutoffUniforms' | 'lightsUniforms' | 'shadowUniforms' ;
76+
7577class 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 > ,
0 commit comments