D3D9Texture2D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. lockedLevel_(-1),
  37. followWindowSize_(false)
  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. if (!graphics_)
  52. return false;
  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) || (followWindowSize_))
  65. Release();
  66. }
  67. void Texture2D::OnDeviceReset()
  68. {
  69. if ((pool_ == D3DPOOL_DEFAULT) || (followWindowSize_))
  70. {
  71. Create();
  72. dataLost_ = true;
  73. }
  74. }
  75. void Texture2D::Release()
  76. {
  77. if (object_)
  78. {
  79. if (!graphics_)
  80. return;
  81. Unlock();
  82. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  83. {
  84. if (graphics_->GetTexture(i) == this)
  85. graphics_->SetTexture(i, 0);
  86. }
  87. if (renderSurface_)
  88. renderSurface_->Release();
  89. ((IDirect3DTexture9*)object_)->Release();
  90. object_ = 0;
  91. }
  92. else
  93. {
  94. if (renderSurface_)
  95. renderSurface_->Release();
  96. }
  97. }
  98. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage)
  99. {
  100. // Delete the old rendersurface if any
  101. renderSurface_.Reset();
  102. pool_ = D3DPOOL_MANAGED;
  103. usage_ = 0;
  104. if ((usage == TEXTURE_RENDERTARGET) || (usage == TEXTURE_DEPTHSTENCIL))
  105. {
  106. renderSurface_ = new RenderSurface(this);
  107. if (usage == TEXTURE_RENDERTARGET)
  108. usage_ |= D3DUSAGE_RENDERTARGET;
  109. else
  110. usage_ |= D3DUSAGE_DEPTHSTENCIL;
  111. pool_ = D3DPOOL_DEFAULT;
  112. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  113. addressMode_[COORD_U] = ADDRESS_CLAMP;
  114. addressMode_[COORD_V] = ADDRESS_CLAMP;
  115. filterMode_ = FILTER_NEAREST;
  116. requestedLevels_ = 1;
  117. }
  118. else if (usage == TEXTURE_DYNAMIC)
  119. {
  120. usage_ |= D3DUSAGE_DYNAMIC;
  121. pool_ = D3DPOOL_DEFAULT;
  122. }
  123. if ((width <= 0) || (height <= 0))
  124. followWindowSize_ = true;
  125. else
  126. {
  127. width_ = width;
  128. height_ = height;
  129. followWindowSize_ = false;
  130. }
  131. format_ = format;
  132. return Create();
  133. }
  134. bool Texture2D::Load(SharedPtr<Image> image)
  135. {
  136. if (!image)
  137. {
  138. LOGERROR("Null image, can not load texture");
  139. return false;
  140. }
  141. unsigned memoryUse = 0;
  142. int quality = QUALITY_HIGH;
  143. Renderer* renderer = GetSubsystem<Renderer>();
  144. if (renderer)
  145. quality = renderer->GetTextureQuality();
  146. if (!image->IsCompressed())
  147. {
  148. unsigned char* levelData = image->GetData();
  149. int levelWidth = image->GetWidth();
  150. int levelHeight = image->GetHeight();
  151. unsigned components = image->GetComponents();
  152. unsigned format = 0;
  153. // Discard unnecessary mip levels
  154. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  155. {
  156. image = image->GetNextLevel();
  157. levelData = image->GetData();
  158. levelWidth = image->GetWidth();
  159. levelHeight = image->GetHeight();
  160. }
  161. switch (components)
  162. {
  163. case 1:
  164. format = Graphics::GetLuminanceFormat();
  165. break;
  166. case 2:
  167. format = Graphics::GetLuminanceAlphaFormat();
  168. break;
  169. case 3:
  170. format = Graphics::GetRGBFormat();
  171. break;
  172. case 4:
  173. format = Graphics::GetRGBAFormat();
  174. break;
  175. }
  176. SetSize(levelWidth, levelHeight, format);
  177. // Make sure manual mip creation is never attempted for D3DPOOL_DEFAULT resources
  178. unsigned manualLevels = (pool_ == D3DPOOL_DEFAULT) ? 1 : levels_;
  179. for (unsigned i = 0; i < manualLevels; ++i)
  180. {
  181. LockedRect hwRect;
  182. if (!Lock(i, 0, hwRect))
  183. return false;
  184. memoryUse += levelWidth * levelHeight * components;
  185. for (int y = 0; y < levelHeight; ++y)
  186. {
  187. unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * y;
  188. unsigned char* src = levelData + components * levelWidth * y;
  189. switch (components)
  190. {
  191. case 1:
  192. case 2:
  193. memcpy(dest, src, components * levelWidth);
  194. break;
  195. case 3:
  196. for (int x = 0; x < levelWidth; ++x)
  197. {
  198. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  199. src += 3;
  200. }
  201. break;
  202. case 4:
  203. for (int x = 0; x < levelWidth; ++x)
  204. {
  205. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  206. src += 4;
  207. }
  208. break;
  209. }
  210. }
  211. if (i < manualLevels - 1)
  212. {
  213. image = image->GetNextLevel();
  214. levelData = image->GetData();
  215. levelWidth = image->GetWidth();
  216. levelHeight = image->GetHeight();
  217. }
  218. Unlock();
  219. }
  220. }
  221. else
  222. {
  223. int width = image->GetWidth();
  224. int height = image->GetHeight();
  225. unsigned levels = image->GetNumCompressedLevels();
  226. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  227. unsigned mipsToSkip = mipsToSkip_[quality];
  228. if (mipsToSkip >= levels)
  229. mipsToSkip = levels - 1;
  230. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  231. --mipsToSkip;
  232. width /= (1 << mipsToSkip);
  233. height /= (1 << mipsToSkip);
  234. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  235. SetSize(width, height, format);
  236. for (unsigned i = 0; (i < levels_) && (i < levels - mipsToSkip); ++i)
  237. {
  238. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  239. memoryUse += level.rows_ * level.rowSize_;
  240. LockedRect hwRect;
  241. if (!Lock(i, 0, hwRect))
  242. return false;
  243. for (unsigned j = 0; j < level.rows_; ++j)
  244. {
  245. unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * j;
  246. unsigned char* src = level.data_ + level.rowSize_ * j;
  247. memcpy(dest, src, level.rowSize_);
  248. }
  249. Unlock();
  250. }
  251. }
  252. SetMemoryUse(memoryUse);
  253. return true;
  254. }
  255. bool Texture2D::Lock(unsigned level, const IntRect* rect, LockedRect& lockedRect)
  256. {
  257. if (!object_)
  258. {
  259. LOGERROR("No texture created, can not lock");
  260. return false;
  261. }
  262. if (lockedLevel_ != -1)
  263. {
  264. LOGERROR("Texture already locked");
  265. return false;
  266. }
  267. D3DLOCKED_RECT d3dLockedRect;
  268. RECT d3dRect;
  269. if (rect)
  270. {
  271. d3dRect.left = rect->left_;
  272. d3dRect.top = rect->top_;
  273. d3dRect.right = rect->right_;
  274. d3dRect.bottom = rect->bottom_;
  275. }
  276. DWORD flags = 0;
  277. if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
  278. flags |= D3DLOCK_DISCARD;
  279. if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, &d3dLockedRect, rect ? &d3dRect : 0, flags)))
  280. {
  281. LOGERROR("Could not lock texture");
  282. return false;
  283. }
  284. lockedRect.bits_ = (unsigned char*)d3dLockedRect.pBits;
  285. lockedRect.pitch_ = d3dLockedRect.Pitch;
  286. lockedLevel_ = level;
  287. return true;
  288. }
  289. void Texture2D::Unlock()
  290. {
  291. if (lockedLevel_ != -1)
  292. {
  293. ((IDirect3DTexture9*)object_)->UnlockRect(lockedLevel_);
  294. lockedLevel_ = -1;
  295. }
  296. }
  297. bool Texture2D::Create()
  298. {
  299. Release();
  300. if (!graphics_)
  301. return false;
  302. if (followWindowSize_)
  303. {
  304. width_ = graphics_->GetWidth();
  305. height_ = graphics_->GetHeight();
  306. }
  307. if ((!width_) || (!height_))
  308. return false;
  309. // If using a default pool texture, must generate mipmaps automatically
  310. if ((pool_ == D3DPOOL_DEFAULT) && (requestedLevels_ != 1))
  311. usage_ |= D3DUSAGE_AUTOGENMIPMAP;
  312. else
  313. usage_ &= ~D3DUSAGE_AUTOGENMIPMAP;
  314. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  315. // If creating a depth stencil texture, and it is not supported, create a depth stencil surface instead
  316. if ((usage_ & D3DUSAGE_DEPTHSTENCIL) && (!graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, usage_, D3DRTYPE_TEXTURE)))
  317. {
  318. if ((!device) || (FAILED(device->CreateDepthStencilSurface(
  319. width_,
  320. height_,
  321. (D3DFORMAT)format_,
  322. D3DMULTISAMPLE_NONE,
  323. 0,
  324. FALSE,
  325. (IDirect3DSurface9**)&renderSurface_->surface_,
  326. 0))))
  327. {
  328. LOGERROR("Could not create depth stencil surface");
  329. return false;
  330. }
  331. levels_ = 1;
  332. }
  333. else
  334. {
  335. if ((!device) || (FAILED(graphics_->GetImpl()->GetDevice()->CreateTexture(
  336. width_,
  337. height_,
  338. requestedLevels_,
  339. usage_,
  340. (D3DFORMAT)format_,
  341. (D3DPOOL)pool_,
  342. (IDirect3DTexture9**)&object_,
  343. 0))))
  344. {
  345. LOGERROR("Could not create texture");
  346. return false;
  347. }
  348. levels_ = ((IDirect3DTexture9*)object_)->GetLevelCount();
  349. if (usage_ & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
  350. ((IDirect3DTexture9*)object_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_);
  351. }
  352. return true;
  353. }