| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Providers; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using Environment = DCL.Environment; |
| | 7 | |
|
| | 8 | | namespace MainScripts.DCL.Controllers.ShaderPrewarm |
| | 9 | | { |
| | 10 | | public class ShaderPrewarm : IShaderPrewarm |
| | 11 | | { |
| | 12 | | private const string SHADER_VARIANTS_ASSET = "ShaderVariantsData"; |
| | 13 | |
|
| | 14 | | private readonly IAddressableResourceProvider addressables; |
| | 15 | | private bool areShadersPrewarm; |
| | 16 | |
|
| 0 | 17 | | public ShaderPrewarm(IAddressableResourceProvider addressables) |
| | 18 | | { |
| 0 | 19 | | this.addressables = addressables; |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | public async UniTask PrewarmAsync(Action<float> progressCallback, CancellationToken cancellationToken) |
| | 23 | | { |
| 0 | 24 | | if (areShadersPrewarm) return; |
| | 25 | |
|
| 0 | 26 | | await UniTask.Yield(); |
| | 27 | |
|
| 0 | 28 | | var variantsData = await addressables.GetAddressable<ShaderVariantsData>(SHADER_VARIANTS_ASSET, cancellation |
| | 29 | |
|
| 0 | 30 | | int length = variantsData.collections.Length; |
| | 31 | |
|
| 0 | 32 | | for (var i = 0; i < length; i++) |
| | 33 | | { |
| 0 | 34 | | ShaderVariantCollection collection = variantsData.collections[i]; |
| | 35 | |
|
| 0 | 36 | | progressCallback.Invoke(i / (float)length); |
| 0 | 37 | | await UniTask.Yield(PlayerLoopTiming.PostLateUpdate, cancellationToken); |
| | 38 | |
|
| 0 | 39 | | collection.WarmUp(); |
| 0 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | areShadersPrewarm = true; |
| 0 | 43 | | } |
| | 44 | | } |
| | 45 | | } |