gfxGLCubemap.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. U32 downscalePower = GFXTextureManager::smTextureReductionLevel;
  281. U32 scaledSize = cubemaps[0]->getSize();
  282. if (downscalePower != 0)
  283. {
  284. // Otherwise apply the appropriate scale...
  285. scaledSize >>= downscalePower;
  286. }
  287. //all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
  288. mSize = scaledSize;
  289. mFormat = cubemaps[0]->getFormat();
  290. mMipMapLevels = cubemaps[0]->getMipMapLevels() - downscalePower;
  291. mNumCubemaps = cubemapCount;
  292. const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
  293. glGenTextures(1, &mCubemap);
  294. PRESERVE_CUBEMAP_ARRAY_TEXTURE();
  295. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  296. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMin(mMipMapLevels - 1, 1));
  297. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  298. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  299. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  300. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  301. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  302. for (U32 i = 0; i < cubemapCount; i++)
  303. {
  304. GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
  305. for (U32 face = 0; face < 6; face++)
  306. {
  307. for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
  308. {
  309. U8 *pixelData = glTex->getTextureData(face, currentMip);
  310. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  311. const U32 mipSize = getMax(U32(1), mSize >> currentMip);
  312. if (isCompressed)
  313. {
  314. const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
  315. glCompressedTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, i * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  316. }
  317. else
  318. {
  319. glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, i * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  320. }
  321. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
  322. delete[] pixelData;
  323. }
  324. }
  325. }
  326. }
  327. //Just allocate the cubemap array but we don't upload any data
  328. void GFXGLCubemapArray::init(const U32 cubemapCount, const U32 cubemapFaceSize, const GFXFormat format)
  329. {
  330. U32 downscalePower = GFXTextureManager::smTextureReductionLevel;
  331. U32 scaledSize = cubemapFaceSize;
  332. if (downscalePower != 0)
  333. {
  334. // Otherwise apply the appropriate scale...
  335. scaledSize >>= downscalePower;
  336. }
  337. //all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
  338. mSize = scaledSize;
  339. mFormat = format;
  340. mMipMapLevels = ImageUtil::getMaxMipCount(scaledSize, scaledSize);
  341. mNumCubemaps = cubemapCount;
  342. const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
  343. glGenTextures(1, &mCubemap);
  344. PRESERVE_CUBEMAP_ARRAY_TEXTURE();
  345. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  346. for (U32 i = 0; i < mMipMapLevels; i++)
  347. {
  348. const U32 mipSize = getMax(U32(1), mSize >> i);
  349. glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, i, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, cubemapCount * 6, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  350. }
  351. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
  352. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  353. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  354. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  355. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  356. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  357. }
  358. void GFXGLCubemapArray::updateTexture(const GFXCubemapHandle &cubemap, const U32 slot)
  359. {
  360. AssertFatal(slot <= mNumCubemaps, "GFXD3D11CubemapArray::updateTexture - trying to update a cubemap texture that is out of bounds!");
  361. if (!cubemap->isInitialized())
  362. return;
  363. const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
  364. GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemap.getPointer());
  365. for (U32 face = 0; face < 6; face++)
  366. {
  367. for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
  368. {
  369. U8 *pixelData = glTex->getTextureData(face, currentMip);
  370. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  371. const U32 mipSize = getMax(U32(1), mSize >> currentMip);
  372. if (isCompressed)
  373. {
  374. const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
  375. glCompressedTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, slot * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  376. }
  377. else
  378. {
  379. glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, slot * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  380. }
  381. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
  382. delete[] pixelData;
  383. }
  384. }
  385. }
  386. void GFXGLCubemapArray::copyTo(GFXCubemapArray *pDstCubemap)
  387. {
  388. AssertFatal(pDstCubemap, "GFXGLCubemapArray::copyTo - Got null GFXCubemapArray");
  389. const U32 dstCount = pDstCubemap->getNumCubemaps();
  390. const GFXFormat dstFmt = pDstCubemap->getFormat();
  391. const U32 dstSize = pDstCubemap->getSize();
  392. const U32 dstMips = pDstCubemap->getMipMapLevels();
  393. AssertFatal(dstCount > mNumCubemaps, "GFXGLCubemapArray::copyTo - Destination too small");
  394. AssertFatal(dstFmt == mFormat, "GFXGLCubemapArray::copyTo - Destination format doesn't match");
  395. AssertFatal(dstSize == mSize, "GFXGLCubemapArray::copyTo - Destination size doesn't match");
  396. AssertFatal(dstMips == mMipMapLevels, "GFXGLCubemapArray::copyTo - Destination mip levels doesn't match");
  397. GFXGLCubemapArray* pDstCube = static_cast<GFXGLCubemapArray*>(pDstCubemap);
  398. for (U32 cubeMap = 0; cubeMap < mNumCubemaps; cubeMap++)
  399. {
  400. for (U32 face = 0; face < CubeFaces; face++)
  401. {
  402. for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
  403. //U32 currentMip = 0;
  404. {
  405. //U8 *pixelData = pDstCube->get->getTextureData(face, currentMip);
  406. const U32 mipSize = getMax(U32(1), mSize >> currentMip);
  407. /*if (isCompressed)
  408. {
  409. const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
  410. glCompressedTexImage2D(GFXGLFaceType[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
  411. }
  412. else
  413. {*/ //TODO figure out xyzOffsets
  414. 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);
  415. //glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  416. //glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, 0, mipSize, mipSize, CubeFaces, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
  417. //}
  418. //delete[] pixelData;
  419. }
  420. }
  421. }
  422. }
  423. void GFXGLCubemapArray::setToTexUnit(U32 tuNum)
  424. {
  425. static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapArrayInternal(tuNum, this);
  426. }
  427. void GFXGLCubemapArray::bind(U32 textureUnit) const
  428. {
  429. glActiveTexture(GL_TEXTURE0 + textureUnit);
  430. glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  431. static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
  432. GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
  433. AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
  434. if (!sb)
  435. return;
  436. const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
  437. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
  438. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  439. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
  440. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
  441. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  442. }