@@ -51,17 +51,12 @@ public VariantCompiler(List<string> shaderSearchPaths, string outputPath)
51
51
_outputPath = outputPath ;
52
52
}
53
53
54
- public void Compile ( ShaderVariantDescription variant )
54
+ public string [ ] Compile ( ShaderVariantDescription variant )
55
55
{
56
56
if ( variant . Shaders . Length == 1 )
57
57
{
58
- if ( variant . Shaders [ 0 ] . Stage == ShaderStages . Vertex ) { CompileVertexFragment ( variant ) ; }
59
- if ( variant . Shaders [ 0 ] . Stage == ShaderStages . Compute ) { CompileCompute ( variant ) ; }
60
- else
61
- {
62
- throw new SpirvCompilationException (
63
- $ "Variant \" { variant . Name } \" has an unsupported set of shader stages.") ;
64
- }
58
+ if ( variant . Shaders [ 0 ] . Stage == ShaderStages . Vertex ) { return CompileVertexFragment ( variant ) ; }
59
+ if ( variant . Shaders [ 0 ] . Stage == ShaderStages . Compute ) { return CompileCompute ( variant ) ; }
65
60
}
66
61
if ( variant . Shaders . Length == 2 )
67
62
{
@@ -82,12 +77,18 @@ public void Compile(ShaderVariantDescription variant)
82
77
throw new SpirvCompilationException ( $ "Variant \" { variant . Name } \" is missing a fragment shader.") ;
83
78
}
84
79
85
- CompileVertexFragment ( variant ) ;
80
+ return CompileVertexFragment ( variant ) ;
81
+ }
82
+ else
83
+ {
84
+ throw new SpirvCompilationException (
85
+ $ "Variant \" { variant . Name } \" has an unsupported combination of shader stages.") ;
86
86
}
87
87
}
88
88
89
- private void CompileVertexFragment ( ShaderVariantDescription variant )
89
+ private string [ ] CompileVertexFragment ( ShaderVariantDescription variant )
90
90
{
91
+ List < string > generatedFiles = new List < string > ( ) ;
91
92
List < Exception > compilationExceptions = new List < Exception > ( ) ;
92
93
byte [ ] vsBytes = null ;
93
94
byte [ ] fsBytes = null ;
@@ -98,6 +99,9 @@ private void CompileVertexFragment(ShaderVariantDescription variant)
98
99
try
99
100
{
100
101
vsBytes = CompileToSpirv ( variant , vertexFileName , ShaderStages . Vertex ) ;
102
+ string spvPath = Path . Combine ( _outputPath , $ "{ variant . Name } _{ ShaderStages . Vertex . ToString ( ) } .spv") ;
103
+ File . WriteAllBytes ( spvPath , vsBytes ) ;
104
+ generatedFiles . Add ( spvPath ) ;
101
105
}
102
106
catch ( Exception e )
103
107
{
@@ -111,6 +115,9 @@ private void CompileVertexFragment(ShaderVariantDescription variant)
111
115
try
112
116
{
113
117
fsBytes = CompileToSpirv ( variant , fragmentFileName , ShaderStages . Fragment ) ;
118
+ string spvPath = Path . Combine ( _outputPath , $ "{ variant . Name } _{ ShaderStages . Fragment . ToString ( ) } .spv") ;
119
+ File . WriteAllBytes ( spvPath , fsBytes ) ;
120
+ generatedFiles . Add ( spvPath ) ;
114
121
}
115
122
catch ( Exception e )
116
123
{
@@ -138,11 +145,13 @@ private void CompileVertexFragment(ShaderVariantDescription variant)
138
145
{
139
146
string vsPath = Path . Combine ( _outputPath , $ "{ variant . Name } _Vertex.{ GetExtension ( target ) } ") ;
140
147
File . WriteAllText ( vsPath , result . VertexShader ) ;
148
+ generatedFiles . Add ( vsPath ) ;
141
149
}
142
150
if ( result . FragmentShader != null )
143
151
{
144
152
string fsPath = Path . Combine ( _outputPath , $ "{ variant . Name } _Fragment.{ GetExtension ( target ) } ") ;
145
153
File . WriteAllText ( fsPath , result . FragmentShader ) ;
154
+ generatedFiles . Add ( fsPath ) ;
146
155
}
147
156
}
148
157
catch ( Exception e )
@@ -155,6 +164,8 @@ private void CompileVertexFragment(ShaderVariantDescription variant)
155
164
{
156
165
throw new AggregateException ( $ "Errors were encountered when compiling shader variant(s).", compilationExceptions ) ;
157
166
}
167
+
168
+ return generatedFiles . ToArray ( ) ;
158
169
}
159
170
160
171
private string GetExtension ( CrossCompileTarget target )
@@ -186,8 +197,6 @@ private byte[] CompileToSpirv(
186
197
fileName ,
187
198
stage ,
188
199
glslOptions ) ;
189
- string spvPath = Path . Combine ( _outputPath , $ "{ variant . Name } _{ stage . ToString ( ) } .spv") ;
190
- File . WriteAllBytes ( spvPath , result . SpirvBytes ) ;
191
200
return result . SpirvBytes ;
192
201
}
193
202
@@ -212,9 +221,13 @@ private string LoadGlsl(string fileName)
212
221
throw new FileNotFoundException ( $ "Unable to find shader file \" { fileName } \" .") ;
213
222
}
214
223
215
- private void CompileCompute ( ShaderVariantDescription variant )
224
+ private string [ ] CompileCompute ( ShaderVariantDescription variant )
216
225
{
226
+ List < string > generatedFiles = new List < string > ( ) ;
217
227
byte [ ] csBytes = CompileToSpirv ( variant , variant . Shaders [ 0 ] . FileName , ShaderStages . Compute ) ;
228
+ string spvPath = Path . Combine ( _outputPath , $ "{ variant . Name } _{ ShaderStages . Compute . ToString ( ) } .spv") ;
229
+ File . WriteAllBytes ( spvPath , csBytes ) ;
230
+ generatedFiles . Add ( spvPath ) ;
218
231
219
232
List < Exception > compilationExceptions = new List < Exception > ( ) ;
220
233
foreach ( CrossCompileTarget target in variant . Targets )
@@ -224,6 +237,7 @@ private void CompileCompute(ShaderVariantDescription variant)
224
237
ComputeCompilationResult result = SpirvCompilation . CompileCompute ( csBytes , target , variant . CrossCompileOptions ) ;
225
238
string csPath = Path . Combine ( _outputPath , $ "{ variant . Name } _Compute.{ GetExtension ( target ) } ") ;
226
239
File . WriteAllText ( csPath , result . ComputeShader ) ;
240
+ generatedFiles . Add ( csPath ) ;
227
241
}
228
242
catch ( Exception e )
229
243
{
@@ -235,6 +249,8 @@ private void CompileCompute(ShaderVariantDescription variant)
235
249
{
236
250
throw new AggregateException ( $ "Errors were encountered when compiling shader variant(s).", compilationExceptions ) ;
237
251
}
252
+
253
+ return generatedFiles . ToArray ( ) ;
238
254
}
239
255
}
240
256
}
0 commit comments