CmGLTexture.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "OgreException.h"
  31. #include "OgreStringConverter.h"
  32. #include "OgreBitwise.h"
  33. #include "CmTextureManager.h"
  34. #include "CmRenderSystemManager.h"
  35. #include "CmGLFBORenderTexture.h"
  36. #if CM_PLATFORM == CM_PLATFORM_WIN32
  37. # define WIN32_LEAN_AND_MEAN
  38. # if !defined(NOMINMAX) && defined(_MSC_VER)
  39. # define NOMINMAX // required to stop windows.h messing up std::min
  40. # endif
  41. # include <windows.h>
  42. # include <wingdi.h>
  43. #endif
  44. namespace CamelotEngine {
  45. GLTexture::GLTexture(GLSupport& support)
  46. : Texture(),
  47. mTextureID(0), mGLSupport(support)
  48. {
  49. }
  50. GLTexture::~GLTexture()
  51. {
  52. freeInternalResources();
  53. }
  54. GLenum GLTexture::getGLTextureTarget(void) const
  55. {
  56. switch(mTextureType)
  57. {
  58. case TEX_TYPE_1D:
  59. return GL_TEXTURE_1D;
  60. case TEX_TYPE_2D:
  61. return GL_TEXTURE_2D;
  62. case TEX_TYPE_3D:
  63. return GL_TEXTURE_3D;
  64. case TEX_TYPE_CUBE_MAP:
  65. return GL_TEXTURE_CUBE_MAP;
  66. default:
  67. return 0;
  68. };
  69. }
  70. //* Creation / loading methods ********************************************
  71. void GLTexture::createInternalResourcesImpl(void)
  72. {
  73. if (!GLEW_VERSION_1_2 && mTextureType == TEX_TYPE_3D)
  74. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  75. "3D Textures not supported before OpenGL 1.2",
  76. "GLTexture::createInternalResourcesImpl");
  77. // Convert to nearest power-of-two size if required
  78. mWidth = GLPixelUtil::optionalPO2(mWidth);
  79. mHeight = GLPixelUtil::optionalPO2(mHeight);
  80. mDepth = GLPixelUtil::optionalPO2(mDepth);
  81. // Adjust format if required
  82. mFormat = TextureManager::getSingleton().getNativeFormat(mTextureType, mFormat, mUsage);
  83. // Check requested number of mipmaps
  84. size_t maxMips = GLPixelUtil::getMaxMipmaps(mWidth, mHeight, mDepth, mFormat);
  85. mNumMipmaps = mNumRequestedMipmaps;
  86. if(mNumMipmaps>maxMips)
  87. mNumMipmaps = maxMips;
  88. // Generate texture name
  89. glGenTextures( 1, &mTextureID );
  90. // Set texture type
  91. glBindTexture( getGLTextureTarget(), mTextureID );
  92. // This needs to be set otherwise the texture doesn't get rendered
  93. if (GLEW_VERSION_1_2)
  94. glTexParameteri( getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, mNumMipmaps );
  95. // Set some misc default parameters so NVidia won't complain, these can of course be changed later
  96. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  97. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  98. if (GLEW_VERSION_1_2)
  99. {
  100. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  101. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  102. }
  103. // If we can do automip generation and the user desires this, do so
  104. mMipmapsHardwareGenerated =
  105. CamelotEngine::RenderSystemManager::getActive()->getCapabilities()->hasCapability(RSC_AUTOMIPMAP);
  106. // NVIDIA 175.16 drivers break hardware mip generation for non-compressed
  107. // textures - disable until fixed
  108. // Leave hardware gen on compressed textures since that's the only way we
  109. // can realistically do it since GLU doesn't support DXT
  110. // However DON'T do this on Apple, their drivers aren't subject to this
  111. // problem yet and in fact software generation appears to cause a crash
  112. // in some cases which I've yet to track down
  113. #if CM_PLATFORM != CM_PLATFORM_APPLE
  114. if (CamelotEngine::RenderSystemManager::getActive()->getCapabilities()->getVendor() == GPU_NVIDIA
  115. && !PixelUtil::isCompressed(mFormat))
  116. {
  117. mMipmapsHardwareGenerated = false;
  118. }
  119. #endif
  120. if((mUsage & TU_AUTOMIPMAP) &&
  121. mNumRequestedMipmaps && mMipmapsHardwareGenerated)
  122. {
  123. glTexParameteri( getGLTextureTarget(), GL_GENERATE_MIPMAP, GL_TRUE );
  124. }
  125. // Allocate internal buffer so that glTexSubImageXD can be used
  126. // Internal format
  127. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat, mHwGamma);
  128. size_t width = mWidth;
  129. size_t height = mHeight;
  130. size_t depth = mDepth;
  131. if(PixelUtil::isCompressed(mFormat))
  132. {
  133. // Compressed formats
  134. size_t size = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  135. // Provide temporary buffer filled with zeroes as glCompressedTexImageXD does not
  136. // accept a 0 pointer like normal glTexImageXD
  137. // Run through this process for every mipmap to pregenerate mipmap piramid
  138. UINT8 *tmpdata = new UINT8[size];
  139. memset(tmpdata, 0, size);
  140. for(size_t mip=0; mip<=mNumMipmaps; mip++)
  141. {
  142. size = PixelUtil::getMemorySize(width, height, depth, mFormat);
  143. switch(mTextureType)
  144. {
  145. case TEX_TYPE_1D:
  146. glCompressedTexImage1DARB(GL_TEXTURE_1D, mip, format,
  147. width, 0,
  148. size, tmpdata);
  149. break;
  150. case TEX_TYPE_2D:
  151. glCompressedTexImage2DARB(GL_TEXTURE_2D, mip, format,
  152. width, height, 0,
  153. size, tmpdata);
  154. break;
  155. case TEX_TYPE_3D:
  156. glCompressedTexImage3DARB(GL_TEXTURE_3D, mip, format,
  157. width, height, depth, 0,
  158. size, tmpdata);
  159. break;
  160. case TEX_TYPE_CUBE_MAP:
  161. for(int face=0; face<6; face++) {
  162. glCompressedTexImage2DARB(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  163. width, height, 0,
  164. size, tmpdata);
  165. }
  166. break;
  167. };
  168. if(width>1) width = width/2;
  169. if(height>1) height = height/2;
  170. if(depth>1) depth = depth/2;
  171. }
  172. delete [] tmpdata;
  173. }
  174. else
  175. {
  176. // Run through this process to pregenerate mipmap piramid
  177. for(size_t mip=0; mip<=mNumMipmaps; mip++)
  178. {
  179. // Normal formats
  180. switch(mTextureType)
  181. {
  182. case TEX_TYPE_1D:
  183. glTexImage1D(GL_TEXTURE_1D, mip, format,
  184. width, 0,
  185. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  186. break;
  187. case TEX_TYPE_2D:
  188. glTexImage2D(GL_TEXTURE_2D, mip, format,
  189. width, height, 0,
  190. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  191. break;
  192. case TEX_TYPE_3D:
  193. glTexImage3D(GL_TEXTURE_3D, mip, format,
  194. width, height, depth, 0,
  195. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  196. break;
  197. case TEX_TYPE_CUBE_MAP:
  198. for(int face=0; face<6; face++) {
  199. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  200. width, height, 0,
  201. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  202. }
  203. break;
  204. };
  205. if(width>1) width = width/2;
  206. if(height>1) height = height/2;
  207. if(depth>1) depth = depth/2;
  208. }
  209. }
  210. _createSurfaceList();
  211. // Get final internal format
  212. mFormat = getBuffer(0,0)->getFormat();
  213. }
  214. void GLTexture::createRenderTexture(void)
  215. {
  216. // Create the GL texture
  217. // This already does everything neccessary
  218. createInternalResources();
  219. }
  220. // TODO PORT - Not supported in port. Not sure what its used for
  221. // static inline void do_image_io(const String &name, const String &group,
  222. // const String &ext,
  223. // vector<Image>::type &images,
  224. // Resource *r)
  225. // {
  226. //size_t imgIdx = images.size();
  227. // images.push_back(Image());
  228. // DataStreamPtr dstream =
  229. // ResourceGroupManager::getSingleton().openResource(
  230. // name, group, true, r);
  231. // images[imgIdx].load(dstream, ext);
  232. // }
  233. void GLTexture::prepareImpl()
  234. {
  235. if( mUsage & TU_RENDERTARGET ) return;
  236. // TODO PORT - Image loading not supported
  237. //String baseName, ext;
  238. //size_t pos = mName.find_last_of(".");
  239. //baseName = mName.substr(0, pos);
  240. //if( pos != String::npos )
  241. // ext = mName.substr(pos+1);
  242. //LoadedImages loadedImages = LoadedImages(new vector<Image>::type());
  243. //if(mTextureType == TEX_TYPE_1D || mTextureType == TEX_TYPE_2D ||
  244. // mTextureType == TEX_TYPE_3D)
  245. //{
  246. // do_image_io(mName, mGroup, ext, *loadedImages, this);
  247. // // If this is a cube map, set the texture type flag accordingly.
  248. // if ((*loadedImages)[0].hasFlag(IF_CUBEMAP))
  249. // mTextureType = TEX_TYPE_CUBE_MAP;
  250. // // If this is a volumetric texture set the texture type flag accordingly.
  251. // if((*loadedImages)[0].getDepth() > 1)
  252. // mTextureType = TEX_TYPE_3D;
  253. //}
  254. //else if (mTextureType == TEX_TYPE_CUBE_MAP)
  255. //{
  256. // if(getSourceFileType() == "dds")
  257. // {
  258. // // XX HACK there should be a better way to specify whether
  259. // // all faces are in the same file or not
  260. // do_image_io(mName, mGroup, ext, *loadedImages, this);
  261. // }
  262. // else
  263. // {
  264. // vector<Image>::type images(6);
  265. // ConstImagePtrList imagePtrs;
  266. // static const String suffixes[6] = {"_rt", "_lf", "_up", "_dn", "_fr", "_bk"};
  267. // for(size_t i = 0; i < 6; i++)
  268. // {
  269. // String fullName = baseName + suffixes[i];
  270. // if (!ext.empty())
  271. // fullName = fullName + "." + ext;
  272. // // find & load resource data intro stream to allow resource
  273. // // group changes if required
  274. // do_image_io(fullName,mGroup,ext,*loadedImages,this);
  275. // }
  276. // }
  277. //}
  278. //else
  279. // OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED, "**** Unknown texture type ****", "GLTexture::prepare" );
  280. //mLoadedImages = loadedImages;
  281. }
  282. void GLTexture::unprepareImpl()
  283. {
  284. //mLoadedImages.setNull();
  285. }
  286. void GLTexture::loadImpl()
  287. {
  288. if( mUsage & TU_RENDERTARGET )
  289. {
  290. createRenderTexture();
  291. return;
  292. }
  293. // TODO PORT - Image loading not supported
  294. //// Now the only copy is on the stack and will be cleaned in case of
  295. //// exceptions being thrown from _loadImages
  296. //LoadedImages loadedImages = mLoadedImages;
  297. //mLoadedImages.setNull();
  298. //// Call internal _loadImages, not loadImage since that's external and
  299. //// will determine load status etc again
  300. //ConstImagePtrList imagePtrs;
  301. //for (size_t i=0 ; i<loadedImages->size() ; ++i) {
  302. // imagePtrs.push_back(&(*loadedImages)[i]);
  303. //}
  304. //_loadImages(imagePtrs);
  305. }
  306. //*************************************************************************
  307. void GLTexture::freeInternalResourcesImpl()
  308. {
  309. mSurfaceList.clear();
  310. glDeleteTextures( 1, &mTextureID );
  311. }
  312. //---------------------------------------------------------------------------------------------
  313. void GLTexture::_createSurfaceList()
  314. {
  315. mSurfaceList.clear();
  316. // For all faces and mipmaps, store surfaces as HardwarePixelBufferPtr
  317. bool wantGeneratedMips = (mUsage & TU_AUTOMIPMAP)!=0;
  318. // Do mipmapping in software? (uses GLU) For some cards, this is still needed. Of course,
  319. // only when mipmap generation is desired.
  320. bool doSoftware = wantGeneratedMips && !mMipmapsHardwareGenerated && getNumMipmaps();
  321. for(size_t face=0; face<getNumFaces(); face++)
  322. {
  323. for(size_t mip=0; mip<=getNumMipmaps(); mip++)
  324. {
  325. GLHardwarePixelBuffer *buf = new GLTextureBuffer("", getGLTextureTarget(), mTextureID, face, mip,
  326. static_cast<HardwareBuffer::Usage>(mUsage), doSoftware && mip==0, mHwGamma, mFSAA);
  327. mSurfaceList.push_back(HardwarePixelBufferPtr(buf));
  328. /// Check for error
  329. if(buf->getWidth()==0 || buf->getHeight()==0 || buf->getDepth()==0)
  330. {
  331. OGRE_EXCEPT(
  332. Exception::ERR_RENDERINGAPI_ERROR,
  333. "Zero sized texture surface on texture face "
  334. + StringConverter::toString(face)
  335. + " mipmap "+StringConverter::toString(mip)
  336. + ". Probably, the GL driver refused to create the texture.",
  337. "GLTexture::_createSurfaceList");
  338. }
  339. }
  340. }
  341. }
  342. //---------------------------------------------------------------------------------------------
  343. HardwarePixelBufferPtr GLTexture::getBuffer(size_t face, size_t mipmap)
  344. {
  345. if(face >= getNumFaces())
  346. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Face index out of range",
  347. "GLTexture::getBuffer");
  348. if(mipmap > mNumMipmaps)
  349. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Mipmap index out of range",
  350. "GLTexture::getBuffer");
  351. unsigned int idx = face*(mNumMipmaps+1) + mipmap;
  352. assert(idx < mSurfaceList.size());
  353. return mSurfaceList[idx];
  354. }
  355. //---------------------------------------------------------------------------------------------
  356. void GLTexture::getCustomAttribute(const String& name, void* pData)
  357. {
  358. if (name == "GLID")
  359. *static_cast<GLuint*>(pData) = mTextureID;
  360. }
  361. }