BsTexture.cpp 11 KB

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