-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFXResourceVariable.cs
439 lines (327 loc) · 14.7 KB
/
FXResourceVariable.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX.D3DCompiler;
using SharpDX;
using SharpDX.DXGI;
using SharpDX.Direct3D11;
using Buffer = SharpDX.Direct3D11.Buffer;
using D3D = SharpDX.Direct3D11;
using FxMaths.GMaps;
using SharpDX.Direct3D;
namespace FXFramework
{
public class FXResourceVariable : IFXResources,IDisposable
{
#region Slots for shaders
public readonly int Slot_PS;
public readonly int Slot_VS;
public readonly int Slot_CS;
public readonly int Slot_GS;
#endregion
public ShaderType whereIsExist = ShaderType.None;
private ShaderResourceView localSRV;
private UnorderedAccessView localUAV;
private AccessViewType accessViewType = AccessViewType.SRV;
private String resourceName;
#region Properties
/// <summary>
/// Get the bit field for the shaders that this buffer exist
/// </summary>
public ShaderType WhereIsExist
{
get { return whereIsExist; }
}
/// <summary>
/// The name of the resource(like debug name).
/// </summary>
public String ResourceName
{
get { return resourceName; }
}
#endregion
#region Constructor
public FXResourceVariable( String resource_name,
ShaderReflection psShaderReflection = null, ShaderReflection vsShaderReflection = null,
ShaderReflection csShaderReflection = null, ShaderReflection gsShaderReflection = null )
{
resourceName = resource_name;
/// ----------------- Get the Pixel shader buffer ------------------------
///
#region Pixel Shader
try {
if ( psShaderReflection != null ) {
InputBindingDescription ibd = psShaderReflection.GetResourceBindingDescription( resource_name );
// find the bind point of the Resources
Slot_PS = ibd.BindPoint;
// set the bit for the pixel shader
whereIsExist |= ShaderType.Pixel;
// find the access type
if ( ibd.Type == ShaderInputType.UnorderedAccessViewRWByteAddress || (int)ibd.Type == 4 )
accessViewType = AccessViewType.UAV;
else
accessViewType = AccessViewType.SRV;
}
} catch ( Exception ex ) { /* do nothing */ }
#endregion
/// ----------------- Get the Vertex shader buffer ------------------------
///
#region Vertex Shader
try {
if ( vsShaderReflection != null ) {
// find the bind point of the Resources
Slot_VS = vsShaderReflection.GetResourceBindingDescription( resource_name ).BindPoint;
// set the bit for the pixel shader
whereIsExist |= ShaderType.Vertex;
}
} catch ( Exception ex ) { /* do nothing */ }
#endregion
/// ----------------- Get the Compute shader buffer ------------------------
///
#region Compute Shader
try {
if ( csShaderReflection != null ) {
InputBindingDescription ibd = csShaderReflection.GetResourceBindingDescription( resource_name );
// find the bind point of the Resources
Slot_CS = ibd.BindPoint;
// set the bit for the pixel shader
whereIsExist |= ShaderType.Compute;
// find the access type
if (ibd.Type == ShaderInputType.UnorderedAccessViewRWByteAddress || ibd.Type == ShaderInputType.UnorderedAccessViewRWStructured || ibd.Type == ShaderInputType.UnorderedAccessViewRWStructuredWithCounter || (int)ibd.Type == 4)
accessViewType = AccessViewType.UAV;
else
accessViewType = AccessViewType.SRV;
}
} catch ( Exception ex ) { /* do nothing */ }
#endregion
/// ----------------- Get the Geometry shader buffer ------------------------
#region Geometry Shader
try {
if ( gsShaderReflection != null ) {
// find the bind point of the Resources
Slot_GS = gsShaderReflection.GetResourceBindingDescription( resource_name ).BindPoint;
// set the bit for the pixel shader
whereIsExist |= ShaderType.Geometry;
}
} catch ( Exception ex ) { /* do nothing */ }
#endregion
}
#endregion
#region Set
/// <summary>
/// Set the view resource
/// </summary>
/// <param name="srv"></param>
public void SetResource( ShaderResourceView srv )
{
// store the srv local
localSRV = srv;
// rest the uav
localUAV = null;
}
/// <summary>
/// Set the view resource
/// </summary>
/// <param name="uav"></param>
public void SetResource( UnorderedAccessView uav )
{
// unlink the local srv
localSRV = null;
// store the uav local
localUAV = uav;
}
#endregion
#region Commit
public void Commit( DeviceContext deviceContext, ShaderType type )
{
// check if we this cb exist in the specific shader type
if ( !WhereIsExist.HasFlag( type ) )
return;
// we can use switch here beacuse we call this commit only for one shader type
switch ( type ) {
case ShaderType.Pixel:
if ( accessViewType == AccessViewType.SRV )
deviceContext.PixelShader.SetShaderResource(Slot_PS , localSRV);
//else
//deviceContext.PixelShader.SetUnorderedAccessView( localUAV, Slot_PS );
break;
case ShaderType.Vertex:
deviceContext.VertexShader.SetShaderResource(Slot_VS, localSRV);
break;
case ShaderType.Compute:
if ( accessViewType == AccessViewType.SRV)
deviceContext.ComputeShader.SetShaderResource(Slot_CS, localSRV);
else
deviceContext.ComputeShader.SetUnorderedAccessView( Slot_CS, localUAV );
break;
case ShaderType.Geometry:
deviceContext.GeometryShader.SetShaderResource(Slot_GS, localSRV);
break;
}
}
#endregion
#region Clean Binding
/// <summary>
/// Clean the resource binding
/// </summary>
/// <param name="deviceContext"></param>
/// <param name="type"></param>
public void CleanBind(DeviceContext deviceContext, ShaderType type)
{
// check if we this cb exist in the specific shader type
if (!WhereIsExist.HasFlag(type))
return;
// we can use switch here beacuse we call this commit only for one shader type
switch (type)
{
case ShaderType.Pixel:
if (accessViewType == AccessViewType.SRV)
deviceContext.PixelShader.SetShaderResource(Slot_CS, null);
break;
case ShaderType.Vertex:
deviceContext.VertexShader.SetShaderResource(Slot_VS, null);
break;
case ShaderType.Compute:
if (accessViewType == AccessViewType.SRV)
deviceContext.ComputeShader.SetShaderResource(Slot_CS, null);
else
deviceContext.ComputeShader.SetUnorderedAccessView(Slot_CS, null);
break;
case ShaderType.Geometry:
deviceContext.GeometryShader.SetShaderResource(Slot_GS, null);
break;
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (localSRV != null)
localSRV.Dispose();
if (localUAV != null)
localUAV.Dispose();
}
#endregion
#region UAV/Staging
public static ShaderResourceView InitSRVResource(SharpDX.Direct3D11.Device dev, SharpDX.Direct3D11.Buffer buffer)
{
// create desc for the UAV
ShaderResourceViewDescription srvbufferDesc = new ShaderResourceViewDescription
{
Dimension = ShaderResourceViewDimension.ExtendedBuffer,
Format = Format.Unknown,
Buffer = new ShaderResourceViewDescription.BufferResource
{
FirstElement = 0,
ElementCount = buffer.Description.SizeInBytes / buffer.Description.StructureByteStride,
},
};
ShaderResourceView tmpSRV = new ShaderResourceView(dev, buffer, srvbufferDesc);
tmpSRV.DebugName = "SRV_" + buffer.DebugName;
return tmpSRV;
}
public static UnorderedAccessView InitUAVResource(SharpDX.Direct3D11.Device dev, SharpDX.Direct3D11.Buffer buffer)
{
// create desc for the UAV
UnorderedAccessViewDescription uavbufferDesc = new UnorderedAccessViewDescription
{
Dimension = UnorderedAccessViewDimension.Buffer,
Format = Format.Unknown,
Buffer = new UnorderedAccessViewDescription.BufferResource
{
FirstElement = 0,
ElementCount = buffer.Description.SizeInBytes / buffer.Description.StructureByteStride,
},
};
UnorderedAccessView tmpUAV = new UnorderedAccessView(dev, buffer, uavbufferDesc);
tmpUAV.DebugName = "UAV_"+buffer.DebugName;
return tmpUAV;
}
/// <summary>
/// Read the GPU buffer to local array.
/// The buffer must be a staging buffer ....
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dev"></param>
/// <param name="cpuBuffer"></param>
/// <param name="gpuBuffer"></param>
/// <param name="localBuffer"></param>
public static void ReadBuffer<T>(SharpDX.Direct3D11.Device dev, Buffer cpuBuffer, Buffer gpuBuffer, ref T[] localBuffer) where T:struct
{
DataStream stream;
// copy the data from GPU
dev.ImmediateContext.CopyResource(gpuBuffer, cpuBuffer);
// map the data to be able to read it from CPU
DataBox outputData = dev.ImmediateContext.MapSubresource(cpuBuffer, D3D.MapMode.Read, D3D.MapFlags.None, out stream);
// read the data from the gpu
for (int i = 0; i < localBuffer.Length; i++)
localBuffer[i] = stream.Read<T>();
// unmap the data
dev.ImmediateContext.UnmapSubresource(cpuBuffer, 0);
}
/// <summary>
/// Read the GPU buffer to local array.
/// The buffer must be a staging buffer ....
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dev"></param>
/// <param name="cpuBuffer"></param>
/// <param name="gpuBuffer"></param>
/// <param name="localBuffer"></param>
public static void ReadBufferVector<T>(SharpDX.Direct3D11.Device dev, Buffer cpuBuffer, Buffer gpuBuffer, ref IVertex<T>[] localBuffer) where T : struct , IComparable<T>
{
DataStream stream;
// copy the data from GPU
dev.ImmediateContext.CopyResource(gpuBuffer, cpuBuffer);
// map the data to be able to read it from CPU
DataBox outputData = dev.ImmediateContext.MapSubresource(cpuBuffer, D3D.MapMode.Read, D3D.MapFlags.None, out stream);
// read the data from the gpu
for (int i = 0; i < localBuffer.Length; i++)
{
localBuffer[i] = (IVertex<T>)FxMaths.Vector.FxVector2f.sReadFromDataStream(stream);
}
// unmap the data
dev.ImmediateContext.UnmapSubresource(cpuBuffer, 0);
}
public static void WriteBufferStuct<T>(SharpDX.Direct3D11.Device dev, Buffer cpuBuffer, Buffer gpuBuffer, T[] inputBuffer) where T : struct
{
DataStream stream;
// map the data to be able to read it from CPU
DataBox outputData = dev.ImmediateContext.MapSubresource(cpuBuffer, D3D.MapMode.WriteDiscard, D3D.MapFlags.None, out stream);
// write the data from the gpu
for (int i = 0; i < inputBuffer.Length; i++)
stream.Write<T>(inputBuffer[i]);
// unmap the data
dev.ImmediateContext.UnmapSubresource(cpuBuffer, 0);
// copy the data to GPU
dev.ImmediateContext.CopyResource(cpuBuffer, gpuBuffer);
}
public static void WriteBufferVertex<T>(SharpDX.Direct3D11.Device dev, Buffer cpuBuffer, Buffer gpuBuffer, ICollection<IVertex<T>> inputBuffer) where T : struct , IComparable<T>
{
DataStream stream;
// map the data to be able to read it from CPU
DataBox outputData = dev.ImmediateContext.MapSubresource(cpuBuffer, D3D.MapMode.Write, D3D.MapFlags.None, out stream);
// write the data from the gpu
foreach (var vec in inputBuffer)
vec.WriteToDataStream(stream);
// unmap the data
dev.ImmediateContext.UnmapSubresource(cpuBuffer, 0);
// copy the data to GPU
dev.ImmediateContext.CopyResource(cpuBuffer ,gpuBuffer);
}
public static void WriteBuffer(SharpDX.Direct3D11.Device dev, Buffer cpuBuffer, Buffer gpuBuffer, DataStream streamIn)
{
DataStream stream;
// map the data to be able to read it from CPU
DataBox outputData = dev.ImmediateContext.MapSubresource(cpuBuffer, D3D.MapMode.Write, D3D.MapFlags.None, out stream);
// read the data from the gpu
streamIn.CopyTo(stream);
// unmap the data
dev.ImmediateContext.UnmapSubresource(cpuBuffer, 0);
// copy the data to GPU
dev.ImmediateContext.CopyResource(cpuBuffer, gpuBuffer);
}
#endregion
}
}