2
0

BsGLRenderTexture.cpp 11 KB

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