CmTexture.cpp 11 KB

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