-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy path9on12InputLayout.cpp
More file actions
171 lines (145 loc) · 5.77 KB
/
9on12InputLayout.cpp
File metadata and controls
171 lines (145 loc) · 5.77 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
namespace D3D9on12
{
_Check_return_ HRESULT APIENTRY CreateVertexShaderDecl(
_In_ HANDLE hDevice,
_Inout_ D3DDDIARG_CREATEVERTEXSHADERDECL*pCreateVertexShaderDeclArg,
_In_ CONST D3DDDIVERTEXELEMENT* pVertexElements)
{
D3D9on12_DDI_ENTRYPOINT_START(TRUE);
Device* pDevice = Device::GetDeviceFromHandle(hDevice);
if (pDevice == nullptr || pCreateVertexShaderDeclArg == nullptr)
{
return E_INVALIDARG;
}
if (pCreateVertexShaderDeclArg->NumVertexElements > 0 && pVertexElements == nullptr)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
InputLayout *pInputLayout = new InputLayout(*pDevice);
if (pInputLayout == nullptr)
{
return E_OUTOFMEMORY;
}
if (SUCCEEDED(hr))
{
hr = pInputLayout->Init(pVertexElements, pCreateVertexShaderDeclArg->NumVertexElements);
}
if (SUCCEEDED(hr))
{
pCreateVertexShaderDeclArg->ShaderHandle = InputLayout::GetHandleFromInputLayout(pInputLayout);
}
D3D9on12_DDI_ENTRYPOINT_END_AND_RETURN_HR(hr);
}
_Check_return_ HRESULT APIENTRY DeleteVertexShaderDecl(_In_ HANDLE hDevice, _In_ HANDLE hShader)
{
D3D9on12_DDI_ENTRYPOINT_START(TRUE);
Device* pDevice = Device::GetDeviceFromHandle(hDevice);
InputLayout *pInputLayout = InputLayout::GetInputLayoutFromHandle(hShader);
if (pDevice == nullptr || pInputLayout == nullptr)
{
RETURN_E_INVALIDARG_AND_CHECK();
}
delete(pInputLayout);
D3D9on12_DDI_ENTRYPOINT_END_AND_RETURN_HR(S_OK);
}
_Check_return_ HRESULT APIENTRY SetStreamSourceFreq(_In_ HANDLE hDevice, _In_ CONST D3DDDIARG_SETSTREAMSOURCEFREQ *pStreamSourceFreqArg)
{
D3D9on12_DDI_ENTRYPOINT_START(TRUE);
Device* pDevice = Device::GetDeviceFromHandle(hDevice);
if (pDevice == nullptr || pStreamSourceFreqArg == nullptr || pStreamSourceFreqArg->Stream >= MAX_VERTEX_STREAMS)
{
return E_INVALIDARG;
}
pDevice->SetStreamFrequency(pStreamSourceFreqArg->Stream, pStreamSourceFreqArg->Divider);
D3D9on12_DDI_ENTRYPOINT_END_AND_RETURN_HR(S_OK);
}
_Check_return_ HRESULT APIENTRY SetVertexShaderDecl(_In_ HANDLE hDevice, _In_ HANDLE hInputLayout)
{
D3D9on12_DDI_ENTRYPOINT_START(TRUE);
Device* pDevice = Device::GetDeviceFromHandle(hDevice);
InputLayout *pInputLayout = InputLayout::GetInputLayoutFromHandle(hInputLayout);
if (pDevice == nullptr)
{
return E_INVALIDARG;
}
pDevice->GetPipelineState().GetInputAssembly().SetVertexDeclaration(pInputLayout);
D3D9on12_DDI_ENTRYPOINT_END_AND_RETURN_HR(S_OK);
}
InputLayout::InputLayout(Device &device) :
m_hasPreTransformedVertices(false),
m_hasPerVertexPointSize(false),
m_hash(0),
m_numVertexElements(0),
m_streamMask(0),
m_vsInputDecls(MAXD3DDECLLENGTH),
m_device(device)
{
memset(m_pVertexElements, 0, sizeof(m_pVertexElements));
}
InputLayout::~InputLayout()
{
auto &InputAssembly = m_device.GetPipelineState().GetInputAssembly();
if (&InputAssembly.GetInputLayout() == this)
{
InputAssembly.SetVertexDeclaration(nullptr);
}
}
HRESULT InputLayout::Init(_In_ CONST D3DDDIVERTEXELEMENT* pVertexElements, UINT numElements)
{
Check9on12(numElements < MAX_INPUT_ELEMENTS);
m_numVertexElements = numElements;
for (UINT i = 0; i < numElements; i++)
{
UINT isTransformedPosition = 0;
m_pVertexElements[i] = pVertexElements[i];
if (m_pVertexElements[i].Usage == D3DDECLUSAGE_POSITIONT && m_pVertexElements[i].UsageIndex == 0)
{
m_pVertexElements[i].Usage = D3DDECLUSAGE_POSITION;
m_hasPreTransformedVertices = true;
isTransformedPosition = 1;
}
if (m_pVertexElements[i].Usage == D3DDECLUSAGE_PSIZE)
{
m_hasPerVertexPointSize = true;
}
UINT conversions = [](D3DDECLTYPE type)
{
switch (type)
{
case D3DDECLTYPE_UDEC3:
return ShaderConv::VSInputDecl::UDEC3;
case D3DDECLTYPE_DEC3N:
return ShaderConv::VSInputDecl::DEC3N;
default:
return IsIntType(type) ?
ShaderConv::VSInputDecl::NeedsIntToFloatConversion :
ShaderConv::VSInputDecl::None;
}
}((D3DDECLTYPE)m_pVertexElements[i].Type);
m_vsInputDecls.AddDecl(m_pVertexElements[i].Usage, m_pVertexElements[i].UsageIndex, i, isTransformedPosition, conversions);
m_streamMask |= BIT(m_pVertexElements[i].Stream);
}
GetHash();
return S_OK;
}
WeakHash InputLayout::GetHash()
{
if (m_hash.Initialized() == false)
{
if (m_numVertexElements != 0)
{
m_hash = HashData(m_pVertexElements, m_numVertexElements * sizeof(m_pVertexElements[0]), m_hash);
m_hash = HashData(&m_vsInputDecls[0], sizeof(m_vsInputDecls[0]) * m_vsInputDecls.GetSize(), m_hash);
}
else
{
m_hash = WeakHash::GetHashForEmptyObject();
}
}
return m_hash;
}
};