#nullable enable using System; using System.Diagnostics.CodeAnalysis; using T3.Core.DataTypes; using T3.Core.Operator.Slots; // ReSharper disable ConvertToLocalFunction namespace T3.Core.Resource; /// /// Creates or loads shaders as "resources" and handles their filehooks, compilation, etc /// Could do with some simplification - perhaps their arguments should be condensed into a struct? /// public static partial class ResourceManager { public static Resource CreateShaderResource(string relativePath, IResourceConsumer? instance, Func getEntryPoint, Action? onShaderCompiled = null) where TShader : AbstractShader { ArgumentNullException.ThrowIfNull(getEntryPoint, nameof(getEntryPoint)); TryGenerate func = (FileResource fileResource, TShader? currentValue, [NotNullWhen(true)]out TShader? newShader, [NotNullWhen(false)]out string? reason) => { var success = ShaderCompiling.ShaderCompiler.TryGetShaderFromFile(fileResource, ref currentValue, instance, out reason, getEntryPoint()); newShader = currentValue; onShaderCompiled?.Invoke(currentValue); return success; }; return new Resource(relativePath, instance, func); } internal static Resource CreateShaderResource(InputSlot sourceSlot, Func getEntryPoint) where TShader : AbstractShader { ArgumentNullException.ThrowIfNull(getEntryPoint, nameof(getEntryPoint)); var instance = sourceSlot.Parent; TryGenerate func = (FileResource fileResource, TShader? currentValue, [NotNullWhen(true)]out TShader? newShader, [NotNullWhen(false)]out string? reason) => { var success = ShaderCompiling.ShaderCompiler.TryGetShaderFromFile(fileResource, ref currentValue, instance, out reason, getEntryPoint()); newShader = currentValue; return success; }; return new Resource(sourceSlot, func); } }