BsGLRenderTexture.cpp 11 KB

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