BsGLRenderTexture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLRenderTexture.h"
  4. #include "BsGLPixelFormat.h"
  5. #include "BsGLPixelBuffer.h"
  6. #include "RenderAPI/BsTextureView.h"
  7. namespace bs
  8. {
  9. #define PROBE_SIZE 16
  10. static const GLenum depthFormats[] =
  11. {
  12. GL_NONE,
  13. GL_DEPTH_COMPONENT16,
  14. GL_DEPTH_COMPONENT32,
  15. GL_DEPTH24_STENCIL8,
  16. GL_DEPTH32F_STENCIL8
  17. };
  18. #define DEPTHFORMAT_COUNT (sizeof(depthFormats)/sizeof(GLenum))
  19. GLRenderTexture::GLRenderTexture(const RENDER_TEXTURE_DESC& desc)
  20. :RenderTexture(desc), mProperties(desc, true)
  21. {
  22. }
  23. namespace ct
  24. {
  25. GLRenderTexture::GLRenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx)
  26. :RenderTexture(desc, deviceIdx), mProperties(desc, true), mFB(nullptr)
  27. {
  28. assert(deviceIdx == 0 && "Multiple GPUs not supported natively on OpenGL.");
  29. }
  30. GLRenderTexture::~GLRenderTexture()
  31. {
  32. if (mFB != nullptr)
  33. bs_delete(mFB);
  34. }
  35. void GLRenderTexture::initialize()
  36. {
  37. RenderTexture::initialize();
  38. if (mFB != nullptr)
  39. bs_delete(mFB);
  40. mFB = bs_new<GLFrameBufferObject>();
  41. for (size_t i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  42. {
  43. if (mColorSurfaces[i] != nullptr)
  44. {
  45. GLTexture* glColorSurface = static_cast<GLTexture*>(mDesc.colorSurfaces[i].texture.get());
  46. GLSurfaceDesc surfaceDesc;
  47. surfaceDesc.numSamples = getProperties().multisampleCount;
  48. if (mColorSurfaces[i]->getNumArraySlices() == 1) // Binding a single texture layer
  49. {
  50. surfaceDesc.allLayers = glColorSurface->getProperties().getNumFaces() == 1;
  51. if (glColorSurface->getProperties().getTextureType() != TEX_TYPE_3D)
  52. {
  53. surfaceDesc.zoffset = 0;
  54. surfaceDesc.buffer = glColorSurface->getBuffer(mColorSurfaces[i]->getFirstArraySlice(),
  55. mColorSurfaces[i]->getMostDetailedMip());
  56. }
  57. else
  58. {
  59. surfaceDesc.zoffset = 0;
  60. surfaceDesc.buffer = glColorSurface->getBuffer(0, mColorSurfaces[i]->getMostDetailedMip());
  61. }
  62. }
  63. else // Binding an array of textures or a range of 3D texture slices
  64. {
  65. surfaceDesc.allLayers = true;
  66. if (glColorSurface->getProperties().getTextureType() != TEX_TYPE_3D)
  67. {
  68. if (mColorSurfaces[i]->getNumArraySlices() != glColorSurface->getProperties().getNumFaces())
  69. LOGWRN("OpenGL doesn't support binding of arbitrary ranges for array textures. The entire range will be bound instead.");
  70. surfaceDesc.zoffset = 0;
  71. surfaceDesc.buffer = glColorSurface->getBuffer(0, mColorSurfaces[i]->getMostDetailedMip());
  72. }
  73. else
  74. {
  75. surfaceDesc.zoffset = 0;
  76. surfaceDesc.buffer = glColorSurface->getBuffer(0, mColorSurfaces[i]->getMostDetailedMip());
  77. }
  78. }
  79. mFB->bindSurface((UINT32)i, surfaceDesc);
  80. }
  81. else
  82. {
  83. mFB->unbindSurface((UINT32)i);
  84. }
  85. }
  86. if (mDepthStencilSurface != nullptr && mDesc.depthStencilSurface.texture != nullptr)
  87. {
  88. GLTexture* glDepthStencilTexture = static_cast<GLTexture*>(mDesc.depthStencilSurface.texture.get());
  89. SPtr<GLPixelBuffer> depthStencilBuffer = nullptr;
  90. bool allLayers = true;
  91. if (mDepthStencilSurface->getNumArraySlices() == 1) // Binding a single texture layer
  92. allLayers = glDepthStencilTexture->getProperties().getNumFaces() == 1;
  93. if (glDepthStencilTexture->getProperties().getTextureType() != TEX_TYPE_3D)
  94. {
  95. UINT32 firstSlice = 0;
  96. if (!allLayers)
  97. firstSlice = mDepthStencilSurface->getFirstArraySlice();
  98. depthStencilBuffer = glDepthStencilTexture->getBuffer(firstSlice,
  99. mDepthStencilSurface->getMostDetailedMip());
  100. }
  101. mFB->bindDepthStencil(depthStencilBuffer, allLayers);
  102. }
  103. mFB->rebuild();
  104. }
  105. void GLRenderTexture::getCustomAttribute(const String& name, void* data) const
  106. {
  107. if(name=="FBO")
  108. {
  109. *static_cast<GLFrameBufferObject**>(data) = mFB;
  110. }
  111. else if (name == "GL_FBOID" || name == "GL_MULTISAMPLEFBOID")
  112. {
  113. *static_cast<GLuint*>(data) = mFB->getGLFBOID();
  114. }
  115. }
  116. GLRTTManager::GLRTTManager()
  117. :mBlitReadFBO(0), mBlitWriteFBO(0)
  118. {
  119. detectFBOFormats();
  120. glGenFramebuffers(1, &mBlitReadFBO);
  121. BS_CHECK_GL_ERROR();
  122. glGenFramebuffers(1, &mBlitWriteFBO);
  123. BS_CHECK_GL_ERROR();
  124. }
  125. GLRTTManager::~GLRTTManager()
  126. {
  127. glDeleteFramebuffers(1, &mBlitReadFBO);
  128. BS_CHECK_GL_ERROR();
  129. glDeleteFramebuffers(1, &mBlitWriteFBO);
  130. BS_CHECK_GL_ERROR();
  131. }
  132. bool GLRTTManager::_tryFormat(GLenum depthFormat, GLenum stencilFormat)
  133. {
  134. GLuint status, depthRB = 0, stencilRB = 0;
  135. bool failed = false;
  136. if (depthFormat != GL_NONE)
  137. {
  138. // Generate depth renderbuffer
  139. glGenRenderbuffers(1, &depthRB);
  140. BS_CHECK_GL_ERROR();
  141. // Bind it to FBO
  142. glBindRenderbuffer(GL_RENDERBUFFER, depthRB);
  143. BS_CHECK_GL_ERROR();
  144. // Allocate storage for depth buffer
  145. glRenderbufferStorage(GL_RENDERBUFFER, depthFormat, PROBE_SIZE, PROBE_SIZE);
  146. if (glGetError() != GL_NO_ERROR)
  147. failed = true;
  148. // Attach depth
  149. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRB);
  150. if (glGetError() != GL_NO_ERROR)
  151. failed = true;
  152. }
  153. if (stencilFormat != GL_NONE)
  154. {
  155. // Generate stencil renderbuffer
  156. glGenRenderbuffers(1, &stencilRB);
  157. BS_CHECK_GL_ERROR();
  158. // Bind it to FBO
  159. glBindRenderbuffer(GL_RENDERBUFFER, stencilRB);
  160. BS_CHECK_GL_ERROR();
  161. // Allocate storage for stencil buffer
  162. glRenderbufferStorage(GL_RENDERBUFFER, stencilFormat, PROBE_SIZE, PROBE_SIZE);
  163. if (glGetError() != GL_NO_ERROR)
  164. failed = true;
  165. // Attach stencil
  166. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilRB);
  167. if (glGetError() != GL_NO_ERROR)
  168. failed = true;
  169. }
  170. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  171. BS_CHECK_GL_ERROR();
  172. // Detach and destroy
  173. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  174. BS_CHECK_GL_ERROR();
  175. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
  176. BS_CHECK_GL_ERROR();
  177. if (depthRB)
  178. {
  179. glDeleteRenderbuffers(1, &depthRB);
  180. BS_CHECK_GL_ERROR();
  181. }
  182. if (stencilRB)
  183. {
  184. glDeleteRenderbuffers(1, &stencilRB);
  185. BS_CHECK_GL_ERROR();
  186. }
  187. return status == GL_FRAMEBUFFER_COMPLETE && !failed;
  188. }
  189. bool GLRTTManager::_tryPackedFormat(GLenum packedFormat)
  190. {
  191. GLuint packedRB = 0;
  192. bool failed = false; // flag on GL errors
  193. // Generate renderbuffer
  194. glGenRenderbuffers(1, &packedRB);
  195. BS_CHECK_GL_ERROR();
  196. // Bind it to FBO
  197. glBindRenderbuffer(GL_RENDERBUFFER, packedRB);
  198. BS_CHECK_GL_ERROR();
  199. // Allocate storage for buffer
  200. glRenderbufferStorage(GL_RENDERBUFFER, packedFormat, PROBE_SIZE, PROBE_SIZE);
  201. if (glGetError() != GL_NO_ERROR)
  202. failed = true;
  203. // Attach depth
  204. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  205. GL_RENDERBUFFER, packedRB);
  206. if (glGetError() != GL_NO_ERROR)
  207. failed = true;
  208. // Attach stencil
  209. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  210. GL_RENDERBUFFER, packedRB);
  211. if (glGetError() != GL_NO_ERROR)
  212. failed = true;
  213. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  214. BS_CHECK_GL_ERROR();
  215. // Detach and destroy
  216. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  217. BS_CHECK_GL_ERROR();
  218. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
  219. BS_CHECK_GL_ERROR();
  220. glDeleteRenderbuffers(1, &packedRB);
  221. BS_CHECK_GL_ERROR();
  222. return status == GL_FRAMEBUFFER_COMPLETE && !failed;
  223. }
  224. void GLRTTManager::detectFBOFormats()
  225. {
  226. // Try all formats, and report which ones work as target
  227. GLuint fb = 0, tid = 0;
  228. GLint oldDrawbuffer = 0, oldReadbuffer = 0;
  229. GLenum target = GL_TEXTURE_2D;
  230. glGetIntegerv(GL_DRAW_BUFFER, &oldDrawbuffer);
  231. BS_CHECK_GL_ERROR();
  232. glGetIntegerv(GL_READ_BUFFER, &oldReadbuffer);
  233. BS_CHECK_GL_ERROR();
  234. for (UINT32 x = 0; x < PF_COUNT; ++x)
  235. {
  236. mProps[x].valid = false;
  237. // Fetch GL format token
  238. GLenum fmt = GLPixelUtil::getGLInternalFormat((PixelFormat)x);
  239. if (fmt == GL_NONE && x != 0)
  240. continue;
  241. // No test for compressed formats
  242. if(PixelUtil::isCompressed((PixelFormat)x))
  243. continue;
  244. // No test for unnormalized integer targets
  245. if (!PixelUtil::isNormalized((PixelFormat)x) && !PixelUtil::isFloatingPoint((PixelFormat)x))
  246. continue;
  247. // Create and attach framebuffer
  248. glGenFramebuffers(1, &fb);
  249. BS_CHECK_GL_ERROR();
  250. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  251. BS_CHECK_GL_ERROR();
  252. if (fmt != GL_NONE && !PixelUtil::isDepth((PixelFormat)x))
  253. {
  254. // Create and attach texture
  255. glGenTextures(1, &tid);
  256. BS_CHECK_GL_ERROR();
  257. glBindTexture(target, tid);
  258. BS_CHECK_GL_ERROR();
  259. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
  260. BS_CHECK_GL_ERROR();
  261. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  262. BS_CHECK_GL_ERROR();
  263. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  264. BS_CHECK_GL_ERROR();
  265. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  266. BS_CHECK_GL_ERROR();
  267. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  268. BS_CHECK_GL_ERROR();
  269. glTexImage2D(target, 0, fmt, PROBE_SIZE, PROBE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  270. BS_CHECK_GL_ERROR();
  271. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, tid, 0);
  272. BS_CHECK_GL_ERROR();
  273. }
  274. else
  275. {
  276. // Draw to nowhere (stencil/depth only)
  277. glDrawBuffer(GL_NONE);
  278. BS_CHECK_GL_ERROR();
  279. glReadBuffer(GL_NONE);
  280. BS_CHECK_GL_ERROR();
  281. }
  282. // Check status
  283. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  284. BS_CHECK_GL_ERROR();
  285. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  286. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  287. // might still be supported, so we must continue probing.
  288. if (fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE)
  289. {
  290. mProps[x].valid = true;
  291. // For each depth/stencil formats
  292. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  293. {
  294. if (depthFormats[depth] != GL_DEPTH24_STENCIL8 && depthFormats[depth] != GL_DEPTH32F_STENCIL8)
  295. {
  296. if (_tryFormat(depthFormats[depth], GL_NONE))
  297. {
  298. /// Add mode to allowed modes
  299. FormatProperties::Mode mode;
  300. mode.depth = depth;
  301. mode.stencil = 0;
  302. mProps[x].modes.push_back(mode);
  303. }
  304. }
  305. else
  306. {
  307. // Packed depth/stencil format
  308. if (_tryPackedFormat(depthFormats[depth]))
  309. {
  310. /// Add mode to allowed modes
  311. FormatProperties::Mode mode;
  312. mode.depth = depth;
  313. mode.stencil = 0; // unuse
  314. mProps[x].modes.push_back(mode);
  315. }
  316. }
  317. }
  318. }
  319. // Delete texture and framebuffer
  320. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  321. BS_CHECK_GL_ERROR();
  322. glDeleteFramebuffers(1, &fb);
  323. BS_CHECK_GL_ERROR();
  324. glFinish();
  325. BS_CHECK_GL_ERROR();
  326. if (fmt != GL_NONE)
  327. {
  328. glDeleteTextures(1, &tid);
  329. BS_CHECK_GL_ERROR();
  330. }
  331. }
  332. glDrawBuffer(oldDrawbuffer);
  333. BS_CHECK_GL_ERROR();
  334. glReadBuffer(oldReadbuffer);
  335. BS_CHECK_GL_ERROR();
  336. }
  337. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  338. {
  339. if (checkFormat(format))
  340. return format;
  341. // Find first alternative
  342. PixelComponentType pct = PixelUtil::getElementType(format);
  343. switch (pct)
  344. {
  345. case PCT_BYTE: format = PF_RGBA8; break;
  346. case PCT_FLOAT16: format = PF_RGBA16F; break;
  347. case PCT_FLOAT32: format = PF_RGBA32F; break;
  348. default: break;
  349. }
  350. if (checkFormat(format))
  351. return format;
  352. // If none at all, return to default
  353. return PF_RGBA8;
  354. }
  355. }
  356. }