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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
//--------------------------------------------------------------------------------------
// File: d3dxGlobal.cpp
//
// Direct3D 11 Effects implementation for helper data structures
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/p/?LinkId=271568
//--------------------------------------------------------------------------------------
#include "pchfx.h"
#include <intsafe.h>
#include <stdio.h>
#include <stdarg.h>
namespace D3DX11Core
{
//////////////////////////////////////////////////////////////////////////
// CMemoryStream - A class to simplify reading binary data
//////////////////////////////////////////////////////////////////////////
CMemoryStream::CMemoryStream() noexcept :
m_pData(nullptr),
m_cbData(0),
m_readPtr(0)
{
}
CMemoryStream::~CMemoryStream()
{
}
_Use_decl_annotations_
HRESULT CMemoryStream::SetData(const void *pData, size_t size)
{
m_pData = (uint8_t*) pData;
m_cbData = size;
m_readPtr = 0;
return S_OK;
}
_Use_decl_annotations_
HRESULT CMemoryStream::ReadAtOffset(size_t offset, size_t size, void **ppData)
{
if (offset >= m_cbData)
return E_FAIL;
m_readPtr = offset;
return Read(ppData, size);
}
_Use_decl_annotations_
HRESULT CMemoryStream::ReadAtOffset(size_t offset, LPCSTR *ppString)
{
if (offset >= m_cbData)
return E_FAIL;
m_readPtr = offset;
return Read(ppString);
}
_Use_decl_annotations_
HRESULT CMemoryStream::Read(void **ppData, size_t size)
{
size_t temp = m_readPtr + size;
if (temp < m_readPtr || temp > m_cbData)
return E_FAIL;
*ppData = m_pData + m_readPtr;
m_readPtr = temp;
return S_OK;
}
_Use_decl_annotations_
HRESULT CMemoryStream::Read(uint32_t *pDword)
{
uint32_t *pTempDword;
HRESULT hr;
hr = Read((void**) &pTempDword, sizeof(uint32_t));
if (FAILED(hr))
return E_FAIL;
*pDword = *pTempDword;
return S_OK;
}
_Use_decl_annotations_
HRESULT CMemoryStream::Read(LPCSTR *ppString)
{
size_t iChar=m_readPtr;
for(; m_pData[iChar]; iChar++)
{
if (iChar > m_cbData)
return E_FAIL;
}
*ppString = (LPCSTR) (m_pData + m_readPtr);
m_readPtr = iChar;
return S_OK;
}
size_t CMemoryStream::GetPosition()
{
return m_readPtr;
}
HRESULT CMemoryStream::Seek(_In_ size_t offset)
{
if (offset > m_cbData)
return E_FAIL;
m_readPtr = offset;
return S_OK;
}
}
//////////////////////////////////////////////////////////////////////////
// CDataBlock - used to dynamically build up the effect file in memory
//////////////////////////////////////////////////////////////////////////
CDataBlock::CDataBlock() noexcept :
m_size(0),
m_maxSize(0),
m_pData(nullptr),
m_pNext(nullptr),
m_IsAligned(false)
{
}
CDataBlock::~CDataBlock()
{
SAFE_DELETE_ARRAY(m_pData);
SAFE_DELETE(m_pNext);
}
void CDataBlock::EnableAlignment()
{
m_IsAligned = true;
}
_Use_decl_annotations_
HRESULT CDataBlock::AddData(const void *pvNewData, uint32_t bufferSize, CDataBlock **ppBlock)
{
HRESULT hr = S_OK;
uint32_t bytesToCopy;
const uint8_t *pNewData = (const uint8_t*) pvNewData;
if (m_maxSize == 0)
{
// This is a brand new DataBlock, fill it up
m_maxSize = std::max<uint32_t>(8192, bufferSize);
VN( m_pData = new uint8_t[m_maxSize] );
}
assert(m_pData == AlignToPowerOf2(m_pData, c_DataAlignment));
bytesToCopy = std::min(m_maxSize - m_size, bufferSize);
memcpy(m_pData + m_size, pNewData, bytesToCopy);
pNewData += bytesToCopy;
if (m_IsAligned)
{
assert(m_size == AlignToPowerOf2(m_size, c_DataAlignment));
m_size += AlignToPowerOf2(bytesToCopy, c_DataAlignment);
}
else
{
m_size += bytesToCopy;
}
bufferSize -= bytesToCopy;
*ppBlock = this;
if (bufferSize != 0)
{
assert(nullptr == m_pNext); // make sure we're not overwriting anything
// Couldn't fit all data into this block, spill over into next
VN( m_pNext = new CDataBlock() );
if (m_IsAligned)
{
m_pNext->EnableAlignment();
}
VH( m_pNext->AddData(pNewData, bufferSize, ppBlock) );
}
lExit:
return hr;
}
_Use_decl_annotations_
void* CDataBlock::Allocate(uint32_t bufferSize, CDataBlock **ppBlock)
{
void *pRetValue;
uint32_t temp = m_size + bufferSize;
if (temp < m_size)
return nullptr;
*ppBlock = this;
if (m_maxSize == 0)
{
// This is a brand new DataBlock, fill it up
m_maxSize = std::max<uint32_t>(8192, bufferSize);
m_pData = new uint8_t[m_maxSize];
if (!m_pData)
return nullptr;
memset(m_pData, 0xDD, m_maxSize);
}
else if (temp > m_maxSize)
{
assert(nullptr == m_pNext); // make sure we're not overwriting anything
// Couldn't fit data into this block, spill over into next
m_pNext = new CDataBlock();
if (!m_pNext)
return nullptr;
if (m_IsAligned)
{
m_pNext->EnableAlignment();
}
return m_pNext->Allocate(bufferSize, ppBlock);
}
assert(m_pData == AlignToPowerOf2(m_pData, c_DataAlignment));
pRetValue = m_pData + m_size;
if (m_IsAligned)
{
assert(m_size == AlignToPowerOf2(m_size, c_DataAlignment));
m_size = AlignToPowerOf2(temp, c_DataAlignment);
}
else
{
m_size = temp;
}
return pRetValue;
}
//////////////////////////////////////////////////////////////////////////
CDataBlockStore::CDataBlockStore() noexcept :
m_pFirst(nullptr),
m_pLast(nullptr),
m_Size(0),
m_Offset(0),
m_IsAligned(false)
{
#if _DEBUG
m_cAllocations = 0;
#endif
}
CDataBlockStore::~CDataBlockStore()
{
// Can't just do SAFE_DELETE(m_pFirst) since it blows the stack when deleting long chains of data
CDataBlock* pData = m_pFirst;
while(pData)
{
CDataBlock* pCurrent = pData;
pData = pData->m_pNext;
pCurrent->m_pNext = nullptr;
delete pCurrent;
}
// m_pLast will be deleted automatically
}
void CDataBlockStore::EnableAlignment()
{
m_IsAligned = true;
}
_Use_decl_annotations_
HRESULT CDataBlockStore::AddString(LPCSTR pString, uint32_t *pOffset)
{
size_t strSize = strlen(pString) + 1;
assert( strSize <= 0xffffffff );
return AddData(pString, (uint32_t)strSize, pOffset);
}
_Use_decl_annotations_
HRESULT CDataBlockStore::AddData(const void *pNewData, uint32_t bufferSize, uint32_t *pCurOffset)
{
HRESULT hr = S_OK;
if (bufferSize == 0)
{
if (pCurOffset)
{
*pCurOffset = 0;
}
goto lExit;
}
if (!m_pFirst)
{
VN( m_pFirst = new CDataBlock() );
if (m_IsAligned)
{
m_pFirst->EnableAlignment();
}
m_pLast = m_pFirst;
}
if (pCurOffset)
*pCurOffset = m_Size + m_Offset;
VH( m_pLast->AddData(pNewData, bufferSize, &m_pLast) );
m_Size += bufferSize;
lExit:
return hr;
}
void* CDataBlockStore::Allocate(_In_ uint32_t bufferSize)
{
void *pRetValue = nullptr;
#if _DEBUG
m_cAllocations++;
#endif
if (!m_pFirst)
{
m_pFirst = new CDataBlock();
if (!m_pFirst)
return nullptr;
if (m_IsAligned)
{
m_pFirst->EnableAlignment();
}
m_pLast = m_pFirst;
}
if (FAILED(UIntAdd(m_Size, bufferSize, &m_Size)))
return nullptr;
pRetValue = m_pLast->Allocate(bufferSize, &m_pLast);
if (!pRetValue)
return nullptr;
return pRetValue;
}
uint32_t CDataBlockStore::GetSize()
{
return m_Size;
}
//////////////////////////////////////////////////////////////////////////
static bool s_mute = false;
bool D3DX11DebugMute(bool mute)
{
bool previous = s_mute;
s_mute = mute;
return previous;
}
#ifdef _DEBUG
_Use_decl_annotations_
void __cdecl D3DXDebugPrintf(UINT lvl, LPCSTR szFormat, ...)
{
if (s_mute)
return;
UNREFERENCED_PARAMETER(lvl);
char strA[4096] = {};
char strB[4096] = {};
va_list ap;
va_start(ap, szFormat);
vsprintf_s(strA, sizeof(strA), szFormat, ap);
strA[4095] = '\0';
va_end(ap);
sprintf_s(strB, sizeof(strB), "Effects11: %s\r\n", strA);
strB[4095] = '\0';
OutputDebugStringA(strB);
}
#endif // _DEBUG
|