CmGLRenderTexture.cpp 13 KB

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