D3D9Texture2D.cpp 15 KB

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