D3D9Texture3D.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Renderer.h"
  30. #include "Profiler.h"
  31. #include "ResourceCache.h"
  32. #include "Texture3D.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  37. Texture3D::Texture3D(Context* context) :
  38. Texture(context)
  39. {
  40. }
  41. Texture3D::~Texture3D()
  42. {
  43. Release();
  44. }
  45. void Texture3D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Texture3D>();
  48. }
  49. bool Texture3D::Load(Deserializer& source)
  50. {
  51. PROFILE(LoadTexture3D);
  52. // In headless mode, do not actually load the texture, just return success
  53. if (!graphics_)
  54. return true;
  55. // If device is lost, retry later
  56. if (graphics_->IsDeviceLost())
  57. {
  58. LOGWARNING("Texture load while device is lost");
  59. dataPending_ = true;
  60. return true;
  61. }
  62. // If over the texture budget, see if materials can be freed to allow textures to be freed
  63. CheckTextureBudget(GetTypeStatic());
  64. // Before actually loading the texture, get optional parameters from an XML description file
  65. LoadParameters();
  66. String texPath, texName, texExt;
  67. SplitPath(GetName(), texPath, texName, texExt);
  68. SharedPtr<XMLFile> xml(new XMLFile(context_));
  69. if (!xml->Load(source))
  70. return false;
  71. XMLElement textureElem = xml->GetRoot();
  72. XMLElement volumeElem = textureElem.GetChild("volume");
  73. XMLElement colorlutElem = textureElem.GetChild("colorlut");
  74. if (volumeElem)
  75. {
  76. String name = volumeElem.GetAttribute("name");
  77. String volumeTexPath, volumeTexName, volumeTexExt;
  78. SplitPath(name, volumeTexPath, volumeTexName, volumeTexExt);
  79. // If path is empty, add the XML file path
  80. if (volumeTexPath.Empty())
  81. name = texPath + name;
  82. SharedPtr<Image> image(GetSubsystem<ResourceCache>()->GetResource<Image>(name));
  83. return Load(image);
  84. }
  85. else if (colorlutElem)
  86. {
  87. String name = colorlutElem.GetAttribute("name");
  88. String colorlutTexPath, colorlutTexName, colorlutTexExt;
  89. SplitPath(name, colorlutTexPath, colorlutTexName, colorlutTexExt);
  90. // If path is empty, add the XML file path
  91. if (colorlutTexPath.Empty())
  92. name = texPath + name;
  93. SharedPtr<File> file = GetSubsystem<ResourceCache>()->GetFile(name);
  94. SharedPtr<Image> image(new Image(context_));
  95. if (!image->LoadColorLUT(*(file.Get())))
  96. return false;
  97. return Load(image);
  98. }
  99. return false;
  100. }
  101. void Texture3D::OnDeviceLost()
  102. {
  103. if (pool_ == D3DPOOL_DEFAULT)
  104. Release();
  105. }
  106. void Texture3D::OnDeviceReset()
  107. {
  108. if (pool_ == D3DPOOL_DEFAULT || !object_)
  109. {
  110. // If has a file name, reload through the resource cache. Otherwise just recreate.
  111. if (!GetName().Trimmed().Empty())
  112. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  113. else
  114. {
  115. Create();
  116. dataLost_ = true;
  117. }
  118. }
  119. else if (dataPending_)
  120. {
  121. if (!GetName().Trimmed().Empty())
  122. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  123. else
  124. dataLost_ = true;
  125. }
  126. dataPending_ = false;
  127. }
  128. void Texture3D::Release()
  129. {
  130. if (object_)
  131. {
  132. if (!graphics_)
  133. return;
  134. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  135. {
  136. if (graphics_->GetTexture(i) == this)
  137. graphics_->SetTexture(i, 0);
  138. }
  139. if (renderSurface_)
  140. renderSurface_->Release();
  141. ((IDirect3DVolumeTexture9*)object_)->Release();
  142. object_ = 0;
  143. }
  144. else
  145. {
  146. if (renderSurface_)
  147. renderSurface_->Release();
  148. }
  149. }
  150. bool Texture3D::SetSize(int width, int height, int depth, unsigned format, TextureUsage usage)
  151. {
  152. // Delete the old rendersurface if any
  153. renderSurface_.Reset();
  154. pool_ = D3DPOOL_MANAGED;
  155. usage_ = 0;
  156. if (usage == TEXTURE_RENDERTARGET)
  157. {
  158. renderSurface_ = new RenderSurface(this);
  159. usage_ |= D3DUSAGE_RENDERTARGET;
  160. pool_ = D3DPOOL_DEFAULT;
  161. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  162. addressMode_[COORD_U] = ADDRESS_CLAMP;
  163. addressMode_[COORD_V] = ADDRESS_CLAMP;
  164. filterMode_ = FILTER_NEAREST;
  165. requestedLevels_ = 1;
  166. }
  167. else if (usage == TEXTURE_DYNAMIC)
  168. {
  169. usage_ |= D3DUSAGE_DYNAMIC;
  170. pool_ = D3DPOOL_DEFAULT;
  171. }
  172. if (usage == TEXTURE_RENDERTARGET)
  173. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(Texture3D, HandleRenderSurfaceUpdate));
  174. else
  175. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  176. width_ = width;
  177. height_ = height;
  178. depth_ = depth;
  179. format_ = format;
  180. return Create();
  181. }
  182. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  183. {
  184. if (!object_)
  185. {
  186. LOGERROR("No texture created, can not set data");
  187. return false;
  188. }
  189. if (!data)
  190. {
  191. LOGERROR("Null source for setting data");
  192. return false;
  193. }
  194. if (level >= levels_)
  195. {
  196. LOGERROR("Illegal mip level for setting data");
  197. return false;
  198. }
  199. if (graphics_->IsDeviceLost())
  200. {
  201. LOGWARNING("Texture data assignment while device is lost");
  202. dataPending_ = true;
  203. return true;
  204. }
  205. if (IsCompressed())
  206. {
  207. x &= ~3;
  208. y &= ~3;
  209. }
  210. int levelWidth = GetLevelWidth(level);
  211. int levelHeight = GetLevelHeight(level);
  212. int levelDepth = GetLevelDepth(level);
  213. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || z < 0 || z + depth > levelDepth || width <= 0 || height <= 0 || depth <= 0)
  214. {
  215. LOGERROR("Illegal dimensions for setting data");
  216. return false;
  217. }
  218. D3DLOCKED_BOX d3dLockedBox;
  219. D3DBOX d3dBox;
  220. d3dBox.Left = x;
  221. d3dBox.Top = y;
  222. d3dBox.Front = z;
  223. d3dBox.Right = x + width;
  224. d3dBox.Bottom = y + height;
  225. d3dBox.Back = z + depth;
  226. DWORD flags = 0;
  227. if (level == 0 && x == 0 && y == 0 && z == 0 && width == levelWidth && height == levelHeight && depth == levelDepth && pool_ == D3DPOOL_DEFAULT)
  228. flags |= D3DLOCK_DISCARD;
  229. if (FAILED(((IDirect3DVolumeTexture9*)object_)->LockBox(level, &d3dLockedBox, (flags & D3DLOCK_DISCARD) ? 0 : &d3dBox, flags)))
  230. {
  231. LOGERROR("Could not lock texture");
  232. return false;
  233. }
  234. if (IsCompressed())
  235. {
  236. height = (height + 3) >> 2;
  237. y >>= 2;
  238. }
  239. unsigned char* src = (unsigned char*)data;
  240. unsigned rowSize = GetRowDataSize(width);
  241. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  242. if (format_ == D3DFMT_X8R8G8B8)
  243. rowSize = rowSize / 3 * 4;
  244. // Perform conversion from RGB / RGBA as necessary
  245. switch (format_)
  246. {
  247. default:
  248. for (int k = 0; k < levelDepth; ++k)
  249. {
  250. for (int i = 0; i < height; ++i)
  251. {
  252. unsigned char* dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  253. memcpy(dest, src, rowSize);
  254. src += rowSize;
  255. }
  256. }
  257. break;
  258. case D3DFMT_X8R8G8B8:
  259. for (int k = 0; k < levelDepth; ++k)
  260. {
  261. for (int i = 0; i < height; ++i)
  262. {
  263. unsigned char* dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  264. for (int j = 0; j < width; ++j)
  265. {
  266. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  267. src += 3;
  268. }
  269. }
  270. }
  271. break;
  272. case D3DFMT_A8R8G8B8:
  273. for (int k = 0; k < levelDepth; ++k)
  274. {
  275. for (int i = 0; i < height; ++i)
  276. {
  277. unsigned char* dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  278. for (int j = 0; j < width; ++j)
  279. {
  280. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  281. src += 4;
  282. }
  283. }
  284. }
  285. break;
  286. }
  287. ((IDirect3DVolumeTexture9*)object_)->UnlockBox(level);
  288. return true;
  289. }
  290. bool Texture3D::Load(SharedPtr<Image> image, bool useAlpha)
  291. {
  292. if (!image)
  293. {
  294. LOGERROR("Null image, can not load texture");
  295. return false;
  296. }
  297. unsigned memoryUse = sizeof(Texture3D);
  298. int quality = QUALITY_HIGH;
  299. Renderer* renderer = GetSubsystem<Renderer>();
  300. if (renderer)
  301. quality = renderer->GetTextureQuality();
  302. if (!image->IsCompressed())
  303. {
  304. unsigned char* levelData = image->GetData();
  305. int levelWidth = image->GetWidth();
  306. int levelHeight = image->GetHeight();
  307. int levelDepth = image->GetDepth();
  308. unsigned components = image->GetComponents();
  309. unsigned format = 0;
  310. // Discard unnecessary mip levels
  311. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  312. {
  313. image = image->GetNextLevel();
  314. levelData = image->GetData();
  315. levelWidth = image->GetWidth();
  316. levelHeight = image->GetHeight();
  317. levelDepth = image->GetDepth();
  318. }
  319. switch (components)
  320. {
  321. case 1:
  322. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  323. break;
  324. case 2:
  325. format = Graphics::GetLuminanceAlphaFormat();
  326. break;
  327. case 3:
  328. format = Graphics::GetRGBFormat();
  329. break;
  330. case 4:
  331. format = Graphics::GetRGBAFormat();
  332. break;
  333. }
  334. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  335. if (IsCompressed() && requestedLevels_ > 1)
  336. requestedLevels_ = 0;
  337. SetSize(levelWidth, levelHeight, levelDepth, format);
  338. for (unsigned i = 0; i < levels_; ++i)
  339. {
  340. SetData(i, 0, 0, 0, levelWidth, levelHeight, levelDepth, levelData);
  341. memoryUse += levelWidth * levelHeight * levelDepth * components;
  342. if (i < levels_ - 1)
  343. {
  344. image = image->GetNextLevel();
  345. levelData = image->GetData();
  346. levelWidth = image->GetWidth();
  347. levelHeight = image->GetHeight();
  348. levelDepth = image->GetDepth();
  349. }
  350. }
  351. }
  352. else
  353. {
  354. int width = image->GetWidth();
  355. int height = image->GetHeight();
  356. int depth = image->GetDepth();
  357. unsigned levels = image->GetNumCompressedLevels();
  358. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  359. bool needDecompress = false;
  360. if (!format)
  361. {
  362. format = Graphics::GetRGBAFormat();
  363. needDecompress = true;
  364. }
  365. unsigned mipsToSkip = mipsToSkip_[quality];
  366. if (mipsToSkip >= levels)
  367. mipsToSkip = levels - 1;
  368. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4 || depth / (1 << mipsToSkip) < 4))
  369. --mipsToSkip;
  370. width /= (1 << mipsToSkip);
  371. height /= (1 << mipsToSkip);
  372. depth /= (1 << mipsToSkip);
  373. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  374. SetSize(width, height, depth, format);
  375. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  376. {
  377. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  378. if (!needDecompress)
  379. {
  380. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, level.data_);
  381. memoryUse += level.depth_ * level.rows_ * level.rowSize_;
  382. }
  383. else
  384. {
  385. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * level.depth_ * 4];
  386. level.Decompress(rgbaData);
  387. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, rgbaData);
  388. memoryUse += level.width_ * level.height_ * level.depth_ * 4;
  389. delete[] rgbaData;
  390. }
  391. }
  392. }
  393. SetMemoryUse(memoryUse);
  394. return true;
  395. }
  396. bool Texture3D::GetData(unsigned level, void* dest) const
  397. {
  398. if (!object_)
  399. {
  400. LOGERROR("No texture created, can not get data");
  401. return false;
  402. }
  403. if (!dest)
  404. {
  405. LOGERROR("Null destination for getting data");
  406. return false;
  407. }
  408. if (level >= levels_)
  409. {
  410. LOGERROR("Illegal mip level for getting data");
  411. return false;
  412. }
  413. if (graphics_->IsDeviceLost())
  414. {
  415. LOGWARNING("Getting texture data while device is lost");
  416. return false;
  417. }
  418. int levelWidth = GetLevelWidth(level);
  419. int levelHeight = GetLevelHeight(level);
  420. int levelDepth = GetLevelDepth(level);
  421. D3DLOCKED_BOX d3dLockedBox;
  422. D3DBOX d3dBox;
  423. d3dBox.Left = 0;
  424. d3dBox.Top = 0;
  425. d3dBox.Front = 0;
  426. d3dBox.Right = levelWidth;
  427. d3dBox.Bottom = levelHeight;
  428. d3dBox.Back = levelDepth;
  429. if (FAILED(((IDirect3DVolumeTexture9*)object_)->LockBox(level, &d3dLockedBox, &d3dBox, D3DLOCK_READONLY)))
  430. {
  431. LOGERROR("Could not lock texture");
  432. return false;
  433. }
  434. int height = levelHeight;
  435. if (IsCompressed())
  436. height = (height + 3) >> 2;
  437. unsigned char* destPtr = (unsigned char*)dest;
  438. unsigned rowSize = GetRowDataSize(levelWidth);
  439. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  440. if (format_ == D3DFMT_X8R8G8B8)
  441. rowSize = rowSize / 3 * 4;
  442. // Perform conversion to RGB / RGBA as necessary
  443. switch (format_)
  444. {
  445. default:
  446. for (int k = 0; k < levelDepth; ++k)
  447. {
  448. for (int i = 0; i < height; ++i)
  449. {
  450. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  451. memcpy(destPtr, src, rowSize);
  452. destPtr += rowSize;
  453. }
  454. }
  455. break;
  456. case D3DFMT_X8R8G8B8:
  457. for (int k = 0; k < levelDepth; ++k)
  458. {
  459. for (int i = 0; i < height; ++i)
  460. {
  461. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  462. for (int j = 0; j < levelWidth; ++j)
  463. {
  464. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  465. destPtr += 3;
  466. }
  467. }
  468. }
  469. break;
  470. case D3DFMT_A8R8G8B8:
  471. for (int k = 0; k < levelDepth; ++k)
  472. {
  473. for (int i = 0; i < height; ++i)
  474. {
  475. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  476. for (int j = 0; j < levelWidth; ++j)
  477. {
  478. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  479. destPtr += 4;
  480. }
  481. }
  482. }
  483. break;
  484. }
  485. ((IDirect3DVolumeTexture9*)object_)->UnlockBox(level);
  486. return true;
  487. }
  488. bool Texture3D::Create()
  489. {
  490. Release();
  491. if (!graphics_ || !width_ || !height_)
  492. return false;
  493. if (graphics_->IsDeviceLost())
  494. {
  495. LOGWARNING("Texture creation while device is lost");
  496. return true;
  497. }
  498. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  499. if (!device || FAILED(graphics_->GetImpl()->GetDevice()->CreateVolumeTexture(
  500. width_,
  501. height_,
  502. depth_,
  503. requestedLevels_,
  504. usage_,
  505. (D3DFORMAT)format_,
  506. (D3DPOOL)pool_,
  507. (IDirect3DVolumeTexture9**)&object_,
  508. 0)))
  509. {
  510. LOGERROR("Could not create texture");
  511. return false;
  512. }
  513. levels_ = ((IDirect3DVolumeTexture9*)object_)->GetLevelCount();
  514. return true;
  515. }
  516. void Texture3D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  517. {
  518. if (renderSurface_ && renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  519. renderSurface_->QueueUpdate();
  520. }
  521. }