BsD3D9PixelBuffer.cpp 9.8 KB

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