CmGLRenderTexture.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "CmGLRenderTexture.h"
  25. #include "CmGLPixelFormat.h"
  26. #include "CmGLPixelBuffer.h"
  27. #include "CmTextureView.h"
  28. namespace CamelotEngine
  29. {
  30. GLRenderTexture::GLRenderTexture()
  31. :mFB(nullptr)
  32. {
  33. }
  34. GLRenderTexture::~GLRenderTexture()
  35. {
  36. if(mFB != nullptr)
  37. delete mFB;
  38. }
  39. void GLRenderTexture::createInternalResourcesImpl()
  40. {
  41. if(mFB != nullptr)
  42. delete mFB;
  43. mFB = new GLFrameBufferObject(mFSAA);
  44. GLSurfaceDesc surfaceDesc;
  45. surfaceDesc.numSamples = mFSAA;
  46. surfaceDesc.zoffset = 0;
  47. GLTexture* glTexture = static_cast<GLTexture*>(mColorSurface->getTexture().get());
  48. surfaceDesc.buffer = std::static_pointer_cast<GLPixelBuffer>(
  49. glTexture->getBuffer(mColorSurface->getFirstArraySlice(), mColorSurface->getMostDetailedMip()));
  50. mFB->bindSurface(0, surfaceDesc);
  51. GLTexture* glDepthStencilTexture = static_cast<GLTexture*>(mDepthStencilSurface->getTexture().get());
  52. GLPixelBufferPtr depthStencilBuffer = std::static_pointer_cast<GLPixelBuffer>(
  53. glDepthStencilTexture->getBuffer(mDepthStencilSurface->getFirstArraySlice(), mDepthStencilSurface->getMostDetailedMip()));
  54. mFB->bindDepthStencil(depthStencilBuffer);
  55. }
  56. void GLRenderTexture::getCustomAttribute(const String& name, void* pData)
  57. {
  58. if(name=="FBO")
  59. {
  60. *static_cast<GLFrameBufferObject **>(pData) = mFB;
  61. }
  62. else if (name == "GL_FBOID" || name == "GL_MULTISAMPLEFBOID")
  63. {
  64. *static_cast<GLuint*>(pData) = mFB->getGLFBOID();
  65. }
  66. }
  67. /// Size of probe texture
  68. #define PROBE_SIZE 16
  69. static const GLenum depthFormats[] =
  70. {
  71. GL_NONE,
  72. GL_DEPTH_COMPONENT16,
  73. GL_DEPTH_COMPONENT32,
  74. GL_DEPTH24_STENCIL8, // packed depth / stencil
  75. GL_DEPTH32F_STENCIL8
  76. };
  77. static const UINT32 depthBits[] =
  78. {
  79. 0,16,32,24,32
  80. };
  81. #define DEPTHFORMAT_COUNT (sizeof(depthFormats)/sizeof(GLenum))
  82. GLRTTManager::GLRTTManager()
  83. {
  84. detectFBOFormats();
  85. glGenFramebuffersEXT(1, &mTempFBO);
  86. }
  87. GLRTTManager::~GLRTTManager()
  88. {
  89. glDeleteFramebuffersEXT(1, &mTempFBO);
  90. }
  91. /** Try a certain FBO format, and return the status. Also sets mDepthRB and mStencilRB.
  92. @returns true if this combo is supported
  93. false if this combo is not supported
  94. */
  95. GLuint GLRTTManager::_tryFormat(GLenum depthFormat, GLenum stencilFormat)
  96. {
  97. GLuint status, depthRB = 0, stencilRB = 0;
  98. bool failed = false; // flag on GL errors
  99. if(depthFormat != GL_NONE)
  100. {
  101. /// Generate depth renderbuffer
  102. glGenRenderbuffersEXT(1, &depthRB);
  103. /// Bind it to FBO
  104. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRB);
  105. /// Allocate storage for depth buffer
  106. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat,
  107. PROBE_SIZE, PROBE_SIZE);
  108. /// Attach depth
  109. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  110. GL_RENDERBUFFER_EXT, depthRB);
  111. }
  112. if(stencilFormat != GL_NONE)
  113. {
  114. /// Generate stencil renderbuffer
  115. glGenRenderbuffersEXT(1, &stencilRB);
  116. /// Bind it to FBO
  117. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencilRB);
  118. glGetError(); // NV hack
  119. /// Allocate storage for stencil buffer
  120. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, stencilFormat,
  121. PROBE_SIZE, PROBE_SIZE);
  122. if(glGetError() != GL_NO_ERROR) // NV hack
  123. failed = true;
  124. /// Attach stencil
  125. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  126. GL_RENDERBUFFER_EXT, stencilRB);
  127. if(glGetError() != GL_NO_ERROR) // NV hack
  128. failed = true;
  129. }
  130. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  131. /// If status is negative, clean up
  132. // Detach and destroy
  133. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  134. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  135. if (depthRB)
  136. glDeleteRenderbuffersEXT(1, &depthRB);
  137. if (stencilRB)
  138. glDeleteRenderbuffersEXT(1, &stencilRB);
  139. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  140. }
  141. /** Try a certain packed depth/stencil format, and return the status.
  142. @returns true if this combo is supported
  143. false if this combo is not supported
  144. */
  145. bool GLRTTManager::_tryPackedFormat(GLenum packedFormat)
  146. {
  147. GLuint packedRB = 0;
  148. bool failed = false; // flag on GL errors
  149. /// Generate renderbuffer
  150. glGenRenderbuffersEXT(1, &packedRB);
  151. /// Bind it to FBO
  152. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, packedRB);
  153. /// Allocate storage for buffer
  154. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, packedFormat, PROBE_SIZE, PROBE_SIZE);
  155. glGetError(); // NV hack
  156. /// Attach depth
  157. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  158. GL_RENDERBUFFER_EXT, packedRB);
  159. if(glGetError() != GL_NO_ERROR) // NV hack
  160. failed = true;
  161. /// Attach stencil
  162. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  163. GL_RENDERBUFFER_EXT, packedRB);
  164. if(glGetError() != GL_NO_ERROR) // NV hack
  165. failed = true;
  166. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  167. /// Detach and destroy
  168. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  169. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  170. glDeleteRenderbuffersEXT(1, &packedRB);
  171. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  172. }
  173. /** Detect which internal formats are allowed as RTT
  174. Also detect what combinations of stencil and depth are allowed with this internal
  175. format.
  176. */
  177. void GLRTTManager::detectFBOFormats()
  178. {
  179. // Try all formats, and report which ones work as target
  180. GLuint fb = 0, tid = 0;
  181. GLint old_drawbuffer = 0, old_readbuffer = 0;
  182. GLenum target = GL_TEXTURE_2D;
  183. glGetIntegerv (GL_DRAW_BUFFER, &old_drawbuffer);
  184. glGetIntegerv (GL_READ_BUFFER, &old_readbuffer);
  185. for(size_t x=0; x<PF_COUNT; ++x)
  186. {
  187. mProps[x].valid = false;
  188. // Fetch GL format token
  189. GLenum fmt = GLPixelUtil::getGLInternalFormat((PixelFormat)x);
  190. if(fmt == GL_NONE && x!=0)
  191. continue;
  192. // No test for compressed formats
  193. if(PixelUtil::isCompressed((PixelFormat)x))
  194. continue;
  195. // Create and attach framebuffer
  196. glGenFramebuffersEXT(1, &fb);
  197. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  198. if (fmt!=GL_NONE && !PixelUtil::isDepth((PixelFormat)x))
  199. {
  200. // Create and attach texture
  201. glGenTextures(1, &tid);
  202. glBindTexture(target, tid);
  203. // Set some default parameters so it won't fail on NVidia cards
  204. if (GLEW_VERSION_1_2)
  205. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
  206. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  207. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  208. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  209. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  210. glTexImage2D(target, 0, fmt, PROBE_SIZE, PROBE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  211. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  212. target, tid, 0);
  213. }
  214. else
  215. {
  216. // Draw to nowhere -- stencil/depth only
  217. glDrawBuffer(GL_NONE);
  218. glReadBuffer(GL_NONE);
  219. }
  220. // Check status
  221. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  222. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  223. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  224. // might still be supported, so we must continue probing.
  225. if(fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE_EXT)
  226. {
  227. mProps[x].valid = true;
  228. // For each depth/stencil formats
  229. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  230. {
  231. if (depthFormats[depth] != GL_DEPTH24_STENCIL8_EXT && depthFormats[depth] != GL_DEPTH32F_STENCIL8)
  232. {
  233. if (_tryFormat(depthFormats[depth], GL_NONE))
  234. {
  235. /// Add mode to allowed modes
  236. FormatProperties::Mode mode;
  237. mode.depth = depth;
  238. mode.stencil = 0;
  239. mProps[x].modes.push_back(mode);
  240. }
  241. }
  242. else
  243. {
  244. // Packed depth/stencil format
  245. if (_tryPackedFormat(depthFormats[depth]))
  246. {
  247. /// Add mode to allowed modes
  248. FormatProperties::Mode mode;
  249. mode.depth = depth;
  250. mode.stencil = 0; // unuse
  251. mProps[x].modes.push_back(mode);
  252. }
  253. }
  254. }
  255. }
  256. // Delete texture and framebuffer
  257. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  258. glDeleteFramebuffersEXT(1, &fb);
  259. glFinish();
  260. if (fmt!=GL_NONE)
  261. glDeleteTextures(1, &tid);
  262. }
  263. // It seems a bug in nVidia driver: glBindFramebufferEXT should restore
  264. // draw and read buffers, but in some unclear circumstances it won't.
  265. glDrawBuffer(old_drawbuffer);
  266. glReadBuffer(old_readbuffer);
  267. String fmtstring = "";
  268. for(size_t x=0; x<PF_COUNT; ++x)
  269. {
  270. if(mProps[x].valid)
  271. fmtstring += PixelUtil::getFormatName((PixelFormat)x)+" ";
  272. }
  273. }
  274. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  275. {
  276. if(checkFormat(format))
  277. return format;
  278. /// Find first alternative
  279. PixelComponentType pct = PixelUtil::getComponentType(format);
  280. switch(pct)
  281. {
  282. case PCT_BYTE: format = PF_A8R8G8B8; break;
  283. case PCT_SHORT: format = PF_SHORT_RGBA; break;
  284. case PCT_FLOAT16: format = PF_FLOAT16_RGBA; break;
  285. case PCT_FLOAT32: format = PF_FLOAT32_RGBA; break;
  286. case PCT_COUNT: break;
  287. }
  288. if(checkFormat(format))
  289. return format;
  290. /// If none at all, return to default
  291. return PF_A8R8G8B8;
  292. }
  293. }