2
0

CmGLTexture.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "CmRenderSystemManager.h"
  34. #include "CmGLFBORenderTexture.h"
  35. #if CM_PLATFORM == CM_PLATFORM_WIN32
  36. # define WIN32_LEAN_AND_MEAN
  37. # if !defined(NOMINMAX) && defined(_MSC_VER)
  38. # define NOMINMAX // required to stop windows.h messing up std::min
  39. # endif
  40. # include <windows.h>
  41. # include <wingdi.h>
  42. #endif
  43. #if CM_DEBUG_MODE
  44. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  45. #else
  46. #define THROW_IF_NOT_RENDER_THREAD
  47. #endif
  48. namespace CamelotEngine {
  49. GLTexture::GLTexture(GLSupport& support)
  50. : Texture(),
  51. mTextureID(0), mGLSupport(support)
  52. {
  53. }
  54. GLTexture::~GLTexture()
  55. {
  56. THROW_IF_NOT_RENDER_THREAD;
  57. freeInternalResources();
  58. }
  59. void GLTexture::initialize_internal()
  60. {
  61. createInternalResources();
  62. if( mUsage & TU_RENDERTARGET )
  63. {
  64. createRenderTexture();
  65. }
  66. Resource::initialize_internal();
  67. }
  68. GLenum GLTexture::getGLTextureTarget_internal(void) const
  69. {
  70. switch(mTextureType)
  71. {
  72. case TEX_TYPE_1D:
  73. return GL_TEXTURE_1D;
  74. case TEX_TYPE_2D:
  75. return GL_TEXTURE_2D;
  76. case TEX_TYPE_3D:
  77. return GL_TEXTURE_3D;
  78. case TEX_TYPE_CUBE_MAP:
  79. return GL_TEXTURE_CUBE_MAP;
  80. default:
  81. return 0;
  82. };
  83. }
  84. GLuint GLTexture::getGLID_internal() const
  85. {
  86. THROW_IF_NOT_RENDER_THREAD;
  87. return mTextureID;
  88. }
  89. //* Creation / loading methods ********************************************
  90. void GLTexture::createInternalResourcesImpl(void)
  91. {
  92. if (!GLEW_VERSION_1_2 && mTextureType == TEX_TYPE_3D)
  93. CM_EXCEPT(NotImplementedException,
  94. "3D Textures not supported before OpenGL 1.2");
  95. // Convert to nearest power-of-two size if required
  96. mWidth = GLPixelUtil::optionalPO2(mWidth);
  97. mHeight = GLPixelUtil::optionalPO2(mHeight);
  98. mDepth = GLPixelUtil::optionalPO2(mDepth);
  99. // Adjust format if required
  100. mFormat = TextureManager::instance().getNativeFormat(mTextureType, mFormat, mUsage);
  101. // Check requested number of mipmaps
  102. UINT32 maxMips = GLPixelUtil::getMaxMipmaps(mWidth, mHeight, mDepth, mFormat);
  103. if(mNumMipmaps>maxMips)
  104. mNumMipmaps = maxMips;
  105. // Generate texture name
  106. glGenTextures( 1, &mTextureID );
  107. // Set texture type
  108. glBindTexture( getGLTextureTarget_internal(), mTextureID );
  109. // This needs to be set otherwise the texture doesn't get rendered
  110. if (GLEW_VERSION_1_2)
  111. glTexParameteri( getGLTextureTarget_internal(), GL_TEXTURE_MAX_LEVEL, mNumMipmaps );
  112. // Set some misc default parameters so NVidia won't complain, these can of course be changed later
  113. glTexParameteri(getGLTextureTarget_internal(), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  114. glTexParameteri(getGLTextureTarget_internal(), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  115. if (GLEW_VERSION_1_2)
  116. {
  117. glTexParameteri(getGLTextureTarget_internal(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  118. glTexParameteri(getGLTextureTarget_internal(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  119. }
  120. // Allocate internal buffer so that glTexSubImageXD can be used
  121. // Internal format
  122. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat, mHwGamma);
  123. UINT32 width = mWidth;
  124. UINT32 height = mHeight;
  125. UINT32 depth = mDepth;
  126. if(PixelUtil::isCompressed(mFormat))
  127. {
  128. // Compressed formats
  129. UINT32 size = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  130. // Provide temporary buffer filled with zeroes as glCompressedTexImageXD does not
  131. // accept a 0 pointer like normal glTexImageXD
  132. // Run through this process for every mipmap to pregenerate mipmap piramid
  133. UINT8 *tmpdata = new UINT8[size];
  134. memset(tmpdata, 0, size);
  135. for(UINT32 mip=0; mip<=mNumMipmaps; mip++)
  136. {
  137. size = PixelUtil::getMemorySize(width, height, depth, mFormat);
  138. switch(mTextureType)
  139. {
  140. case TEX_TYPE_1D:
  141. glCompressedTexImage1DARB(GL_TEXTURE_1D, mip, format,
  142. width, 0,
  143. size, tmpdata);
  144. break;
  145. case TEX_TYPE_2D:
  146. glCompressedTexImage2DARB(GL_TEXTURE_2D, mip, format,
  147. width, height, 0,
  148. size, tmpdata);
  149. break;
  150. case TEX_TYPE_3D:
  151. glCompressedTexImage3DARB(GL_TEXTURE_3D, mip, format,
  152. width, height, depth, 0,
  153. size, tmpdata);
  154. break;
  155. case TEX_TYPE_CUBE_MAP:
  156. for(int face=0; face<6; face++) {
  157. glCompressedTexImage2DARB(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  158. width, height, 0,
  159. size, tmpdata);
  160. }
  161. break;
  162. };
  163. if(width>1) width = width/2;
  164. if(height>1) height = height/2;
  165. if(depth>1) depth = depth/2;
  166. }
  167. delete [] tmpdata;
  168. }
  169. else
  170. {
  171. // Run through this process to pregenerate mipmap piramid
  172. for(UINT32 mip=0; mip<=mNumMipmaps; mip++)
  173. {
  174. // Normal formats
  175. switch(mTextureType)
  176. {
  177. case TEX_TYPE_1D:
  178. glTexImage1D(GL_TEXTURE_1D, mip, format,
  179. width, 0,
  180. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  181. break;
  182. case TEX_TYPE_2D:
  183. glTexImage2D(GL_TEXTURE_2D, mip, format,
  184. width, height, 0,
  185. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  186. break;
  187. case TEX_TYPE_3D:
  188. glTexImage3D(GL_TEXTURE_3D, mip, format,
  189. width, height, depth, 0,
  190. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  191. break;
  192. case TEX_TYPE_CUBE_MAP:
  193. for(int face=0; face<6; face++) {
  194. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  195. width, height, 0,
  196. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  197. }
  198. break;
  199. };
  200. if(width>1) width = width/2;
  201. if(height>1) height = height/2;
  202. if(depth>1) depth = depth/2;
  203. }
  204. }
  205. createSurfaceList();
  206. // Get final internal format
  207. mFormat = getBuffer_internal(0,0)->getFormat();
  208. }
  209. void GLTexture::createRenderTexture(void)
  210. {
  211. // Create the GL texture
  212. // This already does everything neccessary
  213. createInternalResources();
  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_internal(), 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_internal(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_internal(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