CmGLTexture.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "CmGLTexture.h"
  25. #include "CmGLSupport.h"
  26. #include "CmGLPixelFormat.h"
  27. #include "CmGLPixelBuffer.h"
  28. #include "CmException.h"
  29. #include "CmBitwise.h"
  30. #include "CmTextureManager.h"
  31. #include "CmGLRenderTexture.h"
  32. #if CM_PLATFORM == CM_PLATFORM_WIN32
  33. # define WIN32_LEAN_AND_MEAN
  34. # if !defined(NOMINMAX) && defined(_MSC_VER)
  35. # define NOMINMAX // required to stop windows.h messing up std::min
  36. # endif
  37. # include <windows.h>
  38. # include <wingdi.h>
  39. #endif
  40. #if CM_DEBUG_MODE
  41. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  42. #else
  43. #define THROW_IF_NOT_RENDER_THREAD
  44. #endif
  45. namespace CamelotFramework {
  46. GLTexture::GLTexture(GLSupport& support)
  47. : Texture(),
  48. mTextureID(0), mGLSupport(support)
  49. {
  50. }
  51. GLTexture::~GLTexture()
  52. {
  53. }
  54. void GLTexture::initialize_internal()
  55. {
  56. Texture::initialize_internal();
  57. // Convert to nearest power-of-two size if required
  58. mWidth = GLPixelUtil::optionalPO2(mWidth);
  59. mHeight = GLPixelUtil::optionalPO2(mHeight);
  60. mDepth = GLPixelUtil::optionalPO2(mDepth);
  61. // Adjust format if required
  62. mFormat = TextureManager::instance().getNativeFormat(mTextureType, mFormat, mUsage);
  63. // Check requested number of mipmaps
  64. UINT32 maxMips = GLPixelUtil::getMaxMipmaps(mWidth, mHeight, mDepth, mFormat);
  65. if(mNumMipmaps>maxMips)
  66. mNumMipmaps = maxMips;
  67. if((mUsage & TU_RENDERTARGET) != 0)
  68. {
  69. mNumMipmaps = 1;
  70. if(mTextureType != TEX_TYPE_2D)
  71. CM_EXCEPT(NotImplementedException, "Only 2D render targets are supported at the moment");
  72. }
  73. if((mUsage & TU_DEPTHSTENCIL) != 0)
  74. {
  75. mNumMipmaps = 1;
  76. if(mTextureType != TEX_TYPE_2D)
  77. CM_EXCEPT(NotImplementedException, "Only 2D depth stencil targets are supported at the moment");
  78. if(!PixelUtil::isDepth(mFormat))
  79. CM_EXCEPT(NotImplementedException, "Supplied format is not a depth stencil format. Format: " + toString(mFormat));
  80. }
  81. // Generate texture name
  82. glGenTextures( 1, &mTextureID );
  83. // Set texture type
  84. glBindTexture( getGLTextureTarget(), mTextureID );
  85. // This needs to be set otherwise the texture doesn't get rendered
  86. glTexParameteri( getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, mNumMipmaps );
  87. // Set some misc default parameters so NVidia won't complain, these can of course be changed later
  88. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  89. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  90. if (GLEW_VERSION_1_2)
  91. {
  92. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  93. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  94. }
  95. // Allocate internal buffer so that glTexSubImageXD can be used
  96. // Internal format
  97. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat, mHwGamma);
  98. UINT32 width = mWidth;
  99. UINT32 height = mHeight;
  100. UINT32 depth = mDepth;
  101. if(PixelUtil::isCompressed(mFormat))
  102. {
  103. if((mUsage & TU_RENDERTARGET) != 0)
  104. CM_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a render target.");
  105. if((mUsage & TU_DEPTHSTENCIL) != 0)
  106. CM_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a depth stencil target.");
  107. }
  108. if((mUsage & TU_RENDERTARGET) != 0 && mTextureType == TEX_TYPE_2D && mFSAA > 0)
  109. {
  110. glTexImage2DMultisample(GL_TEXTURE_2D, mFSAA, format,
  111. width, height, GL_FALSE);
  112. }
  113. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  114. {
  115. if(mFSAA > 0)
  116. {
  117. glTexImage2DMultisample(GL_TEXTURE_2D, mFSAA, format,
  118. width, height, GL_FALSE);
  119. }
  120. else
  121. {
  122. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilTypeFromFormat(mFormat);
  123. glTexImage2D(GL_TEXTURE_2D, 0, format,
  124. width, height, 0,
  125. GL_DEPTH_STENCIL, depthStencilFormat, nullptr);
  126. }
  127. }
  128. else
  129. {
  130. // Run through this process to pregenerate mipmap piramid
  131. for(UINT32 mip=0; mip<=mNumMipmaps; mip++)
  132. {
  133. // Normal formats
  134. switch(mTextureType)
  135. {
  136. case TEX_TYPE_1D:
  137. glTexImage1D(GL_TEXTURE_1D, mip, format,
  138. width, 0,
  139. GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  140. break;
  141. case TEX_TYPE_2D:
  142. glTexImage2D(GL_TEXTURE_2D, mip, format,
  143. width, height, 0,
  144. GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  145. break;
  146. case TEX_TYPE_3D:
  147. glTexImage3D(GL_TEXTURE_3D, mip, format,
  148. width, height, depth, 0,
  149. GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  150. break;
  151. case TEX_TYPE_CUBE_MAP:
  152. for(int face=0; face<6; face++) {
  153. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  154. width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  155. }
  156. break;
  157. };
  158. if(width>1) width = width/2;
  159. if(height>1) height = height/2;
  160. if(depth>1) depth = depth/2;
  161. }
  162. }
  163. createSurfaceList();
  164. }
  165. void GLTexture::destroy_internal()
  166. {
  167. mSurfaceList.clear();
  168. glDeleteTextures( 1, &mTextureID );
  169. clearBufferViews();
  170. Texture::destroy_internal();
  171. }
  172. GLenum GLTexture::getGLTextureTarget(void) const
  173. {
  174. switch(mTextureType)
  175. {
  176. case TEX_TYPE_1D:
  177. return GL_TEXTURE_1D;
  178. case TEX_TYPE_2D:
  179. if(mFSAA > 0)
  180. return GL_TEXTURE_2D_MULTISAMPLE;
  181. else
  182. return GL_TEXTURE_2D;
  183. case TEX_TYPE_3D:
  184. return GL_TEXTURE_3D;
  185. case TEX_TYPE_CUBE_MAP:
  186. return GL_TEXTURE_CUBE_MAP;
  187. default:
  188. return 0;
  189. };
  190. }
  191. GLuint GLTexture::getGLID() const
  192. {
  193. THROW_IF_NOT_RENDER_THREAD;
  194. return mTextureID;
  195. }
  196. //* Creation / loading methods ********************************************
  197. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  198. {
  199. if(mLockedBuffer != nullptr)
  200. CM_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  201. UINT32 mipWidth = mWidth >> mipLevel;
  202. UINT32 mipHeight = mHeight >> mipLevel;
  203. UINT32 mipDepth = mDepth >> mipLevel;
  204. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  205. mLockedBuffer = getBuffer(face, mipLevel);
  206. lockedArea.data = mLockedBuffer->lock(options);
  207. return lockedArea;
  208. }
  209. /****************************************************************************************/
  210. void GLTexture::unlockImpl()
  211. {
  212. if(mLockedBuffer == nullptr)
  213. CM_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  214. mLockedBuffer->unlock();
  215. mLockedBuffer = nullptr;
  216. }
  217. /****************************************************************************************/
  218. void GLTexture::copyImpl(TexturePtr& target)
  219. {
  220. size_t numMips = std::min(getNumMipmaps(), target->getNumMipmaps());
  221. GLTexture* glTexture = static_cast<GLTexture*>(target.get());
  222. for(unsigned int face=0; face<getNumFaces(); face++)
  223. {
  224. for(unsigned int mip=0; mip<=numMips; mip++)
  225. {
  226. glTexture->getBuffer(face, mip)->blit(getBuffer(face, mip));
  227. }
  228. }
  229. }
  230. //---------------------------------------------------------------------------------------------
  231. void GLTexture::createSurfaceList()
  232. {
  233. mSurfaceList.clear();
  234. for(UINT32 face=0; face<getNumFaces(); face++)
  235. {
  236. for(UINT32 mip=0; mip<=getNumMipmaps(); mip++)
  237. {
  238. GLPixelBuffer *buf = CM_NEW(GLTextureBuffer, PoolAlloc) GLTextureBuffer("", getGLTextureTarget(), mTextureID, face, mip,
  239. static_cast<GpuBufferUsage>(mUsage), false, mHwGamma, mFSAA);
  240. mSurfaceList.push_back(PixelBufferPtr(buf, &MemAllocDeleter<GLPixelBuffer, PoolAlloc>::deleter));
  241. /// Check for error
  242. if(buf->getWidth()==0 || buf->getHeight()==0 || buf->getDepth()==0)
  243. {
  244. CM_EXCEPT(RenderingAPIException,
  245. "Zero sized texture surface on texture face "
  246. + toString(face)
  247. + " mipmap "+toString(mip)
  248. + ". Probably, the GL driver refused to create the texture.");
  249. }
  250. }
  251. }
  252. }
  253. //---------------------------------------------------------------------------------------------
  254. PixelBufferPtr GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  255. {
  256. THROW_IF_NOT_RENDER_THREAD;
  257. if(face >= getNumFaces())
  258. CM_EXCEPT(InvalidParametersException, "Face index out of range");
  259. if(mipmap > mNumMipmaps)
  260. CM_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  261. unsigned int idx = face*(mNumMipmaps+1) + mipmap;
  262. assert(idx < mSurfaceList.size());
  263. return mSurfaceList[idx];
  264. }
  265. }
  266. #undef THROW_IF_NOT_RENDER_THREAD