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