gfxGLTextureTarget.cpp 14 KB

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