D3D9Texture2D.cpp 15 KB

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