D3D9Texture2D.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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. OBJECTTYPESTATIC(Texture2D);
  34. Texture2D::Texture2D(Context* context) :
  35. Texture(context)
  36. {
  37. }
  38. Texture2D::~Texture2D()
  39. {
  40. Release();
  41. }
  42. void Texture2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<Texture2D>();
  45. }
  46. bool Texture2D::Load(Deserializer& source)
  47. {
  48. PROFILE(LoadTexture2D);
  49. // In headless mode, do not actually load the texture, just return success
  50. Graphics* graphics = GetSubsystem<Graphics>();
  51. if (!graphics)
  52. return true;
  53. // If device is lost, retry later
  54. if (graphics_->IsDeviceLost())
  55. {
  56. LOGWARNING("Texture load while device is lost");
  57. dataPending_ = true;
  58. return true;
  59. }
  60. // If over the texture budget, see if materials can be freed to allow textures to be freed
  61. CheckTextureBudget(GetTypeStatic());
  62. SharedPtr<Image> image(new Image(context_));
  63. if (!image->Load(source))
  64. return false;
  65. // Before actually loading the texture, get optional parameters from an XML description file
  66. LoadParameters();
  67. return Load(image);
  68. }
  69. void Texture2D::OnDeviceLost()
  70. {
  71. if (pool_ == D3DPOOL_DEFAULT)
  72. Release();
  73. }
  74. void Texture2D::OnDeviceReset()
  75. {
  76. if (pool_ == D3DPOOL_DEFAULT || !object_)
  77. {
  78. // If has a file name, reload through the resource cache. Otherwise just recreate.
  79. if (!GetName().Trimmed().Empty())
  80. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  81. else
  82. {
  83. Create();
  84. dataLost_ = true;
  85. }
  86. }
  87. else if (dataPending_)
  88. {
  89. if (!GetName().Trimmed().Empty())
  90. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  91. else
  92. dataLost_ = true;
  93. }
  94. dataPending_ = false;
  95. }
  96. void Texture2D::Release()
  97. {
  98. if (object_)
  99. {
  100. if (!graphics_)
  101. return;
  102. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  103. {
  104. if (graphics_->GetTexture(i) == this)
  105. graphics_->SetTexture(i, 0);
  106. }
  107. if (renderSurface_)
  108. renderSurface_->Release();
  109. ((IDirect3DTexture9*)object_)->Release();
  110. object_ = 0;
  111. }
  112. else
  113. {
  114. if (renderSurface_)
  115. renderSurface_->Release();
  116. }
  117. }
  118. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage)
  119. {
  120. // Delete the old rendersurface if any
  121. renderSurface_.Reset();
  122. pool_ = D3DPOOL_MANAGED;
  123. usage_ = 0;
  124. if (usage == TEXTURE_RENDERTARGET || usage == TEXTURE_DEPTHSTENCIL)
  125. {
  126. renderSurface_ = new RenderSurface(this);
  127. if (usage == TEXTURE_RENDERTARGET)
  128. usage_ |= D3DUSAGE_RENDERTARGET;
  129. else
  130. usage_ |= D3DUSAGE_DEPTHSTENCIL;
  131. pool_ = D3DPOOL_DEFAULT;
  132. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  133. addressMode_[COORD_U] = ADDRESS_CLAMP;
  134. addressMode_[COORD_V] = ADDRESS_CLAMP;
  135. filterMode_ = FILTER_NEAREST;
  136. requestedLevels_ = 1;
  137. }
  138. else if (usage == TEXTURE_DYNAMIC)
  139. {
  140. usage_ |= D3DUSAGE_DYNAMIC;
  141. pool_ = D3DPOOL_DEFAULT;
  142. }
  143. width_ = width;
  144. height_ = height;
  145. format_ = format;
  146. return Create();
  147. }
  148. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  149. {
  150. if (!object_)
  151. {
  152. LOGERROR("No texture created, can not set data");
  153. return false;
  154. }
  155. if (!data)
  156. {
  157. LOGERROR("Null source for setting data");
  158. return false;
  159. }
  160. if (level >= levels_)
  161. {
  162. LOGERROR("Illegal mip level for setting data");
  163. return false;
  164. }
  165. if (graphics_->IsDeviceLost())
  166. {
  167. LOGWARNING("Texture data assignment while device is lost");
  168. dataPending_ = true;
  169. return true;
  170. }
  171. if (IsCompressed())
  172. {
  173. x &= ~3;
  174. y &= ~3;
  175. }
  176. int levelWidth = GetLevelWidth(level);
  177. int levelHeight = GetLevelHeight(level);
  178. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  179. {
  180. LOGERROR("Illegal dimensions for setting data");
  181. return false;
  182. }
  183. D3DLOCKED_RECT d3dLockedRect;
  184. RECT d3dRect;
  185. d3dRect.left = x;
  186. d3dRect.top = y;
  187. d3dRect.right = x + width;
  188. d3dRect.bottom = y + height;
  189. DWORD flags = 0;
  190. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  191. flags |= D3DLOCK_DISCARD;
  192. if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  193. {
  194. LOGERROR("Could not lock texture");
  195. return false;
  196. }
  197. if (IsCompressed())
  198. {
  199. height = (height + 3) >> 2;
  200. y >>= 2;
  201. }
  202. unsigned char* src = (unsigned char*)data;
  203. unsigned rowSize = GetRowDataSize(width);
  204. unsigned rowOffset = GetRowDataSize(x);
  205. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  206. if (format_ == D3DFMT_X8R8G8B8)
  207. {
  208. rowSize = rowSize / 3 * 4;
  209. rowOffset = rowOffset / 3 * 4;
  210. }
  211. // Perform conversion from RGB / RGBA as necessary
  212. switch (format_)
  213. {
  214. default:
  215. for (int i = 0; i < height; ++i)
  216. {
  217. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  218. memcpy(dest, src, rowSize);
  219. src += rowSize;
  220. }
  221. break;
  222. case D3DFMT_X8R8G8B8:
  223. for (int i = 0; i < height; ++i)
  224. {
  225. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  226. for (int j = 0; j < levelWidth; ++j)
  227. {
  228. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  229. src += 3;
  230. }
  231. }
  232. break;
  233. case D3DFMT_A8R8G8B8:
  234. for (int i = 0; i < height; ++i)
  235. {
  236. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  237. for (int j = 0; j < levelWidth; ++j)
  238. {
  239. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  240. src += 4;
  241. }
  242. }
  243. break;
  244. }
  245. ((IDirect3DTexture9*)object_)->UnlockRect(level);
  246. return true;
  247. }
  248. bool Texture2D::Load(SharedPtr<Image> image, bool useAlpha)
  249. {
  250. if (!image)
  251. {
  252. LOGERROR("Null image, can not load texture");
  253. return false;
  254. }
  255. unsigned memoryUse = sizeof(Texture2D);
  256. int quality = QUALITY_HIGH;
  257. Renderer* renderer = GetSubsystem<Renderer>();
  258. if (renderer)
  259. quality = renderer->GetTextureQuality();
  260. if (!image->IsCompressed())
  261. {
  262. unsigned char* levelData = image->GetData();
  263. int levelWidth = image->GetWidth();
  264. int levelHeight = image->GetHeight();
  265. unsigned components = image->GetComponents();
  266. unsigned format = 0;
  267. // Discard unnecessary mip levels
  268. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  269. {
  270. image = image->GetNextLevel();
  271. levelData = image->GetData();
  272. levelWidth = image->GetWidth();
  273. levelHeight = image->GetHeight();
  274. }
  275. switch (components)
  276. {
  277. case 1:
  278. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  279. break;
  280. case 2:
  281. format = Graphics::GetLuminanceAlphaFormat();
  282. break;
  283. case 3:
  284. format = Graphics::GetRGBFormat();
  285. break;
  286. case 4:
  287. format = Graphics::GetRGBAFormat();
  288. break;
  289. }
  290. SetSize(levelWidth, levelHeight, format);
  291. for (unsigned i = 0; i < levels_; ++i)
  292. {
  293. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  294. memoryUse += levelWidth * levelHeight * components;
  295. if (i < levels_ - 1)
  296. {
  297. image = image->GetNextLevel();
  298. levelData = image->GetData();
  299. levelWidth = image->GetWidth();
  300. levelHeight = image->GetHeight();
  301. }
  302. }
  303. }
  304. else
  305. {
  306. int width = image->GetWidth();
  307. int height = image->GetHeight();
  308. unsigned levels = image->GetNumCompressedLevels();
  309. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  310. unsigned mipsToSkip = mipsToSkip_[quality];
  311. if (mipsToSkip >= levels)
  312. mipsToSkip = levels - 1;
  313. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  314. --mipsToSkip;
  315. width /= (1 << mipsToSkip);
  316. height /= (1 << mipsToSkip);
  317. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  318. SetSize(width, height, format);
  319. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  320. {
  321. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  322. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  323. memoryUse += level.rows_ * level.rowSize_;
  324. }
  325. }
  326. SetMemoryUse(memoryUse);
  327. return true;
  328. }
  329. bool Texture2D::GetData(unsigned level, void* dest) const
  330. {
  331. if (!object_)
  332. {
  333. LOGERROR("No texture created, can not get data");
  334. return false;
  335. }
  336. if (!dest)
  337. {
  338. LOGERROR("Null destination for getting data");
  339. return false;
  340. }
  341. if (level >= levels_)
  342. {
  343. LOGERROR("Illegal mip level for getting data");
  344. return false;
  345. }
  346. if (graphics_->IsDeviceLost())
  347. {
  348. LOGWARNING("Getting texture data while device is lost");
  349. return false;
  350. }
  351. int levelWidth = GetLevelWidth(level);
  352. int levelHeight = GetLevelHeight(level);
  353. D3DLOCKED_RECT d3dLockedRect;
  354. RECT d3dRect;
  355. d3dRect.left = 0;
  356. d3dRect.top = 0;
  357. d3dRect.right = levelWidth;
  358. d3dRect.bottom = levelHeight;
  359. if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  360. {
  361. LOGERROR("Could not lock texture");
  362. return false;
  363. }
  364. int height = levelHeight;
  365. if (IsCompressed())
  366. height = (height + 3) >> 2;
  367. unsigned char* destPtr = (unsigned char*)dest;
  368. unsigned rowSize = GetRowDataSize(levelWidth);
  369. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  370. if (format_ == D3DFMT_X8R8G8B8)
  371. rowSize = rowSize / 3 * 4;
  372. // Perform conversion to RGB / RGBA as necessary
  373. switch (format_)
  374. {
  375. default:
  376. for (int i = 0; i < height; ++i)
  377. {
  378. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  379. memcpy(destPtr, src, rowSize);
  380. destPtr += rowSize;
  381. }
  382. break;
  383. case D3DFMT_X8R8G8B8:
  384. for (int i = 0; i < height; ++i)
  385. {
  386. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  387. for (int j = 0; j < levelWidth; ++j)
  388. {
  389. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  390. destPtr += 3;
  391. }
  392. }
  393. break;
  394. case D3DFMT_A8R8G8B8:
  395. for (int i = 0; i < height; ++i)
  396. {
  397. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  398. for (int j = 0; j < levelWidth; ++j)
  399. {
  400. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  401. destPtr += 4;
  402. }
  403. }
  404. break;
  405. }
  406. ((IDirect3DTexture9*)object_)->UnlockRect(level);
  407. return true;
  408. }
  409. bool Texture2D::Create()
  410. {
  411. Release();
  412. if (!graphics_ || !width_ || !height_)
  413. return false;
  414. if (graphics_->IsDeviceLost())
  415. {
  416. LOGWARNING("Texture creation while device is lost");
  417. return true;
  418. }
  419. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  420. // If creating a depth-stencil texture, and it is not supported, create a depth-stencil surface instead
  421. if (usage_ & D3DUSAGE_DEPTHSTENCIL && !graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, usage_, D3DRTYPE_TEXTURE))
  422. {
  423. if (!device || FAILED(device->CreateDepthStencilSurface(
  424. width_,
  425. height_,
  426. (D3DFORMAT)format_,
  427. D3DMULTISAMPLE_NONE,
  428. 0,
  429. FALSE,
  430. (IDirect3DSurface9**)&renderSurface_->surface_,
  431. 0)))
  432. {
  433. LOGERROR("Could not create depth-stencil surface");
  434. return false;
  435. }
  436. levels_ = 1;
  437. }
  438. else
  439. {
  440. if (!device || FAILED(graphics_->GetImpl()->GetDevice()->CreateTexture(
  441. width_,
  442. height_,
  443. requestedLevels_,
  444. usage_,
  445. (D3DFORMAT)format_,
  446. (D3DPOOL)pool_,
  447. (IDirect3DTexture9**)&object_,
  448. 0)))
  449. {
  450. LOGERROR("Could not create texture");
  451. return false;
  452. }
  453. levels_ = ((IDirect3DTexture9*)object_)->GetLevelCount();
  454. if (usage_ & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
  455. ((IDirect3DTexture9*)object_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_);
  456. }
  457. return true;
  458. }