-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFXEffect.cs
487 lines (345 loc) · 14.4 KB
/
FXEffect.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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;
namespace FXFramework
{
public class FXEffect : IDisposable
{
#region Variables
#region Bytecode
/// <summary>
/// The bytecode of pixel shader
/// </summary>
private ShaderBytecode PixelShaderByteCode;
/// <summary>
/// The bytecode of vertex shader
/// </summary>
private ShaderBytecode VertexShaderByteCode;
/// <summary>
/// The bytecode of compute shader
/// </summary>
private ShaderBytecode ComputeShaderByteCode;
/// <summary>
/// The bytecode of geometry shader
/// </summary>
private ShaderBytecode GeometryShaderByteCode;
#endregion
#region shaders
/// <summary>
/// Pixel Shader
/// </summary>
public PixelShader PixelShader;
/// <summary>
/// Vertex Shader
/// </summary>
public VertexShader VertexShader;
/// <summary>
/// Compute Shader
/// </summary>
public ComputeShader ComputeShader;
/// <summary>
/// Geometry Shader
/// </summary>
public GeometryShader GeometryShader;
#endregion
/// <summary>
/// The 3d device
/// </summary>
private Device device;
#region ShaderReflection
/// <summary>
/// The reflection of the pixel shader.
/// </summary>
public ShaderReflection PixelShaderReflection;
/// <summary>
/// The reflection of the Vertex shader.
/// </summary>
public ShaderReflection VertexShaderReflection;
/// <summary>
/// The reflection of the Compute shader.
/// </summary>
public ShaderReflection ComputeShaderReflection;
/// <summary>
/// The reflection of the Geometry shader.
/// </summary>
public ShaderReflection GeometryShaderReflection;
#endregion
#region List of constant buffer
/// <summary>
/// List with constant Buffers of pixel shader
/// </summary>
private List<IFXResources> ListWithConstantBufferOfPixel;
/// <summary>
/// List with constant Buffers of vertex shader
/// </summary>
private List<IFXResources> ListWithConstantBufferOfVertex;
/// <summary>
/// List with constant Buffers of Compute shader
/// </summary>
private List<IFXResources> ListWithConstantBufferOfCompute;
/// <summary>
/// List with constant Buffers of Geometry shader
/// </summary>
private List<IFXResources> ListWithConstantBufferOfGeometry;
#endregion
/// <summary>
/// Which shaders have be include in this effect.
/// </summary>
private ShaderType IncludedShaders = ShaderType.None;
#endregion
#region Constractor
public FXEffect( Device dev, ShaderBytecode psByteCode = null, ShaderBytecode vsByteCode = null, ShaderBytecode csByteCode = null, ShaderBytecode gsByteCode = null )
{
// store the bytecodes
this.PixelShaderByteCode = psByteCode;
this.VertexShaderByteCode = vsByteCode;
this.ComputeShaderByteCode = csByteCode;
this.GeometryShaderByteCode = gsByteCode;
#region init Vertex
// check for the shader
if ( vsByteCode != null ) {
// set the flag for the shader
IncludedShaders |= ShaderType.Vertex;
// init the shader
this.VertexShader = new VertexShader( dev, vsByteCode );
// create the reflection of shaders
VertexShaderReflection = new ShaderReflection( vsByteCode );
// init the lists
ListWithConstantBufferOfVertex = new List<IFXResources>();
}
#endregion
#region init Pixel
// check for the shader
if ( psByteCode != null ) {
// set the flag for the shader
IncludedShaders |= ShaderType.Pixel;
// init the shader
this.PixelShader = new PixelShader( dev, psByteCode );
// create the reflection of shaders
PixelShaderReflection = new ShaderReflection( psByteCode );
// init the lists
ListWithConstantBufferOfPixel = new List<IFXResources>();
}
#endregion
#region init Compute
// check for the shader
if ( csByteCode != null ) {
// set the flag for the shader
IncludedShaders |= ShaderType.Compute;
try
{
// init the shader
this.ComputeShader = new ComputeShader(dev, csByteCode);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
System.Windows.Forms.MessageBox.Show(ex.StackTrace);
System.Windows.Forms.MessageBox.Show(ex.Source);
}
// create the reflection of shaders
ComputeShaderReflection = new ShaderReflection( csByteCode );
// init the lists
ListWithConstantBufferOfCompute = new List<IFXResources>();
}
#endregion
#region init Geometry
// check for the shader
if ( gsByteCode != null ) {
// set the flag for the shader
IncludedShaders |= ShaderType.Geometry;
// init the shader
GeometryShader = new GeometryShader( dev, gsByteCode );
// create the reflection of shaders
GeometryShaderReflection = new ShaderReflection( gsByteCode );
// init the lists
ListWithConstantBufferOfGeometry = new List<IFXResources>();
}
#endregion
// store the device
this.device = dev;
}
#endregion
#region Get
public FXConstantBuffer GetConstantBufferByName( String Name )
{
/// create the buffer base on the name
FXConstantBuffer tmpCB = new FXConstantBuffer( device, Name, PixelShaderReflection, VertexShaderReflection, ComputeShaderReflection, GeometryShaderReflection );
/// add the new cb to the list to use it when we execute the shader
/// insert the cb in the lists that they are going to use it
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Pixel ) )
ListWithConstantBufferOfPixel.Add( tmpCB );
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Vertex ) )
ListWithConstantBufferOfVertex.Add( tmpCB );
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Compute ) )
ListWithConstantBufferOfCompute.Add( tmpCB );
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Geometry ) )
ListWithConstantBufferOfGeometry.Add( tmpCB );
return tmpCB;
}
public FXConstantBuffer<T> GetConstantBufferByName<T>( String Name ) where T:struct
{
/// create the buffer base on the name
FXConstantBuffer<T> tmpCB = new FXConstantBuffer<T>( device, Name, PixelShaderReflection, VertexShaderReflection, ComputeShaderReflection, GeometryShaderReflection );
/// add the new cb to the list to use it when we execute the shader
/// insert the cb in the lists that they are going to use it
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Pixel ) )
ListWithConstantBufferOfPixel.Add( tmpCB );
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Vertex ) )
ListWithConstantBufferOfVertex.Add( tmpCB );
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Compute ) )
ListWithConstantBufferOfCompute.Add( tmpCB );
if ( tmpCB.WhereIsExist.HasFlag( ShaderType.Geometry ) )
ListWithConstantBufferOfGeometry.Add( tmpCB );
if (tmpCB != null && tmpCB.Buffer != null)
tmpCB.Buffer.DebugName = Name;
return tmpCB;
}
public FXResourceVariable GetResourceByName( string Name )
{
/// create the buffer base on the name
FXResourceVariable tmpRV = new FXResourceVariable( Name, PixelShaderReflection, VertexShaderReflection , ComputeShaderReflection, GeometryShaderReflection );
/// add the new cb to the list to use it when we execute the shader
/// insert the cb in the lists that they are going to use it
if ( tmpRV.WhereIsExist.HasFlag( ShaderType.Pixel ) )
ListWithConstantBufferOfPixel.Add( tmpRV );
if ( tmpRV.WhereIsExist.HasFlag( ShaderType.Vertex ) )
ListWithConstantBufferOfVertex.Add( tmpRV );
if ( tmpRV.WhereIsExist.HasFlag( ShaderType.Compute ) )
ListWithConstantBufferOfCompute.Add( tmpRV );
if ( tmpRV.WhereIsExist.HasFlag( ShaderType.Geometry ) )
ListWithConstantBufferOfGeometry.Add( tmpRV );
return tmpRV;
}
#endregion
#region Apply
public void Apply(DeviceContext deviceContext)
{
// ===================== set the shaders =====================
// -------- set the Pixel shader ----------
if (IncludedShaders.HasFlag(ShaderType.Pixel))
{
// set the shader
deviceContext.PixelShader.Set(PixelShader);
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfPixel)
cb.Commit(deviceContext, ShaderType.Pixel);
}
else
{
deviceContext.PixelShader.Set(null);
}
// -------- set the Vertex shader ----------
if (IncludedShaders.HasFlag(ShaderType.Vertex))
{
// set the shader
deviceContext.VertexShader.Set(VertexShader);
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfVertex)
cb.Commit(deviceContext, ShaderType.Vertex);
}
else
{
deviceContext.VertexShader.Set(null);
}
// -------- set the Compute shader ----------
if (IncludedShaders.HasFlag(ShaderType.Compute))
{
// set the shader
deviceContext.ComputeShader.Set(ComputeShader);
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfCompute)
cb.Commit(deviceContext, ShaderType.Compute);
}
else
{
deviceContext.ComputeShader.Set(null);
}
// -------- set the Geometry shader ----------
if (IncludedShaders.HasFlag(ShaderType.Geometry))
{
// set the shader
deviceContext.GeometryShader.Set(GeometryShader);
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfGeometry)
cb.Commit(deviceContext, ShaderType.Geometry);
}
else
{
deviceContext.GeometryShader.Set(null);
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
// Pixel
PixelShader.Dispose();
PixelShaderByteCode.Dispose();
PixelShaderReflection.Dispose();
// vertex
VertexShader.Dispose();
VertexShaderByteCode.Dispose();
VertexShaderReflection.Dispose();
// Compute
ComputeShader.Dispose();
ComputeShaderByteCode.Dispose();
// Geometry
GeometryShader.Dispose();
GeometryShaderByteCode.Dispose();
// CB
foreach ( IFXResources cb in ListWithConstantBufferOfPixel )
cb.Dispose();
foreach ( IFXResources cb in ListWithConstantBufferOfVertex )
cb.Dispose();
foreach ( IFXResources cb in ListWithConstantBufferOfCompute )
cb.Dispose();
foreach ( IFXResources cb in ListWithConstantBufferOfGeometry )
cb.Dispose();
}
#endregion
#region Clean Binding
public void CleanBind(DeviceContext deviceContext)
{
// ===================== set the shaders =====================
// -------- set the Pixel shader ----------
if (IncludedShaders.HasFlag(ShaderType.Pixel))
{
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfPixel)
cb.CleanBind(deviceContext, ShaderType.Pixel);
}
// -------- set the Vertex shader ----------
if (IncludedShaders.HasFlag(ShaderType.Vertex))
{
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfVertex)
cb.CleanBind(deviceContext, ShaderType.Vertex);
}
// -------- set the Compute shader ----------
if (IncludedShaders.HasFlag(ShaderType.Compute))
{
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfCompute)
cb.CleanBind(deviceContext, ShaderType.Compute);
}
// -------- set the Geometry shader ----------
if (IncludedShaders.HasFlag(ShaderType.Geometry))
{
// set the buffers
foreach (IFXResources cb in ListWithConstantBufferOfGeometry)
cb.CleanBind(deviceContext, ShaderType.Geometry);
}
}
#endregion
}
}