BsTexture.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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);
  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. if(pixelData.getWidth() != getWidth() ||pixelData.getHeight() != getHeight() ||
  70. pixelData.getDepth() != getDepth() || pixelData.getFormat() != getFormat())
  71. {
  72. BS_EXCEPT(RenderingAPIException, "Provided buffer is not of valid dimensions or format in order to write to this texture.");
  73. }
  74. UINT32 face = 0;
  75. UINT32 mip = 0;
  76. mapFromSubresourceIdx(subresourceIdx, face, mip);
  77. writeData(pixelData, mip, face, discardEntireBuffer);
  78. }
  79. void Texture::readSubresource(UINT32 subresourceIdx, GpuResourceData& data)
  80. {
  81. THROW_IF_NOT_CORE_THREAD;
  82. if(data.getTypeId() != TID_PixelData)
  83. BS_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only PixelData is supported.");
  84. PixelData& pixelData = static_cast<PixelData&>(data);
  85. if(pixelData.getWidth() != getWidth() ||pixelData.getHeight() != getHeight() ||
  86. pixelData.getDepth() != getDepth() || pixelData.getFormat() != getFormat())
  87. {
  88. BS_EXCEPT(RenderingAPIException, "Provided buffer is not of valid dimensions or format in order to read from this texture.");
  89. }
  90. UINT32 face = 0;
  91. UINT32 mip = 0;
  92. mapFromSubresourceIdx(subresourceIdx, face, mip);
  93. readData(pixelData, mip, face);
  94. }
  95. PixelDataPtr Texture::allocateSubresourceBuffer(UINT32 subresourceIdx) const
  96. {
  97. UINT32 face = 0;
  98. UINT32 mip = 0;
  99. mapFromSubresourceIdx(subresourceIdx, face, mip);
  100. UINT32 numMips = getNumMipmaps();
  101. UINT32 width = getWidth();
  102. UINT32 height = getHeight();
  103. UINT32 depth = getDepth();
  104. UINT32 totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  105. for(UINT32 j = 0; j < mip; j++)
  106. {
  107. totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  108. if(width != 1) width /= 2;
  109. if(height != 1) height /= 2;
  110. if(depth != 1) depth /= 2;
  111. }
  112. PixelDataPtr dst = bs_shared_ptr<PixelData, PoolAlloc>(width, height, depth, getFormat());
  113. dst->allocateInternalBuffer();
  114. return dst;
  115. }
  116. void Texture::mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const
  117. {
  118. UINT32 numMipmaps = getNumMipmaps() + 1;
  119. face = Math::floorToInt((subresourceIdx) / (float)numMipmaps);
  120. mip = subresourceIdx % numMipmaps;
  121. }
  122. UINT32 Texture::mapToSubresourceIdx(UINT32 face, UINT32 mip) const
  123. {
  124. return face * (getNumMipmaps() + 1) + mip;
  125. }
  126. PixelData Texture::lock(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  127. {
  128. THROW_IF_NOT_CORE_THREAD;
  129. if(mipLevel < 0 || mipLevel > mNumMipmaps)
  130. BS_EXCEPT(InvalidParametersException, "Invalid mip level: " + toString(mipLevel) + ". Min is 0, max is " + toString(getNumMipmaps()));
  131. if(face < 0 || face >= getNumFaces())
  132. BS_EXCEPT(InvalidParametersException, "Invalid face index: " + toString(face) + ". Min is 0, max is " + toString(getNumFaces()));
  133. return lockImpl(options, mipLevel, face);
  134. }
  135. void Texture::unlock()
  136. {
  137. THROW_IF_NOT_CORE_THREAD;
  138. unlockImpl();
  139. }
  140. void Texture::copy(TexturePtr& target)
  141. {
  142. THROW_IF_NOT_CORE_THREAD;
  143. if (target->getUsage() != this->getUsage() ||
  144. target->getTextureType() != this->getTextureType())
  145. {
  146. BS_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
  147. }
  148. if(getWidth() != target->getWidth() || getHeight() != target->getHeight() || getDepth() != target->getDepth())
  149. {
  150. BS_EXCEPT(InvalidParametersException, "Texture sizes don't match." \
  151. " Width: " + toString(getWidth()) + "/" + toString(target->getWidth()) +
  152. " Height: " + toString(getHeight()) + "/" + toString(target->getHeight()) +
  153. " Depth: " + toString(getDepth()) + "/" + toString(target->getDepth()));
  154. }
  155. if(getNumFaces() != target->getNumFaces())
  156. {
  157. BS_EXCEPT(InvalidParametersException, "Number of texture faces doesn't match." \
  158. " Num faces: " + toString(getNumFaces()) + "/" + toString(target->getNumFaces()));
  159. }
  160. if(getNumMipmaps() != target->getNumMipmaps())
  161. {
  162. BS_EXCEPT(InvalidParametersException, "Number of mipmaps doesn't match." \
  163. " Num mipmaps: " + toString(getNumMipmaps()) + "/" + toString(target->getNumMipmaps()));
  164. }
  165. copyImpl(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. assert(texture != nullptr);
  183. TEXTURE_VIEW_DESC key;
  184. key.mostDetailMip = mostDetailMip;
  185. key.numMips = numMips;
  186. key.firstArraySlice = firstArraySlice;
  187. key.numArraySlices = numArraySlices;
  188. key.usage = usage;
  189. auto iterFind = texture->mTextureViews.find(key);
  190. if(iterFind == texture->mTextureViews.end())
  191. {
  192. TextureViewPtr newView = texture->createView();
  193. newView->initialize(texture, key);
  194. texture->mTextureViews[key] = new (bs_alloc<TextureViewReference, PoolAlloc>()) TextureViewReference(newView);
  195. iterFind = texture->mTextureViews.find(key);
  196. }
  197. iterFind->second->refCount++;
  198. return iterFind->second->view;
  199. }
  200. void Texture::releaseView(TextureViewPtr view)
  201. {
  202. assert(view != nullptr);
  203. TexturePtr texture = view->getTexture();
  204. auto iterFind = texture->mTextureViews.find(view->getDesc());
  205. if(iterFind == texture->mTextureViews.end())
  206. {
  207. BS_EXCEPT(InternalErrorException, "Trying to release a texture view that doesn't exist!");
  208. }
  209. iterFind->second->refCount--;
  210. if(iterFind->second->refCount == 0)
  211. {
  212. TextureViewReference* toRemove = iterFind->second;
  213. texture->mTextureViews.erase(iterFind);
  214. bs_delete<PoolAlloc>(toRemove);
  215. }
  216. }
  217. /************************************************************************/
  218. /* SERIALIZATION */
  219. /************************************************************************/
  220. RTTITypeBase* Texture::getRTTIStatic()
  221. {
  222. return TextureRTTI::instance();
  223. }
  224. RTTITypeBase* Texture::getRTTI() const
  225. {
  226. return Texture::getRTTIStatic();
  227. }
  228. /************************************************************************/
  229. /* STATICS */
  230. /************************************************************************/
  231. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  232. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  233. {
  234. TexturePtr texturePtr = _createPtr(texType,
  235. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  236. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  237. }
  238. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height,
  239. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  240. {
  241. TexturePtr texturePtr = _createPtr(texType,
  242. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  243. return static_resource_cast<Texture>(gResources()._createResourceHandle(texturePtr));
  244. }
  245. TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  246. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  247. {
  248. return TextureManager::instance().createTexture(texType,
  249. width, height, depth, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  250. }
  251. TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height,
  252. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, const String& multisampleHint)
  253. {
  254. return TextureManager::instance().createTexture(texType,
  255. width, height, num_mips, format, usage, hwGammaCorrection, multisampleCount, multisampleHint);
  256. }
  257. const HTexture& Texture::dummy()
  258. {
  259. return TextureManager::instance().getDummyTexture();
  260. }
  261. }