FramebufferImpl.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/gl/FramebufferImpl.h>
  6. #include <AnKi/Gr/Texture.h>
  7. #include <AnKi/Gr/gl/TextureViewImpl.h>
  8. #include <AnKi/Gr/gl/GlState.h>
  9. #include <AnKi/Gr/GrManager.h>
  10. #include <AnKi/Gr/gl/GrManagerImpl.h>
  11. #include <AnKi/Gr/gl/RenderingThread.h>
  12. #include <AnKi/Util/Logger.h>
  13. namespace anki {
  14. Error FramebufferImpl::init(const FramebufferInitInfo& init)
  15. {
  16. ANKI_ASSERT(!isCreated());
  17. m_in = init;
  18. glGenFramebuffers(1, &m_glName);
  19. ANKI_ASSERT(m_glName != 0);
  20. const GLenum target = GL_FRAMEBUFFER;
  21. glBindFramebuffer(target, m_glName);
  22. // Attach color
  23. for(U i = 0; i < m_in.m_colorAttachmentCount; i++)
  24. {
  25. const FramebufferAttachmentInfo& att = m_in.m_colorAttachments[i];
  26. const TextureViewImpl& viewImpl = static_cast<const TextureViewImpl&>(*att.m_textureView);
  27. ANKI_ASSERT(viewImpl.m_tex->isSubresourceGoodForFramebufferAttachment(viewImpl.getSubresource()));
  28. const GLenum binding = GL_COLOR_ATTACHMENT0 + i;
  29. attachTextureInternal(binding, viewImpl, att);
  30. m_drawBuffers[i] = binding;
  31. if(att.m_loadOperation == AttachmentLoadOperation::kDontCare)
  32. {
  33. m_invalidateBuffers[m_invalidateBuffersCount++] = binding;
  34. }
  35. if(m_fbSize[0] == 0)
  36. {
  37. m_fbSize[0] = viewImpl.m_tex->getWidth() >> viewImpl.getSubresource().m_firstMipmap;
  38. m_fbSize[1] = viewImpl.m_tex->getHeight() >> viewImpl.getSubresource().m_firstMipmap;
  39. }
  40. else
  41. {
  42. ANKI_ASSERT(m_fbSize[0] == (viewImpl.m_tex->getWidth() >> viewImpl.getSubresource().m_firstMipmap));
  43. ANKI_ASSERT(m_fbSize[1] == (viewImpl.m_tex->getHeight() >> viewImpl.getSubresource().m_firstMipmap));
  44. }
  45. }
  46. // Attach depth/stencil
  47. if(m_in.m_depthStencilAttachment.m_textureView.isCreated())
  48. {
  49. const FramebufferAttachmentInfo& att = m_in.m_depthStencilAttachment;
  50. const TextureViewImpl& viewImpl = static_cast<const TextureViewImpl&>(*att.m_textureView);
  51. ANKI_ASSERT(viewImpl.m_tex->isSubresourceGoodForFramebufferAttachment(viewImpl.getSubresource()));
  52. GLenum binding;
  53. if(viewImpl.getSubresource().m_depthStencilAspect == DepthStencilAspectBit::kDepth)
  54. {
  55. binding = GL_DEPTH_ATTACHMENT;
  56. }
  57. else if(viewImpl.getSubresource().m_depthStencilAspect == DepthStencilAspectBit::STENCIL)
  58. {
  59. binding = GL_STENCIL_ATTACHMENT;
  60. }
  61. else
  62. {
  63. ANKI_ASSERT(viewImpl.getSubresource().m_depthStencilAspect == DepthStencilAspectBit::DEPTH_STENCIL);
  64. binding = GL_DEPTH_STENCIL_ATTACHMENT;
  65. }
  66. attachTextureInternal(binding, viewImpl, att);
  67. if(att.m_loadOperation == AttachmentLoadOperation::kDontCare)
  68. {
  69. m_invalidateBuffers[m_invalidateBuffersCount++] = binding;
  70. }
  71. if(m_fbSize[0] == 0)
  72. {
  73. m_fbSize[0] = viewImpl.m_tex->getWidth() >> viewImpl.getSubresource().m_firstMipmap;
  74. m_fbSize[1] = viewImpl.m_tex->getHeight() >> viewImpl.getSubresource().m_firstMipmap;
  75. }
  76. else
  77. {
  78. ANKI_ASSERT(m_fbSize[0] == (viewImpl.m_tex->getWidth() >> viewImpl.getSubresource().m_firstMipmap));
  79. ANKI_ASSERT(m_fbSize[1] == (viewImpl.m_tex->getHeight() >> viewImpl.getSubresource().m_firstMipmap));
  80. }
  81. // Misc
  82. m_clearDepth = !!(viewImpl.getSubresource().m_depthStencilAspect & DepthStencilAspectBit::kDepth)
  83. && att.m_loadOperation == AttachmentLoadOperation::kClear;
  84. m_clearStencil = !!(viewImpl.getSubresource().m_depthStencilAspect & DepthStencilAspectBit::STENCIL)
  85. && att.m_stencilLoadOperation == AttachmentLoadOperation::kClear;
  86. }
  87. // Check completeness
  88. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  89. if(status != GL_FRAMEBUFFER_COMPLETE)
  90. {
  91. ANKI_GL_LOGE("FBO is incomplete. Status: 0x%x", status);
  92. return Error::kFunctionFailed;
  93. }
  94. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  95. return Error::kNone;
  96. }
  97. void FramebufferImpl::attachTextureInternal(GLenum attachment, const TextureViewImpl& view, const FramebufferAttachmentInfo& info)
  98. {
  99. const GLenum target = GL_FRAMEBUFFER;
  100. const TextureImpl& tex = static_cast<const TextureImpl&>(*view.m_tex);
  101. switch(tex.m_target)
  102. {
  103. case GL_TEXTURE_2D:
  104. #if ANKI_GL == ANKI_GL_DESKTOP
  105. case GL_TEXTURE_2D_MULTISAMPLE:
  106. #endif
  107. glFramebufferTexture2D(target, attachment, tex.m_target, tex.getGlName(), view.getSubresource().m_firstMipmap);
  108. break;
  109. case GL_TEXTURE_CUBE_MAP:
  110. glFramebufferTexture2D(target, attachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + view.getSubresource().m_firstFace, tex.getGlName(),
  111. view.getSubresource().m_firstMipmap);
  112. break;
  113. case GL_TEXTURE_2D_ARRAY:
  114. glFramebufferTextureLayer(target, attachment, tex.getGlName(), view.getSubresource().m_firstMipmap, view.getSubresource().m_firstLayer);
  115. break;
  116. case GL_TEXTURE_3D:
  117. ANKI_ASSERT(!"TODO");
  118. break;
  119. case GL_TEXTURE_CUBE_MAP_ARRAY:
  120. glFramebufferTextureLayer(target, attachment, tex.getGlName(), view.getSubresource().m_firstMipmap,
  121. view.getSubresource().m_firstLayer * 6 + view.getSubresource().m_firstFace);
  122. break;
  123. default:
  124. ANKI_ASSERT(0);
  125. break;
  126. }
  127. }
  128. void FramebufferImpl::bind(const GlState& state, U32 minx, U32 miny, U32 width, U32 height) const
  129. {
  130. ANKI_ASSERT(width > 0 && height > 0);
  131. if(m_in.getName() && static_cast<const GrManagerImpl&>(getManager()).debugMarkersEnabled())
  132. {
  133. glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, m_glName, 0, &m_in.getName()[0]);
  134. }
  135. // Clear in the render area
  136. const U maxx = min<U>(minx + width, m_fbSize[0]);
  137. const U maxy = min<U>(miny + height, m_fbSize[1]);
  138. ANKI_ASSERT(minx < m_fbSize[0] && miny < m_fbSize[1] && maxx <= m_fbSize[0] && maxy <= m_fbSize[1]);
  139. width = maxx - minx;
  140. height = maxy - miny;
  141. glScissor(minx, miny, width, height);
  142. ANKI_ASSERT(m_glName != 0);
  143. glBindFramebuffer(GL_FRAMEBUFFER, m_glName);
  144. // Set the draw buffers
  145. if(m_in.m_colorAttachmentCount)
  146. {
  147. glDrawBuffers(m_in.m_colorAttachmentCount, &m_drawBuffers[0]);
  148. }
  149. // Invalidate
  150. if(m_invalidateBuffersCount)
  151. {
  152. glInvalidateSubFramebuffer(GL_FRAMEBUFFER, m_invalidateBuffersCount, &m_invalidateBuffers[0], minx, miny, width, height);
  153. }
  154. // Clear buffers
  155. for(U i = 0; i < m_in.m_colorAttachmentCount; i++)
  156. {
  157. const FramebufferAttachmentInfo& att = m_in.m_colorAttachments[i];
  158. if(att.m_loadOperation == AttachmentLoadOperation::kClear)
  159. {
  160. // Enable write mask in case a pipeline changed it (else no clear will happen) and then restore state
  161. Bool restore = false;
  162. if(state.m_colorWriteMasks[i][0] != true || state.m_colorWriteMasks[i][1] != true || state.m_colorWriteMasks[i][2] != true
  163. || state.m_colorWriteMasks[i][3] != true)
  164. {
  165. glColorMaski(i, true, true, true, true);
  166. restore = true;
  167. }
  168. glClearBufferfv(GL_COLOR, i, &att.m_clearValue.m_colorf[0]);
  169. if(restore)
  170. {
  171. glColorMaski(i, state.m_colorWriteMasks[i][0], state.m_colorWriteMasks[i][1], state.m_colorWriteMasks[i][2],
  172. state.m_colorWriteMasks[i][3]);
  173. }
  174. }
  175. }
  176. // Clear depth
  177. if(m_clearDepth)
  178. {
  179. // Enable write mask in case a pipeline changed it (else no clear will happen) and then restore state
  180. if(state.m_depthWriteMask == false)
  181. {
  182. glDepthMask(true);
  183. }
  184. glClearBufferfv(GL_DEPTH, 0, &m_in.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_depth);
  185. if(state.m_depthWriteMask == false)
  186. {
  187. glDepthMask(false);
  188. }
  189. }
  190. // Clear stencil
  191. if(m_clearStencil)
  192. {
  193. // Enable write mask in case a pipeline changed it (else no clear will happen) and then restore state
  194. // From the spec: The clear operation always uses the front stencil write mask when clearing the stencil
  195. // buffer
  196. if(state.m_stencilWriteMask[0] != kMaxU32)
  197. {
  198. glStencilMaskSeparate(GL_FRONT, kMaxU32);
  199. }
  200. GLint clearVal = m_in.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_stencil;
  201. glClearBufferiv(GL_STENCIL, 0, &clearVal);
  202. if(state.m_stencilWriteMask[0] != kMaxU32)
  203. {
  204. glStencilMaskSeparate(GL_FRONT, state.m_stencilWriteMask[0]);
  205. }
  206. }
  207. glScissor(state.m_scissor[0], state.m_scissor[1], state.m_scissor[2], state.m_scissor[3]);
  208. }
  209. void FramebufferImpl::endRenderPass() const
  210. {
  211. if(m_in.getName() && static_cast<const GrManagerImpl&>(getManager()).debugMarkersEnabled())
  212. {
  213. glPopDebugGroup();
  214. }
  215. }
  216. } // end namespace anki