Skip to content

Commit

Permalink
cache render window texture
Browse files Browse the repository at this point in the history
  • Loading branch information
star-e committed Sep 26, 2024
1 parent 5ea5825 commit 0f472be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
13 changes: 11 additions & 2 deletions native/cocos/renderer/pipeline/custom/NativePipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,16 @@ uint32_t addDepthStencilImpl(
if (swapchain) {
CC_EXPECTS(residency == ResourceResidency::BACKBUFFER);
CC_EXPECTS(ppl.defaultFramebufferHasDepthStencil);
RenderSwapchain sc{swapchain, true};
sc.texture = RenderSwapchain::getDepthStencilTexture(swapchain);
resID = addVertex(
SwapchainTag{},
std::forward_as_tuple(name.c_str()),
std::forward_as_tuple(desc),
std::forward_as_tuple(ResourceTraits{residency}),
std::forward_as_tuple(),
std::forward_as_tuple(samplerInfo),
std::forward_as_tuple(RenderSwapchain{swapchain, true}),
std::forward_as_tuple(sc),
ppl.resourceGraph);
} else {
CC_EXPECTS(residency == ResourceResidency::MANAGED || residency == ResourceResidency::MEMORYLESS);
Expand Down Expand Up @@ -378,6 +380,9 @@ uint32_t NativePipeline::addRenderWindow(
sc.renderWindow = renderWindow;
CC_ENSURES(!sc.isDepthStencil);

sc.texture = RenderSwapchain::getColorTexture(renderWindow);
CC_ENSURES(sc.texture);

CC_ENSURES(desc.format != gfx::Format::UNKNOWN);
return addVertex(
SwapchainTag{},
Expand All @@ -396,14 +401,18 @@ uint32_t NativePipeline::addRenderWindow(
desc.format = renderWindow->getFramebuffer()->getColorTextures()[0]->getFormat();
CC_ENSURES(desc.format != gfx::Format::UNKNOWN);

RenderSwapchain sc{renderWindow->getSwapchain(), false};
sc.texture = RenderSwapchain::getColorTexture(renderWindow->getSwapchain());
CC_ENSURES(sc.texture);

return addVertex(
SwapchainTag{},
std::forward_as_tuple(name.c_str()),
std::forward_as_tuple(desc),
std::forward_as_tuple(ResourceTraits{ResourceResidency::BACKBUFFER}),
std::forward_as_tuple(),
std::forward_as_tuple(),
std::forward_as_tuple(RenderSwapchain{renderWindow->getSwapchain(), false}),
std::forward_as_tuple(sc),
resourceGraph);
}

Expand Down
41 changes: 24 additions & 17 deletions native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ namespace cc {

namespace render {

gfx::Texture* RenderSwapchain::getColorTexture(gfx::Swapchain* swapchain) noexcept {
CC_EXPECTS(swapchain);
gfx::Texture* texture = swapchain->getColorTexture();
CC_ENSURES(texture);
return texture;
}

gfx::Texture* RenderSwapchain::getColorTexture(scene::RenderWindow* renderWindow) noexcept {
const auto& fb = renderWindow->getFramebuffer();
CC_EXPECTS(fb);
CC_EXPECTS(fb->getColorTextures().size() == 1);
gfx::Texture* texture = fb->getColorTextures()[0];
CC_ENSURES(texture);
return texture;
}

gfx::Texture* RenderSwapchain::getDepthStencilTexture(gfx::Swapchain* swapchain) noexcept {
CC_EXPECTS(swapchain);
gfx::Texture* texture = swapchain->getDepthStencilTexture();
CC_ENSURES(texture);
return texture;
}

ResourceGroup::~ResourceGroup() noexcept {
for (const auto& buffer : instancingBuffers) {
buffer->clear();
Expand Down Expand Up @@ -398,23 +421,7 @@ gfx::Texture* ResourceGraph::getTexture(vertex_descriptor resID) {
return fb->getColorTextures()[0];
},
[](const RenderSwapchain& sc) -> gfx::Texture* {
gfx::Texture* texture1 = nullptr;
if (sc.swapchain) {
if (sc.isDepthStencil) {
texture1 = sc.swapchain->getDepthStencilTexture();
} else {
texture1 = sc.swapchain->getColorTexture();
}
} else {
CC_EXPECTS(sc.renderWindow);
CC_EXPECTS(!sc.isDepthStencil);
const auto& fb = sc.renderWindow->getFramebuffer();
CC_EXPECTS(fb);
CC_EXPECTS(fb->getColorTextures().size() == 1);
CC_EXPECTS(fb->getColorTextures().at(0));
texture1 = fb->getColorTextures()[0];
}
return texture1;
return sc.texture;
},
[](const FormatView& view) -> gfx::Texture* {
// TODO(zhouzhenglong): add ImageView support
Expand Down
5 changes: 5 additions & 0 deletions native/cocos/renderer/pipeline/custom/RenderGraphTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,17 @@ struct RenderSwapchain {
: swapchain(swapchainIn),
isDepthStencil(isDepthStencilIn) {}

static gfx::Texture* getColorTexture(gfx::Swapchain* swapchain) noexcept;
static gfx::Texture* getColorTexture(scene::RenderWindow* renderWindow) noexcept;
static gfx::Texture* getDepthStencilTexture(gfx::Swapchain* swapchain) noexcept;

gfx::Swapchain* swapchain{nullptr};
scene::RenderWindow* renderWindow{nullptr};
uint32_t currentID{0};
uint32_t numBackBuffers{0};
uint32_t generation{0xFFFFFFFF};
bool isDepthStencil{false};
gfx::Texture* texture{nullptr};
};

struct ResourceStates {
Expand Down

0 comments on commit 0f472be

Please sign in to comment.