gfxGLTextureTarget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 drawbufs[16];
  142. int bufsize = 0;
  143. for (int i = 0; i < 16; i++)
  144. drawbufs[i] = false;
  145. bool hasColor = false;
  146. for(int i = 0; i < GFXGL->getNumRenderTargets(); ++i)
  147. {
  148. _GFXGLTargetDesc* color = mTarget->getTargetDesc( static_cast<GFXTextureTarget::RenderSlot>(GFXTextureTarget::Color0+i ));
  149. if(color)
  150. {
  151. hasColor = true;
  152. const GLenum binding = color->getBinding();
  153. if( binding == GL_TEXTURE_2D || (binding >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && binding <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) )
  154. glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, color->getBinding( ), color->getHandle( ), color->getMipLevel( ) );
  155. else if( binding == GL_TEXTURE_1D )
  156. glFramebufferTexture1D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, color->getBinding( ), color->getHandle( ), color->getMipLevel( ) );
  157. else if( binding == GL_TEXTURE_3D )
  158. glFramebufferTexture3D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, color->getBinding( ), color->getHandle( ), color->getMipLevel( ), color->getZOffset( ) );
  159. else
  160. Con::errorf("_GFXGLTextureTargetFBOImpl::applyState - Bad binding");
  161. }
  162. else
  163. {
  164. // Clears the texture (note that the binding is irrelevent)
  165. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, GL_TEXTURE_2D, 0, 0);
  166. }
  167. }
  168. _GFXGLTargetDesc* depthStecil = mTarget->getTargetDesc(GFXTextureTarget::DepthStencil);
  169. if(depthStecil)
  170. {
  171. // Certain drivers have issues with depth only FBOs. That and the next two asserts assume we have a color target.
  172. AssertFatal(hasColor, "GFXGLTextureTarget::applyState() - Cannot set DepthStencil target without Color0 target!");
  173. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, depthStecil->getBinding(), depthStecil->getHandle(), depthStecil->getMipLevel());
  174. }
  175. else
  176. {
  177. // Clears the texture (note that the binding is irrelevent)
  178. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  179. }
  180. GLenum *buf = new GLenum[bufsize];
  181. int count = 0;
  182. for (int i = 0; i < bufsize; i++)
  183. {
  184. if (drawbufs[i])
  185. {
  186. buf[count] = GL_COLOR_ATTACHMENT0 + i;
  187. count++;
  188. }
  189. }
  190. glDrawBuffers(bufsize, buf);
  191. delete[] buf;
  192. CHECK_FRAMEBUFFER_STATUS();
  193. }
  194. void _GFXGLTextureTargetFBOImpl::makeActive()
  195. {
  196. glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
  197. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mFramebuffer);
  198. int i = 0;
  199. GLenum draws[16];
  200. for( i = 0; i < GFXGL->getNumRenderTargets(); ++i)
  201. {
  202. _GFXGLTargetDesc* color = mTarget->getTargetDesc( static_cast<GFXTextureTarget::RenderSlot>(GFXTextureTarget::Color0+i ));
  203. if(color)
  204. draws[i] = GL_COLOR_ATTACHMENT0 + i;
  205. else
  206. break;
  207. }
  208. glDrawBuffers( i, draws );
  209. }
  210. void _GFXGLTextureTargetFBOImpl::finish()
  211. {
  212. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  213. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, 0);
  214. for(int i = 0; i < GFXGL->getNumRenderTargets(); ++i)
  215. {
  216. _GFXGLTargetDesc* color = mTarget->getTargetDesc( static_cast<GFXTextureTarget::RenderSlot>(GFXTextureTarget::Color0+i ) );
  217. if(!color || !(color->hasMips()))
  218. continue;
  219. // Generate mips if necessary
  220. // Assumes a 2D texture.
  221. GLenum binding = color->getBinding();
  222. binding = (binding >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && binding <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) ? GL_TEXTURE_CUBE_MAP : binding;
  223. PRESERVE_TEXTURE( binding );
  224. glBindTexture( binding, color->getHandle() );
  225. glGenerateMipmap( binding );
  226. }
  227. }
  228. // Actual GFXGLTextureTarget interface
  229. GFXGLTextureTarget::GFXGLTextureTarget() : mCopyFboSrc(0), mCopyFboDst(0)
  230. {
  231. for(U32 i=0; i<MaxRenderSlotId; i++)
  232. mTargets[i] = NULL;
  233. GFXTextureManager::addEventDelegate( this, &GFXGLTextureTarget::_onTextureEvent );
  234. _impl = new _GFXGLTextureTargetFBOImpl(this);
  235. glGenFramebuffers(1, &mCopyFboSrc);
  236. glGenFramebuffers(1, &mCopyFboDst);
  237. }
  238. GFXGLTextureTarget::~GFXGLTextureTarget()
  239. {
  240. GFXTextureManager::removeEventDelegate(this, &GFXGLTextureTarget::_onTextureEvent);
  241. glDeleteFramebuffers(1, &mCopyFboSrc);
  242. glDeleteFramebuffers(1, &mCopyFboDst);
  243. }
  244. const Point2I GFXGLTextureTarget::getSize()
  245. {
  246. if(mTargets[Color0].isValid())
  247. return Point2I(mTargets[Color0]->getWidth(), mTargets[Color0]->getHeight());
  248. return Point2I(0, 0);
  249. }
  250. GFXFormat GFXGLTextureTarget::getFormat()
  251. {
  252. if(mTargets[Color0].isValid())
  253. return mTargets[Color0]->getFormat();
  254. return GFXFormatR8G8B8A8;
  255. }
  256. void GFXGLTextureTarget::attachTexture( RenderSlot slot, GFXTextureObject *tex, U32 mipLevel/*=0*/, U32 zOffset /*= 0*/ )
  257. {
  258. if( tex == GFXTextureTarget::sDefaultDepthStencil )
  259. tex = GFXGL->getDefaultDepthTex();
  260. _GFXGLTextureTargetDesc* mTex = static_cast<_GFXGLTextureTargetDesc*>(mTargets[slot].ptr());
  261. if( (!tex && !mTex) || (mTex && mTex->getTextureObject() == tex) )
  262. return;
  263. // Triggers an update when we next render
  264. invalidateState();
  265. // We stash the texture and info into an internal struct.
  266. GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(tex);
  267. if(tex && tex != GFXTextureTarget::sDefaultDepthStencil)
  268. mTargets[slot] = new _GFXGLTextureTargetDesc(glTexture, mipLevel, zOffset);
  269. else
  270. mTargets[slot] = NULL;
  271. }
  272. void GFXGLTextureTarget::attachTexture( RenderSlot slot, GFXCubemap *tex, U32 face, U32 mipLevel/*=0*/ )
  273. {
  274. // No depth cubemaps, sorry
  275. AssertFatal(slot != DepthStencil, "GFXGLTextureTarget::attachTexture (cube) - Cube depth textures not supported!");
  276. if(slot == DepthStencil)
  277. return;
  278. // Triggers an update when we next render
  279. invalidateState();
  280. // We stash the texture and info into an internal struct.
  281. GFXGLCubemap* glTexture = static_cast<GFXGLCubemap*>(tex);
  282. if(tex)
  283. mTargets[slot] = new _GFXGLCubemapTargetDesc(glTexture, face, mipLevel, 0);
  284. else
  285. mTargets[slot] = NULL;
  286. }
  287. void GFXGLTextureTarget::clearAttachments()
  288. {
  289. deactivate();
  290. for(S32 i=1; i<MaxRenderSlotId; i++)
  291. attachTexture((RenderSlot)i, NULL);
  292. }
  293. void GFXGLTextureTarget::zombify()
  294. {
  295. invalidateState();
  296. // Will be recreated in applyState
  297. _impl = NULL;
  298. }
  299. void GFXGLTextureTarget::resurrect()
  300. {
  301. // Dealt with when the target is next bound
  302. }
  303. void GFXGLTextureTarget::makeActive()
  304. {
  305. _impl->makeActive();
  306. }
  307. void GFXGLTextureTarget::deactivate()
  308. {
  309. _impl->finish();
  310. }
  311. void GFXGLTextureTarget::applyState()
  312. {
  313. if(!isPendingState())
  314. return;
  315. // So we don't do this over and over again
  316. stateApplied();
  317. if(_impl.isNull())
  318. _impl = new _GFXGLTextureTargetFBOImpl(this);
  319. _impl->applyState();
  320. }
  321. _GFXGLTargetDesc* GFXGLTextureTarget::getTargetDesc(RenderSlot slot) const
  322. {
  323. // This can only be called by our implementations, and then will not actually store the pointer so this is (almost) safe
  324. return mTargets[slot].ptr();
  325. }
  326. void GFXGLTextureTarget::_onTextureEvent( GFXTexCallbackCode code )
  327. {
  328. invalidateState();
  329. }
  330. const String GFXGLTextureTarget::describeSelf() const
  331. {
  332. String ret = String::ToString(" Color0 Attachment: %i", mTargets[Color0].isValid() ? mTargets[Color0]->getHandle() : 0);
  333. ret += String::ToString(" Depth Attachment: %i", mTargets[DepthStencil].isValid() ? mTargets[DepthStencil]->getHandle() : 0);
  334. return ret;
  335. }
  336. void GFXGLTextureTarget::resolve()
  337. {
  338. }
  339. void GFXGLTextureTarget::resolveTo(GFXTextureObject* obj)
  340. {
  341. AssertFatal(dynamic_cast<GFXGLTextureObject*>(obj), "GFXGLTextureTarget::resolveTo - Incorrect type of texture, expected a GFXGLTextureObject");
  342. GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(obj);
  343. if( GFXGL->mCapabilities.copyImage && mTargets[Color0]->isCompatible(glTexture) )
  344. {
  345. GLenum binding = mTargets[Color0]->getBinding();
  346. binding = (binding >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && binding <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) ? GL_TEXTURE_CUBE_MAP : binding;
  347. U32 srcStartDepth = binding == GL_TEXTURE_CUBE_MAP ? mTargets[Color0]->getBinding() - GL_TEXTURE_CUBE_MAP_POSITIVE_X : 0;
  348. glCopyImageSubData(
  349. mTargets[Color0]->getHandle(), binding, 0, 0, 0, srcStartDepth,
  350. glTexture->getHandle(), glTexture->getBinding(), 0, 0, 0, 0,
  351. mTargets[Color0]->getWidth(), mTargets[Color0]->getHeight(), 1);
  352. return;
  353. }
  354. PRESERVE_FRAMEBUFFER();
  355. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mCopyFboDst);
  356. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, glTexture->getBinding(), glTexture->getHandle(), 0);
  357. glBindFramebuffer(GL_READ_FRAMEBUFFER, mCopyFboSrc);
  358. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTargets[Color0]->getBinding(), mTargets[Color0]->getHandle(), 0);
  359. glBlitFramebuffer(0, 0, mTargets[Color0]->getWidth(), mTargets[Color0]->getHeight(),
  360. 0, 0, glTexture->getWidth(), glTexture->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
  361. }