Skip to content

Commit ada8a14

Browse files
author
Alexey Panteleev
committed
Split ResourceStates::ShaderResource into PixelShaderResource and NonPixelShaderResource to support DX12 compute queues that cannot use D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE state in barriers.
1 parent d2fd4ac commit ada8a14

7 files changed

Lines changed: 56 additions & 28 deletions

File tree

include/nvrhi/nvrhi.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace nvrhi
6464
{
6565
// Version of the public API provided by NVRHI.
6666
// Increment this when any changes to the API are made.
67-
static constexpr uint32_t c_HeaderVersion = 24;
67+
static constexpr uint32_t c_HeaderVersion = 25;
6868

6969
// Verifies that the version of the implementation matches the version of the header.
7070
// Returns true if they match. Use this when initializing apps using NVRHI as a shared library.
@@ -354,26 +354,28 @@ namespace nvrhi
354354
VertexBuffer = 0x00000004,
355355
IndexBuffer = 0x00000008,
356356
IndirectArgument = 0x00000010,
357-
ShaderResource = 0x00000020,
358-
UnorderedAccess = 0x00000040,
359-
RenderTarget = 0x00000080,
360-
DepthWrite = 0x00000100,
361-
DepthRead = 0x00000200,
362-
StreamOut = 0x00000400,
363-
CopyDest = 0x00000800,
364-
CopySource = 0x00001000,
365-
ResolveDest = 0x00002000,
366-
ResolveSource = 0x00004000,
367-
Present = 0x00008000,
368-
AccelStructRead = 0x00010000,
369-
AccelStructWrite = 0x00020000,
370-
AccelStructBuildInput = 0x00040000,
371-
AccelStructBuildBlas = 0x00080000,
372-
ShadingRateSurface = 0x00100000,
373-
OpacityMicromapWrite = 0x00200000,
374-
OpacityMicromapBuildInput = 0x00400000,
375-
ConvertCoopVecMatrixInput = 0x00800000,
376-
ConvertCoopVecMatrixOutput = 0x01000000,
357+
PixelShaderResource = 0x00000020,
358+
NonPixelShaderResource = 0x00000040,
359+
ShaderResource = PixelShaderResource | NonPixelShaderResource,
360+
UnorderedAccess = 0x00000080,
361+
RenderTarget = 0x00000100,
362+
DepthWrite = 0x00000200,
363+
DepthRead = 0x00000400,
364+
StreamOut = 0x00000800,
365+
CopyDest = 0x00001000,
366+
CopySource = 0x00002000,
367+
ResolveDest = 0x00004000,
368+
ResolveSource = 0x00008000,
369+
Present = 0x00010000,
370+
AccelStructRead = 0x00020000,
371+
AccelStructWrite = 0x00040000,
372+
AccelStructBuildInput = 0x00080000,
373+
AccelStructBuildBlas = 0x00100000,
374+
ShadingRateSurface = 0x00200000,
375+
OpacityMicromapWrite = 0x00400000,
376+
OpacityMicromapBuildInput = 0x00800000,
377+
ConvertCoopVecMatrixInput = 0x01000000,
378+
ConvertCoopVecMatrixOutput = 0x02000000,
377379
};
378380

379381
NVRHI_ENUM_CLASS_FLAG_OPERATORS(ResourceStates)

src/common/state-tracking.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,16 @@ namespace nvrhi
451451

452452
return tracking;
453453
}
454+
455+
ResourceStates getShaderResourceStateForBindingLayout(IBindingLayout* bindingLayout)
456+
{
457+
ShaderType const visibility = bindingLayout->getDesc()->visibility;
458+
459+
bool const hasPixelShader = (visibility & ShaderType::Pixel) != 0;
460+
461+
return hasPixelShader
462+
? ResourceStates::ShaderResource
463+
: ResourceStates::NonPixelShaderResource;
464+
}
465+
454466
} // namespace nvrhi

src/common/state-tracking.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,9 @@ namespace nvrhi
138138

139139
bool verifyPermanentResourceState(ResourceStates permanentState, ResourceStates requiredState, bool isTexture, const std::string& debugName, IMessageCallback* messageCallback);
140140

141+
// Derives the state for SRV type resources used in binding sets.
142+
// We don't want to include the PixelShaderResource bit everywhere because it cannot be used in a compute queue.
143+
// So, use the binding layout's visibility mask to determine if a pixel shader might be necessary.
144+
ResourceStates getShaderResourceStateForBindingLayout(IBindingLayout* bindingLayout);
145+
141146
} // namespace nvrhi

src/d3d12/d3d12-constants.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ namespace nvrhi::d3d12
251251
if ((stateBits & ResourceStates::VertexBuffer) != 0) result |= D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
252252
if ((stateBits & ResourceStates::IndexBuffer) != 0) result |= D3D12_RESOURCE_STATE_INDEX_BUFFER;
253253
if ((stateBits & ResourceStates::IndirectArgument) != 0) result |= D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
254-
if ((stateBits & ResourceStates::ShaderResource) != 0) result |= D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
254+
if ((stateBits & ResourceStates::PixelShaderResource) != 0) result |= D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
255+
if ((stateBits & ResourceStates::NonPixelShaderResource) != 0) result |= D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
255256
if ((stateBits & ResourceStates::UnorderedAccess) != 0) result |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
256257
if ((stateBits & ResourceStates::RenderTarget) != 0) result |= D3D12_RESOURCE_STATE_RENDER_TARGET;
257258
if ((stateBits & ResourceStates::DepthWrite) != 0) result |= D3D12_RESOURCE_STATE_DEPTH_WRITE;

src/d3d12/d3d12-state-tracking.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace nvrhi::d3d12
3333
return; // is bindless
3434

3535
BindingSet* bindingSet = checked_cast<BindingSet*>(_bindingSet);
36+
37+
ResourceStates const shaderResourceState = getShaderResourceStateForBindingLayout(bindingSet->layout);
3638

3739
for (auto bindingIndex : bindingSet->bindingsThatNeedTransitions)
3840
{
@@ -41,7 +43,7 @@ namespace nvrhi::d3d12
4143
switch (binding.type) // NOLINT(clang-diagnostic-switch-enum)
4244
{
4345
case ResourceType::Texture_SRV:
44-
requireTextureState(checked_cast<ITexture*>(binding.resourceHandle), binding.subresources, ResourceStates::ShaderResource);
46+
requireTextureState(checked_cast<ITexture*>(binding.resourceHandle), binding.subresources, shaderResourceState);
4547
break;
4648

4749
case ResourceType::Texture_UAV:
@@ -51,7 +53,7 @@ namespace nvrhi::d3d12
5153
case ResourceType::TypedBuffer_SRV:
5254
case ResourceType::StructuredBuffer_SRV:
5355
case ResourceType::RawBuffer_SRV:
54-
requireBufferState(checked_cast<IBuffer*>(binding.resourceHandle), ResourceStates::ShaderResource);
56+
requireBufferState(checked_cast<IBuffer*>(binding.resourceHandle), shaderResourceState);
5557
break;
5658

5759
case ResourceType::TypedBuffer_UAV:

src/vulkan/vulkan-constants.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ namespace nvrhi::vulkan
231231
vk::PipelineStageFlagBits2::eDrawIndirect,
232232
vk::AccessFlagBits2::eIndirectCommandRead,
233233
vk::ImageLayout::eUndefined },
234-
{ ResourceStates::ShaderResource,
234+
{ ResourceStates::PixelShaderResource,
235+
vk::PipelineStageFlagBits2::eFragmentShader,
236+
vk::AccessFlagBits2::eShaderRead,
237+
vk::ImageLayout::eShaderReadOnlyOptimal },
238+
{ ResourceStates::NonPixelShaderResource,
235239
vk::PipelineStageFlagBits2::eAllCommands,
236240
vk::AccessFlagBits2::eShaderRead,
237241
vk::ImageLayout::eShaderReadOnlyOptimal },

src/vulkan/vulkan-state-tracking.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace nvrhi::vulkan
3535

3636
BindingSet* bindingSet = checked_cast<BindingSet*>(_bindingSet);
3737

38+
ResourceStates const shaderResourceState = getShaderResourceStateForBindingLayout(bindingSet->layout);
39+
3840
for (auto bindingIndex : bindingSet->bindingsThatNeedTransitions)
3941
{
4042
const BindingSetItem& binding = bindingSet->desc.bindings[bindingIndex];
@@ -50,8 +52,8 @@ namespace nvrhi::vulkan
5052
auto* texture = checked_cast<Texture*>(binding.resourceHandle);
5153
const FormatInfo& fmtInfo = getFormatInfo(texture->desc.format);
5254
const ResourceStates srvState = (fmtInfo.hasDepth || fmtInfo.hasStencil)
53-
? (ResourceStates::ShaderResource | ResourceStates::DepthRead)
54-
: ResourceStates::ShaderResource;
55+
? (shaderResourceState | ResourceStates::DepthRead)
56+
: shaderResourceState;
5557
requireTextureState(texture, binding.subresources, srvState);
5658
break;
5759
}
@@ -63,7 +65,7 @@ namespace nvrhi::vulkan
6365
case ResourceType::TypedBuffer_SRV:
6466
case ResourceType::StructuredBuffer_SRV:
6567
case ResourceType::RawBuffer_SRV:
66-
requireBufferState(checked_cast<IBuffer*>(binding.resourceHandle), ResourceStates::ShaderResource);
68+
requireBufferState(checked_cast<IBuffer*>(binding.resourceHandle), shaderResourceState);
6769
break;
6870

6971
case ResourceType::TypedBuffer_UAV:

0 commit comments

Comments
 (0)