gfxGLCubemap.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gfx/gl/gfxGLDevice.h"
  23. #include "gfx/gl/gfxGLTextureObject.h"
  24. #include "gfx/gl/gfxGLEnumTranslate.h"
  25. #include "gfx/gl/gfxGLUtils.h"
  26. #include "gfx/gl/gfxGLCubemap.h"
  27. #include "gfx/gfxTextureManager.h"
  28. #include "gfx/gfxCardProfile.h"
  29. #include "gfx/bitmap/ddsFile.h"
  30. #include "gfx/bitmap/imageUtils.h"
  31. GLenum GFXGLCubemap::faceList[6] =
  32. {
  33. GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  34. GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  35. GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
  36. };
  37. GFXGLCubemap::GFXGLCubemap() :
  38. mCubemap(0),
  39. mDynamicTexSize(0),
  40. mFaceFormat( GFXFormatR8G8B8A8 )
  41. {
  42. for(U32 i = 0; i < 6; i++)
  43. mTextures[i] = NULL;
  44. GFXTextureManager::addEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
  45. }
  46. GFXGLCubemap::~GFXGLCubemap()
  47. {
  48. glDeleteTextures(1, &mCubemap);
  49. GFXTextureManager::removeEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
  50. }
  51. void GFXGLCubemap::fillCubeTextures(GFXTexHandle* faces)
  52. {
  53. AssertFatal( faces, "");
  54. AssertFatal( faces[0]->mMipLevels > 0, "");
  55. PRESERVE_CUBEMAP_TEXTURE();
  56. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  57. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, faces[0]->mMipLevels - 1 );
  58. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  59. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  60. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  61. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  62. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  63. U32 reqWidth = faces[0]->getWidth();
  64. U32 reqHeight = faces[0]->getHeight();
  65. GFXFormat regFaceFormat = faces[0]->getFormat();
  66. const bool isCompressed = ImageUtil::isCompressedFormat(regFaceFormat);
  67. mWidth = reqWidth;
  68. mHeight = reqHeight;
  69. mFaceFormat = regFaceFormat;
  70. mMipMapLevels = getMax( (U32)1, faces[0]->mMipLevels);
  71. AssertFatal(reqWidth == reqHeight, "GFXGLCubemap::fillCubeTextures - Width and height must be equal!");
  72. for(U32 i = 0; i < 6; i++)
  73. {
  74. AssertFatal(faces[i], avar("GFXGLCubemap::fillCubeFaces - texture %i is NULL!", i));
  75. AssertFatal((faces[i]->getWidth() == reqWidth) && (faces[i]->getHeight() == reqHeight), "GFXGLCubemap::fillCubeFaces - All textures must have identical dimensions!");
  76. AssertFatal(faces[i]->getFormat() == regFaceFormat, "GFXGLCubemap::fillCubeFaces - All textures must have identical formats!");
  77. mTextures[i] = faces[i];
  78. GFXFormat faceFormat = faces[i]->getFormat();
  79. GFXGLTextureObject* glTex = static_cast<GFXGLTextureObject*>(faces[i].getPointer());
  80. if( isCompressed )
  81. {
  82. for( U32 mip = 0; mip < mMipMapLevels; ++mip )
  83. {
  84. const U32 mipWidth = getMax( U32(1), faces[i]->getWidth() >> mip );
  85. const U32 mipHeight = getMax( U32(1), faces[i]->getHeight() >> mip );
  86. const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, mWidth, mHeight, mip );
  87. U8* buf = glTex->getTextureData( mip );
  88. glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, mipDataSize, buf);
  89. delete[] buf;
  90. }
  91. }
  92. else
  93. {
  94. U8* buf = glTex->getTextureData();
  95. glTexImage2D(faceList[i], 0, GFXGLTextureInternalFormat[faceFormat], mWidth, mHeight,
  96. 0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], buf);
  97. delete[] buf;
  98. }
  99. }
  100. if( !isCompressed )
  101. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  102. }
  103. void GFXGLCubemap::initStatic(GFXTexHandle* faces)
  104. {
  105. if(mCubemap)
  106. return;
  107. if(faces)
  108. {
  109. AssertFatal(faces[0], "GFXGLCubemap::initStatic - empty texture passed");
  110. glGenTextures(1, &mCubemap);
  111. fillCubeTextures(faces);
  112. }
  113. }
  114. void GFXGLCubemap::initStatic( DDSFile *dds )
  115. {
  116. if(mCubemap)
  117. return;
  118. AssertFatal( dds, "GFXGLCubemap::initStatic - Got null DDS file!" );
  119. AssertFatal( dds->isCubemap(), "GFXGLCubemap::initStatic - Got non-cubemap DDS file!" );
  120. AssertFatal( dds->mSurfaces.size() == 6, "GFXGLCubemap::initStatic - DDS has less than 6 surfaces!" );
  121. mWidth = dds->getWidth();
  122. mHeight = dds->getHeight();
  123. mFaceFormat = dds->getFormat();
  124. mMipMapLevels = dds->getMipLevels();
  125. const bool isCompressed = ImageUtil::isCompressedFormat(mFaceFormat);
  126. glGenTextures(1, &mCubemap);
  127. PRESERVE_CUBEMAP_TEXTURE();
  128. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  129. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
  130. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  131. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  132. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  133. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  134. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  135. AssertFatal(mWidth == mHeight, "GFXGLCubemap::initStatic - Width and height must be equal!");
  136. for(U32 i = 0; i < 6; i++)
  137. {
  138. if ( !dds->mSurfaces[i] )
  139. {
  140. // TODO: The DDS can skip surfaces, but i'm unsure what i should
  141. // do here when creating the cubemap. Ignore it for now.
  142. continue;
  143. }
  144. // convert to Z up
  145. const U32 faceIndex = _zUpFaceIndex(i);
  146. // Now loop thru the mip levels!
  147. for (U32 mip = 0; mip < mMipMapLevels; ++mip)
  148. {
  149. const U32 mipWidth = getMax( U32(1), mWidth >> mip );
  150. const U32 mipHeight = getMax( U32(1), mHeight >> mip );
  151. if (isCompressed)
  152. glCompressedTexImage2D(faceList[faceIndex], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, dds->getSurfaceSize(mip), dds->mSurfaces[i]->mMips[mip]);
  153. else
  154. glTexImage2D(faceList[faceIndex], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0,
  155. GFXGLTextureFormat[mFaceFormat], GFXGLTextureType[mFaceFormat], dds->mSurfaces[i]->mMips[mip]);
  156. }
  157. }
  158. }
  159. void GFXGLCubemap::initDynamic(U32 texSize, GFXFormat faceFormat)
  160. {
  161. mDynamicTexSize = texSize;
  162. mFaceFormat = faceFormat;
  163. const bool isCompressed = ImageUtil::isCompressedFormat(faceFormat);
  164. mMipMapLevels = getMax( (U32)1, getMaxMipmaps( texSize, texSize, 1 ) );
  165. glGenTextures(1, &mCubemap);
  166. PRESERVE_CUBEMAP_TEXTURE();
  167. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  168. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
  169. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  170. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  171. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  172. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  173. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  174. mWidth = texSize;
  175. mHeight = texSize;
  176. for(U32 i = 0; i < 6; i++)
  177. {
  178. if( ImageUtil::isCompressedFormat(faceFormat) )
  179. {
  180. for( U32 mip = 0; mip < mMipMapLevels; ++mip )
  181. {
  182. const U32 mipSize = getMax( U32(1), texSize >> mip );
  183. const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, texSize, texSize, mip );
  184. glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipSize, mipSize, 0, mipDataSize, NULL);
  185. }
  186. }
  187. else
  188. {
  189. glTexImage2D( faceList[i], 0, GFXGLTextureInternalFormat[faceFormat], texSize, texSize,
  190. 0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], NULL);
  191. }
  192. }
  193. if( !isCompressed )
  194. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  195. }
  196. void GFXGLCubemap::zombify()
  197. {
  198. glDeleteTextures(1, &mCubemap);
  199. mCubemap = 0;
  200. }
  201. void GFXGLCubemap::resurrect()
  202. {
  203. // Handled in tmResurrect
  204. }
  205. void GFXGLCubemap::tmResurrect()
  206. {
  207. if(mDynamicTexSize)
  208. initDynamic(mDynamicTexSize,mFaceFormat);
  209. else
  210. {
  211. if ( mDDSFile )
  212. initStatic( mDDSFile );
  213. else
  214. initStatic( mTextures );
  215. }
  216. }
  217. void GFXGLCubemap::setToTexUnit(U32 tuNum)
  218. {
  219. static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapInternal(tuNum, this);
  220. }
  221. void GFXGLCubemap::bind(U32 textureUnit) const
  222. {
  223. glActiveTexture(GL_TEXTURE0 + textureUnit);
  224. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  225. static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP, mCubemap);
  226. GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
  227. AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
  228. if (!sb)
  229. return;
  230. const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
  231. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
  232. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  233. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
  234. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
  235. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  236. }
  237. void GFXGLCubemap::_onTextureEvent( GFXTexCallbackCode code )
  238. {
  239. if ( code == GFXZombify )
  240. zombify();
  241. else
  242. tmResurrect();
  243. }