Texture2D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. D3DFORMAT format = D3DFMT_UNKNOWN;
  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 = D3DFMT_L8;
  165. break;
  166. case 2:
  167. format = D3DFMT_A8L8;
  168. break;
  169. case 3:
  170. format = D3DFMT_X8R8G8B8;
  171. break;
  172. case 4:
  173. format = D3DFMT_A8R8G8B8;
  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. D3DLOCKED_RECT 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 = (unsigned char*)hwRect.pBits + 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. D3DFORMAT format = (D3DFORMAT)GetCompressedD3DFormat(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. D3DLOCKED_RECT hwRect;
  241. if (!Lock(i, 0, &hwRect))
  242. return false;
  243. for (unsigned j = 0; j < level.rows_; ++j)
  244. {
  245. unsigned char* dest = (unsigned char*)hwRect.pBits + 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, void* rect, void* hwRect)
  256. {
  257. D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
  258. if (!lockedRect)
  259. {
  260. LOGERROR("Null locked rect structure");
  261. return false;
  262. }
  263. if (!object_)
  264. {
  265. LOGERROR("No texture created, can not lock");
  266. return false;
  267. }
  268. if (lockedLevel_ != -1)
  269. {
  270. LOGERROR("Texture already locked");
  271. return false;
  272. }
  273. DWORD flags = 0;
  274. if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
  275. flags |= D3DLOCK_DISCARD;
  276. if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, lockedRect, (RECT*)rect, flags)))
  277. {
  278. LOGERROR("Could not lock texture");
  279. return false;
  280. }
  281. lockedLevel_ = level;
  282. return true;
  283. }
  284. void Texture2D::Unlock()
  285. {
  286. if (lockedLevel_ != -1)
  287. {
  288. ((IDirect3DTexture9*)object_)->UnlockRect(lockedLevel_);
  289. lockedLevel_ = -1;
  290. }
  291. }
  292. bool Texture2D::Create()
  293. {
  294. Release();
  295. if (!graphics_)
  296. return false;
  297. if (followWindowSize_)
  298. {
  299. width_ = graphics_->GetWidth();
  300. height_ = graphics_->GetHeight();
  301. }
  302. if ((!width_) || (!height_))
  303. return false;
  304. // If using a default pool texture, must generate mipmaps automatically
  305. if ((pool_ == D3DPOOL_DEFAULT) && (requestedLevels_ != 1))
  306. usage_ |= D3DUSAGE_AUTOGENMIPMAP;
  307. else
  308. usage_ &= ~D3DUSAGE_AUTOGENMIPMAP;
  309. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  310. // If creating a depth stencil texture, and it is not supported, create a depth stencil surface instead
  311. if ((usage_ & D3DUSAGE_DEPTHSTENCIL) && (!graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_, usage_, D3DRTYPE_TEXTURE)))
  312. {
  313. if ((!device) || (FAILED(device->CreateDepthStencilSurface(
  314. width_,
  315. height_,
  316. (D3DFORMAT)format_,
  317. D3DMULTISAMPLE_NONE,
  318. 0,
  319. FALSE,
  320. (IDirect3DSurface9**)&renderSurface_->surface_,
  321. 0))))
  322. {
  323. LOGERROR("Could not create depth stencil surface");
  324. return false;
  325. }
  326. levels_ = 1;
  327. }
  328. else
  329. {
  330. if ((!device) || (FAILED(graphics_->GetImpl()->GetDevice()->CreateTexture(
  331. width_,
  332. height_,
  333. requestedLevels_,
  334. usage_,
  335. (D3DFORMAT)format_,
  336. (D3DPOOL)pool_,
  337. (IDirect3DTexture9**)&object_,
  338. 0))))
  339. {
  340. LOGERROR("Could not create texture");
  341. return false;
  342. }
  343. levels_ = ((IDirect3DTexture9*)object_)->GetLevelCount();
  344. if (usage_ & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL))
  345. ((IDirect3DTexture9*)object_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_);
  346. }
  347. return true;
  348. }