A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Collections.Generic;
6
7namespace osu.Framework.Graphics.Shaders
8{
9 /// <summary>
10 /// A mapping of a global uniform to many shaders which need to receive updates on a change.
11 /// </summary>
12 internal class UniformMapping<T> : IUniformMapping
13 where T : struct, IEquatable<T>
14 {
15 private T val;
16
17 public T Value
18 {
19 get => val;
20 set
21 {
22 if (value.Equals(val))
23 return;
24
25 val = value;
26
27 for (int i = 0; i < LinkedUniforms.Count; i++)
28 LinkedUniforms[i].UpdateValue(this);
29 }
30 }
31
32 public List<GlobalUniform<T>> LinkedUniforms = new List<GlobalUniform<T>>();
33
34 public string Name { get; }
35
36 public UniformMapping(string name)
37 {
38 Name = name;
39 }
40
41 public void LinkShaderUniform(IUniform uniform)
42 {
43 var typedUniform = (GlobalUniform<T>)uniform;
44
45 typedUniform.UpdateValue(this);
46 LinkedUniforms.Add(typedUniform);
47 }
48
49 public void UnlinkShaderUniform(IUniform uniform)
50 {
51 var typedUniform = (GlobalUniform<T>)uniform;
52 LinkedUniforms.Remove(typedUniform);
53 }
54
55 public void UpdateValue(ref T newValue)
56 {
57 if (newValue.Equals(val))
58 return;
59
60 val = newValue;
61
62 for (int i = 0; i < LinkedUniforms.Count; i++)
63 LinkedUniforms[i].UpdateValue(this);
64 }
65
66 public ref T GetValueByRef() => ref val;
67 }
68}