gfxGLTextureObject.cpp 11 KB

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