BsGLRenderTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. glGenFramebuffers(1, &mBlitReadFBO);
  69. glGenFramebuffers(1, &mBlitWriteFBO);
  70. }
  71. GLRTTManager::~GLRTTManager()
  72. {
  73. glDeleteFramebuffers(1, &mBlitReadFBO);
  74. glDeleteFramebuffers(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. glGenRenderbuffers(1, &depthRB);
  84. // Bind it to FBO
  85. glBindRenderbuffer(GL_RENDERBUFFER, depthRB);
  86. // Allocate storage for depth buffer
  87. glRenderbufferStorage(GL_RENDERBUFFER, depthFormat,
  88. PROBE_SIZE, PROBE_SIZE);
  89. // Attach depth
  90. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  91. GL_RENDERBUFFER, depthRB);
  92. }
  93. if(stencilFormat != GL_NONE)
  94. {
  95. // Generate stencil renderbuffer
  96. glGenRenderbuffers(1, &stencilRB);
  97. // Bind it to FBO
  98. glBindRenderbuffer(GL_RENDERBUFFER, stencilRB);
  99. glGetError();
  100. // Allocate storage for stencil buffer
  101. glRenderbufferStorage(GL_RENDERBUFFER, stencilFormat,
  102. PROBE_SIZE, PROBE_SIZE);
  103. if(glGetError() != GL_NO_ERROR)
  104. failed = true;
  105. // Attach stencil
  106. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  107. GL_RENDERBUFFER, stencilRB);
  108. if(glGetError() != GL_NO_ERROR)
  109. failed = true;
  110. }
  111. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  112. // Detach and destroy
  113. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  114. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
  115. if (depthRB)
  116. glDeleteRenderbuffers(1, &depthRB);
  117. if (stencilRB)
  118. glDeleteRenderbuffers(1, &stencilRB);
  119. return status == GL_FRAMEBUFFER_COMPLETE && !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. glGenRenderbuffers(1, &packedRB);
  127. // Bind it to FBO
  128. glBindRenderbuffer(GL_RENDERBUFFER, packedRB);
  129. // Allocate storage for buffer
  130. glRenderbufferStorage(GL_RENDERBUFFER, packedFormat, PROBE_SIZE, PROBE_SIZE);
  131. glGetError();
  132. // Attach depth
  133. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  134. GL_RENDERBUFFER, packedRB);
  135. if(glGetError() != GL_NO_ERROR)
  136. failed = true;
  137. // Attach stencil
  138. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  139. GL_RENDERBUFFER, packedRB);
  140. if(glGetError() != GL_NO_ERROR)
  141. failed = true;
  142. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  143. // Detach and destroy
  144. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  145. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
  146. glDeleteRenderbuffers(1, &packedRB);
  147. return status == GL_FRAMEBUFFER_COMPLETE && !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. glGenFramebuffers(1, &fb);
  169. glBindFramebuffer(GL_FRAMEBUFFER, 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. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, tid, 0);
  184. }
  185. else
  186. {
  187. // Draw to nowhere -- stencil/depth only
  188. glDrawBuffer(GL_NONE);
  189. glReadBuffer(GL_NONE);
  190. }
  191. // Check status
  192. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  193. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  194. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  195. // might still be supported, so we must continue probing.
  196. if(fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE)
  197. {
  198. mProps[x].valid = true;
  199. // For each depth/stencil formats
  200. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  201. {
  202. if (depthFormats[depth] != GL_DEPTH24_STENCIL8 && depthFormats[depth] != GL_DEPTH32F_STENCIL8)
  203. {
  204. if (_tryFormat(depthFormats[depth], GL_NONE))
  205. {
  206. /// Add mode to allowed modes
  207. FormatProperties::Mode mode;
  208. mode.depth = depth;
  209. mode.stencil = 0;
  210. mProps[x].modes.push_back(mode);
  211. }
  212. }
  213. else
  214. {
  215. // Packed depth/stencil format
  216. if (_tryPackedFormat(depthFormats[depth]))
  217. {
  218. /// Add mode to allowed modes
  219. FormatProperties::Mode mode;
  220. mode.depth = depth;
  221. mode.stencil = 0; // unuse
  222. mProps[x].modes.push_back(mode);
  223. }
  224. }
  225. }
  226. }
  227. // Delete texture and framebuffer
  228. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  229. glDeleteFramebuffers(1, &fb);
  230. glFinish();
  231. if (fmt != GL_NONE)
  232. glDeleteTextures(1, &tid);
  233. }
  234. // It seems a bug in nVidia driver: glBindFramebuffer should restore
  235. // draw and read buffers, but in some unclear circumstances it won't.
  236. glDrawBuffer(old_drawbuffer);
  237. glReadBuffer(old_readbuffer);
  238. String fmtstring = "";
  239. for(size_t x = 0; x < PF_COUNT; ++x)
  240. {
  241. if(mProps[x].valid)
  242. fmtstring += PixelUtil::getFormatName((PixelFormat)x)+" ";
  243. }
  244. }
  245. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  246. {
  247. if(checkFormat(format))
  248. return format;
  249. // Find first alternative
  250. PixelComponentType pct = PixelUtil::getElementType(format);
  251. switch(pct)
  252. {
  253. case PCT_BYTE: format = PF_A8R8G8B8; break;
  254. case PCT_FLOAT16: format = PF_FLOAT16_RGBA; break;
  255. case PCT_FLOAT32: format = PF_FLOAT32_RGBA; break;
  256. case PCT_COUNT: break;
  257. }
  258. if(checkFormat(format))
  259. return format;
  260. // If none at all, return to default
  261. return PF_A8R8G8B8;
  262. }
  263. }