Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

525 lines
10 KiB

  1. #include <windows.h>
  2. #include <d3dx8tex.h>
  3. DEFINE_GUID(g_GUIDDXVBLOCK, 0x5dd2e8da, 0x1c77, 0x4d40, 0xb0, 0xcf, 0x98, 0xfe, 0xfd, 0xff, 0x95, 0x12);
  4. typedef struct _LOCKDATA {
  5. SAFEARRAY *psaRealArray;
  6. BOOL bLocked;
  7. SAFEARRAY *psaLockedArray;
  8. } LOCKDATA;
  9. HRESULT WINAPI DXLockArray8(IUnknown *resource, void *pBits, SAFEARRAY **ppSafeArray)
  10. {
  11. if (!resource) return E_INVALIDARG;
  12. if (!ppSafeArray) return E_INVALIDARG;
  13. if (!*ppSafeArray) return E_INVALIDARG;
  14. LOCKDATA LockData; //Structure we copy into resource
  15. HRESULT hr;
  16. IDirect3DResource8 *pResource=NULL;
  17. DWORD dwSize=sizeof(LOCKDATA);
  18. //See if we have a resource
  19. hr = resource->QueryInterface(IID_IDirect3DResource8,(void**)&pResource);
  20. if FAILED(hr) return E_INVALIDARG;
  21. GUID g=g_GUIDDXVBLOCK; //Guid to identify data in resource
  22. //See if there is any data in the resource
  23. ZeroMemory(&LockData,sizeof(LOCKDATA));
  24. hr=pResource->GetPrivateData(g,&LockData,&dwSize);
  25. //if it was locked allready - we need to fail
  26. //and not lock it twice
  27. if (SUCCEEDED(hr) && (LockData.bLocked)){
  28. pResource->Release();
  29. return E_FAIL; //CONSIDER returning DDERR_LOCKEDSURFACES;
  30. }
  31. //SAVE the vb pointer to the safe array
  32. LockData.psaRealArray=*ppSafeArray; //should be NULL
  33. //Set this flag to make sure we dont lock twice
  34. LockData.bLocked=TRUE;
  35. //Allocate our own new safe array
  36. LockData.psaLockedArray=(SAFEARRAY*)malloc(sizeof(SAFEARRAY));
  37. if (!LockData.psaLockedArray)
  38. return E_OUTOFMEMORY;
  39. ZeroMemory(LockData.psaLockedArray,sizeof(SAFEARRAY));
  40. memcpy(LockData.psaLockedArray,*ppSafeArray,sizeof(SAFEARRAY));
  41. LockData.psaLockedArray->pvData = pBits;
  42. *ppSafeArray=LockData.psaLockedArray;
  43. hr=pResource->SetPrivateData(g,&LockData,dwSize,0);
  44. pResource->Release();
  45. #if 0
  46. DWORD dwElemSize=0;
  47. D3DRESOURCETYPE resType=pResource->GetType();
  48. switch (resType)
  49. {
  50. case D3DRESOURCETYPE_VERTEXBUFFER:
  51. LPDIRECT3DVERTEXBUFFER8 *pVertBuff=NULL;
  52. //User must have created a 1d array
  53. if ((*ppSafeArray)->cbElements != 1) {
  54. pResource->Release();
  55. return E_INVALIDARG;
  56. }
  57. hr=pResource->QueryInterface(IID_IDirect3DVertexBuffer8,(void**)&pVertBuff);
  58. if FAILED(hr) {
  59. pResource->Release();
  60. return E_INVALIDARG;
  61. }
  62. D3DVERTEXBUFFER_DESC vbdesc =pVertBuff->GetVertexBufferDesc()
  63. dwElemSize=(*ppSafeArray)->cbElements;
  64. //Make sure our size is evenly divisible by the vertex format
  65. if ((vbdesc.Size % dwElemSize) !=0) {
  66. pResource->Release();
  67. pVertBuff->Release();
  68. return E_INVALIDARG;
  69. }
  70. //Take Element size from our safearray
  71. LockData.psaLockedArray->cbElements =dwElemSize;
  72. LockData.psaLockedArray->cDims =1;
  73. LockData.psaLockedArray->rgsabound[0].lLbound =0;
  74. LockData.psaLockedArray->rgsabound[0].cElements = vdesc.Size / dwElemSize;
  75. LockData.psaLockedArray->pvData = pBits;
  76. pVertexBuffer->Release();
  77. break;
  78. case D3DRESOURCETYPE_INDEXBUFFER:
  79. LPDIRECT3DINDEXBUFFER8 *pIndBuff=NULL;
  80. hr=pResource->QueryInterface(IID_IDirect3DIndexBuffer8,(void**)&pIndBuff);
  81. if FAILED(hr) {
  82. pResource->Release();
  83. return E_INVALIDARG;
  84. }
  85. D3DINDEXBUFFER_DESC ibdesc =pVertBuff->GetIndexBufferDesc()
  86. dwElemSize=(*ppSafeArray)->cbElements;
  87. //Make sure the created the right kind of array
  88. if ((ibdesc.Format==D3DFMT_INDEX_16)&&(dwElemSize!=2)){
  89. pResource->Release();
  90. pIndBuffer->Release();
  91. return E_INVALIDARG;
  92. }
  93. if ((ibdesc.Format==D3DFMT_INDEX_32)&&(dwElemSize!=4)){
  94. pResource->Release();
  95. pIndBuffer->Release();
  96. return E_INVALIDARG;
  97. }
  98. //User must have created a 1d array
  99. if ((*ppSafeArray)->cbElements != 1) {
  100. pResource->Release();
  101. pIndBuffer->Release();
  102. return E_INVALIDARG;
  103. }
  104. //Make sure our size is evenly divisible
  105. if ((vbdesc.Size % dwElemSize) !=0) {
  106. pResource->Release();
  107. pIndBuff->Release();
  108. return E_INVALIDARG;
  109. }
  110. //Take Element size from our safearray
  111. LockData.psaLockedArray->cbElements =dwElemSize;
  112. LockData.psaLockedArray->cDims =1;
  113. LockData.psaLockedArray->rgsabound[0].lLbound =0;
  114. LockData.psaLockedArray->rgsabound[0].cElements = vdesc.Size / dwElemSize;
  115. LockData.psaLockedArray->pvData = pBits;
  116. pIndBuffer->Release();
  117. break;
  118. }
  119. #endif
  120. return hr;
  121. }
  122. HRESULT WINAPI DXUnlockArray8(IUnknown *resource, SAFEARRAY **ppSafeArray)
  123. {
  124. LOCKDATA LockData;
  125. DWORD dwSize=sizeof(LOCKDATA);
  126. HRESULT hr;
  127. LPDIRECT3DRESOURCE8 pResource=NULL;
  128. if (!resource) return E_INVALIDARG;
  129. if (!ppSafeArray) return E_INVALIDARG;
  130. if (!*ppSafeArray) return E_INVALIDARG;
  131. //See if we have a resource
  132. hr = resource->QueryInterface(IID_IDirect3DResource8,(void**)&pResource);
  133. if FAILED(hr) return E_INVALIDARG;
  134. GUID g=g_GUIDDXVBLOCK;
  135. ZeroMemory(&LockData,sizeof(LOCKDATA));
  136. hr=pResource->GetPrivateData(g,&LockData,&dwSize);
  137. if FAILED(hr) {
  138. pResource->Release();
  139. return E_FAIL;
  140. }
  141. if (!LockData.bLocked) {
  142. pResource->Release();
  143. return E_FAIL; //CONSIDER DDERR_LOCKEDSURFACES;
  144. }
  145. (*ppSafeArray)=LockData.psaRealArray;
  146. if (LockData.psaLockedArray) free(LockData.psaLockedArray);
  147. ZeroMemory(&LockData,sizeof(LOCKDATA));
  148. hr=pResource->SetPrivateData(g,&LockData,dwSize,0);
  149. pResource->Release();
  150. return hr;
  151. }
  152. HRESULT WINAPI D3DVertexBuffer8SetData(IDirect3DVertexBuffer8 *pVBuffer,int offset, int size, DWORD flags, void *data)
  153. {
  154. if (!pVBuffer) return E_INVALIDARG;
  155. HRESULT hr;
  156. BYTE *pbData=NULL;
  157. hr=pVBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  158. if FAILED(hr) return hr;
  159. _try {
  160. memcpy ((void*)pbData,data,(DWORD)size);
  161. }
  162. _except(1,1)
  163. {
  164. return E_INVALIDARG;
  165. }
  166. hr=pVBuffer->Unlock();
  167. return hr;
  168. }
  169. HRESULT WINAPI D3DVertexBuffer8GetData(IDirect3DVertexBuffer8 *pVBuffer,int offset, int size, DWORD flags,void *data)
  170. {
  171. if (!pVBuffer) return E_INVALIDARG;
  172. HRESULT hr;
  173. BYTE *pbData=NULL;
  174. hr=pVBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  175. if FAILED(hr) return hr;
  176. _try {
  177. memcpy (data,(void*)pbData,(DWORD)size);
  178. }
  179. _except(1,1)
  180. {
  181. return E_INVALIDARG;
  182. }
  183. hr=pVBuffer->Unlock();
  184. return hr;
  185. }
  186. HRESULT WINAPI D3DIndexBuffer8SetData(IDirect3DIndexBuffer8 *pIBuffer,int offset, int size,DWORD flags, void *data)
  187. {
  188. if (!pIBuffer) return E_INVALIDARG;
  189. HRESULT hr;
  190. BYTE *pbData=NULL;
  191. hr=pIBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  192. if FAILED(hr) return hr;
  193. _try {
  194. memcpy ((void*)pbData,data,(DWORD)size);
  195. }
  196. _except(1,1)
  197. {
  198. return E_INVALIDARG;
  199. }
  200. hr=pIBuffer->Unlock();
  201. return hr;
  202. }
  203. HRESULT WINAPI D3DIndexBuffer8GetData(IDirect3DIndexBuffer8 *pIBuffer,int offset, int size,DWORD flags, void *data)
  204. {
  205. if (!pIBuffer) return E_INVALIDARG;
  206. HRESULT hr;
  207. BYTE *pbData=NULL;
  208. hr=pIBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  209. if FAILED(hr) return hr;
  210. _try {
  211. memcpy (data,(void*)pbData,(DWORD)size);
  212. }
  213. _except(1,1)
  214. {
  215. return E_INVALIDARG;
  216. }
  217. hr=pIBuffer->Unlock();
  218. return hr;
  219. }
  220. //////////////////////////////////////////////////////////
  221. HRESULT WINAPI D3DXMeshVertexBuffer8SetData(IUnknown *pObj,int offset, int size, DWORD flags, void *data)
  222. {
  223. HRESULT hr;
  224. BYTE *pbData=NULL;
  225. LPD3DXBASEMESH pMesh=NULL;
  226. LPDIRECT3DVERTEXBUFFER8 pVBuffer=NULL;
  227. if (!pObj) return E_INVALIDARG;
  228. hr=pObj->QueryInterface(IID_ID3DXBaseMesh,(void**)&pMesh);
  229. if FAILED(hr) return E_INVALIDARG;
  230. hr=pMesh->GetVertexBuffer(&pVBuffer);
  231. pMesh->Release();
  232. if FAILED(hr) return hr;
  233. hr=pVBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  234. if FAILED(hr) return hr;
  235. _try {
  236. memcpy ((void*)pbData,data,(DWORD)size);
  237. }
  238. _except(1,1)
  239. {
  240. pVBuffer->Release();
  241. return E_INVALIDARG;
  242. }
  243. hr=pVBuffer->Unlock();
  244. pVBuffer->Release();
  245. return hr;
  246. }
  247. HRESULT WINAPI D3DXMeshVertexBuffer8GetData(IUnknown *pObj,int offset, int size, DWORD flags,void *data)
  248. {
  249. HRESULT hr;
  250. BYTE *pbData=NULL;
  251. LPD3DXBASEMESH pMesh=NULL;
  252. LPDIRECT3DVERTEXBUFFER8 pVBuffer=NULL;
  253. if (!pObj) return E_INVALIDARG;
  254. hr=pObj->QueryInterface(IID_ID3DXBaseMesh,(void**)&pMesh);
  255. if FAILED(hr) return E_INVALIDARG;
  256. hr=pMesh->GetVertexBuffer(&pVBuffer);
  257. pMesh->Release();
  258. if FAILED(hr) return hr;
  259. hr=pVBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  260. if FAILED(hr) return hr;
  261. _try {
  262. memcpy (data,(void*)pbData,(DWORD)size);
  263. }
  264. _except(1,1)
  265. {
  266. pVBuffer->Release();
  267. return E_INVALIDARG;
  268. }
  269. hr=pVBuffer->Unlock();
  270. pVBuffer->Release();
  271. return hr;
  272. }
  273. HRESULT WINAPI D3DXMeshIndexBuffer8SetData(IUnknown *pObj,int offset, int size, DWORD flags, void *data)
  274. {
  275. HRESULT hr;
  276. BYTE *pbData=NULL;
  277. LPD3DXBASEMESH pMesh=NULL;
  278. LPDIRECT3DINDEXBUFFER8 pIBuffer=NULL;
  279. if (!pObj) return E_INVALIDARG;
  280. hr=pObj->QueryInterface(IID_ID3DXBaseMesh,(void**)&pMesh);
  281. if FAILED(hr) return E_INVALIDARG;
  282. hr=pMesh->GetIndexBuffer(&pIBuffer);
  283. pMesh->Release();
  284. if FAILED(hr) return hr;
  285. hr=pIBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  286. if FAILED(hr) return hr;
  287. _try {
  288. memcpy ((void*)pbData,data,(DWORD)size);
  289. }
  290. _except(1,1)
  291. {
  292. pIBuffer->Release();
  293. return E_INVALIDARG;
  294. }
  295. hr=pIBuffer->Unlock();
  296. pIBuffer->Release();
  297. return hr;
  298. }
  299. HRESULT WINAPI D3DXMeshIndexBuffer8GetData(IUnknown *pObj,int offset, int size, DWORD flags, void *data)
  300. {
  301. HRESULT hr;
  302. BYTE *pbData=NULL;
  303. LPD3DXBASEMESH pMesh=NULL;
  304. LPDIRECT3DINDEXBUFFER8 pIBuffer=NULL;
  305. if (!pObj) return E_INVALIDARG;
  306. hr=pObj->QueryInterface(IID_ID3DXBaseMesh,(void**)&pMesh);
  307. if FAILED(hr) return E_INVALIDARG;
  308. hr=pMesh->GetIndexBuffer(&pIBuffer);
  309. pMesh->Release();
  310. if FAILED(hr) return hr;
  311. hr=pIBuffer->Lock((UINT)offset,(UINT)size, &pbData,flags);
  312. if FAILED(hr) return hr;
  313. _try {
  314. memcpy (data,(void*)pbData,(DWORD)size);
  315. //memcpy (data,(void*)&(pbData[offset]),(DWORD)size);
  316. }
  317. _except(1,1)
  318. {
  319. pIBuffer->Release();
  320. return E_INVALIDARG;
  321. }
  322. hr=pIBuffer->Unlock();
  323. pIBuffer->Release();
  324. return hr;
  325. }
  326. HRESULT WINAPI DXCopyMemory (void *Dest, void *Src, DWORD size)
  327. {
  328. _try {
  329. memcpy (Dest,Src,size);
  330. }
  331. _except(1,1)
  332. {
  333. return E_INVALIDARG;
  334. }
  335. return S_OK;
  336. }