BsGLRenderTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. glGenFramebuffers(1, &mBlitWriteFBO);
  122. }
  123. GLRTTManager::~GLRTTManager()
  124. {
  125. glDeleteFramebuffers(1, &mBlitReadFBO);
  126. glDeleteFramebuffers(1, &mBlitWriteFBO);
  127. }
  128. bool GLRTTManager::_tryFormat(GLenum depthFormat, GLenum stencilFormat)
  129. {
  130. GLuint status, depthRB = 0, stencilRB = 0;
  131. bool failed = false;
  132. if (depthFormat != GL_NONE)
  133. {
  134. // Generate depth renderbuffer
  135. glGenRenderbuffers(1, &depthRB);
  136. // Bind it to FBO
  137. glBindRenderbuffer(GL_RENDERBUFFER, depthRB);
  138. // Allocate storage for depth buffer
  139. glRenderbufferStorage(GL_RENDERBUFFER, depthFormat,
  140. PROBE_SIZE, PROBE_SIZE);
  141. // Attach depth
  142. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  143. GL_RENDERBUFFER, depthRB);
  144. }
  145. if (stencilFormat != GL_NONE)
  146. {
  147. // Generate stencil renderbuffer
  148. glGenRenderbuffers(1, &stencilRB);
  149. // Bind it to FBO
  150. glBindRenderbuffer(GL_RENDERBUFFER, stencilRB);
  151. glGetError();
  152. // Allocate storage for stencil buffer
  153. glRenderbufferStorage(GL_RENDERBUFFER, stencilFormat,
  154. PROBE_SIZE, PROBE_SIZE);
  155. if (glGetError() != GL_NO_ERROR)
  156. failed = true;
  157. // Attach stencil
  158. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  159. GL_RENDERBUFFER, stencilRB);
  160. if (glGetError() != GL_NO_ERROR)
  161. failed = true;
  162. }
  163. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  164. // Detach and destroy
  165. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  166. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
  167. if (depthRB)
  168. glDeleteRenderbuffers(1, &depthRB);
  169. if (stencilRB)
  170. glDeleteRenderbuffers(1, &stencilRB);
  171. return status == GL_FRAMEBUFFER_COMPLETE && !failed;
  172. }
  173. bool GLRTTManager::_tryPackedFormat(GLenum packedFormat)
  174. {
  175. GLuint packedRB = 0;
  176. bool failed = false; // flag on GL errors
  177. // Generate renderbuffer
  178. glGenRenderbuffers(1, &packedRB);
  179. // Bind it to FBO
  180. glBindRenderbuffer(GL_RENDERBUFFER, packedRB);
  181. // Allocate storage for buffer
  182. glRenderbufferStorage(GL_RENDERBUFFER, packedFormat, PROBE_SIZE, PROBE_SIZE);
  183. glGetError();
  184. // Attach depth
  185. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  186. GL_RENDERBUFFER, packedRB);
  187. if (glGetError() != GL_NO_ERROR)
  188. failed = true;
  189. // Attach stencil
  190. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  191. GL_RENDERBUFFER, packedRB);
  192. if (glGetError() != GL_NO_ERROR)
  193. failed = true;
  194. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  195. // Detach and destroy
  196. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
  197. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
  198. glDeleteRenderbuffers(1, &packedRB);
  199. return status == GL_FRAMEBUFFER_COMPLETE && !failed;
  200. }
  201. void GLRTTManager::detectFBOFormats()
  202. {
  203. // Try all formats, and report which ones work as target
  204. GLuint fb = 0, tid = 0;
  205. GLint oldDrawbuffer = 0, oldReadbuffer = 0;
  206. GLenum target = GL_TEXTURE_2D;
  207. glGetIntegerv(GL_DRAW_BUFFER, &oldDrawbuffer);
  208. glGetIntegerv(GL_READ_BUFFER, &oldReadbuffer);
  209. for (size_t x = 0; x < PF_COUNT; ++x)
  210. {
  211. mProps[x].valid = false;
  212. // Fetch GL format token
  213. GLenum fmt = GLPixelUtil::getGLInternalFormat((PixelFormat)x);
  214. if (fmt == GL_NONE && x != 0)
  215. continue;
  216. // No test for compressed formats
  217. if(PixelUtil::isCompressed((PixelFormat)x))
  218. continue;
  219. // No test for unnormalized integer targets
  220. if (!PixelUtil::isNormalized((PixelFormat)x) && !PixelUtil::isFloatingPoint((PixelFormat)x))
  221. continue;
  222. // Create and attach framebuffer
  223. glGenFramebuffers(1, &fb);
  224. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  225. if (fmt != GL_NONE && !PixelUtil::isDepth((PixelFormat)x))
  226. {
  227. // Create and attach texture
  228. glGenTextures(1, &tid);
  229. glBindTexture(target, tid);
  230. // Set some default parameters so it won't fail on NVidia cards
  231. if (GLEW_VERSION_1_2)
  232. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
  233. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  234. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  235. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  236. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  237. glTexImage2D(target, 0, fmt, PROBE_SIZE, PROBE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  238. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, tid, 0);
  239. }
  240. else
  241. {
  242. // Draw to nowhere -- stencil/depth only
  243. glDrawBuffer(GL_NONE);
  244. glReadBuffer(GL_NONE);
  245. }
  246. // Check status
  247. GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  248. // Ignore status in case of fmt==GL_NONE, because no implementation will accept
  249. // a buffer without *any* attachment. Buffers with only stencil and depth attachment
  250. // might still be supported, so we must continue probing.
  251. if (fmt == GL_NONE || status == GL_FRAMEBUFFER_COMPLETE)
  252. {
  253. mProps[x].valid = true;
  254. // For each depth/stencil formats
  255. for (UINT32 depth = 0; depth < DEPTHFORMAT_COUNT; ++depth)
  256. {
  257. if (depthFormats[depth] != GL_DEPTH24_STENCIL8 && depthFormats[depth] != GL_DEPTH32F_STENCIL8)
  258. {
  259. if (_tryFormat(depthFormats[depth], GL_NONE))
  260. {
  261. /// Add mode to allowed modes
  262. FormatProperties::Mode mode;
  263. mode.depth = depth;
  264. mode.stencil = 0;
  265. mProps[x].modes.push_back(mode);
  266. }
  267. }
  268. else
  269. {
  270. // Packed depth/stencil format
  271. if (_tryPackedFormat(depthFormats[depth]))
  272. {
  273. /// Add mode to allowed modes
  274. FormatProperties::Mode mode;
  275. mode.depth = depth;
  276. mode.stencil = 0; // unuse
  277. mProps[x].modes.push_back(mode);
  278. }
  279. }
  280. }
  281. }
  282. // Delete texture and framebuffer
  283. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  284. glDeleteFramebuffers(1, &fb);
  285. glFinish();
  286. if (fmt != GL_NONE)
  287. glDeleteTextures(1, &tid);
  288. }
  289. glDrawBuffer(oldDrawbuffer);
  290. glReadBuffer(oldReadbuffer);
  291. }
  292. PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format)
  293. {
  294. if (checkFormat(format))
  295. return format;
  296. // Find first alternative
  297. PixelComponentType pct = PixelUtil::getElementType(format);
  298. switch (pct)
  299. {
  300. case PCT_BYTE: format = PF_RGBA8; break;
  301. case PCT_FLOAT16: format = PF_RGBA16F; break;
  302. case PCT_FLOAT32: format = PF_RGBA32F; break;
  303. default: break;
  304. }
  305. if (checkFormat(format))
  306. return format;
  307. // If none at all, return to default
  308. return PF_RGBA8;
  309. }
  310. }
  311. }