gfxGLTextureObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 == GFXFormatR16G16B16A16F || mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_LINEAR_FORCE || mFormat == GFXFormatR8G8B8A8_SRGB, "copyToBmp: invalid format");
  132. if (mFormat != GFXFormatR16G16B16A16F && mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_LINEAR_FORCE && mFormat != GFXFormatR8G8B8A8_SRGB)
  133. return false;
  134. AssertFatal(bmp->getWidth() == getWidth(), "GFXGLTextureObject::copyToBmp - invalid size");
  135. AssertFatal(bmp->getHeight() == getHeight(), "GFXGLTextureObject::copyToBmp - invalid size");
  136. PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
  137. PRESERVE_TEXTURE(mBinding);
  138. glBindTexture(mBinding, mHandle);
  139. U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() );
  140. U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat );
  141. FrameAllocatorMarker mem;
  142. U32 srcPixelCount = mTextureSize.x * mTextureSize.y;
  143. U8 *dest = bmp->getWritableBits();
  144. U8 *orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel);
  145. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig);
  146. PROFILE_START(GFXGLTextureObject_copyToBmp_pixCopy);
  147. if (mFormat == GFXFormatR16G16B16A16F)
  148. {
  149. dMemcpy(dest, orig, sizeof(U16) * 4);
  150. }
  151. else
  152. {
  153. for (int i = 0; i < srcPixelCount; ++i)
  154. {
  155. dest[0] = orig[0];
  156. dest[1] = orig[1];
  157. dest[2] = orig[2];
  158. if (dstBytesPerPixel == 4)
  159. dest[3] = orig[3];
  160. orig += srcBytesPerPixel;
  161. dest += dstBytesPerPixel;
  162. }
  163. }
  164. PROFILE_END();
  165. return true;
  166. }
  167. void GFXGLTextureObject::initSamplerState(const GFXSamplerStateDesc &ssd)
  168. {
  169. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  170. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  171. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  172. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  173. if(mBinding == GL_TEXTURE_3D)
  174. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  175. if(static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  176. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  177. mNeedInitSamplerState = false;
  178. mSampler = ssd;
  179. }
  180. void GFXGLTextureObject::bind(U32 textureUnit)
  181. {
  182. glActiveTexture(GL_TEXTURE0 + textureUnit);
  183. glBindTexture(mBinding, mHandle);
  184. GFXGL->getOpenglCache()->setCacheBindedTex(textureUnit, mBinding, mHandle);
  185. if(GFXGL->mCapabilities.samplerObjects)
  186. return;
  187. GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
  188. AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
  189. if (!sb)
  190. return;
  191. const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
  192. if(mNeedInitSamplerState)
  193. {
  194. initSamplerState(ssd);
  195. return;
  196. }
  197. if(mSampler.minFilter != ssd.minFilter || mSampler.mipFilter != ssd.mipFilter)
  198. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  199. if(mSampler.magFilter != ssd.magFilter)
  200. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  201. if(mSampler.addressModeU != ssd.addressModeU)
  202. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  203. if(mSampler.addressModeV != ssd.addressModeV)
  204. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  205. if(mBinding == GL_TEXTURE_3D && mSampler.addressModeW != ssd.addressModeW )
  206. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  207. if(mSampler.maxAnisotropy != ssd.maxAnisotropy && static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  208. glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  209. mSampler = ssd;
  210. }
  211. U8* GFXGLTextureObject::getTextureData( U32 mip )
  212. {
  213. AssertFatal( mMipLevels, "");
  214. mip = (mip < mMipLevels) ? mip : 0;
  215. const U32 dataSize = ImageUtil::isCompressedFormat(mFormat)
  216. ? getCompressedSurfaceSize( mFormat, mTextureSize.x, mTextureSize.y, mip )
  217. : (mTextureSize.x >> mip) * (mTextureSize.y >> mip) * mBytesPerTexel;
  218. U8* data = new U8[dataSize];
  219. PRESERVE_TEXTURE(mBinding);
  220. glBindTexture(mBinding, mHandle);
  221. if( ImageUtil::isCompressedFormat(mFormat) )
  222. glGetCompressedTexImage( mBinding, mip, data );
  223. else
  224. glGetTexImage(mBinding, mip, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], data);
  225. return data;
  226. }
  227. void GFXGLTextureObject::copyIntoCache()
  228. {
  229. PRESERVE_TEXTURE(mBinding);
  230. glBindTexture(mBinding, mHandle);
  231. U32 cacheSize = mTextureSize.x * mTextureSize.y;
  232. if(mBinding == GL_TEXTURE_3D)
  233. cacheSize *= mTextureSize.z;
  234. cacheSize *= mBytesPerTexel;
  235. mZombieCache = new U8[cacheSize];
  236. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  237. }
  238. void GFXGLTextureObject::reloadFromCache()
  239. {
  240. if(!mZombieCache)
  241. return;
  242. if(mBinding == GL_TEXTURE_3D)
  243. {
  244. static_cast<GFXGLTextureManager*>(TEXMGR)->_loadTexture(this, mZombieCache);
  245. delete[] mZombieCache;
  246. mZombieCache = NULL;
  247. return;
  248. }
  249. PRESERVE_TEXTURE(mBinding);
  250. glBindTexture(mBinding, mHandle);
  251. if(mBinding == GL_TEXTURE_2D)
  252. glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  253. else if(mBinding == GL_TEXTURE_1D)
  254. glTexSubImage1D(mBinding, 0, 0, (mTextureSize.x > 1 ? mTextureSize.x : mTextureSize.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  255. if(mMipLevels != 1)
  256. glGenerateMipmap(mBinding);
  257. delete[] mZombieCache;
  258. mZombieCache = NULL;
  259. mIsZombie = false;
  260. }
  261. void GFXGLTextureObject::zombify()
  262. {
  263. if(mIsZombie)
  264. return;
  265. mIsZombie = true;
  266. if(!mProfile->doStoreBitmap() && !mProfile->isRenderTarget() && !mProfile->isDynamic() && !mProfile->isZTarget())
  267. copyIntoCache();
  268. release();
  269. }
  270. void GFXGLTextureObject::resurrect()
  271. {
  272. if(!mIsZombie)
  273. return;
  274. glGenTextures(1, &mHandle);
  275. glGenBuffers(1, &mBuffer);
  276. }
  277. F32 GFXGLTextureObject::getMaxUCoord() const
  278. {
  279. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getWidth();
  280. }
  281. F32 GFXGLTextureObject::getMaxVCoord() const
  282. {
  283. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getHeight();
  284. }
  285. const String GFXGLTextureObject::describeSelf() const
  286. {
  287. String ret = Parent::describeSelf();
  288. ret += String::ToString(" GL Handle: %i", mHandle);
  289. return ret;
  290. }