BsD3D9PixelBuffer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsD3D9PixelBuffer.h"
  5. #include "BsD3D9Texture.h"
  6. #include "BsD3D9Mappings.h"
  7. #include "BsException.h"
  8. #include "BsBitwise.h"
  9. #include "BsRenderSystem.h"
  10. #include "BsRenderStats.h"
  11. namespace BansheeEngine
  12. {
  13. void fromD3DLock(PixelData& rval, const D3DLOCKED_RECT& lrect)
  14. {
  15. UINT32 bpp = PixelUtil::getNumElemBytes(rval.getFormat());
  16. if (bpp != 0)
  17. {
  18. rval.setRowPitch(lrect.Pitch / bpp);
  19. rval.setSlicePitch(rval.getRowPitch() * rval.getHeight());
  20. assert((lrect.Pitch % bpp) == 0);
  21. }
  22. else if (PixelUtil::isCompressed(rval.getFormat()))
  23. {
  24. rval.setRowPitch(rval.getWidth());
  25. rval.setSlicePitch(rval.getWidth() * rval.getHeight());
  26. }
  27. else
  28. {
  29. BS_EXCEPT(InvalidParametersException, "Invalid pixel format.");
  30. }
  31. rval.setExternalBuffer((UINT8*)lrect.pBits);
  32. }
  33. void fromD3DLock(PixelData& rval, const D3DLOCKED_BOX& lbox)
  34. {
  35. UINT32 bpp = PixelUtil::getNumElemBytes(rval.getFormat());
  36. if (bpp != 0)
  37. {
  38. rval.setRowPitch(lbox.RowPitch / bpp);
  39. rval.setSlicePitch(lbox.SlicePitch / bpp);
  40. assert((lbox.RowPitch % bpp) == 0);
  41. assert((lbox.SlicePitch % bpp) == 0);
  42. }
  43. else if (PixelUtil::isCompressed(rval.getFormat()))
  44. {
  45. rval.setRowPitch(rval.getWidth());
  46. rval.setSlicePitch(rval.getWidth() * rval.getHeight());
  47. }
  48. else
  49. {
  50. BS_EXCEPT(InvalidParametersException, "Invalid pixel format.");
  51. }
  52. rval.setExternalBuffer((UINT8*)lbox.pBits);
  53. }
  54. RECT toD3DRECT(const PixelVolume &lockBox)
  55. {
  56. RECT prect;
  57. assert(lockBox.getDepth() == 1);
  58. prect.left = (LONG)lockBox.left;
  59. prect.right = (LONG)lockBox.right;
  60. prect.top = (LONG)lockBox.top;
  61. prect.bottom = (LONG)lockBox.bottom;
  62. return prect;
  63. }
  64. D3DBOX toD3DBOX(const PixelVolume &lockBox)
  65. {
  66. D3DBOX pbox;
  67. pbox.Left = (UINT)lockBox.left;
  68. pbox.Right = (UINT)lockBox.right;
  69. pbox.Top = (UINT)lockBox.top;
  70. pbox.Bottom = (UINT)lockBox.bottom;
  71. pbox.Front = (UINT)lockBox.front;
  72. pbox.Back = (UINT)lockBox.back;
  73. return pbox;
  74. }
  75. D3D9PixelBuffer::D3D9PixelBuffer(GpuBufferUsage usage, D3D9Texture* ownerTexture)
  76. :PixelBuffer(0, 0, 0, PF_UNKNOWN, usage, false),
  77. mDoMipmapGen(0), mHWMipmaps(0), mOwnerTexture(ownerTexture)
  78. { }
  79. D3D9PixelBuffer::~D3D9PixelBuffer()
  80. {
  81. D3D9_DEVICE_ACCESS_CRITICAL_SECTION;
  82. auto iter = mMapDeviceToBufferResources.begin();
  83. while (iter != mMapDeviceToBufferResources.end())
  84. {
  85. SAFE_RELEASE(iter->second->surface);
  86. SAFE_RELEASE(iter->second->volume);
  87. if(iter->second != nullptr)
  88. bs_delete(iter->second);
  89. auto toRemove = iter++;
  90. mMapDeviceToBufferResources.erase(toRemove);
  91. }
  92. }
  93. void D3D9PixelBuffer::bind(IDirect3DDevice9* dev, IDirect3DSurface9* surface, IDirect3DBaseTexture9* mipTex)
  94. {
  95. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  96. BufferResources* bufferResources = getBufferResources(dev);
  97. bool isNewBuffer = false;
  98. if (bufferResources == NULL)
  99. {
  100. bufferResources = createBufferResources();
  101. mMapDeviceToBufferResources[dev] = bufferResources;
  102. isNewBuffer = true;
  103. }
  104. bufferResources->mipTex = mipTex;
  105. bufferResources->surface = surface;
  106. bufferResources->surface->AddRef();
  107. D3DSURFACE_DESC desc;
  108. if(surface->GetDesc(&desc) != D3D_OK)
  109. BS_EXCEPT(RenderingAPIException, "Could not get surface information");
  110. mWidth = desc.Width;
  111. mHeight = desc.Height;
  112. mDepth = 1;
  113. mFormat = D3D9Mappings::_getPF(desc.Format);
  114. mRowPitch = mWidth;
  115. mSlicePitch = mHeight*mWidth;
  116. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  117. }
  118. void D3D9PixelBuffer::bind(IDirect3DDevice9* dev, IDirect3DVolume9* volume, IDirect3DBaseTexture9* mipTex)
  119. {
  120. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  121. BufferResources* bufferResources = getBufferResources(dev);
  122. bool isNewBuffer = false;
  123. if (bufferResources == NULL)
  124. {
  125. bufferResources = createBufferResources();
  126. mMapDeviceToBufferResources[dev] = bufferResources;
  127. isNewBuffer = true;
  128. }
  129. bufferResources->mipTex = mipTex;
  130. bufferResources->volume = volume;
  131. bufferResources->volume->AddRef();
  132. D3DVOLUME_DESC desc;
  133. if(volume->GetDesc(&desc) != D3D_OK)
  134. BS_EXCEPT(RenderingAPIException, "Could not get volume information");
  135. mWidth = desc.Width;
  136. mHeight = desc.Height;
  137. mDepth = desc.Depth;
  138. mFormat = D3D9Mappings::_getPF(desc.Format);
  139. mRowPitch = mWidth;
  140. mSlicePitch = mHeight*mWidth;
  141. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  142. }
  143. D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::getBufferResources(IDirect3DDevice9* d3d9Device)
  144. {
  145. auto iterFind = mMapDeviceToBufferResources.find(d3d9Device);
  146. if (iterFind != mMapDeviceToBufferResources.end())
  147. return iterFind->second;
  148. return nullptr;
  149. }
  150. D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::createBufferResources()
  151. {
  152. BufferResources* newResources = bs_new<BufferResources>();
  153. memset(newResources, 0, sizeof(BufferResources));
  154. return newResources;
  155. }
  156. void D3D9PixelBuffer::destroyBufferResources(IDirect3DDevice9* d3d9Device)
  157. {
  158. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  159. auto iterFind = mMapDeviceToBufferResources.find(d3d9Device);
  160. if (iterFind != mMapDeviceToBufferResources.end())
  161. {
  162. SAFE_RELEASE(iterFind->second->surface);
  163. SAFE_RELEASE(iterFind->second->volume);
  164. if(iterFind->second != nullptr)
  165. bs_delete(iterFind->second);
  166. mMapDeviceToBufferResources.erase(iterFind);
  167. }
  168. }
  169. void D3D9PixelBuffer::lockDeviceAccess()
  170. {
  171. D3D9_DEVICE_ACCESS_LOCK;
  172. }
  173. void D3D9PixelBuffer::unlockDeviceAccess()
  174. {
  175. D3D9_DEVICE_ACCESS_UNLOCK;
  176. }
  177. PixelData D3D9PixelBuffer::lockImpl(PixelVolume lockBox, GpuLockOptions options)
  178. {
  179. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  180. #if BS_PROFILING_ENABLED
  181. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  182. {
  183. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  184. }
  185. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  186. {
  187. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  188. }
  189. #endif
  190. DWORD flags = 0;
  191. switch(options)
  192. {
  193. case GBL_WRITE_ONLY_DISCARD:
  194. if (mUsage & GBU_DYNAMIC)
  195. flags |= D3DLOCK_DISCARD;
  196. break;
  197. case GBL_READ_ONLY:
  198. flags |= D3DLOCK_READONLY;
  199. break;
  200. default:
  201. break;
  202. };
  203. if (mMapDeviceToBufferResources.size() == 0)
  204. {
  205. BS_EXCEPT(RenderingAPIException, "There are no resources attached to this pixel buffer !!");
  206. }
  207. mLockedBox = lockBox;
  208. mLockFlags = flags;
  209. BufferResources* bufferResources = mMapDeviceToBufferResources.begin()->second;
  210. return lockBuffer(bufferResources, lockBox, flags);
  211. }
  212. PixelData D3D9PixelBuffer::lockBuffer(BufferResources* bufferResources, const PixelVolume& lockBox, DWORD flags)
  213. {
  214. PixelData rval(lockBox.getWidth(), lockBox.getHeight(), lockBox.getDepth(), mFormat);
  215. if (bufferResources->surface != nullptr)
  216. {
  217. D3DLOCKED_RECT lrect;
  218. HRESULT hr;
  219. if (lockBox.left == 0 && lockBox.top == 0 && lockBox.right == mWidth && lockBox.bottom == mHeight)
  220. {
  221. hr = bufferResources->surface->LockRect(&lrect, nullptr, flags);
  222. }
  223. else
  224. {
  225. RECT prect = toD3DRECT(lockBox);
  226. hr = bufferResources->surface->LockRect(&lrect, &prect, flags);
  227. }
  228. if (FAILED(hr))
  229. BS_EXCEPT(RenderingAPIException, "Surface locking failed");
  230. fromD3DLock(rval, lrect);
  231. }
  232. else if(bufferResources->volume)
  233. {
  234. D3DBOX pbox = toD3DBOX(lockBox);
  235. D3DLOCKED_BOX lbox;
  236. if(bufferResources->volume->LockBox(&lbox, &pbox, flags) != D3D_OK)
  237. BS_EXCEPT(RenderingAPIException, "Volume locking failed");
  238. fromD3DLock(rval, lbox);
  239. }
  240. return rval;
  241. }
  242. void D3D9PixelBuffer::unlockImpl()
  243. {
  244. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  245. if (mMapDeviceToBufferResources.size() == 0)
  246. BS_EXCEPT(RenderingAPIException, "There are no resources attached to this pixel buffer.");
  247. auto it = mMapDeviceToBufferResources.begin();
  248. unlockBuffer(it->second);
  249. if(mDoMipmapGen)
  250. genMipmaps(it->second->mipTex);
  251. }
  252. void D3D9PixelBuffer::unlockBuffer(BufferResources* bufferResources)
  253. {
  254. if(bufferResources->surface)
  255. {
  256. bufferResources->surface->UnlockRect();
  257. }
  258. else if(bufferResources->volume)
  259. {
  260. bufferResources->volume->UnlockBox();
  261. }
  262. }
  263. void D3D9PixelBuffer::genMipmaps(IDirect3DBaseTexture9* mipTex)
  264. {
  265. assert(mipTex != nullptr);
  266. if (mHWMipmaps)
  267. {
  268. mipTex->GenerateMipSubLevels();
  269. }
  270. else
  271. {
  272. if(D3DXFilterTexture(mipTex, nullptr, D3DX_DEFAULT, D3DX_DEFAULT) != D3D_OK)
  273. {
  274. BS_EXCEPT(RenderingAPIException, "Failed to generate mipmaps.");
  275. }
  276. }
  277. }
  278. void D3D9PixelBuffer::setMipmapping(bool doMipmapGen, bool HWMipmaps)
  279. {
  280. mDoMipmapGen = doMipmapGen;
  281. mHWMipmaps = HWMipmaps;
  282. }
  283. void D3D9PixelBuffer::releaseSurfaces(IDirect3DDevice9* d3d9Device)
  284. {
  285. BufferResources* bufferResources = getBufferResources(d3d9Device);
  286. if (bufferResources != nullptr)
  287. {
  288. SAFE_RELEASE(bufferResources->surface);
  289. SAFE_RELEASE(bufferResources->volume);
  290. }
  291. }
  292. IDirect3DSurface9* D3D9PixelBuffer::getSurface(IDirect3DDevice9* d3d9Device)
  293. {
  294. BufferResources* bufferResources = getBufferResources(d3d9Device);
  295. if (bufferResources == nullptr)
  296. {
  297. mOwnerTexture->createInternalResources(d3d9Device);
  298. bufferResources = getBufferResources(d3d9Device);
  299. }
  300. return bufferResources->surface;
  301. }
  302. };