CmTexture.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "CmHardwarePixelBuffer.h"
  25. #include "CmTexture.h"
  26. #include "CmTextureRTTI.h"
  27. #include "CmDataStream.h"
  28. #include "CmException.h"
  29. #include "CmDebug.h"
  30. #include "CmRenderSystem.h"
  31. #include "CmRenderSystemManager.h"
  32. #include "CmAsyncOp.h"
  33. #if CM_DEBUG_MODE
  34. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  35. #else
  36. #define THROW_IF_NOT_RENDER_THREAD
  37. #endif
  38. namespace CamelotEngine {
  39. //--------------------------------------------------------------------------
  40. Texture::Texture()
  41. :
  42. mHeight(512),
  43. mWidth(512),
  44. mDepth(1),
  45. mNumMipmaps(0),
  46. mHwGamma(false),
  47. mFSAA(0),
  48. mTextureType(TEX_TYPE_2D),
  49. mFormat(PF_UNKNOWN),
  50. mUsage(TU_DEFAULT)
  51. {
  52. }
  53. //-------------------------------------------------------------------------
  54. void Texture::initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  55. PixelFormat format, int usage, bool hwGamma, UINT32 fsaa, const String& fsaaHint)
  56. {
  57. mTextureType = textureType;
  58. mWidth = width;
  59. mHeight = height;
  60. mDepth = depth;
  61. mNumMipmaps = numMipmaps;
  62. mFormat = format;
  63. mUsage = usage;
  64. mHwGamma = hwGamma;
  65. mFSAA = fsaa;
  66. mFSAAHint = fsaaHint;
  67. mSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  68. RenderSystemManager::getActive()->queueResourceCommand(boost::bind(&Texture::initialize_internal, this));
  69. }
  70. //--------------------------------------------------------------------------
  71. bool Texture::hasAlpha(void) const
  72. {
  73. return PixelUtil::hasAlpha(mFormat);
  74. }
  75. //--------------------------------------------------------------------------
  76. UINT32 Texture::calculateSize(void) const
  77. {
  78. return getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  79. }
  80. //--------------------------------------------------------------------------
  81. UINT32 Texture::getNumFaces(void) const
  82. {
  83. return getTextureType() == TEX_TYPE_CUBE_MAP ? 6 : 1;
  84. }
  85. //-------------------------------------------------------------------------
  86. void Texture::getCustomAttribute_internal(const String& name, void* pData)
  87. {
  88. THROW_IF_NOT_RENDER_THREAD;
  89. }
  90. //-----------------------------------------------------------------------------
  91. void Texture::createInternalResources(void)
  92. {
  93. createInternalResourcesImpl();
  94. }
  95. //-----------------------------------------------------------------------------
  96. void Texture::freeInternalResources(void)
  97. {
  98. freeInternalResourcesImpl();
  99. }
  100. void Texture::setRawPixels(const PixelData& data, UINT32 face, UINT32 mip)
  101. {
  102. RenderSystemManager::getActive()->queueResourceCommand(boost::bind(&Texture::setRawPixels_internal, this, data, face, mip), true);
  103. }
  104. void Texture::setRawPixels_async(const PixelData& data, UINT32 face, UINT32 mip)
  105. {
  106. RenderSystemManager::getActive()->queueResourceCommand(boost::bind(&Texture::setRawPixels_internal, this, data, face, mip));
  107. }
  108. void Texture::setRawPixels_internal(const PixelData& data, UINT32 face, UINT32 mip)
  109. {
  110. THROW_IF_NOT_RENDER_THREAD
  111. if(mip < 0 || mip > mNumMipmaps)
  112. CM_EXCEPT(InvalidParametersException, "Invalid mip level: " + toString(mip) + ". Min is 0, max is " + toString(mNumMipmaps));
  113. if(face < 0 || mip > getNumFaces())
  114. CM_EXCEPT(InvalidParametersException, "Invalid face index: " + toString(face) + ". Min is 0, max is " + toString(getNumFaces()));
  115. if(mFormat != data.format)
  116. CM_EXCEPT(InvalidParametersException, "Provided PixelData has invalid format. Needed: " + toString(mFormat) + ". Got: " + toString(data.format));
  117. if(mWidth != data.getWidth() || mHeight != data.getHeight() || mDepth != data.getDepth())
  118. {
  119. CM_EXCEPT(InvalidParametersException, "Provided PixelData size doesn't match the texture size." \
  120. " Width: " + toString(mWidth) + "/" + toString(data.getWidth()) +
  121. " Height: " + toString(mHeight) + "/" + toString(data.getHeight()) +
  122. " Depth: " + toString(mDepth) + "/" + toString(data.getDepth()));
  123. }
  124. getBuffer_internal(face, mip)->blitFromMemory(data);
  125. }
  126. PixelDataPtr Texture::getRawPixels(UINT32 face, UINT32 mip)
  127. {
  128. AsyncOp op = RenderSystemManager::getActive()->queueResourceReturnCommand(boost::bind(&Texture::getRawPixels_internal, this, face, mip, _1), true);
  129. return op.getReturnValue<PixelDataPtr>();
  130. }
  131. AsyncOp Texture::getRawPixels_async(UINT32 face, UINT32 mip)
  132. {
  133. return RenderSystemManager::getActive()->queueResourceReturnCommand(boost::bind(&Texture::getRawPixels_internal, this, face, mip, _1));
  134. }
  135. void Texture::getRawPixels_internal(UINT32 face, UINT32 mip, AsyncOp& op)
  136. {
  137. THROW_IF_NOT_RENDER_THREAD;
  138. if(mip < 0 || mip > mNumMipmaps)
  139. CM_EXCEPT(InvalidParametersException, "Invalid mip level: " + toString(mip) + ". Min is 0, max is " + toString(mNumMipmaps));
  140. if(face < 0 || mip > getNumFaces())
  141. CM_EXCEPT(InvalidParametersException, "Invalid face index: " + toString(face) + ". Min is 0, max is " + toString(getNumFaces()));
  142. UINT32 numMips = getNumMipmaps();
  143. UINT32 totalSize = 0;
  144. UINT32 width = getWidth();
  145. UINT32 height = getHeight();
  146. UINT32 depth = getDepth();
  147. for(UINT32 j = 0; j <= mip; j++)
  148. {
  149. totalSize = PixelUtil::getMemorySize(width, height, depth, mFormat);
  150. if(width != 1) width /= 2;
  151. if(height != 1) height /= 2;
  152. if(depth != 1) depth /= 2;
  153. }
  154. UINT8* buffer = new UINT8[totalSize];
  155. PixelDataPtr src(new PixelData(width, height, depth, getFormat(), buffer, true));
  156. getBuffer_internal(face, mip)->blitToMemory(*src);
  157. op.completeOperation(src);
  158. }
  159. //-----------------------------------------------------------------------------
  160. void Texture::unloadImpl(void)
  161. {
  162. THROW_IF_NOT_RENDER_THREAD;
  163. freeInternalResources();
  164. }
  165. //-----------------------------------------------------------------------------
  166. void Texture::copy_internal( TexturePtr& target )
  167. {
  168. THROW_IF_NOT_RENDER_THREAD;
  169. if(target->getNumFaces() != getNumFaces())
  170. {
  171. CM_EXCEPT(InvalidParametersException,
  172. "Texture types must match");
  173. }
  174. size_t numMips = std::min(getNumMipmaps(), target->getNumMipmaps());
  175. for(unsigned int face=0; face<getNumFaces(); face++)
  176. {
  177. for(unsigned int mip=0; mip<=numMips; mip++)
  178. {
  179. target->getBuffer_internal(face, mip)->blit(getBuffer_internal(face, mip));
  180. }
  181. }
  182. }
  183. //-----------------------------------------------------------------------------
  184. void Texture::throwIfNotRenderThread() const
  185. {
  186. if(CM_THREAD_CURRENT_ID != RenderSystemManager::getActive()->getRenderThreadId())
  187. CM_EXCEPT(InternalErrorException, "Calling an internal texture method from a non-render thread!");
  188. }
  189. /************************************************************************/
  190. /* SERIALIZATION */
  191. /************************************************************************/
  192. RTTITypeBase* Texture::getRTTIStatic()
  193. {
  194. return TextureRTTI::instance();
  195. }
  196. RTTITypeBase* Texture::getRTTI() const
  197. {
  198. return Texture::getRTTIStatic();
  199. }
  200. /************************************************************************/
  201. /* STATICS */
  202. /************************************************************************/
  203. TexturePtr Texture::create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  204. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 fsaa, const String& fsaaHint)
  205. {
  206. return TextureManager::instance().create(texType,
  207. width, height, depth, num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
  208. }
  209. TexturePtr Texture::create(TextureType texType, UINT32 width, UINT32 height,
  210. int num_mips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 fsaa, const String& fsaaHint)
  211. {
  212. return TextureManager::instance().create(texType,
  213. width, height, num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
  214. }
  215. }
  216. #undef THROW_IF_NOT_RENDER_THREAD