D3D9Texture2D.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "Log.h"
  28. #include "Renderer.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "Texture2D.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. OBJECTTYPESTATIC(Texture2D);
  36. Texture2D::Texture2D(Context* context) :
  37. Texture(context)
  38. {
  39. }
  40. Texture2D::~Texture2D()
  41. {
  42. Release();
  43. }
  44. void Texture2D::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<Texture2D>();
  47. }
  48. bool Texture2D::Load(Deserializer& source)
  49. {
  50. PROFILE(LoadTexture2D);
  51. // In headless mode, do not actually load the texture, just return success
  52. Graphics* graphics = GetSubsystem<Graphics>();
  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. SharedPtr<Image> image(new Image(context_));
  65. if (!image->Load(source))
  66. return false;
  67. // Before actually loading the texture, get optional parameters from an XML description file
  68. LoadParameters();
  69. return Load(image);
  70. }
  71. void Texture2D::OnDeviceLost()
  72. {
  73. if (pool_ == D3DPOOL_DEFAULT)
  74. Release();
  75. }
  76. void Texture2D::OnDeviceReset()
  77. {
  78. if (pool_ == D3DPOOL_DEFAULT || !object_)
  79. {
  80. // If has a file name, reload through the resource cache. Otherwise just recreate.
  81. if (!GetName().Trimmed().Empty())
  82. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  83. else
  84. {
  85. Create();
  86. dataLost_ = true;
  87. }
  88. }
  89. else if (dataPending_)
  90. {
  91. if (!GetName().Trimmed().Empty())
  92. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  93. else
  94. dataLost_ = true;
  95. }
  96. dataPending_ = false;
  97. }
  98. void Texture2D::Release()
  99. {
  100. if (object_)
  101. {
  102. if (!graphics_)
  103. return;
  104. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  105. {
  106. if (graphics_->GetTexture(i) == this)
  107. graphics_->SetTexture(i, 0);
  108. }
  109. if (renderSurface_)
  110. renderSurface_->Release();
  111. ((IDirect3DTexture9*)object_)->Release();
  112. object_ = 0;
  113. }
  114. else
  115. {
  116. if (renderSurface_)
  117. renderSurface_->Release();
  118. }
  119. }
  120. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage)
  121. {
  122. // Delete the old rendersurface if any
  123. renderSurface_.Reset();
  124. pool_ = D3DPOOL_MANAGED;
  125. usage_ = 0;
  126. if (usage == TEXTURE_RENDERTARGET || usage == TEXTURE_DEPTHSTENCIL)
  127. {
  128. renderSurface_ = new RenderSurface(this);
  129. if (usage == TEXTURE_RENDERTARGET)
  130. usage_ |= D3DUSAGE_RENDERTARGET;
  131. else
  132. usage_ |= D3DUSAGE_DEPTHSTENCIL;
  133. pool_ = D3DPOOL_DEFAULT;
  134. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  135. addressMode_[COORD_U] = ADDRESS_CLAMP;
  136. addressMode_[COORD_V] = ADDRESS_CLAMP;
  137. filterMode_ = FILTER_NEAREST;
  138. requestedLevels_ = 1;
  139. }
  140. else if (usage == TEXTURE_DYNAMIC)
  141. {
  142. usage_ |= D3DUSAGE_DYNAMIC;
  143. pool_ = D3DPOOL_DEFAULT;
  144. }
  145. width_ = width;
  146. height_ = height;
  147. format_ = format;
  148. return Create();
  149. }
  150. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  151. {
  152. if (!object_)
  153. {
  154. LOGERROR("No texture created, can not set data");
  155. return false;
  156. }
  157. if (!data)
  158. {
  159. LOGERROR("Null source for setting data");
  160. return false;
  161. }
  162. if (level >= levels_)
  163. {
  164. LOGERROR("Illegal mip level for setting data");
  165. return false;
  166. }
  167. if (graphics_->IsDeviceLost())
  168. {
  169. LOGWARNING("Texture data assignment while device is lost");
  170. dataPending_ = true;
  171. return true;
  172. }
  173. if (IsCompressed())
  174. {
  175. x &= ~3;
  176. y &= ~3;
  177. }
  178. int levelWidth = GetLevelWidth(level);
  179. int levelHeight = GetLevelHeight(level);
  180. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  181. {
  182. LOGERROR("Illegal dimensions for setting data");
  183. return false;
  184. }
  185. D3DLOCKED_RECT d3dLockedRect;
  186. RECT d3dRect;
  187. d3dRect.left = x;
  188. d3dRect.top = y;
  189. d3dRect.right = x + width;
  190. d3dRect.bottom = y + height;
  191. DWORD flags = 0;
  192. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  193. flags |= D3DLOCK_DISCARD;
  194. if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  195. {
  196. LOGERROR("Could not lock texture");
  197. return false;
  198. }
  199. if (IsCompressed())
  200. {
  201. height = (height + 3) >> 2;
  202. y >>= 2;
  203. }
  204. unsigned char* src = (unsigned char*)data;
  205. unsigned rowSize = GetRowDataSize(width);
  206. unsigned rowOffset = GetRowDataSize(x);
  207. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  208. if (format_ == D3DFMT_X8R8G8B8)
  209. {
  210. rowSize = rowSize / 3 * 4;
  211. rowOffset = rowOffset / 3 * 4;
  212. }
  213. // Perform conversion from RGB / RGBA as necessary
  214. switch (format_)
  215. {
  216. default:
  217. for (int i = 0; i < height; ++i)
  218. {
  219. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  220. memcpy(dest, src, rowSize);
  221. src += rowSize;
  222. }
  223. break;
  224. case D3DFMT_X8R8G8B8:
  225. for (int i = 0; i < height; ++i)
  226. {
  227. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  228. for (int j = 0; j < levelWidth; ++j)
  229. {
  230. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  231. src += 3;
  232. }
  233. }
  234. break;
  235. case D3DFMT_A8R8G8B8:
  236. for (int i = 0; i < height; ++i)
  237. {
  238. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  239. for (int j = 0; j < levelWidth; ++j)
  240. {
  241. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  242. src += 4;
  243. }
  244. }
  245. break;
  246. }
  247. ((IDirect3DTexture9*)object_)->UnlockRect(level);
  248. return true;
  249. }
  250. bool Texture2D::Load(SharedPtr<Image> image, bool useAlpha)
  251. {
  252. if (!image)
  253. {
  254. LOGERROR("Null image, can not load texture");
  255. return false;
  256. }
  257. unsigned memoryUse = sizeof(Texture2D);
  258. int quality = QUALITY_HIGH;
  259. Renderer* renderer = GetSubsystem<Renderer>();
  260. if (renderer)
  261. quality = renderer->GetTextureQuality();
  262. if (!image->IsCompressed())
  263. {
  264. unsigned char* levelData = image->GetData();
  265. int levelWidth = image->GetWidth();
  266. int levelHeight = image->GetHeight();
  267. unsigned components = image->GetComponents();
  268. unsigned format = 0;
  269. // Discard unnecessary mip levels
  270. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  271. {
  272. image = image->GetNextLevel();
  273. levelData = image->GetData();
  274. levelWidth = image->GetWidth();
  275. levelHeight = image->GetHeight();
  276. }
  277. switch (components)
  278. {
  279. case 1:
  280. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  281. break;
  282. case 2:
  283. format = Graphics::GetLuminanceAlphaFormat();
  284. break;
  285. case 3:
  286. format = Graphics::GetRGBFormat();
  287. break;
  288. case 4:
  289. format = Graphics::GetRGBAFormat();
  290. break;
  291. }
  292. SetSize(levelWidth, levelHeight, format);
  293. for (unsigned i = 0; i < levels_; ++i)
  294. {
  295. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  296. memoryUse += levelWidth * levelHeight * components;
  297. if (i < levels_ - 1)
  298. {
  299. image = image->GetNextLevel();
  300. levelData = image->GetData();
  301. levelWidth = image->GetWidth();
  302. levelHeight = image->GetHeight();
  303. }
  304. }
  305. }
  306. else
  307. {
  308. int width = image->GetWidth();
  309. int height = image->GetHeight();
  310. unsigned levels = image->GetNumCompressedLevels();
  311. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  312. bool needDecompress = false;
  313. if (!format)
  314. {
  315. format = Graphics::GetRGBAFormat();
  316. needDecompress = true;
  317. }
  318. unsigned mipsToSkip = mipsToSkip_[quality];
  319. if (mipsToSkip >= levels)
  320. mipsToSkip = levels - 1;
  321. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  322. --mipsToSkip;
  323. width /= (1 << mipsToSkip);
  324. height /= (1 << mipsToSkip);
  325. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  326. SetSize(width, height, format);
  327. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  328. {
  329. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  330. if (!needDecompress)
  331. {
  332. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  333. memoryUse += level.rows_ * level.rowSize_;
  334. }
  335. else
  336. {
  337. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  338. level.Decompress(rgbaData);
  339. SetData(i, 0, 0, level.width_, level.height_, rgbaData);
  340. memoryUse += level.width_ * level.height_ * 4;
  341. delete[] rgbaData;
  342. }
  343. }
  344. }
  345. SetMemoryUse(memoryUse);
  346. return true;
  347. }
  348. bool Texture2D::GetData(unsigned level, void* dest) const
  349. {
  350. if (!object_)
  351. {
  352. LOGERROR("No texture created, can not get data");
  353. return false;
  354. }
  355. if (!dest)
  356. {
  357. LOGERROR("Null destination for getting data");
  358. return false;
  359. }
  360. if (level >= levels_)
  361. {
  362. LOGERROR("Illegal mip level for getting data");
  363. return false;
  364. }
  365. if (graphics_->IsDeviceLost())
  366. {
  367. LOGWARNING("Getting texture data while device is lost");
  368. return false;
  369. }
  370. int levelWidth = GetLevelWidth(level);
  371. int levelHeight = GetLevelHeight(level);
  372. D3DLOCKED_RECT d3dLockedRect;
  373. RECT d3dRect;
  374. d3dRect.left = 0;
  375. d3dRect.top = 0;
  376. d3dRect.right = levelWidth;
  377. d3dRect.bottom = levelHeight;
  378. if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  379. {
  380. LOGERROR("Could not lock texture");
  381. return false;
  382. }
  383. int height = levelHeight;
  384. if (IsCompressed())
  385. height = (height + 3) >> 2;
  386. unsigned char* destPtr = (unsigned char*)dest;
  387. unsigned rowSize = GetRowDataSize(levelWidth);
  388. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  389. if (format_ == D3DFMT_X8R8G8B8)
  390. rowSize = rowSize / 3 * 4;
  391. // Perform conversion to RGB / RGBA as necessary
  392. switch (format_)
  393. {
  394. default:
  395. for (int i = 0; i < height; ++i)
  396. {
  397. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  398. memcpy(destPtr, src, rowSize);
  399. destPtr += rowSize;
  400. }
  401. break;
  402. case D3DFMT_X8R8G8B8:
  403. for (int i = 0; i < height; ++i)
  404. {
  405. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  406. for (int j = 0; j < levelWidth; ++j)
  407. {
  408. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  409. destPtr += 3;
  410. }
  411. }
  412. break;
  413. case D3DFMT_A8R8G8B8:
  414. for (int i = 0; i < height; ++i)
  415. {
  416. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  417. for (int j = 0; j < levelWidth; ++j)
  418. {
  419. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  420. destPtr += 4;
  421. }
  422. }
  423. break;
  424. }
  425. ((IDirect3DTexture9*)object_)->UnlockRect(level);
  426. return true;
  427. }
  428. bool Texture2D::Create()
  429. {
  430. Release();
  431. if (!graphics_ || !width_ || !height_)
  432. return false;
  433. if (graphics_->IsDeviceLost())
  434. {
  435. LOGWARNING("Texture creation while device is lost");
  436. return true;
  437. }
  438. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  439. // If creating a depth-stencil texture, and it is not supported, create a depth-stencil surface instead
  440. if (usage_ & D3DUSAGE_DEPTHSTENCIL && !graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, usage_, D3DRTYPE_TEXTURE))
  441. {
  442. if (!device || FAILED(device->CreateDepthStencilSurface(
  443. width_,
  444. height_,
  445. (D3DFORMAT)format_,
  446. D3DMULTISAMPLE_NONE,
  447. 0,
  448. FALSE,
  449. (IDirect3DSurface9**)&renderSurface_->surface_,
  450. 0)))
  451. {
  452. LOGERROR("Could not create depth-stencil surface");
  453. return false;
  454. }
  455. levels_ = 1;
  456. }
  457. else
  458. {
  459. if (!device || FAILED(graphics_->GetImpl()->GetDevice()->CreateTexture(
  460. width_,
  461. height_,
  462. requestedLevels_,
  463. usage_,
  464. (D3DFORMAT)format_,
  465. (D3DPOOL)pool_,
  466. (IDirect3DTexture9**)&object_,
  467. 0)))
  468. {
  469. LOGERROR("Could not create texture");
  470. return false;
  471. }
  472. levels_ = ((IDirect3DTexture9*)object_)->GetLevelCount();
  473. if (usage_ & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
  474. ((IDirect3DTexture9*)object_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_);
  475. }
  476. return true;
  477. }
  478. }