BsGLRenderTexture.cpp 11 KB

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