gfxGLCubemap.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. GFXGLCubemap::GFXGLCubemap() :
  32. mCubemap(0),
  33. mDynamicTexSize(0),
  34. mWidth(0),
  35. mHeight(0),
  36. mFaceFormat( GFXFormatR8G8B8A8 )
  37. {
  38. for(U32 i = 0; i < 6; i++)
  39. mTextures[i] = NULL;
  40. GFXTextureManager::addEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
  41. }
  42. GFXGLCubemap::~GFXGLCubemap()
  43. {
  44. glDeleteTextures(1, &mCubemap);
  45. GFXTextureManager::removeEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
  46. }
  47. GLenum GFXGLCubemap::getEnumForFaceNumber(U32 face)
  48. {
  49. return GFXGLFaceType[face];
  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(GFXGLFaceType[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, mipDataSize, buf);
  89. delete[] buf;
  90. }
  91. }
  92. else
  93. {
  94. U8* buf = glTex->getTextureData();
  95. glTexImage2D(GFXGLFaceType[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. mInitialized = true;
  114. }
  115. void GFXGLCubemap::initStatic( DDSFile *dds )
  116. {
  117. if(mCubemap)
  118. return;
  119. AssertFatal( dds, "GFXGLCubemap::initStatic - Got null DDS file!" );
  120. AssertFatal( dds->isCubemap(), "GFXGLCubemap::initStatic - Got non-cubemap DDS file!" );
  121. AssertFatal( dds->mSurfaces.size() == 6, "GFXGLCubemap::initStatic - DDS has less than 6 surfaces!" );
  122. mWidth = dds->getWidth();
  123. mHeight = dds->getHeight();
  124. mFaceFormat = dds->getFormat();
  125. mMipMapLevels = dds->getMipLevels();
  126. const bool isCompressed = ImageUtil::isCompressedFormat(mFaceFormat);
  127. glGenTextures(1, &mCubemap);
  128. PRESERVE_CUBEMAP_TEXTURE();
  129. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  130. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
  131. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  132. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  133. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  134. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  135. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  136. AssertFatal(mWidth == mHeight, "GFXGLCubemap::initStatic - Width and height must be equal!");
  137. for(U32 i = 0; i < 6; i++)
  138. {
  139. if ( !dds->mSurfaces[i] )
  140. {
  141. // TODO: The DDS can skip surfaces, but i'm unsure what i should
  142. // do here when creating the cubemap. Ignore it for now.
  143. continue;
  144. }
  145. // convert to Z up
  146. const U32 faceIndex = zUpFaceIndex(i);
  147. // Now loop thru the mip levels!
  148. for (U32 mip = 0; mip < mMipMapLevels; ++mip)
  149. {
  150. const U32 mipWidth = getMax( U32(1), mWidth >> mip );
  151. const U32 mipHeight = getMax( U32(1), mHeight >> mip );
  152. if (isCompressed)
  153. glCompressedTexImage2D(GFXGLFaceType[faceIndex], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, dds->getSurfaceSize(mip), dds->mSurfaces[i]->mMips[mip]);
  154. else
  155. glTexImage2D(GFXGLFaceType[faceIndex], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0,
  156. GFXGLTextureFormat[mFaceFormat], GFXGLTextureType[mFaceFormat], dds->mSurfaces[i]->mMips[mip]);
  157. }
  158. }
  159. mInitialized = true;
  160. }
  161. void GFXGLCubemap::initDynamic(U32 texSize, GFXFormat faceFormat, U32 mipLevels)
  162. {
  163. mDynamicTexSize = texSize;
  164. mFaceFormat = faceFormat;
  165. const bool isCompressed = ImageUtil::isCompressedFormat(faceFormat);
  166. mMipMapLevels = ImageUtil::getMaxMipCount( texSize, texSize);
  167. glGenTextures(1, &mCubemap);
  168. PRESERVE_CUBEMAP_TEXTURE();
  169. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  170. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
  171. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  172. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  173. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  174. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  175. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  176. mWidth = texSize;
  177. mHeight = texSize;
  178. for(U32 i = 0; i < 6; i++)
  179. {
  180. if( ImageUtil::isCompressedFormat(faceFormat) )
  181. {
  182. for( U32 mip = 0; mip < mMipMapLevels; ++mip )
  183. {
  184. const U32 mipSize = getMax( U32(1), texSize >> mip );
  185. const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, texSize, texSize, mip );
  186. glCompressedTexImage2D(GFXGLFaceType[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipSize, mipSize, 0, mipDataSize, NULL);
  187. }
  188. }
  189. else
  190. {
  191. glTexImage2D( GFXGLFaceType[i], 0, GFXGLTextureInternalFormat[faceFormat], texSize, texSize,
  192. 0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], NULL);
  193. }
  194. }
  195. if( !isCompressed && !mipLevels)
  196. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  197. mInitialized = true;
  198. }
  199. void GFXGLCubemap::zombify()
  200. {
  201. glDeleteTextures(1, &mCubemap);
  202. mCubemap = 0;
  203. }
  204. void GFXGLCubemap::resurrect()
  205. {
  206. // Handled in tmResurrect
  207. }
  208. void GFXGLCubemap::tmResurrect()
  209. {
  210. if(mDynamicTexSize)
  211. initDynamic(mDynamicTexSize,mFaceFormat);
  212. else
  213. {
  214. if ( mDDSFile )
  215. initStatic( mDDSFile );
  216. else
  217. initStatic( mTextures );
  218. }
  219. }
  220. void GFXGLCubemap::setToTexUnit(U32 tuNum)
  221. {
  222. static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapInternal(tuNum, this);
  223. }
  224. void GFXGLCubemap::bind(U32 textureUnit) const
  225. {
  226. glActiveTexture(GL_TEXTURE0 + textureUnit);
  227. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  228. static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP, mCubemap);
  229. GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
  230. AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
  231. if (!sb)
  232. return;
  233. const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
  234. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
  235. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  236. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
  237. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
  238. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  239. }
  240. void GFXGLCubemap::_onTextureEvent( GFXTexCallbackCode code )
  241. {
  242. if ( code == GFXZombify )
  243. zombify();
  244. else
  245. tmResurrect();
  246. }
  247. U8* GFXGLCubemap::getTextureData(U32 face, U32 mip)
  248. {
  249. AssertFatal(mMipMapLevels, "");
  250. mip = (mip < mMipMapLevels) ? mip : 0;
  251. const U32 bytesPerTexel = 8; //TODO make work with more formats!!!!!
  252. const U32 dataSize = ImageUtil::isCompressedFormat(mFaceFormat)
  253. ? getCompressedSurfaceSize(mFaceFormat, mWidth, mHeight, mip)
  254. : (mWidth >> mip) * (mHeight >> mip) * bytesPerTexel;
  255. U8* data = new U8[dataSize];
  256. PRESERVE_TEXTURE(GL_TEXTURE_CUBE_MAP);
  257. glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
  258. if (ImageUtil::isCompressedFormat(mFaceFormat))
  259. glGetCompressedTexImage(GFXGLFaceType[face], mip, data);
  260. else
  261. glGetTexImage(GFXGLFaceType[face], mip, GFXGLTextureFormat[mFaceFormat], GFXGLTextureType[mFaceFormat], data);
  262. return data;
  263. }
  264. //-----------------------------------------------------------------------------
  265. // Cubemap Array
  266. //-----------------------------------------------------------------------------
  267. GFXGLCubemapArray::GFXGLCubemapArray()
  268. {
  269. mCubemap = 0;
  270. }
  271. GFXGLCubemapArray::~GFXGLCubemapArray()
  272. {
  273. glDeleteTextures(1, &mCubemap);
  274. }
  275. //TODO: really need a common private 'init' function to avoid code double up with these init* functions
  276. void GFXGLCubemapArray::init(GFXCubemapHandle *cubemaps, const U32 cubemapCount)
  277. {
  278. AssertFatal(cubemaps, "GFXGLCubemapArray- Got null GFXCubemapHandle!");
  279. AssertFatal(*cubemaps, "GFXGLCubemapArray - Got empty cubemap!");
  280. setCubeTexSize(cubemaps);
  281. mFormat = cubemaps[0]->getFormat();
  282. mNumCubemaps = cubemapCount;
  283. const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
  284. glGenTextures(1, &mCubemap);
  285. PRESERVE_CUBEMAP_ARRAY_TEXTURE();
  286. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  287. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMin(mMipMapLevels - 1, 1));
  288. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  289. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  290. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  291. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  292. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  293. for (U32 i = 0; i < cubemapCount; i++)
  294. {
  295. GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
  296. //yes checking the first one(cubemap at index 0) is pointless but saves a further if statement
  297. if (cubemaps[i]->getSize() != mSize || cubemaps[i]->getFormat() != mFormat || cubemaps[i]->getMipMapLevels() != mMipMapLevels)
  298. {
  299. Con::printf("Trying to add an invalid Cubemap to a CubemapArray");
  300. //destroy array here first
  301. AssertFatal(false, "GFXD3D11CubemapArray::initStatic - invalid cubemap");
  302. }
  303. for (U32 face = 0; face < 6; face++)
  304. {
  305. for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
  306. {
  307. U8 *pixelData = glTex->getTextureData(face, currentMip);
  308. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  309. const U32 mipSize = getMax(U32(1), mSize >> currentMip);
  310. if (isCompressed)
  311. {
  312. const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
  313. glCompressedTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, i * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  314. }
  315. else
  316. {
  317. glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, i * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  318. }
  319. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
  320. delete[] pixelData;
  321. }
  322. }
  323. }
  324. }
  325. //Just allocate the cubemap array but we don't upload any data
  326. void GFXGLCubemapArray::init(const U32 cubemapCount, const U32 cubemapFaceSize, const GFXFormat format)
  327. {
  328. setCubeTexSize(cubemapFaceSize);
  329. mFormat = format;
  330. mNumCubemaps = cubemapCount;
  331. const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
  332. glGenTextures(1, &mCubemap);
  333. PRESERVE_CUBEMAP_ARRAY_TEXTURE();
  334. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  335. for (U32 i = 0; i < mMipMapLevels; i++)
  336. {
  337. const U32 mipSize = getMax(U32(1), mSize >> i);
  338. glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, i, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, cubemapCount * 6, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  339. }
  340. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
  341. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  342. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  343. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  344. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  345. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  346. }
  347. void GFXGLCubemapArray::updateTexture(const GFXCubemapHandle &cubemap, const U32 slot)
  348. {
  349. AssertFatal(slot <= mNumCubemaps, "GFXD3D11CubemapArray::updateTexture - trying to update a cubemap texture that is out of bounds!");
  350. if (!cubemap->isInitialized())
  351. return;
  352. const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
  353. GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemap.getPointer());
  354. for (U32 face = 0; face < 6; face++)
  355. {
  356. for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
  357. {
  358. U8 *pixelData = glTex->getTextureData(face, currentMip);
  359. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  360. const U32 mipSize = getMax(U32(1), mSize >> currentMip);
  361. if (isCompressed)
  362. {
  363. const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
  364. glCompressedTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, slot * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  365. }
  366. else
  367. {
  368. glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, slot * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  369. }
  370. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
  371. delete[] pixelData;
  372. }
  373. }
  374. }
  375. void GFXGLCubemapArray::copyTo(GFXCubemapArray *pDstCubemap)
  376. {
  377. AssertFatal(pDstCubemap, "GFXGLCubemapArray::copyTo - Got null GFXCubemapArray");
  378. const U32 dstCount = pDstCubemap->getNumCubemaps();
  379. const GFXFormat dstFmt = pDstCubemap->getFormat();
  380. const U32 dstSize = pDstCubemap->getSize();
  381. const U32 dstMips = pDstCubemap->getMipMapLevels();
  382. AssertFatal(dstCount > mNumCubemaps, "GFXGLCubemapArray::copyTo - Destination too small");
  383. AssertFatal(dstFmt == mFormat, "GFXGLCubemapArray::copyTo - Destination format doesn't match");
  384. AssertFatal(dstSize == mSize, "GFXGLCubemapArray::copyTo - Destination size doesn't match");
  385. AssertFatal(dstMips == mMipMapLevels, "GFXGLCubemapArray::copyTo - Destination mip levels doesn't match");
  386. GFXGLCubemapArray* pDstCube = static_cast<GFXGLCubemapArray*>(pDstCubemap);
  387. for (U32 cubeMap = 0; cubeMap < mNumCubemaps; cubeMap++)
  388. {
  389. for (U32 face = 0; face < CubeFaces; face++)
  390. {
  391. for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
  392. //U32 currentMip = 0;
  393. {
  394. //U8 *pixelData = pDstCube->get->getTextureData(face, currentMip);
  395. const U32 mipSize = getMax(U32(1), mSize >> currentMip);
  396. /*if (isCompressed)
  397. {
  398. const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
  399. glCompressedTexImage2D(GFXGLFaceType[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
  400. }
  401. else
  402. {*/ //TODO figure out xyzOffsets
  403. glCopyImageSubData(mCubemap, GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, cubeMap * face, pDstCube->mCubemap, GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, cubeMap * face, mipSize, mipSize, 6);
  404. //glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  405. //glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, 0, mipSize, mipSize, CubeFaces, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  406. //}
  407. //delete[] pixelData;
  408. }
  409. }
  410. }
  411. }
  412. void GFXGLCubemapArray::setToTexUnit(U32 tuNum)
  413. {
  414. static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapArrayInternal(tuNum, this);
  415. }
  416. void GFXGLCubemapArray::bind(U32 textureUnit) const
  417. {
  418. glActiveTexture(GL_TEXTURE0 + textureUnit);
  419. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  420. static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  421. GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
  422. AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
  423. if (!sb)
  424. return;
  425. const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
  426. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
  427. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  428. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
  429. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
  430. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  431. }