BsTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "BsPixelBuffer.h"
  2. #include "BsTexture.h"
  3. #include "BsTextureRTTI.h"
  4. #include "BsDataStream.h"
  5. #include "BsException.h"
  6. #include "BsDebug.h"
  7. #include "BsCoreThread.h"
  8. #include "BsAsyncOp.h"
  9. #include "BsResources.h"
  10. namespace BansheeEngine
  11. {
  12. Texture::Texture()
  13. :mHeight(32), mWidth(32), mDepth(1), mNumMipmaps(0),
  14. mHwGamma(false), mMultisampleCount(0), mTextureType(TEX_TYPE_2D),
  15. mFormat(PF_UNKNOWN), mUsage(TU_DEFAULT)
  16. {
  17. }
  18. void Texture::initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  19. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount, const String& multisampleHint)
  20. {
  21. mTextureType = textureType;
  22. mWidth = width;
  23. mHeight = height;
  24. mDepth = depth;
  25. mNumMipmaps = numMipmaps;
  26. mUsage = usage;
  27. mHwGamma = hwGamma;
  28. mMultisampleCount = multisampleCount;
  29. mMultisampleHint = multisampleHint;
  30. // Adjust format if required
  31. mFormat = TextureManager::instance().getNativeFormat(mTextureType, format, mUsage, hwGamma);
  32. mSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  33. Resource::initialize();
  34. }
  35. bool Texture::hasAlpha() const
  36. {
  37. return PixelUtil::hasAlpha(mFormat);
  38. }
  39. UINT32 Texture::calculateSize() const
  40. {
  41. return getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  42. }
  43. UINT32 Texture::getNumFaces() const
  44. {
  45. return getTextureType() == TEX_TYPE_CUBE_MAP ? 6 : 1;
  46. }
  47. void Texture::writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer)
  48. {
  49. THROW_IF_NOT_CORE_THREAD;
  50. if(data.getTypeId() != TID_PixelData)
  51. BS_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only PixelData is supported.");
  52. if(discardEntireBuffer)
  53. {
  54. if(mUsage != TU_DYNAMIC)
  55. {
  56. LOGWRN("Buffer discard is enabled but buffer was not created as dynamic. Disabling discard.");
  57. discardEntireBuffer = false;
  58. }
  59. }
  60. else
  61. {
  62. if(mUsage == TU_DYNAMIC)
  63. {
  64. LOGWRN("Buffer discard is not enabled but buffer was not created as dynamic. Enabling discard.");
  65. discardEntireBuffer = true;
  66. }
  67. }
  68. const PixelData& pixelData = static_cast<const PixelData&>(data);
  69. UINT32 face = 0;
  70. UINT32 mip = 0;
  71. mapFromSubresourceIdx(subresourceIdx, face, mip);
  72. writeData(pixelData, mip, face, discardEntireBuffer);
  73. }
  74. void Texture::readSubresource(UINT32 subresourceIdx, GpuResourceData& data)
  75. {
  76. THROW_IF_NOT_CORE_THREAD;
  77. if(data.getTypeId() != TID_PixelData)
  78. BS_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only PixelData is supported.");
  79. PixelData& pixelData = static_cast<PixelData&>(data);
  80. if(pixelData.getWidth() != getWidth() ||pixelData.getHeight() != getHeight() ||
  81. pixelData.getDepth() != getDepth() || pixelData.getFormat() != getFormat())
  82. {
  83. BS_EXCEPT(RenderingAPIException, "Provided buffer is not of valid dimensions or format in order to read from this texture.");
  84. }
  85. UINT32 face = 0;
  86. UINT32 mip = 0;
  87. mapFromSubresourceIdx(subresourceIdx, face, mip);
  88. readData(pixelData, mip, face);
  89. }
  90. PixelDataPtr Texture::allocateSubresourceBuffer(UINT32 subresourceIdx) const
  91. {
  92. UINT32 face = 0;
  93. UINT32 mip = 0;
  94. mapFromSubresourceIdx(subresourceIdx, face, mip);
  95. UINT32 numMips = getNumMipmaps();
  96. UINT32 width = getWidth();
  97. UINT32 height = getHeight();
  98. UINT32 depth = getDepth();
  99. UINT32 totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  100. for(UINT32 j = 0; j < mip; j++)
  101. {
  102. totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  103. if(width != 1) width /= 2;
  104. if(height != 1) height /= 2;
  105. if(depth != 1) depth /= 2;
  106. }
  107. PixelDataPtr dst = bs_shared_ptr<PixelData, PoolAlloc>(width, height, depth, getFormat());
  108. dst->allocateInternalBuffer();
  109. return dst;
  110. }
  111. void Texture::mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const
  112. {
  113. UINT32 numMipmaps = getNumMipmaps() + 1;
  114. face = Math::floorToInt((subresourceIdx) / (float)numMipmaps);
  115. mip = subresourceIdx % numMipmaps;
  116. }
  117. UINT32 Texture::mapToSubresourceIdx(UINT32 face, UINT32 mip) const
  118. {
  119. return face * (getNumMipmaps() + 1) + mip;
  120. }
  121. PixelData Texture::lock(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  122. {
  123. THROW_IF_NOT_CORE_THREAD;
  124. if(mipLevel < 0 || mipLevel > mNumMipmaps)
  125. BS_EXCEPT(InvalidParametersException, "Invalid mip level: " + toString(mipLevel) + ". Min is 0, max is " + toString(getNumMipmaps()));
  126. if(face < 0 || face >= getNumFaces())
  127. BS_EXCEPT(InvalidParametersException, "Invalid face index: " + toString(face) + ". Min is 0, max is " + toString(getNumFaces()));
  128. return lockImpl(options, mipLevel, face);
  129. }
  130. void Texture::unlock()
  131. {
  132. THROW_IF_NOT_CORE_THREAD;
  133. unlockImpl();
  134. }
  135. void Texture::copy(UINT32 srcSubresourceIdx, UINT32 destSubresourceIdx, TexturePtr& target)
  136. {
  137. THROW_IF_NOT_CORE_THREAD;
  138. if (target->getTextureType() != this->getTextureType())
  139. BS_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
  140. if (getFormat() != target->getFormat()) // Note: It might be okay to use different formats of the same size
  141. BS_EXCEPT(InvalidParametersException, "Source and destination texture formats must match.");
  142. if (target->getMultisampleCount() > 0 && getMultisampleCount() != target->getMultisampleCount())
  143. BS_EXCEPT(InvalidParametersException, "When copying to a multisampled texture, source texture must have the same number of samples.");
  144. UINT32 srcFace = 0;
  145. UINT32 srcMipLevel = 0;
  146. UINT32 destFace = 0;
  147. UINT32 destMipLevel = 0;
  148. mapFromSubresourceIdx(srcSubresourceIdx, srcFace, srcMipLevel);
  149. target->mapFromSubresourceIdx(destSubresourceIdx, destFace, destMipLevel);
  150. if (destFace >= getNumFaces())
  151. BS_EXCEPT(InvalidParametersException, "Invalid destination face index");
  152. if (srcFace >= target->getNumFaces())
  153. BS_EXCEPT(InvalidParametersException, "Invalid destination face index");
  154. if (srcMipLevel > getNumMipmaps())
  155. BS_EXCEPT(InvalidParametersException, "Source mip level out of range. Valid range is [0, " + toString(getNumMipmaps()) + "]");
  156. if (destMipLevel > target->getNumMipmaps())
  157. BS_EXCEPT(InvalidParametersException, "Destination mip level out of range. Valid range is [0, " + toString(target->getNumMipmaps()) + "]");
  158. UINT32 srcMipWidth = mWidth >> srcMipLevel;
  159. UINT32 srcMipHeight = mHeight >> srcMipLevel;
  160. UINT32 srcMipDepth = mDepth >> srcMipLevel;
  161. UINT32 dstMipWidth = target->getWidth() >> destMipLevel;
  162. UINT32 dstMipHeight = target->getHeight() >> destMipLevel;
  163. UINT32 dstMipDepth = target->getDepth() >> destMipLevel;
  164. if (srcMipWidth != dstMipWidth || srcMipHeight != dstMipHeight || srcMipDepth != dstMipDepth)
  165. BS_EXCEPT(InvalidParametersException, "Source and destination sizes must match");
  166. copyImpl(srcFace, srcMipLevel, destFace, destMipLevel, target);
  167. }
  168. /************************************************************************/
  169. /* TEXTURE VIEW */
  170. /************************************************************************/
  171. TextureViewPtr Texture::createView()
  172. {
  173. TextureViewPtr viewPtr = bs_core_ptr<TextureView, PoolAlloc>(new (bs_alloc<TextureView, PoolAlloc>()) TextureView());
  174. viewPtr->_setThisPtr(viewPtr);
  175. return viewPtr;
  176. }
  177. void Texture::clearBufferViews()
  178. {
  179. mTextureViews.clear();
  180. }
  181. TextureViewPtr Texture::requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage)
  182. {
  183. THROW_IF_NOT_CORE_THREAD;
  184. assert(texture != nullptr);
  185. TEXTURE_VIEW_DESC key;
  186. key.mostDetailMip = mostDetailMip;
  187. key.numMips = numMips;
  188. key.firstArraySlice = firstArraySlice;
  189. key.numArraySlices = numArraySlices;
  190. key.usage = usage;
  191. auto iterFind = texture->mTextureViews.find(key);
  192. if(iterFind == texture->mTextureViews.end())
  193. {
  194. TextureViewPtr newView = texture->createView();
  195. newView->initialize(texture, key);
  196. texture->mTextureViews[key] = new (bs_alloc<TextureViewReference, PoolAlloc>()) TextureViewReference(newView);
  197. iterFind = texture->mTextureViews.find(key);
  198. }
  199. iterFind->second->refCount++;
  200. return iterFind->second->view;
  201. }
  202. void Texture::releaseView(TextureViewPtr view)
  203. {
  204. THROW_IF_NOT_CORE_THREAD;
  205. assert(view != nullptr);
  206. TexturePtr 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<PoolAlloc>(toRemove);
  218. }
  219. }
  220. /************************************************************************/
  221. /* SERIALIZATION */
  222. /************************************************************************/
  223. RTTITypeBase* Texture::getRTTIStatic()
  224. {
  225. return TextureRTTI::instance();
  226. }
  227. RTTITypeBase* Texture::getRTTI() const
  228. {
  229. return Texture::getRTTIStatic();
  230. }
  231. /************************************************************************/
  232. /* STATICS */
  233. /************************************************************************/
  234. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  235. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  236. {
  237. TexturePtr texturePtr = _createPtr(texType,
  238. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  239. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  240. }
  241. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height,
  242. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  243. {
  244. TexturePtr texturePtr = _createPtr(texType,
  245. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  246. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  247. }
  248. TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  249. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  250. {
  251. return TextureManager::instance().createTexture(texType,
  252. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  253. }
  254. TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height,
  255. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  256. {
  257. return TextureManager::instance().createTexture(texType,
  258. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  259. }
  260. const HTexture& Texture::dummy()
  261. {
  262. return TextureManager::instance().getDummyTexture();
  263. }
  264. }