11using System ;
2+ using System . Collections . Generic ;
23using System . Reflection ;
34using System . Runtime . CompilerServices ;
4- using UnityEditor . LightBaking ;
55
66namespace UnityEngine . Rendering
77{
88 partial class AdaptiveProbeVolumes
99 {
1010 // This class is used to (1) access the internal class UnityEditor.LightBaking.BakePipelineDriver and (2) provide a slightly higher level API.
11- private sealed class BakePipelineDriver : IDisposable
11+ sealed class BakePipelineDriver : IDisposable
1212 {
13- // Keep in sync with the enum BakePipeline::Run::StageName
14- public enum StageName : int
15- {
16- Initialized ,
17- Preprocess ,
18- Bake ,
19- PostProcess ,
20- AdditionalBake ,
21- Done
22- }
23-
24- private readonly object _bakePipelineDriver ;
25- private readonly Type _bakePipelineDriverType ;
13+ readonly object m_BakePipelineDriver ;
14+ readonly Type m_BakePipelineDriverType ;
15+ readonly Type m_StageNameType ;
2616
2717 internal BakePipelineDriver ( )
2818 {
29- _bakePipelineDriverType = Type . GetType ( "UnityEditor.LightBaking.BakePipelineDriver, UnityEditor" ) ;
30- bool newed = _bakePipelineDriverType != null ;
31- Debug . Assert ( newed , "Unexpected, could not find the type UnityEditor.LightBaking.BakePipelineDriver" ) ;
32- _bakePipelineDriver = newed ? Activator . CreateInstance ( _bakePipelineDriverType ) : null ;
33- Debug . Assert ( _bakePipelineDriver != null , "Unexpected, could not new up a BakePipelineDriver" ) ;
19+ Debug . Assert ( UnityEditorInternal . InternalEditorUtility . CurrentThreadIsMainThread ( ) ) ;
20+
21+ m_BakePipelineDriverType = Type . GetType ( "UnityEditor.LightBaking.BakePipelineDriver, UnityEditor" ) ;
22+ Debug . Assert ( m_BakePipelineDriverType != null , "Unexpected, could not find the type UnityEditor.LightBaking.BakePipelineDriver" ) ;
23+ m_StageNameType = m_BakePipelineDriverType . GetNestedType ( "StageName" , BindingFlags . NonPublic | BindingFlags . Public ) ;
24+ Debug . Assert ( m_StageNameType is { IsEnum : true } , "Unexpected, could not find the nested enum StageName on BakePipelineDriver" ) ;
25+ Debug . Assert ( IsStageNameEnumConsistent ( m_StageNameType ) , "Unexpected, StageName enum is not consistent with BakePipelineDriver.StageName enum" ) ;
26+ m_BakePipelineDriver = Activator . CreateInstance ( m_BakePipelineDriverType , nonPublic : true ) ;
27+ Debug . Assert ( m_BakePipelineDriver != null , "Unexpected, could not new up a BakePipelineDriver" ) ;
3428 }
3529
3630 internal void StartBake ( bool enablePatching , ref float progress , ref StageName stage )
@@ -51,36 +45,78 @@ internal bool RunInProgress()
5145 internal void Step ( ref float progress , ref StageName stage ) =>
5246 Update ( true , true , true , out progress , out stage ) ;
5347
54- private void SetEnableBakedLightmaps ( bool enable ) =>
48+ void SetEnableBakedLightmaps ( bool enable ) =>
5549 InvokeMethod ( new object [ ] { enable } , out _ ) ;
5650
57- private void SetEnablePatching ( bool enable ) =>
51+ void SetEnablePatching ( bool enable ) =>
5852 InvokeMethod ( new object [ ] { enable } , out _ ) ;
5953
60- private void Update ( bool isOnDemandBakeInProgress , bool isOnDemandBakeAsync , bool shouldBeRunning ,
54+ void Update ( bool isOnDemandBakeInProgress , bool isOnDemandBakeAsync , bool shouldBeRunning ,
6155 out float progress , out StageName stage )
6256 {
63- object [ ] parameters = { isOnDemandBakeInProgress , isOnDemandBakeAsync , shouldBeRunning , - 1.0f , - 1 } ;
57+ progress = - 1.0f ;
58+ stage = StageName . Invalid ;
59+ object [ ] parameters = { isOnDemandBakeInProgress , isOnDemandBakeAsync , shouldBeRunning , progress ,
60+ Enum . ToObject ( m_StageNameType , ( int ) stage ) } ;
6461 InvokeMethod ( parameters , out _ ) ;
6562 progress = ( float ) parameters [ 3 ] ;
66- stage = ( StageName ) parameters [ 4 ] ;
63+ stage = ( StageName ) Convert . ToInt32 ( parameters [ 4 ] ) ;
6764 }
6865
6966 public void Dispose ( ) =>
7067 InvokeMethod ( new object [ ] { } , out _ ) ;
7168
72- private bool InvokeMethod ( object [ ] parameters , out object result , [ CallerMemberName ] string methodName = "" )
69+ bool InvokeMethod ( object [ ] parameters , out object result , [ CallerMemberName ] string methodName = "" )
7370 {
74- MethodInfo methodInfo = _bakePipelineDriverType . GetMethod ( methodName , BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ) ;
71+ MethodInfo methodInfo = m_BakePipelineDriverType . GetMethod ( methodName , BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ) ;
7572 bool gotMethod = methodInfo != null ;
7673 Debug . Assert ( gotMethod , $ "Unexpected, could not find { methodName } on BakePipelineDriver") ;
77- if ( ! gotMethod )
78- {
79- result = null ;
74+
75+ result = methodInfo . Invoke ( m_BakePipelineDriver , parameters ) ;
76+
77+ return true ;
78+ }
79+
80+ // Keep this in sync with the enum in Editor\Src\GI\BakePipeline\BakePipeline.bindings.h
81+ public enum StageName
82+ {
83+ Invalid = - 1 ,
84+ Initialized = 0 ,
85+ Preprocess = 1 ,
86+ PreprocessProbes = 2 ,
87+ Bake = 3 ,
88+ PostProcess = 4 ,
89+ AdditionalBake = 5 ,
90+ Done = 6
91+ }
92+
93+ // If StageName is not kept in sync, this should return false
94+ static bool IsStageNameEnumConsistent ( Type otherType )
95+ {
96+ string [ ] ourNames = Enum . GetNames ( typeof ( StageName ) ) ;
97+ string [ ] otherNames = Enum . GetNames ( otherType ) ;
98+
99+ if ( ourNames . Length != otherNames . Length )
80100 return false ;
81- }
82101
83- result = methodInfo . Invoke ( _bakePipelineDriver , parameters ) ;
102+ Array ourValues = Enum . GetValues ( typeof ( StageName ) ) ;
103+ Array otherValues = Enum . GetValues ( otherType ) ;
104+
105+ // Brute-force compare each local name against the external by lookup
106+ for ( int i = 0 ; i < ourNames . Length ; i ++ )
107+ {
108+ string name = ourNames [ i ] ;
109+
110+ int otherIndex = Array . IndexOf ( otherNames , name ) ;
111+ if ( otherIndex < 0 )
112+ return false ;
113+
114+ int ourVal = Convert . ToInt32 ( ourValues . GetValue ( i ) ) ;
115+ int otherVal = Convert . ToInt32 ( otherValues . GetValue ( otherIndex ) ) ;
116+
117+ if ( ourVal != otherVal )
118+ return false ;
119+ }
84120
85121 return true ;
86122 }
0 commit comments