gfxGLCubemap.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. GLenum GFXGLCubemap::faceList[6] =
  31. {
  32. GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  33. GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  34. GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
  35. };
  36. GFXGLCubemap::GFXGLCubemap() :
  37. mCubemap(0),
  38. mDynamicTexSize(0),
  39. mFaceFormat( GFXFormatR8G8B8A8 )
  40. {
  41. for(U32 i = 0; i < 6; i++)
  42. mTextures[i] = NULL;
  43. GFXTextureManager::addEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
  44. }
  45. GFXGLCubemap::~GFXGLCubemap()
  46. {
  47. glDeleteTextures(1, &mCubemap);
  48. GFXTextureManager::removeEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
  49. }
  50. void GFXGLCubemap::fillCubeTextures(GFXTexHandle* faces)
  51. {
  52. AssertFatal( faces, "");
  53. AssertFatal( faces[0]->mMipLevels > 0, "");
  54. PRESERVE_CUBEMAP_TEXTURE();
  55. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  56. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, faces[0]->mMipLevels - 1 );
  57. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  58. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  59. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  60. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  61. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  62. U32 reqWidth = faces[0]->getWidth();
  63. U32 reqHeight = faces[0]->getHeight();
  64. GFXFormat regFaceFormat = faces[0]->getFormat();
  65. const bool isCompressed = isCompressedFormat(regFaceFormat);
  66. mWidth = reqWidth;
  67. mHeight = reqHeight;
  68. mFaceFormat = regFaceFormat;
  69. mMipLevels = getMax( (U32)1, faces[0]->mMipLevels);
  70. AssertFatal(reqWidth == reqHeight, "GFXGLCubemap::fillCubeTextures - Width and height must be equal!");
  71. for(U32 i = 0; i < 6; i++)
  72. {
  73. AssertFatal(faces[i], avar("GFXGLCubemap::fillCubeFaces - texture %i is NULL!", i));
  74. AssertFatal((faces[i]->getWidth() == reqWidth) && (faces[i]->getHeight() == reqHeight), "GFXGLCubemap::fillCubeFaces - All textures must have identical dimensions!");
  75. AssertFatal(faces[i]->getFormat() == regFaceFormat, "GFXGLCubemap::fillCubeFaces - All textures must have identical formats!");
  76. mTextures[i] = faces[i];
  77. GFXFormat faceFormat = faces[i]->getFormat();
  78. GFXGLTextureObject* glTex = static_cast<GFXGLTextureObject*>(faces[i].getPointer());
  79. if( isCompressed )
  80. {
  81. for( U32 mip = 0; mip < mMipLevels; ++mip )
  82. {
  83. const U32 mipWidth = getMax( U32(1), faces[i]->getWidth() >> mip );
  84. const U32 mipHeight = getMax( U32(1), faces[i]->getHeight() >> mip );
  85. const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, mWidth, mHeight, mip );
  86. U8* buf = glTex->getTextureData( mip );
  87. glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, mipDataSize, buf);
  88. delete[] buf;
  89. }
  90. }
  91. else
  92. {
  93. U8* buf = glTex->getTextureData();
  94. glTexImage2D(faceList[i], 0, GFXGLTextureInternalFormat[faceFormat], mWidth, mHeight,
  95. 0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], buf);
  96. delete[] buf;
  97. }
  98. }
  99. if( !isCompressed )
  100. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  101. }
  102. void GFXGLCubemap::initStatic(GFXTexHandle* faces)
  103. {
  104. if(mCubemap)
  105. return;
  106. if(faces)
  107. {
  108. AssertFatal(faces[0], "GFXGLCubemap::initStatic - empty texture passed");
  109. glGenTextures(1, &mCubemap);
  110. fillCubeTextures(faces);
  111. }
  112. }
  113. void GFXGLCubemap::initStatic( DDSFile *dds )
  114. {
  115. if(mCubemap)
  116. return;
  117. AssertFatal( dds, "GFXGLCubemap::initStatic - Got null DDS file!" );
  118. AssertFatal( dds->isCubemap(), "GFXGLCubemap::initStatic - Got non-cubemap DDS file!" );
  119. AssertFatal( dds->mSurfaces.size() == 6, "GFXGLCubemap::initStatic - DDS has less than 6 surfaces!" );
  120. // HACK: I cannot put the genie back in the bottle and assign a
  121. // DDSFile pointer back to a Resource<>.
  122. //
  123. // So we do a second lookup which works out ok for now, but shows
  124. // the weakness in the ResourceManager not having a common base
  125. // reference type.
  126. //
  127. mDDSFile = ResourceManager::get().load( dds->getSourcePath() );
  128. AssertFatal( mDDSFile == dds, "GFXGLCubemap::initStatic - Couldn't find DDSFile resource!" );
  129. mWidth = dds->getWidth();
  130. mHeight = dds->getHeight();
  131. mFaceFormat = dds->getFormat();
  132. mMipLevels = dds->getMipLevels();
  133. glGenTextures(1, &mCubemap);
  134. PRESERVE_CUBEMAP_TEXTURE();
  135. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  136. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipLevels - 1);
  137. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  138. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  139. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  140. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  141. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  142. AssertFatal(mWidth == mHeight, "GFXGLCubemap::initStatic - Width and height must be equal!");
  143. for(U32 i = 0; i < 6; i++)
  144. {
  145. if ( !dds->mSurfaces[i] )
  146. {
  147. // TODO: The DDS can skip surfaces, but i'm unsure what i should
  148. // do here when creating the cubemap. Ignore it for now.
  149. continue;
  150. }
  151. // Now loop thru the mip levels!
  152. for (U32 mip = 0; mip < mMipLevels; ++mip)
  153. {
  154. const U32 mipWidth = getMax( U32(1), mWidth >> mip );
  155. const U32 mipHeight = getMax( U32(1), mHeight >> mip );
  156. glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, dds->getSurfaceSize(mip), dds->mSurfaces[i]->mMips[mip]);
  157. }
  158. }
  159. }
  160. void GFXGLCubemap::initDynamic(U32 texSize, GFXFormat faceFormat)
  161. {
  162. mDynamicTexSize = texSize;
  163. mFaceFormat = faceFormat;
  164. const bool isCompressed = isCompressedFormat(faceFormat);
  165. mMipLevels = getMax( (U32)1, getMaxMipmaps( texSize, texSize, 1 ) );
  166. glGenTextures(1, &mCubemap);
  167. PRESERVE_CUBEMAP_TEXTURE();
  168. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  169. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipLevels - 1);
  170. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  171. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  172. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  173. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  174. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  175. mWidth = texSize;
  176. mHeight = texSize;
  177. for(U32 i = 0; i < 6; i++)
  178. {
  179. if( isCompressedFormat(faceFormat) )
  180. {
  181. for( U32 mip = 0; mip < mMipLevels; ++mip )
  182. {
  183. const U32 mipSize = getMax( U32(1), texSize >> mip );
  184. const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, texSize, texSize, mip );
  185. glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipSize, mipSize, 0, mipDataSize, NULL);
  186. }
  187. }
  188. else
  189. {
  190. glTexImage2D( faceList[i], 0, GFXGLTextureInternalFormat[faceFormat], texSize, texSize,
  191. 0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], NULL);
  192. }
  193. }
  194. if( !isCompressed )
  195. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  196. }
  197. void GFXGLCubemap::zombify()
  198. {
  199. glDeleteTextures(1, &mCubemap);
  200. mCubemap = 0;
  201. }
  202. void GFXGLCubemap::resurrect()
  203. {
  204. // Handled in tmResurrect
  205. }
  206. void GFXGLCubemap::tmResurrect()
  207. {
  208. if(mDynamicTexSize)
  209. initDynamic(mDynamicTexSize,mFaceFormat);
  210. else
  211. {
  212. if ( mDDSFile )
  213. initStatic( mDDSFile );
  214. else
  215. initStatic( mTextures );
  216. }
  217. }
  218. void GFXGLCubemap::setToTexUnit(U32 tuNum)
  219. {
  220. static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapInternal(tuNum, this);
  221. }
  222. void GFXGLCubemap::bind(U32 textureUnit) const
  223. {
  224. glActiveTexture(GL_TEXTURE0 + textureUnit);
  225. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  226. static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP, mCubemap);
  227. GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
  228. AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
  229. if (!sb)
  230. return;
  231. const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
  232. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
  233. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  234. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
  235. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
  236. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  237. }
  238. void GFXGLCubemap::_onTextureEvent( GFXTexCallbackCode code )
  239. {
  240. if ( code == GFXZombify )
  241. zombify();
  242. else
  243. tmResurrect();
  244. }