Graphics/FrameGraph: Add support for cubemap and slice rendering

This commit is contained in:
SirLynix
2022-12-02 23:00:44 +01:00
committed by Jérôme Leclercq
parent 4ae3f51174
commit 5a57976b4b
6 changed files with 229 additions and 61 deletions

View File

@@ -149,29 +149,48 @@ namespace Nz
for (auto& textureData : m_textures)
{
TextureInfo textureCreationParams;
textureCreationParams.type = ImageType::E2D;
textureCreationParams.usageFlags = textureData.usage;
textureCreationParams.pixelFormat = textureData.format;
textureCreationParams.width = 1;
textureCreationParams.height = 1;
switch (textureData.size)
if (textureData.viewData)
{
case FramePassAttachmentSize::Fixed:
textureCreationParams.width = textureData.width;
textureCreationParams.height = textureData.height;
break;
TextureData& parentTexture = m_textures[textureData.viewData->parentTextureId];
case FramePassAttachmentSize::SwapchainFactor:
textureCreationParams.width = frameWidth * textureData.width / 100'000;
textureCreationParams.height = frameHeight * textureData.height / 100'000;
break;
// This is a view on another texture
TextureViewInfo textureViewParams;
textureViewParams.viewType = textureData.type;
textureViewParams.reinterpretFormat = textureData.format;
textureViewParams.baseArrayLayer = textureData.viewData->arrayLayer;
textureData.texture = parentTexture.texture->CreateView(textureViewParams);
}
else
{
textureData.texture = renderDevice->InstantiateTexture(textureCreationParams);
if (!textureData.name.empty())
textureData.texture->UpdateDebugName(textureData.name);
TextureInfo textureCreationParams;
textureCreationParams.type = textureData.type;
textureCreationParams.usageFlags = textureData.usage;
textureCreationParams.pixelFormat = textureData.format;
if (textureCreationParams.type == ImageType::Cubemap)
textureCreationParams.layerCount = 6;
textureCreationParams.width = 1;
textureCreationParams.height = 1;
switch (textureData.size)
{
case FramePassAttachmentSize::Fixed:
textureCreationParams.width = textureData.width;
textureCreationParams.height = textureData.height;
break;
case FramePassAttachmentSize::SwapchainFactor:
textureCreationParams.width = frameWidth * textureData.width / 100'000;
textureCreationParams.height = frameHeight * textureData.height / 100'000;
break;
}
textureData.texture = renderDevice->InstantiateTexture(textureCreationParams);
if (!textureData.name.empty())
textureData.texture->UpdateDebugName(textureData.name);
}
}
std::vector<std::shared_ptr<Texture>> textures;

View File

@@ -39,7 +39,8 @@ namespace Nz
m_pending.physicalPasses.clear();
m_pending.renderPasses.clear();
m_pending.textures.clear();
m_pending.texturePool.clear();
m_pending.texture2DPool.clear();
m_pending.textureCubePool.clear();
BuildReadWriteList();
@@ -113,12 +114,7 @@ namespace Nz
for (auto& texture : m_pending.textures)
{
auto& bakedTexture = bakedTextures.emplace_back();
bakedTexture.name = std::move(texture.name);
bakedTexture.format = texture.format;
bakedTexture.height = texture.height;
bakedTexture.size = texture.size;
bakedTexture.usage = texture.usage;
bakedTexture.width = texture.width;
static_cast<FrameGraphTextureData&>(bakedTexture) = std::move(texture);
}
return BakedFrameGraph(std::move(bakedPasses), std::move(bakedTextures), std::move(m_pending.attachmentToTextures), std::move(m_pending.passIdToPhysicalPassIndex));
@@ -195,7 +191,7 @@ namespace Nz
{
std::size_t textureId = RegisterTexture(input.attachmentId);
TextureData& attachmentData = m_pending.textures[textureId];
FrameGraphTextureData& attachmentData = m_pending.textures[textureId];
attachmentData.usage |= TextureUsage::ShaderSampling;
}
@@ -203,7 +199,7 @@ namespace Nz
{
std::size_t textureId = RegisterTexture(output.attachmentId);
TextureData& attachmentData = m_pending.textures[textureId];
FrameGraphTextureData& attachmentData = m_pending.textures[textureId];
attachmentData.usage |= TextureUsage::ColorAttachment;
}
@@ -211,7 +207,7 @@ namespace Nz
{
std::size_t textureId = RegisterTexture(depthStencilInput);
TextureData& attachmentData = m_pending.textures[textureId];
FrameGraphTextureData& attachmentData = m_pending.textures[textureId];
attachmentData.usage |= TextureUsage::DepthStencilAttachment;
if (std::size_t depthStencilOutput = framePass.GetDepthStencilOutput(); depthStencilOutput != FramePass::InvalidAttachmentId)
@@ -240,7 +236,7 @@ namespace Nz
{
std::size_t textureId = RegisterTexture(depthStencilOutput);
TextureData& attachmentData = m_pending.textures[textureId];
FrameGraphTextureData& attachmentData = m_pending.textures[textureId];
attachmentData.usage |= TextureUsage::DepthStencilAttachment;
}
@@ -253,10 +249,21 @@ namespace Nz
// If this pass is the last one where this attachment is used, push the texture to the reuse pool
if (it != m_pending.attachmentLastUse.end() && passIndex == it->second)
{
std::size_t textureId = Retrieve(m_pending.attachmentToTextures, attachmentId);
const auto& attachmentData = m_attachments[attachmentId];
if (std::holds_alternative<FramePassAttachment>(attachmentData))
{
std::size_t textureId = Retrieve(m_pending.attachmentToTextures, attachmentId);
assert(std::find(m_pending.texturePool.begin(), m_pending.texturePool.end(), textureId) == m_pending.texturePool.end());
m_pending.texturePool.push_back(textureId);
assert(std::find(m_pending.texture2DPool.begin(), m_pending.texture2DPool.end(), textureId) == m_pending.texture2DPool.end());
m_pending.texture2DPool.push_back(textureId);
}
else if (std::holds_alternative<AttachmentCube>(attachmentData))
{
std::size_t textureId = Retrieve(m_pending.attachmentToTextures, attachmentId);
assert(std::find(m_pending.textureCubePool.begin(), m_pending.textureCubePool.end(), textureId) == m_pending.textureCubePool.end());
m_pending.textureCubePool.push_back(textureId);
}
}
});
}
@@ -270,6 +277,16 @@ namespace Nz
auto& backbufferTexture = m_pending.textures[it->second];
backbufferTexture.usage |= TextureUsage::ShaderSampling;
}
// Apply texture view usage to their parents
for (auto& textureData : m_pending.textures)
{
if (textureData.viewData)
{
auto& parentTextureData = m_pending.textures[textureData.viewData->parentTextureId];
parentTextureData.usage |= textureData.usage;
}
}
}
void FrameGraph::BuildBarriers()
@@ -946,18 +963,20 @@ namespace Nz
const FramePassAttachment& attachmentData = arg;
// Fetch from reuse pool if possible
for (auto it = m_pending.texturePool.begin(); it != m_pending.texturePool.end(); ++it)
for (auto it = m_pending.texture2DPool.begin(); it != m_pending.texture2DPool.end(); ++it)
{
std::size_t textureId = *it;
TextureData& data = m_pending.textures[textureId];
FrameGraphTextureData& data = m_pending.textures[textureId];
assert(data.type == ImageType::E2D);
if (data.format != attachmentData.format ||
data.width != attachmentData.width ||
data.width != attachmentData.width ||
data.height != attachmentData.height ||
data.size != attachmentData.size)
data.size != attachmentData.size)
continue;
m_pending.texturePool.erase(it);
m_pending.texture2DPool.erase(it);
m_pending.attachmentToTextures.emplace(attachmentIndex, textureId);
if (!attachmentData.name.empty() && data.name != attachmentData.name)
@@ -969,7 +988,8 @@ namespace Nz
std::size_t textureId = m_pending.textures.size();
m_pending.attachmentToTextures.emplace(attachmentIndex, textureId);
TextureData& data = m_pending.textures.emplace_back();
FrameGraphTextureData& data = m_pending.textures.emplace_back();
data.type = ImageType::E2D;
data.name = attachmentData.name;
data.format = attachmentData.format;
data.width = attachmentData.width;
@@ -978,6 +998,72 @@ namespace Nz
return textureId;
}
else if constexpr (std::is_same_v<T, AttachmentCube>)
{
const AttachmentCube& attachmentData = arg;
// Fetch from reuse pool if possible
for (auto it = m_pending.textureCubePool.begin(); it != m_pending.textureCubePool.end(); ++it)
{
std::size_t textureId = *it;
FrameGraphTextureData& data = m_pending.textures[textureId];
assert(data.type == ImageType::Cubemap);
if (data.format != attachmentData.format ||
data.width != attachmentData.width ||
data.height != attachmentData.height ||
data.size != attachmentData.size)
continue;
m_pending.textureCubePool.erase(it);
m_pending.attachmentToTextures.emplace(attachmentIndex, textureId);
if (!attachmentData.name.empty() && data.name != attachmentData.name)
data.name += " / " + attachmentData.name;
return textureId;
}
std::size_t textureId = m_pending.textures.size();
m_pending.attachmentToTextures.emplace(attachmentIndex, textureId);
FrameGraphTextureData& data = m_pending.textures.emplace_back();
data.type = ImageType::Cubemap;
data.name = attachmentData.name;
data.format = attachmentData.format;
data.width = attachmentData.width;
data.height = attachmentData.height;
data.size = attachmentData.size;
return textureId;
}
else if constexpr (std::is_same_v<T, AttachmentLayer>)
{
const AttachmentLayer& texLayer = arg;
// TODO: Reuse texture views from pool?
std::size_t parentTextureId = RegisterTexture(texLayer.attachmentId);
std::size_t textureId = m_pending.textures.size();
m_pending.attachmentToTextures.emplace(attachmentIndex, textureId);
FrameGraphTextureData& data = m_pending.textures.emplace_back();
const FrameGraphTextureData& parentTexture = m_pending.textures[parentTextureId];
data.type = ImageType::E2D;
data.format = parentTexture.format;
data.width = parentTexture.width;
data.height = parentTexture.height;
data.size = parentTexture.size;
data.viewData = {
parentTextureId,
texLayer.layerIndex
};
return textureId;
}
else if constexpr (std::is_same_v<T, AttachmentProxy>)
{
const AttachmentProxy& proxy = arg;
@@ -1022,8 +1108,9 @@ namespace Nz
std::size_t FrameGraph::ResolveAttachmentIndex(std::size_t attachmentIndex) const
{
assert(attachmentIndex < m_attachments.size());
if (const AttachmentProxy* proxy = std::get_if<AttachmentProxy>(&m_attachments[attachmentIndex]))
return proxy->attachmentId;
while (const AttachmentProxy* proxy = std::get_if<AttachmentProxy>(&m_attachments[attachmentIndex]))
attachmentIndex = proxy->attachmentId;
return attachmentIndex;
}