gfxGLTextureObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "console/console.h"
  23. #include "gfx/gl/tGL/tGL.h"
  24. #include "math/mRect.h"
  25. #include "gfx/gl/gfxGLTextureObject.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gl/gfxGLEnumTranslate.h"
  28. #include "gfx/gl/gfxGLTextureManager.h"
  29. #include "gfx/gl/gfxGLUtils.h"
  30. #include "gfx/gfxCardProfile.h"
  31. GFXGLTextureObject::GFXGLTextureObject(GFXDevice * aDevice, GFXTextureProfile *profile) :
  32. GFXTextureObject(aDevice, profile),
  33. mBinding(GL_TEXTURE_2D),
  34. mBytesPerTexel(4),
  35. mLockedRectRect(0, 0, 0, 0),
  36. mGLDevice(static_cast<GFXGLDevice*>(mDevice)),
  37. mZombieCache(NULL),
  38. mNeedInitSamplerState(true),
  39. mFrameAllocatorMark(0),
  40. mFrameAllocatorPtr(NULL)
  41. {
  42. AssertFatal(dynamic_cast<GFXGLDevice*>(mDevice), "GFXGLTextureObject::GFXGLTextureObject - Invalid device type, expected GFXGLDevice!");
  43. glGenTextures(1, &mHandle);
  44. glGenBuffers(1, &mBuffer);
  45. }
  46. GFXGLTextureObject::~GFXGLTextureObject()
  47. {
  48. glDeleteTextures(1, &mHandle);
  49. glDeleteBuffers(1, &mBuffer);
  50. delete[] mZombieCache;
  51. kill();
  52. }
  53. GFXLockedRect* GFXGLTextureObject::lock(U32 mipLevel, RectI *inRect)
  54. {
  55. AssertFatal(mBinding != GL_TEXTURE_3D, "GFXGLTextureObject::lock - We don't support locking 3D textures yet");
  56. U32 width = mTextureSize.x >> mipLevel;
  57. U32 height = mTextureSize.y >> mipLevel;
  58. if(inRect)
  59. {
  60. if((inRect->point.x + inRect->extent.x > width) || (inRect->point.y + inRect->extent.y > height))
  61. AssertFatal(false, "GFXGLTextureObject::lock - Rectangle too big!");
  62. mLockedRectRect = *inRect;
  63. }
  64. else
  65. {
  66. mLockedRectRect = RectI(0, 0, width, height);
  67. }
  68. mLockedRect.pitch = mLockedRectRect.extent.x * mBytesPerTexel;
  69. // CodeReview [ags 12/19/07] This one texel boundary is necessary to keep the clipmap code from crashing. Figure out why.
  70. U32 size = (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel;
  71. AssertFatal(!mFrameAllocatorMark && !mFrameAllocatorPtr, "");
  72. mFrameAllocatorMark = FrameAllocator::getWaterMark();
  73. mFrameAllocatorPtr = (U8*)FrameAllocator::alloc( size );
  74. mLockedRect.bits = mFrameAllocatorPtr;
  75. #if TORQUE_DEBUG
  76. mFrameAllocatorMarkGuard = FrameAllocator::getWaterMark();
  77. #endif
  78. if( !mLockedRect.bits )
  79. return NULL;
  80. return &mLockedRect;
  81. }
  82. void GFXGLTextureObject::unlock(U32 mipLevel)
  83. {
  84. if(!mLockedRect.bits)
  85. return;
  86. // I know this is in unlock, but in GL we actually do our submission in unlock.
  87. PROFILE_SCOPE(GFXGLTextureObject_lockRT);
  88. PRESERVE_TEXTURE(mBinding);
  89. glBindTexture(mBinding, mHandle);
  90. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
  91. glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, mFrameAllocatorPtr, GL_STREAM_DRAW);
  92. if(mBinding == GL_TEXTURE_2D)
  93. glTexSubImage2D(mBinding, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
  94. mLockedRectRect.extent.x, mLockedRectRect.extent.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  95. else if(mBinding == GL_TEXTURE_1D)
  96. glTexSubImage1D(mBinding, mipLevel, (mLockedRectRect.point.x > 1 ? mLockedRectRect.point.x : mLockedRectRect.point.y),
  97. (mLockedRectRect.extent.x > 1 ? mLockedRectRect.extent.x : mLockedRectRect.extent.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  98. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  99. mLockedRect.bits = NULL;
  100. #if TORQUE_DEBUG
  101. AssertFatal(mFrameAllocatorMarkGuard == FrameAllocator::getWaterMark(), "");
  102. #endif
  103. FrameAllocator::setWaterMark(mFrameAllocatorMark);
  104. mFrameAllocatorMark = 0;
  105. mFrameAllocatorPtr = NULL;
  106. }
  107. void GFXGLTextureObject::release()
  108. {
  109. glDeleteTextures(1, &mHandle);
  110. glDeleteBuffers(1, &mBuffer);
  111. mHandle = 0;
  112. mBuffer = 0;
  113. }
  114. void GFXGLTextureObject::reInit()
  115. {
  116. AssertFatal(!mHandle && !mBuffer,"Must release before reInit");
  117. glGenTextures(1, &mHandle);
  118. glGenBuffers(1, &mBuffer);
  119. }
  120. bool GFXGLTextureObject::copyToBmp(GBitmap * bmp)
  121. {
  122. if (!bmp)
  123. return false;
  124. // check format limitations
  125. // at the moment we only support RGBA for the source (other 4 byte formats should
  126. // be easy to add though)
  127. AssertFatal(mFormat == GFXFormatR8G8B8A8, "GFXGLTextureObject::copyToBmp - invalid format");
  128. AssertFatal(bmp->getFormat() == GFXFormatR8G8B8A8 || bmp->getFormat() == GFXFormatR8G8B8, "GFXGLTextureObject::copyToBmp - invalid format");
  129. if(mFormat != GFXFormatR8G8B8A8)
  130. return false;
  131. if(bmp->getFormat() != GFXFormatR8G8B8A8 && bmp->getFormat() != GFXFormatR8G8B8)
  132. return false;
  133. AssertFatal(bmp->getWidth() == getWidth(), "GFXGLTextureObject::copyToBmp - invalid size");
  134. AssertFatal(bmp->getHeight() == getHeight(), "GFXGLTextureObject::copyToBmp - invalid size");
  135. PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
  136. PRESERVE_TEXTURE(mBinding);
  137. glBindTexture(mBinding, mHandle);
  138. U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() );
  139. U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat );
  140. if(dstBytesPerPixel == srcBytesPerPixel)
  141. {
  142. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], bmp->getWritableBits());
  143. return true;
  144. }
  145. FrameAllocatorMarker mem;
  146. U32 srcPixelCount = mTextureSize.x * mTextureSize.y;
  147. U8 *dest = bmp->getWritableBits();
  148. U8 *orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel);
  149. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig);
  150. PROFILE_START(GFXGLTextureObject_copyToBmp_pixCopy);
  151. for(int i = 0; i < srcPixelCount; ++i)
  152. {
  153. dest[0] = orig[0];
  154. dest[1] = orig[1];
  155. dest[2] = orig[2];
  156. if(dstBytesPerPixel == 4)
  157. dest[3] = orig[3];
  158. orig += srcBytesPerPixel;
  159. dest += dstBytesPerPixel;
  160. }
  161. PROFILE_END();
  162. return true;
  163. }
  164. void GFXGLTextureObject::initSamplerState(const GFXSamplerStateDesc &ssd)
  165. {
  166. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  167. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  168. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  169. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  170. if(mBinding == GL_TEXTURE_3D)
  171. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  172. if(static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  173. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  174. mNeedInitSamplerState = false;
  175. mSampler = ssd;
  176. }
  177. void GFXGLTextureObject::bind(U32 textureUnit)
  178. {
  179. glActiveTexture(GL_TEXTURE0 + textureUnit);
  180. glBindTexture(mBinding, mHandle);
  181. GFXGL->getOpenglCache()->setCacheBindedTex(textureUnit, mBinding, mHandle);
  182. if(GFXGL->mCapabilities.samplerObjects)
  183. return;
  184. GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
  185. AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
  186. if (!sb)
  187. return;
  188. const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
  189. if(mNeedInitSamplerState)
  190. {
  191. initSamplerState(ssd);
  192. return;
  193. }
  194. if(mSampler.minFilter != ssd.minFilter || mSampler.mipFilter != ssd.mipFilter)
  195. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  196. if(mSampler.magFilter != ssd.magFilter)
  197. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  198. if(mSampler.addressModeU != ssd.addressModeU)
  199. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  200. if(mSampler.addressModeV != ssd.addressModeV)
  201. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  202. if(mBinding == GL_TEXTURE_3D && mSampler.addressModeW != ssd.addressModeW )
  203. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  204. if(mSampler.maxAnisotropy != ssd.maxAnisotropy && static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  205. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  206. mSampler = ssd;
  207. }
  208. U8* GFXGLTextureObject::getTextureData( U32 mip )
  209. {
  210. AssertFatal( mMipLevels, "");
  211. mip = (mip < mMipLevels) ? mip : 0;
  212. const U32 dataSize = isCompressedFormat(mFormat)
  213. ? getCompressedSurfaceSize( mFormat, mTextureSize.x, mTextureSize.y, mip )
  214. : (mTextureSize.x >> mip) * (mTextureSize.y >> mip) * mBytesPerTexel;
  215. U8* data = new U8[dataSize];
  216. PRESERVE_TEXTURE(mBinding);
  217. glBindTexture(mBinding, mHandle);
  218. if( isCompressedFormat(mFormat) )
  219. glGetCompressedTexImage( mBinding, mip, data );
  220. else
  221. glGetTexImage(mBinding, mip, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], data);
  222. return data;
  223. }
  224. void GFXGLTextureObject::copyIntoCache()
  225. {
  226. PRESERVE_TEXTURE(mBinding);
  227. glBindTexture(mBinding, mHandle);
  228. U32 cacheSize = mTextureSize.x * mTextureSize.y;
  229. if(mBinding == GL_TEXTURE_3D)
  230. cacheSize *= mTextureSize.z;
  231. cacheSize *= mBytesPerTexel;
  232. mZombieCache = new U8[cacheSize];
  233. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  234. }
  235. void GFXGLTextureObject::reloadFromCache()
  236. {
  237. if(!mZombieCache)
  238. return;
  239. if(mBinding == GL_TEXTURE_3D)
  240. {
  241. static_cast<GFXGLTextureManager*>(TEXMGR)->_loadTexture(this, mZombieCache);
  242. delete[] mZombieCache;
  243. mZombieCache = NULL;
  244. return;
  245. }
  246. PRESERVE_TEXTURE(mBinding);
  247. glBindTexture(mBinding, mHandle);
  248. if(mBinding == GL_TEXTURE_2D)
  249. glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  250. else if(mBinding == GL_TEXTURE_1D)
  251. glTexSubImage1D(mBinding, 0, 0, (mTextureSize.x > 1 ? mTextureSize.x : mTextureSize.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  252. if(mMipLevels != 1)
  253. glGenerateMipmap(mBinding);
  254. delete[] mZombieCache;
  255. mZombieCache = NULL;
  256. mIsZombie = false;
  257. }
  258. void GFXGLTextureObject::zombify()
  259. {
  260. if(mIsZombie)
  261. return;
  262. mIsZombie = true;
  263. if(!mProfile->doStoreBitmap() && !mProfile->isRenderTarget() && !mProfile->isDynamic() && !mProfile->isZTarget())
  264. copyIntoCache();
  265. release();
  266. }
  267. void GFXGLTextureObject::resurrect()
  268. {
  269. if(!mIsZombie)
  270. return;
  271. glGenTextures(1, &mHandle);
  272. glGenBuffers(1, &mBuffer);
  273. }
  274. F32 GFXGLTextureObject::getMaxUCoord() const
  275. {
  276. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getWidth();
  277. }
  278. F32 GFXGLTextureObject::getMaxVCoord() const
  279. {
  280. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getHeight();
  281. }
  282. const String GFXGLTextureObject::describeSelf() const
  283. {
  284. String ret = Parent::describeSelf();
  285. ret += String::ToString(" GL Handle: %i", mHandle);
  286. return ret;
  287. }