-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFXVariable.cs
165 lines (112 loc) · 4.37 KB
/
FXVariable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.D3DCompiler;
// resolve conflict - DXGI.Device & Direct3D10.Device
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
using Effect = SharpDX.Direct3D11.Effect;
using EffectFlags = SharpDX.D3DCompiler.EffectFlags;
using System.Runtime.InteropServices;
namespace FXFramework
{
// TODO: Check the type that we give with the type that is expect
public class FXVariable<T> : IFXVariable , IDisposable
where T : struct
{
private FXConstantBuffer CBParent;
private T localValue;
private readonly int StartOffset;
private Boolean is_Dirty;
/// <summary>
/// We have changes to the buffer ?
/// </summary>
public Boolean isDirty
{
get { return is_Dirty; }
}
#region Constructor
public FXVariable( FXConstantBuffer CBParent, String resource_name )
{
// save the local variables
this.CBParent = CBParent;
ShaderReflectionVariable srv = null;
// get the offset of the variable from the start of the constant buffer
if ( CBParent.whereIsExist.HasFlag( ShaderType.Vertex ) ) {
srv = CBParent.constantBuffer_vs.GetVariable( resource_name );
} else if ( CBParent.whereIsExist.HasFlag( ShaderType.Pixel ) ) {
srv = CBParent.constantBuffer_ps.GetVariable( resource_name );
} else if ( CBParent.whereIsExist.HasFlag( ShaderType.Compute ) ) {
srv = CBParent.constantBuffer_cs.GetVariable( resource_name );
} else if ( CBParent.whereIsExist.HasFlag( ShaderType.Geometry ) ) {
srv = CBParent.constantBuffer_gs.GetVariable( resource_name );
}
// check the type compatibility
if ( Utils.CheckCompatibilityOfType<T>( localValue, srv.GetVariableType().Description ) ) {
// get the offset of the variable
StartOffset = srv.Description.StartOffset;
} else {
throw new System.ApplicationException( "Wrong Shader Type" );
}
// set the dirty to false because we don't have actual data to send to GPU
is_Dirty = false;
}
#endregion
#region Set
public void Set( T value )
{
lock ( localValue ) {
// if the input value is matrix then fix the row-Col issue
if ( value is Matrix ) {
Matrix tmp = value as Matrix? ?? Matrix.Identity; // fucking coding
value = Matrix.Transpose( tmp ) as T? ?? value;
}
// check if we have actual change with our variable
if ( !value.Equals( localValue ) ) {
// set the local value
localValue = value;
// set that we need to commit the changes to the hardware
is_Dirty = true;
// set the dirtyness and to the parent
CBParent.isDirty = true;
}
}
}
#endregion
#region Commit
/// <summary>
/// Update the data in datastream of the constant buffer if the data are dirty.
/// </summary>
/// <returns>If we have change</returns>
public Boolean Commit()
{
// commit the memmory changes
if ( is_Dirty ) {
//lock ( localValue )
{
// lock ( CBParent.dataStream )
{
// pass the data to the data stream of the constant buffer
CBParent.dataStream.Seek( StartOffset, System.IO.SeekOrigin.Begin );
CBParent.dataStream.Write<T>( localValue );
}
}
// reset the dirtyness
is_Dirty = false;
// return that we have change
return true;
}
// return that we have no change
return false;
}
#endregion
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}