D3D9Texture3D.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Precompiled.h"
  23. #include "../../Core/Context.h"
  24. #include "../../Core/Profiler.h"
  25. #include "../../Graphics/Graphics.h"
  26. #include "../../Graphics/GraphicsEvents.h"
  27. #include "../../Graphics/GraphicsImpl.h"
  28. #include "../../Graphics/Renderer.h"
  29. #include "../../Graphics/Texture3D.h"
  30. #include "../../IO/FileSystem.h"
  31. #include "../../IO/Log.h"
  32. #include "../../Resource/ResourceCache.h"
  33. #include "../../Resource/XMLFile.h"
  34. #include "../../DebugNew.h"
  35. namespace Atomic
  36. {
  37. void Texture3D::OnDeviceLost()
  38. {
  39. if (usage_ > TEXTURE_STATIC)
  40. Release();
  41. }
  42. void Texture3D::OnDeviceReset()
  43. {
  44. if (usage_ > TEXTURE_STATIC || !object_.ptr_ || dataPending_)
  45. {
  46. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  47. ResourceCache* cache = GetSubsystem<ResourceCache>();
  48. if (cache->Exists(GetName()))
  49. dataLost_ = !cache->ReloadResource(this);
  50. if (!object_.ptr_)
  51. {
  52. Create();
  53. dataLost_ = true;
  54. }
  55. }
  56. dataPending_ = false;
  57. }
  58. void Texture3D::Release()
  59. {
  60. if (graphics_)
  61. {
  62. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  63. {
  64. if (graphics_->GetTexture(i) == this)
  65. graphics_->SetTexture(i, 0);
  66. }
  67. }
  68. ATOMIC_SAFE_RELEASE(object_.ptr_);
  69. }
  70. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  71. {
  72. ATOMIC_PROFILE(SetTextureData);
  73. if (!object_.ptr_)
  74. {
  75. ATOMIC_LOGERROR("No texture created, can not set data");
  76. return false;
  77. }
  78. if (!data)
  79. {
  80. ATOMIC_LOGERROR("Null source for setting data");
  81. return false;
  82. }
  83. if (level >= levels_)
  84. {
  85. ATOMIC_LOGERROR("Illegal mip level for setting data");
  86. return false;
  87. }
  88. if (graphics_->IsDeviceLost())
  89. {
  90. ATOMIC_LOGWARNING("Texture data assignment while device is lost");
  91. dataPending_ = true;
  92. return true;
  93. }
  94. if (IsCompressed())
  95. {
  96. x &= ~3;
  97. y &= ~3;
  98. }
  99. int levelWidth = GetLevelWidth(level);
  100. int levelHeight = GetLevelHeight(level);
  101. int levelDepth = GetLevelDepth(level);
  102. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || z < 0 || z + depth > levelDepth || width <= 0 ||
  103. height <= 0 || depth <= 0)
  104. {
  105. ATOMIC_LOGERROR("Illegal dimensions for setting data");
  106. return false;
  107. }
  108. D3DLOCKED_BOX d3dLockedBox;
  109. D3DBOX d3dBox;
  110. d3dBox.Left = (UINT)x;
  111. d3dBox.Top = (UINT)y;
  112. d3dBox.Front = (UINT)z;
  113. d3dBox.Right = (UINT)(x + width);
  114. d3dBox.Bottom = (UINT)(y + height);
  115. d3dBox.Back = (UINT)(z + depth);
  116. DWORD flags = 0;
  117. if (level == 0 && x == 0 && y == 0 && z == 0 && width == levelWidth && height == levelHeight && depth == levelDepth &&
  118. usage_ > TEXTURE_STATIC)
  119. flags |= D3DLOCK_DISCARD;
  120. HRESULT hr = ((IDirect3DVolumeTexture9*)object_.ptr_)->LockBox(level, &d3dLockedBox, (flags & D3DLOCK_DISCARD) ? 0 : &d3dBox, flags);
  121. if (FAILED(hr))
  122. {
  123. ATOMIC_LOGD3DERROR("Could not lock texture", hr);
  124. return false;
  125. }
  126. if (IsCompressed())
  127. {
  128. height = (height + 3) >> 2;
  129. y >>= 2;
  130. }
  131. unsigned char* src = (unsigned char*)data;
  132. unsigned rowSize = GetRowDataSize(width);
  133. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  134. if (format_ == D3DFMT_X8R8G8B8)
  135. rowSize = rowSize / 3 * 4;
  136. // Perform conversion from RGB / RGBA as necessary
  137. switch (format_)
  138. {
  139. default:
  140. for (int k = 0; k < levelDepth; ++k)
  141. {
  142. for (int i = 0; i < height; ++i)
  143. {
  144. unsigned char
  145. * dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  146. memcpy(dest, src, rowSize);
  147. src += rowSize;
  148. }
  149. }
  150. break;
  151. case D3DFMT_X8R8G8B8:
  152. for (int k = 0; k < levelDepth; ++k)
  153. {
  154. for (int i = 0; i < height; ++i)
  155. {
  156. unsigned char
  157. * dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  158. for (int j = 0; j < width; ++j)
  159. {
  160. *dest++ = src[2];
  161. *dest++ = src[1];
  162. *dest++ = src[0];
  163. *dest++ = 255;
  164. src += 3;
  165. }
  166. }
  167. }
  168. break;
  169. case D3DFMT_A8R8G8B8:
  170. for (int k = 0; k < levelDepth; ++k)
  171. {
  172. for (int i = 0; i < height; ++i)
  173. {
  174. unsigned char
  175. * dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  176. for (int j = 0; j < width; ++j)
  177. {
  178. *dest++ = src[2];
  179. *dest++ = src[1];
  180. *dest++ = src[0];
  181. *dest++ = src[3];
  182. src += 4;
  183. }
  184. }
  185. }
  186. break;
  187. }
  188. ((IDirect3DVolumeTexture9*)object_.ptr_)->UnlockBox(level);
  189. return true;
  190. }
  191. bool Texture3D::SetData(Image* image, bool useAlpha)
  192. {
  193. if (!image)
  194. {
  195. ATOMIC_LOGERROR("Null image, can not load texture");
  196. return false;
  197. }
  198. // Use a shared ptr for managing the temporary mip images created during this function
  199. SharedPtr<Image> mipImage;
  200. unsigned memoryUse = sizeof(Texture3D);
  201. int quality = QUALITY_HIGH;
  202. Renderer* renderer = GetSubsystem<Renderer>();
  203. if (renderer)
  204. quality = renderer->GetTextureQuality();
  205. if (!image->IsCompressed())
  206. {
  207. unsigned char* levelData = image->GetData();
  208. int levelWidth = image->GetWidth();
  209. int levelHeight = image->GetHeight();
  210. int levelDepth = image->GetDepth();
  211. unsigned components = image->GetComponents();
  212. unsigned format = 0;
  213. // Discard unnecessary mip levels
  214. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  215. {
  216. mipImage = image->GetNextLevel(); image = mipImage;
  217. levelData = image->GetData();
  218. levelWidth = image->GetWidth();
  219. levelHeight = image->GetHeight();
  220. levelDepth = image->GetDepth();
  221. }
  222. switch (components)
  223. {
  224. case 1:
  225. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  226. break;
  227. case 2:
  228. format = Graphics::GetLuminanceAlphaFormat();
  229. break;
  230. case 3:
  231. format = Graphics::GetRGBFormat();
  232. break;
  233. case 4:
  234. format = Graphics::GetRGBAFormat();
  235. break;
  236. default:
  237. assert(false); // Should never reach here
  238. break;
  239. }
  240. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  241. if (IsCompressed() && requestedLevels_ > 1)
  242. requestedLevels_ = 0;
  243. SetSize(levelWidth, levelHeight, levelDepth, format);
  244. for (unsigned i = 0; i < levels_; ++i)
  245. {
  246. SetData(i, 0, 0, 0, levelWidth, levelHeight, levelDepth, levelData);
  247. memoryUse += levelWidth * levelHeight * levelDepth * components;
  248. if (i < levels_ - 1)
  249. {
  250. mipImage = image->GetNextLevel(); image = mipImage;
  251. levelData = image->GetData();
  252. levelWidth = image->GetWidth();
  253. levelHeight = image->GetHeight();
  254. levelDepth = image->GetDepth();
  255. }
  256. }
  257. }
  258. else
  259. {
  260. int width = image->GetWidth();
  261. int height = image->GetHeight();
  262. int depth = image->GetDepth();
  263. unsigned levels = image->GetNumCompressedLevels();
  264. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  265. bool needDecompress = false;
  266. if (!format)
  267. {
  268. format = Graphics::GetRGBAFormat();
  269. needDecompress = true;
  270. }
  271. unsigned mipsToSkip = mipsToSkip_[quality];
  272. if (mipsToSkip >= levels)
  273. mipsToSkip = levels - 1;
  274. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4 || depth / (1 << mipsToSkip) < 4))
  275. --mipsToSkip;
  276. width /= (1 << mipsToSkip);
  277. height /= (1 << mipsToSkip);
  278. depth /= (1 << mipsToSkip);
  279. SetNumLevels(Max((levels - mipsToSkip), 1U));
  280. SetSize(width, height, depth, format);
  281. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  282. {
  283. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  284. if (!needDecompress)
  285. {
  286. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, level.data_);
  287. memoryUse += level.depth_ * level.rows_ * level.rowSize_;
  288. }
  289. else
  290. {
  291. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * level.depth_ * 4];
  292. level.Decompress(rgbaData);
  293. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, rgbaData);
  294. memoryUse += level.width_ * level.height_ * level.depth_ * 4;
  295. delete[] rgbaData;
  296. }
  297. }
  298. }
  299. SetMemoryUse(memoryUse);
  300. return true;
  301. }
  302. bool Texture3D::GetData(unsigned level, void* dest) const
  303. {
  304. if (!object_.ptr_)
  305. {
  306. ATOMIC_LOGERROR("No texture created, can not get data");
  307. return false;
  308. }
  309. if (!dest)
  310. {
  311. ATOMIC_LOGERROR("Null destination for getting data");
  312. return false;
  313. }
  314. if (level >= levels_)
  315. {
  316. ATOMIC_LOGERROR("Illegal mip level for getting data");
  317. return false;
  318. }
  319. if (graphics_->IsDeviceLost())
  320. {
  321. ATOMIC_LOGWARNING("Getting texture data while device is lost");
  322. return false;
  323. }
  324. int levelWidth = GetLevelWidth(level);
  325. int levelHeight = GetLevelHeight(level);
  326. int levelDepth = GetLevelDepth(level);
  327. D3DLOCKED_BOX d3dLockedBox;
  328. D3DBOX d3dBox;
  329. d3dBox.Left = 0;
  330. d3dBox.Top = 0;
  331. d3dBox.Front = 0;
  332. d3dBox.Right = (UINT)levelWidth;
  333. d3dBox.Bottom = (UINT)levelHeight;
  334. d3dBox.Back = (UINT)levelDepth;
  335. HRESULT hr = ((IDirect3DVolumeTexture9*)object_.ptr_)->LockBox(level, &d3dLockedBox, &d3dBox, D3DLOCK_READONLY);
  336. if (FAILED(hr))
  337. {
  338. ATOMIC_LOGD3DERROR("Could not lock texture", hr);
  339. return false;
  340. }
  341. int height = levelHeight;
  342. if (IsCompressed())
  343. height = (height + 3) >> 2;
  344. unsigned char* destPtr = (unsigned char*)dest;
  345. unsigned rowSize = GetRowDataSize(levelWidth);
  346. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  347. if (format_ == D3DFMT_X8R8G8B8)
  348. rowSize = rowSize / 3 * 4;
  349. // Perform conversion to RGB / RGBA as necessary
  350. switch (format_)
  351. {
  352. default:
  353. for (int k = 0; k < levelDepth; ++k)
  354. {
  355. for (int i = 0; i < height; ++i)
  356. {
  357. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  358. memcpy(destPtr, src, rowSize);
  359. destPtr += rowSize;
  360. }
  361. }
  362. break;
  363. case D3DFMT_X8R8G8B8:
  364. for (int k = 0; k < levelDepth; ++k)
  365. {
  366. for (int i = 0; i < height; ++i)
  367. {
  368. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  369. for (int j = 0; j < levelWidth; ++j)
  370. {
  371. destPtr[2] = *src++;
  372. destPtr[1] = *src++;
  373. destPtr[0] = *src++;
  374. ++src;
  375. destPtr += 3;
  376. }
  377. }
  378. }
  379. break;
  380. case D3DFMT_A8R8G8B8:
  381. for (int k = 0; k < levelDepth; ++k)
  382. {
  383. for (int i = 0; i < height; ++i)
  384. {
  385. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  386. for (int j = 0; j < levelWidth; ++j)
  387. {
  388. destPtr[2] = *src++;
  389. destPtr[1] = *src++;
  390. destPtr[0] = *src++;
  391. destPtr[3] = *src++;
  392. destPtr += 4;
  393. }
  394. }
  395. }
  396. break;
  397. }
  398. ((IDirect3DVolumeTexture9*)object_.ptr_)->UnlockBox(level);
  399. return true;
  400. }
  401. bool Texture3D::Create()
  402. {
  403. Release();
  404. if (!graphics_ || !width_ || !height_)
  405. return false;
  406. if (graphics_->IsDeviceLost())
  407. {
  408. ATOMIC_LOGWARNING("Texture creation while device is lost");
  409. return true;
  410. }
  411. unsigned pool = usage_ > TEXTURE_STATIC ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  412. unsigned d3dUsage = usage_ == TEXTURE_DYNAMIC ? D3DUSAGE_DYNAMIC : 0;
  413. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  414. HRESULT hr = device->CreateVolumeTexture(
  415. (UINT)width_,
  416. (UINT)height_,
  417. (UINT)depth_,
  418. requestedLevels_,
  419. d3dUsage,
  420. (D3DFORMAT)format_,
  421. (D3DPOOL)pool,
  422. (IDirect3DVolumeTexture9**)&object_,
  423. 0);
  424. if (FAILED(hr))
  425. {
  426. ATOMIC_LOGD3DERROR("Could not create texture", hr);
  427. ATOMIC_SAFE_RELEASE(object_.ptr_);
  428. return false;
  429. }
  430. levels_ = ((IDirect3DVolumeTexture9*)object_.ptr_)->GetLevelCount();
  431. return true;
  432. }
  433. }