gfxGLTextureObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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) * getDepth() * 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, mBuffer);
  91. glBufferData(GL_PIXEL_UNPACK_BUFFER, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, mFrameAllocatorPtr, GL_STREAM_DRAW);
  92. S32 z = getDepth();
  93. if (mBinding == GL_TEXTURE_3D)
  94. glTexSubImage3D(mBinding, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y, z,
  95. mLockedRectRect.extent.x, mLockedRectRect.extent.y, z, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  96. else if(mBinding == GL_TEXTURE_2D)
  97. glTexSubImage2D(mBinding, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
  98. mLockedRectRect.extent.x, mLockedRectRect.extent.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  99. else if(mBinding == GL_TEXTURE_1D)
  100. glTexSubImage1D(mBinding, mipLevel, (mLockedRectRect.point.x > 1 ? mLockedRectRect.point.x : mLockedRectRect.point.y),
  101. (mLockedRectRect.extent.x > 1 ? mLockedRectRect.extent.x : mLockedRectRect.extent.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
  102. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  103. mLockedRect.bits = NULL;
  104. #if TORQUE_DEBUG
  105. AssertFatal(mFrameAllocatorMarkGuard == FrameAllocator::getWaterMark(), "");
  106. #endif
  107. FrameAllocator::setWaterMark(mFrameAllocatorMark);
  108. mFrameAllocatorMark = 0;
  109. mFrameAllocatorPtr = NULL;
  110. }
  111. void GFXGLTextureObject::release()
  112. {
  113. glDeleteTextures(1, &mHandle);
  114. glDeleteBuffers(1, &mBuffer);
  115. mHandle = 0;
  116. mBuffer = 0;
  117. }
  118. void GFXGLTextureObject::reInit()
  119. {
  120. AssertFatal(!mHandle && !mBuffer,"Must release before reInit");
  121. glGenTextures(1, &mHandle);
  122. glGenBuffers(1, &mBuffer);
  123. }
  124. bool GFXGLTextureObject::copyToBmp(GBitmap * bmp)
  125. {
  126. if (!bmp)
  127. return false;
  128. // check format limitations
  129. // at the moment we only support RGBA for the source (other 4 byte formats should
  130. // be easy to add though)
  131. AssertFatal(mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_SRGB , "GFXGLTextureObject::copyToBmp - invalid format");
  132. AssertFatal(bmp->getFormat() == GFXFormatR8G8B8A8 || bmp->getFormat() == GFXFormatR8G8B8 || bmp->getFormat() == GFXFormatR8G8B8A8_SRGB, "GFXGLTextureObject::copyToBmp - invalid format");
  133. if(mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_SRGB)
  134. return false;
  135. if(bmp->getFormat() != GFXFormatR8G8B8A8 && bmp->getFormat() != GFXFormatR8G8B8 && bmp->getFormat() != GFXFormatR8G8B8A8_SRGB )
  136. return false;
  137. AssertFatal(bmp->getWidth() == getWidth(), "GFXGLTextureObject::copyToBmp - invalid size");
  138. AssertFatal(bmp->getHeight() == getHeight(), "GFXGLTextureObject::copyToBmp - invalid size");
  139. PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
  140. PRESERVE_TEXTURE(mBinding);
  141. glBindTexture(mBinding, mHandle);
  142. U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() );
  143. U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat );
  144. if(dstBytesPerPixel == srcBytesPerPixel)
  145. {
  146. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], bmp->getWritableBits());
  147. return true;
  148. }
  149. FrameAllocatorMarker mem;
  150. U32 srcPixelCount = mTextureSize.x * mTextureSize.y;
  151. U8 *dest = bmp->getWritableBits();
  152. U8 *orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel);
  153. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig);
  154. PROFILE_START(GFXGLTextureObject_copyToBmp_pixCopy);
  155. for(int i = 0; i < srcPixelCount; ++i)
  156. {
  157. dest[0] = orig[0];
  158. dest[1] = orig[1];
  159. dest[2] = orig[2];
  160. if(dstBytesPerPixel == 4)
  161. dest[3] = orig[3];
  162. orig += srcBytesPerPixel;
  163. dest += dstBytesPerPixel;
  164. }
  165. PROFILE_END();
  166. return true;
  167. }
  168. void GFXGLTextureObject::initSamplerState(const GFXSamplerStateDesc &ssd)
  169. {
  170. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  171. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  172. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  173. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  174. if(mBinding == GL_TEXTURE_3D)
  175. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  176. if(static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  177. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  178. mNeedInitSamplerState = false;
  179. mSampler = ssd;
  180. }
  181. void GFXGLTextureObject::bind(U32 textureUnit)
  182. {
  183. glActiveTexture(GL_TEXTURE0 + textureUnit);
  184. glBindTexture(mBinding, mHandle);
  185. GFXGL->getOpenglCache()->setCacheBindedTex(textureUnit, mBinding, mHandle);
  186. if(GFXGL->mCapabilities.samplerObjects)
  187. return;
  188. GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
  189. AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
  190. if (!sb)
  191. return;
  192. const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
  193. if(mNeedInitSamplerState)
  194. {
  195. initSamplerState(ssd);
  196. return;
  197. }
  198. if(mSampler.minFilter != ssd.minFilter || mSampler.mipFilter != ssd.mipFilter)
  199. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  200. if(mSampler.magFilter != ssd.magFilter)
  201. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  202. if(mSampler.addressModeU != ssd.addressModeU)
  203. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  204. if(mSampler.addressModeV != ssd.addressModeV)
  205. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  206. if(mBinding == GL_TEXTURE_3D && mSampler.addressModeW != ssd.addressModeW )
  207. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  208. if(mSampler.maxAnisotropy != ssd.maxAnisotropy && static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  209. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  210. mSampler = ssd;
  211. }
  212. U8* GFXGLTextureObject::getTextureData( U32 mip )
  213. {
  214. AssertFatal( mMipLevels, "");
  215. mip = (mip < mMipLevels) ? mip : 0;
  216. const U32 dataSize = ImageUtil::isCompressedFormat(mFormat)
  217. ? getCompressedSurfaceSize( mFormat, mTextureSize.x, mTextureSize.y, mip )
  218. : (mTextureSize.x >> mip) * (mTextureSize.y >> mip) * mBytesPerTexel;
  219. U8* data = new U8[dataSize];
  220. PRESERVE_TEXTURE(mBinding);
  221. glBindTexture(mBinding, mHandle);
  222. if( ImageUtil::isCompressedFormat(mFormat) )
  223. glGetCompressedTexImage( mBinding, mip, data );
  224. else
  225. glGetTexImage(mBinding, mip, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], data);
  226. return data;
  227. }
  228. void GFXGLTextureObject::copyIntoCache()
  229. {
  230. PRESERVE_TEXTURE(mBinding);
  231. glBindTexture(mBinding, mHandle);
  232. U32 cacheSize = mTextureSize.x * mTextureSize.y;
  233. if(mBinding == GL_TEXTURE_3D)
  234. cacheSize *= mTextureSize.z;
  235. cacheSize *= mBytesPerTexel;
  236. mZombieCache = new U8[cacheSize];
  237. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  238. }
  239. void GFXGLTextureObject::reloadFromCache()
  240. {
  241. if(!mZombieCache)
  242. return;
  243. if(mBinding == GL_TEXTURE_3D)
  244. {
  245. static_cast<GFXGLTextureManager*>(TEXMGR)->_loadTexture(this, mZombieCache);
  246. delete[] mZombieCache;
  247. mZombieCache = NULL;
  248. return;
  249. }
  250. PRESERVE_TEXTURE(mBinding);
  251. glBindTexture(mBinding, mHandle);
  252. if(mBinding == GL_TEXTURE_2D)
  253. glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  254. else if(mBinding == GL_TEXTURE_1D)
  255. glTexSubImage1D(mBinding, 0, 0, (mTextureSize.x > 1 ? mTextureSize.x : mTextureSize.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  256. if(mMipLevels != 1)
  257. glGenerateMipmap(mBinding);
  258. delete[] mZombieCache;
  259. mZombieCache = NULL;
  260. mIsZombie = false;
  261. }
  262. void GFXGLTextureObject::zombify()
  263. {
  264. if(mIsZombie)
  265. return;
  266. mIsZombie = true;
  267. if(!mProfile->doStoreBitmap() && !mProfile->isRenderTarget() && !mProfile->isDynamic() && !mProfile->isZTarget())
  268. copyIntoCache();
  269. release();
  270. }
  271. void GFXGLTextureObject::resurrect()
  272. {
  273. if(!mIsZombie)
  274. return;
  275. glGenTextures(1, &mHandle);
  276. glGenBuffers(1, &mBuffer);
  277. }
  278. F32 GFXGLTextureObject::getMaxUCoord() const
  279. {
  280. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getWidth();
  281. }
  282. F32 GFXGLTextureObject::getMaxVCoord() const
  283. {
  284. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getHeight();
  285. }
  286. const String GFXGLTextureObject::describeSelf() const
  287. {
  288. String ret = Parent::describeSelf();
  289. ret += String::ToString(" GL Handle: %i", mHandle);
  290. return ret;
  291. }