BsTexture.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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(TexturePtr& target)
  136. {
  137. THROW_IF_NOT_CORE_THREAD;
  138. if (target->getUsage() != this->getUsage() ||
  139. target->getTextureType() != this->getTextureType())
  140. {
  141. BS_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
  142. }
  143. if(getWidth() != target->getWidth() || getHeight() != target->getHeight() || getDepth() != target->getDepth())
  144. {
  145. BS_EXCEPT(InvalidParametersException, "Texture sizes don't match." \
  146. " Width: " + toString(getWidth()) + "/" + toString(target->getWidth()) +
  147. " Height: " + toString(getHeight()) + "/" + toString(target->getHeight()) +
  148. " Depth: " + toString(getDepth()) + "/" + toString(target->getDepth()));
  149. }
  150. if(getNumFaces() != target->getNumFaces())
  151. {
  152. BS_EXCEPT(InvalidParametersException, "Number of texture faces doesn't match." \
  153. " Num faces: " + toString(getNumFaces()) + "/" + toString(target->getNumFaces()));
  154. }
  155. if(getNumMipmaps() != target->getNumMipmaps())
  156. {
  157. BS_EXCEPT(InvalidParametersException, "Number of mipmaps doesn't match." \
  158. " Num mipmaps: " + toString(getNumMipmaps()) + "/" + toString(target->getNumMipmaps()));
  159. }
  160. copyImpl(target);
  161. }
  162. /************************************************************************/
  163. /* TEXTURE VIEW */
  164. /************************************************************************/
  165. TextureViewPtr Texture::createView()
  166. {
  167. TextureViewPtr viewPtr = bs_core_ptr<TextureView, PoolAlloc>(new (bs_alloc<TextureView, PoolAlloc>()) TextureView());
  168. viewPtr->_setThisPtr(viewPtr);
  169. return viewPtr;
  170. }
  171. void Texture::clearBufferViews()
  172. {
  173. mTextureViews.clear();
  174. }
  175. TextureViewPtr Texture::requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage)
  176. {
  177. assert(texture != nullptr);
  178. TEXTURE_VIEW_DESC key;
  179. key.mostDetailMip = mostDetailMip;
  180. key.numMips = numMips;
  181. key.firstArraySlice = firstArraySlice;
  182. key.numArraySlices = numArraySlices;
  183. key.usage = usage;
  184. auto iterFind = texture->mTextureViews.find(key);
  185. if(iterFind == texture->mTextureViews.end())
  186. {
  187. TextureViewPtr newView = texture->createView();
  188. newView->initialize(texture, key);
  189. texture->mTextureViews[key] = new (bs_alloc<TextureViewReference, PoolAlloc>()) TextureViewReference(newView);
  190. iterFind = texture->mTextureViews.find(key);
  191. }
  192. iterFind->second->refCount++;
  193. return iterFind->second->view;
  194. }
  195. void Texture::releaseView(TextureViewPtr view)
  196. {
  197. assert(view != nullptr);
  198. TexturePtr texture = view->getTexture();
  199. auto iterFind = texture->mTextureViews.find(view->getDesc());
  200. if(iterFind == texture->mTextureViews.end())
  201. {
  202. BS_EXCEPT(InternalErrorException, "Trying to release a texture view that doesn't exist!");
  203. }
  204. iterFind->second->refCount--;
  205. if(iterFind->second->refCount == 0)
  206. {
  207. TextureViewReference* toRemove = iterFind->second;
  208. texture->mTextureViews.erase(iterFind);
  209. bs_delete<PoolAlloc>(toRemove);
  210. }
  211. }
  212. /************************************************************************/
  213. /* SERIALIZATION */
  214. /************************************************************************/
  215. RTTITypeBase* Texture::getRTTIStatic()
  216. {
  217. return TextureRTTI::instance();
  218. }
  219. RTTITypeBase* Texture::getRTTI() const
  220. {
  221. return Texture::getRTTIStatic();
  222. }
  223. /************************************************************************/
  224. /* STATICS */
  225. /************************************************************************/
  226. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  227. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  228. {
  229. TexturePtr texturePtr = _createPtr(texType,
  230. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  231. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  232. }
  233. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height,
  234. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  235. {
  236. TexturePtr texturePtr = _createPtr(texType,
  237. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  238. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  239. }
  240. TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  241. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  242. {
  243. return TextureManager::instance().createTexture(texType,
  244. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  245. }
  246. TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height,
  247. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  248. {
  249. return TextureManager::instance().createTexture(texType,
  250. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  251. }
  252. const HTexture& Texture::dummy()
  253. {
  254. return TextureManager::instance().getDummyTexture();
  255. }
  256. }