Skip to content

Commit

Permalink
Cached framebuffer state
Browse files Browse the repository at this point in the history
Summary: Reattach only when state is changed

Reviewed By: EricGriffith

Differential Revision: D50475013

fbshipit-source-id: f1158191608417cb8f1c9d609811b64f694b714b
  • Loading branch information
rudybear authored and facebook-github-bot committed Oct 24, 2023
1 parent b985b7e commit 66e5afc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
76 changes: 52 additions & 24 deletions src/igl/opengl/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <igl/opengl/Device.h>
#include <igl/opengl/DummyTexture.h>
#include <igl/opengl/Errors.h>
#include <igl/opengl/Texture.h>

#include <algorithm>
#if !IGL_PLATFORM_ANDROID
Expand Down Expand Up @@ -73,20 +72,6 @@ Result checkFramebufferStatus(IContext& context, bool read) {
return Result(code, message);
}

void attachAsColor(igl::ITexture& texture,
uint32_t index,
const Texture::AttachmentParams& params) {
static_cast<Texture&>(texture).attachAsColor(index, params);
}

void attachAsDepth(igl::ITexture& texture, const Texture::AttachmentParams& params) {
static_cast<Texture&>(texture).attachAsDepth(params);
}

void attachAsStencil(igl::ITexture& texture, const Texture::AttachmentParams& params) {
static_cast<Texture&>(texture).attachAsStencil(params);
}

Texture::AttachmentParams toAttachmentParams(const RenderPassDesc::BaseAttachmentDesc& attachment,
FramebufferMode mode) {
Texture::AttachmentParams params{};
Expand Down Expand Up @@ -165,6 +150,32 @@ FramebufferBindingGuard::~FramebufferBindingGuard() {

Framebuffer::Framebuffer(IContext& context) : WithContext(context) {}

void Framebuffer::attachAsColor(igl::ITexture& texture,
uint32_t index,
const Texture::AttachmentParams& params) const {
static_cast<Texture&>(texture).attachAsColor(index, params);
IGL_ASSERT(index >= 0 && index < kNumCachedStates);
colorCachedState_[index].updateCache(params.stereo ? FramebufferMode::Stereo
: FramebufferMode::Mono,
params.layer,
params.face,
params.mipLevel);
}

void Framebuffer::attachAsDepth(igl::ITexture& texture,
const Texture::AttachmentParams& params) const {
static_cast<Texture&>(texture).attachAsDepth(params);
depthCachedState_.updateCache(params.stereo ? FramebufferMode::Stereo : FramebufferMode::Mono,
params.layer,
params.face,
params.mipLevel);
}

void Framebuffer::attachAsStencil(igl::ITexture& texture,
const Texture::AttachmentParams& params) const {
static_cast<Texture&>(texture).attachAsStencil(params);
}

void Framebuffer::bindBuffer() const {
getContext().bindFramebuffer(GL_FRAMEBUFFER, frameBufferID_);
}
Expand Down Expand Up @@ -294,6 +305,23 @@ void Framebuffer::copyTextureColorAttachment(ICommandQueue& /*cmdQueue*/,
static_cast<GLsizei>(range.height));
}

bool Framebuffer::CachedState::needsUpdate(FramebufferMode mode,
uint8_t layer,
uint8_t face,
uint8_t mipLevel) {
return mode_ != mode || layer_ != layer || face_ != face || mipLevel_ != mipLevel;
}

void Framebuffer::CachedState::updateCache(FramebufferMode mode,
uint8_t layer,
uint8_t face,
uint8_t mipLevel) {
mode_ = mode;
layer_ = layer;
face_ = face;
mipLevel_ = mipLevel;
}

///--------------------------------------
/// MARK: - CustomFramebuffer

Expand Down Expand Up @@ -560,22 +588,22 @@ void CustomFramebuffer::bind(const RenderPassDesc& renderPass) const {
// When setting up a framebuffer, we attach textures as though they were a non-array
// texture with and set layer, mip level and face equal to 0.
// If any of these assumptions are not true, we need to reattach with proper values.
const bool needsToBeReattached =
renderTarget_.mode == FramebufferMode::Stereo || renderPassAttachment.layer > 0 ||
renderPassAttachment.face > 0 || renderPassAttachment.mipLevel > 0;

if (needsToBeReattached) {
IGL_ASSERT(index >= 0 && index < kNumCachedStates);
if (colorCachedState_[index].needsUpdate(renderTarget_.mode,
renderPassAttachment.layer,
renderPassAttachment.face,
renderPassAttachment.mipLevel)) {
attachAsColor(*colorAttachmentTexture,
static_cast<uint32_t>(index),
toAttachmentParams(renderPassAttachment, renderTarget_.mode));
}
}
if (renderTarget_.depthAttachment.texture) {
const auto& renderPassAttachment = renderPass.depthAttachment;
const bool needsToBeReattached =
renderTarget_.mode == FramebufferMode::Stereo || renderPassAttachment.layer > 0 ||
renderPassAttachment.face > 0 || renderPassAttachment.mipLevel > 0;
if (needsToBeReattached) {
if (depthCachedState_.needsUpdate(renderTarget_.mode,
renderPassAttachment.layer,
renderPassAttachment.face,
renderPassAttachment.mipLevel)) {
attachAsDepth(*renderTarget_.depthAttachment.texture,
toAttachmentParams(renderPassAttachment, renderTarget_.mode));
}
Expand Down
21 changes: 20 additions & 1 deletion src/igl/opengl/Framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include <igl/RenderPass.h>
#include <igl/opengl/GLIncludes.h>
#include <igl/opengl/IContext.h>
#include <igl/opengl/Texture.h>

namespace igl {
class ICommandBuffer;
namespace opengl {
class Texture;

///--------------------------------------
/// MARK: - FramebufferBindingGuard
Expand Down Expand Up @@ -72,9 +72,28 @@ class Framebuffer : public WithContext, public IFramebuffer {
}

protected:
void attachAsColor(igl::ITexture& texture,
uint32_t index,
const Texture::AttachmentParams& params) const;
void attachAsDepth(igl::ITexture& texture, const Texture::AttachmentParams& params) const;
void attachAsStencil(igl::ITexture& texture, const Texture::AttachmentParams& params) const;
// NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
GLuint frameBufferID_ = 0;

struct CachedState {
FramebufferMode mode_ = igl::FramebufferMode::Mono;
uint8_t layer_ = 0;
uint8_t face_ = 0;
uint8_t mipLevel_ = 0;

bool needsUpdate(FramebufferMode mode, uint8_t layer, uint8_t face, uint8_t mipLevel);
void updateCache(FramebufferMode mode, uint8_t layer, uint8_t face, uint8_t mipLevel);
};

constexpr static auto kNumCachedStates = 8; // We allow up to 8 color attachments
mutable std::array<CachedState, kNumCachedStates> colorCachedState_;
mutable CachedState depthCachedState_;

std::shared_ptr<Framebuffer> resolveFramebuffer = nullptr;
};

Expand Down

0 comments on commit 66e5afc

Please sign in to comment.