BsGLRenderTexture.cpp 11 KB

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