gfxGLTextureObject.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/ggl/ggl.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. {
  39. AssertFatal(dynamic_cast<GFXGLDevice*>(mDevice), "GFXGLTextureObject::GFXGLTextureObject - Invalid device type, expected GFXGLDevice!");
  40. glGenTextures(1, &mHandle);
  41. glGenBuffers(1, &mBuffer);
  42. }
  43. GFXGLTextureObject::~GFXGLTextureObject()
  44. {
  45. glDeleteBuffers(1, &mBuffer);
  46. delete[] mZombieCache;
  47. kill();
  48. }
  49. GFXLockedRect* GFXGLTextureObject::lock(U32 mipLevel, RectI *inRect)
  50. {
  51. AssertFatal(mBinding != GL_TEXTURE_3D, "GFXGLTextureObject::lock - We don't support locking 3D textures yet");
  52. U32 width = mTextureSize.x >> mipLevel;
  53. U32 height = mTextureSize.y >> mipLevel;
  54. if(inRect)
  55. {
  56. if((inRect->point.x + inRect->extent.x > width) || (inRect->point.y + inRect->extent.y > height))
  57. AssertFatal(false, "GFXGLTextureObject::lock - Rectangle too big!");
  58. mLockedRectRect = *inRect;
  59. }
  60. else
  61. {
  62. mLockedRectRect = RectI(0, 0, width, height);
  63. }
  64. mLockedRect.pitch = mLockedRectRect.extent.x * mBytesPerTexel;
  65. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
  66. // CodeReview [ags 12/19/07] This one texel boundary is necessary to keep the clipmap code from crashing. Figure out why.
  67. glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, NULL, GL_STREAM_DRAW);
  68. mLockedRect.bits = (U8*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
  69. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  70. if( !mLockedRect.bits )
  71. return NULL;
  72. return &mLockedRect;
  73. }
  74. void GFXGLTextureObject::unlock(U32 mipLevel)
  75. {
  76. if(!mLockedRect.bits)
  77. return;
  78. glActiveTexture(GL_TEXTURE0);
  79. U32 boundTexture;
  80. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&boundTexture);
  81. glBindTexture(GL_TEXTURE_2D, mHandle);
  82. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
  83. glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
  84. glTexSubImage2D(GL_TEXTURE_2D, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
  85. mLockedRectRect.extent.x, mLockedRectRect.extent.y, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
  86. glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  87. mLockedRect.bits = NULL;
  88. glBindTexture(GL_TEXTURE_2D, boundTexture);
  89. }
  90. void GFXGLTextureObject::release()
  91. {
  92. glDeleteTextures(1, &mHandle);
  93. glDeleteBuffers(1, &mBuffer);
  94. mHandle = 0;
  95. mBuffer = 0;
  96. }
  97. bool GFXGLTextureObject::copyToBmp(GBitmap * bmp)
  98. {
  99. GLint oldTex;
  100. glGetIntegerv(0x8069, &oldTex);
  101. glBindTexture(GL_TEXTURE_2D, mHandle);
  102. GLint textureFormat = GFXGLTextureFormat[bmp->getFormat()];
  103. // Don't swizzle outgoing textures.
  104. if(textureFormat == GL_BGRA)
  105. textureFormat = GL_RGBA;
  106. glGetTexImage(GL_TEXTURE_2D, 0, textureFormat, GL_UNSIGNED_BYTE, bmp->getWritableBits());
  107. glBindTexture(GL_TEXTURE_2D, oldTex);
  108. return true;
  109. }
  110. void GFXGLTextureObject::bind(U32 textureUnit) const
  111. {
  112. glActiveTexture(GL_TEXTURE0 + textureUnit);
  113. glBindTexture(mBinding, mHandle);
  114. glEnable(mBinding);
  115. GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
  116. AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
  117. if (!sb)
  118. return;
  119. const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
  120. glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
  121. glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  122. glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
  123. glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
  124. if(mBinding == GL_TEXTURE_3D)
  125. glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  126. glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, ssd.mipLODBias);
  127. }
  128. U8* GFXGLTextureObject::getTextureData()
  129. {
  130. U8* data = new U8[mTextureSize.x * mTextureSize.y * mBytesPerTexel];
  131. glBindTexture(GL_TEXTURE_2D, mHandle);
  132. glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
  133. return data;
  134. }
  135. void GFXGLTextureObject::copyIntoCache()
  136. {
  137. glBindTexture(mBinding, mHandle);
  138. U32 cacheSize = mTextureSize.x * mTextureSize.y;
  139. if(mBinding == GL_TEXTURE_3D)
  140. cacheSize *= mTextureSize.z;
  141. cacheSize *= mBytesPerTexel;
  142. mZombieCache = new U8[cacheSize];
  143. glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  144. glBindTexture(mBinding, 0);
  145. }
  146. void GFXGLTextureObject::reloadFromCache()
  147. {
  148. if(!mZombieCache)
  149. return;
  150. if(mBinding == GL_TEXTURE_3D)
  151. {
  152. static_cast<GFXGLTextureManager*>(TEXMGR)->_loadTexture(this, mZombieCache);
  153. delete[] mZombieCache;
  154. mZombieCache = NULL;
  155. return;
  156. }
  157. glBindTexture(mBinding, mHandle);
  158. glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
  159. if(GFX->getCardProfiler()->queryProfile("GL::Workaround::needsExplicitGenerateMipmap") && mMipLevels != 1)
  160. glGenerateMipmapEXT(mBinding);
  161. delete[] mZombieCache;
  162. mZombieCache = NULL;
  163. mIsZombie = false;
  164. }
  165. void GFXGLTextureObject::zombify()
  166. {
  167. if(mIsZombie)
  168. return;
  169. mIsZombie = true;
  170. if(!mProfile->doStoreBitmap() && !mProfile->isRenderTarget() && !mProfile->isDynamic() && !mProfile->isZTarget())
  171. copyIntoCache();
  172. release();
  173. }
  174. void GFXGLTextureObject::resurrect()
  175. {
  176. if(!mIsZombie)
  177. return;
  178. glGenTextures(1, &mHandle);
  179. glGenBuffers(1, &mBuffer);
  180. }
  181. F32 GFXGLTextureObject::getMaxUCoord() const
  182. {
  183. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getWidth();
  184. }
  185. F32 GFXGLTextureObject::getMaxVCoord() const
  186. {
  187. return mBinding == GL_TEXTURE_2D ? 1.0f : (F32)getHeight();
  188. }
  189. const String GFXGLTextureObject::describeSelf() const
  190. {
  191. String ret = Parent::describeSelf();
  192. ret += String::ToString(" GL Handle: %i", mHandle);
  193. return ret;
  194. }