BsGLRenderTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. {
  66. detectFBOFormats();
  67. glGenFramebuffersEXT(1, &mTempFBO);
  68. }
  69. GLRTTManager::~GLRTTManager()
  70. {
  71. glDeleteFramebuffersEXT(1, &mTempFBO);
  72. }
  73. bool GLRTTManager::_tryFormat(GLenum depthFormat, GLenum stencilFormat)
  74. {
  75. GLuint status, depthRB = 0, stencilRB = 0;
  76. bool failed = false;
  77. if(depthFormat != GL_NONE)
  78. {
  79. // Generate depth renderbuffer
  80. glGenRenderbuffersEXT(1, &depthRB);
  81. // Bind it to FBO
  82. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthRB);
  83. // Allocate storage for depth buffer
  84. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat,
  85. PROBE_SIZE, PROBE_SIZE);
  86. // Attach depth
  87. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  88. GL_RENDERBUFFER_EXT, depthRB);
  89. }
  90. if(stencilFormat != GL_NONE)
  91. {
  92. // Generate stencil renderbuffer
  93. glGenRenderbuffersEXT(1, &stencilRB);
  94. // Bind it to FBO
  95. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencilRB);
  96. glGetError();
  97. // Allocate storage for stencil buffer
  98. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, stencilFormat,
  99. PROBE_SIZE, PROBE_SIZE);
  100. if(glGetError() != GL_NO_ERROR)
  101. failed = true;
  102. // Attach stencil
  103. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  104. GL_RENDERBUFFER_EXT, stencilRB);
  105. if(glGetError() != GL_NO_ERROR)
  106. failed = true;
  107. }
  108. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  109. // Detach and destroy
  110. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  111. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  112. if (depthRB)
  113. glDeleteRenderbuffersEXT(1, &depthRB);
  114. if (stencilRB)
  115. glDeleteRenderbuffersEXT(1, &stencilRB);
  116. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  117. }
  118. bool GLRTTManager::_tryPackedFormat(GLenum packedFormat)
  119. {
  120. GLuint packedRB = 0;
  121. bool failed = false; // flag on GL errors
  122. // Generate renderbuffer
  123. glGenRenderbuffersEXT(1, &packedRB);
  124. // Bind it to FBO
  125. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, packedRB);
  126. // Allocate storage for buffer
  127. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, packedFormat, PROBE_SIZE, PROBE_SIZE);
  128. glGetError();
  129. // Attach depth
  130. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  131. GL_RENDERBUFFER_EXT, packedRB);
  132. if(glGetError() != GL_NO_ERROR)
  133. failed = true;
  134. // Attach stencil
  135. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  136. GL_RENDERBUFFER_EXT, packedRB);
  137. if(glGetError() != GL_NO_ERROR)
  138. failed = true;
  139. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  140. // Detach and destroy
  141. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  142. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  143. glDeleteRenderbuffersEXT(1, &packedRB);
  144. return status == GL_FRAMEBUFFER_COMPLETE_EXT && !failed;
  145. }
  146. void GLRTTManager::detectFBOFormats()
  147. {
  148. // Try all formats, and report which ones work as target
  149. GLuint fb = 0, tid = 0;
  150. GLint old_drawbuffer = 0, old_readbuffer = 0;
  151. GLenum target = GL_TEXTURE_2D;
  152. glGetIntegerv (GL_DRAW_BUFFER, &old_drawbuffer);
  153. glGetIntegerv (GL_READ_BUFFER, &old_readbuffer);
  154. for(size_t x=0; x<PF_COUNT; ++x)
  155. {
  156. mProps[x].valid = false;
  157. // Fetch GL format token
  158. GLenum fmt = GLPixelUtil::getGLInternalFormat((PixelFormat)x);
  159. if(fmt == GL_NONE && x!=0)
  160. continue;
  161. // No test for compressed formats
  162. if(PixelUtil::isCompressed((PixelFormat)x))
  163. continue;
  164. // Create and attach framebuffer
  165. glGenFramebuffersEXT(1, &fb);
  166. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  167. if (fmt!=GL_NONE && !PixelUtil::isDepth((PixelFormat)x))
  168. {
  169. // Create and attach texture
  170. glGenTextures(1, &tid);
  171. glBindTexture(target, tid);
  172. // Set some default parameters so it won't fail on NVidia cards
  173. if (GLEW_VERSION_1_2)
  174. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
  175. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  176. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  177. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  178. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  179. glTexImage2D(target, 0, fmt, PROBE_SIZE, PROBE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  180. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  181. target, tid, 0);
  182. }
  183. else
  184. {
  185. // Draw to nowhere -- stencil/depth only
  186. glDrawBuffer(GL_NONE);
  187. glReadBuffer(GL_NONE);
  188. }
  189. // Check status
  190. GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  191. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  192. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  193. // might still be supported, so we must continue probing.
  194. if(fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE_EXT)
  195. {
  196. mProps[x].valid = true;
  197. // For each depth/stencil formats
  198. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  199. {
  200. if (depthFormats[depth] != GL_DEPTH24_STENCIL8_EXT && depthFormats[depth] != GL_DEPTH32F_STENCIL8)
  201. {
  202. if (_tryFormat(depthFormats[depth], GL_NONE))
  203. {
  204. /// Add mode to allowed modes
  205. FormatProperties::Mode mode;
  206. mode.depth = depth;
  207. mode.stencil = 0;
  208. mProps[x].modes.push_back(mode);
  209. }
  210. }
  211. else
  212. {
  213. // Packed depth/stencil format
  214. if (_tryPackedFormat(depthFormats[depth]))
  215. {
  216. /// Add mode to allowed modes
  217. FormatProperties::Mode mode;
  218. mode.depth = depth;
  219. mode.stencil = 0; // unuse
  220. mProps[x].modes.push_back(mode);
  221. }
  222. }
  223. }
  224. }
  225. // Delete texture and framebuffer
  226. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  227. glDeleteFramebuffersEXT(1, &fb);
  228. glFinish();
  229. if (fmt!=GL_NONE)
  230. glDeleteTextures(1, &tid);
  231. }
  232. // It seems a bug in nVidia driver: glBindFramebufferEXT should restore
  233. // draw and read buffers, but in some unclear circumstances it won't.
  234. glDrawBuffer(old_drawbuffer);
  235. glReadBuffer(old_readbuffer);
  236. String fmtstring = "";
  237. for(size_t x=0; x<PF_COUNT; ++x)
  238. {
  239. if(mProps[x].valid)
  240. fmtstring += PixelUtil::getFormatName((PixelFormat)x)+" ";
  241. }
  242. }
  243. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  244. {
  245. if(checkFormat(format))
  246. return format;
  247. // Find first alternative
  248. PixelComponentType pct = PixelUtil::getElementType(format);
  249. switch(pct)
  250. {
  251. case PCT_BYTE: format = PF_A8R8G8B8; break;
  252. case PCT_FLOAT16: format = PF_FLOAT16_RGBA; break;
  253. case PCT_FLOAT32: format = PF_FLOAT32_RGBA; break;
  254. case PCT_COUNT: break;
  255. }
  256. if(checkFormat(format))
  257. return format;
  258. // If none at all, return to default
  259. return PF_A8R8G8B8;
  260. }
  261. }