2
0

BsGLRenderTexture.cpp 11 KB

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