forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstantBufferData.sharpdx.cs
131 lines (104 loc) · 4.68 KB
/
ConstantBufferData.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace TwoMGFX
{
internal partial class ConstantBufferData
{
public ConstantBufferData(SharpDX.D3DCompiler.ConstantBuffer cb)
{
Name = string.Empty;
Size = cb.Description.Size;
ParameterIndex = new List<int>();
var parameters = new List<EffectObject.d3dx_parameter>();
// Gather all the parameters.
for (var i = 0; i < cb.Description.VariableCount; i++)
{
var vdesc = cb.GetVariable(i);
var param = GetParameterFromType(vdesc.GetVariableType());
param.name = vdesc.Description.Name;
param.semantic = string.Empty;
param.bufferOffset = vdesc.Description.StartOffset;
var size = param.columns * param.rows * 4;
var data = new byte[size];
if (vdesc.Description.DefaultValue != IntPtr.Zero)
Marshal.Copy(vdesc.Description.DefaultValue, data, 0, (int)size);
param.data = data;
parameters.Add(param);
}
// Sort them by the offset for some consistent results.
Parameters = parameters.OrderBy(e => e.bufferOffset).ToList();
// Store the parameter offsets.
ParameterOffset = new List<int>();
foreach (var param in Parameters)
ParameterOffset.Add(param.bufferOffset);
}
private static EffectObject.d3dx_parameter GetParameterFromType(SharpDX.D3DCompiler.ShaderReflectionType type)
{
var param = new EffectObject.d3dx_parameter();
param.rows = (uint)type.Description.RowCount;
param.columns = (uint)type.Description.ColumnCount;
param.name = type.Description.Name ?? string.Empty;
param.semantic = string.Empty;
param.bufferOffset = type.Description.Offset;
switch (type.Description.Class)
{
case SharpDX.D3DCompiler.ShaderVariableClass.Scalar:
param.class_ = EffectObject.D3DXPARAMETER_CLASS.SCALAR;
break;
case SharpDX.D3DCompiler.ShaderVariableClass.Vector:
param.class_ = EffectObject.D3DXPARAMETER_CLASS.VECTOR;
break;
case SharpDX.D3DCompiler.ShaderVariableClass.MatrixColumns:
param.class_ = EffectObject.D3DXPARAMETER_CLASS.MATRIX_COLUMNS;
break;
default:
throw new Exception("Unsupported parameter class!");
}
switch (type.Description.Type)
{
case SharpDX.D3DCompiler.ShaderVariableType.Bool:
param.type = EffectObject.D3DXPARAMETER_TYPE.BOOL;
break;
case SharpDX.D3DCompiler.ShaderVariableType.Float:
param.type = EffectObject.D3DXPARAMETER_TYPE.FLOAT;
break;
case SharpDX.D3DCompiler.ShaderVariableType.Int:
param.type = EffectObject.D3DXPARAMETER_TYPE.INT;
break;
default:
throw new Exception("Unsupported parameter type!");
}
param.member_count = (uint)type.Description.MemberCount;
param.element_count = (uint)type.Description.ElementCount;
if (param.member_count > 0)
{
param.member_handles = new EffectObject.d3dx_parameter[param.member_count];
for (var i = 0; i < param.member_count; i++)
{
var mparam = GetParameterFromType(type.GetMemberType(i));
mparam.name = type.GetMemberTypeName(i) ?? string.Empty;
param.member_handles[i] = mparam;
}
}
else
{
param.member_handles = new EffectObject.d3dx_parameter[param.element_count];
for (var i = 0; i < param.element_count; i++)
{
var mparam = new EffectObject.d3dx_parameter();
mparam.name = string.Empty;
mparam.semantic = string.Empty;
mparam.type = param.type;
mparam.class_ = param.class_;
mparam.rows = param.rows;
mparam.columns = param.columns;
mparam.data = new byte[param.columns * param.rows * 4];
param.member_handles[i] = mparam;
}
}
return param;
}
}
}