Skip to content

Commit edfe8e2

Browse files
DeviceContextVk: enabled VRS with dynamic rendering
1 parent 75c8eac commit edfe8e2

File tree

6 files changed

+80
-40
lines changed

6 files changed

+80
-40
lines changed

Graphics/GraphicsEngineVulkan/include/FramebufferCache.hpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,22 @@ class FramebufferCache
7979
void OnDestroyImageView(VkImageView ImgView);
8080
void OnDestroyRenderPass(VkRenderPass Pass);
8181

82+
struct CreateDyanmicRenderInfoAttribs
83+
{
84+
VkExtent2D Extent = {};
85+
uint32_t Layers = 0;
86+
uint32_t ViewMask = 0;
87+
88+
VkExtent2D ShadingRateTexelSize = {};
89+
90+
bool UseDepthAttachment = false;
91+
bool UseStencilAttachment = false;
92+
};
8293
static std::unique_ptr<VulkanUtilities::RenderingInfoWrapper> CreateDyanmicRenderInfo(
83-
const FramebufferCacheKey& Key,
84-
bool UseDepthAttachment,
85-
bool UseStencilAttachment,
86-
uint32_t width,
87-
uint32_t height,
88-
uint32_t layers,
89-
uint32_t viewMask);
94+
const FramebufferCacheKey& Key,
95+
const CreateDyanmicRenderInfoAttribs& Attribs);
9096

97+
private:
9198
RenderDeviceVkImpl& m_DeviceVk;
9299

93100
struct FramebufferCacheKeyHash

Graphics/GraphicsEngineVulkan/include/VulkanUtilities/RenderingInfoWrapper.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,15 @@ class RenderingInfoWrapper
9595
return m_Attachments[m_StencilAttachmentIndex];
9696
}
9797

98+
VkRenderingFragmentShadingRateAttachmentInfoKHR& GetShadingRateAttachment();
99+
98100
private:
99101
VkRenderingInfoKHR m_RI;
100102

101103
const size_t m_Hash = 0;
102104

103-
std::unique_ptr<VkRenderingAttachmentInfoKHR[]> m_Attachments;
105+
std::unique_ptr<VkRenderingAttachmentInfoKHR[]> m_Attachments;
106+
std::unique_ptr<VkRenderingFragmentShadingRateAttachmentInfoKHR> m_ShadingRateAttachment;
104107

105108
uint32_t m_DepthAttachmentIndex = ~0u;
106109
uint32_t m_StencilAttachmentIndex = ~0u;

Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,21 +1911,30 @@ void DeviceContextVkImpl::ChooseRenderPassAndFramebuffer()
19111911
}
19121912
else
19131913
{
1914-
bool UseDepthAttachment = false;
1915-
bool UseStencilAttachment = false;
1914+
FramebufferCache::CreateDyanmicRenderInfoAttribs CreateRIAttribs;
1915+
CreateRIAttribs.Extent = {m_FramebufferWidth, m_FramebufferHeight};
1916+
CreateRIAttribs.Layers = m_FramebufferSlices;
1917+
CreateRIAttribs.ViewMask = (1u << m_NumViewports) - 1u;
19161918
if (m_pBoundDepthStencil)
19171919
{
19181920
const TEXTURE_FORMAT DepthStencilFmt = m_pBoundDepthStencil->GetTexture()->GetDesc().Format;
19191921
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(DepthStencilFmt);
19201922

1921-
UseDepthAttachment = true;
1922-
UseStencilAttachment = FmtAttribs.ComponentType == COMPONENT_TYPE_DEPTH_STENCIL;
1923+
CreateRIAttribs.UseDepthAttachment = true;
1924+
CreateRIAttribs.UseStencilAttachment = FmtAttribs.ComponentType == COMPONENT_TYPE_DEPTH_STENCIL;
1925+
}
1926+
if (m_pBoundShadingRateMap)
1927+
{
1928+
const TextureDesc& ShadingRateDesc = m_pBoundShadingRateMap->GetTexture()->GetDesc();
1929+
// Framebuffer size may not be an integer multiple of the shading rate texel size
1930+
// Shading rate texel size be a power of two
1931+
// https://registry.khronos.org/vulkan/specs/latest/man/html/VkRenderingFragmentShadingRateAttachmentInfoKHR.html#VUID-VkRenderingFragmentShadingRateAttachmentInfoKHR-imageView-06149
1932+
// https://registry.khronos.org/vulkan/specs/latest/man/html/VkRenderingFragmentShadingRateAttachmentInfoKHR.html#VUID-VkRenderingFragmentShadingRateAttachmentInfoKHR-imageView-06152
1933+
CreateRIAttribs.ShadingRateTexelSize.width = AlignUpToPowerOfTwo(m_FramebufferWidth / ShadingRateDesc.Width);
1934+
CreateRIAttribs.ShadingRateTexelSize.height = AlignUpToPowerOfTwo(m_FramebufferHeight / ShadingRateDesc.Height);
19231935
}
1924-
uint32_t ViewMask = (1u << m_NumViewports) - 1u;
19251936

1926-
m_DynamicRenderingInfo = FramebufferCache::CreateDyanmicRenderInfo(FBKey, UseDepthAttachment, UseStencilAttachment,
1927-
m_FramebufferWidth, m_FramebufferHeight, m_FramebufferSlices,
1928-
ViewMask);
1937+
m_DynamicRenderingInfo = FramebufferCache::CreateDyanmicRenderInfo(FBKey, CreateRIAttribs);
19291938
}
19301939
}
19311940

Graphics/GraphicsEngineVulkan/src/FramebufferCache.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,17 @@ VkFramebuffer FramebufferCache::GetFramebuffer(const FramebufferCacheKey& Key, u
137137
}
138138
}
139139

140-
std::unique_ptr<VulkanUtilities::RenderingInfoWrapper> FramebufferCache::CreateDyanmicRenderInfo(const FramebufferCacheKey& Key,
141-
bool UseDepthAttachment,
142-
bool UseStencilAttachment,
143-
uint32_t width,
144-
uint32_t height,
145-
uint32_t layers,
146-
uint32_t viewMask)
140+
std::unique_ptr<VulkanUtilities::RenderingInfoWrapper> FramebufferCache::CreateDyanmicRenderInfo(const FramebufferCacheKey& Key,
141+
const CreateDyanmicRenderInfoAttribs& Attribs)
147142
{
148143
std::unique_ptr<VulkanUtilities::RenderingInfoWrapper> RI = std::make_unique<VulkanUtilities::RenderingInfoWrapper>(
149-
Key.GetHash(), Key.NumRenderTargets, UseDepthAttachment, UseStencilAttachment);
144+
Key.GetHash(), Key.NumRenderTargets, Attribs.UseDepthAttachment, Attribs.UseStencilAttachment);
150145

151-
RI->SetRenderArea({{0, 0}, {width, height}})
152-
.SetLayerCount(layers)
153-
.SetViewMask(viewMask);
146+
RI->SetRenderArea({{0, 0}, Attribs.Extent})
147+
.SetLayerCount(Attribs.Layers)
148+
.SetViewMask(Attribs.ViewMask);
154149

155-
auto InitAttachment = [](VkRenderingAttachmentInfo& Attachment, VkImageView View, VkImageLayout Layout) {
150+
auto InitAttachment = [](VkRenderingAttachmentInfoKHR& Attachment, VkImageView View, VkImageLayout Layout) {
156151
Attachment.imageView = View;
157152
Attachment.imageLayout = Layout;
158153
Attachment.resolveMode = VK_RESOLVE_MODE_NONE_KHR;
@@ -165,22 +160,31 @@ std::unique_ptr<VulkanUtilities::RenderingInfoWrapper> FramebufferCache::CreateD
165160

166161
for (Uint32 rt = 0; rt < Key.NumRenderTargets; ++rt)
167162
{
168-
VkRenderingAttachmentInfo& RTAttachment = RI->GetColorAttachment(rt);
163+
VkRenderingAttachmentInfoKHR& RTAttachment = RI->GetColorAttachment(rt);
169164
InitAttachment(RTAttachment, Key.RTVs[rt], VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
170165
}
171166

172-
if (UseDepthAttachment)
167+
if (Attribs.UseDepthAttachment)
173168
{
174-
VkRenderingAttachmentInfo& DepthAttachment = RI->GetDepthAttachment();
169+
VkRenderingAttachmentInfoKHR& DepthAttachment = RI->GetDepthAttachment();
175170
InitAttachment(DepthAttachment, Key.DSV, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
176171
}
177172

178-
if (UseStencilAttachment)
173+
if (Attribs.UseStencilAttachment)
179174
{
180-
VkRenderingAttachmentInfo& StencilAttachment = RI->GetStencilAttachment();
175+
VkRenderingAttachmentInfoKHR& StencilAttachment = RI->GetStencilAttachment();
181176
InitAttachment(StencilAttachment, Key.DSV, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
182177
}
183178

179+
if (Key.ShadingRate)
180+
{
181+
VkRenderingFragmentShadingRateAttachmentInfoKHR& ShadingRateAttachment = RI->GetShadingRateAttachment();
182+
183+
ShadingRateAttachment.imageView = Key.ShadingRate;
184+
ShadingRateAttachment.imageLayout = VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR;
185+
ShadingRateAttachment.shadingRateAttachmentTexelSize = Attribs.ShadingRateTexelSize;
186+
}
187+
184188
return RI;
185189
}
186190

Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ void CreateGraphicsPipeline(RenderDeviceVkImpl* pDevic
138138
const VulkanUtilities::VulkanLogicalDevice& LogicalDevice = pDeviceVk->GetLogicalDevice();
139139
const VulkanUtilities::VulkanPhysicalDevice& PhysicalDevice = pDeviceVk->GetPhysicalDevice();
140140

141+
VkGraphicsPipelineCreateInfo PipelineCI{};
142+
PipelineCI.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
143+
PipelineCI.pNext = nullptr;
144+
#ifdef DILIGENT_DEBUG
145+
PipelineCI.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
146+
#endif
147+
141148
VkPipelineRenderingCreateInfoKHR PipelineRenderingCI{};
142149
std::vector<VkFormat> ColorAttachmentFormats;
143150
if (pRenderPass == nullptr)
@@ -159,16 +166,13 @@ void CreateGraphicsPipeline(RenderDeviceVkImpl* pDevic
159166
{
160167
// VK_KHR_dynamic_rendering
161168
PipelineRenderingCI = GraphicsPipelineDesc_To_VkPipelineRenderingCreateInfo(GraphicsPipeline, ColorAttachmentFormats);
169+
if ((GraphicsPipeline.ShadingRateFlags & PIPELINE_SHADING_RATE_FLAG_TEXTURE_BASED) != 0)
170+
{
171+
PipelineCI.flags |= VK_PIPELINE_CREATE_RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
172+
}
162173
}
163174
}
164175

165-
VkGraphicsPipelineCreateInfo PipelineCI{};
166-
PipelineCI.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
167-
PipelineCI.pNext = nullptr;
168-
#ifdef DILIGENT_DEBUG
169-
PipelineCI.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
170-
#endif
171-
172176
PipelineCI.stageCount = static_cast<Uint32>(Stages.size());
173177
PipelineCI.pStages = Stages.data();
174178
PipelineCI.layout = Layout.GetVkPipelineLayout();

Graphics/GraphicsEngineVulkan/src/VulkanUtilities/RenderingInfoWrapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,17 @@ RenderingInfoWrapper::RenderingInfoWrapper(size_t Hash,
6969
VERIFY_EXPR(AttachmentInd == TotalAttachmentCount);
7070
}
7171

72+
VkRenderingFragmentShadingRateAttachmentInfoKHR& RenderingInfoWrapper::GetShadingRateAttachment()
73+
{
74+
if (!m_ShadingRateAttachment)
75+
{
76+
m_ShadingRateAttachment = std::make_unique<VkRenderingFragmentShadingRateAttachmentInfoKHR>();
77+
m_ShadingRateAttachment->sType = VK_STRUCTURE_TYPE_RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_INFO_KHR;
78+
79+
m_RI.pNext = m_ShadingRateAttachment.get();
80+
}
81+
82+
return *m_ShadingRateAttachment;
83+
}
84+
7285
} // namespace VulkanUtilities

0 commit comments

Comments
 (0)