Align RenderGraph imported texture ownership

This commit is contained in:
2026-04-28 14:47:50 +08:00
parent 8c66cdac07
commit 0e506f21ec
10 changed files with 170 additions and 27 deletions

View File

@@ -122,13 +122,50 @@ private:
Format m_format = Format::Unknown;
};
class MockImportedTexture final : public RHITexture {
public:
explicit MockImportedTexture(
Format format = Format::R8G8B8A8_UNorm,
uint32_t width = 1280u,
uint32_t height = 720u)
: m_format(format)
, m_width(width)
, m_height(height) {
}
uint32_t GetWidth() const override { return m_width; }
uint32_t GetHeight() const override { return m_height; }
uint32_t GetDepth() const override { return 1u; }
uint32_t GetMipLevels() const override { return 1u; }
Format GetFormat() const override { return m_format; }
TextureType GetTextureType() const override { return TextureType::Texture2D; }
ResourceStates GetState() const override { return m_stateValue; }
void SetState(ResourceStates state) override { m_stateValue = state; }
void* GetNativeHandle() override { return nullptr; }
const std::string& GetName() const override { return m_name; }
void SetName(const std::string& name) override { m_name = name; }
void Shutdown() override {}
private:
Format m_format = Format::Unknown;
uint32_t m_width = 0u;
uint32_t m_height = 0u;
ResourceStates m_stateValue = ResourceStates::Common;
std::string m_name;
};
class MockImportedView final : public RHIResourceView {
public:
explicit MockImportedView(
ResourceViewType viewType = ResourceViewType::RenderTarget,
Format format = Format::R8G8B8A8_UNorm)
Format format = Format::R8G8B8A8_UNorm,
RHITexture* texture = nullptr)
: m_viewType(viewType)
, m_format(format) {
, m_format(format)
, m_ownedTexture(texture == nullptr
? std::make_unique<MockImportedTexture>(format)
: nullptr)
, m_texture(texture != nullptr ? texture : m_ownedTexture.get()) {
}
void Shutdown() override {}
@@ -137,10 +174,13 @@ public:
ResourceViewType GetViewType() const override { return m_viewType; }
ResourceViewDimension GetDimension() const override { return ResourceViewDimension::Texture2D; }
Format GetFormat() const override { return m_format; }
RHITexture* GetTextureResource() const override { return m_texture; }
private:
ResourceViewType m_viewType = ResourceViewType::RenderTarget;
Format m_format = Format::R8G8B8A8_UNorm;
std::unique_ptr<MockImportedTexture> m_ownedTexture;
RHITexture* m_texture = nullptr;
};
class MockTransientDevice final : public RHIDevice {
@@ -396,14 +436,16 @@ TEST(RenderGraph_Test, OrdersImportedTextureHazardsAcrossFullscreenStyleChain) {
RenderGraphBuilder builder(graph);
const RenderGraphTextureDesc desc = BuildTestTextureDesc();
MockImportedView sceneColorView;
MockImportedView postColorView;
const RenderGraphTextureHandle sceneColor = builder.ImportTexture(
"SceneColor",
desc,
reinterpret_cast<RHIResourceView*>(1));
&sceneColorView);
const RenderGraphTextureHandle postColor = builder.ImportTexture(
"PostColor",
desc,
reinterpret_cast<RHIResourceView*>(2));
&postColorView);
builder.AddRasterPass(
"MainScene",
@@ -457,10 +499,13 @@ TEST(RenderGraph_Test, PreservesImportedTextureStateContractAcrossCompile) {
importedOptions.finalState = ResourceStates::PixelShaderResource;
importedOptions.graphOwnsTransitions = true;
MockImportedView importedView(
ResourceViewType::ShaderResource,
Format::R8G8B8A8_UNorm);
const RenderGraphTextureHandle importedTexture = builder.ImportTexture(
"ImportedColor",
desc,
reinterpret_cast<RHIResourceView*>(7),
&importedView,
importedOptions);
builder.AddRasterPass(
@@ -676,15 +721,15 @@ TEST(RenderGraph_Test, ExecutesTransientDepthTransitionsWithDepthStencilView) {
EXPECT_EQ(allocationState->createTextureCalls, 1);
EXPECT_EQ(allocationState->createRenderTargetViewCalls, 0);
EXPECT_EQ(allocationState->createDepthViewCalls, 1);
EXPECT_EQ(allocationState->createShaderViewCalls, 0);
EXPECT_EQ(allocationState->createShaderViewCalls, 1);
EXPECT_EQ(allocationState->shutdownTextureCalls, 1);
EXPECT_EQ(allocationState->shutdownRenderTargetViewCalls, 0);
EXPECT_EQ(allocationState->shutdownDepthViewCalls, 1);
EXPECT_EQ(allocationState->shutdownShaderViewCalls, 0);
EXPECT_EQ(allocationState->shutdownShaderViewCalls, 1);
EXPECT_EQ(allocationState->destroyTextureCalls, 1);
EXPECT_EQ(allocationState->destroyRenderTargetViewCalls, 0);
EXPECT_EQ(allocationState->destroyDepthViewCalls, 1);
EXPECT_EQ(allocationState->destroyShaderViewCalls, 0);
EXPECT_EQ(allocationState->destroyShaderViewCalls, 1);
}
TEST(RenderGraph_Test, ExecutesGraphOwnedImportedDepthTransitionsAtGraphBoundaries) {
@@ -864,10 +909,11 @@ TEST(RenderGraph_Test, ExecutesCompiledPassCallbacksInCompiledOrder) {
const RenderGraphTextureDesc desc = BuildTestTextureDesc();
const RenderGraphTextureHandle sceneColor = builder.CreateTransientTexture("SceneColor", desc);
MockImportedView backBufferView;
const RenderGraphTextureHandle backBuffer = builder.ImportTexture(
"BackBuffer",
desc,
reinterpret_cast<RHIResourceView*>(1));
&backBufferView);
std::vector<std::string> eventLog;
auto allocationState = std::make_shared<MockTransientAllocationState>();