BsD3D9PixelBuffer.cpp 9.1 KB

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