D3D9Texture2D.cpp 15 KB

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