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

@@ -77,15 +77,25 @@ RenderGraphTextureHandle ImportRenderGraphTexture(
return {};
}
const auto existing = registry.find(view);
RHI::RHITexture* const texture = view->GetTextureResource();
RenderGraphImportedTextureRegistryKey registryKey = {};
registryKey.texture = texture;
registryKey.view = texture == nullptr ? view : nullptr;
const auto existing = registry.find(registryKey);
if (existing != registry.end()) {
builder.MergeImportedTextureOptions(existing->second, importedOptions);
return existing->second;
}
RenderGraphImportedTextureDesc importedDesc = {};
importedDesc.textureDesc = desc;
importedDesc.texture = texture;
importedDesc.primaryView = view;
importedDesc.options = importedOptions;
const RenderGraphTextureHandle handle =
builder.ImportTexture(name, desc, view, importedOptions);
registry.emplace(view, handle);
builder.ImportTexture(name, importedDesc);
registry.emplace(registryKey, handle);
return handle;
}

View File

@@ -5,11 +5,13 @@
#include <XCEngine/Rendering/Graph/RenderGraphTypes.h>
#include <XCEngine/Rendering/RenderSurface.h>
#include <functional>
#include <unordered_map>
#include <vector>
namespace XCEngine {
namespace RHI {
class RHITexture;
class RHIResourceView;
} // namespace RHI
@@ -30,8 +32,27 @@ enum class RenderGraphSurfaceAccessMode {
ColorDepth = 1
};
struct RenderGraphImportedTextureRegistryKey {
RHI::RHITexture* texture = nullptr;
RHI::RHIResourceView* view = nullptr;
bool operator==(const RenderGraphImportedTextureRegistryKey& other) const {
return texture == other.texture && view == other.view;
}
};
struct RenderGraphImportedTextureRegistryKeyHash {
size_t operator()(const RenderGraphImportedTextureRegistryKey& key) const {
return std::hash<RHI::RHITexture*>{}(key.texture) ^
(std::hash<RHI::RHIResourceView*>{}(key.view) << 1u);
}
};
using RenderGraphImportedTextureRegistry =
std::unordered_map<RHI::RHIResourceView*, RenderGraphTextureHandle>;
std::unordered_map<
RenderGraphImportedTextureRegistryKey,
RenderGraphTextureHandle,
RenderGraphImportedTextureRegistryKeyHash>;
enum class RenderGraphSurfaceImportUsage {
Source = 0,

View File

@@ -79,17 +79,14 @@ void RenderGraphBuilder::Reset() {
RenderGraphTextureHandle RenderGraphBuilder::ImportTexture(
const Containers::String& name,
const RenderGraphTextureDesc& desc,
RHI::RHIResourceView* importedView,
const RenderGraphImportedTextureOptions& importedOptions) {
const RenderGraphImportedTextureDesc& importedDesc) {
RenderGraph::TextureResource resource = {};
resource.name = name;
resource.desc = desc;
resource.desc = importedDesc.textureDesc;
resource.kind = RenderGraphTextureKind::Imported;
resource.importedView = importedView;
resource.importedTexture =
importedView != nullptr ? importedView->GetTextureResource() : nullptr;
resource.importedOptions = importedOptions;
resource.importedView = importedDesc.primaryView;
resource.importedTexture = importedDesc.texture;
resource.importedOptions = importedDesc.options;
m_graph.m_textures.push_back(resource);
RenderGraphTextureHandle handle = {};
@@ -97,6 +94,20 @@ RenderGraphTextureHandle RenderGraphBuilder::ImportTexture(
return handle;
}
RenderGraphTextureHandle RenderGraphBuilder::ImportTexture(
const Containers::String& name,
const RenderGraphTextureDesc& desc,
RHI::RHIResourceView* importedView,
const RenderGraphImportedTextureOptions& importedOptions) {
RenderGraphImportedTextureDesc importedDesc = {};
importedDesc.textureDesc = desc;
importedDesc.texture =
importedView != nullptr ? importedView->GetTextureResource() : nullptr;
importedDesc.primaryView = importedView;
importedDesc.options = importedOptions;
return ImportTexture(name, importedDesc);
}
RenderGraphTextureHandle RenderGraphBuilder::CreateTransientTexture(
const Containers::String& name,
const RenderGraphTextureDesc& desc) {

View File

@@ -117,7 +117,7 @@ bool RenderGraphCompiler::Compile(
texture.importedOptions.graphOwnsTransitions &&
texture.importedTexture == nullptr) {
WriteError(
Containers::String("RenderGraph imported texture requires a texture-backed view when graph owns transitions: ") +
Containers::String("RenderGraph graph-owned imported texture requires an explicit RHI texture resource: ") +
texture.name,
outErrorMessage);
return false;