BsTexture.cpp 20 KB

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