CmGLTexture.cpp 10.0 KB

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