forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDxcContainerBuilder.cpp
More file actions
290 lines (265 loc) · 11.1 KB
/
DxcContainerBuilder.cpp
File metadata and controls
290 lines (265 loc) · 11.1 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
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
///////////////////////////////////////////////////////////////////////////////
// //
// dxcontainerbuilder.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Implements the Dxil Container Builder //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/DxilContainer/DxcContainerBuilder.h"
#include "dxc/DxilContainer/DxilContainer.h"
#include "dxc/Support/ErrorCodes.h"
#include "dxc/Support/FileIOHelper.h"
#include "dxc/Support/dxcapi.impl.h"
#include "dxc/Support/microcom.h"
#include "dxc/dxcapi.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
// This declaration is used for the locally-linked validator.
HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv);
template <class TInterface>
HRESULT DxilLibCreateInstance(REFCLSID rclsid, TInterface **ppInterface);
using namespace hlsl;
HRESULT STDMETHODCALLTYPE DxcContainerBuilder::Load(IDxcBlob *pSource) {
DxcThreadMalloc TM(m_pMalloc);
try {
IFTBOOL(m_pContainer == nullptr && pSource != nullptr &&
IsDxilContainerLike(pSource->GetBufferPointer(),
pSource->GetBufferSize()),
E_INVALIDARG);
m_pContainer = pSource;
const DxilContainerHeader *pHeader =
(DxilContainerHeader *)pSource->GetBufferPointer();
for (DxilPartIterator it = begin(pHeader), itEnd = end(pHeader);
it != itEnd; ++it) {
const DxilPartHeader *pPartHeader = *it;
CComPtr<IDxcBlob> pBlob;
IFT(DxcCreateBlobFromPinned((const void *)(pPartHeader + 1),
pPartHeader->PartSize, &pBlob));
AddPart(DxilPart(pPartHeader->PartFourCC, pBlob));
}
// Collect hash function.
const DxilContainerHeader *Header =
(DxilContainerHeader *)pSource->GetBufferPointer();
DetermineHashFunctionFromContainerContents(Header);
return S_OK;
}
CATCH_CPP_RETURN_HRESULT();
}
HRESULT STDMETHODCALLTYPE DxcContainerBuilder::AddPart(UINT32 fourCC,
IDxcBlob *pSource) {
DxcThreadMalloc TM(m_pMalloc);
try {
IFTBOOL(pSource != nullptr &&
!IsDxilContainerLike(pSource->GetBufferPointer(),
pSource->GetBufferSize()),
E_INVALIDARG);
// You can only add debug info, debug info name, rootsignature, or private
// data blob
IFTBOOL(fourCC == DxilFourCC::DFCC_ShaderDebugInfoDXIL ||
fourCC == DxilFourCC::DFCC_ShaderDebugName ||
fourCC == DxilFourCC::DFCC_RootSignature ||
fourCC == DxilFourCC::DFCC_ShaderStatistics ||
fourCC == DxilFourCC::DFCC_PrivateData,
E_INVALIDARG);
AddPart(DxilPart(fourCC, pSource));
if (fourCC == DxilFourCC::DFCC_RootSignature) {
m_RequireValidation = true;
}
return S_OK;
}
CATCH_CPP_RETURN_HRESULT();
}
HRESULT STDMETHODCALLTYPE DxcContainerBuilder::RemovePart(UINT32 fourCC) {
DxcThreadMalloc TM(m_pMalloc);
try {
IFTBOOL(fourCC == DxilFourCC::DFCC_ShaderDebugInfoDXIL ||
fourCC == DxilFourCC::DFCC_ShaderDebugName ||
fourCC == DxilFourCC::DFCC_RootSignature ||
fourCC == DxilFourCC::DFCC_PrivateData ||
fourCC == DxilFourCC::DFCC_ShaderStatistics,
E_INVALIDARG); // You can only remove debug info, debug info name,
// rootsignature, or private data blob
PartList::iterator it =
std::find_if(m_parts.begin(), m_parts.end(),
[&](DxilPart part) { return part.m_fourCC == fourCC; });
IFTBOOL(it != m_parts.end(), DXC_E_MISSING_PART);
m_parts.erase(it);
if (fourCC == DxilFourCC::DFCC_PrivateData) {
m_HasPrivateData = false;
}
return S_OK;
}
CATCH_CPP_RETURN_HRESULT();
}
HRESULT STDMETHODCALLTYPE
DxcContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) {
if (ppResult == nullptr)
return E_INVALIDARG;
DxcThreadMalloc TM(m_pMalloc);
try {
// Allocate memory for new dxil container.
uint32_t ContainerSize = ComputeContainerSize();
CComPtr<AbstractMemoryStream> pMemoryStream;
CComPtr<IDxcBlob> pResult;
IFT(CreateMemoryStream(m_pMalloc, &pMemoryStream));
IFT(pMemoryStream->QueryInterface(&pResult));
IFT(pMemoryStream->Reserve(ContainerSize))
// Update Dxil Container
IFT(UpdateContainerHeader(pMemoryStream, ContainerSize));
// Update offset Table
IFT(UpdateOffsetTable(pMemoryStream));
// Update Parts
IFT(UpdateParts(pMemoryStream));
CComPtr<IDxcBlobUtf8> pValErrorUtf8;
HRESULT valHR = S_OK;
if (m_RequireValidation) {
CComPtr<IDxcValidator> pValidator;
IFT(CreateDxcValidator(IID_PPV_ARGS(&pValidator)));
CComPtr<IDxcOperationResult> pValidationResult;
IFT(pValidator->Validate(pResult, DxcValidatorFlags_RootSignatureOnly,
&pValidationResult));
IFT(pValidationResult->GetStatus(&valHR));
if (FAILED(valHR)) {
CComPtr<IDxcBlobEncoding> pValError;
IFT(pValidationResult->GetErrorBuffer(&pValError));
if (pValError->GetBufferPointer() && pValError->GetBufferSize())
IFT(hlsl::DxcGetBlobAsUtf8(pValError, m_pMalloc, &pValErrorUtf8));
}
}
// Combine existing warnings and errors from validation
CComPtr<IDxcBlobEncoding> pErrorBlob;
CDxcMallocHeapPtr<char> errorHeap(m_pMalloc);
SIZE_T totalErrorLength =
pValErrorUtf8 ? pValErrorUtf8->GetStringLength() : 0;
if (totalErrorLength) {
SIZE_T errorSizeInBytes = totalErrorLength + 1;
errorHeap.AllocateBytes(errorSizeInBytes);
memcpy(errorHeap.m_pData, pValErrorUtf8->GetStringPointer(),
totalErrorLength);
errorHeap.m_pData[totalErrorLength] = L'\0';
IFT(hlsl::DxcCreateBlobWithEncodingOnMalloc(errorHeap.m_pData, m_pMalloc,
errorSizeInBytes, DXC_CP_UTF8,
&pErrorBlob));
errorHeap.Detach();
}
// Add Hash.
if (SUCCEEDED(valHR))
HashAndUpdate(IsDxilContainerLike(pResult->GetBufferPointer(),
pResult->GetBufferSize()));
IFT(DxcResult::Create(
valHR, DXC_OUT_OBJECT,
{DxcOutputObject::DataOutput(DXC_OUT_OBJECT, pResult, DxcOutNoName),
DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pErrorBlob, DxcOutNoName)},
ppResult));
}
CATCH_CPP_RETURN_HRESULT();
return S_OK;
}
// Try hashing the source contained in ContainerHeader using retail and debug
// hashing functions. If either of them match the stored result, set the
// HashFunction to the matching variant. If neither match, set it to null.
void DxcContainerBuilder::DetermineHashFunctionFromContainerContents(
const DxilContainerHeader *ContainerHeader) {
DXASSERT(ContainerHeader != nullptr &&
IsDxilContainerLike(ContainerHeader,
ContainerHeader->ContainerSizeInBytes),
"otherwise load function should have returned an error.");
constexpr uint32_t HashStartOffset =
offsetof(struct DxilContainerHeader, Version);
auto *DataToHash = (const BYTE *)ContainerHeader + HashStartOffset;
UINT AmountToHash = ContainerHeader->ContainerSizeInBytes - HashStartOffset;
BYTE Result[DxilContainerHashSize];
ComputeHashRetail(DataToHash, AmountToHash, Result);
if (0 == memcmp(Result, ContainerHeader->Hash.Digest, sizeof(Result))) {
m_HashFunction = ComputeHashRetail;
} else {
ComputeHashDebug(DataToHash, AmountToHash, Result);
if (0 == memcmp(Result, ContainerHeader->Hash.Digest, sizeof(Result)))
m_HashFunction = ComputeHashDebug;
else
m_HashFunction = nullptr;
}
}
// For Internal hash function.
void DxcContainerBuilder::HashAndUpdate(DxilContainerHeader *ContainerHeader) {
if (m_HashFunction != nullptr) {
DXASSERT(ContainerHeader != nullptr,
"Otherwise serialization should have failed.");
static const UINT32 HashStartOffset =
offsetof(struct DxilContainerHeader, Version);
const BYTE *DataToHash = (const BYTE *)ContainerHeader + HashStartOffset;
UINT AmountToHash = ContainerHeader->ContainerSizeInBytes - HashStartOffset;
m_HashFunction(DataToHash, AmountToHash, ContainerHeader->Hash.Digest);
}
}
UINT32 DxcContainerBuilder::ComputeContainerSize() {
UINT32 partsSize = 0;
for (DxilPart part : m_parts) {
partsSize += part.m_Blob->GetBufferSize();
}
return GetDxilContainerSizeFromParts(m_parts.size(), partsSize);
}
HRESULT
DxcContainerBuilder::UpdateContainerHeader(AbstractMemoryStream *pStream,
uint32_t containerSize) {
DxilContainerHeader header;
InitDxilContainer(&header, m_parts.size(), containerSize);
ULONG cbWritten;
IFR(pStream->Write(&header, sizeof(DxilContainerHeader), &cbWritten));
if (cbWritten != sizeof(DxilContainerHeader)) {
return E_FAIL;
}
return S_OK;
}
HRESULT DxcContainerBuilder::UpdateOffsetTable(AbstractMemoryStream *pStream) {
UINT32 offset =
sizeof(DxilContainerHeader) + GetOffsetTableSize(m_parts.size());
for (size_t i = 0; i < m_parts.size(); ++i) {
ULONG cbWritten;
IFR(pStream->Write(&offset, sizeof(UINT32), &cbWritten));
if (cbWritten != sizeof(UINT32)) {
return E_FAIL;
}
offset += sizeof(DxilPartHeader) + m_parts[i].m_Blob->GetBufferSize();
}
return S_OK;
}
HRESULT DxcContainerBuilder::UpdateParts(AbstractMemoryStream *pStream) {
for (size_t i = 0; i < m_parts.size(); ++i) {
ULONG cbWritten;
CComPtr<IDxcBlob> pBlob = m_parts[i].m_Blob;
// Write part header
DxilPartHeader partHeader = {m_parts[i].m_fourCC,
(uint32_t)pBlob->GetBufferSize()};
IFR(pStream->Write(&partHeader, sizeof(DxilPartHeader), &cbWritten));
if (cbWritten != sizeof(DxilPartHeader)) {
return E_FAIL;
}
// Write part content
IFR(pStream->Write(pBlob->GetBufferPointer(), pBlob->GetBufferSize(),
&cbWritten));
if (cbWritten != pBlob->GetBufferSize()) {
return E_FAIL;
}
}
return S_OK;
}
void DxcContainerBuilder::AddPart(DxilPart &&part) {
PartList::iterator it =
std::find_if(m_parts.begin(), m_parts.end(), [&](DxilPart checkPart) {
return checkPart.m_fourCC == part.m_fourCC;
});
IFTBOOL(it == m_parts.end(), DXC_E_DUPLICATE_PART);
if (m_HasPrivateData) {
// Keep PrivateData at end, since it may have unaligned size.
m_parts.insert(m_parts.end() - 1, std::move(part));
} else {
m_parts.emplace_back(std::move(part));
}
if (part.m_fourCC == DxilFourCC::DFCC_PrivateData) {
m_HasPrivateData = true;
}
}