Skip to content

Commit 78e8d0e

Browse files
authored
cache render window texture (#17669)
1 parent 5ea5825 commit 78e8d0e

3 files changed

Lines changed: 40 additions & 19 deletions

File tree

native/cocos/renderer/pipeline/custom/NativePipeline.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,16 @@ uint32_t addDepthStencilImpl(
289289
if (swapchain) {
290290
CC_EXPECTS(residency == ResourceResidency::BACKBUFFER);
291291
CC_EXPECTS(ppl.defaultFramebufferHasDepthStencil);
292+
RenderSwapchain sc{swapchain, true};
293+
sc.texture = RenderSwapchain::getDepthStencilTexture(swapchain);
292294
resID = addVertex(
293295
SwapchainTag{},
294296
std::forward_as_tuple(name.c_str()),
295297
std::forward_as_tuple(desc),
296298
std::forward_as_tuple(ResourceTraits{residency}),
297299
std::forward_as_tuple(),
298300
std::forward_as_tuple(samplerInfo),
299-
std::forward_as_tuple(RenderSwapchain{swapchain, true}),
301+
std::forward_as_tuple(sc),
300302
ppl.resourceGraph);
301303
} else {
302304
CC_EXPECTS(residency == ResourceResidency::MANAGED || residency == ResourceResidency::MEMORYLESS);
@@ -378,6 +380,9 @@ uint32_t NativePipeline::addRenderWindow(
378380
sc.renderWindow = renderWindow;
379381
CC_ENSURES(!sc.isDepthStencil);
380382

383+
sc.texture = RenderSwapchain::getColorTexture(renderWindow);
384+
CC_ENSURES(sc.texture);
385+
381386
CC_ENSURES(desc.format != gfx::Format::UNKNOWN);
382387
return addVertex(
383388
SwapchainTag{},
@@ -396,14 +401,18 @@ uint32_t NativePipeline::addRenderWindow(
396401
desc.format = renderWindow->getFramebuffer()->getColorTextures()[0]->getFormat();
397402
CC_ENSURES(desc.format != gfx::Format::UNKNOWN);
398403

404+
RenderSwapchain sc{renderWindow->getSwapchain(), false};
405+
sc.texture = RenderSwapchain::getColorTexture(renderWindow->getSwapchain());
406+
CC_ENSURES(sc.texture);
407+
399408
return addVertex(
400409
SwapchainTag{},
401410
std::forward_as_tuple(name.c_str()),
402411
std::forward_as_tuple(desc),
403412
std::forward_as_tuple(ResourceTraits{ResourceResidency::BACKBUFFER}),
404413
std::forward_as_tuple(),
405414
std::forward_as_tuple(),
406-
std::forward_as_tuple(RenderSwapchain{renderWindow->getSwapchain(), false}),
415+
std::forward_as_tuple(sc),
407416
resourceGraph);
408417
}
409418

native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ namespace cc {
3636

3737
namespace render {
3838

39+
gfx::Texture* RenderSwapchain::getColorTexture(gfx::Swapchain* swapchain) noexcept {
40+
CC_EXPECTS(swapchain);
41+
gfx::Texture* texture = swapchain->getColorTexture();
42+
CC_ENSURES(texture);
43+
return texture;
44+
}
45+
46+
gfx::Texture* RenderSwapchain::getColorTexture(scene::RenderWindow* renderWindow) noexcept {
47+
const auto& fb = renderWindow->getFramebuffer();
48+
CC_EXPECTS(fb);
49+
CC_EXPECTS(fb->getColorTextures().size() == 1);
50+
gfx::Texture* texture = fb->getColorTextures()[0];
51+
CC_ENSURES(texture);
52+
return texture;
53+
}
54+
55+
gfx::Texture* RenderSwapchain::getDepthStencilTexture(gfx::Swapchain* swapchain) noexcept {
56+
CC_EXPECTS(swapchain);
57+
gfx::Texture* texture = swapchain->getDepthStencilTexture();
58+
CC_ENSURES(texture);
59+
return texture;
60+
}
61+
3962
ResourceGroup::~ResourceGroup() noexcept {
4063
for (const auto& buffer : instancingBuffers) {
4164
buffer->clear();
@@ -398,23 +421,7 @@ gfx::Texture* ResourceGraph::getTexture(vertex_descriptor resID) {
398421
return fb->getColorTextures()[0];
399422
},
400423
[](const RenderSwapchain& sc) -> gfx::Texture* {
401-
gfx::Texture* texture1 = nullptr;
402-
if (sc.swapchain) {
403-
if (sc.isDepthStencil) {
404-
texture1 = sc.swapchain->getDepthStencilTexture();
405-
} else {
406-
texture1 = sc.swapchain->getColorTexture();
407-
}
408-
} else {
409-
CC_EXPECTS(sc.renderWindow);
410-
CC_EXPECTS(!sc.isDepthStencil);
411-
const auto& fb = sc.renderWindow->getFramebuffer();
412-
CC_EXPECTS(fb);
413-
CC_EXPECTS(fb->getColorTextures().size() == 1);
414-
CC_EXPECTS(fb->getColorTextures().at(0));
415-
texture1 = fb->getColorTextures()[0];
416-
}
417-
return texture1;
424+
return sc.texture;
418425
},
419426
[](const FormatView& view) -> gfx::Texture* {
420427
// TODO(zhouzhenglong): add ImageView support

native/cocos/renderer/pipeline/custom/RenderGraphTypes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,17 @@ struct RenderSwapchain {
192192
: swapchain(swapchainIn),
193193
isDepthStencil(isDepthStencilIn) {}
194194

195+
static gfx::Texture* getColorTexture(gfx::Swapchain* swapchain) noexcept;
196+
static gfx::Texture* getColorTexture(scene::RenderWindow* renderWindow) noexcept;
197+
static gfx::Texture* getDepthStencilTexture(gfx::Swapchain* swapchain) noexcept;
198+
195199
gfx::Swapchain* swapchain{nullptr};
196200
scene::RenderWindow* renderWindow{nullptr};
197201
uint32_t currentID{0};
198202
uint32_t numBackBuffers{0};
199203
uint32_t generation{0xFFFFFFFF};
200204
bool isDepthStencil{false};
205+
gfx::Texture* texture{nullptr};
201206
};
202207

203208
struct ResourceStates {

0 commit comments

Comments
 (0)