CmGLRenderTexture.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /// Stencil and depth formats to be tried
  70. static const GLenum stencilFormats[] =
  71. {
  72. GL_NONE, // No stencil
  73. GL_STENCIL_INDEX1_EXT,
  74. GL_STENCIL_INDEX4_EXT,
  75. GL_STENCIL_INDEX8_EXT,
  76. GL_STENCIL_INDEX16_EXT
  77. };
  78. static const UINT32 stencilBits[] =
  79. {
  80. 0, 1, 4, 8, 16
  81. };
  82. #define STENCILFORMAT_COUNT (sizeof(stencilFormats)/sizeof(GLenum))
  83. static const GLenum depthFormats[] =
  84. {
  85. GL_NONE,
  86. GL_DEPTH_COMPONENT16,
  87. GL_DEPTH_COMPONENT24, // Prefer 24 bit depth
  88. GL_DEPTH_COMPONENT32,
  89. GL_DEPTH24_STENCIL8_EXT // packed depth / stencil
  90. };
  91. static const UINT32 depthBits[] =
  92. {
  93. 0,16,24,32,24
  94. };
  95. #define DEPTHFORMAT_COUNT (sizeof(depthFormats)/sizeof(GLenum))
  96. GLRTTManager::GLRTTManager()
  97. {
  98. detectFBOFormats();
  99. glGenFramebuffersEXT(1, &mTempFBO);
  100. }
  101. GLRTTManager::~GLRTTManager()
  102. {
  103. glDeleteFramebuffersEXT(1, &mTempFBO);
  104. }
  105. /** Try a certain FBO format, and return the status. Also sets mDepthRB and mStencilRB.
  106. @returns true if this combo is supported
  107. false if this combo is not supported
  108. */
  109. GLuint GLRTTManager::_tryFormat(GLenum depthFormat, GLenum stencilFormat)
  110. {
  111. GLuint status, depthRB = 0, stencilRB = 0;
  112. bool failed = false; // flag on GL errors
  113. if(depthFormat != GL_NONE)
  114. {
  115. /// Generate depth renderbuffer
  116. glGenRenderbuffersEXT(1, &depthRB);
  117. /// Bind it to FBO
  118. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRB);
  119. /// Allocate storage for depth buffer
  120. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat,
  121. PROBE_SIZE, PROBE_SIZE);
  122. /// Attach depth
  123. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  124. GL_RENDERBUFFER_EXT, depthRB);
  125. }
  126. if(stencilFormat != GL_NONE)
  127. {
  128. /// Generate stencil renderbuffer
  129. glGenRenderbuffersEXT(1, &stencilRB);
  130. /// Bind it to FBO
  131. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencilRB);
  132. glGetError(); // NV hack
  133. /// Allocate storage for stencil buffer
  134. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, stencilFormat,
  135. PROBE_SIZE, PROBE_SIZE);
  136. if(glGetError() != GL_NO_ERROR) // NV hack
  137. failed = true;
  138. /// Attach stencil
  139. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  140. GL_RENDERBUFFER_EXT, stencilRB);
  141. if(glGetError() != GL_NO_ERROR) // NV hack
  142. failed = true;
  143. }
  144. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  145. /// If status is negative, clean up
  146. // Detach and destroy
  147. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  148. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  149. if (depthRB)
  150. glDeleteRenderbuffersEXT(1, &depthRB);
  151. if (stencilRB)
  152. glDeleteRenderbuffersEXT(1, &stencilRB);
  153. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  154. }
  155. /** Try a certain packed depth/stencil format, and return the status.
  156. @returns true if this combo is supported
  157. false if this combo is not supported
  158. */
  159. bool GLRTTManager::_tryPackedFormat(GLenum packedFormat)
  160. {
  161. GLuint packedRB = 0;
  162. bool failed = false; // flag on GL errors
  163. /// Generate renderbuffer
  164. glGenRenderbuffersEXT(1, &packedRB);
  165. /// Bind it to FBO
  166. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, packedRB);
  167. /// Allocate storage for buffer
  168. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, packedFormat, PROBE_SIZE, PROBE_SIZE);
  169. glGetError(); // NV hack
  170. /// Attach depth
  171. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  172. GL_RENDERBUFFER_EXT, packedRB);
  173. if(glGetError() != GL_NO_ERROR) // NV hack
  174. failed = true;
  175. /// Attach stencil
  176. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  177. GL_RENDERBUFFER_EXT, packedRB);
  178. if(glGetError() != GL_NO_ERROR) // NV hack
  179. failed = true;
  180. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  181. /// Detach and destroy
  182. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  183. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  184. glDeleteRenderbuffersEXT(1, &packedRB);
  185. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  186. }
  187. /** Detect which internal formats are allowed as RTT
  188. Also detect what combinations of stencil and depth are allowed with this internal
  189. format.
  190. */
  191. void GLRTTManager::detectFBOFormats()
  192. {
  193. // Try all formats, and report which ones work as target
  194. GLuint fb = 0, tid = 0;
  195. GLint old_drawbuffer = 0, old_readbuffer = 0;
  196. GLenum target = GL_TEXTURE_2D;
  197. glGetIntegerv (GL_DRAW_BUFFER, &old_drawbuffer);
  198. glGetIntegerv (GL_READ_BUFFER, &old_readbuffer);
  199. for(size_t x=0; x<PF_COUNT; ++x)
  200. {
  201. mProps[x].valid = false;
  202. // Fetch GL format token
  203. GLenum fmt = GLPixelUtil::getGLInternalFormat((PixelFormat)x);
  204. if(fmt == GL_NONE && x!=0)
  205. continue;
  206. // No test for compressed formats
  207. if(PixelUtil::isCompressed((PixelFormat)x))
  208. continue;
  209. // Create and attach framebuffer
  210. glGenFramebuffersEXT(1, &fb);
  211. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  212. if (fmt!=GL_NONE)
  213. {
  214. // Create and attach texture
  215. glGenTextures(1, &tid);
  216. glBindTexture(target, tid);
  217. // Set some default parameters so it won't fail on NVidia cards
  218. if (GLEW_VERSION_1_2)
  219. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
  220. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  221. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  222. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  223. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  224. glTexImage2D(target, 0, fmt, PROBE_SIZE, PROBE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  225. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  226. target, tid, 0);
  227. }
  228. else
  229. {
  230. // Draw to nowhere -- stencil/depth only
  231. glDrawBuffer(GL_NONE);
  232. glReadBuffer(GL_NONE);
  233. }
  234. // Check status
  235. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  236. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  237. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  238. // might still be supported, so we must continue probing.
  239. if(fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE_EXT)
  240. {
  241. mProps[x].valid = true;
  242. // For each depth/stencil formats
  243. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  244. {
  245. if (depthFormats[depth] != GL_DEPTH24_STENCIL8_EXT)
  246. {
  247. // General depth/stencil combination
  248. for (UINT32 stencil = 0; stencil < STENCILFORMAT_COUNT; ++stencil)
  249. {
  250. if (_tryFormat(depthFormats[depth], stencilFormats[stencil]))
  251. {
  252. /// Add mode to allowed modes
  253. FormatProperties::Mode mode;
  254. mode.depth = depth;
  255. mode.stencil = stencil;
  256. mProps[x].modes.push_back(mode);
  257. }
  258. }
  259. }
  260. else
  261. {
  262. // Packed depth/stencil format
  263. if (_tryPackedFormat(depthFormats[depth]))
  264. {
  265. /// Add mode to allowed modes
  266. FormatProperties::Mode mode;
  267. mode.depth = depth;
  268. mode.stencil = 0; // unuse
  269. mProps[x].modes.push_back(mode);
  270. }
  271. }
  272. }
  273. }
  274. // Delete texture and framebuffer
  275. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  276. glDeleteFramebuffersEXT(1, &fb);
  277. glFinish();
  278. if (fmt!=GL_NONE)
  279. glDeleteTextures(1, &tid);
  280. }
  281. // It seems a bug in nVidia driver: glBindFramebufferEXT should restore
  282. // draw and read buffers, but in some unclear circumstances it won't.
  283. glDrawBuffer(old_drawbuffer);
  284. glReadBuffer(old_readbuffer);
  285. String fmtstring = "";
  286. for(size_t x=0; x<PF_COUNT; ++x)
  287. {
  288. if(mProps[x].valid)
  289. fmtstring += PixelUtil::getFormatName((PixelFormat)x)+" ";
  290. }
  291. }
  292. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  293. {
  294. if(checkFormat(format))
  295. return format;
  296. /// Find first alternative
  297. PixelComponentType pct = PixelUtil::getComponentType(format);
  298. switch(pct)
  299. {
  300. case PCT_BYTE: format = PF_A8R8G8B8; break;
  301. case PCT_SHORT: format = PF_SHORT_RGBA; break;
  302. case PCT_FLOAT16: format = PF_FLOAT16_RGBA; break;
  303. case PCT_FLOAT32: format = PF_FLOAT32_RGBA; break;
  304. case PCT_COUNT: break;
  305. }
  306. if(checkFormat(format))
  307. return format;
  308. /// If none at all, return to default
  309. return PF_A8R8G8B8;
  310. }
  311. }