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 osu.Framework.Graphics.OpenGL;
5using System;
6
7namespace osu.Framework.Graphics.Shaders
8{
9 public class Uniform<T> : IUniformWithValue<T>
10 where T : struct, IEquatable<T>
11 {
12 public Shader Owner { get; }
13 public string Name { get; }
14 public int Location { get; }
15
16 public bool HasChanged { get; private set; } = true;
17
18 private T val;
19
20 public T Value
21 {
22 get => val;
23 set
24 {
25 if (value.Equals(val))
26 return;
27
28 val = value;
29 HasChanged = true;
30
31 if (Owner.IsBound)
32 Update();
33 }
34 }
35
36 public Uniform(Shader owner, string name, int uniformLocation)
37 {
38 Owner = owner;
39 Name = name;
40 Location = uniformLocation;
41 }
42
43 public void UpdateValue(ref T newValue)
44 {
45 if (newValue.Equals(val))
46 return;
47
48 val = newValue;
49 HasChanged = true;
50
51 if (Owner.IsBound)
52 Update();
53 }
54
55 public void Update()
56 {
57 if (!HasChanged) return;
58
59 GLWrapper.SetUniform(this);
60 HasChanged = false;
61 }
62
63 ref T IUniformWithValue<T>.GetValueByRef() => ref val;
64 T IUniformWithValue<T>.GetValue() => val;
65 }
66}