D3D9Texture3D.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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_ || dataPending_)
  109. {
  110. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  111. ResourceCache* cache = GetSubsystem<ResourceCache>();
  112. if (cache->Exists(GetName()))
  113. dataLost_ = !cache->ReloadResource(this);
  114. if (!object_)
  115. {
  116. Create();
  117. dataLost_ = true;
  118. }
  119. }
  120. dataPending_ = false;
  121. }
  122. void Texture3D::Release()
  123. {
  124. if (object_)
  125. {
  126. if (!graphics_)
  127. return;
  128. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  129. {
  130. if (graphics_->GetTexture(i) == this)
  131. graphics_->SetTexture(i, 0);
  132. }
  133. if (renderSurface_)
  134. renderSurface_->Release();
  135. ((IDirect3DVolumeTexture9*)object_)->Release();
  136. object_ = 0;
  137. }
  138. else
  139. {
  140. if (renderSurface_)
  141. renderSurface_->Release();
  142. }
  143. }
  144. bool Texture3D::SetSize(int width, int height, int depth, unsigned format, TextureUsage usage)
  145. {
  146. // Delete the old rendersurface if any
  147. renderSurface_.Reset();
  148. pool_ = D3DPOOL_MANAGED;
  149. usage_ = 0;
  150. if (usage == TEXTURE_RENDERTARGET)
  151. {
  152. renderSurface_ = new RenderSurface(this);
  153. usage_ |= D3DUSAGE_RENDERTARGET;
  154. pool_ = D3DPOOL_DEFAULT;
  155. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  156. addressMode_[COORD_U] = ADDRESS_CLAMP;
  157. addressMode_[COORD_V] = ADDRESS_CLAMP;
  158. filterMode_ = FILTER_NEAREST;
  159. requestedLevels_ = 1;
  160. }
  161. else if (usage == TEXTURE_DYNAMIC)
  162. {
  163. usage_ |= D3DUSAGE_DYNAMIC;
  164. pool_ = D3DPOOL_DEFAULT;
  165. }
  166. if (usage == TEXTURE_RENDERTARGET)
  167. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(Texture3D, HandleRenderSurfaceUpdate));
  168. else
  169. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  170. width_ = width;
  171. height_ = height;
  172. depth_ = depth;
  173. format_ = format;
  174. return Create();
  175. }
  176. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  177. {
  178. if (!object_)
  179. {
  180. LOGERROR("No texture created, can not set data");
  181. return false;
  182. }
  183. if (!data)
  184. {
  185. LOGERROR("Null source for setting data");
  186. return false;
  187. }
  188. if (level >= levels_)
  189. {
  190. LOGERROR("Illegal mip level for setting data");
  191. return false;
  192. }
  193. if (graphics_->IsDeviceLost())
  194. {
  195. LOGWARNING("Texture data assignment while device is lost");
  196. dataPending_ = true;
  197. return true;
  198. }
  199. if (IsCompressed())
  200. {
  201. x &= ~3;
  202. y &= ~3;
  203. }
  204. int levelWidth = GetLevelWidth(level);
  205. int levelHeight = GetLevelHeight(level);
  206. int levelDepth = GetLevelDepth(level);
  207. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || z < 0 || z + depth > levelDepth || width <= 0 || height <= 0 || depth <= 0)
  208. {
  209. LOGERROR("Illegal dimensions for setting data");
  210. return false;
  211. }
  212. D3DLOCKED_BOX d3dLockedBox;
  213. D3DBOX d3dBox;
  214. d3dBox.Left = x;
  215. d3dBox.Top = y;
  216. d3dBox.Front = z;
  217. d3dBox.Right = x + width;
  218. d3dBox.Bottom = y + height;
  219. d3dBox.Back = z + depth;
  220. DWORD flags = 0;
  221. if (level == 0 && x == 0 && y == 0 && z == 0 && width == levelWidth && height == levelHeight && depth == levelDepth && pool_ == D3DPOOL_DEFAULT)
  222. flags |= D3DLOCK_DISCARD;
  223. if (FAILED(((IDirect3DVolumeTexture9*)object_)->LockBox(level, &d3dLockedBox, (flags & D3DLOCK_DISCARD) ? 0 : &d3dBox, flags)))
  224. {
  225. LOGERROR("Could not lock texture");
  226. return false;
  227. }
  228. if (IsCompressed())
  229. {
  230. height = (height + 3) >> 2;
  231. y >>= 2;
  232. }
  233. unsigned char* src = (unsigned char*)data;
  234. unsigned rowSize = GetRowDataSize(width);
  235. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  236. if (format_ == D3DFMT_X8R8G8B8)
  237. rowSize = rowSize / 3 * 4;
  238. // Perform conversion from RGB / RGBA as necessary
  239. switch (format_)
  240. {
  241. default:
  242. for (int k = 0; k < levelDepth; ++k)
  243. {
  244. for (int i = 0; i < height; ++i)
  245. {
  246. unsigned char* dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  247. memcpy(dest, src, rowSize);
  248. src += rowSize;
  249. }
  250. }
  251. break;
  252. case D3DFMT_X8R8G8B8:
  253. for (int k = 0; k < levelDepth; ++k)
  254. {
  255. for (int i = 0; i < height; ++i)
  256. {
  257. unsigned char* dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  258. for (int j = 0; j < width; ++j)
  259. {
  260. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  261. src += 3;
  262. }
  263. }
  264. }
  265. break;
  266. case D3DFMT_A8R8G8B8:
  267. for (int k = 0; k < levelDepth; ++k)
  268. {
  269. for (int i = 0; i < height; ++i)
  270. {
  271. unsigned char* dest = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  272. for (int j = 0; j < width; ++j)
  273. {
  274. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  275. src += 4;
  276. }
  277. }
  278. }
  279. break;
  280. }
  281. ((IDirect3DVolumeTexture9*)object_)->UnlockBox(level);
  282. return true;
  283. }
  284. bool Texture3D::Load(SharedPtr<Image> image, bool useAlpha)
  285. {
  286. if (!image)
  287. {
  288. LOGERROR("Null image, can not load texture");
  289. return false;
  290. }
  291. unsigned memoryUse = sizeof(Texture3D);
  292. int quality = QUALITY_HIGH;
  293. Renderer* renderer = GetSubsystem<Renderer>();
  294. if (renderer)
  295. quality = renderer->GetTextureQuality();
  296. if (!image->IsCompressed())
  297. {
  298. unsigned char* levelData = image->GetData();
  299. int levelWidth = image->GetWidth();
  300. int levelHeight = image->GetHeight();
  301. int levelDepth = image->GetDepth();
  302. unsigned components = image->GetComponents();
  303. unsigned format = 0;
  304. // Discard unnecessary mip levels
  305. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  306. {
  307. image = image->GetNextLevel();
  308. levelData = image->GetData();
  309. levelWidth = image->GetWidth();
  310. levelHeight = image->GetHeight();
  311. levelDepth = image->GetDepth();
  312. }
  313. switch (components)
  314. {
  315. case 1:
  316. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  317. break;
  318. case 2:
  319. format = Graphics::GetLuminanceAlphaFormat();
  320. break;
  321. case 3:
  322. format = Graphics::GetRGBFormat();
  323. break;
  324. case 4:
  325. format = Graphics::GetRGBAFormat();
  326. break;
  327. }
  328. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  329. if (IsCompressed() && requestedLevels_ > 1)
  330. requestedLevels_ = 0;
  331. SetSize(levelWidth, levelHeight, levelDepth, format);
  332. for (unsigned i = 0; i < levels_; ++i)
  333. {
  334. SetData(i, 0, 0, 0, levelWidth, levelHeight, levelDepth, levelData);
  335. memoryUse += levelWidth * levelHeight * levelDepth * components;
  336. if (i < levels_ - 1)
  337. {
  338. image = image->GetNextLevel();
  339. levelData = image->GetData();
  340. levelWidth = image->GetWidth();
  341. levelHeight = image->GetHeight();
  342. levelDepth = image->GetDepth();
  343. }
  344. }
  345. }
  346. else
  347. {
  348. int width = image->GetWidth();
  349. int height = image->GetHeight();
  350. int depth = image->GetDepth();
  351. unsigned levels = image->GetNumCompressedLevels();
  352. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  353. bool needDecompress = false;
  354. if (!format)
  355. {
  356. format = Graphics::GetRGBAFormat();
  357. needDecompress = true;
  358. }
  359. unsigned mipsToSkip = mipsToSkip_[quality];
  360. if (mipsToSkip >= levels)
  361. mipsToSkip = levels - 1;
  362. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4 || depth / (1 << mipsToSkip) < 4))
  363. --mipsToSkip;
  364. width /= (1 << mipsToSkip);
  365. height /= (1 << mipsToSkip);
  366. depth /= (1 << mipsToSkip);
  367. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  368. SetSize(width, height, depth, format);
  369. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  370. {
  371. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  372. if (!needDecompress)
  373. {
  374. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, level.data_);
  375. memoryUse += level.depth_ * level.rows_ * level.rowSize_;
  376. }
  377. else
  378. {
  379. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * level.depth_ * 4];
  380. level.Decompress(rgbaData);
  381. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, rgbaData);
  382. memoryUse += level.width_ * level.height_ * level.depth_ * 4;
  383. delete[] rgbaData;
  384. }
  385. }
  386. }
  387. SetMemoryUse(memoryUse);
  388. return true;
  389. }
  390. bool Texture3D::GetData(unsigned level, void* dest) const
  391. {
  392. if (!object_)
  393. {
  394. LOGERROR("No texture created, can not get data");
  395. return false;
  396. }
  397. if (!dest)
  398. {
  399. LOGERROR("Null destination for getting data");
  400. return false;
  401. }
  402. if (level >= levels_)
  403. {
  404. LOGERROR("Illegal mip level for getting data");
  405. return false;
  406. }
  407. if (graphics_->IsDeviceLost())
  408. {
  409. LOGWARNING("Getting texture data while device is lost");
  410. return false;
  411. }
  412. int levelWidth = GetLevelWidth(level);
  413. int levelHeight = GetLevelHeight(level);
  414. int levelDepth = GetLevelDepth(level);
  415. D3DLOCKED_BOX d3dLockedBox;
  416. D3DBOX d3dBox;
  417. d3dBox.Left = 0;
  418. d3dBox.Top = 0;
  419. d3dBox.Front = 0;
  420. d3dBox.Right = levelWidth;
  421. d3dBox.Bottom = levelHeight;
  422. d3dBox.Back = levelDepth;
  423. if (FAILED(((IDirect3DVolumeTexture9*)object_)->LockBox(level, &d3dLockedBox, &d3dBox, D3DLOCK_READONLY)))
  424. {
  425. LOGERROR("Could not lock texture");
  426. return false;
  427. }
  428. int height = levelHeight;
  429. if (IsCompressed())
  430. height = (height + 3) >> 2;
  431. unsigned char* destPtr = (unsigned char*)dest;
  432. unsigned rowSize = GetRowDataSize(levelWidth);
  433. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  434. if (format_ == D3DFMT_X8R8G8B8)
  435. rowSize = rowSize / 3 * 4;
  436. // Perform conversion to RGB / RGBA as necessary
  437. switch (format_)
  438. {
  439. default:
  440. for (int k = 0; k < levelDepth; ++k)
  441. {
  442. for (int i = 0; i < height; ++i)
  443. {
  444. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  445. memcpy(destPtr, src, rowSize);
  446. destPtr += rowSize;
  447. }
  448. }
  449. break;
  450. case D3DFMT_X8R8G8B8:
  451. for (int k = 0; k < levelDepth; ++k)
  452. {
  453. for (int i = 0; i < height; ++i)
  454. {
  455. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  456. for (int j = 0; j < levelWidth; ++j)
  457. {
  458. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  459. destPtr += 3;
  460. }
  461. }
  462. }
  463. break;
  464. case D3DFMT_A8R8G8B8:
  465. for (int k = 0; k < levelDepth; ++k)
  466. {
  467. for (int i = 0; i < height; ++i)
  468. {
  469. unsigned char* src = (unsigned char*)d3dLockedBox.pBits + (k * d3dLockedBox.SlicePitch) + i * d3dLockedBox.RowPitch;
  470. for (int j = 0; j < levelWidth; ++j)
  471. {
  472. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  473. destPtr += 4;
  474. }
  475. }
  476. }
  477. break;
  478. }
  479. ((IDirect3DVolumeTexture9*)object_)->UnlockBox(level);
  480. return true;
  481. }
  482. bool Texture3D::Create()
  483. {
  484. Release();
  485. if (!graphics_ || !width_ || !height_)
  486. return false;
  487. if (graphics_->IsDeviceLost())
  488. {
  489. LOGWARNING("Texture creation while device is lost");
  490. return true;
  491. }
  492. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  493. if (!device || FAILED(graphics_->GetImpl()->GetDevice()->CreateVolumeTexture(
  494. width_,
  495. height_,
  496. depth_,
  497. requestedLevels_,
  498. usage_,
  499. (D3DFORMAT)format_,
  500. (D3DPOOL)pool_,
  501. (IDirect3DVolumeTexture9**)&object_,
  502. 0)))
  503. {
  504. LOGERROR("Could not create texture");
  505. return false;
  506. }
  507. levels_ = ((IDirect3DVolumeTexture9*)object_)->GetLevelCount();
  508. return true;
  509. }
  510. void Texture3D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  511. {
  512. if (renderSurface_ && renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  513. renderSurface_->QueueUpdate();
  514. }
  515. }