BsGLRenderTexture.cpp 11 KB

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