BsTexture.cpp 10 KB

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