// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; namespace osu.Framework.Graphics.Shaders { /// /// A mapping of a global uniform to many shaders which need to receive updates on a change. /// internal class UniformMapping : IUniformMapping where T : struct, IEquatable { private T val; public T Value { get => val; set { if (value.Equals(val)) return; val = value; for (int i = 0; i < LinkedUniforms.Count; i++) LinkedUniforms[i].UpdateValue(this); } } public List> LinkedUniforms = new List>(); public string Name { get; } public UniformMapping(string name) { Name = name; } public void LinkShaderUniform(IUniform uniform) { var typedUniform = (GlobalUniform)uniform; typedUniform.UpdateValue(this); LinkedUniforms.Add(typedUniform); } public void UnlinkShaderUniform(IUniform uniform) { var typedUniform = (GlobalUniform)uniform; LinkedUniforms.Remove(typedUniform); } public void UpdateValue(ref T newValue) { if (newValue.Equals(val)) return; val = newValue; for (int i = 0; i < LinkedUniforms.Count; i++) LinkedUniforms[i].UpdateValue(this); } public ref T GetValueByRef() => ref val; } }