D3D9TextureCube.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "ResourceCache.h"
  32. #include "TextureCube.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. OBJECTTYPESTATIC(TextureCube);
  39. TextureCube::TextureCube(Context* context) :
  40. Texture(context),
  41. lockedLevel_(-1)
  42. {
  43. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  44. faceMemoryUse_[i] = 0;
  45. }
  46. TextureCube::~TextureCube()
  47. {
  48. Release();
  49. }
  50. void TextureCube::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<TextureCube>();
  53. }
  54. void TextureCube::OnDeviceLost()
  55. {
  56. if (pool_ == D3DPOOL_DEFAULT)
  57. Release();
  58. }
  59. void TextureCube::OnDeviceReset()
  60. {
  61. if (pool_ == D3DPOOL_DEFAULT)
  62. {
  63. Create();
  64. dataLost_ = true;
  65. }
  66. }
  67. void TextureCube::Release()
  68. {
  69. if (object_)
  70. {
  71. if (!graphics_)
  72. return;
  73. Unlock();
  74. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  75. {
  76. if (graphics_->GetTexture(i) == this)
  77. graphics_->SetTexture(i, 0);
  78. }
  79. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  80. {
  81. if (renderSurfaces_[i])
  82. renderSurfaces_[i]->Release();
  83. }
  84. ((IDirect3DCubeTexture9*)object_)->Release();
  85. object_ = 0;
  86. }
  87. }
  88. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  89. {
  90. if (size <= 0)
  91. {
  92. LOGERROR("Zero or negative cube texture size");
  93. return false;
  94. }
  95. if (usage == TEXTURE_DEPTHSTENCIL)
  96. {
  97. LOGERROR("Depth stencil usage not supported for cube maps");
  98. return false;
  99. }
  100. // Delete the old rendersurfaces if any
  101. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  102. {
  103. renderSurfaces_[i].Reset();
  104. faceMemoryUse_[i] = 0;
  105. }
  106. pool_ = D3DPOOL_MANAGED;
  107. usage_ = 0;
  108. if (usage == TEXTURE_RENDERTARGET)
  109. {
  110. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  111. renderSurfaces_[i] = new RenderSurface(this);
  112. usage_ |= D3DUSAGE_RENDERTARGET;
  113. pool_ = D3DPOOL_DEFAULT;
  114. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  115. addressMode_[COORD_U] = ADDRESS_CLAMP;
  116. addressMode_[COORD_V] = ADDRESS_CLAMP;
  117. addressMode_[COORD_W] = ADDRESS_CLAMP;
  118. filterMode_ = FILTER_NEAREST;
  119. requestedLevels_ = 1;
  120. }
  121. else if (usage == TEXTURE_DYNAMIC)
  122. {
  123. usage_ |= D3DUSAGE_DYNAMIC;
  124. pool_ = D3DPOOL_DEFAULT;
  125. }
  126. width_ = size;
  127. height_ = size;
  128. format_ = format;
  129. return Create();
  130. }
  131. bool TextureCube::Load(Deserializer& source)
  132. {
  133. PROFILE(LoadTextureCube);
  134. ResourceCache* cache = GetSubsystem<ResourceCache>();
  135. if (!graphics_)
  136. return false;
  137. // If over the texture budget, see if materials can be freed to allow textures to be freed
  138. CheckTextureBudget(GetTypeStatic());
  139. String texPath, texName, texExt;
  140. SplitPath(GetName(), texPath, texName, texExt);
  141. SharedPtr<XMLFile> xml(new XMLFile(context_));
  142. if (!xml->Load(source))
  143. return false;
  144. LoadParameters(xml);
  145. XMLElement textureElem = xml->GetRootElement();
  146. XMLElement faceElem = textureElem.GetChildElement("face");
  147. unsigned faces = 0;
  148. while ((faceElem) && (faces < MAX_CUBEMAP_FACES))
  149. {
  150. String name = faceElem.GetString("name");
  151. String faceTexPath, faceTexName, faceTexExt;
  152. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  153. // If path is empty, add the XML file path
  154. if (faceTexPath.Empty())
  155. name = texPath + name;
  156. SharedPtr<Image> image(cache->GetResource<Image>(name));
  157. Load((CubeMapFace)faces, image);
  158. faces++;
  159. faceElem = faceElem.GetNextElement("face");
  160. }
  161. return true;
  162. }
  163. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  164. {
  165. PROFILE(LoadTextureCube);
  166. SharedPtr<Image> image(new Image(context_));
  167. if (!image->Load(source))
  168. return false;
  169. return Load(face, image);
  170. }
  171. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
  172. {
  173. if (!image)
  174. {
  175. LOGERROR("Null image, can not load texture");
  176. return false;
  177. }
  178. unsigned memoryUse = 0;
  179. int quality = QUALITY_HIGH;
  180. Renderer* renderer = GetSubsystem<Renderer>();
  181. if (renderer)
  182. quality = renderer->GetTextureQuality();
  183. if (!image->IsCompressed())
  184. {
  185. unsigned char* levelData = image->GetData();
  186. int levelWidth = image->GetWidth();
  187. int levelHeight = image->GetHeight();
  188. unsigned components = image->GetComponents();
  189. unsigned format = 0;
  190. if (levelWidth != levelHeight)
  191. {
  192. LOGERROR("Cube texture width not equal to height");
  193. return false;
  194. }
  195. // Discard unnecessary mip levels
  196. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  197. {
  198. image = image->GetNextLevel();
  199. levelWidth = image->GetWidth();
  200. levelHeight = image->GetHeight();
  201. }
  202. switch (components)
  203. {
  204. case 1:
  205. format = Graphics::GetLuminanceFormat();
  206. break;
  207. case 2:
  208. format = Graphics::GetLuminanceAlphaFormat();
  209. break;
  210. case 3:
  211. format = Graphics::GetRGBFormat();
  212. break;
  213. case 4:
  214. format = Graphics::GetRGBAFormat();
  215. break;
  216. }
  217. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  218. if (!face)
  219. SetSize(levelWidth, format);
  220. else
  221. {
  222. if (!object_)
  223. {
  224. LOGERROR("Cube texture face 0 must be loaded first");
  225. return false;
  226. }
  227. if ((levelWidth != width_) || (format != format_))
  228. {
  229. LOGERROR("Cube texture face does not match size or format of face 0");
  230. return false;
  231. }
  232. }
  233. // Make sure manual mip creation is never attempted for D3DPOOL_DEFAULT resources
  234. unsigned manualLevels = (pool_ == D3DPOOL_DEFAULT) ? 1 : levels_;
  235. for (unsigned i = 0; i < manualLevels; ++i)
  236. {
  237. LockedRect hwRect;
  238. if (!Lock(face, i, 0, hwRect))
  239. return false;
  240. memoryUse += levelWidth * levelHeight * components;
  241. for (int y = 0; y < levelHeight; ++y)
  242. {
  243. unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * y;
  244. unsigned char* src = levelData + components * levelWidth * y;
  245. switch (components)
  246. {
  247. case 1:
  248. case 2:
  249. memcpy(dest, src, components * levelWidth);
  250. break;
  251. case 3:
  252. for (int x = 0; x < levelWidth; ++x)
  253. {
  254. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  255. src += 3;
  256. }
  257. break;
  258. case 4:
  259. for (int x = 0; x < levelWidth; ++x)
  260. {
  261. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  262. src += 4;
  263. }
  264. break;
  265. }
  266. }
  267. if (i < manualLevels - 1)
  268. {
  269. image = image->GetNextLevel();
  270. levelData = image->GetData();
  271. levelWidth = image->GetWidth();
  272. levelHeight = image->GetHeight();
  273. }
  274. Unlock();
  275. }
  276. }
  277. else
  278. {
  279. int width = image->GetWidth();
  280. int height = image->GetHeight();
  281. unsigned levels = image->GetNumCompressedLevels();
  282. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  283. if (width != height)
  284. {
  285. LOGERROR("Cube texture width not equal to height");
  286. return false;
  287. }
  288. unsigned mipsToSkip = mipsToSkip_[quality];
  289. if (mipsToSkip >= levels)
  290. mipsToSkip = levels - 1;
  291. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  292. --mipsToSkip;
  293. width /= (1 << mipsToSkip);
  294. height /= (1 << mipsToSkip);
  295. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  296. if (!face)
  297. {
  298. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  299. SetSize(width, format);
  300. }
  301. else
  302. {
  303. if (!object_)
  304. {
  305. LOGERROR("Cube texture face 0 must be loaded first");
  306. return false;
  307. }
  308. if ((width != width_) || (format != format_))
  309. {
  310. LOGERROR("Cube texture face does not match size or format of face 0");
  311. return false;
  312. }
  313. }
  314. for (unsigned i = 0; (i < levels_) && (i < levels - mipsToSkip); ++i)
  315. {
  316. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  317. memoryUse += level.rows_ * level.rowSize_;
  318. LockedRect hwRect;
  319. if (!Lock(face, i, 0, hwRect))
  320. return false;
  321. for (unsigned j = 0; j < level.rows_; ++j)
  322. {
  323. unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * j;
  324. unsigned char* src = level.data_ + level.rowSize_ * j;
  325. memcpy(dest, src, level.rowSize_);
  326. }
  327. Unlock();
  328. }
  329. }
  330. faceMemoryUse_[face] = memoryUse;
  331. unsigned totalMemoryUse = 0;
  332. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  333. totalMemoryUse += faceMemoryUse_[i];
  334. SetMemoryUse(totalMemoryUse);
  335. return true;
  336. }
  337. bool TextureCube::Lock(CubeMapFace face, unsigned level, const IntRect* rect, LockedRect& lockedRect)
  338. {
  339. if (!object_)
  340. {
  341. LOGERROR("No texture created, can not lock");
  342. return false;
  343. }
  344. if (lockedLevel_ != -1)
  345. {
  346. LOGERROR("Texture already locked");
  347. return false;
  348. }
  349. D3DLOCKED_RECT d3dLockedRect;
  350. RECT d3dRect;
  351. if (rect)
  352. {
  353. d3dRect.left = rect->left_;
  354. d3dRect.top = rect->top_;
  355. d3dRect.right = rect->right_;
  356. d3dRect.bottom = rect->bottom_;
  357. }
  358. DWORD flags = 0;
  359. if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
  360. flags |= D3DLOCK_DISCARD;
  361. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, rect ? &d3dRect : 0, flags)))
  362. {
  363. LOGERROR("Could not lock texture");
  364. return false;
  365. }
  366. lockedRect.bits_ = (unsigned char*)d3dLockedRect.pBits;
  367. lockedRect.pitch_ = d3dLockedRect.Pitch;
  368. lockedLevel_ = level;
  369. lockedFace_ = face;
  370. return true;
  371. }
  372. void TextureCube::Unlock()
  373. {
  374. if (lockedLevel_ != -1)
  375. {
  376. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)lockedFace_, lockedLevel_);
  377. lockedLevel_ = -1;
  378. }
  379. }
  380. bool TextureCube::Create()
  381. {
  382. Release();
  383. if (!graphics_)
  384. return false;
  385. if ((!width_) || (!height_))
  386. return false;
  387. // If using a default pool texture, must generate mipmaps automatically
  388. if ((pool_ == D3DPOOL_DEFAULT) && (requestedLevels_ != 1))
  389. usage_ |= D3DUSAGE_AUTOGENMIPMAP;
  390. else
  391. usage_ &= ~D3DUSAGE_AUTOGENMIPMAP;
  392. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  393. if ((!device) || (FAILED(device->CreateCubeTexture(
  394. width_,
  395. requestedLevels_,
  396. usage_,
  397. (D3DFORMAT)format_,
  398. (D3DPOOL)pool_,
  399. (IDirect3DCubeTexture9**)&object_,
  400. 0))))
  401. {
  402. LOGERROR("Could not create cube texture");
  403. return false;
  404. }
  405. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  406. if (usage_ & D3DUSAGE_RENDERTARGET)
  407. {
  408. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  409. {
  410. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  411. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  412. }
  413. }
  414. return true;
  415. }