BsGLRenderTexture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "BsGLRenderTexture.h"
  25. #include "BsGLPixelFormat.h"
  26. #include "BsGLPixelBuffer.h"
  27. #include "BsTextureView.h"
  28. namespace BansheeEngine
  29. {
  30. GLRenderTexture::GLRenderTexture()
  31. :mFB(nullptr)
  32. {
  33. }
  34. GLRenderTexture::~GLRenderTexture()
  35. {
  36. }
  37. void GLRenderTexture::destroy_internal()
  38. {
  39. if(mFB != nullptr)
  40. bs_delete<PoolAlloc>(mFB);
  41. RenderTexture::destroy_internal();
  42. }
  43. void GLRenderTexture::initialize_internal()
  44. {
  45. if(mFB != nullptr)
  46. bs_delete<PoolAlloc>(mFB);
  47. mFB = bs_new<GLFrameBufferObject, PoolAlloc>(mMultisampleCount);
  48. GLSurfaceDesc surfaceDesc;
  49. surfaceDesc.numSamples = mMultisampleCount;
  50. surfaceDesc.zoffset = 0;
  51. GLTexture* glTexture = static_cast<GLTexture*>(mColorSurface->getTexture().get());
  52. surfaceDesc.buffer = glTexture->getBuffer(mColorSurface->getFirstArraySlice(), mColorSurface->getMostDetailedMip());
  53. mFB->bindSurface(0, surfaceDesc);
  54. GLTexture* glDepthStencilTexture = static_cast<GLTexture*>(mDepthStencilSurface->getTexture().get());
  55. GLPixelBufferPtr depthStencilBuffer =
  56. glDepthStencilTexture->getBuffer(mDepthStencilSurface->getFirstArraySlice(), mDepthStencilSurface->getMostDetailedMip());
  57. mFB->bindDepthStencil(depthStencilBuffer);
  58. RenderTexture::initialize_internal();
  59. }
  60. void GLRenderTexture::getCustomAttribute(const String& name, void* pData) const
  61. {
  62. if(name=="FBO")
  63. {
  64. *static_cast<GLFrameBufferObject **>(pData) = mFB;
  65. }
  66. else if (name == "GL_FBOID" || name == "GL_MULTISAMPLEFBOID")
  67. {
  68. *static_cast<GLuint*>(pData) = mFB->getGLFBOID();
  69. }
  70. }
  71. /// Size of probe texture
  72. #define PROBE_SIZE 16
  73. static const GLenum depthFormats[] =
  74. {
  75. GL_NONE,
  76. GL_DEPTH_COMPONENT16,
  77. GL_DEPTH_COMPONENT32,
  78. GL_DEPTH24_STENCIL8, // packed depth / stencil
  79. GL_DEPTH32F_STENCIL8
  80. };
  81. static const UINT32 depthBits[] =
  82. {
  83. 0,16,32,24,32
  84. };
  85. #define DEPTHFORMAT_COUNT (sizeof(depthFormats)/sizeof(GLenum))
  86. GLRTTManager::GLRTTManager()
  87. {
  88. detectFBOFormats();
  89. glGenFramebuffersEXT(1, &mTempFBO);
  90. }
  91. GLRTTManager::~GLRTTManager()
  92. {
  93. glDeleteFramebuffersEXT(1, &mTempFBO);
  94. }
  95. /** Try a certain FBO format, and return the status. Also sets mDepthRB and mStencilRB.
  96. @returns true if this combo is supported
  97. false if this combo is not supported
  98. */
  99. GLuint GLRTTManager::_tryFormat(GLenum depthFormat, GLenum stencilFormat)
  100. {
  101. GLuint status, depthRB = 0, stencilRB = 0;
  102. bool failed = false; // flag on GL errors
  103. if(depthFormat != GL_NONE)
  104. {
  105. /// Generate depth renderbuffer
  106. glGenRenderbuffersEXT(1, &depthRB);
  107. /// Bind it to FBO
  108. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRB);
  109. /// Allocate storage for depth buffer
  110. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat,
  111. PROBE_SIZE, PROBE_SIZE);
  112. /// Attach depth
  113. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  114. GL_RENDERBUFFER_EXT, depthRB);
  115. }
  116. if(stencilFormat != GL_NONE)
  117. {
  118. /// Generate stencil renderbuffer
  119. glGenRenderbuffersEXT(1, &stencilRB);
  120. /// Bind it to FBO
  121. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencilRB);
  122. glGetError(); // NV hack
  123. /// Allocate storage for stencil buffer
  124. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, stencilFormat,
  125. PROBE_SIZE, PROBE_SIZE);
  126. if(glGetError() != GL_NO_ERROR) // NV hack
  127. failed = true;
  128. /// Attach stencil
  129. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  130. GL_RENDERBUFFER_EXT, stencilRB);
  131. if(glGetError() != GL_NO_ERROR) // NV hack
  132. failed = true;
  133. }
  134. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  135. /// If status is negative, clean up
  136. // Detach and destroy
  137. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  138. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  139. if (depthRB)
  140. glDeleteRenderbuffersEXT(1, &depthRB);
  141. if (stencilRB)
  142. glDeleteRenderbuffersEXT(1, &stencilRB);
  143. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  144. }
  145. /** Try a certain packed depth/stencil format, and return the status.
  146. @returns true if this combo is supported
  147. false if this combo is not supported
  148. */
  149. bool GLRTTManager::_tryPackedFormat(GLenum packedFormat)
  150. {
  151. GLuint packedRB = 0;
  152. bool failed = false; // flag on GL errors
  153. /// Generate renderbuffer
  154. glGenRenderbuffersEXT(1, &packedRB);
  155. /// Bind it to FBO
  156. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, packedRB);
  157. /// Allocate storage for buffer
  158. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, packedFormat, PROBE_SIZE, PROBE_SIZE);
  159. glGetError(); // NV hack
  160. /// Attach depth
  161. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  162. GL_RENDERBUFFER_EXT, packedRB);
  163. if(glGetError() != GL_NO_ERROR) // NV hack
  164. failed = true;
  165. /// Attach stencil
  166. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  167. GL_RENDERBUFFER_EXT, packedRB);
  168. if(glGetError() != GL_NO_ERROR) // NV hack
  169. failed = true;
  170. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  171. /// Detach and destroy
  172. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  173. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  174. glDeleteRenderbuffersEXT(1, &packedRB);
  175. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  176. }
  177. /** Detect which internal formats are allowed as RTT
  178. Also detect what combinations of stencil and depth are allowed with this internal
  179. format.
  180. */
  181. void GLRTTManager::detectFBOFormats()
  182. {
  183. // Try all formats, and report which ones work as target
  184. GLuint fb = 0, tid = 0;
  185. GLint old_drawbuffer = 0, old_readbuffer = 0;
  186. GLenum target = GL_TEXTURE_2D;
  187. glGetIntegerv (GL_DRAW_BUFFER, &old_drawbuffer);
  188. glGetIntegerv (GL_READ_BUFFER, &old_readbuffer);
  189. for(size_t x=0; x<PF_COUNT; ++x)
  190. {
  191. mProps[x].valid = false;
  192. // Fetch GL format token
  193. GLenum fmt = GLPixelUtil::getGLInternalFormat((PixelFormat)x);
  194. if(fmt == GL_NONE && x!=0)
  195. continue;
  196. // No test for compressed formats
  197. if(PixelUtil::isCompressed((PixelFormat)x))
  198. continue;
  199. // Create and attach framebuffer
  200. glGenFramebuffersEXT(1, &fb);
  201. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  202. if (fmt!=GL_NONE && !PixelUtil::isDepth((PixelFormat)x))
  203. {
  204. // Create and attach texture
  205. glGenTextures(1, &tid);
  206. glBindTexture(target, tid);
  207. // Set some default parameters so it won't fail on NVidia cards
  208. if (GLEW_VERSION_1_2)
  209. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
  210. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  211. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  212. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  213. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  214. glTexImage2D(target, 0, fmt, PROBE_SIZE, PROBE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  215. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  216. target, tid, 0);
  217. }
  218. else
  219. {
  220. // Draw to nowhere -- stencil/depth only
  221. glDrawBuffer(GL_NONE);
  222. glReadBuffer(GL_NONE);
  223. }
  224. // Check status
  225. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  226. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  227. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  228. // might still be supported, so we must continue probing.
  229. if(fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE_EXT)
  230. {
  231. mProps[x].valid = true;
  232. // For each depth/stencil formats
  233. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  234. {
  235. if (depthFormats[depth] != GL_DEPTH24_STENCIL8_EXT && depthFormats[depth] != GL_DEPTH32F_STENCIL8)
  236. {
  237. if (_tryFormat(depthFormats[depth], GL_NONE))
  238. {
  239. /// Add mode to allowed modes
  240. FormatProperties::Mode mode;
  241. mode.depth = depth;
  242. mode.stencil = 0;
  243. mProps[x].modes.push_back(mode);
  244. }
  245. }
  246. else
  247. {
  248. // Packed depth/stencil format
  249. if (_tryPackedFormat(depthFormats[depth]))
  250. {
  251. /// Add mode to allowed modes
  252. FormatProperties::Mode mode;
  253. mode.depth = depth;
  254. mode.stencil = 0; // unuse
  255. mProps[x].modes.push_back(mode);
  256. }
  257. }
  258. }
  259. }
  260. // Delete texture and framebuffer
  261. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  262. glDeleteFramebuffersEXT(1, &fb);
  263. glFinish();
  264. if (fmt!=GL_NONE)
  265. glDeleteTextures(1, &tid);
  266. }
  267. // It seems a bug in nVidia driver: glBindFramebufferEXT should restore
  268. // draw and read buffers, but in some unclear circumstances it won't.
  269. glDrawBuffer(old_drawbuffer);
  270. glReadBuffer(old_readbuffer);
  271. String fmtstring = "";
  272. for(size_t x=0; x<PF_COUNT; ++x)
  273. {
  274. if(mProps[x].valid)
  275. fmtstring += PixelUtil::getFormatName((PixelFormat)x)+" ";
  276. }
  277. }
  278. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  279. {
  280. if(checkFormat(format))
  281. return format;
  282. /// Find first alternative
  283. PixelComponentType pct = PixelUtil::getElementType(format);
  284. switch(pct)
  285. {
  286. case PCT_BYTE: format = PF_A8R8G8B8; break;
  287. case PCT_FLOAT16: format = PF_FLOAT16_RGBA; break;
  288. case PCT_FLOAT32: format = PF_FLOAT32_RGBA; break;
  289. case PCT_COUNT: break;
  290. }
  291. if(checkFormat(format))
  292. return format;
  293. /// If none at all, return to default
  294. return PF_A8R8G8B8;
  295. }
  296. }