BsD3D9PixelBuffer.cpp 9.2 KB

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