forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderData.sharpdx.cs
156 lines (138 loc) · 7.15 KB
/
ShaderData.sharpdx.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
using System.Collections.Generic;
using SharpDX.Direct3D;
using TwoMGFX.TPGParser;
namespace TwoMGFX
{
internal partial class ShaderData
{
public static ShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List<ConstantBufferData> cbuffers, int sharedIndex, Dictionary<string, SamplerStateInfo> samplerStates, bool debug)
{
var dxshader = new ShaderData(isVertexShader, sharedIndex, byteCode);
dxshader._attributes = new Attribute[0];
// Strip the bytecode we're gonna save!
var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;
if (!debug)
stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;
using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
{
// Strip the bytecode for saving to disk.
var stripped = original.Strip(stripFlags);
{
// Only SM4 and above works with strip... so this can return null!
if (stripped != null)
{
dxshader.ShaderCode = stripped;
}
else
{
// TODO: There is a way to strip SM3 and below
// but we have to write the method ourselves.
//
// If we need to support it then consider porting
// this code over...
//
// http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
//
dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
}
}
// Use reflection to get details of the shader.
using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
{
// Get the samplers.
var samplers = new List<Sampler>();
for (var i = 0; i < refelect.Description.BoundResources; i++)
{
var rdesc = refelect.GetResourceBindingDescription(i);
if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
{
var samplerName = rdesc.Name;
var sampler = new Sampler
{
samplerName = string.Empty,
textureSlot = rdesc.BindPoint,
samplerSlot = rdesc.BindPoint,
parameterName = samplerName
};
SamplerStateInfo state;
if (samplerStates.TryGetValue(samplerName, out state))
{
sampler.parameterName = state.TextureName ?? samplerName;
sampler.state = state.State;
}
else
{
foreach (var s in samplerStates.Values)
{
if (samplerName == s.TextureName)
{
sampler.state = s.State;
samplerName = s.Name;
break;
}
}
}
// Find sampler slot, which can be different from the texture slot.
for (int j = 0; j < refelect.Description.BoundResources; j++)
{
var samplerrdesc = refelect.GetResourceBindingDescription(j);
if (samplerrdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Sampler &&
samplerrdesc.Name == samplerName)
{
sampler.samplerSlot = samplerrdesc.BindPoint;
break;
}
}
switch (rdesc.Dimension)
{
case ShaderResourceViewDimension.Texture1D:
case ShaderResourceViewDimension.Texture1DArray:
sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_1D;
break;
case ShaderResourceViewDimension.Texture2D:
case ShaderResourceViewDimension.Texture2DArray:
case ShaderResourceViewDimension.Texture2DMultisampled:
case ShaderResourceViewDimension.Texture2DMultisampledArray:
sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D;
break;
case ShaderResourceViewDimension.Texture3D:
sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_VOLUME;
break;
case ShaderResourceViewDimension.TextureCube:
case ShaderResourceViewDimension.TextureCubeArray:
sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_CUBE;
break;
}
samplers.Add(sampler);
}
}
dxshader._samplers = samplers.ToArray();
// Gather all the constant buffers used by this shader.
dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
{
var cb = new ConstantBufferData(refelect.GetConstantBuffer(i));
// Look for a duplicate cbuffer in the list.
for (var c = 0; c < cbuffers.Count; c++)
{
if (cb.SameAs(cbuffers[c]))
{
cb = null;
dxshader._cbuffers[i] = c;
break;
}
}
// Add a new cbuffer.
if (cb != null)
{
dxshader._cbuffers[i] = cbuffers.Count;
cbuffers.Add(cb);
}
}
}
}
return dxshader;
}
}
}