/
EffectSystem.cs
416 lines (350 loc) · 16.7 KB
/
EffectSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Stride.Core;
using Stride.Core.Diagnostics;
using Stride.Core.IO;
using Stride.Core.ReferenceCounting;
using Stride.Games;
using Stride.Graphics;
using Stride.Shaders;
using Stride.Shaders.Compiler;
namespace Stride.Rendering
{
/// <summary>
/// The effect system.
/// </summary>
public class EffectSystem : GameSystemBase
{
private static readonly Logger Log = GlobalLogger.GetLogger("EffectSystem");
private EffectCompilerParameters effectCompilerParameters = EffectCompilerParameters.Default;
private EffectCompilerBase compiler;
private readonly Dictionary<string, List<CompilerResults>> earlyCompilerCache = new Dictionary<string, List<CompilerResults>>();
private Dictionary<EffectBytecode, Effect> cachedEffects = new Dictionary<EffectBytecode, Effect>();
#if STRIDE_PLATFORM_WINDOWS_DESKTOP
private DirectoryWatcher directoryWatcher;
#endif
private bool isInitialized;
/// <summary>
/// Called each time a non-cached effect is requested.
/// </summary>
internal Action<EffectCompileRequest, CompilerResults> EffectUsed;
private readonly HashSet<string> recentlyModifiedShaders = new HashSet<string>();
public IEffectCompiler Compiler { get { return compiler; } set { compiler = (EffectCompilerBase)value; } }
/// <summary>
/// Gets or sets the database file provider, to use for loading effects and shader sources.
/// </summary>
/// <value>
/// The database file provider.
/// </value>
public IVirtualFileProvider FileProvider => compiler.FileProvider;
/// <summary>
/// Initializes a new instance of the <see cref="EffectSystem"/> class.
/// </summary>
/// <param name="services">The services.</param>
public EffectSystem(IServiceRegistry services)
: base(services)
{
}
public override void Initialize()
{
base.Initialize();
// Get graphics device service
base.InitGraphicsDeviceService();
#if STRIDE_PLATFORM_WINDOWS_DESKTOP
Enabled = true;
directoryWatcher = new DirectoryWatcher("*.sdsl");
directoryWatcher.Modified += FileModifiedEvent;
// TODO: sdfx too
#endif
isInitialized = true;
}
public void SetCompilationMode(CompilationMode compilationMode)
{
effectCompilerParameters.ApplyCompilationMode(compilationMode);
}
protected override void Destroy()
{
// Mark effect system as destroyed (so that async effect compilation are ignored)
lock (cachedEffects)
{
// Clear effects
foreach (var effect in cachedEffects)
{
effect.Value.ReleaseInternal();
}
cachedEffects.Clear();
// Mark as not initialized anymore
isInitialized = false;
}
#if STRIDE_PLATFORM_WINDOWS_DESKTOP
if (directoryWatcher != null)
{
directoryWatcher.Modified -= FileModifiedEvent;
directoryWatcher.Dispose();
directoryWatcher = null;
}
#endif
Compiler?.Dispose();
Compiler = null;
base.Destroy();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
UpdateEffects();
}
public bool IsValid(Effect effect)
{
lock (cachedEffects)
{
return cachedEffects.ContainsKey(effect.Bytecode);
}
}
/// <summary>
/// Loads the effect.
/// </summary>
/// <param name="effectName">Name of the effect.</param>
/// <param name="compilerParameters">The compiler parameters.</param>
/// <param name="usedParameters">The used parameters.</param>
/// <returns>A new instance of an effect.</returns>
/// <exception cref="System.InvalidOperationException">Could not compile shader. Need fallback.</exception>
public TaskOrResult<Effect> LoadEffect(string effectName, CompilerParameters compilerParameters)
{
if (effectName == null) throw new ArgumentNullException("effectName");
if (compilerParameters == null) throw new ArgumentNullException("compilerParameters");
// Setup compilation parameters
// GraphicsDevice might have been not valid until this point, which is why we compute platform and profile only at this point
compilerParameters.EffectParameters.Platform = GraphicsDevice.Platform;
compilerParameters.EffectParameters.Profile = GraphicsDevice.ShaderProfile ?? GraphicsDevice.Features.RequestedProfile;
// Copy optimization/debug levels
compilerParameters.EffectParameters.OptimizationLevel = effectCompilerParameters.OptimizationLevel;
compilerParameters.EffectParameters.Debug = effectCompilerParameters.Debug;
// Get the compiled result
var compilerResult = GetCompilerResults(effectName, compilerParameters);
CheckResult(compilerResult);
// Only take the sub-effect
var bytecode = compilerResult.Bytecode;
if (bytecode.Task != null && !bytecode.Task.IsCompleted)
{
// Ensure the continuation is scheduled on the thread pool or we might end up in a dead lock when then calling thread
// is already waiting on the result
var result = bytecode.Task.ContinueWith(
x => CreateEffect(effectName, x.Result, compilerResult),
scheduler: TaskScheduler.Default);
return result;
}
else
{
return CreateEffect(effectName, bytecode.WaitForResult(), compilerResult);
}
}
// TODO: THIS IS JUST A WORKAROUND, REMOVE THIS
private static void CheckResult(LoggerResult compilerResult)
{
// Check errors
if (compilerResult.HasErrors)
{
throw new InvalidOperationException("Could not compile shader. See error messages." + compilerResult.ToText());
}
}
private Effect CreateEffect(string effectName, EffectBytecodeCompilerResult effectBytecodeCompilerResult, CompilerResults compilerResult)
{
Effect effect;
lock (cachedEffects)
{
if (!isInitialized)
throw new ObjectDisposedException(nameof(EffectSystem), "EffectSystem has been disposed. This Effect compilation has been cancelled.");
if (effectBytecodeCompilerResult.CompilationLog.HasErrors)
{
// Unregister result (or should we keep it so that failure never change?)
List<CompilerResults> effectCompilerResults;
if (earlyCompilerCache.TryGetValue(effectName, out effectCompilerResults))
{
effectCompilerResults.Remove(compilerResult);
}
}
CheckResult(effectBytecodeCompilerResult.CompilationLog);
var bytecode = effectBytecodeCompilerResult.Bytecode;
if (bytecode == null)
throw new InvalidOperationException("EffectCompiler returned no shader and no compilation error.");
if (!cachedEffects.TryGetValue(bytecode, out effect))
{
effect = new Effect(GraphicsDevice, bytecode) { Name = effectName };
cachedEffects.Add(bytecode, effect);
#if STRIDE_PLATFORM_WINDOWS_DESKTOP
foreach (var type in bytecode.HashSources.Keys)
{
var storagePath = EffectCompilerBase.GetStoragePathFromShaderType(type);
if (!FileProvider.TryGetFileLocation(storagePath, out var filePath, out _, out _))
{
// TODO: the "/path" is hardcoded, used in ImportStreamCommand and ShaderSourceManager. Find a place to share this correctly.
var pathUrl = storagePath + "/path";
if (FileProvider.FileExists(pathUrl))
{
using (var pathStream = FileProvider.OpenStream(pathUrl, VirtualFileMode.Open, VirtualFileAccess.Read))
using (var reader = new StreamReader(pathStream))
{
filePath = reader.ReadToEnd();
}
}
}
if (filePath != null)
directoryWatcher.Track(filePath);
}
#endif
}
}
return effect;
}
private CompilerResults GetCompilerResults(string effectName, CompilerParameters compilerParameters)
{
// Compile shader
var isXkfx = ShaderMixinManager.Contains(effectName);
// getting the effect from the used parameters only makes sense when the source files are the same
// TODO: improve this by updating earlyCompilerCache - cache can still be relevant
CompilerResults compilerResult = null;
if (isXkfx)
{
// perform an early test only based on the parameters
compilerResult = GetShaderFromParameters(effectName, compilerParameters);
}
if (compilerResult == null)
{
var source = isXkfx ? new ShaderMixinGeneratorSource(effectName) : (ShaderSource)new ShaderClassSource(effectName);
compilerResult = compiler.Compile(source, compilerParameters);
EffectUsed?.Invoke(new EffectCompileRequest(effectName, new CompilerParameters(compilerParameters)), compilerResult);
if (!compilerResult.HasErrors && isXkfx)
{
lock (earlyCompilerCache)
{
List<CompilerResults> effectCompilerResults;
if (!earlyCompilerCache.TryGetValue(effectName, out effectCompilerResults))
{
effectCompilerResults = new List<CompilerResults>();
earlyCompilerCache.Add(effectName, effectCompilerResults);
}
// Register bytecode used parameters so that they are checked when another effect is instanced
effectCompilerResults.Add(compilerResult);
}
}
}
foreach (var message in compilerResult.Messages)
{
Log.Log(message);
}
return compilerResult;
}
private void UpdateEffects()
{
lock (recentlyModifiedShaders)
{
if (recentlyModifiedShaders.Count == 0)
{
return;
}
// Clear cache for recently modified shaders
compiler.ResetCache(recentlyModifiedShaders);
var bytecodeRemoved = new List<EffectBytecode>();
lock (cachedEffects)
{
foreach (var shaderSourceName in recentlyModifiedShaders)
{
// TODO: cache keys in a HashSet instead of ToHashSet
var bytecodes = new HashSet<EffectBytecode>(cachedEffects.Keys);
foreach (var bytecode in bytecodes)
{
if (bytecode.HashSources.ContainsKey(shaderSourceName))
{
bytecodeRemoved.Add(bytecode);
// Dispose previous effect
var effect = cachedEffects[bytecode];
//todo should be reference counted instead of disposed
effect.Dispose();
effect.SourceChanged = true;
// Remove effect from cache
cachedEffects.Remove(bytecode);
}
}
}
}
lock (earlyCompilerCache)
{
foreach (var effectCompilerResults in earlyCompilerCache.Values)
{
foreach (var bytecode in bytecodeRemoved)
{
effectCompilerResults.RemoveAll(results => results.Bytecode.GetCurrentResult().Bytecode == bytecode);
}
}
}
recentlyModifiedShaders.Clear();
}
}
private void FileModifiedEvent(object sender, FileEvent e)
{
if (e.ChangeType == FileEventChangeType.Changed || e.ChangeType == FileEventChangeType.Renamed)
{
lock (recentlyModifiedShaders)
{
recentlyModifiedShaders.Add(Path.GetFileNameWithoutExtension(e.Name));
}
}
}
/// <summary>
/// Get the shader from the database based on the parameters used for its compilation.
/// </summary>
/// <param name="effectName">Name of the effect.</param>
/// <param name="parameters">The parameters.</param>
/// <returns>The EffectBytecode if found.</returns>
protected CompilerResults GetShaderFromParameters(string effectName, CompilerParameters parameters)
{
lock (earlyCompilerCache)
{
List<CompilerResults> compilerResultsList;
if (!earlyCompilerCache.TryGetValue(effectName, out compilerResultsList))
return null;
// Compiler Parameters are supposed to be created in the same order every time, so we just check if they were created in the same order (ParameterKeyInfos) with same values (ObjectValues)
// TODO GRAPHICS REFACTOR we could probably compute a hash for faster lookup
foreach (var compiledResults in compilerResultsList)
{
var compiledParameters = compiledResults.SourceParameters;
var compiledParameterKeyInfos = compiledParameters.ParameterKeyInfos;
var parameterKeyInfos = parameters.ParameterKeyInfos;
// Early check
if (parameterKeyInfos.Count != compiledParameterKeyInfos.Count)
continue;
for (int index = 0; index < parameterKeyInfos.Count; ++index)
{
var parameterKeyInfo = parameterKeyInfos[index];
var compiledParameterKeyInfo = compiledParameterKeyInfos[index];
if (parameterKeyInfo != compiledParameterKeyInfo)
goto different;
// Should not happen in practice (CompilerParameters should only consist of permutation values)
if (parameterKeyInfo.Key.Type != ParameterKeyType.Permutation)
continue;
for (int i = 0; i < parameterKeyInfo.Count; ++i)
{
var object1 = parameters.ObjectValues[parameterKeyInfo.BindingSlot + i];
var object2 = compiledParameters.ObjectValues[compiledParameterKeyInfo.BindingSlot + i];
if (object1 == null && object2 == null)
continue;
if ((object1 == null && object2 != null) || (object2 == null && object1 != null))
goto different;
if (!object1.Equals(object2))
goto different;
}
}
return compiledResults;
different:
;
}
}
return null;
}
}
}