BsGLRenderTexture.cpp 11 KB

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