d3dxGlobal.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //--------------------------------------------------------------------------------------
  2. // File: d3dxGlobal.cpp
  3. //
  4. // Direct3D 11 Effects implementation for helper 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. #include "pchfx.h"
  16. // VS 2010's stdint.h conflicts with intsafe.h
  17. #pragma warning(push)
  18. #pragma warning(disable : 4005)
  19. #include <intsafe.h>
  20. #pragma warning(pop)
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. namespace D3DX11Core
  24. {
  25. //////////////////////////////////////////////////////////////////////////
  26. // CMemoryStream - A class to simplify reading binary data
  27. //////////////////////////////////////////////////////////////////////////
  28. CMemoryStream::CMemoryStream() : m_pData(nullptr), m_cbData(0), m_readPtr(0)
  29. {
  30. }
  31. CMemoryStream::~CMemoryStream()
  32. {
  33. }
  34. _Use_decl_annotations_
  35. HRESULT CMemoryStream::SetData(const void *pData, size_t size)
  36. {
  37. m_pData = (uint8_t*) pData;
  38. m_cbData = size;
  39. m_readPtr = 0;
  40. return S_OK;
  41. }
  42. _Use_decl_annotations_
  43. HRESULT CMemoryStream::ReadAtOffset(size_t offset, size_t size, void **ppData)
  44. {
  45. if (offset >= m_cbData)
  46. return E_FAIL;
  47. m_readPtr = offset;
  48. return Read(ppData, size);
  49. }
  50. _Use_decl_annotations_
  51. HRESULT CMemoryStream::ReadAtOffset(size_t offset, LPCSTR *ppString)
  52. {
  53. if (offset >= m_cbData)
  54. return E_FAIL;
  55. m_readPtr = offset;
  56. return Read(ppString);
  57. }
  58. _Use_decl_annotations_
  59. HRESULT CMemoryStream::Read(void **ppData, size_t size)
  60. {
  61. size_t temp = m_readPtr + size;
  62. if (temp < m_readPtr || temp > m_cbData)
  63. return E_FAIL;
  64. *ppData = m_pData + m_readPtr;
  65. m_readPtr = temp;
  66. return S_OK;
  67. }
  68. _Use_decl_annotations_
  69. HRESULT CMemoryStream::Read(uint32_t *pDword)
  70. {
  71. uint32_t *pTempDword;
  72. HRESULT hr;
  73. hr = Read((void**) &pTempDword, sizeof(uint32_t));
  74. if (FAILED(hr))
  75. return E_FAIL;
  76. *pDword = *pTempDword;
  77. return S_OK;
  78. }
  79. _Use_decl_annotations_
  80. HRESULT CMemoryStream::Read(LPCSTR *ppString)
  81. {
  82. size_t iChar=m_readPtr;
  83. for(; m_pData[iChar]; iChar++)
  84. {
  85. if (iChar > m_cbData)
  86. return E_FAIL;
  87. }
  88. *ppString = (LPCSTR) (m_pData + m_readPtr);
  89. m_readPtr = iChar;
  90. return S_OK;
  91. }
  92. size_t CMemoryStream::GetPosition()
  93. {
  94. return m_readPtr;
  95. }
  96. HRESULT CMemoryStream::Seek(_In_ size_t offset)
  97. {
  98. if (offset > m_cbData)
  99. return E_FAIL;
  100. m_readPtr = offset;
  101. return S_OK;
  102. }
  103. }
  104. //////////////////////////////////////////////////////////////////////////
  105. // CDataBlock - used to dynamically build up the effect file in memory
  106. //////////////////////////////////////////////////////////////////////////
  107. CDataBlock::CDataBlock() :
  108. m_size(0),
  109. m_maxSize(0),
  110. m_pData(nullptr),
  111. m_pNext(nullptr),
  112. m_IsAligned(false)
  113. {
  114. }
  115. CDataBlock::~CDataBlock()
  116. {
  117. SAFE_DELETE_ARRAY(m_pData);
  118. SAFE_DELETE(m_pNext);
  119. }
  120. void CDataBlock::EnableAlignment()
  121. {
  122. m_IsAligned = true;
  123. }
  124. _Use_decl_annotations_
  125. HRESULT CDataBlock::AddData(const void *pvNewData, uint32_t bufferSize, CDataBlock **ppBlock)
  126. {
  127. HRESULT hr = S_OK;
  128. uint32_t bytesToCopy;
  129. const uint8_t *pNewData = (const uint8_t*) pvNewData;
  130. if (m_maxSize == 0)
  131. {
  132. // This is a brand new DataBlock, fill it up
  133. m_maxSize = std::max<uint32_t>(8192, bufferSize);
  134. VN( m_pData = new uint8_t[m_maxSize] );
  135. }
  136. assert(m_pData == AlignToPowerOf2(m_pData, c_DataAlignment));
  137. bytesToCopy = std::min(m_maxSize - m_size, bufferSize);
  138. memcpy(m_pData + m_size, pNewData, bytesToCopy);
  139. pNewData += bytesToCopy;
  140. if (m_IsAligned)
  141. {
  142. assert(m_size == AlignToPowerOf2(m_size, c_DataAlignment));
  143. m_size += AlignToPowerOf2(bytesToCopy, c_DataAlignment);
  144. }
  145. else
  146. {
  147. m_size += bytesToCopy;
  148. }
  149. bufferSize -= bytesToCopy;
  150. *ppBlock = this;
  151. if (bufferSize != 0)
  152. {
  153. assert(nullptr == m_pNext); // make sure we're not overwriting anything
  154. // Couldn't fit all data into this block, spill over into next
  155. VN( m_pNext = new CDataBlock() );
  156. if (m_IsAligned)
  157. {
  158. m_pNext->EnableAlignment();
  159. }
  160. VH( m_pNext->AddData(pNewData, bufferSize, ppBlock) );
  161. }
  162. lExit:
  163. return hr;
  164. }
  165. _Use_decl_annotations_
  166. void* CDataBlock::Allocate(uint32_t bufferSize, CDataBlock **ppBlock)
  167. {
  168. void *pRetValue;
  169. uint32_t temp = m_size + bufferSize;
  170. if (temp < m_size)
  171. return nullptr;
  172. *ppBlock = this;
  173. if (m_maxSize == 0)
  174. {
  175. // This is a brand new DataBlock, fill it up
  176. m_maxSize = std::max<uint32_t>(8192, bufferSize);
  177. m_pData = new uint8_t[m_maxSize];
  178. if (!m_pData)
  179. return nullptr;
  180. memset(m_pData, 0xDD, m_maxSize);
  181. }
  182. else if (temp > m_maxSize)
  183. {
  184. assert(nullptr == m_pNext); // make sure we're not overwriting anything
  185. // Couldn't fit data into this block, spill over into next
  186. m_pNext = new CDataBlock();
  187. if (!m_pNext)
  188. return nullptr;
  189. if (m_IsAligned)
  190. {
  191. m_pNext->EnableAlignment();
  192. }
  193. return m_pNext->Allocate(bufferSize, ppBlock);
  194. }
  195. assert(m_pData == AlignToPowerOf2(m_pData, c_DataAlignment));
  196. pRetValue = m_pData + m_size;
  197. if (m_IsAligned)
  198. {
  199. assert(m_size == AlignToPowerOf2(m_size, c_DataAlignment));
  200. m_size = AlignToPowerOf2(temp, c_DataAlignment);
  201. }
  202. else
  203. {
  204. m_size = temp;
  205. }
  206. return pRetValue;
  207. }
  208. //////////////////////////////////////////////////////////////////////////
  209. CDataBlockStore::CDataBlockStore() :
  210. m_pFirst(nullptr),
  211. m_pLast(nullptr),
  212. m_Size(0),
  213. m_Offset(0),
  214. m_IsAligned(false)
  215. {
  216. #if _DEBUG
  217. m_cAllocations = 0;
  218. #endif
  219. }
  220. CDataBlockStore::~CDataBlockStore()
  221. {
  222. // Can't just do SAFE_DELETE(m_pFirst) since it blows the stack when deleting long chains of data
  223. CDataBlock* pData = m_pFirst;
  224. while(pData)
  225. {
  226. CDataBlock* pCurrent = pData;
  227. pData = pData->m_pNext;
  228. pCurrent->m_pNext = nullptr;
  229. delete pCurrent;
  230. }
  231. // m_pLast will be deleted automatically
  232. }
  233. void CDataBlockStore::EnableAlignment()
  234. {
  235. m_IsAligned = true;
  236. }
  237. _Use_decl_annotations_
  238. HRESULT CDataBlockStore::AddString(LPCSTR pString, uint32_t *pOffset)
  239. {
  240. size_t strSize = strlen(pString) + 1;
  241. assert( strSize <= 0xffffffff );
  242. return AddData(pString, (uint32_t)strSize, pOffset);
  243. }
  244. _Use_decl_annotations_
  245. HRESULT CDataBlockStore::AddData(const void *pNewData, uint32_t bufferSize, uint32_t *pCurOffset)
  246. {
  247. HRESULT hr = S_OK;
  248. if (bufferSize == 0)
  249. {
  250. if (pCurOffset)
  251. {
  252. *pCurOffset = 0;
  253. }
  254. goto lExit;
  255. }
  256. if (!m_pFirst)
  257. {
  258. VN( m_pFirst = new CDataBlock() );
  259. if (m_IsAligned)
  260. {
  261. m_pFirst->EnableAlignment();
  262. }
  263. m_pLast = m_pFirst;
  264. }
  265. if (pCurOffset)
  266. *pCurOffset = m_Size + m_Offset;
  267. VH( m_pLast->AddData(pNewData, bufferSize, &m_pLast) );
  268. m_Size += bufferSize;
  269. lExit:
  270. return hr;
  271. }
  272. void* CDataBlockStore::Allocate(_In_ uint32_t bufferSize)
  273. {
  274. void *pRetValue = nullptr;
  275. #if _DEBUG
  276. m_cAllocations++;
  277. #endif
  278. if (!m_pFirst)
  279. {
  280. m_pFirst = new CDataBlock();
  281. if (!m_pFirst)
  282. return nullptr;
  283. if (m_IsAligned)
  284. {
  285. m_pFirst->EnableAlignment();
  286. }
  287. m_pLast = m_pFirst;
  288. }
  289. if (FAILED(UIntAdd(m_Size, bufferSize, &m_Size)))
  290. return nullptr;
  291. pRetValue = m_pLast->Allocate(bufferSize, &m_pLast);
  292. if (!pRetValue)
  293. return nullptr;
  294. return pRetValue;
  295. }
  296. uint32_t CDataBlockStore::GetSize()
  297. {
  298. return m_Size;
  299. }
  300. //////////////////////////////////////////////////////////////////////////
  301. #ifdef _DEBUG
  302. _Use_decl_annotations_
  303. void __cdecl D3DXDebugPrintf(UINT lvl, LPCSTR szFormat, ...)
  304. {
  305. UNREFERENCED_PARAMETER(lvl);
  306. char strA[4096];
  307. char strB[4096];
  308. va_list ap;
  309. va_start(ap, szFormat);
  310. vsprintf_s(strA, sizeof(strA), szFormat, ap);
  311. strA[4095] = '\0';
  312. va_end(ap);
  313. sprintf_s(strB, sizeof(strB), "Effects11: %s\r\n", strA);
  314. strB[4095] = '\0';
  315. OutputDebugStringA(strB);
  316. }
  317. #endif // _DEBUG