d3dxGlobal.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. //--------------------------------------------------------------------------------------
  2. // File: D3DXGlobal.h
  3. //
  4. // Direct3D 11 Effects helper defines and data structures
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  9. // PARTICULAR PURPOSE.
  10. //
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //
  13. // http://go.microsoft.com/fwlink/p/?LinkId=271568
  14. //--------------------------------------------------------------------------------------
  15. #pragma once
  16. #include <assert.h>
  17. #include <string.h>
  18. namespace D3DX11Debug
  19. {
  20. // Helper sets a D3D resource name string (used by PIX and debug layer leak reporting).
  21. inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_z_ const char *name )
  22. {
  23. #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
  24. resource->SetPrivateData( WKPDID_D3DDebugObjectName, static_cast<UINT>(strlen(name)), name );
  25. #else
  26. UNREFERENCED_PARAMETER(resource);
  27. UNREFERENCED_PARAMETER(name);
  28. #endif
  29. }
  30. template<UINT TNameLength>
  31. inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_z_ const char (&name)[TNameLength])
  32. {
  33. #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
  34. resource->SetPrivateData(WKPDID_D3DDebugObjectName, TNameLength - 1, name);
  35. #else
  36. UNREFERENCED_PARAMETER(resource);
  37. UNREFERENCED_PARAMETER(name);
  38. #endif
  39. }
  40. }
  41. using namespace D3DX11Debug;
  42. #define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p) = nullptr; } }
  43. #define SAFE_ADDREF(p) { if (p) { (p)->AddRef(); } }
  44. #define SAFE_DELETE_ARRAY(p) { delete [](p); p = nullptr; }
  45. #define SAFE_DELETE(p) { delete (p); p = nullptr; }
  46. #if FXDEBUG
  47. #define __BREAK_ON_FAIL { __debugbreak(); }
  48. #else
  49. #define __BREAK_ON_FAIL
  50. #endif
  51. #define VA(x, action) { hr = (x); if (FAILED(hr)) { action; __BREAK_ON_FAIL; return hr; } }
  52. #define VNA(x,action) { if (!(x)) { action; __BREAK_ON_FAIL; hr = E_OUTOFMEMORY; goto lExit; } }
  53. #define VBA(x,action) { if (!(x)) { action; __BREAK_ON_FAIL; hr = E_FAIL; goto lExit; } }
  54. #define VHA(x,action) { hr = (x); if (FAILED(hr)) { action; __BREAK_ON_FAIL; goto lExit; } }
  55. #define V(x) { VA (x, 0) }
  56. #define VN(x) { VNA(x, 0) }
  57. #define VB(x) { VBA(x, 0) }
  58. #define VH(x) { VHA(x, 0) }
  59. #define VBD(x,str) { VBA(x, DPF(1,str)) }
  60. #define VHD(x,str) { VHA(x, DPF(1,str)) }
  61. #define VEASSERT(x) { hr = (x); if (FAILED(hr)) { __BREAK_ON_FAIL; assert(!#x); goto lExit; } }
  62. #define VNASSERT(x) { if (!(x)) { __BREAK_ON_FAIL; assert(!#x); hr = E_OUTOFMEMORY; goto lExit; } }
  63. #define D3DX11FLTASSIGN(a,b) { *reinterpret_cast< UINT32* >(&(a)) = *reinterpret_cast< UINT32* >(&(b)); }
  64. // Preferred data alignment -- must be a power of 2!
  65. static const uint32_t c_DataAlignment = sizeof(UINT_PTR);
  66. inline uint32_t AlignToPowerOf2(uint32_t Value, uint32_t Alignment)
  67. {
  68. assert((Alignment & (Alignment - 1)) == 0);
  69. // to align to 2^N, add 2^N - 1 and AND with all but lowest N bits set
  70. _Analysis_assume_(Alignment > 0 && Value < MAXDWORD - Alignment);
  71. return (Value + Alignment - 1) & (~(Alignment - 1));
  72. }
  73. inline void * AlignToPowerOf2(void *pValue, UINT_PTR Alignment)
  74. {
  75. assert((Alignment & (Alignment - 1)) == 0);
  76. // to align to 2^N, add 2^N - 1 and AND with all but lowest N bits set
  77. return (void *)(((UINT_PTR)pValue + Alignment - 1) & (~((UINT_PTR)Alignment - 1)));
  78. }
  79. namespace D3DX11Core
  80. {
  81. //////////////////////////////////////////////////////////////////////////
  82. // CMemoryStream - A class to simplify reading binary data
  83. //////////////////////////////////////////////////////////////////////////
  84. class CMemoryStream
  85. {
  86. uint8_t *m_pData;
  87. size_t m_cbData;
  88. size_t m_readPtr;
  89. public:
  90. HRESULT SetData(_In_reads_bytes_(size) const void *pData, _In_ size_t size);
  91. HRESULT Read(_Out_ uint32_t *pUint);
  92. HRESULT Read(_Outptr_result_buffer_(size) void **ppData, _In_ size_t size);
  93. HRESULT Read(_Outptr_ LPCSTR *ppString);
  94. HRESULT ReadAtOffset(_In_ size_t offset, _In_ size_t size, _Outptr_result_buffer_(size) void **ppData);
  95. HRESULT ReadAtOffset(_In_ size_t offset, _Outptr_result_z_ LPCSTR *ppString);
  96. size_t GetPosition();
  97. HRESULT Seek(_In_ size_t offset);
  98. CMemoryStream();
  99. ~CMemoryStream();
  100. };
  101. }
  102. #if defined(_DEBUG) && !defined(_M_X64)
  103. namespace D3DX11Debug
  104. {
  105. // This variable indicates how many ticks to go before rolling over
  106. // all of the timer variables in the FX system.
  107. // It is read from the system registry in debug builds; in retail the high bit is simply tested.
  108. _declspec(selectany) unsigned int g_TimerRolloverCount = 0x80000000;
  109. }
  110. #endif // _DEBUG && !_M_X64
  111. //////////////////////////////////////////////////////////////////////////
  112. // CEffectVector - A vector implementation
  113. //////////////////////////////////////////////////////////////////////////
  114. template<class T> class CEffectVector
  115. {
  116. protected:
  117. #if _DEBUG
  118. T *m_pCastData; // makes debugging easier to have a casted version of the data
  119. #endif // _DEBUG
  120. uint8_t *m_pData;
  121. uint32_t m_MaxSize;
  122. uint32_t m_CurSize;
  123. HRESULT Grow()
  124. {
  125. return Reserve(m_CurSize + 1);
  126. }
  127. HRESULT Reserve(_In_ uint32_t DesiredSize)
  128. {
  129. if (DesiredSize > m_MaxSize)
  130. {
  131. uint8_t *pNewData;
  132. uint32_t newSize = std::max(m_MaxSize * 2, DesiredSize);
  133. if (newSize < 16)
  134. newSize = 16;
  135. if ((newSize < m_MaxSize) || (newSize < m_CurSize) || (newSize >= UINT_MAX / sizeof(T)))
  136. {
  137. m_hLastError = E_OUTOFMEMORY;
  138. return m_hLastError;
  139. }
  140. pNewData = new uint8_t[newSize * sizeof(T)];
  141. if (pNewData == nullptr)
  142. {
  143. m_hLastError = E_OUTOFMEMORY;
  144. return m_hLastError;
  145. }
  146. if (m_pData)
  147. {
  148. memcpy(pNewData, m_pData, m_CurSize * sizeof(T));
  149. delete []m_pData;
  150. }
  151. m_pData = pNewData;
  152. m_MaxSize = newSize;
  153. }
  154. #if _DEBUG
  155. m_pCastData = (T*) m_pData;
  156. #endif // _DEBUG
  157. return S_OK;
  158. }
  159. public:
  160. HRESULT m_hLastError;
  161. CEffectVector<T>() : m_hLastError(S_OK), m_pData(nullptr), m_CurSize(0), m_MaxSize(0)
  162. {
  163. #if _DEBUG
  164. m_pCastData = nullptr;
  165. #endif // _DEBUG
  166. }
  167. ~CEffectVector<T>()
  168. {
  169. Clear();
  170. }
  171. // cleanly swaps two vectors -- useful for when you want
  172. // to reallocate a vector and copy data over, then swap them back
  173. void SwapVector(_Out_ CEffectVector<T> &vOther)
  174. {
  175. uint8_t tempData[sizeof(*this)];
  176. memcpy(tempData, this, sizeof(*this));
  177. memcpy(this, &vOther, sizeof(*this));
  178. memcpy(&vOther, tempData, sizeof(*this));
  179. }
  180. HRESULT CopyFrom(_In_ const CEffectVector<T> &vOther)
  181. {
  182. HRESULT hr = S_OK;
  183. Clear();
  184. VN( m_pData = new uint8_t[vOther.m_MaxSize * sizeof(T)] );
  185. m_CurSize = vOther.m_CurSize;
  186. m_MaxSize = vOther.m_MaxSize;
  187. m_hLastError = vOther.m_hLastError;
  188. for (size_t i = 0; i < m_CurSize; ++ i)
  189. {
  190. ((T*)m_pData)[i] = ((T*)vOther.m_pData)[i];
  191. }
  192. lExit:
  193. #if _DEBUG
  194. m_pCastData = (T*) m_pData;
  195. #endif // _DEBUG
  196. return hr;
  197. }
  198. void Clear()
  199. {
  200. Empty();
  201. SAFE_DELETE_ARRAY(m_pData);
  202. m_MaxSize = 0;
  203. #if _DEBUG
  204. m_pCastData = nullptr;
  205. #endif // _DEBUG
  206. }
  207. void ClearWithoutDestructor()
  208. {
  209. m_CurSize = 0;
  210. m_hLastError = S_OK;
  211. SAFE_DELETE_ARRAY(m_pData);
  212. m_MaxSize = 0;
  213. #if _DEBUG
  214. m_pCastData = nullptr;
  215. #endif // _DEBUG
  216. }
  217. void Empty()
  218. {
  219. // manually invoke destructor on all elements
  220. for (size_t i = 0; i < m_CurSize; ++ i)
  221. {
  222. ((T*)m_pData + i)->~T();
  223. }
  224. m_CurSize = 0;
  225. m_hLastError = S_OK;
  226. }
  227. T* Add()
  228. {
  229. if (FAILED(Grow()))
  230. return nullptr;
  231. // placement new
  232. return new((T*)m_pData + (m_CurSize ++)) T;
  233. }
  234. T* AddRange(_In_ uint32_t count)
  235. {
  236. if (m_CurSize + count < m_CurSize)
  237. {
  238. m_hLastError = E_OUTOFMEMORY;
  239. return nullptr;
  240. }
  241. if (FAILED(Reserve(m_CurSize + count)))
  242. return nullptr;
  243. T *pData = (T*)m_pData + m_CurSize;
  244. for (size_t i = 0; i < count; ++ i)
  245. {
  246. new(pData + i) T;
  247. }
  248. m_CurSize += count;
  249. return pData;
  250. }
  251. HRESULT Add(_In_ const T& var)
  252. {
  253. if (FAILED(Grow()))
  254. return m_hLastError;
  255. memcpy((T*)m_pData + m_CurSize, &var, sizeof(T));
  256. m_CurSize++;
  257. return S_OK;
  258. }
  259. HRESULT AddRange(_In_reads_(count) const T *pVar, _In_ uint32_t count)
  260. {
  261. if (m_CurSize + count < m_CurSize)
  262. {
  263. m_hLastError = E_OUTOFMEMORY;
  264. return m_hLastError;
  265. }
  266. if (FAILED(Reserve(m_CurSize + count)))
  267. return m_hLastError;
  268. memcpy((T*)m_pData + m_CurSize, pVar, count * sizeof(T));
  269. m_CurSize += count;
  270. return S_OK;
  271. }
  272. HRESULT Insert(_In_ const T& var, _In_ uint32_t index)
  273. {
  274. assert(index < m_CurSize);
  275. if (FAILED(Grow()))
  276. return m_hLastError;
  277. memmove((T*)m_pData + index + 1, (T*)m_pData + index, (m_CurSize - index) * sizeof(T));
  278. memcpy((T*)m_pData + index, &var, sizeof(T));
  279. m_CurSize++;
  280. return S_OK;
  281. }
  282. HRESULT InsertRange(_In_reads_(count) const T *pVar, _In_ uint32_t index, _In_ uint32_t count)
  283. {
  284. assert(index < m_CurSize);
  285. if (m_CurSize + count < m_CurSize)
  286. {
  287. m_hLastError = E_OUTOFMEMORY;
  288. return m_hLastError;
  289. }
  290. if (FAILED(Reserve(m_CurSize + count)))
  291. return m_hLastError;
  292. memmove((T*)m_pData + index + count, (T*)m_pData + index, (m_CurSize - index) * sizeof(T));
  293. memcpy((T*)m_pData + index, pVar, count * sizeof(T));
  294. m_CurSize += count;
  295. return S_OK;
  296. }
  297. inline T& operator[](_In_ size_t index)
  298. {
  299. assert(index < m_CurSize);
  300. return ((T*)m_pData)[index];
  301. }
  302. // Deletes element at index and shifts all other values down
  303. void Delete(_In_ uint32_t index)
  304. {
  305. assert(index < m_CurSize);
  306. -- m_CurSize;
  307. memmove((T*)m_pData + index, (T*)m_pData + index + 1, (m_CurSize - index) * sizeof(T));
  308. }
  309. // Deletes element at index and moves the last element into its place
  310. void QuickDelete(_In_ uint32_t index)
  311. {
  312. assert(index < m_CurSize);
  313. -- m_CurSize;
  314. memcpy((T*)m_pData + index, (T*)m_pData + m_CurSize, sizeof(T));
  315. }
  316. inline uint32_t GetSize() const
  317. {
  318. return m_CurSize;
  319. }
  320. inline T* GetData() const
  321. {
  322. return (T*)m_pData;
  323. }
  324. uint32_t FindIndexOf(_In_ const void *pEntry) const
  325. {
  326. for (size_t i = 0; i < m_CurSize; ++ i)
  327. {
  328. if (((T*)m_pData + i) == pEntry)
  329. return i;
  330. }
  331. return -1;
  332. }
  333. void Sort(int (__cdecl *pfnCompare)(const void *pElem1, const void *pElem2))
  334. {
  335. qsort(m_pData, m_CurSize, sizeof(T), pfnCompare);
  336. }
  337. };
  338. //////////////////////////////////////////////////////////////////////////
  339. // CEffectVectorOwner - implements a vector of ptrs to objects. The vector owns the objects.
  340. //////////////////////////////////////////////////////////////////////////
  341. template<class T> class CEffectVectorOwner : public CEffectVector<T*>
  342. {
  343. public:
  344. ~CEffectVectorOwner<T>()
  345. {
  346. Clear();
  347. for (size_t i=0; i<m_CurSize; i++)
  348. SAFE_DELETE(((T**)m_pData)[i]);
  349. SAFE_DELETE_ARRAY(m_pData);
  350. }
  351. void Clear()
  352. {
  353. Empty();
  354. SAFE_DELETE_ARRAY(m_pData);
  355. m_MaxSize = 0;
  356. }
  357. void Empty()
  358. {
  359. // manually invoke destructor on all elements
  360. for (size_t i = 0; i < m_CurSize; ++ i)
  361. {
  362. SAFE_DELETE(((T**)m_pData)[i]);
  363. }
  364. m_CurSize = 0;
  365. m_hLastError = S_OK;
  366. }
  367. void Delete(_In_ uint32_t index)
  368. {
  369. assert(index < m_CurSize);
  370. SAFE_DELETE(((T**)m_pData)[index]);
  371. CEffectVector<T*>::Delete(index);
  372. }
  373. };
  374. //////////////////////////////////////////////////////////////////////////
  375. // Checked uint32_t, uint64_t
  376. // Use CheckedNumber only with uint32_t and uint64_t
  377. //////////////////////////////////////////////////////////////////////////
  378. template <class T, T MaxValue> class CheckedNumber
  379. {
  380. T m_Value;
  381. bool m_bValid;
  382. public:
  383. CheckedNumber<T, MaxValue>() : m_Value(0), m_bValid(true)
  384. {
  385. }
  386. CheckedNumber<T, MaxValue>(const T &value) : m_Value(value), m_bValid(true)
  387. {
  388. }
  389. CheckedNumber<T, MaxValue>(const CheckedNumber<T, MaxValue> &value) : m_bValid(value.m_bValid), m_Value(value.m_Value)
  390. {
  391. }
  392. CheckedNumber<T, MaxValue> &operator+(const CheckedNumber<T, MaxValue> &other)
  393. {
  394. CheckedNumber<T, MaxValue> Res(*this);
  395. return Res+=other;
  396. }
  397. CheckedNumber<T, MaxValue> &operator+=(const CheckedNumber<T, MaxValue> &other)
  398. {
  399. if (!other.m_bValid)
  400. {
  401. m_bValid = false;
  402. }
  403. else
  404. {
  405. m_Value += other.m_Value;
  406. if (m_Value < other.m_Value)
  407. m_bValid = false;
  408. }
  409. return *this;
  410. }
  411. CheckedNumber<T, MaxValue> &operator*(const CheckedNumber<T, MaxValue> &other)
  412. {
  413. CheckedNumber<T, MaxValue> Res(*this);
  414. return Res*=other;
  415. }
  416. CheckedNumber<T, MaxValue> &operator*=(const CheckedNumber<T, MaxValue> &other)
  417. {
  418. if (!other.m_bValid)
  419. {
  420. m_bValid = false;
  421. }
  422. else
  423. {
  424. if (other.m_Value != 0)
  425. {
  426. if (m_Value > MaxValue / other.m_Value)
  427. {
  428. m_bValid = false;
  429. }
  430. }
  431. m_Value *= other.m_Value;
  432. }
  433. return *this;
  434. }
  435. HRESULT GetValue(_Out_ T *pValue)
  436. {
  437. if (!m_bValid)
  438. {
  439. *pValue = uint32_t(-1);
  440. return E_FAIL;
  441. }
  442. *pValue = m_Value;
  443. return S_OK;
  444. }
  445. };
  446. typedef CheckedNumber<uint32_t, _UI32_MAX> CCheckedDword;
  447. typedef CheckedNumber<uint64_t, _UI64_MAX> CCheckedDword64;
  448. //////////////////////////////////////////////////////////////////////////
  449. // Data Block Store - A linked list of allocations
  450. //////////////////////////////////////////////////////////////////////////
  451. class CDataBlock
  452. {
  453. protected:
  454. uint32_t m_size;
  455. uint32_t m_maxSize;
  456. uint8_t *m_pData;
  457. CDataBlock *m_pNext;
  458. bool m_IsAligned; // Whether or not to align the data to c_DataAlignment
  459. public:
  460. // AddData appends an existing use buffer to the data block
  461. HRESULT AddData(_In_reads_bytes_(bufferSize) const void *pNewData, _In_ uint32_t bufferSize, _Outptr_ CDataBlock **ppBlock);
  462. // Allocate reserves bufferSize bytes of contiguous memory and returns a pointer to the user
  463. _Success_(return != nullptr)
  464. void* Allocate(_In_ uint32_t bufferSize, _Outptr_ CDataBlock **ppBlock);
  465. void EnableAlignment();
  466. CDataBlock();
  467. ~CDataBlock();
  468. friend class CDataBlockStore;
  469. };
  470. class CDataBlockStore
  471. {
  472. protected:
  473. CDataBlock *m_pFirst;
  474. CDataBlock *m_pLast;
  475. uint32_t m_Size;
  476. uint32_t m_Offset; // m_Offset gets added to offsets returned from AddData & AddString. Use this to set a global for the entire string block
  477. bool m_IsAligned; // Whether or not to align the data to c_DataAlignment
  478. public:
  479. #if _DEBUG
  480. uint32_t m_cAllocations;
  481. #endif
  482. public:
  483. HRESULT AddString(_In_z_ LPCSTR pString, _Inout_ uint32_t *pOffset);
  484. // Writes a null-terminated string to buffer
  485. HRESULT AddData(_In_reads_bytes_(bufferSize) const void *pNewData, _In_ uint32_t bufferSize, _Inout_ uint32_t *pOffset);
  486. // Writes data block to buffer
  487. // Memory allocator support
  488. void* Allocate(_In_ uint32_t bufferSize);
  489. uint32_t GetSize();
  490. void EnableAlignment();
  491. CDataBlockStore();
  492. ~CDataBlockStore();
  493. };
  494. // Custom allocator that uses CDataBlockStore
  495. // The trick is that we never free, so we don't have to keep as much state around
  496. // Use PRIVATENEW in CEffectLoader
  497. inline void* __cdecl operator new(_In_ size_t s, _In_ CDataBlockStore &pAllocator)
  498. {
  499. #ifdef _M_X64
  500. assert( s <= 0xffffffff );
  501. #endif
  502. return pAllocator.Allocate( (uint32_t)s );
  503. }
  504. inline void __cdecl operator delete(_In_opt_ void* p, _In_ CDataBlockStore &pAllocator)
  505. {
  506. UNREFERENCED_PARAMETER(p);
  507. UNREFERENCED_PARAMETER(pAllocator);
  508. }
  509. //////////////////////////////////////////////////////////////////////////
  510. // Hash table
  511. //////////////////////////////////////////////////////////////////////////
  512. #define HASH_MIX(a,b,c) \
  513. { \
  514. a -= b; a -= c; a ^= (c>>13); \
  515. b -= c; b -= a; b ^= (a<<8); \
  516. c -= a; c -= b; c ^= (b>>13); \
  517. a -= b; a -= c; a ^= (c>>12); \
  518. b -= c; b -= a; b ^= (a<<16); \
  519. c -= a; c -= b; c ^= (b>>5); \
  520. a -= b; a -= c; a ^= (c>>3); \
  521. b -= c; b -= a; b ^= (a<<10); \
  522. c -= a; c -= b; c ^= (b>>15); \
  523. }
  524. static uint32_t ComputeHash(_In_reads_bytes_(cbToHash) const uint8_t *pb, _In_ uint32_t cbToHash)
  525. {
  526. uint32_t cbLeft = cbToHash;
  527. uint32_t a;
  528. uint32_t b;
  529. a = b = 0x9e3779b9; // the golden ratio; an arbitrary value
  530. uint32_t c = 0;
  531. while (cbLeft >= 12)
  532. {
  533. const uint32_t *pdw = reinterpret_cast<const uint32_t *>(pb);
  534. a += pdw[0];
  535. b += pdw[1];
  536. c += pdw[2];
  537. HASH_MIX(a,b,c);
  538. pb += 12;
  539. cbLeft -= 12;
  540. }
  541. c += cbToHash;
  542. switch(cbLeft) // all the case statements fall through
  543. {
  544. case 11: c+=((uint32_t) pb[10] << 24);
  545. case 10: c+=((uint32_t) pb[9] << 16);
  546. case 9 : c+=((uint32_t) pb[8] << 8);
  547. // the first byte of c is reserved for the length
  548. case 8 : b+=((uint32_t) pb[7] << 24);
  549. case 7 : b+=((uint32_t) pb[6] << 16);
  550. case 6 : b+=((uint32_t) pb[5] << 8);
  551. case 5 : b+=pb[4];
  552. case 4 : a+=((uint32_t) pb[3] << 24);
  553. case 3 : a+=((uint32_t) pb[2] << 16);
  554. case 2 : a+=((uint32_t) pb[1] << 8);
  555. case 1 : a+=pb[0];
  556. }
  557. HASH_MIX(a,b,c);
  558. return c;
  559. }
  560. static uint32_t ComputeHashLower(_In_reads_bytes_(cbToHash) const uint8_t *pb, _In_ uint32_t cbToHash)
  561. {
  562. uint32_t cbLeft = cbToHash;
  563. uint32_t a;
  564. uint32_t b;
  565. a = b = 0x9e3779b9; // the golden ratio; an arbitrary value
  566. uint32_t c = 0;
  567. while (cbLeft >= 12)
  568. {
  569. uint8_t pbT[12];
  570. for( size_t i = 0; i < 12; i++ )
  571. pbT[i] = (uint8_t)tolower(pb[i]);
  572. uint32_t *pdw = reinterpret_cast<uint32_t *>(pbT);
  573. a += pdw[0];
  574. b += pdw[1];
  575. c += pdw[2];
  576. HASH_MIX(a,b,c);
  577. pb += 12;
  578. cbLeft -= 12;
  579. }
  580. c += cbToHash;
  581. uint8_t pbT[12];
  582. for( size_t i = 0; i < cbLeft; i++ )
  583. pbT[i] = (uint8_t)tolower(pb[i]);
  584. switch(cbLeft) // all the case statements fall through
  585. {
  586. case 11: c+=((uint32_t) pbT[10] << 24);
  587. case 10: c+=((uint32_t) pbT[9] << 16);
  588. case 9 : c+=((uint32_t) pbT[8] << 8);
  589. // the first byte of c is reserved for the length
  590. case 8 : b+=((uint32_t) pbT[7] << 24);
  591. case 7 : b+=((uint32_t) pbT[6] << 16);
  592. case 6 : b+=((uint32_t) pbT[5] << 8);
  593. case 5 : b+=pbT[4];
  594. case 4 : a+=((uint32_t) pbT[3] << 24);
  595. case 3 : a+=((uint32_t) pbT[2] << 16);
  596. case 2 : a+=((uint32_t) pbT[1] << 8);
  597. case 1 : a+=pbT[0];
  598. }
  599. HASH_MIX(a,b,c);
  600. return c;
  601. }
  602. static uint32_t ComputeHash(_In_z_ LPCSTR pString)
  603. {
  604. return ComputeHash(reinterpret_cast<const uint8_t*>(pString), (uint32_t)strlen(pString));
  605. }
  606. // 1) these numbers are prime
  607. // 2) each is slightly less than double the last
  608. // 4) each is roughly in between two powers of 2;
  609. // (2^n hash table sizes are VERY BAD; they effectively truncate your
  610. // precision down to the n least significant bits of the hash)
  611. static const uint32_t c_PrimeSizes[] =
  612. {
  613. 11,
  614. 23,
  615. 53,
  616. 97,
  617. 193,
  618. 389,
  619. 769,
  620. 1543,
  621. 3079,
  622. 6151,
  623. 12289,
  624. 24593,
  625. 49157,
  626. 98317,
  627. 196613,
  628. 393241,
  629. 786433,
  630. 1572869,
  631. 3145739,
  632. 6291469,
  633. 12582917,
  634. 25165843,
  635. 50331653,
  636. 100663319,
  637. 201326611,
  638. 402653189,
  639. 805306457,
  640. 1610612741,
  641. };
  642. template<typename T, bool (*pfnIsEqual)(const T &Data1, const T &Data2)>
  643. class CEffectHashTable
  644. {
  645. protected:
  646. struct SHashEntry
  647. {
  648. uint32_t Hash;
  649. T Data;
  650. SHashEntry *pNext;
  651. };
  652. // Array of hash entries
  653. SHashEntry **m_rgpHashEntries;
  654. uint32_t m_NumHashSlots;
  655. uint32_t m_NumEntries;
  656. bool m_bOwnHashEntryArray;
  657. public:
  658. class CIterator
  659. {
  660. friend class CEffectHashTable;
  661. protected:
  662. SHashEntry **ppHashSlot;
  663. SHashEntry *pHashEntry;
  664. public:
  665. T GetData()
  666. {
  667. assert(pHashEntry != 0);
  668. _Analysis_assume_(pHashEntry != 0);
  669. return pHashEntry->Data;
  670. }
  671. uint32_t GetHash()
  672. {
  673. assert(pHashEntry != 0);
  674. _Analysis_assume_(pHashEntry != 0);
  675. return pHashEntry->Hash;
  676. }
  677. };
  678. CEffectHashTable() : m_rgpHashEntries(nullptr), m_NumHashSlots(0), m_NumEntries(0), m_bOwnHashEntryArray(false)
  679. {
  680. }
  681. HRESULT Initialize(_In_ const CEffectHashTable *pOther)
  682. {
  683. HRESULT hr = S_OK;
  684. SHashEntry **rgpNewHashEntries = nullptr;
  685. uint32_t valuesMigrated = 0;
  686. uint32_t actualSize;
  687. Cleanup();
  688. actualSize = pOther->m_NumHashSlots;
  689. VN( rgpNewHashEntries = new SHashEntry*[actualSize] );
  690. ZeroMemory(rgpNewHashEntries, sizeof(SHashEntry*) * actualSize);
  691. // Expensive operation: rebuild the hash table
  692. CIterator iter, nextIter;
  693. pOther->GetFirstEntry(&iter);
  694. while (!pOther->PastEnd(&iter))
  695. {
  696. uint32_t index = iter.GetHash() % actualSize;
  697. // we need to advance to the next element
  698. // before we seize control of this element and move
  699. // it to the new table
  700. nextIter = iter;
  701. pOther->GetNextEntry(&nextIter);
  702. // seize this hash entry, migrate it to the new table
  703. SHashEntry *pNewEntry;
  704. VN( pNewEntry = new SHashEntry );
  705. pNewEntry->pNext = rgpNewHashEntries[index];
  706. pNewEntry->Data = iter.pHashEntry->Data;
  707. pNewEntry->Hash = iter.pHashEntry->Hash;
  708. rgpNewHashEntries[index] = pNewEntry;
  709. iter = nextIter;
  710. ++ valuesMigrated;
  711. }
  712. assert(valuesMigrated == pOther->m_NumEntries);
  713. m_rgpHashEntries = rgpNewHashEntries;
  714. m_NumHashSlots = actualSize;
  715. m_NumEntries = pOther->m_NumEntries;
  716. m_bOwnHashEntryArray = true;
  717. rgpNewHashEntries = nullptr;
  718. lExit:
  719. SAFE_DELETE_ARRAY( rgpNewHashEntries );
  720. return hr;
  721. }
  722. protected:
  723. void CleanArray()
  724. {
  725. if (m_bOwnHashEntryArray)
  726. {
  727. SAFE_DELETE_ARRAY(m_rgpHashEntries);
  728. m_bOwnHashEntryArray = false;
  729. }
  730. }
  731. public:
  732. void Cleanup()
  733. {
  734. for (size_t i = 0; i < m_NumHashSlots; ++ i)
  735. {
  736. SHashEntry *pCurrentEntry = m_rgpHashEntries[i];
  737. SHashEntry *pTempEntry;
  738. while (nullptr != pCurrentEntry)
  739. {
  740. pTempEntry = pCurrentEntry->pNext;
  741. SAFE_DELETE(pCurrentEntry);
  742. pCurrentEntry = pTempEntry;
  743. -- m_NumEntries;
  744. }
  745. }
  746. CleanArray();
  747. m_NumHashSlots = 0;
  748. assert(m_NumEntries == 0);
  749. }
  750. ~CEffectHashTable()
  751. {
  752. Cleanup();
  753. }
  754. static uint32_t GetNextHashTableSize(_In_ uint32_t DesiredSize)
  755. {
  756. // figure out the next logical size to use
  757. for (size_t i = 0; i < _countof(c_PrimeSizes); ++i )
  758. {
  759. if (c_PrimeSizes[i] >= DesiredSize)
  760. {
  761. return c_PrimeSizes[i];
  762. }
  763. }
  764. return DesiredSize;
  765. }
  766. // O(n) function
  767. // Grows to the next suitable size (based off of the prime number table)
  768. // DesiredSize is merely a suggestion
  769. HRESULT Grow(_In_ uint32_t DesiredSize,
  770. _In_ uint32_t ProvidedArraySize = 0,
  771. _In_reads_opt_(ProvidedArraySize) void** ProvidedArray = nullptr,
  772. _In_ bool OwnProvidedArray = false)
  773. {
  774. HRESULT hr = S_OK;
  775. SHashEntry **rgpNewHashEntries = nullptr;
  776. uint32_t valuesMigrated = 0;
  777. uint32_t actualSize;
  778. VB( DesiredSize > m_NumHashSlots );
  779. actualSize = GetNextHashTableSize(DesiredSize);
  780. if (ProvidedArray &&
  781. ProvidedArraySize >= actualSize)
  782. {
  783. rgpNewHashEntries = reinterpret_cast<SHashEntry**>(ProvidedArray);
  784. }
  785. else
  786. {
  787. OwnProvidedArray = true;
  788. VN( rgpNewHashEntries = new SHashEntry*[actualSize] );
  789. }
  790. ZeroMemory(rgpNewHashEntries, sizeof(SHashEntry*) * actualSize);
  791. // Expensive operation: rebuild the hash table
  792. CIterator iter, nextIter;
  793. GetFirstEntry(&iter);
  794. while (!PastEnd(&iter))
  795. {
  796. uint32_t index = iter.GetHash() % actualSize;
  797. // we need to advance to the next element
  798. // before we seize control of this element and move
  799. // it to the new table
  800. nextIter = iter;
  801. GetNextEntry(&nextIter);
  802. // seize this hash entry, migrate it to the new table
  803. iter.pHashEntry->pNext = rgpNewHashEntries[index];
  804. rgpNewHashEntries[index] = iter.pHashEntry;
  805. iter = nextIter;
  806. ++ valuesMigrated;
  807. }
  808. assert(valuesMigrated == m_NumEntries);
  809. CleanArray();
  810. m_rgpHashEntries = rgpNewHashEntries;
  811. m_NumHashSlots = actualSize;
  812. m_bOwnHashEntryArray = OwnProvidedArray;
  813. lExit:
  814. return hr;
  815. }
  816. HRESULT AutoGrow()
  817. {
  818. // arbitrary heuristic -- grow if 1:1
  819. if (m_NumEntries >= m_NumHashSlots)
  820. {
  821. // grows this hash table so that it is roughly 50% full
  822. return Grow(m_NumEntries * 2 + 1);
  823. }
  824. return S_OK;
  825. }
  826. #if _DEBUG
  827. void PrintHashTableStats()
  828. {
  829. if (m_NumHashSlots == 0)
  830. {
  831. DPF(0, "Uninitialized hash table!");
  832. return;
  833. }
  834. float variance = 0.0f;
  835. float mean = (float)m_NumEntries / (float)m_NumHashSlots;
  836. uint32_t unusedSlots = 0;
  837. DPF(0, "Hash table slots: %d, Entries in table: %d", m_NumHashSlots, m_NumEntries);
  838. for (size_t i = 0; i < m_NumHashSlots; ++ i)
  839. {
  840. uint32_t entries = 0;
  841. SHashEntry *pCurrentEntry = m_rgpHashEntries[i];
  842. while (nullptr != pCurrentEntry)
  843. {
  844. SHashEntry *pCurrentEntry2 = m_rgpHashEntries[i];
  845. // check other hash entries in this slot for hash collisions or duplications
  846. while (pCurrentEntry2 != pCurrentEntry)
  847. {
  848. if (pCurrentEntry->Hash == pCurrentEntry2->Hash)
  849. {
  850. if (pfnIsEqual(pCurrentEntry->Data, pCurrentEntry2->Data))
  851. {
  852. assert(0);
  853. DPF(0, "Duplicate entry (identical hash, identical data) found!");
  854. }
  855. else
  856. {
  857. DPF(0, "Hash collision (hash: %d)", pCurrentEntry->Hash);
  858. }
  859. }
  860. pCurrentEntry2 = pCurrentEntry2->pNext;
  861. }
  862. pCurrentEntry = pCurrentEntry->pNext;
  863. ++ entries;
  864. }
  865. if (0 == entries)
  866. {
  867. ++ unusedSlots;
  868. }
  869. // mean must be greater than 0 at this point
  870. variance += (float)entries * (float)entries / mean;
  871. }
  872. variance /= std::max(1.0f, (m_NumHashSlots - 1));
  873. variance -= (mean * mean);
  874. DPF(0, "Mean number of entries per slot: %f, Standard deviation: %f, Unused slots; %d", mean, variance, unusedSlots);
  875. }
  876. #endif // _DEBUG
  877. // S_OK if element is found, E_FAIL otherwise
  878. HRESULT FindValueWithHash(_In_ T Data, _In_ uint32_t Hash, _Out_ CIterator *pIterator)
  879. {
  880. assert(m_NumHashSlots > 0);
  881. uint32_t index = Hash % m_NumHashSlots;
  882. SHashEntry *pEntry = m_rgpHashEntries[index];
  883. while (nullptr != pEntry)
  884. {
  885. if (Hash == pEntry->Hash && pfnIsEqual(pEntry->Data, Data))
  886. {
  887. pIterator->ppHashSlot = m_rgpHashEntries + index;
  888. pIterator->pHashEntry = pEntry;
  889. return S_OK;
  890. }
  891. pEntry = pEntry->pNext;
  892. }
  893. return E_FAIL;
  894. }
  895. // S_OK if element is found, E_FAIL otherwise
  896. HRESULT FindFirstMatchingValue(_In_ uint32_t Hash, _Out_ CIterator *pIterator)
  897. {
  898. assert(m_NumHashSlots > 0);
  899. uint32_t index = Hash % m_NumHashSlots;
  900. SHashEntry *pEntry = m_rgpHashEntries[index];
  901. while (nullptr != pEntry)
  902. {
  903. if (Hash == pEntry->Hash)
  904. {
  905. pIterator->ppHashSlot = m_rgpHashEntries + index;
  906. pIterator->pHashEntry = pEntry;
  907. return S_OK;
  908. }
  909. pEntry = pEntry->pNext;
  910. }
  911. return E_FAIL;
  912. }
  913. // Adds data at the specified hash slot without checking for existence
  914. HRESULT AddValueWithHash(_In_ T Data, _In_ uint32_t Hash)
  915. {
  916. HRESULT hr = S_OK;
  917. assert(m_NumHashSlots > 0);
  918. SHashEntry *pHashEntry;
  919. uint32_t index = Hash % m_NumHashSlots;
  920. VN( pHashEntry = new SHashEntry );
  921. pHashEntry->pNext = m_rgpHashEntries[index];
  922. pHashEntry->Data = Data;
  923. pHashEntry->Hash = Hash;
  924. m_rgpHashEntries[index] = pHashEntry;
  925. ++ m_NumEntries;
  926. lExit:
  927. return hr;
  928. }
  929. // Iterator code:
  930. //
  931. // CMyHashTable::CIterator myIt;
  932. // for (myTable.GetFirstEntry(&myIt); !myTable.PastEnd(&myIt); myTable.GetNextEntry(&myIt)
  933. // { myTable.GetData(&myIt); }
  934. void GetFirstEntry(_Out_ CIterator *pIterator)
  935. {
  936. SHashEntry **ppEnd = m_rgpHashEntries + m_NumHashSlots;
  937. pIterator->ppHashSlot = m_rgpHashEntries;
  938. while (pIterator->ppHashSlot < ppEnd)
  939. {
  940. if (nullptr != *(pIterator->ppHashSlot))
  941. {
  942. pIterator->pHashEntry = *(pIterator->ppHashSlot);
  943. return;
  944. }
  945. ++ pIterator->ppHashSlot;
  946. }
  947. }
  948. bool PastEnd(_Inout_ CIterator *pIterator)
  949. {
  950. SHashEntry **ppEnd = m_rgpHashEntries + m_NumHashSlots;
  951. assert(pIterator->ppHashSlot >= m_rgpHashEntries && pIterator->ppHashSlot <= ppEnd);
  952. return (pIterator->ppHashSlot == ppEnd);
  953. }
  954. void GetNextEntry(_Inout_ CIterator *pIterator)
  955. {
  956. SHashEntry **ppEnd = m_rgpHashEntries + m_NumHashSlots;
  957. assert(pIterator->ppHashSlot >= m_rgpHashEntries && pIterator->ppHashSlot <= ppEnd);
  958. assert(pIterator->pHashEntry != 0);
  959. _Analysis_assume_(pIterator->pHashEntry != 0);
  960. pIterator->pHashEntry = pIterator->pHashEntry->pNext;
  961. if (nullptr != pIterator->pHashEntry)
  962. {
  963. return;
  964. }
  965. ++ pIterator->ppHashSlot;
  966. while (pIterator->ppHashSlot < ppEnd)
  967. {
  968. pIterator->pHashEntry = *(pIterator->ppHashSlot);
  969. if (nullptr != pIterator->pHashEntry)
  970. {
  971. return;
  972. }
  973. ++ pIterator->ppHashSlot;
  974. }
  975. // hit the end of the list, ppHashSlot == ppEnd
  976. }
  977. void RemoveEntry(_Inout_ CIterator *pIterator)
  978. {
  979. SHashEntry *pTemp;
  980. SHashEntry **ppPrev;
  981. SHashEntry **ppEnd = m_rgpHashEntries + m_NumHashSlots;
  982. assert(pIterator && !PastEnd(pIterator));
  983. ppPrev = pIterator->ppHashSlot;
  984. pTemp = *ppPrev;
  985. while (pTemp)
  986. {
  987. if (pTemp == pIterator->pHashEntry)
  988. {
  989. *ppPrev = pTemp->pNext;
  990. pIterator->ppHashSlot = ppEnd;
  991. delete pTemp;
  992. return;
  993. }
  994. ppPrev = &pTemp->pNext;
  995. pTemp = pTemp->pNext;
  996. }
  997. // Should never get here
  998. assert(0);
  999. }
  1000. };
  1001. // Allocates the hash slots on the regular heap (since the
  1002. // hash table can grow), but all hash entries are allocated on
  1003. // a private heap
  1004. template<typename T, bool (*pfnIsEqual)(const T &Data1, const T &Data2)>
  1005. class CEffectHashTableWithPrivateHeap : public CEffectHashTable<T, pfnIsEqual>
  1006. {
  1007. protected:
  1008. CDataBlockStore *m_pPrivateHeap;
  1009. public:
  1010. CEffectHashTableWithPrivateHeap()
  1011. {
  1012. m_pPrivateHeap = nullptr;
  1013. }
  1014. void Cleanup()
  1015. {
  1016. CleanArray();
  1017. m_NumHashSlots = 0;
  1018. m_NumEntries = 0;
  1019. }
  1020. ~CEffectHashTableWithPrivateHeap()
  1021. {
  1022. Cleanup();
  1023. }
  1024. // Call this only once
  1025. void SetPrivateHeap(_In_ CDataBlockStore *pPrivateHeap)
  1026. {
  1027. assert(nullptr == m_pPrivateHeap);
  1028. m_pPrivateHeap = pPrivateHeap;
  1029. }
  1030. // Adds data at the specified hash slot without checking for existence
  1031. HRESULT AddValueWithHash(_In_ T Data, _In_ uint32_t Hash)
  1032. {
  1033. HRESULT hr = S_OK;
  1034. assert(m_pPrivateHeap);
  1035. _Analysis_assume_(m_pPrivateHeap);
  1036. assert(m_NumHashSlots > 0);
  1037. SHashEntry *pHashEntry;
  1038. uint32_t index = Hash % m_NumHashSlots;
  1039. VN( pHashEntry = new(*m_pPrivateHeap) SHashEntry );
  1040. pHashEntry->pNext = m_rgpHashEntries[index];
  1041. pHashEntry->Data = Data;
  1042. pHashEntry->Hash = Hash;
  1043. m_rgpHashEntries[index] = pHashEntry;
  1044. ++ m_NumEntries;
  1045. lExit:
  1046. return hr;
  1047. }
  1048. };