BsGLRenderTexture.cpp 12 KB

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