BsTexture.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPixelBuffer.h"
  4. #include "BsTexture.h"
  5. #include "BsTextureRTTI.h"
  6. #include "BsDataStream.h"
  7. #include "BsException.h"
  8. #include "BsDebug.h"
  9. #include "BsCoreThread.h"
  10. #include "BsAsyncOp.h"
  11. #include "BsResources.h"
  12. #include "BsPixelUtil.h"
  13. namespace BansheeEngine
  14. {
  15. TextureProperties::TextureProperties()
  16. :mHeight(32), mWidth(32), mDepth(1), mNumMipmaps(0),
  17. mHwGamma(false), mMultisampleCount(0), mTextureType(TEX_TYPE_2D),
  18. mFormat(PF_UNKNOWN), mUsage(TU_DEFAULT)
  19. {
  20. }
  21. TextureProperties::TextureProperties(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  22. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount)
  23. :mHeight(height), mWidth(width), mDepth(depth), mNumMipmaps(numMipmaps),
  24. mHwGamma(hwGamma), mMultisampleCount(multisampleCount), mTextureType(textureType),
  25. mFormat(format), mUsage(usage)
  26. {
  27. }
  28. bool TextureProperties::hasAlpha() const
  29. {
  30. return PixelUtil::hasAlpha(mFormat);
  31. }
  32. UINT32 TextureProperties::getNumFaces() const
  33. {
  34. return getTextureType() == TEX_TYPE_CUBE_MAP ? 6 : 1;
  35. }
  36. void TextureProperties::mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const
  37. {
  38. UINT32 numMipmaps = getNumMipmaps() + 1;
  39. face = Math::floorToInt((subresourceIdx) / (float)numMipmaps);
  40. mip = subresourceIdx % numMipmaps;
  41. }
  42. UINT32 TextureProperties::mapToSubresourceIdx(UINT32 face, UINT32 mip) const
  43. {
  44. return face * (getNumMipmaps() + 1) + mip;
  45. }
  46. SPtr<PixelData> TextureProperties::allocateSubresourceBuffer(UINT32 subresourceIdx) const
  47. {
  48. UINT32 face = 0;
  49. UINT32 mip = 0;
  50. mapFromSubresourceIdx(subresourceIdx, face, mip);
  51. UINT32 width = getWidth();
  52. UINT32 height = getHeight();
  53. UINT32 depth = getDepth();
  54. UINT32 totalSize = PixelUtil::getMemorySize(width, height, depth, getFormat());
  55. for (UINT32 j = 0; j < mip; j++)
  56. {
  57. totalSize = PixelUtil::getMemorySize(width, height, depth, getFormat());
  58. if (width != 1) width /= 2;
  59. if (height != 1) height /= 2;
  60. if (depth != 1) depth /= 2;
  61. }
  62. SPtr<PixelData> dst = bs_shared_ptr_new<PixelData>(width, height, depth, getFormat());
  63. dst->allocateInternalBuffer();
  64. return dst;
  65. }
  66. SPtr<TextureCore> TextureCore::WHITE;
  67. SPtr<TextureCore> TextureCore::BLACK;
  68. SPtr<TextureCore> TextureCore::NORMAL;
  69. TextureCore::TextureCore(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  70. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount, const SPtr<PixelData>& initData)
  71. :mProperties(textureType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount),
  72. mInitData(initData)
  73. { }
  74. void TextureCore::initialize()
  75. {
  76. if (mInitData != nullptr)
  77. {
  78. writeSubresource(0, *mInitData, true);
  79. mInitData->_unlock();
  80. mInitData = nullptr;
  81. }
  82. }
  83. void TextureCore::writeSubresource(UINT32 subresourceIdx, const PixelData& pixelData, bool discardEntireBuffer)
  84. {
  85. THROW_IF_NOT_CORE_THREAD;
  86. if(discardEntireBuffer)
  87. {
  88. if(mProperties.getUsage() != TU_DYNAMIC)
  89. {
  90. // Buffer discard is enabled but buffer was not created as dynamic. Disabling discard.
  91. discardEntireBuffer = false;
  92. }
  93. }
  94. else
  95. {
  96. if (mProperties.getUsage() == TU_DYNAMIC)
  97. {
  98. LOGWRN("Buffer discard is not enabled but buffer was not created as dynamic. Enabling discard.");
  99. discardEntireBuffer = true;
  100. }
  101. }
  102. UINT32 face = 0;
  103. UINT32 mip = 0;
  104. mProperties.mapFromSubresourceIdx(subresourceIdx, face, mip);
  105. writeData(pixelData, mip, face, discardEntireBuffer);
  106. }
  107. void TextureCore::readSubresource(UINT32 subresourceIdx, PixelData& data)
  108. {
  109. THROW_IF_NOT_CORE_THREAD;
  110. UINT32 face = 0;
  111. UINT32 mip = 0;
  112. mProperties.mapFromSubresourceIdx(subresourceIdx, face, mip);
  113. PixelData& pixelData = static_cast<PixelData&>(data);
  114. UINT32 mipWidth, mipHeight, mipDepth;
  115. PixelUtil::getSizeForMipLevel(mProperties.getWidth(), mProperties.getHeight(), mProperties.getDepth(),
  116. mip, mipWidth, mipHeight, mipDepth);
  117. if (pixelData.getWidth() != mipWidth || pixelData.getHeight() != mipHeight ||
  118. pixelData.getDepth() != mipDepth || pixelData.getFormat() != mProperties.getFormat())
  119. {
  120. BS_EXCEPT(RenderingAPIException, "Provided buffer is not of valid dimensions or format in order to read from this texture.");
  121. }
  122. readData(pixelData, mip, face);
  123. }
  124. PixelData TextureCore::lock(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  125. {
  126. THROW_IF_NOT_CORE_THREAD;
  127. if (mipLevel > mProperties.getNumMipmaps())
  128. BS_EXCEPT(InvalidParametersException, "Invalid mip level: " + toString(mipLevel) + ". Min is 0, max is " + toString(mProperties.getNumMipmaps()));
  129. if (face >= mProperties.getNumFaces())
  130. BS_EXCEPT(InvalidParametersException, "Invalid face index: " + toString(face) + ". Min is 0, max is " + toString(mProperties.getNumFaces()));
  131. return lockImpl(options, mipLevel, face);
  132. }
  133. void TextureCore::unlock()
  134. {
  135. THROW_IF_NOT_CORE_THREAD;
  136. unlockImpl();
  137. }
  138. void TextureCore::copy(UINT32 srcSubresourceIdx, UINT32 destSubresourceIdx, const SPtr<TextureCore>& target)
  139. {
  140. THROW_IF_NOT_CORE_THREAD;
  141. if (target->mProperties.getTextureType() != mProperties.getTextureType())
  142. BS_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
  143. if (mProperties.getFormat() != target->mProperties.getFormat()) // Note: It might be okay to use different formats of the same size
  144. BS_EXCEPT(InvalidParametersException, "Source and destination texture formats must match.");
  145. if (target->mProperties.getMultisampleCount() > 0 && mProperties.getMultisampleCount() != target->mProperties.getMultisampleCount())
  146. BS_EXCEPT(InvalidParametersException, "When copying to a multisampled texture, source texture must have the same number of samples.");
  147. UINT32 srcFace = 0;
  148. UINT32 srcMipLevel = 0;
  149. UINT32 destFace = 0;
  150. UINT32 destMipLevel = 0;
  151. mProperties.mapFromSubresourceIdx(srcSubresourceIdx, srcFace, srcMipLevel);
  152. target->mProperties.mapFromSubresourceIdx(destSubresourceIdx, destFace, destMipLevel);
  153. if (destFace >= mProperties.getNumFaces())
  154. BS_EXCEPT(InvalidParametersException, "Invalid destination face index");
  155. if (srcFace >= target->mProperties.getNumFaces())
  156. BS_EXCEPT(InvalidParametersException, "Invalid destination face index");
  157. if (srcMipLevel > mProperties.getNumMipmaps())
  158. BS_EXCEPT(InvalidParametersException, "Source mip level out of range. Valid range is [0, " + toString(mProperties.getNumMipmaps()) + "]");
  159. if (destMipLevel > target->mProperties.getNumMipmaps())
  160. BS_EXCEPT(InvalidParametersException, "Destination mip level out of range. Valid range is [0, " + toString(target->mProperties.getNumMipmaps()) + "]");
  161. UINT32 srcMipWidth = mProperties.getWidth() >> srcMipLevel;
  162. UINT32 srcMipHeight = mProperties.getHeight() >> srcMipLevel;
  163. UINT32 srcMipDepth = mProperties.getDepth() >> srcMipLevel;
  164. UINT32 dstMipWidth = target->mProperties.getWidth() >> destMipLevel;
  165. UINT32 dstMipHeight = target->mProperties.getHeight() >> destMipLevel;
  166. UINT32 dstMipDepth = target->mProperties.getDepth() >> destMipLevel;
  167. if (srcMipWidth != dstMipWidth || srcMipHeight != dstMipHeight || srcMipDepth != dstMipDepth)
  168. BS_EXCEPT(InvalidParametersException, "Source and destination sizes must match");
  169. copyImpl(srcFace, srcMipLevel, destFace, destMipLevel, target);
  170. }
  171. /************************************************************************/
  172. /* TEXTURE VIEW */
  173. /************************************************************************/
  174. SPtr<TextureView> TextureCore::createView(const SPtr<TextureCore>& texture, const TEXTURE_VIEW_DESC& desc)
  175. {
  176. return bs_shared_ptr<TextureView>(new (bs_alloc<TextureView>()) TextureView(texture, desc));
  177. }
  178. void TextureCore::clearBufferViews()
  179. {
  180. mTextureViews.clear();
  181. }
  182. SPtr<TextureView> TextureCore::requestView(const SPtr<TextureCore>& texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage)
  183. {
  184. THROW_IF_NOT_CORE_THREAD;
  185. assert(texture != nullptr);
  186. TEXTURE_VIEW_DESC key;
  187. key.mostDetailMip = mostDetailMip;
  188. key.numMips = numMips;
  189. key.firstArraySlice = firstArraySlice;
  190. key.numArraySlices = numArraySlices;
  191. key.usage = usage;
  192. auto iterFind = texture->mTextureViews.find(key);
  193. if (iterFind == texture->mTextureViews.end())
  194. {
  195. SPtr<TextureView> newView = texture->createView(texture, key);
  196. texture->mTextureViews[key] = new (bs_alloc<TextureViewReference>()) TextureViewReference(newView);
  197. iterFind = texture->mTextureViews.find(key);
  198. }
  199. iterFind->second->refCount++;
  200. return iterFind->second->view;
  201. }
  202. void TextureCore::releaseView(const SPtr<TextureView>& view)
  203. {
  204. THROW_IF_NOT_CORE_THREAD;
  205. assert(view != nullptr);
  206. SPtr<TextureCore> texture = view->getTexture();
  207. auto iterFind = texture->mTextureViews.find(view->getDesc());
  208. if (iterFind == texture->mTextureViews.end())
  209. {
  210. BS_EXCEPT(InternalErrorException, "Trying to release a texture view that doesn't exist!");
  211. }
  212. iterFind->second->refCount--;
  213. if (iterFind->second->refCount == 0)
  214. {
  215. TextureViewReference* toRemove = iterFind->second;
  216. texture->mTextureViews.erase(iterFind);
  217. bs_delete(toRemove);
  218. }
  219. }
  220. /************************************************************************/
  221. /* STATICS */
  222. /************************************************************************/
  223. SPtr<TextureCore> TextureCore::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  224. int numMips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  225. {
  226. return TextureCoreManager::instance().createTexture(texType,
  227. width, height, depth, numMips, format, usage, hwGammaCorrection, multisampleCount);
  228. }
  229. SPtr<TextureCore> TextureCore::create(TextureType texType, UINT32 width, UINT32 height,
  230. int numMips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  231. {
  232. return TextureCoreManager::instance().createTexture(texType,
  233. width, height, 1, numMips, format, usage, hwGammaCorrection, multisampleCount);
  234. }
  235. SPtr<TextureCore> TextureCore::create(const SPtr<PixelData>& pixelData, int usage, bool hwGammaCorrection)
  236. {
  237. return TextureCoreManager::instance().createTextureInternal(pixelData->getDepth() > 1 ? TEX_TYPE_3D : TEX_TYPE_2D,
  238. pixelData->getWidth(), pixelData->getHeight(),
  239. pixelData->getDepth(), 0, pixelData->getFormat(), usage, hwGammaCorrection, 0, pixelData);
  240. }
  241. Texture::Texture()
  242. {
  243. }
  244. Texture::Texture(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  245. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount)
  246. :mProperties(textureType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount)
  247. {
  248. }
  249. Texture::Texture(const SPtr<PixelData>& pixelData, int usage, bool hwGamma)
  250. : mProperties(pixelData->getDepth() > 1 ? TEX_TYPE_3D : TEX_TYPE_2D, pixelData->getWidth(), pixelData->getHeight(),
  251. pixelData->getDepth(), 0, pixelData->getFormat(), usage, hwGamma, 0), mInitData(pixelData)
  252. {
  253. if (mInitData != nullptr)
  254. mInitData->_lock();
  255. }
  256. void Texture::initialize()
  257. {
  258. mSize = calculateSize();
  259. // Allocate CPU buffers if needed
  260. if ((mProperties.getUsage() & TU_CPUCACHED) != 0)
  261. {
  262. createCPUBuffers();
  263. if (mInitData != nullptr)
  264. updateCPUBuffers(0, *mInitData);
  265. }
  266. Resource::initialize();
  267. }
  268. SPtr<CoreObjectCore> Texture::createCore() const
  269. {
  270. const TextureProperties& props = getProperties();
  271. SPtr<CoreObjectCore> coreObj = TextureCoreManager::instance().createTextureInternal(props.getTextureType(), props.getWidth(), props.getHeight(),
  272. props.getDepth(), props.getNumMipmaps(), props.getFormat(), props.getUsage(), props.isHardwareGammaEnabled(), props.getMultisampleCount(), mInitData);
  273. if ((mProperties.getUsage() & TU_CPUCACHED) == 0)
  274. mInitData = nullptr;
  275. return coreObj;
  276. }
  277. AsyncOp Texture::writeSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const SPtr<PixelData>& data, bool discardEntireBuffer)
  278. {
  279. updateCPUBuffers(subresourceIdx, *data);
  280. data->_lock();
  281. std::function<void(const SPtr<TextureCore>&, UINT32, const SPtr<PixelData>&, bool, AsyncOp&)> func =
  282. [&](const SPtr<TextureCore>& texture, UINT32 _subresourceIdx, const SPtr<PixelData>& _pixData, bool _discardEntireBuffer, AsyncOp& asyncOp)
  283. {
  284. texture->writeSubresource(_subresourceIdx, *_pixData, _discardEntireBuffer);
  285. _pixData->_unlock();
  286. asyncOp._completeOperation();
  287. };
  288. return accessor.queueReturnCommand(std::bind(func, getCore(), subresourceIdx,
  289. data, discardEntireBuffer, std::placeholders::_1));
  290. }
  291. AsyncOp Texture::readSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const SPtr<PixelData>& data)
  292. {
  293. data->_lock();
  294. std::function<void(const SPtr<TextureCore>&, UINT32, const SPtr<PixelData>&, AsyncOp&)> func =
  295. [&](const SPtr<TextureCore>& texture, UINT32 _subresourceIdx, const SPtr<PixelData>& _pixData, AsyncOp& asyncOp)
  296. {
  297. texture->readSubresource(_subresourceIdx, *_pixData);
  298. _pixData->_unlock();
  299. asyncOp._completeOperation();
  300. };
  301. return accessor.queueReturnCommand(std::bind(func, getCore(), subresourceIdx,
  302. data, std::placeholders::_1));
  303. }
  304. UINT32 Texture::calculateSize() const
  305. {
  306. return mProperties.getNumFaces() * PixelUtil::getMemorySize(mProperties.getWidth(),
  307. mProperties.getHeight(), mProperties.getDepth(), mProperties.getFormat());
  308. }
  309. void Texture::updateCPUBuffers(UINT32 subresourceIdx, const PixelData& pixelData)
  310. {
  311. if ((mProperties.getUsage() & TU_CPUCACHED) == 0)
  312. return;
  313. if (subresourceIdx >= (UINT32)mCPUSubresourceData.size())
  314. {
  315. LOGERR("Invalid subresource index: " + toString(subresourceIdx) + ". Supported range: 0 .. " + toString(mCPUSubresourceData.size()));
  316. return;
  317. }
  318. UINT32 mipLevel;
  319. UINT32 face;
  320. mProperties.mapFromSubresourceIdx(subresourceIdx, face, mipLevel);
  321. UINT32 mipWidth, mipHeight, mipDepth;
  322. PixelUtil::getSizeForMipLevel(mProperties.getWidth(), mProperties.getHeight(), mProperties.getDepth(),
  323. mipLevel, mipWidth, mipHeight, mipDepth);
  324. if (pixelData.getWidth() != mipWidth || pixelData.getHeight() != mipHeight ||
  325. pixelData.getDepth() != mipDepth || pixelData.getFormat() != mProperties.getFormat())
  326. {
  327. LOGERR("Provided buffer is not of valid dimensions or format in order to update this texture.");
  328. return;
  329. }
  330. if (mCPUSubresourceData[subresourceIdx]->getSize() != pixelData.getSize())
  331. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match.");
  332. UINT8* dest = mCPUSubresourceData[subresourceIdx]->getData();
  333. UINT8* src = pixelData.getData();
  334. memcpy(dest, src, pixelData.getSize());
  335. }
  336. void Texture::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  337. {
  338. if ((mProperties.getUsage() & TU_CPUCACHED) == 0)
  339. {
  340. LOGERR("Attempting to read CPU data from a texture that is created without CPU caching.");
  341. return;
  342. }
  343. UINT32 mipWidth, mipHeight, mipDepth;
  344. PixelUtil::getSizeForMipLevel(mProperties.getWidth(), mProperties.getHeight(), mProperties.getDepth(),
  345. mipLevel, mipWidth, mipHeight, mipDepth);
  346. if (dest.getWidth() != mipWidth || dest.getHeight() != mipHeight ||
  347. dest.getDepth() != mipDepth || dest.getFormat() != mProperties.getFormat())
  348. {
  349. LOGERR("Provided buffer is not of valid dimensions or format in order to read from this texture.");
  350. return;
  351. }
  352. UINT32 subresourceIdx = mProperties.mapToSubresourceIdx(face, mipLevel);
  353. if (subresourceIdx >= (UINT32)mCPUSubresourceData.size())
  354. {
  355. LOGERR("Invalid subresource index: " + toString(subresourceIdx) + ". Supported range: 0 .. " + toString(mCPUSubresourceData.size()));
  356. return;
  357. }
  358. if (mCPUSubresourceData[subresourceIdx]->getSize() != dest.getSize())
  359. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match.");
  360. UINT8* srcPtr = mCPUSubresourceData[subresourceIdx]->getData();
  361. UINT8* destPtr = dest.getData();
  362. memcpy(destPtr, srcPtr, dest.getSize());
  363. }
  364. void Texture::createCPUBuffers()
  365. {
  366. UINT32 numFaces = mProperties.getNumFaces();
  367. UINT32 numMips = mProperties.getNumMipmaps() + 1;
  368. UINT32 numSubresources = numFaces * numMips;
  369. mCPUSubresourceData.resize(numSubresources);
  370. for (UINT32 i = 0; i < numFaces; i++)
  371. {
  372. UINT32 curWidth = mProperties.getWidth();
  373. UINT32 curHeight = mProperties.getHeight();
  374. UINT32 curDepth = mProperties.getDepth();
  375. for (UINT32 j = 0; j < numMips; j++)
  376. {
  377. UINT32 subresourceIdx = mProperties.mapToSubresourceIdx(i, j);
  378. mCPUSubresourceData[subresourceIdx] = bs_shared_ptr_new<PixelData>(curWidth, curHeight, curDepth, mProperties.getFormat());
  379. mCPUSubresourceData[subresourceIdx]->allocateInternalBuffer();
  380. if (curWidth > 1)
  381. curWidth = curWidth / 2;
  382. if (curHeight > 1)
  383. curHeight = curHeight / 2;
  384. if (curDepth > 1)
  385. curDepth = curDepth / 2;
  386. }
  387. }
  388. }
  389. SPtr<TextureCore> Texture::getCore() const
  390. {
  391. return std::static_pointer_cast<TextureCore>(mCoreSpecific);
  392. }
  393. /************************************************************************/
  394. /* SERIALIZATION */
  395. /************************************************************************/
  396. RTTITypeBase* Texture::getRTTIStatic()
  397. {
  398. return TextureRTTI::instance();
  399. }
  400. RTTITypeBase* Texture::getRTTI() const
  401. {
  402. return Texture::getRTTIStatic();
  403. }
  404. /************************************************************************/
  405. /* STATICS */
  406. /************************************************************************/
  407. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  408. int numMips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  409. {
  410. SPtr<Texture> texturePtr = _createPtr(texType,
  411. width, height, depth, numMips, format, usage, hwGammaCorrection, multisampleCount);
  412. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  413. }
  414. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height,
  415. int numMips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  416. {
  417. SPtr<Texture> texturePtr = _createPtr(texType,
  418. width, height, numMips, format, usage, hwGammaCorrection, multisampleCount);
  419. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  420. }
  421. HTexture Texture::create(const SPtr<PixelData>& pixelData, int usage, bool hwGammaCorrection)
  422. {
  423. SPtr<Texture> texturePtr = _createPtr(pixelData, usage, hwGammaCorrection);
  424. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  425. }
  426. SPtr<Texture> Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  427. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  428. {
  429. return TextureManager::instance().createTexture(texType,
  430. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount);
  431. }
  432. SPtr<Texture> Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height,
  433. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  434. {
  435. return TextureManager::instance().createTexture(texType,
  436. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount);
  437. }
  438. SPtr<Texture> Texture::_createPtr(const SPtr<PixelData>& pixelData, int usage, bool hwGammaCorrection)
  439. {
  440. return TextureManager::instance().createTexture(pixelData, usage, hwGammaCorrection);
  441. }
  442. }