BsGLRenderTexture.cpp 11 KB

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