gfxGLTextureTarget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/gfxGLDevice.h"
  24. #include "gfx/gl/gfxGLTextureTarget.h"
  25. #include "gfx/gl/gfxGLTextureObject.h"
  26. #include "gfx/gl/gfxGLCubemap.h"
  27. #include "gfx/gfxTextureManager.h"
  28. #include "gfx/gl/gfxGLUtils.h"
  29. /// Internal struct used to track texture information for FBO attachments
  30. /// This serves as an abstract base so we can deal with cubemaps and standard
  31. /// 2D/Rect textures through the same interface
  32. class _GFXGLTargetDesc
  33. {
  34. public:
  35. _GFXGLTargetDesc(U32 _mipLevel, U32 _zOffset) :
  36. mipLevel(_mipLevel), zOffset(_zOffset)
  37. {
  38. }
  39. virtual ~_GFXGLTargetDesc() {}
  40. virtual U32 getHandle() = 0;
  41. virtual U32 getWidth() = 0;
  42. virtual U32 getHeight() = 0;
  43. virtual U32 getDepth() = 0;
  44. virtual bool hasMips() = 0;
  45. virtual GLenum getBinding() = 0;
  46. virtual GFXFormat getFormat() = 0;
  47. virtual bool isCompatible(const GFXGLTextureObject* tex) = 0;
  48. U32 getMipLevel() { return mipLevel; }
  49. U32 getZOffset() { return zOffset; }
  50. private:
  51. U32 mipLevel;
  52. U32 zOffset;
  53. };
  54. /// Internal struct used to track 2D/Rect texture information for FBO attachment
  55. class _GFXGLTextureTargetDesc : public _GFXGLTargetDesc
  56. {
  57. public:
  58. _GFXGLTextureTargetDesc(GFXGLTextureObject* tex, U32 _mipLevel, U32 _zOffset)
  59. : _GFXGLTargetDesc(_mipLevel, _zOffset), mTex(tex)
  60. {
  61. }
  62. virtual ~_GFXGLTextureTargetDesc() {}
  63. virtual U32 getHandle() { return mTex->getHandle(); }
  64. virtual U32 getWidth() { return mTex->getWidth(); }
  65. virtual U32 getHeight() { return mTex->getHeight(); }
  66. virtual U32 getDepth() { return mTex->getDepth(); }
  67. virtual bool hasMips() { return mTex->mMipLevels != 1; }
  68. virtual GLenum getBinding() { return mTex->getBinding(); }
  69. virtual GFXFormat getFormat() { return mTex->getFormat(); }
  70. virtual bool isCompatible(const GFXGLTextureObject* tex)
  71. {
  72. return mTex->getFormat() == tex->getFormat()
  73. && mTex->getWidth() == tex->getWidth()
  74. && mTex->getHeight() == tex->getHeight();
  75. }
  76. GFXGLTextureObject* getTextureObject() const {return mTex; }
  77. private:
  78. StrongRefPtr<GFXGLTextureObject> mTex;
  79. };
  80. /// Internal struct used to track Cubemap texture information for FBO attachment
  81. class _GFXGLCubemapTargetDesc : public _GFXGLTargetDesc
  82. {
  83. public:
  84. _GFXGLCubemapTargetDesc(GFXGLCubemap* tex, U32 _face, U32 _mipLevel, U32 _zOffset)
  85. : _GFXGLTargetDesc(_mipLevel, _zOffset), mTex(tex), mFace(_face)
  86. {
  87. }
  88. virtual ~_GFXGLCubemapTargetDesc() {}
  89. virtual U32 getHandle() { return mTex->getHandle(); }
  90. virtual U32 getWidth() { return mTex->getWidth(); }
  91. virtual U32 getHeight() { return mTex->getHeight(); }
  92. virtual U32 getDepth() { return 0; }
  93. virtual bool hasMips() { return mTex->getNumMipLevels() != 1; }
  94. virtual GLenum getBinding() { return GFXGLCubemap::getEnumForFaceNumber(mFace); }
  95. virtual GFXFormat getFormat() { return mTex->getFormat(); }
  96. virtual bool isCompatible(const GFXGLTextureObject* tex)
  97. {
  98. return mTex->getFormat() == tex->getFormat()
  99. && mTex->getWidth() == tex->getWidth()
  100. && mTex->getHeight() == tex->getHeight();
  101. }
  102. private:
  103. StrongRefPtr<GFXGLCubemap> mTex;
  104. U32 mFace;
  105. };
  106. // Internal implementations
  107. class _GFXGLTextureTargetImpl // TODO OPENGL remove and implement on GFXGLTextureTarget
  108. {
  109. public:
  110. GFXGLTextureTarget* mTarget;
  111. virtual ~_GFXGLTextureTargetImpl() {}
  112. virtual void applyState() = 0;
  113. virtual void makeActive() = 0;
  114. virtual void finish() = 0;
  115. };
  116. // Use FBOs to render to texture. This is the preferred implementation and is almost always used.
  117. class _GFXGLTextureTargetFBOImpl : public _GFXGLTextureTargetImpl
  118. {
  119. public:
  120. GLuint mFramebuffer;
  121. _GFXGLTextureTargetFBOImpl(GFXGLTextureTarget* target);
  122. virtual ~_GFXGLTextureTargetFBOImpl();
  123. virtual void applyState();
  124. virtual void makeActive();
  125. virtual void finish();
  126. };
  127. _GFXGLTextureTargetFBOImpl::_GFXGLTextureTargetFBOImpl(GFXGLTextureTarget* target)
  128. {
  129. mTarget = target;
  130. glGenFramebuffers(1, &mFramebuffer);
  131. }
  132. _GFXGLTextureTargetFBOImpl::~_GFXGLTextureTargetFBOImpl()
  133. {
  134. glDeleteFramebuffers(1, &mFramebuffer);
  135. }
  136. void _GFXGLTextureTargetFBOImpl::applyState()
  137. {
  138. // REMINDER: When we implement MRT support, check against GFXGLDevice::getNumRenderTargets()
  139. PRESERVE_FRAMEBUFFER();
  140. glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
  141. bool hasColor = false;
  142. for(int i = 0; i < GFXGL->getNumRenderTargets(); ++i)
  143. {
  144. _GFXGLTargetDesc* color = mTarget->getTargetDesc( static_cast<GFXTextureTarget::RenderSlot>(GFXTextureTarget::Color0+i ));
  145. if(color)
  146. {
  147. hasColor = true;
  148. if( color->getBinding( ) == GL_TEXTURE_2D )
  149. glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, color->getBinding( ), color->getHandle( ), color->getMipLevel( ) );
  150. else if( color->getBinding( ) == GL_TEXTURE_1D )
  151. glFramebufferTexture1D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, color->getBinding( ), color->getHandle( ), color->getMipLevel( ) );
  152. else if( color->getBinding( ) == GL_TEXTURE_3D )
  153. glFramebufferTexture3D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, color->getBinding( ), color->getHandle( ), color->getMipLevel( ), color->getZOffset( ) );
  154. }
  155. else
  156. {
  157. // Clears the texture (note that the binding is irrelevent)
  158. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, GL_TEXTURE_2D, 0, 0);
  159. }
  160. }
  161. _GFXGLTargetDesc* depthStecil = mTarget->getTargetDesc(GFXTextureTarget::DepthStencil);
  162. if(depthStecil)
  163. {
  164. // Certain drivers have issues with depth only FBOs. That and the next two asserts assume we have a color target.
  165. AssertFatal(hasColor, "GFXGLTextureTarget::applyState() - Cannot set DepthStencil target without Color0 target!");
  166. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthStecil->getBinding(), depthStecil->getHandle(), depthStecil->getMipLevel());
  167. }
  168. else
  169. {
  170. // Clears the texture (note that the binding is irrelevent)
  171. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  172. }
  173. CHECK_FRAMEBUFFER_STATUS();
  174. }
  175. void _GFXGLTextureTargetFBOImpl::makeActive()
  176. {
  177. glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
  178. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mFramebuffer);
  179. int i = 0;
  180. GLenum draws[16];
  181. for( i = 0; i < GFXGL->getNumRenderTargets(); ++i)
  182. {
  183. _GFXGLTargetDesc* color = mTarget->getTargetDesc( static_cast<GFXTextureTarget::RenderSlot>(GFXTextureTarget::Color0+i ));
  184. if(color)
  185. draws[i] = GL_COLOR_ATTACHMENT0 + i;
  186. else
  187. break;
  188. }
  189. glDrawBuffers( i, draws );
  190. }
  191. void _GFXGLTextureTargetFBOImpl::finish()
  192. {
  193. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  194. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, 0);
  195. for(int i = 0; i < GFXGL->getNumRenderTargets(); ++i)
  196. {
  197. _GFXGLTargetDesc* color = mTarget->getTargetDesc( static_cast<GFXTextureTarget::RenderSlot>(GFXTextureTarget::Color0+i ) );
  198. if(!color || !(color->hasMips()))
  199. continue;
  200. // Generate mips if necessary
  201. // Assumes a 2D texture.
  202. PRESERVE_TEXTURE(color->getBinding());
  203. glBindTexture(color->getBinding(), color->getHandle());
  204. glGenerateMipmapEXT(GL_TEXTURE_2D);
  205. }
  206. }
  207. // Actual GFXGLTextureTarget interface
  208. GFXGLTextureTarget::GFXGLTextureTarget() : mCopyFboSrc(0), mCopyFboDst(0)
  209. {
  210. for(U32 i=0; i<MaxRenderSlotId; i++)
  211. mTargets[i] = NULL;
  212. GFXTextureManager::addEventDelegate( this, &GFXGLTextureTarget::_onTextureEvent );
  213. _impl = new _GFXGLTextureTargetFBOImpl(this);
  214. glGenFramebuffers(1, &mCopyFboSrc);
  215. glGenFramebuffers(1, &mCopyFboDst);
  216. }
  217. GFXGLTextureTarget::~GFXGLTextureTarget()
  218. {
  219. GFXTextureManager::removeEventDelegate( this, &GFXGLTextureTarget::_onTextureEvent );
  220. }
  221. const Point2I GFXGLTextureTarget::getSize()
  222. {
  223. if(mTargets[Color0].isValid())
  224. return Point2I(mTargets[Color0]->getWidth(), mTargets[Color0]->getHeight());
  225. return Point2I(0, 0);
  226. }
  227. GFXFormat GFXGLTextureTarget::getFormat()
  228. {
  229. if(mTargets[Color0].isValid())
  230. return mTargets[Color0]->getFormat();
  231. return GFXFormatR8G8B8A8;
  232. }
  233. void GFXGLTextureTarget::attachTexture( RenderSlot slot, GFXTextureObject *tex, U32 mipLevel/*=0*/, U32 zOffset /*= 0*/ )
  234. {
  235. if( tex == GFXTextureTarget::sDefaultDepthStencil )
  236. tex = GFXGL->getDefaultDepthTex();
  237. _GFXGLTextureTargetDesc* mTex = static_cast<_GFXGLTextureTargetDesc*>(mTargets[slot].ptr());
  238. if( (!tex && !mTex) || (mTex && mTex->getTextureObject() == tex) )
  239. return;
  240. // Triggers an update when we next render
  241. invalidateState();
  242. // We stash the texture and info into an internal struct.
  243. GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(tex);
  244. if(tex && tex != GFXTextureTarget::sDefaultDepthStencil)
  245. mTargets[slot] = new _GFXGLTextureTargetDesc(glTexture, mipLevel, zOffset);
  246. else
  247. mTargets[slot] = NULL;
  248. }
  249. void GFXGLTextureTarget::attachTexture( RenderSlot slot, GFXCubemap *tex, U32 face, U32 mipLevel/*=0*/ )
  250. {
  251. // No depth cubemaps, sorry
  252. AssertFatal(slot != DepthStencil, "GFXGLTextureTarget::attachTexture (cube) - Cube depth textures not supported!");
  253. if(slot == DepthStencil)
  254. return;
  255. // Triggers an update when we next render
  256. invalidateState();
  257. // We stash the texture and info into an internal struct.
  258. GFXGLCubemap* glTexture = static_cast<GFXGLCubemap*>(tex);
  259. if(tex)
  260. mTargets[slot] = new _GFXGLCubemapTargetDesc(glTexture, face, mipLevel, 0);
  261. else
  262. mTargets[slot] = NULL;
  263. }
  264. void GFXGLTextureTarget::clearAttachments()
  265. {
  266. deactivate();
  267. for(S32 i=1; i<MaxRenderSlotId; i++)
  268. attachTexture((RenderSlot)i, NULL);
  269. }
  270. void GFXGLTextureTarget::zombify()
  271. {
  272. invalidateState();
  273. // Will be recreated in applyState
  274. _impl = NULL;
  275. }
  276. void GFXGLTextureTarget::resurrect()
  277. {
  278. // Dealt with when the target is next bound
  279. }
  280. void GFXGLTextureTarget::makeActive()
  281. {
  282. _impl->makeActive();
  283. }
  284. void GFXGLTextureTarget::deactivate()
  285. {
  286. _impl->finish();
  287. }
  288. void GFXGLTextureTarget::applyState()
  289. {
  290. if(!isPendingState())
  291. return;
  292. // So we don't do this over and over again
  293. stateApplied();
  294. if(_impl.isNull())
  295. _impl = new _GFXGLTextureTargetFBOImpl(this);
  296. _impl->applyState();
  297. }
  298. _GFXGLTargetDesc* GFXGLTextureTarget::getTargetDesc(RenderSlot slot) const
  299. {
  300. // This can only be called by our implementations, and then will not actually store the pointer so this is (almost) safe
  301. return mTargets[slot].ptr();
  302. }
  303. void GFXGLTextureTarget::_onTextureEvent( GFXTexCallbackCode code )
  304. {
  305. invalidateState();
  306. }
  307. const String GFXGLTextureTarget::describeSelf() const
  308. {
  309. String ret = String::ToString(" Color0 Attachment: %i", mTargets[Color0].isValid() ? mTargets[Color0]->getHandle() : 0);
  310. ret += String::ToString(" Depth Attachment: %i", mTargets[DepthStencil].isValid() ? mTargets[DepthStencil]->getHandle() : 0);
  311. return ret;
  312. }
  313. void GFXGLTextureTarget::resolve()
  314. {
  315. }
  316. void GFXGLTextureTarget::resolveTo(GFXTextureObject* obj)
  317. {
  318. AssertFatal(dynamic_cast<GFXGLTextureObject*>(obj), "GFXGLTextureTarget::resolveTo - Incorrect type of texture, expected a GFXGLTextureObject");
  319. GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(obj);
  320. if( gglHasExtension(ARB_copy_image) && mTargets[Color0]->isCompatible(glTexture) )
  321. {
  322. glCopyImageSubData(
  323. mTargets[Color0]->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
  324. glTexture->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
  325. mTargets[Color0]->getWidth(), mTargets[Color0]->getHeight(), 1);
  326. return;
  327. }
  328. PRESERVE_FRAMEBUFFER();
  329. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mCopyFboDst);
  330. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glTexture->getHandle(), 0);
  331. glBindFramebuffer(GL_READ_FRAMEBUFFER, mCopyFboSrc);
  332. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,mTargets[Color0]->getHandle(), 0);
  333. glBlitFramebuffer(0, 0, mTargets[Color0]->getWidth(), mTargets[Color0]->getHeight(),
  334. 0, 0, glTexture->getWidth(), glTexture->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
  335. }