CmTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmPixelBuffer.h"
  25. #include "CmTexture.h"
  26. #include "CmTextureRTTI.h"
  27. #include "CmDataStream.h"
  28. #include "CmException.h"
  29. #include "CmDebug.h"
  30. #include "CmCoreThread.h"
  31. #include "CmAsyncOp.h"
  32. namespace CamelotFramework {
  33. //--------------------------------------------------------------------------
  34. Texture::Texture()
  35. :
  36. mHeight(512),
  37. mWidth(512),
  38. mDepth(1),
  39. mNumMipmaps(0),
  40. mHwGamma(false),
  41. mFSAA(0),
  42. mTextureType(TEX_TYPE_2D),
  43. mFormat(PF_UNKNOWN),
  44. mUsage(TU_DEFAULT)
  45. {
  46. }
  47. //-------------------------------------------------------------------------
  48. void Texture::initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  49. PixelFormat format, int usage, bool hwGamma, UINT32 fsaa, const String& fsaaHint)
  50. {
  51. mTextureType = textureType;
  52. mWidth = width;
  53. mHeight = height;
  54. mDepth = depth;
  55. mNumMipmaps = numMipmaps;
  56. mFormat = format;
  57. mUsage = usage;
  58. mHwGamma = hwGamma;
  59. mFSAA = fsaa;
  60. mFSAAHint = fsaaHint;
  61. mSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  62. Resource::initialize();
  63. }
  64. //--------------------------------------------------------------------------
  65. bool Texture::hasAlpha(void) const
  66. {
  67. return PixelUtil::hasAlpha(mFormat);
  68. }
  69. //--------------------------------------------------------------------------
  70. UINT32 Texture::calculateSize(void) const
  71. {
  72. return getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  73. }
  74. //--------------------------------------------------------------------------
  75. UINT32 Texture::getNumFaces(void) const
  76. {
  77. return getTextureType() == TEX_TYPE_CUBE_MAP ? 6 : 1;
  78. }
  79. void Texture::writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer)
  80. {
  81. THROW_IF_NOT_CORE_THREAD;
  82. if(data.getTypeId() != TID_PixelData)
  83. CM_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only PixelData is supported.");
  84. if(discardEntireBuffer)
  85. {
  86. if(mUsage != TU_DYNAMIC)
  87. {
  88. LOGWRN("Buffer discard is enabled but buffer was not created as dynamic. Disabling discard.");
  89. discardEntireBuffer = false;
  90. }
  91. }
  92. else
  93. {
  94. if(mUsage == TU_DYNAMIC)
  95. {
  96. LOGWRN("Buffer discard is not enabled but buffer was not created as dynamic. Enabling discard.");
  97. discardEntireBuffer = true;
  98. }
  99. }
  100. const PixelData& pixelData = static_cast<const PixelData&>(data);
  101. if(pixelData.getWidth() != getWidth() ||pixelData.getHeight() != getHeight() ||
  102. pixelData.getDepth() != getDepth() || pixelData.getFormat() != getFormat())
  103. {
  104. CM_EXCEPT(RenderingAPIException, "Provided buffer is not of valid dimensions or format in order to write to this texture.");
  105. }
  106. UINT32 face = 0;
  107. UINT32 mip = 0;
  108. mapFromSubresourceIdx(subresourceIdx, face, mip);
  109. writeData(pixelData, mip, face, discardEntireBuffer);
  110. }
  111. void Texture::readSubresource(UINT32 subresourceIdx, GpuResourceData& data)
  112. {
  113. THROW_IF_NOT_CORE_THREAD;
  114. if(data.getTypeId() != TID_PixelData)
  115. CM_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only PixelData is supported.");
  116. PixelData& pixelData = static_cast<PixelData&>(data);
  117. if(pixelData.getWidth() != getWidth() ||pixelData.getHeight() != getHeight() ||
  118. pixelData.getDepth() != getDepth() || pixelData.getFormat() != getFormat())
  119. {
  120. CM_EXCEPT(RenderingAPIException, "Provided buffer is not of valid dimensions or format in order to read from this texture.");
  121. }
  122. UINT32 face = 0;
  123. UINT32 mip = 0;
  124. mapFromSubresourceIdx(subresourceIdx, face, mip);
  125. readData(pixelData, mip, face);
  126. }
  127. PixelDataPtr Texture::allocateSubresourceBuffer(UINT32 subresourceIdx) const
  128. {
  129. UINT32 face = 0;
  130. UINT32 mip = 0;
  131. mapFromSubresourceIdx(subresourceIdx, face, mip);
  132. UINT32 numMips = getNumMipmaps();
  133. UINT32 width = getWidth();
  134. UINT32 height = getHeight();
  135. UINT32 depth = getDepth();
  136. UINT32 totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  137. for(UINT32 j = 0; j < mip; j++)
  138. {
  139. totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  140. if(width != 1) width /= 2;
  141. if(height != 1) height /= 2;
  142. if(depth != 1) depth /= 2;
  143. }
  144. PixelDataPtr dst = cm_shared_ptr<PixelData, PoolAlloc>(width, height, depth, getFormat());
  145. dst->allocateInternalBuffer();
  146. return dst;
  147. }
  148. void Texture::mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const
  149. {
  150. UINT32 numMipmaps = getNumMipmaps() + 1;
  151. face = Math::FloorToInt((subresourceIdx) / (float)numMipmaps);
  152. mip = subresourceIdx % numMipmaps;
  153. }
  154. UINT32 Texture::mapToSubresourceIdx(UINT32 face, UINT32 mip) const
  155. {
  156. return face * (getNumMipmaps() + 1) + mip;
  157. }
  158. PixelData Texture::lock(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  159. {
  160. THROW_IF_NOT_CORE_THREAD;
  161. if(mipLevel < 0 || mipLevel > mNumMipmaps)
  162. CM_EXCEPT(InvalidParametersException, "Invalid mip level: " + toString(mipLevel) + ". Min is 0, max is " + toString(getNumMipmaps()));
  163. if(face < 0 || face >= getNumFaces())
  164. CM_EXCEPT(InvalidParametersException, "Invalid face index: " + toString(face) + ". Min is 0, max is " + toString(getNumFaces()));
  165. return lockImpl(options, mipLevel, face);
  166. }
  167. void Texture::unlock()
  168. {
  169. THROW_IF_NOT_CORE_THREAD;
  170. unlockImpl();
  171. }
  172. void Texture::copy(TexturePtr& target)
  173. {
  174. THROW_IF_NOT_CORE_THREAD;
  175. if (target->getUsage() != this->getUsage() ||
  176. target->getTextureType() != this->getTextureType())
  177. {
  178. CM_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
  179. }
  180. if(getWidth() != target->getWidth() || getHeight() != target->getHeight() || getDepth() != target->getDepth())
  181. {
  182. CM_EXCEPT(InvalidParametersException, "Texture sizes don't match." \
  183. " Width: " + toString(getWidth()) + "/" + toString(target->getWidth()) +
  184. " Height: " + toString(getHeight()) + "/" + toString(target->getHeight()) +
  185. " Depth: " + toString(getDepth()) + "/" + toString(target->getDepth()));
  186. }
  187. if(getNumFaces() != target->getNumFaces())
  188. {
  189. CM_EXCEPT(InvalidParametersException, "Number of texture faces doesn't match." \
  190. " Num faces: " + toString(getNumFaces()) + "/" + toString(target->getNumFaces()));
  191. }
  192. if(getNumMipmaps() != target->getNumMipmaps())
  193. {
  194. CM_EXCEPT(InvalidParametersException, "Number of mipmaps doesn't match." \
  195. " Num mipmaps: " + toString(getNumMipmaps()) + "/" + toString(target->getNumMipmaps()));
  196. }
  197. copyImpl(target);
  198. }
  199. /************************************************************************/
  200. /* TEXTURE VIEW */
  201. /************************************************************************/
  202. TextureViewPtr Texture::createView()
  203. {
  204. TextureViewPtr viewPtr = cm_core_ptr<TextureView, PoolAlloc>(new (cm_alloc<TextureView, PoolAlloc>()) TextureView());
  205. viewPtr->setThisPtr(viewPtr);
  206. return viewPtr;
  207. }
  208. void Texture::clearBufferViews()
  209. {
  210. mTextureViews.clear();
  211. }
  212. TextureViewPtr Texture::requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage)
  213. {
  214. assert(texture != nullptr);
  215. TEXTURE_VIEW_DESC key;
  216. key.mostDetailMip = mostDetailMip;
  217. key.numMips = numMips;
  218. key.firstArraySlice = firstArraySlice;
  219. key.numArraySlices = numArraySlices;
  220. key.usage = usage;
  221. auto iterFind = texture->mTextureViews.find(key);
  222. if(iterFind == texture->mTextureViews.end())
  223. {
  224. TextureViewPtr newView = texture->createView();
  225. newView->initialize(texture, key);
  226. texture->mTextureViews[key] = new (cm_alloc<TextureViewReference, PoolAlloc>()) TextureViewReference(newView);
  227. iterFind = texture->mTextureViews.find(key);
  228. }
  229. iterFind->second->refCount++;
  230. return iterFind->second->view;
  231. }
  232. void Texture::releaseView(TextureViewPtr view)
  233. {
  234. assert(view != nullptr);
  235. TexturePtr texture = view->getTexture();
  236. auto iterFind = texture->mTextureViews.find(view->getDesc());
  237. if(iterFind == texture->mTextureViews.end())
  238. {
  239. CM_EXCEPT(InternalErrorException, "Trying to release a texture view that doesn't exist!");
  240. }
  241. iterFind->second->refCount--;
  242. if(iterFind->second->refCount == 0)
  243. {
  244. TextureViewReference* toRemove = iterFind->second;
  245. texture->mTextureViews.erase(iterFind);
  246. cm_delete<PoolAlloc>(toRemove);
  247. }
  248. }
  249. /************************************************************************/
  250. /* SERIALIZATION */
  251. /************************************************************************/
  252. RTTITypeBase* Texture::getRTTIStatic()
  253. {
  254. return TextureRTTI::instance();
  255. }
  256. RTTITypeBase* Texture::getRTTI() const
  257. {
  258. return Texture::getRTTIStatic();
  259. }
  260. /************************************************************************/
  261. /* STATICS */
  262. /************************************************************************/
  263. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  264. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 fsaa, const String& fsaaHint)
  265. {
  266. TexturePtr texturePtr = TextureManager::instance().createTexture(texType,
  267. width, height, depth, num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
  268. return static_resource_cast<Texture>(Resource::_createResourceHandle(texturePtr));
  269. }
  270. HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height,
  271. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 fsaa, const String& fsaaHint)
  272. {
  273. TexturePtr texturePtr = TextureManager::instance().createTexture(texType,
  274. width, height, num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
  275. return static_resource_cast<Texture>(Resource::_createResourceHandle(texturePtr));
  276. }
  277. HTexture Texture::dummy()
  278. {
  279. return TextureManager::instance().getDummyTexture();
  280. }
  281. }