-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cs
187 lines (169 loc) · 5.27 KB
/
Shader.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
// ***********************************************************************
// Assembly : SharpDXWrapper
// Author : Andrew
// Created : 07-21-2017
//
// Last Modified By : Andrew
// Last Modified On : 07-21-2017
// ***********************************************************************
using SharpDX;
using SharpDX.D3DCompiler;
using System;
using D3D11 = SharpDX.Direct3D11;
namespace SharpDXWrapper
{
/// <summary>
/// Create vertex and pixel shader from loaded shader file
/// </summary>
/// <seealso cref="System.IDisposable" />
/// <seealso cref="SharpDXWrapper.IApply{T}" />
public class Shader : IDisposable, IApply<D3D11.DeviceContext>
{
private bool disposed = false;
/// <summary>
/// Gets or sets the vertex shader.
/// </summary>
/// <value>The vertex shader.</value>
public D3D11.VertexShader VertexShader { get; set; }
/// <summary>
/// Gets or sets the pixel shader.
/// </summary>
/// <value>The pixel shader.</value>
public D3D11.PixelShader PixelShader { get; set; }
/// <summary>
/// Gets or sets the input layout.
/// </summary>
/// <value>The input layout.</value>
public D3D11.InputLayout InputLayout { get; set; }
/// <summary>
/// Gets or sets the input signature.
/// </summary>
/// <value>The input signature.</value>
public ShaderSignature InputSignature { get; set; }
/// <summary>
/// Gets or sets the D3D11Device.
/// </summary>
/// <value>The device.</value>
public D3D11.Device Device { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Shader"/> class.
/// </summary>
/// <param name="device">The device.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="vertexShaderEntryPoint">The vertex shader entry point.</param>
/// <param name="pixelShaderEntryPoint">The pixel shader entry point.</param>
/// <param name="elements">The elements.</param>
/// <exception cref="System.ArgumentNullException"></exception>
/// <exception cref="SharpDX.CompilationException">
/// </exception>
public Shader(D3D11.Device device, string fileName, string vertexShaderEntryPoint,
string pixelShaderEntryPoint, D3D11.InputElement[] elements)
{
if (device == null)
{
throw new ArgumentNullException("device");
}
if ( elements == null)
{
throw new ArgumentNullException("elements");
}
if (fileName == string.Empty)
{
throw new ArgumentException("fileName is empty");
}
this.Device = device;
using (var vertexShaderByteCode = ShaderBytecode.CompileFromFile(fileName, vertexShaderEntryPoint, "vs_4_0", ShaderFlags.Debug))
{
if (vertexShaderByteCode.Bytecode != null)
{
using (var pixelShaderByteCode = ShaderBytecode.CompileFromFile(fileName, pixelShaderEntryPoint, "ps_4_0", ShaderFlags.Debug))
{
if (pixelShaderByteCode.Bytecode != null)
{
this.VertexShader = new D3D11.VertexShader(device, vertexShaderByteCode);
this.PixelShader = new D3D11.PixelShader(device, pixelShaderByteCode);
this.InputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode);
this.InputLayout = new D3D11.InputLayout(device, InputSignature, elements);
this.Apply(device.ImmediateContext);
}
else
{
throw new CompilationException(pixelShaderByteCode.ResultCode, pixelShaderByteCode.Message);
}
}
}
else
{
throw new CompilationException(vertexShaderByteCode.ResultCode, vertexShaderByteCode.Message);
}
}
}
/// <summary>
/// Apply resources to device context.
/// </summary>
/// <param name="deviceContext">The device context.</param>
public void Apply(D3D11.DeviceContext deviceContext)
{
deviceContext.InputAssembler.InputLayout = InputLayout;
deviceContext.VertexShader.Set(VertexShader);
deviceContext.PixelShader.Set(PixelShader);
}
/// <summary>
/// Finalizes an instance of the <see cref="Shader"/> class.
/// </summary>
~Shader()
{
Dispose(false);
}
/// <summary>
/// Performs application-defined tasks related to the release or reset of unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (VertexShader != null)
{
if (Device.ImmediateContext.VertexShader.Get().NativePointer == VertexShader.NativePointer)
{
Device.ImmediateContext.VertexShader.Set(null);
VertexShader.Dispose();
VertexShader = null;
}
}
if (PixelShader != null)
{
if (Device.ImmediateContext.PixelShader.Get().NativePointer == PixelShader.NativePointer)
{
Device.ImmediateContext.PixelShader.Set(null);
PixelShader.Dispose();
PixelShader = null;
}
}
if (InputLayout != null)
{
InputLayout.Dispose();
InputLayout = null;
}
if (InputSignature != null)
{
InputSignature.Dispose();
InputSignature = null;
}
}
disposed = true;
}
}
}
}